runsuite.c 31.6 KB
Newer Older
1
/*
2
 * runsuite.c: C program to run libxml2 againts published testsuites 
3 4 5 6 7 8
 *
 * See Copyright for the status of this software.
 *
 * daniel@veillard.com
 */

9 10 11 12 13 14
#ifdef HAVE_CONFIG_H
#include "libxml.h"
#else
#include <stdio.h>
#endif

15
#if !defined(_WIN32) || defined(__CYGWIN__)
16
#include <unistd.h>
17
#endif
18 19 20 21 22 23
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <libxml/parser.h>
24
#include <libxml/parserInternals.h>
25 26
#include <libxml/tree.h>
#include <libxml/uri.h>
27
#if defined(LIBXML_SCHEMAS_ENABLED) && defined(LIBXML_XPATH_ENABLED)
28 29 30 31 32 33 34 35 36
#include <libxml/xmlreader.h>

#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

#include <libxml/relaxng.h>
#include <libxml/xmlschemas.h>
#include <libxml/xmlschemastypes.h>

37
#define LOGFILE "runsuite.log"
38
static FILE *logfile = NULL;
39 40 41 42 43 44 45 46 47 48
static int verbose = 0;



#if defined(_WIN32) && !defined(__CYGWIN__)

#define vsnprintf _vsnprintf

#define snprintf _snprintf

49
#endif
50

51 52 53 54 55 56 57 58 59 60 61 62
/************************************************************************
 *									*
 *		File name and path utilities				*
 *									*
 ************************************************************************/

static int checkTestFile(const char *filename) {
    struct stat buf;

    if (stat(filename, &buf) == -1)
        return(0);

63 64 65 66
#if defined(_WIN32) && !defined(__CYGWIN__)
    if (!(buf.st_mode & _S_IFREG))
        return(0);
#else
67 68
    if (!S_ISREG(buf.st_mode))
        return(0);
69
#endif
70 71 72

    return(1);
}
73

74 75 76 77 78 79 80 81 82
static xmlChar *composeDir(const xmlChar *dir, const xmlChar *path) {
    char buf[500];

    if (dir == NULL) return(xmlStrdup(path));
    if (path == NULL) return(NULL);

    snprintf(buf, 500, "%s/%s", (const char *) dir, (const char *) path);
    return(xmlStrdup((const xmlChar *) buf));
}
83 84 85 86 87 88 89 90 91

/************************************************************************
 *									*
 *		Libxml2 specific routines				*
 *									*
 ************************************************************************/

static int nb_tests = 0;
static int nb_errors = 0;
92 93
static int nb_internals = 0;
static int nb_schematas = 0;
94
static int nb_unimplemented = 0;
95 96 97 98 99 100 101 102 103
static int nb_leaks = 0;
static int extraMemoryFromResolver = 0;

static int
fatalError(void) {
    fprintf(stderr, "Exitting tests on fatal error\n");
    exit(1);
}

104 105 106 107
/*
 * that's needed to implement <resource>
 */
#define MAX_ENTITIES 20
108 109 110
static char *testEntitiesName[MAX_ENTITIES];
static char *testEntitiesValue[MAX_ENTITIES];
static int nb_entities = 0;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static void resetEntities(void) {
    int i;

    for (i = 0;i < nb_entities;i++) {
        if (testEntitiesName[i] != NULL)
	    xmlFree(testEntitiesName[i]);
        if (testEntitiesValue[i] != NULL)
	    xmlFree(testEntitiesValue[i]);
    }
    nb_entities = 0;
}
static int addEntity(char *name, char *content) {
    if (nb_entities >= MAX_ENTITIES) {
	fprintf(stderr, "Too many entities defined\n");
	return(-1);
    }
    testEntitiesName[nb_entities] = name;
    testEntitiesValue[nb_entities] = content;
    nb_entities++;
    return(0);
}

133 134 135 136 137 138 139 140 141
/*
 * We need to trap calls to the resolver to not account memory for the catalog
 * which is shared to the current running test. We also don't want to have
 * network downloads modifying tests.
 */
static xmlParserInputPtr 
testExternalEntityLoader(const char *URL, const char *ID,
			 xmlParserCtxtPtr ctxt) {
    xmlParserInputPtr ret;
142 143 144 145 146 147 148 149 150 151 152 153 154
    int i;

    for (i = 0;i < nb_entities;i++) {
        if (!strcmp(testEntitiesName[i], URL)) {
	    ret = xmlNewStringInputStream(ctxt,
	                (const xmlChar *) testEntitiesValue[i]);
	    if (ret != NULL) {
	        ret->filename = (const char *)
		                xmlStrdup((xmlChar *)testEntitiesName[i]);
	    }
	    return(ret);
	}
    }
155 156 157 158 159 160 161
    if (checkTestFile(URL)) {
	ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
    } else {
	int memused = xmlMemUsed();
	ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
	extraMemoryFromResolver += xmlMemUsed() - memused;
    }
162
#if 0
163 164 165
    if (ret == NULL) {
        fprintf(stderr, "Failed to find resource %s\n", URL);
    }
166
#endif
167 168 169 170 171 172 173 174 175 176 177
      
    return(ret);
}

/*
 * Trapping the error messages at the generic level to grab the equivalent of
 * stderr messages on CLI tools.
 */
