2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <saml/SAMLConfig.h>
19 #include <saml/saml2/binding/SAML2ArtifactType0004.h>
20 #include <saml/saml2/metadata/Metadata.h>
21 #include <saml/saml2/metadata/MetadataProvider.h>
22 #include <xmltooling/security/SecurityHelper.h>
24 using namespace opensaml::saml2md;
25 using namespace opensaml::saml2p;
26 using namespace opensaml;
28 class XMLMetadataProviderTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
31 XMLCh* supportedProtocol;
32 XMLCh* supportedProtocol2;
36 entityID=XMLString::transcode("urn:mace:incommon:washington.edu");
37 entityID2=XMLString::transcode("urn:mace:incommon:rochester.edu");
38 supportedProtocol=XMLString::transcode("urn:oasis:names:tc:SAML:1.1:protocol");
39 supportedProtocol2=XMLString::transcode("urn:mace:shibboleth:1.0");
40 SAMLObjectBaseTestCase::setUp();
44 XMLString::release(&entityID);
45 XMLString::release(&entityID2);
46 XMLString::release(&supportedProtocol);
47 XMLString::release(&supportedProtocol2);
48 SAMLObjectBaseTestCase::tearDown();
51 void testXMLProvider() {
52 string config = data_path + "saml2/metadata/XMLMetadataProvider.xml";
53 ifstream in(config.c_str());
54 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
55 XercesJanitor<DOMDocument> janitor(doc);
57 auto_ptr_XMLCh path("path");
58 string s = data_path + "saml2/metadata/InCommon-metadata.xml";
59 auto_ptr_XMLCh file(s.c_str());
60 doc->getDocumentElement()->setAttributeNS(nullptr,path.get(),file.get());
62 auto_ptr<MetadataProvider> metadataProvider(
63 SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
66 metadataProvider->init();
68 catch (XMLToolingException& ex) {
73 Locker locker(metadataProvider.get());
74 const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(entityID,nullptr,nullptr,false)).first;
75 TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=nullptr);
76 assertEquals("Entity's ID does not match requested ID", entityID, descriptor->getEntityID());
77 TSM_ASSERT_EQUALS("Unexpected number of roles", 1, descriptor->getIDPSSODescriptors().size());
78 TSM_ASSERT("Role lookup failed", find_if(descriptor->getIDPSSODescriptors(), isValidForProtocol(supportedProtocol))!=nullptr);
79 TSM_ASSERT("Role lookup failed", find_if(descriptor->getIDPSSODescriptors(), isValidForProtocol(supportedProtocol2))!=nullptr);
81 static const char* providerIdStr = "urn:mace:incommon:washington.edu";
82 auto_ptr<SAML2ArtifactType0004> artifact(
83 new SAML2ArtifactType0004(
84 SecurityHelper::doHash("SHA1", providerIdStr, strlen(providerIdStr), false), 1
87 descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(artifact.get(),nullptr,nullptr,false)).first;
88 TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=nullptr);
89 assertEquals("Entity's ID does not match requested ID", entityID, descriptor->getEntityID());
92 void testXMLWithBlacklists() {
93 string config = data_path + "saml2/metadata/XMLWithBlacklists.xml";
94 ifstream in(config.c_str());
95 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
96 XercesJanitor<DOMDocument> janitor(doc);
98 auto_ptr_XMLCh path("path");
99 string s = data_path + "saml2/metadata/InCommon-metadata.xml";
100 auto_ptr_XMLCh file(s.c_str());
101 doc->getDocumentElement()->setAttributeNS(nullptr,path.get(),file.get());
103 auto_ptr<MetadataProvider> metadataProvider(
104 SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
107 metadataProvider->init();
109 catch (XMLToolingException& ex) {
114 Locker locker(metadataProvider.get());
115 const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(entityID,nullptr,nullptr,false)).first;
116 TSM_ASSERT("Retrieved entity descriptor was not null", descriptor==nullptr);
117 descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(entityID2,nullptr,nullptr,false)).first;
118 TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=nullptr);
119 assertEquals("Entity's ID does not match requested ID", entityID2, descriptor->getEntityID());
122 void testXMLWithWhitelists() {
123 string config = data_path + "saml2/metadata/XMLWithWhitelists.xml";
124 ifstream in(config.c_str());
125 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
126 XercesJanitor<DOMDocument> janitor(doc);
128 auto_ptr_XMLCh path("path");
129 string s = data_path + "saml2/metadata/InCommon-metadata.xml";
130 auto_ptr_XMLCh file(s.c_str());
131 doc->getDocumentElement()->setAttributeNS(nullptr,path.get(),file.get());
133 auto_ptr<MetadataProvider> metadataProvider(
134 SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
137 metadataProvider->init();
139 catch (XMLToolingException& ex) {
144 Locker locker(metadataProvider.get());
145 const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(entityID2,nullptr,nullptr,false)).first;
146 TSM_ASSERT("Retrieved entity descriptor was not null", descriptor==nullptr);
147 descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(entityID,nullptr,nullptr,false)).first;
148 TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=nullptr);
149 assertEquals("Entity's ID does not match requested ID", entityID, descriptor->getEntityID());