2 * Copyright 2001-2009 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>
24 #include <saml/exceptions.h>
25 #include <saml/util/SAMLConstants.h>
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>
35 using namespace xmltooling;
36 using namespace xercesc;
39 extern string data_path;
41 class SAMLObjectBaseTestCase
44 /** Location of file containing a single element with NO optional attributes */
45 string singleElementFile;
47 /** Location of file containing a single element with all optional attributes */
48 string singleElementOptionalAttributesFile;
50 /** Location of file containing a single element with child elements */
51 string childElementsFile;
53 /** The expected result of a marshalled single element with no optional attributes */
54 DOMDocument* expectedDOM;
56 /** The expected result of a marshalled single element with all optional attributes */
57 DOMDocument* expectedOptionalAttributesDOM;
59 /** The expected result of a marshalled single element with child elements */
60 DOMDocument* expectedChildElementsDOM;
63 * Unmarshalls an element file into its SAML XMLObject.
65 * @return the SAML XMLObject from the file
67 XMLObject* unmarshallElement(string elementFile) {
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);
75 catch (XMLToolingException& e) {
76 TS_TRACE(typeid(e).name());
82 void assertEquals(const char* failMessage, DOMDocument* expectedDOM, XMLObject* xmlObject, bool canMarshall=true) {
83 DOMElement* generatedDOM = xmlObject->getDOM();
86 TSM_ASSERT("DOM not available", false);
89 generatedDOM = xmlObject->marshall();
92 if (!generatedDOM->isEqualNode(expectedDOM->getDocumentElement())) {
94 XMLHelper::serialize(generatedDOM, buf);
95 TS_TRACE(buf.c_str());
97 XMLHelper::serialize(expectedDOM->getDocumentElement(), buf);
98 TS_TRACE(buf.c_str());
99 TSM_ASSERT(failMessage, false);
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);
108 void assertEquals(const char* failMessage, const XMLCh* expectedString, const XMLCh* testString) {
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);
123 ParserPool& p=XMLToolingConfig::getConfig().getParser();
124 if (!singleElementFile.empty()) {
125 ifstream fs(singleElementFile.c_str());
126 expectedDOM = p.parse(fs);
129 if (!singleElementOptionalAttributesFile.empty()) {
130 ifstream fs(singleElementOptionalAttributesFile.c_str());
131 expectedOptionalAttributesDOM = p.parse(fs);
134 if (!childElementsFile.empty()) {
135 ifstream fs(childElementsFile.c_str());
136 expectedChildElementsDOM = p.parse(fs);
141 if (expectedDOM) expectedDOM->release();
142 if (expectedOptionalAttributesDOM) expectedOptionalAttributesDOM->release();
143 if (expectedChildElementsDOM) expectedChildElementsDOM->release();
147 class SAMLObjectValidatorBaseTestCase : virtual public SAMLObjectBaseTestCase {
150 SAMLObjectValidatorBaseTestCase() : target(NULL), targetQName(NULL), builder(NULL), validator(NULL) {}
152 virtual ~SAMLObjectValidatorBaseTestCase() {
157 /** The primary XMLObject which will be the target of a given test run */
160 /** QName of the object to be tested */
161 xmltooling::QName targetQName;
163 /** Builder for XMLObjects of type targetQName */
164 const XMLObjectBuilder* builder;
166 /** Validator for the type corresponding to the test target */
167 Validator* validator;
169 /** Subclasses should override to populate required elements and attributes */
170 virtual void populateRequiredData() { }
173 * Asserts that the validation of default test XMLObject target
174 * was successful, as expected.
178 void assertValidationPass(const char* message) {
179 assertValidationPass(message, target);
183 * Asserts that the validation of the specified XMLObject target
184 * was successful, as expected.
187 * @param validateTarget
189 void assertValidationPass(const char* message, XMLObject* validateTarget) {
191 validator->validate(validateTarget);
192 } catch (ValidationException &e) {
194 TS_TRACE("Expected success, but validation failure raised following ValidationException: ");
195 TS_FAIL(e.getMessage());
200 * Asserts that the validation of the default test XMLObject target
201 * failed, as expected.
205 void assertValidationFail(const char* message) {
206 assertValidationFail(message, target);
210 * Asserts that the validation of the specified XMLObject target
211 * failed, as expected.
214 * @param validateTarget
216 void assertValidationFail(const char* message, XMLObject* validateTarget) {
218 validator->validate(validateTarget);
220 TS_FAIL("Validation success, expected failure to raise ValidationException");
221 } catch (ValidationException&) {
226 * Build an XMLObject based on the specified QName
228 * @param targetQName QName of the type of object to build
229 * @returns new XMLObject of type targetQName
231 XMLObject* buildXMLObject(xmltooling::QName &targetQName) {
232 // Create the builder on the first request only, for efficiency
233 if (builder == NULL) {
234 builder = XMLObjectBuilder::getBuilder(targetQName);
235 TSM_ASSERT("Unable to retrieve builder for object QName: " + targetQName.toString(), builder!=NULL);
237 return builder->buildObject(targetQName.getNamespaceURI(), targetQName.getLocalPart(), targetQName.getPrefix());
244 SAMLObjectBaseTestCase::setUp();
246 TSM_ASSERT("targetQName was empty", targetQName.hasLocalPart());
248 TSM_ASSERT("validator was null", validator!=NULL);
250 target = buildXMLObject(targetQName);
251 TSM_ASSERT("XMLObject target was NULL", target!=NULL);
252 populateRequiredData();
258 SAMLObjectBaseTestCase::tearDown();