1edf109807df005ed6c4a1ad8da45a8287281235
[shibboleth/cpp-opensaml.git] / samltest / signature / SAML1AssertionTest.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 #include <saml/saml1/core/Assertions.h>
19
20 #include <fstream>
21
22 using namespace opensaml::saml1;
23
24 class SAML1AssertionTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
25 public:
26     void setUp() {
27         childElementsFile  = data_path + "signature/SAML1Assertion.xml";
28         SAMLSignatureTestBase::setUp();
29     }
30
31     void tearDown() {
32         SAMLSignatureTestBase::tearDown();
33     }
34
35     void testSignature() {
36         auto_ptr_XMLCh issuer("issuer");
37         auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
38         auto_ptr_XMLCh id("ident");
39         auto_ptr_XMLCh method("method");
40         auto_ptr_XMLCh nameid("John Doe");
41         
42         NameIdentifier* n=NameIdentifierBuilder::buildNameIdentifier();
43         n->setName(nameid.get());        
44         Subject* subject=SubjectBuilder::buildSubject();
45         subject->setNameIdentifier(n);
46
47         AuthenticationStatement* statement=AuthenticationStatementBuilder::buildAuthenticationStatement();
48         statement->setAuthenticationInstant(issueInstant.get());
49         statement->setAuthenticationMethod(method.get());
50         statement->setSubject(subject);
51         
52         auto_ptr<Assertion> assertion(AssertionBuilder::buildAssertion());
53         assertion->setAssertionID(id.get());
54         assertion->setIssueInstant(issueInstant.get());
55         assertion->setIssuer(issuer.get());
56         assertion->getAuthenticationStatements().push_back(statement);
57
58         // Append a Signature.
59         Signature* sig=SignatureBuilder::buildSignature();
60         assertion->setSignature(sig);
61
62         // Sign while marshalling.
63         vector<Signature*> sigs(1,sig);
64         CredentialCriteria cc;
65         cc.setUsage(Credential::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=assertion->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> assertion2(b->buildFromDocument(doc));
86         assertEquals("Unmarshalled assertion does not match", expectedChildElementsDOM, assertion2.get(), false);
87         
88         try {
89             opensaml::SignatureProfileValidator spv;
90             SignatureValidator sv(cred);
91             spv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
92             sv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
93         }
94         catch (XMLToolingException& e) {
95             TS_TRACE(e.what());
96             throw;
97         }
98     }
99
100 };