VirtualBox

source: vbox/trunk/src/libs/libxml2-2.13.2/testparser.c@ 105945

Last change on this file since 105945 was 105420, checked in by vboxsync, 6 months ago

libxml2-2.12.6: Applied and adjusted our libxml2 changes to 2.12.6. bugref:10730

File size: 13.7 KB
Line 
1/*
2 * testparser.c: Additional parser tests
3 *
4 * See Copyright for the status of this software.
5 */
6
7#include <libxml/parser.h>
8#include <libxml/uri.h>
9#include <libxml/xmlreader.h>
10#include <libxml/xmlwriter.h>
11#include <libxml/HTMLparser.h>
12
13#include <string.h>
14
15static int
16testNewDocNode(void) {
17 xmlNodePtr node;
18 int err = 0;
19
20 node = xmlNewDocNode(NULL, NULL, BAD_CAST "c", BAD_CAST "");
21 if (node->children != NULL) {
22 fprintf(stderr, "empty node has children\n");
23 err = 1;
24 }
25 xmlFreeNode(node);
26
27 return err;
28}
29
30static int
31testStandaloneWithEncoding(void) {
32 xmlDocPtr doc;
33 const char *str =
34 "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
35 "<doc></doc>\n";
36 int err = 0;
37
38 xmlResetLastError();
39
40 doc = xmlReadDoc(BAD_CAST str, NULL, "UTF-8", 0);
41 if (doc == NULL) {
42 fprintf(stderr, "xmlReadDoc failed\n");
43 err = 1;
44 }
45 xmlFreeDoc(doc);
46
47 return err;
48}
49
50static int
51testUnsupportedEncoding(void) {
52 xmlDocPtr doc;
53 const xmlError *error;
54 int err = 0;
55
56 xmlResetLastError();
57
58 doc = xmlReadDoc(BAD_CAST "<doc/>", NULL, "#unsupported",
59 XML_PARSE_NOWARNING);
60 if (doc == NULL) {
61 fprintf(stderr, "xmlReadDoc failed with unsupported encoding\n");
62 err = 1;
63 }
64 xmlFreeDoc(doc);
65
66 error = xmlGetLastError();
67 if (error == NULL ||
68 error->code != XML_ERR_UNSUPPORTED_ENCODING ||
69 error->level != XML_ERR_WARNING ||
70 strcmp(error->message, "Unsupported encoding: #unsupported\n") != 0)
71 {
72 fprintf(stderr, "xmlReadDoc failed to raise correct error\n");
73 err = 1;
74 }
75
76 return err;
77}
78
79static int
80testNodeGetContent(void) {
81 xmlDocPtr doc;
82 xmlChar *content;
83 int err = 0;
84
85 doc = xmlReadDoc(BAD_CAST "<doc/>", NULL, NULL, 0);
86 xmlAddChild(doc->children, xmlNewReference(doc, BAD_CAST "lt"));
87 content = xmlNodeGetContent((xmlNodePtr) doc);
88 if (strcmp((char *) content, "<") != 0) {
89 fprintf(stderr, "xmlNodeGetContent failed\n");
90 err = 1;
91 }
92 xmlFree(content);
93 xmlFreeDoc(doc);
94
95 return err;
96}
97
98#ifdef LIBXML_SAX1_ENABLED
99static int
100testBalancedChunk(void) {
101 xmlNodePtr list;
102 xmlNodePtr elem;
103 int ret;
104 int err = 0;
105
106 ret = xmlParseBalancedChunkMemory(NULL, NULL, NULL, 0,
107 BAD_CAST "start <node xml:lang='en'>abc</node> end", &list);
108
109 if ((ret != XML_ERR_OK) ||
110 (list == NULL) ||
111 ((elem = list->next) == NULL) ||
112 (elem->type != XML_ELEMENT_NODE) ||
113 (elem->nsDef == NULL) ||
114 (!xmlStrEqual(elem->nsDef->href, XML_XML_NAMESPACE))) {
115 fprintf(stderr, "xmlParseBalancedChunkMemory failed\n");
116 err = 1;
117 }
118
119 xmlFreeNodeList(list);
120
121 return(err);
122}
123#endif
124
125#ifdef LIBXML_PUSH_ENABLED
126static int
127testHugePush(void) {
128 xmlParserCtxtPtr ctxt;
129 int err, i;
130
131 ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
132
133 /*
134 * Push parse a document larger than XML_MAX_LOOKUP_LIMIT
135 * (10,000,000 bytes). This mainly tests whether shrinking the
136 * buffer works when push parsing.
137 */
138 xmlParseChunk(ctxt, "<doc>", 5, 0);
139 for (i = 0; i < 1000000; i++)
140 xmlParseChunk(ctxt, "<elem>text</elem>", 17, 0);
141 xmlParseChunk(ctxt, "</doc>", 6, 1);
142
143 err = ctxt->wellFormed ? 0 : 1;
144 xmlFreeDoc(ctxt->myDoc);
145 xmlFreeParserCtxt(ctxt);
146
147 return err;
148}
149
150static int
151testHugeEncodedChunk(void) {
152 xmlBufferPtr buf;
153 xmlChar *chunk;
154 xmlParserCtxtPtr ctxt;
155 int err, i;
156
157 /*
158 * Test the push parser with a built-in encoding handler like ISO-8859-1
159 * and a chunk larger than the initial decoded buffer (currently 4 KB).
160 */
161 buf = xmlBufferCreate();
162 xmlBufferCat(buf,
163 BAD_CAST "<?xml version='1.0' encoding='ISO-8859-1'?>\n");
164 xmlBufferCat(buf, BAD_CAST "<doc><!-- ");
165 for (i = 0; i < 2000; i++)
166 xmlBufferCat(buf, BAD_CAST "0123456789");
167 xmlBufferCat(buf, BAD_CAST " --></doc>");
168 chunk = xmlBufferDetach(buf);
169 xmlBufferFree(buf);
170
171 ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
172
173 xmlParseChunk(ctxt, (char *) chunk, xmlStrlen(chunk), 0);
174 xmlParseChunk(ctxt, NULL, 0, 1);
175
176 err = ctxt->wellFormed ? 0 : 1;
177 xmlFreeDoc(ctxt->myDoc);
178 xmlFreeParserCtxt(ctxt);
179 xmlFree(chunk);
180
181 return err;
182}
183
184#ifdef LIBXML_HTML_ENABLED
185static int
186testHtmlPushWithEncoding(void) {
187 htmlParserCtxtPtr ctxt;
188 htmlDocPtr doc;
189 htmlNodePtr node;
190 int err = 0;
191
192 ctxt = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL,
193 XML_CHAR_ENCODING_UTF8);
194 htmlParseChunk(ctxt, "-\xC3\xA4-", 4, 1);
195
196 doc = ctxt->myDoc;
197 if (!xmlStrEqual(doc->encoding, BAD_CAST "UTF-8")) {
198 fprintf(stderr, "testHtmlPushWithEncoding failed\n");
199 err = 1;
200 }
201
202 node = xmlDocGetRootElement(doc)->children->children->children;
203 if (!xmlStrEqual(node->content, BAD_CAST "-\xC3\xA4-")) {
204 fprintf(stderr, "testHtmlPushWithEncoding failed\n");
205 err = 1;
206 }
207
208 xmlFreeDoc(doc);
209 htmlFreeParserCtxt(ctxt);
210 return err;
211}
212#endif
213#endif
214
215#ifdef LIBXML_READER_ENABLED
216static int
217testReaderEncoding(void) {
218 xmlBuffer *buf;
219 xmlTextReader *reader;
220 xmlChar *xml;
221 const xmlChar *encoding;
222 int err = 0;
223 int i;
224
225 buf = xmlBufferCreate();
226 xmlBufferCCat(buf, "<?xml version='1.0' encoding='ISO-8859-1'?>\n");
227 xmlBufferCCat(buf, "<doc>");
228 for (i = 0; i < 8192; i++)
229 xmlBufferCCat(buf, "x");
230 xmlBufferCCat(buf, "</doc>");
231 xml = xmlBufferDetach(buf);
232 xmlBufferFree(buf);
233
234 reader = xmlReaderForDoc(BAD_CAST xml, NULL, NULL, 0);
235 xmlTextReaderRead(reader);
236 encoding = xmlTextReaderConstEncoding(reader);
237
238 if (!xmlStrEqual(encoding, BAD_CAST "ISO-8859-1")) {
239 fprintf(stderr, "testReaderEncoding failed\n");
240 err = 1;
241 }
242
243 xmlFreeTextReader(reader);
244 xmlFree(xml);
245 return err;
246}
247
248static int
249testReaderContent(void) {
250 xmlTextReader *reader;
251 const xmlChar *xml = BAD_CAST "<d>x<e>y</e><f>z</f></d>";
252 xmlChar *string;
253 int err = 0;
254
255 reader = xmlReaderForDoc(xml, NULL, NULL, 0);
256 xmlTextReaderRead(reader);
257
258 string = xmlTextReaderReadOuterXml(reader);
259 if (!xmlStrEqual(string, xml)) {
260 fprintf(stderr, "xmlTextReaderReadOuterXml failed\n");
261 err = 1;
262 }
263 xmlFree(string);
264
265 string = xmlTextReaderReadInnerXml(reader);
266 if (!xmlStrEqual(string, BAD_CAST "x<e>y</e><f>z</f>")) {
267 fprintf(stderr, "xmlTextReaderReadInnerXml failed\n");
268 err = 1;
269 }
270 xmlFree(string);
271
272 string = xmlTextReaderReadString(reader);
273 if (!xmlStrEqual(string, BAD_CAST "xyz")) {
274 fprintf(stderr, "xmlTextReaderReadString failed\n");
275 err = 1;
276 }
277 xmlFree(string);
278
279 xmlFreeTextReader(reader);
280 return err;
281}
282
283#ifdef LIBXML_XINCLUDE_ENABLED
284typedef struct {
285 char *message;
286 int code;
287} testReaderErrorCtxt;
288
289static void
290testReaderError(void *arg, const char *msg,
291 xmlParserSeverities severity ATTRIBUTE_UNUSED,
292 xmlTextReaderLocatorPtr locator ATTRIBUTE_UNUSED) {
293 testReaderErrorCtxt *ctxt = arg;
294
295 if (ctxt->message != NULL)
296 xmlFree(ctxt->message);
297 ctxt->message = xmlMemStrdup(msg);
298}
299
300static void
301testStructuredReaderError(void *arg, const xmlError *error) {
302 testReaderErrorCtxt *ctxt = arg;
303
304 if (ctxt->message != NULL)
305 xmlFree(ctxt->message);
306 ctxt->message = xmlMemStrdup(error->message);
307 ctxt->code = error->code;
308}
309
310static int
311testReaderXIncludeError(void) {
312 /*
313 * Test whether XInclude errors are reported to the custom error
314 * handler of a reader.
315 */
316 const char *doc =
317 "<doc xmlns:xi='http://www.w3.org/2001/XInclude'>\n"
318 " <xi:include/>\n"
319 "</doc>\n";
320 xmlTextReader *reader;
321 testReaderErrorCtxt errorCtxt;
322 int err = 0;
323
324 reader = xmlReaderForDoc(BAD_CAST doc, NULL, NULL, XML_PARSE_XINCLUDE);
325 xmlTextReaderSetErrorHandler(reader, testReaderError, &errorCtxt);
326 errorCtxt.message = NULL;
327 errorCtxt.code = 0;
328 while (xmlTextReaderRead(reader) > 0)
329 ;
330
331 if (errorCtxt.message == NULL ||
332 strstr(errorCtxt.message, "href or xpointer") == NULL) {
333 fprintf(stderr, "xmlTextReaderSetErrorHandler failed\n");
334 err = 1;
335 }
336
337 xmlFree(errorCtxt.message);
338 xmlFreeTextReader(reader);
339
340 reader = xmlReaderForDoc(BAD_CAST doc, NULL, NULL, XML_PARSE_XINCLUDE);
341 xmlTextReaderSetStructuredErrorHandler(reader, testStructuredReaderError,
342 &errorCtxt);
343 errorCtxt.message = NULL;
344 errorCtxt.code = 0;
345 while (xmlTextReaderRead(reader) > 0)
346 ;
347
348 if (errorCtxt.code != XML_XINCLUDE_NO_HREF ||
349 errorCtxt.message == NULL ||
350 strstr(errorCtxt.message, "href or xpointer") == NULL) {
351 fprintf(stderr, "xmlTextReaderSetStructuredErrorHandler failed\n");
352 err = 1;
353 }
354
355 xmlFree(errorCtxt.message);
356 xmlFreeTextReader(reader);
357
358 return err;
359}
360#endif
361#endif
362
363#ifdef LIBXML_WRITER_ENABLED
364static int
365testWriterIOWrite(void *ctxt, const char *data, int len) {
366 (void) ctxt;
367 (void) data;
368
369 return len;
370}
371
372static int
373testWriterIOClose(void *ctxt) {
374 (void) ctxt;
375
376 return XML_IO_ENAMETOOLONG;
377}
378
379static int
380testWriterClose(void){
381 xmlOutputBufferPtr out;
382 xmlTextWriterPtr writer;
383 int err = 0;
384 int result;
385
386 out = xmlOutputBufferCreateIO(testWriterIOWrite, testWriterIOClose,
387 NULL, NULL);
388 writer = xmlNewTextWriter(out);
389 xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);
390 xmlTextWriterStartElement(writer, BAD_CAST "elem");
391 xmlTextWriterEndElement(writer);
392 xmlTextWriterEndDocument(writer);
393 result = xmlTextWriterClose(writer);
394
395 if (result != XML_IO_ENAMETOOLONG) {
396 fprintf(stderr, "xmlTextWriterClose reported wrong error %d\n",
397 result);
398 err = 1;
399 }
400
401 xmlFreeTextWriter(writer);
402 return err;
403}
404#endif
405
406typedef struct {
407 const char *uri;
408 const char *base;
409 const char *result;
410} xmlRelativeUriTest;
411
412static int
413testBuildRelativeUri(void) {
414 xmlChar *res;
415 int err = 0;
416 int i;
417
418 static const xmlRelativeUriTest tests[] = {
419 {
420 "/a/b1/c1",
421 "/a/b2/c2",
422 "../b1/c1"
423 }, {
424 "a/b1/c1",
425 "a/b2/c2",
426 "../b1/c1"
427 }, {
428 "a/././b1/x/y/../z/../.././c1",
429 "./a/./b2/././b2",
430 "../b1/c1"
431 }, {
432 "file:///a/b1/c1",
433 "/a/b2/c2",
434 NULL
435 }, {
436 "/a/b1/c1",
437 "file:///a/b2/c2",
438 NULL
439 }, {
440 "a/b1/c1",
441 "/a/b2/c2",
442 NULL
443 }, {
444 "/a/b1/c1",
445 "a/b2/c2",
446 NULL
447 }, {
448 "http://example.org/a/b1/c1",
449 "http://example.org/a/b2/c2",
450 "../b1/c1"
451 }, {
452 "http://example.org/a/b1/c1",
453 "https://example.org/a/b2/c2",
454 NULL
455 }, {
456 "http://example.org/a/b1/c1",
457 "http://localhost/a/b2/c2",
458 NULL
459 }, {
460 "with space/x x/y y",
461 "with space/b2/c2",
462 "../x%20x/y%20y"
463 }, {
464 "with space/x x/y y",
465 "/b2/c2",
466 "with%20space/x%20x/y%20y"
467 }
468#if defined(_WIN32) || defined(__CYGWIN__)
469 , {
470 "\\a\\b1\\c1",
471 "\\a\\b2\\c2",
472 "../b1/c1"
473 }, {
474 "\\a\\b1\\c1",
475 "/a/b2/c2",
476 "../b1/c1"
477 }, {
478 "a\\b1\\c1",
479 "a/b2/c2",
480 "../b1/c1"
481 }, {
482 "file://server/a/b1/c1",
483 "\\\\?\\UNC\\server\\a\\b2\\c2",
484 "../b1/c1"
485 }, {
486 "file://server/a/b1/c1",
487 "\\\\server\\a\\b2\\c2",
488 "../b1/c1"
489 }, {
490 "file:///x:/a/b1/c1",
491 "x:\\a\\b2\\c2",
492 "../b1/c1"
493 }, {
494 "file:///x:/a/b1/c1",
495 "\\\\?\\x:\\a\\b2\\c2",
496 "../b1/c1"
497 }, {
498 "file:///x:/a/b1/c1",
499 "file:///y:/a/b2/c2",
500 NULL
501 }, {
502 "x:/a/b1/c1",
503 "y:/a/b2/c2",
504 "file:///x:/a/b1/c1"
505 }, {
506 "/a/b1/c1",
507 "y:/a/b2/c2",
508 NULL
509 }, {
510 "\\\\server\\a\\b1\\c1",
511 "a/b2/c2",
512 "file://server/a/b1/c1"
513 }
514#endif
515 };
516
517 for (i = 0; (size_t) i < sizeof(tests) / sizeof(tests[0]); i++) {
518 const xmlRelativeUriTest *test = tests + i;
519 const char *expect;
520
521 res = xmlBuildRelativeURI(BAD_CAST test->uri, BAD_CAST test->base);
522 expect = test->result ? test->result : test->uri;
523 if (!xmlStrEqual(res, BAD_CAST expect)) {
524 fprintf(stderr, "xmlBuildRelativeURI failed uri=%s base=%s "
525 "result=%s expected=%s\n", test->uri, test->base,
526 res, expect);
527 err = 1;
528 }
529 xmlFree(res);
530 }
531
532 return err;
533}
534
535int
536main(void) {
537 int err = 0;
538
539 err |= testNewDocNode();
540 err |= testStandaloneWithEncoding();
541 err |= testUnsupportedEncoding();
542 err |= testNodeGetContent();
543#ifdef LIBXML_SAX1_ENABLED
544 err |= testBalancedChunk();
545#endif
546#ifdef LIBXML_PUSH_ENABLED
547 err |= testHugePush();
548 err |= testHugeEncodedChunk();
549#ifdef LIBXML_HTML_ENABLED
550 err |= testHtmlPushWithEncoding();
551#endif
552#endif
553#ifdef LIBXML_READER_ENABLED
554 err |= testReaderEncoding();
555 err |= testReaderContent();
556#ifdef LIBXML_XINCLUDE_ENABLED
557 err |= testReaderXIncludeError();
558#endif
559#endif
560#ifdef LIBXML_WRITER_ENABLED
561 err |= testWriterClose();
562#endif
563 err |= testBuildRelativeUri();
564
565 return err;
566}
567
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette