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/HTMLparser.h>
|
---|
9 |
|
---|
10 | static int
|
---|
11 | testStandaloneWithEncoding(void) {
|
---|
12 | xmlDocPtr doc;
|
---|
13 | const char *str =
|
---|
14 | "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
|
---|
15 | "<doc></doc>\n";
|
---|
16 | int err = 0;
|
---|
17 |
|
---|
18 | xmlResetLastError();
|
---|
19 |
|
---|
20 | doc = xmlReadDoc(BAD_CAST str, NULL, "UTF-8", 0);
|
---|
21 | if (doc == NULL) {
|
---|
22 | fprintf(stderr, "xmlReadDoc failed\n");
|
---|
23 | err = 1;
|
---|
24 | }
|
---|
25 | xmlFreeDoc(doc);
|
---|
26 |
|
---|
27 | return err;
|
---|
28 | }
|
---|
29 |
|
---|
30 | #ifdef LIBXML_PUSH_ENABLED
|
---|
31 | static int
|
---|
32 | testHugePush(void) {
|
---|
33 | xmlParserCtxtPtr ctxt;
|
---|
34 | int err, i;
|
---|
35 |
|
---|
36 | ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * Push parse a document larger than XML_MAX_LOOKUP_LIMIT
|
---|
40 | * (10,000,000 bytes). This mainly tests whether shrinking the
|
---|
41 | * buffer works when push parsing.
|
---|
42 | */
|
---|
43 | xmlParseChunk(ctxt, "<doc>", 5, 0);
|
---|
44 | for (i = 0; i < 1000000; i++)
|
---|
45 | xmlParseChunk(ctxt, "<elem>text</elem>", 17, 0);
|
---|
46 | xmlParseChunk(ctxt, "</doc>", 6, 1);
|
---|
47 |
|
---|
48 | err = ctxt->wellFormed ? 0 : 1;
|
---|
49 | xmlFreeDoc(ctxt->myDoc);
|
---|
50 | xmlFreeParserCtxt(ctxt);
|
---|
51 |
|
---|
52 | return err;
|
---|
53 | }
|
---|
54 |
|
---|
55 | static int
|
---|
56 | testHugeEncodedChunk(void) {
|
---|
57 | xmlBufferPtr buf;
|
---|
58 | xmlChar *chunk;
|
---|
59 | xmlParserCtxtPtr ctxt;
|
---|
60 | int err, i;
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * Test the push parser with a built-in encoding handler like ISO-8859-1
|
---|
64 | * and a chunk larger than the initial decoded buffer (currently 4 KB).
|
---|
65 | */
|
---|
66 | buf = xmlBufferCreate();
|
---|
67 | xmlBufferCat(buf,
|
---|
68 | BAD_CAST "<?xml version='1.0' encoding='ISO-8859-1'?>\n");
|
---|
69 | xmlBufferCat(buf, BAD_CAST "<doc><!-- ");
|
---|
70 | for (i = 0; i < 2000; i++)
|
---|
71 | xmlBufferCat(buf, BAD_CAST "0123456789");
|
---|
72 | xmlBufferCat(buf, BAD_CAST " --></doc>");
|
---|
73 | chunk = xmlBufferDetach(buf);
|
---|
74 | xmlBufferFree(buf);
|
---|
75 |
|
---|
76 | ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
|
---|
77 |
|
---|
78 | xmlParseChunk(ctxt, (char *) chunk, xmlStrlen(chunk), 0);
|
---|
79 | xmlParseChunk(ctxt, NULL, 0, 1);
|
---|
80 |
|
---|
81 | err = ctxt->wellFormed ? 0 : 1;
|
---|
82 | xmlFreeDoc(ctxt->myDoc);
|
---|
83 | xmlFreeParserCtxt(ctxt);
|
---|
84 | xmlFree(chunk);
|
---|
85 |
|
---|
86 | return err;
|
---|
87 | }
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | #if defined(LIBXML_HTML_ENABLED) && defined(LIBXML_PUSH_ENABLED)
|
---|
91 | static int
|
---|
92 | testHtmlPushWithEncoding(void) {
|
---|
93 | htmlParserCtxtPtr ctxt;
|
---|
94 | htmlDocPtr doc;
|
---|
95 | htmlNodePtr node;
|
---|
96 | int err = 0;
|
---|
97 |
|
---|
98 | ctxt = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL,
|
---|
99 | XML_CHAR_ENCODING_UTF8);
|
---|
100 | htmlParseChunk(ctxt, "-\xC3\xA4-", 4, 1);
|
---|
101 |
|
---|
102 | doc = ctxt->myDoc;
|
---|
103 | if (!xmlStrEqual(doc->encoding, BAD_CAST "UTF-8")) {
|
---|
104 | fprintf(stderr, "testHtmlPushWithEncoding failed\n");
|
---|
105 | err = 1;
|
---|
106 | }
|
---|
107 |
|
---|
108 | node = xmlDocGetRootElement(doc)->children->children->children;
|
---|
109 | if (!xmlStrEqual(node->content, BAD_CAST "-\xC3\xA4-")) {
|
---|
110 | fprintf(stderr, "testHtmlPushWithEncoding failed\n");
|
---|
111 | err = 1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | xmlFreeDoc(doc);
|
---|
115 | htmlFreeParserCtxt(ctxt);
|
---|
116 | return err;
|
---|
117 | }
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | int
|
---|
121 | main(void) {
|
---|
122 | int err = 0;
|
---|
123 |
|
---|
124 | err |= testStandaloneWithEncoding();
|
---|
125 | #ifdef LIBXML_PUSH_ENABLED
|
---|
126 | err |= testHugePush();
|
---|
127 | err |= testHugeEncodedChunk();
|
---|
128 | #endif
|
---|
129 | #if defined(LIBXML_HTML_ENABLED) && defined(LIBXML_PUSH_ENABLED)
|
---|
130 | err |= testHtmlPushWithEncoding();
|
---|
131 | #endif
|
---|
132 |
|
---|
133 | return err;
|
---|
134 | }
|
---|
135 |
|
---|