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