• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 #ifdef LIBXML_PUSH_ENABLED
10 static int
testHugePush(void)11 testHugePush(void) {
12     xmlParserCtxtPtr ctxt;
13     int err, i;
14 
15     ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
16 
17     /*
18      * Push parse a document larger than XML_MAX_LOOKUP_LIMIT
19      * (10,000,000 bytes). This mainly tests whether shrinking the
20      * buffer works when push parsing.
21      */
22     xmlParseChunk(ctxt, "<doc>", 5, 0);
23     for (i = 0; i < 1000000; i++)
24         xmlParseChunk(ctxt, "<elem>text</elem>", 17, 0);
25     xmlParseChunk(ctxt, "</doc>", 6, 1);
26 
27     err = ctxt->wellFormed ? 0 : 1;
28     xmlFreeDoc(ctxt->myDoc);
29     xmlFreeParserCtxt(ctxt);
30 
31     return err;
32 }
33 
34 static int
testHugeEncodedChunk(void)35 testHugeEncodedChunk(void) {
36     xmlBufferPtr buf;
37     xmlChar *chunk;
38     xmlParserCtxtPtr ctxt;
39     int err, i;
40 
41     /*
42      * Test the push parser with a built-in encoding handler like ISO-8859-1
43      * and a chunk larger than the initial decoded buffer (currently 4 KB).
44      */
45     buf = xmlBufferCreate();
46     xmlBufferCat(buf,
47             BAD_CAST "<?xml version='1.0' encoding='ISO-8859-1'?>\n");
48     xmlBufferCat(buf, BAD_CAST "<doc><!-- ");
49     for (i = 0; i < 2000; i++)
50         xmlBufferCat(buf, BAD_CAST "0123456789");
51     xmlBufferCat(buf, BAD_CAST " --></doc>");
52     chunk = xmlBufferDetach(buf);
53     xmlBufferFree(buf);
54 
55     ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
56 
57     xmlParseChunk(ctxt, (char *) chunk, xmlStrlen(chunk), 0);
58     xmlParseChunk(ctxt, NULL, 0, 1);
59 
60     err = ctxt->wellFormed ? 0 : 1;
61     xmlFreeDoc(ctxt->myDoc);
62     xmlFreeParserCtxt(ctxt);
63     xmlFree(chunk);
64 
65     return err;
66 }
67 #endif
68 
69 int
main(void)70 main(void) {
71     int err = 0;
72 
73 #ifdef LIBXML_PUSH_ENABLED
74     err |= testHugePush();
75     err |= testHugeEncodedChunk();
76 #endif
77 
78     return err;
79 }
80 
81