Blame testrecurse.c

Packit 423ecb
/*
Packit 423ecb
 * testrecurse.c: C program to run libxml2 regression tests checking entities
Packit 423ecb
 *            recursions
Packit 423ecb
 *
Packit 423ecb
 * To compile on Unixes:
Packit 423ecb
 * cc -o testrecurse `xml2-config --cflags` testrecurse.c `xml2-config --libs` -lpthread
Packit 423ecb
 *
Packit 423ecb
 * See Copyright for the status of this software.
Packit 423ecb
 *
Packit 423ecb
 * daniel@veillard.com
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
#include "libxml.h"
Packit 423ecb
#include <stdio.h>
Packit 423ecb
Packit 423ecb
#if !defined(_WIN32) || defined(__CYGWIN__)
Packit 423ecb
#include <unistd.h>
Packit 423ecb
#endif
Packit 423ecb
#include <string.h>
Packit 423ecb
#include <sys/types.h>
Packit 423ecb
#include <sys/stat.h>
Packit 423ecb
#include <fcntl.h>
Packit 423ecb
Packit 423ecb
#include <libxml/parser.h>
Packit 423ecb
#include <libxml/tree.h>
Packit 423ecb
#include <libxml/uri.h>
Packit 423ecb
#ifdef LIBXML_READER_ENABLED
Packit 423ecb
#include <libxml/xmlreader.h>
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * O_BINARY is just for Windows compatibility - if it isn't defined
Packit 423ecb
 * on this system, avoid any compilation error
Packit 423ecb
 */
Packit 423ecb
#ifdef	O_BINARY
Packit 423ecb
#define RD_FLAGS	O_RDONLY | O_BINARY
Packit 423ecb
#else
Packit 423ecb
#define	RD_FLAGS	O_RDONLY
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
typedef int (*functest) (const char *filename, const char *result,
Packit 423ecb
                         const char *error, int options);
