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