4b26224ea3cf6886e3c630dbd329641951a263ea
[shibboleth/cpp-opensaml.git] / samltest / saml1 / binding / SAML1POSTTest.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/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         m_fields.clear();
28         SAMLBindingBaseTestCase::setUp();
29     }
30
31     void tearDown() {
32         m_fields.clear();
33         SAMLBindingBaseTestCase::tearDown();
34     }
35
36     void testSAML1POSTTrusted() {
37         try {
38             // Read message to use from file.
39             string path = data_path + "saml1/binding/SAML1Response.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(
53                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAMLConstants::SAML1_PROFILE_BROWSER_POST, NULL)
54                 );
55             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);
56             toSend.release();
57             
58             // Decode message.
59             string relayState;
60             const RoleDescriptor* issuer=NULL;
61             bool trusted=false;
62             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
63             auto_ptr<MessageDecoder> decoder(
64                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAMLConstants::SAML1_PROFILE_BROWSER_POST, NULL)
65                 );
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)
70                     )
71                 );
72             
73             // Test the results.
74             TSM_ASSERT_EQUALS("TARGET 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);
80         }
81         catch (XMLToolingException& ex) {
82             TS_TRACE(ex.what());
83             throw;
84         }
85     }
86
87     void testSAML1POSTUntrusted() {
88         try {
89             // Read message to use from file.
90             string path = data_path + "saml1/binding/SAML1Response.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))
96                 );
97             janitor.release();
98
99             // Freshen timestamp and clear ID.
100             toSend->setIssueInstant(time(NULL));
101             toSend->setResponseID(NULL);
102     
103             // Encode message.
104             auto_ptr<MessageEncoder> encoder(
105                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAMLConstants::SAML1_PROFILE_BROWSER_POST, NULL)
106                 );
107             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state");
108             toSend.release();
109             
110             // Decode message.
111             string relayState;
112             const RoleDescriptor* issuer=NULL;
113             bool trusted=false;
114             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
115             auto_ptr<MessageDecoder> decoder(
116                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAMLConstants::SAML1_PROFILE_BROWSER_POST, NULL)
117                 );
118             Locker locker(m_metadata);
119             auto_ptr<Response> response(
120                 dynamic_cast<Response*>(
121                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole)
122                     )
123                 );
124             
125             // Test the results.
126             TSM_ASSERT_EQUALS("TARGET 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);
132
133             // Trigger a replay.
134             TSM_ASSERT_THROWS("Did not catch the replay.", 
135                 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),
136                 BindingException);
137         }
138         catch (XMLToolingException& ex) {
139             TS_TRACE(ex.what());
140             throw;
141         }
142     }
143
144     const char* getMethod() const {
145         return "POST";
146     } 
147
148     const char* getRequestURL() const {
149         return "https://sp.example.org/SAML/POST";
150     }
151     
152     const char* getQueryString() const {
153         return NULL;
154     }
155 };