Set fourth file version digit to signify rebuild.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / EncryptionTest.h
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #include "XMLObjectBaseTestCase.h"
22
23 #include <xmltooling/encryption/Decrypter.h>
24 #include <xmltooling/encryption/Encrypter.h>
25 #include <xmltooling/encryption/Encryption.h>
26 #include <xmltooling/security/Credential.h>
27 #include <xmltooling/security/CredentialCriteria.h>
28 #include <xmltooling/security/CredentialResolver.h>
29
30 #include <fstream>
31 #include <xercesc/util/XMLUniDefs.hpp>
32 #include <xsec/dsig/DSIGReference.hpp>
33
34 using namespace xmlencryption;
35
36 class EncryptionTest : public CxxTest::TestSuite {
37     CredentialResolver* m_resolver;
38 public:
39     void setUp() {
40         m_resolver=nullptr;
41         string config = data_path + "FilesystemCredentialResolver.xml";
42         ifstream in(config.c_str());
43         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
44         XercesJanitor<DOMDocument> janitor(doc);
45         m_resolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(
46             CHAINING_CREDENTIAL_RESOLVER,doc->getDocumentElement()
47             );
48         XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
49     }
50
51     void tearDown() {
52         XMLObjectBuilder::deregisterDefaultBuilder();
53         delete m_resolver;
54     }
55
56     void testEncryption() {
57         string path=data_path + "ComplexXMLObject.xml";
58         ifstream fs(path.c_str());
59         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
60         TS_ASSERT(doc!=nullptr);
61
62         try {
63             CredentialCriteria cc;
64             cc.setUsage(Credential::ENCRYPTION_CREDENTIAL);
65             Locker locker(m_resolver);
66             const Credential* cred=m_resolver->resolve(&cc);
67             TSM_ASSERT("Retrieved credential was null", cred!=nullptr);
68
69             Encrypter encrypter;
70             Encrypter::EncryptionParams ep;
71             Encrypter::KeyEncryptionParams kep(*cred);
72             auto_ptr<EncryptedData> encData(encrypter.encryptElement(doc->getDocumentElement(),ep,&kep));
73
74             string buf;
75             XMLHelper::serialize(encData->marshall(), buf);
76             //TS_TRACE(buf.c_str());
77             istringstream is(buf);
78             DOMDocument* doc2=XMLToolingConfig::getConfig().getValidatingParser().parse(is);
79             auto_ptr<EncryptedData> encData2(
80                 dynamic_cast<EncryptedData*>(XMLObjectBuilder::buildOneFromElement(doc2->getDocumentElement(),true))
81                 );
82
83             Decrypter decrypter(m_resolver);
84             DOMDocumentFragment* frag = decrypter.decryptData(*encData2.get());
85             XMLHelper::serialize(static_cast<DOMElement*>(frag->getFirstChild()), buf);
86             //TS_TRACE(buf.c_str());
87             TS_ASSERT(doc->getDocumentElement()->isEqualNode(frag->getFirstChild()));
88             frag->release();
89             doc->release();
90         }
91         catch (XMLToolingException& e) {
92             TS_TRACE(e.what());
93             doc->release();
94             throw;
95         }
96     }
97
98 };