400de422415c8ff58cd01b50cfa07babdc7531b5
[shibboleth/cpp-opensaml.git] / samltest / saml2 / core / impl / AuthnQuery20Test.h
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "internal.h"
18 #include <saml/saml2/core/Protocols.h>
19 #include <saml/util/SAMLConstants.h>
20
21 using namespace opensaml::saml2p;
22 using namespace opensaml::saml2;
23
24 class AuthnQuery20Test : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
25     XMLCh* expectedID; 
26     XMLCh* expectedVersion; 
27     XMLCh* expectedConsent; 
28     XMLCh* expectedDestination; 
29     DateTime* expectedIssueInstant; 
30     XMLCh* expectedSessionIndex; 
31
32 public:
33     void setUp() {
34         expectedID = XMLString::transcode("abc123");; 
35         expectedVersion = XMLString::transcode("2.0"); 
36         expectedConsent = XMLString::transcode("urn:string:consent"); 
37         expectedDestination = XMLString::transcode("http://idp.example.org/endpoint"); 
38         expectedIssueInstant = new DateTime(XMLString::transcode("2006-02-21T16:40:00.000Z"));
39         expectedIssueInstant->parseDateTime();
40         expectedSessionIndex = XMLString::transcode("session12345"); 
41
42         singleElementFile = data_path + "saml2/core/impl/AuthnQuery.xml";
43         singleElementOptionalAttributesFile = data_path + "saml2/core/impl/AuthnQueryOptionalAttributes.xml";
44         childElementsFile  = data_path + "saml2/core/impl/AuthnQueryChildElements.xml";    
45         SAMLObjectBaseTestCase::setUp();
46     }
47     
48     void tearDown() {
49         XMLString::release(&expectedID);
50         XMLString::release(&expectedVersion);
51         XMLString::release(&expectedConsent);
52         XMLString::release(&expectedDestination);
53         XMLString::release(&expectedSessionIndex);
54         delete expectedIssueInstant;
55         SAMLObjectBaseTestCase::tearDown();
56     }
57
58     void testSingleElementUnmarshall() {
59         auto_ptr<XMLObject> xo(unmarshallElement(singleElementFile));
60         AuthnQuery* query = dynamic_cast<AuthnQuery*>(xo.get());
61         TS_ASSERT(query!=NULL);
62         assertEquals("ID attribute", expectedID, query->getID());
63         assertEquals("Version attribute", expectedVersion, query->getVersion());
64         TSM_ASSERT_EQUALS("IssueInstant attribute", expectedIssueInstant->getEpoch(), query->getIssueInstant()->getEpoch());
65
66         TS_ASSERT(query->getIssuer()==NULL);
67         TS_ASSERT(query->getSignature()==NULL);
68         TS_ASSERT(query->getExtensions()==NULL);
69         TS_ASSERT(query->getSubject()==NULL);
70         TS_ASSERT(query->getRequestedAuthnContext()==NULL);
71     }
72
73     void testSingleElementOptionalAttributesUnmarshall() {
74         auto_ptr<XMLObject> xo(unmarshallElement(singleElementOptionalAttributesFile));
75         AuthnQuery* query = dynamic_cast<AuthnQuery*>(xo.get());
76         TS_ASSERT(query!=NULL);
77         assertEquals("Consent attribute", expectedConsent, query->getConsent());
78         assertEquals("Destination attribute", expectedDestination, query->getDestination());
79         assertEquals("SessionIndex attribute", expectedSessionIndex, query->getSessionIndex());
80
81         TS_ASSERT(query->getIssuer()==NULL);
82         TS_ASSERT(query->getSignature()==NULL);
83         TS_ASSERT(query->getExtensions()==NULL);
84         TS_ASSERT(query->getSubject()==NULL);
85         TS_ASSERT(query->getRequestedAuthnContext()==NULL);
86     }
87
88     void testChildElementsUnmarshall() {
89         auto_ptr<XMLObject> xo(unmarshallElement(childElementsFile));
90         AuthnQuery* query= dynamic_cast<AuthnQuery*>(xo.get());
91         TS_ASSERT(query!=NULL);
92         TS_ASSERT(query->getIssuer()!=NULL);
93         TS_ASSERT(query->getSignature()==NULL);
94         TS_ASSERT(query->getExtensions()==NULL);
95         TS_ASSERT(query->getSubject()!=NULL);
96         TS_ASSERT(query->getRequestedAuthnContext()!=NULL);
97     }
98
99     void testSingleElementMarshall() {
100         AuthnQuery* query=AuthnQueryBuilder::buildAuthnQuery();
101         query->setID(expectedID);
102         query->setIssueInstant(expectedIssueInstant);
103         //query->setVersion(expectedVersion);
104         assertEquals(expectedDOM, query);
105     }
106
107     void testSingleElementOptionalAttributesMarshall() {
108         AuthnQuery* query=AuthnQueryBuilder::buildAuthnQuery();
109         query->setID(expectedID);
110         query->setIssueInstant(expectedIssueInstant);
111         //query->setVersion(expectedVersion);
112         query->setConsent(expectedConsent);
113         query->setDestination(expectedDestination);
114         query->setSessionIndex(expectedSessionIndex);
115         assertEquals(expectedOptionalAttributesDOM, query);
116     }
117
118     void testChildElementsMarshall() {
119         AuthnQuery* query=AuthnQueryBuilder::buildAuthnQuery();
120         query->setID(expectedID);
121         query->setIssueInstant(expectedIssueInstant);
122         // Do this just so don't have to redeclare the saml namespace prefix on every child element in the control XML file
123         Namespace* ns = new Namespace(opensaml::SAMLConstants::SAML20_NS, opensaml::SAMLConstants::SAML20_PREFIX);
124         query->addNamespace(*ns);
125         query->setIssuer(IssuerBuilder::buildIssuer());
126         query->setSubject(SubjectBuilder::buildSubject());
127         query->setRequestedAuthnContext(RequestedAuthnContextBuilder::buildRequestedAuthnContext());
128         assertEquals(expectedChildElementsDOM, query);
129         delete ns;
130     }
131
132 };