2 * runsuite.c: C program to run libxml2 againts published testsuites
4 * See Copyright for the status of this software.
12 #ifdef LIBXML_XPATH_ENABLED
14 #if !defined(_WIN32) || defined(__CYGWIN__)
18 #include <sys/types.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 */
44 /************************************************************************
46 * File name and path utilities *
48 ************************************************************************/
50 static int checkTestFile(const char *filename
) {
53 if (stat(filename
, &buf
) == -1)
56 #if defined(_WIN32) && !defined(__CYGWIN__)
57 if (!(buf
.st_mode
& _S_IFREG
))
60 if (!S_ISREG(buf
.st_mode
))
67 static xmlChar
*composeDir(const xmlChar
*dir
, const xmlChar
*path
) {
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 /************************************************************************
79 * Libxml2 specific routines *
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
);
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
, ...) {
113 if (logfile
!= NULL
) {
114 fprintf(logfile
, "\n------------\n");
116 vfprintf(logfile
, msg
, args
);
118 fprintf(logfile
, "%s", testErrors
);
119 testErrorsSize
= 0; testErrors
[0] = 0;
123 vfprintf(stderr
, msg
, args
);
129 testErrorHandler(void *userData ATTRIBUTE_UNUSED
, xmlErrorPtr error
) {
132 if (testErrorsSize
>= 32768)
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
)
140 else if (error
->level
== XML_ERR_ERROR
)
142 if (testErrorsSize
+ res
>= 32768) {
144 testErrorsSize
= 32768;
145 testErrors
[testErrorsSize
] = 0;
147 testErrorsSize
+= res
;
149 testErrors
[testErrorsSize
] = 0;
152 static xmlXPathContextPtr ctxtXPath
;
155 initializeLibxml2(void) {
156 xmlGetWarningsDefaultValue
= 0;
157 xmlPedanticParserDefault(0);
159 xmlMemSetup(xmlMemFree
, xmlMemMalloc
, xmlMemRealloc
, xmlMemoryStrdup
);
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 ************************************************************************/
183 xmlconfTestInvalid(const char *id
, const char *filename
, int options
) {
185 xmlParserCtxtPtr ctxt
;
188 ctxt
= xmlNewParserCtxt();
190 test_log("test %s : %s out of memory\n",
194 doc
= xmlCtxtReadFile(ctxt
, filename
, NULL
, options
);
196 test_log("test %s : %s invalid document turned not well-formed too\n",
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",
208 xmlFreeParserCtxt(ctxt
);
213 xmlconfTestValid(const char *id
, const char *filename
, int options
) {
215 xmlParserCtxtPtr ctxt
;
218 ctxt
= xmlNewParserCtxt();
220 test_log("test %s : %s out of memory\n",
224 doc
= xmlCtxtReadFile(ctxt
, filename
, NULL
, options
);
226 test_log("test %s : %s failed to parse a valid document\n",
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",
240 xmlFreeParserCtxt(ctxt
);
245 xmlconfTestNotNSWF(const char *id
, const char *filename
, int options
) {
250 * In case of Namespace errors, libxml2 will still parse the document
251 * but log a Namesapce error.
253 doc
= xmlReadFile(filename
, NULL
, options
);
255 test_log("test %s : %s failed to parse the XML\n",
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",
273 xmlconfTestNotWF(const char *id
, const char *filename
, int options
) {
277 doc
= xmlReadFile(filename
, NULL
, options
);
279 test_log("test %s : %s failed to detect not well formedness\n",
289 xmlconfTestItem(xmlDocPtr doc
, xmlNodePtr cur
) {
291 xmlChar
*type
= NULL
;
292 xmlChar
*filename
= NULL
;
294 xmlChar
*base
= NULL
;
297 xmlChar
*version
= NULL
;
298 xmlChar
*entities
= NULL
;
299 xmlChar
*edition
= NULL
;
305 testErrorsSize
= 0; testErrors
[0] = 0;
308 id
= xmlGetProp(cur
, BAD_CAST
"ID");
310 test_log("test missing ID, line %ld\n", xmlGetLineNo(cur
));
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
);
321 type
= xmlGetProp(cur
, BAD_CAST
"TYPE");
323 test_log("test %s missing TYPE\n", (char *) id
);
326 uri
= xmlGetProp(cur
, BAD_CAST
"URI");
328 test_log("test %s missing URI\n", (char *) id
);
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"));
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");
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
,
360 } else if ((xmlStrEqual(rec
, BAD_CAST
"NS1.0")) ||
361 (xmlStrEqual(rec
, BAD_CAST
"NS1.0-errata1e"))) {
365 test_log("Skipping test %s for REC %s\n", (char *) id
, (char *) rec
);
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
380 testErrorsSize
= 0; testErrors
[0] = 0;
383 if (xmlStrEqual(type
, BAD_CAST
"not-wf")) {
385 xmlconfTestNotWF((char *) id
, (char *) filename
, options
);
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
);
400 test_log("test %s unknown TYPE value %s\n", (char *) id
, (char *)type
);
406 * Reset errors and check memory usage after the test
409 final
= xmlMemUsed();
411 test_log("test %s : %s leaked %d bytes\n",
412 id
, filename
, final
- mem
);
414 xmlMemDisplayLast(logfile
, final
- mem
);
421 if (entities
!= NULL
)
427 if (filename
!= NULL
)
441 xmlconfTestCases(xmlDocPtr doc
, xmlNodePtr cur
, int level
) {
448 profile
= xmlGetProp(cur
, BAD_CAST
"PROFILE");
449 if (profile
!= NULL
) {
452 printf("Test cases: %s\n", (char *) profile
);
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)
467 fprintf(stderr
, "Unhandled element %s\n", (char *)cur
->name
);
474 printf("Test cases: %d tests\n", tests
);
480 xmlconfTestSuite(xmlDocPtr doc
, xmlNodePtr cur
) {
484 profile
= xmlGetProp(cur
, BAD_CAST
"PROFILE");
485 if (profile
!= NULL
) {
486 printf("Test suite: %s\n", (char *) profile
);
489 printf("Test suite\n");
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);
497 fprintf(stderr
, "Unhandled element %s\n", (char *)cur
->name
);
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");
515 const char *confxml
= "xmlconf/xmlconf.xml";
520 if (!checkTestFile(confxml
)) {
521 fprintf(stderr
, "%s is missing \n", confxml
);
525 doc
= xmlReadFile(confxml
, NULL
, XML_PARSE_NOENT
);
527 fprintf(stderr
, "%s is corrupted \n", confxml
);
532 cur
= xmlDocGetRootElement(doc
);
533 if ((cur
== NULL
) || (!xmlStrEqual(cur
->name
, BAD_CAST
"TESTSUITE"))) {
534 fprintf(stderr
, "Unexpected format %s\n", confxml
);
538 ret
= xmlconfTestSuite(doc
, cur
);
544 /************************************************************************
546 * The driver for the tests *
548 ************************************************************************/
551 main(int argc ATTRIBUTE_UNUSED
, char **argv ATTRIBUTE_UNUSED
) {
553 int old_errors
, old_tests
, old_leaks
;
555 logfile
= fopen(LOGFILE
, "w");
556 if (logfile
== NULL
) {
558 "Could not open the log file, running in verbose mode\n");
563 if ((argc
>= 2) && (!strcmp(argv
[1], "-v")))
567 old_errors
= nb_errors
;
568 old_tests
= nb_tests
;
569 old_leaks
= nb_leaks
;
571 if ((nb_errors
== old_errors
) && (nb_leaks
== old_leaks
))
572 printf("Ran %d tests, no errors\n", nb_tests
- old_tests
);
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)) {
580 printf("Total %d tests, no errors\n",
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
);
592 xmlXPathFreeContext(ctxtXPath
);
601 #else /* ! LIBXML_XPATH_ENABLED */
604 main(int argc
, char **argv
) {
605 fprintf(stderr
, "%s need XPath support\n", argv
[0]);