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 Assertion20Test : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
28     const XMLCh* expectedVersion;
29     XMLCh* expectedID;
30     scoped_ptr<XMLDateTime> expectedIssueInstant;
31 
32 public:
setUp()33     void setUp() {
34         expectedVersion = samlconstants::SAML20_VERSION;
35         expectedID = XMLString::transcode("abc123");
36         expectedIssueInstant.reset(new XMLDateTime(XMLString::transcode("1984-08-26T10:01:30.043Z")));
37         expectedIssueInstant->parseDateTime();
38 
39         singleElementFile = data_path + "saml2/core/impl/Assertion.xml";
40         singleElementOptionalAttributesFile = data_path + "saml2/core/impl/AssertionOptionalAttributes.xml";
41         childElementsFile  = data_path + "saml2/core/impl/AssertionChildElements.xml";
42         SAMLObjectBaseTestCase::setUp();
43     }
44 
tearDown()45     void tearDown() {
46         expectedIssueInstant.reset();
47         XMLString::release(&expectedID);
48         SAMLObjectBaseTestCase::tearDown();
49     }
50 
testSingleElementUnmarshall()51     void testSingleElementUnmarshall() {
52         scoped_ptr<XMLObject> xo(unmarshallElement(singleElementFile));
53         Assertion* assertion = dynamic_cast<Assertion*>(xo.get());
54         TS_ASSERT(assertion!=nullptr);
55 
56         assertEquals("ID attribute", expectedID, assertion->getID());
57         assertEquals("Version attribute", expectedVersion, assertion->getVersion());
58         TSM_ASSERT_EQUALS("IssueInstant attribute", expectedIssueInstant->getEpoch(), assertion->getIssueInstant()->getEpoch());
59 
60         TS_ASSERT(assertion->getIssuer()==nullptr);
61         TS_ASSERT(assertion->getSignature()==nullptr);
62         TS_ASSERT(assertion->getSubject()==nullptr);
63         TS_ASSERT(assertion->getConditions()==nullptr);
64         TS_ASSERT(assertion->getAdvice()==nullptr);
65 
66         TSM_ASSERT_EQUALS("# of Statement child elements", 0, assertion->getStatements().size());
67         TSM_ASSERT_EQUALS("# of AuthnStatement child elements", 0, assertion->getAuthnStatements().size());
68         TSM_ASSERT_EQUALS("# of AttributeStatement child elements", 0, assertion->getAttributeStatements().size());
69         TSM_ASSERT_EQUALS("# of AuthzDecisionStatement child elements", 0, assertion->getAuthzDecisionStatements().size());
70     }
71 
testChildElementsUnmarshall()72     void testChildElementsUnmarshall() {
73         scoped_ptr<XMLObject> xo(unmarshallElement(childElementsFile));
74         Assertion* assertion= dynamic_cast<Assertion*>(xo.get());
75         TS_ASSERT(assertion!=nullptr);
76 
77         assertEquals("ID attribute", expectedID, assertion->getID());
78         assertEquals("Version attribute", expectedVersion, assertion->getVersion());
79         TSM_ASSERT_EQUALS("IssueInstant attribute", expectedIssueInstant->getEpoch(), assertion->getIssueInstant()->getEpoch());
80 
81         TS_ASSERT(assertion->getIssuer()!=nullptr);
82         TS_ASSERT(assertion->getSignature()==nullptr);
83         TS_ASSERT(assertion->getSubject()!=nullptr);
84         TS_ASSERT(assertion->getConditions()!=nullptr);
85         TS_ASSERT(assertion->getAdvice()!=nullptr);
86 
87         TSM_ASSERT_EQUALS("# of Statement child elements", 1, assertion->getStatements().size());
88         TSM_ASSERT_EQUALS("# of AuthnStatement child elements", 1, assertion->getAuthnStatements().size());
89         TSM_ASSERT_EQUALS("# of AttributeStatement child elements", 3, assertion->getAttributeStatements().size());
90         TSM_ASSERT_EQUALS("# of AuthzDecisionStatement child elements", 2, assertion->getAuthzDecisionStatements().size());
91     }
92 
testSingleElementMarshall()93     void testSingleElementMarshall() {
94         Assertion* assertion=AssertionBuilder::buildAssertion();
95         assertion->setID(expectedID);
96         assertion->setIssueInstant(expectedIssueInstant.get());
97         assertEquals(expectedDOM, assertion);
98     }
99 
testChildElementsMarshall()100     void testChildElementsMarshall() {
101         xmltooling::QName qext("http://www.opensaml.org/", "Foo", "ext");
102 
103         Assertion* assertion=AssertionBuilder::buildAssertion();
104         assertion->setID(expectedID);
105         assertion->setIssueInstant(expectedIssueInstant.get());
106         assertion->setIssuer(IssuerBuilder::buildIssuer());
107         assertion->setSubject(SubjectBuilder::buildSubject());
108         assertion->setConditions(ConditionsBuilder::buildConditions());
109         assertion->setAdvice(AdviceBuilder::buildAdvice());
110 
111         //Test storing children as their direct type
112         assertion->getAuthnStatements().push_back(AuthnStatementBuilder::buildAuthnStatement());
113         assertion->getAttributeStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());
114         assertion->getAttributeStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());
115         assertion->getAuthzDecisionStatements().push_back(AuthzDecisionStatementBuilder::buildAuthzDecisionStatement());
116         assertion->getAuthzDecisionStatements().push_back(AuthzDecisionStatementBuilder::buildAuthzDecisionStatement());
117         assertion->getAttributeStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());
118         assertion->getStatements().push_back(StatementBuilder::buildStatement(qext));
119         assertEquals(expectedChildElementsDOM, assertion);
120 
121         // Note: assertEquals() above has already 'delete'-ed the XMLObject* it was passed
122         assertion=nullptr;
123         assertion=AssertionBuilder::buildAssertion();
124         assertion->setID(expectedID);
125         assertion->setIssueInstant(expectedIssueInstant.get());
126         assertion->setIssuer(IssuerBuilder::buildIssuer());
127         assertion->setSubject(SubjectBuilder::buildSubject());
128         assertion->setConditions(ConditionsBuilder::buildConditions());
129         assertion->setAdvice(AdviceBuilder::buildAdvice());
130 
131         //Test storing children as a Statement (each is a derived type of StatementAbstractType)
132         assertion->getStatements().push_back(AuthnStatementBuilder::buildAuthnStatement());
133         assertion->getStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());
134         assertion->getStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());
135         assertion->getStatements().push_back(AuthzDecisionStatementBuilder::buildAuthzDecisionStatement());
136         assertion->getStatements().push_back(AuthzDecisionStatementBuilder::buildAuthzDecisionStatement());
137         assertion->getStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());
138         assertion->getStatements().push_back(StatementBuilder::buildStatement(qext));
139         assertEquals(expectedChildElementsDOM, assertion);
140     }
141 
142 };
143