Multi-line svn commit, see body.
[shibboleth/cpp-opensaml.git] / samltest / saml2 / core / impl / StatusResponse20Test.h
1 /*\r
2  *  Copyright 2001-2006 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 "internal.h"\r
18 #include <saml/saml2/core/Protocols.h>\r
19 #include <saml/util/SAMLConstants.h>\r
20 \r
21 using namespace opensaml::saml2p;\r
22 using namespace opensaml::saml2;\r
23 \r
24 /*\r
25    This tests building an element of type StatusResponseType with a non-SAML element name and namespace\r
26  */\r
27 \r
28 class StatusResponse20Test : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {\r
29     XMLCh* expectedID; \r
30     XMLCh* expectedInResponseTo; \r
31     XMLCh* expectedVersion; \r
32     XMLCh* expectedConsent; \r
33     XMLCh* expectedDestination; \r
34     DateTime* expectedIssueInstant; \r
35 \r
36     //TODO possibly move these up to SAMLObjectBaseTestCase, for use in optional helper methods below\r
37     XMLCh* elementName;\r
38     XMLCh* elementNS;\r
39     XMLCh* elementPrefix;\r
40     const XMLCh* typeName;\r
41     const XMLCh* typeNS;\r
42     const XMLCh* typePrefix;\r
43 \r
44 public:\r
45     void setUp() {\r
46         expectedID = XMLString::transcode("def456"); \r
47         expectedInResponseTo = XMLString::transcode("abc123"); \r
48         expectedVersion = XMLString::transcode("2.0"); \r
49         expectedConsent = XMLString::transcode("urn:string:consent"); \r
50         expectedDestination = XMLString::transcode("http://sp.example.org/endpoint"); \r
51         expectedIssueInstant = new DateTime(XMLString::transcode("2006-02-21T16:40:00.000Z"));\r
52         expectedIssueInstant->parseDateTime();\r
53 \r
54         elementName = XMLString::transcode("Foo");;\r
55         elementNS = XMLString::transcode("http://www.example.org/test");\r
56         elementPrefix = XMLString::transcode("test");;\r
57         typeName = StatusResponse::TYPE_NAME;\r
58         typeNS = SAMLConstants::SAML20P_NS;\r
59         typePrefix = SAMLConstants::SAML20P_PREFIX;\r
60 \r
61         singleElementFile = data_path + "saml2/core/impl/StatusResponse.xml";\r
62         singleElementOptionalAttributesFile = data_path + "saml2/core/impl/StatusResponseOptionalAttributes.xml";\r
63         childElementsFile  = data_path + "saml2/core/impl/StatusResponseChildElements.xml";    \r
64         SAMLObjectBaseTestCase::setUp();\r
65     }\r
66     \r
67     void tearDown() {\r
68         XMLString::release(&expectedID);\r
69         XMLString::release(&expectedInResponseTo);\r
70         XMLString::release(&expectedVersion);\r
71         XMLString::release(&expectedConsent);\r
72         XMLString::release(&expectedDestination);\r
73         XMLString::release(&elementName);\r
74         XMLString::release(&elementNS);\r
75         XMLString::release(&elementPrefix);\r
76         delete expectedIssueInstant;\r
77         SAMLObjectBaseTestCase::tearDown();\r
78     }\r
79 \r
80     //TODO possibly move this functionality up to SAMLObjectBaseTestCase, as optional helper method\r
81     void checkNameAndType(XMLObject* xo) {\r
82         assertEquals("Element name", elementName, xo->getElementQName().getLocalPart());\r
83         assertEquals("Element namespace", elementNS, xo->getElementQName().getNamespaceURI());\r
84         assertEquals("Element namespace prefix", elementPrefix, xo->getElementQName().getPrefix());\r
85 \r
86         assertEquals("Schema type name", typeName, xo->getSchemaType()->getLocalPart());\r
87         assertEquals("Schema type namespace", typeNS, xo->getSchemaType()->getNamespaceURI());\r
88         assertEquals("Schema type namespace prefix", typePrefix, xo->getSchemaType()->getPrefix());\r
89     }\r
90 \r
91     //TODO possibly move this functionality up to SAMLObjectBaseTestCase, as optional helper method\r
92     XMLObject * buildObject() {\r
93         const XMLObjectBuilder* builder = XMLObjectBuilder::getBuilder(QName(typeNS,typeName));\r
94         QName type(typeNS,typeName,typePrefix);\r
95         return builder->buildObject(elementNS, elementName, elementPrefix, &type);\r
96     }\r
97 \r
98     void testSingleElementUnmarshall() {\r
99         auto_ptr<XMLObject> xo(unmarshallElement(singleElementFile));\r
100         StatusResponse* response = dynamic_cast<StatusResponse*>(xo.get());\r
101         TS_ASSERT(response!=NULL);\r
102 \r
103         checkNameAndType(response);\r
104 \r
105         assertEquals("ID attribute", expectedID, response->getID());\r
106         assertEquals("Version attribute", expectedVersion, response->getVersion());\r
107         TSM_ASSERT_EQUALS("IssueInstant attribute", expectedIssueInstant->getEpoch(), response->getIssueInstant()->getEpoch());\r
108 \r
109         TS_ASSERT(response->getIssuer()==NULL);\r
110         TS_ASSERT(response->getSignature()==NULL);\r
111         TS_ASSERT(response->getExtensions()==NULL);\r
112         TS_ASSERT(response->getStatus()==NULL);\r
113     }\r
114 \r
115     void testSingleElementOptionalAttributesUnmarshall() {\r
116         auto_ptr<XMLObject> xo(unmarshallElement(singleElementOptionalAttributesFile));\r
117         StatusResponse* response = dynamic_cast<StatusResponse*>(xo.get());\r
118         TS_ASSERT(response!=NULL);\r
119 \r
120         checkNameAndType(response);\r
121 \r
122         assertEquals("Consent attribute", expectedConsent, response->getConsent());\r
123         assertEquals("Destination attribute", expectedDestination, response->getDestination());\r
124         assertEquals("InResponseTo attribute", expectedInResponseTo, response->getInResponseTo());\r
125 \r
126         TS_ASSERT(response->getIssuer()==NULL);\r
127         TS_ASSERT(response->getSignature()==NULL);\r
128         TS_ASSERT(response->getExtensions()==NULL);\r
129         TS_ASSERT(response->getStatus()==NULL);\r
130     }\r
131 \r
132     void testChildElementsUnmarshall() {\r
133         auto_ptr<XMLObject> xo(unmarshallElement(childElementsFile));\r
134         StatusResponse* response= dynamic_cast<StatusResponse*>(xo.get());\r
135         TS_ASSERT(response!=NULL);\r
136 \r
137         checkNameAndType(response);\r
138 \r
139         TS_ASSERT(response->getIssuer()!=NULL);\r
140         TS_ASSERT(response->getSignature()==NULL);\r
141         TS_ASSERT(response->getExtensions()==NULL);\r
142         TS_ASSERT(response->getStatus()!=NULL);\r
143     }\r
144 \r
145     void testSingleElementMarshall() {\r
146         StatusResponse* response = dynamic_cast<StatusResponse*>(buildObject());\r
147         TS_ASSERT(response!=NULL);\r
148         checkNameAndType(response);\r
149 \r
150         response->setID(expectedID);\r
151         response->setIssueInstant(expectedIssueInstant);\r
152         //response->setVersion(expectedVersion);\r
153         assertEquals(expectedDOM, response);\r
154     }\r
155 \r
156     void testSingleElementOptionalAttributesMarshall() {\r
157         StatusResponse* response = dynamic_cast<StatusResponse*>(buildObject());\r
158         TS_ASSERT(response!=NULL);\r
159         checkNameAndType(response);\r
160 \r
161         response->setID(expectedID);\r
162         response->setInResponseTo(expectedInResponseTo);\r
163         response->setIssueInstant(expectedIssueInstant);\r
164         //response->setVersion(expectedVersion);\r
165         response->setConsent(expectedConsent);\r
166         response->setDestination(expectedDestination);\r
167         response->setInResponseTo(expectedInResponseTo);\r
168         assertEquals(expectedOptionalAttributesDOM, response);\r
169     }\r
170 \r
171     void testChildElementsMarshall() {\r
172         StatusResponse* response = dynamic_cast<StatusResponse*>(buildObject());\r
173         TS_ASSERT(response!=NULL);\r
174         checkNameAndType(response);\r
175 \r
176         response->setID(expectedID);\r
177         response->setIssueInstant(expectedIssueInstant);\r
178         // Do this just so don't have to redeclare the saml namespace prefix on every child element in the control XML file\r
179         Namespace* ns = new Namespace(opensaml::SAMLConstants::SAML20_NS, opensaml::SAMLConstants::SAML20_PREFIX);\r
180         response->addNamespace(*ns);\r
181         response->setIssuer(IssuerBuilder::buildIssuer());\r
182         response->setStatus(StatusBuilder::buildStatus());\r
183         assertEquals(expectedChildElementsDOM, response);\r
184         delete ns;\r
185     }\r
186 \r
187 };\r