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) {
76 DOMElement* generatedDOM = xmlObject->marshall();
77 if (!generatedDOM->isEqualNode(expectedDOM->getDocumentElement())) {
79 XMLHelper::serialize(generatedDOM, buf);
80 TS_TRACE(buf.c_str());
82 XMLHelper::serialize(expectedDOM->getDocumentElement(), buf);
83 TS_TRACE(buf.c_str());
84 TSM_ASSERT(failMessage, false);
88 void assertEquals(DOMDocument* expectedDOM, XMLObject* xmlObject) {
89 assertEquals("Marshalled DOM was not the same as the expected DOM", expectedDOM, xmlObject);
93 void assertEquals(const char* failMessage, const XMLCh* expectedString, const XMLCh* testString) {
95 if (!XMLString::equals(expectedString, testString)) {
96 buf = XMLString::transcode(testString);
98 XMLString::release(&buf);
99 buf = XMLString::transcode(expectedString);
101 XMLString::release(&buf);
102 TSM_ASSERT(failMessage, false);
108 ParserPool& p=XMLToolingConfig::getConfig().getParser();
109 if (!singleElementFile.empty()) {
110 ifstream fs(singleElementFile.c_str());
111 expectedDOM = p.parse(fs);
114 if (!singleElementOptionalAttributesFile.empty()) {
115 ifstream fs(singleElementOptionalAttributesFile.c_str());
116 expectedOptionalAttributesDOM = p.parse(fs);
119 if (!childElementsFile.empty()) {
120 ifstream fs(childElementsFile.c_str());
121 expectedChildElementsDOM = p.parse(fs);
126 if (expectedDOM) expectedDOM->release();
127 if (expectedOptionalAttributesDOM) expectedOptionalAttributesDOM->release();
128 if (expectedChildElementsDOM) expectedChildElementsDOM->release();
132 class SAMLObjectValidatorBaseTestCase : virtual public SAMLObjectBaseTestCase {
135 SAMLObjectValidatorBaseTestCase() : target(NULL), targetQName(NULL), builder(NULL), validator(NULL) {}
137 virtual ~SAMLObjectValidatorBaseTestCase() {
142 /** The primary XMLObject which will be the target of a given test run */
145 /** QName of the object to be tested */
148 /** Builder for XMLObjects of type targetQName */
149 const XMLObjectBuilder* builder;
151 /** Validator for the type corresponding to the test target */
152 Validator* validator;
154 /** Subclasses should override to populate required elements and attributes */
155 virtual void populateRequiredData() { }
158 * Asserts that the validation of default test XMLObject target
159 * was successful, as expected.
163 void assertValidationPass(const char* message) {
164 assertValidationPass(message, target);
168 * Asserts that the validation of the specified XMLObject target
169 * was successful, as expected.
172 * @param validateTarget
174 void assertValidationPass(const char* message, XMLObject* validateTarget) {
176 validator->validate(validateTarget);
177 } catch (ValidationException &e) {
179 TS_TRACE("Expected success, but validation failure raised following ValidationException: ");
180 TS_FAIL(e.getMessage());
185 * Asserts that the validation of the default test XMLObject target
186 * failed, as expected.
190 void assertValidationFail(const char* message) {
191 assertValidationFail(message, target);
195 * Asserts that the validation of the specified XMLObject target
196 * failed, as expected.
199 * @param validateTarget
201 void assertValidationFail(const char* message, XMLObject* validateTarget) {
203 validator->validate(validateTarget);
205 TS_FAIL("Validation success, expected failure to raise ValidationException");
206 } catch (ValidationException &e) {
211 * Build an XMLObject based on the specified QName
213 * @param targetQName QName of the type of object to build
214 * @returns new XMLObject of type targetQName
216 XMLObject* buildXMLObject(QName &targetQName) {
217 // Create the builder on the first request only, for efficiency
218 if (builder == NULL) {
219 builder = XMLObjectBuilder::getBuilder(targetQName);
220 TSM_ASSERT("Unable to retrieve builder for object QName: " + targetQName.toString(), builder!=NULL);
222 return builder->buildObject(targetQName.getNamespaceURI(), targetQName.getLocalPart(), targetQName.getPrefix());
229 SAMLObjectBaseTestCase::setUp();
231 TSM_ASSERT("targetQName was empty", targetQName.hasLocalPart());
233 TSM_ASSERT("validator was null", validator!=NULL);
235 target = buildXMLObject(targetQName);
236 TSM_ASSERT("XMLObject target was NULL", target!=NULL);
237 populateRequiredData();
243 SAMLObjectBaseTestCase::tearDown();