Fix linefeeds
[shibboleth/cpp-opensaml.git] / samltest / saml2 / binding / SAML2POSTTest.h
1 /*
2  *  Copyright 2001-2005 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 SAML2POSTTest : public CxxTest::TestSuite, public SAMLBindingBaseTestCase {
25 public:
26     void setUp() {
27         m_fields.clear();
28         SAMLBindingBaseTestCase::setUp();
29     }
30
31     void tearDown() {
32         m_fields.clear();
33         SAMLBindingBaseTestCase::tearDown();
34     }
35
36     void testSAML2POSTTrusted() {
37         try {
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))
45                 );
46             janitor.release();
47
48             // Freshen timestamp.
49             toSend->setIssueInstant(time(NULL));
50     
51             // Encode message.
52             auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML2_POST_ENCODER, NULL));
53             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);
54             toSend.release();
55             
56             // Decode message.
57             string relayState;
58             const RoleDescriptor* issuer=NULL;
59             bool trusted=false;
60             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
61             auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML2_POST_DECODER, NULL));
62             Locker locker(m_metadata);
63             auto_ptr<Response> response(
64                 dynamic_cast<Response*>(
65                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)
66                     )
67                 );
68             
69             // Test the results.
70             TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
71             TSM_ASSERT("SAML Response not decoded successfully.", response.get());
72             TSM_ASSERT("Message was not verified.", issuer && trusted);
73             auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
74             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
75             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
76         }
77         catch (XMLToolingException& ex) {
78             TS_TRACE(ex.what());
79             throw;
80         }
81     }
82
83     void testSAML2POSTUntrusted() {
84         try {
85             // Read message to use from file.
86             string path = data_path + "saml2/binding/SAML2Response.xml";
87             ifstream in(path.c_str());
88             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
89             XercesJanitor<DOMDocument> janitor(doc);
90             auto_ptr<Response> toSend(
91                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
92                 );
93             janitor.release();
94
95             // Freshen timestamp and clear ID.
96             toSend->setIssueInstant(time(NULL));
97             toSend->setID(NULL);
98     
99             // Encode message.
100             auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML2_POST_ENCODER, NULL));
101             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state");
102             toSend.release();
103             
104             // Decode message.
105             string relayState;
106             const RoleDescriptor* issuer=NULL;
107             bool trusted=false;
108             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
109             auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML2_POST_DECODER, NULL));
110             Locker locker(m_metadata);
111             auto_ptr<Response> response(
112                 dynamic_cast<Response*>(
113                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole)
114                     )
115                 );
116             
117             // Test the results.
118             TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
119             TSM_ASSERT("SAML Response not decoded successfully.", response.get());
120             TSM_ASSERT("Message was verified.", issuer && !trusted);
121             auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());
122             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
123             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
124
125             // Trigger a replay.
126             TSM_ASSERT_THROWS("Did not catch the replay.", 
127                 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),
128                 BindingException);
129         }
130         catch (XMLToolingException& ex) {
131             TS_TRACE(ex.what());
132             throw;
133         }
134     }
135
136     const char* getMethod() const {
137         return "POST";
138     } 
139
140     const char* getRequestURL() const {
141         return "https://sp.example.org/SAML/POST";
142     }
143     
144     const char* getQueryString() const {
145         return NULL;
146     }
147 };