blob: fefbafdbd0674aa6efe8bc99934760df796fd789 [file] [log] [blame]
Daniel Veillardf2e066a2005-06-30 13:04:44 +00001/*
2 * runsuite.c: C program to run libxml2 againts external published testsuites
3 *
4 * See Copyright for the status of this software.
5 *
6 * daniel@veillard.com
7 */
8
9#include <unistd.h>
10#include <string.h>
11#include <stdio.h>
12#include <glob.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <fcntl.h>
16#include <unistd.h>
17
18#include <libxml/parser.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#include <libxml/relaxng.h>
27#include <libxml/xmlschemas.h>
28#include <libxml/xmlschemastypes.h>
29
30/************************************************************************
31 * *
32 * File name and path utilities *
33 * *
34 ************************************************************************/
35
36static int checkTestFile(const char *filename) {
37 struct stat buf;
38
39 if (stat(filename, &buf) == -1)
40 return(0);
41
42 if (!S_ISREG(buf.st_mode))
43 return(0);
44
45 return(1);
46}
47
48/************************************************************************
49 * *
50 * Libxml2 specific routines *
51 * *
52 ************************************************************************/
53
54static int nb_tests = 0;
55static int nb_errors = 0;
56static int nb_leaks = 0;
57static long libxmlMemoryAllocatedBase = 0;
58static int extraMemoryFromResolver = 0;
59
60static int
61fatalError(void) {
62 fprintf(stderr, "Exitting tests on fatal error\n");
63 exit(1);
64}
65
66/*
67 * We need to trap calls to the resolver to not account memory for the catalog
68 * which is shared to the current running test. We also don't want to have
69 * network downloads modifying tests.
70 */
71static xmlParserInputPtr
72testExternalEntityLoader(const char *URL, const char *ID,
73 xmlParserCtxtPtr ctxt) {
74 xmlParserInputPtr ret;
75
76 if (checkTestFile(URL)) {
77 ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
78 } else {
79 int memused = xmlMemUsed();
80 ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
81 extraMemoryFromResolver += xmlMemUsed() - memused;
82 }
83
84 return(ret);
85}
86
87/*
88 * Trapping the error messages at the generic level to grab the equivalent of
89 * stderr messages on CLI tools.
90 */
91static char testErrors[32769];
92static int testErrorsSize = 0;
93
94static void
95testErrorHandler(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
96 va_list args;
97 int res;
98
99 if (testErrorsSize >= 32768)
100 return;
101 va_start(args, msg);
102 res = vsnprintf(&testErrors[testErrorsSize],
103 32768 - testErrorsSize,
104 msg, args);
105 va_end(args);
106 if (testErrorsSize + res >= 32768) {
107 /* buffer is full */
108 testErrorsSize = 32768;
109 testErrors[testErrorsSize] = 0;
110 } else {
111 testErrorsSize += res;
112 }
113 testErrors[testErrorsSize] = 0;
114}
115static void
116initializeLibxml2(void) {
117 xmlGetWarningsDefaultValue = 0;
118 xmlPedanticParserDefault(0);
119
120 xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
121 xmlInitParser();
122 xmlSetExternalEntityLoader(testExternalEntityLoader);
123#ifdef LIBXML_SCHEMAS_ENABLED
124 xmlSchemaInitTypes();
125 xmlRelaxNGInitTypes();
126#endif
127 libxmlMemoryAllocatedBase = xmlMemUsed();
128}
129
130static xmlNodePtr
131getNext(xmlNodePtr cur, const char *xpath) {
132 xmlNodePtr ret = NULL;
133 xmlXPathObjectPtr res;
134 xmlXPathContextPtr ctxt;
135 xmlXPathCompExprPtr comp;
136
137 if ((cur == NULL) || (cur->doc == NULL) || (xpath == NULL))
138 return(NULL);
139 ctxt = xmlXPathNewContext(cur->doc);
140 ctxt->node = cur;
141 comp = xmlXPathCompile(BAD_CAST xpath);
142 if (comp == NULL) {
143 fprintf(stderr, "Failed to compile %s\n", xpath);
144 xmlXPathFreeContext(ctxt);
145 return(NULL);
146 }
147 res = xmlXPathCompiledEval(comp, ctxt);
148 xmlXPathFreeCompExpr(comp);
149 xmlXPathFreeContext(ctxt);
150 if (res == NULL)
151 return(NULL);
152 if ((res->type == XPATH_NODESET) &&
153 (res->nodesetval != NULL) &&
154 (res->nodesetval->nodeNr > 0) &&
155 (res->nodesetval->nodeTab != NULL))
156 ret = res->nodesetval->nodeTab[0];
157 xmlXPathFreeObject(res);
158 return(ret);
159}
160
161static xmlChar *
162getString(xmlNodePtr cur, const char *xpath) {
163 xmlChar *ret = NULL;
164 xmlXPathObjectPtr res;
165 xmlXPathContextPtr ctxt;
166 xmlXPathCompExprPtr comp;
167
168 if ((cur == NULL) || (cur->doc == NULL) || (xpath == NULL))
169 return(NULL);
170 ctxt = xmlXPathNewContext(cur->doc);
171 ctxt->node = cur;
172 comp = xmlXPathCompile(BAD_CAST xpath);
173 if (comp == NULL) {
174 fprintf(stderr, "Failed to compile %s\n", xpath);
175 return(NULL);
176 }
177 res = xmlXPathCompiledEval(comp, ctxt);
178 xmlXPathFreeCompExpr(comp);
179 xmlXPathFreeContext(ctxt);
180 if (res == NULL)
181 return(NULL);
182 if (res->type == XPATH_STRING) {
183 ret = res->stringval;
184 res->stringval = NULL;
185 }
186 xmlXPathFreeObject(res);
187 return(ret);
188}
189
190/************************************************************************
191 * *
192 * Test test/xsdtest/xsdtestsuite.xml *
193 * *
194 ************************************************************************/
195
196static int
197xsdIncorectTestCase(int verbose, xmlNodePtr cur) {
198 xmlNodePtr test;
199 xmlBufferPtr buf;
200 xmlRelaxNGParserCtxtPtr pctxt;
201 xmlRelaxNGPtr rng = NULL;
202 int ret = 0, memt;
203
204 cur = getNext(cur, "./incorrect[1]");
205 if (cur == NULL) {
206 return(0);
207 }
208
209 test = getNext(cur, "./*");
210 if (test == NULL) {
211 fprintf(stderr, "Failed to find test in correct line %ld\n",
212 xmlGetLineNo(cur));
213 return(1);
214 }
215
216 memt = xmlMemUsed();
217 extraMemoryFromResolver = 0;
218 /*
219 * dump the schemas to a buffer, then reparse it and compile the schemas
220 */
221 buf = xmlBufferCreate();
222 if (buf == NULL) {
223 fprintf(stderr, "out of memory !\n");
224 fatalError();
225 }
226 xmlNodeDump(buf, test->doc, test, 0, 0);
227 pctxt = xmlRelaxNGNewMemParserCtxt((const char *)buf->content, buf->use);
228 xmlRelaxNGSetParserErrors(pctxt,
229 (xmlRelaxNGValidityErrorFunc) testErrorHandler,
230 (xmlRelaxNGValidityWarningFunc) testErrorHandler,
231 pctxt);
232 rng = xmlRelaxNGParse(pctxt);
233 xmlRelaxNGFreeParserCtxt(pctxt);
234 if (rng != NULL) {
235 fprintf(stderr, "Failed to detect incorect RNG line %ld\n",
236 xmlGetLineNo(test));
237 ret = 1;
238 goto done;
239 }
240
241done:
242 if (buf != NULL)
243 xmlBufferFree(buf);
244 if (rng != NULL)
245 xmlRelaxNGFree(rng);
246 xmlResetLastError();
247 if ((memt != xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
248 fprintf(stderr, "Validation of tests starting line %ld leaked %d\n",
249 xmlGetLineNo(cur), xmlMemUsed() - memt);
250 nb_leaks++;
251 }
252 return(ret);
253}
254
255static int
256xsdTestCase(int verbose, xmlNodePtr tst) {
257 xmlNodePtr test, tmp, cur;
258 xmlBufferPtr buf;
259 xmlDocPtr doc = NULL;
260 xmlRelaxNGParserCtxtPtr pctxt;
261 xmlRelaxNGValidCtxtPtr ctxt;
262 xmlRelaxNGPtr rng = NULL;
263 int ret = 0, mem, memt;
264
265 cur = getNext(tst, "./correct[1]");
266 if (cur == NULL) {
267 return(xsdIncorectTestCase(verbose, tst));
268 }
269
270 test = getNext(cur, "./*");
271 if (test == NULL) {
272 fprintf(stderr, "Failed to find test in correct line %ld\n",
273 xmlGetLineNo(cur));
274 return(1);
275 }
276
277 memt = xmlMemUsed();
278 extraMemoryFromResolver = 0;
279 /*
280 * dump the schemas to a buffer, then reparse it and compile the schemas
281 */
282 buf = xmlBufferCreate();
283 if (buf == NULL) {
284 fprintf(stderr, "out of memory !\n");
285 fatalError();
286 }
287 xmlNodeDump(buf, test->doc, test, 0, 0);
288 pctxt = xmlRelaxNGNewMemParserCtxt((const char *)buf->content, buf->use);
289 xmlRelaxNGSetParserErrors(pctxt,
290 (xmlRelaxNGValidityErrorFunc) testErrorHandler,
291 (xmlRelaxNGValidityWarningFunc) testErrorHandler,
292 pctxt);
293 rng = xmlRelaxNGParse(pctxt);
294 xmlRelaxNGFreeParserCtxt(pctxt);
295 if (extraMemoryFromResolver)
296 memt = 0;
297
298 if (rng == NULL) {
299 fprintf(stderr, "Failed to parse RNGtest line %ld\n",
300 xmlGetLineNo(test));
301 nb_errors++;
302 ret = 1;
303 goto done;
304 }
305 /*
306 * now scan all the siblings of correct to process the <valid> tests
307 */
308 tmp = getNext(cur, "following-sibling::valid[1]");
309 while (tmp != NULL) {
310 test = getNext(tmp, "./*");
311 if (test == NULL) {
312 fprintf(stderr, "Failed to find test in <valid> line %ld\n",
313 xmlGetLineNo(tmp));
314
315 } else {
316 xmlBufferEmpty(buf);
317 xmlNodeDump(buf, test->doc, test, 0, 0);
318
319 /*
320 * We are ready to run the test
321 */
322 mem = xmlMemUsed();
323 extraMemoryFromResolver = 0;
324 doc = xmlReadMemory((const char *)buf->content, buf->use,
325 "test", NULL, 0);
326 if (doc == NULL) {
327 fprintf(stderr,
328 "Failed to parse valid instance line %ld\n",
329 xmlGetLineNo(tmp));
330 nb_errors++;
331 } else {
332 nb_tests++;
333 ctxt = xmlRelaxNGNewValidCtxt(rng);
334 xmlRelaxNGSetValidErrors(ctxt,
335 (xmlRelaxNGValidityErrorFunc) testErrorHandler,
336 (xmlRelaxNGValidityWarningFunc) testErrorHandler,
337 ctxt);
338 ret = xmlRelaxNGValidateDoc(ctxt, doc);
339 xmlRelaxNGFreeValidCtxt(ctxt);
340 if (ret > 0) {
341 fprintf(stderr,
342 "Failed to validate valid instance line %ld\n",
343 xmlGetLineNo(tmp));
344 nb_errors++;
345 } else if (ret < 0) {
346 fprintf(stderr,
347 "Internal error validating instance line %ld\n",
348 xmlGetLineNo(tmp));
349 nb_errors++;
350 }
351 xmlFreeDoc(doc);
352 }
353 xmlResetLastError();
354 if ((mem != xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
355 fprintf(stderr, "Validation of instance line %ld leaked %d\n",
356 xmlGetLineNo(tmp), xmlMemUsed() - mem);
357 xmlMemoryDump();
358 nb_leaks++;
359 }
360 }
361 tmp = getNext(tmp, "following-sibling::valid[1]");
362 }
363 /*
364 * now scan all the siblings of correct to process the <invalid> tests
365 */
366 tmp = getNext(cur, "following-sibling::invalid[1]");
367 while (tmp != NULL) {
368 test = getNext(tmp, "./*");
369 if (test == NULL) {
370 fprintf(stderr, "Failed to find test in <invalid> line %ld\n",
371 xmlGetLineNo(tmp));
372
373 } else {
374 xmlBufferEmpty(buf);
375 xmlNodeDump(buf, test->doc, test, 0, 0);
376
377 /*
378 * We are ready to run the test
379 */
380 mem = xmlMemUsed();
381 extraMemoryFromResolver = 0;
382 doc = xmlReadMemory((const char *)buf->content, buf->use,
383 "test", NULL, 0);
384 if (doc == NULL) {
385 fprintf(stderr,
386 "Failed to parse valid instance line %ld\n",
387 xmlGetLineNo(tmp));
388 nb_errors++;
389 } else {
390 nb_tests++;
391 ctxt = xmlRelaxNGNewValidCtxt(rng);
392 xmlRelaxNGSetValidErrors(ctxt,
393 (xmlRelaxNGValidityErrorFunc) testErrorHandler,
394 (xmlRelaxNGValidityWarningFunc) testErrorHandler,
395 ctxt);
396 ret = xmlRelaxNGValidateDoc(ctxt, doc);
397 xmlRelaxNGFreeValidCtxt(ctxt);
398 if (ret == 0) {
399 fprintf(stderr,
400 "Failed to detect invalid instance line %ld\n",
401 xmlGetLineNo(tmp));
402 nb_errors++;
403 } else if (ret < 0) {
404 fprintf(stderr,
405 "Internal error validating instance line %ld\n",
406 xmlGetLineNo(tmp));
407 nb_errors++;
408 }
409 xmlFreeDoc(doc);
410 }
411 xmlResetLastError();
412 if ((mem != xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
413 fprintf(stderr, "Validation of instance line %ld leaked %d\n",
414 xmlGetLineNo(tmp), xmlMemUsed() - mem);
415 xmlMemoryDump();
416 nb_leaks++;
417 }
418 }
419 tmp = getNext(tmp, "following-sibling::invalid[1]");
420 }
421
422done:
423 if (buf != NULL)
424 xmlBufferFree(buf);
425 if (rng != NULL)
426 xmlRelaxNGFree(rng);
427 xmlResetLastError();
428 if ((memt != xmlMemUsed()) && (memt != 0)) {
429 fprintf(stderr, "Validation of tests starting line %ld leaked %d\n",
430 xmlGetLineNo(cur), xmlMemUsed() - memt);
431 nb_leaks++;
432 }
433 return(ret);
434}
435
436static int
437xsdTestSuite(int verbose, xmlNodePtr cur) {
438 if (verbose) {
439 xmlChar *doc = getString(cur, "string(documentation)");
440
441 if (doc != NULL) {
442 printf("Suite %s\n", doc);
443 xmlFree(doc);
444 }
445 }
446 cur = getNext(cur, "./testCase[1]");
447 while (cur != NULL) {
448 xsdTestCase(verbose, cur);
449 cur = getNext(cur, "following-sibling::testCase[1]");
450 }
451
452 return(0);
453}
454
455static int
456xsdTest(int verbose) {
457 xmlDocPtr doc;
458 xmlNodePtr cur;
459 const char *filename = "test/xsdtest/xsdtestsuite.xml";
460 int ret = 0;
461
462 doc = xmlReadFile(filename, NULL, XML_PARSE_NOENT);
463 if (doc == NULL) {
464 fprintf(stderr, "Failed to parse %s\n", filename);
465 return(-1);
466 }
467 printf("## XML Schemas datatypes test suite from James Clark\n");
468
469 cur = xmlDocGetRootElement(doc);
470 if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
471 fprintf(stderr, "Unexpected format %s\n", filename);
472 ret = -1;
473 goto done;
474 }
475
476 cur = getNext(cur, "./testSuite[1]");
477 if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
478 fprintf(stderr, "Unexpected format %s\n", filename);
479 ret = -1;
480 goto done;
481 }
482 while (cur != NULL) {
483 xsdTestSuite(verbose, cur);
484 cur = getNext(cur, "following-sibling::testSuite[1]");
485 }
486
487done:
488 if (doc != NULL)
489 xmlFreeDoc(doc);
490 return(ret);
491}
492
493static int
494rngTestSuite(int verbose, xmlNodePtr cur) {
495 if (verbose) {
496 xmlChar *doc = getString(cur, "string(documentation)");
497
498 if (doc != NULL) {
499 printf("Suite %s\n", doc);
500 xmlFree(doc);
501 } else {
502 doc = getString(cur, "string(section)");
503 if (doc != NULL) {
504 printf("Section %s\n", doc);
505 xmlFree(doc);
506 }
507 }
508 }
509 cur = getNext(cur, "./testSuite[1]");
510 while (cur != NULL) {
511 xsdTestSuite(verbose, cur);
512 cur = getNext(cur, "following-sibling::testSuite[1]");
513 }
514
515 return(0);
516}
517
518static int
519rngTest1(int verbose) {
520 xmlDocPtr doc;
521 xmlNodePtr cur;
522 const char *filename = "test/relaxng/OASIS/spectest.xml";
523 int ret = 0;
524
525 doc = xmlReadFile(filename, NULL, XML_PARSE_NOENT);
526 if (doc == NULL) {
527 fprintf(stderr, "Failed to parse %s\n", filename);
528 return(-1);
529 }
530 printf("## Relax NG test suite 1 from James Clark\n");
531
532 cur = xmlDocGetRootElement(doc);
533 if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
534 fprintf(stderr, "Unexpected format %s\n", filename);
535 ret = -1;
536 goto done;
537 }
538
539 cur = getNext(cur, "./testSuite[1]");
540 if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
541 fprintf(stderr, "Unexpected format %s\n", filename);
542 ret = -1;
543 goto done;
544 }
545 while (cur != NULL) {
546 rngTestSuite(verbose, cur);
547 cur = getNext(cur, "following-sibling::testSuite[1]");
548 }
549
550done:
551 if (doc != NULL)
552 xmlFreeDoc(doc);
553 return(ret);
554}
555
556/************************************************************************
557 * *
558 * Libxml2 specific routines *
559 * *
560 ************************************************************************/
561
562int
563main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
564 int res, ret = 0;
565 int verbose = 0;
566 int old_errors, old_tests, old_leaks;
567
568 initializeLibxml2();
569
570 if ((argc >= 2) && (!strcmp(argv[1], "-v")))
571 verbose = 1;
572
573
574 res = xsdTest(verbose);
575 if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
576 printf("Ran %d tests, no errors\n", nb_tests - old_tests);
577 else
578 printf("Ran %d tests, %d errors, %d leaks\n",
579 nb_tests - old_tests,
580 nb_errors - old_errors,
581 nb_leaks - old_leaks);
582 old_errors = nb_errors;
583 old_tests = nb_tests;
584 old_leaks = nb_leaks;
585 res = rngTest1(verbose);
586 if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
587 printf("Ran %d tests, no errors\n", nb_tests - old_tests);
588 else
589 printf("Ran %d tests, %d errors, %d leaks\n",
590 nb_tests - old_tests,
591 nb_errors - old_errors,
592 nb_leaks - old_leaks);
593 old_errors = nb_errors;
594 old_tests = nb_tests;
595 old_leaks = nb_leaks;
596
597 if ((nb_errors == 0) && (nb_leaks == 0)) {
598 ret = 0;
599 printf("Total %d tests, no errors\n",
600 nb_tests);
601 } else {
602 ret = 1;
603 printf("Total %d tests, %d errors, %d leaks\n",
604 nb_tests, nb_errors, nb_leaks);
605 }
606 xmlCleanupParser();
607 xmlMemoryDump();
608
609 return(ret);
610}