summaryrefslogtreecommitdiff
path: root/testOOM.c
blob: 7d9b41162ea5f675a58bcfded1ccced6b05a6e39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * testOOM.c: Test out-of-memory handling
 *
 * See Copyright for the status of this software.
 *
 * hp@redhat.com
 */

/* FIXME this test would be much better if instead of just checking
 * for debug spew or crashes on OOM, it also validated the expected
 * results of parsing a particular file vs. the actual results
 */

#include "libxml.h"

#include <string.h>
#include <stdarg.h>

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <libxml/xmlreader.h>

#include "testOOMlib.h"

#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif


int debug = 0;
int dump = 0;
int noent = 0;
int count = 0;
int valid = 0;

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");
    exit(1);
}
static unsigned int elem, attrs, chars;

static int processNode(xmlTextReaderPtr reader) {
    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;
	}
    }

    return TRUE;
}


struct file_params {
  const char *filename;
  unsigned int elem, attrs, chars;
};

/* This always returns TRUE since we don't validate the results of
 * parsing a particular document vs. the expected results of parsing
 * that document. The idea is that such a failure would return FALSE.
 */
static int
check_load_file_memory_func (void *data)
{
     struct file_params *p = data;
     xmlTextReaderPtr reader;
     int ret, status;

     if (count) {
          elem = 0;
          attrs = 0;
          chars = 0;
     }

     status = TRUE;
     reader = xmlNewTextReaderFilename(p->filename);

     if (reader != 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))
	      goto out;
          }
	  if (ret == -1)
	    goto out;

          /*
           * Done, cleanup and status
           */
	  if (count)
	    {
	      if (p->elem == (unsigned int)-1) {
		p->elem = elem;
		p->attrs = attrs;
		p->chars = chars;
	      }
	      else {
		status = (elem == p->elem && attrs == p->attrs && chars == p->chars);
		fprintf (stderr, "# %s: %u elems, %u attrs, %u chars %s\n",
			 p->filename, elem, attrs, chars,
			 status ? "ok" : "wrong");
	      }
	    }
     }
 out:
     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);
    
    for (i = 1; i < argc ; i++) {
	if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
	    debug++;
	else if ((!strcmp(argv[i], "-dump")) || (!strcmp(argv[i], "--dump")))
	    dump++;
	else 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++;
    }
    if (noent != 0)
      xmlSubstituteEntitiesDefault(1);
    for (i = 1; i < argc ; i++) {
	if (argv[i][0] != '-') {
             struct file_params p;
	     p.filename = argv[i];
	     p.elem = -1;

	     /* Run once to count */
	     check_load_file_memory_func (&p);
	     if (count) {
	       fprintf (stderr, "# %s: %u elems, %u attrs, %u chars\n",
			p.filename, p.elem, p.attrs, p.chars);
	     }
             if (!test_oom_handling (check_load_file_memory_func,
                                     &p)) {
                  fprintf (stderr, "Failed!\n");
                  return (1);
             }

             xmlCleanupParser();

             if (test_get_malloc_blocks_outstanding () > 0) {
                  fprintf (stderr, "%d blocks leaked\n",
                           test_get_malloc_blocks_outstanding ());
		  xmlMemoryDump();
                  return (1);
             }
             
	    files ++;
	}
    }
    xmlMemoryDump();

    return(0);
}