Change license header, remove stale pkg files.
[shibboleth/cpp-opensaml.git] / samltest / saml2 / core / impl / Response20Test.h
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 #include "internal.h"
22 #include <saml/saml2/core/Protocols.h>
23 #include <saml/util/SAMLConstants.h>
24 #include <xmltooling/signature/Signature.h>
25
26 using namespace opensaml::saml2p;
27 using namespace opensaml::saml2;
28
29
30 class Response20Test : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
31     XMLCh* expectedID; 
32     XMLCh* expectedInResponseTo; 
33     XMLCh* expectedVersion; 
34     XMLCh* expectedConsent; 
35     XMLCh* expectedDestination; 
36     DateTime* expectedIssueInstant; 
37
38     // Assertion marshaller autogenerates ID, Version and IssueInstant if they are nullptr,
39     // so have to agree on something to put in the control XML
40     XMLCh* assertionID1, * assertionID2, * assertionID3;
41
42 public:
43     void setUp() {
44         expectedID = XMLString::transcode("def456"); 
45         expectedInResponseTo = XMLString::transcode("abc123"); 
46         expectedVersion = XMLString::transcode("2.0"); 
47         expectedConsent = XMLString::transcode("urn:string:consent"); 
48         expectedDestination = XMLString::transcode("http://sp.example.org/endpoint"); 
49         expectedIssueInstant = new DateTime(XMLString::transcode("2006-02-21T16:40:00.000Z"));
50         expectedIssueInstant->parseDateTime();
51
52         assertionID1 = XMLString::transcode("test1"); 
53         assertionID2= XMLString::transcode("test2"); 
54         assertionID3 = XMLString::transcode("test3"); 
55
56         singleElementFile = data_path + "saml2/core/impl/Response.xml";
57         singleElementOptionalAttributesFile = data_path + "saml2/core/impl/ResponseOptionalAttributes.xml";
58         childElementsFile  = data_path + "saml2/core/impl/ResponseChildElements.xml";    
59         SAMLObjectBaseTestCase::setUp();
60     }
61     
62     void tearDown() {
63         XMLString::release(&expectedID);
64         XMLString::release(&expectedInResponseTo);
65         XMLString::release(&expectedVersion);
66         XMLString::release(&expectedConsent);
67         XMLString::release(&expectedDestination);
68         XMLString::release(&assertionID1);
69         XMLString::release(&assertionID2);
70         XMLString::release(&assertionID3);
71         delete expectedIssueInstant;
72         SAMLObjectBaseTestCase::tearDown();
73     }
74
75     void testSingleElementUnmarshall() {
76         auto_ptr<XMLObject> xo(unmarshallElement(singleElementFile));
77         Response* response = dynamic_cast<Response*>(xo.get());
78         TS_ASSERT(response!=nullptr);
79
80         assertEquals("ID attribute", expectedID, response->getID());
81         assertEquals("Version attribute", expectedVersion, response->getVersion());
82         TSM_ASSERT_EQUALS("IssueInstant attribute", expectedIssueInstant->getEpoch(), response->getIssueInstant()->getEpoch());
83
84         TS_ASSERT(response->getIssuer()==nullptr);
85         TS_ASSERT(response->getSignature()==nullptr);
86         TS_ASSERT(response->getExtensions()==nullptr);
87         TS_ASSERT(response->getStatus()==nullptr);
88         TSM_ASSERT_EQUALS("# of Assertion child elements", 0, response->getAssertions().size());
89         TSM_ASSERT_EQUALS("# of EncryptedAssertion child elements", 0, response->getEncryptedAssertions().size());
90     }
91
92     void testSingleElementOptionalAttributesUnmarshall() {
93         auto_ptr<XMLObject> xo(unmarshallElement(singleElementOptionalAttributesFile));
94         Response* response = dynamic_cast<Response*>(xo.get());
95         TS_ASSERT(response!=nullptr);
96
97         assertEquals("Consent attribute", expectedConsent, response->getConsent());
98         assertEquals("Destination attribute", expectedDestination, response->getDestination());
99         assertEquals("InResponseTo attribute", expectedInResponseTo, response->getInResponseTo());
100
101         TS_ASSERT(response->getIssuer()==nullptr);
102         TS_ASSERT(response->getSignature()==nullptr);
103         TS_ASSERT(response->getExtensions()==nullptr);
104         TS_ASSERT(response->getStatus()==nullptr);
105         TSM_ASSERT_EQUALS("# of Assertion child elements", 0, response->getAssertions().size());
106         TSM_ASSERT_EQUALS("# of EncryptedAssertion child elements", 0, response->getEncryptedAssertions().size());
107     }
108
109     void testChildElementsUnmarshall() {
110         auto_ptr<XMLObject> xo(unmarshallElement(childElementsFile));
111         Response* response= dynamic_cast<Response*>(xo.get());
112         TS_ASSERT(response!=nullptr);
113
114         TS_ASSERT(response->getIssuer()!=nullptr);
115         TS_ASSERT(response->getSignature()!=nullptr);
116         TS_ASSERT(response->getExtensions()!=nullptr);
117         TS_ASSERT(response->getStatus()!=nullptr);
118         TSM_ASSERT_EQUALS("# of Assertion child elements", 3, response->getAssertions().size());
119         TSM_ASSERT_EQUALS("# of EncryptedAssertion child elements", 1, response->getEncryptedAssertions().size());
120     }
121
122     void testSingleElementMarshall() {
123         Response* response = ResponseBuilder::buildResponse();
124         TS_ASSERT(response!=nullptr);
125
126         response->setID(expectedID);
127         response->setIssueInstant(expectedIssueInstant);
128         //response->setVersion(expectedVersion);
129         assertEquals(expectedDOM, response);
130     }
131
132     void testSingleElementOptionalAttributesMarshall() {
133         Response* response = ResponseBuilder::buildResponse();
134         TS_ASSERT(response!=nullptr);
135
136         response->setID(expectedID);
137         response->setInResponseTo(expectedInResponseTo);
138         response->setIssueInstant(expectedIssueInstant);
139         //response->setVersion(expectedVersion);
140         response->setConsent(expectedConsent);
141         response->setDestination(expectedDestination);
142         response->setInResponseTo(expectedInResponseTo);
143         assertEquals(expectedOptionalAttributesDOM, response);
144     }
145
146     void testChildElementsMarshall() {
147         Response* response = ResponseBuilder::buildResponse();
148         TS_ASSERT(response!=nullptr);
149
150         response->setID(expectedID);
151         response->setIssueInstant(expectedIssueInstant);
152         // Do this just so don't have to redeclare the saml namespace prefix on every child element in the control XML file
153         Namespace* ns = new Namespace(samlconstants::SAML20_NS, samlconstants::SAML20_PREFIX);
154         response->addNamespace(*ns);
155         response->setIssuer(IssuerBuilder::buildIssuer());
156         // If the form of the default, basic, empty signature that is emittted changes wrt whitespace, etc,
157         // this will probably break the test.  In that case need to fix the control XML.
158         response->setSignature(xmlsignature::SignatureBuilder::buildSignature());
159         response->setExtensions(ExtensionsBuilder::buildExtensions());
160         response->setStatus(StatusBuilder::buildStatus());
161
162         Assertion* assertion=nullptr;
163
164         assertion = AssertionBuilder::buildAssertion();
165         assertion->setIssueInstant(expectedIssueInstant);
166         assertion->setID(assertionID1);
167         response->getAssertions().push_back(assertion);
168
169         assertion = AssertionBuilder::buildAssertion();
170         assertion->setIssueInstant(expectedIssueInstant);
171         assertion->setID(assertionID2);
172         response->getAssertions().push_back(assertion);
173
174         response->getEncryptedAssertions().push_back((EncryptedAssertionBuilder::buildEncryptedAssertion()));
175
176         assertion = AssertionBuilder::buildAssertion();
177         assertion->setIssueInstant(expectedIssueInstant);
178         assertion->setID(assertionID3);
179         response->getAssertions().push_back(assertion);
180
181
182         assertEquals(expectedChildElementsDOM, response);
183         delete ns;
184     }
185
186 };