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