5f0796718052425e88264ce7c3fdb1cebfea1f2a
[shibboleth/cpp-opensaml.git] / samltest / signature / SAML1ResponseTest.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 "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         Locker locker(m_resolver);
65         assertion->getSignature()->setSigningKey(m_resolver->getKey());
66
67         // Build KeyInfo.
68         KeyInfo* keyInfo=KeyInfoBuilder::buildKeyInfo();
69         X509Data* x509Data=X509DataBuilder::buildX509Data();
70         keyInfo->getX509Datas().push_back(x509Data);
71         for_each(m_resolver->getCertificates().begin(),m_resolver->getCertificates().end(),bind1st(_addcert(),x509Data));
72         assertion->getSignature()->setKeyInfo(keyInfo);
73
74         // Sign assertion while marshalling.
75         vector<Signature*> sigs(1,assertion->getSignature());
76         DOMElement* rootElement = NULL;
77         try {
78             rootElement=assertion->marshall((DOMDocument*)NULL,&sigs);
79         }
80         catch (XMLToolingException& e) {
81             TS_TRACE(e.what());
82             delete assertion;
83             throw;
84         }
85
86         StatusCode* sc=StatusCodeBuilder::buildStatusCode();
87         sc->setValue(&StatusCode::SUCCESS);
88         Status* status=StatusBuilder::buildStatus();
89         status->setStatusCode(sc);
90
91         auto_ptr<Response> response(ResponseBuilder::buildResponse());
92         response->setResponseID(rid.get());
93         response->setIssueInstant(issueInstant.get());
94         response->setStatus(status);
95         response->getAssertions().push_back(assertion);
96         response->setSignature(SignatureBuilder::buildSignature());
97         response->getSignature()->setSigningKey(m_resolver->getKey());
98         response->getSignature()->setKeyInfo(keyInfo->cloneKeyInfo());
99
100         // Sign response while marshalling.
101         sigs.clear();
102         sigs.push_back(response->getSignature());
103         rootElement = NULL;
104         try {
105             rootElement=response->marshall((DOMDocument*)NULL,&sigs);
106         }
107         catch (XMLToolingException& e) {
108             TS_TRACE(e.what());
109             throw;
110         }
111
112         string buf;
113         XMLHelper::serialize(rootElement, buf);
114         istringstream in(buf);
115         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
116         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
117         
118         auto_ptr<XMLObject> response2(b->buildFromDocument(doc));
119         assertEquals("Unmarshalled response does not match", expectedChildElementsDOM, response2.get(), false);
120         
121         try {
122             opensaml::SignatureProfileValidator spv;
123             spv.validate(dynamic_cast<Response*>(response2.get())->getAssertions().front()->getSignature());
124             spv.validate(dynamic_cast<Response*>(response2.get())->getSignature());
125
126             SignatureValidator sv(new KeyResolver(m_resolver->getKey()));
127             sv.validate(dynamic_cast<Response*>(response2.get())->getAssertions().front()->getSignature());
128             sv.validate(dynamic_cast<Response*>(response2.get())->getSignature());
129         }
130         catch (XMLToolingException& e) {
131             TS_TRACE(e.what());
132             throw;
133         }
134     }
135
136 };