testrecurse.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * testrecurse.c: C program to run libxml2 regression tests checking entities
  3. * recursions
  4. *
  5. * To compile on Unixes:
  6. * cc -o testrecurse `xml2-config --cflags` testrecurse.c `xml2-config --libs` -lpthread
  7. *
  8. * See Copyright for the status of this software.
  9. *
  10. * daniel@veillard.com
  11. */
  12. #include "libxml.h"
  13. #include <stdio.h>
  14. #if !defined(_WIN32)
  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/tree.h>
  23. #include <libxml/uri.h>
  24. #ifdef LIBXML_READER_ENABLED
  25. #include <libxml/xmlreader.h>
  26. #endif
  27. /*
  28. * O_BINARY is just for Windows compatibility - if it isn't defined
  29. * on this system, avoid any compilation error
  30. */
  31. #ifdef O_BINARY
  32. #define RD_FLAGS O_RDONLY | O_BINARY
  33. #else
  34. #define RD_FLAGS O_RDONLY
  35. #endif
  36. typedef int (*functest) (const char *filename, const char *result,
  37. const char *error, int options);
  38. typedef struct testDesc testDesc;
  39. typedef testDesc *testDescPtr;
  40. struct testDesc {
  41. const char *desc; /* description of the test */
  42. functest func; /* function implementing the test */
  43. const char *in; /* glob to path for input files */
  44. const char *out; /* output directory */
  45. const char *suffix;/* suffix for output files */
  46. const char *err; /* suffix for error output files */
  47. int options; /* parser options for the test */
  48. };
  49. static int checkTestFile(const char *filename);
  50. #if defined(_WIN32)
  51. #include <windows.h>
  52. #include <io.h>
  53. typedef struct
  54. {
  55. size_t gl_pathc; /* Count of paths matched so far */
  56. char **gl_pathv; /* List of matched pathnames. */
  57. size_t gl_offs; /* Slots to reserve in 'gl_pathv'. */
  58. } glob_t;
  59. #define GLOB_DOOFFS 0
  60. static int glob(const char *pattern, ATTRIBUTE_UNUSED int flags,
  61. ATTRIBUTE_UNUSED int errfunc(const char *epath, int eerrno),
  62. glob_t *pglob) {
  63. glob_t *ret;
  64. WIN32_FIND_DATA FindFileData;
  65. HANDLE hFind;
  66. unsigned int nb_paths = 0;
  67. char directory[500];
  68. int len;
  69. if ((pattern == NULL) || (pglob == NULL)) return(-1);
  70. strncpy(directory, pattern, 499);
  71. for (len = strlen(directory);len >= 0;len--) {
  72. if (directory[len] == '/') {
  73. len++;
  74. directory[len] = 0;
  75. break;
  76. }
  77. }
  78. if (len <= 0)
  79. len = 0;
  80. ret = pglob;
  81. memset(ret, 0, sizeof(glob_t));
  82. hFind = FindFirstFileA(pattern, &FindFileData);
  83. if (hFind == INVALID_HANDLE_VALUE)
  84. return(0);
  85. nb_paths = 20;
  86. ret->gl_pathv = (char **) malloc(nb_paths * sizeof(char *));
  87. if (ret->gl_pathv == NULL) {
  88. FindClose(hFind);
  89. return(-1);
  90. }
  91. strncpy(directory + len, FindFileData.cFileName, 499 - len);
  92. ret->gl_pathv[ret->gl_pathc] = strdup(directory);
  93. if (ret->gl_pathv[ret->gl_pathc] == NULL)
  94. goto done;
  95. ret->gl_pathc++;
  96. while(FindNextFileA(hFind, &FindFileData)) {
  97. if (FindFileData.cFileName[0] == '.')
  98. continue;
  99. if (ret->gl_pathc + 2 > nb_paths) {
  100. char **tmp = realloc(ret->gl_pathv, nb_paths * 2 * sizeof(char *));
  101. if (tmp == NULL)
  102. break;
  103. ret->gl_pathv = tmp;
  104. nb_paths *= 2;
  105. }
  106. strncpy(directory + len, FindFileData.cFileName, 499 - len);
  107. ret->gl_pathv[ret->gl_pathc] = strdup(directory);
  108. if (ret->gl_pathv[ret->gl_pathc] == NULL)
  109. break;
  110. ret->gl_pathc++;
  111. }
  112. ret->gl_pathv[ret->gl_pathc] = NULL;
  113. done:
  114. FindClose(hFind);
  115. return(0);
  116. }
  117. static void globfree(glob_t *pglob) {
  118. unsigned int i;
  119. if (pglob == NULL)
  120. return;
  121. for (i = 0;i < pglob->gl_pathc;i++) {
  122. if (pglob->gl_pathv[i] != NULL)
  123. free(pglob->gl_pathv[i]);
  124. }
  125. }
  126. #else
  127. #include <glob.h>
  128. #endif
  129. /************************************************************************
  130. * *
  131. * Huge document generator *
  132. * *
  133. ************************************************************************/
  134. #include <libxml/xmlIO.h>
  135. static const char *start = "<!DOCTYPE foo [\
  136. <!ENTITY f 'some internal data'> \
  137. <!ENTITY e '&f;&f;'> \
  138. <!ENTITY d '&e;&e;'> \
  139. ]> \
  140. <foo>";
  141. static const char *segment = " <bar>&e; &f; &d;</bar>\n";
  142. static const char *finish = "</foo>";
  143. static int curseg = 0;
  144. static const char *current;
  145. static int rlen;
  146. /**
  147. * hugeMatch:
  148. * @URI: an URI to test
  149. *
  150. * Check for an huge: query
  151. *
  152. * Returns 1 if yes and 0 if another Input module should be used
  153. */
  154. static int
  155. hugeMatch(const char * URI) {
  156. if ((URI != NULL) && (!strncmp(URI, "huge:", 4)))
  157. return(1);
  158. return(0);
  159. }
  160. /**
  161. * hugeOpen:
  162. * @URI: an URI to test
  163. *
  164. * Return a pointer to the huge: query handler, in this example simply
  165. * the current pointer...
  166. *
  167. * Returns an Input context or NULL in case or error
  168. */
  169. static void *
  170. hugeOpen(const char * URI) {
  171. if ((URI == NULL) || (strncmp(URI, "huge:", 4)))
  172. return(NULL);
  173. rlen = strlen(start);
  174. current = start;
  175. return((void *) current);
  176. }
  177. /**
  178. * hugeClose:
  179. * @context: the read context
  180. *
  181. * Close the huge: query handler
  182. *
  183. * Returns 0 or -1 in case of error
  184. */
  185. static int
  186. hugeClose(void * context) {
  187. if (context == NULL) return(-1);
  188. return(0);
  189. }
  190. #define MAX_NODES 1000000
  191. /**
  192. * hugeRead:
  193. * @context: the read context
  194. * @buffer: where to store data
  195. * @len: number of bytes to read
  196. *
  197. * Implement an huge: query read.
  198. *
  199. * Returns the number of bytes read or -1 in case of error
  200. */
  201. static int
  202. hugeRead(void *context, char *buffer, int len)
  203. {
  204. if ((context == NULL) || (buffer == NULL) || (len < 0))
  205. return (-1);
  206. if (len >= rlen) {
  207. if (curseg >= MAX_NODES + 1) {
  208. rlen = 0;
  209. return(0);
  210. }
  211. len = rlen;
  212. rlen = 0;
  213. memcpy(buffer, current, len);
  214. curseg ++;
  215. if (curseg == MAX_NODES) {
  216. fprintf(stderr, "\n");
  217. rlen = strlen(finish);
  218. current = finish;
  219. } else {
  220. if (curseg % (MAX_NODES / 10) == 0)
  221. fprintf(stderr, ".");
  222. rlen = strlen(segment);
  223. current = segment;
  224. }
  225. } else {
  226. memcpy(buffer, current, len);
  227. rlen -= len;
  228. current += len;
  229. }
  230. return (len);
  231. }
  232. /************************************************************************
  233. * *
  234. * Libxml2 specific routines *
  235. * *
  236. ************************************************************************/
  237. static int nb_tests = 0;
  238. static int nb_errors = 0;
  239. static int nb_leaks = 0;
  240. static int extraMemoryFromResolver = 0;
  241. static int
  242. fatalError(void) {
  243. fprintf(stderr, "Exitting tests on fatal error\n");
  244. exit(1);
  245. }
  246. /*
  247. * We need to trap calls to the resolver to not account memory for the catalog
  248. * which is shared to the current running test. We also don't want to have
  249. * network downloads modifying tests.
  250. */
  251. static xmlParserInputPtr
  252. testExternalEntityLoader(const char *URL, const char *ID,
  253. xmlParserCtxtPtr ctxt) {
  254. xmlParserInputPtr ret;
  255. if (checkTestFile(URL)) {
  256. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  257. } else {
  258. int memused = xmlMemUsed();
  259. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  260. extraMemoryFromResolver += xmlMemUsed() - memused;
  261. }
  262. return(ret);
  263. }
  264. /*
  265. * Trapping the error messages at the generic level to grab the equivalent of
  266. * stderr messages on CLI tools.
  267. */
  268. static char testErrors[32769];
  269. static int testErrorsSize = 0;
  270. static void XMLCDECL
  271. channel(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
  272. va_list args;
  273. int res;
  274. if (testErrorsSize >= 32768)
  275. return;
  276. va_start(args, msg);
  277. res = vsnprintf(&testErrors[testErrorsSize],
  278. 32768 - testErrorsSize,
  279. msg, args);
  280. va_end(args);
  281. if (testErrorsSize + res >= 32768) {
  282. /* buffer is full */
  283. testErrorsSize = 32768;
  284. testErrors[testErrorsSize] = 0;
  285. } else {
  286. testErrorsSize += res;
  287. }
  288. testErrors[testErrorsSize] = 0;
  289. }
  290. /**
  291. * xmlParserPrintFileContext:
  292. * @input: an xmlParserInputPtr input
  293. *
  294. * Displays current context within the input content for error tracking
  295. */
  296. static void
  297. xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
  298. xmlGenericErrorFunc chanl, void *data ) {
  299. const xmlChar *cur, *base;
  300. unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
  301. xmlChar content[81]; /* space for 80 chars + line terminator */
  302. xmlChar *ctnt;
  303. if (input == NULL) return;
  304. cur = input->cur;
  305. base = input->base;
  306. /* skip backwards over any end-of-lines */
  307. while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
  308. cur--;
  309. }
  310. n = 0;
  311. /* search backwards for beginning-of-line (to max buff size) */
  312. while ((n++ < (sizeof(content)-1)) && (cur > base) &&
  313. (*(cur) != '\n') && (*(cur) != '\r'))
  314. cur--;
  315. if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
  316. /* calculate the error position in terms of the current position */
  317. col = input->cur - cur;
  318. /* search forward for end-of-line (to max buff size) */
  319. n = 0;
  320. ctnt = content;
  321. /* copy selected text to our buffer */
  322. while ((*cur != 0) && (*(cur) != '\n') &&
  323. (*(cur) != '\r') && (n < sizeof(content)-1)) {
  324. *ctnt++ = *cur++;
  325. n++;
  326. }
  327. *ctnt = 0;
  328. /* print out the selected text */
  329. chanl(data ,"%s\n", content);
  330. /* create blank line with problem pointer */
  331. n = 0;
  332. ctnt = content;
  333. /* (leave buffer space for pointer + line terminator) */
  334. while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
  335. if (*(ctnt) != '\t')
  336. *(ctnt) = ' ';
  337. ctnt++;
  338. }
  339. *ctnt++ = '^';
  340. *ctnt = 0;
  341. chanl(data ,"%s\n", content);
  342. }
  343. static void
  344. testStructuredErrorHandler(void *ctx ATTRIBUTE_UNUSED, xmlErrorPtr err) {
  345. char *file = NULL;
  346. int line = 0;
  347. int code = -1;
  348. int domain;
  349. void *data = NULL;
  350. const char *str;
  351. const xmlChar *name = NULL;
  352. xmlNodePtr node;
  353. xmlErrorLevel level;
  354. xmlParserInputPtr input = NULL;
  355. xmlParserInputPtr cur = NULL;
  356. xmlParserCtxtPtr ctxt = NULL;
  357. if (err == NULL)
  358. return;
  359. file = err->file;
  360. line = err->line;
  361. code = err->code;
  362. domain = err->domain;
  363. level = err->level;
  364. node = err->node;
  365. if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
  366. (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
  367. (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
  368. ctxt = err->ctxt;
  369. }
  370. str = err->message;
  371. if (code == XML_ERR_OK)
  372. return;
  373. if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
  374. name = node->name;
  375. /*
  376. * Maintain the compatibility with the legacy error handling
  377. */
  378. if (ctxt != NULL) {
  379. input = ctxt->input;
  380. if ((input != NULL) && (input->filename == NULL) &&
  381. (ctxt->inputNr > 1)) {
  382. cur = input;
  383. input = ctxt->inputTab[ctxt->inputNr - 2];
  384. }
  385. if (input != NULL) {
  386. if (input->filename)
  387. channel(data, "%s:%d: ", input->filename, input->line);
  388. else if ((line != 0) && (domain == XML_FROM_PARSER))
  389. channel(data, "Entity: line %d: ", input->line);
  390. }
  391. } else {
  392. if (file != NULL)
  393. channel(data, "%s:%d: ", file, line);
  394. else if ((line != 0) && (domain == XML_FROM_PARSER))
  395. channel(data, "Entity: line %d: ", line);
  396. }
  397. if (name != NULL) {
  398. channel(data, "element %s: ", name);
  399. }
  400. if (code == XML_ERR_OK)
  401. return;
  402. switch (domain) {
  403. case XML_FROM_PARSER:
  404. channel(data, "parser ");
  405. break;
  406. case XML_FROM_NAMESPACE:
  407. channel(data, "namespace ");
  408. break;
  409. case XML_FROM_DTD:
  410. case XML_FROM_VALID:
  411. channel(data, "validity ");
  412. break;
  413. case XML_FROM_HTML:
  414. channel(data, "HTML parser ");
  415. break;
  416. case XML_FROM_MEMORY:
  417. channel(data, "memory ");
  418. break;
  419. case XML_FROM_OUTPUT:
  420. channel(data, "output ");
  421. break;
  422. case XML_FROM_IO:
  423. channel(data, "I/O ");
  424. break;
  425. case XML_FROM_XINCLUDE:
  426. channel(data, "XInclude ");
  427. break;
  428. case XML_FROM_XPATH:
  429. channel(data, "XPath ");
  430. break;
  431. case XML_FROM_XPOINTER:
  432. channel(data, "parser ");
  433. break;
  434. case XML_FROM_REGEXP:
  435. channel(data, "regexp ");
  436. break;
  437. case XML_FROM_MODULE:
  438. channel(data, "module ");
  439. break;
  440. case XML_FROM_SCHEMASV:
  441. channel(data, "Schemas validity ");
  442. break;
  443. case XML_FROM_SCHEMASP:
  444. channel(data, "Schemas parser ");
  445. break;
  446. case XML_FROM_RELAXNGP:
  447. channel(data, "Relax-NG parser ");
  448. break;
  449. case XML_FROM_RELAXNGV:
  450. channel(data, "Relax-NG validity ");
  451. break;
  452. case XML_FROM_CATALOG:
  453. channel(data, "Catalog ");
  454. break;
  455. case XML_FROM_C14N:
  456. channel(data, "C14N ");
  457. break;
  458. case XML_FROM_XSLT:
  459. channel(data, "XSLT ");
  460. break;
  461. default:
  462. break;
  463. }
  464. if (code == XML_ERR_OK)
  465. return;
  466. switch (level) {
  467. case XML_ERR_NONE:
  468. channel(data, ": ");
  469. break;
  470. case XML_ERR_WARNING:
  471. channel(data, "warning : ");
  472. break;
  473. case XML_ERR_ERROR:
  474. channel(data, "error : ");
  475. break;
  476. case XML_ERR_FATAL:
  477. channel(data, "error : ");
  478. break;
  479. }
  480. if (code == XML_ERR_OK)
  481. return;
  482. if (str != NULL) {
  483. int len;
  484. len = xmlStrlen((const xmlChar *)str);
  485. if ((len > 0) && (str[len - 1] != '\n'))
  486. channel(data, "%s\n", str);
  487. else
  488. channel(data, "%s", str);
  489. } else {
  490. channel(data, "%s\n", "out of memory error");
  491. }
  492. if (code == XML_ERR_OK)
  493. return;
  494. if (ctxt != NULL) {
  495. xmlParserPrintFileContextInternal(input, channel, data);
  496. if (cur != NULL) {
  497. if (cur->filename)
  498. channel(data, "%s:%d: \n", cur->filename, cur->line);
  499. else if ((line != 0) && (domain == XML_FROM_PARSER))
  500. channel(data, "Entity: line %d: \n", cur->line);
  501. xmlParserPrintFileContextInternal(cur, channel, data);
  502. }
  503. }
  504. if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
  505. (err->int1 < 100) &&
  506. (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
  507. xmlChar buf[150];
  508. int i;
  509. channel(data, "%s\n", err->str1);
  510. for (i=0;i < err->int1;i++)
  511. buf[i] = ' ';
  512. buf[i++] = '^';
  513. buf[i] = 0;
  514. channel(data, "%s\n", buf);
  515. }
  516. }
  517. static void
  518. initializeLibxml2(void) {
  519. xmlGetWarningsDefaultValue = 0;
  520. xmlPedanticParserDefault(0);
  521. xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
  522. xmlInitParser();
  523. xmlSetExternalEntityLoader(testExternalEntityLoader);
  524. xmlSetStructuredErrorFunc(NULL, testStructuredErrorHandler);
  525. /*
  526. * register the new I/O handlers
  527. */
  528. if (xmlRegisterInputCallbacks(hugeMatch, hugeOpen,
  529. hugeRead, hugeClose) < 0) {
  530. fprintf(stderr, "failed to register Huge handler\n");
  531. exit(1);
  532. }
  533. }
  534. /************************************************************************
  535. * *
  536. * File name and path utilities *
  537. * *
  538. ************************************************************************/
  539. static const char *baseFilename(const char *filename) {
  540. const char *cur;
  541. if (filename == NULL)
  542. return(NULL);
  543. cur = &filename[strlen(filename)];
  544. while ((cur > filename) && (*cur != '/'))
  545. cur--;
  546. if (*cur == '/')
  547. return(cur + 1);
  548. return(cur);
  549. }
  550. static char *resultFilename(const char *filename, const char *out,
  551. const char *suffix) {
  552. const char *base;
  553. char res[500];
  554. char suffixbuff[500];
  555. /*************
  556. if ((filename[0] == 't') && (filename[1] == 'e') &&
  557. (filename[2] == 's') && (filename[3] == 't') &&
  558. (filename[4] == '/'))
  559. filename = &filename[5];
  560. *************/
  561. base = baseFilename(filename);
  562. if (suffix == NULL)
  563. suffix = ".tmp";
  564. if (out == NULL)
  565. out = "";
  566. strncpy(suffixbuff,suffix,499);
  567. #ifdef VMS
  568. if(strstr(base,".") && suffixbuff[0]=='.')
  569. suffixbuff[0]='_';
  570. #endif
  571. if (snprintf(res, 499, "%s%s%s", out, base, suffixbuff) >= 499)
  572. res[499] = 0;
  573. return(strdup(res));
  574. }
  575. static int checkTestFile(const char *filename) {
  576. struct stat buf;
  577. if (stat(filename, &buf) == -1)
  578. return(0);
  579. #if defined(_WIN32)
  580. if (!(buf.st_mode & _S_IFREG))
  581. return(0);
  582. #else
  583. if (!S_ISREG(buf.st_mode))
  584. return(0);
  585. #endif
  586. return(1);
  587. }
  588. /************************************************************************
  589. * *
  590. * Test to detect or not recursive entities *
  591. * *
  592. ************************************************************************/
  593. /**
  594. * recursiveDetectTest:
  595. * @filename: the file to parse
  596. * @result: the file with expected result
  597. * @err: the file with error messages: unused
  598. *
  599. * Parse a file loading DTD and replacing entities check it fails for
  600. * lol cases
  601. *
  602. * Returns 0 in case of success, an error code otherwise
  603. */
  604. static int
  605. recursiveDetectTest(const char *filename,
  606. const char *result ATTRIBUTE_UNUSED,
  607. const char *err ATTRIBUTE_UNUSED,
  608. int options ATTRIBUTE_UNUSED) {
  609. xmlDocPtr doc;
  610. xmlParserCtxtPtr ctxt;
  611. int res = 0;
  612. nb_tests++;
  613. ctxt = xmlNewParserCtxt();
  614. /*
  615. * base of the test, parse with the old API
  616. */
  617. doc = xmlCtxtReadFile(ctxt, filename, NULL,
  618. XML_PARSE_NOENT | XML_PARSE_DTDLOAD);
  619. if ((doc != NULL) || (ctxt->lastError.code != XML_ERR_ENTITY_LOOP)) {
  620. fprintf(stderr, "Failed to detect recursion in %s\n", filename);
  621. xmlFreeParserCtxt(ctxt);
  622. xmlFreeDoc(doc);
  623. return(1);
  624. }
  625. xmlFreeParserCtxt(ctxt);
  626. return(res);
  627. }
  628. /**
  629. * notRecursiveDetectTest:
  630. * @filename: the file to parse
  631. * @result: the file with expected result
  632. * @err: the file with error messages: unused
  633. *
  634. * Parse a file loading DTD and replacing entities check it works for
  635. * good cases
  636. *
  637. * Returns 0 in case of success, an error code otherwise
  638. */
  639. static int
  640. notRecursiveDetectTest(const char *filename,
  641. const char *result ATTRIBUTE_UNUSED,
  642. const char *err ATTRIBUTE_UNUSED,
  643. int options ATTRIBUTE_UNUSED) {
  644. xmlDocPtr doc;
  645. xmlParserCtxtPtr ctxt;
  646. int res = 0;
  647. nb_tests++;
  648. ctxt = xmlNewParserCtxt();
  649. /*
  650. * base of the test, parse with the old API
  651. */
  652. doc = xmlCtxtReadFile(ctxt, filename, NULL,
  653. XML_PARSE_NOENT | XML_PARSE_DTDLOAD);
  654. if (doc == NULL) {
  655. fprintf(stderr, "Failed to parse correct file %s\n", filename);
  656. xmlFreeParserCtxt(ctxt);
  657. return(1);
  658. }
  659. xmlFreeDoc(doc);
  660. xmlFreeParserCtxt(ctxt);
  661. return(res);
  662. }
  663. #ifdef LIBXML_READER_ENABLED
  664. /**
  665. * notRecursiveHugeTest:
  666. * @filename: the file to parse
  667. * @result: the file with expected result
  668. * @err: the file with error messages: unused
  669. *
  670. * Parse a memory generated file
  671. * good cases
  672. *
  673. * Returns 0 in case of success, an error code otherwise
  674. */
  675. static int
  676. notRecursiveHugeTest(const char *filename ATTRIBUTE_UNUSED,
  677. const char *result ATTRIBUTE_UNUSED,
  678. const char *err ATTRIBUTE_UNUSED,
  679. int options ATTRIBUTE_UNUSED) {
  680. xmlTextReaderPtr reader;
  681. int res = 0;
  682. int ret;
  683. nb_tests++;
  684. reader = xmlReaderForFile("huge:test" , NULL,
  685. XML_PARSE_NOENT | XML_PARSE_DTDLOAD);
  686. if (reader == NULL) {
  687. fprintf(stderr, "Failed to open huge:test\n");
  688. return(1);
  689. }
  690. ret = xmlTextReaderRead(reader);
  691. while (ret == 1) {
  692. ret = xmlTextReaderRead(reader);
  693. }
  694. if (ret != 0) {
  695. fprintf(stderr, "Failed to parser huge:test with entities\n");
  696. res = 1;
  697. }
  698. xmlFreeTextReader(reader);
  699. return(res);
  700. }
  701. #endif
  702. /************************************************************************
  703. * *
  704. * Tests Descriptions *
  705. * *
  706. ************************************************************************/
  707. static
  708. testDesc testDescriptions[] = {
  709. { "Parsing recursive test cases" ,
  710. recursiveDetectTest, "./test/recurse/lol*.xml", NULL, NULL, NULL,
  711. 0 },
  712. { "Parsing non-recursive test cases" ,
  713. notRecursiveDetectTest, "./test/recurse/good*.xml", NULL, NULL, NULL,
  714. 0 },
  715. #ifdef LIBXML_READER_ENABLED
  716. { "Parsing non-recursive huge case" ,
  717. notRecursiveHugeTest, NULL, NULL, NULL, NULL,
  718. 0 },
  719. #endif
  720. {NULL, NULL, NULL, NULL, NULL, NULL, 0}
  721. };
  722. /************************************************************************
  723. * *
  724. * The main code driving the tests *
  725. * *
  726. ************************************************************************/
  727. static int
  728. launchTests(testDescPtr tst) {
  729. int res = 0, err = 0;
  730. size_t i;
  731. char *result;
  732. char *error;
  733. int mem;
  734. if (tst == NULL) return(-1);
  735. if (tst->in != NULL) {
  736. glob_t globbuf;
  737. globbuf.gl_offs = 0;
  738. glob(tst->in, GLOB_DOOFFS, NULL, &globbuf);
  739. for (i = 0;i < globbuf.gl_pathc;i++) {
  740. if (!checkTestFile(globbuf.gl_pathv[i]))
  741. continue;
  742. if (tst->suffix != NULL) {
  743. result = resultFilename(globbuf.gl_pathv[i], tst->out,
  744. tst->suffix);
  745. if (result == NULL) {
  746. fprintf(stderr, "Out of memory !\n");
  747. fatalError();
  748. }
  749. } else {
  750. result = NULL;
  751. }
  752. if (tst->err != NULL) {
  753. error = resultFilename(globbuf.gl_pathv[i], tst->out,
  754. tst->err);
  755. if (error == NULL) {
  756. fprintf(stderr, "Out of memory !\n");
  757. fatalError();
  758. }
  759. } else {
  760. error = NULL;
  761. }
  762. if ((result) &&(!checkTestFile(result))) {
  763. fprintf(stderr, "Missing result file %s\n", result);
  764. } else if ((error) &&(!checkTestFile(error))) {
  765. fprintf(stderr, "Missing error file %s\n", error);
  766. } else {
  767. mem = xmlMemUsed();
  768. extraMemoryFromResolver = 0;
  769. testErrorsSize = 0;
  770. testErrors[0] = 0;
  771. res = tst->func(globbuf.gl_pathv[i], result, error,
  772. tst->options | XML_PARSE_COMPACT);
  773. xmlResetLastError();
  774. if (res != 0) {
  775. fprintf(stderr, "File %s generated an error\n",
  776. globbuf.gl_pathv[i]);
  777. nb_errors++;
  778. err++;
  779. }
  780. else if (xmlMemUsed() != mem) {
  781. if ((xmlMemUsed() != mem) &&
  782. (extraMemoryFromResolver == 0)) {
  783. fprintf(stderr, "File %s leaked %d bytes\n",
  784. globbuf.gl_pathv[i], xmlMemUsed() - mem);
  785. nb_leaks++;
  786. err++;
  787. }
  788. }
  789. testErrorsSize = 0;
  790. }
  791. if (result)
  792. free(result);
  793. if (error)
  794. free(error);
  795. }
  796. globfree(&globbuf);
  797. } else {
  798. testErrorsSize = 0;
  799. testErrors[0] = 0;
  800. extraMemoryFromResolver = 0;
  801. res = tst->func(NULL, NULL, NULL, tst->options);
  802. if (res != 0) {
  803. nb_errors++;
  804. err++;
  805. }
  806. }
  807. return(err);
  808. }
  809. static int verbose = 0;
  810. static int tests_quiet = 0;
  811. static int
  812. runtest(int i) {
  813. int ret = 0, res;
  814. int old_errors, old_tests, old_leaks;
  815. old_errors = nb_errors;
  816. old_tests = nb_tests;
  817. old_leaks = nb_leaks;
  818. if ((tests_quiet == 0) && (testDescriptions[i].desc != NULL))
  819. printf("## %s\n", testDescriptions[i].desc);
  820. res = launchTests(&testDescriptions[i]);
  821. if (res != 0)
  822. ret++;
  823. if (verbose) {
  824. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  825. printf("Ran %d tests, no errors\n", nb_tests - old_tests);
  826. else
  827. printf("Ran %d tests, %d errors, %d leaks\n",
  828. nb_tests - old_tests,
  829. nb_errors - old_errors,
  830. nb_leaks - old_leaks);
  831. }
  832. return(ret);
  833. }
  834. int
  835. main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  836. int i, a, ret = 0;
  837. int subset = 0;
  838. initializeLibxml2();
  839. for (a = 1; a < argc;a++) {
  840. if (!strcmp(argv[a], "-v"))
  841. verbose = 1;
  842. else if (!strcmp(argv[a], "-quiet"))
  843. tests_quiet = 1;
  844. else {
  845. for (i = 0; testDescriptions[i].func != NULL; i++) {
  846. if (strstr(testDescriptions[i].desc, argv[a])) {
  847. ret += runtest(i);
  848. subset++;
  849. }
  850. }
  851. }
  852. }
  853. if (subset == 0) {
  854. for (i = 0; testDescriptions[i].func != NULL; i++) {
  855. ret += runtest(i);
  856. }
  857. }
  858. if ((nb_errors == 0) && (nb_leaks == 0)) {
  859. ret = 0;
  860. printf("Total %d tests, no errors\n",
  861. nb_tests);
  862. } else {
  863. ret = 1;
  864. printf("Total %d tests, %d errors, %d leaks\n",
  865. nb_tests, nb_errors, nb_leaks);
  866. }
  867. xmlCleanupParser();
  868. xmlMemoryDump();
  869. return(ret);
  870. }