testrecurse.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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 "config.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/stat.h>
  17. #include <libxml/parser.h>
  18. #include <libxml/parserInternals.h>
  19. #include <libxml/tree.h>
  20. #include <libxml/uri.h>
  21. /*
  22. * O_BINARY is just for Windows compatibility - if it isn't defined
  23. * on this system, avoid any compilation error
  24. */
  25. #ifdef O_BINARY
  26. #define RD_FLAGS O_RDONLY | O_BINARY
  27. #else
  28. #define RD_FLAGS O_RDONLY
  29. #endif
  30. #define OPT_SAX (1<<0)
  31. #define OPT_NO_SUBST (1<<1)
  32. typedef int (*functest) (const char *filename, const char *result,
  33. const char *error, int options);
  34. typedef struct testDesc testDesc;
  35. typedef testDesc *testDescPtr;
  36. struct testDesc {
  37. const char *desc; /* description of the test */
  38. functest func; /* function implementing the test */
  39. const char *in; /* glob to path for input files */
  40. const char *out; /* output directory */
  41. const char *suffix;/* suffix for output files */
  42. const char *err; /* suffix for error output files */
  43. int options; /* parser options for the test */
  44. };
  45. static int checkTestFile(const char *filename);
  46. #if defined(_WIN32)
  47. #include <windows.h>
  48. typedef struct
  49. {
  50. size_t gl_pathc; /* Count of paths matched so far */
  51. char **gl_pathv; /* List of matched pathnames. */
  52. size_t gl_offs; /* Slots to reserve in 'gl_pathv'. */
  53. } glob_t;
  54. #define GLOB_DOOFFS 0
  55. static int glob(const char *pattern, ATTRIBUTE_UNUSED int flags,
  56. ATTRIBUTE_UNUSED int errfunc(const char *epath, int eerrno),
  57. glob_t *pglob) {
  58. glob_t *ret;
  59. WIN32_FIND_DATA FindFileData;
  60. HANDLE hFind;
  61. unsigned int nb_paths = 0;
  62. char directory[500];
  63. int len;
  64. if ((pattern == NULL) || (pglob == NULL)) return(-1);
  65. strncpy(directory, pattern, 499);
  66. for (len = strlen(directory);len >= 0;len--) {
  67. if (directory[len] == '/') {
  68. len++;
  69. directory[len] = 0;
  70. break;
  71. }
  72. }
  73. if (len <= 0)
  74. len = 0;
  75. ret = pglob;
  76. memset(ret, 0, sizeof(glob_t));
  77. hFind = FindFirstFileA(pattern, &FindFileData);
  78. if (hFind == INVALID_HANDLE_VALUE)
  79. return(0);
  80. nb_paths = 20;
  81. ret->gl_pathv = (char **) malloc(nb_paths * sizeof(char *));
  82. if (ret->gl_pathv == NULL) {
  83. FindClose(hFind);
  84. return(-1);
  85. }
  86. strncpy(directory + len, FindFileData.cFileName, 499 - len);
  87. ret->gl_pathv[ret->gl_pathc] = strdup(directory);
  88. if (ret->gl_pathv[ret->gl_pathc] == NULL)
  89. goto done;
  90. ret->gl_pathc++;
  91. while(FindNextFileA(hFind, &FindFileData)) {
  92. if (FindFileData.cFileName[0] == '.')
  93. continue;
  94. if (ret->gl_pathc + 2 > nb_paths) {
  95. char **tmp = realloc(ret->gl_pathv, nb_paths * 2 * sizeof(char *));
  96. if (tmp == NULL)
  97. break;
  98. ret->gl_pathv = tmp;
  99. nb_paths *= 2;
  100. }
  101. strncpy(directory + len, FindFileData.cFileName, 499 - len);
  102. ret->gl_pathv[ret->gl_pathc] = strdup(directory);
  103. if (ret->gl_pathv[ret->gl_pathc] == NULL)
  104. break;
  105. ret->gl_pathc++;
  106. }
  107. ret->gl_pathv[ret->gl_pathc] = NULL;
  108. done:
  109. FindClose(hFind);
  110. return(0);
  111. }
  112. static void globfree(glob_t *pglob) {
  113. unsigned int i;
  114. if (pglob == NULL)
  115. return;
  116. for (i = 0;i < pglob->gl_pathc;i++) {
  117. if (pglob->gl_pathv[i] != NULL)
  118. free(pglob->gl_pathv[i]);
  119. }
  120. }
  121. #else
  122. #include <glob.h>
  123. #endif
  124. /************************************************************************
  125. * *
  126. * Huge document generator *
  127. * *
  128. ************************************************************************/
  129. #include <libxml/xmlIO.h>
  130. typedef struct {
  131. const char *URL;
  132. const char *start;
  133. const char *segment;
  134. const char *finish;
  135. } xmlHugeDocParts;
  136. static const xmlHugeDocParts hugeDocTable[] = {
  137. {
  138. "test/recurse/huge.xml",
  139. "<!DOCTYPE foo ["
  140. "<!ELEMENT foo (bar*)> "
  141. "<!ELEMENT bar (#PCDATA)> "
  142. "<!ATTLIST bar attr CDATA #IMPLIED> "
  143. "<!ENTITY a SYSTEM 'ga.ent'> "
  144. "<!ENTITY b SYSTEM 'gb.ent'> "
  145. "<!ENTITY c SYSTEM 'gc.ent'> "
  146. "<!ENTITY f 'some internal data'> "
  147. "<!ENTITY e '&f;&f;'> "
  148. "<!ENTITY d '&e;&e;'> "
  149. "]> "
  150. "<foo>",
  151. " <bar attr='&e; &f; &d;'>&a; &b; &c; &e; &f; &d;</bar>\n"
  152. " <bar>_123456789_123456789_123456789_123456789</bar>\n"
  153. " <bar>_123456789_123456789_123456789_123456789</bar>\n"
  154. " <bar>_123456789_123456789_123456789_123456789</bar>\n"
  155. " <bar>_123456789_123456789_123456789_123456789</bar>\n",
  156. "</foo>"
  157. },
  158. {
  159. "test/recurse/huge_dtd.dtd",
  160. "<!ELEMENT foo (#PCDATA)>\n"
  161. "<!ENTITY ent 'success'>\n"
  162. "<!ENTITY % a SYSTEM 'pa.ent'>\n"
  163. "<!ENTITY % b SYSTEM 'pb.ent'>\n"
  164. "<!ENTITY % c SYSTEM 'pc.ent'>\n"
  165. "<!ENTITY % d '<!-- comment -->'>\n"
  166. "<!ENTITY % e '%d;%d;'>\n"
  167. "<!ENTITY % f '%e;%e;'>\n",
  168. "<!ENTITY ent '%a; %b; %c; %d; %e; %f;'>\n"
  169. "%a; %b; %c; %d; %e; %f;\n"
  170. "<!-- _123456789_123456789_123456789_123456789 -->\n"
  171. "<!-- _123456789_123456789_123456789_123456789 -->\n"
  172. "<!-- _123456789_123456789_123456789_123456789 -->\n",
  173. ""
  174. },
  175. { NULL, NULL, NULL, NULL }
  176. };
  177. static const xmlHugeDocParts *hugeDocParts;
  178. static int curseg = 0;
  179. static const char *current;
  180. static int rlen;
  181. /**
  182. * hugeMatch:
  183. * @URI: an URI to test
  184. *
  185. * Check for a huge query
  186. *
  187. * Returns 1 if yes and 0 if another Input module should be used
  188. */
  189. static int
  190. hugeMatch(const char * URI) {
  191. int i;
  192. if (URI == NULL)
  193. return(0);
  194. for (i = 0; hugeDocTable[i].URL; i++) {
  195. if (strcmp(URI, hugeDocTable[i].URL) == 0)
  196. return(1);
  197. }
  198. return(0);
  199. }
  200. /**
  201. * hugeOpen:
  202. * @URI: an URI to test
  203. *
  204. * Return a pointer to the huge query handler, in this example simply
  205. * the current pointer...
  206. *
  207. * Returns an Input context or NULL in case or error
  208. */
  209. static void *
  210. hugeOpen(const char * URI) {
  211. int i;
  212. if (URI == NULL)
  213. return(NULL);
  214. for (i = 0; hugeDocTable[i].URL; i++) {
  215. if (strcmp(URI, hugeDocTable[i].URL) == 0) {
  216. hugeDocParts = hugeDocTable + i;
  217. curseg = 0;
  218. current = hugeDocParts->start;
  219. rlen = strlen(current);
  220. return((void *) current);
  221. }
  222. }
  223. return(NULL);
  224. }
  225. /**
  226. * hugeClose:
  227. * @context: the read context
  228. *
  229. * Close the huge query handler
  230. *
  231. * Returns 0 or -1 in case of error
  232. */
  233. static int
  234. hugeClose(void * context) {
  235. if (context == NULL) return(-1);
  236. return(0);
  237. }
  238. #define MAX_NODES 1000
  239. /**
  240. * hugeRead:
  241. * @context: the read context
  242. * @buffer: where to store data
  243. * @len: number of bytes to read
  244. *
  245. * Implement an huge query read.
  246. *
  247. * Returns the number of bytes read or -1 in case of error
  248. */
  249. static int
  250. hugeRead(void *context, char *buffer, int len)
  251. {
  252. if ((context == NULL) || (buffer == NULL) || (len < 0))
  253. return (-1);
  254. if (len >= rlen) {
  255. if (curseg >= MAX_NODES + 1) {
  256. rlen = 0;
  257. return(0);
  258. }
  259. len = rlen;
  260. rlen = 0;
  261. memcpy(buffer, current, len);
  262. curseg ++;
  263. if (curseg == MAX_NODES) {
  264. current = hugeDocParts->finish;
  265. } else {
  266. current = hugeDocParts->segment;
  267. }
  268. rlen = strlen(current);
  269. } else {
  270. memcpy(buffer, current, len);
  271. rlen -= len;
  272. current += len;
  273. }
  274. return (len);
  275. }
  276. /************************************************************************
  277. * *
  278. * Libxml2 specific routines *
  279. * *
  280. ************************************************************************/
  281. static int nb_tests = 0;
  282. static int nb_errors = 0;
  283. static int nb_leaks = 0;
  284. static int extraMemoryFromResolver = 0;
  285. static int
  286. fatalError(void) {
  287. fprintf(stderr, "Exitting tests on fatal error\n");
  288. exit(1);
  289. }
  290. /*
  291. * We need to trap calls to the resolver to not account memory for the catalog
  292. * which is shared to the current running test. We also don't want to have
  293. * network downloads modifying tests.
  294. */
  295. static xmlParserInputPtr
  296. testExternalEntityLoader(const char *URL, const char *ID,
  297. xmlParserCtxtPtr ctxt) {
  298. xmlParserInputPtr ret;
  299. if (checkTestFile(URL)) {
  300. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  301. } else {
  302. int memused = xmlMemUsed();
  303. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  304. extraMemoryFromResolver += xmlMemUsed() - memused;
  305. }
  306. return(ret);
  307. }
  308. /*
  309. * Trapping the error messages at the generic level to grab the equivalent of
  310. * stderr messages on CLI tools.
  311. */
  312. static char testErrors[32769];
  313. static int testErrorsSize = 0;
  314. static void
  315. channel(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
  316. va_list args;
  317. int res;
  318. if (testErrorsSize >= 32768)
  319. return;
  320. va_start(args, msg);
  321. res = vsnprintf(&testErrors[testErrorsSize],
  322. 32768 - testErrorsSize,
  323. msg, args);
  324. va_end(args);
  325. if (testErrorsSize + res >= 32768) {
  326. /* buffer is full */
  327. testErrorsSize = 32768;
  328. testErrors[testErrorsSize] = 0;
  329. } else {
  330. testErrorsSize += res;
  331. }
  332. testErrors[testErrorsSize] = 0;
  333. }
  334. /**
  335. * xmlParserPrintFileContext:
  336. * @input: an xmlParserInputPtr input
  337. *
  338. * Displays current context within the input content for error tracking
  339. */
  340. static void
  341. xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
  342. xmlGenericErrorFunc chanl, void *data ) {
  343. const xmlChar *cur, *base;
  344. unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
  345. xmlChar content[81]; /* space for 80 chars + line terminator */
  346. xmlChar *ctnt;
  347. if (input == NULL) return;
  348. cur = input->cur;
  349. base = input->base;
  350. /* skip backwards over any end-of-lines */
  351. while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
  352. cur--;
  353. }
  354. n = 0;
  355. /* search backwards for beginning-of-line (to max buff size) */
  356. while ((n++ < (sizeof(content)-1)) && (cur > base) &&
  357. (*(cur) != '\n') && (*(cur) != '\r'))
  358. cur--;
  359. if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
  360. /* calculate the error position in terms of the current position */
  361. col = input->cur - cur;
  362. /* search forward for end-of-line (to max buff size) */
  363. n = 0;
  364. ctnt = content;
  365. /* copy selected text to our buffer */
  366. while ((*cur != 0) && (*(cur) != '\n') &&
  367. (*(cur) != '\r') && (n < sizeof(content)-1)) {
  368. *ctnt++ = *cur++;
  369. n++;
  370. }
  371. *ctnt = 0;
  372. /* print out the selected text */
  373. chanl(data ,"%s\n", content);
  374. /* create blank line with problem pointer */
  375. n = 0;
  376. ctnt = content;
  377. /* (leave buffer space for pointer + line terminator) */
  378. while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
  379. if (*(ctnt) != '\t')
  380. *(ctnt) = ' ';
  381. ctnt++;
  382. }
  383. *ctnt++ = '^';
  384. *ctnt = 0;
  385. chanl(data ,"%s\n", content);
  386. }
  387. static void
  388. testStructuredErrorHandler(void *ctx ATTRIBUTE_UNUSED, const xmlError *err) {
  389. char *file = NULL;
  390. int line = 0;
  391. int code = -1;
  392. int domain;
  393. void *data = NULL;
  394. const char *str;
  395. const xmlChar *name = NULL;
  396. xmlNodePtr node;
  397. xmlErrorLevel level;
  398. xmlParserInputPtr input = NULL;
  399. xmlParserInputPtr cur = NULL;
  400. xmlParserCtxtPtr ctxt = NULL;
  401. if (err == NULL)
  402. return;
  403. file = err->file;
  404. line = err->line;
  405. code = err->code;
  406. domain = err->domain;
  407. level = err->level;
  408. node = err->node;
  409. if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
  410. (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
  411. (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
  412. ctxt = err->ctxt;
  413. }
  414. str = err->message;
  415. if (code == XML_ERR_OK)
  416. return;
  417. if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
  418. name = node->name;
  419. /*
  420. * Maintain the compatibility with the legacy error handling
  421. */
  422. if (ctxt != NULL) {
  423. input = ctxt->input;
  424. if ((input != NULL) && (input->filename == NULL) &&
  425. (ctxt->inputNr > 1)) {
  426. cur = input;
  427. input = ctxt->inputTab[ctxt->inputNr - 2];
  428. }
  429. if (input != NULL) {
  430. if (input->filename)
  431. channel(data, "%s:%d: ", input->filename, input->line);
  432. else if ((line != 0) && (domain == XML_FROM_PARSER))
  433. channel(data, "Entity: line %d: ", input->line);
  434. }
  435. } else {
  436. if (file != NULL)
  437. channel(data, "%s:%d: ", file, line);
  438. else if ((line != 0) && (domain == XML_FROM_PARSER))
  439. channel(data, "Entity: line %d: ", line);
  440. }
  441. if (name != NULL) {
  442. channel(data, "element %s: ", name);
  443. }
  444. if (code == XML_ERR_OK)
  445. return;
  446. switch (domain) {
  447. case XML_FROM_PARSER:
  448. channel(data, "parser ");
  449. break;
  450. case XML_FROM_NAMESPACE:
  451. channel(data, "namespace ");
  452. break;
  453. case XML_FROM_DTD:
  454. case XML_FROM_VALID:
  455. channel(data, "validity ");
  456. break;
  457. case XML_FROM_HTML:
  458. channel(data, "HTML parser ");
  459. break;
  460. case XML_FROM_MEMORY:
  461. channel(data, "memory ");
  462. break;
  463. case XML_FROM_OUTPUT:
  464. channel(data, "output ");
  465. break;
  466. case XML_FROM_IO:
  467. channel(data, "I/O ");
  468. break;
  469. case XML_FROM_XINCLUDE:
  470. channel(data, "XInclude ");
  471. break;
  472. case XML_FROM_XPATH:
  473. channel(data, "XPath ");
  474. break;
  475. case XML_FROM_XPOINTER:
  476. channel(data, "parser ");
  477. break;
  478. case XML_FROM_REGEXP:
  479. channel(data, "regexp ");
  480. break;
  481. case XML_FROM_MODULE:
  482. channel(data, "module ");
  483. break;
  484. case XML_FROM_SCHEMASV:
  485. channel(data, "Schemas validity ");
  486. break;
  487. case XML_FROM_SCHEMASP:
  488. channel(data, "Schemas parser ");
  489. break;
  490. case XML_FROM_RELAXNGP:
  491. channel(data, "Relax-NG parser ");
  492. break;
  493. case XML_FROM_RELAXNGV:
  494. channel(data, "Relax-NG validity ");
  495. break;
  496. case XML_FROM_CATALOG:
  497. channel(data, "Catalog ");
  498. break;
  499. case XML_FROM_C14N:
  500. channel(data, "C14N ");
  501. break;
  502. case XML_FROM_XSLT:
  503. channel(data, "XSLT ");
  504. break;
  505. default:
  506. break;
  507. }
  508. if (code == XML_ERR_OK)
  509. return;
  510. switch (level) {
  511. case XML_ERR_NONE:
  512. channel(data, ": ");
  513. break;
  514. case XML_ERR_WARNING:
  515. channel(data, "warning : ");
  516. break;
  517. case XML_ERR_ERROR:
  518. channel(data, "error : ");
  519. break;
  520. case XML_ERR_FATAL:
  521. channel(data, "error : ");
  522. break;
  523. }
  524. if (code == XML_ERR_OK)
  525. return;
  526. if (str != NULL) {
  527. int len;
  528. len = xmlStrlen((const xmlChar *)str);
  529. if ((len > 0) && (str[len - 1] != '\n'))
  530. channel(data, "%s\n", str);
  531. else
  532. channel(data, "%s", str);
  533. } else {
  534. channel(data, "%s\n", "out of memory error");
  535. }
  536. if (code == XML_ERR_OK)
  537. return;
  538. if (ctxt != NULL) {
  539. xmlParserPrintFileContextInternal(input, channel, data);
  540. if (cur != NULL) {
  541. if (cur->filename)
  542. channel(data, "%s:%d: \n", cur->filename, cur->line);
  543. else if ((line != 0) && (domain == XML_FROM_PARSER))
  544. channel(data, "Entity: line %d: \n", cur->line);
  545. xmlParserPrintFileContextInternal(cur, channel, data);
  546. }
  547. }
  548. if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
  549. (err->int1 < 100) &&
  550. (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
  551. xmlChar buf[150];
  552. int i;
  553. channel(data, "%s\n", err->str1);
  554. for (i=0;i < err->int1;i++)
  555. buf[i] = ' ';
  556. buf[i++] = '^';
  557. buf[i] = 0;
  558. channel(data, "%s\n", buf);
  559. }
  560. }
  561. static void
  562. initializeLibxml2(void) {
  563. xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
  564. xmlInitParser();
  565. xmlSetExternalEntityLoader(testExternalEntityLoader);
  566. xmlSetStructuredErrorFunc(NULL, testStructuredErrorHandler);
  567. /*
  568. * register the new I/O handlers
  569. */
  570. if (xmlRegisterInputCallbacks(hugeMatch, hugeOpen,
  571. hugeRead, hugeClose) < 0) {
  572. fprintf(stderr, "failed to register Huge handler\n");
  573. exit(1);
  574. }
  575. }
  576. static void
  577. initSAX(xmlParserCtxtPtr ctxt) {
  578. ctxt->sax->startElementNs = NULL;
  579. ctxt->sax->endElementNs = NULL;
  580. ctxt->sax->startElement = NULL;
  581. ctxt->sax->endElement = NULL;
  582. ctxt->sax->characters = NULL;
  583. ctxt->sax->cdataBlock = NULL;
  584. ctxt->sax->ignorableWhitespace = NULL;
  585. ctxt->sax->processingInstruction = NULL;
  586. ctxt->sax->comment = NULL;
  587. }
  588. /************************************************************************
  589. * *
  590. * File name and path utilities *
  591. * *
  592. ************************************************************************/
  593. static const char *baseFilename(const char *filename) {
  594. const char *cur;
  595. if (filename == NULL)
  596. return(NULL);
  597. cur = &filename[strlen(filename)];
  598. while ((cur > filename) && (*cur != '/'))
  599. cur--;
  600. if (*cur == '/')
  601. return(cur + 1);
  602. return(cur);
  603. }
  604. static char *resultFilename(const char *filename, const char *out,
  605. const char *suffix) {
  606. const char *base;
  607. char res[500];
  608. char suffixbuff[500];
  609. /*************
  610. if ((filename[0] == 't') && (filename[1] == 'e') &&
  611. (filename[2] == 's') && (filename[3] == 't') &&
  612. (filename[4] == '/'))
  613. filename = &filename[5];
  614. *************/
  615. base = baseFilename(filename);
  616. if (suffix == NULL)
  617. suffix = ".tmp";
  618. if (out == NULL)
  619. out = "";
  620. strncpy(suffixbuff,suffix,499);
  621. #ifdef VMS
  622. if(strstr(base,".") && suffixbuff[0]=='.')
  623. suffixbuff[0]='_';
  624. #endif
  625. if (snprintf(res, 499, "%s%s%s", out, base, suffixbuff) >= 499)
  626. res[499] = 0;
  627. return(strdup(res));
  628. }
  629. static int checkTestFile(const char *filename) {
  630. struct stat buf;
  631. if (stat(filename, &buf) == -1)
  632. return(0);
  633. #if defined(_WIN32)
  634. if (!(buf.st_mode & _S_IFREG))
  635. return(0);
  636. #else
  637. if (!S_ISREG(buf.st_mode))
  638. return(0);
  639. #endif
  640. return(1);
  641. }
  642. /************************************************************************
  643. * *
  644. * Test to detect or not recursive entities *
  645. * *
  646. ************************************************************************/
  647. /**
  648. * recursiveDetectTest:
  649. * @filename: the file to parse
  650. * @result: the file with expected result
  651. * @err: the file with error messages: unused
  652. *
  653. * Parse a file loading DTD and replacing entities check it fails for
  654. * lol cases
  655. *
  656. * Returns 0 in case of success, an error code otherwise
  657. */
  658. static int
  659. recursiveDetectTest(const char *filename,
  660. const char *result ATTRIBUTE_UNUSED,
  661. const char *err ATTRIBUTE_UNUSED,
  662. int options) {
  663. xmlDocPtr doc;
  664. xmlParserCtxtPtr ctxt;
  665. int res = 0;
  666. /*
  667. * XML_PARSE_DTDVALID is the only way to load external entities
  668. * without XML_PARSE_NOENT. The validation result doesn't matter
  669. * anyway.
  670. */
  671. int parserOptions = XML_PARSE_DTDVALID;
  672. nb_tests++;
  673. ctxt = xmlNewParserCtxt();
  674. if (options & OPT_SAX)
  675. initSAX(ctxt);
  676. if ((options & OPT_NO_SUBST) == 0)
  677. parserOptions |= XML_PARSE_NOENT;
  678. /*
  679. * base of the test, parse with the old API
  680. */
  681. doc = xmlCtxtReadFile(ctxt, filename, NULL, parserOptions);
  682. if ((doc != NULL) || (ctxt->lastError.code != XML_ERR_ENTITY_LOOP)) {
  683. fprintf(stderr, "Failed to detect recursion in %s\n", filename);
  684. xmlFreeParserCtxt(ctxt);
  685. xmlFreeDoc(doc);
  686. return(1);
  687. }
  688. xmlFreeParserCtxt(ctxt);
  689. return(res);
  690. }
  691. /**
  692. * notRecursiveDetectTest:
  693. * @filename: the file to parse
  694. * @result: the file with expected result
  695. * @err: the file with error messages: unused
  696. *
  697. * Parse a file loading DTD and replacing entities check it works for
  698. * good cases
  699. *
  700. * Returns 0 in case of success, an error code otherwise
  701. */
  702. static int
  703. notRecursiveDetectTest(const char *filename,
  704. const char *result ATTRIBUTE_UNUSED,
  705. const char *err ATTRIBUTE_UNUSED,
  706. int options) {
  707. xmlDocPtr doc;
  708. xmlParserCtxtPtr ctxt;
  709. int res = 0;
  710. int parserOptions = XML_PARSE_DTDLOAD;
  711. nb_tests++;
  712. ctxt = xmlNewParserCtxt();
  713. if (options & OPT_SAX)
  714. initSAX(ctxt);
  715. if ((options & OPT_NO_SUBST) == 0)
  716. parserOptions |= XML_PARSE_NOENT;
  717. /*
  718. * base of the test, parse with the old API
  719. */
  720. doc = xmlCtxtReadFile(ctxt, filename, NULL, parserOptions);
  721. if (doc == NULL) {
  722. fprintf(stderr, "Failed to parse correct file %s\n", filename);
  723. xmlFreeParserCtxt(ctxt);
  724. return(1);
  725. }
  726. xmlFreeDoc(doc);
  727. xmlFreeParserCtxt(ctxt);
  728. return(res);
  729. }
  730. /**
  731. * notRecursiveHugeTest:
  732. * @filename: the file to parse
  733. * @result: the file with expected result
  734. * @err: the file with error messages: unused
  735. *
  736. * Parse a memory generated file
  737. * good cases
  738. *
  739. * Returns 0 in case of success, an error code otherwise
  740. */
  741. static int
  742. notRecursiveHugeTest(const char *filename ATTRIBUTE_UNUSED,
  743. const char *result ATTRIBUTE_UNUSED,
  744. const char *err ATTRIBUTE_UNUSED,
  745. int options) {
  746. xmlParserCtxtPtr ctxt;
  747. xmlDocPtr doc;
  748. int res = 0;
  749. int parserOptions = XML_PARSE_DTDVALID;
  750. nb_tests++;
  751. ctxt = xmlNewParserCtxt();
  752. if (options & OPT_SAX)
  753. initSAX(ctxt);
  754. if ((options & OPT_NO_SUBST) == 0)
  755. parserOptions |= XML_PARSE_NOENT;
  756. doc = xmlCtxtReadFile(ctxt, "test/recurse/huge.xml", NULL, parserOptions);
  757. if (doc == NULL) {
  758. fprintf(stderr, "Failed to parse huge.xml\n");
  759. res = 1;
  760. } else {
  761. xmlEntityPtr ent;
  762. unsigned long fixed_cost = 20;
  763. unsigned long allowed_expansion = 1000000;
  764. unsigned long f_size = xmlStrlen(BAD_CAST "some internal data");
  765. unsigned long e_size;
  766. unsigned long d_size;
  767. unsigned long total_size;
  768. ent = xmlGetDocEntity(doc, BAD_CAST "e");
  769. e_size = f_size * 2 +
  770. xmlStrlen(BAD_CAST "&f;") * 2 +
  771. fixed_cost * 2;
  772. if (ent->expandedSize != e_size) {
  773. fprintf(stderr, "Wrong size for entity e: %lu (expected %lu)\n",
  774. ent->expandedSize, e_size);
  775. res = 1;
  776. }
  777. ent = xmlGetDocEntity(doc, BAD_CAST "b");
  778. if (ent->expandedSize != e_size) {
  779. fprintf(stderr, "Wrong size for entity b: %lu (expected %lu)\n",
  780. ent->expandedSize, e_size);
  781. res = 1;
  782. }
  783. ent = xmlGetDocEntity(doc, BAD_CAST "d");
  784. d_size = e_size * 2 +
  785. xmlStrlen(BAD_CAST "&e;") * 2 +
  786. fixed_cost * 2;
  787. if (ent->expandedSize != d_size) {
  788. fprintf(stderr, "Wrong size for entity d: %lu (expected %lu)\n",
  789. ent->expandedSize, d_size);
  790. res = 1;
  791. }
  792. ent = xmlGetDocEntity(doc, BAD_CAST "c");
  793. if (ent->expandedSize != d_size) {
  794. fprintf(stderr, "Wrong size for entity c: %lu (expected %lu)\n",
  795. ent->expandedSize, d_size);
  796. res = 1;
  797. }
  798. if (ctxt->sizeentcopy < allowed_expansion) {
  799. fprintf(stderr, "Total entity size too small: %lu\n",
  800. ctxt->sizeentcopy);
  801. res = 1;
  802. }
  803. total_size = (f_size + e_size + d_size + 3 * fixed_cost) *
  804. (MAX_NODES - 1) * 3;
  805. if (ctxt->sizeentcopy != total_size) {
  806. fprintf(stderr, "Wrong total entity size: %lu (expected %lu)\n",
  807. ctxt->sizeentcopy, total_size);
  808. res = 1;
  809. }
  810. if (ctxt->sizeentities != 30) {
  811. fprintf(stderr, "Wrong parsed entity size: %lu (expected %lu)\n",
  812. ctxt->sizeentities, 30lu);
  813. res = 1;
  814. }
  815. }
  816. xmlFreeDoc(doc);
  817. xmlFreeParserCtxt(ctxt);
  818. return(res);
  819. }
  820. /**
  821. * notRecursiveHugeTest:
  822. * @filename: the file to parse
  823. * @result: the file with expected result
  824. * @err: the file with error messages: unused
  825. *
  826. * Parse a memory generated file
  827. * good cases
  828. *
  829. * Returns 0 in case of success, an error code otherwise
  830. */
  831. static int
  832. hugeDtdTest(const char *filename ATTRIBUTE_UNUSED,
  833. const char *result ATTRIBUTE_UNUSED,
  834. const char *err ATTRIBUTE_UNUSED,
  835. int options) {
  836. xmlParserCtxtPtr ctxt;
  837. xmlDocPtr doc;
  838. int res = 0;
  839. int parserOptions = XML_PARSE_DTDVALID;
  840. nb_tests++;
  841. ctxt = xmlNewParserCtxt();
  842. if (options & OPT_SAX)
  843. initSAX(ctxt);
  844. if ((options & OPT_NO_SUBST) == 0)
  845. parserOptions |= XML_PARSE_NOENT;
  846. doc = xmlCtxtReadFile(ctxt, "test/recurse/huge_dtd.xml", NULL,
  847. parserOptions);
  848. if (doc == NULL) {
  849. fprintf(stderr, "Failed to parse huge_dtd.xml\n");
  850. res = 1;
  851. } else {
  852. unsigned long fixed_cost = 20;
  853. unsigned long allowed_expansion = 1000000;
  854. unsigned long a_size = xmlStrlen(BAD_CAST "<!-- comment -->");
  855. unsigned long b_size;
  856. unsigned long c_size;
  857. unsigned long e_size;
  858. unsigned long f_size;
  859. unsigned long total_size;
  860. if (ctxt->sizeentcopy < allowed_expansion) {
  861. fprintf(stderr, "Total entity size too small: %lu\n",
  862. ctxt->sizeentcopy);
  863. res = 1;
  864. }
  865. b_size = (a_size + strlen("&a;") + fixed_cost) * 2;
  866. c_size = (b_size + strlen("&b;") + fixed_cost) * 2;
  867. /*
  868. * Internal parameter entites are substitued eagerly and
  869. * need different accounting.
  870. */
  871. e_size = a_size * 2;
  872. f_size = e_size * 2;
  873. total_size = /* internal */
  874. e_size + f_size + fixed_cost * 4 +
  875. (a_size + e_size + f_size + fixed_cost * 3) *
  876. (MAX_NODES - 1) * 2 +
  877. /* external */
  878. (a_size + b_size + c_size + fixed_cost * 3) *
  879. (MAX_NODES - 1) * 2 +
  880. /* final reference in main doc */
  881. strlen("success") + fixed_cost;
  882. if (ctxt->sizeentcopy != total_size) {
  883. fprintf(stderr, "Wrong total entity size: %lu (expected %lu)\n",
  884. ctxt->sizeentcopy, total_size);
  885. res = 1;
  886. }
  887. total_size = strlen(hugeDocParts->start) +
  888. strlen(hugeDocParts->segment) * (MAX_NODES - 1) +
  889. strlen(hugeDocParts->finish) +
  890. /*
  891. * Other external entities pa.ent, pb.ent, pc.ent.
  892. * These are currently counted twice because they're
  893. * used both in DTD and EntityValue.
  894. */
  895. (16 + 6 + 6) * 2;
  896. if (ctxt->sizeentities != total_size) {
  897. fprintf(stderr, "Wrong parsed entity size: %lu (expected %lu)\n",
  898. ctxt->sizeentities, total_size);
  899. res = 1;
  900. }
  901. }
  902. xmlFreeDoc(doc);
  903. xmlFreeParserCtxt(ctxt);
  904. return(res);
  905. }
  906. /************************************************************************
  907. * *
  908. * Tests Descriptions *
  909. * *
  910. ************************************************************************/
  911. static
  912. testDesc testDescriptions[] = {
  913. { "Parsing recursive test cases" ,
  914. recursiveDetectTest, "./test/recurse/lol*.xml", NULL, NULL, NULL,
  915. 0 },
  916. { "Parsing recursive test cases (no substitution)" ,
  917. recursiveDetectTest, "./test/recurse/lol*.xml", NULL, NULL, NULL,
  918. OPT_NO_SUBST },
  919. { "Parsing recursive test cases (SAX)" ,
  920. recursiveDetectTest, "./test/recurse/lol*.xml", NULL, NULL, NULL,
  921. OPT_SAX },
  922. { "Parsing recursive test cases (SAX, no substitution)" ,
  923. recursiveDetectTest, "./test/recurse/lol*.xml", NULL, NULL, NULL,
  924. OPT_SAX | OPT_NO_SUBST },
  925. { "Parsing non-recursive test cases" ,
  926. notRecursiveDetectTest, "./test/recurse/good*.xml", NULL, NULL, NULL,
  927. 0 },
  928. { "Parsing non-recursive test cases (SAX)" ,
  929. notRecursiveDetectTest, "./test/recurse/good*.xml", NULL, NULL, NULL,
  930. OPT_SAX },
  931. { "Parsing non-recursive huge case" ,
  932. notRecursiveHugeTest, NULL, NULL, NULL, NULL,
  933. 0 },
  934. { "Parsing non-recursive huge case (no substitution)" ,
  935. notRecursiveHugeTest, NULL, NULL, NULL, NULL,
  936. OPT_NO_SUBST },
  937. { "Parsing non-recursive huge case (SAX)" ,
  938. notRecursiveHugeTest, NULL, NULL, NULL, NULL,
  939. OPT_SAX },
  940. { "Parsing non-recursive huge case (SAX, no substitution)" ,
  941. notRecursiveHugeTest, NULL, NULL, NULL, NULL,
  942. OPT_SAX | OPT_NO_SUBST },
  943. { "Parsing non-recursive huge DTD case" ,
  944. hugeDtdTest, NULL, NULL, NULL, NULL,
  945. 0 },
  946. {NULL, NULL, NULL, NULL, NULL, NULL, 0}
  947. };
  948. /************************************************************************
  949. * *
  950. * The main code driving the tests *
  951. * *
  952. ************************************************************************/
  953. static int
  954. launchTests(testDescPtr tst) {
  955. int res = 0, err = 0;
  956. size_t i;
  957. char *result;
  958. char *error;
  959. int mem;
  960. if (tst == NULL) return(-1);
  961. if (tst->in != NULL) {
  962. glob_t globbuf;
  963. globbuf.gl_offs = 0;
  964. glob(tst->in, GLOB_DOOFFS, NULL, &globbuf);
  965. for (i = 0;i < globbuf.gl_pathc;i++) {
  966. if (!checkTestFile(globbuf.gl_pathv[i]))
  967. continue;
  968. if (tst->suffix != NULL) {
  969. result = resultFilename(globbuf.gl_pathv[i], tst->out,
  970. tst->suffix);
  971. if (result == NULL) {
  972. fprintf(stderr, "Out of memory !\n");
  973. fatalError();
  974. }
  975. } else {
  976. result = NULL;
  977. }
  978. if (tst->err != NULL) {
  979. error = resultFilename(globbuf.gl_pathv[i], tst->out,
  980. tst->err);
  981. if (error == NULL) {
  982. fprintf(stderr, "Out of memory !\n");
  983. fatalError();
  984. }
  985. } else {
  986. error = NULL;
  987. }
  988. if ((result) &&(!checkTestFile(result))) {
  989. fprintf(stderr, "Missing result file %s\n", result);
  990. } else if ((error) &&(!checkTestFile(error))) {
  991. fprintf(stderr, "Missing error file %s\n", error);
  992. } else {
  993. mem = xmlMemUsed();
  994. extraMemoryFromResolver = 0;
  995. testErrorsSize = 0;
  996. testErrors[0] = 0;
  997. res = tst->func(globbuf.gl_pathv[i], result, error,
  998. tst->options | XML_PARSE_COMPACT);
  999. xmlResetLastError();
  1000. if (res != 0) {
  1001. fprintf(stderr, "File %s generated an error\n",
  1002. globbuf.gl_pathv[i]);
  1003. nb_errors++;
  1004. err++;
  1005. }
  1006. else if (xmlMemUsed() != mem) {
  1007. if ((xmlMemUsed() != mem) &&
  1008. (extraMemoryFromResolver == 0)) {
  1009. fprintf(stderr, "File %s leaked %d bytes\n",
  1010. globbuf.gl_pathv[i], xmlMemUsed() - mem);
  1011. nb_leaks++;
  1012. err++;
  1013. }
  1014. }
  1015. testErrorsSize = 0;
  1016. }
  1017. if (result)
  1018. free(result);
  1019. if (error)
  1020. free(error);
  1021. }
  1022. globfree(&globbuf);
  1023. } else {
  1024. testErrorsSize = 0;
  1025. testErrors[0] = 0;
  1026. extraMemoryFromResolver = 0;
  1027. res = tst->func(NULL, NULL, NULL, tst->options);
  1028. if (res != 0) {
  1029. nb_errors++;
  1030. err++;
  1031. }
  1032. }
  1033. return(err);
  1034. }
  1035. static int verbose = 0;
  1036. static int tests_quiet = 0;
  1037. static int
  1038. runtest(int i) {
  1039. int ret = 0, res;
  1040. int old_errors, old_tests, old_leaks;
  1041. old_errors = nb_errors;
  1042. old_tests = nb_tests;
  1043. old_leaks = nb_leaks;
  1044. if ((tests_quiet == 0) && (testDescriptions[i].desc != NULL))
  1045. printf("## %s\n", testDescriptions[i].desc);
  1046. res = launchTests(&testDescriptions[i]);
  1047. if (res != 0)
  1048. ret++;
  1049. if (verbose) {
  1050. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  1051. printf("Ran %d tests, no errors\n", nb_tests - old_tests);
  1052. else
  1053. printf("Ran %d tests, %d errors, %d leaks\n",
  1054. nb_tests - old_tests,
  1055. nb_errors - old_errors,
  1056. nb_leaks - old_leaks);
  1057. }
  1058. return(ret);
  1059. }
  1060. int
  1061. main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  1062. int i, a, ret = 0;
  1063. int subset = 0;
  1064. initializeLibxml2();
  1065. for (a = 1; a < argc;a++) {
  1066. if (!strcmp(argv[a], "-v"))
  1067. verbose = 1;
  1068. else if (!strcmp(argv[a], "-quiet"))
  1069. tests_quiet = 1;
  1070. else {
  1071. for (i = 0; testDescriptions[i].func != NULL; i++) {
  1072. if (strstr(testDescriptions[i].desc, argv[a])) {
  1073. ret += runtest(i);
  1074. subset++;
  1075. }
  1076. }
  1077. }
  1078. }
  1079. if (subset == 0) {
  1080. for (i = 0; testDescriptions[i].func != NULL; i++) {
  1081. ret += runtest(i);
  1082. }
  1083. }
  1084. if ((nb_errors == 0) && (nb_leaks == 0)) {
  1085. ret = 0;
  1086. printf("Total %d tests, no errors\n",
  1087. nb_tests);
  1088. } else {
  1089. ret = 1;
  1090. printf("Total %d tests, %d errors, %d leaks\n",
  1091. nb_tests, nb_errors, nb_leaks);
  1092. }
  1093. xmlCleanupParser();
  1094. return(ret);
  1095. }