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