Change license header, remove stale pkg files.
[shibboleth/cpp-opensaml.git] / samltest / signature / SAML1ResponseTest.h
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #include "signature/SAMLSignatureTestBase.h"
22
23 #include <saml/saml1/core/Assertions.h>
24 #include <saml/saml1/core/Protocols.h>
25
26 #include <fstream>
27
28 using namespace opensaml::saml1p;
29 using namespace opensaml::saml1;
30
31 class SAML1ResponseTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
32 public:
33     void setUp() {
34         childElementsFile  = data_path + "signature/SAML1Response.xml";
35         SAMLSignatureTestBase::setUp();
36     }
37
38     void tearDown() {
39         SAMLSignatureTestBase::tearDown();
40     }
41
42     void testSignature() {
43         auto_ptr_XMLCh issuer("issuer");
44         auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
45         auto_ptr_XMLCh aid("aident");
46         auto_ptr_XMLCh rid("rident");
47         auto_ptr_XMLCh method("method");
48         auto_ptr_XMLCh nameid("John Doe");
49         
50         NameIdentifier* n=NameIdentifierBuilder::buildNameIdentifier();
51         n->setName(nameid.get());        
52         Subject* subject=SubjectBuilder::buildSubject();
53         subject->setNameIdentifier(n);
54
55         AuthenticationStatement* statement=AuthenticationStatementBuilder::buildAuthenticationStatement();
56         statement->setAuthenticationInstant(issueInstant.get());
57         statement->setAuthenticationMethod(method.get());
58         statement->setSubject(subject);
59         
60         Assertion* assertion=AssertionBuilder::buildAssertion();
61         assertion->setAssertionID(aid.get());
62         assertion->setIssueInstant(issueInstant.get());
63         assertion->setIssuer(issuer.get());
64         assertion->getAuthenticationStatements().push_back(statement);
65
66         // Append a Signature.
67         assertion->setSignature(SignatureBuilder::buildSignature());
68
69         // Sign assertion while marshalling.
70         vector<Signature*> sigs(1,assertion->getSignature());
71         CredentialCriteria cc;
72         cc.setUsage(Credential::SIGNING_CREDENTIAL);
73         Locker locker(m_resolver);
74         const Credential* cred = m_resolver->resolve(&cc);
75         TSM_ASSERT("Retrieved credential was null", cred!=nullptr);
76
77         DOMElement* rootElement = nullptr;
78         try {
79             rootElement=assertion->marshall((DOMDocument*)nullptr,&sigs,cred);
80         }
81         catch (XMLToolingException& e) {
82             TS_TRACE(e.what());
83             delete assertion;
84             throw;
85         }
86
87         StatusCode* sc=StatusCodeBuilder::buildStatusCode();
88         sc->setValue(&StatusCode::SUCCESS);
89         Status* status=StatusBuilder::buildStatus();
90         status->setStatusCode(sc);
91         sc = StatusCodeBuilder::buildStatusCode();
92         xmltooling::QName subcode("urn:mace:shibboleth", "NoReally", "shib");
93         sc->setValue(&subcode);
94         status->getStatusCode()->setStatusCode(sc);
95
96         auto_ptr<Response> response(ResponseBuilder::buildResponse());
97         response->setResponseID(rid.get());
98         response->setIssueInstant(issueInstant.get());
99         response->setStatus(status);
100         response->getAssertions().push_back(assertion);
101         response->setSignature(SignatureBuilder::buildSignature());
102
103         // Sign response while marshalling.
104         sigs.clear();
105         sigs.push_back(response->getSignature());
106         rootElement = nullptr;
107         try {
108             rootElement=response->marshall((DOMDocument*)nullptr,&sigs,cred);
109         }
110         catch (XMLToolingException& e) {
111             TS_TRACE(e.what());
112             throw;
113         }
114
115         string buf;
116         XMLHelper::serialize(rootElement, buf);
117         istringstream in(buf);
118         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
119         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
120         
121         auto_ptr<XMLObject> response2(b->buildFromDocument(doc));
122         assertEquals("Unmarshalled response does not match", expectedChildElementsDOM, response2.get(), false);
123
124         auto_ptr<Response> response3(dynamic_cast<Response*>(response2.get())->cloneResponse());
125         
126         try {
127             opensaml::SignatureProfileValidator spv;
128             spv.validate(dynamic_cast<Response*>(response3.get())->getAssertions().front()->getSignature());
129             spv.validate(dynamic_cast<Response*>(response3.get())->getSignature());
130
131             SignatureValidator sv(cred);
132             sv.validate(dynamic_cast<Response*>(response3.get())->getAssertions().front()->getSignature());
133             sv.validate(dynamic_cast<Response*>(response3.get())->getSignature());
134         }
135         catch (XMLToolingException& e) {
136             TS_TRACE(e.what());
137             throw;
138         }
139     }
140
141 };