static char testErrors[32769];
static int testErrorsSize = 0;

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
static void test_log(const char *msg, ...) {
    va_list args;
    if (logfile != NULL) {
        fprintf(logfile, "\n------------\n");
	va_start(args, msg);
	vfprintf(logfile, msg, args);
	va_end(args);
	fprintf(logfile, "%s", testErrors);
	testErrorsSize = 0; testErrors[0] = 0;
    }
    if (verbose) {
	va_start(args, msg);
	vfprintf(stderr, msg, args);
	va_end(args);
    }
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
static void
testErrorHandler(void *ctx  ATTRIBUTE_UNUSED, const char *msg, ...) {
    va_list args;
    int res;

    if (testErrorsSize >= 32768)
        return;
    va_start(args, msg);
    res = vsnprintf(&testErrors[testErrorsSize],
                    32768 - testErrorsSize,
		    msg, args);
    va_end(args);
    if (testErrorsSize + res >= 32768) {
        /* buffer is full */
	testErrorsSize = 32768;
	testErrors[testErrorsSize] = 0;
    } else {
        testErrorsSize += res;
    }
    testErrors[testErrorsSize] = 0;
}
216

217
static xmlXPathContextPtr ctxtXPath;
218

219 220 221 222 223 224 225 226
static void
initializeLibxml2(void) {
    xmlGetWarningsDefaultValue = 0;
    xmlPedanticParserDefault(0);

    xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
    xmlInitParser();
    xmlSetExternalEntityLoader(testExternalEntityLoader);
227
    ctxtXPath = xmlXPathNewContext(NULL);
228 229 230 231
    /*
    * Deactivate the cache if created; otherwise we have to create/free it
    * for every test, since it will confuse the memory leak detection.
    * Note that normally this need not be done, since the cache is not
232
    * created until set explicitely with xmlXPathContextSetCache();
233 234 235
    * but for test purposes it is sometimes usefull to activate the
    * cache by default for the whole library.
    */
236 237
    if (ctxtXPath->cache != NULL)
	xmlXPathContextSetCache(ctxtXPath, 0, -1, 0);
238 239 240 241 242
    /* used as default nanemspace in xstc tests */
    xmlXPathRegisterNs(ctxtXPath, BAD_CAST "ts", BAD_CAST "TestSuite");
    xmlXPathRegisterNs(ctxtXPath, BAD_CAST "xlink",
                       BAD_CAST "http://www.w3.org/1999/xlink");
    xmlSetGenericErrorFunc(NULL, testErrorHandler);
243 244 245 246 247 248 249 250 251 252 253 254 255 256
#ifdef LIBXML_SCHEMAS_ENABLED
    xmlSchemaInitTypes();
    xmlRelaxNGInitTypes();
#endif
}

static xmlNodePtr
getNext(xmlNodePtr cur, const char *xpath) {
    xmlNodePtr ret = NULL;
    xmlXPathObjectPtr res;
    xmlXPathCompExprPtr comp;

    if ((cur == NULL)  || (cur->doc == NULL) || (xpath == NULL))
        return(NULL);
257 258
    ctxtXPath->doc = cur->doc;
    ctxtXPath->node = cur;
259 260 261 262 263
    comp = xmlXPathCompile(BAD_CAST xpath);
    if (comp == NULL) {
        fprintf(stderr, "Failed to compile %s\n", xpath);
	return(NULL);
    }
264
    res = xmlXPathCompiledEval(comp, ctxtXPath);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    xmlXPathFreeCompExpr(comp);
    if (res == NULL)
        return(NULL);
    if ((res->type == XPATH_NODESET) &&
        (res->nodesetval != NULL) &&
	(res->nodesetval->nodeNr > 0) &&
	(res->nodesetval->nodeTab != NULL))
	ret = res->nodesetval->nodeTab[0];
    xmlXPathFreeObject(res);
    return(ret);
}

static xmlChar *
getString(xmlNodePtr cur, const char *xpath) {
    xmlChar *ret = NULL;
    xmlXPathObjectPtr res;
    xmlXPathCompExprPtr comp;

    if ((cur == NULL)  || (cur->doc == NULL) || (xpath == NULL))
        return(NULL);
285 286
    ctxtXPath->doc = cur->doc;
    ctxtXPath->node = cur;
287 288 289 290 291
    comp = xmlXPathCompile(BAD_CAST xpath);
    if (comp == NULL) {
        fprintf(stderr, "Failed to compile %s\n", xpath);
	return(NULL);
    }
292
    res = xmlXPathCompiledEval(comp, ctxtXPath);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    xmlXPathFreeCompExpr(comp);
    if (res == NULL)
        return(NULL);
    if (res->type == XPATH_STRING) {
        ret = res->stringval;
	res->stringval = NULL;
    }
    xmlXPathFreeObject(res);
    return(ret);
}

/************************************************************************
 *									*
 *		Test test/xsdtest/xsdtestsuite.xml			*
 *									*
 ************************************************************************/

static int 
311
xsdIncorectTestCase(xmlNodePtr cur) {
312 313 314 315 316 317 318 319 320 321 322 323 324
    xmlNodePtr test;
    xmlBufferPtr buf;
    xmlRelaxNGParserCtxtPtr pctxt;
    xmlRelaxNGPtr rng = NULL;
    int ret = 0, memt;

    cur = getNext(cur, "./incorrect[1]");
    if (cur == NULL) {
        return(0);
    }
    
    test = getNext(cur, "./*");
    if (test == NULL) {
325
        test_log("Failed to find test in correct line %ld\n",
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	        xmlGetLineNo(cur));
        return(1);
    }

    memt = xmlMemUsed();
    extraMemoryFromResolver = 0;
    /*
     * dump the schemas to a buffer, then reparse it and compile the schemas
     */
    buf = xmlBufferCreate();
    if (buf == NULL) {
        fprintf(stderr, "out of memory !\n");
	fatalError();
    }
    xmlNodeDump(buf, test->doc, test, 0, 0);
    pctxt = xmlRelaxNGNewMemParserCtxt((const char *)buf->content, buf->use);
    xmlRelaxNGSetParserErrors(pctxt,
         (xmlRelaxNGValidityErrorFunc) testErrorHandler,
         (xmlRelaxNGValidityWarningFunc) testErrorHandler,
	 pctxt);
    rng = xmlRelaxNGParse(pctxt);
    xmlRelaxNGFreeParserCtxt(pctxt);
    if (rng != NULL) {
349
	test_log("Failed to detect incorect RNG line %ld\n",
350
		    xmlGetLineNo(test));
351 352 353 354 355 356 357 358 359 360
        ret = 1;
	goto done;
    }

done:
    if (buf != NULL)
	xmlBufferFree(buf);
    if (rng != NULL)
        xmlRelaxNGFree(rng);
    xmlResetLastError();
361
    if ((memt < xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
362
	test_log("Validation of tests starting line %ld leaked %d\n",
363 364 365 366 367 368
		xmlGetLineNo(cur), xmlMemUsed() - memt);
	nb_leaks++;
    }
    return(ret);
}

369 370 371 372 373
static void
installResources(xmlNodePtr tst, const xmlChar *base) {
    xmlNodePtr test;
    xmlBufferPtr buf;
    xmlChar *name, *content, *res;
374

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    buf = xmlBufferCreate();
    if (buf == NULL) {
        fprintf(stderr, "out of memory !\n");
	fatalError();
    }
    xmlNodeDump(buf, tst->doc, tst, 0, 0);

    while (tst != NULL) {
	test = getNext(tst, "./*");
	if (test != NULL) {
	    xmlBufferEmpty(buf);
	    xmlNodeDump(buf, test->doc, test, 0, 0);
	    name = getString(tst, "string(@name)");
	    content = xmlStrdup(buf->content);
	    if ((name != NULL) && (content != NULL)) {
	        res = composeDir(base, name);
		xmlFree(name);
	        addEntity((char *) res, (char *) content);
	    } else {
	        if (name != NULL) xmlFree(name);
	        if (content != NULL) xmlFree(content);
	    }
	}
	tst = getNext(tst, "following-sibling::resource[1]");
    }
    if (buf != NULL)
	xmlBufferFree(buf);
}

static void
installDirs(xmlNodePtr tst, const xmlChar *base) {
    xmlNodePtr test;
    xmlChar *name, *res;

    name = getString(tst, "string(@name)");
    if (name == NULL)
        return;
    res = composeDir(base, name);
    xmlFree(name);
    if (res == NULL) {
	return;
    }
    /* Now process resources and subdir recursively */
    test = getNext(tst, "./resource[1]");
    if (test != NULL) {
        installResources(test, res);
    }
    test = getNext(tst, "./dir[1]");
    while (test != NULL) {
        installDirs(test, res);
	test = getNext(test, "following-sibling::dir[1]");
    }
427
    xmlFree(res);
428 429
}

430
static int 
431
xsdTestCase(xmlNodePtr tst) {
432 433 434 435 436 437 438
    xmlNodePtr test, tmp, cur;
    xmlBufferPtr buf;
    xmlDocPtr doc = NULL;
    xmlRelaxNGParserCtxtPtr pctxt;
    xmlRelaxNGValidCtxtPtr ctxt;
    xmlRelaxNGPtr rng = NULL;
    int ret = 0, mem, memt;
439 440 441
    xmlChar *dtd;

    resetEntities();
442
    testErrorsSize = 0; testErrors[0] = 0;
443 444 445 446 447 448 449 450 451

    tmp = getNext(tst, "./dir[1]");
    if (tmp != NULL) {
        installDirs(tmp, NULL);
    }
    tmp = getNext(tst, "./resource[1]");
    if (tmp != NULL) {
        installResources(tmp, NULL);
    }
452 453 454

    cur = getNext(tst, "./correct[1]");
    if (cur == NULL) {
455
        return(xsdIncorectTestCase(tst));
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    }
    
    test = getNext(cur, "./*");
    if (test == NULL) {
        fprintf(stderr, "Failed to find test in correct line %ld\n",
	        xmlGetLineNo(cur));
        return(1);
    }

    memt = xmlMemUsed();
    extraMemoryFromResolver = 0;
    /*
     * dump the schemas to a buffer, then reparse it and compile the schemas
     */
    buf = xmlBufferCreate();
    if (buf == NULL) {
        fprintf(stderr, "out of memory !\n");
	fatalError();
    }
    xmlNodeDump(buf, test->doc, test, 0, 0);
    pctxt = xmlRelaxNGNewMemParserCtxt((const char *)buf->content, buf->use);
    xmlRelaxNGSetParserErrors(pctxt,
         (xmlRelaxNGValidityErrorFunc) testErrorHandler,
         (xmlRelaxNGValidityWarningFunc) testErrorHandler,
	 pctxt);
    rng = xmlRelaxNGParse(pctxt);
    xmlRelaxNGFreeParserCtxt(pctxt);
    if (extraMemoryFromResolver)
        memt = 0;

    if (rng == NULL) {
487
        test_log("Failed to parse RNGtest line %ld\n",
488 489 490 491 492 493 494 495 496 497
	        xmlGetLineNo(test));
	nb_errors++;
        ret = 1;
	goto done;
    }
    /*
     * now scan all the siblings of correct to process the <valid> tests
     */
    tmp = getNext(cur, "following-sibling::valid[1]");
    while (tmp != NULL) {
498
	dtd = xmlGetProp(tmp, BAD_CAST "dtd");
499 500 501 502 503 504 505
	test = getNext(tmp, "./*");
	if (test == NULL) {
	    fprintf(stderr, "Failed to find test in <valid> line %ld\n",
		    xmlGetLineNo(tmp));
	    
	} else {
	    xmlBufferEmpty(buf);
506 507
	    if (dtd != NULL)
		xmlBufferAdd(buf, dtd, -1);
508 509 510 511 512 513 514 515 516 517
	    xmlNodeDump(buf, test->doc, test, 0, 0);

	    /*
	     * We are ready to run the test
	     */
	    mem = xmlMemUsed();
	    extraMemoryFromResolver = 0;
            doc = xmlReadMemory((const char *)buf->content, buf->use,
	                        "test", NULL, 0);
	    if (doc == NULL) {
518
		test_log("Failed to parse valid instance line %ld\n",
519 520 521 522 523 524 525 526 527 528 529 530
			xmlGetLineNo(tmp));
		nb_errors++;
	    } else {
		nb_tests++;
	        ctxt = xmlRelaxNGNewValidCtxt(rng);
		xmlRelaxNGSetValidErrors(ctxt,
		     (xmlRelaxNGValidityErrorFunc) testErrorHandler,
		     (xmlRelaxNGValidityWarningFunc) testErrorHandler,
		     ctxt);
		ret = xmlRelaxNGValidateDoc(ctxt, doc);
		xmlRelaxNGFreeValidCtxt(ctxt);
		if (ret > 0) {
531
		    test_log("Failed to validate valid instance line %ld\n",
532
				xmlGetLineNo(tmp));
533 534
		    nb_errors++;
		} else if (ret < 0) {
535
		    test_log("Internal error validating instance line %ld\n",
536 537 538 539 540 541 542
			    xmlGetLineNo(tmp));
		    nb_errors++;
		}
		xmlFreeDoc(doc);
	    }
	    xmlResetLastError();
	    if ((mem != xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
543
	        test_log("Validation of instance line %ld leaked %d\n",
544 545 546 547 548
		        xmlGetLineNo(tmp), xmlMemUsed() - mem);
		xmlMemoryDump();
	        nb_leaks++;
	    }
	}
549 550
	if (dtd != NULL)
	    xmlFree(dtd);
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
	tmp = getNext(tmp, "following-sibling::valid[1]");
    }
    /*
     * now scan all the siblings of correct to process the <invalid> tests
     */
    tmp = getNext(cur, "following-sibling::invalid[1]");
    while (tmp != NULL) {
	test = getNext(tmp, "./*");
	if (test == NULL) {
	    fprintf(stderr, "Failed to find test in <invalid> line %ld\n",
		    xmlGetLineNo(tmp));
	    
	} else {
	    xmlBufferEmpty(buf);
	    xmlNodeDump(buf, test->doc, test, 0, 0);

	    /*
	     * We are ready to run the test
	     */
	    mem = xmlMemUsed();
	    extraMemoryFromResolver = 0;
            doc = xmlReadMemory((const char *)buf->content, buf->use,
	                        "test", NULL, 0);
	    if (doc == NULL) {
575
		test_log("Failed to parse valid instance line %ld\n",
576 577 578 579 580 581 582 583 584 585 586 587
			xmlGetLineNo(tmp));
		nb_errors++;
	    } else {
		nb_tests++;
	        ctxt = xmlRelaxNGNewValidCtxt(rng);
		xmlRelaxNGSetValidErrors(ctxt,
		     (xmlRelaxNGValidityErrorFunc) testErrorHandler,
		     (xmlRelaxNGValidityWarningFunc) testErrorHandler,
		     ctxt);
		ret = xmlRelaxNGValidateDoc(ctxt, doc);
		xmlRelaxNGFreeValidCtxt(ctxt);
		if (ret == 0) {
588
		    test_log("Failed to detect invalid instance line %ld\n",
589
				xmlGetLineNo(tmp));
590 591
		    nb_errors++;
		} else if (ret < 0) {
592
		    test_log("Internal error validating instance line %ld\n",
593 594 595 596 597 598 599
			    xmlGetLineNo(tmp));
		    nb_errors++;
		}
		xmlFreeDoc(doc);
	    }
	    xmlResetLastError();
	    if ((mem != xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
600
	        test_log("Validation of instance line %ld leaked %d\n",
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
		        xmlGetLineNo(tmp), xmlMemUsed() - mem);
		xmlMemoryDump();
	        nb_leaks++;
	    }
	}
	tmp = getNext(tmp, "following-sibling::invalid[1]");
    }

done:
    if (buf != NULL)
	xmlBufferFree(buf);
    if (rng != NULL)
        xmlRelaxNGFree(rng);
    xmlResetLastError();
    if ((memt != xmlMemUsed()) && (memt != 0)) {
616
	test_log("Validation of tests starting line %ld leaked %d\n",
617 618 619 620 621 622 623
		xmlGetLineNo(cur), xmlMemUsed() - memt);
	nb_leaks++;
    }
    return(ret);
}

static int 
624
xsdTestSuite(xmlNodePtr cur) {
625 626 627 628 629 630 631 632 633 634
    if (verbose) {
	xmlChar *doc = getString(cur, "string(documentation)");

	if (doc != NULL) {
	    printf("Suite %s\n", doc);
	    xmlFree(doc);
	}
    }
    cur = getNext(cur, "./testCase[1]");
    while (cur != NULL) {
635
        xsdTestCase(cur);
636 637 638 639 640 641 642
	cur = getNext(cur, "following-sibling::testCase[1]");
    }
        
    return(0);
}

static int 
643
xsdTest(void) {
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    xmlDocPtr doc;
    xmlNodePtr cur;
    const char *filename = "test/xsdtest/xsdtestsuite.xml";
    int ret = 0;

    doc = xmlReadFile(filename, NULL, XML_PARSE_NOENT);
    if (doc == NULL) {
        fprintf(stderr, "Failed to parse %s\n", filename);
	return(-1);
    }
    printf("## XML Schemas datatypes test suite from James Clark\n");

    cur = xmlDocGetRootElement(doc);
    if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
        fprintf(stderr, "Unexpected format %s\n", filename);
	ret = -1;
	goto done;
    }

    cur = getNext(cur, "./testSuite[1]");
    if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
        fprintf(stderr, "Unexpected format %s\n", filename);
	ret = -1;
	goto done;
    }
    while (cur != NULL) {
670
        xsdTestSuite(cur);
671 672 673 674 675 676 677 678 679 680
	cur = getNext(cur, "following-sibling::testSuite[1]");
    }

done:
    if (doc != NULL)
	xmlFreeDoc(doc);
    return(ret);
}

static int 
681
rngTestSuite(xmlNodePtr cur) {
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
    if (verbose) {
	xmlChar *doc = getString(cur, "string(documentation)");

	if (doc != NULL) {
	    printf("Suite %s\n", doc);
	    xmlFree(doc);
	} else {
	    doc = getString(cur, "string(section)");
	    if (doc != NULL) {
		printf("Section %s\n", doc);
		xmlFree(doc);
	    }
	}
    }
    cur = getNext(cur, "./testSuite[1]");
    while (cur != NULL) {
698
        xsdTestSuite(cur);
699 700 701 702 703 704 705
	cur = getNext(cur, "following-sibling::testSuite[1]");
    }
        
    return(0);
}

static int 
706
rngTest1(void) {
707 708 709 710 711 712 713 714 715 716
    xmlDocPtr doc;
    xmlNodePtr cur;
    const char *filename = "test/relaxng/OASIS/spectest.xml";
    int ret = 0;

    doc = xmlReadFile(filename, NULL, XML_PARSE_NOENT);
    if (doc == NULL) {
        fprintf(stderr, "Failed to parse %s\n", filename);
	return(-1);
    }
717
    printf("## Relax NG test suite from James Clark\n");
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

    cur = xmlDocGetRootElement(doc);
    if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
        fprintf(stderr, "Unexpected format %s\n", filename);
	ret = -1;
	goto done;
    }

    cur = getNext(cur, "./testSuite[1]");
    if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
        fprintf(stderr, "Unexpected format %s\n", filename);
	ret = -1;
	goto done;
    }
    while (cur != NULL) {
733
        rngTestSuite(cur);
734 735 736 737 738 739 740 741 742
	cur = getNext(cur, "following-sibling::testSuite[1]");
    }

done:
    if (doc != NULL)
	xmlFreeDoc(doc);
    return(ret);
}

