diff --git a/Makefile.am b/Makefile.am index 513e270e..441cf5f2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -183,10 +183,6 @@ runxmlconf_SOURCES=runxmlconf.c runxmlconf_DEPENDENCIES = $(DEPS) runxmlconf_LDADD= $(LDADDS) -#testOOM_SOURCES=testOOM.c testOOMlib.h testOOMlib.c -#testOOM_DEPENDENCIES = $(DEPS) -#testOOM_LDADD= $(LDADDS) - check-local: [ -d test ] || $(LN_S) $(srcdir)/test . [ -d result ] || $(LN_S) $(srcdir)/result . diff --git a/doc/apibuild.py b/doc/apibuild.py index e45fcdaa..79822405 100755 --- a/doc/apibuild.py +++ b/doc/apibuild.py @@ -21,9 +21,6 @@ debugsym=None ignored_files = { "config.h": "generated portability layer", "libxml.h": "internal only", - "testOOM.c": "out of memory tester", - "testOOMlib.h": "out of memory tester", - "testOOMlib.c": "out of memory tester", "rngparser.c": "not yet integrated", "testModule.c": "test tool", "testThreads.c": "test tool", diff --git a/testOOM.c b/testOOM.c deleted file mode 100644 index 24d55c19..00000000 --- a/testOOM.c +++ /dev/null @@ -1,360 +0,0 @@ -/* - * testOOM.c: Test out-of-memory handling - * - * See Copyright for the status of this software. - * - * hp@redhat.com - */ - -#include -#include -#include - -#include - -#include "testOOMlib.h" - -#ifndef TRUE -#define TRUE (1) -#endif -#ifndef FALSE -#define FALSE (0) -#endif - -#define EXIT_OOM 2 - -int error = FALSE; -int errcount = 0; -int noent = 0; -int count = 0; -int valid = 0; -int showErrs = 0; - -/* - * Since we are using the xmlTextReader functions, we set up - * strings for the element types to help in debugging any error - * output - */ -const char *elementNames[] = { - "XML_READER_TYPE_NONE", - "XML_READER_TYPE_ELEMENT", - "XML_READER_TYPE_ATTRIBUTE", - "XML_READER_TYPE_TEXT", - "XML_READER_TYPE_CDATA", - "XML_READER_TYPE_ENTITY_REFERENCE", - "XML_READER_TYPE_ENTITY", - "XML_READER_TYPE_PROCESSING_INSTRUCTION", - "XML_READER_TYPE_COMMENT", - "XML_READER_TYPE_DOCUMENT", - "XML_READER_TYPE_DOCUMENT_TYPE", - "XML_READER_TYPE_DOCUMENT_FRAGMENT", - "XML_READER_TYPE_NOTATION", - "XML_READER_TYPE_WHITESPACE", - "XML_READER_TYPE_SIGNIFICANT_WHITESPACE", - "XML_READER_TYPE_END_ELEMENT", - "XML_READER_TYPE_END_ENTITY", - "XML_READER_TYPE_XML_DECLARATION"}; - -/* not using xmlBuff here because I don't want those - * mallocs to interfere */ -struct buffer { - char *str; - size_t len; - size_t max; -}; - -static struct buffer *buffer_create (size_t init_len) -{ - struct buffer *b; - b = malloc (sizeof *b); - if (b == NULL) - exit (EXIT_OOM); - if (init_len) { - b->str = malloc (init_len); - if (b->str == NULL) - exit (EXIT_OOM); - } - else - b->str = NULL; - b->len = 0; - b->max = init_len; - return b; -} - -static void buffer_free (struct buffer *b) -{ - free (b->str); - free (b); -} - -static size_t buffer_get_length (struct buffer *b) -{ - return b->len; -} - -static void buffer_expand (struct buffer *b, size_t min) -{ - void *new_str; - size_t new_size = b->max ? b->max : 512; - while (new_size < b->len + min) - new_size *= 2; - if (new_size > b->max) { - new_str = realloc (b->str, new_size); - if (new_str == NULL) - exit (EXIT_OOM); - b->str = new_str; - b->max = new_size; - } -} - -static void buffer_add_char (struct buffer *b, char c) -{ - buffer_expand (b, 1); - b->str[b->len] = c; - b->len += 1; -} - -static void buffer_add_string (struct buffer *b, const char *s) -{ - size_t size = strlen(s) + 1; - unsigned int ix; - for (ix=0; ixstr + b->len, s); - b->str[b->len+size-1] = '\n'; /* replace string term with newline */ - b->len += size; -} - -static int buffer_equal (struct buffer *b1, struct buffer *b2) -{ - return (b1->len == b2->len && - (b1->len == 0 || (memcmp (b1->str, b2->str, b1->len) == 0))); -} - -static void buffer_dump (struct buffer *b, const char *fname) -{ - FILE *f = fopen (fname, "wb"); - if (f != NULL) { - fwrite (b->str, 1, b->len, f); - fclose (f); - } -} - - -static void usage(const char *progname) { - printf("Usage : %s [options] XMLfiles ...\n", progname); - printf("\tParse the XML files using the xmlTextReader API\n"); - printf("\t --count: count the number of attribute and elements\n"); - printf("\t --valid: validate the document\n"); - printf("\t --show: display the error messages encountered\n"); - exit(1); -} -static unsigned int elem, attrs, chars; - -static int processNode (xmlTextReaderPtr reader, void *data) -{ - struct buffer *buff = data; - int type; - - type = xmlTextReaderNodeType(reader); - if (count) { - if (type == 1) { - elem++; - attrs += xmlTextReaderAttributeCount(reader); - } else if (type == 3) { - const xmlChar *txt; - txt = xmlTextReaderConstValue(reader); - if (txt != NULL) - chars += xmlStrlen (txt); - else - return FALSE; - } - } - - if (buff != NULL) { - int ret; - const char *s; - - buffer_add_string (buff, elementNames[type]); - - if (type == 1) { - s = (const char *)xmlTextReaderConstName (reader); - if (s == NULL) return FALSE; - buffer_add_string (buff, s); - while ((ret = xmlTextReaderMoveToNextAttribute (reader)) == 1) { - s = (const char *)xmlTextReaderConstName (reader); - if (s == NULL) return FALSE; - buffer_add_string (buff, s); - buffer_add_char (buff, '='); - s = (const char *)xmlTextReaderConstValue (reader); - if (s == NULL) return FALSE; - buffer_add_string (buff, s); - } - if (ret == -1) return FALSE; - } - else if (type == 3) { - s = (const char *)xmlTextReaderConstValue (reader); - if (s == NULL) return FALSE; - buffer_add_string (buff, s); - } - } - - return TRUE; -} - - -struct file_params { - const char *filename; - struct buffer *verif_buff; -}; - -static void -error_func (void *data ATTRIBUTE_UNUSED, xmlErrorPtr err) -{ - - errcount++; - if (err->level == XML_ERR_ERROR || - err->level == XML_ERR_FATAL) - error = TRUE; - if (showErrs) { - printf("%3d line %d: %s\n", error, err->line, err->message); - } -} - -static int -check_load_file_memory_func (void *data) -{ - struct file_params *p = data; - struct buffer *b; - xmlTextReaderPtr reader; - int ret, status, first_run; - - if (count) { - elem = 0; - attrs = 0; - chars = 0; - } - - first_run = p->verif_buff == NULL; - status = TRUE; - error = FALSE; - if (first_run) - b = buffer_create (0); - else - b = buffer_create (buffer_get_length (p->verif_buff)); - - reader = xmlNewTextReaderFilename (p->filename); - if (reader == NULL) - goto out; - - xmlTextReaderSetStructuredErrorHandler (reader, error_func, NULL); - - if (valid) { - if (xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1) == -1) - goto out; - } - - /* - * Process all nodes in sequence - */ - while ((ret = xmlTextReaderRead(reader)) == 1) { - if (!processNode(reader, b)) - goto out; - } - if (ret == -1) - goto out; - - if (error) { - fprintf (stdout, "error handler was called but parse completed successfully (last error #%d)\n", errcount); - return FALSE; - } - - /* - * Done, cleanup and status - */ - if (! first_run) { - status = buffer_equal (p->verif_buff, b); - if (! status) { - buffer_dump (p->verif_buff, ".OOM.verif_buff"); - buffer_dump (b, ".OOM.buff"); - } - } - - if (count) - { - fprintf (stdout, "# %s: %u elems, %u attrs, %u chars %s\n", - p->filename, elem, attrs, chars, - status ? "ok" : "wrong"); - } - - out: - if (first_run) - p->verif_buff = b; - else - buffer_free (b); - if (reader) - xmlFreeTextReader (reader); - return status; -} - -int main(int argc, char **argv) { - int i; - int files = 0; - - if (argc <= 1) { - usage(argv[0]); - return(1); - } - LIBXML_TEST_VERSION; - - xmlMemSetup (test_free, - test_malloc, - test_realloc, - test_strdup); - - xmlInitParser(); - - for (i = 1; i < argc ; i++) { - if ((!strcmp(argv[i], "-count")) || (!strcmp(argv[i], "--count"))) - count++; - else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid"))) - valid++; - else if ((!strcmp(argv[i], "-noent")) || - (!strcmp(argv[i], "--noent"))) - noent++; - else if ((!strcmp(argv[i], "-show")) || - (!strcmp(argv[i], "--show"))) - showErrs++; - } - if (noent != 0) - xmlSubstituteEntitiesDefault(1); - for (i = 1; i < argc ; i++) { - if (argv[i][0] != '-') { - struct file_params p; - p.filename = argv[i]; - p.verif_buff = NULL; - - if (!test_oom_handling (check_load_file_memory_func, - &p)) { - fprintf (stdout, "Failed!\n"); - return 1; - } - - buffer_free (p.verif_buff); - xmlCleanupParser(); - - if (test_get_malloc_blocks_outstanding () > 0) { - fprintf (stdout, "%d blocks leaked\n", - test_get_malloc_blocks_outstanding ()); - return 1; - } - - files ++; - } - } - - return 0; -} diff --git a/testOOMlib.c b/testOOMlib.c deleted file mode 100644 index 310714fa..00000000 --- a/testOOMlib.c +++ /dev/null @@ -1,266 +0,0 @@ -/* - * testOOM.c: Test out-of-memory handling - * - * See Copyright for the status of this software. - * - * Copyright 2003 Red Hat, Inc. - * Written by: hp@redhat.com - */ - -#include "testOOMlib.h" - -#include -#include - -#define _TEST_INT_MAX 2147483647 -#ifndef TRUE -#define TRUE (1) -#endif -#ifndef FALSE -#define FALSE (0) -#endif -#ifndef NULL -#define NULL ((void*)0) -#endif - -#include - -static int fail_alloc_counter = _TEST_INT_MAX; -static int n_failures_per_failure = 1; -static int n_failures_this_failure = 0; -static int n_blocks_outstanding = 0; - -/** - * set_fail_alloc_counter: - * @until_next_fail: number of successful allocs before one fails - * - * Sets the number of allocations until we simulate a failed - * allocation. If set to 0, the next allocation to run - * fails; if set to 1, one succeeds then the next fails; etc. - * Set to _TEST_INT_MAX to not fail anything. - */ -static void -set_fail_alloc_counter (int until_next_fail) -{ - fail_alloc_counter = until_next_fail; -} - -/** - * get_fail_alloc_counter: - * - * Returns the number of successful allocs until we'll simulate - * a failed alloc. - */ -static int -get_fail_alloc_counter (void) -{ - return fail_alloc_counter; -} - -/** - * set_fail_alloc_failures: - * @failures_per_failure: number to fail - * - * Sets how many mallocs to fail when the fail alloc counter reaches - * 0. - * - */ -static void -set_fail_alloc_failures (int failures_per_failure) -{ - n_failures_per_failure = failures_per_failure; -} - -/** - * decrement_fail_alloc_counter: - * - * Called when about to alloc some memory; if - * it returns #TRUE, then the allocation should - * fail. If it returns #FALSE, then the allocation - * should not fail. - * - * returns #TRUE if this alloc should fail - */ -static int -decrement_fail_alloc_counter (void) -{ - if (fail_alloc_counter <= 0) - { - n_failures_this_failure += 1; - if (n_failures_this_failure >= n_failures_per_failure) - { - fail_alloc_counter = _TEST_INT_MAX; - - n_failures_this_failure = 0; - } - - return TRUE; - } - else - { - fail_alloc_counter -= 1; - return FALSE; - } -} - -/** - * test_get_malloc_blocks_outstanding: - * - * Get the number of outstanding malloc()'d blocks. - * - * Returns number of blocks - */ -int -test_get_malloc_blocks_outstanding (void) -{ - return n_blocks_outstanding; -} - -void* -test_malloc (size_t bytes) -{ - if (decrement_fail_alloc_counter ()) - { - /* FAIL the malloc */ - return NULL; - } - - if (bytes == 0) /* some system mallocs handle this, some don't */ - return NULL; - else - { - void *mem; - mem = xmlMemMalloc (bytes); - - if (mem) - n_blocks_outstanding += 1; - - return mem; - } -} - -void* -test_realloc (void *memory, - size_t bytes) -{ - if (decrement_fail_alloc_counter ()) - { - /* FAIL */ - return NULL; - } - - if (bytes == 0) /* guarantee this is safe */ - { - test_free (memory); - return NULL; - } - else - { - void *mem; - mem = xmlMemRealloc (memory, bytes); - - if (memory == NULL && mem != NULL) - n_blocks_outstanding += 1; - - return mem; - } -} - -void -test_free (void *memory) -{ - if (memory) /* we guarantee it's safe to free (NULL) */ - { - n_blocks_outstanding -= 1; - - xmlMemFree (memory); - } -} - -char* -test_strdup (const char *str) -{ - int len; - char *copy; - - if (str == NULL) - return NULL; - - len = strlen (str); - - copy = test_malloc (len + 1); - if (copy == NULL) - return NULL; - - memcpy (copy, str, len + 1); - - return copy; -} - -static int -run_failing_each_malloc (int n_mallocs, - TestMemoryFunction func, - void *data) -{ - n_mallocs += 10; /* fudge factor to ensure reallocs etc. are covered */ - - while (n_mallocs >= 0) - { - set_fail_alloc_counter (n_mallocs); - - if (!(* func) (data)) - return FALSE; - - n_mallocs -= 1; - } - - set_fail_alloc_counter (_TEST_INT_MAX); - - return TRUE; -} - -/** - * test_oom_handling: - * @func: function to call - * @data: data to pass to function - * - * Tests how well the given function responds to out-of-memory - * situations. Calls the function repeatedly, failing a different - * call to malloc() each time. If the function ever returns #FALSE, - * the test fails. The function should return #TRUE whenever something - * valid (such as returning an error, or succeeding) occurs, and #FALSE - * if it gets confused in some way. - * - * Returns #TRUE if the function never returns FALSE - */ -int -test_oom_handling (TestMemoryFunction func, - void *data) -{ - int approx_mallocs; - - /* Run once to see about how many mallocs are involved */ - - set_fail_alloc_counter (_TEST_INT_MAX); - - if (!(* func) (data)) - return FALSE; - - approx_mallocs = _TEST_INT_MAX - get_fail_alloc_counter (); - - set_fail_alloc_failures (1); - if (!run_failing_each_malloc (approx_mallocs, func, data)) - return FALSE; - - set_fail_alloc_failures (2); - if (!run_failing_each_malloc (approx_mallocs, func, data)) - return FALSE; - - set_fail_alloc_failures (3); - if (!run_failing_each_malloc (approx_mallocs, func, data)) - return FALSE; - - set_fail_alloc_counter (_TEST_INT_MAX); - - return TRUE; -} diff --git a/testOOMlib.h b/testOOMlib.h deleted file mode 100644 index fff148f7..00000000 --- a/testOOMlib.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef TEST_OOM_LIB_H -#define TEST_OOM_LIB_H - -#include - -void* test_malloc (size_t bytes); -void* test_realloc (void *memory, - size_t bytes); -void test_free (void *memory); -char* test_strdup (const char *str); - -/* returns true on success */ -typedef int (* TestMemoryFunction) (void *data); - -/* returns true on success */ -int test_oom_handling (TestMemoryFunction func, - void *data); - -/* get number of blocks leaked */ -int test_get_malloc_blocks_outstanding (void); - -#endif