73a17ec64c8413a06e57866149d45b6455b6efeb
[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         SAMLBindingBaseTestCase::setUp();
28     }
29
30     void tearDown() {
31         SAMLBindingBaseTestCase::tearDown();
32     }
33
34     void testSAML1POSTTrusted() {
35         try {
36             // Read message to use from file.
37             string path = data_path + "saml1/binding/SAML1Response.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))
43                 );
44             janitor.release();
45
46             // Freshen timestamp.
47             toSend->setIssueInstant(time(NULL));
48     
49             // Encode message.
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::SAML1_PROFILE_BROWSER_POST, encoder_config->getDocumentElement()
61                     )
62                 );
63             encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/POST","https://sp.example.org/","state",m_creds);
64             toSend.release();
65             
66             // Decode message.
67             string relayState;
68             const RoleDescriptor* issuer=NULL;
69             bool trusted=false;
70             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
71             auto_ptr<MessageDecoder> decoder(
72                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAMLConstants::SAML1_PROFILE_BROWSER_POST, NULL)
73                 );
74             Locker locker(m_metadata);
75             auto_ptr<Response> response(
76                 dynamic_cast<Response*>(
77                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)
78                     )
79                 );
80             
81             // Test the results.
82             TSM_ASSERT_EQUALS("TARGET 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 && trusted);
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);
88         }
89         catch (XMLToolingException& ex) {
90             TS_TRACE(ex.what());
91             throw;
92         }
93     }
94
95     void testSAML1POSTUntrusted() {
96         try {
97             // Read message to use from file.
98             string path = data_path + "saml1/binding/SAML1Response.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))
104                 );
105             janitor.release();
106
107             // Freshen timestamp and clear ID.
108             toSend->setIssueInstant(time(NULL));
109             toSend->setResponseID(NULL);
110     
111             // Encode message.
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::SAML1_PROFILE_BROWSER_POST, encoder_config->getDocumentElement()
123                     )
124                 );
125             encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/POST","https://sp.example.org/","state");
126             toSend.release();
127             
128             // Decode message.
129             string relayState;
130             const RoleDescriptor* issuer=NULL;
131             bool trusted=false;
132             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
133             auto_ptr<MessageDecoder> decoder(
134                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAMLConstants::SAML1_PROFILE_BROWSER_POST, NULL)
135                 );
136             Locker locker(m_metadata);
137             auto_ptr<Response> response(
138                 dynamic_cast<Response*>(
139                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole)
140                     )
141                 );
142             
143             // Test the results.
144             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");
145             TSM_ASSERT("SAML Response not decoded successfully.", response.get());
146             TSM_ASSERT("Message was verified.", issuer && !trusted);
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);
150
151             // Trigger a replay.
152             TSM_ASSERT_THROWS("Did not catch the replay.", 
153                 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),
154                 BindingException);
155         }
156         catch (XMLToolingException& ex) {
157             TS_TRACE(ex.what());
158             throw;
159         }
160     }
161 };