Fix linefeeds
[shibboleth/cpp-opensaml.git] / samltest / saml1 / binding / SAML1ArtifactTest.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/binding/ArtifactMap.h>
20 #include <saml/saml1/core/Assertions.h>
21 #include <saml/saml1/core/Protocols.h>
22 #include <saml/saml1/binding/SAMLArtifactType0001.h>
23 #include <xmltooling/validation/ValidatorSuite.h>
24
25 using namespace opensaml::saml1p;
26 using namespace opensaml::saml1;
27
28 class SAML1ArtifactTest : public CxxTest::TestSuite,
29     public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {
30 public:
31     void setUp() {
32         m_fields.clear();
33         SAMLBindingBaseTestCase::setUp();
34     }
35
36     void tearDown() {
37         m_fields.clear();
38         SAMLBindingBaseTestCase::tearDown();
39     }
40
41     void testSAML1Artifact() {
42         try {
43             // Read message to use from file.
44             string path = data_path + "saml1/binding/SAML1Assertion.xml";
45             ifstream in(path.c_str());
46             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
47             XercesJanitor<DOMDocument> janitor(doc);
48             auto_ptr<Assertion> toSend(
49                 dynamic_cast<Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
50                 );
51             janitor.release();
52
53             // Encode message.
54             auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML1_ARTIFACT_ENCODER, NULL));
55             encoder->setArtifactGenerator(this);
56             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);
57             toSend.release();
58             
59             // Decode message.
60             string relayState;
61             const RoleDescriptor* issuer=NULL;
62             bool trusted=false;
63             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);
64             auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML1_ARTIFACT_DECODER, NULL));
65             decoder->setArtifactResolver(this);
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             // Trigger a replay.
82             TSM_ASSERT_THROWS("Did not catch the replay.", 
83                 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),
84                 BindingException);
85         }
86         catch (XMLToolingException& ex) {
87             TS_TRACE(ex.what());
88             throw;
89         }
90     }
91
92     const char* getMethod() const {
93         return "GET";
94     } 
95
96     const char* getRequestURL() const {
97         return "https://sp.example.org/SAML/Artifact";
98     }
99     
100     const char* getQueryString() const {
101         return NULL;
102     }
103     
104     SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {
105         return new SAMLArtifactType0001(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"));
106     }
107     
108     saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {
109         throw BindingException("Not implemented.");
110     }
111     
112     Response* resolve(
113         bool& authenticated,
114         const vector<SAMLArtifact*>& artifacts,
115         const IDPSSODescriptor& idpDescriptor,
116         const X509TrustEngine* trustEngine=NULL
117         ) const {
118         TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);
119         XMLObject* xmlObject =
120             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");
121         Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
122         TSM_ASSERT("Not an assertion.", assertion!=NULL);
123         auto_ptr<Response> response(ResponseBuilder::buildResponse());
124         response->getAssertions().push_back(assertion);
125         Status* status = StatusBuilder::buildStatus();
126         response->setStatus(status);
127         StatusCode* sc = StatusCodeBuilder::buildStatusCode();
128         status->setStatusCode(sc);
129         sc->setValue(&StatusCode::SUCCESS);
130         response->marshall();
131         SchemaValidators.validate(response.get());
132         authenticated = true;
133         return response.release();
134     }
135
136     saml2p::ArtifactResponse* resolve(
137         bool& authenticated,
138         const saml2p::SAML2Artifact& artifact,
139         const SSODescriptorType& ssoDescriptor,
140         const X509TrustEngine* trustEngine=NULL
141         ) const {
142         throw BindingException("Not implemented.");
143     }
144 };