blob: 0529058ec3e3335826f70233a31b058dfed76297 [file] [log] [blame]
Daniel Veillard7e5c3f42008-07-29 16:12:31 +00001/*
2 * runsuite.c: C program to run libxml2 againts published testsuites
3 *
4 * See Copyright for the status of this software.
5 *
6 * daniel@veillard.com
7 */
8
9#ifdef HAVE_CONFIG_H
10#include "libxml.h"
11#else
12#include <stdio.h>
13#endif
14
15#if !defined(_WIN32) || defined(__CYGWIN__)
16#include <unistd.h>
17#endif
18#include <string.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22
23#include <libxml/parser.h>
24#include <libxml/parserInternals.h>
25#include <libxml/tree.h>
26#include <libxml/uri.h>
27#include <libxml/xmlreader.h>
28
29#include <libxml/xpath.h>
30#include <libxml/xpathInternals.h>
31
Daniel Veillard051d52c2008-07-29 16:44:59 +000032#define LOGFILE "runxmlconf.log"
Daniel Veillard7e5c3f42008-07-29 16:12:31 +000033static FILE *logfile = NULL;
34static int verbose = 0;
35
36
37
38#if defined(_WIN32) && !defined(__CYGWIN__)
39
40#define vsnprintf _vsnprintf
41
42#define snprintf _snprintf
43
44#endif
45
Daniel Veillard37334572008-07-31 08:20:02 +000046const char *skipped_tests[] = {
47/* http://lists.w3.org/Archives/Public/public-xml-testsuite/2008Jul/0000.html */
48 "rmt-ns10-035",
49 NULL
50};
51
Daniel Veillard7e5c3f42008-07-29 16:12:31 +000052/************************************************************************
53 * *
54 * File name and path utilities *
55 * *
56 ************************************************************************/
57
58static int checkTestFile(const char *filename) {
59 struct stat buf;
60
61 if (stat(filename, &buf) == -1)
62 return(0);
63
64#if defined(_WIN32) && !defined(__CYGWIN__)
65 if (!(buf.st_mode & _S_IFREG))
66 return(0);
67#else
68 if (!S_ISREG(buf.st_mode))
69 return(0);
70#endif
71
72 return(1);
73}
74
75static xmlChar *composeDir(const xmlChar *dir, const xmlChar *path) {
76 char buf[500];
77
78 if (dir == NULL) return(xmlStrdup(path));
79 if (path == NULL) return(NULL);
80
81 snprintf(buf, 500, "%s/%s", (const char *) dir, (const char *) path);
82 return(xmlStrdup((const xmlChar *) buf));
83}
84
85/************************************************************************
86 * *
87 * Libxml2 specific routines *
88 * *
89 ************************************************************************/
90
91static int nb_skipped = 0;
92static int nb_tests = 0;
93static int nb_errors = 0;
94static int nb_leaks = 0;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +000095
96/*
97 * We need to trap calls to the resolver to not account memory for the catalog
Daniel Veillard09459bf2008-07-30 12:58:11 +000098 * and not rely on any external resources.
Daniel Veillard7e5c3f42008-07-29 16:12:31 +000099 */
100static xmlParserInputPtr
Daniel Veillardae0765b2008-07-31 19:54:59 +0000101testExternalEntityLoader(const char *URL, const char *ID ATTRIBUTE_UNUSED,
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000102 xmlParserCtxtPtr ctxt) {
103 xmlParserInputPtr ret;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000104
Daniel Veillard09459bf2008-07-30 12:58:11 +0000105 ret = xmlNewInputFromFile(ctxt, (const char *) URL);
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000106
107 return(ret);
108}
109
110/*
111 * Trapping the error messages at the generic level to grab the equivalent of
112 * stderr messages on CLI tools.
113 */
114static char testErrors[32769];
115static int testErrorsSize = 0;
Daniel Veillardae0765b2008-07-31 19:54:59 +0000116static int nbError = 0;
117static int nbFatal = 0;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000118
119static void test_log(const char *msg, ...) {
120 va_list args;
121 if (logfile != NULL) {
122 fprintf(logfile, "\n------------\n");
123 va_start(args, msg);
124 vfprintf(logfile, msg, args);
125 va_end(args);
126 fprintf(logfile, "%s", testErrors);
127 testErrorsSize = 0; testErrors[0] = 0;
128 }
129 if (verbose) {
130 va_start(args, msg);
131 vfprintf(stderr, msg, args);
132 va_end(args);
133 }
134}
135
136static void
Daniel Veillardae0765b2008-07-31 19:54:59 +0000137testErrorHandler(void *userData ATTRIBUTE_UNUSED, xmlErrorPtr error) {
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000138 int res;
139
140 if (testErrorsSize >= 32768)
141 return;
Daniel Veillardae0765b2008-07-31 19:54:59 +0000142 res = snprintf(&testErrors[testErrorsSize],
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000143 32768 - testErrorsSize,
Daniel Veillardae0765b2008-07-31 19:54:59 +0000144 "%s:%d: %s\n", (error->file ? error->file : "entity"),
145 error->line, error->message);
146 if (error->level == XML_ERR_FATAL)
147 nbFatal++;
148 else if (error->level == XML_ERR_ERROR)
149 nbError++;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000150 if (testErrorsSize + res >= 32768) {
151 /* buffer is full */
152 testErrorsSize = 32768;
153 testErrors[testErrorsSize] = 0;
154 } else {
155 testErrorsSize += res;
156 }
157 testErrors[testErrorsSize] = 0;
158}
159
160static xmlXPathContextPtr ctxtXPath;
161
162static void
163initializeLibxml2(void) {
164 xmlGetWarningsDefaultValue = 0;
165 xmlPedanticParserDefault(0);
166
167 xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
168 xmlInitParser();
169 xmlSetExternalEntityLoader(testExternalEntityLoader);
170 ctxtXPath = xmlXPathNewContext(NULL);
171 /*
172 * Deactivate the cache if created; otherwise we have to create/free it
173 * for every test, since it will confuse the memory leak detection.
174 * Note that normally this need not be done, since the cache is not
175 * created until set explicitely with xmlXPathContextSetCache();
176 * but for test purposes it is sometimes usefull to activate the
177 * cache by default for the whole library.
178 */
179 if (ctxtXPath->cache != NULL)
180 xmlXPathContextSetCache(ctxtXPath, 0, -1, 0);
Daniel Veillardae0765b2008-07-31 19:54:59 +0000181 xmlSetStructuredErrorFunc(NULL, testErrorHandler);
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000182}
183
184/************************************************************************
185 * *
186 * Run the xmlconf test if found *
187 * *
188 ************************************************************************/
189
190static int
Daniel Veillardae0765b2008-07-31 19:54:59 +0000191xmlconfTestInvalid(const char *id, const char *filename, int options) {
192 xmlDocPtr doc;
193 xmlParserCtxtPtr ctxt;
194 int ret = 1;
195
196 ctxt = xmlNewParserCtxt();
197 if (ctxt == NULL) {
198 test_log("test %s : %s out of memory\n",
199 id, filename);
200 return(0);
201 }
202 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
203 if (doc == NULL) {
204 test_log("test %s : %s invalid document turned not well-formed too\n",
205 id, filename);
206 } else {
207 /* invalidity should be reported both in the context and in the document */
208 if ((ctxt->valid != 0) || (doc->properties & XML_DOC_DTDVALID)) {
209 test_log("test %s : %s failed to detect invalid document\n",
210 id, filename);
211 nb_errors++;
212 ret = 0;
213 }
214 xmlFreeDoc(doc);
215 }
216 xmlFreeParserCtxt(ctxt);
217 return(ret);
218}
219
220static int
221xmlconfTestValid(const char *id, const char *filename, int options) {
222 xmlDocPtr doc;
223 xmlParserCtxtPtr ctxt;
224 int ret = 1;
225
226 ctxt = xmlNewParserCtxt();
227 if (ctxt == NULL) {
228 test_log("test %s : %s out of memory\n",
229 id, filename);
230 return(0);
231 }
232 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
233 if (doc == NULL) {
234 test_log("test %s : %s failed to parse a valid document\n",
235 id, filename);
236 nb_errors++;
237 ret = 0;
238 } else {
239 /* validity should be reported both in the context and in the document */
240 if ((ctxt->valid == 0) || ((doc->properties & XML_DOC_DTDVALID) == 0)) {
241 test_log("test %s : %s failed to validate a valid document\n",
242 id, filename);
243 nb_errors++;
244 ret = 0;
245 }
246 xmlFreeDoc(doc);
247 }
248 xmlFreeParserCtxt(ctxt);
249 return(ret);
250}
251
252static int
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000253xmlconfTestNotNSWF(const char *id, const char *filename, int options) {
254 xmlDocPtr doc;
255 int ret = 1;
256
257 /*
258 * In case of Namespace errors, libxml2 will still parse the document
259 * but log a Namesapce error.
260 */
261 doc = xmlReadFile(filename, NULL, options);
262 if (doc == NULL) {
263 test_log("test %s : %s failed to parse the XML\n",
264 id, filename);
265 nb_errors++;
266 ret = 0;
267 } else {
268 if ((xmlLastError.code == XML_ERR_OK) ||
269 (xmlLastError.domain != XML_FROM_NAMESPACE)) {
270 test_log("test %s : %s failed to detect namespace error\n",
271 id, filename);
272 nb_errors++;
273 ret = 0;
274 }
275 xmlFreeDoc(doc);
276 }
277 return(ret);
278}
279
280static int
281xmlconfTestNotWF(const char *id, const char *filename, int options) {
282 xmlDocPtr doc;
283 int ret = 1;
284
285 doc = xmlReadFile(filename, NULL, options);
286 if (doc != NULL) {
287 test_log("test %s : %s failed to detect not well formedness\n",
288 id, filename);
289 nb_errors++;
290 xmlFreeDoc(doc);
291 ret = 0;
292 }
293 return(ret);
294}
295
296static int
297xmlconfTestItem(xmlDocPtr doc, xmlNodePtr cur) {
298 int ret = -1;
299 xmlChar *type = NULL;
300 xmlChar *filename = NULL;
301 xmlChar *uri = NULL;
302 xmlChar *base = NULL;
303 xmlChar *id = NULL;
304 xmlChar *rec = NULL;
Daniel Veillardae0765b2008-07-31 19:54:59 +0000305 xmlChar *version = NULL;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000306 xmlChar *entities = NULL;
307 xmlChar *edition = NULL;
308 int options = 0;
309 int nstest = 0;
310 int mem, final;
Daniel Veillard37334572008-07-31 08:20:02 +0000311 int i;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000312
Daniel Veillard37334572008-07-31 08:20:02 +0000313 testErrorsSize = 0; testErrors[0] = 0;
Daniel Veillardae0765b2008-07-31 19:54:59 +0000314 nbError = 0;
315 nbFatal = 0;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000316 id = xmlGetProp(cur, BAD_CAST "ID");
317 if (id == NULL) {
318 test_log("test missing ID, line %ld\n", xmlGetLineNo(cur));
319 goto error;
320 }
Daniel Veillard37334572008-07-31 08:20:02 +0000321 for (i = 0;skipped_tests[i] != NULL;i++) {
322 if (!strcmp(skipped_tests[i], (char *) id)) {
323 test_log("Skipping test %s from skipped list\n", (char *) id);
324 ret = 0;
325 nb_skipped++;
326 goto error;
327 }
328 }
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000329 type = xmlGetProp(cur, BAD_CAST "TYPE");
330 if (type == NULL) {
331 test_log("test %s missing TYPE\n", (char *) id);
332 goto error;
333 }
334 uri = xmlGetProp(cur, BAD_CAST "URI");
335 if (uri == NULL) {
336 test_log("test %s missing URI\n", (char *) id);
337 goto error;
338 }
339 base = xmlNodeGetBase(doc, cur);
340 filename = composeDir(base, uri);
341 if (!checkTestFile((char *) filename)) {
342 test_log("test %s missing file %s \n", id,
343 (filename ? (char *)filename : "NULL"));
344 goto error;
345 }
346
Daniel Veillardae0765b2008-07-31 19:54:59 +0000347 version = xmlGetProp(cur, BAD_CAST "VERSION");
348
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000349 entities = xmlGetProp(cur, BAD_CAST "ENTITIES");
350 if (!xmlStrEqual(entities, BAD_CAST "none")) {
351 options |= XML_PARSE_DTDLOAD;
Daniel Veillard40ec29a2008-07-30 12:35:40 +0000352 options |= XML_PARSE_NOENT;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000353 }
354 rec = xmlGetProp(cur, BAD_CAST "RECOMMENDATION");
355 if ((rec == NULL) ||
356 (xmlStrEqual(rec, BAD_CAST "XML1.0")) ||
357 (xmlStrEqual(rec, BAD_CAST "XML1.0-errata2e")) ||
358 (xmlStrEqual(rec, BAD_CAST "XML1.0-errata3e")) ||
359 (xmlStrEqual(rec, BAD_CAST "XML1.0-errata4e"))) {
Daniel Veillardae0765b2008-07-31 19:54:59 +0000360 if ((version != NULL) && (!xmlStrEqual(version, BAD_CAST "1.0"))) {
361 test_log("Skipping test %s for %s\n", (char *) id,
362 (char *) version);
363 ret = 0;
364 nb_skipped++;
365 goto error;
366 }
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000367 ret = 1;
368 } else if ((xmlStrEqual(rec, BAD_CAST "NS1.0")) ||
369 (xmlStrEqual(rec, BAD_CAST "NS1.0-errata1e"))) {
370 ret = 1;
371 nstest = 1;
372 } else {
373 test_log("Skipping test %s for REC %s\n", (char *) id, (char *) rec);
374 ret = 0;
375 nb_skipped++;
376 goto error;
377 }
378 edition = xmlGetProp(cur, BAD_CAST "EDITION");
379 if ((edition != NULL) && (xmlStrchr(edition, '5') == NULL)) {
380 /* test limited to all versions before 5th */
381 options |= XML_PARSE_OLD10;
382 }
383
384 /*
385 * Reset errors and check memory usage before the test
386 */
387 xmlResetLastError();
388 testErrorsSize = 0; testErrors[0] = 0;
389 mem = xmlMemUsed();
390
391 if (xmlStrEqual(type, BAD_CAST "not-wf")) {
392 if (nstest == 0)
393 xmlconfTestNotWF((char *) id, (char *) filename, options);
394 else
395 xmlconfTestNotNSWF((char *) id, (char *) filename, options);
396 } else if (xmlStrEqual(type, BAD_CAST "valid")) {
Daniel Veillardae0765b2008-07-31 19:54:59 +0000397 options |= XML_PARSE_DTDVALID;
398 xmlconfTestValid((char *) id, (char *) filename, options);
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000399 } else if (xmlStrEqual(type, BAD_CAST "invalid")) {
Daniel Veillardae0765b2008-07-31 19:54:59 +0000400 options |= XML_PARSE_DTDVALID;
401 xmlconfTestInvalid((char *) id, (char *) filename, options);
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000402 } else if (xmlStrEqual(type, BAD_CAST "error")) {
Daniel Veillardae0765b2008-07-31 19:54:59 +0000403 test_log("Skipping error test %s \n", (char *) id);
404 ret = 0;
405 nb_skipped++;
406 goto error;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000407 } else {
408 test_log("test %s unknown TYPE value %s\n", (char *) id, (char *)type);
409 ret = -1;
410 goto error;
411 }
412
413 /*
414 * Reset errors and check memory usage after the test
415 */
416 xmlResetLastError();
417 final = xmlMemUsed();
418 if (final > mem) {
419 test_log("test %s : %s leaked %d bytes\n",
420 id, filename, final - mem);
421 nb_leaks++;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000422 xmlMemDisplayLast(logfile, final - mem);
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000423 }
424 nb_tests++;
425
426error:
427 if (type != NULL)
428 xmlFree(type);
429 if (entities != NULL)
430 xmlFree(entities);
431 if (edition != NULL)
432 xmlFree(edition);
Daniel Veillardae0765b2008-07-31 19:54:59 +0000433 if (version != NULL)
434 xmlFree(version);
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000435 if (filename != NULL)
436 xmlFree(filename);
437 if (uri != NULL)
438 xmlFree(uri);
439 if (base != NULL)
440 xmlFree(base);
441 if (id != NULL)
442 xmlFree(id);
443 if (rec != NULL)
444 xmlFree(rec);
445 return(ret);
446}
447
448static int
Daniel Veillardaa6de472008-08-25 14:53:31 +0000449xmlconfTestCases(xmlDocPtr doc, xmlNodePtr cur, int level) {
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000450 xmlChar *profile;
451 int ret = 0;
452 int tests = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000453 int output = 0;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000454
Daniel Veillardaa6de472008-08-25 14:53:31 +0000455 if (level == 1) {
456 profile = xmlGetProp(cur, BAD_CAST "PROFILE");
457 if (profile != NULL) {
458 output = 1;
459 level++;
460 printf("Test cases: %s\n", (char *) profile);
461 xmlFree(profile);
462 }
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000463 }
464 cur = cur->children;
465 while (cur != NULL) {
466 /* look only at elements we ignore everything else */
467 if (cur->type == XML_ELEMENT_NODE) {
468 if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
Daniel Veillardaa6de472008-08-25 14:53:31 +0000469 ret += xmlconfTestCases(doc, cur, level);
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000470 } else if (xmlStrEqual(cur->name, BAD_CAST "TEST")) {
471 if (xmlconfTestItem(doc, cur) >= 0)
472 ret++;
473 tests++;
474 } else {
475 fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
476 }
477 }
478 cur = cur->next;
479 }
Daniel Veillardaa6de472008-08-25 14:53:31 +0000480 if (output == 1) {
481 if (tests > 0)
482 printf("Test cases: %d tests\n", tests);
483 }
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000484 return(ret);
485}
486
487static int
488xmlconfTestSuite(xmlDocPtr doc, xmlNodePtr cur) {
489 xmlChar *profile;
490 int ret = 0;
491
492 profile = xmlGetProp(cur, BAD_CAST "PROFILE");
493 if (profile != NULL) {
494 printf("Test suite: %s\n", (char *) profile);
495 xmlFree(profile);
496 } else
497 printf("Test suite\n");
498 cur = cur->children;
499 while (cur != NULL) {
500 /* look only at elements we ignore everything else */
501 if (cur->type == XML_ELEMENT_NODE) {
502 if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
Daniel Veillardaa6de472008-08-25 14:53:31 +0000503 ret += xmlconfTestCases(doc, cur, 1);
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000504 } else {
505 fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
506 }
507 }
508 cur = cur->next;
509 }
510 return(ret);
511}
512
513static void
514xmlconfInfo(void) {
515 fprintf(stderr, " you need to fetch and extract the\n");
516 fprintf(stderr, " latest XML Conformance Test Suites\n");
517 fprintf(stderr, " http://www.w3.org/XML/Test/xmlts20080205.tar.gz\n");
518 fprintf(stderr, " see http://www.w3.org/XML/Test/ for informations\n");
519}
520
521static int
522xmlconfTest(void) {
523 const char *confxml = "xmlconf/xmlconf.xml";
524 xmlDocPtr doc;
525 xmlNodePtr cur;
526 int ret = 0;
527
528 if (!checkTestFile(confxml)) {
529 fprintf(stderr, "%s is missing \n", confxml);
530 xmlconfInfo();
531 return(-1);
532 }
533 doc = xmlReadFile(confxml, NULL, XML_PARSE_NOENT);
534 if (doc == NULL) {
535 fprintf(stderr, "%s is corrupted \n", confxml);
536 xmlconfInfo();
537 return(-1);
538 }
539
540 cur = xmlDocGetRootElement(doc);
541 if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "TESTSUITE"))) {
542 fprintf(stderr, "Unexpected format %s\n", confxml);
543 xmlconfInfo();
544 ret = -1;
545 } else {
546 ret = xmlconfTestSuite(doc, cur);
547 }
548 xmlFreeDoc(doc);
549 return(ret);
550}
551
552/************************************************************************
553 * *
554 * The driver for the tests *
555 * *
556 ************************************************************************/
557
558int
559main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
560 int ret = 0;
561 int old_errors, old_tests, old_leaks;
562
563 logfile = fopen(LOGFILE, "w");
564 if (logfile == NULL) {
565 fprintf(stderr,
566 "Could not open the log file, running in verbose mode\n");
567 verbose = 1;
568 }
569 initializeLibxml2();
570
571 if ((argc >= 2) && (!strcmp(argv[1], "-v")))
572 verbose = 1;
573
574
575 old_errors = nb_errors;
576 old_tests = nb_tests;
577 old_leaks = nb_leaks;
578 xmlconfTest();
579 if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
580 printf("Ran %d tests, no errors\n", nb_tests - old_tests);
581 else
582 printf("Ran %d tests, %d errors, %d leaks\n",
583 nb_tests - old_tests,
584 nb_errors - old_errors,
585 nb_leaks - old_leaks);
586 if ((nb_errors == 0) && (nb_leaks == 0)) {
587 ret = 0;
588 printf("Total %d tests, no errors\n",
589 nb_tests);
590 } else {
591 ret = 1;
592 printf("Total %d tests, %d errors, %d leaks\n",
593 nb_tests, nb_errors, nb_leaks);
Daniel Veillardaa6de472008-08-25 14:53:31 +0000594 printf("See %s for detailed output\n", LOGFILE);
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000595 }
596 xmlXPathFreeContext(ctxtXPath);
597 xmlCleanupParser();
598 xmlMemoryDump();
599
600 if (logfile != NULL)
601 fclose(logfile);
602 return(ret);
603}