📄 runsuite.c
字号:
int ret = 0, mem;
xmlResetLastError();
testErrorsSize = 0; testErrors[0] = 0;
mem = xmlMemUsed();
href = getString(cur,
"string(ts:instanceDocument/@xlink:href)");
if ((href == NULL) || (href[0] == 0)) {
test_log("testGroup line %ld misses href for schemaDocument\n",
xmlGetLineNo(cur));
ret = -1;
goto done;
}
path = xmlBuildURI(href, BAD_CAST base);
if (path == NULL) {
fprintf(stderr,
"Failed to build path to schemas testGroup line %ld : %s\n",
xmlGetLineNo(cur), href);
ret = -1;
goto done;
}
if (checkTestFile((const char *) path) <= 0) {
test_log("schemas for testGroup line %ld is missing: %s\n",
xmlGetLineNo(cur), path);
ret = -1;
goto done;
}
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;
}
nb_tests++;
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")) {
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",
path, spath);
nb_internals++;
nb_errors++;
}
} else if (xmlStrEqual(validity, BAD_CAST "invalid")) {
if (ret == 0) {
test_log("Failed to detect invalid instance %s against %s\n",
path, spath);
nb_errors++;
}
} else {
test_log("instanceDocument line %ld has unexpected validity value%s\n",
xmlGetLineNo(cur), validity);
ret = -1;
goto done;
}
done:
if (href != NULL) xmlFree(href);
if (path != NULL) xmlFree(path);
if (validity != NULL) xmlFree(validity);
if (ctxt != NULL) xmlSchemaFreeValidCtxt(ctxt);
if (doc != NULL) xmlFreeDoc(doc);
xmlResetLastError();
if (mem != xmlMemUsed()) {
test_log("Validation of tests starting line %ld leaked %d\n",
xmlGetLineNo(cur), xmlMemUsed() - mem);
nb_leaks++;
}
return(ret);
}
static int
xstcTestGroup(xmlNodePtr cur, const char *base) {
xmlChar *href = NULL;
xmlChar *path = NULL;
xmlChar *validity = NULL;
xmlSchemaPtr schemas = NULL;
xmlSchemaParserCtxtPtr ctxt;
xmlNodePtr instance;
int ret = 0, mem;
xmlResetLastError();
testErrorsSize = 0; testErrors[0] = 0;
mem = xmlMemUsed();
href = getString(cur,
"string(ts:schemaTest/ts:schemaDocument/@xlink:href)");
if ((href == NULL) || (href[0] == 0)) {
test_log("testGroup line %ld misses href for schemaDocument\n",
xmlGetLineNo(cur));
ret = -1;
goto done;
}
path = xmlBuildURI(href, BAD_CAST base);
if (path == NULL) {
test_log("Failed to build path to schemas testGroup line %ld : %s\n",
xmlGetLineNo(cur), href);
ret = -1;
goto done;
}
if (checkTestFile((const char *) path) <= 0) {
test_log("schemas for testGroup line %ld is missing: %s\n",
xmlGetLineNo(cur), path);
ret = -1;
goto done;
}
validity = getString(cur,
"string(ts:schemaTest/ts:expected/@validity)");
if (validity == NULL) {
test_log("testGroup line %ld misses expected validity\n",
xmlGetLineNo(cur));
ret = -1;
goto done;
}
nb_tests++;
if (xmlStrEqual(validity, BAD_CAST "valid")) {
nb_schematas++;
ctxt = xmlSchemaNewParserCtxt((const char *) path);
xmlSchemaSetParserErrors(ctxt,
(xmlSchemaValidityErrorFunc) testErrorHandler,
(xmlSchemaValidityWarningFunc) testErrorHandler,
ctxt);
schemas = xmlSchemaParse(ctxt);
xmlSchemaFreeParserCtxt(ctxt);
if (schemas == NULL) {
test_log("valid schemas %s failed to parse\n",
path);
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++;
nb_errors++;
}
instance = getNext(cur, "./ts:instanceTest[1]");
while (instance != NULL) {
xstcTestInstance(instance, schemas, path, base);
instance = getNext(instance,
"following-sibling::ts:instanceTest[1]");
}
} else if (xmlStrEqual(validity, BAD_CAST "invalid")) {
nb_schematas++;
ctxt = xmlSchemaNewParserCtxt((const char *) path);
xmlSchemaSetParserErrors(ctxt,
(xmlSchemaValidityErrorFunc) testErrorHandler,
(xmlSchemaValidityWarningFunc) testErrorHandler,
ctxt);
schemas = xmlSchemaParse(ctxt);
xmlSchemaFreeParserCtxt(ctxt);
if (schemas != NULL) {
test_log("Failed to detect error in schemas %s\n",
path);
nb_errors++;
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++;
}
} else {
test_log("testGroup line %ld misses unexpected validity value%s\n",
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);
xmlResetLastError();
if ((mem != xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
test_log("Processing test line %ld %s leaked %d\n",
xmlGetLineNo(cur), path, xmlMemUsed() - mem);
nb_leaks++;
}
return(ret);
}
static int
xstcMetadata(const char *metadata, const char *base) {
xmlDocPtr doc;
xmlNodePtr cur;
xmlChar *contributor;
xmlChar *name;
int ret = 0;
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) {
xstcTestGroup(cur, base);
cur = getNext(cur, "following-sibling::ts:testGroup[1]");
}
done:
xmlFreeDoc(doc);
return(ret);
}
/************************************************************************
* *
* The driver for the tests *
* *
************************************************************************/
int
main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
int res, ret = 0;
int old_errors, old_tests, old_leaks;
logfile = fopen(LOGFILE, "w");
if (logfile == NULL) {
fprintf(stderr,
"Could not open the log file, running in verbose mode\n");
verbose = 1;
}
initializeLibxml2();
if ((argc >= 2) && (!strcmp(argv[1], "-v")))
verbose = 1;
old_errors = nb_errors;
old_tests = nb_tests;
old_leaks = nb_leaks;
res = xsdTest();
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;
res = rngTest1();
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;
res = rngTest2();
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;
nb_internals = 0;
nb_schematas = 0;
res = xstcMetadata(
"xstc/Tests/Metadata/NISTXMLSchemaDatatypes.testSet",
"xstc/Tests/Metadata/");
if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
printf("Ran %d tests (%d schemata), no errors\n",
nb_tests - old_tests, nb_schematas);
else
printf("Ran %d tests (%d schemata), %d errors (%d internals), %d leaks\n",
nb_tests - old_tests,
nb_schematas,
nb_errors - old_errors,
nb_internals,
nb_leaks - old_leaks);
old_errors = nb_errors;
old_tests = nb_tests;
old_leaks = nb_leaks;
nb_internals = 0;
nb_schematas = 0;
res = xstcMetadata(
"xstc/Tests/Metadata/SunXMLSchema1-0-20020116.testSet",
"xstc/Tests/");
if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
printf("Ran %d tests (%d schemata), no errors\n",
nb_tests - old_tests, nb_schematas);
else
printf("Ran %d tests (%d schemata), %d errors (%d internals), %d leaks\n",
nb_tests - old_tests,
nb_schematas,
nb_errors - old_errors,
nb_internals,
nb_leaks - old_leaks);
old_errors = nb_errors;
old_tests = nb_tests;
old_leaks = nb_leaks;
nb_internals = 0;
nb_schematas = 0;
res = xstcMetadata(
"xstc/Tests/Metadata/MSXMLSchema1-0-20020116.testSet",
"xstc/Tests/");
if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
printf("Ran %d tests (%d schemata), no errors\n",
nb_tests - old_tests, nb_schematas);
else
printf("Ran %d tests (%d schemata), %d errors (%d internals), %d leaks\n",
nb_tests - old_tests,
nb_schematas,
nb_errors - old_errors,
nb_internals,
nb_leaks - old_leaks);
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);
}
xmlXPathFreeContext(ctxtXPath);
xmlCleanupParser();
xmlMemoryDump();
if (logfile != NULL)
fclose(logfile);
return(ret);
}
#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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -