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 {
27 SAMLBindingBaseTestCase::setUp();
31 SAMLBindingBaseTestCase::tearDown();
34 void testSAML2POSTTrusted() {
36 // Read message to use from file.
37 string path = data_path + "saml2/binding/SAML2Response.xml";
38 ifstream in(path.c_str());
39 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
40 XercesJanitor<DOMDocument> janitor(doc);
41 auto_ptr<Response> toSend(
42 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
47 toSend->setIssueInstant(time(NULL));
50 auto_ptr_XMLCh lit1("MessageEncoder");
51 auto_ptr_XMLCh lit2("template");
52 path = data_path + "binding/template.html";
53 auto_ptr_XMLCh lit3(path.c_str());
54 DOMDocument* encoder_config = XMLToolingConfig::getConfig().getParser().newDocument();
55 XercesJanitor<DOMDocument> janitor2(encoder_config);
56 encoder_config->appendChild(encoder_config->createElementNS(NULL,lit1.get()));
57 encoder_config->getDocumentElement()->setAttributeNS(NULL,lit2.get(),lit3.get());
58 auto_ptr<MessageEncoder> encoder(
59 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
60 samlconstants::SAML20_BINDING_HTTP_POST, encoder_config->getDocumentElement()
63 encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/POST","https://sp.example.org/","state",m_creds);
68 const RoleDescriptor* issuer=NULL;
69 const XMLCh* securityMech=NULL;
70 QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
71 auto_ptr<MessageDecoder> decoder(
72 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_POST, NULL)
74 Locker locker(m_metadata);
75 auto_ptr<Response> response(
76 dynamic_cast<Response*>(
77 decoder->decode(relayState,issuer,securityMech,*this,m_metadata,&idprole,m_trust)
82 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
83 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
84 TSM_ASSERT("Message was not verified.", issuer && securityMech && securityMech==samlconstants::SAML20P_NS);
85 auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
86 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
87 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
89 catch (XMLToolingException& ex) {
95 void testSAML2POSTUntrusted() {
97 // Read message to use from file.
98 string path = data_path + "saml2/binding/SAML2Response.xml";
99 ifstream in(path.c_str());
100 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
101 XercesJanitor<DOMDocument> janitor(doc);
102 auto_ptr<Response> toSend(
103 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
107 // Freshen timestamp and clear ID.
108 toSend->setIssueInstant(time(NULL));
112 auto_ptr_XMLCh lit1("MessageEncoder");
113 auto_ptr_XMLCh lit2("template");
114 path = data_path + "binding/template.html";
115 auto_ptr_XMLCh lit3(path.c_str());
116 DOMDocument* encoder_config = XMLToolingConfig::getConfig().getParser().newDocument();
117 XercesJanitor<DOMDocument> janitor2(encoder_config);
118 encoder_config->appendChild(encoder_config->createElementNS(NULL,lit1.get()));
119 encoder_config->getDocumentElement()->setAttributeNS(NULL,lit2.get(),lit3.get());
120 auto_ptr<MessageEncoder> encoder(
121 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
122 samlconstants::SAML20_BINDING_HTTP_POST, encoder_config->getDocumentElement()
125 encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/POST","https://sp.example.org/","state");
130 const RoleDescriptor* issuer=NULL;
131 const XMLCh* securityMech=NULL;
132 QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
133 auto_ptr<MessageDecoder> decoder(
134 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_POST, NULL)
136 Locker locker(m_metadata);
137 auto_ptr<Response> response(
138 dynamic_cast<Response*>(
139 decoder->decode(relayState,issuer,securityMech,*this,m_metadata,&idprole)
144 TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
145 TSM_ASSERT("SAML Response not decoded successfully.", response.get());
146 TSM_ASSERT("Message was verified.", issuer && !securityMech);
147 auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
148 TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
149 TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
152 TSM_ASSERT_THROWS("Did not catch the replay.",
153 decoder->decode(relayState,issuer,securityMech,*this,m_metadata,&idprole,m_trust),
156 catch (XMLToolingException& ex) {