Changed "virtual" URL to match between bindings.
[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         SAMLBindingBaseTestCase::setUp();\r
32     }\r
33 \r
34     void tearDown() {\r
35         SAMLBindingBaseTestCase::tearDown();\r
36     }\r
37 \r
38     void testSAML2Artifact() {\r
39         try {\r
40             QName idprole(samlconstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);\r
41             SecurityPolicy policy(m_rules, m_metadata, &idprole, m_trust);\r
42 \r
43             // Read message to use from file.\r
44             string path = data_path + "saml2/binding/SAML2Response.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<Response> toSend(\r
49                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))\r
50                 );\r
51             janitor.release();\r
52 \r
53             // Freshen timestamp.\r
54             toSend->setIssueInstant(time(NULL));\r
55 \r
56             // Encode message.\r
57             auto_ptr<MessageEncoder> encoder(\r
58                 SAMLConfig::getConfig().MessageEncoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_ARTIFACT, NULL)\r
59                 );\r
60             encoder->setArtifactGenerator(this);\r
61             encoder->encode(*this,toSend.get(),"https://sp.example.org/SAML/SSO","https://sp.example.org/","state",m_creds);\r
62             toSend.release();\r
63             \r
64             // Decode message.\r
65             string relayState;\r
66             auto_ptr<MessageDecoder> decoder(\r
67                 SAMLConfig::getConfig().MessageDecoderManager.newPlugin(samlconstants::SAML20_BINDING_HTTP_ARTIFACT, NULL)\r
68                 );\r
69             decoder->setArtifactResolver(this);\r
70             Locker locker(m_metadata);\r
71             auto_ptr<Response> response(dynamic_cast<Response*>(decoder->decode(relayState,*this,policy)));\r
72             \r
73             // Test the results.\r
74             TSM_ASSERT_EQUALS("RelayState 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.", policy.getIssuer()!=NULL);\r
77             auto_ptr_char entityID(policy.getIssuer()->getName());\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.", decoder->decode(relayState,*this,policy), BindingException);\r
83         }\r
84         catch (XMLToolingException& ex) {\r
85             TS_TRACE(ex.what());\r
86             throw;\r
87         }\r
88     }\r
89     \r
90     SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {\r
91         throw BindingException("Not implemented.");\r
92     }\r
93     \r
94     saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {\r
95         return new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"),1);\r
96     }\r
97     \r
98     saml1p::Response* resolve(\r
99         const vector<SAMLArtifact*>& artifacts,\r
100         const IDPSSODescriptor& idpDescriptor,\r
101         SecurityPolicy& policy\r
102         ) const {\r
103         throw BindingException("Not implemented.");\r
104     }\r
105 \r
106     ArtifactResponse* resolve(\r
107         const SAML2Artifact& artifact,\r
108         const SSODescriptorType& ssoDescriptor,\r
109         SecurityPolicy& policy\r
110         ) const {\r
111         XMLObject* xmlObject =\r
112             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(&artifact, "https://sp.example.org/");\r
113         Response* payload = dynamic_cast<Response*>(xmlObject);\r
114         TSM_ASSERT("Not a response.", payload!=NULL);\r
115         auto_ptr<ArtifactResponse> response(ArtifactResponseBuilder::buildArtifactResponse());\r
116         response->setPayload(payload);\r
117         Status* status = StatusBuilder::buildStatus();\r
118         response->setStatus(status);\r
119         StatusCode* sc = StatusCodeBuilder::buildStatusCode();\r
120         status->setStatusCode(sc);\r
121         sc->setValue(StatusCode::SUCCESS);\r
122         response->marshall();\r
123         SchemaValidators.validate(response.get());\r
124         return response.release();\r
125     }\r
126 };\r