345e26c9ef14f6862f87ad1b107f6288deb8bda5
[shibboleth/cpp-opensaml.git] / samltest / saml2 / binding / SAML2RedirectTest.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 "binding.h"
18
19 #include <saml/saml2/core/Protocols.h>
20
21 using namespace opensaml::saml2p;
22 using namespace opensaml::saml2;
23
24 class SAML2RedirectTest : public CxxTest::TestSuite, public SAMLBindingBaseTestCase {
25 public:
26     void setUp() {
27         SAMLBindingBaseTestCase::setUp();
28     }
29
30     void tearDown() {
31         SAMLBindingBaseTestCase::tearDown();
32     }
33
34     void testSAML2Redirect() {
35         try {
36             xmltooling::QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
37             SecurityPolicy policy(m_metadata, &idprole, m_trust, false);
38             policy.getRules().assign(m_rules.begin(), m_rules.end());
39
40             // Read message to use from file.
41             string path = data_path + "saml2/binding/SAML2Response.xml";
42             ifstream in(path.c_str());
43             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
44             XercesJanitor<DOMDocument> janitor(doc);
45             auto_ptr<Response> toSend(
46                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
47                 );
48             janitor.release();
49
50             CredentialCriteria cc;
51             cc.setUsage(Credential::SIGNING_CREDENTIAL);
52             Locker clocker(m_creds);
53             const Credential* cred = m_creds->resolve(&cc);
54             TSM_ASSERT("Retrieved credential was null", cred!=NULL);
55
56             // Freshen timestamp and ID.
57             toSend->setIssueInstant(time(NULL));
58             toSend->setID(NULL);
59     
60             // Encode message.
61             auto_ptr<MessageEncoder> encoder(
62                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
63                     samlconstants::SAML20_BINDING_HTTP_REDIRECT, pair<const DOMElement*,const XMLCh*>(NULL,NULL)
64                     )
65                 );
66             Locker locker(m_metadata);
67             encoder->encode(
68                 *this,
69                 toSend.get(),
70                 "https://sp.example.org/SAML/SSO",
71                 m_metadata->getEntityDescriptor(MetadataProvider::Criteria("https://sp.example.org/")).first,
72                 "state",
73                 NULL,
74                 cred
75                 );
76             toSend.release();
77             
78             // Decode message.
79             string relayState;
80             auto_ptr<MessageDecoder> decoder(
81                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(
82                     samlconstants::SAML20_BINDING_HTTP_REDIRECT, pair<const DOMElement*,const XMLCh*>(NULL,NULL)
83                     )
84                 );
85             auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
86             
87             // Test the results.
88             TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
89             TSM_ASSERT("SAML Response not decoded successfully.", response.get());
90             TSM_ASSERT("Message was not verified.", policy.isAuthenticated());
91             auto_ptr_char entityID(policy.getIssuer()->getName());
92             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
93             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
94
95             // Trigger a replay.
96             policy.reset();
97             TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), SecurityPolicyException);
98         }
99         catch (XMLToolingException& ex) {
100             TS_TRACE(ex.what());
101             throw;
102         }
103     }
104 };