2 * testrecurse.c: C program to run libxml2 regression tests checking entities
5 * To compile on Unixes:
6 * cc -o testrecurse `xml2-config --cflags` testrecurse.c `xml2-config --libs` -lpthread
8 * See Copyright for the status of this software.
19 #if !defined(_WIN32) || defined(__CYGWIN__)
23 #include <sys/types.h>
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
29 #include <libxml/uri.h>
30 #ifdef LIBXML_READER_ENABLED
31 #include <libxml/xmlreader.h>
35 * O_BINARY is just for Windows compatibility - if it isn't defined
36 * on this system, avoid any compilation error
39 #define RD_FLAGS O_RDONLY | O_BINARY
41 #define RD_FLAGS O_RDONLY
44 typedef int (*functest) (const char *filename, const char *result,
45 const char *error, int options);
47 typedef struct testDesc testDesc;
48 typedef testDesc *testDescPtr;
50 const char *desc; /* descripton of the test */
51 functest func; /* function implementing the test */
52 const char *in; /* glob to path for input files */
53 const char *out; /* output directory */
54 const char *suffix;/* suffix for output files */
55 const char *err; /* suffix for error output files */
56 int options; /* parser options for the test */
59 static int checkTestFile(const char *filename);
62 #if defined(_WIN32) && !defined(__CYGWIN__)
69 size_t gl_pathc; /* Count of paths matched so far */
70 char **gl_pathv; /* List of matched pathnames. */
71 size_t gl_offs; /* Slots to reserve in 'gl_pathv'. */
75 static int glob(const char *pattern, int flags,
76 int errfunc(const char *epath, int eerrno),
79 WIN32_FIND_DATA FindFileData;
81 unsigned int nb_paths = 0;
85 if ((pattern == NULL) || (pglob == NULL)) return(-1);
87 strncpy(directory, pattern, 499);
88 for (len = strlen(directory);len >= 0;len--) {
89 if (directory[len] == '/') {
100 memset(ret, 0, sizeof(glob_t));
102 hFind = FindFirstFileA(pattern, &FindFileData);
103 if (hFind == INVALID_HANDLE_VALUE)
106 ret->gl_pathv = (char **) malloc(nb_paths * sizeof(char *));
107 if (ret->gl_pathv == NULL) {
111 strncpy(directory + len, FindFileData.cFileName, 499 - len);
112 ret->gl_pathv[ret->gl_pathc] = strdup(directory);
113 if (ret->gl_pathv[ret->gl_pathc] == NULL)
116 while(FindNextFileA(hFind, &FindFileData)) {
117 if (FindFileData.cFileName[0] == '.')
119 if (ret->gl_pathc + 2 > nb_paths) {
120 char **tmp = realloc(ret->gl_pathv, nb_paths * 2 * sizeof(char *));
126 strncpy(directory + len, FindFileData.cFileName, 499 - len);
127 ret->gl_pathv[ret->gl_pathc] = strdup(directory);
128 if (ret->gl_pathv[ret->gl_pathc] == NULL)
132 ret->gl_pathv[ret->gl_pathc] = NULL;
141 static void globfree(glob_t *pglob) {
146 for (i = 0;i < pglob->gl_pathc;i++) {
147 if (pglob->gl_pathv[i] != NULL)
148 free(pglob->gl_pathv[i]);
152 #if !defined(__MINGW32__)
153 #define vsnprintf _vsnprintf
154 #define snprintf _snprintf
160 /************************************************************************
162 * Huge document generator *
164 ************************************************************************/
166 #include <libxml/xmlIO.h>
169 static const char *start = "<!DOCTYPE foo [\
170 <!ENTITY f 'some internal data'> \
171 <!ENTITY e '&f;&f;'> \
172 <!ENTITY d '&e;&e;'> \
176 static const char *segment = " <bar>&e; &f; &d;</bar>\n";
177 static const char *finish = "</foo>";
179 static int curseg = 0;
180 static const char *current;
185 * @URI: an URI to test
187 * Check for an huge: query
189 * Returns 1 if yes and 0 if another Input module should be used
192 hugeMatch(const char * URI) {
193 if ((URI != NULL) && (!strncmp(URI, "huge:", 4)))
200 * @URI: an URI to test
202 * Return a pointer to the huge: query handler, in this example simply
203 * the current pointer...
205 * Returns an Input context or NULL in case or error
208 hugeOpen(const char * URI) {
209 if ((URI == NULL) || (strncmp(URI, "huge:", 4)))
211 rlen = strlen(start);
213 return((void *) current);
218 * @context: the read context
220 * Close the huge: query handler
222 * Returns 0 or -1 in case of error
225 hugeClose(void * context) {
226 if (context == NULL) return(-1);
230 #define MAX_NODES 1000000
234 * @context: the read context
235 * @buffer: where to store data
236 * @len: number of bytes to read
238 * Implement an huge: query read.
240 * Returns the number of bytes read or -1 in case of error
243 hugeRead(void *context, char *buffer, int len)
245 if ((context == NULL) || (buffer == NULL) || (len < 0))
249 if (curseg >= MAX_NODES + 1) {
255 memcpy(buffer, current, len);
257 if (curseg == MAX_NODES) {
258 fprintf(stderr, "\n");
259 rlen = strlen(finish);
262 if (curseg % (MAX_NODES / 10) == 0)
263 fprintf(stderr, ".");
264 rlen = strlen(segment);
268 memcpy(buffer, current, len);
275 /************************************************************************
277 * Libxml2 specific routines *
279 ************************************************************************/
281 static int nb_tests = 0;
282 static int nb_errors = 0;
283 static int nb_leaks = 0;
284 static int extraMemoryFromResolver = 0;
288 fprintf(stderr, "Exitting tests on fatal error\n");
293 * We need to trap calls to the resolver to not account memory for the catalog
294 * which is shared to the current running test. We also don't want to have
295 * network downloads modifying tests.
297 static xmlParserInputPtr
298 testExternalEntityLoader(const char *URL, const char *ID,
299 xmlParserCtxtPtr ctxt) {
300 xmlParserInputPtr ret;
302 if (checkTestFile(URL)) {
303 ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
305 int memused = xmlMemUsed();
306 ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
307 extraMemoryFromResolver += xmlMemUsed() - memused;
314 * Trapping the error messages at the generic level to grab the equivalent of
315 * stderr messages on CLI tools.
317 static char testErrors[32769];
318 static int testErrorsSize = 0;
321 channel(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
325 if (testErrorsSize >= 32768)
328 res = vsnprintf(&testErrors[testErrorsSize],
329 32768 - testErrorsSize,
332 if (testErrorsSize + res >= 32768) {
334 testErrorsSize = 32768;
335 testErrors[testErrorsSize] = 0;
337 testErrorsSize += res;
339 testErrors[testErrorsSize] = 0;
343 * xmlParserPrintFileContext:
344 * @input: an xmlParserInputPtr input
346 * Displays current context within the input content for error tracking
350 xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
351 xmlGenericErrorFunc chanl, void *data ) {
352 const xmlChar *cur, *base;
353 unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
354 xmlChar content[81]; /* space for 80 chars + line terminator */
357 if (input == NULL) return;
360 /* skip backwards over any end-of-lines */
361 while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
365 /* search backwards for beginning-of-line (to max buff size) */
366 while ((n++ < (sizeof(content)-1)) && (cur > base) &&
367 (*(cur) != '\n') && (*(cur) != '\r'))
369 if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
370 /* calculate the error position in terms of the current position */
371 col = input->cur - cur;
372 /* search forward for end-of-line (to max buff size) */
375 /* copy selected text to our buffer */
376 while ((*cur != 0) && (*(cur) != '\n') &&
377 (*(cur) != '\r') && (n < sizeof(content)-1)) {
382 /* print out the selected text */
383 chanl(data ,"%s\n", content);
384 /* create blank line with problem pointer */
387 /* (leave buffer space for pointer + line terminator) */
388 while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
395 chanl(data ,"%s\n", content);
399 testStructuredErrorHandler(void *ctx ATTRIBUTE_UNUSED, xmlErrorPtr err) {
406 const xmlChar *name = NULL;
409 xmlParserInputPtr input = NULL;
410 xmlParserInputPtr cur = NULL;
411 xmlParserCtxtPtr ctxt = NULL;
419 domain = err->domain;
422 if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
423 (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
424 (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
429 if (code == XML_ERR_OK)
432 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
436 * Maintain the compatibility with the legacy error handling
440 if ((input != NULL) && (input->filename == NULL) &&
441 (ctxt->inputNr > 1)) {
443 input = ctxt->inputTab[ctxt->inputNr - 2];
447 channel(data, "%s:%d: ", input->filename, input->line);
448 else if ((line != 0) && (domain == XML_FROM_PARSER))
449 channel(data, "Entity: line %d: ", input->line);
453 channel(data, "%s:%d: ", file, line);
454 else if ((line != 0) && (domain == XML_FROM_PARSER))
455 channel(data, "Entity: line %d: ", line);
458 channel(data, "element %s: ", name);
460 if (code == XML_ERR_OK)
463 case XML_FROM_PARSER:
464 channel(data, "parser ");
466 case XML_FROM_NAMESPACE:
467 channel(data, "namespace ");
471 channel(data, "validity ");
474 channel(data, "HTML parser ");
476 case XML_FROM_MEMORY:
477 channel(data, "memory ");
479 case XML_FROM_OUTPUT:
480 channel(data, "output ");
483 channel(data, "I/O ");
485 case XML_FROM_XINCLUDE:
486 channel(data, "XInclude ");
489 channel(data, "XPath ");
491 case XML_FROM_XPOINTER:
492 channel(data, "parser ");
494 case XML_FROM_REGEXP:
495 channel(data, "regexp ");
497 case XML_FROM_MODULE:
498 channel(data, "module ");
500 case XML_FROM_SCHEMASV:
501 channel(data, "Schemas validity ");
503 case XML_FROM_SCHEMASP:
504 channel(data, "Schemas parser ");
506 case XML_FROM_RELAXNGP:
507 channel(data, "Relax-NG parser ");
509 case XML_FROM_RELAXNGV:
510 channel(data, "Relax-NG validity ");
512 case XML_FROM_CATALOG:
513 channel(data, "Catalog ");
516 channel(data, "C14N ");
519 channel(data, "XSLT ");
524 if (code == XML_ERR_OK)
530 case XML_ERR_WARNING:
531 channel(data, "warning : ");
534 channel(data, "error : ");
537 channel(data, "error : ");
540 if (code == XML_ERR_OK)
544 len = xmlStrlen((const xmlChar *)str);
545 if ((len > 0) && (str[len - 1] != '\n'))
546 channel(data, "%s\n", str);
548 channel(data, "%s", str);
550 channel(data, "%s\n", "out of memory error");
552 if (code == XML_ERR_OK)
556 xmlParserPrintFileContextInternal(input, channel, data);
559 channel(data, "%s:%d: \n", cur->filename, cur->line);
560 else if ((line != 0) && (domain == XML_FROM_PARSER))
561 channel(data, "Entity: line %d: \n", cur->line);
562 xmlParserPrintFileContextInternal(cur, channel, data);
565 if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
567 (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
571 channel(data, "%s\n", err->str1);
572 for (i=0;i < err->int1;i++)
576 channel(data, "%s\n", buf);
581 initializeLibxml2(void) {
582 xmlGetWarningsDefaultValue = 0;
583 xmlPedanticParserDefault(0);
585 xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
587 xmlSetExternalEntityLoader(testExternalEntityLoader);
588 xmlSetStructuredErrorFunc(NULL, testStructuredErrorHandler);
590 * register the new I/O handlers
592 if (xmlRegisterInputCallbacks(hugeMatch, hugeOpen,
593 hugeRead, hugeClose) < 0) {
594 fprintf(stderr, "failed to register Huge handler\n");
599 /************************************************************************
601 * File name and path utilities *
603 ************************************************************************/
605 static const char *baseFilename(const char *filename) {
607 if (filename == NULL)
609 cur = &filename[strlen(filename)];
610 while ((cur > filename) && (*cur != '/'))
617 static char *resultFilename(const char *filename, const char *out,
618 const char *suffix) {
621 char suffixbuff[500];
624 if ((filename[0] == 't') && (filename[1] == 'e') &&
625 (filename[2] == 's') && (filename[3] == 't') &&
626 (filename[4] == '/'))
627 filename = &filename[5];
630 base = baseFilename(filename);
636 strncpy(suffixbuff,suffix,499);
638 if(strstr(base,".") && suffixbuff[0]=='.')
642 snprintf(res, 499, "%s%s%s", out, base, suffixbuff);
647 static int checkTestFile(const char *filename) {
650 if (stat(filename, &buf) == -1)
653 #if defined(_WIN32) && !defined(__CYGWIN__)
654 if (!(buf.st_mode & _S_IFREG))
657 if (!S_ISREG(buf.st_mode))
666 /************************************************************************
668 * Test to detect or not recursive entities *
670 ************************************************************************/
672 * recursiveDetectTest:
673 * @filename: the file to parse
674 * @result: the file with expected result
675 * @err: the file with error messages: unused
677 * Parse a file loading DTD and replacing entities check it fails for
680 * Returns 0 in case of success, an error code otherwise
683 recursiveDetectTest(const char *filename,
684 const char *result ATTRIBUTE_UNUSED,
685 const char *err ATTRIBUTE_UNUSED,
686 int options ATTRIBUTE_UNUSED) {
688 xmlParserCtxtPtr ctxt;
694 ctxt = xmlNewParserCtxt();
697 * base of the test, parse with the old API
699 doc = xmlCtxtReadFile(ctxt, filename, NULL,
700 XML_PARSE_NOENT | XML_PARSE_DTDLOAD);
701 if ((doc != NULL) || (ctxt->lastError.code != XML_ERR_ENTITY_LOOP)) {
702 fprintf(stderr, "Failed to detect recursion in %s\n", filename);
703 xmlFreeParserCtxt(ctxt);
707 xmlFreeParserCtxt(ctxt);
713 * notRecursiveDetectTest:
714 * @filename: the file to parse
715 * @result: the file with expected result
716 * @err: the file with error messages: unused
718 * Parse a file loading DTD and replacing entities check it works for
721 * Returns 0 in case of success, an error code otherwise
724 notRecursiveDetectTest(const char *filename,
725 const char *result ATTRIBUTE_UNUSED,
726 const char *err ATTRIBUTE_UNUSED,
727 int options ATTRIBUTE_UNUSED) {
729 xmlParserCtxtPtr ctxt;
735 ctxt = xmlNewParserCtxt();
738 * base of the test, parse with the old API
740 doc = xmlCtxtReadFile(ctxt, filename, NULL,
741 XML_PARSE_NOENT | XML_PARSE_DTDLOAD);
743 fprintf(stderr, "Failed to parse correct file %s\n", filename);
744 xmlFreeParserCtxt(ctxt);
748 xmlFreeParserCtxt(ctxt);
753 #ifdef LIBXML_READER_ENABLED
755 * notRecursiveHugeTest:
756 * @filename: the file to parse
757 * @result: the file with expected result
758 * @err: the file with error messages: unused
760 * Parse a memory generated file
763 * Returns 0 in case of success, an error code otherwise
766 notRecursiveHugeTest(const char *filename ATTRIBUTE_UNUSED,
767 const char *result ATTRIBUTE_UNUSED,
768 const char *err ATTRIBUTE_UNUSED,
769 int options ATTRIBUTE_UNUSED) {
770 xmlTextReaderPtr reader;
776 reader = xmlReaderForFile("huge:test" , NULL,
777 XML_PARSE_NOENT | XML_PARSE_DTDLOAD);
778 if (reader == NULL) {
779 fprintf(stderr, "Failed to open huge:test\n");
782 ret = xmlTextReaderRead(reader);
784 ret = xmlTextReaderRead(reader);
787 fprintf(stderr, "Failed to parser huge:test with entities\n");
790 xmlFreeTextReader(reader);
796 /************************************************************************
798 * Tests Descriptions *
800 ************************************************************************/
803 testDesc testDescriptions[] = {
804 { "Parsing recursive test cases" ,
805 recursiveDetectTest, "./test/recurse/lol*.xml", NULL, NULL, NULL,
807 { "Parsing non-recursive test cases" ,
808 notRecursiveDetectTest, "./test/recurse/good*.xml", NULL, NULL, NULL,
810 #ifdef LIBXML_READER_ENABLED
811 { "Parsing non-recursive huge case" ,
812 notRecursiveHugeTest, NULL, NULL, NULL, NULL,
815 {NULL, NULL, NULL, NULL, NULL, NULL, 0}
818 /************************************************************************
820 * The main code driving the tests *
822 ************************************************************************/
825 launchTests(testDescPtr tst) {
826 int res = 0, err = 0;
832 if (tst == NULL) return(-1);
833 if (tst->in != NULL) {
837 glob(tst->in, GLOB_DOOFFS, NULL, &globbuf);
838 for (i = 0;i < globbuf.gl_pathc;i++) {
839 if (!checkTestFile(globbuf.gl_pathv[i]))
841 if (tst->suffix != NULL) {
842 result = resultFilename(globbuf.gl_pathv[i], tst->out,
844 if (result == NULL) {
845 fprintf(stderr, "Out of memory !\n");
851 if (tst->err != NULL) {
852 error = resultFilename(globbuf.gl_pathv[i], tst->out,
855 fprintf(stderr, "Out of memory !\n");
861 if ((result) &&(!checkTestFile(result))) {
862 fprintf(stderr, "Missing result file %s\n", result);
863 } else if ((error) &&(!checkTestFile(error))) {
864 fprintf(stderr, "Missing error file %s\n", error);
867 extraMemoryFromResolver = 0;
870 res = tst->func(globbuf.gl_pathv[i], result, error,
871 tst->options | XML_PARSE_COMPACT);
874 fprintf(stderr, "File %s generated an error\n",
875 globbuf.gl_pathv[i]);
879 else if (xmlMemUsed() != mem) {
880 if ((xmlMemUsed() != mem) &&
881 (extraMemoryFromResolver == 0)) {
882 fprintf(stderr, "File %s leaked %d bytes\n",
883 globbuf.gl_pathv[i], xmlMemUsed() - mem);
899 extraMemoryFromResolver = 0;
900 res = tst->func(NULL, NULL, NULL, tst->options);
909 static int verbose = 0;
910 static int tests_quiet = 0;
915 int old_errors, old_tests, old_leaks;
917 old_errors = nb_errors;
918 old_tests = nb_tests;
919 old_leaks = nb_leaks;
920 if ((tests_quiet == 0) && (testDescriptions[i].desc != NULL))
921 printf("## %s\n", testDescriptions[i].desc);
922 res = launchTests(&testDescriptions[i]);
926 if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
927 printf("Ran %d tests, no errors\n", nb_tests - old_tests);
929 printf("Ran %d tests, %d errors, %d leaks\n",
930 nb_tests - old_tests,
931 nb_errors - old_errors,
932 nb_leaks - old_leaks);
938 main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
944 for (a = 1; a < argc;a++) {
945 if (!strcmp(argv[a], "-v"))
947 else if (!strcmp(argv[a], "-quiet"))
950 for (i = 0; testDescriptions[i].func != NULL; i++) {
951 if (strstr(testDescriptions[i].desc, argv[a])) {
959 for (i = 0; testDescriptions[i].func != NULL; i++) {
963 if ((nb_errors == 0) && (nb_leaks == 0)) {
965 printf("Total %d tests, no errors\n",
969 printf("Total %d tests, %d errors, %d leaks\n",
970 nb_tests, nb_errors, nb_leaks);