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