Adjust cxxtest path in build rules.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / InlineKeyResolverTest.h
1 /*
2  *  Copyright 2001-2010 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 <fstream>
20 #include <xmltooling/security/X509Credential.h>
21 #include <xmltooling/security/KeyInfoResolver.h>
22 #include <xmltooling/signature/KeyInfo.h>
23 #include <xsec/enc/XSECCryptoKey.hpp>
24
25 using namespace xmlsignature;
26
27 class InlineKeyResolverTest : public CxxTest::TestSuite {
28     KeyInfoResolver* m_resolver;
29 public:
30     InlineKeyResolverTest() : m_resolver(nullptr) {}
31
32     void setUp() {
33         string config = data_path + "InlineKeyResolver.xml";
34         ifstream in(config.c_str());
35         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
36         XercesJanitor<DOMDocument> janitor(doc);
37         m_resolver=XMLToolingConfig::getConfig().KeyInfoResolverManager.newPlugin(INLINE_KEYINFO_RESOLVER,doc->getDocumentElement());
38     }
39
40     void tearDown() {
41         delete m_resolver;
42         m_resolver=nullptr;
43     }
44
45     void testResolver() {
46         string path=data_path + "KeyInfo1.xml";
47         ifstream fs(path.c_str());
48         DOMDocument* doc=XMLToolingConfig::getConfig().getValidatingParser().parse(fs);
49         TS_ASSERT(doc!=nullptr);
50         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
51         TS_ASSERT(b!=nullptr);
52         auto_ptr<KeyInfo> kiObject(dynamic_cast<KeyInfo*>(b->buildFromDocument(doc)));
53         TS_ASSERT(kiObject.get()!=nullptr);
54
55         auto_ptr<X509Credential> cred(dynamic_cast<X509Credential*>(m_resolver->resolve(kiObject.get())));
56         TSM_ASSERT("Unable to resolve KeyInfo into Credential.", cred.get()!=nullptr);
57
58         TSM_ASSERT("Unable to resolve public key.", cred->getPublicKey()!=nullptr);
59         TSM_ASSERT_EQUALS("Unexpected key type.", cred->getPublicKey()->getKeyType(), XSECCryptoKey::KEY_RSA_PUBLIC);
60         TSM_ASSERT_EQUALS("Wrong certificate count.", cred->getEntityCertificateChain().size(), 1);
61         TSM_ASSERT_EQUALS("Wrong CRL count.", cred->getCRLs().size(), 3);
62     }
63
64     void testDER() {
65         string path=data_path + "KeyInfo5.xml";
66         ifstream fs(path.c_str());
67         DOMDocument* doc=XMLToolingConfig::getConfig().getValidatingParser().parse(fs);
68         TS_ASSERT(doc!=nullptr);
69         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
70         TS_ASSERT(b!=nullptr);
71         auto_ptr<KeyInfo> kiObject(dynamic_cast<KeyInfo*>(b->buildFromDocument(doc)));
72         TS_ASSERT(kiObject.get()!=nullptr);
73
74         auto_ptr<X509Credential> cred(dynamic_cast<X509Credential*>(m_resolver->resolve(kiObject.get())));
75         TSM_ASSERT("Unable to resolve KeyInfo into Credential.", cred.get()!=nullptr);
76
77         TSM_ASSERT("Unable to resolve public key.", cred->getPublicKey()!=nullptr);
78         TSM_ASSERT_EQUALS("Unexpected key type.", cred->getPublicKey()->getKeyType(), XSECCryptoKey::KEY_RSA_PUBLIC);
79         TSM_ASSERT_EQUALS("Wrong certificate count.", cred->getEntityCertificateChain().size(), 0);
80         TSM_ASSERT_EQUALS("Wrong CRL count.", cred->getCRLs().size(), 0);
81     }
82 };