b36c040e79c99307b0dc31e4e21b4ae784cfa3af
[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/Protocols.h>\r
21 #include <saml/saml1/binding/SAMLArtifactType0001.h>\r
22 \r
23 using namespace opensaml::saml1p;\r
24 using namespace opensaml::saml1;\r
25 \r
26 class SAML1ArtifactTest : 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 testSAML1Artifact() {\r
40         try {\r
41             // Read message to use from file.\r
42             string path = data_path + "saml1/binding/SAML1Assertion.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<Assertion> toSend(\r
47                 dynamic_cast<Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))\r
48                 );\r
49             janitor.release();\r
50 \r
51             // Encode message.\r
52             auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML1_ARTIFACT_ENCODER, NULL));\r
53             encoder->setArtifactGenerator(this);\r
54             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);\r
55             toSend.release();\r
56             \r
57             // Decode message.\r
58             string relayState;\r
59             const RoleDescriptor* issuer=NULL;\r
60             bool trusted=false;\r
61             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);\r
62             auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML1_ARTIFACT_DECODER, NULL));\r
63             decoder->setArtifactResolver(this);\r
64             Locker locker(m_metadata);\r
65             auto_ptr<Response> response(\r
66                 dynamic_cast<Response*>(\r
67                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)\r
68                     )\r
69                 );\r
70             \r
71             // Test the results.\r
72             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");\r
73             TSM_ASSERT("SAML Response not decoded successfully.", response.get());\r
74             TSM_ASSERT("Message was not verified.", issuer && trusted);\r
75             auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());\r
76             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));\r
77             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);\r
78 \r
79             // Trigger a replay.\r
80             TSM_ASSERT_THROWS("Did not catch the replay.", \r
81                 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),\r
82                 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     const char* getMethod() const {\r
91         return "GET";\r
92     } \r
93 \r
94     const char* getRequestURL() const {\r
95         return "https://sp.example.org/SAML/Artifact";\r
96     }\r
97     \r
98     const char* getQueryString() const {\r
99         return NULL;\r
100     }\r
101     \r
102     SAMLArtifact* generateSAML1Artifact(const char* relyingParty) const {\r
103         return new SAMLArtifactType0001(SAMLConfig::getConfig().hashSHA1("https://idp.example.org/"));\r
104     }\r
105     \r
106     saml2p::SAML2Artifact* generateSAML2Artifact(const char* relyingParty) const {\r
107         throw BindingException("Not implemented.");\r
108     }\r
109     \r
110     Response* resolve(\r
111         bool& authenticated,\r
112         const vector<SAMLArtifact*>& artifacts,\r
113         const IDPSSODescriptor& idpDescriptor,\r
114         const X509TrustEngine* trustEngine=NULL\r
115         ) const {\r
116         TSM_ASSERT_EQUALS("Too many artifacts.", artifacts.size(), 1);\r
117         XMLObject* xmlObject =\r
118             SAMLConfig::getConfig().getArtifactMap()->retrieveContent(artifacts.front(), "https://sp.example.org/");\r
119         Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);\r
120         TSM_ASSERT("Not an assertion.", assertion!=NULL);\r
121         auto_ptr<Response> response(ResponseBuilder::buildResponse());\r
122         response->getAssertions().push_back(assertion);\r
123         Status* status = StatusBuilder::buildStatus();\r
124         response->setStatus(status);\r
125         StatusCode* sc = StatusCodeBuilder::buildStatusCode();\r
126         status->setStatusCode(sc);\r
127         sc->setValue(&StatusCode::SUCCESS);\r
128         response->marshall();\r
129         SchemaValidators.validate(response.get());\r
130         authenticated = true;\r
131         return response.release();\r
132     }\r
133 \r
134     saml2p::ArtifactResponse* resolve(\r
135         bool& authenticated,\r
136         const saml2p::SAML2Artifact& artifact,\r
137         const SSODescriptorType& ssoDescriptor,\r
138         const X509TrustEngine* trustEngine=NULL\r
139         ) const {\r
140         throw BindingException("Not implemented.");\r
141     }\r
142 };\r