Unit test for 1.x POST binding, plus fixes.
[shibboleth/cpp-opensaml.git] / samltest / saml1 / binding / SAML1POSTTest.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/saml1/core/Protocols.h>\r
20 \r
21 using namespace opensaml::saml1p;\r
22 using namespace opensaml::saml1;\r
23 \r
24 class SAML1POSTTest : public CxxTest::TestSuite, public SAMLBindingBaseTestCase {\r
25 public:\r
26     void setUp() {\r
27         m_fields.clear();\r
28         SAMLBindingBaseTestCase::setUp();\r
29     }\r
30 \r
31     void tearDown() {\r
32         m_fields.clear();\r
33         SAMLBindingBaseTestCase::tearDown();\r
34     }\r
35 \r
36     void testSAML1POSTTrusted() {\r
37         try {\r
38             // Read message to use from file.\r
39             string path = data_path + "saml1/binding/SAML1Response.xml";\r
40             ifstream in(path.c_str());\r
41             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);\r
42             XercesJanitor<DOMDocument> janitor(doc);\r
43             auto_ptr<Response> toSend(\r
44                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))\r
45                 );\r
46             janitor.release();\r
47 \r
48             // Freshen timestamp.\r
49             toSend->setIssueInstant(time(NULL));\r
50     \r
51             // Encode message.\r
52             auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML1_POST_ENCODER, NULL));\r
53             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state",m_creds);\r
54             toSend.release();\r
55             \r
56             // Decode message.\r
57             string relayState;\r
58             const RoleDescriptor* issuer=NULL;\r
59             bool trusted=false;\r
60             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);\r
61             auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML1_POST_DECODER, NULL));\r
62             auto_ptr<Response> response(\r
63                 dynamic_cast<Response*>(\r
64                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)\r
65                     )\r
66                 );\r
67             \r
68             // Test the results.\r
69             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");\r
70             TSM_ASSERT("SAML Response not decoded successfully.", response.get());\r
71             TSM_ASSERT("Message was not verified.", issuer && trusted);\r
72             auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());\r
73             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));\r
74             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);\r
75         }\r
76         catch (XMLToolingException& ex) {\r
77             TS_TRACE(ex.what());\r
78             throw;\r
79         }\r
80     }\r
81 \r
82     void testSAML1POSTUntrusted() {\r
83         try {\r
84             // Read message to use from file.\r
85             string path = data_path + "saml1/binding/SAML1Response.xml";\r
86             ifstream in(path.c_str());\r
87             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);\r
88             XercesJanitor<DOMDocument> janitor(doc);\r
89             auto_ptr<Response> toSend(\r
90                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))\r
91                 );\r
92             janitor.release();\r
93 \r
94             // Freshen timestamp and clear ID.\r
95             toSend->setIssueInstant(time(NULL));\r
96             toSend->setResponseID(NULL);\r
97     \r
98             // Encode message.\r
99             auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML1_POST_ENCODER, NULL));\r
100             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state");\r
101             toSend.release();\r
102             \r
103             // Decode message.\r
104             string relayState;\r
105             const RoleDescriptor* issuer=NULL;\r
106             bool trusted=false;\r
107             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);\r
108             auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML1_POST_DECODER, NULL));\r
109             auto_ptr<Response> response(\r
110                 dynamic_cast<Response*>(\r
111                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole)\r
112                     )\r
113                 );\r
114             \r
115             // Test the results.\r
116             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");\r
117             TSM_ASSERT("SAML Response not decoded successfully.", response.get());\r
118             TSM_ASSERT("Message was verified.", issuer && !trusted);\r
119             auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());\r
120             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));\r
121             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);\r
122 \r
123             // Trigger a replay.\r
124             TSM_ASSERT_THROWS("Did not catch the replay.", \r
125                 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),\r
126                 BindingException);\r
127         }\r
128         catch (XMLToolingException& ex) {\r
129             TS_TRACE(ex.what());\r
130             throw;\r
131         }\r
132     }\r
133 \r
134     const char* getMethod() const {\r
135         return "POST";\r
136     } \r
137 \r
138     const char* getRequestURL() const {\r
139         return "https://sp.example.org/SAML/POST";\r
140     }\r
141     \r
142     const char* getQueryString() const {\r
143         return NULL;\r
144     }\r
145 };\r