https://issues.shibboleth.net/jira/browse/CPPXT-77
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / KeyInfoTest.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/signature/KeyInfo.h>
21 #include <xmltooling/validation/ValidatorSuite.h>
22
23 using namespace xmlsignature;
24
25 class KeyInfoTest : public CxxTest::TestSuite {
26 public:
27     KeyInfoTest() {}
28
29     void setUp() {
30         XMLObjectBuilder::registerDefaultBuilder(new AnyElementBuilder());
31     }
32
33     void tearDown() {
34         XMLObjectBuilder::deregisterDefaultBuilder();
35     }
36
37     void testKeyInfo1() {
38         string path=data_path + "KeyInfo1.xml";
39         ifstream fs(path.c_str());
40         DOMDocument* doc=XMLToolingConfig::getConfig().getValidatingParser().parse(fs);
41         TS_ASSERT(doc!=nullptr);
42
43         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
44         TS_ASSERT(b!=nullptr);
45
46         auto_ptr<KeyInfo> kiObject(dynamic_cast<KeyInfo*>(b->buildFromDocument(doc)));
47         TS_ASSERT(kiObject.get()!=nullptr);
48         TSM_ASSERT_EQUALS("Number of child elements was not expected value",
49             4, kiObject->getOrderedChildren().size());
50         TSM_ASSERT_EQUALS("Number of child elements was not expected value",
51             1, kiObject->getKeyValues().size());
52         TSM_ASSERT_EQUALS("Number of child elements was not expected value",
53             1, kiObject->getX509Datas().front()->getX509Certificates().size());
54
55         auto_ptr_XMLCh expected("Public Key for CN=xmldap.org, OU=Domain Control Validated, O=xmldap.org");
56         TSM_ASSERT("KeyName was not expected value", XMLString::equals(expected.get(), kiObject->getKeyNames().front()->getName()));
57
58         SchemaValidators.validate(kiObject.get());
59     }
60
61     void testKeyInfo2() {
62         string path=data_path + "KeyInfo2.xml";
63         ifstream fs(path.c_str());
64         DOMDocument* doc=XMLToolingConfig::getConfig().getValidatingParser().parse(fs);
65         TS_ASSERT(doc!=nullptr);
66
67         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
68         TS_ASSERT(b!=nullptr);
69
70         auto_ptr<KeyInfo> kiObject(dynamic_cast<KeyInfo*>(b->buildFromDocument(doc)));
71         TS_ASSERT(kiObject.get()!=nullptr);
72         TSM_ASSERT_EQUALS("Number of child elements was not expected value",
73             2, kiObject->getOrderedChildren().size());
74         TSM_ASSERT_EQUALS("Number of child elements was not expected value",
75             1, kiObject->getRetrievalMethods().size());
76         TSM_ASSERT_EQUALS("Number of child elements was not expected value",
77             2, kiObject->getSPKIDatas().front()->getSPKISexps().size());
78
79         SchemaValidators.validate(kiObject.get());
80     }
81
82     void testKeyInfo3() {
83         string path=data_path + "KeyInfo3.xml";
84         ifstream fs(path.c_str());
85         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
86         TS_ASSERT(doc!=nullptr);
87
88         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
89         TS_ASSERT(b!=nullptr);
90
91         auto_ptr<KeyInfo> kiObject(dynamic_cast<KeyInfo*>(b->buildFromDocument(doc)));
92         TS_ASSERT(kiObject.get()!=nullptr);
93         TS_ASSERT_THROWS(SchemaValidators.validate(kiObject.get()),ValidationException);
94     }
95
96     void testKeyInfo4() {
97         string path=data_path + "KeyInfo4.xml";
98         ifstream fs(path.c_str());
99         DOMDocument* doc=XMLToolingConfig::getConfig().getValidatingParser().parse(fs);
100         TS_ASSERT(doc!=nullptr);
101
102         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
103         TS_ASSERT(b!=nullptr);
104
105         auto_ptr<KeyInfo> kiObject(dynamic_cast<KeyInfo*>(b->buildFromDocument(doc)));
106         TS_ASSERT(kiObject.get()!=nullptr);
107         TSM_ASSERT_EQUALS("Number of child elements was not expected value",
108             1, kiObject->getKeyValues().size());
109         ECKeyValue* kv = kiObject->getKeyValues().front()->getECKeyValue();
110         TSM_ASSERT("Missing ECKeyValue", kv!=nullptr);
111         TSM_ASSERT("Missing NamedCurve", kv->getNamedCurve()!=nullptr);
112         TSM_ASSERT("Missing PublicKey", kv->getPublicKey()!=nullptr);
113
114         SchemaValidators.validate(kiObject.get());
115     }
116 };