runxmlconf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * runsuite.c: C program to run libxml2 againts published testsuites
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * daniel@veillard.com
  7. */
  8. #ifdef HAVE_CONFIG_H
  9. #include "libxml.h"
  10. #else
  11. #include <stdio.h>
  12. #endif
  13. #ifdef LIBXML_XPATH_ENABLED
  14. #if !defined(_WIN32) || defined(__CYGWIN__)
  15. #include <unistd.h>
  16. #endif
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <libxml/parser.h>
  22. #include <libxml/parserInternals.h>
  23. #include <libxml/tree.h>
  24. #include <libxml/uri.h>
  25. #include <libxml/xmlreader.h>
  26. #include <libxml/xpath.h>
  27. #include <libxml/xpathInternals.h>
  28. #define LOGFILE "runxmlconf.log"
  29. static FILE *logfile = NULL;
  30. static int verbose = 0;
  31. #define NB_EXPECTED_ERRORS 15
  32. #if defined(_WIN32) && !defined(__CYGWIN__)
  33. #define vsnprintf _vsnprintf
  34. #define snprintf _snprintf
  35. #endif
  36. const char *skipped_tests[] = {
  37. /* http://lists.w3.org/Archives/Public/public-xml-testsuite/2008Jul/0000.html */
  38. "rmt-ns10-035",
  39. NULL
  40. };
  41. /************************************************************************
  42. * *
  43. * File name and path utilities *
  44. * *
  45. ************************************************************************/
  46. static int checkTestFile(const char *filename) {
  47. struct stat buf;
  48. if (stat(filename, &buf) == -1)
  49. return(0);
  50. #if defined(_WIN32) && !defined(__CYGWIN__)
  51. if (!(buf.st_mode & _S_IFREG))
  52. return(0);
  53. #else
  54. if (!S_ISREG(buf.st_mode))
  55. return(0);
  56. #endif
  57. return(1);
  58. }
  59. static xmlChar *composeDir(const xmlChar *dir, const xmlChar *path) {
  60. char buf[500];
  61. if (dir == NULL) return(xmlStrdup(path));
  62. if (path == NULL) return(NULL);
  63. snprintf(buf, 500, "%s/%s", (const char *) dir, (const char *) path);
  64. return(xmlStrdup((const xmlChar *) buf));
  65. }
  66. /************************************************************************
  67. * *
  68. * Libxml2 specific routines *
  69. * *
  70. ************************************************************************/
  71. static int nb_skipped = 0;
  72. static int nb_tests = 0;
  73. static int nb_errors = 0;
  74. static int nb_leaks = 0;
  75. /*
  76. * We need to trap calls to the resolver to not account memory for the catalog
  77. * and not rely on any external resources.
  78. */
  79. static xmlParserInputPtr
  80. testExternalEntityLoader(const char *URL, const char *ID ATTRIBUTE_UNUSED,
  81. xmlParserCtxtPtr ctxt) {
  82. xmlParserInputPtr ret;
  83. ret = xmlNewInputFromFile(ctxt, (const char *) URL);
  84. return(ret);
  85. }
  86. /*
  87. * Trapping the error messages at the generic level to grab the equivalent of
  88. * stderr messages on CLI tools.
  89. */
  90. static char testErrors[32769];
  91. static int testErrorsSize = 0;
  92. static int nbError = 0;
  93. static int nbFatal = 0;
  94. static void test_log(const char *msg, ...) {
  95. va_list args;
  96. if (logfile != NULL) {
  97. fprintf(logfile, "\n------------\n");
  98. va_start(args, msg);
  99. vfprintf(logfile, msg, args);
  100. va_end(args);
  101. fprintf(logfile, "%s", testErrors);
  102. testErrorsSize = 0; testErrors[0] = 0;
  103. }
  104. if (verbose) {
  105. va_start(args, msg);
  106. vfprintf(stderr, msg, args);
  107. va_end(args);
  108. }
  109. }
  110. static void
  111. testErrorHandler(void *userData ATTRIBUTE_UNUSED, xmlErrorPtr error) {
  112. int res;
  113. if (testErrorsSize >= 32768)
  114. return;
  115. res = snprintf(&testErrors[testErrorsSize],
  116. 32768 - testErrorsSize,
  117. "%s:%d: %s\n", (error->file ? error->file : "entity"),
  118. error->line, error->message);
  119. if (error->level == XML_ERR_FATAL)
  120. nbFatal++;
  121. else if (error->level == XML_ERR_ERROR)
  122. nbError++;
  123. if (testErrorsSize + res >= 32768) {
  124. /* buffer is full */
  125. testErrorsSize = 32768;
  126. testErrors[testErrorsSize] = 0;
  127. } else {
  128. testErrorsSize += res;
  129. }
  130. testErrors[testErrorsSize] = 0;
  131. }
  132. static xmlXPathContextPtr ctxtXPath;
  133. static void
  134. initializeLibxml2(void) {
  135. xmlGetWarningsDefaultValue = 0;
  136. xmlPedanticParserDefault(0);
  137. xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
  138. xmlInitParser();
  139. xmlSetExternalEntityLoader(testExternalEntityLoader);
  140. ctxtXPath = xmlXPathNewContext(NULL);
  141. /*
  142. * Deactivate the cache if created; otherwise we have to create/free it
  143. * for every test, since it will confuse the memory leak detection.
  144. * Note that normally this need not be done, since the cache is not
  145. * created until set explicitely with xmlXPathContextSetCache();
  146. * but for test purposes it is sometimes usefull to activate the
  147. * cache by default for the whole library.
  148. */
  149. if (ctxtXPath->cache != NULL)
  150. xmlXPathContextSetCache(ctxtXPath, 0, -1, 0);
  151. xmlSetStructuredErrorFunc(NULL, testErrorHandler);
  152. }
  153. /************************************************************************
  154. * *
  155. * Run the xmlconf test if found *
  156. * *
  157. ************************************************************************/
  158. static int
  159. xmlconfTestInvalid(const char *id, const char *filename, int options) {
  160. xmlDocPtr doc;
  161. xmlParserCtxtPtr ctxt;
  162. int ret = 1;
  163. ctxt = xmlNewParserCtxt();
  164. if (ctxt == NULL) {
  165. test_log("test %s : %s out of memory\n",
  166. id, filename);
  167. return(0);
  168. }
  169. doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
  170. if (doc == NULL) {
  171. test_log("test %s : %s invalid document turned not well-formed too\n",
  172. id, filename);
  173. } else {
  174. /* invalidity should be reported both in the context and in the document */
  175. if ((ctxt->valid != 0) || (doc->properties & XML_DOC_DTDVALID)) {
  176. test_log("test %s : %s failed to detect invalid document\n",
  177. id, filename);
  178. nb_errors++;
  179. ret = 0;
  180. }
  181. xmlFreeDoc(doc);
  182. }
  183. xmlFreeParserCtxt(ctxt);
  184. return(ret);
  185. }
  186. static int
  187. xmlconfTestValid(const char *id, const char *filename, int options) {
  188. xmlDocPtr doc;
  189. xmlParserCtxtPtr ctxt;
  190. int ret = 1;
  191. ctxt = xmlNewParserCtxt();
  192. if (ctxt == NULL) {
  193. test_log("test %s : %s out of memory\n",
  194. id, filename);
  195. return(0);
  196. }
  197. doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
  198. if (doc == NULL) {
  199. test_log("test %s : %s failed to parse a valid document\n",
  200. id, filename);
  201. nb_errors++;
  202. ret = 0;
  203. } else {
  204. /* validity should be reported both in the context and in the document */
  205. if ((ctxt->valid == 0) || ((doc->properties & XML_DOC_DTDVALID) == 0)) {
  206. test_log("test %s : %s failed to validate a valid document\n",
  207. id, filename);
  208. nb_errors++;
  209. ret = 0;
  210. }
  211. xmlFreeDoc(doc);
  212. }
  213. xmlFreeParserCtxt(ctxt);
  214. return(ret);
  215. }
  216. static int
  217. xmlconfTestNotNSWF(const char *id, const char *filename, int options) {
  218. xmlDocPtr doc;
  219. int ret = 1;
  220. /*
  221. * In case of Namespace errors, libxml2 will still parse the document
  222. * but log a Namesapce error.
  223. */
  224. doc = xmlReadFile(filename, NULL, options);
  225. if (doc == NULL) {
  226. test_log("test %s : %s failed to parse the XML\n",
  227. id, filename);
  228. nb_errors++;
  229. ret = 0;
  230. } else {
  231. if ((xmlLastError.code == XML_ERR_OK) ||
  232. (xmlLastError.domain != XML_FROM_NAMESPACE)) {
  233. test_log("test %s : %s failed to detect namespace error\n",
  234. id, filename);
  235. nb_errors++;
  236. ret = 0;
  237. }
  238. xmlFreeDoc(doc);
  239. }
  240. return(ret);
  241. }
  242. static int
  243. xmlconfTestNotWF(const char *id, const char *filename, int options) {
  244. xmlDocPtr doc;
  245. int ret = 1;
  246. doc = xmlReadFile(filename, NULL, options);
  247. if (doc != NULL) {
  248. test_log("test %s : %s failed to detect not well formedness\n",
  249. id, filename);
  250. nb_errors++;
  251. xmlFreeDoc(doc);
  252. ret = 0;
  253. }
  254. return(ret);
  255. }
  256. static int
  257. xmlconfTestItem(xmlDocPtr doc, xmlNodePtr cur) {
  258. int ret = -1;
  259. xmlChar *type = NULL;
  260. xmlChar *filename = NULL;
  261. xmlChar *uri = NULL;
  262. xmlChar *base = NULL;
  263. xmlChar *id = NULL;
  264. xmlChar *rec = NULL;
  265. xmlChar *version = NULL;
  266. xmlChar *entities = NULL;
  267. xmlChar *edition = NULL;
  268. int options = 0;
  269. int nstest = 0;
  270. int mem, final;
  271. int i;
  272. testErrorsSize = 0; testErrors[0] = 0;
  273. nbError = 0;
  274. nbFatal = 0;
  275. id = xmlGetProp(cur, BAD_CAST "ID");
  276. if (id == NULL) {
  277. test_log("test missing ID, line %ld\n", xmlGetLineNo(cur));
  278. goto error;
  279. }
  280. for (i = 0;skipped_tests[i] != NULL;i++) {
  281. if (!strcmp(skipped_tests[i], (char *) id)) {
  282. test_log("Skipping test %s from skipped list\n", (char *) id);
  283. ret = 0;
  284. nb_skipped++;
  285. goto error;
  286. }
  287. }
  288. type = xmlGetProp(cur, BAD_CAST "TYPE");
  289. if (type == NULL) {
  290. test_log("test %s missing TYPE\n", (char *) id);
  291. goto error;
  292. }
  293. uri = xmlGetProp(cur, BAD_CAST "URI");
  294. if (uri == NULL) {
  295. test_log("test %s missing URI\n", (char *) id);
  296. goto error;
  297. }
  298. base = xmlNodeGetBase(doc, cur);
  299. filename = composeDir(base, uri);
  300. if (!checkTestFile((char *) filename)) {
  301. test_log("test %s missing file %s \n", id,
  302. (filename ? (char *)filename : "NULL"));
  303. goto error;
  304. }
  305. version = xmlGetProp(cur, BAD_CAST "VERSION");
  306. entities = xmlGetProp(cur, BAD_CAST "ENTITIES");
  307. if (!xmlStrEqual(entities, BAD_CAST "none")) {
  308. options |= XML_PARSE_DTDLOAD;
  309. options |= XML_PARSE_NOENT;
  310. }
  311. rec = xmlGetProp(cur, BAD_CAST "RECOMMENDATION");
  312. if ((rec == NULL) ||
  313. (xmlStrEqual(rec, BAD_CAST "XML1.0")) ||
  314. (xmlStrEqual(rec, BAD_CAST "XML1.0-errata2e")) ||
  315. (xmlStrEqual(rec, BAD_CAST "XML1.0-errata3e")) ||
  316. (xmlStrEqual(rec, BAD_CAST "XML1.0-errata4e"))) {
  317. if ((version != NULL) && (!xmlStrEqual(version, BAD_CAST "1.0"))) {
  318. test_log("Skipping test %s for %s\n", (char *) id,
  319. (char *) version);
  320. ret = 0;
  321. nb_skipped++;
  322. goto error;
  323. }
  324. ret = 1;
  325. } else if ((xmlStrEqual(rec, BAD_CAST "NS1.0")) ||
  326. (xmlStrEqual(rec, BAD_CAST "NS1.0-errata1e"))) {
  327. ret = 1;
  328. nstest = 1;
  329. } else {
  330. test_log("Skipping test %s for REC %s\n", (char *) id, (char *) rec);
  331. ret = 0;
  332. nb_skipped++;
  333. goto error;
  334. }
  335. edition = xmlGetProp(cur, BAD_CAST "EDITION");
  336. if ((edition != NULL) && (xmlStrchr(edition, '5') == NULL)) {
  337. /* test limited to all versions before 5th */
  338. options |= XML_PARSE_OLD10;
  339. }
  340. /*
  341. * Reset errors and check memory usage before the test
  342. */
  343. xmlResetLastError();
  344. testErrorsSize = 0; testErrors[0] = 0;
  345. mem = xmlMemUsed();
  346. if (xmlStrEqual(type, BAD_CAST "not-wf")) {
  347. if (nstest == 0)
  348. xmlconfTestNotWF((char *) id, (char *) filename, options);
  349. else
  350. xmlconfTestNotNSWF((char *) id, (char *) filename, options);
  351. } else if (xmlStrEqual(type, BAD_CAST "valid")) {
  352. options |= XML_PARSE_DTDVALID;
  353. xmlconfTestValid((char *) id, (char *) filename, options);
  354. } else if (xmlStrEqual(type, BAD_CAST "invalid")) {
  355. options |= XML_PARSE_DTDVALID;
  356. xmlconfTestInvalid((char *) id, (char *) filename, options);
  357. } else if (xmlStrEqual(type, BAD_CAST "error")) {
  358. test_log("Skipping error test %s \n", (char *) id);
  359. ret = 0;
  360. nb_skipped++;
  361. goto error;
  362. } else {
  363. test_log("test %s unknown TYPE value %s\n", (char *) id, (char *)type);
  364. ret = -1;
  365. goto error;
  366. }
  367. /*
  368. * Reset errors and check memory usage after the test
  369. */
  370. xmlResetLastError();
  371. final = xmlMemUsed();
  372. if (final > mem) {
  373. test_log("test %s : %s leaked %d bytes\n",
  374. id, filename, final - mem);
  375. nb_leaks++;
  376. xmlMemDisplayLast(logfile, final - mem);
  377. }
  378. nb_tests++;
  379. error:
  380. if (type != NULL)
  381. xmlFree(type);
  382. if (entities != NULL)
  383. xmlFree(entities);
  384. if (edition != NULL)
  385. xmlFree(edition);
  386. if (version != NULL)
  387. xmlFree(version);
  388. if (filename != NULL)
  389. xmlFree(filename);
  390. if (uri != NULL)
  391. xmlFree(uri);
  392. if (base != NULL)
  393. xmlFree(base);
  394. if (id != NULL)
  395. xmlFree(id);
  396. if (rec != NULL)
  397. xmlFree(rec);
  398. return(ret);
  399. }
  400. static int
  401. xmlconfTestCases(xmlDocPtr doc, xmlNodePtr cur, int level) {
  402. xmlChar *profile;
  403. int ret = 0;
  404. int tests = 0;
  405. int output = 0;
  406. if (level == 1) {
  407. profile = xmlGetProp(cur, BAD_CAST "PROFILE");
  408. if (profile != NULL) {
  409. output = 1;
  410. level++;
  411. printf("Test cases: %s\n", (char *) profile);
  412. xmlFree(profile);
  413. }
  414. }
  415. cur = cur->children;
  416. while (cur != NULL) {
  417. /* look only at elements we ignore everything else */
  418. if (cur->type == XML_ELEMENT_NODE) {
  419. if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
  420. ret += xmlconfTestCases(doc, cur, level);
  421. } else if (xmlStrEqual(cur->name, BAD_CAST "TEST")) {
  422. if (xmlconfTestItem(doc, cur) >= 0)
  423. ret++;
  424. tests++;
  425. } else {
  426. fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
  427. }
  428. }
  429. cur = cur->next;
  430. }
  431. if (output == 1) {
  432. if (tests > 0)
  433. printf("Test cases: %d tests\n", tests);
  434. }
  435. return(ret);
  436. }
  437. static int
  438. xmlconfTestSuite(xmlDocPtr doc, xmlNodePtr cur) {
  439. xmlChar *profile;
  440. int ret = 0;
  441. profile = xmlGetProp(cur, BAD_CAST "PROFILE");
  442. if (profile != NULL) {
  443. printf("Test suite: %s\n", (char *) profile);
  444. xmlFree(profile);
  445. } else
  446. printf("Test suite\n");
  447. cur = cur->children;
  448. while (cur != NULL) {
  449. /* look only at elements we ignore everything else */
  450. if (cur->type == XML_ELEMENT_NODE) {
  451. if (xmlStrEqual(cur->name, BAD_CAST "TESTCASES")) {
  452. ret += xmlconfTestCases(doc, cur, 1);
  453. } else {
  454. fprintf(stderr, "Unhandled element %s\n", (char *)cur->name);
  455. }
  456. }
  457. cur = cur->next;
  458. }
  459. return(ret);
  460. }
  461. static void
  462. xmlconfInfo(void) {
  463. fprintf(stderr, " you need to fetch and extract the\n");
  464. fprintf(stderr, " latest XML Conformance Test Suites\n");
  465. fprintf(stderr, " http://www.w3.org/XML/Test/xmlts20080205.tar.gz\n");
  466. fprintf(stderr, " see http://www.w3.org/XML/Test/ for informations\n");
  467. }
  468. static int
  469. xmlconfTest(void) {
  470. const char *confxml = "xmlconf/xmlconf.xml";
  471. xmlDocPtr doc;
  472. xmlNodePtr cur;
  473. int ret = 0;
  474. if (!checkTestFile(confxml)) {
  475. fprintf(stderr, "%s is missing \n", confxml);
  476. xmlconfInfo();
  477. return(-1);
  478. }
  479. doc = xmlReadFile(confxml, NULL, XML_PARSE_NOENT);
  480. if (doc == NULL) {
  481. fprintf(stderr, "%s is corrupted \n", confxml);
  482. xmlconfInfo();
  483. return(-1);
  484. }
  485. cur = xmlDocGetRootElement(doc);
  486. if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "TESTSUITE"))) {
  487. fprintf(stderr, "Unexpected format %s\n", confxml);
  488. xmlconfInfo();
  489. ret = -1;
  490. } else {
  491. ret = xmlconfTestSuite(doc, cur);
  492. }
  493. xmlFreeDoc(doc);
  494. return(ret);
  495. }
  496. /************************************************************************
  497. * *
  498. * The driver for the tests *
  499. * *
  500. ************************************************************************/
  501. int
  502. main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  503. int ret = 0;
  504. int old_errors, old_tests, old_leaks;
  505. logfile = fopen(LOGFILE, "w");
  506. if (logfile == NULL) {
  507. fprintf(stderr,
  508. "Could not open the log file, running in verbose mode\n");
  509. verbose = 1;
  510. }
  511. initializeLibxml2();
  512. if ((argc >= 2) && (!strcmp(argv[1], "-v")))
  513. verbose = 1;
  514. old_errors = nb_errors;
  515. old_tests = nb_tests;
  516. old_leaks = nb_leaks;
  517. xmlconfTest();
  518. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  519. printf("Ran %d tests, no errors\n", nb_tests - old_tests);
  520. else
  521. printf("Ran %d tests, %d errors, %d leaks\n",
  522. nb_tests - old_tests,
  523. nb_errors - old_errors,
  524. nb_leaks - old_leaks);
  525. if ((nb_errors == 0) && (nb_leaks == 0)) {
  526. ret = 0;
  527. printf("Total %d tests, no errors\n",
  528. nb_tests);
  529. } else {
  530. ret = 1;
  531. printf("Total %d tests, %d errors, %d leaks\n",
  532. nb_tests, nb_errors, nb_leaks);
  533. printf("See %s for detailed output\n", LOGFILE);
  534. if ((nb_leaks == 0) && (nb_errors == NB_EXPECTED_ERRORS)) {
  535. printf("%d errors were expected\n", nb_errors);
  536. ret = 0;
  537. }
  538. }
  539. xmlXPathFreeContext(ctxtXPath);
  540. xmlCleanupParser();
  541. xmlMemoryDump();
  542. if (logfile != NULL)
  543. fclose(logfile);
  544. return(ret);
  545. }
  546. #else /* ! LIBXML_XPATH_ENABLED */
  547. #include <stdio.h>
  548. int
  549. main(int argc, char **argv) {
  550. fprintf(stderr, "%s need XPath support\n", argv[0]);
  551. }
  552. #endif