Removed ChildlessElement class references, ensure ComplexElement gets copied during...
[shibboleth/cpp-opensaml.git] / samltest / saml1 / binding / SAML1ArtifactTest.h
1 /*\r
2  *  Copyright 2001-2005 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 #include "binding.h"\r
18 \r
19 #include <saml/binding/ArtifactMap.h>\r
20 #include <saml/saml1/core/Assertions.h>\r
21 #include <saml/saml1/core/Protocols.h>\r
22 #include <saml/saml1/binding/SAMLArtifactType0001.h>\r
23 #include <xmltooling/validation/ValidatorSuite.h>\r
24 \r
25 using namespace opensaml::saml1p;\r
26 using namespace opensaml::saml1;\r
27 \r
28 class SAML1ArtifactTest : public CxxTest::TestSuite,\r
29     public SAMLBindingBaseTestCase, public MessageEncoder::ArtifactGenerator, public MessageDecoder::ArtifactResolver {\r
30 public:\r
31     void setUp() {\r
32         m_fields.clear();\r
33         SAMLBindingBaseTestCase::setUp();\r
34     }\r
35 \r
36     void tearDown() {\r
37         m_fields.clear();\r
38         SAMLBindingBaseTestCase::tearDown();\r
39     }\r
40 \r
41     void testSAML1Artifact() {\r
42         try {\r
43             // Read message to use from file.\r
44             string path = data_path + "saml1/binding/SAML1Assertion.xml";\r
45             ifstream in(path.c_str());\r
46             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);\r
47             XercesJanitor<DOMDocument> janitor(doc);\r
48             auto_ptr<Assertion> toSend(\r
49                 dynamic_cast<Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))\r
50                 );\r
51             janitor.release();\r
52 \r
53             // Encode message.\r
54             auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML1_ARTIFACT_ENCODER, NULL));\r
55             encoder->setArtifactGenerator(this);\r
56             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);\r
57             toSend.release();\r
58             \r
59             // Decode message.\r
60             string relayState;\r
61             const RoleDescriptor* issuer=NULL;\r
62             bool trusted=false;\r
63             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);\r
64             auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML1_ARTIFACT_DECODER, NULL));\r
65             decoder->setArtifactResolver(this);\r
66             Locker locker(m_metadata);\r
67             auto_ptr<Response> response(\r
68                 dynamic_cast<Response*>(\r
69                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)\r
70                     )\r
71                 );\r
72             \r
73             // Test the results.\r
74             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");\r
75             TSM_ASSERT("SAML Response not decoded successfully.", response.get());\r
76             TSM_ASSERT("Message was not verified.", issuer && trusted);\r
77             auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());\r
78             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));\r
79             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);\r
80 \r
81             // Trigger a replay.\r
82             TSM_ASSERT_THROWS("Did not catch the replay.", \r
83                 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),\r
84                 BindingException);\r
85         }\r
86         catch (XMLToolingException& ex) {\r
87             TS_TRACE(ex.what());\r
88             throw;\r
89         }\r
90     }\r
91 \r
92     const char* getMethod() const {\r
93         return "GET";\r
94     } \r
95 \r
96     const char* getRequestURL() const {\r
97         return "https://sp.example.org/SAML/Artifact";\r
98     }\r
99     \r
100     const char* getQueryString() const {\r
101         return NULL;\r
102     }\r
103     \r
104     SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {\r
105         return new SAMLArtifactType0001(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"));\r
106     }\r
107     \r
108     saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {\r
109         throw BindingException("Not implemented.");\r
110     }\r
111     \r
112     Response* resolve(\r
113         bool& authenticated,\r
114         const vector<SAMLArtifact*>& artifacts,\r
115         const IDPSSODescriptor& idpDescriptor,\r
116         const X509TrustEngine* trustEngine=NULL\r
117         ) const {\r
118         TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);\r
119         XMLObject* xmlObject =\r
120             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");\r
121         Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);\r
122         TSM_ASSERT("Not an assertion.", assertion!=NULL);\r
123         auto_ptr<Response> response(ResponseBuilder::buildResponse());\r
124         response->getAssertions().push_back(assertion);\r
125         Status* status = StatusBuilder::buildStatus();\r
126         response->setStatus(status);\r
127         StatusCode* sc = StatusCodeBuilder::buildStatusCode();\r
128         status->setStatusCode(sc);\r
129         sc->setValue(&StatusCode::SUCCESS);\r
130         response->marshall();\r
131         SchemaValidators.validate(response.get());\r
132         authenticated = true;\r
133         return response.release();\r
134     }\r
135 \r
136     saml2p::ArtifactResponse* resolve(\r
137         bool& authenticated,\r
138         const saml2p::SAML2Artifact& artifact,\r
139         const SSODescriptorType& ssoDescriptor,\r
140         const X509TrustEngine* trustEngine=NULL\r
141         ) const {\r
142         throw BindingException("Not implemented.");\r
143     }\r
144 };\r