2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "signature/SAMLSignatureTestBase.h"
18 #include <saml/saml2/core/Assertions.h>
22 using namespace opensaml::saml2;
24 class SAML2AssertionTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
27 childElementsFile = data_path + "signature/SAML2Assertion.xml";
28 SAMLSignatureTestBase::setUp();
32 SAMLSignatureTestBase::tearDown();
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");
42 Issuer* is=IssuerBuilder::buildIssuer();
43 is->setName(issuer.get());
45 NameID* n=NameIDBuilder::buildNameID();
46 n->setName(nameid.get());
47 Subject* subject=SubjectBuilder::buildSubject();
48 subject->setNameID(n);
50 AuthnStatement* statement=AuthnStatementBuilder::buildAuthnStatement();
51 statement->setAuthnInstant(issueInstant.get());
53 AuthnContext* ac=AuthnContextBuilder::buildAuthnContext();
54 AuthnContextClassRef* acc=AuthnContextClassRefBuilder::buildAuthnContextClassRef();
55 acc->setReference(method.get());
56 ac->setAuthnContextClassRef(acc);
57 statement->setAuthnContext(ac);
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);
66 // Append a Signature.
67 Signature* sig=SignatureBuilder::buildSignature();
68 assertion->setSignature(sig);
70 // Sign while marshalling.
71 vector<Signature*> sigs(1,sig);
72 CredentialCriteria cc;
73 cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
74 Locker locker(m_resolver);
75 const Credential* cred = m_resolver->resolve(&cc);
76 TSM_ASSERT("Retrieved credential was null", cred!=NULL);
78 DOMElement* rootElement = NULL;
80 rootElement=assertion->marshall((DOMDocument*)NULL,&sigs,cred);
82 catch (XMLToolingException& e) {
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());
93 auto_ptr<XMLObject> assertion2(b->buildFromDocument(doc));
94 assertEquals("Unmarshalled assertion does not match", expectedChildElementsDOM, assertion2.get(), false);
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());
102 catch (XMLToolingException& e) {