743
static int 
744
rngTest2(void) {
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
    xmlDocPtr doc;
    xmlNodePtr cur;
    const char *filename = "test/relaxng/testsuite.xml";
    int ret = 0;

    doc = xmlReadFile(filename, NULL, XML_PARSE_NOENT);
    if (doc == NULL) {
        fprintf(stderr, "Failed to parse %s\n", filename);
	return(-1);
    }
    printf("## Relax NG test suite for libxml2\n");

    cur = xmlDocGetRootElement(doc);
    if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
        fprintf(stderr, "Unexpected format %s\n", filename);
	ret = -1;
	goto done;
    }

    cur = getNext(cur, "./testSuite[1]");
    if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
        fprintf(stderr, "Unexpected format %s\n", filename);
	ret = -1;
	goto done;
    }
    while (cur != NULL) {
771
        xsdTestSuite(cur);
772 773 774 775 776 777 778 779 780
	cur = getNext(cur, "following-sibling::testSuite[1]");
    }

done:
    if (doc != NULL)
	xmlFreeDoc(doc);
    return(ret);
}

781 782
/************************************************************************
 *									*
783 784 785 786 787
 *		Schemas test suites from W3C/NIST/MS/Sun		*
 *									*
 ************************************************************************/

static int
788
xstcTestInstance(xmlNodePtr cur, xmlSchemaPtr schemas,
789
                 const xmlChar *spath, const char *base) {
790 791
    xmlChar *href = NULL;
    xmlChar *path = NULL;
792 793 794 795
    xmlChar *validity = NULL;
    xmlSchemaValidCtxtPtr ctxt = NULL;
    xmlDocPtr doc = NULL;
    int ret = 0, mem;
796

797
    xmlResetLastError();
798
    testErrorsSize = 0; testErrors[0] = 0;
799
    mem = xmlMemUsed();
800
    href = getString(cur,
801
                     "string(ts:instanceDocument/@xlink:href)");
802
    if ((href == NULL) || (href[0] == 0)) {
803
	test_log("testGroup line %ld misses href for schemaDocument\n",
804 805 806 807
		    xmlGetLineNo(cur));
	ret = -1;
	goto done;
    }
808
    path = xmlBuildURI(href, BAD_CAST base);
809 810 811 812 813 814 815
    if (path == NULL) {
	fprintf(stderr,
	        "Failed to build path to schemas testGroup line %ld : %s\n",
		xmlGetLineNo(cur), href);
	ret = -1;
	goto done;
    }
816
    if (checkTestFile((const char *) path) <= 0) {
817
	test_log("schemas for testGroup line %ld is missing: %s\n",
818 819 820 821
		xmlGetLineNo(cur), path);
	ret = -1;
	goto done;
    }
822 823 824 825 826 827 828 829
    validity = getString(cur,
                         "string(ts:expected/@validity)");
    if (validity == NULL) {
        fprintf(stderr, "instanceDocument line %ld misses expected validity\n",
	        xmlGetLineNo(cur));
	ret = -1;
	goto done;
    }
830
    nb_tests++;
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
    doc = xmlReadFile((const char *) path, NULL, XML_PARSE_NOENT);
    if (doc == NULL) {
        fprintf(stderr, "instance %s fails to parse\n", path);
	ret = -1;
	nb_errors++;
	goto done;
    }

    ctxt = xmlSchemaNewValidCtxt(schemas);
    xmlSchemaSetValidErrors(ctxt,
         (xmlSchemaValidityErrorFunc) testErrorHandler,
         (xmlSchemaValidityWarningFunc) testErrorHandler,
	 ctxt);
    ret = xmlSchemaValidateDoc(ctxt, doc);

    if (xmlStrEqual(validity, BAD_CAST "valid")) {
847 848 849 850 851 852
	if (ret > 0) {
	    test_log("valid instance %s failed to validate against %s\n",
			path, spath);
	    nb_errors++;
	} else if (ret < 0) {
	    test_log("valid instance %s got internal error validating %s\n",
853
			path, spath);
854
	    nb_internals++;
855 856 857 858
	    nb_errors++;
	}
    } else if (xmlStrEqual(validity, BAD_CAST "invalid")) {
	if (ret == 0) {
859
	    test_log("Failed to detect invalid instance %s against %s\n",
860 861 862 863
			path, spath);
	    nb_errors++;
	}
    } else {
864
        test_log("instanceDocument line %ld has unexpected validity value%s\n",
865 866 867 868
	        xmlGetLineNo(cur), validity);
	ret = -1;
	goto done;
    }
869 870 871 872

done:
    if (href != NULL) xmlFree(href);
    if (path != NULL) xmlFree(path);
873 874 875 876 877
    if (validity != NULL) xmlFree(validity);
    if (ctxt != NULL) xmlSchemaFreeValidCtxt(ctxt);
    if (doc != NULL) xmlFreeDoc(doc);
    xmlResetLastError();
    if (mem != xmlMemUsed()) {
878
	test_log("Validation of tests starting line %ld leaked %d\n",
879 880 881
		xmlGetLineNo(cur), xmlMemUsed() - mem);
	nb_leaks++;
    }
882 883
    return(ret);
}
884

