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