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