libxml2 2.9.1 clean sources
[tomato.git] / release / src / router / libxml2 / runxmlconf.c
blobcef20f484b5ea5db720a040708f85010bb38e03e
1 /*
2 * runsuite.c: C program to run libxml2 againts published testsuites
4 * See Copyright for the status of this software.
6 * daniel@veillard.com
7 */
9 #include "libxml.h"
10 #include <stdio.h>
12 #ifdef LIBXML_XPATH_ENABLED
14 #if !defined(_WIN32) || defined(__CYGWIN__)
15 #include <unistd.h>
16 #endif
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
22 #include <libxml/parser.h>
23 #include <libxml/parserInternals.h>
24 #include <libxml/tree.h>
25 #include <libxml/uri.h>
26 #include <libxml/xmlreader.h>
28 #include <libxml/xpath.h>
29 #include <libxml/xpathInternals.h>
31 #define LOGFILE "runxmlconf.log"
32 static FILE *logfile = NULL;
33 static int verbose = 0;
35 #define NB_EXPECTED_ERRORS 15
38 const char *skipped_tests[] = {
39 /* http://lists.w3.org/Archives/Public/public-xml-testsuite/2008Jul/0000.html */
40 "rmt-ns10-035",
41 NULL
44 /************************************************************************
45 * *
46 * File name and path utilities *
47 * *
48 ************************************************************************/
50 static int checkTestFile(const char *filename) {
51 struct stat buf;
53 if (stat(filename, &buf) == -1)
54 return(0);
56 #if defined(_WIN32) && !defined(__CYGWIN__)
57 if (!(buf.st_mode & _S_IFREG))
58 return(0);
59 #else
60 if (!S_ISREG(buf.st_mode))
61 return(0);
62 #endif
64 return(1);
67 static xmlChar *composeDir(const xmlChar *dir, const xmlChar *path) {
68 char buf[500];
70 if (dir == NULL) return(xmlStrdup(path));
71 if (path == NULL) return(NULL);
73 snprintf(buf, 500, "%s/%s", (const char *) dir, (const char *) path);
74 return(xmlStrdup((const xmlChar *) buf));
77 /************************************************************************
78 * *
79 * Libxml2 specific routines *
80 * *
81 ************************************************************************/
83 static int nb_skipped = 0;
84 static int nb_tests = 0;
85 static int nb_errors = 0;
86 static int nb_leaks = 0;
89 * We need to trap calls to the resolver to not account memory for the catalog
90 * and not rely on any external resources.
92 static xmlParserInputPtr
93 testExternalEntityLoader(const char *URL, const char *ID ATTRIBUTE_UNUSED,
94 xmlParserCtxtPtr ctxt) {
95 xmlParserInputPtr ret;
97 ret = xmlNewInputFromFile(ctxt, (const char *) URL);
99 return(ret);
103 * Trapping the error messages at the generic level to grab the equivalent of
104 * stderr messages on CLI tools.
106 static char testErrors[32769];
107 static int testErrorsSize = 0;
108 static int nbError = 0;
109 static int nbFatal = 0;
111 static void test_log(const char *msg, ...) {
112 va_list args;
113 if (logfile != NULL) {
114 fprintf(logfile, "\n------------\n");
115 va_start(args, msg);
116 vfprintf(logfile, msg, args);
117 va_end(args);
118 fprintf(logfile, "%s", testErrors);
119 testErrorsSize = 0; testErrors[0] = 0;
121 if (verbose) {
122 va_start(args, msg);
123 vfprintf(stderr, msg, args);
124 va_end(args);
128 static void
129 testErrorHandler(void *userData ATTRIBUTE_UNUSED, xmlErrorPtr error) {
130 int res;
132 if (testErrorsSize >= 32768)
133 return;
134 res = snprintf(&testErrors[testErrorsSize],
135 32768 - testErrorsSize,
136 "%s:%d: %s\n", (error->file ? error->file : "entity"),
137 error->line, error->message);
138 if (error->level == XML_ERR_FATAL)
139 nbFatal++;
140 else if (error->level == XML_ERR_ERROR)
141 nbError++;
142 if (testErrorsSize + res >= 32768) {
143 /* buffer is full */
144 testErrorsSize = 32768;
145 testErrors[testErrorsSize] = 0;
146 } else {
147 testErrorsSize += res;
149 testErrors[testErrorsSize] = 0;
152 static xmlXPathContextPtr ctxtXPath;
154 static void
155 initializeLibxml2(void) {
156 xmlGetWarningsDefaultValue = 0;
157 xmlPedanticParserDefault(0);
159 xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
160 xmlInitParser();
161 xmlSetExternalEntityLoader(testExternalEntityLoader);
162 ctxtXPath = xmlXPathNewContext(NULL);
164 * Deactivate the cache if created; otherwise we have to create/free it
165 * for every test, since it will confuse the memory leak detection.
166 * Note that normally this need not be done, since the cache is not
167 * created until set explicitely with xmlXPathContextSetCache();
168 * but for test purposes it is sometimes usefull to activate the
169 * cache by default for the whole library.
171 if (ctxtXPath->cache != NULL)
172 xmlXPathContextSetCache(ctxtXPath, 0, -1, 0);
173 xmlSetStructuredErrorFunc(NULL, testErrorHandler);
176 /************************************************************************
178 * Run the xmlconf test if found *
180 ************************************************************************/
182 static int
183 xmlconfTestInvalid(const char *id, const char *filename, int options) {
184 xmlDocPtr doc;
185 xmlParserCtxtPtr ctxt;
186 int ret = 1;
188 ctxt = xmlNewParserCtxt();
189 if (ctxt == NULL) {
190 test_log("test %s : %s out of memory\n",
191 id, filename);
192 return(0);
194 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
195 if (doc == NULL) {
196 test_log("test %s : %s invalid document turned not well-formed too\n",
197 id, filename);
198 } else {
199 /* invalidity should be reported both in the context and in the document */
200 if ((ctxt->valid != 0) || (doc->properties & XML_DOC_DTDVALID)) {
201 test_log("test %s : %s failed to detect invalid document\n",
202 id, filename);
203 nb_errors++;
204 ret = 0;
206 xmlFreeDoc(doc);
208 xmlFreeParserCtxt(ctxt);
209 return(ret);
212 static int
213 xmlconfTestValid(const char *id, const char *filename, int options) {
214 xmlDocPtr doc;
215 xmlParserCtxtPtr ctxt;
216 int ret = 1;
218 ctxt = xmlNewParserCtxt();
219 if (ctxt == NULL) {
220 test_log("test %s : %s out of memory\n",
221 id, filename);
222 return(0);
224 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
225 if (doc == NULL) {
226 test_log("test %s : %s failed to parse a valid document\n",
227 id, filename);
228 nb_errors++;
229 ret = 0;
230 } else {
231 /* validity should be reported both in the context and in the document */
232 if ((ctxt->valid == 0) || ((doc->properties & XML_DOC_DTDVALID) == 0)) {
233 test_log("test %s : %s failed to validate a valid document\n",
234 id, filename);
235 nb_errors++;
236 ret = 0;
238 xmlFreeDoc(doc);
240 xmlFreeParserCtxt(ctxt);
241 return(ret);
244 static int
245 xmlconfTestNotNSWF(const char *id, const char *filename, int options) {
246 xmlDocPtr doc;
247 int ret = 1;
250 * In case of Namespace errors, libxml2 will still parse the document
251 * but log a Namesapce error.
253 doc = xmlReadFile(filename, NULL, options);
254 if (doc == NULL) {
255 test_log("test %s : %s failed to parse the XML\n",
256 id, filename);
257 nb_errors++;
258 ret = 0;
259 } else {
260 if ((xmlLastError.code == XML_ERR_OK) ||
261 (xmlLastError.domain != XML_FROM_NAMESPACE)) {
262 test_log("test %s : %s failed to detect namespace error\n",
263 id, filename);
264 nb_errors++;
265 ret = 0;
267 xmlFreeDoc(doc);
269 return(ret);
272 static int
273 xmlconfTestNotWF(const char *id, const char *filename, int options) {
274 xmlDocPtr doc;
275 int ret = 1;
277 doc = xmlReadFile(filename, NULL, options);
278 if (doc != NULL) {
279 test_log("test %s : %s failed to detect not well formedness\n",
280 id, filename);
281 nb_errors++;
282 xmlFreeDoc(doc);
283 ret = 0;
285 return(ret);
288 static int
289 xmlconfTestItem(xmlDocPtr doc, xmlNodePtr cur) {
290 int ret = -1;
291 xmlChar *type = NULL;
292 xmlChar *filename = NULL;
293 xmlChar *uri = NULL;
294 xmlChar *base = NULL;
295 xmlChar *id = NULL;
296 xmlChar *rec = NULL;
297 xmlChar *version = NULL;
298 xmlChar *entities = NULL;
299 xmlChar *edition = NULL;
300 int options = 0;
301 int nstest = 0;
302 int mem, final;
303 int i;
305 testErrorsSize = 0; testErrors[0] = 0;
306 nbError = 0;
307 nbFatal = 0;
308 id = xmlGetProp(cur, BAD_CAST "ID");
309 if (id == NULL) {
310 test_log("test missing ID, line %ld\n", xmlGetLineNo(cur));
311 goto error;
313 for (i = 0;skipped_tests[i] != NULL;i++) {
314 if (!strcmp(skipped_tests[i], (char *) id)) {
315 test_log("Skipping test %s from skipped list\n", (char *) id);
316 ret = 0;
317 nb_skipped++;
318 goto error;
321 type = xmlGetProp(cur, BAD_CAST "TYPE");
322 if (type == NULL) {
323 test_log("test %s missing TYPE\n", (char *) id);
324 goto error;
326 uri = xmlGetProp(cur, BAD_CAST "URI");
327 if (uri == NULL) {
328 test_log("test %s missing URI\n", (char *) id);
329 goto error;
331 base = xmlNodeGetBase(doc, cur);
332 filename = composeDir(base, uri);
333 if (!checkTestFile((char *) filename)) {
334 test_log("test %s missing file %s \n", id,
335 (filename ? (char *)filename : "NULL"));
336 goto error;
339 version = xmlGetProp(cur, BAD_CAST "VERSION");
341 entities = xmlGetProp(cur, BAD_CAST "ENTITIES");
342 if (!xmlStrEqual(entities, BAD_CAST "none")) {
343 options |= XML_PARSE_DTDLOAD;
344 options |= XML_PARSE_NOENT;
346 rec = xmlGetProp(cur, BAD_CAST "RECOMMENDATION");
347 if ((rec == NULL) ||
348 (xmlStrEqual(rec, BAD_CAST "XML1.0")) ||
349 (xmlStrEqual(rec, BAD_CAST "XML1.0-errata2e")) ||
350 (xmlStrEqual(rec, BAD_CAST "XML1.0-errata3e")) ||
351 (xmlStrEqual(rec, BAD_CAST "XML1.0-errata4e"))) {
352 if ((version != NULL) && (!xmlStrEqual(version, BAD_CAST "1.0"))) {
353 test_log("Skipping test %s for %s\n", (char *) id,
354 (char *) version);
355 ret = 0;
356 nb_skipped++;
357 goto error;
359 ret = 1;
360 } else if ((xmlStrEqual(rec, BAD_CAST "NS1.0")) ||
361 (xmlStrEqual(rec, BAD_CAST "NS1.0-errata1e"))) {
362 ret = 1;
363 nstest = 1;
364 } else {
365 test_log("Skipping test %s for REC %s\n", (char *) id, (char *) rec);
366 ret = 0;
367 nb_skipped++;
368 goto error;
370 edition = xmlGetProp(cur, BAD_CAST "EDITION");
371 if ((edition != NULL) && (xmlStrchr(edition, '5') == NULL)) {
372 /* test limited to all versions before 5th */
373 options |= XML_PARSE_OLD10;
377 * Reset errors and check memory usage before the test
379 xmlResetLastError();
380 testErrorsSize = 0; testErrors[0] = 0;
381 mem = xmlMemUsed();
383 if (xmlStrEqual(type, BAD_CAST "not-wf")) {
384 if (nstest == 0)
385 xmlconfTestNotWF((char *) id, (char *) filename, options);
386 else
387 xmlconfTestNotNSWF((char *) id, (char *) filename, options);
388 } else if (xmlStrEqual(type, BAD_CAST "valid")) {
389 options |= XML_PARSE_DTDVALID;
390 xmlconfTestValid((char *) id, (char *) filename, options);
391 } else if (xmlStrEqual(type, BAD_CAST "invalid")) {
392 options |= XML_PARSE_DTDVALID;
393 xmlconfTestInvalid((char *) id, (char *) filename, options);
394 } else if (xmlStrEqual(type, BAD_CAST "error")) {
395 test_log("Skipping error test %s \n", (char *) id);
396 ret = 0;
397 nb_skipped++;
398 goto error;
399 } else {
400 test_log("test %s unknown TYPE value %s\n", (char *) id, (char *)type);
401 ret = -1;
402 goto error;
406 * Reset errors and check memory usage after the test
408 xmlResetLastError();
409 final = xmlMemUsed();
410 if (final > mem) {
411 test_log("test %s : %s leaked %d bytes\n",
412 id, filename, final - mem);
413 nb_leaks++;
414 xmlMemDisplayLast(logfile, final - mem);
416 nb_tests++;
418 error:
419 if (type != NULL)
420 xmlFree(type);
421 if (entities != NULL)
422 xmlFree(entities);
423 if (edition != NULL)
424 xmlFree(edition);
425 if (version != NULL)
426 xmlFree(version);
427 if (filename != NULL)
428 xmlFree(filename);
429 if (uri != NULL)
430 xmlFree(uri);
431 if (base != NULL)
432 xmlFree(base);
433 if (id != NULL)
434 xmlFree(id);
435 if (rec != NULL)
436 xmlFree(rec);
437 return(ret);
440 static int
441 xmlconfTestCases(xmlDocPtr doc, xmlNodePtr cur, int level) {
442 xmlChar *profile;
443 int ret = 0;
444 int tests = 0;
445 int output = 0;
447 if (level == 1) {
448 profile = xmlGetProp(cur, BAD_CAST "PROFILE");
449 if (profile != NULL) {
450 output = 1;
451 level++;
452 printf("Test cases: %s\n", (char *) profile);
453 xmlFree(profile);
456 cur = cur->children;
457 while (cur != NULL) {
458 /* look only at elements we ignore everything else */
459 if (cur->type == XML_ELEMENT_NODE) {
460 if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
461 ret += xmlconfTestCases(doc, cur, level);
462 } else if (xmlStrEqual(cur->name, BAD_CAST "TEST")) {
463 if (xmlconfTestItem(doc, cur) >= 0)
464 ret++;
465 tests++;
466 } else {
467 fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
470 cur = cur->next;
472 if (output == 1) {
473 if (tests > 0)
474 printf("Test cases: %d tests\n", tests);
476 return(ret);
479 static int
480 xmlconfTestSuite(xmlDocPtr doc, xmlNodePtr cur) {
481 xmlChar *profile;
482 int ret = 0;
484 profile = xmlGetProp(cur, BAD_CAST "PROFILE");
485 if (profile != NULL) {
486 printf("Test suite: %s\n", (char *) profile);
487 xmlFree(profile);
488 } else
489 printf("Test suite\n");
490 cur = cur->children;
491 while (cur != NULL) {
492 /* look only at elements we ignore everything else */
493 if (cur->type == XML_ELEMENT_NODE) {
494 if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
495 ret += xmlconfTestCases(doc, cur, 1);
496 } else {
497 fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
500 cur = cur->next;
502 return(ret);
505 static void
506 xmlconfInfo(void) {
507 fprintf(stderr, " you need to fetch and extract the\n");
508 fprintf(stderr, " latest XML Conformance Test Suites\n");
509 fprintf(stderr, " http://www.w3.org/XML/Test/xmlts20080827.tar.gz\n");
510 fprintf(stderr, " see http://www.w3.org/XML/Test/ for informations\n");
513 static int
514 xmlconfTest(void) {
515 const char *confxml = "xmlconf/xmlconf.xml";
516 xmlDocPtr doc;
517 xmlNodePtr cur;
518 int ret = 0;
520 if (!checkTestFile(confxml)) {
521 fprintf(stderr, "%s is missing \n", confxml);
522 xmlconfInfo();
523 return(-1);
525 doc = xmlReadFile(confxml, NULL, XML_PARSE_NOENT);
526 if (doc == NULL) {
527 fprintf(stderr, "%s is corrupted \n", confxml);
528 xmlconfInfo();
529 return(-1);
532 cur = xmlDocGetRootElement(doc);
533 if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "TESTSUITE"))) {
534 fprintf(stderr, "Unexpected format %s\n", confxml);
535 xmlconfInfo();
536 ret = -1;
537 } else {
538 ret = xmlconfTestSuite(doc, cur);
540 xmlFreeDoc(doc);
541 return(ret);
544 /************************************************************************
546 * The driver for the tests *
548 ************************************************************************/
551 main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
552 int ret = 0;
553 int old_errors, old_tests, old_leaks;
555 logfile = fopen(LOGFILE, "w");
556 if (logfile == NULL) {
557 fprintf(stderr,
558 "Could not open the log file, running in verbose mode\n");
559 verbose = 1;
561 initializeLibxml2();
563 if ((argc >= 2) && (!strcmp(argv[1], "-v")))
564 verbose = 1;
567 old_errors = nb_errors;
568 old_tests = nb_tests;
569 old_leaks = nb_leaks;
570 xmlconfTest();
571 if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
572 printf("Ran %d tests, no errors\n", nb_tests - old_tests);
573 else
574 printf("Ran %d tests, %d errors, %d leaks\n",
575 nb_tests - old_tests,
576 nb_errors - old_errors,
577 nb_leaks - old_leaks);
578 if ((nb_errors == 0) && (nb_leaks == 0)) {
579 ret = 0;
580 printf("Total %d tests, no errors\n",
581 nb_tests);
582 } else {
583 ret = 1;
584 printf("Total %d tests, %d errors, %d leaks\n",
585 nb_tests, nb_errors, nb_leaks);
586 printf("See %s for detailed output\n", LOGFILE);
587 if ((nb_leaks == 0) && (nb_errors == NB_EXPECTED_ERRORS)) {
588 printf("%d errors were expected\n", nb_errors);
589 ret = 0;
592 xmlXPathFreeContext(ctxtXPath);
593 xmlCleanupParser();
594 xmlMemoryDump();
596 if (logfile != NULL)
597 fclose(logfile);
598 return(ret);
601 #else /* ! LIBXML_XPATH_ENABLED */
602 #include <stdio.h>
604 main(int argc, char **argv) {
605 fprintf(stderr, "%s need XPath support\n", argv[0]);
607 #endif