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