• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "libxml.h"
10 #include <stdio.h>
11 
12 #ifdef LIBXML_XPATH_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,xmlErrorPtr error)124 testErrorHandler(void *userData ATTRIBUTE_UNUSED, xmlErrorPtr 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     xmlGetWarningsDefaultValue = 0;
152     xmlPedanticParserDefault(0);
153 
154     xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
155     xmlInitParser();
156     xmlSetExternalEntityLoader(testExternalEntityLoader);
157     ctxtXPath = xmlXPathNewContext(NULL);
158     /*
159     * Deactivate the cache if created; otherwise we have to create/free it
160     * for every test, since it will confuse the memory leak detection.
161     * Note that normally this need not be done, since the cache is not
162     * created until set explicitly with xmlXPathContextSetCache();
163     * but for test purposes it is sometimes useful to activate the
164     * cache by default for the whole library.
165     */
166     if (ctxtXPath->cache != NULL)
167 	xmlXPathContextSetCache(ctxtXPath, 0, -1, 0);
168     xmlSetStructuredErrorFunc(NULL, testErrorHandler);
169 }
170 
171 /************************************************************************
172  *									*
173  *		Run the xmlconf test if found				*
174  *									*
175  ************************************************************************/
176 
177 static int
xmlconfTestInvalid(const char * id,const char * filename,int options)178 xmlconfTestInvalid(const char *id, const char *filename, int options) {
179     xmlDocPtr doc;
180     xmlParserCtxtPtr ctxt;
181     int ret = 1;
182 
183     ctxt = xmlNewParserCtxt();
184     if (ctxt == NULL) {
185         test_log("test %s : %s out of memory\n",
186 	         id, filename);
187         return(0);
188     }
189     doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
190     if (doc == NULL) {
191         test_log("test %s : %s invalid document turned not well-formed too\n",
192 	         id, filename);
193     } else {
194     /* invalidity should be reported both in the context and in the document */
195         if ((ctxt->valid != 0) || (doc->properties & XML_DOC_DTDVALID)) {
196 	    test_log("test %s : %s failed to detect invalid document\n",
197 		     id, filename);
198 	    nb_errors++;
199 	    ret = 0;
200 	}
201 	xmlFreeDoc(doc);
202     }
203     xmlFreeParserCtxt(ctxt);
204     return(ret);
205 }
206 
207 static int
xmlconfTestValid(const char * id,const char * filename,int options)208 xmlconfTestValid(const char *id, const char *filename, int options) {
209     xmlDocPtr doc;
210     xmlParserCtxtPtr ctxt;
211     int ret = 1;
212 
213     ctxt = xmlNewParserCtxt();
214     if (ctxt == NULL) {
215         test_log("test %s : %s out of memory\n",
216 	         id, filename);
217         return(0);
218     }
219     doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
220     if (doc == NULL) {
221         test_log("test %s : %s failed to parse a valid document\n",
222 	         id, filename);
223         nb_errors++;
224 	ret = 0;
225     } else {
226     /* validity should be reported both in the context and in the document */
227         if ((ctxt->valid == 0) || ((doc->properties & XML_DOC_DTDVALID) == 0)) {
228 	    test_log("test %s : %s failed to validate a valid document\n",
229 		     id, filename);
230 	    nb_errors++;
231 	    ret = 0;
232 	}
233 	xmlFreeDoc(doc);
234     }
235     xmlFreeParserCtxt(ctxt);
236     return(ret);
237 }
238 
239 static int
xmlconfTestNotNSWF(const char * id,const char * filename,int options)240 xmlconfTestNotNSWF(const char *id, const char *filename, int options) {
241     xmlDocPtr doc;
242     int ret = 1;
243 
244     /*
245      * In case of Namespace errors, libxml2 will still parse the document
246      * but log a Namespace error.
247      */
248     doc = xmlReadFile(filename, NULL, options);
249     if (doc == NULL) {
250         test_log("test %s : %s failed to parse the XML\n",
251 	         id, filename);
252         nb_errors++;
253 	ret = 0;
254     } else {
255 	if ((xmlLastError.code == XML_ERR_OK) ||
256 	    (xmlLastError.domain != XML_FROM_NAMESPACE)) {
257 	    test_log("test %s : %s failed to detect namespace error\n",
258 		     id, filename);
259 	    nb_errors++;
260 	    ret = 0;
261 	}
262 	xmlFreeDoc(doc);
263     }
264     return(ret);
265 }
266 
267 static int
xmlconfTestNotWF(const char * id,const char * filename,int options)268 xmlconfTestNotWF(const char *id, const char *filename, int options) {
269     xmlDocPtr doc;
270     int ret = 1;
271 
272     doc = xmlReadFile(filename, NULL, options);
273     if (doc != NULL) {
274         test_log("test %s : %s failed to detect not well formedness\n",
275 	         id, filename);
276         nb_errors++;
277 	xmlFreeDoc(doc);
278 	ret = 0;
279     }
280     return(ret);
281 }
282 
283 static int
xmlconfTestItem(xmlDocPtr doc,xmlNodePtr cur)284 xmlconfTestItem(xmlDocPtr doc, xmlNodePtr cur) {
285     int ret = -1;
286     xmlChar *type = NULL;
287     xmlChar *filename = NULL;
288     xmlChar *uri = NULL;
289     xmlChar *base = NULL;
290     xmlChar *id = NULL;
291     xmlChar *rec = NULL;
292     xmlChar *version = NULL;
293     xmlChar *entities = NULL;
294     xmlChar *edition = NULL;
295     int options = 0;
296     int nstest = 0;
297     int mem, final;
298     int i;
299 
300     testErrorsSize = 0; testErrors[0] = 0;
301     nbError = 0;
302     nbFatal = 0;
303     id = xmlGetProp(cur, BAD_CAST "ID");
304     if (id == NULL) {
305         test_log("test missing ID, line %ld\n", xmlGetLineNo(cur));
306 	goto error;
307     }
308     for (i = 0;skipped_tests[i] != NULL;i++) {
309         if (!strcmp(skipped_tests[i], (char *) id)) {
310 	    test_log("Skipping test %s from skipped list\n", (char *) id);
311 	    ret = 0;
312 	    nb_skipped++;
313 	    goto error;
314 	}
315     }
316     type = xmlGetProp(cur, BAD_CAST "TYPE");
317     if (type == NULL) {
318         test_log("test %s missing TYPE\n", (char *) id);
319 	goto error;
320     }
321     uri = xmlGetProp(cur, BAD_CAST "URI");
322     if (uri == NULL) {
323         test_log("test %s missing URI\n", (char *) id);
324 	goto error;
325     }
326     base = xmlNodeGetBase(doc, cur);
327     filename = composeDir(base, uri);
328     if (!checkTestFile((char *) filename)) {
329         test_log("test %s missing file %s \n", id,
330 	         (filename ? (char *)filename : "NULL"));
331 	goto error;
332     }
333 
334     version = xmlGetProp(cur, BAD_CAST "VERSION");
335 
336     entities = xmlGetProp(cur, BAD_CAST "ENTITIES");
337     if (!xmlStrEqual(entities, BAD_CAST "none")) {
338         options |= XML_PARSE_DTDLOAD;
339         options |= XML_PARSE_NOENT;
340     }
341     rec = xmlGetProp(cur, BAD_CAST "RECOMMENDATION");
342     if ((rec == NULL) ||
343         (xmlStrEqual(rec, BAD_CAST "XML1.0")) ||
344 	(xmlStrEqual(rec, BAD_CAST "XML1.0-errata2e")) ||
345 	(xmlStrEqual(rec, BAD_CAST "XML1.0-errata3e")) ||
346 	(xmlStrEqual(rec, BAD_CAST "XML1.0-errata4e"))) {
347 	if ((version != NULL) && (!xmlStrEqual(version, BAD_CAST "1.0"))) {
348 	    test_log("Skipping test %s for %s\n", (char *) id,
349 	             (char *) version);
350 	    ret = 0;
351 	    nb_skipped++;
352 	    goto error;
353 	}
354 	ret = 1;
355     } else if ((xmlStrEqual(rec, BAD_CAST "NS1.0")) ||
356 	       (xmlStrEqual(rec, BAD_CAST "NS1.0-errata1e"))) {
357 	ret = 1;
358 	nstest = 1;
359     } else {
360         test_log("Skipping test %s for REC %s\n", (char *) id, (char *) rec);
361 	ret = 0;
362 	nb_skipped++;
363 	goto error;
364     }
365     edition = xmlGetProp(cur, BAD_CAST "EDITION");
366     if ((edition != NULL) && (xmlStrchr(edition, '5') == NULL)) {
367         /* test limited to all versions before 5th */
368 	options |= XML_PARSE_OLD10;
369     }
370 
371     /*
372      * Reset errors and check memory usage before the test
373      */
374     xmlResetLastError();
375     testErrorsSize = 0; testErrors[0] = 0;
376     mem = xmlMemUsed();
377 
378     if (xmlStrEqual(type, BAD_CAST "not-wf")) {
379         if (nstest == 0)
380 	    xmlconfTestNotWF((char *) id, (char *) filename, options);
381         else
382 	    xmlconfTestNotNSWF((char *) id, (char *) filename, options);
383     } else if (xmlStrEqual(type, BAD_CAST "valid")) {
384         options |= XML_PARSE_DTDVALID;
385 	xmlconfTestValid((char *) id, (char *) filename, options);
386     } else if (xmlStrEqual(type, BAD_CAST "invalid")) {
387         options |= XML_PARSE_DTDVALID;
388 	xmlconfTestInvalid((char *) id, (char *) filename, options);
389     } else if (xmlStrEqual(type, BAD_CAST "error")) {
390         test_log("Skipping error test %s \n", (char *) id);
391 	ret = 0;
392 	nb_skipped++;
393 	goto error;
394     } else {
395         test_log("test %s unknown TYPE value %s\n", (char *) id, (char *)type);
396 	ret = -1;
397 	goto error;
398     }
399 
400     /*
401      * Reset errors and check memory usage after the test
402      */
403     xmlResetLastError();
404     final = xmlMemUsed();
405     if (final > mem) {
406         test_log("test %s : %s leaked %d bytes\n",
407 	         id, filename, final - mem);
408         nb_leaks++;
409 	xmlMemDisplayLast(logfile, final - mem);
410     }
411     nb_tests++;
412 
413 error:
414     if (type != NULL)
415         xmlFree(type);
416     if (entities != NULL)
417         xmlFree(entities);
418     if (edition != NULL)
419         xmlFree(edition);
420     if (version != NULL)
421         xmlFree(version);
422     if (filename != NULL)
423         xmlFree(filename);
424     if (uri != NULL)
425         xmlFree(uri);
426     if (base != NULL)
427         xmlFree(base);
428     if (id != NULL)
429         xmlFree(id);
430     if (rec != NULL)
431         xmlFree(rec);
432     return(ret);
433 }
434 
435 static int
xmlconfTestCases(xmlDocPtr doc,xmlNodePtr cur,int level)436 xmlconfTestCases(xmlDocPtr doc, xmlNodePtr cur, int level) {
437     xmlChar *profile;
438     int ret = 0;
439     int tests = 0;
440     int output = 0;
441 
442     if (level == 1) {
443 	profile = xmlGetProp(cur, BAD_CAST "PROFILE");
444 	if (profile != NULL) {
445 	    output = 1;
446 	    level++;
447 	    printf("Test cases: %s\n", (char *) profile);
448 	    xmlFree(profile);
449 	}
450     }
451     cur = cur->children;
452     while (cur != NULL) {
453         /* look only at elements we ignore everything else */
454         if (cur->type == XML_ELEMENT_NODE) {
455 	    if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
456 	        ret += xmlconfTestCases(doc, cur, level);
457 	    } else if (xmlStrEqual(cur->name, BAD_CAST "TEST")) {
458 	        if (xmlconfTestItem(doc, cur) >= 0)
459 		    ret++;
460 		tests++;
461 	    } else {
462 	        fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
463 	    }
464 	}
465         cur = cur->next;
466     }
467     if (output == 1) {
468 	if (tests > 0)
469 	    printf("Test cases: %d tests\n", tests);
470     }
471     return(ret);
472 }
473 
474 static int
xmlconfTestSuite(xmlDocPtr doc,xmlNodePtr cur)475 xmlconfTestSuite(xmlDocPtr doc, xmlNodePtr cur) {
476     xmlChar *profile;
477     int ret = 0;
478 
479     profile = xmlGetProp(cur, BAD_CAST "PROFILE");
480     if (profile != NULL) {
481         printf("Test suite: %s\n", (char *) profile);
482 	xmlFree(profile);
483     } else
484         printf("Test suite\n");
485     cur = cur->children;
486     while (cur != NULL) {
487         /* look only at elements we ignore everything else */
488         if (cur->type == XML_ELEMENT_NODE) {
489 	    if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
490 	        ret += xmlconfTestCases(doc, cur, 1);
491 	    } else {
492 	        fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
493 	    }
494 	}
495         cur = cur->next;
496     }
497     return(ret);
498 }
499 
500 static void
xmlconfInfo(void)501 xmlconfInfo(void) {
502     fprintf(stderr, "  you need to fetch and extract the\n");
503     fprintf(stderr, "  latest XML Conformance Test Suites\n");
504     fprintf(stderr, "  http://www.w3.org/XML/Test/xmlts20080827.tar.gz\n");
505     fprintf(stderr, "  see http://www.w3.org/XML/Test/ for information\n");
506 }
507 
508 static int
xmlconfTest(void)509 xmlconfTest(void) {
510     const char *confxml = "xmlconf/xmlconf.xml";
511     xmlDocPtr doc;
512     xmlNodePtr cur;
513     int ret = 0;
514 
515     if (!checkTestFile(confxml)) {
516         fprintf(stderr, "%s is missing \n", confxml);
517 	xmlconfInfo();
518 	return(-1);
519     }
520     doc = xmlReadFile(confxml, NULL, XML_PARSE_NOENT);
521     if (doc == NULL) {
522         fprintf(stderr, "%s is corrupted \n", confxml);
523 	xmlconfInfo();
524 	return(-1);
525     }
526 
527     cur = xmlDocGetRootElement(doc);
528     if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "TESTSUITE"))) {
529         fprintf(stderr, "Unexpected format %s\n", confxml);
530 	xmlconfInfo();
531 	ret = -1;
532     } else {
533         ret = xmlconfTestSuite(doc, cur);
534     }
535     xmlFreeDoc(doc);
536     return(ret);
537 }
538 
539 /************************************************************************
540  *									*
541  *		The driver for the tests				*
542  *									*
543  ************************************************************************/
544 
545 int
main(int argc ATTRIBUTE_UNUSED,char ** argv ATTRIBUTE_UNUSED)546 main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
547     int ret = 0;
548     int old_errors, old_tests, old_leaks;
549 
550     logfile = fopen(LOGFILE, "w");
551     if (logfile == NULL) {
552         fprintf(stderr,
553 	        "Could not open the log file, running in verbose mode\n");
554 	verbose = 1;
555     }
556     initializeLibxml2();
557 
558     if ((argc >= 2) && (!strcmp(argv[1], "-v")))
559         verbose = 1;
560 
561 
562     old_errors = nb_errors;
563     old_tests = nb_tests;
564     old_leaks = nb_leaks;
565     xmlconfTest();
566     if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
567 	printf("Ran %d tests, no errors\n", nb_tests - old_tests);
568     else
569 	printf("Ran %d tests, %d errors, %d leaks\n",
570 	       nb_tests - old_tests,
571 	       nb_errors - old_errors,
572 	       nb_leaks - old_leaks);
573     if ((nb_errors == 0) && (nb_leaks == 0)) {
574         ret = 0;
575 	printf("Total %d tests, no errors\n",
576 	       nb_tests);
577     } else {
578 	ret = 1;
579 	printf("Total %d tests, %d errors, %d leaks\n",
580 	       nb_tests, nb_errors, nb_leaks);
581 	printf("See %s for detailed output\n", LOGFILE);
582 	if ((nb_leaks == 0) && (nb_errors == NB_EXPECTED_ERRORS)) {
583 	    printf("%d errors were expected\n", nb_errors);
584 	    ret = 0;
585 	}
586     }
587     xmlXPathFreeContext(ctxtXPath);
588     xmlCleanupParser();
589     xmlMemoryDump();
590 
591     if (logfile != NULL)
592         fclose(logfile);
593     return(ret);
594 }
595 
596 #else /* ! LIBXML_XPATH_ENABLED */
597 #include <stdio.h>
598 int
main(int argc ATTRIBUTE_UNUSED,char ** argv)599 main(int argc ATTRIBUTE_UNUSED, char **argv) {
600     fprintf(stderr, "%s need XPath support\n", argv[0]);
601 }
602 #endif
603