885
static int
886
xstcTestGroup(xmlNodePtr cur, const char *base) {
887 888 889 890 891 892
    xmlChar *href = NULL;
    xmlChar *path = NULL;
    xmlChar *validity = NULL;
    xmlSchemaPtr schemas = NULL;
    xmlSchemaParserCtxtPtr ctxt;
    xmlNodePtr instance;
893
    int ret = 0, mem;
894

895
    xmlResetLastError();
896
    testErrorsSize = 0; testErrors[0] = 0;
897
    mem = xmlMemUsed();
898 899 900
    href = getString(cur,
                     "string(ts:schemaTest/ts:schemaDocument/@xlink:href)");
    if ((href == NULL) || (href[0] == 0)) {
901
        test_log("testGroup line %ld misses href for schemaDocument\n",
902 903 904 905
		    xmlGetLineNo(cur));
	ret = -1;
	goto done;
    }
906
    path = xmlBuildURI(href, BAD_CAST base);
907
    if (path == NULL) {
908
	test_log("Failed to build path to schemas testGroup line %ld : %s\n",
909 910 911 912
		xmlGetLineNo(cur), href);
	ret = -1;
	goto done;
    }
913
    if (checkTestFile((const char *) path) <= 0) {
914
	test_log("schemas for testGroup line %ld is missing: %s\n",
915 916 917 918 919 920 921
		xmlGetLineNo(cur), path);
	ret = -1;
	goto done;
    }
    validity = getString(cur,
                         "string(ts:schemaTest/ts:expected/@validity)");
    if (validity == NULL) {
922
        test_log("testGroup line %ld misses expected validity\n",
923 924 925 926
	        xmlGetLineNo(cur));
	ret = -1;
	goto done;
    }
927
    nb_tests++;
928
    if (xmlStrEqual(validity, BAD_CAST "valid")) {
929
        nb_schematas++;
930
	ctxt = xmlSchemaNewParserCtxt((const char *) path);
931 932 933 934 935 936 937
	xmlSchemaSetParserErrors(ctxt,
	     (xmlSchemaValidityErrorFunc) testErrorHandler,
	     (xmlSchemaValidityWarningFunc) testErrorHandler,
	     ctxt);
	schemas = xmlSchemaParse(ctxt);
	xmlSchemaFreeParserCtxt(ctxt);
	if (schemas == NULL) {
938
	    test_log("valid schemas %s failed to parse\n",
939
			path);
940 941 942 943 944 945 946 947
	    ret = 1;
	    nb_errors++;
	}
	if ((ret == 0) && (strstr(testErrors, "nimplemented") != NULL)) {
	    test_log("valid schemas %s hit an unimplemented block\n",
			path);
	    ret = 1;
	    nb_unimplemented++;
948 949 950 951
	    nb_errors++;
	}
	instance = getNext(cur, "./ts:instanceTest[1]");
	while (instance != NULL) {
952 953 954 955 956 957 958 959 960
	    if (schemas != NULL) {
		xstcTestInstance(instance, schemas, path, base);		
	    } else {
		/*
		* We'll automatically mark the instances as failed
		* if the schema was broken.
		*/
		nb_errors++;
	    }
961
	    instance = getNext(instance,
962
		"following-sibling::ts:instanceTest[1]");
963 964
	}
    } else if (xmlStrEqual(validity, BAD_CAST "invalid")) {
965
        nb_schematas++;
966
	ctxt = xmlSchemaNewParserCtxt((const char *) path);
967 968 969 970 971 972
	xmlSchemaSetParserErrors(ctxt,
	     (xmlSchemaValidityErrorFunc) testErrorHandler,
	     (xmlSchemaValidityWarningFunc) testErrorHandler,
	     ctxt);
	schemas = xmlSchemaParse(ctxt);
	xmlSchemaFreeParserCtxt(ctxt);
973
	if (schemas != NULL) {
974
	    test_log("Failed to detect error in schemas %s\n",
975 976
			path);
	    nb_errors++;
977 978 979 980 981 982 983 984
	    ret = 1;
	}
	if ((ret == 0) && (strstr(testErrors, "nimplemented") != NULL)) {
	    nb_unimplemented++;
	    test_log("invalid schemas %s hit an unimplemented block\n",
			path);
	    ret = 1;
	    nb_errors++;
985 986
	}
    } else {
987
        test_log("testGroup line %ld misses unexpected validity value%s\n",
988 989 990 991 992 993 994 995 996 997
	        xmlGetLineNo(cur), validity);
	ret = -1;
	goto done;
    }

done:
    if (href != NULL) xmlFree(href);
    if (path != NULL) xmlFree(path);
    if (validity != NULL) xmlFree(validity);
    if (schemas != NULL) xmlSchemaFree(schemas);
998 999
    xmlResetLastError();
    if ((mem != xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
1000 1001
	test_log("Processing test line %ld %s leaked %d\n",
		xmlGetLineNo(cur), path, xmlMemUsed() - mem);
1002 1003
	nb_leaks++;
    }
1004 1005 1006 1007
    return(ret);
}

static int
1008
xstcMetadata(const char *metadata, const char *base) {
1009 1010 1011 1012
    xmlDocPtr doc;
    xmlNodePtr cur;
    xmlChar *contributor;
    xmlChar *name;
1013
    int ret = 0;
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044

    doc = xmlReadFile(metadata, NULL, XML_PARSE_NOENT);
    if (doc == NULL) {
        fprintf(stderr, "Failed to parse %s\n", metadata);
	return(-1);
    }

    cur = xmlDocGetRootElement(doc);
    if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSet"))) {
        fprintf(stderr, "Unexpected format %s\n", metadata);
	return(-1);
    }
    contributor = xmlGetProp(cur, BAD_CAST "contributor");
    if (contributor == NULL) {
        contributor = xmlStrdup(BAD_CAST "Unknown");
    }
    name = xmlGetProp(cur, BAD_CAST "name");
    if (name == NULL) {
        name = xmlStrdup(BAD_CAST "Unknown");
    }
    printf("## %s test suite for Schemas version %s\n", contributor, name);
    xmlFree(contributor);
    xmlFree(name);

    cur = getNext(cur, "./ts:testGroup[1]");
    if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testGroup"))) {
        fprintf(stderr, "Unexpected format %s\n", metadata);
	ret = -1;
	goto done;
    }
    while (cur != NULL) {
1045
        xstcTestGroup(cur, base);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	cur = getNext(cur, "following-sibling::ts:testGroup[1]");
    }

