4bd3ddbed2f25f1bd7ec3d88542cfc80795f68f6
[shibboleth/cpp-opensaml.git] / samltest / saml2 / core / impl / Assertion20Test.h
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 #include "internal.h"\r
18 #include <saml/saml2/core/Assertions.h>\r
19 #include <saml/util/SAMLConstants.h>\r
20 \r
21 using namespace opensaml::saml2;\r
22 \r
23 class Assertion20Test : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {\r
24     const XMLCh* expectedVersion;\r
25     XMLCh* expectedID;\r
26     DateTime* expectedIssueInstant;\r
27 \r
28 public:\r
29     void setUp() {\r
30         expectedVersion = SAMLConstants::SAML20_VERSION;\r
31         expectedID = XMLString::transcode("abc123");\r
32         expectedIssueInstant = new DateTime(XMLString::transcode("1984-08-26T10:01:30.043Z"));\r
33         expectedIssueInstant->parseDateTime();\r
34     \r
35 \r
36         singleElementFile = data_path + "saml2/core/impl/Assertion.xml";\r
37         singleElementOptionalAttributesFile = data_path + "saml2/core/impl/AssertionOptionalAttributes.xml";\r
38         childElementsFile  = data_path + "saml2/core/impl/AssertionChildElements.xml";    \r
39         SAMLObjectBaseTestCase::setUp();\r
40     }\r
41     \r
42     void tearDown() {\r
43         delete expectedIssueInstant;\r
44         XMLString::release(&expectedID);\r
45         SAMLObjectBaseTestCase::tearDown();\r
46     }\r
47 \r
48     void testSingleElementUnmarshall() {\r
49         auto_ptr<XMLObject> xo(unmarshallElement(singleElementFile));\r
50         Assertion* assertion = dynamic_cast<Assertion*>(xo.get());\r
51         TS_ASSERT(assertion!=NULL);\r
52 \r
53         assertEquals("ID attribute", expectedID, assertion->getID());\r
54         assertEquals("Version attribute", expectedVersion, assertion->getVersion());\r
55         TSM_ASSERT_EQUALS("IssueInstant attribute", expectedIssueInstant->getEpoch(), assertion->getIssueInstant()->getEpoch());\r
56 \r
57         TS_ASSERT(assertion->getIssuer()==NULL);\r
58         TS_ASSERT(assertion->getSignature()==NULL);\r
59         TS_ASSERT(assertion->getSubject()==NULL);\r
60         TS_ASSERT(assertion->getConditions()==NULL);\r
61         TS_ASSERT(assertion->getAdvice()==NULL);\r
62 \r
63         TSM_ASSERT_EQUALS("# of Statement child elements", 0, assertion->getStatements().size());\r
64         TSM_ASSERT_EQUALS("# of AuthnStatement child elements", 0, assertion->getAuthnStatements().size());\r
65         TSM_ASSERT_EQUALS("# of AttributeStatement child elements", 0, assertion->getAttributeStatements().size());\r
66         TSM_ASSERT_EQUALS("# of AuthzDecisionStatement child elements", 0, assertion->getAuthzDecisionStatements().size());\r
67     }\r
68 \r
69     void testChildElementsUnmarshall() {\r
70         auto_ptr<XMLObject> xo(unmarshallElement(childElementsFile));\r
71         Assertion* assertion= dynamic_cast<Assertion*>(xo.get());\r
72         TS_ASSERT(assertion!=NULL);\r
73 \r
74         assertEquals("ID attribute", expectedID, assertion->getID());\r
75         assertEquals("Version attribute", expectedVersion, assertion->getVersion());\r
76         TSM_ASSERT_EQUALS("IssueInstant attribute", expectedIssueInstant->getEpoch(), assertion->getIssueInstant()->getEpoch());\r
77 \r
78         TS_ASSERT(assertion->getIssuer()!=NULL);\r
79         TS_ASSERT(assertion->getSignature()==NULL);\r
80         TS_ASSERT(assertion->getSubject()!=NULL);\r
81         TS_ASSERT(assertion->getConditions()!=NULL);\r
82         TS_ASSERT(assertion->getAdvice()!=NULL);\r
83 \r
84         TSM_ASSERT_EQUALS("# of Statement child elements", 0, assertion->getStatements().size());\r
85         TSM_ASSERT_EQUALS("# of AuthnStatement child elements", 1, assertion->getAuthnStatements().size());\r
86         TSM_ASSERT_EQUALS("# of AttributeStatement child elements", 3, assertion->getAttributeStatements().size());\r
87         TSM_ASSERT_EQUALS("# of AuthzDecisionStatement child elements", 2, assertion->getAuthzDecisionStatements().size());\r
88     }\r
89 \r
90     void testSingleElementMarshall() {\r
91         Assertion* assertion=AssertionBuilder::buildAssertion();\r
92         assertion->setID(expectedID);\r
93         assertion->setIssueInstant(expectedIssueInstant);\r
94         assertEquals(expectedDOM, assertion);\r
95     }\r
96 \r
97     void testChildElementsMarshall() {\r
98         Assertion* assertion=AssertionBuilder::buildAssertion();\r
99         assertion->setID(expectedID);\r
100         assertion->setIssueInstant(expectedIssueInstant);\r
101         assertion->setIssuer(IssuerBuilder::buildIssuer());\r
102         assertion->setSubject(SubjectBuilder::buildSubject());\r
103         assertion->setConditions(ConditionsBuilder::buildConditions());\r
104         assertion->setAdvice(AdviceBuilder::buildAdvice());\r
105 \r
106         //Test storing children as their direct type \r
107         assertion->getAuthnStatements().push_back(AuthnStatementBuilder::buildAuthnStatement());\r
108         assertion->getAttributeStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());\r
109         assertion->getAttributeStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());\r
110         assertion->getAuthzDecisionStatements().push_back(AuthzDecisionStatementBuilder::buildAuthzDecisionStatement());\r
111         assertion->getAuthzDecisionStatements().push_back(AuthzDecisionStatementBuilder::buildAuthzDecisionStatement());\r
112         assertion->getAttributeStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());\r
113         assertEquals(expectedChildElementsDOM, assertion);\r
114 \r
115         // Note: assertEquals() above has already 'delete'-ed the XMLObject* it was passed\r
116         assertion=NULL;\r
117         assertion=AssertionBuilder::buildAssertion();\r
118         assertion->setID(expectedID);\r
119         assertion->setIssueInstant(expectedIssueInstant);\r
120         assertion->setIssuer(IssuerBuilder::buildIssuer());\r
121         assertion->setSubject(SubjectBuilder::buildSubject());\r
122         assertion->setConditions(ConditionsBuilder::buildConditions());\r
123         assertion->setAdvice(AdviceBuilder::buildAdvice());\r
124 \r
125         //Test storing children as a Statement (each is a derived type of StatementAbstractType)\r
126         assertion->getStatements().push_back(AuthnStatementBuilder::buildAuthnStatement());\r
127         assertion->getStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());\r
128         assertion->getStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());\r
129         assertion->getStatements().push_back(AuthzDecisionStatementBuilder::buildAuthzDecisionStatement());\r
130         assertion->getStatements().push_back(AuthzDecisionStatementBuilder::buildAuthzDecisionStatement());\r
131         assertion->getStatements().push_back(AttributeStatementBuilder::buildAttributeStatement());\r
132         assertEquals(expectedChildElementsDOM, assertion);\r
133     }\r
134 \r
135 };\r