blob: 8e9a30247429b0e2af5bfdeacbe6ddd7f5dfbfbd [file] [log] [blame]
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001/*
2 * testOOM.c: Test out-of-memory handling
3 *
4 * See Copyright for the status of this software.
5 *
6 * hp@redhat.com
7 */
8
9/* FIXME this test would be much better if instead of just checking
10 * for debug spew or crashes on OOM, it also validated the expected
11 * results of parsing a particular file vs. the actual results
12 */
13
14#include "libxml.h"
15
16#include <string.h>
17#include <stdarg.h>
18
19#ifdef HAVE_SYS_TYPES_H
20#include <sys/types.h>
21#endif
22#ifdef HAVE_UNISTD_H
23#include <unistd.h>
24#endif
25#ifdef HAVE_STDLIB_H
26#include <stdlib.h>
27#endif
28#ifdef HAVE_STRING_H
29#include <string.h>
30#endif
31
32#include <libxml/xmlreader.h>
33
34#include "testOOMlib.h"
35
36#ifndef TRUE
37#define TRUE (1)
38#endif
39#ifndef FALSE
40#define FALSE (0)
41#endif
42
43
44int debug = 0;
45int dump = 0;
46int noent = 0;
47int count = 0;
48int valid = 0;
49
50static void usage(const char *progname) {
51 printf("Usage : %s [options] XMLfiles ...\n", progname);
52 printf("\tParse the XML files using the xmlTextReader API\n");
53 printf("\t --count: count the number of attribute and elements\n");
54 printf("\t --valid: validate the document\n");
55 exit(1);
56}
57static int elem, attrs;
58
59static int processNode(xmlTextReaderPtr reader) {
60 int type;
61
62 type = xmlTextReaderNodeType(reader);
63 if (count) {
64 if (type == 1) {
65 elem++;
66 attrs += xmlTextReaderAttributeCount(reader);
67 }
68 }
69
70 return TRUE;
71}
72
73/* This always returns TRUE since we don't validate the results of
74 * parsing a particular document vs. the expected results of parsing
75 * that document. The idea is that such a failure would return FALSE.
76 */
77static int
78check_load_file_memory_func (void *data)
79{
80 const char *filename = data;
81 xmlTextReaderPtr reader;
82 int ret;
83
84 if (count) {
85 elem = 0;
86 attrs = 0;
87 }
88
89 reader = xmlNewTextReaderFilename(filename);
90
91 if (reader != NULL) {
92 if (valid) {
93 if (xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1) == -1) {
94 xmlFreeTextReader (reader);
95 return TRUE;
96 }
97 }
98
99 /*
100 * Process all nodes in sequence
101 */
102 ret = xmlTextReaderRead (reader);
103
104 while (TRUE) {
105 if (ret == -1) {
106 xmlFreeTextReader (reader);
107 return TRUE;
108 } else if (ret != 1)
109 break;
110
111 if (!processNode(reader)) {
112 xmlFreeTextReader (reader);
113 return FALSE;
114 }
115
116 ret = xmlTextReaderRead(reader);
117 }
118
119 /*
120 * Done, cleanup and status
121 */
122 xmlFreeTextReader (reader);
123
124 return TRUE;
125 } else {
126 return TRUE;
127 }
128}
129
130int main(int argc, char **argv) {
131 int i;
132 int files = 0;
133
134 if (argc <= 1) {
135 usage(argv[0]);
136 return(1);
137 }
138 LIBXML_TEST_VERSION;
139
140 xmlMemSetup (test_free,
141 test_malloc,
142 test_realloc,
143 test_strdup);
144
145 for (i = 1; i < argc ; i++) {
146 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
147 debug++;
148 else if ((!strcmp(argv[i], "-dump")) || (!strcmp(argv[i], "--dump")))
149 dump++;
150 else if ((!strcmp(argv[i], "-count")) || (!strcmp(argv[i], "--count")))
151 count++;
152 else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
153 valid++;
154 else if ((!strcmp(argv[i], "-noent")) ||
155 (!strcmp(argv[i], "--noent")))
156 noent++;
157 }
158 if (noent != 0)
159 xmlSubstituteEntitiesDefault(1);
160 for (i = 1; i < argc ; i++) {
161 if (argv[i][0] != '-') {
162 if (!test_oom_handling (check_load_file_memory_func,
163 argv[i])) {
164 fprintf (stderr, "Failed!\n");
165 return (1);
166 }
167
168 xmlCleanupParser();
169
170 if (test_get_malloc_blocks_outstanding () > 0) {
171 fprintf (stderr, "%d blocks leaked\n",
172 test_get_malloc_blocks_outstanding ());
173 xmlMemoryDump();
174 return (1);
175 }
176
177 files ++;
178 }
179 }
180 xmlMemoryDump();
181
182 return(0);
183}