done:
    xmlFreeDoc(doc);
    return(ret);
}

/************************************************************************
 *									*
 *		The driver for the tests				*
1057 1058 1059 1060 1061
 *									*
 ************************************************************************/

int
main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
1062
    int ret = 0;
1063 1064
    int old_errors, old_tests, old_leaks;

1065 1066 1067 1068 1069 1070
    logfile = fopen(LOGFILE, "w");
    if (logfile == NULL) {
        fprintf(stderr,
	        "Could not open the log file, running in verbose mode\n");
	verbose = 1;
    }
1071 1072 1073 1074 1075 1076
    initializeLibxml2();

    if ((argc >= 2) && (!strcmp(argv[1], "-v")))
        verbose = 1;


1077 1078 1079
    old_errors = nb_errors;
    old_tests = nb_tests;
    old_leaks = nb_leaks;
1080
    xsdTest();
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
	printf("Ran %d tests, no errors\n", nb_tests - old_tests);
    else
	printf("Ran %d tests, %d errors, %d leaks\n",
	       nb_tests - old_tests,
	       nb_errors - old_errors,
	       nb_leaks - old_leaks);
    old_errors = nb_errors;
    old_tests = nb_tests;
    old_leaks = nb_leaks;
1091
    rngTest1();
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
	printf("Ran %d tests, no errors\n", nb_tests - old_tests);
    else
	printf("Ran %d tests, %d errors, %d leaks\n",
	       nb_tests - old_tests,
	       nb_errors - old_errors,
	       nb_leaks - old_leaks);
    old_errors = nb_errors;
    old_tests = nb_tests;
    old_leaks = nb_leaks;
