SAML TrustEngine wrappers, ExplicitKeyTrustEngine plugin.
[shibboleth/cpp-opensaml.git] / samltest / security / ExplicitKeyTrustEngineTest.h
1 /*\r
2  *  Copyright 2001-2006 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/security/TrustEngine.h>\r
19 #include <saml/saml2/metadata/MetadataProvider.h>\r
20 \r
21 using namespace opensaml::saml2;\r
22 using namespace opensaml::saml2md;\r
23 using namespace xmlsignature;\r
24 \r
25 class ExplicitKeyTrustEngineTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {\r
26 public:\r
27     void setUp() {\r
28         SAMLObjectBaseTestCase::setUp();\r
29     }\r
30     \r
31     void tearDown() {\r
32         SAMLObjectBaseTestCase::tearDown();\r
33     }\r
34 \r
35     void testExplicitKeyTrustEngine() {\r
36         string config = data_path + "security/FilesystemMetadataProvider.xml";\r
37         ifstream in(config.c_str());\r
38         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);\r
39         XercesJanitor<DOMDocument> janitor(doc);\r
40 \r
41         auto_ptr_XMLCh path("path");\r
42         string s = data_path + "security/example-metadata.xml";\r
43         auto_ptr_XMLCh file(s.c_str());\r
44         doc->getDocumentElement()->setAttributeNS(NULL,path.get(),file.get());\r
45 \r
46         // Build metadata provider.\r
47         auto_ptr<MetadataProvider> metadataProvider(\r
48             SAMLConfig::getConfig().MetadataProviderManager.newPlugin(FILESYSTEM_METADATA_PROVIDER,doc->getDocumentElement())\r
49             );\r
50         try {\r
51             metadataProvider->init();\r
52         }\r
53         catch (XMLToolingException& ex) {\r
54             TS_TRACE(ex.what());\r
55             throw;\r
56         }\r
57         \r
58         // Build trust engine.\r
59         auto_ptr<opensaml::TrustEngine> trustEngine(\r
60             SAMLConfig::getConfig().TrustEngineManager.newPlugin(EXPLICIT_KEY_SAMLTRUSTENGINE, NULL)\r
61             );\r
62         \r
63         // Get signed assertion.\r
64         config = data_path + "signature/SAML2Assertion.xml";\r
65         ifstream in2(config.c_str());\r
66         DOMDocument* doc2=XMLToolingConfig::getConfig().getParser().parse(in2);\r
67         XercesJanitor<DOMDocument> janitor2(doc2);\r
68         auto_ptr<Assertion> assertion(dynamic_cast<Assertion*>(XMLObjectBuilder::getBuilder(doc2->getDocumentElement())->buildFromDocument(doc2)));\r
69         janitor2.release();\r
70 \r
71         Locker locker(metadataProvider.get());\r
72         const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor("https://idp.example.org");\r
73         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);\r
74         \r
75         Signature* sig=assertion->getSignature();\r
76         TSM_ASSERT("Signature not present", sig!=NULL);\r
77 \r
78         RoleDescriptor* role=descriptor->getIDPSSODescriptors().front();\r
79         TSM_ASSERT("Role not present", role!=NULL);\r
80         \r
81         TSM_ASSERT("Signature failed to validate.", trustEngine->validate(*sig, *role, metadataProvider->getKeyResolver()));\r
82     }\r
83 };\r