runsuite.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. #if !defined(_WIN32) || defined(__CYGWIN__)
  14. #include <unistd.h>
  15. #endif
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <libxml/parser.h>
  21. #include <libxml/parserInternals.h>
  22. #include <libxml/tree.h>
  23. #include <libxml/uri.h>
  24. #if defined(LIBXML_SCHEMAS_ENABLED) && defined(LIBXML_XPATH_ENABLED)
  25. #include <libxml/xmlreader.h>
  26. #include <libxml/xpath.h>
  27. #include <libxml/xpathInternals.h>
  28. #include <libxml/relaxng.h>
  29. #include <libxml/xmlschemas.h>
  30. #include <libxml/xmlschemastypes.h>
  31. #define LOGFILE "runsuite.log"
  32. static FILE *logfile = NULL;
  33. static int verbose = 0;
  34. #if defined(_WIN32) && !defined(__CYGWIN__)
  35. #define vsnprintf _vsnprintf
  36. #define snprintf _snprintf
  37. #endif
  38. /************************************************************************
  39. * *
  40. * File name and path utilities *
  41. * *
  42. ************************************************************************/
  43. static int checkTestFile(const char *filename) {
  44. struct stat buf;
  45. if (stat(filename, &buf) == -1)
  46. return(0);
  47. #if defined(_WIN32) && !defined(__CYGWIN__)
  48. if (!(buf.st_mode & _S_IFREG))
  49. return(0);
  50. #else
  51. if (!S_ISREG(buf.st_mode))
  52. return(0);
  53. #endif
  54. return(1);
  55. }
  56. static xmlChar *composeDir(const xmlChar *dir, const xmlChar *path) {
  57. char buf[500];
  58. if (dir == NULL) return(xmlStrdup(path));
  59. if (path == NULL) return(NULL);
  60. snprintf(buf, 500, "%s/%s", (const char *) dir, (const char *) path);
  61. return(xmlStrdup((const xmlChar *) buf));
  62. }
  63. /************************************************************************
  64. * *
  65. * Libxml2 specific routines *
  66. * *
  67. ************************************************************************/
  68. static int nb_tests = 0;
  69. static int nb_errors = 0;
  70. static int nb_internals = 0;
  71. static int nb_schematas = 0;
  72. static int nb_unimplemented = 0;
  73. static int nb_leaks = 0;
  74. static int extraMemoryFromResolver = 0;
  75. static int
  76. fatalError(void) {
  77. fprintf(stderr, "Exitting tests on fatal error\n");
  78. exit(1);
  79. }
  80. /*
  81. * that's needed to implement <resource>
  82. */
  83. #define MAX_ENTITIES 20
  84. static char *testEntitiesName[MAX_ENTITIES];
  85. static char *testEntitiesValue[MAX_ENTITIES];
  86. static int nb_entities = 0;
  87. static void resetEntities(void) {
  88. int i;
  89. for (i = 0;i < nb_entities;i++) {
  90. if (testEntitiesName[i] != NULL)
  91. xmlFree(testEntitiesName[i]);
  92. if (testEntitiesValue[i] != NULL)
  93. xmlFree(testEntitiesValue[i]);
  94. }
  95. nb_entities = 0;
  96. }
  97. static int addEntity(char *name, char *content) {
  98. if (nb_entities >= MAX_ENTITIES) {
  99. fprintf(stderr, "Too many entities defined\n");
  100. return(-1);
  101. }
  102. testEntitiesName[nb_entities] = name;
  103. testEntitiesValue[nb_entities] = content;
  104. nb_entities++;
  105. return(0);
  106. }
  107. /*
  108. * We need to trap calls to the resolver to not account memory for the catalog
  109. * which is shared to the current running test. We also don't want to have
  110. * network downloads modifying tests.
  111. */
  112. static xmlParserInputPtr
  113. testExternalEntityLoader(const char *URL, const char *ID,
  114. xmlParserCtxtPtr ctxt) {
  115. xmlParserInputPtr ret;
  116. int i;
  117. for (i = 0;i < nb_entities;i++) {
  118. if (!strcmp(testEntitiesName[i], URL)) {
  119. ret = xmlNewStringInputStream(ctxt,
  120. (const xmlChar *) testEntitiesValue[i]);
  121. if (ret != NULL) {
  122. ret->filename = (const char *)
  123. xmlStrdup((xmlChar *)testEntitiesName[i]);
  124. }
  125. return(ret);
  126. }
  127. }
  128. if (checkTestFile(URL)) {
  129. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  130. } else {
  131. int memused = xmlMemUsed();
  132. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  133. extraMemoryFromResolver += xmlMemUsed() - memused;
  134. }
  135. #if 0
  136. if (ret == NULL) {
  137. fprintf(stderr, "Failed to find resource %s\n", URL);
  138. }
  139. #endif
  140. return(ret);
  141. }
  142. /*
  143. * Trapping the error messages at the generic level to grab the equivalent of
  144. * stderr messages on CLI tools.
  145. */
  146. static char testErrors[32769];
  147. static int testErrorsSize = 0;
  148. static void test_log(const char *msg, ...) {
  149. va_list args;
  150. if (logfile != NULL) {
  151. fprintf(logfile, "\n------------\n");
  152. va_start(args, msg);
  153. vfprintf(logfile, msg, args);
  154. va_end(args);
  155. fprintf(logfile, "%s", testErrors);
  156. testErrorsSize = 0; testErrors[0] = 0;
  157. }
  158. if (verbose) {
  159. va_start(args, msg);
  160. vfprintf(stderr, msg, args);
  161. va_end(args);
  162. }
  163. }
  164. static void
  165. testErrorHandler(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
  166. va_list args;
  167. int res;
  168. if (testErrorsSize >= 32768)
  169. return;
  170. va_start(args, msg);
  171. res = vsnprintf(&testErrors[testErrorsSize],
  172. 32768 - testErrorsSize,
  173. msg, args);
  174. va_end(args);
  175. if (testErrorsSize + res >= 32768) {
  176. /* buffer is full */
  177. testErrorsSize = 32768;
  178. testErrors[testErrorsSize] = 0;
  179. } else {
  180. testErrorsSize += res;
  181. }
  182. testErrors[testErrorsSize] = 0;
  183. }
  184. static xmlXPathContextPtr ctxtXPath;
  185. static void
  186. initializeLibxml2(void) {
  187. xmlGetWarningsDefaultValue = 0;
  188. xmlPedanticParserDefault(0);
  189. xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
  190. xmlInitParser();
  191. xmlSetExternalEntityLoader(testExternalEntityLoader);
  192. ctxtXPath = xmlXPathNewContext(NULL);
  193. /*
  194. * Deactivate the cache if created; otherwise we have to create/free it
  195. * for every test, since it will confuse the memory leak detection.
  196. * Note that normally this need not be done, since the cache is not
  197. * created until set explicitely with xmlXPathContextSetCache();
  198. * but for test purposes it is sometimes usefull to activate the
  199. * cache by default for the whole library.
  200. */
  201. if (ctxtXPath->cache != NULL)
  202. xmlXPathContextSetCache(ctxtXPath, 0, -1, 0);
  203. /* used as default nanemspace in xstc tests */
  204. xmlXPathRegisterNs(ctxtXPath, BAD_CAST "ts", BAD_CAST "TestSuite");
  205. xmlXPathRegisterNs(ctxtXPath, BAD_CAST "xlink",
  206. BAD_CAST "http://www.w3.org/1999/xlink");
  207. xmlSetGenericErrorFunc(NULL, testErrorHandler);
  208. #ifdef LIBXML_SCHEMAS_ENABLED
  209. xmlSchemaInitTypes();
  210. xmlRelaxNGInitTypes();
  211. #endif
  212. }
  213. static xmlNodePtr
  214. getNext(xmlNodePtr cur, const char *xpath) {
  215. xmlNodePtr ret = NULL;
  216. xmlXPathObjectPtr res;
  217. xmlXPathCompExprPtr comp;
  218. if ((cur == NULL) || (cur->doc == NULL) || (xpath == NULL))
  219. return(NULL);
  220. ctxtXPath->doc = cur->doc;
  221. ctxtXPath->node = cur;
  222. comp = xmlXPathCompile(BAD_CAST xpath);
  223. if (comp == NULL) {
  224. fprintf(stderr, "Failed to compile %s\n", xpath);
  225. return(NULL);
  226. }
  227. res = xmlXPathCompiledEval(comp, ctxtXPath);
  228. xmlXPathFreeCompExpr(comp);
  229. if (res == NULL)
  230. return(NULL);
  231. if ((res->type == XPATH_NODESET) &&
  232. (res->nodesetval != NULL) &&
  233. (res->nodesetval->nodeNr > 0) &&
  234. (res->nodesetval->nodeTab != NULL))
  235. ret = res->nodesetval->nodeTab[0];
  236. xmlXPathFreeObject(res);
  237. return(ret);
  238. }
  239. static xmlChar *
  240. getString(xmlNodePtr cur, const char *xpath) {
  241. xmlChar *ret = NULL;
  242. xmlXPathObjectPtr res;
  243. xmlXPathCompExprPtr comp;
  244. if ((cur == NULL) || (cur->doc == NULL) || (xpath == NULL))
  245. return(NULL);
  246. ctxtXPath->doc = cur->doc;
  247. ctxtXPath->node = cur;
  248. comp = xmlXPathCompile(BAD_CAST xpath);
  249. if (comp == NULL) {
  250. fprintf(stderr, "Failed to compile %s\n", xpath);
  251. return(NULL);
  252. }
  253. res = xmlXPathCompiledEval(comp, ctxtXPath);
  254. xmlXPathFreeCompExpr(comp);
  255. if (res == NULL)
  256. return(NULL);
  257. if (res->type == XPATH_STRING) {
  258. ret = res->stringval;
  259. res->stringval = NULL;
  260. }
  261. xmlXPathFreeObject(res);
  262. return(ret);
  263. }
  264. /************************************************************************
  265. * *
  266. * Test test/xsdtest/xsdtestsuite.xml *
  267. * *
  268. ************************************************************************/
  269. static int
  270. xsdIncorectTestCase(xmlNodePtr cur) {
  271. xmlNodePtr test;
  272. xmlBufferPtr buf;
  273. xmlRelaxNGParserCtxtPtr pctxt;
  274. xmlRelaxNGPtr rng = NULL;
  275. int ret = 0, memt;
  276. cur = getNext(cur, "./incorrect[1]");
  277. if (cur == NULL) {
  278. return(0);
  279. }
  280. test = getNext(cur, "./*");
  281. if (test == NULL) {
  282. test_log("Failed to find test in correct line %ld\n",
  283. xmlGetLineNo(cur));
  284. return(1);
  285. }
  286. memt = xmlMemUsed();
  287. extraMemoryFromResolver = 0;
  288. /*
  289. * dump the schemas to a buffer, then reparse it and compile the schemas
  290. */
  291. buf = xmlBufferCreate();
  292. if (buf == NULL) {
  293. fprintf(stderr, "out of memory !\n");
  294. fatalError();
  295. }
  296. xmlNodeDump(buf, test->doc, test, 0, 0);
  297. pctxt = xmlRelaxNGNewMemParserCtxt((const char *)buf->content, buf->use);
  298. xmlRelaxNGSetParserErrors(pctxt,
  299. (xmlRelaxNGValidityErrorFunc) testErrorHandler,
  300. (xmlRelaxNGValidityWarningFunc) testErrorHandler,
  301. pctxt);
  302. rng = xmlRelaxNGParse(pctxt);
  303. xmlRelaxNGFreeParserCtxt(pctxt);
  304. if (rng != NULL) {
  305. test_log("Failed to detect incorect RNG line %ld\n",
  306. xmlGetLineNo(test));
  307. ret = 1;
  308. goto done;
  309. }
  310. done:
  311. if (buf != NULL)
  312. xmlBufferFree(buf);
  313. if (rng != NULL)
  314. xmlRelaxNGFree(rng);
  315. xmlResetLastError();
  316. if ((memt < xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
  317. test_log("Validation of tests starting line %ld leaked %d\n",
  318. xmlGetLineNo(cur), xmlMemUsed() - memt);
  319. nb_leaks++;
  320. }
  321. return(ret);
  322. }
  323. static void
  324. installResources(xmlNodePtr tst, const xmlChar *base) {
  325. xmlNodePtr test;
  326. xmlBufferPtr buf;
  327. xmlChar *name, *content, *res;
  328. buf = xmlBufferCreate();
  329. if (buf == NULL) {
  330. fprintf(stderr, "out of memory !\n");
  331. fatalError();
  332. }
  333. xmlNodeDump(buf, tst->doc, tst, 0, 0);
  334. while (tst != NULL) {
  335. test = getNext(tst, "./*");
  336. if (test != NULL) {
  337. xmlBufferEmpty(buf);
  338. xmlNodeDump(buf, test->doc, test, 0, 0);
  339. name = getString(tst, "string(@name)");
  340. content = xmlStrdup(buf->content);
  341. if ((name != NULL) && (content != NULL)) {
  342. res = composeDir(base, name);
  343. xmlFree(name);
  344. addEntity((char *) res, (char *) content);
  345. } else {
  346. if (name != NULL) xmlFree(name);
  347. if (content != NULL) xmlFree(content);
  348. }
  349. }
  350. tst = getNext(tst, "following-sibling::resource[1]");
  351. }
  352. if (buf != NULL)
  353. xmlBufferFree(buf);
  354. }
  355. static void
  356. installDirs(xmlNodePtr tst, const xmlChar *base) {
  357. xmlNodePtr test;
  358. xmlChar *name, *res;
  359. name = getString(tst, "string(@name)");
  360. if (name == NULL)
  361. return;
  362. res = composeDir(base, name);
  363. xmlFree(name);
  364. if (res == NULL) {
  365. return;
  366. }
  367. /* Now process resources and subdir recursively */
  368. test = getNext(tst, "./resource[1]");
  369. if (test != NULL) {
  370. installResources(test, res);
  371. }
  372. test = getNext(tst, "./dir[1]");
  373. while (test != NULL) {
  374. installDirs(test, res);
  375. test = getNext(test, "following-sibling::dir[1]");
  376. }
  377. xmlFree(res);
  378. }
  379. static int
  380. xsdTestCase(xmlNodePtr tst) {
  381. xmlNodePtr test, tmp, cur;
  382. xmlBufferPtr buf;
  383. xmlDocPtr doc = NULL;
  384. xmlRelaxNGParserCtxtPtr pctxt;
  385. xmlRelaxNGValidCtxtPtr ctxt;
  386. xmlRelaxNGPtr rng = NULL;
  387. int ret = 0, mem, memt;
  388. xmlChar *dtd;
  389. resetEntities();
  390. testErrorsSize = 0; testErrors[0] = 0;
  391. tmp = getNext(tst, "./dir[1]");
  392. if (tmp != NULL) {
  393. installDirs(tmp, NULL);
  394. }
  395. tmp = getNext(tst, "./resource[1]");
  396. if (tmp != NULL) {
  397. installResources(tmp, NULL);
  398. }
  399. cur = getNext(tst, "./correct[1]");
  400. if (cur == NULL) {
  401. return(xsdIncorectTestCase(tst));
  402. }
  403. test = getNext(cur, "./*");
  404. if (test == NULL) {
  405. fprintf(stderr, "Failed to find test in correct line %ld\n",
  406. xmlGetLineNo(cur));
  407. return(1);
  408. }
  409. memt = xmlMemUsed();
  410. extraMemoryFromResolver = 0;
  411. /*
  412. * dump the schemas to a buffer, then reparse it and compile the schemas
  413. */
  414. buf = xmlBufferCreate();
  415. if (buf == NULL) {
  416. fprintf(stderr, "out of memory !\n");
  417. fatalError();
  418. }
  419. xmlNodeDump(buf, test->doc, test, 0, 0);
  420. pctxt = xmlRelaxNGNewMemParserCtxt((const char *)buf->content, buf->use);
  421. xmlRelaxNGSetParserErrors(pctxt,
  422. (xmlRelaxNGValidityErrorFunc) testErrorHandler,
  423. (xmlRelaxNGValidityWarningFunc) testErrorHandler,
  424. pctxt);
  425. rng = xmlRelaxNGParse(pctxt);
  426. xmlRelaxNGFreeParserCtxt(pctxt);
  427. if (extraMemoryFromResolver)
  428. memt = 0;
  429. if (rng == NULL) {
  430. test_log("Failed to parse RNGtest line %ld\n",
  431. xmlGetLineNo(test));
  432. nb_errors++;
  433. ret = 1;
  434. goto done;
  435. }
  436. /*
  437. * now scan all the siblings of correct to process the <valid> tests
  438. */
  439. tmp = getNext(cur, "following-sibling::valid[1]");
  440. while (tmp != NULL) {
  441. dtd = xmlGetProp(tmp, BAD_CAST "dtd");
  442. test = getNext(tmp, "./*");
  443. if (test == NULL) {
  444. fprintf(stderr, "Failed to find test in <valid> line %ld\n",
  445. xmlGetLineNo(tmp));
  446. } else {
  447. xmlBufferEmpty(buf);
  448. if (dtd != NULL)
  449. xmlBufferAdd(buf, dtd, -1);
  450. xmlNodeDump(buf, test->doc, test, 0, 0);
  451. /*
  452. * We are ready to run the test
  453. */
  454. mem = xmlMemUsed();
  455. extraMemoryFromResolver = 0;
  456. doc = xmlReadMemory((const char *)buf->content, buf->use,
  457. "test", NULL, 0);
  458. if (doc == NULL) {
  459. test_log("Failed to parse valid instance line %ld\n",
  460. xmlGetLineNo(tmp));
  461. nb_errors++;
  462. } else {
  463. nb_tests++;
  464. ctxt = xmlRelaxNGNewValidCtxt(rng);
  465. xmlRelaxNGSetValidErrors(ctxt,
  466. (xmlRelaxNGValidityErrorFunc) testErrorHandler,
  467. (xmlRelaxNGValidityWarningFunc) testErrorHandler,
  468. ctxt);
  469. ret = xmlRelaxNGValidateDoc(ctxt, doc);
  470. xmlRelaxNGFreeValidCtxt(ctxt);
  471. if (ret > 0) {
  472. test_log("Failed to validate valid instance line %ld\n",
  473. xmlGetLineNo(tmp));
  474. nb_errors++;
  475. } else if (ret < 0) {
  476. test_log("Internal error validating instance line %ld\n",
  477. xmlGetLineNo(tmp));
  478. nb_errors++;
  479. }
  480. xmlFreeDoc(doc);
  481. }
  482. xmlResetLastError();
  483. if ((mem != xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
  484. test_log("Validation of instance line %ld leaked %d\n",
  485. xmlGetLineNo(tmp), xmlMemUsed() - mem);
  486. xmlMemoryDump();
  487. nb_leaks++;
  488. }
  489. }
  490. if (dtd != NULL)
  491. xmlFree(dtd);
  492. tmp = getNext(tmp, "following-sibling::valid[1]");
  493. }
  494. /*
  495. * now scan all the siblings of correct to process the <invalid> tests
  496. */
  497. tmp = getNext(cur, "following-sibling::invalid[1]");
  498. while (tmp != NULL) {
  499. test = getNext(tmp, "./*");
  500. if (test == NULL) {
  501. fprintf(stderr, "Failed to find test in <invalid> line %ld\n",
  502. xmlGetLineNo(tmp));
  503. } else {
  504. xmlBufferEmpty(buf);
  505. xmlNodeDump(buf, test->doc, test, 0, 0);
  506. /*
  507. * We are ready to run the test
  508. */
  509. mem = xmlMemUsed();
  510. extraMemoryFromResolver = 0;
  511. doc = xmlReadMemory((const char *)buf->content, buf->use,
  512. "test", NULL, 0);
  513. if (doc == NULL) {
  514. test_log("Failed to parse valid instance line %ld\n",
  515. xmlGetLineNo(tmp));
  516. nb_errors++;
  517. } else {
  518. nb_tests++;
  519. ctxt = xmlRelaxNGNewValidCtxt(rng);
  520. xmlRelaxNGSetValidErrors(ctxt,
  521. (xmlRelaxNGValidityErrorFunc) testErrorHandler,
  522. (xmlRelaxNGValidityWarningFunc) testErrorHandler,
  523. ctxt);
  524. ret = xmlRelaxNGValidateDoc(ctxt, doc);
  525. xmlRelaxNGFreeValidCtxt(ctxt);
  526. if (ret == 0) {
  527. test_log("Failed to detect invalid instance line %ld\n",
  528. xmlGetLineNo(tmp));
  529. nb_errors++;
  530. } else if (ret < 0) {
  531. test_log("Internal error validating instance line %ld\n",
  532. xmlGetLineNo(tmp));
  533. nb_errors++;
  534. }
  535. xmlFreeDoc(doc);
  536. }
  537. xmlResetLastError();
  538. if ((mem != xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
  539. test_log("Validation of instance line %ld leaked %d\n",
  540. xmlGetLineNo(tmp), xmlMemUsed() - mem);
  541. xmlMemoryDump();
  542. nb_leaks++;
  543. }
  544. }
  545. tmp = getNext(tmp, "following-sibling::invalid[1]");
  546. }
  547. done:
  548. if (buf != NULL)
  549. xmlBufferFree(buf);
  550. if (rng != NULL)
  551. xmlRelaxNGFree(rng);
  552. xmlResetLastError();
  553. if ((memt != xmlMemUsed()) && (memt != 0)) {
  554. test_log("Validation of tests starting line %ld leaked %d\n",
  555. xmlGetLineNo(cur), xmlMemUsed() - memt);
  556. nb_leaks++;
  557. }
  558. return(ret);
  559. }
  560. static int
  561. xsdTestSuite(xmlNodePtr cur) {
  562. if (verbose) {
  563. xmlChar *doc = getString(cur, "string(documentation)");
  564. if (doc != NULL) {
  565. printf("Suite %s\n", doc);
  566. xmlFree(doc);
  567. }
  568. }
  569. cur = getNext(cur, "./testCase[1]");
  570. while (cur != NULL) {
  571. xsdTestCase(cur);
  572. cur = getNext(cur, "following-sibling::testCase[1]");
  573. }
  574. return(0);
  575. }
  576. static int
  577. xsdTest(void) {
  578. xmlDocPtr doc;
  579. xmlNodePtr cur;
  580. const char *filename = "test/xsdtest/xsdtestsuite.xml";
  581. int ret = 0;
  582. doc = xmlReadFile(filename, NULL, XML_PARSE_NOENT);
  583. if (doc == NULL) {
  584. fprintf(stderr, "Failed to parse %s\n", filename);
  585. return(-1);
  586. }
  587. printf("## XML Schemas datatypes test suite from James Clark\n");
  588. cur = xmlDocGetRootElement(doc);
  589. if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
  590. fprintf(stderr, "Unexpected format %s\n", filename);
  591. ret = -1;
  592. goto done;
  593. }
  594. cur = getNext(cur, "./testSuite[1]");
  595. if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
  596. fprintf(stderr, "Unexpected format %s\n", filename);
  597. ret = -1;
  598. goto done;
  599. }
  600. while (cur != NULL) {
  601. xsdTestSuite(cur);
  602. cur = getNext(cur, "following-sibling::testSuite[1]");
  603. }
  604. done:
  605. if (doc != NULL)
  606. xmlFreeDoc(doc);
  607. return(ret);
  608. }
  609. static int
  610. rngTestSuite(xmlNodePtr cur) {
  611. if (verbose) {
  612. xmlChar *doc = getString(cur, "string(documentation)");
  613. if (doc != NULL) {
  614. printf("Suite %s\n", doc);
  615. xmlFree(doc);
  616. } else {
  617. doc = getString(cur, "string(section)");
  618. if (doc != NULL) {
  619. printf("Section %s\n", doc);
  620. xmlFree(doc);
  621. }
  622. }
  623. }
  624. cur = getNext(cur, "./testSuite[1]");
  625. while (cur != NULL) {
  626. xsdTestSuite(cur);
  627. cur = getNext(cur, "following-sibling::testSuite[1]");
  628. }
  629. return(0);
  630. }
  631. static int
  632. rngTest1(void) {
  633. xmlDocPtr doc;
  634. xmlNodePtr cur;
  635. const char *filename = "test/relaxng/OASIS/spectest.xml";
  636. int ret = 0;
  637. doc = xmlReadFile(filename, NULL, XML_PARSE_NOENT);
  638. if (doc == NULL) {
  639. fprintf(stderr, "Failed to parse %s\n", filename);
  640. return(-1);
  641. }
  642. printf("## Relax NG test suite from James Clark\n");
  643. cur = xmlDocGetRootElement(doc);
  644. if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
  645. fprintf(stderr, "Unexpected format %s\n", filename);
  646. ret = -1;
  647. goto done;
  648. }
  649. cur = getNext(cur, "./testSuite[1]");
  650. if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
  651. fprintf(stderr, "Unexpected format %s\n", filename);
  652. ret = -1;
  653. goto done;
  654. }
  655. while (cur != NULL) {
  656. rngTestSuite(cur);
  657. cur = getNext(cur, "following-sibling::testSuite[1]");
  658. }
  659. done:
  660. if (doc != NULL)
  661. xmlFreeDoc(doc);
  662. return(ret);
  663. }
  664. static int
  665. rngTest2(void) {
  666. xmlDocPtr doc;
  667. xmlNodePtr cur;
  668. const char *filename = "test/relaxng/testsuite.xml";
  669. int ret = 0;
  670. doc = xmlReadFile(filename, NULL, XML_PARSE_NOENT);
  671. if (doc == NULL) {
  672. fprintf(stderr, "Failed to parse %s\n", filename);
  673. return(-1);
  674. }
  675. printf("## Relax NG test suite for libxml2\n");
  676. cur = xmlDocGetRootElement(doc);
  677. if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
  678. fprintf(stderr, "Unexpected format %s\n", filename);
  679. ret = -1;
  680. goto done;
  681. }
  682. cur = getNext(cur, "./testSuite[1]");
  683. if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSuite"))) {
  684. fprintf(stderr, "Unexpected format %s\n", filename);
  685. ret = -1;
  686. goto done;
  687. }
  688. while (cur != NULL) {
  689. xsdTestSuite(cur);
  690. cur = getNext(cur, "following-sibling::testSuite[1]");
  691. }
  692. done:
  693. if (doc != NULL)
  694. xmlFreeDoc(doc);
  695. return(ret);
  696. }
  697. /************************************************************************
  698. * *
  699. * Schemas test suites from W3C/NIST/MS/Sun *
  700. * *
  701. ************************************************************************/
  702. static int
  703. xstcTestInstance(xmlNodePtr cur, xmlSchemaPtr schemas,
  704. const xmlChar *spath, const char *base) {
  705. xmlChar *href = NULL;
  706. xmlChar *path = NULL;
  707. xmlChar *validity = NULL;
  708. xmlSchemaValidCtxtPtr ctxt = NULL;
  709. xmlDocPtr doc = NULL;
  710. int ret = 0, mem;
  711. xmlResetLastError();
  712. testErrorsSize = 0; testErrors[0] = 0;
  713. mem = xmlMemUsed();
  714. href = getString(cur,
  715. "string(ts:instanceDocument/@xlink:href)");
  716. if ((href == NULL) || (href[0] == 0)) {
  717. test_log("testGroup line %ld misses href for schemaDocument\n",
  718. xmlGetLineNo(cur));
  719. ret = -1;
  720. goto done;
  721. }
  722. path = xmlBuildURI(href, BAD_CAST base);
  723. if (path == NULL) {
  724. fprintf(stderr,
  725. "Failed to build path to schemas testGroup line %ld : %s\n",
  726. xmlGetLineNo(cur), href);
  727. ret = -1;
  728. goto done;
  729. }
  730. if (checkTestFile((const char *) path) <= 0) {
  731. test_log("schemas for testGroup line %ld is missing: %s\n",
  732. xmlGetLineNo(cur), path);
  733. ret = -1;
  734. goto done;
  735. }
  736. validity = getString(cur,
  737. "string(ts:expected/@validity)");
  738. if (validity == NULL) {
  739. fprintf(stderr, "instanceDocument line %ld misses expected validity\n",
  740. xmlGetLineNo(cur));
  741. ret = -1;
  742. goto done;
  743. }
  744. nb_tests++;
  745. doc = xmlReadFile((const char *) path, NULL, XML_PARSE_NOENT);
  746. if (doc == NULL) {
  747. fprintf(stderr, "instance %s fails to parse\n", path);
  748. ret = -1;
  749. nb_errors++;
  750. goto done;
  751. }
  752. ctxt = xmlSchemaNewValidCtxt(schemas);
  753. xmlSchemaSetValidErrors(ctxt,
  754. (xmlSchemaValidityErrorFunc) testErrorHandler,
  755. (xmlSchemaValidityWarningFunc) testErrorHandler,
  756. ctxt);
  757. ret = xmlSchemaValidateDoc(ctxt, doc);
  758. if (xmlStrEqual(validity, BAD_CAST "valid")) {
  759. if (ret > 0) {
  760. test_log("valid instance %s failed to validate against %s\n",
  761. path, spath);
  762. nb_errors++;
  763. } else if (ret < 0) {
  764. test_log("valid instance %s got internal error validating %s\n",
  765. path, spath);
  766. nb_internals++;
  767. nb_errors++;
  768. }
  769. } else if (xmlStrEqual(validity, BAD_CAST "invalid")) {
  770. if (ret == 0) {
  771. test_log("Failed to detect invalid instance %s against %s\n",
  772. path, spath);
  773. nb_errors++;
  774. }
  775. } else {
  776. test_log("instanceDocument line %ld has unexpected validity value%s\n",
  777. xmlGetLineNo(cur), validity);
  778. ret = -1;
  779. goto done;
  780. }
  781. done:
  782. if (href != NULL) xmlFree(href);
  783. if (path != NULL) xmlFree(path);
  784. if (validity != NULL) xmlFree(validity);
  785. if (ctxt != NULL) xmlSchemaFreeValidCtxt(ctxt);
  786. if (doc != NULL) xmlFreeDoc(doc);
  787. xmlResetLastError();
  788. if (mem != xmlMemUsed()) {
  789. test_log("Validation of tests starting line %ld leaked %d\n",
  790. xmlGetLineNo(cur), xmlMemUsed() - mem);
  791. nb_leaks++;
  792. }
  793. return(ret);
  794. }
  795. static int
  796. xstcTestGroup(xmlNodePtr cur, const char *base) {
  797. xmlChar *href = NULL;
  798. xmlChar *path = NULL;
  799. xmlChar *validity = NULL;
  800. xmlSchemaPtr schemas = NULL;
  801. xmlSchemaParserCtxtPtr ctxt;
  802. xmlNodePtr instance;
  803. int ret = 0, mem;
  804. xmlResetLastError();
  805. testErrorsSize = 0; testErrors[0] = 0;
  806. mem = xmlMemUsed();
  807. href = getString(cur,
  808. "string(ts:schemaTest/ts:schemaDocument/@xlink:href)");
  809. if ((href == NULL) || (href[0] == 0)) {
  810. test_log("testGroup line %ld misses href for schemaDocument\n",
  811. xmlGetLineNo(cur));
  812. ret = -1;
  813. goto done;
  814. }
  815. path = xmlBuildURI(href, BAD_CAST base);
  816. if (path == NULL) {
  817. test_log("Failed to build path to schemas testGroup line %ld : %s\n",
  818. xmlGetLineNo(cur), href);
  819. ret = -1;
  820. goto done;
  821. }
  822. if (checkTestFile((const char *) path) <= 0) {
  823. test_log("schemas for testGroup line %ld is missing: %s\n",
  824. xmlGetLineNo(cur), path);
  825. ret = -1;
  826. goto done;
  827. }
  828. validity = getString(cur,
  829. "string(ts:schemaTest/ts:expected/@validity)");
  830. if (validity == NULL) {
  831. test_log("testGroup line %ld misses expected validity\n",
  832. xmlGetLineNo(cur));
  833. ret = -1;
  834. goto done;
  835. }
  836. nb_tests++;
  837. if (xmlStrEqual(validity, BAD_CAST "valid")) {
  838. nb_schematas++;
  839. ctxt = xmlSchemaNewParserCtxt((const char *) path);
  840. xmlSchemaSetParserErrors(ctxt,
  841. (xmlSchemaValidityErrorFunc) testErrorHandler,
  842. (xmlSchemaValidityWarningFunc) testErrorHandler,
  843. ctxt);
  844. schemas = xmlSchemaParse(ctxt);
  845. xmlSchemaFreeParserCtxt(ctxt);
  846. if (schemas == NULL) {
  847. test_log("valid schemas %s failed to parse\n",
  848. path);
  849. ret = 1;
  850. nb_errors++;
  851. }
  852. if ((ret == 0) && (strstr(testErrors, "nimplemented") != NULL)) {
  853. test_log("valid schemas %s hit an unimplemented block\n",
  854. path);
  855. ret = 1;
  856. nb_unimplemented++;
  857. nb_errors++;
  858. }
  859. instance = getNext(cur, "./ts:instanceTest[1]");
  860. while (instance != NULL) {
  861. if (schemas != NULL) {
  862. xstcTestInstance(instance, schemas, path, base);
  863. } else {
  864. /*
  865. * We'll automatically mark the instances as failed
  866. * if the schema was broken.
  867. */
  868. nb_errors++;
  869. }
  870. instance = getNext(instance,
  871. "following-sibling::ts:instanceTest[1]");
  872. }
  873. } else if (xmlStrEqual(validity, BAD_CAST "invalid")) {
  874. nb_schematas++;
  875. ctxt = xmlSchemaNewParserCtxt((const char *) path);
  876. xmlSchemaSetParserErrors(ctxt,
  877. (xmlSchemaValidityErrorFunc) testErrorHandler,
  878. (xmlSchemaValidityWarningFunc) testErrorHandler,
  879. ctxt);
  880. schemas = xmlSchemaParse(ctxt);
  881. xmlSchemaFreeParserCtxt(ctxt);
  882. if (schemas != NULL) {
  883. test_log("Failed to detect error in schemas %s\n",
  884. path);
  885. nb_errors++;
  886. ret = 1;
  887. }
  888. if ((ret == 0) && (strstr(testErrors, "nimplemented") != NULL)) {
  889. nb_unimplemented++;
  890. test_log("invalid schemas %s hit an unimplemented block\n",
  891. path);
  892. ret = 1;
  893. nb_errors++;
  894. }
  895. } else {
  896. test_log("testGroup line %ld misses unexpected validity value%s\n",
  897. xmlGetLineNo(cur), validity);
  898. ret = -1;
  899. goto done;
  900. }
  901. done:
  902. if (href != NULL) xmlFree(href);
  903. if (path != NULL) xmlFree(path);
  904. if (validity != NULL) xmlFree(validity);
  905. if (schemas != NULL) xmlSchemaFree(schemas);
  906. xmlResetLastError();
  907. if ((mem != xmlMemUsed()) && (extraMemoryFromResolver == 0)) {
  908. test_log("Processing test line %ld %s leaked %d\n",
  909. xmlGetLineNo(cur), path, xmlMemUsed() - mem);
  910. nb_leaks++;
  911. }
  912. return(ret);
  913. }
  914. static int
  915. xstcMetadata(const char *metadata, const char *base) {
  916. xmlDocPtr doc;
  917. xmlNodePtr cur;
  918. xmlChar *contributor;
  919. xmlChar *name;
  920. int ret = 0;
  921. doc = xmlReadFile(metadata, NULL, XML_PARSE_NOENT);
  922. if (doc == NULL) {
  923. fprintf(stderr, "Failed to parse %s\n", metadata);
  924. return(-1);
  925. }
  926. cur = xmlDocGetRootElement(doc);
  927. if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testSet"))) {
  928. fprintf(stderr, "Unexpected format %s\n", metadata);
  929. return(-1);
  930. }
  931. contributor = xmlGetProp(cur, BAD_CAST "contributor");
  932. if (contributor == NULL) {
  933. contributor = xmlStrdup(BAD_CAST "Unknown");
  934. }
  935. name = xmlGetProp(cur, BAD_CAST "name");
  936. if (name == NULL) {
  937. name = xmlStrdup(BAD_CAST "Unknown");
  938. }
  939. printf("## %s test suite for Schemas version %s\n", contributor, name);
  940. xmlFree(contributor);
  941. xmlFree(name);
  942. cur = getNext(cur, "./ts:testGroup[1]");
  943. if ((cur == NULL) || (!xmlStrEqual(cur->name, BAD_CAST "testGroup"))) {
  944. fprintf(stderr, "Unexpected format %s\n", metadata);
  945. ret = -1;
  946. goto done;
  947. }
  948. while (cur != NULL) {
  949. xstcTestGroup(cur, base);
  950. cur = getNext(cur, "following-sibling::ts:testGroup[1]");
  951. }
  952. done:
  953. xmlFreeDoc(doc);
  954. return(ret);
  955. }
  956. /************************************************************************
  957. * *
  958. * The driver for the tests *
  959. * *
  960. ************************************************************************/
  961. int
  962. main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  963. int ret = 0;
  964. int old_errors, old_tests, old_leaks;
  965. logfile = fopen(LOGFILE, "w");
  966. if (logfile == NULL) {
  967. fprintf(stderr,
  968. "Could not open the log file, running in verbose mode\n");
  969. verbose = 1;
  970. }
  971. initializeLibxml2();
  972. if ((argc >= 2) && (!strcmp(argv[1], "-v")))
  973. verbose = 1;
  974. old_errors = nb_errors;
  975. old_tests = nb_tests;
  976. old_leaks = nb_leaks;
  977. xsdTest();
  978. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  979. printf("Ran %d tests, no errors\n", nb_tests - old_tests);
  980. else
  981. printf("Ran %d tests, %d errors, %d leaks\n",
  982. nb_tests - old_tests,
  983. nb_errors - old_errors,
  984. nb_leaks - old_leaks);
  985. old_errors = nb_errors;
  986. old_tests = nb_tests;
  987. old_leaks = nb_leaks;
  988. rngTest1();
  989. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  990. printf("Ran %d tests, no errors\n", nb_tests - old_tests);
  991. else
  992. printf("Ran %d tests, %d errors, %d leaks\n",
  993. nb_tests - old_tests,
  994. nb_errors - old_errors,
  995. nb_leaks - old_leaks);
  996. old_errors = nb_errors;
  997. old_tests = nb_tests;
  998. old_leaks = nb_leaks;
  999. rngTest2();
  1000. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  1001. printf("Ran %d tests, no errors\n", nb_tests - old_tests);
  1002. else
  1003. printf("Ran %d tests, %d errors, %d leaks\n",
  1004. nb_tests - old_tests,
  1005. nb_errors - old_errors,
  1006. nb_leaks - old_leaks);
  1007. old_errors = nb_errors;
  1008. old_tests = nb_tests;
  1009. old_leaks = nb_leaks;
  1010. nb_internals = 0;
  1011. nb_schematas = 0;
  1012. xstcMetadata("xstc/Tests/Metadata/NISTXMLSchemaDatatypes.testSet",
  1013. "xstc/Tests/Metadata/");
  1014. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  1015. printf("Ran %d tests (%d schemata), no errors\n",
  1016. nb_tests - old_tests, nb_schematas);
  1017. else
  1018. printf("Ran %d tests (%d schemata), %d errors (%d internals), %d leaks\n",
  1019. nb_tests - old_tests,
  1020. nb_schematas,
  1021. nb_errors - old_errors,
  1022. nb_internals,
  1023. nb_leaks - old_leaks);
  1024. old_errors = nb_errors;
  1025. old_tests = nb_tests;
  1026. old_leaks = nb_leaks;
  1027. nb_internals = 0;
  1028. nb_schematas = 0;
  1029. xstcMetadata("xstc/Tests/Metadata/SunXMLSchema1-0-20020116.testSet",
  1030. "xstc/Tests/");
  1031. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  1032. printf("Ran %d tests (%d schemata), no errors\n",
  1033. nb_tests - old_tests, nb_schematas);
  1034. else
  1035. printf("Ran %d tests (%d schemata), %d errors (%d internals), %d leaks\n",
  1036. nb_tests - old_tests,
  1037. nb_schematas,
  1038. nb_errors - old_errors,
  1039. nb_internals,
  1040. nb_leaks - old_leaks);
  1041. old_errors = nb_errors;
  1042. old_tests = nb_tests;
  1043. old_leaks = nb_leaks;
  1044. nb_internals = 0;
  1045. nb_schematas = 0;
  1046. xstcMetadata("xstc/Tests/Metadata/MSXMLSchema1-0-20020116.testSet",
  1047. "xstc/Tests/");
  1048. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  1049. printf("Ran %d tests (%d schemata), no errors\n",
  1050. nb_tests - old_tests, nb_schematas);
  1051. else
  1052. printf("Ran %d tests (%d schemata), %d errors (%d internals), %d leaks\n",
  1053. nb_tests - old_tests,
  1054. nb_schematas,
  1055. nb_errors - old_errors,
  1056. nb_internals,
  1057. nb_leaks - old_leaks);
  1058. if ((nb_errors == 0) && (nb_leaks == 0)) {
  1059. ret = 0;
  1060. printf("Total %d tests, no errors\n",
  1061. nb_tests);
  1062. } else {
  1063. ret = 1;
  1064. printf("Total %d tests, %d errors, %d leaks\n",
  1065. nb_tests, nb_errors, nb_leaks);
  1066. }
  1067. xmlXPathFreeContext(ctxtXPath);
  1068. xmlCleanupParser();
  1069. xmlMemoryDump();
  1070. if (logfile != NULL)
  1071. fclose(logfile);
  1072. return(ret);
  1073. }
  1074. #else /* !SCHEMAS */
  1075. int
  1076. main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  1077. fprintf(stderr, "runsuite requires support for schemas and xpath in libxml2\n");
  1078. }
  1079. #endif