ccb1d40bd1798625f577f201c3011b89dd5eb5d1
[shibboleth/cpp-opensaml.git] / samltest / saml2 / core / impl / ArtifactResponse20Test.h
1 /*
2  *  Copyright 2001-2007 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/Protocols.h>
19 #include <saml/util/SAMLConstants.h>
20
21 using namespace opensaml::saml2p;
22 using namespace opensaml::saml2;
23
24 class ArtifactResponse20Test : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
25     XMLCh* expectedID; 
26     XMLCh* expectedInResponseTo; 
27     XMLCh* expectedVersion; 
28     XMLCh* expectedConsent; 
29     XMLCh* expectedDestination; 
30     DateTime* expectedIssueInstant; 
31
32     // The payload will be an AuthnRequest in this test.
33     // AuthnRequest marshaller autogenerates ID, Version and IssueInstant if they are NULL,
34     // so have to agree on something to put in the control XML
35     XMLCh* authnRequestID;
36
37 public:
38     void setUp() {
39         expectedID = XMLString::transcode("def456"); 
40         expectedInResponseTo = XMLString::transcode("abc123"); 
41         expectedVersion = XMLString::transcode("2.0"); 
42         expectedConsent = XMLString::transcode("urn:string:consent"); 
43         expectedDestination = XMLString::transcode("http://sp.example.org/endpoint"); 
44         expectedIssueInstant = new DateTime(XMLString::transcode("2006-02-21T16:40:00.000Z"));
45         expectedIssueInstant->parseDateTime();
46
47         authnRequestID = XMLString::transcode("test1"); 
48
49         singleElementFile = data_path + "saml2/core/impl/ArtifactResponse.xml";
50         singleElementOptionalAttributesFile = data_path + "saml2/core/impl/ArtifactResponseOptionalAttributes.xml";
51         childElementsFile  = data_path + "saml2/core/impl/ArtifactResponseChildElements.xml";    
52         SAMLObjectBaseTestCase::setUp();
53     }
54     
55     void tearDown() {
56         XMLString::release(&expectedID);
57         XMLString::release(&expectedInResponseTo);
58         XMLString::release(&expectedVersion);
59         XMLString::release(&expectedConsent);
60         XMLString::release(&expectedDestination);
61         XMLString::release(&authnRequestID);
62         delete expectedIssueInstant;
63         SAMLObjectBaseTestCase::tearDown();
64     }
65
66     void testSingleElementUnmarshall() {
67         auto_ptr<XMLObject> xo(unmarshallElement(singleElementFile));
68         ArtifactResponse* response = dynamic_cast<ArtifactResponse*>(xo.get());
69         TS_ASSERT(response!=NULL);
70
71         assertEquals("ID attribute", expectedID, response->getID());
72         assertEquals("Version attribute", expectedVersion, response->getVersion());
73         TSM_ASSERT_EQUALS("IssueInstant attribute", expectedIssueInstant->getEpoch(), response->getIssueInstant()->getEpoch());
74
75         TS_ASSERT(response->getIssuer()==NULL);
76         TS_ASSERT(response->getSignature()==NULL);
77         TS_ASSERT(response->getExtensions()==NULL);
78         TS_ASSERT(response->getStatus()==NULL);
79         TS_ASSERT(response->getPayload()==NULL);
80     }
81
82     void testSingleElementOptionalAttributesUnmarshall() {
83         auto_ptr<XMLObject> xo(unmarshallElement(singleElementOptionalAttributesFile));
84         ArtifactResponse* response = dynamic_cast<ArtifactResponse*>(xo.get());
85         TS_ASSERT(response!=NULL);
86
87         assertEquals("Consent attribute", expectedConsent, response->getConsent());
88         assertEquals("Destination attribute", expectedDestination, response->getDestination());
89         assertEquals("InResponseTo attribute", expectedInResponseTo, response->getInResponseTo());
90
91         TS_ASSERT(response->getIssuer()==NULL);
92         TS_ASSERT(response->getSignature()==NULL);
93         TS_ASSERT(response->getExtensions()==NULL);
94         TS_ASSERT(response->getStatus()==NULL);
95         TS_ASSERT(response->getPayload()==NULL);
96     }
97
98     void testChildElementsUnmarshall() {
99         auto_ptr<XMLObject> xo(unmarshallElement(childElementsFile));
100         ArtifactResponse* response= dynamic_cast<ArtifactResponse*>(xo.get());
101         TS_ASSERT(response!=NULL);
102
103         TS_ASSERT(response->getIssuer()!=NULL);
104         TS_ASSERT(response->getSignature()==NULL);
105         TS_ASSERT(response->getExtensions()==NULL);
106         TS_ASSERT(response->getStatus()!=NULL);
107         TS_ASSERT(response->getPayload()!=NULL);
108     }
109
110     void testSingleElementMarshall() {
111         ArtifactResponse* response = ArtifactResponseBuilder::buildArtifactResponse();
112         TS_ASSERT(response!=NULL);
113
114         response->setID(expectedID);
115         response->setIssueInstant(expectedIssueInstant);
116         //response->setVersion(expectedVersion);
117         assertEquals(expectedDOM, response);
118     }
119
120     void testSingleElementOptionalAttributesMarshall() {
121         ArtifactResponse* response = ArtifactResponseBuilder::buildArtifactResponse();
122         TS_ASSERT(response!=NULL);
123
124         response->setID(expectedID);
125         response->setInResponseTo(expectedInResponseTo);
126         response->setIssueInstant(expectedIssueInstant);
127         //response->setVersion(expectedVersion);
128         response->setConsent(expectedConsent);
129         response->setDestination(expectedDestination);
130         response->setInResponseTo(expectedInResponseTo);
131         assertEquals(expectedOptionalAttributesDOM, response);
132     }
133
134     void testChildElementsMarshall() {
135         ArtifactResponse* response = ArtifactResponseBuilder::buildArtifactResponse();
136         TS_ASSERT(response!=NULL);
137
138         response->setID(expectedID);
139         response->setIssueInstant(expectedIssueInstant);
140         // Do this just so don't have to redeclare the saml namespace prefix on every child element in the control XML file
141         Namespace* ns = new Namespace(samlconstants::SAML20_NS, samlconstants::SAML20_PREFIX);
142         response->addNamespace(*ns);
143         response->setIssuer(IssuerBuilder::buildIssuer());
144         response->setStatus(StatusBuilder::buildStatus());
145
146         AuthnRequest* authnRequest = AuthnRequestBuilder::buildAuthnRequest();
147         authnRequest->setIssueInstant(expectedIssueInstant);
148         authnRequest->setID(authnRequestID);
149         response->setPayload(authnRequest);
150
151         assertEquals(expectedChildElementsDOM, response);
152         delete ns;
153     }
154
155 };