2 * Copyright 2001-2005 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.
19 #include <saml/saml2/core/Protocols.h>
21 using namespace opensaml::saml2p;
22 using namespace opensaml::saml2;
24 class SAML2POSTTest : public CxxTest::TestSuite, public SAMLBindingBaseTestCase {
28 SAMLBindingBaseTestCase::setUp();
33 SAMLBindingBaseTestCase::tearDown();
36 void testSAML2POSTTrusted() {
38 // Read message to use from file.
39 string path = data_path + "saml2/binding/SAML2Response.xml";
40 ifstream in(path.c_str());
41 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
42 XercesJanitor<DOMDocument> janitor(doc);
43 auto_ptr<Response> toSend(
44 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
49 toSend->setIssueInstant(time(NULL));
52 auto_ptr<MessageEncoder> encoder(
53 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAMLConstants::SAML20_BINDING_HTTP_POST, NULL)
55 encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);
60 const RoleDescriptor* issuer=NULL;
62 QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
63 auto_ptr<MessageDecoder> decoder(
64 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAMLConstants::SAML20_BINDING_HTTP_POST, NULL)
66 Locker locker(m_metadata);
67 auto_ptr<Response> response(
68 dynamic_cast<Response*>(
69 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)
74 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
75 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
76 TSM_ASSERT("Message was not verified.", issuer && trusted);
77 auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
78 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
79 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
81 catch (XMLToolingException& ex) {
87 void testSAML2POSTUntrusted() {
89 // Read message to use from file.
90 string path = data_path + "saml2/binding/SAML2Response.xml";
91 ifstream in(path.c_str());
92 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
93 XercesJanitor<DOMDocument> janitor(doc);
94 auto_ptr<Response> toSend(
95 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
99 // Freshen timestamp and clear ID.
100 toSend->setIssueInstant(time(NULL));
104 auto_ptr<MessageEncoder> encoder(
105 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAMLConstants::SAML20_BINDING_HTTP_POST, NULL)
107 encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state");
112 const RoleDescriptor* issuer=NULL;
114 QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
115 auto_ptr<MessageDecoder> decoder(
116 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAMLConstants::SAML20_BINDING_HTTP_POST, NULL)
118 Locker locker(m_metadata);
119 auto_ptr<Response> response(
120 dynamic_cast<Response*>(
121 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole)
126 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
127 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
128 TSM_ASSERT("Message was verified.", issuer && !trusted);
129 auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
130 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
131 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
134 TSM_ASSERT_THROWS("Did not catch the replay.",
135 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),
138 catch (XMLToolingException& ex) {
144 const char* getMethod() const {
148 const char* getRequestURL() const {
149 return "https://sp.example.org/SAML/POST";
152 const char* getQueryString() const {