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