Convert from NULL macro to nullptr, remove unused zlib code.
[shibboleth/cpp-opensaml.git] / samltest / internal.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 #ifdef WIN32
18 # define _CRT_SECURE_NO_DEPRECATE 1
19 # define _CRT_NONSTDC_NO_DEPRECATE 1
20 #endif
21
22 #include <cxxtest/TestSuite.h>
23
24 #include <saml/exceptions.h>
25 #include <saml/util/SAMLConstants.h>
26
27 #include <fstream>
28 #include <xmltooling/XMLObject.h>
29 #include <xmltooling/XMLObjectBuilder.h>
30 #include <xmltooling/XMLToolingConfig.h>
31 #include <xmltooling/util/DateTime.h>
32 #include <xmltooling/util/ParserPool.h>
33 #include <xmltooling/validation/Validator.h>
34
35 using namespace xmltooling;
36 using namespace xercesc;
37 using namespace std;
38
39 extern string data_path;
40
41 class SAMLObjectBaseTestCase
42 {
43 protected:
44     /** Location of file containing a single element with NO optional attributes */
45     string singleElementFile;
46
47     /** Location of file containing a single element with all optional attributes */
48     string singleElementOptionalAttributesFile;
49
50     /** Location of file containing a single element with child elements */
51     string childElementsFile;
52
53     /** The expected result of a marshalled single element with no optional attributes */
54     DOMDocument* expectedDOM;
55
56     /** The expected result of a marshalled single element with all optional attributes */
57     DOMDocument* expectedOptionalAttributesDOM;
58
59     /** The expected result of a marshalled single element with child elements */
60     DOMDocument* expectedChildElementsDOM;
61
62     /**
63      * Unmarshalls an element file into its SAML XMLObject.
64      * 
65      * @return the SAML XMLObject from the file
66      */
67     XMLObject* unmarshallElement(string elementFile) {
68         try {
69             ParserPool& p=XMLToolingConfig::getConfig().getParser();
70             ifstream fs(elementFile.c_str());
71             DOMDocument* doc = p.parse(fs);
72             const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
73             return b->buildFromDocument(doc);
74         }
75         catch (XMLToolingException& e) {
76             TS_TRACE(typeid(e).name());
77             TS_TRACE(e.what());
78             throw;
79         }
80     }
81
82     void assertEquals(const char* failMessage, DOMDocument* expectedDOM, XMLObject* xmlObject, bool canMarshall=true) {
83         DOMElement* generatedDOM = xmlObject->getDOM();
84         if (!generatedDOM) {
85             if (!canMarshall) {
86                 TSM_ASSERT("DOM not available", false);
87             }
88             else {
89                 generatedDOM = xmlObject->marshall();
90             }
91         }
92         if (!generatedDOM->isEqualNode(expectedDOM->getDocumentElement())) {
93             string buf;
94             XMLHelper::serialize(generatedDOM, buf);
95             TS_TRACE(buf.c_str());
96             buf.erase();
97             XMLHelper::serialize(expectedDOM->getDocumentElement(), buf);
98             TS_TRACE(buf.c_str());
99             TSM_ASSERT(failMessage, false);
100         }
101     }
102
103     void assertEquals(DOMDocument* expectedDOM, XMLObject* xmlObject, bool canMarshall=true) {
104         assertEquals("Marshalled DOM was not the same as the expected DOM", expectedDOM, xmlObject, canMarshall);
105         delete xmlObject;
106     }
107
108     void assertEquals(const char* failMessage, const XMLCh* expectedString, const XMLCh* testString) {
109         char* buf = nullptr;
110         if (!XMLString::equals(expectedString, testString)) {
111             buf = XMLString::transcode(testString);
112             TS_TRACE(buf ? buf : "(NULL)");
113             XMLString::release(&buf);
114             buf = XMLString::transcode(expectedString);
115             TS_TRACE(buf ? buf : "(NULL)");
116             XMLString::release(&buf);
117             TSM_ASSERT(failMessage, false);
118         }
119     }
120
121 public:
122     void setUp() {
123         ParserPool& p=XMLToolingConfig::getConfig().getParser();
124         if (!singleElementFile.empty()) {
125             ifstream fs(singleElementFile.c_str());
126             expectedDOM = p.parse(fs);
127         }
128
129         if (!singleElementOptionalAttributesFile.empty()) {
130             ifstream fs(singleElementOptionalAttributesFile.c_str());
131             expectedOptionalAttributesDOM = p.parse(fs);
132         }
133
134         if (!childElementsFile.empty()) {
135             ifstream fs(childElementsFile.c_str());
136             expectedChildElementsDOM = p.parse(fs);
137         }
138     }
139     
140     void tearDown() {
141         if (expectedDOM) expectedDOM->release();
142         if (expectedOptionalAttributesDOM) expectedOptionalAttributesDOM->release();
143         if (expectedChildElementsDOM) expectedChildElementsDOM->release();
144     }
145 };
146
147 class SAMLObjectValidatorBaseTestCase : virtual public SAMLObjectBaseTestCase {
148
149     public:
150         SAMLObjectValidatorBaseTestCase() : target(nullptr), targetQName(nullptr), builder(nullptr), validator(nullptr) {}
151
152         virtual ~SAMLObjectValidatorBaseTestCase() {
153             delete validator;
154         }
155
156     protected: 
157         /** The primary XMLObject which will be the target of a given test run */
158         XMLObject* target;
159
160         /** QName of the object to be tested */
161         xmltooling::QName targetQName;
162
163         /** Builder for XMLObjects of type targetQName */
164         const XMLObjectBuilder* builder;
165
166         /** Validator for the type corresponding to the test target */
167         Validator* validator;
168
169         /** Subclasses should override to populate required elements and attributes */
170         virtual void populateRequiredData() { }
171
172         /**
173          * Asserts that the validation of default test XMLObject target 
174          * was successful, as expected.
175          * 
176          * @param message
177          */
178         void assertValidationPass(const char* message) {
179             assertValidationPass(message, target);
180         }
181
182         /**
183          * Asserts that the validation of the specified XMLObject target 
184          * was successful, as expected.
185          * 
186          * @param message
187          * @param validateTarget
188          */
189         void assertValidationPass(const char* message, XMLObject* validateTarget) {
190             try {
191                 validator->validate(validateTarget);
192             } catch (ValidationException &e) {
193                 TS_TRACE(message);
194                 TS_TRACE("Expected success, but validation failure raised following ValidationException: ");
195                 TS_FAIL(e.getMessage());
196             }
197         }
198
199         /**
200          * Asserts that the validation of the default test XMLObject target 
201          * failed, as expected.
202          * 
203          * @param message
204          */
205         void assertValidationFail(const char* message) {
206             assertValidationFail(message, target);
207         }
208
209         /**
210          * Asserts that the validation of the specified XMLObject target 
211          * failed, as expected.
212          * 
213          * @param message
214          * @param validateTarget
215          */
216         void assertValidationFail(const char* message, XMLObject* validateTarget) {
217             try {
218                 validator->validate(validateTarget);
219                 TS_TRACE(message);
220                 TS_FAIL("Validation success, expected failure to raise ValidationException");
221             } catch (ValidationException&) {
222             }
223         }
224
225         /**
226          * Build an XMLObject based on the specified QName
227          * 
228          * @param targetQName QName of the type of object to build
229          * @returns new XMLObject of type targetQName
230          */
231         XMLObject* buildXMLObject(xmltooling::QName &targetQName) {
232             // Create the builder on the first request only, for efficiency
233             if (builder == nullptr) {
234                 builder = XMLObjectBuilder::getBuilder(targetQName);
235                 TSM_ASSERT("Unable to retrieve builder for object QName: " + targetQName.toString(), builder!=nullptr);
236             }
237             return builder->buildObject(targetQName.getNamespaceURI(), targetQName.getLocalPart(), targetQName.getPrefix());
238
239         }
240
241     public:
242
243         void setUp() {
244             SAMLObjectBaseTestCase::setUp();
245
246             TSM_ASSERT("targetQName was empty", targetQName.hasLocalPart());
247
248             TSM_ASSERT("validator was null", validator!=nullptr);
249
250             target = buildXMLObject(targetQName);
251             TSM_ASSERT("XMLObject target was NULL", target!=nullptr);
252             populateRequiredData();
253         }
254
255         void tearDown() {
256             delete target;
257             target=nullptr;
258             SAMLObjectBaseTestCase::tearDown();
259         }
260
261 };
262