testlimits.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  1. /*
  2. * testlimits.c: C program to run libxml2 regression tests checking various
  3. * limits in document size. Will consume a lot of RAM and CPU cycles
  4. *
  5. * To compile on Unixes:
  6. * cc -o testlimits `xml2-config --cflags` testlimits.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 <time.h>
  22. #include <libxml/parser.h>
  23. #include <libxml/parserInternals.h>
  24. #include <libxml/tree.h>
  25. #include <libxml/uri.h>
  26. #ifdef LIBXML_READER_ENABLED
  27. #include <libxml/xmlreader.h>
  28. #endif
  29. static int verbose = 0;
  30. static int tests_quiet = 0;
  31. /************************************************************************
  32. * *
  33. * time handling *
  34. * *
  35. ************************************************************************/
  36. /* maximum time for one parsing before declaring a timeout */
  37. #define MAX_TIME 2 /* seconds */
  38. static clock_t t0;
  39. int timeout = 0;
  40. static void reset_timout(void) {
  41. timeout = 0;
  42. t0 = clock();
  43. }
  44. static int check_time(void) {
  45. clock_t tnow = clock();
  46. if (((tnow - t0) / CLOCKS_PER_SEC) > MAX_TIME) {
  47. timeout = 1;
  48. return(0);
  49. }
  50. return(1);
  51. }
  52. /************************************************************************
  53. * *
  54. * Huge document generator *
  55. * *
  56. ************************************************************************/
  57. #include <libxml/xmlIO.h>
  58. /*
  59. * Huge documents are built using fixed start and end chunks
  60. * and filling between the two an unconventional amount of char data
  61. */
  62. typedef struct hugeTest hugeTest;
  63. typedef hugeTest *hugeTestPtr;
  64. struct hugeTest {
  65. const char *description;
  66. const char *name;
  67. const char *start;
  68. const char *end;
  69. };
  70. static struct hugeTest hugeTests[] = {
  71. { "Huge text node", "huge:textNode", "<foo>", "</foo>" },
  72. { "Huge attribute node", "huge:attrNode", "<foo bar='", "'/>" },
  73. { "Huge comment node", "huge:commentNode", "<foo><!--", "--></foo>" },
  74. { "Huge PI node", "huge:piNode", "<foo><?bar ", "?></foo>" },
  75. };
  76. static const char *current;
  77. static int rlen;
  78. static unsigned int currentTest = 0;
  79. static int instate = 0;
  80. /**
  81. * hugeMatch:
  82. * @URI: an URI to test
  83. *
  84. * Check for an huge: query
  85. *
  86. * Returns 1 if yes and 0 if another Input module should be used
  87. */
  88. static int
  89. hugeMatch(const char * URI) {
  90. if ((URI != NULL) && (!strncmp(URI, "huge:", 5)))
  91. return(1);
  92. return(0);
  93. }
  94. /**
  95. * hugeOpen:
  96. * @URI: an URI to test
  97. *
  98. * Return a pointer to the huge: query handler, in this example simply
  99. * the current pointer...
  100. *
  101. * Returns an Input context or NULL in case or error
  102. */
  103. static void *
  104. hugeOpen(const char * URI) {
  105. if ((URI == NULL) || (strncmp(URI, "huge:", 5)))
  106. return(NULL);
  107. for (currentTest = 0;currentTest < sizeof(hugeTests)/sizeof(hugeTests[0]);
  108. currentTest++)
  109. if (!strcmp(hugeTests[currentTest].name, URI))
  110. goto found;
  111. return(NULL);
  112. found:
  113. rlen = strlen(hugeTests[currentTest].start);
  114. current = hugeTests[currentTest].start;
  115. instate = 0;
  116. return((void *) current);
  117. }
  118. /**
  119. * hugeClose:
  120. * @context: the read context
  121. *
  122. * Close the huge: query handler
  123. *
  124. * Returns 0 or -1 in case of error
  125. */
  126. static int
  127. hugeClose(void * context) {
  128. if (context == NULL) return(-1);
  129. fprintf(stderr, "\n");
  130. return(0);
  131. }
  132. #define CHUNK 4096
  133. char filling[CHUNK + 1];
  134. static void fillFilling(void) {
  135. int i;
  136. for (i = 0;i < CHUNK;i++) {
  137. filling[i] = 'a';
  138. }
  139. filling[CHUNK] = 0;
  140. }
  141. size_t maxlen = 64 * 1024 * 1024;
  142. size_t curlen = 0;
  143. size_t dotlen;
  144. /**
  145. * hugeRead:
  146. * @context: the read context
  147. * @buffer: where to store data
  148. * @len: number of bytes to read
  149. *
  150. * Implement an huge: query read.
  151. *
  152. * Returns the number of bytes read or -1 in case of error
  153. */
  154. static int
  155. hugeRead(void *context, char *buffer, int len)
  156. {
  157. if ((context == NULL) || (buffer == NULL) || (len < 0))
  158. return (-1);
  159. if (instate == 0) {
  160. if (len >= rlen) {
  161. len = rlen;
  162. rlen = 0;
  163. memcpy(buffer, current, len);
  164. instate = 1;
  165. curlen = 0;
  166. dotlen = maxlen / 10;
  167. } else {
  168. memcpy(buffer, current, len);
  169. rlen -= len;
  170. current += len;
  171. }
  172. } else if (instate == 2) {
  173. if (len >= rlen) {
  174. len = rlen;
  175. rlen = 0;
  176. memcpy(buffer, current, len);
  177. instate = 3;
  178. curlen = 0;
  179. } else {
  180. memcpy(buffer, current, len);
  181. rlen -= len;
  182. current += len;
  183. }
  184. } else if (instate == 1) {
  185. if (len > CHUNK) len = CHUNK;
  186. memcpy(buffer, &filling[0], len);
  187. curlen += len;
  188. if (curlen >= maxlen) {
  189. rlen = strlen(hugeTests[currentTest].end);
  190. current = hugeTests[currentTest].end;
  191. instate = 2;
  192. } else {
  193. if (curlen > dotlen) {
  194. fprintf(stderr, ".");
  195. dotlen += maxlen / 10;
  196. }
  197. }
  198. } else
  199. len = 0;
  200. return (len);
  201. }
  202. /************************************************************************
  203. * *
  204. * Crazy document generator *
  205. * *
  206. ************************************************************************/
  207. unsigned int crazy_indx = 0;
  208. const char *crazy = "<?xml version='1.0' encoding='UTF-8'?>\
  209. <?tst ?>\
  210. <!-- tst -->\
  211. <!DOCTYPE foo [\
  212. <?tst ?>\
  213. <!-- tst -->\
  214. <!ELEMENT foo (#PCDATA)>\
  215. <!ELEMENT p (#PCDATA|emph)* >\
  216. ]>\
  217. <?tst ?>\
  218. <!-- tst -->\
  219. <foo bar='foo'>\
  220. <?tst ?>\
  221. <!-- tst -->\
  222. foo\
  223. <![CDATA[ ]]>\
  224. </foo>\
  225. <?tst ?>\
  226. <!-- tst -->";
  227. /**
  228. * crazyMatch:
  229. * @URI: an URI to test
  230. *
  231. * Check for a crazy: query
  232. *
  233. * Returns 1 if yes and 0 if another Input module should be used
  234. */
  235. static int
  236. crazyMatch(const char * URI) {
  237. if ((URI != NULL) && (!strncmp(URI, "crazy:", 6)))
  238. return(1);
  239. return(0);
  240. }
  241. /**
  242. * crazyOpen:
  243. * @URI: an URI to test
  244. *
  245. * Return a pointer to the crazy: query handler, in this example simply
  246. * the current pointer...
  247. *
  248. * Returns an Input context or NULL in case or error
  249. */
  250. static void *
  251. crazyOpen(const char * URI) {
  252. if ((URI == NULL) || (strncmp(URI, "crazy:", 6)))
  253. return(NULL);
  254. if (crazy_indx > strlen(crazy))
  255. return(NULL);
  256. reset_timout();
  257. rlen = crazy_indx;
  258. current = &crazy[0];
  259. instate = 0;
  260. return((void *) current);
  261. }
  262. /**
  263. * crazyClose:
  264. * @context: the read context
  265. *
  266. * Close the crazy: query handler
  267. *
  268. * Returns 0 or -1 in case of error
  269. */
  270. static int
  271. crazyClose(void * context) {
  272. if (context == NULL) return(-1);
  273. return(0);
  274. }
  275. /**
  276. * crazyRead:
  277. * @context: the read context
  278. * @buffer: where to store data
  279. * @len: number of bytes to read
  280. *
  281. * Implement an crazy: query read.
  282. *
  283. * Returns the number of bytes read or -1 in case of error
  284. */
  285. static int
  286. crazyRead(void *context, char *buffer, int len)
  287. {
  288. if ((context == NULL) || (buffer == NULL) || (len < 0))
  289. return (-1);
  290. if ((check_time() <= 0) && (instate == 1)) {
  291. fprintf(stderr, "\ntimeout in crazy(%d)\n", crazy_indx);
  292. rlen = strlen(crazy) - crazy_indx;
  293. current = &crazy[crazy_indx];
  294. instate = 2;
  295. }
  296. if (instate == 0) {
  297. if (len >= rlen) {
  298. len = rlen;
  299. rlen = 0;
  300. memcpy(buffer, current, len);
  301. instate = 1;
  302. curlen = 0;
  303. } else {
  304. memcpy(buffer, current, len);
  305. rlen -= len;
  306. current += len;
  307. }
  308. } else if (instate == 2) {
  309. if (len >= rlen) {
  310. len = rlen;
  311. rlen = 0;
  312. memcpy(buffer, current, len);
  313. instate = 3;
  314. curlen = 0;
  315. } else {
  316. memcpy(buffer, current, len);
  317. rlen -= len;
  318. current += len;
  319. }
  320. } else if (instate == 1) {
  321. if (len > CHUNK) len = CHUNK;
  322. memcpy(buffer, &filling[0], len);
  323. curlen += len;
  324. if (curlen >= maxlen) {
  325. rlen = strlen(crazy) - crazy_indx;
  326. current = &crazy[crazy_indx];
  327. instate = 2;
  328. }
  329. } else
  330. len = 0;
  331. return (len);
  332. }
  333. /************************************************************************
  334. * *
  335. * Libxml2 specific routines *
  336. * *
  337. ************************************************************************/
  338. static int nb_tests = 0;
  339. static int nb_errors = 0;
  340. static int nb_leaks = 0;
  341. static int extraMemoryFromResolver = 0;
  342. /*
  343. * We need to trap calls to the resolver to not account memory for the catalog
  344. * which is shared to the current running test. We also don't want to have
  345. * network downloads modifying tests.
  346. */
  347. static xmlParserInputPtr
  348. testExternalEntityLoader(const char *URL, const char *ID,
  349. xmlParserCtxtPtr ctxt) {
  350. xmlParserInputPtr ret;
  351. int memused = xmlMemUsed();
  352. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  353. extraMemoryFromResolver += xmlMemUsed() - memused;
  354. return(ret);
  355. }
  356. /*
  357. * Trapping the error messages at the generic level to grab the equivalent of
  358. * stderr messages on CLI tools.
  359. */
  360. static char testErrors[32769];
  361. static int testErrorsSize = 0;
  362. static void XMLCDECL
  363. channel(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
  364. va_list args;
  365. int res;
  366. if (testErrorsSize >= 32768)
  367. return;
  368. va_start(args, msg);
  369. res = vsnprintf(&testErrors[testErrorsSize],
  370. 32768 - testErrorsSize,
  371. msg, args);
  372. va_end(args);
  373. if (testErrorsSize + res >= 32768) {
  374. /* buffer is full */
  375. testErrorsSize = 32768;
  376. testErrors[testErrorsSize] = 0;
  377. } else {
  378. testErrorsSize += res;
  379. }
  380. testErrors[testErrorsSize] = 0;
  381. }
  382. /**
  383. * xmlParserPrintFileContext:
  384. * @input: an xmlParserInputPtr input
  385. *
  386. * Displays current context within the input content for error tracking
  387. */
  388. static void
  389. xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
  390. xmlGenericErrorFunc chanl, void *data ) {
  391. const xmlChar *cur, *base;
  392. unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
  393. xmlChar content[81]; /* space for 80 chars + line terminator */
  394. xmlChar *ctnt;
  395. if (input == NULL) return;
  396. cur = input->cur;
  397. base = input->base;
  398. /* skip backwards over any end-of-lines */
  399. while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
  400. cur--;
  401. }
  402. n = 0;
  403. /* search backwards for beginning-of-line (to max buff size) */
  404. while ((n++ < (sizeof(content)-1)) && (cur > base) &&
  405. (*(cur) != '\n') && (*(cur) != '\r'))
  406. cur--;
  407. if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
  408. /* calculate the error position in terms of the current position */
  409. col = input->cur - cur;
  410. /* search forward for end-of-line (to max buff size) */
  411. n = 0;
  412. ctnt = content;
  413. /* copy selected text to our buffer */
  414. while ((*cur != 0) && (*(cur) != '\n') &&
  415. (*(cur) != '\r') && (n < sizeof(content)-1)) {
  416. *ctnt++ = *cur++;
  417. n++;
  418. }
  419. *ctnt = 0;
  420. /* print out the selected text */
  421. chanl(data ,"%s\n", content);
  422. /* create blank line with problem pointer */
  423. n = 0;
  424. ctnt = content;
  425. /* (leave buffer space for pointer + line terminator) */
  426. while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
  427. if (*(ctnt) != '\t')
  428. *(ctnt) = ' ';
  429. ctnt++;
  430. }
  431. *ctnt++ = '^';
  432. *ctnt = 0;
  433. chanl(data ,"%s\n", content);
  434. }
  435. static void
  436. testStructuredErrorHandler(void *ctx ATTRIBUTE_UNUSED, xmlErrorPtr err) {
  437. char *file = NULL;
  438. int line = 0;
  439. int code = -1;
  440. int domain;
  441. void *data = NULL;
  442. const char *str;
  443. const xmlChar *name = NULL;
  444. xmlNodePtr node;
  445. xmlErrorLevel level;
  446. xmlParserInputPtr input = NULL;
  447. xmlParserInputPtr cur = NULL;
  448. xmlParserCtxtPtr ctxt = NULL;
  449. if (err == NULL)
  450. return;
  451. file = err->file;
  452. line = err->line;
  453. code = err->code;
  454. domain = err->domain;
  455. level = err->level;
  456. node = err->node;
  457. if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
  458. (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
  459. (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
  460. ctxt = err->ctxt;
  461. }
  462. str = err->message;
  463. if (code == XML_ERR_OK)
  464. return;
  465. if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
  466. name = node->name;
  467. /*
  468. * Maintain the compatibility with the legacy error handling
  469. */
  470. if (ctxt != NULL) {
  471. input = ctxt->input;
  472. if ((input != NULL) && (input->filename == NULL) &&
  473. (ctxt->inputNr > 1)) {
  474. cur = input;
  475. input = ctxt->inputTab[ctxt->inputNr - 2];
  476. }
  477. if (input != NULL) {
  478. if (input->filename)
  479. channel(data, "%s:%d: ", input->filename, input->line);
  480. else if ((line != 0) && (domain == XML_FROM_PARSER))
  481. channel(data, "Entity: line %d: ", input->line);
  482. }
  483. } else {
  484. if (file != NULL)
  485. channel(data, "%s:%d: ", file, line);
  486. else if ((line != 0) && (domain == XML_FROM_PARSER))
  487. channel(data, "Entity: line %d: ", line);
  488. }
  489. if (name != NULL) {
  490. channel(data, "element %s: ", name);
  491. }
  492. if (code == XML_ERR_OK)
  493. return;
  494. switch (domain) {
  495. case XML_FROM_PARSER:
  496. channel(data, "parser ");
  497. break;
  498. case XML_FROM_NAMESPACE:
  499. channel(data, "namespace ");
  500. break;
  501. case XML_FROM_DTD:
  502. case XML_FROM_VALID:
  503. channel(data, "validity ");
  504. break;
  505. case XML_FROM_HTML:
  506. channel(data, "HTML parser ");
  507. break;
  508. case XML_FROM_MEMORY:
  509. channel(data, "memory ");
  510. break;
  511. case XML_FROM_OUTPUT:
  512. channel(data, "output ");
  513. break;
  514. case XML_FROM_IO:
  515. channel(data, "I/O ");
  516. break;
  517. case XML_FROM_XINCLUDE:
  518. channel(data, "XInclude ");
  519. break;
  520. case XML_FROM_XPATH:
  521. channel(data, "XPath ");
  522. break;
  523. case XML_FROM_XPOINTER:
  524. channel(data, "parser ");
  525. break;
  526. case XML_FROM_REGEXP:
  527. channel(data, "regexp ");
  528. break;
  529. case XML_FROM_MODULE:
  530. channel(data, "module ");
  531. break;
  532. case XML_FROM_SCHEMASV:
  533. channel(data, "Schemas validity ");
  534. break;
  535. case XML_FROM_SCHEMASP:
  536. channel(data, "Schemas parser ");
  537. break;
  538. case XML_FROM_RELAXNGP:
  539. channel(data, "Relax-NG parser ");
  540. break;
  541. case XML_FROM_RELAXNGV:
  542. channel(data, "Relax-NG validity ");
  543. break;
  544. case XML_FROM_CATALOG:
  545. channel(data, "Catalog ");
  546. break;
  547. case XML_FROM_C14N:
  548. channel(data, "C14N ");
  549. break;
  550. case XML_FROM_XSLT:
  551. channel(data, "XSLT ");
  552. break;
  553. default:
  554. break;
  555. }
  556. if (code == XML_ERR_OK)
  557. return;
  558. switch (level) {
  559. case XML_ERR_NONE:
  560. channel(data, ": ");
  561. break;
  562. case XML_ERR_WARNING:
  563. channel(data, "warning : ");
  564. break;
  565. case XML_ERR_ERROR:
  566. channel(data, "error : ");
  567. break;
  568. case XML_ERR_FATAL:
  569. channel(data, "error : ");
  570. break;
  571. }
  572. if (code == XML_ERR_OK)
  573. return;
  574. if (str != NULL) {
  575. int len;
  576. len = xmlStrlen((const xmlChar *)str);
  577. if ((len > 0) && (str[len - 1] != '\n'))
  578. channel(data, "%s\n", str);
  579. else
  580. channel(data, "%s", str);
  581. } else {
  582. channel(data, "%s\n", "out of memory error");
  583. }
  584. if (code == XML_ERR_OK)
  585. return;
  586. if (ctxt != NULL) {
  587. xmlParserPrintFileContextInternal(input, channel, data);
  588. if (cur != NULL) {
  589. if (cur->filename)
  590. channel(data, "%s:%d: \n", cur->filename, cur->line);
  591. else if ((line != 0) && (domain == XML_FROM_PARSER))
  592. channel(data, "Entity: line %d: \n", cur->line);
  593. xmlParserPrintFileContextInternal(cur, channel, data);
  594. }
  595. }
  596. if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
  597. (err->int1 < 100) &&
  598. (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
  599. xmlChar buf[150];
  600. int i;
  601. channel(data, "%s\n", err->str1);
  602. for (i=0;i < err->int1;i++)
  603. buf[i] = ' ';
  604. buf[i++] = '^';
  605. buf[i] = 0;
  606. channel(data, "%s\n", buf);
  607. }
  608. }
  609. static void
  610. initializeLibxml2(void) {
  611. xmlGetWarningsDefaultValue = 0;
  612. xmlPedanticParserDefault(0);
  613. xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
  614. xmlInitParser();
  615. xmlSetExternalEntityLoader(testExternalEntityLoader);
  616. xmlSetStructuredErrorFunc(NULL, testStructuredErrorHandler);
  617. /*
  618. * register the new I/O handlers
  619. */
  620. if (xmlRegisterInputCallbacks(hugeMatch, hugeOpen,
  621. hugeRead, hugeClose) < 0) {
  622. fprintf(stderr, "failed to register Huge handlers\n");
  623. exit(1);
  624. }
  625. if (xmlRegisterInputCallbacks(crazyMatch, crazyOpen,
  626. crazyRead, crazyClose) < 0) {
  627. fprintf(stderr, "failed to register Crazy handlers\n");
  628. exit(1);
  629. }
  630. }
  631. /************************************************************************
  632. * *
  633. * SAX empty callbacks *
  634. * *
  635. ************************************************************************/
  636. unsigned long callbacks = 0;
  637. /**
  638. * isStandaloneCallback:
  639. * @ctxt: An XML parser context
  640. *
  641. * Is this document tagged standalone ?
  642. *
  643. * Returns 1 if true
  644. */
  645. static int
  646. isStandaloneCallback(void *ctx ATTRIBUTE_UNUSED)
  647. {
  648. callbacks++;
  649. return (0);
  650. }
  651. /**
  652. * hasInternalSubsetCallback:
  653. * @ctxt: An XML parser context
  654. *
  655. * Does this document has an internal subset
  656. *
  657. * Returns 1 if true
  658. */
  659. static int
  660. hasInternalSubsetCallback(void *ctx ATTRIBUTE_UNUSED)
  661. {
  662. callbacks++;
  663. return (0);
  664. }
  665. /**
  666. * hasExternalSubsetCallback:
  667. * @ctxt: An XML parser context
  668. *
  669. * Does this document has an external subset
  670. *
  671. * Returns 1 if true
  672. */
  673. static int
  674. hasExternalSubsetCallback(void *ctx ATTRIBUTE_UNUSED)
  675. {
  676. callbacks++;
  677. return (0);
  678. }
  679. /**
  680. * internalSubsetCallback:
  681. * @ctxt: An XML parser context
  682. *
  683. * Does this document has an internal subset
  684. */
  685. static void
  686. internalSubsetCallback(void *ctx ATTRIBUTE_UNUSED,
  687. const xmlChar * name ATTRIBUTE_UNUSED,
  688. const xmlChar * ExternalID ATTRIBUTE_UNUSED,
  689. const xmlChar * SystemID ATTRIBUTE_UNUSED)
  690. {
  691. callbacks++;
  692. return;
  693. }
  694. /**
  695. * externalSubsetCallback:
  696. * @ctxt: An XML parser context
  697. *
  698. * Does this document has an external subset
  699. */
  700. static void
  701. externalSubsetCallback(void *ctx ATTRIBUTE_UNUSED,
  702. const xmlChar * name ATTRIBUTE_UNUSED,
  703. const xmlChar * ExternalID ATTRIBUTE_UNUSED,
  704. const xmlChar * SystemID ATTRIBUTE_UNUSED)
  705. {
  706. callbacks++;
  707. return;
  708. }
  709. /**
  710. * resolveEntityCallback:
  711. * @ctxt: An XML parser context
  712. * @publicId: The public ID of the entity
  713. * @systemId: The system ID of the entity
  714. *
  715. * Special entity resolver, better left to the parser, it has
  716. * more context than the application layer.
  717. * The default behaviour is to NOT resolve the entities, in that case
  718. * the ENTITY_REF nodes are built in the structure (and the parameter
  719. * values).
  720. *
  721. * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
  722. */
  723. static xmlParserInputPtr
  724. resolveEntityCallback(void *ctx ATTRIBUTE_UNUSED,
  725. const xmlChar * publicId ATTRIBUTE_UNUSED,
  726. const xmlChar * systemId ATTRIBUTE_UNUSED)
  727. {
  728. callbacks++;
  729. return (NULL);
  730. }
  731. /**
  732. * getEntityCallback:
  733. * @ctxt: An XML parser context
  734. * @name: The entity name
  735. *
  736. * Get an entity by name
  737. *
  738. * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
  739. */
  740. static xmlEntityPtr
  741. getEntityCallback(void *ctx ATTRIBUTE_UNUSED,
  742. const xmlChar * name ATTRIBUTE_UNUSED)
  743. {
  744. callbacks++;
  745. return (NULL);
  746. }
  747. /**
  748. * getParameterEntityCallback:
  749. * @ctxt: An XML parser context
  750. * @name: The entity name
  751. *
  752. * Get a parameter entity by name
  753. *
  754. * Returns the xmlParserInputPtr
  755. */
  756. static xmlEntityPtr
  757. getParameterEntityCallback(void *ctx ATTRIBUTE_UNUSED,
  758. const xmlChar * name ATTRIBUTE_UNUSED)
  759. {
  760. callbacks++;
  761. return (NULL);
  762. }
  763. /**
  764. * entityDeclCallback:
  765. * @ctxt: An XML parser context
  766. * @name: the entity name
  767. * @type: the entity type
  768. * @publicId: The public ID of the entity
  769. * @systemId: The system ID of the entity
  770. * @content: the entity value (without processing).
  771. *
  772. * An entity definition has been parsed
  773. */
  774. static void
  775. entityDeclCallback(void *ctx ATTRIBUTE_UNUSED,
  776. const xmlChar * name ATTRIBUTE_UNUSED,
  777. int type ATTRIBUTE_UNUSED,
  778. const xmlChar * publicId ATTRIBUTE_UNUSED,
  779. const xmlChar * systemId ATTRIBUTE_UNUSED,
  780. xmlChar * content ATTRIBUTE_UNUSED)
  781. {
  782. callbacks++;
  783. return;
  784. }
  785. /**
  786. * attributeDeclCallback:
  787. * @ctxt: An XML parser context
  788. * @name: the attribute name
  789. * @type: the attribute type
  790. *
  791. * An attribute definition has been parsed
  792. */
  793. static void
  794. attributeDeclCallback(void *ctx ATTRIBUTE_UNUSED,
  795. const xmlChar * elem ATTRIBUTE_UNUSED,
  796. const xmlChar * name ATTRIBUTE_UNUSED,
  797. int type ATTRIBUTE_UNUSED, int def ATTRIBUTE_UNUSED,
  798. const xmlChar * defaultValue ATTRIBUTE_UNUSED,
  799. xmlEnumerationPtr tree ATTRIBUTE_UNUSED)
  800. {
  801. callbacks++;
  802. return;
  803. }
  804. /**
  805. * elementDeclCallback:
  806. * @ctxt: An XML parser context
  807. * @name: the element name
  808. * @type: the element type
  809. * @content: the element value (without processing).
  810. *
  811. * An element definition has been parsed
  812. */
  813. static void
  814. elementDeclCallback(void *ctx ATTRIBUTE_UNUSED,
  815. const xmlChar * name ATTRIBUTE_UNUSED,
  816. int type ATTRIBUTE_UNUSED,
  817. xmlElementContentPtr content ATTRIBUTE_UNUSED)
  818. {
  819. callbacks++;
  820. return;
  821. }
  822. /**
  823. * notationDeclCallback:
  824. * @ctxt: An XML parser context
  825. * @name: The name of the notation
  826. * @publicId: The public ID of the entity
  827. * @systemId: The system ID of the entity
  828. *
  829. * What to do when a notation declaration has been parsed.
  830. */
  831. static void
  832. notationDeclCallback(void *ctx ATTRIBUTE_UNUSED,
  833. const xmlChar * name ATTRIBUTE_UNUSED,
  834. const xmlChar * publicId ATTRIBUTE_UNUSED,
  835. const xmlChar * systemId ATTRIBUTE_UNUSED)
  836. {
  837. callbacks++;
  838. return;
  839. }
  840. /**
  841. * unparsedEntityDeclCallback:
  842. * @ctxt: An XML parser context
  843. * @name: The name of the entity
  844. * @publicId: The public ID of the entity
  845. * @systemId: The system ID of the entity
  846. * @notationName: the name of the notation
  847. *
  848. * What to do when an unparsed entity declaration is parsed
  849. */
  850. static void
  851. unparsedEntityDeclCallback(void *ctx ATTRIBUTE_UNUSED,
  852. const xmlChar * name ATTRIBUTE_UNUSED,
  853. const xmlChar * publicId ATTRIBUTE_UNUSED,
  854. const xmlChar * systemId ATTRIBUTE_UNUSED,
  855. const xmlChar * notationName ATTRIBUTE_UNUSED)
  856. {
  857. callbacks++;
  858. return;
  859. }
  860. /**
  861. * setDocumentLocatorCallback:
  862. * @ctxt: An XML parser context
  863. * @loc: A SAX Locator
  864. *
  865. * Receive the document locator at startup, actually xmlDefaultSAXLocator
  866. * Everything is available on the context, so this is useless in our case.
  867. */
  868. static void
  869. setDocumentLocatorCallback(void *ctx ATTRIBUTE_UNUSED,
  870. xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
  871. {
  872. callbacks++;
  873. return;
  874. }
  875. /**
  876. * startDocumentCallback:
  877. * @ctxt: An XML parser context
  878. *
  879. * called when the document start being processed.
  880. */
  881. static void
  882. startDocumentCallback(void *ctx ATTRIBUTE_UNUSED)
  883. {
  884. callbacks++;
  885. return;
  886. }
  887. /**
  888. * endDocumentCallback:
  889. * @ctxt: An XML parser context
  890. *
  891. * called when the document end has been detected.
  892. */
  893. static void
  894. endDocumentCallback(void *ctx ATTRIBUTE_UNUSED)
  895. {
  896. callbacks++;
  897. return;
  898. }
  899. #if 0
  900. /**
  901. * startElementCallback:
  902. * @ctxt: An XML parser context
  903. * @name: The element name
  904. *
  905. * called when an opening tag has been processed.
  906. */
  907. static void
  908. startElementCallback(void *ctx ATTRIBUTE_UNUSED,
  909. const xmlChar * name ATTRIBUTE_UNUSED,
  910. const xmlChar ** atts ATTRIBUTE_UNUSED)
  911. {
  912. callbacks++;
  913. return;
  914. }
  915. /**
  916. * endElementCallback:
  917. * @ctxt: An XML parser context
  918. * @name: The element name
  919. *
  920. * called when the end of an element has been detected.
  921. */
  922. static void
  923. endElementCallback(void *ctx ATTRIBUTE_UNUSED,
  924. const xmlChar * name ATTRIBUTE_UNUSED)
  925. {
  926. callbacks++;
  927. return;
  928. }
  929. #endif
  930. /**
  931. * charactersCallback:
  932. * @ctxt: An XML parser context
  933. * @ch: a xmlChar string
  934. * @len: the number of xmlChar
  935. *
  936. * receiving some chars from the parser.
  937. * Question: how much at a time ???
  938. */
  939. static void
  940. charactersCallback(void *ctx ATTRIBUTE_UNUSED,
  941. const xmlChar * ch ATTRIBUTE_UNUSED,
  942. int len ATTRIBUTE_UNUSED)
  943. {
  944. callbacks++;
  945. return;
  946. }
  947. /**
  948. * referenceCallback:
  949. * @ctxt: An XML parser context
  950. * @name: The entity name
  951. *
  952. * called when an entity reference is detected.
  953. */
  954. static void
  955. referenceCallback(void *ctx ATTRIBUTE_UNUSED,
  956. const xmlChar * name ATTRIBUTE_UNUSED)
  957. {
  958. callbacks++;
  959. return;
  960. }
  961. /**
  962. * ignorableWhitespaceCallback:
  963. * @ctxt: An XML parser context
  964. * @ch: a xmlChar string
  965. * @start: the first char in the string
  966. * @len: the number of xmlChar
  967. *
  968. * receiving some ignorable whitespaces from the parser.
  969. * Question: how much at a time ???
  970. */
  971. static void
  972. ignorableWhitespaceCallback(void *ctx ATTRIBUTE_UNUSED,
  973. const xmlChar * ch ATTRIBUTE_UNUSED,
  974. int len ATTRIBUTE_UNUSED)
  975. {
  976. callbacks++;
  977. return;
  978. }
  979. /**
  980. * processingInstructionCallback:
  981. * @ctxt: An XML parser context
  982. * @target: the target name
  983. * @data: the PI data's
  984. * @len: the number of xmlChar
  985. *
  986. * A processing instruction has been parsed.
  987. */
  988. static void
  989. processingInstructionCallback(void *ctx ATTRIBUTE_UNUSED,
  990. const xmlChar * target ATTRIBUTE_UNUSED,
  991. const xmlChar * data ATTRIBUTE_UNUSED)
  992. {
  993. callbacks++;
  994. return;
  995. }
  996. /**
  997. * cdataBlockCallback:
  998. * @ctx: the user data (XML parser context)
  999. * @value: The pcdata content
  1000. * @len: the block length
  1001. *
  1002. * called when a pcdata block has been parsed
  1003. */
  1004. static void
  1005. cdataBlockCallback(void *ctx ATTRIBUTE_UNUSED,
  1006. const xmlChar * value ATTRIBUTE_UNUSED,
  1007. int len ATTRIBUTE_UNUSED)
  1008. {
  1009. callbacks++;
  1010. return;
  1011. }
  1012. /**
  1013. * commentCallback:
  1014. * @ctxt: An XML parser context
  1015. * @value: the comment content
  1016. *
  1017. * A comment has been parsed.
  1018. */
  1019. static void
  1020. commentCallback(void *ctx ATTRIBUTE_UNUSED,
  1021. const xmlChar * value ATTRIBUTE_UNUSED)
  1022. {
  1023. callbacks++;
  1024. return;
  1025. }
  1026. /**
  1027. * warningCallback:
  1028. * @ctxt: An XML parser context
  1029. * @msg: the message to display/transmit
  1030. * @...: extra parameters for the message display
  1031. *
  1032. * Display and format a warning messages, gives file, line, position and
  1033. * extra parameters.
  1034. */
  1035. static void XMLCDECL
  1036. warningCallback(void *ctx ATTRIBUTE_UNUSED,
  1037. const char *msg ATTRIBUTE_UNUSED, ...)
  1038. {
  1039. callbacks++;
  1040. return;
  1041. }
  1042. /**
  1043. * errorCallback:
  1044. * @ctxt: An XML parser context
  1045. * @msg: the message to display/transmit
  1046. * @...: extra parameters for the message display
  1047. *
  1048. * Display and format a error messages, gives file, line, position and
  1049. * extra parameters.
  1050. */
  1051. static void XMLCDECL
  1052. errorCallback(void *ctx ATTRIBUTE_UNUSED, const char *msg ATTRIBUTE_UNUSED,
  1053. ...)
  1054. {
  1055. callbacks++;
  1056. return;
  1057. }
  1058. /**
  1059. * fatalErrorCallback:
  1060. * @ctxt: An XML parser context
  1061. * @msg: the message to display/transmit
  1062. * @...: extra parameters for the message display
  1063. *
  1064. * Display and format a fatalError messages, gives file, line, position and
  1065. * extra parameters.
  1066. */
  1067. static void XMLCDECL
  1068. fatalErrorCallback(void *ctx ATTRIBUTE_UNUSED,
  1069. const char *msg ATTRIBUTE_UNUSED, ...)
  1070. {
  1071. return;
  1072. }
  1073. /*
  1074. * SAX2 specific callbacks
  1075. */
  1076. /**
  1077. * startElementNsCallback:
  1078. * @ctxt: An XML parser context
  1079. * @name: The element name
  1080. *
  1081. * called when an opening tag has been processed.
  1082. */
  1083. static void
  1084. startElementNsCallback(void *ctx ATTRIBUTE_UNUSED,
  1085. const xmlChar * localname ATTRIBUTE_UNUSED,
  1086. const xmlChar * prefix ATTRIBUTE_UNUSED,
  1087. const xmlChar * URI ATTRIBUTE_UNUSED,
  1088. int nb_namespaces ATTRIBUTE_UNUSED,
  1089. const xmlChar ** namespaces ATTRIBUTE_UNUSED,
  1090. int nb_attributes ATTRIBUTE_UNUSED,
  1091. int nb_defaulted ATTRIBUTE_UNUSED,
  1092. const xmlChar ** attributes ATTRIBUTE_UNUSED)
  1093. {
  1094. callbacks++;
  1095. return;
  1096. }
  1097. /**
  1098. * endElementCallback:
  1099. * @ctxt: An XML parser context
  1100. * @name: The element name
  1101. *
  1102. * called when the end of an element has been detected.
  1103. */
  1104. static void
  1105. endElementNsCallback(void *ctx ATTRIBUTE_UNUSED,
  1106. const xmlChar * localname ATTRIBUTE_UNUSED,
  1107. const xmlChar * prefix ATTRIBUTE_UNUSED,
  1108. const xmlChar * URI ATTRIBUTE_UNUSED)
  1109. {
  1110. callbacks++;
  1111. return;
  1112. }
  1113. static xmlSAXHandler callbackSAX2HandlerStruct = {
  1114. internalSubsetCallback,
  1115. isStandaloneCallback,
  1116. hasInternalSubsetCallback,
  1117. hasExternalSubsetCallback,
  1118. resolveEntityCallback,
  1119. getEntityCallback,
  1120. entityDeclCallback,
  1121. notationDeclCallback,
  1122. attributeDeclCallback,
  1123. elementDeclCallback,
  1124. unparsedEntityDeclCallback,
  1125. setDocumentLocatorCallback,
  1126. startDocumentCallback,
  1127. endDocumentCallback,
  1128. NULL,
  1129. NULL,
  1130. referenceCallback,
  1131. charactersCallback,
  1132. ignorableWhitespaceCallback,
  1133. processingInstructionCallback,
  1134. commentCallback,
  1135. warningCallback,
  1136. errorCallback,
  1137. fatalErrorCallback,
  1138. getParameterEntityCallback,
  1139. cdataBlockCallback,
  1140. externalSubsetCallback,
  1141. XML_SAX2_MAGIC,
  1142. NULL,
  1143. startElementNsCallback,
  1144. endElementNsCallback,
  1145. NULL
  1146. };
  1147. static xmlSAXHandlerPtr callbackSAX2Handler = &callbackSAX2HandlerStruct;
  1148. /************************************************************************
  1149. * *
  1150. * The tests front-ends *
  1151. * *
  1152. ************************************************************************/
  1153. /**
  1154. * readerTest:
  1155. * @filename: the file to parse
  1156. * @max_size: size of the limit to test
  1157. * @options: parsing options
  1158. * @fail: should a failure be reported
  1159. *
  1160. * Parse a memory generated file using SAX
  1161. *
  1162. * Returns 0 in case of success, an error code otherwise
  1163. */
  1164. static int
  1165. saxTest(const char *filename, size_t limit, int options, int fail) {
  1166. int res = 0;
  1167. xmlParserCtxtPtr ctxt;
  1168. xmlDocPtr doc;
  1169. xmlSAXHandlerPtr old_sax;
  1170. nb_tests++;
  1171. maxlen = limit;
  1172. ctxt = xmlNewParserCtxt();
  1173. if (ctxt == NULL) {
  1174. fprintf(stderr, "Failed to create parser context\n");
  1175. return(1);
  1176. }
  1177. old_sax = ctxt->sax;
  1178. ctxt->sax = callbackSAX2Handler;
  1179. ctxt->userData = NULL;
  1180. doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
  1181. if (doc != NULL) {
  1182. fprintf(stderr, "SAX parsing generated a document !\n");
  1183. xmlFreeDoc(doc);
  1184. res = 0;
  1185. } else if (ctxt->wellFormed == 0) {
  1186. if (fail)
  1187. res = 0;
  1188. else {
  1189. fprintf(stderr, "Failed to parse '%s' %lu\n", filename,
  1190. (unsigned long) limit);
  1191. res = 1;
  1192. }
  1193. } else {
  1194. if (fail) {
  1195. fprintf(stderr, "Failed to get failure for '%s' %lu\n",
  1196. filename, (unsigned long) limit);
  1197. res = 1;
  1198. } else
  1199. res = 0;
  1200. }
  1201. ctxt->sax = old_sax;
  1202. xmlFreeParserCtxt(ctxt);
  1203. return(res);
  1204. }
  1205. #ifdef LIBXML_READER_ENABLED
  1206. /**
  1207. * readerTest:
  1208. * @filename: the file to parse
  1209. * @max_size: size of the limit to test
  1210. * @options: parsing options
  1211. * @fail: should a failure be reported
  1212. *
  1213. * Parse a memory generated file using the xmlReader
  1214. *
  1215. * Returns 0 in case of success, an error code otherwise
  1216. */
  1217. static int
  1218. readerTest(const char *filename, size_t limit, int options, int fail) {
  1219. xmlTextReaderPtr reader;
  1220. int res = 0;
  1221. int ret;
  1222. nb_tests++;
  1223. maxlen = limit;
  1224. reader = xmlReaderForFile(filename , NULL, options);
  1225. if (reader == NULL) {
  1226. fprintf(stderr, "Failed to open '%s' test\n", filename);
  1227. return(1);
  1228. }
  1229. ret = xmlTextReaderRead(reader);
  1230. while (ret == 1) {
  1231. ret = xmlTextReaderRead(reader);
  1232. }
  1233. if (ret != 0) {
  1234. if (fail)
  1235. res = 0;
  1236. else {
  1237. if (strncmp(filename, "crazy:", 6) == 0)
  1238. fprintf(stderr, "Failed to parse '%s' %u\n",
  1239. filename, crazy_indx);
  1240. else
  1241. fprintf(stderr, "Failed to parse '%s' %lu\n",
  1242. filename, (unsigned long) limit);
  1243. res = 1;
  1244. }
  1245. } else {
  1246. if (fail) {
  1247. if (strncmp(filename, "crazy:", 6) == 0)
  1248. fprintf(stderr, "Failed to get failure for '%s' %u\n",
  1249. filename, crazy_indx);
  1250. else
  1251. fprintf(stderr, "Failed to get failure for '%s' %lu\n",
  1252. filename, (unsigned long) limit);
  1253. res = 1;
  1254. } else
  1255. res = 0;
  1256. }
  1257. if (timeout)
  1258. res = 1;
  1259. xmlFreeTextReader(reader);
  1260. return(res);
  1261. }
  1262. #endif
  1263. /************************************************************************
  1264. * *
  1265. * Tests descriptions *
  1266. * *
  1267. ************************************************************************/
  1268. typedef int (*functest) (const char *filename, size_t limit, int options,
  1269. int fail);
  1270. typedef struct limitDesc limitDesc;
  1271. typedef limitDesc *limitDescPtr;
  1272. struct limitDesc {
  1273. const char *name; /* the huge generator name */
  1274. size_t limit; /* the limit to test */
  1275. int options; /* extra parser options */
  1276. int fail; /* whether the test should fail */
  1277. };
  1278. static limitDesc limitDescriptions[] = {
  1279. /* max length of a text node in content */
  1280. {"huge:textNode", XML_MAX_TEXT_LENGTH - CHUNK, 0, 0},
  1281. {"huge:textNode", XML_MAX_TEXT_LENGTH + CHUNK, 0, 1},
  1282. {"huge:textNode", XML_MAX_TEXT_LENGTH + CHUNK, XML_PARSE_HUGE, 0},
  1283. /* max length of a text node in content */
  1284. {"huge:attrNode", XML_MAX_TEXT_LENGTH - CHUNK, 0, 0},
  1285. {"huge:attrNode", XML_MAX_TEXT_LENGTH + CHUNK, 0, 1},
  1286. {"huge:attrNode", XML_MAX_TEXT_LENGTH + CHUNK, XML_PARSE_HUGE, 0},
  1287. /* max length of a comment node */
  1288. {"huge:commentNode", XML_MAX_TEXT_LENGTH - CHUNK, 0, 0},
  1289. {"huge:commentNode", XML_MAX_TEXT_LENGTH + CHUNK, 0, 1},
  1290. {"huge:commentNode", XML_MAX_TEXT_LENGTH + CHUNK, XML_PARSE_HUGE, 0},
  1291. /* max length of a PI node */
  1292. {"huge:piNode", XML_MAX_TEXT_LENGTH - CHUNK, 0, 0},
  1293. {"huge:piNode", XML_MAX_TEXT_LENGTH + CHUNK, 0, 1},
  1294. {"huge:piNode", XML_MAX_TEXT_LENGTH + CHUNK, XML_PARSE_HUGE, 0},
  1295. };
  1296. typedef struct testDesc testDesc;
  1297. typedef testDesc *testDescPtr;
  1298. struct testDesc {
  1299. const char *desc; /* description of the test */
  1300. functest func; /* function implementing the test */
  1301. };
  1302. static
  1303. testDesc testDescriptions[] = {
  1304. { "Parsing of huge files with the sax parser", saxTest},
  1305. /* { "Parsing of huge files with the tree parser", treeTest}, */
  1306. #ifdef LIBXML_READER_ENABLED
  1307. { "Parsing of huge files with the reader", readerTest},
  1308. #endif
  1309. {NULL, NULL}
  1310. };
  1311. typedef struct testException testException;
  1312. typedef testException *testExceptionPtr;
  1313. struct testException {
  1314. unsigned int test; /* the parser test number */
  1315. unsigned int limit; /* the limit test number */
  1316. int fail; /* new fail value or -1*/
  1317. size_t size; /* new limit value or 0 */
  1318. };
  1319. static
  1320. testException testExceptions[] = {
  1321. /* the SAX parser doesn't hit a limit of XML_MAX_TEXT_LENGTH text nodes */
  1322. { 0, 1, 0, 0},
  1323. };
  1324. static int
  1325. launchTests(testDescPtr tst, unsigned int test) {
  1326. int res = 0, err = 0;
  1327. unsigned int i, j;
  1328. size_t limit;
  1329. int fail;
  1330. if (tst == NULL) return(-1);
  1331. for (i = 0;i < sizeof(limitDescriptions)/sizeof(limitDescriptions[0]);i++) {
  1332. limit = limitDescriptions[i].limit;
  1333. fail = limitDescriptions[i].fail;
  1334. /*
  1335. * Handle exceptions if any
  1336. */
  1337. for (j = 0;j < sizeof(testExceptions)/sizeof(testExceptions[0]);j++) {
  1338. if ((testExceptions[j].test == test) &&
  1339. (testExceptions[j].limit == i)) {
  1340. if (testExceptions[j].fail != -1)
  1341. fail = testExceptions[j].fail;
  1342. if (testExceptions[j].size != 0)
  1343. limit = testExceptions[j].size;
  1344. break;
  1345. }
  1346. }
  1347. res = tst->func(limitDescriptions[i].name, limit,
  1348. limitDescriptions[i].options, fail);
  1349. if (res != 0) {
  1350. nb_errors++;
  1351. err++;
  1352. }
  1353. }
  1354. return(err);
  1355. }
  1356. static int
  1357. runtest(unsigned int i) {
  1358. int ret = 0, res;
  1359. int old_errors, old_tests, old_leaks;
  1360. old_errors = nb_errors;
  1361. old_tests = nb_tests;
  1362. old_leaks = nb_leaks;
  1363. if ((tests_quiet == 0) && (testDescriptions[i].desc != NULL))
  1364. printf("## %s\n", testDescriptions[i].desc);
  1365. res = launchTests(&testDescriptions[i], i);
  1366. if (res != 0)
  1367. ret++;
  1368. if (verbose) {
  1369. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  1370. printf("Ran %d tests, no errors\n", nb_tests - old_tests);
  1371. else
  1372. printf("Ran %d tests, %d errors, %d leaks\n",
  1373. nb_tests - old_tests,
  1374. nb_errors - old_errors,
  1375. nb_leaks - old_leaks);
  1376. }
  1377. return(ret);
  1378. }
  1379. static int
  1380. launchCrazySAX(unsigned int test, int fail) {
  1381. int res = 0, err = 0;
  1382. crazy_indx = test;
  1383. res = saxTest("crazy::test", XML_MAX_LOOKUP_LIMIT - CHUNK, 0, fail);
  1384. if (res != 0) {
  1385. nb_errors++;
  1386. err++;
  1387. }
  1388. if (tests_quiet == 0)
  1389. fprintf(stderr, "%c", crazy[test]);
  1390. return(err);
  1391. }
  1392. #ifdef LIBXML_READER_ENABLED
  1393. static int
  1394. launchCrazy(unsigned int test, int fail) {
  1395. int res = 0, err = 0;
  1396. crazy_indx = test;
  1397. res = readerTest("crazy::test", XML_MAX_LOOKUP_LIMIT - CHUNK, 0, fail);
  1398. if (res != 0) {
  1399. nb_errors++;
  1400. err++;
  1401. }
  1402. if (tests_quiet == 0)
  1403. fprintf(stderr, "%c", crazy[test]);
  1404. return(err);
  1405. }
  1406. #endif
  1407. static int get_crazy_fail(int test) {
  1408. /*
  1409. * adding 1000000 of character 'a' leads to parser failure mostly
  1410. * everywhere except in those special spots. Need to be updated
  1411. * each time crazy is updated
  1412. */
  1413. int fail = 1;
  1414. if ((test == 44) || /* PI in Misc */
  1415. ((test >= 50) && (test <= 55)) || /* Comment in Misc */
  1416. (test == 79) || /* PI in DTD */
  1417. ((test >= 85) && (test <= 90)) || /* Comment in DTD */
  1418. (test == 154) || /* PI in Misc */
  1419. ((test >= 160) && (test <= 165)) || /* Comment in Misc */
  1420. ((test >= 178) && (test <= 181)) || /* attribute value */
  1421. (test == 183) || /* Text */
  1422. (test == 189) || /* PI in Content */
  1423. (test == 191) || /* Text */
  1424. ((test >= 195) && (test <= 200)) || /* Comment in Content */
  1425. ((test >= 203) && (test <= 206)) || /* Text */
  1426. (test == 215) || (test == 216) || /* in CDATA */
  1427. (test == 219) || /* Text */
  1428. (test == 231) || /* PI in Misc */
  1429. ((test >= 237) && (test <= 242))) /* Comment in Misc */
  1430. fail = 0;
  1431. return(fail);
  1432. }
  1433. static int
  1434. runcrazy(void) {
  1435. int ret = 0, res = 0;
  1436. int old_errors, old_tests, old_leaks;
  1437. unsigned int i;
  1438. old_errors = nb_errors;
  1439. old_tests = nb_tests;
  1440. old_leaks = nb_leaks;
  1441. #ifdef LIBXML_READER_ENABLED
  1442. if (tests_quiet == 0) {
  1443. printf("## Crazy tests on reader\n");
  1444. }
  1445. for (i = 0;i < strlen(crazy);i++) {
  1446. res += launchCrazy(i, get_crazy_fail(i));
  1447. if (res != 0)
  1448. ret++;
  1449. }
  1450. #endif
  1451. if (tests_quiet == 0) {
  1452. printf("\n## Crazy tests on SAX\n");
  1453. }
  1454. for (i = 0;i < strlen(crazy);i++) {
  1455. res += launchCrazySAX(i, get_crazy_fail(i));
  1456. if (res != 0)
  1457. ret++;
  1458. }
  1459. if (tests_quiet == 0)
  1460. fprintf(stderr, "\n");
  1461. if (verbose) {
  1462. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  1463. printf("Ran %d tests, no errors\n", nb_tests - old_tests);
  1464. else
  1465. printf("Ran %d tests, %d errors, %d leaks\n",
  1466. nb_tests - old_tests,
  1467. nb_errors - old_errors,
  1468. nb_leaks - old_leaks);
  1469. }
  1470. return(ret);
  1471. }
  1472. int
  1473. main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  1474. int i, a, ret = 0;
  1475. int subset = 0;
  1476. fillFilling();
  1477. initializeLibxml2();
  1478. for (a = 1; a < argc;a++) {
  1479. if (!strcmp(argv[a], "-v"))
  1480. verbose = 1;
  1481. else if (!strcmp(argv[a], "-quiet"))
  1482. tests_quiet = 1;
  1483. else if (!strcmp(argv[a], "-crazy"))
  1484. subset = 1;
  1485. }
  1486. if (subset == 0) {
  1487. for (i = 0; testDescriptions[i].func != NULL; i++) {
  1488. ret += runtest(i);
  1489. }
  1490. }
  1491. ret += runcrazy();
  1492. if ((nb_errors == 0) && (nb_leaks == 0)) {
  1493. ret = 0;
  1494. printf("Total %d tests, no errors\n",
  1495. nb_tests);
  1496. } else {
  1497. ret = 1;
  1498. printf("Total %d tests, %d errors, %d leaks\n",
  1499. nb_tests, nb_errors, nb_leaks);
  1500. }
  1501. xmlCleanupParser();
  1502. xmlMemoryDump();
  1503. return(ret);
  1504. }