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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <saml/saml2/core/Protocols.h>
23 #include <saml/util/SAMLConstants.h>
25 using namespace opensaml::saml2p;
26 using namespace opensaml::saml2;
28 class AuthnRequest20Test : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
30 XMLCh* expectedVersion;
31 DateTime* expectedIssueInstant;
32 XMLCh* expectedConsent;
33 XMLCh* expectedDestination;
34 bool expectedForceAuthn;
35 bool expectedIsPassive;
36 XMLCh* expectedProtocolBinding;
37 int expectedAssertionConsumerServiceIndex;
38 XMLCh* expectedAssertionConsumerServiceURL;
39 int expectedAttributeConsumingServiceIndex;
40 XMLCh* expectedProviderName;
44 expectedID = XMLString::transcode("abc123");;
45 expectedVersion = XMLString::transcode("2.0");
46 expectedIssueInstant = new DateTime(XMLString::transcode("2006-02-21T16:40:00.000Z"));
47 expectedIssueInstant->parseDateTime();
48 expectedConsent = XMLString::transcode("urn:string:consent");
49 expectedDestination = XMLString::transcode("http://idp.example.org/endpoint");
50 expectedForceAuthn = true;
51 expectedIsPassive = true;
52 expectedProtocolBinding = XMLString::transcode("urn:string:protocol-binding");
53 expectedAssertionConsumerServiceIndex = 3;
54 expectedAssertionConsumerServiceURL = XMLString::transcode("http://sp.example.org/acs");
55 expectedAttributeConsumingServiceIndex = 2;
56 expectedProviderName = XMLString::transcode("Example Org");
58 singleElementFile = data_path + "saml2/core/impl/AuthnRequest.xml";
59 singleElementOptionalAttributesFile = data_path + "saml2/core/impl/AuthnRequestOptionalAttributes.xml";
60 childElementsFile = data_path + "saml2/core/impl/AuthnRequestChildElements.xml";
61 SAMLObjectBaseTestCase::setUp();
65 XMLString::release(&expectedID);
66 XMLString::release(&expectedVersion);
67 XMLString::release(&expectedConsent);
68 XMLString::release(&expectedDestination);
69 XMLString::release(&expectedProtocolBinding);
70 XMLString::release(&expectedAssertionConsumerServiceURL);
71 XMLString::release(&expectedProviderName);
72 delete expectedIssueInstant;
73 SAMLObjectBaseTestCase::tearDown();
76 void testSingleElementUnmarshall() {
77 auto_ptr<XMLObject> xo(unmarshallElement(singleElementFile));
78 AuthnRequest* request = dynamic_cast<AuthnRequest*>(xo.get());
79 TS_ASSERT(request!=nullptr);
80 assertEquals("ID attribute", expectedID, request->getID());
81 assertEquals("Version attribute", expectedVersion, request->getVersion());
82 TSM_ASSERT_EQUALS("IssueInstant attribute", expectedIssueInstant->getEpoch(), request->getIssueInstant()->getEpoch());
83 TSM_ASSERT_EQUALS("ForceAuthn attribute presence", xmlconstants::XML_BOOL_NULL, request->getForceAuthn());
84 TSM_ASSERT_EQUALS("IsPassive attribute presence", xmlconstants::XML_BOOL_NULL, request->getIsPassive());
85 TSM_ASSERT_EQUALS("AssertionConsumerServiceIndex attribute presence",false, request->getAssertionConsumerServiceIndex().first);
86 TSM_ASSERT_EQUALS("AttributeConsumingServiceIndex attribute presence", false, request->getAttributeConsumingServiceIndex().first);
88 TS_ASSERT(request->getIssuer()==nullptr);
89 TS_ASSERT(request->getSignature()==nullptr);
90 TS_ASSERT(request->getExtensions()==nullptr);
91 TS_ASSERT(request->getSubject()==nullptr);
92 TS_ASSERT(request->getNameIDPolicy()==nullptr);
93 TS_ASSERT(request->getConditions()==nullptr);
94 TS_ASSERT(request->getRequestedAuthnContext()==nullptr);
95 TS_ASSERT(request->getScoping()==nullptr);
98 void testSingleElementOptionalAttributesUnmarshall() {
99 auto_ptr<XMLObject> xo(unmarshallElement(singleElementOptionalAttributesFile));
100 AuthnRequest* request = dynamic_cast<AuthnRequest*>(xo.get());
101 TS_ASSERT(request!=nullptr);
103 assertEquals("Consent attribute", expectedConsent, request->getConsent());
104 assertEquals("Destination attribute", expectedDestination, request->getDestination());
105 TSM_ASSERT_EQUALS("ForceAuthn attribute value", expectedForceAuthn, request->ForceAuthn());
106 TSM_ASSERT_EQUALS("IsPassive attribute value", expectedIsPassive, request->IsPassive());
107 assertEquals("ProtocolBinding attribute", expectedProtocolBinding,request->getProtocolBinding());
108 TSM_ASSERT_EQUALS("AssertionConsumerServiceIndex attribute presence",true, request->getAssertionConsumerServiceIndex().first);
109 TSM_ASSERT_EQUALS("AssertionConsumerServiceIndex attribute value",expectedAssertionConsumerServiceIndex, request->getAssertionConsumerServiceIndex().second);
110 assertEquals("AssertionConsumerServierURL attribute", expectedAssertionConsumerServiceURL, request->getAssertionConsumerServiceURL());
111 TSM_ASSERT_EQUALS("AttributeConsumingServiceIndex attribute presence", true, request->getAttributeConsumingServiceIndex().first);
112 TSM_ASSERT_EQUALS("AttributeConsumingServiceIndex attribute value", expectedAttributeConsumingServiceIndex, request->getAttributeConsumingServiceIndex().second);
113 assertEquals("ProviderName attribute", expectedProviderName, request->getProviderName());
115 TS_ASSERT(request->getIssuer()==nullptr);
116 TS_ASSERT(request->getSignature()==nullptr);
117 TS_ASSERT(request->getExtensions()==nullptr);
118 TS_ASSERT(request->getSubject()==nullptr);
119 TS_ASSERT(request->getNameIDPolicy()==nullptr);
120 TS_ASSERT(request->getConditions()==nullptr);
121 TS_ASSERT(request->getRequestedAuthnContext()==nullptr);
122 TS_ASSERT(request->getScoping()==nullptr);
125 void testChildElementsUnmarshall() {
126 auto_ptr<XMLObject> xo(unmarshallElement(childElementsFile));
127 AuthnRequest* request= dynamic_cast<AuthnRequest*>(xo.get());
128 TS_ASSERT(request!=nullptr);
129 TS_ASSERT(request->getIssuer()!=nullptr);
130 TS_ASSERT(request->getSignature()==nullptr);
131 TS_ASSERT(request->getExtensions()==nullptr);
132 TS_ASSERT(request->getSubject()!=nullptr);
133 TS_ASSERT(request->getNameIDPolicy()!=nullptr);
134 TS_ASSERT(request->getConditions()!=nullptr);
135 TS_ASSERT(request->getRequestedAuthnContext()!=nullptr);
136 TS_ASSERT(request->getScoping()!=nullptr);
137 TSM_ASSERT_EQUALS("ForceAuthn attribute presence", xmlconstants::XML_BOOL_NULL, request->getForceAuthn());
138 TSM_ASSERT_EQUALS("IsPassive attribute presence", xmlconstants::XML_BOOL_NULL, request->getIsPassive());
139 TSM_ASSERT_EQUALS("AssertionConsumerServiceIndex attribute presence",false, request->getAssertionConsumerServiceIndex().first);
140 TSM_ASSERT_EQUALS("AttributeConsumingServiceIndex attribute presence", false, request->getAttributeConsumingServiceIndex().first);
143 void testSingleElementMarshall() {
144 AuthnRequest* request=AuthnRequestBuilder::buildAuthnRequest();
145 request->setID(expectedID);
146 request->setIssueInstant(expectedIssueInstant);
147 //request->setVersion(expectedVersion);
148 assertEquals(expectedDOM, request);
151 void testSingleElementOptionalAttributesMarshall() {
152 AuthnRequest* request=AuthnRequestBuilder::buildAuthnRequest();
153 request->setID(expectedID);
154 request->setIssueInstant(expectedIssueInstant);
155 //request->setVersion(expectedVersion);
156 request->setConsent(expectedConsent);
157 request->setDestination(expectedDestination);
158 request->ForceAuthn(expectedForceAuthn);
159 request->IsPassive(expectedIsPassive);
160 request->setProtocolBinding(expectedProtocolBinding);
161 request->setAssertionConsumerServiceIndex(expectedAssertionConsumerServiceIndex);
162 request->setAssertionConsumerServiceURL(expectedAssertionConsumerServiceURL);
163 request->setAttributeConsumingServiceIndex(expectedAttributeConsumingServiceIndex);
164 request->setProviderName(expectedProviderName);
165 assertEquals(expectedOptionalAttributesDOM, request);
168 void testChildElementsMarshall() {
169 AuthnRequest* request=AuthnRequestBuilder::buildAuthnRequest();
170 request->setID(expectedID);
171 request->setIssueInstant(expectedIssueInstant);
172 // Do this just so don't have to redeclare the saml namespace prefix on every child element in the control XML file
173 Namespace* ns = new Namespace(samlconstants::SAML20_NS, samlconstants::SAML20_PREFIX);
174 request->addNamespace(*ns);
175 request->setIssuer(IssuerBuilder::buildIssuer());
176 request->setSubject(SubjectBuilder::buildSubject());
177 request->setNameIDPolicy(NameIDPolicyBuilder::buildNameIDPolicy());
178 request->setConditions(ConditionsBuilder::buildConditions());
179 request->setRequestedAuthnContext(RequestedAuthnContextBuilder::buildRequestedAuthnContext());
180 request->setScoping(ScopingBuilder::buildScoping());
181 assertEquals(expectedChildElementsDOM, request);