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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "signature/SAMLSignatureTestBase.h"
22 #include <saml/saml2/core/Assertions.h>
26 using namespace opensaml::saml2;
28 class SAML2AssertionTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
31 childElementsFile = data_path + "signature/SAML2Assertion.xml";
32 SAMLSignatureTestBase::setUp();
36 SAMLSignatureTestBase::tearDown();
39 void testSignature() {
40 auto_ptr_XMLCh issuer("issuer");
41 auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
42 auto_ptr_XMLCh id("ident");
43 auto_ptr_XMLCh method("method");
44 auto_ptr_XMLCh nameid("John Doe");
46 Issuer* is=IssuerBuilder::buildIssuer();
47 is->setName(issuer.get());
49 NameID* n=NameIDBuilder::buildNameID();
50 n->setName(nameid.get());
51 Subject* subject=SubjectBuilder::buildSubject();
52 subject->setNameID(n);
54 AuthnStatement* statement=AuthnStatementBuilder::buildAuthnStatement();
55 statement->setAuthnInstant(issueInstant.get());
57 AuthnContext* ac=AuthnContextBuilder::buildAuthnContext();
58 AuthnContextClassRef* acc=AuthnContextClassRefBuilder::buildAuthnContextClassRef();
59 acc->setReference(method.get());
60 ac->setAuthnContextClassRef(acc);
61 statement->setAuthnContext(ac);
63 auto_ptr<Assertion> assertion(AssertionBuilder::buildAssertion());
64 assertion->setID(id.get());
65 assertion->setIssueInstant(issueInstant.get());
66 assertion->setIssuer(is);
67 assertion->setSubject(subject);
68 assertion->getAuthnStatements().push_back(statement);
70 // Append a Signature.
71 Signature* sig=SignatureBuilder::buildSignature();
72 assertion->setSignature(sig);
74 // Sign while marshalling.
75 vector<Signature*> sigs(1,sig);
76 CredentialCriteria cc;
77 cc.setUsage(Credential::SIGNING_CREDENTIAL);
78 Locker locker(m_resolver);
79 const Credential* cred = m_resolver->resolve(&cc);
80 TSM_ASSERT("Retrieved credential was null", cred!=nullptr);
82 DOMElement* rootElement = nullptr;
84 rootElement=assertion->marshall((DOMDocument*)nullptr,&sigs,cred);
86 catch (XMLToolingException& e) {
92 XMLHelper::serialize(rootElement, buf);
93 istringstream in(buf);
94 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
95 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
97 auto_ptr<XMLObject> assertion2(b->buildFromDocument(doc));
98 assertEquals("Unmarshalled assertion does not match", expectedChildElementsDOM, assertion2.get(), false);
101 opensaml::SignatureProfileValidator spv;
102 SignatureValidator sv(cred);
103 spv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
104 sv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
106 catch (XMLToolingException& e) {