1102
    rngTest2();
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
    if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
	printf("Ran %d tests, no errors\n", nb_tests - old_tests);
    else
	printf("Ran %d tests, %d errors, %d leaks\n",
	       nb_tests - old_tests,
	       nb_errors - old_errors,
	       nb_leaks - old_leaks);
    old_errors = nb_errors;
    old_tests = nb_tests;
    old_leaks = nb_leaks;
1113 1114
    nb_internals = 0;
    nb_schematas = 0;
1115
    xstcMetadata("xstc/Tests/Metadata/NISTXMLSchemaDatatypes.testSet",
1116 1117
		 "xstc/Tests/Metadata/");
    if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
1118 1119
	printf("Ran %d tests (%d schemata), no errors\n",
	       nb_tests - old_tests, nb_schematas);
1120
    else
1121
	printf("Ran %d tests (%d schemata), %d errors (%d internals), %d leaks\n",
1122
	       nb_tests - old_tests,
1123
	       nb_schematas,
1124
	       nb_errors - old_errors,
1125
	       nb_internals,
1126 1127 1128 1129
	       nb_leaks - old_leaks);
    old_errors = nb_errors;
    old_tests = nb_tests;
    old_leaks = nb_leaks;
1130 1131
    nb_internals = 0;
    nb_schematas = 0;
