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