Fix tests to check for new exception type.
[shibboleth/cpp-opensaml.git] / samltest / saml1 / binding / SAML1POSTTest.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/saml1/core/Protocols.h>
20
21 using namespace opensaml::saml1p;
22 using namespace opensaml::saml1;
23
24 class SAML1POSTTest : 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 testSAML1POST() {
35         try {
36             QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
37             SecurityPolicy policy(m_rules1, m_metadata, &idprole, m_trust, false);
38
39             // Read message to use from file.
40             string path = data_path + "saml1/binding/SAML1Response.xml";
41             ifstream in(path.c_str());
42             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
43             XercesJanitor<DOMDocument> janitor(doc);
44             auto_ptr<Response> toSend(
45                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
46                 );
47             janitor.release();
48
49             // Freshen timestamp and ID.
50             toSend->setIssueInstant(time(NULL));
51             toSend->setResponseID(NULL);
52     
53             // Encode message.
54             auto_ptr_XMLCh lit1("MessageEncoder");
55             auto_ptr_XMLCh lit2("template");
56             path = data_path + "binding/template.html";
57             auto_ptr_XMLCh lit3(path.c_str());
58             DOMDocument* encoder_config = XMLToolingConfig::getConfig().getParser().newDocument();
59             XercesJanitor<DOMDocument> janitor2(encoder_config);
60             encoder_config->appendChild(encoder_config->createElementNS(NULL,lit1.get()));
61             encoder_config->getDocumentElement()->setAttributeNS(NULL,lit2.get(),lit3.get());
62             auto_ptr<MessageEncoder> encoder(
63                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
64                     samlconstants::SAML1_PROFILE_BROWSER_POST, encoder_config->getDocumentElement()
65                     )
66                 );
67             encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/SSO","https://sp.example.org/","state",m_creds);
68             toSend.release();
69             
70             // Decode message.
71             string relayState;
72             auto_ptr<MessageDecoder> decoder(
73                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML1_PROFILE_BROWSER_POST, NULL)
74                 );
75             Locker locker(m_metadata);
76             auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
77             
78             // Test the results.
79             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");
80             TSM_ASSERT("SAML Response not decoded successfully.", response.get());
81             TSM_ASSERT("Message was not verified.", policy.isSecure());
82             auto_ptr_char entityID(policy.getIssuer()->getName());
83             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
84             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
85
86             // Trigger a replay.
87             policy.reset();
88             TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), SecurityPolicyException);
89         }
90         catch (XMLToolingException& ex) {
91             TS_TRACE(ex.what());
92             throw;
93         }
94     }
95 };