Completed SAML 1 assertion schema, started test cases.
[shibboleth/cpp-opensaml.git] / samltest / internal.h
1 /*
2  *  Copyright 2001-2006 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 <cxxtest/TestSuite.h>
18
19 #include <fstream>
20 #include <saml/exceptions.h>
21 #include <saml/SAMLConfig.h>
22 #include <saml/util/SAMLConstants.h>
23 #include <xmltooling/XMLObject.h>
24 #include <xmltooling/XMLObjectBuilder.h>
25
26 using namespace opensaml;
27 using namespace xmltooling;
28 using namespace std;
29
30 extern string data_path;
31
32 class SAMLObjectBaseTestCase
33 {
34 protected:
35     /** Location of file containing a single element with NO optional attributes */
36     string singleElementFile;
37
38     /** Location of file containing a single element with all optional attributes */
39     string singleElementOptionalAttributesFile;
40
41     /** Location of file containing a single element with child elements */
42     string childElementsFile;
43
44     /** The expected result of a marshalled single element with no optional attributes */
45     DOMDocument* expectedDOM;
46
47     /** The expected result of a marshalled single element with all optional attributes */
48     DOMDocument* expectedOptionalAttributesDOM;
49
50     /** The expected result of a marshalled single element with child elements */
51     DOMDocument* expectedChildElementsDOM;
52
53     /**
54      * Unmarshalls an element file into its SAML XMLObject.
55      * 
56      * @return the SAML XMLObject from the file
57      */
58     XMLObject* unmarshallElement(string elementFile) {
59         try {
60             ParserPool& p=XMLToolingConfig::getConfig().getParser();
61             ifstream fs(elementFile.c_str());
62             DOMDocument* doc = p.parse(fs);
63             const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
64             return b->buildFromDocument(doc);
65         }
66         catch (XMLToolingException& e) {
67             TS_TRACE(e.what());
68             throw;
69         }
70     }
71
72     void assertEquals(const char* failMessage, DOMDocument* expectedDOM, XMLObject* xmlObject) {
73         DOMElement* generatedDOM = xmlObject->marshall();
74         TSM_ASSERT(failMessage,generatedDOM->isEqualNode(expectedDOM->getDocumentElement()));
75     }
76
77     void assertEquals(DOMDocument* expectedDOM, XMLObject* xmlObject) {
78         assertEquals("Marshalled DOM was not the same as the expected DOM", expectedDOM, xmlObject);
79     }
80
81 public:
82     void setUp() {
83         ParserPool& p=XMLToolingConfig::getConfig().getParser();
84         if (!singleElementFile.empty()) {
85             ifstream fs(singleElementFile.c_str());
86             expectedDOM = p.parse(fs);
87         }
88
89         if (!singleElementOptionalAttributesFile.empty()) {
90             ifstream fs(singleElementOptionalAttributesFile.c_str());
91             expectedOptionalAttributesDOM = p.parse(fs);
92         }
93
94         if (!childElementsFile.empty()) {
95             ifstream fs(childElementsFile.c_str());
96             expectedChildElementsDOM = p.parse(fs);
97         }
98     }
99     
100     void tearDown() {
101         if (expectedDOM) expectedDOM->release();
102         if (expectedOptionalAttributesDOM) expectedOptionalAttributesDOM->release();
103         if (expectedChildElementsDOM) expectedChildElementsDOM->release();
104     }
105 };