38fec27db63b10e9f962063d09f61f440e8e132a
[shibboleth/cpp-opensaml.git] / samltest / signature / SAML2AssertionTest.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/saml2/core/Assertions.h>
19
20 #include <fstream>
21
22 using namespace opensaml::saml2;
23
24 class SAML2AssertionTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
25 public:
26     void setUp() {
27         childElementsFile  = data_path + "signature/SAML2Assertion.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         Issuer* is=IssuerBuilder::buildIssuer();
43         is->setName(issuer.get());
44
45         NameID* n=NameIDBuilder::buildNameID();
46         n->setName(nameid.get());        
47         Subject* subject=SubjectBuilder::buildSubject();
48         subject->setNameID(n);
49
50         AuthnStatement* statement=AuthnStatementBuilder::buildAuthnStatement();
51         statement->setAuthnInstant(issueInstant.get());
52
53         AuthnContext* ac=AuthnContextBuilder::buildAuthnContext();
54         AuthnContextClassRef* acc=AuthnContextClassRefBuilder::buildAuthnContextClassRef();
55         acc->setReference(method.get());
56         ac->setAuthnContextClassRef(acc);
57         statement->setAuthnContext(ac);
58         
59         auto_ptr<Assertion> assertion(AssertionBuilder::buildAssertion());
60         assertion->setID(id.get());
61         assertion->setIssueInstant(issueInstant.get());
62         assertion->setIssuer(is);
63         assertion->setSubject(subject);
64         assertion->getAuthnStatements().push_back(statement);
65
66         // Append a Signature.
67         Signature* sig=SignatureBuilder::buildSignature();
68         assertion->setSignature(sig);
69
70         // Sign while marshalling.
71         vector<Signature*> sigs(1,sig);
72         CredentialCriteria cc;
73         cc.setUsage(Credential::SIGNING_CREDENTIAL);
74         Locker locker(m_resolver);
75         const Credential* cred = m_resolver->resolve(&cc);
76         TSM_ASSERT("Retrieved credential was null", cred!=NULL);
77
78         DOMElement* rootElement = NULL;
79         try {
80             rootElement=assertion->marshall((DOMDocument*)NULL,&sigs,cred);
81         }
82         catch (XMLToolingException& e) {
83             TS_TRACE(e.what());
84             throw;
85         }
86         
87         string buf;
88         XMLHelper::serialize(rootElement, buf);
89         istringstream in(buf);
90         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
91         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
92         
93         auto_ptr<XMLObject> assertion2(b->buildFromDocument(doc));
94         assertEquals("Unmarshalled assertion does not match", expectedChildElementsDOM, assertion2.get(), false);
95         
96         try {
97             opensaml::SignatureProfileValidator spv;
98             SignatureValidator sv(cred);
99             spv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
100             sv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
101         }
102         catch (XMLToolingException& e) {
103             TS_TRACE(e.what());
104             throw;
105         }
106     }
107
108 };