Removed unnecessary class from string literals.
[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         SAMLBindingBaseTestCase::setUp();
33     }
34
35     void tearDown() {
36         SAMLBindingBaseTestCase::tearDown();
37     }
38
39     void testSAML1Artifact() {
40         try {
41             // Read message to use from file.
42             string path = data_path + "saml1/binding/SAML1Assertion.xml";
43             ifstream in(path.c_str());
44             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
45             XercesJanitor<DOMDocument> janitor(doc);
46             auto_ptr<Assertion> toSend(
47                 dynamic_cast<Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
48                 );
49             janitor.release();
50
51             // Encode message.
52             auto_ptr<MessageEncoder> encoder(
53                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, NULL)
54                 );
55             encoder->setArtifactGenerator(this);
56             encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/Artifact","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(
65                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, NULL)
66                 );
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("TARGET 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     SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {
95         return new SAMLArtifactType0001(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"));
96     }
97     
98     saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {
99         throw BindingException("Not implemented.");
100     }
101     
102     Response* resolve(
103         bool& authenticated,
104         const vector<SAMLArtifact*>& artifacts,
105         const IDPSSODescriptor& idpDescriptor,
106         const X509TrustEngine* trustEngine=NULL
107         ) const {
108         TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);
109         XMLObject* xmlObject =
110             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");
111         Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
112         TSM_ASSERT("Not an assertion.", assertion!=NULL);
113         auto_ptr<Response> response(ResponseBuilder::buildResponse());
114         response->getAssertions().push_back(assertion);
115         Status* status = StatusBuilder::buildStatus();
116         response->setStatus(status);
117         StatusCode* sc = StatusCodeBuilder::buildStatusCode();
118         status->setStatusCode(sc);
119         sc->setValue(&StatusCode::SUCCESS);
120         response->marshall();
121         SchemaValidators.validate(response.get());
122         authenticated = true;
123         return response.release();
124     }
125
126     saml2p::ArtifactResponse* resolve(
127         bool& authenticated,
128         const saml2p::SAML2Artifact& artifact,
129         const SSODescriptorType& ssoDescriptor,
130         const X509TrustEngine* trustEngine=NULL
131         ) const {
132         throw BindingException("Not implemented.");
133     }
134 };