xref: /aosp_15_r20/external/libxml2/runxmlconf.c (revision 7c5688314b92172186c154356a6374bf7684c3ca)
1 /*
2  * runxmlconf.c: C program to run XML W3C conformance testsuites
3  *
4  * See Copyright for the status of this software.
5  *
6  * [email protected]
7  */
8 
9 #include "libxml.h"
10 #include <stdio.h>
11 #include <libxml/xmlversion.h>
12 
13 #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_VALID_ENABLED)
14 
15 #include <string.h>
16 #include <sys/stat.h>
17 
18 #include <libxml/catalog.h>
19 #include <libxml/parser.h>
20 #include <libxml/parserInternals.h>
21 #include <libxml/tree.h>
22 #include <libxml/uri.h>
23 #include <libxml/xmlreader.h>
24 
25 #include <libxml/xpath.h>
26 #include <libxml/xpathInternals.h>
27 
28 #define LOGFILE "runxmlconf.log"
29 static FILE *logfile = NULL;
30 static int verbose = 0;
31 
32 #define NB_EXPECTED_ERRORS 15
33 
34 
35 const char *skipped_tests[] = {
36 /* http://lists.w3.org/Archives/Public/public-xml-testsuite/2008Jul/0000.html */
37     "rmt-ns10-035",
38     NULL
39 };
40 
41 /************************************************************************
42  *									*
43  *		File name and path utilities				*
44  *									*
45  ************************************************************************/
46 
checkTestFile(const char * filename)47 static int checkTestFile(const char *filename) {
48     struct stat buf;
49 
50     if (stat(filename, &buf) == -1)
51         return(0);
52 
53 #if defined(_WIN32)
54     if (!(buf.st_mode & _S_IFREG))
55         return(0);
56 #else
57     if (!S_ISREG(buf.st_mode))
58         return(0);
59 #endif
60 
61     return(1);
62 }
63 
composeDir(const xmlChar * dir,const xmlChar * path)64 static xmlChar *composeDir(const xmlChar *dir, const xmlChar *path) {
65     char buf[500];
66 
67     if (dir == NULL) return(xmlStrdup(path));
68     if (path == NULL) return(NULL);
69 
70     snprintf(buf, 500, "%s/%s", (const char *) dir, (const char *) path);
71     return(xmlStrdup((const xmlChar *) buf));
72 }
73 
74 /************************************************************************
75  *									*
76  *		Libxml2 specific routines				*
77  *									*
78  ************************************************************************/
79 
80 static int nb_skipped = 0;
81 static int nb_tests = 0;
82 static int nb_errors = 0;
83 static int nb_leaks = 0;
84 
85 /*
86  * Trapping the error messages at the generic level to grab the equivalent of
87  * stderr messages on CLI tools.
88  */
89 static char testErrors[32769];
90 static int testErrorsSize = 0;
91 static int nbError = 0;
92 static int nbFatal = 0;
93 
test_log(const char * msg,...)94 static void test_log(const char *msg, ...) {
95     va_list args;
96     if (logfile != NULL) {
97         fprintf(logfile, "\n------------\n");
98 	va_start(args, msg);
99 	vfprintf(logfile, msg, args);
100 	va_end(args);
101 	fprintf(logfile, "%s", testErrors);
102 	testErrorsSize = 0; testErrors[0] = 0;
103     }
104     if (verbose) {
105 	va_start(args, msg);
106 	vfprintf(stderr, msg, args);
107 	va_end(args);
108     }
109 }
110 
111 static void
testErrorHandler(void * userData ATTRIBUTE_UNUSED,const xmlError * error)112 testErrorHandler(void *userData ATTRIBUTE_UNUSED, const xmlError *error) {
113     int res;
114 
115     if (testErrorsSize >= 32768)
116         return;
117     res = snprintf(&testErrors[testErrorsSize],
118                     32768 - testErrorsSize,
119 		   "%s:%d: %s\n", (error->file ? error->file : "entity"),
120 		   error->line, error->message);
121     if (error->level == XML_ERR_FATAL)
122         nbFatal++;
123     else if (error->level == XML_ERR_ERROR)
124         nbError++;
125     if (testErrorsSize + res >= 32768) {
126         /* buffer is full */
127 	testErrorsSize = 32768;
128 	testErrors[testErrorsSize] = 0;
129     } else {
130         testErrorsSize += res;
131     }
132     testErrors[testErrorsSize] = 0;
133 }
134 
135 static xmlXPathContextPtr ctxtXPath;
136 
137 static void
initializeLibxml2(void)138 initializeLibxml2(void) {
139     xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
140     xmlInitParser();
141 #ifdef LIBXML_CATALOG_ENABLED
142     xmlInitializeCatalog();
143     xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE);
144 #endif
145     ctxtXPath = xmlXPathNewContext(NULL);
146     /*
147     * Deactivate the cache if created; otherwise we have to create/free it
148     * for every test, since it will confuse the memory leak detection.
149     * Note that normally this need not be done, since the cache is not
150     * created until set explicitly with xmlXPathContextSetCache();
151     * but for test purposes it is sometimes useful to activate the
152     * cache by default for the whole library.
153     */
154     if (ctxtXPath->cache != NULL)
155 	xmlXPathContextSetCache(ctxtXPath, 0, -1, 0);
156 }
157 
158 /************************************************************************
159  *									*
160  *		Run the xmlconf test if found				*
161  *									*
162  ************************************************************************/
163 
164 static int
xmlconfTestInvalid(const char * id,const char * filename,int options)165 xmlconfTestInvalid(const char *id, const char *filename, int options) {
166     xmlDocPtr doc;
167     xmlParserCtxtPtr ctxt;
168     int ret = 1;
169 
170     ctxt = xmlNewParserCtxt();
171     if (ctxt == NULL) {
172         test_log("test %s : %s out of memory\n",
173 	         id, filename);
174         return(0);
175     }
176     xmlCtxtSetErrorHandler(ctxt, testErrorHandler, NULL);
177     doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
178     if (doc == NULL) {
179         test_log("test %s : %s invalid document turned not well-formed too\n",
180 	         id, filename);
181     } else {
182     /* invalidity should be reported both in the context and in the document */
183         if ((ctxt->valid != 0) || (doc->properties & XML_DOC_DTDVALID)) {
184 	    test_log("test %s : %s failed to detect invalid document\n",
185 		     id, filename);
186 	    nb_errors++;
187 	    ret = 0;
188 	}
189 	xmlFreeDoc(doc);
190     }
191     xmlFreeParserCtxt(ctxt);
192     return(ret);
193 }
194 
195 static int
xmlconfTestValid(const char * id,const char * filename,int options)196 xmlconfTestValid(const char *id, const char *filename, int options) {
197     xmlDocPtr doc;
198     xmlParserCtxtPtr ctxt;
199     int ret = 1;
200 
201     ctxt = xmlNewParserCtxt();
202     if (ctxt == NULL) {
203         test_log("test %s : %s out of memory\n",
204 	         id, filename);
205         return(0);
206     }
207     xmlCtxtSetErrorHandler(ctxt, testErrorHandler, NULL);
208     doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
209     if (doc == NULL) {
210         test_log("test %s : %s failed to parse a valid document\n",
211 	         id, filename);
212         nb_errors++;
213 	ret = 0;
214     } else {
215     /* validity should be reported both in the context and in the document */
216         if ((ctxt->valid == 0) || ((doc->properties & XML_DOC_DTDVALID) == 0)) {
217 	    test_log("test %s : %s failed to validate a valid document\n",
218 		     id, filename);
219 	    nb_errors++;
220 	    ret = 0;
221 	}
222 	xmlFreeDoc(doc);
223     }
224     xmlFreeParserCtxt(ctxt);
225     return(ret);
226 }
227 
228 static int
xmlconfTestNotNSWF(const char * id,const char * filename,int options)229 xmlconfTestNotNSWF(const char *id, const char *filename, int options) {
230     xmlParserCtxtPtr ctxt;
231     xmlDocPtr doc;
232     int ret = 1;
233 
234     ctxt = xmlNewParserCtxt();
235     xmlCtxtSetErrorHandler(ctxt, testErrorHandler, NULL);
236     /*
237      * In case of Namespace errors, libxml2 will still parse the document
238      * but log a Namespace error.
239      */
240     doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
241     if (doc == NULL) {
242         test_log("test %s : %s failed to parse the XML\n",
243 	         id, filename);
244         nb_errors++;
245 	ret = 0;
246     } else {
247         const xmlError *error = xmlGetLastError();
248 
249 	if ((error->code == XML_ERR_OK) ||
250 	    (error->domain != XML_FROM_NAMESPACE)) {
251 	    test_log("test %s : %s failed to detect namespace error\n",
252 		     id, filename);
253 	    nb_errors++;
254 	    ret = 0;
255 	}
256 	xmlFreeDoc(doc);
257     }
258     xmlFreeParserCtxt(ctxt);
259     return(ret);
260 }
261 
262 static int
xmlconfTestNotWF(const char * id,const char * filename,int options)263 xmlconfTestNotWF(const char *id, const char *filename, int options) {
264     xmlParserCtxtPtr ctxt;
265     xmlDocPtr doc;
266     int ret = 1;
267 
268     ctxt = xmlNewParserCtxt();
269     xmlCtxtSetErrorHandler(ctxt, testErrorHandler, NULL);
270     doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
271     if (doc != NULL) {
272         test_log("test %s : %s failed to detect not well formedness\n",
273 	         id, filename);
274         nb_errors++;
275 	xmlFreeDoc(doc);
276 	ret = 0;
277     }
278     xmlFreeParserCtxt(ctxt);
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     }
409     nb_tests++;
410 
411 error:
412     if (type != NULL)
413         xmlFree(type);
414     if (entities != NULL)
415         xmlFree(entities);
416     if (edition != NULL)
417         xmlFree(edition);
418     if (version != NULL)
419         xmlFree(version);
420     if (filename != NULL)
421         xmlFree(filename);
422     if (uri != NULL)
423         xmlFree(uri);
424     if (base != NULL)
425         xmlFree(base);
426     if (id != NULL)
427         xmlFree(id);
428     if (rec != NULL)
429         xmlFree(rec);
430     return(ret);
431 }
432 
433 static int
xmlconfTestCases(xmlDocPtr doc,xmlNodePtr cur,int level)434 xmlconfTestCases(xmlDocPtr doc, xmlNodePtr cur, int level) {
435     xmlChar *profile;
436     int ret = 0;
437     int tests = 0;
438     int output = 0;
439 
440     if (level == 1) {
441 	profile = xmlGetProp(cur, BAD_CAST "PROFILE");
442 	if (profile != NULL) {
443 	    output = 1;
444 	    level++;
445 	    printf("Test cases: %s\n", (char *) profile);
446 	    xmlFree(profile);
447 	}
448     }
449     cur = cur->children;
450     while (cur != NULL) {
451         /* look only at elements we ignore everything else */
452         if (cur->type == XML_ELEMENT_NODE) {
453 	    if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
454 	        ret += xmlconfTestCases(doc, cur, level);
455 	    } else if (xmlStrEqual(cur->name, BAD_CAST "TEST")) {
456 	        if (xmlconfTestItem(doc, cur) >= 0)
457 		    ret++;
458 		tests++;
459 	    } else {
460 	        fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
461 	    }
462 	}
463         cur = cur->next;
464     }
465     if (output == 1) {
466 	if (tests > 0)
467 	    printf("Test cases: %d tests\n", tests);
468     }
469     return(ret);
470 }
471 
472 static int
xmlconfTestSuite(xmlDocPtr doc,xmlNodePtr cur)473 xmlconfTestSuite(xmlDocPtr doc, xmlNodePtr cur) {
474     xmlChar *profile;
475     int ret = 0;
476 
477     profile = xmlGetProp(cur, BAD_CAST "PROFILE");
478     if (profile != NULL) {
479         printf("Test suite: %s\n", (char *) profile);
480 	xmlFree(profile);
481     } else
482         printf("Test suite\n");
483     cur = cur->children;
484     while (cur != NULL) {
485         /* look only at elements we ignore everything else */
486         if (cur->type == XML_ELEMENT_NODE) {
487 	    if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
488 	        ret += xmlconfTestCases(doc, cur, 1);
489 	    } else {
490 	        fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
491 	    }
492 	}
493         cur = cur->next;
494     }
495     return(ret);
496 }
497 
498 static void
xmlconfInfo(void)499 xmlconfInfo(void) {
500     fprintf(stderr, "  you need to fetch and extract the\n");
501     fprintf(stderr, "  latest XML Conformance Test Suites\n");
502     fprintf(stderr, "  http://www.w3.org/XML/Test/xmlts20080827.tar.gz\n");
503     fprintf(stderr, "  see http://www.w3.org/XML/Test/ for information\n");
504 }
505 
506 static int
xmlconfTest(void)507 xmlconfTest(void) {
508     const char *confxml = "xmlconf/xmlconf.xml";
509     xmlDocPtr doc;
510     xmlNodePtr cur;
511     int ret = 0;
512 
513     if (!checkTestFile(confxml)) {
514         fprintf(stderr, "%s is missing \n", confxml);
515 	xmlconfInfo();
516 	return(-1);
517     }
518     doc = xmlReadFile(confxml, NULL, XML_PARSE_NOENT);
519     if (doc == NULL) {
520         fprintf(stderr, "%s is corrupted \n", confxml);
521 	xmlconfInfo();
522 	return(-1);
523     }
524 
525     cur = xmlDocGetRootElement(doc);
526     if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "TESTSUITE"))) {
527         fprintf(stderr, "Unexpected format %s\n", confxml);
528 	xmlconfInfo();
529 	ret = -1;
530     } else {
531         ret = xmlconfTestSuite(doc, cur);
532     }
533     xmlFreeDoc(doc);
534     return(ret);
535 }
536 
537 /************************************************************************
538  *									*
539  *		The driver for the tests				*
540  *									*
541  ************************************************************************/
542 
543 int
main(int argc ATTRIBUTE_UNUSED,char ** argv ATTRIBUTE_UNUSED)544 main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
545     int ret = 0;
546     int old_errors, old_tests, old_leaks;
547 
548     logfile = fopen(LOGFILE, "wb");
549     if (logfile == NULL) {
550         fprintf(stderr,
551 	        "Could not open the log file, running in verbose mode\n");
552 	verbose = 1;
553     }
554     initializeLibxml2();
555 
556     if ((argc >= 2) && (!strcmp(argv[1], "-v")))
557         verbose = 1;
558 
559 
560     old_errors = nb_errors;
561     old_tests = nb_tests;
562     old_leaks = nb_leaks;
563     xmlconfTest();
564     if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
565 	printf("Ran %d tests, no errors\n", nb_tests - old_tests);
566     else
567 	printf("Ran %d tests, %d errors, %d leaks\n",
568 	       nb_tests - old_tests,
569 	       nb_errors - old_errors,
570 	       nb_leaks - old_leaks);
571     if ((nb_errors == 0) && (nb_leaks == 0)) {
572         ret = 0;
573 	printf("Total %d tests, no errors\n",
574 	       nb_tests);
575     } else {
576 	ret = 1;
577 	printf("Total %d tests, %d errors, %d leaks\n",
578 	       nb_tests, nb_errors, nb_leaks);
579 	printf("See %s for detailed output\n", LOGFILE);
580 	if ((nb_leaks == 0) && (nb_errors == NB_EXPECTED_ERRORS)) {
581 	    printf("%d errors were expected\n", nb_errors);
582 	    ret = 0;
583 	}
584     }
585     xmlXPathFreeContext(ctxtXPath);
586     xmlCleanupParser();
587 
588     if (logfile != NULL)
589         fclose(logfile);
590     return(ret);
591 }
592 
593 #else /* ! LIBXML_XPATH_ENABLED */
594 int
main(int argc ATTRIBUTE_UNUSED,char ** argv)595 main(int argc ATTRIBUTE_UNUSED, char **argv) {
596     fprintf(stderr, "%s need XPath and validation support\n", argv[0]);
597     return(0);
598 }
599 #endif
600