Convert from NULL macro to nullptr, remove unused zlib code.
[shibboleth/cpp-opensaml.git] / samltest / security / ExplicitKeyTrustEngineTest.h
1 /*
2  *  Copyright 2001-2010 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/MetadataCredentialCriteria.h>
22 #include <saml/saml2/metadata/MetadataProvider.h>
23 #include <xmltooling/security/SignatureTrustEngine.h>
24
25 using namespace opensaml::saml2;
26 using namespace opensaml::saml2md;
27 using namespace xmlsignature;
28
29 class ExplicitKeyTrustEngineTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
30 public:
31     void setUp() {
32         SAMLObjectBaseTestCase::setUp();
33     }
34     
35     void tearDown() {
36         SAMLObjectBaseTestCase::tearDown();
37     }
38
39     void testExplicitKeyTrustEngine() {
40         string config = data_path + "security/XMLMetadataProvider.xml";
41         ifstream in(config.c_str());
42         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
43         XercesJanitor<DOMDocument> janitor(doc);
44
45         auto_ptr_XMLCh path("path");
46         string s = data_path + "security/example-metadata.xml";
47         auto_ptr_XMLCh file(s.c_str());
48         doc->getDocumentElement()->setAttributeNS(nullptr,path.get(),file.get());
49
50         // Build metadata provider.
51         auto_ptr<MetadataProvider> metadataProvider(
52             opensaml::SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
53             );
54         try {
55             metadataProvider->init();
56         }
57         catch (XMLToolingException& ex) {
58             TS_TRACE(ex.what());
59             throw;
60         }
61         
62         // Build trust engine.
63         auto_ptr<TrustEngine> trustEngine(
64             XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(EXPLICIT_KEY_TRUSTENGINE, nullptr)
65             );
66         
67         // Get signed assertion.
68         config = data_path + "signature/SAML2Assertion.xml";
69         ifstream in2(config.c_str());
70         DOMDocument* doc2=XMLToolingConfig::getConfig().getParser().parse(in2);
71         XercesJanitor<DOMDocument> janitor2(doc2);
72         auto_ptr<Assertion> assertion(dynamic_cast<Assertion*>(XMLObjectBuilder::getBuilder(doc2->getDocumentElement())->buildFromDocument(doc2)));
73         janitor2.release();
74
75         Locker locker(metadataProvider.get());
76         const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria("https://idp.example.org")).first;
77         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=nullptr);
78         
79         RoleDescriptor* role=descriptor->getIDPSSODescriptors().front();
80         TSM_ASSERT("Role not present", role!=nullptr);
81         
82         Signature* sig=assertion->getSignature();
83         TSM_ASSERT("Signature not present", sig!=nullptr);
84
85         MetadataCredentialCriteria cc(*role);
86         cc.setPeerName("https://idp.example.org");
87         TSM_ASSERT("Signature failed to validate.", dynamic_cast<SignatureTrustEngine*>(trustEngine.get())->validate(*sig, *metadataProvider, &cc));
88
89         descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria("https://idp2.example.org")).first;
90         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=nullptr);
91         
92         role=descriptor->getIDPSSODescriptors().front();
93         TSM_ASSERT("Role not present", role!=nullptr);
94
95         MetadataCredentialCriteria cc2(*role);
96         cc2.setPeerName("https://idp2.example.org");
97         TSM_ASSERT("Signature validated.", !dynamic_cast<SignatureTrustEngine*>(trustEngine.get())->validate(*sig, *metadataProvider, &cc2));
98     }
99 };