Merged trust engines back into a unified version, made metadata roles a "KeyInfoSource".
[shibboleth/cpp-opensaml.git] / samltest / security / ExplicitKeyTrustEngineTest.h
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "internal.h"
18 #include <saml/SAMLConfig.h>
19 #include <saml/saml2/core/Assertions.h>
20 #include <saml/saml2/metadata/Metadata.h>
21 #include <saml/saml2/metadata/MetadataProvider.h>
22 #include <xmltooling/security/TrustEngine.h>
23
24 using namespace opensaml::saml2;
25 using namespace opensaml::saml2md;
26 using namespace xmlsignature;
27
28 class ExplicitKeyTrustEngineTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
29 public:
30     void setUp() {
31         SAMLObjectBaseTestCase::setUp();
32     }
33     
34     void tearDown() {
35         SAMLObjectBaseTestCase::tearDown();
36     }
37
38     void testExplicitKeyTrustEngine() {
39         string config = data_path + "security/FilesystemMetadataProvider.xml";
40         ifstream in(config.c_str());
41         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
42         XercesJanitor<DOMDocument> janitor(doc);
43
44         auto_ptr_XMLCh path("path");
45         string s = data_path + "security/example-metadata.xml";
46         auto_ptr_XMLCh file(s.c_str());
47         doc->getDocumentElement()->setAttributeNS(NULL,path.get(),file.get());
48
49         // Build metadata provider.
50         auto_ptr<MetadataProvider> metadataProvider(
51             SAMLConfig::getConfig().MetadataProviderManager.newPlugin(FILESYSTEM_METADATA_PROVIDER,doc->getDocumentElement())
52             );
53         try {
54             metadataProvider->init();
55         }
56         catch (XMLToolingException& ex) {
57             TS_TRACE(ex.what());
58             throw;
59         }
60         
61         // Build trust engine.
62         auto_ptr<TrustEngine> trustEngine(
63             XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(EXPLICIT_KEY_TRUSTENGINE, NULL)
64             );
65         
66         // Get signed assertion.
67         config = data_path + "signature/SAML2Assertion.xml";
68         ifstream in2(config.c_str());
69         DOMDocument* doc2=XMLToolingConfig::getConfig().getParser().parse(in2);
70         XercesJanitor<DOMDocument> janitor2(doc2);
71         auto_ptr<Assertion> assertion(dynamic_cast<Assertion*>(XMLObjectBuilder::getBuilder(doc2->getDocumentElement())->buildFromDocument(doc2)));
72         janitor2.release();
73
74         Locker locker(metadataProvider.get());
75         const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor("https://idp.example.org");
76         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
77         
78         RoleDescriptor* role=descriptor->getIDPSSODescriptors().front();
79         TSM_ASSERT("Role not present", role!=NULL);
80         
81         Signature* sig=assertion->getSignature();
82         TSM_ASSERT("Signature not present", sig!=NULL);
83         TSM_ASSERT("Signature failed to validate.", trustEngine->validate(*sig, *role, metadataProvider->getKeyResolver()));
84
85         descriptor = metadataProvider->getEntityDescriptor("https://idp2.example.org");
86         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
87         
88         role=descriptor->getIDPSSODescriptors().front();
89         TSM_ASSERT("Role not present", role!=NULL);
90
91         TSM_ASSERT("Signature validated.", !trustEngine->validate(*sig, *role, metadataProvider->getKeyResolver()));
92     }
93 };