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