2 * Copyright 2001-2006 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/Assertions.h>
19 #include <saml/util/SAMLConstants.h>
21 using namespace opensaml::saml2;
23 class Conditions20Test : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
24 DateTime* expectedNotBefore;
25 DateTime* expectedNotOnOrAfter;
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();
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();
41 delete expectedNotBefore;
42 delete expectedNotOnOrAfter;
43 SAMLObjectBaseTestCase::tearDown();
46 void testSingleElementUnmarshall() {
47 auto_ptr<XMLObject> xo(unmarshallElement(singleElementFile));
48 Conditions* conditions = dynamic_cast<Conditions*>(xo.get());
49 TS_ASSERT(conditions!=NULL);
52 TS_ASSERT(conditions->getNotBefore()==NULL);
53 TS_ASSERT(conditions->getNotOnOrAfter()==NULL);
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());
61 void testSingleElementOptionalAttributesUnmarshall() {
62 auto_ptr<XMLObject> xo(unmarshallElement(singleElementOptionalAttributesFile));
63 Conditions* conditions = dynamic_cast<Conditions*>(xo.get());
64 TS_ASSERT(conditions!=NULL);
66 TSM_ASSERT_EQUALS("NotBefore attribute", expectedNotBefore->getEpoch(), conditions->getNotBefore()->getEpoch());
67 TSM_ASSERT_EQUALS("NotOnOrAfter attribute", expectedNotOnOrAfter->getEpoch(), conditions->getNotOnOrAfter()->getEpoch());
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());
75 void testChildElementsUnmarshall() {
76 auto_ptr<XMLObject> xo(unmarshallElement(childElementsFile));
77 Conditions* conditions= dynamic_cast<Conditions*>(xo.get());
78 TS_ASSERT(conditions!=NULL);
80 TS_ASSERT(conditions->getNotBefore()==NULL);
81 TS_ASSERT(conditions->getNotOnOrAfter()==NULL);
83 TSM_ASSERT_EQUALS("# of Condition child elements", 0, 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());
89 void testSingleElementMarshall() {
90 Conditions* conditions=ConditionsBuilder::buildConditions();
91 assertEquals(expectedDOM, conditions);
94 void testSingleElementOptionalAttributesMarshall() {
95 Conditions* conditions=ConditionsBuilder::buildConditions();
96 conditions->setNotBefore(expectedNotBefore);
97 conditions->setNotOnOrAfter(expectedNotOnOrAfter);
98 assertEquals(expectedOptionalAttributesDOM, conditions);
101 void testChildElementsMarshall() {
102 Conditions* conditions=ConditionsBuilder::buildConditions();
104 //Test storing children as their direct type
105 conditions->getAudienceRestrictions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
106 conditions->getAudienceRestrictions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
107 conditions->getProxyRestrictions().push_back(ProxyRestrictionBuilder::buildProxyRestriction());
108 conditions->getAudienceRestrictions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
109 conditions->getOneTimeUses().push_back(OneTimeUseBuilder::buildOneTimeUse());
110 conditions->getProxyRestrictions().push_back(ProxyRestrictionBuilder::buildProxyRestriction());
111 assertEquals(expectedChildElementsDOM, conditions);
113 // Note: assertEquals() above has already 'delete'-ed the XMLObject* it was passed
115 conditions=ConditionsBuilder::buildConditions();
117 //Test storing children as a Condition (each is a derived type of ConditionAbstractType)
118 conditions->getConditions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
119 conditions->getConditions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
120 conditions->getConditions().push_back(ProxyRestrictionBuilder::buildProxyRestriction());
121 conditions->getConditions().push_back(AudienceRestrictionBuilder::buildAudienceRestriction());
122 conditions->getConditions().push_back(OneTimeUseBuilder::buildOneTimeUse());
123 conditions->getConditions().push_back(ProxyRestrictionBuilder::buildProxyRestriction());
124 assertEquals(expectedChildElementsDOM, conditions);