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