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 assertEquals("Marshalled DOM was not the same as the expected DOM", expectedDOM, xmlObject, canMarshall);
112 void assertEquals(const char* failMessage, const XMLCh* expectedString, const XMLCh* testString) {
114 if (!XMLString::equals(expectedString, testString)) {
115 buf = XMLString::transcode(testString);
116 TS_TRACE(buf ? buf : "(NULL)");
117 XMLString::release(&buf);
118 buf = XMLString::transcode(expectedString);
119 TS_TRACE(buf ? buf : "(NULL)");
120 XMLString::release(&buf);
121 TSM_ASSERT(failMessage, false);
127 ParserPool& p=XMLToolingConfig::getConfig().getParser();
128 if (!singleElementFile.empty()) {
129 ifstream fs(singleElementFile.c_str());
130 expectedDOM = p.parse(fs);
133 if (!singleElementOptionalAttributesFile.empty()) {
134 ifstream fs(singleElementOptionalAttributesFile.c_str());
135 expectedOptionalAttributesDOM = p.parse(fs);
138 if (!childElementsFile.empty()) {
139 ifstream fs(childElementsFile.c_str());
140 expectedChildElementsDOM = p.parse(fs);
145 if (expectedDOM) expectedDOM->release();
146 if (expectedOptionalAttributesDOM) expectedOptionalAttributesDOM->release();
147 if (expectedChildElementsDOM) expectedChildElementsDOM->release();
151 class SAMLObjectValidatorBaseTestCase : virtual public SAMLObjectBaseTestCase {
154 SAMLObjectValidatorBaseTestCase() : target(nullptr), targetQName(nullptr), builder(nullptr), validator(nullptr) {}
156 virtual ~SAMLObjectValidatorBaseTestCase() {
161 /** The primary XMLObject which will be the target of a given test run */
164 /** QName of the object to be tested */
165 xmltooling::QName targetQName;
167 /** Builder for XMLObjects of type targetQName */
168 const XMLObjectBuilder* builder;
170 /** Validator for the type corresponding to the test target */
171 Validator* validator;
173 /** Subclasses should override to populate required elements and attributes */
174 virtual void populateRequiredData() { }
177 * Asserts that the validation of default test XMLObject target
178 * was successful, as expected.
182 void assertValidationPass(const char* message) {
183 assertValidationPass(message, target);
187 * Asserts that the validation of the specified XMLObject target
188 * was successful, as expected.
191 * @param validateTarget
193 void assertValidationPass(const char* message, XMLObject* validateTarget) {
195 validator->validate(validateTarget);
196 } catch (ValidationException &e) {
198 TS_TRACE("Expected success, but validation failure raised following ValidationException: ");
199 TS_FAIL(e.getMessage());
204 * Asserts that the validation of the default test XMLObject target
205 * failed, as expected.
209 void assertValidationFail(const char* message) {
210 assertValidationFail(message, target);
214 * Asserts that the validation of the specified XMLObject target
215 * failed, as expected.
218 * @param validateTarget
220 void assertValidationFail(const char* message, XMLObject* validateTarget) {
222 validator->validate(validateTarget);
224 TS_FAIL("Validation success, expected failure to raise ValidationException");
225 } catch (ValidationException&) {
230 * Build an XMLObject based on the specified QName
232 * @param targetQName QName of the type of object to build
233 * @returns new XMLObject of type targetQName
235 XMLObject* buildXMLObject(xmltooling::QName &targetQName) {
236 // Create the builder on the first request only, for efficiency
237 if (builder == nullptr) {
238 builder = XMLObjectBuilder::getBuilder(targetQName);
239 TSM_ASSERT("Unable to retrieve builder for object QName: " + targetQName.toString(), builder!=nullptr);
241 return builder->buildObject(targetQName.getNamespaceURI(), targetQName.getLocalPart(), targetQName.getPrefix());
248 SAMLObjectBaseTestCase::setUp();
250 TSM_ASSERT("targetQName was empty", targetQName.hasLocalPart());
252 TSM_ASSERT("validator was null", validator!=nullptr);
254 target = buildXMLObject(targetQName);
255 TSM_ASSERT("XMLObject target was NULL", target!=nullptr);
256 populateRequiredData();
262 SAMLObjectBaseTestCase::tearDown();