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