Major revamp of credential and trust handling code, PKIX engine still needs work.
[shibboleth/cpp-opensaml.git] / samltest / saml2 / binding / SAML2POSTTest.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 SAML2POSTTest : 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 testSAML2POST() {
35         try {
36             QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
37             SecurityPolicy policy(m_rules2, m_metadata, &idprole, m_trust, false);
38
39             // Read message to use from file.
40             string path = data_path + "saml2/binding/SAML2Response.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             CredentialCriteria cc;\r
50             cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);\r
51             Locker clocker(m_creds);\r
52             const Credential* cred = m_creds->resolve(&cc);\r
53             TSM_ASSERT("Retrieved credential was null", cred!=NULL);\r
54
55             // Freshen timestamp and ID.
56             toSend->setIssueInstant(time(NULL));
57             toSend->setID(NULL);
58     
59             // Encode message.
60             auto_ptr_XMLCh lit1("MessageEncoder");
61             auto_ptr_XMLCh lit2("template");
62             path = data_path + "binding/template.html";
63             auto_ptr_XMLCh lit3(path.c_str());
64             DOMDocument* encoder_config = XMLToolingConfig::getConfig().getParser().newDocument();
65             XercesJanitor<DOMDocument> janitor2(encoder_config);
66             encoder_config->appendChild(encoder_config->createElementNS(NULL,lit1.get()));
67             encoder_config->getDocumentElement()->setAttributeNS(NULL,lit2.get(),lit3.get());
68             auto_ptr<MessageEncoder> encoder(
69                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
70                     samlconstants::SAML20_BINDING_HTTP_POST, encoder_config->getDocumentElement()
71                     )
72                 );
73             encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/SSO","https://sp.example.org/","state",cred);
74             toSend.release();
75             
76             // Decode message.
77             string relayState;
78             auto_ptr<MessageDecoder> decoder(
79                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_POST, NULL)
80                 );
81             Locker locker(m_metadata);
82             auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
83             
84             // Test the results.
85             TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
86             TSM_ASSERT("SAML Response not decoded successfully.", response.get());
87             TSM_ASSERT("Message was not verified.", policy.isSecure());
88             auto_ptr_char entityID(policy.getIssuer()->getName());
89             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
90             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
91
92             // Trigger a replay.
93             policy.reset();
94             TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), SecurityPolicyException);
95         }
96         catch (XMLToolingException& ex) {
97             TS_TRACE(ex.what());
98             throw;
99         }
100     }
101
102     void testSAML2POSTSimpleSign() {
103         try {
104             QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
105             SecurityPolicy policy(m_rules2, m_metadata, &idprole, m_trust, false);
106
107             // Read message to use from file.
108             string path = data_path + "saml2/binding/SAML2Response.xml";
109             ifstream in(path.c_str());
110             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
111             XercesJanitor<DOMDocument> janitor(doc);
112             auto_ptr<Response> toSend(
113                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
114                 );
115             janitor.release();
116
117             CredentialCriteria cc;\r
118             cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);\r
119             Locker clocker(m_creds);\r
120             const Credential* cred = m_creds->resolve(&cc);\r
121             TSM_ASSERT("Retrieved credential was null", cred!=NULL);\r
122
123             // Freshen timestamp and ID.
124             toSend->setIssueInstant(time(NULL));
125             toSend->setID(NULL);
126     
127             // Encode message.
128             auto_ptr_XMLCh lit1("MessageEncoder");
129             auto_ptr_XMLCh lit2("template");
130             path = data_path + "binding/template.html";
131             auto_ptr_XMLCh lit3(path.c_str());
132             DOMDocument* encoder_config = XMLToolingConfig::getConfig().getParser().newDocument();
133             XercesJanitor<DOMDocument> janitor2(encoder_config);
134             encoder_config->appendChild(encoder_config->createElementNS(NULL,lit1.get()));
135             encoder_config->getDocumentElement()->setAttributeNS(NULL,lit2.get(),lit3.get());
136             auto_ptr<MessageEncoder> encoder(
137                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(
138                     samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN, encoder_config->getDocumentElement()
139                     )
140                 );
141             encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/SSO","https://sp.example.org/","state",cred);
142             toSend.release();
143             
144             // Decode message.
145             string relayState;
146             auto_ptr<MessageDecoder> decoder(
147                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN, NULL)
148                 );
149             Locker locker(m_metadata);
150             auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));
151             
152             // Test the results.
153             TSM_ASSERT_EQUALS("RelayState was not the expected result.", relayState, "state");
154             TSM_ASSERT("SAML Response not decoded successfully.", response.get());
155             TSM_ASSERT("Message was not verified.", policy.isSecure());
156             auto_ptr_char entityID(policy.getIssuer()->getName());
157             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));
158             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);
159
160             // Trigger a replay.
161             policy.reset();
162             TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), SecurityPolicyException);
163         }
164         catch (XMLToolingException& ex) {
165             TS_TRACE(ex.what());
166             throw;
167         }
168     }
169 };