1132
    xstcMetadata("xstc/Tests/Metadata/SunXMLSchema1-0-20020116.testSet",
1133
		 "xstc/Tests/");
1134
    if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
1135 1136
	printf("Ran %d tests (%d schemata), no errors\n",
	       nb_tests - old_tests, nb_schematas);
1137
    else
1138
	printf("Ran %d tests (%d schemata), %d errors (%d internals), %d leaks\n",
1139
	       nb_tests - old_tests,
1140
	       nb_schematas,
1141
	       nb_errors - old_errors,
1142
	       nb_internals,
1143 1144 1145 1146
	       nb_leaks - old_leaks);
    old_errors = nb_errors;
    old_tests = nb_tests;
    old_leaks = nb_leaks;
1147 1148
    nb_internals = 0;
    nb_schematas = 0;
1149
    xstcMetadata("xstc/Tests/Metadata/MSXMLSchema1-0-20020116.testSet",
1150
		 "xstc/Tests/");
1151
    if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
1152 1153
	printf("Ran %d tests (%d schemata), no errors\n",
	       nb_tests - old_tests, nb_schematas);
1154
    else
1155
	printf("Ran %d tests (%d schemata), %d errors (%d internals), %d leaks\n",
1156
	       nb_tests - old_tests,
1157
	       nb_schematas,
1158
	       nb_errors - old_errors,
1159
	       nb_internals,
1160
	       nb_leaks - old_leaks);
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

    if ((nb_errors == 0) && (nb_leaks == 0)) {
        ret = 0;
	printf("Total %d tests, no errors\n",
	       nb_tests);
    } else {
        ret = 1;
	printf("Total %d tests, %d errors, %d leaks\n",
	       nb_tests, nb_errors, nb_leaks);
    }
1171
    xmlXPathFreeContext(ctxtXPath);
1172 1173 1174
    xmlCleanupParser();
    xmlMemoryDump();

1175 1176
    if (logfile != NULL)
        fclose(logfile);
1177 1178
    return(ret);
}
1179 1180 1181 1182 1183 1184
#else /* !SCHEMAS */
int
main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
    fprintf(stderr, "runsuite requires support for schemas and xpath in libxml2\n");
}
#endif