f0f8d8134a2b3b211438c02e8bb6782eb90dafba
[shibboleth/cpp-opensaml.git] / samltest / saml2 / metadata / XMLMetadataProviderTest.h
1 /*
2  *  Copyright 2001-2009 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/binding/SAML2ArtifactType0004.h>
20 #include <saml/saml2/metadata/Metadata.h>
21 #include <saml/saml2/metadata/MetadataProvider.h>
22 #include <xmltooling/security/SecurityHelper.h>
23
24 using namespace opensaml::saml2md;
25 using namespace opensaml::saml2p;
26 using namespace opensaml;
27
28 class XMLMetadataProviderTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
29     XMLCh* entityID;
30     XMLCh* entityID2;
31     XMLCh* supportedProtocol;
32     XMLCh* supportedProtocol2;
33
34 public:
35     void setUp() {
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();
41     }
42     
43     void tearDown() {
44         XMLString::release(&entityID);
45         XMLString::release(&entityID2);
46         XMLString::release(&supportedProtocol);
47         XMLString::release(&supportedProtocol2);
48         SAMLObjectBaseTestCase::tearDown();
49     }
50
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);
56
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(NULL,path.get(),file.get());
61
62         auto_ptr<MetadataProvider> metadataProvider(
63             SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
64             );
65         try {
66             metadataProvider->init();
67         }
68         catch (XMLToolingException& ex) {
69             TS_TRACE(ex.what());
70             throw;
71         }
72         
73         Locker locker(metadataProvider.get());
74         const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(entityID,NULL,NULL,false)).first;
75         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
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))!=NULL);
79         TSM_ASSERT("Role lookup failed", find_if(descriptor->getIDPSSODescriptors(), isValidForProtocol(supportedProtocol2))!=NULL);
80
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
85                 )
86             );
87         descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(artifact.get(),NULL,NULL,false)).first;
88         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
89         assertEquals("Entity's ID does not match requested ID", entityID, descriptor->getEntityID());
90     }
91
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);
97
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(NULL,path.get(),file.get());
102
103         auto_ptr<MetadataProvider> metadataProvider(
104             SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
105             );
106         try {
107             metadataProvider->init();
108         }
109         catch (XMLToolingException& ex) {
110             TS_TRACE(ex.what());
111             throw;
112         }
113
114         Locker locker(metadataProvider.get());
115         const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(entityID,NULL,NULL,false)).first;
116         TSM_ASSERT("Retrieved entity descriptor was not null", descriptor==NULL);
117         descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(entityID2,NULL,NULL,false)).first;
118         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
119         assertEquals("Entity's ID does not match requested ID", entityID2, descriptor->getEntityID());
120     }
121
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);
127
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(NULL,path.get(),file.get());
132
133         auto_ptr<MetadataProvider> metadataProvider(
134             SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
135             );
136         try {
137             metadataProvider->init();
138         }
139         catch (XMLToolingException& ex) {
140             TS_TRACE(ex.what());
141             throw;
142         }
143
144         Locker locker(metadataProvider.get());
145         const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(entityID2,NULL,NULL,false)).first;
146         TSM_ASSERT("Retrieved entity descriptor was not null", descriptor==NULL);
147         descriptor = metadataProvider->getEntityDescriptor(MetadataProvider::Criteria(entityID,NULL,NULL,false)).first;
148         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
149         assertEquals("Entity's ID does not match requested ID", entityID, descriptor->getEntityID());
150     }
151 };