Xerces 3 revisions.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / KeyInfoTest.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 <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!=NULL);
42
43         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
44         TS_ASSERT(b!=NULL);
45
46         auto_ptr<KeyInfo> kiObject(dynamic_cast<KeyInfo*>(b->buildFromDocument(doc)));
47         TS_ASSERT(kiObject.get()!=NULL);
48         TSM_ASSERT_EQUALS("Number of child elements was not expected value",
49             3, 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!=NULL);
66
67         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
68         TS_ASSERT(b!=NULL);
69
70         auto_ptr<KeyInfo> kiObject(dynamic_cast<KeyInfo*>(b->buildFromDocument(doc)));
71         TS_ASSERT(kiObject.get()!=NULL);
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!=NULL);
87
88         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
89         TS_ASSERT(b!=NULL);
90
91         auto_ptr<KeyInfo> kiObject(dynamic_cast<KeyInfo*>(b->buildFromDocument(doc)));
92         TS_ASSERT(kiObject.get()!=NULL);
93         TS_ASSERT_THROWS(SchemaValidators.validate(kiObject.get()),ValidationException);
94     }
95 };