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