14fa1a91a47fc7fd4f2aceed857bd0fc239f868d
[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         Locker locker(m_resolver);
70         sig->setSigningKey(m_resolver->getKey());
71
72         // Build KeyInfo.
73         KeyInfo* keyInfo=KeyInfoBuilder::buildKeyInfo();
74         X509Data* x509Data=X509DataBuilder::buildX509Data();
75         keyInfo->getX509Datas().push_back(x509Data);
76         for_each(m_resolver->getCertificates().begin(),m_resolver->getCertificates().end(),bind1st(_addcert(),x509Data));
77         sig->setKeyInfo(keyInfo);
78
79         // Sign while marshalling.
80         vector<Signature*> sigs(1,sig);
81         DOMElement* rootElement = NULL;
82         try {
83             rootElement=assertion->marshall((DOMDocument*)NULL,&sigs);
84         }
85         catch (XMLToolingException& e) {
86             TS_TRACE(e.what());
87             throw;
88         }
89         
90         string buf;
91         XMLHelper::serialize(rootElement, buf);
92         istringstream in(buf);
93         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
94         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
95         
96         auto_ptr<XMLObject> assertion2(b->buildFromDocument(doc));
97         assertEquals("Unmarshalled assertion does not match", expectedChildElementsDOM, assertion2.get(), false);
98         
99         try {
100             opensaml::SignatureProfileValidator spv;
101             SignatureValidator sv(new KeyResolver(m_resolver->getKey()));
102             spv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
103             sv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
104         }
105         catch (XMLToolingException& e) {
106             TS_TRACE(e.what());
107             throw;
108         }
109     }
110
111 };