2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 # define _CRT_SECURE_NO_DEPRECATE 1
23 # define _CRT_NONSTDC_NO_DEPRECATE 1
26 #include <cxxtest/TestSuite.h>
28 #include <saml/exceptions.h>
29 #include <saml/util/SAMLConstants.h>
32 #include <xmltooling/XMLObject.h>
33 #include <xmltooling/XMLObjectBuilder.h>
34 #include <xmltooling/XMLToolingConfig.h>
35 #include <xmltooling/util/DateTime.h>
36 #include <xmltooling/util/ParserPool.h>
37 #include <xmltooling/validation/Validator.h>
39 using namespace xmltooling;
40 using namespace xercesc;
43 extern string data_path;
45 class SAMLObjectBaseTestCase
48 /** Location of file containing a single element with NO optional attributes */
49 string singleElementFile;
51 /** Location of file containing a single element with all optional attributes */
52 string singleElementOptionalAttributesFile;
54 /** Location of file containing a single element with child elements */
55 string childElementsFile;
57 /** The expected result of a marshalled single element with no optional attributes */
58 DOMDocument* expectedDOM;
60 /** The expected result of a marshalled single element with all optional attributes */
61 DOMDocument* expectedOptionalAttributesDOM;
63 /** The expected result of a marshalled single element with child elements */
64 DOMDocument* expectedChildElementsDOM;
67 * Unmarshalls an element file into its SAML XMLObject.
69 * @return the SAML XMLObject from the file
71 XMLObject* unmarshallElement(string elementFile) {
73 ParserPool& p=XMLToolingConfig::getConfig().getParser();
74 ifstream fs(elementFile.c_str());
75 DOMDocument* doc = p.parse(fs);
76 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
77 return b->buildFromDocument(doc);
79 catch (XMLToolingException& e) {
80 TS_TRACE(typeid(e).name());
86 void assertEquals(const char* failMessage, DOMDocument* expectedDOM, XMLObject* xmlObject, bool canMarshall=true) {
87 DOMElement* generatedDOM = xmlObject->getDOM();
90 TSM_ASSERT("DOM not available", false);
93 generatedDOM = xmlObject->marshall();
96 if (!generatedDOM->isEqualNode(expectedDOM->getDocumentElement())) {
98 XMLHelper::serialize(generatedDOM, buf);
99 TS_TRACE(buf.c_str());
101 XMLHelper::serialize(expectedDOM->getDocumentElement(), buf);
102 TS_TRACE(buf.c_str());
103 TSM_ASSERT(failMessage, false);
107 void assertEquals(DOMDocument* expectedDOM, XMLObject* xmlObject, bool canMarshall=true) {
108 xmlObject->releaseThisAndChildrenDOM();
109 auto_ptr<XMLObject> cloned(xmlObject->clone());
110 assertEquals("Marshalled DOM was not the same as the expected DOM", expectedDOM, cloned.get(), canMarshall);
114 void assertEquals(const char* failMessage, const XMLCh* expectedString, const XMLCh* testString) {
116 if (!XMLString::equals(expectedString, testString)) {
117 buf = XMLString::transcode(testString);
118 TS_TRACE(buf ? buf : "(NULL)");
119 XMLString::release(&buf);
120 buf = XMLString::transcode(expectedString);
121 TS_TRACE(buf ? buf : "(NULL)");
122 XMLString::release(&buf);
123 TSM_ASSERT(failMessage, false);
129 ParserPool& p=XMLToolingConfig::getConfig().getParser();
130 if (!singleElementFile.empty()) {
131 ifstream fs(singleElementFile.c_str());
132 expectedDOM = p.parse(fs);
135 if (!singleElementOptionalAttributesFile.empty()) {
136 ifstream fs(singleElementOptionalAttributesFile.c_str());
137 expectedOptionalAttributesDOM = p.parse(fs);
140 if (!childElementsFile.empty()) {
141 ifstream fs(childElementsFile.c_str());
142 expectedChildElementsDOM = p.parse(fs);
147 if (expectedDOM) expectedDOM->release();
148 if (expectedOptionalAttributesDOM) expectedOptionalAttributesDOM->release();
149 if (expectedChildElementsDOM) expectedChildElementsDOM->release();
153 class SAMLObjectValidatorBaseTestCase : virtual public SAMLObjectBaseTestCase {
156 SAMLObjectValidatorBaseTestCase() : target(nullptr), targetQName(nullptr), builder(nullptr), validator(nullptr) {}
158 virtual ~SAMLObjectValidatorBaseTestCase() {
163 /** The primary XMLObject which will be the target of a given test run */
166 /** QName of the object to be tested */
167 xmltooling::QName targetQName;
169 /** Builder for XMLObjects of type targetQName */
170 const XMLObjectBuilder* builder;
172 /** Validator for the type corresponding to the test target */
173 Validator* validator;
175 /** Subclasses should override to populate required elements and attributes */
176 virtual void populateRequiredData() { }
179 * Asserts that the validation of default test XMLObject target
180 * was successful, as expected.
184 void assertValidationPass(const char* message) {
185 assertValidationPass(message, target);
189 * Asserts that the validation of the specified XMLObject target
190 * was successful, as expected.
193 * @param validateTarget
195 void assertValidationPass(const char* message, XMLObject* validateTarget) {
197 validator->validate(validateTarget);
198 } catch (ValidationException &e) {
200 TS_TRACE("Expected success, but validation failure raised following ValidationException: ");
201 TS_FAIL(e.getMessage());
206 * Asserts that the validation of the default test XMLObject target
207 * failed, as expected.
211 void assertValidationFail(const char* message) {
212 assertValidationFail(message, target);
216 * Asserts that the validation of the specified XMLObject target
217 * failed, as expected.
220 * @param validateTarget
222 void assertValidationFail(const char* message, XMLObject* validateTarget) {
224 validator->validate(validateTarget);
226 TS_FAIL("Validation success, expected failure to raise ValidationException");
227 } catch (ValidationException&) {
232 * Build an XMLObject based on the specified QName
234 * @param targetQName QName of the type of object to build
235 * @returns new XMLObject of type targetQName
237 XMLObject* buildXMLObject(xmltooling::QName &targetQName) {
238 // Create the builder on the first request only, for efficiency
239 if (builder == nullptr) {
240 builder = XMLObjectBuilder::getBuilder(targetQName);
241 TSM_ASSERT("Unable to retrieve builder for object QName: " + targetQName.toString(), builder!=nullptr);
243 return builder->buildObject(targetQName.getNamespaceURI(), targetQName.getLocalPart(), targetQName.getPrefix());
250 SAMLObjectBaseTestCase::setUp();
252 TSM_ASSERT("targetQName was empty", targetQName.hasLocalPart());
254 TSM_ASSERT("validator was null", validator!=nullptr);
256 target = buildXMLObject(targetQName);
257 TSM_ASSERT("XMLObject target was NULL", target!=nullptr);
258 populateRequiredData();
264 SAMLObjectBaseTestCase::tearDown();