2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 # define _CRT_SECURE_NO_DEPRECATE 1
19 # define _CRT_NONSTDC_NO_DEPRECATE 1
22 #include <cxxtest/TestSuite.h>
25 #include <saml/exceptions.h>
26 #include <saml/util/SAMLConstants.h>
27 #include <xmltooling/XMLObject.h>
28 #include <xmltooling/XMLObjectBuilder.h>
29 #include <xmltooling/XMLToolingConfig.h>
30 #include <xmltooling/validation/Validator.h>
32 using namespace xmltooling;
33 using namespace xercesc;
36 extern string data_path;
38 class SAMLObjectBaseTestCase
41 /** Location of file containing a single element with NO optional attributes */
42 string singleElementFile;
44 /** Location of file containing a single element with all optional attributes */
45 string singleElementOptionalAttributesFile;
47 /** Location of file containing a single element with child elements */
48 string childElementsFile;
50 /** The expected result of a marshalled single element with no optional attributes */
51 DOMDocument* expectedDOM;
53 /** The expected result of a marshalled single element with all optional attributes */
54 DOMDocument* expectedOptionalAttributesDOM;
56 /** The expected result of a marshalled single element with child elements */
57 DOMDocument* expectedChildElementsDOM;
60 * Unmarshalls an element file into its SAML XMLObject.
62 * @return the SAML XMLObject from the file
64 XMLObject* unmarshallElement(string elementFile) {
66 ParserPool& p=XMLToolingConfig::getConfig().getParser();
67 ifstream fs(elementFile.c_str());
68 DOMDocument* doc = p.parse(fs);
69 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
70 return b->buildFromDocument(doc);
72 catch (XMLToolingException& e) {
73 TS_TRACE(typeid(e).name());
79 void assertEquals(const char* failMessage, DOMDocument* expectedDOM, XMLObject* xmlObject, bool canMarshall=true) {
80 DOMElement* generatedDOM = xmlObject->getDOM();
83 TSM_ASSERT("DOM not available", false);
86 generatedDOM = xmlObject->marshall();
89 if (!generatedDOM->isEqualNode(expectedDOM->getDocumentElement())) {
91 XMLHelper::serialize(generatedDOM, buf);
92 TS_TRACE(buf.c_str());
94 XMLHelper::serialize(expectedDOM->getDocumentElement(), buf);
95 TS_TRACE(buf.c_str());
96 TSM_ASSERT(failMessage, false);
100 void assertEquals(DOMDocument* expectedDOM, XMLObject* xmlObject, bool canMarshall=true) {
101 assertEquals("Marshalled DOM was not the same as the expected DOM", expectedDOM, xmlObject, canMarshall);
105 void assertEquals(const char* failMessage, const XMLCh* expectedString, const XMLCh* testString) {
107 if (!XMLString::equals(expectedString, testString)) {
108 buf = XMLString::transcode(testString);
109 TS_TRACE(buf ? buf : "(NULL)");
110 XMLString::release(&buf);
111 buf = XMLString::transcode(expectedString);
112 TS_TRACE(buf ? buf : "(NULL)");
113 XMLString::release(&buf);
114 TSM_ASSERT(failMessage, false);
120 ParserPool& p=XMLToolingConfig::getConfig().getParser();
121 if (!singleElementFile.empty()) {
122 ifstream fs(singleElementFile.c_str());
123 expectedDOM = p.parse(fs);
126 if (!singleElementOptionalAttributesFile.empty()) {
127 ifstream fs(singleElementOptionalAttributesFile.c_str());
128 expectedOptionalAttributesDOM = p.parse(fs);
131 if (!childElementsFile.empty()) {
132 ifstream fs(childElementsFile.c_str());
133 expectedChildElementsDOM = p.parse(fs);
138 if (expectedDOM) expectedDOM->release();
139 if (expectedOptionalAttributesDOM) expectedOptionalAttributesDOM->release();
140 if (expectedChildElementsDOM) expectedChildElementsDOM->release();
144 class SAMLObjectValidatorBaseTestCase : virtual public SAMLObjectBaseTestCase {
147 SAMLObjectValidatorBaseTestCase() : target(NULL), targetQName(NULL), builder(NULL), validator(NULL) {}
149 virtual ~SAMLObjectValidatorBaseTestCase() {
154 /** The primary XMLObject which will be the target of a given test run */
157 /** QName of the object to be tested */
158 xmltooling::QName targetQName;
160 /** Builder for XMLObjects of type targetQName */
161 const XMLObjectBuilder* builder;
163 /** Validator for the type corresponding to the test target */
164 Validator* validator;
166 /** Subclasses should override to populate required elements and attributes */
167 virtual void populateRequiredData() { }
170 * Asserts that the validation of default test XMLObject target
171 * was successful, as expected.
175 void assertValidationPass(const char* message) {
176 assertValidationPass(message, target);
180 * Asserts that the validation of the specified XMLObject target
181 * was successful, as expected.
184 * @param validateTarget
186 void assertValidationPass(const char* message, XMLObject* validateTarget) {
188 validator->validate(validateTarget);
189 } catch (ValidationException &e) {
191 TS_TRACE("Expected success, but validation failure raised following ValidationException: ");
192 TS_FAIL(e.getMessage());
197 * Asserts that the validation of the default test XMLObject target
198 * failed, as expected.
202 void assertValidationFail(const char* message) {
203 assertValidationFail(message, target);
207 * Asserts that the validation of the specified XMLObject target
208 * failed, as expected.
211 * @param validateTarget
213 void assertValidationFail(const char* message, XMLObject* validateTarget) {
215 validator->validate(validateTarget);
217 TS_FAIL("Validation success, expected failure to raise ValidationException");
218 } catch (ValidationException&) {
223 * Build an XMLObject based on the specified QName
225 * @param targetQName QName of the type of object to build
226 * @returns new XMLObject of type targetQName
228 XMLObject* buildXMLObject(xmltooling::QName &targetQName) {
229 // Create the builder on the first request only, for efficiency
230 if (builder == NULL) {
231 builder = XMLObjectBuilder::getBuilder(targetQName);
232 TSM_ASSERT("Unable to retrieve builder for object QName: " + targetQName.toString(), builder!=NULL);
234 return builder->buildObject(targetQName.getNamespaceURI(), targetQName.getLocalPart(), targetQName.getPrefix());
241 SAMLObjectBaseTestCase::setUp();
243 TSM_ASSERT("targetQName was empty", targetQName.hasLocalPart());
245 TSM_ASSERT("validator was null", validator!=NULL);
247 target = buildXMLObject(targetQName);
248 TSM_ASSERT("XMLObject target was NULL", target!=NULL);
249 populateRequiredData();
255 SAMLObjectBaseTestCase::tearDown();