Major revamp of credential and trust handling code, PKIX engine still needs work.
[shibboleth/cpp-opensaml.git] / samltest / signature / SAML1RequestTest.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 SAML1RequestTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
28 public:
29     void setUp() {
30         childElementsFile  = data_path + "signature/SAML1Request.xml";
31         SAMLSignatureTestBase::setUp();
32     }
33
34     void tearDown() {
35         SAMLSignatureTestBase::tearDown();
36     }
37
38     void testSignature() {
39         auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
40         auto_ptr_XMLCh id("ident");
41         auto_ptr_XMLCh method("method");
42         auto_ptr_XMLCh nameid("John Doe");
43         
44         NameIdentifier* n=NameIdentifierBuilder::buildNameIdentifier();
45         n->setName(nameid.get());        
46         Subject* subject=SubjectBuilder::buildSubject();
47         subject->setNameIdentifier(n);
48
49         AuthenticationQuery* query=AuthenticationQueryBuilder::buildAuthenticationQuery();
50         query->setAuthenticationMethod(method.get());
51         query->setSubject(subject);
52         
53         auto_ptr<Request> request(RequestBuilder::buildRequest());
54         request->setRequestID(id.get());
55         request->setIssueInstant(issueInstant.get());
56         request->setAuthenticationQuery(query);
57
58         // Append a Signature.
59         Signature* sig=SignatureBuilder::buildSignature();
60         request->setSignature(sig);
61
62         // Sign while marshalling.
63         vector<Signature*> sigs(1,sig);
64         CredentialCriteria cc;
65         cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
66         Locker locker(m_resolver);
67         const Credential* cred = m_resolver->resolve(&cc);
68         TSM_ASSERT("Retrieved credential was null", cred!=NULL);
69
70         DOMElement* rootElement = NULL;
71         try {
72             rootElement=request->marshall((DOMDocument*)NULL,&sigs,cred);
73         }
74         catch (XMLToolingException& e) {
75             TS_TRACE(e.what());
76             throw;
77         }
78         
79         string buf;
80         XMLHelper::serialize(rootElement, buf);
81         istringstream in(buf);
82         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
83         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
84
85         auto_ptr<XMLObject> request2(b->buildFromDocument(doc));
86         assertEquals("Unmarshalled request does not match", expectedChildElementsDOM, request2.get(), false);
87         
88         try {
89             opensaml::SignatureProfileValidator spv;
90             SignatureValidator sv(cred);
91             spv.validate(dynamic_cast<Request*>(request2.get())->getSignature());
92             sv.validate(dynamic_cast<Request*>(request2.get())->getSignature());
93         }
94         catch (XMLToolingException& e) {
95             TS_TRACE(e.what());
96             throw;
97         }
98     }
99
100 };