runsuite.c 31 KB

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