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