Change license header, remove stale pkg files.
[shibboleth/cpp-opensaml.git] / samltest / saml2 / core / impl / Conditions20Test.h
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #include "internal.h"
22 #include <saml/saml2/core/Assertions.h>
23 #include <saml/util/SAMLConstants.h>
24
25 using namespace opensaml::saml2;
26
27 class Conditions20Test : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
28     DateTime* expectedNotBefore;
29     DateTime* expectedNotOnOrAfter;
30
31 public:
32     void setUp() {
33         expectedNotBefore = new DateTime(XMLString::transcode("1984-08-26T10:01:30.043Z"));
34         expectedNotBefore->parseDateTime();
35         expectedNotOnOrAfter = new DateTime(XMLString::transcode("1984-08-26T10:11:30.043Z"));
36         expectedNotOnOrAfter->parseDateTime();
37
38         singleElementFile = data_path + "saml2/core/impl/Conditions.xml";
39         singleElementOptionalAttributesFile = data_path + "saml2/core/impl/ConditionsOptionalAttributes.xml";
40         childElementsFile  = data_path + "saml2/core/impl/ConditionsChildElements.xml";
41         SAMLObjectBaseTestCase::setUp();
42     }
43
44     void tearDown() {
45         delete expectedNotBefore;
46         delete expectedNotOnOrAfter;
47         SAMLObjectBaseTestCase::tearDown();
48     }
49
50     void testSingleElementUnmarshall() {
51         auto_ptr<XMLObject> xo(unmarshallElement(singleElementFile));
52         Conditions* conditions = dynamic_cast<Conditions*>(xo.get());
53         TS_ASSERT(conditions!=nullptr);
54
55
56         TS_ASSERT(conditions->getNotBefore()==nullptr);
57         TS_ASSERT(conditions->getNotOnOrAfter()==nullptr);
58
59         TSM_ASSERT_EQUALS("# of Condition child elements", 0, conditions->getConditions().size());
60         TSM_ASSERT_EQUALS("# of AudienceRestriction child elements", 0, conditions->getAudienceRestrictions().size());
61         TSM_ASSERT_EQUALS("# of OneTimeUse child elements", 0, conditions->getOneTimeUses().size());
62         TSM_ASSERT_EQUALS("# of ProxyRestriction child elements", 0, conditions->getProxyRestrictions().size());
63     }
64
65     void testSingleElementOptionalAttributesUnmarshall() {
66         auto_ptr<XMLObject> xo(unmarshallElement(singleElementOptionalAttributesFile));
67         Conditions* conditions = dynamic_cast<Conditions*>(xo.get());
68         TS_ASSERT(conditions!=nullptr);
69
70         TSM_ASSERT_EQUALS("NotBefore attribute", expectedNotBefore->getEpoch(), conditions->getNotBefore()->getEpoch());
71         TSM_ASSERT_EQUALS("NotOnOrAfter attribute", expectedNotOnOrAfter->getEpoch(), conditions->getNotOnOrAfter()->getEpoch());
72
73         TSM_ASSERT_EQUALS("# of Condition child elements", 0, conditions->getConditions().size());
74         TSM_ASSERT_EQUALS("# of AudienceRestriction child elements", 0, conditions->getAudienceRestrictions().size());
75         TSM_ASSERT_EQUALS("# of OneTimeUse child elements", 0, conditions->getOneTimeUses().size());
76         TSM_ASSERT_EQUALS("# of ProxyRestriction child elements", 0, conditions->getProxyRestrictions().size());
77     }
78
79     void testChildElementsUnmarshall() {
80         auto_ptr<XMLObject> xo(unmarshallElement(childElementsFile));
81         Conditions* conditions= dynamic_cast<Conditions*>(xo.get());
82         TS_ASSERT(conditions!=nullptr);
83
84         TS_ASSERT(conditions->getNotBefore()==nullptr);
85         TS_ASSERT(conditions->getNotOnOrAfter()==nullptr);
86
87         TSM_ASSERT_EQUALS("# of Condition child elements", 1, conditions->getConditions().size());
88         TSM_ASSERT_EQUALS("# of AudienceRestriction child elements", 3, conditions->getAudienceRestrictions().size());
89         TSM_ASSERT_EQUALS("# of OneTimeUse child elements", 1, conditions->getOneTimeUses().size());
90         TSM_ASSERT_EQUALS("# of ProxyRestriction child elements", 2, conditions->getProxyRestrictions().size());
91     }
92
93     void testSingleElementMarshall() {
94         Conditions* conditions=ConditionsBuilder::buildConditions();
95         assertEquals(expectedDOM, conditions);
96     }
97
98     void testSingleElementOptionalAttributesMarshall() {
99         Conditions* conditions=ConditionsBuilder::buildConditions();
100         conditions->setNotBefore(expectedNotBefore);
101         conditions->setNotOnOrAfter(expectedNotOnOrAfter);
102         assertEquals(expectedOptionalAttributesDOM, conditions);
103     }
104
105     void testChildElementsMarshall() {
106         xmltooling::QName qext("http://www.opensaml.org/", "Foo", "ext");
107         Conditions* conditions=ConditionsBuilder::buildConditions();
108
109         //Test storing children as their direct type
110         conditions->getAudienceRestrictions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
111         conditions->getAudienceRestrictions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
112         conditions->getProxyRestrictions().push_back(ProxyRestrictionBuilder::buildProxyRestriction());
113         conditions->getAudienceRestrictions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
114         conditions->getOneTimeUses().push_back(OneTimeUseBuilder::buildOneTimeUse());
115         conditions->getProxyRestrictions().push_back(ProxyRestrictionBuilder::buildProxyRestriction());
116         conditions->getConditions().push_back(ConditionBuilder::buildCondition(qext));
117         assertEquals(expectedChildElementsDOM, conditions);
118
119         // Note: assertEquals() above has already 'delete'-ed the XMLObject* it was passed
120         conditions=nullptr;
121         conditions=ConditionsBuilder::buildConditions();
122
123         //Test storing children as a Condition (each is a derived type of ConditionAbstractType)
124         conditions->getConditions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
125         conditions->getConditions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
126         conditions->getConditions().push_back(ProxyRestrictionBuilder::buildProxyRestriction());
127         conditions->getConditions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
128         conditions->getConditions().push_back(OneTimeUseBuilder::buildOneTimeUse());
129         conditions->getConditions().push_back(ProxyRestrictionBuilder::buildProxyRestriction());
130         conditions->getConditions().push_back(ConditionBuilder::buildCondition(qext));
131         assertEquals(expectedChildElementsDOM, conditions);
132     }
133
134 };