Update copyright.
[shibboleth/cpp-opensaml.git] / samltest / saml2 / metadata / XMLMetadataProviderTest.h
1 /*
2  *  Copyright 2001-2007 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
23 using namespace opensaml::saml2md;
24 using namespace opensaml::saml2p;
25
26 class XMLMetadataProviderTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
27     XMLCh* entityID;
28     XMLCh* entityID2;
29     XMLCh* supportedProtocol;
30     XMLCh* supportedProtocol2;
31
32 public:
33     void setUp() {
34         entityID=XMLString::transcode("urn:mace:incommon:washington.edu");
35         entityID2=XMLString::transcode("urn:mace:incommon:rochester.edu");
36         supportedProtocol=XMLString::transcode("urn:oasis:names:tc:SAML:1.1:protocol");
37         supportedProtocol2=XMLString::transcode("urn:mace:shibboleth:1.0");
38         SAMLObjectBaseTestCase::setUp();
39     }
40     
41     void tearDown() {
42         XMLString::release(&entityID);
43         XMLString::release(&entityID2);
44         XMLString::release(&supportedProtocol);
45         XMLString::release(&supportedProtocol2);
46         SAMLObjectBaseTestCase::tearDown();
47     }
48
49     void testXMLProvider() {
50         string config = data_path + "saml2/metadata/XMLMetadataProvider.xml";
51         ifstream in(config.c_str());
52         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
53         XercesJanitor<DOMDocument> janitor(doc);
54
55         auto_ptr_XMLCh path("path");
56         string s = data_path + "saml2/metadata/InCommon-metadata.xml";
57         auto_ptr_XMLCh file(s.c_str());
58         doc->getDocumentElement()->setAttributeNS(NULL,path.get(),file.get());
59
60         auto_ptr<MetadataProvider> metadataProvider(
61             SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
62             );
63         try {
64             metadataProvider->init();
65         }
66         catch (XMLToolingException& ex) {
67             TS_TRACE(ex.what());
68             throw;
69         }
70         
71         Locker locker(metadataProvider.get());
72         const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor(entityID);
73         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
74         assertEquals("Entity's ID does not match requested ID", entityID, descriptor->getEntityID());
75         TSM_ASSERT_EQUALS("Unexpected number of roles", 1, descriptor->getIDPSSODescriptors().size());
76         TSM_ASSERT("Role lookup failed", descriptor->getIDPSSODescriptor(supportedProtocol)!=NULL);
77         TSM_ASSERT("Role lookup failed", descriptor->getIDPSSODescriptor(supportedProtocol2)!=NULL);
78
79         auto_ptr<SAML2ArtifactType0004> artifact(
80             new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1("urn:mace:incommon:washington.edu"),1)
81             );
82         descriptor = metadataProvider->getEntityDescriptor(artifact.get());
83         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
84         assertEquals("Entity's ID does not match requested ID", entityID, descriptor->getEntityID());
85     }
86
87     void testXMLWithBlacklists() {
88         string config = data_path + "saml2/metadata/XMLWithBlacklists.xml";
89         ifstream in(config.c_str());
90         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
91         XercesJanitor<DOMDocument> janitor(doc);
92
93         auto_ptr_XMLCh path("path");
94         string s = data_path + "saml2/metadata/InCommon-metadata.xml";
95         auto_ptr_XMLCh file(s.c_str());
96         doc->getDocumentElement()->setAttributeNS(NULL,path.get(),file.get());
97
98         auto_ptr<MetadataProvider> metadataProvider(
99             SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
100             );
101         try {
102             metadataProvider->init();
103         }
104         catch (XMLToolingException& ex) {
105             TS_TRACE(ex.what());
106             throw;
107         }
108
109         Locker locker(metadataProvider.get());
110         const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor(entityID);
111         TSM_ASSERT("Retrieved entity descriptor was not null", descriptor==NULL);
112         descriptor = metadataProvider->getEntityDescriptor(entityID2);
113         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
114         assertEquals("Entity's ID does not match requested ID", entityID2, descriptor->getEntityID());
115     }
116
117     void testXMLWithWhitelists() {
118         string config = data_path + "saml2/metadata/XMLWithWhitelists.xml";
119         ifstream in(config.c_str());
120         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
121         XercesJanitor<DOMDocument> janitor(doc);
122
123         auto_ptr_XMLCh path("path");
124         string s = data_path + "saml2/metadata/InCommon-metadata.xml";
125         auto_ptr_XMLCh file(s.c_str());
126         doc->getDocumentElement()->setAttributeNS(NULL,path.get(),file.get());
127
128         auto_ptr<MetadataProvider> metadataProvider(
129             SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
130             );
131         try {
132             metadataProvider->init();
133         }
134         catch (XMLToolingException& ex) {
135             TS_TRACE(ex.what());
136             throw;
137         }
138
139         Locker locker(metadataProvider.get());
140         const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor(entityID2);
141         TSM_ASSERT("Retrieved entity descriptor was not null", descriptor==NULL);
142         descriptor = metadataProvider->getEntityDescriptor(entityID);
143         TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
144         assertEquals("Entity's ID does not match requested ID", entityID, descriptor->getEntityID());
145     }
146 };