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