Convert from NULL macro to nullptr, remove unused zlib code.
[shibboleth/cpp-opensaml.git] / samltest / saml2 / core / impl / Assertion20Test.h
1 /*
2  *  Copyright 2001-2010 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 Assertion20Test : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
24     const XMLCh* expectedVersion;
25     XMLCh* expectedID;
26     DateTime* expectedIssueInstant;
27
28 public:
29     void setUp() {
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();
34
35
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();
40     }
41
42     void tearDown() {
43         delete expectedIssueInstant;
44         XMLString::release(&expectedID);
45         SAMLObjectBaseTestCase::tearDown();
46     }
47
48     void testSingleElementUnmarshall() {
49         auto_ptr<XMLObject> xo(unmarshallElement(singleElementFile));
50         Assertion* assertion = dynamic_cast<Assertion*>(xo.get());
51         TS_ASSERT(assertion!=nullptr);
52
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());
56
57         TS_ASSERT(assertion->getIssuer()==nullptr);
58         TS_ASSERT(assertion->getSignature()==nullptr);
59         TS_ASSERT(assertion->getSubject()==nullptr);
60         TS_ASSERT(assertion->getConditions()==nullptr);
61         TS_ASSERT(assertion->getAdvice()==nullptr);
62
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());
67     }
68
69     void testChildElementsUnmarshall() {
70         auto_ptr<XMLObject> xo(unmarshallElement(childElementsFile));
71         Assertion* assertion= dynamic_cast<Assertion*>(xo.get());
72         TS_ASSERT(assertion!=nullptr);
73
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());
77
78         TS_ASSERT(assertion->getIssuer()!=nullptr);
79         TS_ASSERT(assertion->getSignature()==nullptr);
80         TS_ASSERT(assertion->getSubject()!=nullptr);
81         TS_ASSERT(assertion->getConditions()!=nullptr);
82         TS_ASSERT(assertion->getAdvice()!=nullptr);
83
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());
88     }
89
90     void testSingleElementMarshall() {
91         Assertion* assertion=AssertionBuilder::buildAssertion();
92         assertion->setID(expectedID);
93         assertion->setIssueInstant(expectedIssueInstant);
94         assertEquals(expectedDOM, assertion);
95     }
96
97     void testChildElementsMarshall() {
98         xmltooling::QName qext("http://www.opensaml.org/", "Foo", "ext");
99
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());
107
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);
117
118         // Note: assertEquals() above has already 'delete'-ed the XMLObject* it was passed
119         assertion=nullptr;
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());
127
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);
137     }
138
139 };