summaryrefslogtreecommitdiffstats
path: root/test/tst-sax.c
blob: 2b78fffff82b1a10b40861ea6726573df4e5adfd (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* iksemel (XML parser for Jabber)
** Copyright (C) 2000-2003 Gurer Ozen
** This code is free software; you can redistribute it and/or
** modify it under the terms of GNU Lesser General Public License.
*/

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

#include "iksemel.h"

struct element_s {
	struct element_s *next;
	enum ikstype type;

	enum ikstagtype tag;
	char *name;
	int nr_atts;
	char *atts[10];
	char *vals[10];

	char *cdata;
	int len;
};

struct {
	char *doc;
	int len;
	struct element_s *elements;
	struct element_s *last_element;
	struct element_s *cur;
	int nr_tests;
	int nr_cur;
	int blocksize;
} tester;

void
document (char *xml)
{
	if (tester.elements) {
		struct element_s *tmp;
		for (; tester.elements; tester.elements = tmp) {
			tmp = tester.elements->next;
			free (tester.elements);
		}
	}
	tester.doc = xml;
	tester.len = strlen (xml);
	tester.elements = NULL;
	tester.last_element = NULL;
	tester.nr_tests++;
}

void
element (enum ikstype type, ...)
{
	struct element_s *el;
	va_list ap;
	char *tmp;

	el = malloc (sizeof (struct element_s));
	memset (el, 0, sizeof (struct element_s));
	el->type = type;

	va_start (ap, type);
	switch (type) {
		case IKS_TAG:
			el->tag = va_arg (ap, int);
			el->name = va_arg (ap, char*);
			if (IKS_CLOSE == el->tag) break;
			while (1) {
				tmp = va_arg (ap, char*);
				if (tmp) {
					el->atts[el->nr_atts] = tmp;
					el->vals[el->nr_atts] = va_arg (ap, char*);
					el->nr_atts++;
				} else {
					break;
				}
			}
			break;
		case IKS_CDATA:
			tmp = va_arg (ap, char*);
			el->cdata = tmp;
			el->len = strlen (tmp);
			break;
		case IKS_NONE:
		case IKS_ATTRIBUTE:
			puts ("invalid element() call");
			exit (1);
	}
	va_end (ap);

	if (NULL == tester.elements) tester.elements = el;
	if (tester.last_element) tester.last_element->next = el;
	tester.last_element = el;
}

#define PRINT_TEST 	printf ("Sax test %d, blocksize %d, element %d:\n", tester.nr_tests, tester.blocksize, tester.nr_cur)
#define NEXT_ELEM { tester.cur = tester.cur->next; tester.nr_cur++; }

void
debug_tag (enum ikstagtype type, char *name, char **atts)
{
	int i;

	PRINT_TEST;
	if (tester.cur && tester.cur->type == IKS_TAG) {
		switch (tester.cur->tag) {
			case IKS_OPEN:
				printf ("  Expecting tag <%s>\n", tester.cur->name);
				break;
			case IKS_CLOSE:
				printf ("  Expecting tag </%s>\n", tester.cur->name);
				break;
			case IKS_SINGLE:
				printf ("  Expecting tag <%s/>\n", tester.cur->name);
				break;
		}
		for (i = 0; i < tester.cur->nr_atts; i++) {
			printf ("    %s='%s'\n", tester.cur->atts[i], tester.cur->vals[i]);
		}
	} else {
		printf ("  Not expecting a tag here.\n");
	}
	switch (type) {
		case IKS_OPEN:
			printf ("  Got tag <%s>\n", name);
			break;
		case IKS_CLOSE:
			printf ("  Got tag </%s>\n", name);
			break;
		case IKS_SINGLE:
			printf ("  Got tag <%s/>\n", name);
			break;
	}
	i = 0;
	while (atts && atts[i]) {
		printf ("    %s='%s'\n", atts[i], atts[i+1]);
		i += 2;
	}
}

#define TAG_FAIL { debug_tag (type,name,atts); exit (1); }

int
tagHook (void *udata, char *name, char **atts, int type)
{
	int nr, i, flag;

	if (!tester.cur) TAG_FAIL;
	if (tester.cur->type != IKS_TAG) TAG_FAIL;
	if (tester.cur->tag != type) TAG_FAIL;
	if (iks_strcmp (tester.cur->name, name) != 0) TAG_FAIL;
	if (!atts && tester.cur->nr_atts > 0) TAG_FAIL;
	if (atts && tester.cur->nr_atts == 0) TAG_FAIL;

	nr = tester.cur->nr_atts;
	while (nr) {
		flag = 0;
		for (i = 0;atts[i]; i+= 2) {
			if (iks_strcmp (atts[i], tester.cur->atts[nr-1]) == 0 && iks_strcmp (atts[i+1], tester.cur->vals[nr-1]) == 0) {
				flag = 1;
				break;
			}
		}
		if (flag == 0) TAG_FAIL;
		nr--;
	}

	NEXT_ELEM;
	return IKS_OK;
}

void
debug_cdata (char *data, size_t len, int pos)
{
	int i;

	PRINT_TEST;
	if (tester.cur && tester.cur->type == IKS_CDATA)
		printf ("  Expecting cdata [%s]\n", tester.cur->cdata);
	else
		printf ("  Not expecting cdata here\n");
	printf ("  Got cdata [");
	for (i = 0; i < len; i++) putchar (data[i]);
	printf ("] at the pos %d.\n", pos);
}

#define CDATA_FAIL { debug_cdata (data, len, pos); exit (1); }

int
cdataHook (void *udata, char *data, size_t len)
{
	static int pos = 0;

	if (!tester.cur) CDATA_FAIL;
	if (tester.cur->type != IKS_CDATA) CDATA_FAIL;
	if (iks_strncmp (tester.cur->cdata + pos, data, len) != 0) CDATA_FAIL;
	pos += len;
	if (pos > tester.cur->len) CDATA_FAIL;
	if (pos == tester.cur->len) {
		pos = 0;
		NEXT_ELEM;
	}
	return IKS_OK;
}

void
test_size (int blocksize)
{
	enum ikserror err;
	iksparser *prs;
	int i, len;

	tester.cur = tester.elements;
	tester.nr_cur = 1;
	tester.blocksize = blocksize;
	len = tester.len;

	prs = iks_sax_new (NULL, tagHook, cdataHook);
	i = 0;
	if (0 == blocksize) blocksize = len;
	while (i < len) {
		if (i + blocksize > len) blocksize = len - i;
		err = iks_parse (prs, tester.doc + i, blocksize, 0);
		switch (err) {
			case IKS_OK:
				break;
			case IKS_NOMEM:
				exit (1);
			case IKS_BADXML:
				PRINT_TEST;
				printf ("Invalid xml at byte %ld in\n[%s]\n", iks_nr_bytes (prs), tester.doc);
				exit (1);
			case IKS_HOOK:
				exit (1);
		}
		i += blocksize;
	}
	if (tester.cur) exit (1);
	iks_parser_delete (prs);
}

void
test (void)
{
	int i;

	for (i = 0; i < tester.len; i++) {
		test_size (i);
	}
}

void
test_bad (int badbyte)
{
	iksparser *p;
	enum ikserror err;

	p = iks_sax_new (NULL, NULL, NULL);
	err = iks_parse (p, tester.doc, tester.len, 1);
	switch (err) {
		case IKS_OK:
			break;
		case IKS_NOMEM:
			exit (1);
		case IKS_BADXML:
			if (iks_nr_bytes (p) == badbyte) return;
			break;
		case IKS_HOOK:
			exit (1);
	}
	printf ("Sax test %d:\n", tester.nr_tests);
	printf ("Expected bad byte %d, got %ld in\n[%s]\n", badbyte, iks_nr_bytes (p), tester.doc);
	exit (1);
}

/* No more attributes */
#define NMA ((char *) 0)

int
main (int argc, char *argv[])
{
	document ("<lonely/>");
	element (IKS_TAG, IKS_SINGLE, "lonely", NMA);
	test ();

	document ("<?xml version='1.0'?><parent><child/><child/>child</parent>");
	element (IKS_TAG, IKS_OPEN, "parent", NMA);
	element (IKS_TAG, IKS_SINGLE, "child", NMA);
	element (IKS_TAG, IKS_SINGLE, "child", NMA);
	element (IKS_CDATA, "child");
	element (IKS_TAG, IKS_CLOSE, "parent");
	test ();

	document ("<mytag abc='123' id=\"XC72\"></mytag>");
	element (IKS_TAG, IKS_OPEN, "mytag", "abc", "123", "id", "XC72", NMA);
	element (IKS_TAG, IKS_CLOSE, "mytag");
	test ();

	document ("<body>I&apos;m fixing parser&amp;tester for &quot;&lt;&quot; and &quot;&gt;&quot; chars.</body>");
	element (IKS_TAG, IKS_OPEN, "body", NMA);
	element (IKS_CDATA, "I'm fixing parser&tester for \"<\" and \">\" chars.");
	element (IKS_TAG, IKS_CLOSE, "body");
	test ();

	document ("<tag a='1' b='2' c='3' d='4' e='5' f='6' g='7' id='xyz9'><sub></sub></tag>");
	element (IKS_TAG, IKS_OPEN, "tag", "a", "1", "b", "2", "c", "3", "d", "4", "e", "5", "f", "6", "g", "7", "id", "xyz9", NMA);
	element (IKS_TAG, IKS_OPEN, "sub", NMA);
	element (IKS_TAG, IKS_CLOSE, "sub");
	element (IKS_TAG, IKS_CLOSE, "tag");
	test ();

	document ("<item url='http://jabber.org'><!-- little comment -->Jabber Site</item>");
	element (IKS_TAG, IKS_OPEN, "item", "url", "http://jabber.org", NMA);
	element (IKS_CDATA, "Jabber Site");
	element (IKS_TAG, IKS_CLOSE, "item");
	test ();

	document ("<index><!-- <item> - tag has no childs --><item name='lala' page='42'/></index>");
	element (IKS_TAG, IKS_OPEN, "index", NMA);
	element (IKS_TAG, IKS_SINGLE, "item", "name", "lala", "page", "42", NMA);
	element (IKS_TAG, IKS_CLOSE, "index");
	test ();

	document ("<ka>1234<![CDATA[ <ka> lala ] ]] ]]] ]]>4321</ka>");
	element (IKS_TAG, IKS_OPEN, "ka", NMA);
	element (IKS_CDATA, "1234 <ka> lala ] ]] ]]] 4321");
	element (IKS_TAG, IKS_CLOSE, "ka");
	test ();

	document ("<test><standalone be='happy'/>abcd<br/>&lt;escape&gt;</test>");
	element (IKS_TAG, IKS_OPEN, "test", NMA);
	element (IKS_TAG, IKS_SINGLE, "standalone", "be", "happy", NMA);
	element (IKS_CDATA, "abcd");
	element (IKS_TAG, IKS_SINGLE, "br", NMA);
	element (IKS_CDATA, "<escape>");
	element (IKS_TAG, IKS_CLOSE, "test");
	test ();

	document ("<a><b>john&amp;mary<c><d e='f' g='123456' h='madcat' klm='nop'/></c></b></a>");
	element (IKS_TAG, IKS_OPEN, "a", NMA);
	element (IKS_TAG, IKS_OPEN, "b", NMA);
	element (IKS_CDATA, "john&mary");
	element (IKS_TAG, IKS_OPEN, "c", NMA);
	element (IKS_TAG, IKS_SINGLE, "d", "e", "f", "g", "123456", "h", "madcat", "klm", "nop", NMA);
	element (IKS_TAG, IKS_CLOSE, "c", NMA);
	element (IKS_TAG, IKS_CLOSE, "b", NMA);
	element (IKS_TAG, IKS_CLOSE, "a", NMA);
	test ();

	document ("<a><b x1 ='lala'/><c x2\t= \t'bibi'/></a>");
	element (IKS_TAG, IKS_OPEN, "a", NMA);
	element (IKS_TAG, IKS_SINGLE, "b", "x1", "lala", NMA);
	element (IKS_TAG, IKS_SINGLE, "c", "x2", "bibi", NMA);
	element (IKS_TAG, IKS_CLOSE, "a", NMA);
	test ();

	document ("<a>[[bg:Чингис хан]][[bn:চেঙ্গিজ খান]]</a>");
	element (IKS_TAG, IKS_OPEN, "a", NMA);
	element (IKS_CDATA, "[[bg:Чингис хан]][[bn:চেঙ্গিজ খান]]");
	element (IKS_TAG, IKS_CLOSE, "a", NMA);
	test ();

	document ("<test>\xFF</test>");
	test_bad (6);

	document ("<t\0></t>");
	tester.len = 8;
	test_bad (2);

	document ("<a><b/><c></c/></a>");
	test_bad (13);

	document ("<e><!-- -- --></e>");
	test_bad (10);

	document ("<g><test a='123'/ b='lala'></g>");
	test_bad (17);

	document ("<ha><!-- <lala> --><!- comment -></ha>");
	test_bad (22);

	document ("<lol>&lt;<&gt;</lol>");
	test_bad (16);

	document ("<a>\xC0\x80</a>");
	test_bad (4);

	document ("<\x8F\x85></\x8F\x85>");
	test_bad (1);

	document ("<utf8>\xC1\x80<br/>\xED\x95\x9C\xEA\xB5\xAD\xEC\x96\xB4<err>\xC1\x65</err></utf8>");
	test_bad (7);

	return 0;
}