Refactor some tests.
[shibboleth/cpp-opensaml.git] / samltest / signature / SAMLSignatureTestBase.h
1 /*\r
2  *  Copyright 2001-2005 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 #include "internal.h"\r
18 #include <saml/signature/SignatureProfileValidator.h>\r
19 #include <xmltooling/signature/SignatureValidator.h>\r
20 \r
21 \r
22 #include <openssl/pem.h>\r
23 #include <xsec/enc/XSECKeyInfoResolverDefault.hpp>\r
24 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>\r
25 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>\r
26 #include <xsec/enc/XSECCryptoException.hpp>\r
27 #include <xsec/framework/XSECException.hpp>\r
28 \r
29 using namespace xmlsignature;\r
30 \r
31 class _addcert : public std::binary_function<X509Data*,XSECCryptoX509*,void> {\r
32 public:\r
33     void operator()(X509Data* bag, XSECCryptoX509* cert) const {\r
34         safeBuffer& buf=cert->getDEREncodingSB();\r
35         X509Certificate* x=X509CertificateBuilder::buildX509Certificate();\r
36         x->setValue(buf.sbStrToXMLCh());\r
37         bag->getX509Certificates().push_back(x);\r
38     }\r
39 };\r
40 \r
41 class SAMLSignatureTestBase : public SAMLObjectBaseTestCase {\r
42 protected:\r
43     XSECCryptoKey* m_key;\r
44     vector<XSECCryptoX509*> m_certs;\r
45 public:\r
46     void setUp() {\r
47         SAMLObjectBaseTestCase::setUp();\r
48         string keypath=data_path + "key.pem";\r
49         BIO* in=BIO_new(BIO_s_file_internal());\r
50         if (in && BIO_read_filename(in,keypath.c_str())>0) {\r
51             EVP_PKEY* pkey=PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);\r
52             if (pkey) {\r
53                 m_key=new OpenSSLCryptoKeyRSA(pkey);\r
54                 EVP_PKEY_free(pkey);\r
55             }\r
56         }\r
57         if (in) BIO_free(in);\r
58         TS_ASSERT(m_key!=NULL);\r
59 \r
60         string certpath=data_path + "cert.pem";\r
61         in=BIO_new(BIO_s_file_internal());\r
62         if (in && BIO_read_filename(in,certpath.c_str())>0) {\r
63             X509* x=NULL;\r
64             while (x=PEM_read_bio_X509(in,NULL,NULL,NULL)) {\r
65                 m_certs.push_back(new OpenSSLCryptoX509(x));\r
66                 X509_free(x);\r
67             }\r
68         }\r
69         if (in) BIO_free(in);\r
70         TS_ASSERT(m_certs.size()>0);\r
71     }\r
72 \r
73     void tearDown() {\r
74         SAMLObjectBaseTestCase::tearDown();\r
75         delete m_key;\r
76         for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup<XSECCryptoX509>());\r
77     }\r
78 };\r