1634 lines · 41.1 KB · C
RawDownload
/* * testlimits.c: C program to run libxml2 regression tests checking various *       limits in document size. Will consume a lot of RAM and CPU cycles * * To compile on Unixes: * cc -o testlimits `xml2-config --cflags` testlimits.c `xml2-config --libs` -lpthread * * See Copyright for the status of this software. * * daniel@veillard.com */ #ifdef HAVE_CONFIG_H#include "libxml.h"#else#include <stdio.h>#endif #if !defined(_WIN32) || defined(__CYGWIN__)#include <unistd.h>#endif#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <time.h> #include <libxml/parser.h>#include <libxml/parserInternals.h>#include <libxml/tree.h>#include <libxml/uri.h>#ifdef LIBXML_READER_ENABLED#include <libxml/xmlreader.h>#endif static int verbose = 0;static int tests_quiet = 0; /************************************************************************ *									* *		time handling                                           * *									* ************************************************************************/ /* maximum time for one parsing before declaring a timeout */#define MAX_TIME 2 /* seconds */ static clock_t t0;int timeout = 0; static void reset_timout(void) {    timeout = 0;    t0 = clock();} static int check_time(void) {    clock_t tnow = clock();    if (((tnow - t0) / CLOCKS_PER_SEC) > MAX_TIME) {        timeout = 1;        return(0);    }    return(1);} /************************************************************************ *									* *		Huge document generator					* *									* ************************************************************************/ #include <libxml/xmlIO.h> /* * Huge documents are built using fixed start and end chunks * and filling between the two an unconventional amount of char data */typedef struct hugeTest hugeTest;typedef hugeTest *hugeTestPtr;struct hugeTest {    const char *description;    const char *name;    const char *start;    const char *end;}; static struct hugeTest hugeTests[] = {    { "Huge text node", "huge:textNode", "<foo>", "</foo>" },    { "Huge attribute node", "huge:attrNode", "<foo bar='", "'/>" },    { "Huge comment node", "huge:commentNode", "<foo><!--", "--></foo>" },    { "Huge PI node", "huge:piNode", "<foo><?bar ", "?></foo>" },}; static const char *current;static int rlen;static unsigned int currentTest = 0;static int instate = 0; /** * hugeMatch: * @URI: an URI to test * * Check for an huge: query * * Returns 1 if yes and 0 if another Input module should be used */static inthugeMatch(const char * URI) {    if ((URI != NULL) && (!strncmp(URI, "huge:", 5)))        return(1);    return(0);} /** * hugeOpen: * @URI: an URI to test * * Return a pointer to the huge: query handler, in this example simply * the current pointer... * * Returns an Input context or NULL in case or error */static void *hugeOpen(const char * URI) {    if ((URI == NULL) || (strncmp(URI, "huge:", 5)))        return(NULL);     for (currentTest = 0;currentTest < sizeof(hugeTests)/sizeof(hugeTests[0]);         currentTest++)         if (!strcmp(hugeTests[currentTest].name, URI))             goto found;     return(NULL); found:    rlen = strlen(hugeTests[currentTest].start);    current = hugeTests[currentTest].start;    instate = 0;    return((void *) current);} /** * hugeClose: * @context: the read context * * Close the huge: query handler * * Returns 0 or -1 in case of error */static inthugeClose(void * context) {    if (context == NULL) return(-1);    fprintf(stderr, "\n");    return(0);} #define CHUNK 4096 char filling[CHUNK + 1]; static void fillFilling(void) {    int i;     for (i = 0;i < CHUNK;i++) {        filling[i] = 'a';    }    filling[CHUNK] = 0;} size_t maxlen = 64 * 1024 * 1024;size_t curlen = 0;size_t dotlen; /** * hugeRead: * @context: the read context * @buffer: where to store data * @len: number of bytes to read * * Implement an huge: query read. * * Returns the number of bytes read or -1 in case of error */static inthugeRead(void *context, char *buffer, int len){    if ((context == NULL) || (buffer == NULL) || (len < 0))        return (-1);     if (instate == 0) {        if (len >= rlen) {            len = rlen;            rlen = 0;            memcpy(buffer, current, len);            instate = 1;            curlen = 0;            dotlen = maxlen / 10;        } else {            memcpy(buffer, current, len);            rlen -= len;            current += len;        }    } else if (instate == 2) {        if (len >= rlen) {            len = rlen;            rlen = 0;            memcpy(buffer, current, len);            instate = 3;            curlen = 0;        } else {            memcpy(buffer, current, len);            rlen -= len;            current += len;        }    } else if (instate == 1) {        if (len > CHUNK) len = CHUNK;        memcpy(buffer, &filling[0], len);        curlen += len;        if (curlen >= maxlen) {            rlen = strlen(hugeTests[currentTest].end);            current = hugeTests[currentTest].end;            instate = 2;	} else {            if (curlen > dotlen) {                fprintf(stderr, ".");                dotlen += maxlen / 10;            }        }    } else      len = 0;    return (len);} /************************************************************************ *									* *		Crazy document generator				* *									* ************************************************************************/ unsigned int crazy_indx = 0; const char *crazy = "<?xml version='1.0' encoding='UTF-8'?>\<?tst ?>\<!-- tst -->\<!DOCTYPE foo [\<?tst ?>\<!-- tst -->\<!ELEMENT foo (#PCDATA)>\<!ELEMENT p (#PCDATA|emph)* >\]>\<?tst ?>\<!-- tst -->\<foo bar='foo'>\<?tst ?>\<!-- tst -->\foo\<![CDATA[ ]]>\</foo>\<?tst ?>\<!-- tst -->"; /** * crazyMatch: * @URI: an URI to test * * Check for a crazy: query * * Returns 1 if yes and 0 if another Input module should be used */static intcrazyMatch(const char * URI) {    if ((URI != NULL) && (!strncmp(URI, "crazy:", 6)))        return(1);    return(0);} /** * crazyOpen: * @URI: an URI to test * * Return a pointer to the crazy: query handler, in this example simply * the current pointer... * * Returns an Input context or NULL in case or error */static void *crazyOpen(const char * URI) {    if ((URI == NULL) || (strncmp(URI, "crazy:", 6)))        return(NULL);     if (crazy_indx > strlen(crazy))        return(NULL);    reset_timout();    rlen = crazy_indx;    current = &crazy[0];    instate = 0;    return((void *) current);} /** * crazyClose: * @context: the read context * * Close the crazy: query handler * * Returns 0 or -1 in case of error */static intcrazyClose(void * context) {    if (context == NULL) return(-1);    return(0);}  /** * crazyRead: * @context: the read context * @buffer: where to store data * @len: number of bytes to read * * Implement an crazy: query read. * * Returns the number of bytes read or -1 in case of error */static intcrazyRead(void *context, char *buffer, int len){    if ((context == NULL) || (buffer == NULL) || (len < 0))        return (-1);     if ((check_time() <= 0) && (instate == 1)) {        fprintf(stderr, "\ntimeout in crazy(%d)\n", crazy_indx);        rlen = strlen(crazy) - crazy_indx;        current = &crazy[crazy_indx];        instate = 2;    }    if (instate == 0) {        if (len >= rlen) {            len = rlen;            rlen = 0;            memcpy(buffer, current, len);            instate = 1;            curlen = 0;        } else {            memcpy(buffer, current, len);            rlen -= len;            current += len;        }    } else if (instate == 2) {        if (len >= rlen) {            len = rlen;            rlen = 0;            memcpy(buffer, current, len);            instate = 3;            curlen = 0;        } else {            memcpy(buffer, current, len);            rlen -= len;            current += len;        }    } else if (instate == 1) {        if (len > CHUNK) len = CHUNK;        memcpy(buffer, &filling[0], len);        curlen += len;        if (curlen >= maxlen) {            rlen = strlen(crazy) - crazy_indx;            current = &crazy[crazy_indx];            instate = 2;        }    } else      len = 0;    return (len);}/************************************************************************ *									* *		Libxml2 specific routines				* *									* ************************************************************************/ static int nb_tests = 0;static int nb_errors = 0;static int nb_leaks = 0;static int extraMemoryFromResolver = 0; /* * We need to trap calls to the resolver to not account memory for the catalog * which is shared to the current running test. We also don't want to have * network downloads modifying tests. */static xmlParserInputPtrtestExternalEntityLoader(const char *URL, const char *ID,			 xmlParserCtxtPtr ctxt) {    xmlParserInputPtr ret;    int memused = xmlMemUsed();     ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);    extraMemoryFromResolver += xmlMemUsed() - memused;     return(ret);} /* * Trapping the error messages at the generic level to grab the equivalent of * stderr messages on CLI tools. */static char testErrors[32769];static int testErrorsSize = 0; static void XMLCDECLchannel(void *ctx  ATTRIBUTE_UNUSED, const char *msg, ...) {    va_list args;    int res;     if (testErrorsSize >= 32768)        return;    va_start(args, msg);    res = vsnprintf(&testErrors[testErrorsSize],                    32768 - testErrorsSize,		    msg, args);    va_end(args);    if (testErrorsSize + res >= 32768) {        /* buffer is full */	testErrorsSize = 32768;	testErrors[testErrorsSize] = 0;    } else {        testErrorsSize += res;    }    testErrors[testErrorsSize] = 0;} /** * xmlParserPrintFileContext: * @input:  an xmlParserInputPtr input * * Displays current context within the input content for error tracking */ static voidxmlParserPrintFileContextInternal(xmlParserInputPtr input ,		xmlGenericErrorFunc chanl, void *data ) {    const xmlChar *cur, *base;    unsigned int n, col;	/* GCC warns if signed, because compared with sizeof() */    xmlChar  content[81]; /* space for 80 chars + line terminator */    xmlChar *ctnt;     if (input == NULL) return;    cur = input->cur;    base = input->base;    /* skip backwards over any end-of-lines */    while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {	cur--;    }    n = 0;    /* search backwards for beginning-of-line (to max buff size) */    while ((n++ < (sizeof(content)-1)) && (cur > base) &&   (*(cur) != '\n') && (*(cur) != '\r'))        cur--;    if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;    /* calculate the error position in terms of the current position */    col = input->cur - cur;    /* search forward for end-of-line (to max buff size) */    n = 0;    ctnt = content;    /* copy selected text to our buffer */    while ((*cur != 0) && (*(cur) != '\n') &&   (*(cur) != '\r') && (n < sizeof(content)-1)) {		*ctnt++ = *cur++;	n++;    }    *ctnt = 0;    /* print out the selected text */    chanl(data ,"%s\n", content);    /* create blank line with problem pointer */    n = 0;    ctnt = content;    /* (leave buffer space for pointer + line terminator) */    while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {	if (*(ctnt) != '\t')	    *(ctnt) = ' ';	ctnt++;    }    *ctnt++ = '^';    *ctnt = 0;    chanl(data ,"%s\n", content);} static voidtestStructuredErrorHandler(void *ctx  ATTRIBUTE_UNUSED, xmlErrorPtr err) {    char *file = NULL;    int line = 0;    int code = -1;    int domain;    void *data = NULL;    const char *str;    const xmlChar *name = NULL;    xmlNodePtr node;    xmlErrorLevel level;    xmlParserInputPtr input = NULL;    xmlParserInputPtr cur = NULL;    xmlParserCtxtPtr ctxt = NULL; 

Lines 1–500 of 1634.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t