Packit 423ecb
Packit 423ecb
typedef struct testDesc testDesc;
Packit 423ecb
typedef testDesc *testDescPtr;
Packit 423ecb
struct testDesc {
Packit 423ecb
    const char *desc; /* descripton of the test */
Packit 423ecb
    functest    func; /* function implementing the test */
Packit 423ecb
    const char *in;   /* glob to path for input files */
Packit 423ecb
    const char *out;  /* output directory */
Packit 423ecb
    const char *suffix;/* suffix for output files */
Packit 423ecb
    const char *err;  /* suffix for error output files */
Packit 423ecb
    int     options;  /* parser options for the test */
Packit 423ecb
};
Packit 423ecb
Packit 423ecb
static int checkTestFile(const char *filename);
Packit 423ecb
Packit 423ecb
Packit 423ecb
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit 423ecb
Packit 423ecb
#include <windows.h>
Packit 423ecb
#include <io.h>
Packit 423ecb
Packit 423ecb
typedef struct
Packit 423ecb
{
Packit 423ecb
      size_t gl_pathc;    /* Count of paths matched so far  */
Packit 423ecb
      char **gl_pathv;    /* List of matched pathnames.  */
Packit 423ecb
      size_t gl_offs;     /* Slots to reserve in 'gl_pathv'.  */
Packit 423ecb
} glob_t;
Packit 423ecb
Packit 423ecb
#define GLOB_DOOFFS 0
Packit 423ecb
static int glob(const char *pattern, ATTRIBUTE_UNUSED int flags,
Packit 423ecb
                ATTRIBUTE_UNUSED int errfunc(const char *epath, int eerrno),
Packit 423ecb
                glob_t *pglob) {
Packit 423ecb
    glob_t *ret;
Packit 423ecb
    WIN32_FIND_DATA FindFileData;
Packit 423ecb
    HANDLE hFind;
Packit 423ecb
    unsigned int nb_paths = 0;
Packit 423ecb
    char directory[500];
Packit 423ecb
    int len;
Packit 423ecb
Packit 423ecb
    if ((pattern == NULL) || (pglob == NULL)) return(-1);
Packit 423ecb
Packit 423ecb
    strncpy(directory, pattern, 499);
Packit 423ecb
    for (len = strlen(directory);len >= 0;len--) {
Packit 423ecb
        if (directory[len] == '/') {
Packit 423ecb
	    len++;
Packit 423ecb
	    directory[len] = 0;
Packit 423ecb
	    break;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    if (len <= 0)
Packit 423ecb
        len = 0;
Packit 423ecb
Packit 423ecb
Packit 423ecb
    ret = pglob;
Packit 423ecb
    memset(ret, 0, sizeof(glob_t));
Packit 423ecb
Packit 423ecb
    hFind = FindFirstFileA(pattern, &FindFileData);
Packit 423ecb
    if (hFind == INVALID_HANDLE_VALUE)
Packit 423ecb
        return(0);
Packit 423ecb
    nb_paths = 20;
Packit 423ecb
    ret->gl_pathv = (char **) malloc(nb_paths * sizeof(char *));
Packit 423ecb
    if (ret->gl_pathv == NULL) {
Packit 423ecb
	FindClose(hFind);
Packit 423ecb
        return(-1);
Packit 423ecb
    }
Packit 423ecb
    strncpy(directory + len, FindFileData.cFileName, 499 - len);
Packit 423ecb
    ret->gl_pathv[ret->gl_pathc] = strdup(directory);
Packit 423ecb
    if (ret->gl_pathv[ret->gl_pathc] == NULL)
Packit 423ecb
        goto done;
Packit 423ecb
    ret->gl_pathc++;
Packit 423ecb
    while(FindNextFileA(hFind, &FindFileData)) {
Packit 423ecb
        if (FindFileData.cFileName[0] == '.')
Packit 423ecb
	    continue;
Packit 423ecb
        if (ret->gl_pathc + 2 > nb_paths) {
Packit 423ecb
            char **tmp = realloc(ret->gl_pathv, nb_paths * 2 * sizeof(char *));
Packit 423ecb
            if (tmp == NULL)
Packit 423ecb
                break;
Packit 423ecb
            ret->gl_pathv = tmp;
Packit 423ecb
            nb_paths *= 2;
Packit 423ecb
	}
Packit 423ecb
	strncpy(directory + len, FindFileData.cFileName, 499 - len);
Packit 423ecb
	ret->gl_pathv[ret->gl_pathc] = strdup(directory);
Packit 423ecb
        if (ret->gl_pathv[ret->gl_pathc] == NULL)
Packit 423ecb
            break;
Packit 423ecb
        ret->gl_pathc++;
Packit 423ecb
    }
Packit 423ecb
    ret->gl_pathv[ret->gl_pathc] = NULL;
Packit 423ecb
Packit 423ecb
done:
Packit 423ecb
    FindClose(hFind);
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
Packit 423ecb
static void globfree(glob_t *pglob) {
Packit 423ecb
    unsigned int i;
Packit 423ecb
    if (pglob == NULL)
Packit 423ecb
        return;
Packit 423ecb
Packit 423ecb
    for (i = 0;i < pglob->gl_pathc;i++) {
Packit 423ecb
         if (pglob->gl_pathv[i] != NULL)
Packit 423ecb
             free(pglob->gl_pathv[i]);
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#else
Packit 423ecb
#include <glob.h>
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Huge document generator					*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
#include <libxml/xmlIO.h>
Packit 423ecb
Packit 423ecb
Packit 423ecb
static const char *start = "
Packit 423ecb
 \
Packit 423ecb
 \
Packit 423ecb
 \
Packit 423ecb
]> \
Packit 423ecb
<foo>";
Packit 423ecb
Packit 423ecb
static const char *segment = "  <bar>&e; &f; &d;</bar>\n";
Packit 423ecb
static const char *finish = "</foo>";
Packit 423ecb
Packit 423ecb
static int curseg = 0;
Packit 423ecb
static const char *current;
Packit 423ecb
static int rlen;
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * hugeMatch:
Packit 423ecb
 * @URI: an URI to test
Packit 423ecb
 *
Packit 423ecb
 * Check for an huge: query
Packit 423ecb
 *
Packit 423ecb
 * Returns 1 if yes and 0 if another Input module should be used
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
hugeMatch(const char * URI) {
Packit 423ecb
    if ((URI != NULL) && (!strncmp(URI, "huge:", 4)))
Packit 423ecb
        return(1);
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * hugeOpen:
Packit 423ecb
 * @URI: an URI to test
Packit 423ecb
 *
Packit 423ecb
 * Return a pointer to the huge: query handler, in this example simply
Packit 423ecb
 * the current pointer...
Packit 423ecb
 *
Packit 423ecb
 * Returns an Input context or NULL in case or error
Packit 423ecb
 */
Packit 423ecb
static void *
Packit 423ecb
hugeOpen(const char * URI) {
Packit 423ecb
    if ((URI == NULL) || (strncmp(URI, "huge:", 4)))
Packit 423ecb
        return(NULL);
Packit 423ecb
    rlen = strlen(start);
Packit 423ecb
    current = start;
Packit 423ecb
    return((void *) current);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * hugeClose:
Packit 423ecb
 * @context: the read context
Packit 423ecb
 *
Packit 423ecb
 * Close the huge: query handler
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
hugeClose(void * context) {
Packit 423ecb
    if (context == NULL) return(-1);
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#define MAX_NODES 1000000
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * hugeRead:
Packit 423ecb
 * @context: the read context
Packit 423ecb
 * @buffer: where to store data
Packit 423ecb
 * @len: number of bytes to read
Packit 423ecb
 *
Packit 423ecb
 * Implement an huge: query read.
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of bytes read or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
hugeRead(void *context, char *buffer, int len)
Packit 423ecb
{
Packit 423ecb
    if ((context == NULL) || (buffer == NULL) || (len < 0))
Packit 423ecb
        return (-1);
Packit 423ecb
Packit 423ecb
    if (len >= rlen) {
Packit 423ecb
        if (curseg >= MAX_NODES + 1) {
Packit 423ecb
            rlen = 0;
Packit 423ecb
            return(0);
Packit 423ecb
        }
Packit 423ecb
        len = rlen;
Packit 423ecb
        rlen = 0;
Packit 423ecb
	memcpy(buffer, current, len);
Packit 423ecb
        curseg ++;
Packit 423ecb
        if (curseg == MAX_NODES) {
Packit 423ecb
	    fprintf(stderr, "\n");
Packit 423ecb
            rlen = strlen(finish);
Packit 423ecb
            current = finish;
Packit 423ecb
	} else {
Packit 423ecb
	    if (curseg % (MAX_NODES / 10) == 0)
Packit 423ecb
	        fprintf(stderr, ".");
Packit 423ecb
            rlen = strlen(segment);
Packit 423ecb
            current = segment;
Packit 423ecb
	}
Packit 423ecb
    } else {
Packit 423ecb
	memcpy(buffer, current, len);
Packit 423ecb
	rlen -= len;
Packit 423ecb
        current += len;
Packit 423ecb
    }
Packit 423ecb
    return (len);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Libxml2 specific routines				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
static int nb_tests = 0;
Packit 423ecb
static int nb_errors = 0;
Packit 423ecb
static int nb_leaks = 0;
Packit 423ecb
static int extraMemoryFromResolver = 0;
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
fatalError(void) {
Packit 423ecb
    fprintf(stderr, "Exitting tests on fatal error\n");
Packit 423ecb
    exit(1);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * We need to trap calls to the resolver to not account memory for the catalog
Packit 423ecb
 * which is shared to the current running test. We also don't want to have
Packit 423ecb
 * network downloads modifying tests.
Packit 423ecb
 */
Packit 423ecb
static xmlParserInputPtr
Packit 423ecb
testExternalEntityLoader(const char *URL, const char *ID,
Packit 423ecb
			 xmlParserCtxtPtr ctxt) {
Packit 423ecb
    xmlParserInputPtr ret;
Packit 423ecb
Packit 423ecb
    if (checkTestFile(URL)) {
Packit 423ecb
	ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
Packit 423ecb
    } else {
Packit 423ecb
	int memused = xmlMemUsed();
Packit 423ecb
	ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
Packit 423ecb
	extraMemoryFromResolver += xmlMemUsed() - memused;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * Trapping the error messages at the generic level to grab the equivalent of
Packit 423ecb
 * stderr messages on CLI tools.
Packit 423ecb
 */
Packit 423ecb
static char testErrors[32769];
Packit 423ecb
static int testErrorsSize = 0;
Packit 423ecb
Packit 423ecb
static void XMLCDECL
Packit 423ecb
channel(void *ctx  ATTRIBUTE_UNUSED, const char *msg, ...) {
Packit 423ecb
    va_list args;
Packit 423ecb
    int res;
Packit 423ecb
Packit 423ecb
    if (testErrorsSize >= 32768)
Packit 423ecb
        return;
Packit 423ecb
    va_start(args, msg);
Packit 423ecb
    res = vsnprintf(&testErrors[testErrorsSize],
Packit 423ecb
                    32768 - testErrorsSize,
Packit 423ecb
		    msg, args);
Packit 423ecb
    va_end(args);
Packit 423ecb
    if (testErrorsSize + res >= 32768) {
Packit 423ecb
        /* buffer is full */
Packit 423ecb
	testErrorsSize = 32768;
Packit 423ecb
	testErrors[testErrorsSize] = 0;
Packit 423ecb
    } else {
Packit 423ecb
        testErrorsSize += res;
Packit 423ecb
    }
Packit 423ecb
    testErrors[testErrorsSize] = 0;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlParserPrintFileContext:
Packit 423ecb
 * @input:  an xmlParserInputPtr input
Packit 423ecb
 *
Packit 423ecb
 * Displays current context within the input content for error tracking
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
Packit 423ecb
		xmlGenericErrorFunc chanl, void *data ) {
Packit 423ecb
    const xmlChar *cur, *base;
Packit 423ecb
    unsigned int n, col;	/* GCC warns if signed, because compared with sizeof() */
Packit 423ecb
    xmlChar  content[81]; /* space for 80 chars + line terminator */
Packit 423ecb
    xmlChar *ctnt;
Packit 423ecb
Packit 423ecb
    if (input == NULL) return;
Packit 423ecb
    cur = input->cur;
Packit 423ecb
    base = input->base;
Packit 423ecb
    /* skip backwards over any end-of-lines */
Packit 423ecb
    while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
Packit 423ecb
	cur--;
Packit 423ecb
    }
Packit 423ecb
    n = 0;
Packit 423ecb
    /* search backwards for beginning-of-line (to max buff size) */
Packit 423ecb
    while ((n++ < (sizeof(content)-1)) && (cur > base) &&
Packit 423ecb
   (*(cur) != '\n') && (*(cur) != '\r'))
Packit 423ecb
        cur--;
Packit 423ecb
    if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
Packit 423ecb
    /* calculate the error position in terms of the current position */
Packit 423ecb
    col = input->cur - cur;
Packit 423ecb
    /* search forward for end-of-line (to max buff size) */
Packit 423ecb
    n = 0;
Packit 423ecb
    ctnt = content;
Packit 423ecb
    /* copy selected text to our buffer */
Packit 423ecb
    while ((*cur != 0) && (*(cur) != '\n') &&
Packit 423ecb
   (*(cur) != '\r') && (n < sizeof(content)-1)) {
Packit 423ecb
		*ctnt++ = *cur++;
Packit 423ecb
	n++;
Packit 423ecb
    }
Packit 423ecb
    *ctnt = 0;
Packit 423ecb
    /* print out the selected text */
Packit 423ecb
    chanl(data ,"%s\n", content);
Packit 423ecb
    /* create blank line with problem pointer */
Packit 423ecb
    n = 0;
Packit 423ecb
    ctnt = content;
Packit 423ecb
    /* (leave buffer space for pointer + line terminator) */
Packit 423ecb
    while ((n
Packit 423ecb
	if (*(ctnt) != '\t')
Packit 423ecb
	    *(ctnt) = ' ';
Packit 423ecb
	ctnt++;
Packit 423ecb
    }
Packit 423ecb
    *ctnt++ = '^';
Packit 423ecb
    *ctnt = 0;
Packit 423ecb
    chanl(data ,"%s\n", content);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
testStructuredErrorHandler(void *ctx  ATTRIBUTE_UNUSED, xmlErrorPtr err) {
Packit 423ecb
    char *file = NULL;
Packit 423ecb
    int line = 0;
Packit 423ecb
    int code = -1;
Packit 423ecb
    int domain;
Packit 423ecb
    void *data = NULL;
Packit 423ecb
    const char *str;
Packit 423ecb
    const xmlChar *name = NULL;
Packit 423ecb
    xmlNodePtr node;
Packit 423ecb
    xmlErrorLevel level;
Packit 423ecb
    xmlParserInputPtr input = NULL;
Packit 423ecb
    xmlParserInputPtr cur = NULL;
Packit 423ecb
    xmlParserCtxtPtr ctxt = NULL;
Packit 423ecb
Packit 423ecb
    if (err == NULL)
Packit 423ecb
        return;
Packit 423ecb
Packit 423ecb
    file = err->file;
Packit 423ecb
    line = err->line;
Packit 423ecb
    code = err->code;
Packit 423ecb
    domain = err->domain;
Packit 423ecb
    level = err->level;
Packit 423ecb
    node = err->node;
Packit 423ecb
    if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
Packit 423ecb
        (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
Packit 423ecb
	(domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
Packit 423ecb
	ctxt = err->ctxt;
Packit 423ecb
    }
Packit 423ecb
    str = err->message;
Packit 423ecb
Packit 423ecb
    if (code == XML_ERR_OK)
Packit 423ecb
        return;
Packit 423ecb
Packit 423ecb
    if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
Packit 423ecb
        name = node->name;
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Maintain the compatibility with the legacy error handling
Packit 423ecb
     */
Packit 423ecb
    if (ctxt != NULL) {
Packit 423ecb
        input = ctxt->input;
Packit 423ecb
        if ((input != NULL) && (input->filename == NULL) &&
Packit 423ecb
            (ctxt->inputNr > 1)) {
Packit 423ecb
            cur = input;
Packit 423ecb
            input = ctxt->inputTab[ctxt->inputNr - 2];
Packit 423ecb
        }
Packit 423ecb
        if (input != NULL) {
Packit 423ecb
            if (input->filename)
Packit 423ecb
                channel(data, "%s:%d: ", input->filename, input->line);
Packit 423ecb
            else if ((line != 0) && (domain == XML_FROM_PARSER))
Packit 423ecb
                channel(data, "Entity: line %d: ", input->line);
Packit 423ecb
        }
Packit 423ecb
    } else {
Packit 423ecb
        if (file != NULL)
Packit 423ecb
            channel(data, "%s:%d: ", file, line);
Packit 423ecb
        else if ((line != 0) && (domain == XML_FROM_PARSER))
Packit 423ecb
            channel(data, "Entity: line %d: ", line);
Packit 423ecb
    }
Packit 423ecb
    if (name != NULL) {
Packit 423ecb
        channel(data, "element %s: ", name);
Packit 423ecb
    }
Packit 423ecb
    if (code == XML_ERR_OK)
Packit 423ecb
        return;
Packit 423ecb
    switch (domain) {
Packit 423ecb
        case XML_FROM_PARSER:
Packit 423ecb
            channel(data, "parser ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_NAMESPACE:
Packit 423ecb
            channel(data, "namespace ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_DTD:
Packit 423ecb
        case XML_FROM_VALID:
Packit 423ecb
            channel(data, "validity ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_HTML:
Packit 423ecb
            channel(data, "HTML parser ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_MEMORY:
Packit 423ecb
            channel(data, "memory ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_OUTPUT:
Packit 423ecb
            channel(data, "output ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_IO:
Packit 423ecb
            channel(data, "I/O ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_XINCLUDE:
Packit 423ecb
            channel(data, "XInclude ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_XPATH:
Packit 423ecb
            channel(data, "XPath ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_XPOINTER:
Packit 423ecb
            channel(data, "parser ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_REGEXP:
Packit 423ecb
            channel(data, "regexp ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_MODULE:
Packit 423ecb
            channel(data, "module ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_SCHEMASV:
Packit 423ecb
            channel(data, "Schemas validity ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_SCHEMASP:
Packit 423ecb
            channel(data, "Schemas parser ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_RELAXNGP:
Packit 423ecb
            channel(data, "Relax-NG parser ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_RELAXNGV:
Packit 423ecb
            channel(data, "Relax-NG validity ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_CATALOG:
Packit 423ecb
            channel(data, "Catalog ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_C14N:
Packit 423ecb
            channel(data, "C14N ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_FROM_XSLT:
Packit 423ecb
            channel(data, "XSLT ");
Packit 423ecb
            break;
Packit 423ecb
        default:
Packit 423ecb
            break;
Packit 423ecb
    }
Packit 423ecb
    if (code == XML_ERR_OK)
Packit 423ecb
        return;
Packit 423ecb
    switch (level) {
Packit 423ecb
        case XML_ERR_NONE:
Packit 423ecb
            channel(data, ": ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_ERR_WARNING:
Packit 423ecb
            channel(data, "warning : ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_ERR_ERROR:
Packit 423ecb
            channel(data, "error : ");
Packit 423ecb
            break;
Packit 423ecb
        case XML_ERR_FATAL:
Packit 423ecb
            channel(data, "error : ");
Packit 423ecb
            break;
Packit 423ecb
    }
Packit 423ecb
    if (code == XML_ERR_OK)
Packit 423ecb
        return;
Packit 423ecb
    if (str != NULL) {
Packit 423ecb
        int len;
Packit 423ecb
	len = xmlStrlen((const xmlChar *)str);
Packit 423ecb
	if ((len > 0) && (str[len - 1] != '\n'))
Packit 423ecb
	    channel(data, "%s\n", str);
Packit 423ecb
	else
Packit 423ecb
	    channel(data, "%s", str);
Packit 423ecb
    } else {
Packit 423ecb
        channel(data, "%s\n", "out of memory error");
Packit 423ecb
    }
Packit 423ecb
    if (code == XML_ERR_OK)
Packit 423ecb
        return;
Packit 423ecb
Packit 423ecb
    if (ctxt != NULL) {
Packit 423ecb
        xmlParserPrintFileContextInternal(input, channel, data);
Packit 423ecb
        if (cur != NULL) {
Packit 423ecb
            if (cur->filename)
Packit 423ecb
                channel(data, "%s:%d: \n", cur->filename, cur->line);
Packit 423ecb
            else if ((line != 0) && (domain == XML_FROM_PARSER))
Packit 423ecb
                channel(data, "Entity: line %d: \n", cur->line);
Packit 423ecb
            xmlParserPrintFileContextInternal(cur, channel, data);
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
    if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
Packit 423ecb
        (err->int1 < 100) &&
Packit 423ecb
	(err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
Packit 423ecb
	xmlChar buf[150];
Packit 423ecb
	int i;
Packit 423ecb
Packit 423ecb
	channel(data, "%s\n", err->str1);
Packit 423ecb
	for (i=0;i < err->int1;i++)
Packit 423ecb
	     buf[i] = ' ';
Packit 423ecb
	buf[i++] = '^';
Packit 423ecb
	buf[i] = 0;
Packit 423ecb
	channel(data, "%s\n", buf);
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
initializeLibxml2(void) {
Packit 423ecb
    xmlGetWarningsDefaultValue = 0;
Packit 423ecb
    xmlPedanticParserDefault(0);
Packit 423ecb
Packit 423ecb
    xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
Packit 423ecb
    xmlInitParser();
Packit 423ecb
    xmlSetExternalEntityLoader(testExternalEntityLoader);
Packit 423ecb
    xmlSetStructuredErrorFunc(NULL, testStructuredErrorHandler);
Packit 423ecb
    /*
Packit 423ecb
     * register the new I/O handlers
Packit 423ecb
     */
Packit 423ecb
    if (xmlRegisterInputCallbacks(hugeMatch, hugeOpen,
Packit 423ecb
                                  hugeRead, hugeClose) < 0) {
Packit 423ecb
        fprintf(stderr, "failed to register Huge handler\n");
Packit 423ecb
	exit(1);
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		File name and path utilities				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
static const char *baseFilename(const char *filename) {
Packit 423ecb
    const char *cur;
Packit 423ecb
    if (filename == NULL)
Packit 423ecb
        return(NULL);
Packit 423ecb
    cur = &filename[strlen(filename)];
Packit 423ecb
    while ((cur > filename) && (*cur != '/'))
Packit 423ecb
        cur--;
Packit 423ecb
    if (*cur == '/')
Packit 423ecb
        return(cur + 1);
Packit 423ecb
    return(cur);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static char *resultFilename(const char *filename, const char *out,
Packit 423ecb
                            const char *suffix) {
Packit 423ecb
    const char *base;
Packit 423ecb
    char res[500];
Packit 423ecb
    char suffixbuff[500];
Packit 423ecb
Packit 423ecb
/*************
Packit 423ecb
    if ((filename[0] == 't') && (filename[1] == 'e') &&
Packit 423ecb
        (filename[2] == 's') && (filename[3] == 't') &&
Packit 423ecb
	(filename[4] == '/'))
Packit 423ecb
	filename = &filename[5];
Packit 423ecb
 *************/
Packit 423ecb
Packit 423ecb
    base = baseFilename(filename);
Packit 423ecb
    if (suffix == NULL)
Packit 423ecb
        suffix = ".tmp";
Packit 423ecb
    if (out == NULL)
Packit 423ecb
        out = "";
Packit 423ecb
Packit 423ecb
    strncpy(suffixbuff,suffix,499);
Packit 423ecb
#ifdef VMS
Packit 423ecb
    if(strstr(base,".") && suffixbuff[0]=='.')
Packit 423ecb
      suffixbuff[0]='_';
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    snprintf(res, 499, "%s%s%s", out, base, suffixbuff);
Packit 423ecb
    res[499] = 0;
Packit 423ecb
    return(strdup(res));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static int checkTestFile(const char *filename) {
Packit 423ecb
    struct stat buf;
Packit 423ecb
Packit 423ecb
    if (stat(filename, &buf) == -1)
Packit 423ecb
        return(0);
Packit 423ecb
Packit 423ecb
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit 423ecb
    if (!(buf.st_mode & _S_IFREG))
Packit 423ecb
        return(0);
Packit 423ecb
#else
Packit 423ecb
    if (!S_ISREG(buf.st_mode))
Packit 423ecb
        return(0);
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    return(1);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Test to detect or not recursive entities		*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
/**
Packit 423ecb
 * recursiveDetectTest:
Packit 423ecb
 * @filename: the file to parse
Packit 423ecb
 * @result: the file with expected result
Packit 423ecb
 * @err: the file with error messages: unused
Packit 423ecb
 *
Packit 423ecb
 * Parse a file loading DTD and replacing entities check it fails for
Packit 423ecb
 * lol cases
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success, an error code otherwise
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
recursiveDetectTest(const char *filename,
Packit 423ecb
             const char *result ATTRIBUTE_UNUSED,
Packit 423ecb
             const char *err ATTRIBUTE_UNUSED,
Packit 423ecb
	     int options ATTRIBUTE_UNUSED) {
Packit 423ecb
    xmlDocPtr doc;
Packit 423ecb
    xmlParserCtxtPtr ctxt;
Packit 423ecb
    int res = 0;
Packit 423ecb
Packit 423ecb
    nb_tests++;
Packit 423ecb
Packit 423ecb
    ctxt = xmlNewParserCtxt();
Packit 423ecb
    /*
Packit 423ecb
     * base of the test, parse with the old API
Packit 423ecb
     */
Packit 423ecb
    doc = xmlCtxtReadFile(ctxt, filename, NULL,
Packit 423ecb
                          XML_PARSE_NOENT | XML_PARSE_DTDLOAD);
Packit 423ecb
    if ((doc != NULL) || (ctxt->lastError.code != XML_ERR_ENTITY_LOOP)) {
Packit 423ecb
        fprintf(stderr, "Failed to detect recursion in %s\n", filename);
Packit 423ecb
	xmlFreeParserCtxt(ctxt);
Packit 423ecb
	xmlFreeDoc(doc);
Packit 423ecb
        return(1);
Packit 423ecb
    }
Packit 423ecb
    xmlFreeParserCtxt(ctxt);
Packit 423ecb
Packit 423ecb
    return(res);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * notRecursiveDetectTest:
Packit 423ecb
 * @filename: the file to parse
Packit 423ecb
 * @result: the file with expected result
Packit 423ecb
 * @err: the file with error messages: unused
Packit 423ecb
 *
Packit 423ecb
 * Parse a file loading DTD and replacing entities check it works for
Packit 423ecb
 * good cases
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success, an error code otherwise
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
notRecursiveDetectTest(const char *filename,
Packit 423ecb
             const char *result ATTRIBUTE_UNUSED,
Packit 423ecb
             const char *err ATTRIBUTE_UNUSED,
Packit 423ecb
	     int options ATTRIBUTE_UNUSED) {
Packit 423ecb
    xmlDocPtr doc;
Packit 423ecb
    xmlParserCtxtPtr ctxt;
Packit 423ecb
    int res = 0;
Packit 423ecb
Packit 423ecb
    nb_tests++;
Packit 423ecb
Packit 423ecb
    ctxt = xmlNewParserCtxt();
Packit 423ecb
    /*
Packit 423ecb
     * base of the test, parse with the old API
Packit 423ecb
     */
Packit 423ecb
    doc = xmlCtxtReadFile(ctxt, filename, NULL,
Packit 423ecb
                          XML_PARSE_NOENT | XML_PARSE_DTDLOAD);
Packit 423ecb
    if (doc == NULL) {
Packit 423ecb
        fprintf(stderr, "Failed to parse correct file %s\n", filename);
Packit 423ecb
	xmlFreeParserCtxt(ctxt);
Packit 423ecb
        return(1);
Packit 423ecb
    }
Packit 423ecb
    xmlFreeDoc(doc);
Packit 423ecb
    xmlFreeParserCtxt(ctxt);
Packit 423ecb
Packit 423ecb
    return(res);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_READER_ENABLED
Packit 423ecb
/**
Packit 423ecb
 * notRecursiveHugeTest:
Packit 423ecb
 * @filename: the file to parse
Packit 423ecb
 * @result: the file with expected result
Packit 423ecb
 * @err: the file with error messages: unused
Packit 423ecb
 *
Packit 423ecb
 * Parse a memory generated file
Packit 423ecb
 * good cases
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success, an error code otherwise
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
notRecursiveHugeTest(const char *filename ATTRIBUTE_UNUSED,
Packit 423ecb
             const char *result ATTRIBUTE_UNUSED,
Packit 423ecb
             const char *err ATTRIBUTE_UNUSED,
Packit 423ecb
	     int options ATTRIBUTE_UNUSED) {
Packit 423ecb
    xmlTextReaderPtr reader;
Packit 423ecb
    int res = 0;
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    nb_tests++;
Packit 423ecb
Packit 423ecb
    reader = xmlReaderForFile("huge:test" , NULL,
Packit 423ecb
                              XML_PARSE_NOENT | XML_PARSE_DTDLOAD);
Packit 423ecb
    if (reader == NULL) {
Packit 423ecb
        fprintf(stderr, "Failed to open huge:test\n");
Packit 423ecb
	return(1);
Packit 423ecb
    }
Packit 423ecb
    ret = xmlTextReaderRead(reader);
Packit 423ecb
    while (ret == 1) {
Packit 423ecb
        ret = xmlTextReaderRead(reader);
Packit 423ecb
    }
Packit 423ecb
    if (ret != 0) {
Packit 423ecb
        fprintf(stderr, "Failed to parser huge:test with entities\n");
Packit 423ecb
	res = 1;
Packit 423ecb
    }
Packit 423ecb
    xmlFreeTextReader(reader);
Packit 423ecb
Packit 423ecb
    return(res);
Packit 423ecb
}
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *			Tests Descriptions				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
static
Packit 423ecb
testDesc testDescriptions[] = {
Packit 423ecb
    { "Parsing recursive test cases" ,
Packit 423ecb
      recursiveDetectTest, "./test/recurse/lol*.xml", NULL, NULL, NULL,
Packit 423ecb
      0 },
Packit 423ecb
    { "Parsing non-recursive test cases" ,
Packit 423ecb
      notRecursiveDetectTest, "./test/recurse/good*.xml", NULL, NULL, NULL,
Packit 423ecb
      0 },
Packit 423ecb
#ifdef LIBXML_READER_ENABLED
Packit 423ecb
    { "Parsing non-recursive huge case" ,
Packit 423ecb
      notRecursiveHugeTest, NULL, NULL, NULL, NULL,
Packit 423ecb
      0 },
Packit 423ecb
#endif
Packit 423ecb
    {NULL, NULL, NULL, NULL, NULL, NULL, 0}
Packit 423ecb
};
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		The main code driving the tests				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
launchTests(testDescPtr tst) {
Packit 423ecb
    int res = 0, err = 0;
Packit 423ecb
    size_t i;
Packit 423ecb
    char *result;
Packit 423ecb
    char *error;
Packit 423ecb
    int mem;
Packit 423ecb
Packit 423ecb
    if (tst == NULL) return(-1);
Packit 423ecb
    if (tst->in != NULL) {
Packit 423ecb
	glob_t globbuf;
Packit 423ecb
Packit 423ecb
	globbuf.gl_offs = 0;
Packit 423ecb
	glob(tst->in, GLOB_DOOFFS, NULL, &globbuf);
Packit 423ecb
	for (i = 0;i < globbuf.gl_pathc;i++) {
Packit 423ecb
	    if (!checkTestFile(globbuf.gl_pathv[i]))
Packit 423ecb
	        continue;
Packit 423ecb
	    if (tst->suffix != NULL) {
Packit 423ecb
		result = resultFilename(globbuf.gl_pathv[i], tst->out,
Packit 423ecb
					tst->suffix);
Packit 423ecb
		if (result == NULL) {
Packit 423ecb
		    fprintf(stderr, "Out of memory !\n");
Packit 423ecb
		    fatalError();
Packit 423ecb
		}
Packit 423ecb
	    } else {
Packit 423ecb
	        result = NULL;
Packit 423ecb
	    }
Packit 423ecb
	    if (tst->err != NULL) {
Packit 423ecb
		error = resultFilename(globbuf.gl_pathv[i], tst->out,
Packit 423ecb
		                        tst->err);
Packit 423ecb
		if (error == NULL) {
Packit 423ecb
		    fprintf(stderr, "Out of memory !\n");
Packit 423ecb
		    fatalError();
Packit 423ecb
		}
Packit 423ecb
	    } else {
Packit 423ecb
	        error = NULL;
Packit 423ecb
	    }
Packit 423ecb
	    if ((result) &&(!checkTestFile(result))) {
Packit 423ecb
	        fprintf(stderr, "Missing result file %s\n", result);
Packit 423ecb
	    } else if ((error) &&(!checkTestFile(error))) {
Packit 423ecb
	        fprintf(stderr, "Missing error file %s\n", error);
Packit 423ecb
	    } else {
Packit 423ecb
		mem = xmlMemUsed();
Packit 423ecb
		extraMemoryFromResolver = 0;
Packit 423ecb
		testErrorsSize = 0;
Packit 423ecb
		testErrors[0] = 0;
Packit 423ecb
		res = tst->func(globbuf.gl_pathv[i], result, error,
Packit 423ecb
		                tst->options | XML_PARSE_COMPACT);
Packit 423ecb
		xmlResetLastError();
Packit 423ecb
		if (res != 0) {
Packit 423ecb
		    fprintf(stderr, "File %s generated an error\n",
Packit 423ecb
		            globbuf.gl_pathv[i]);
Packit 423ecb
		    nb_errors++;
Packit 423ecb
		    err++;
Packit 423ecb
		}
Packit 423ecb
		else if (xmlMemUsed() != mem) {
Packit 423ecb
		    if ((xmlMemUsed() != mem) &&
Packit 423ecb
		        (extraMemoryFromResolver == 0)) {
Packit 423ecb
			fprintf(stderr, "File %s leaked %d bytes\n",
Packit 423ecb
				globbuf.gl_pathv[i], xmlMemUsed() - mem);
Packit 423ecb
			nb_leaks++;
Packit 423ecb
			err++;
Packit 423ecb
		    }
Packit 423ecb
		}
Packit 423ecb
		testErrorsSize = 0;
Packit 423ecb
	    }
Packit 423ecb
	    if (result)
Packit 423ecb
		free(result);
Packit 423ecb
	    if (error)
Packit 423ecb
		free(error);
Packit 423ecb
	}
Packit 423ecb
	globfree(&globbuf);
Packit 423ecb
    } else {
Packit 423ecb
        testErrorsSize = 0;
Packit 423ecb
	testErrors[0] = 0;
Packit 423ecb
	extraMemoryFromResolver = 0;
Packit 423ecb
        res = tst->func(NULL, NULL, NULL, tst->options);
Packit 423ecb
	if (res != 0) {
Packit 423ecb
	    nb_errors++;
Packit 423ecb
	    err++;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    return(err);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static int verbose = 0;
Packit 423ecb
static int tests_quiet = 0;
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
runtest(int i) {
Packit 423ecb
    int ret = 0, res;
Packit 423ecb
    int old_errors, old_tests, old_leaks;
Packit 423ecb
Packit 423ecb
    old_errors = nb_errors;
Packit 423ecb
    old_tests = nb_tests;
Packit 423ecb
    old_leaks = nb_leaks;
Packit 423ecb
    if ((tests_quiet == 0) && (testDescriptions[i].desc != NULL))
Packit 423ecb
	printf("## %s\n", testDescriptions[i].desc);
Packit 423ecb
    res = launchTests(&testDescriptions[i]);
Packit 423ecb
    if (res != 0)
Packit 423ecb
	ret++;
Packit 423ecb
    if (verbose) {
Packit 423ecb
	if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
Packit 423ecb
	    printf("Ran %d tests, no errors\n", nb_tests - old_tests);
Packit 423ecb
	else
Packit 423ecb
	    printf("Ran %d tests, %d errors, %d leaks\n",
Packit 423ecb
		   nb_tests - old_tests,
Packit 423ecb
		   nb_errors - old_errors,
Packit 423ecb
		   nb_leaks - old_leaks);
Packit 423ecb
    }
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
Packit 423ecb
    int i, a, ret = 0;
Packit 423ecb
    int subset = 0;
Packit 423ecb
Packit 423ecb
    initializeLibxml2();
Packit 423ecb
Packit 423ecb
    for (a = 1; a < argc;a++) {
Packit 423ecb
        if (!strcmp(argv[a], "-v"))
Packit 423ecb
	    verbose = 1;
Packit 423ecb
        else if (!strcmp(argv[a], "-quiet"))
Packit 423ecb
	    tests_quiet = 1;
Packit 423ecb
	else {
Packit 423ecb
	    for (i = 0; testDescriptions[i].func != NULL; i++) {
Packit 423ecb
	        if (strstr(testDescriptions[i].desc, argv[a])) {
Packit 423ecb
		    ret += runtest(i);
Packit 423ecb
		    subset++;
Packit 423ecb
		}
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    if (subset == 0) {
Packit 423ecb
	for (i = 0; testDescriptions[i].func != NULL; i++) {
Packit 423ecb
	    ret += runtest(i);
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    if ((nb_errors == 0) && (nb_leaks == 0)) {
Packit 423ecb
        ret = 0;
Packit 423ecb
	printf("Total %d tests, no errors\n",
Packit 423ecb
	       nb_tests);
Packit 423ecb
    } else {
Packit 423ecb
        ret = 1;
Packit 423ecb
	printf("Total %d tests, %d errors, %d leaks\n",
Packit 423ecb
	       nb_tests, nb_errors, nb_leaks);
Packit 423ecb
    }
Packit 423ecb
    xmlCleanupParser();
Packit 423ecb
    xmlMemoryDump();
Packit 423ecb
Packit 423ecb
    return(ret);
Packit 423ecb
}