2 * runsuite.c: C program to run libxml2 againts published testsuites
4 * See Copyright for the status of this software.
15 #ifdef LIBXML_XPATH_ENABLED
17 #if !defined(_WIN32) || defined(__CYGWIN__)
21 #include <sys/types.h>
25 #include <libxml/parser.h>
26 #include <libxml/parserInternals.h>
27 #include <libxml/tree.h>
28 #include <libxml/uri.h>
29 #include <libxml/xmlreader.h>
31 #include <libxml/xpath.h>
32 #include <libxml/xpathInternals.h>
34 #define LOGFILE "runxmlconf.log"
35 static FILE *logfile
= NULL
;
36 static int verbose
= 0;
38 #define NB_EXPECTED_ERRORS 15
40 #if defined(_WIN32) && !defined(__CYGWIN__)
42 #define vsnprintf _vsnprintf
44 #define snprintf _snprintf
48 const char *skipped_tests
[] = {
49 /* http://lists.w3.org/Archives/Public/public-xml-testsuite/2008Jul/0000.html */
54 /************************************************************************
56 * File name and path utilities *
58 ************************************************************************/
60 static int checkTestFile(const char *filename
) {
63 if (stat(filename
, &buf
) == -1)
66 #if defined(_WIN32) && !defined(__CYGWIN__)
67 if (!(buf
.st_mode
& _S_IFREG
))
70 if (!S_ISREG(buf
.st_mode
))
77 static xmlChar
*composeDir(const xmlChar
*dir
, const xmlChar
*path
) {
80 if (dir
== NULL
) return(xmlStrdup(path
));
81 if (path
== NULL
) return(NULL
);
83 snprintf(buf
, 500, "%s/%s", (const char *) dir
, (const char *) path
);
84 return(xmlStrdup((const xmlChar
*) buf
));
87 /************************************************************************
89 * Libxml2 specific routines *
91 ************************************************************************/
93 static int nb_skipped
= 0;
94 static int nb_tests
= 0;
95 static int nb_errors
= 0;
96 static int nb_leaks
= 0;
99 * We need to trap calls to the resolver to not account memory for the catalog
100 * and not rely on any external resources.
102 static xmlParserInputPtr
103 testExternalEntityLoader(const char *URL
, const char *ID ATTRIBUTE_UNUSED
,
104 xmlParserCtxtPtr ctxt
) {
105 xmlParserInputPtr ret
;
107 ret
= xmlNewInputFromFile(ctxt
, (const char *) URL
);
113 * Trapping the error messages at the generic level to grab the equivalent of
114 * stderr messages on CLI tools.
116 static char testErrors
[32769];
117 static int testErrorsSize
= 0;
118 static int nbError
= 0;
119 static int nbFatal
= 0;
121 static void test_log(const char *msg
, ...) {
123 if (logfile
!= NULL
) {
124 fprintf(logfile
, "\n------------\n");
126 vfprintf(logfile
, msg
, args
);
128 fprintf(logfile
, "%s", testErrors
);
129 testErrorsSize
= 0; testErrors
[0] = 0;
133 vfprintf(stderr
, msg
, args
);
139 testErrorHandler(void *userData ATTRIBUTE_UNUSED
, xmlErrorPtr error
) {
142 if (testErrorsSize
>= 32768)
144 res
= snprintf(&testErrors
[testErrorsSize
],
145 32768 - testErrorsSize
,
146 "%s:%d: %s\n", (error
->file
? error
->file
: "entity"),
147 error
->line
, error
->message
);
148 if (error
->level
== XML_ERR_FATAL
)
150 else if (error
->level
== XML_ERR_ERROR
)
152 if (testErrorsSize
+ res
>= 32768) {
154 testErrorsSize
= 32768;
155 testErrors
[testErrorsSize
] = 0;
157 testErrorsSize
+= res
;
159 testErrors
[testErrorsSize
] = 0;
162 static xmlXPathContextPtr ctxtXPath
;
165 initializeLibxml2(void) {
166 xmlGetWarningsDefaultValue
= 0;
167 xmlPedanticParserDefault(0);
169 xmlMemSetup(xmlMemFree
, xmlMemMalloc
, xmlMemRealloc
, xmlMemoryStrdup
);
171 xmlSetExternalEntityLoader(testExternalEntityLoader
);
172 ctxtXPath
= xmlXPathNewContext(NULL
);
174 * Deactivate the cache if created; otherwise we have to create/free it
175 * for every test, since it will confuse the memory leak detection.
176 * Note that normally this need not be done, since the cache is not
177 * created until set explicitely with xmlXPathContextSetCache();
178 * but for test purposes it is sometimes usefull to activate the
179 * cache by default for the whole library.
181 if (ctxtXPath
->cache
!= NULL
)
182 xmlXPathContextSetCache(ctxtXPath
, 0, -1, 0);
183 xmlSetStructuredErrorFunc(NULL
, testErrorHandler
);
186 /************************************************************************
188 * Run the xmlconf test if found *
190 ************************************************************************/
193 xmlconfTestInvalid(const char *id
, const char *filename
, int options
) {
195 xmlParserCtxtPtr ctxt
;
198 ctxt
= xmlNewParserCtxt();
200 test_log("test %s : %s out of memory\n",
204 doc
= xmlCtxtReadFile(ctxt
, filename
, NULL
, options
);
206 test_log("test %s : %s invalid document turned not well-formed too\n",
209 /* invalidity should be reported both in the context and in the document */
210 if ((ctxt
->valid
!= 0) || (doc
->properties
& XML_DOC_DTDVALID
)) {
211 test_log("test %s : %s failed to detect invalid document\n",
218 xmlFreeParserCtxt(ctxt
);
223 xmlconfTestValid(const char *id
, const char *filename
, int options
) {
225 xmlParserCtxtPtr ctxt
;
228 ctxt
= xmlNewParserCtxt();
230 test_log("test %s : %s out of memory\n",
234 doc
= xmlCtxtReadFile(ctxt
, filename
, NULL
, options
);
236 test_log("test %s : %s failed to parse a valid document\n",
241 /* validity should be reported both in the context and in the document */
242 if ((ctxt
->valid
== 0) || ((doc
->properties
& XML_DOC_DTDVALID
) == 0)) {
243 test_log("test %s : %s failed to validate a valid document\n",
250 xmlFreeParserCtxt(ctxt
);
255 xmlconfTestNotNSWF(const char *id
, const char *filename
, int options
) {
260 * In case of Namespace errors, libxml2 will still parse the document
261 * but log a Namesapce error.
263 doc
= xmlReadFile(filename
, NULL
, options
);
265 test_log("test %s : %s failed to parse the XML\n",
270 if ((xmlLastError
.code
== XML_ERR_OK
) ||
271 (xmlLastError
.domain
!= XML_FROM_NAMESPACE
)) {
272 test_log("test %s : %s failed to detect namespace error\n",
283 xmlconfTestNotWF(const char *id
, const char *filename
, int options
) {
287 doc
= xmlReadFile(filename
, NULL
, options
);
289 test_log("test %s : %s failed to detect not well formedness\n",
299 xmlconfTestItem(xmlDocPtr doc
, xmlNodePtr cur
) {
301 xmlChar
*type
= NULL
;
302 xmlChar
*filename
= NULL
;
304 xmlChar
*base
= NULL
;
307 xmlChar
*version
= NULL
;
308 xmlChar
*entities
= NULL
;
309 xmlChar
*edition
= NULL
;
315 testErrorsSize
= 0; testErrors
[0] = 0;
318 id
= xmlGetProp(cur
, BAD_CAST
"ID");
320 test_log("test missing ID, line %ld\n", xmlGetLineNo(cur
));
323 for (i
= 0;skipped_tests
[i
] != NULL
;i
++) {
324 if (!strcmp(skipped_tests
[i
], (char *) id
)) {
325 test_log("Skipping test %s from skipped list\n", (char *) id
);
331 type
= xmlGetProp(cur
, BAD_CAST
"TYPE");
333 test_log("test %s missing TYPE\n", (char *) id
);
336 uri
= xmlGetProp(cur
, BAD_CAST
"URI");
338 test_log("test %s missing URI\n", (char *) id
);
341 base
= xmlNodeGetBase(doc
, cur
);
342 filename
= composeDir(base
, uri
);
343 if (!checkTestFile((char *) filename
)) {
344 test_log("test %s missing file %s \n", id
,
345 (filename
? (char *)filename
: "NULL"));
349 version
= xmlGetProp(cur
, BAD_CAST
"VERSION");
351 entities
= xmlGetProp(cur
, BAD_CAST
"ENTITIES");
352 if (!xmlStrEqual(entities
, BAD_CAST
"none")) {
353 options
|= XML_PARSE_DTDLOAD
;
354 options
|= XML_PARSE_NOENT
;
356 rec
= xmlGetProp(cur
, BAD_CAST
"RECOMMENDATION");
358 (xmlStrEqual(rec
, BAD_CAST
"XML1.0")) ||
359 (xmlStrEqual(rec
, BAD_CAST
"XML1.0-errata2e")) ||
360 (xmlStrEqual(rec
, BAD_CAST
"XML1.0-errata3e")) ||
361 (xmlStrEqual(rec
, BAD_CAST
"XML1.0-errata4e"))) {
362 if ((version
!= NULL
) && (!xmlStrEqual(version
, BAD_CAST
"1.0"))) {
363 test_log("Skipping test %s for %s\n", (char *) id
,
370 } else if ((xmlStrEqual(rec
, BAD_CAST
"NS1.0")) ||
371 (xmlStrEqual(rec
, BAD_CAST
"NS1.0-errata1e"))) {
375 test_log("Skipping test %s for REC %s\n", (char *) id
, (char *) rec
);
380 edition
= xmlGetProp(cur
, BAD_CAST
"EDITION");
381 if ((edition
!= NULL
) && (xmlStrchr(edition
, '5') == NULL
)) {
382 /* test limited to all versions before 5th */
383 options
|= XML_PARSE_OLD10
;
387 * Reset errors and check memory usage before the test
390 testErrorsSize
= 0; testErrors
[0] = 0;
393 if (xmlStrEqual(type
, BAD_CAST
"not-wf")) {
395 xmlconfTestNotWF((char *) id
, (char *) filename
, options
);
397 xmlconfTestNotNSWF((char *) id
, (char *) filename
, options
);
398 } else if (xmlStrEqual(type
, BAD_CAST
"valid")) {
399 options
|= XML_PARSE_DTDVALID
;
400 xmlconfTestValid((char *) id
, (char *) filename
, options
);
401 } else if (xmlStrEqual(type
, BAD_CAST
"invalid")) {
402 options
|= XML_PARSE_DTDVALID
;
403 xmlconfTestInvalid((char *) id
, (char *) filename
, options
);
404 } else if (xmlStrEqual(type
, BAD_CAST
"error")) {
405 test_log("Skipping error test %s \n", (char *) id
);
410 test_log("test %s unknown TYPE value %s\n", (char *) id
, (char *)type
);
416 * Reset errors and check memory usage after the test
419 final
= xmlMemUsed();
421 test_log("test %s : %s leaked %d bytes\n",
422 id
, filename
, final
- mem
);
424 xmlMemDisplayLast(logfile
, final
- mem
);
431 if (entities
!= NULL
)
437 if (filename
!= NULL
)
451 xmlconfTestCases(xmlDocPtr doc
, xmlNodePtr cur
, int level
) {
458 profile
= xmlGetProp(cur
, BAD_CAST
"PROFILE");
459 if (profile
!= NULL
) {
462 printf("Test cases: %s\n", (char *) profile
);
467 while (cur
!= NULL
) {
468 /* look only at elements we ignore everything else */
469 if (cur
->type
== XML_ELEMENT_NODE
) {
470 if (xmlStrEqual(cur
->name
, BAD_CAST
"TESTCASES")) {
471 ret
+= xmlconfTestCases(doc
, cur
, level
);
472 } else if (xmlStrEqual(cur
->name
, BAD_CAST
"TEST")) {
473 if (xmlconfTestItem(doc
, cur
) >= 0)
477 fprintf(stderr
, "Unhandled element %s\n", (char *)cur
->name
);
484 printf("Test cases: %d tests\n", tests
);
490 xmlconfTestSuite(xmlDocPtr doc
, xmlNodePtr cur
) {
494 profile
= xmlGetProp(cur
, BAD_CAST
"PROFILE");
495 if (profile
!= NULL
) {
496 printf("Test suite: %s\n", (char *) profile
);
499 printf("Test suite\n");
501 while (cur
!= NULL
) {
502 /* look only at elements we ignore everything else */
503 if (cur
->type
== XML_ELEMENT_NODE
) {
504 if (xmlStrEqual(cur
->name
, BAD_CAST
"TESTCASES")) {
505 ret
+= xmlconfTestCases(doc
, cur
, 1);
507 fprintf(stderr
, "Unhandled element %s\n", (char *)cur
->name
);
517 fprintf(stderr
, " you need to fetch and extract the\n");
518 fprintf(stderr
, " latest XML Conformance Test Suites\n");
519 fprintf(stderr
, " http://www.w3.org/XML/Test/xmlts20080205.tar.gz\n");
520 fprintf(stderr
, " see http://www.w3.org/XML/Test/ for informations\n");
525 const char *confxml
= "xmlconf/xmlconf.xml";
530 if (!checkTestFile(confxml
)) {
531 fprintf(stderr
, "%s is missing \n", confxml
);
535 doc
= xmlReadFile(confxml
, NULL
, XML_PARSE_NOENT
);
537 fprintf(stderr
, "%s is corrupted \n", confxml
);
542 cur
= xmlDocGetRootElement(doc
);
543 if ((cur
== NULL
) || (!xmlStrEqual(cur
->name
, BAD_CAST
"TESTSUITE"))) {
544 fprintf(stderr
, "Unexpected format %s\n", confxml
);
548 ret
= xmlconfTestSuite(doc
, cur
);
554 /************************************************************************
556 * The driver for the tests *
558 ************************************************************************/
561 main(int argc ATTRIBUTE_UNUSED
, char **argv ATTRIBUTE_UNUSED
) {
563 int old_errors
, old_tests
, old_leaks
;
565 logfile
= fopen(LOGFILE
, "w");
566 if (logfile
== NULL
) {
568 "Could not open the log file, running in verbose mode\n");
573 if ((argc
>= 2) && (!strcmp(argv
[1], "-v")))
577 old_errors
= nb_errors
;
578 old_tests
= nb_tests
;
579 old_leaks
= nb_leaks
;
581 if ((nb_errors
== old_errors
) && (nb_leaks
== old_leaks
))
582 printf("Ran %d tests, no errors\n", nb_tests
- old_tests
);
584 printf("Ran %d tests, %d errors, %d leaks\n",
585 nb_tests
- old_tests
,
586 nb_errors
- old_errors
,
587 nb_leaks
- old_leaks
);
588 if ((nb_errors
== 0) && (nb_leaks
== 0)) {
590 printf("Total %d tests, no errors\n",
594 printf("Total %d tests, %d errors, %d leaks\n",
595 nb_tests
, nb_errors
, nb_leaks
);
596 printf("See %s for detailed output\n", LOGFILE
);
597 if ((nb_leaks
== 0) && (nb_errors
== NB_EXPECTED_ERRORS
)) {
598 printf("%d errors were expected\n", nb_errors
);
602 xmlXPathFreeContext(ctxtXPath
);
611 #else /* ! LIBXML_XPATH_ENABLED */
614 main(int argc
, char **argv
) {
615 fprintf(stderr
, "%s need XPath support\n", argv
[0]);