blob: 2a1aa9dd16eb90a830cf5fe4004689afae0b6c30 [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
449xmlconfTestCases(xmlDocPtr doc, xmlNodePtr cur) {
450 xmlChar *profile;
451 int ret = 0;
452 int tests = 0;
453
454 profile = xmlGetProp(cur, BAD_CAST "PROFILE");
455 if (profile != NULL) {
456 printf("Test cases: %s\n", (char *) profile);
457 xmlFree(profile);
458 }
459 cur = cur->children;
460 while (cur != NULL) {
461 /* look only at elements we ignore everything else */
462 if (cur->type == XML_ELEMENT_NODE) {
463 if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
464 ret += xmlconfTestCases(doc, cur);
465 } else if (xmlStrEqual(cur->name, BAD_CAST "TEST")) {
466 if (xmlconfTestItem(doc, cur) >= 0)
467 ret++;
468 tests++;
469 } else {
470 fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
471 }
472 }
473 cur = cur->next;
474 }
475 if (tests > 0)
476 printf("Test cases: %d tests\n", tests);
477 return(ret);
478}
479
480static int
481xmlconfTestSuite(xmlDocPtr doc, xmlNodePtr cur) {
482 xmlChar *profile;
483 int ret = 0;
484
485 profile = xmlGetProp(cur, BAD_CAST "PROFILE");
486 if (profile != NULL) {
487 printf("Test suite: %s\n", (char *) profile);
488 xmlFree(profile);
489 } else
490 printf("Test suite\n");
491 cur = cur->children;
492 while (cur != NULL) {
493 /* look only at elements we ignore everything else */
494 if (cur->type == XML_ELEMENT_NODE) {
495 if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
496 ret += xmlconfTestCases(doc, cur);
497 } else {
498 fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
499 }
500 }
501 cur = cur->next;
502 }
503 return(ret);
504}
505
506static void
507xmlconfInfo(void) {
508 fprintf(stderr, " you need to fetch and extract the\n");
509 fprintf(stderr, " latest XML Conformance Test Suites\n");
510 fprintf(stderr, " http://www.w3.org/XML/Test/xmlts20080205.tar.gz\n");
511 fprintf(stderr, " see http://www.w3.org/XML/Test/ for informations\n");
512}
513
514static int
515xmlconfTest(void) {
516 const char *confxml = "xmlconf/xmlconf.xml";
517 xmlDocPtr doc;
518 xmlNodePtr cur;
519 int ret = 0;
520
521 if (!checkTestFile(confxml)) {
522 fprintf(stderr, "%s is missing \n", confxml);
523 xmlconfInfo();
524 return(-1);
525 }
526 doc = xmlReadFile(confxml, NULL, XML_PARSE_NOENT);
527 if (doc == NULL) {
528 fprintf(stderr, "%s is corrupted \n", confxml);
529 xmlconfInfo();
530 return(-1);
531 }
532
533 cur = xmlDocGetRootElement(doc);
534 if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "TESTSUITE"))) {
535 fprintf(stderr, "Unexpected format %s\n", confxml);
536 xmlconfInfo();
537 ret = -1;
538 } else {
539 ret = xmlconfTestSuite(doc, cur);
540 }
541 xmlFreeDoc(doc);
542 return(ret);
543}
544
545/************************************************************************
546 * *
547 * The driver for the tests *
548 * *
549 ************************************************************************/
550
551int
552main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
553 int ret = 0;
554 int old_errors, old_tests, old_leaks;
555
556 logfile = fopen(LOGFILE, "w");
557 if (logfile == NULL) {
558 fprintf(stderr,
559 "Could not open the log file, running in verbose mode\n");
560 verbose = 1;
561 }
562 initializeLibxml2();
563
564 if ((argc >= 2) && (!strcmp(argv[1], "-v")))
565 verbose = 1;
566
567
568 old_errors = nb_errors;
569 old_tests = nb_tests;
570 old_leaks = nb_leaks;
571 xmlconfTest();
572 if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
573 printf("Ran %d tests, no errors\n", nb_tests - old_tests);
574 else
575 printf("Ran %d tests, %d errors, %d leaks\n",
576 nb_tests - old_tests,
577 nb_errors - old_errors,
578 nb_leaks - old_leaks);
579 if ((nb_errors == 0) && (nb_leaks == 0)) {
580 ret = 0;
581 printf("Total %d tests, no errors\n",
582 nb_tests);
583 } else {
584 ret = 1;
585 printf("Total %d tests, %d errors, %d leaks\n",
586 nb_tests, nb_errors, nb_leaks);
587 }
588 xmlXPathFreeContext(ctxtXPath);
589 xmlCleanupParser();
590 xmlMemoryDump();
591
592 if (logfile != NULL)
593 fclose(logfile);
594 return(ret);
595}