2 * Copyright 2001-2006 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.
17 #include <cxxtest/TestSuite.h>
20 #include <saml/exceptions.h>
21 #include <saml/SAMLConfig.h>
22 #include <saml/util/SAMLConstants.h>
23 #include <xmltooling/exceptions.h>
24 #include <xmltooling/validation/Validator.h>
25 #include <xmltooling/XMLObject.h>
26 #include <xmltooling/XMLObjectBuilder.h>
28 using namespace opensaml;
29 using namespace xmltooling;
32 extern string data_path;
34 class SAMLObjectBaseTestCase
37 /** Location of file containing a single element with NO optional attributes */
38 string singleElementFile;
40 /** Location of file containing a single element with all optional attributes */
41 string singleElementOptionalAttributesFile;
43 /** Location of file containing a single element with child elements */
44 string childElementsFile;
46 /** The expected result of a marshalled single element with no optional attributes */
47 DOMDocument* expectedDOM;
49 /** The expected result of a marshalled single element with all optional attributes */
50 DOMDocument* expectedOptionalAttributesDOM;
52 /** The expected result of a marshalled single element with child elements */
53 DOMDocument* expectedChildElementsDOM;
56 * Unmarshalls an element file into its SAML XMLObject.
58 * @return the SAML XMLObject from the file
60 XMLObject* unmarshallElement(string elementFile) {
62 ParserPool& p=XMLToolingConfig::getConfig().getParser();
63 ifstream fs(elementFile.c_str());
64 DOMDocument* doc = p.parse(fs);
65 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
66 return b->buildFromDocument(doc);
68 catch (XMLToolingException& e) {
69 TS_TRACE(typeid(e).name());
75 void assertEquals(const char* failMessage, DOMDocument* expectedDOM, XMLObject* xmlObject, bool canMarshall=true) {
76 DOMElement* generatedDOM = xmlObject->getDOM();
79 TSM_ASSERT("DOM not available", false);
82 generatedDOM = xmlObject->marshall();
85 if (!generatedDOM->isEqualNode(expectedDOM->getDocumentElement())) {
87 XMLHelper::serialize(generatedDOM, buf);
88 TS_TRACE(buf.c_str());
90 XMLHelper::serialize(expectedDOM->getDocumentElement(), buf);
91 TS_TRACE(buf.c_str());
92 TSM_ASSERT(failMessage, false);
96 void assertEquals(DOMDocument* expectedDOM, XMLObject* xmlObject, bool canMarshall=true) {
97 assertEquals("Marshalled DOM was not the same as the expected DOM", expectedDOM, xmlObject, canMarshall);
101 void assertEquals(const char* failMessage, const XMLCh* expectedString, const XMLCh* testString) {
103 if (!XMLString::equals(expectedString, testString)) {
104 buf = XMLString::transcode(testString);
106 XMLString::release(&buf);
107 buf = XMLString::transcode(expectedString);
109 XMLString::release(&buf);
110 TSM_ASSERT(failMessage, false);
116 ParserPool& p=XMLToolingConfig::getConfig().getParser();
117 if (!singleElementFile.empty()) {
118 ifstream fs(singleElementFile.c_str());
119 expectedDOM = p.parse(fs);
122 if (!singleElementOptionalAttributesFile.empty()) {
123 ifstream fs(singleElementOptionalAttributesFile.c_str());
124 expectedOptionalAttributesDOM = p.parse(fs);
127 if (!childElementsFile.empty()) {
128 ifstream fs(childElementsFile.c_str());
129 expectedChildElementsDOM = p.parse(fs);
134 if (expectedDOM) expectedDOM->release();
135 if (expectedOptionalAttributesDOM) expectedOptionalAttributesDOM->release();
136 if (expectedChildElementsDOM) expectedChildElementsDOM->release();
140 class SAMLObjectValidatorBaseTestCase : virtual public SAMLObjectBaseTestCase {
143 SAMLObjectValidatorBaseTestCase() : target(NULL), targetQName(NULL), builder(NULL), validator(NULL) {}
145 virtual ~SAMLObjectValidatorBaseTestCase() {
150 /** The primary XMLObject which will be the target of a given test run */
153 /** QName of the object to be tested */
156 /** Builder for XMLObjects of type targetQName */
157 const XMLObjectBuilder* builder;
159 /** Validator for the type corresponding to the test target */
160 Validator* validator;
162 /** Subclasses should override to populate required elements and attributes */
163 virtual void populateRequiredData() { }
166 * Asserts that the validation of default test XMLObject target
167 * was successful, as expected.
171 void assertValidationPass(const char* message) {
172 assertValidationPass(message, target);
176 * Asserts that the validation of the specified XMLObject target
177 * was successful, as expected.
180 * @param validateTarget
182 void assertValidationPass(const char* message, XMLObject* validateTarget) {
184 validator->validate(validateTarget);
185 } catch (ValidationException &e) {
187 TS_TRACE("Expected success, but validation failure raised following ValidationException: ");
188 TS_FAIL(e.getMessage());
193 * Asserts that the validation of the default test XMLObject target
194 * failed, as expected.
198 void assertValidationFail(const char* message) {
199 assertValidationFail(message, target);
203 * Asserts that the validation of the specified XMLObject target
204 * failed, as expected.
207 * @param validateTarget
209 void assertValidationFail(const char* message, XMLObject* validateTarget) {
211 validator->validate(validateTarget);
213 TS_FAIL("Validation success, expected failure to raise ValidationException");
214 } catch (ValidationException&) {
219 * Build an XMLObject based on the specified QName
221 * @param targetQName QName of the type of object to build
222 * @returns new XMLObject of type targetQName
224 XMLObject* buildXMLObject(QName &targetQName) {
225 // Create the builder on the first request only, for efficiency
226 if (builder == NULL) {
227 builder = XMLObjectBuilder::getBuilder(targetQName);
228 TSM_ASSERT("Unable to retrieve builder for object QName: " + targetQName.toString(), builder!=NULL);
230 return builder->buildObject(targetQName.getNamespaceURI(), targetQName.getLocalPart(), targetQName.getPrefix());
237 SAMLObjectBaseTestCase::setUp();
239 TSM_ASSERT("targetQName was empty", targetQName.hasLocalPart());
241 TSM_ASSERT("validator was null", validator!=NULL);
243 target = buildXMLObject(targetQName);
244 TSM_ASSERT("XMLObject target was NULL", target!=NULL);
245 populateRequiredData();
251 SAMLObjectBaseTestCase::tearDown();