27e96eefbd82c1be369eeac8ef963867b6e69868
[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             Locker locker(m_metadata);\r
63             auto_ptr<Response> response(\r
64                 dynamic_cast<Response*>(\r
65                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust)\r
66                     )\r
67                 );\r
68             \r
69             // Test the results.\r
70             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");\r
71             TSM_ASSERT("SAML Response not decoded successfully.", response.get());\r
72             TSM_ASSERT("Message was not verified.", issuer && trusted);\r
73             auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());\r
74             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));\r
75             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);\r
76         }\r
77         catch (XMLToolingException& ex) {\r
78             TS_TRACE(ex.what());\r
79             throw;\r
80         }\r
81     }\r
82 \r
83     void testSAML1POSTUntrusted() {\r
84         try {\r
85             // Read message to use from file.\r
86             string path = data_path + "saml1/binding/SAML1Response.xml";\r
87             ifstream in(path.c_str());\r
88             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);\r
89             XercesJanitor<DOMDocument> janitor(doc);\r
90             auto_ptr<Response> toSend(\r
91                 dynamic_cast<Response*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))\r
92                 );\r
93             janitor.release();\r
94 \r
95             // Freshen timestamp and clear ID.\r
96             toSend->setIssueInstant(time(NULL));\r
97             toSend->setResponseID(NULL);\r
98     \r
99             // Encode message.\r
100             auto_ptr<MessageEncoder> encoder(SAMLConfig::getConfig().MessageEncoderManager.newPlugin(SAML1_POST_ENCODER, NULL));\r
101             encoder->encode(m_fields,toSend.get(),"https://sp.example.org/","state");\r
102             toSend.release();\r
103             \r
104             // Decode message.\r
105             string relayState;\r
106             const RoleDescriptor* issuer=NULL;\r
107             bool trusted=false;\r
108             QName idprole(SAMLConstants::SAML20MD_NS, IDPSSODescriptor::LOCAL_NAME);\r
109             auto_ptr<MessageDecoder> decoder(SAMLConfig::getConfig().MessageDecoderManager.newPlugin(SAML1_POST_DECODER, NULL));\r
110             Locker locker(m_metadata);\r
111             auto_ptr<Response> response(\r
112                 dynamic_cast<Response*>(\r
113                     decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole)\r
114                     )\r
115                 );\r
116             \r
117             // Test the results.\r
118             TSM_ASSERT_EQUALS("TARGET was not the expected result.", relayState, "state");\r
119             TSM_ASSERT("SAML Response not decoded successfully.", response.get());\r
120             TSM_ASSERT("Message was verified.", issuer && !trusted);\r
121             auto_ptr_char entityID(dynamic_cast<const EntityDescriptor*>(issuer->getParent())->getEntityID());\r
122             TSM_ASSERT("Issuer was not expected.", !strcmp(entityID.get(),"https://idp.example.org/"));\r
123             TSM_ASSERT_EQUALS("Assertion count was not correct.", response->getAssertions().size(), 1);\r
124 \r
125             // Trigger a replay.\r
126             TSM_ASSERT_THROWS("Did not catch the replay.", \r
127                 decoder->decode(relayState,issuer,trusted,*this,m_metadata,&idprole,m_trust),\r
128                 BindingException);\r
129         }\r
130         catch (XMLToolingException& ex) {\r
131             TS_TRACE(ex.what());\r
132             throw;\r
133         }\r
134     }\r
135 \r
136     const char* getMethod() const {\r
137         return "POST";\r
138     } \r
139 \r
140     const char* getRequestURL() const {\r
141         return "https://sp.example.org/SAML/POST";\r
142     }\r
143     \r
144     const char* getQueryString() const {\r
145         return NULL;\r
146     }\r
147 };\r