testlimits.c 41 KB

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