2 * runsuite.c: C program to run libxml2 againts published testsuites
4 * See Copyright for the status of this software.
15 #ifdef LIBXML_XPATH_ENABLED
17 #if !defined(_WIN32) || defined(__CYGWIN__)
21 #include <sys/types.h>
25 #include <libxml/parser.h>
26 #include <libxml/parserInternals.h>
27 #include <libxml/tree.h>
28 #include <libxml/uri.h>
29 #include <libxml/xmlreader.h>
31 #include <libxml/xpath.h>
32 #include <libxml/xpathInternals.h>
34 #define LOGFILE "runxmlconf.log"
35 static FILE *logfile = NULL;
36 static int verbose = 0;
38 #define NB_EXPECTED_ERRORS 15
40 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
41 #define vsnprintf _vsnprintf
42 #define snprintf _snprintf
45 const char *skipped_tests[] = {
46 /* http://lists.w3.org/Archives/Public/public-xml-testsuite/2008Jul/0000.html */
51 /************************************************************************
53 * File name and path utilities *
55 ************************************************************************/
57 static int checkTestFile(const char *filename) {
60 if (stat(filename, &buf) == -1)
63 #if defined(_WIN32) && !defined(__CYGWIN__)
64 if (!(buf.st_mode & _S_IFREG))
67 if (!S_ISREG(buf.st_mode))
74 static xmlChar *composeDir(const xmlChar *dir, const xmlChar *path) {
77 if (dir == NULL) return(xmlStrdup(path));
78 if (path == NULL) return(NULL);
80 snprintf(buf, 500, "%s/%s", (const char *) dir, (const char *) path);
81 return(xmlStrdup((const xmlChar *) buf));
84 /************************************************************************
86 * Libxml2 specific routines *
88 ************************************************************************/
90 static int nb_skipped = 0;
91 static int nb_tests = 0;
92 static int nb_errors = 0;
93 static int nb_leaks = 0;
96 * We need to trap calls to the resolver to not account memory for the catalog
97 * and not rely on any external resources.
99 static xmlParserInputPtr
100 testExternalEntityLoader(const char *URL, const char *ID ATTRIBUTE_UNUSED,
101 xmlParserCtxtPtr ctxt) {
102 xmlParserInputPtr ret;
104 ret = xmlNewInputFromFile(ctxt, (const char *) URL);
110 * Trapping the error messages at the generic level to grab the equivalent of
111 * stderr messages on CLI tools.
113 static char testErrors[32769];
114 static int testErrorsSize = 0;
115 static int nbError = 0;
116 static int nbFatal = 0;
118 static void test_log(const char *msg, ...) {
120 if (logfile != NULL) {
121 fprintf(logfile, "\n------------\n");
123 vfprintf(logfile, msg, args);
125 fprintf(logfile, "%s", testErrors);
126 testErrorsSize = 0; testErrors[0] = 0;
130 vfprintf(stderr, msg, args);
136 testErrorHandler(void *userData ATTRIBUTE_UNUSED, xmlErrorPtr error) {
139 if (testErrorsSize >= 32768)
141 res = snprintf(&testErrors[testErrorsSize],
142 32768 - testErrorsSize,
143 "%s:%d: %s\n", (error->file ? error->file : "entity"),
144 error->line, error->message);
145 if (error->level == XML_ERR_FATAL)
147 else if (error->level == XML_ERR_ERROR)
149 if (testErrorsSize + res >= 32768) {
151 testErrorsSize = 32768;
152 testErrors[testErrorsSize] = 0;
154 testErrorsSize += res;
156 testErrors[testErrorsSize] = 0;
159 static xmlXPathContextPtr ctxtXPath;
162 initializeLibxml2(void) {
163 xmlGetWarningsDefaultValue = 0;
164 xmlPedanticParserDefault(0);
166 xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
168 xmlSetExternalEntityLoader(testExternalEntityLoader);
169 ctxtXPath = xmlXPathNewContext(NULL);
171 * Deactivate the cache if created; otherwise we have to create/free it
172 * for every test, since it will confuse the memory leak detection.
173 * Note that normally this need not be done, since the cache is not
174 * created until set explicitely with xmlXPathContextSetCache();
175 * but for test purposes it is sometimes usefull to activate the
176 * cache by default for the whole library.
178 if (ctxtXPath->cache != NULL)
179 xmlXPathContextSetCache(ctxtXPath, 0, -1, 0);
180 xmlSetStructuredErrorFunc(NULL, testErrorHandler);
183 /************************************************************************
185 * Run the xmlconf test if found *
187 ************************************************************************/
190 xmlconfTestInvalid(const char *id, const char *filename, int options) {
192 xmlParserCtxtPtr ctxt;
195 ctxt = xmlNewParserCtxt();
197 test_log("test %s : %s out of memory\n",
201 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
203 test_log("test %s : %s invalid document turned not well-formed too\n",
206 /* invalidity should be reported both in the context and in the document */
207 if ((ctxt->valid != 0) || (doc->properties & XML_DOC_DTDVALID)) {
208 test_log("test %s : %s failed to detect invalid document\n",
215 xmlFreeParserCtxt(ctxt);
220 xmlconfTestValid(const char *id, const char *filename, int options) {
222 xmlParserCtxtPtr ctxt;
225 ctxt = xmlNewParserCtxt();
227 test_log("test %s : %s out of memory\n",
231 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
233 test_log("test %s : %s failed to parse a valid document\n",
238 /* validity should be reported both in the context and in the document */
239 if ((ctxt->valid == 0) || ((doc->properties & XML_DOC_DTDVALID) == 0)) {
240 test_log("test %s : %s failed to validate a valid document\n",
247 xmlFreeParserCtxt(ctxt);
252 xmlconfTestNotNSWF(const char *id, const char *filename, int options) {
257 * In case of Namespace errors, libxml2 will still parse the document
258 * but log a Namesapce error.
260 doc = xmlReadFile(filename, NULL, options);
262 test_log("test %s : %s failed to parse the XML\n",
267 if ((xmlLastError.code == XML_ERR_OK) ||
268 (xmlLastError.domain != XML_FROM_NAMESPACE)) {
269 test_log("test %s : %s failed to detect namespace error\n",
280 xmlconfTestNotWF(const char *id, const char *filename, int options) {
284 doc = xmlReadFile(filename, NULL, options);
286 test_log("test %s : %s failed to detect not well formedness\n",
296 xmlconfTestItem(xmlDocPtr doc, xmlNodePtr cur) {
298 xmlChar *type = NULL;
299 xmlChar *filename = NULL;
301 xmlChar *base = NULL;
304 xmlChar *version = NULL;
305 xmlChar *entities = NULL;
306 xmlChar *edition = NULL;
312 testErrorsSize = 0; testErrors[0] = 0;
315 id = xmlGetProp(cur, BAD_CAST "ID");
317 test_log("test missing ID, line %ld\n", xmlGetLineNo(cur));
320 for (i = 0;skipped_tests[i] != NULL;i++) {
321 if (!strcmp(skipped_tests[i], (char *) id)) {
322 test_log("Skipping test %s from skipped list\n", (char *) id);
328 type = xmlGetProp(cur, BAD_CAST "TYPE");
330 test_log("test %s missing TYPE\n", (char *) id);
333 uri = xmlGetProp(cur, BAD_CAST "URI");
335 test_log("test %s missing URI\n", (char *) id);
338 base = xmlNodeGetBase(doc, cur);
339 filename = composeDir(base, uri);
340 if (!checkTestFile((char *) filename)) {
341 test_log("test %s missing file %s \n", id,
342 (filename ? (char *)filename : "NULL"));
346 version = xmlGetProp(cur, BAD_CAST "VERSION");
348 entities = xmlGetProp(cur, BAD_CAST "ENTITIES");
349 if (!xmlStrEqual(entities, BAD_CAST "none")) {
350 options |= XML_PARSE_DTDLOAD;
351 options |= XML_PARSE_NOENT;
353 rec = xmlGetProp(cur, BAD_CAST "RECOMMENDATION");
355 (xmlStrEqual(rec, BAD_CAST "XML1.0")) ||
356 (xmlStrEqual(rec, BAD_CAST "XML1.0-errata2e")) ||
357 (xmlStrEqual(rec, BAD_CAST "XML1.0-errata3e")) ||
358 (xmlStrEqual(rec, BAD_CAST "XML1.0-errata4e"))) {
359 if ((version != NULL) && (!xmlStrEqual(version, BAD_CAST "1.0"))) {
360 test_log("Skipping test %s for %s\n", (char *) id,
367 } else if ((xmlStrEqual(rec, BAD_CAST "NS1.0")) ||
368 (xmlStrEqual(rec, BAD_CAST "NS1.0-errata1e"))) {
372 test_log("Skipping test %s for REC %s\n", (char *) id, (char *) rec);
377 edition = xmlGetProp(cur, BAD_CAST "EDITION");
378 if ((edition != NULL) && (xmlStrchr(edition, '5') == NULL)) {
379 /* test limited to all versions before 5th */
380 options |= XML_PARSE_OLD10;
384 * Reset errors and check memory usage before the test
387 testErrorsSize = 0; testErrors[0] = 0;
390 if (xmlStrEqual(type, BAD_CAST "not-wf")) {
392 xmlconfTestNotWF((char *) id, (char *) filename, options);
394 xmlconfTestNotNSWF((char *) id, (char *) filename, options);
395 } else if (xmlStrEqual(type, BAD_CAST "valid")) {
396 options |= XML_PARSE_DTDVALID;
397 xmlconfTestValid((char *) id, (char *) filename, options);
398 } else if (xmlStrEqual(type, BAD_CAST "invalid")) {
399 options |= XML_PARSE_DTDVALID;
400 xmlconfTestInvalid((char *) id, (char *) filename, options);
401 } else if (xmlStrEqual(type, BAD_CAST "error")) {
402 test_log("Skipping error test %s \n", (char *) id);
407 test_log("test %s unknown TYPE value %s\n", (char *) id, (char *)type);
413 * Reset errors and check memory usage after the test
416 final = xmlMemUsed();
418 test_log("test %s : %s leaked %d bytes\n",
419 id, filename, final - mem);
421 xmlMemDisplayLast(logfile, final - mem);
428 if (entities != NULL)
434 if (filename != NULL)
448 xmlconfTestCases(xmlDocPtr doc, xmlNodePtr cur, int level) {
455 profile = xmlGetProp(cur, BAD_CAST "PROFILE");
456 if (profile != NULL) {
459 printf("Test cases: %s\n", (char *) profile);
464 while (cur != NULL) {
465 /* look only at elements we ignore everything else */
466 if (cur->type == XML_ELEMENT_NODE) {
467 if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
468 ret += xmlconfTestCases(doc, cur, level);
469 } else if (xmlStrEqual(cur->name, BAD_CAST "TEST")) {
470 if (xmlconfTestItem(doc, cur) >= 0)
474 fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
481 printf("Test cases: %d tests\n", tests);
487 xmlconfTestSuite(xmlDocPtr doc, xmlNodePtr cur) {
491 profile = xmlGetProp(cur, BAD_CAST "PROFILE");
492 if (profile != NULL) {
493 printf("Test suite: %s\n", (char *) profile);
496 printf("Test suite\n");
498 while (cur != NULL) {
499 /* look only at elements we ignore everything else */
500 if (cur->type == XML_ELEMENT_NODE) {
501 if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
502 ret += xmlconfTestCases(doc, cur, 1);
504 fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
514 fprintf(stderr, " you need to fetch and extract the\n");
515 fprintf(stderr, " latest XML Conformance Test Suites\n");
516 fprintf(stderr, " http://www.w3.org/XML/Test/xmlts20080827.tar.gz\n");
517 fprintf(stderr, " see http://www.w3.org/XML/Test/ for informations\n");
522 const char *confxml = "xmlconf/xmlconf.xml";
527 if (!checkTestFile(confxml)) {
528 fprintf(stderr, "%s is missing \n", confxml);
532 doc = xmlReadFile(confxml, NULL, XML_PARSE_NOENT);
534 fprintf(stderr, "%s is corrupted \n", confxml);
539 cur = xmlDocGetRootElement(doc);
540 if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "TESTSUITE"))) {
541 fprintf(stderr, "Unexpected format %s\n", confxml);
545 ret = xmlconfTestSuite(doc, cur);
551 /************************************************************************
553 * The driver for the tests *
555 ************************************************************************/
558 main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
560 int old_errors, old_tests, old_leaks;
562 logfile = fopen(LOGFILE, "w");
563 if (logfile == NULL) {
565 "Could not open the log file, running in verbose mode\n");
570 if ((argc >= 2) && (!strcmp(argv[1], "-v")))
574 old_errors = nb_errors;
575 old_tests = nb_tests;
576 old_leaks = nb_leaks;
578 if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
579 printf("Ran %d tests, no errors\n", nb_tests - old_tests);
581 printf("Ran %d tests, %d errors, %d leaks\n",
582 nb_tests - old_tests,
583 nb_errors - old_errors,
584 nb_leaks - old_leaks);
585 if ((nb_errors == 0) && (nb_leaks == 0)) {
587 printf("Total %d tests, no errors\n",
591 printf("Total %d tests, %d errors, %d leaks\n",
592 nb_tests, nb_errors, nb_leaks);
593 printf("See %s for detailed output\n", LOGFILE);
594 if ((nb_leaks == 0) && (nb_errors == NB_EXPECTED_ERRORS)) {
595 printf("%d errors were expected\n", nb_errors);
599 xmlXPathFreeContext(ctxtXPath);
608 #else /* ! LIBXML_XPATH_ENABLED */
611 main(int argc, char **argv) {
612 fprintf(stderr, "%s need XPath support\n", argv[0]);