e763ff35d4320b0167c8da8d261f5d535896beeb
[shibboleth/cpp-opensaml.git] / samltest / saml2 / binding / SAML2ArtifactTest.h
1 /*\r
2  *  Copyright 2001-2007 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_rules2, m_metadata, &idprole, m_trust, false);\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.isSecure());\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             policy.reset();\r
83             TSM_ASSERT_THROWS("Did not catch the replay.", decoder->decode(relayState,*this,policy), BindingException);\r
84         }\r
85         catch (XMLToolingException& ex) {\r
86             TS_TRACE(ex.what());\r
87             throw;\r
88         }\r
89     }\r
90     \r
91     SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {\r
92         throw BindingException("Not implemented.");\r
93     }\r
94     \r
95     saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {\r
96         return new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"),1);\r
97     }\r
98     \r
99     saml1p::Response* resolve(\r
100         const vector<SAMLArtifact*>& artifacts,\r
101         const IDPSSODescriptor& idpDescriptor,\r
102         SecurityPolicy& policy\r
103         ) const {\r
104         throw BindingException("Not implemented.");\r
105     }\r
106 \r
107     ArtifactResponse* resolve(\r
108         const SAML2Artifact& artifact,\r
109         const SSODescriptorType& ssoDescriptor,\r
110         SecurityPolicy& policy\r
111         ) const {\r
112         XMLObject* xmlObject =\r
113             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(&artifact, "https://sp.example.org/");\r
114         Response* payload = dynamic_cast<Response*>(xmlObject);\r
115         TSM_ASSERT("Not a response.", payload!=NULL);\r
116         auto_ptr<ArtifactResponse> response(ArtifactResponseBuilder::buildArtifactResponse());\r
117         response->setPayload(payload);\r
118         Status* status = StatusBuilder::buildStatus();\r
119         response->setStatus(status);\r
120         StatusCode* sc = StatusCodeBuilder::buildStatusCode();\r
121         status->setStatusCode(sc);\r
122         sc->setValue(StatusCode::SUCCESS);\r
123         response->marshall();\r
124         SchemaValidators.validate(response.get());\r
125         policy.evaluate(*(response.get()), this);\r
126         return response.release();\r
127     }\r
128 };\r