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