1 /* Tests in the "basic" test case for the Expat test suite
2 __ __ _
3 ___\ \/ /_ __ __ _| |_
4 / _ \\ /| '_ \ / _` | __|
5 | __// \| |_) | (_| | |_
6 \___/_/\_\ .__/ \__,_|\__|
7 |_| XML parser
8
9 Copyright (c) 2001-2006 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
10 Copyright (c) 2003 Greg Stein <gstein@users.sourceforge.net>
11 Copyright (c) 2005-2007 Steven Solie <steven@solie.ca>
12 Copyright (c) 2005-2012 Karl Waclawek <karl@waclawek.net>
13 Copyright (c) 2016-2025 Sebastian Pipping <sebastian@pipping.org>
14 Copyright (c) 2017-2022 Rhodri James <rhodri@wildebeest.org.uk>
15 Copyright (c) 2017 Joe Orton <jorton@redhat.com>
16 Copyright (c) 2017 José Gutiérrez de la Concha <jose@zeroc.com>
17 Copyright (c) 2018 Marco Maggi <marco.maggi-ipsu@poste.it>
18 Copyright (c) 2019 David Loffredo <loffredo@steptools.com>
19 Copyright (c) 2020 Tim Gates <tim.gates@iress.com>
20 Copyright (c) 2021 Donghee Na <donghee.na@python.org>
21 Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <snild@sony.com>
22 Copyright (c) 2024-2025 Berkay Eren Ürün <berkay.ueruen@siemens.com>
23 Licensed under the MIT license:
24
25 Permission is hereby granted, free of charge, to any person obtaining
26 a copy of this software and associated documentation files (the
27 "Software"), to deal in the Software without restriction, including
28 without limitation the rights to use, copy, modify, merge, publish,
29 distribute, sublicense, and/or sell copies of the Software, and to permit
30 persons to whom the Software is furnished to do so, subject to the
31 following conditions:
32
33 The above copyright notice and this permission notice shall be included
34 in all copies or substantial portions of the Software.
35
36 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
39 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
40 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
41 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
42 USE OR OTHER DEALINGS IN THE SOFTWARE.
43 */
44
45 #if defined(NDEBUG)
46 # undef NDEBUG /* because test suite relies on assert(...) at the moment */
47 #endif
48
49 #include <assert.h>
50
51 #include <stdio.h>
52 #include <string.h>
53 #include <time.h>
54
55 #if ! defined(__cplusplus)
56 # include <stdbool.h>
57 #endif
58
59 #include "expat_config.h"
60
61 #include "expat.h"
62 #include "internal.h"
63 #include "minicheck.h"
64 #include "structdata.h"
65 #include "common.h"
66 #include "dummy.h"
67 #include "handlers.h"
68 #include "siphash.h"
69 #include "basic_tests.h"
70
71 static void
basic_setup(void)72 basic_setup(void) {
73 g_parser = XML_ParserCreate(NULL);
74 if (g_parser == NULL)
75 fail("Parser not created.");
76 }
77
78 /*
79 * Character & encoding tests.
80 */
81
START_TEST(test_nul_byte)82 START_TEST(test_nul_byte) {
83 char text[] = "<doc>\0</doc>";
84
85 /* test that a NUL byte (in US-ASCII data) is an error */
86 if (_XML_Parse_SINGLE_BYTES(g_parser, text, sizeof(text) - 1, XML_TRUE)
87 == XML_STATUS_OK)
88 fail("Parser did not report error on NUL-byte.");
89 if (XML_GetErrorCode(g_parser) != XML_ERROR_INVALID_TOKEN)
90 xml_failure(g_parser);
91 }
92 END_TEST
93
START_TEST(test_u0000_char)94 START_TEST(test_u0000_char) {
95 /* test that a NUL byte (in US-ASCII data) is an error */
96 expect_failure("<doc>�</doc>", XML_ERROR_BAD_CHAR_REF,
97 "Parser did not report error on NUL-byte.");
98 }
99 END_TEST
100
START_TEST(test_siphash_self)101 START_TEST(test_siphash_self) {
102 if (! sip24_valid())
103 fail("SipHash self-test failed");
104 }
105 END_TEST
106
START_TEST(test_siphash_spec)107 START_TEST(test_siphash_spec) {
108 /* https://131002.net/siphash/siphash.pdf (page 19, "Test values") */
109 const char message[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"
110 "\x0a\x0b\x0c\x0d\x0e";
111 const size_t len = sizeof(message) - 1;
112 const uint64_t expected = SIP_ULL(0xa129ca61U, 0x49be45e5U);
113 struct siphash state;
114 struct sipkey key;
115
116 sip_tokey(&key, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"
117 "\x0a\x0b\x0c\x0d\x0e\x0f");
118 sip24_init(&state, &key);
119
120 /* Cover spread across calls */
121 sip24_update(&state, message, 4);
122 sip24_update(&state, message + 4, len - 4);
123
124 /* Cover null length */
125 sip24_update(&state, message, 0);
126
127 if (sip24_final(&state) != expected)
128 fail("sip24_final failed spec test\n");
129
130 /* Cover wrapper */
131 if (siphash24(message, len, &key) != expected)
132 fail("siphash24 failed spec test\n");
133 }
134 END_TEST
135
START_TEST(test_bom_utf8)136 START_TEST(test_bom_utf8) {
137 /* This test is really just making sure we don't core on a UTF-8 BOM. */
138 const char *text = "\357\273\277<e/>";
139
140 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
141 == XML_STATUS_ERROR)
142 xml_failure(g_parser);
143 }
144 END_TEST
145
START_TEST(test_bom_utf16_be)146 START_TEST(test_bom_utf16_be) {
147 char text[] = "\376\377\0<\0e\0/\0>";
148
149 if (_XML_Parse_SINGLE_BYTES(g_parser, text, sizeof(text) - 1, XML_TRUE)
150 == XML_STATUS_ERROR)
151 xml_failure(g_parser);
152 }
153 END_TEST
154
START_TEST(test_bom_utf16_le)155 START_TEST(test_bom_utf16_le) {
156 char text[] = "\377\376<\0e\0/\0>\0";
157
158 if (_XML_Parse_SINGLE_BYTES(g_parser, text, sizeof(text) - 1, XML_TRUE)
159 == XML_STATUS_ERROR)
160 xml_failure(g_parser);
161 }
162 END_TEST
163
START_TEST(test_nobom_utf16_le)164 START_TEST(test_nobom_utf16_le) {
165 char text[] = " \0<\0e\0/\0>\0";
166
167 if (g_chunkSize == 1) {
168 // TODO: with just the first byte, we can't tell the difference between
169 // UTF-16-LE and UTF-8. Avoid the failure for now.
170 return;
171 }
172
173 if (_XML_Parse_SINGLE_BYTES(g_parser, text, sizeof(text) - 1, XML_TRUE)
174 == XML_STATUS_ERROR)
175 xml_failure(g_parser);
176 }
177 END_TEST
178
START_TEST(test_hash_collision)179 START_TEST(test_hash_collision) {
180 /* For full coverage of the lookup routine, we need to ensure a
181 * hash collision even though we can only tell that we have one
182 * through breakpoint debugging or coverage statistics. The
183 * following will cause a hash collision on machines with a 64-bit
184 * long type; others will have to experiment. The full coverage
185 * tests invoked from qa.sh usually provide a hash collision, but
186 * not always. This is an attempt to provide insurance.
187 */
188 #define COLLIDING_HASH_SALT (unsigned long)SIP_ULL(0xffffffffU, 0xff99fc90U)
189 const char *text
190 = "<doc>\n"
191 "<a1/><a2/><a3/><a4/><a5/><a6/><a7/><a8/>\n"
192 "<b1></b1><b2 attr='foo'>This is a foo</b2><b3></b3><b4></b4>\n"
193 "<b5></b5><b6></b6><b7></b7><b8></b8>\n"
194 "<c1/><c2/><c3/><c4/><c5/><c6/><c7/><c8/>\n"
195 "<d1/><d2/><d3/><d4/><d5/><d6/><d7/>\n"
196 "<d8>This triggers the table growth and collides with b2</d8>\n"
197 "</doc>\n";
198
199 XML_SetHashSalt(g_parser, COLLIDING_HASH_SALT);
200 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
201 == XML_STATUS_ERROR)
202 xml_failure(g_parser);
203 }
204 END_TEST
205 #undef COLLIDING_HASH_SALT
206
207 /* Regression test for SF bug #491986. */
START_TEST(test_danish_latin1)208 START_TEST(test_danish_latin1) {
209 const char *text = "<?xml version='1.0' encoding='iso-8859-1'?>\n"
210 "<e>J\xF8rgen \xE6\xF8\xE5\xC6\xD8\xC5</e>";
211 #ifdef XML_UNICODE
212 const XML_Char *expected
213 = XCS("J\x00f8rgen \x00e6\x00f8\x00e5\x00c6\x00d8\x00c5");
214 #else
215 const XML_Char *expected
216 = XCS("J\xC3\xB8rgen \xC3\xA6\xC3\xB8\xC3\xA5\xC3\x86\xC3\x98\xC3\x85");
217 #endif
218 run_character_check(text, expected);
219 }
220 END_TEST
221
222 /* Regression test for SF bug #514281. */
START_TEST(test_french_charref_hexidecimal)223 START_TEST(test_french_charref_hexidecimal) {
224 const char *text = "<?xml version='1.0' encoding='iso-8859-1'?>\n"
225 "<doc>éèàçêÈ</doc>";
226 #ifdef XML_UNICODE
227 const XML_Char *expected = XCS("\x00e9\x00e8\x00e0\x00e7\x00ea\x00c8");
228 #else
229 const XML_Char *expected
230 = XCS("\xC3\xA9\xC3\xA8\xC3\xA0\xC3\xA7\xC3\xAA\xC3\x88");
231 #endif
232 run_character_check(text, expected);
233 }
234 END_TEST
235
START_TEST(test_french_charref_decimal)236 START_TEST(test_french_charref_decimal) {
237 const char *text = "<?xml version='1.0' encoding='iso-8859-1'?>\n"
238 "<doc>éèàçêÈ</doc>";
239 #ifdef XML_UNICODE
240 const XML_Char *expected = XCS("\x00e9\x00e8\x00e0\x00e7\x00ea\x00c8");
241 #else
242 const XML_Char *expected
243 = XCS("\xC3\xA9\xC3\xA8\xC3\xA0\xC3\xA7\xC3\xAA\xC3\x88");
244 #endif
245 run_character_check(text, expected);
246 }
247 END_TEST
248
START_TEST(test_french_latin1)249 START_TEST(test_french_latin1) {
250 const char *text = "<?xml version='1.0' encoding='iso-8859-1'?>\n"
251 "<doc>\xE9\xE8\xE0\xE7\xEa\xC8</doc>";
252 #ifdef XML_UNICODE
253 const XML_Char *expected = XCS("\x00e9\x00e8\x00e0\x00e7\x00ea\x00c8");
254 #else
255 const XML_Char *expected
256 = XCS("\xC3\xA9\xC3\xA8\xC3\xA0\xC3\xA7\xC3\xAA\xC3\x88");
257 #endif
258 run_character_check(text, expected);
259 }
260 END_TEST
261
START_TEST(test_french_utf8)262 START_TEST(test_french_utf8) {
263 const char *text = "<?xml version='1.0' encoding='utf-8'?>\n"
264 "<doc>\xC3\xA9</doc>";
265 #ifdef XML_UNICODE
266 const XML_Char *expected = XCS("\x00e9");
267 #else
268 const XML_Char *expected = XCS("\xC3\xA9");
269 #endif
270 run_character_check(text, expected);
271 }
272 END_TEST
273
274 /* Regression test for SF bug #600479.
275 XXX There should be a test that exercises all legal XML Unicode
276 characters as PCDATA and attribute value content, and XML Name
277 characters as part of element and attribute names.
278 */
START_TEST(test_utf8_false_rejection)279 START_TEST(test_utf8_false_rejection) {
280 const char *text = "<doc>\xEF\xBA\xBF</doc>";
281 #ifdef XML_UNICODE
282 const XML_Char *expected = XCS("\xfebf");
283 #else
284 const XML_Char *expected = XCS("\xEF\xBA\xBF");
285 #endif
286 run_character_check(text, expected);
287 }
288 END_TEST
289
290 /* Regression test for SF bug #477667.
291 This test assures that any 8-bit character followed by a 7-bit
292 character will not be mistakenly interpreted as a valid UTF-8
293 sequence.
294 */
START_TEST(test_illegal_utf8)295 START_TEST(test_illegal_utf8) {
296 char text[100];
297 int i;
298
299 for (i = 128; i <= 255; ++i) {
300 snprintf(text, sizeof(text), "<e>%ccd</e>", i);
301 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
302 == XML_STATUS_OK) {
303 snprintf(text, sizeof(text),
304 "expected token error for '%c' (ordinal %d) in UTF-8 text", i,
305 i);
306 fail(text);
307 } else if (XML_GetErrorCode(g_parser) != XML_ERROR_INVALID_TOKEN)
308 xml_failure(g_parser);
309 /* Reset the parser since we use the same parser repeatedly. */
310 XML_ParserReset(g_parser, NULL);
311 }
312 }
313 END_TEST
314
315 /* Examples, not masks: */
316 #define UTF8_LEAD_1 "\x7f" /* 0b01111111 */
317 #define UTF8_LEAD_2 "\xdf" /* 0b11011111 */
318 #define UTF8_LEAD_3 "\xef" /* 0b11101111 */
319 #define UTF8_LEAD_4 "\xf7" /* 0b11110111 */
320 #define UTF8_FOLLOW "\xbf" /* 0b10111111 */
321
START_TEST(test_utf8_auto_align)322 START_TEST(test_utf8_auto_align) {
323 struct TestCase {
324 ptrdiff_t expectedMovementInChars;
325 const char *input;
326 };
327
328 struct TestCase cases[] = {
329 {00, ""},
330
331 {00, UTF8_LEAD_1},
332
333 {-1, UTF8_LEAD_2},
334 {00, UTF8_LEAD_2 UTF8_FOLLOW},
335
336 {-1, UTF8_LEAD_3},
337 {-2, UTF8_LEAD_3 UTF8_FOLLOW},
338 {00, UTF8_LEAD_3 UTF8_FOLLOW UTF8_FOLLOW},
339
340 {-1, UTF8_LEAD_4},
341 {-2, UTF8_LEAD_4 UTF8_FOLLOW},
342 {-3, UTF8_LEAD_4 UTF8_FOLLOW UTF8_FOLLOW},
343 {00, UTF8_LEAD_4 UTF8_FOLLOW UTF8_FOLLOW UTF8_FOLLOW},
344 };
345
346 size_t i = 0;
347 bool success = true;
348 for (; i < sizeof(cases) / sizeof(*cases); i++) {
349 const char *fromLim = cases[i].input + strlen(cases[i].input);
350 const char *const fromLimInitially = fromLim;
351 ptrdiff_t actualMovementInChars;
352
353 _INTERNAL_trim_to_complete_utf8_characters(cases[i].input, &fromLim);
354
355 actualMovementInChars = (fromLim - fromLimInitially);
356 if (actualMovementInChars != cases[i].expectedMovementInChars) {
357 size_t j = 0;
358 success = false;
359 printf("[-] UTF-8 case %2u: Expected movement by %2d chars"
360 ", actually moved by %2d chars: \"",
361 (unsigned)(i + 1), (int)cases[i].expectedMovementInChars,
362 (int)actualMovementInChars);
363 for (; j < strlen(cases[i].input); j++) {
364 printf("\\x%02x", (unsigned char)cases[i].input[j]);
365 }
366 printf("\"\n");
367 }
368 }
369
370 if (! success) {
371 fail("UTF-8 auto-alignment is not bullet-proof\n");
372 }
373 }
374 END_TEST
375
START_TEST(test_utf16)376 START_TEST(test_utf16) {
377 /* <?xml version="1.0" encoding="UTF-16"?>
378 * <doc a='123'>some {A} text</doc>
379 *
380 * where {A} is U+FF21, FULLWIDTH LATIN CAPITAL LETTER A
381 */
382 char text[]
383 = "\000<\000?\000x\000m\000\154\000 \000v\000e\000r\000s\000i\000o"
384 "\000n\000=\000'\0001\000.\000\060\000'\000 \000e\000n\000c\000o"
385 "\000d\000i\000n\000g\000=\000'\000U\000T\000F\000-\0001\000\066"
386 "\000'\000?\000>\000\n"
387 "\000<\000d\000o\000c\000 \000a\000=\000'\0001\0002\0003\000'\000>"
388 "\000s\000o\000m\000e\000 \xff\x21\000 \000t\000e\000x\000t\000"
389 "<\000/\000d\000o\000c\000>";
390 #ifdef XML_UNICODE
391 const XML_Char *expected = XCS("some \xff21 text");
392 #else
393 const XML_Char *expected = XCS("some \357\274\241 text");
394 #endif
395 CharData storage;
396
397 CharData_Init(&storage);
398 XML_SetUserData(g_parser, &storage);
399 XML_SetCharacterDataHandler(g_parser, accumulate_characters);
400 if (_XML_Parse_SINGLE_BYTES(g_parser, text, sizeof(text) - 1, XML_TRUE)
401 == XML_STATUS_ERROR)
402 xml_failure(g_parser);
403 CharData_CheckXMLChars(&storage, expected);
404 }
405 END_TEST
406
START_TEST(test_utf16_le_epilog_newline)407 START_TEST(test_utf16_le_epilog_newline) {
408 unsigned int first_chunk_bytes = 17;
409 char text[] = "\xFF\xFE" /* BOM */
410 "<\000e\000/\000>\000" /* document element */
411 "\r\000\n\000\r\000\n\000"; /* epilog */
412
413 if (first_chunk_bytes >= sizeof(text) - 1)
414 fail("bad value of first_chunk_bytes");
415 if (_XML_Parse_SINGLE_BYTES(g_parser, text, first_chunk_bytes, XML_FALSE)
416 == XML_STATUS_ERROR)
417 xml_failure(g_parser);
418 else {
419 enum XML_Status rc;
420 rc = _XML_Parse_SINGLE_BYTES(g_parser, text + first_chunk_bytes,
421 sizeof(text) - first_chunk_bytes - 1,
422 XML_TRUE);
423 if (rc == XML_STATUS_ERROR)
424 xml_failure(g_parser);
425 }
426 }
427 END_TEST
428
429 /* Test that an outright lie in the encoding is faulted */
START_TEST(test_not_utf16)430 START_TEST(test_not_utf16) {
431 const char *text = "<?xml version='1.0' encoding='utf-16'?>"
432 "<doc>Hi</doc>";
433
434 /* Use a handler to provoke the appropriate code paths */
435 XML_SetXmlDeclHandler(g_parser, dummy_xdecl_handler);
436 expect_failure(text, XML_ERROR_INCORRECT_ENCODING,
437 "UTF-16 declared in UTF-8 not faulted");
438 }
439 END_TEST
440
441 /* Test that an unknown encoding is rejected */
START_TEST(test_bad_encoding)442 START_TEST(test_bad_encoding) {
443 const char *text = "<doc>Hi</doc>";
444
445 if (! XML_SetEncoding(g_parser, XCS("unknown-encoding")))
446 fail("XML_SetEncoding failed");
447 expect_failure(text, XML_ERROR_UNKNOWN_ENCODING,
448 "Unknown encoding not faulted");
449 }
450 END_TEST
451
452 /* Regression test for SF bug #481609, #774028. */
START_TEST(test_latin1_umlauts)453 START_TEST(test_latin1_umlauts) {
454 const char *text
455 = "<?xml version='1.0' encoding='iso-8859-1'?>\n"
456 "<e a='\xE4 \xF6 \xFC ä ö ü ä ö ü >'\n"
457 " >\xE4 \xF6 \xFC ä ö ü ä ö ü ></e>";
458 #ifdef XML_UNICODE
459 /* Expected results in UTF-16 */
460 const XML_Char *expected = XCS("\x00e4 \x00f6 \x00fc ")
461 XCS("\x00e4 \x00f6 \x00fc ") XCS("\x00e4 \x00f6 \x00fc >");
462 #else
463 /* Expected results in UTF-8 */
464 const XML_Char *expected = XCS("\xC3\xA4 \xC3\xB6 \xC3\xBC ")
465 XCS("\xC3\xA4 \xC3\xB6 \xC3\xBC ") XCS("\xC3\xA4 \xC3\xB6 \xC3\xBC >");
466 #endif
467
468 run_character_check(text, expected);
469 XML_ParserReset(g_parser, NULL);
470 run_attribute_check(text, expected);
471 /* Repeat with a default handler */
472 XML_ParserReset(g_parser, NULL);
473 XML_SetDefaultHandler(g_parser, dummy_default_handler);
474 run_character_check(text, expected);
475 XML_ParserReset(g_parser, NULL);
476 XML_SetDefaultHandler(g_parser, dummy_default_handler);
477 run_attribute_check(text, expected);
478 }
479 END_TEST
480
481 /* Test that an element name with a 4-byte UTF-8 character is rejected */
START_TEST(test_long_utf8_character)482 START_TEST(test_long_utf8_character) {
483 const char *text
484 = "<?xml version='1.0' encoding='utf-8'?>\n"
485 /* 0xf0 0x90 0x80 0x80 = U+10000, the first Linear B character */
486 "<do\xf0\x90\x80\x80/>";
487 expect_failure(text, XML_ERROR_INVALID_TOKEN,
488 "4-byte UTF-8 character in element name not faulted");
489 }
490 END_TEST
491
492 /* Test that a long latin-1 attribute (too long to convert in one go)
493 * is correctly converted
494 */
START_TEST(test_long_latin1_attribute)495 START_TEST(test_long_latin1_attribute) {
496 const char *text
497 = "<?xml version='1.0' encoding='iso-8859-1'?>\n"
498 "<doc att='"
499 /* 64 characters per line */
500 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
501 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
502 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
503 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
504 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
505 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
506 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
507 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
508 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
509 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
510 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
511 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
512 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
513 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
514 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
515 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
516 /* Last character splits across a buffer boundary */
517 "\xe4'>\n</doc>";
518
519 const XML_Char *expected =
520 /* 64 characters per line */
521 /* clang-format off */
522 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
523 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
524 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
525 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
526 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
527 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
528 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
529 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
530 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
531 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
532 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
533 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
534 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
535 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
536 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
537 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO")
538 /* clang-format on */
539 #ifdef XML_UNICODE
540 XCS("\x00e4");
541 #else
542 XCS("\xc3\xa4");
543 #endif
544
545 run_attribute_check(text, expected);
546 }
547 END_TEST
548
549 /* Test that a long ASCII attribute (too long to convert in one go)
550 * is correctly converted
551 */
START_TEST(test_long_ascii_attribute)552 START_TEST(test_long_ascii_attribute) {
553 const char *text
554 = "<?xml version='1.0' encoding='us-ascii'?>\n"
555 "<doc att='"
556 /* 64 characters per line */
557 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
558 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
559 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
560 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
561 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
562 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
563 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
564 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
565 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
566 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
567 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
568 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
569 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
570 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
571 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
572 "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
573 "01234'>\n</doc>";
574 const XML_Char *expected =
575 /* 64 characters per line */
576 /* clang-format off */
577 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
578 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
579 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
580 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
581 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
582 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
583 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
584 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
585 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
586 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
587 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
588 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
589 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
590 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
591 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
592 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
593 XCS("01234");
594 /* clang-format on */
595
596 run_attribute_check(text, expected);
597 }
598 END_TEST
599
600 /* Regression test #1 for SF bug #653180. */
START_TEST(test_line_number_after_parse)601 START_TEST(test_line_number_after_parse) {
602 const char *text = "<tag>\n"
603 "\n"
604 "\n</tag>";
605 XML_Size lineno;
606
607 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
608 == XML_STATUS_ERROR)
609 xml_failure(g_parser);
610 lineno = XML_GetCurrentLineNumber(g_parser);
611 if (lineno != 4) {
612 char buffer[100];
613 snprintf(buffer, sizeof(buffer),
614 "expected 4 lines, saw %" XML_FMT_INT_MOD "u", lineno);
615 fail(buffer);
616 }
617 }
618 END_TEST
619
620 /* Regression test #2 for SF bug #653180. */
START_TEST(test_column_number_after_parse)621 START_TEST(test_column_number_after_parse) {
622 const char *text = "<tag></tag>";
623 XML_Size colno;
624
625 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
626 == XML_STATUS_ERROR)
627 xml_failure(g_parser);
628 colno = XML_GetCurrentColumnNumber(g_parser);
629 if (colno != 11) {
630 char buffer[100];
631 snprintf(buffer, sizeof(buffer),
632 "expected 11 columns, saw %" XML_FMT_INT_MOD "u", colno);
633 fail(buffer);
634 }
635 }
636 END_TEST
637
638 /* Regression test #3 for SF bug #653180. */
START_TEST(test_line_and_column_numbers_inside_handlers)639 START_TEST(test_line_and_column_numbers_inside_handlers) {
640 const char *text = "<a>\n" /* Unix end-of-line */
641 " <b>\r\n" /* Windows end-of-line */
642 " <c/>\r" /* Mac OS end-of-line */
643 " </b>\n"
644 " <d>\n"
645 " <f/>\n"
646 " </d>\n"
647 "</a>";
648 const StructDataEntry expected[]
649 = {{XCS("a"), 0, 1, STRUCT_START_TAG}, {XCS("b"), 2, 2, STRUCT_START_TAG},
650 {XCS("c"), 4, 3, STRUCT_START_TAG}, {XCS("c"), 8, 3, STRUCT_END_TAG},
651 {XCS("b"), 2, 4, STRUCT_END_TAG}, {XCS("d"), 2, 5, STRUCT_START_TAG},
652 {XCS("f"), 4, 6, STRUCT_START_TAG}, {XCS("f"), 8, 6, STRUCT_END_TAG},
653 {XCS("d"), 2, 7, STRUCT_END_TAG}, {XCS("a"), 0, 8, STRUCT_END_TAG}};
654 const int expected_count = sizeof(expected) / sizeof(StructDataEntry);
655 StructData storage;
656
657 StructData_Init(&storage);
658 XML_SetUserData(g_parser, &storage);
659 XML_SetStartElementHandler(g_parser, start_element_event_handler2);
660 XML_SetEndElementHandler(g_parser, end_element_event_handler2);
661 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
662 == XML_STATUS_ERROR)
663 xml_failure(g_parser);
664
665 StructData_CheckItems(&storage, expected, expected_count);
666 StructData_Dispose(&storage);
667 }
668 END_TEST
669
670 /* Regression test #4 for SF bug #653180. */
START_TEST(test_line_number_after_error)671 START_TEST(test_line_number_after_error) {
672 const char *text = "<a>\n"
673 " <b>\n"
674 " </a>"; /* missing </b> */
675 XML_Size lineno;
676 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
677 != XML_STATUS_ERROR)
678 fail("Expected a parse error");
679
680 lineno = XML_GetCurrentLineNumber(g_parser);
681 if (lineno != 3) {
682 char buffer[100];
683 snprintf(buffer, sizeof(buffer),
684 "expected 3 lines, saw %" XML_FMT_INT_MOD "u", lineno);
685 fail(buffer);
686 }
687 }
688 END_TEST
689
690 /* Regression test #5 for SF bug #653180. */
START_TEST(test_column_number_after_error)691 START_TEST(test_column_number_after_error) {
692 const char *text = "<a>\n"
693 " <b>\n"
694 " </a>"; /* missing </b> */
695 XML_Size colno;
696 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
697 != XML_STATUS_ERROR)
698 fail("Expected a parse error");
699
700 colno = XML_GetCurrentColumnNumber(g_parser);
701 if (colno != 4) {
702 char buffer[100];
703 snprintf(buffer, sizeof(buffer),
704 "expected 4 columns, saw %" XML_FMT_INT_MOD "u", colno);
705 fail(buffer);
706 }
707 }
708 END_TEST
709
710 /* Regression test for SF bug #478332. */
START_TEST(test_really_long_lines)711 START_TEST(test_really_long_lines) {
712 /* This parses an input line longer than INIT_DATA_BUF_SIZE
713 characters long (defined to be 1024 in xmlparse.c). We take a
714 really cheesy approach to building the input buffer, because
715 this avoids writing bugs in buffer-filling code.
716 */
717 const char *text
718 = "<e>"
719 /* 64 chars */
720 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
721 /* until we have at least 1024 characters on the line: */
722 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
723 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
724 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
725 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
726 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
727 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
728 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
729 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
730 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
731 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
732 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
733 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
734 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
735 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
736 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
737 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
738 "</e>";
739 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
740 == XML_STATUS_ERROR)
741 xml_failure(g_parser);
742 }
743 END_TEST
744
745 /* Test cdata processing across a buffer boundary */
START_TEST(test_really_long_encoded_lines)746 START_TEST(test_really_long_encoded_lines) {
747 /* As above, except that we want to provoke an output buffer
748 * overflow with a non-trivial encoding. For this we need to pass
749 * the whole cdata in one go, not byte-by-byte.
750 */
751 void *buffer;
752 const char *text
753 = "<?xml version='1.0' encoding='iso-8859-1'?>"
754 "<e>"
755 /* 64 chars */
756 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
757 /* until we have at least 1024 characters on the line: */
758 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
759 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
760 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
761 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
762 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
763 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
764 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
765 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
766 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
767 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
768 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
769 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
770 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
771 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
772 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
773 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"
774 "</e>";
775 int parse_len = (int)strlen(text);
776
777 /* Need a cdata handler to provoke the code path we want to test */
778 XML_SetCharacterDataHandler(g_parser, dummy_cdata_handler);
779 buffer = XML_GetBuffer(g_parser, parse_len);
780 if (buffer == NULL)
781 fail("Could not allocate parse buffer");
782 assert(buffer != NULL);
783 memcpy(buffer, text, parse_len);
784 if (XML_ParseBuffer(g_parser, parse_len, XML_TRUE) == XML_STATUS_ERROR)
785 xml_failure(g_parser);
786 }
787 END_TEST
788
789 /*
790 * Element event tests.
791 */
792
START_TEST(test_end_element_events)793 START_TEST(test_end_element_events) {
794 const char *text = "<a><b><c/></b><d><f/></d></a>";
795 const XML_Char *expected = XCS("/c/b/f/d/a");
796 CharData storage;
797
798 CharData_Init(&storage);
799 XML_SetUserData(g_parser, &storage);
800 XML_SetEndElementHandler(g_parser, end_element_event_handler);
801 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
802 == XML_STATUS_ERROR)
803 xml_failure(g_parser);
804 CharData_CheckXMLChars(&storage, expected);
805 }
806 END_TEST
807
808 /*
809 * Attribute tests.
810 */
811
812 /* Helper used by the following tests; this checks any "attr" and "refs"
813 attributes to make sure whitespace has been normalized.
814
815 Return true if whitespace has been normalized in a string, using
816 the rules for attribute value normalization. The 'is_cdata' flag
817 is needed since CDATA attributes don't need to have multiple
818 whitespace characters collapsed to a single space, while other
819 attribute data types do. (Section 3.3.3 of the recommendation.)
820 */
821 static int
is_whitespace_normalized(const XML_Char * s,int is_cdata)822 is_whitespace_normalized(const XML_Char *s, int is_cdata) {
823 int blanks = 0;
824 int at_start = 1;
825 while (*s) {
826 if (*s == XCS(' '))
827 ++blanks;
828 else if (*s == XCS('\t') || *s == XCS('\n') || *s == XCS('\r'))
829 return 0;
830 else {
831 if (at_start) {
832 at_start = 0;
833 if (blanks && ! is_cdata)
834 /* illegal leading blanks */
835 return 0;
836 } else if (blanks > 1 && ! is_cdata)
837 return 0;
838 blanks = 0;
839 }
840 ++s;
841 }
842 if (blanks && ! is_cdata)
843 return 0;
844 return 1;
845 }
846
847 /* Check the attribute whitespace checker: */
START_TEST(test_helper_is_whitespace_normalized)848 START_TEST(test_helper_is_whitespace_normalized) {
849 assert(is_whitespace_normalized(XCS("abc"), 0));
850 assert(is_whitespace_normalized(XCS("abc"), 1));
851 assert(is_whitespace_normalized(XCS("abc def ghi"), 0));
852 assert(is_whitespace_normalized(XCS("abc def ghi"), 1));
853 assert(! is_whitespace_normalized(XCS(" abc def ghi"), 0));
854 assert(is_whitespace_normalized(XCS(" abc def ghi"), 1));
855 assert(! is_whitespace_normalized(XCS("abc def ghi"), 0));
856 assert(is_whitespace_normalized(XCS("abc def ghi"), 1));
857 assert(! is_whitespace_normalized(XCS("abc def ghi "), 0));
858 assert(is_whitespace_normalized(XCS("abc def ghi "), 1));
859 assert(! is_whitespace_normalized(XCS(" "), 0));
860 assert(is_whitespace_normalized(XCS(" "), 1));
861 assert(! is_whitespace_normalized(XCS("\t"), 0));
862 assert(! is_whitespace_normalized(XCS("\t"), 1));
863 assert(! is_whitespace_normalized(XCS("\n"), 0));
864 assert(! is_whitespace_normalized(XCS("\n"), 1));
865 assert(! is_whitespace_normalized(XCS("\r"), 0));
866 assert(! is_whitespace_normalized(XCS("\r"), 1));
867 assert(! is_whitespace_normalized(XCS("abc\t def"), 1));
868 }
869 END_TEST
870
871 static void XMLCALL
check_attr_contains_normalized_whitespace(void * userData,const XML_Char * name,const XML_Char ** atts)872 check_attr_contains_normalized_whitespace(void *userData, const XML_Char *name,
873 const XML_Char **atts) {
874 int i;
875 UNUSED_P(userData);
876 UNUSED_P(name);
877 for (i = 0; atts[i] != NULL; i += 2) {
878 const XML_Char *attrname = atts[i];
879 const XML_Char *value = atts[i + 1];
880 if (xcstrcmp(XCS("attr"), attrname) == 0
881 || xcstrcmp(XCS("ents"), attrname) == 0
882 || xcstrcmp(XCS("refs"), attrname) == 0) {
883 if (! is_whitespace_normalized(value, 0)) {
884 char buffer[256];
885 snprintf(buffer, sizeof(buffer),
886 "attribute value not normalized: %" XML_FMT_STR
887 "='%" XML_FMT_STR "'",
888 attrname, value);
889 fail(buffer);
890 }
891 }
892 }
893 }
894
START_TEST(test_attr_whitespace_normalization)895 START_TEST(test_attr_whitespace_normalization) {
896 const char *text
897 = "<!DOCTYPE doc [\n"
898 " <!ATTLIST doc\n"
899 " attr NMTOKENS #REQUIRED\n"
900 " ents ENTITIES #REQUIRED\n"
901 " refs IDREFS #REQUIRED>\n"
902 "]>\n"
903 "<doc attr=' a b c\t\td\te\t' refs=' id-1 \t id-2\t\t' \n"
904 " ents=' ent-1 \t\r\n"
905 " ent-2 ' >\n"
906 " <e id='id-1'/>\n"
907 " <e id='id-2'/>\n"
908 "</doc>";
909
910 XML_SetStartElementHandler(g_parser,
911 check_attr_contains_normalized_whitespace);
912 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
913 == XML_STATUS_ERROR)
914 xml_failure(g_parser);
915 }
916 END_TEST
917
918 /*
919 * XML declaration tests.
920 */
921
START_TEST(test_xmldecl_misplaced)922 START_TEST(test_xmldecl_misplaced) {
923 expect_failure("\n"
924 "<?xml version='1.0'?>\n"
925 "<a/>",
926 XML_ERROR_MISPLACED_XML_PI,
927 "failed to report misplaced XML declaration");
928 }
929 END_TEST
930
START_TEST(test_xmldecl_invalid)931 START_TEST(test_xmldecl_invalid) {
932 expect_failure("<?xml version='1.0' \xc3\xa7?>\n<doc/>", XML_ERROR_XML_DECL,
933 "Failed to report invalid XML declaration");
934 }
935 END_TEST
936
START_TEST(test_xmldecl_missing_attr)937 START_TEST(test_xmldecl_missing_attr) {
938 expect_failure("<?xml ='1.0'?>\n<doc/>\n", XML_ERROR_XML_DECL,
939 "Failed to report missing XML declaration attribute");
940 }
941 END_TEST
942
START_TEST(test_xmldecl_missing_value)943 START_TEST(test_xmldecl_missing_value) {
944 expect_failure("<?xml version='1.0' encoding='us-ascii' standalone?>\n"
945 "<doc/>",
946 XML_ERROR_XML_DECL,
947 "Failed to report missing attribute value");
948 }
949 END_TEST
950
951 /* Regression test for SF bug #584832. */
START_TEST(test_unknown_encoding_internal_entity)952 START_TEST(test_unknown_encoding_internal_entity) {
953 const char *text = "<?xml version='1.0' encoding='unsupported-encoding'?>\n"
954 "<!DOCTYPE test [<!ENTITY foo 'bar'>]>\n"
955 "<test a='&foo;'/>";
956
957 XML_SetUnknownEncodingHandler(g_parser, UnknownEncodingHandler, NULL);
958 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
959 == XML_STATUS_ERROR)
960 xml_failure(g_parser);
961 }
962 END_TEST
963
964 /* Test unrecognised encoding handler */
START_TEST(test_unrecognised_encoding_internal_entity)965 START_TEST(test_unrecognised_encoding_internal_entity) {
966 const char *text = "<?xml version='1.0' encoding='unsupported-encoding'?>\n"
967 "<!DOCTYPE test [<!ENTITY foo 'bar'>]>\n"
968 "<test a='&foo;'/>";
969
970 XML_SetUnknownEncodingHandler(g_parser, UnrecognisedEncodingHandler, NULL);
971 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
972 != XML_STATUS_ERROR)
973 fail("Unrecognised encoding not rejected");
974 }
975 END_TEST
976
977 /* Regression test for SF bug #620106. */
START_TEST(test_ext_entity_set_encoding)978 START_TEST(test_ext_entity_set_encoding) {
979 const char *text = "<!DOCTYPE doc [\n"
980 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
981 "]>\n"
982 "<doc>&en;</doc>";
983 ExtTest test_data
984 = {/* This text says it's an unsupported encoding, but it's really
985 UTF-8, which we tell Expat using XML_SetEncoding().
986 */
987 "<?xml encoding='iso-8859-3'?>\xC3\xA9", XCS("utf-8"), NULL};
988 #ifdef XML_UNICODE
989 const XML_Char *expected = XCS("\x00e9");
990 #else
991 const XML_Char *expected = XCS("\xc3\xa9");
992 #endif
993
994 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
995 run_ext_character_check(text, &test_data, expected);
996 }
997 END_TEST
998
999 /* Test external entities with no handler */
START_TEST(test_ext_entity_no_handler)1000 START_TEST(test_ext_entity_no_handler) {
1001 const char *text = "<!DOCTYPE doc [\n"
1002 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
1003 "]>\n"
1004 "<doc>&en;</doc>";
1005
1006 XML_SetDefaultHandler(g_parser, dummy_default_handler);
1007 run_character_check(text, XCS(""));
1008 }
1009 END_TEST
1010
1011 /* Test UTF-8 BOM is accepted */
START_TEST(test_ext_entity_set_bom)1012 START_TEST(test_ext_entity_set_bom) {
1013 const char *text = "<!DOCTYPE doc [\n"
1014 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
1015 "]>\n"
1016 "<doc>&en;</doc>";
1017 ExtTest test_data = {"\xEF\xBB\xBF" /* BOM */
1018 "<?xml encoding='iso-8859-3'?>"
1019 "\xC3\xA9",
1020 XCS("utf-8"), NULL};
1021 #ifdef XML_UNICODE
1022 const XML_Char *expected = XCS("\x00e9");
1023 #else
1024 const XML_Char *expected = XCS("\xc3\xa9");
1025 #endif
1026
1027 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
1028 run_ext_character_check(text, &test_data, expected);
1029 }
1030 END_TEST
1031
1032 /* Test that bad encodings are faulted */
START_TEST(test_ext_entity_bad_encoding)1033 START_TEST(test_ext_entity_bad_encoding) {
1034 const char *text = "<!DOCTYPE doc [\n"
1035 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
1036 "]>\n"
1037 "<doc>&en;</doc>";
1038 ExtFaults fault
1039 = {"<?xml encoding='iso-8859-3'?>u", "Unsupported encoding not faulted",
1040 XCS("unknown"), XML_ERROR_UNKNOWN_ENCODING};
1041
1042 XML_SetExternalEntityRefHandler(g_parser, external_entity_faulter);
1043 XML_SetUserData(g_parser, &fault);
1044 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
1045 "Bad encoding should not have been accepted");
1046 }
1047 END_TEST
1048
1049 /* Try handing an invalid encoding to an external entity parser */
START_TEST(test_ext_entity_bad_encoding_2)1050 START_TEST(test_ext_entity_bad_encoding_2) {
1051 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
1052 "<!DOCTYPE doc SYSTEM 'foo'>\n"
1053 "<doc>&entity;</doc>";
1054 ExtFaults fault
1055 = {"<!ELEMENT doc (#PCDATA)*>", "Unknown encoding not faulted",
1056 XCS("unknown-encoding"), XML_ERROR_UNKNOWN_ENCODING};
1057
1058 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1059 XML_SetExternalEntityRefHandler(g_parser, external_entity_faulter);
1060 XML_SetUserData(g_parser, &fault);
1061 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
1062 "Bad encoding not faulted in external entity handler");
1063 }
1064 END_TEST
1065
1066 /* Test that no error is reported for unknown entities if we don't
1067 read an external subset. This was fixed in Expat 1.95.5.
1068 */
START_TEST(test_wfc_undeclared_entity_unread_external_subset)1069 START_TEST(test_wfc_undeclared_entity_unread_external_subset) {
1070 const char *text = "<!DOCTYPE doc SYSTEM 'foo'>\n"
1071 "<doc>&entity;</doc>";
1072
1073 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1074 == XML_STATUS_ERROR)
1075 xml_failure(g_parser);
1076 }
1077 END_TEST
1078
1079 /* Test that an error is reported for unknown entities if we don't
1080 have an external subset.
1081 */
START_TEST(test_wfc_undeclared_entity_no_external_subset)1082 START_TEST(test_wfc_undeclared_entity_no_external_subset) {
1083 expect_failure("<doc>&entity;</doc>", XML_ERROR_UNDEFINED_ENTITY,
1084 "Parser did not report undefined entity w/out a DTD.");
1085 }
1086 END_TEST
1087
1088 /* Test that an error is reported for unknown entities if we don't
1089 read an external subset, but have been declared standalone.
1090 */
START_TEST(test_wfc_undeclared_entity_standalone)1091 START_TEST(test_wfc_undeclared_entity_standalone) {
1092 const char *text
1093 = "<?xml version='1.0' encoding='us-ascii' standalone='yes'?>\n"
1094 "<!DOCTYPE doc SYSTEM 'foo'>\n"
1095 "<doc>&entity;</doc>";
1096
1097 expect_failure(text, XML_ERROR_UNDEFINED_ENTITY,
1098 "Parser did not report undefined entity (standalone).");
1099 }
1100 END_TEST
1101
1102 /* Test that an error is reported for unknown entities if we have read
1103 an external subset, and standalone is true.
1104 */
START_TEST(test_wfc_undeclared_entity_with_external_subset_standalone)1105 START_TEST(test_wfc_undeclared_entity_with_external_subset_standalone) {
1106 const char *text
1107 = "<?xml version='1.0' encoding='us-ascii' standalone='yes'?>\n"
1108 "<!DOCTYPE doc SYSTEM 'foo'>\n"
1109 "<doc>&entity;</doc>";
1110 ExtTest test_data = {"<!ELEMENT doc (#PCDATA)*>", NULL, NULL};
1111
1112 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1113 XML_SetUserData(g_parser, &test_data);
1114 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
1115 expect_failure(text, XML_ERROR_UNDEFINED_ENTITY,
1116 "Parser did not report undefined entity (external DTD).");
1117 }
1118 END_TEST
1119
1120 /* Test that external entity handling is not done if the parsing flag
1121 * is set to UNLESS_STANDALONE
1122 */
START_TEST(test_entity_with_external_subset_unless_standalone)1123 START_TEST(test_entity_with_external_subset_unless_standalone) {
1124 const char *text
1125 = "<?xml version='1.0' encoding='us-ascii' standalone='yes'?>\n"
1126 "<!DOCTYPE doc SYSTEM 'foo'>\n"
1127 "<doc>&entity;</doc>";
1128 ExtTest test_data = {"<!ENTITY entity 'bar'>", NULL, NULL};
1129
1130 XML_SetParamEntityParsing(g_parser,
1131 XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1132 XML_SetUserData(g_parser, &test_data);
1133 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
1134 expect_failure(text, XML_ERROR_UNDEFINED_ENTITY,
1135 "Parser did not report undefined entity");
1136 }
1137 END_TEST
1138
1139 /* Test that no error is reported for unknown entities if we have read
1140 an external subset, and standalone is false.
1141 */
START_TEST(test_wfc_undeclared_entity_with_external_subset)1142 START_TEST(test_wfc_undeclared_entity_with_external_subset) {
1143 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
1144 "<!DOCTYPE doc SYSTEM 'foo'>\n"
1145 "<doc>&entity;</doc>";
1146 ExtTest test_data = {"<!ELEMENT doc (#PCDATA)*>", NULL, NULL};
1147
1148 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1149 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
1150 run_ext_character_check(text, &test_data, XCS(""));
1151 }
1152 END_TEST
1153
1154 /* Test that an error is reported if our NotStandalone handler fails */
START_TEST(test_not_standalone_handler_reject)1155 START_TEST(test_not_standalone_handler_reject) {
1156 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
1157 "<!DOCTYPE doc SYSTEM 'foo'>\n"
1158 "<doc>&entity;</doc>";
1159 ExtTest test_data = {"<!ELEMENT doc (#PCDATA)*>", NULL, NULL};
1160
1161 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1162 XML_SetUserData(g_parser, &test_data);
1163 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
1164 XML_SetNotStandaloneHandler(g_parser, reject_not_standalone_handler);
1165 expect_failure(text, XML_ERROR_NOT_STANDALONE,
1166 "NotStandalone handler failed to reject");
1167
1168 /* Try again but without external entity handling */
1169 XML_ParserReset(g_parser, NULL);
1170 XML_SetNotStandaloneHandler(g_parser, reject_not_standalone_handler);
1171 expect_failure(text, XML_ERROR_NOT_STANDALONE,
1172 "NotStandalone handler failed to reject");
1173 }
1174 END_TEST
1175
1176 /* Test that no error is reported if our NotStandalone handler succeeds */
START_TEST(test_not_standalone_handler_accept)1177 START_TEST(test_not_standalone_handler_accept) {
1178 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
1179 "<!DOCTYPE doc SYSTEM 'foo'>\n"
1180 "<doc>&entity;</doc>";
1181 ExtTest test_data = {"<!ELEMENT doc (#PCDATA)*>", NULL, NULL};
1182
1183 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1184 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
1185 XML_SetNotStandaloneHandler(g_parser, accept_not_standalone_handler);
1186 run_ext_character_check(text, &test_data, XCS(""));
1187
1188 /* Repeat without the external entity handler */
1189 XML_ParserReset(g_parser, NULL);
1190 XML_SetNotStandaloneHandler(g_parser, accept_not_standalone_handler);
1191 run_character_check(text, XCS(""));
1192 }
1193 END_TEST
1194
START_TEST(test_entity_start_tag_level_greater_than_one)1195 START_TEST(test_entity_start_tag_level_greater_than_one) {
1196 const char *const text = "<!DOCTYPE t1 [\n"
1197 " <!ENTITY e1 'hello'>\n"
1198 "]>\n"
1199 "<t1>\n"
1200 " <t2>&e1;</t2>\n"
1201 "</t1>\n";
1202
1203 XML_Parser parser = XML_ParserCreate(NULL);
1204 assert_true(_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text),
1205 /*isFinal*/ XML_TRUE)
1206 == XML_STATUS_OK);
1207 XML_ParserFree(parser);
1208 }
1209 END_TEST
1210
START_TEST(test_wfc_no_recursive_entity_refs)1211 START_TEST(test_wfc_no_recursive_entity_refs) {
1212 const char *text = "<!DOCTYPE doc [\n"
1213 " <!ENTITY entity '&entity;'>\n"
1214 "]>\n"
1215 "<doc>&entity;</doc>";
1216
1217 expect_failure(text, XML_ERROR_RECURSIVE_ENTITY_REF,
1218 "Parser did not report recursive entity reference.");
1219 }
1220 END_TEST
1221
START_TEST(test_no_indirectly_recursive_entity_refs)1222 START_TEST(test_no_indirectly_recursive_entity_refs) {
1223 struct TestCase {
1224 const char *doc;
1225 bool usesParameterEntities;
1226 };
1227
1228 const struct TestCase cases[] = {
1229 // general entity + character data
1230 {"<!DOCTYPE a [\n"
1231 " <!ENTITY e1 '&e2;'>\n"
1232 " <!ENTITY e2 '&e1;'>\n"
1233 "]><a>&e2;</a>\n",
1234 false},
1235
1236 // general entity + attribute value
1237 {"<!DOCTYPE a [\n"
1238 " <!ENTITY e1 '&e2;'>\n"
1239 " <!ENTITY e2 '&e1;'>\n"
1240 "]><a k1='&e2;' />\n",
1241 false},
1242
1243 // parameter entity
1244 {"<!DOCTYPE doc [\n"
1245 " <!ENTITY % p1 '%p2;'>\n"
1246 " <!ENTITY % p2 '%p1;'>\n"
1247 " <!ENTITY % define_g \"<!ENTITY g '%p2;'>\">\n"
1248 " %define_g;\n"
1249 "]>\n"
1250 "<doc/>\n",
1251 true},
1252 };
1253 const XML_Bool reset_or_not[] = {XML_TRUE, XML_FALSE};
1254
1255 for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
1256 for (size_t j = 0; j < sizeof(reset_or_not) / sizeof(reset_or_not[0]);
1257 j++) {
1258 const XML_Bool reset_wanted = reset_or_not[j];
1259 const char *const doc = cases[i].doc;
1260 const bool usesParameterEntities = cases[i].usesParameterEntities;
1261
1262 set_subtest("[%i,reset=%i] %s", (int)i, (int)j, doc);
1263
1264 #ifdef XML_DTD // both GE and DTD
1265 const bool rejection_expected = true;
1266 #elif XML_GE == 1 // GE but not DTD
1267 const bool rejection_expected = ! usesParameterEntities;
1268 #else // neither DTD nor GE
1269 const bool rejection_expected = false;
1270 #endif
1271
1272 XML_Parser parser = XML_ParserCreate(NULL);
1273
1274 #ifdef XML_DTD
1275 if (usesParameterEntities) {
1276 assert_true(
1277 XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)
1278 == 1);
1279 }
1280 #else
1281 UNUSED_P(usesParameterEntities);
1282 #endif // XML_DTD
1283
1284 const enum XML_Status status
1285 = _XML_Parse_SINGLE_BYTES(parser, doc, (int)strlen(doc),
1286 /*isFinal*/ XML_TRUE);
1287
1288 if (rejection_expected) {
1289 assert_true(status == XML_STATUS_ERROR);
1290 assert_true(XML_GetErrorCode(parser) == XML_ERROR_RECURSIVE_ENTITY_REF);
1291 } else {
1292 assert_true(status == XML_STATUS_OK);
1293 }
1294
1295 if (reset_wanted) {
1296 // This covers free'ing of (eventually) all three open entity lists by
1297 // XML_ParserReset.
1298 XML_ParserReset(parser, NULL);
1299 }
1300
1301 // This covers free'ing of (eventually) all three open entity lists by
1302 // XML_ParserFree (unless XML_ParserReset has already done that above).
1303 XML_ParserFree(parser);
1304 }
1305 }
1306 }
1307 END_TEST
1308
START_TEST(test_recursive_external_parameter_entity_2)1309 START_TEST(test_recursive_external_parameter_entity_2) {
1310 struct TestCase {
1311 const char *doc;
1312 enum XML_Status expectedStatus;
1313 };
1314
1315 struct TestCase cases[] = {
1316 {"<!ENTITY % p1 '%p1;'>", XML_STATUS_ERROR},
1317 {"<!ENTITY % p1 '%p1;'>"
1318 "<!ENTITY % p1 'first declaration wins'>",
1319 XML_STATUS_ERROR},
1320 {"<!ENTITY % p1 'first declaration wins'>"
1321 "<!ENTITY % p1 '%p1;'>",
1322 XML_STATUS_OK},
1323 {"<!ENTITY % p1 '%p1;'>", XML_STATUS_OK},
1324 };
1325
1326 for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
1327 const char *const doc = cases[i].doc;
1328 const enum XML_Status expectedStatus = cases[i].expectedStatus;
1329 set_subtest("%s", doc);
1330
1331 XML_Parser parser = XML_ParserCreate(NULL);
1332 assert_true(parser != NULL);
1333
1334 XML_Parser ext_parser = XML_ExternalEntityParserCreate(parser, NULL, NULL);
1335 assert_true(ext_parser != NULL);
1336
1337 const enum XML_Status actualStatus
1338 = _XML_Parse_SINGLE_BYTES(ext_parser, doc, (int)strlen(doc), XML_TRUE);
1339
1340 assert_true(actualStatus == expectedStatus);
1341 if (actualStatus != XML_STATUS_OK) {
1342 assert_true(XML_GetErrorCode(ext_parser)
1343 == XML_ERROR_RECURSIVE_ENTITY_REF);
1344 }
1345
1346 XML_ParserFree(ext_parser);
1347 XML_ParserFree(parser);
1348 }
1349 }
1350 END_TEST
1351
1352 /* Test incomplete external entities are faulted */
START_TEST(test_ext_entity_invalid_parse)1353 START_TEST(test_ext_entity_invalid_parse) {
1354 const char *text = "<!DOCTYPE doc [\n"
1355 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
1356 "]>\n"
1357 "<doc>&en;</doc>";
1358 const ExtFaults faults[]
1359 = {{"<", "Incomplete element declaration not faulted", NULL,
1360 XML_ERROR_UNCLOSED_TOKEN},
1361 {"<\xe2\x82", /* First two bytes of a three-byte char */
1362 "Incomplete character not faulted", NULL, XML_ERROR_PARTIAL_CHAR},
1363 {"<tag>\xe2\x82", "Incomplete character in CDATA not faulted", NULL,
1364 XML_ERROR_PARTIAL_CHAR},
1365 {NULL, NULL, NULL, XML_ERROR_NONE}};
1366 const ExtFaults *fault = faults;
1367
1368 for (; fault->parse_text != NULL; fault++) {
1369 set_subtest("\"%s\"", fault->parse_text);
1370 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1371 XML_SetExternalEntityRefHandler(g_parser, external_entity_faulter);
1372 XML_SetUserData(g_parser, (void *)fault);
1373 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
1374 "Parser did not report external entity error");
1375 XML_ParserReset(g_parser, NULL);
1376 }
1377 }
1378 END_TEST
1379
1380 /* Regression test for SF bug #483514. */
START_TEST(test_dtd_default_handling)1381 START_TEST(test_dtd_default_handling) {
1382 const char *text = "<!DOCTYPE doc [\n"
1383 "<!ENTITY e SYSTEM 'http://example.org/e'>\n"
1384 "<!NOTATION n SYSTEM 'http://example.org/n'>\n"
1385 "<!ELEMENT doc EMPTY>\n"
1386 "<!ATTLIST doc a CDATA #IMPLIED>\n"
1387 "<?pi in dtd?>\n"
1388 "<!--comment in dtd-->\n"
1389 "]><doc/>";
1390
1391 XML_SetDefaultHandler(g_parser, accumulate_characters);
1392 XML_SetStartDoctypeDeclHandler(g_parser, dummy_start_doctype_handler);
1393 XML_SetEndDoctypeDeclHandler(g_parser, dummy_end_doctype_handler);
1394 XML_SetEntityDeclHandler(g_parser, dummy_entity_decl_handler);
1395 XML_SetNotationDeclHandler(g_parser, dummy_notation_decl_handler);
1396 XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
1397 XML_SetAttlistDeclHandler(g_parser, dummy_attlist_decl_handler);
1398 XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler);
1399 XML_SetCommentHandler(g_parser, dummy_comment_handler);
1400 XML_SetStartCdataSectionHandler(g_parser, dummy_start_cdata_handler);
1401 XML_SetEndCdataSectionHandler(g_parser, dummy_end_cdata_handler);
1402 run_character_check(text, XCS("\n\n\n\n\n\n\n<doc/>"));
1403 }
1404 END_TEST
1405
1406 /* Test handling of attribute declarations */
START_TEST(test_dtd_attr_handling)1407 START_TEST(test_dtd_attr_handling) {
1408 const char *prolog = "<!DOCTYPE doc [\n"
1409 "<!ELEMENT doc EMPTY>\n";
1410 AttTest attr_data[]
1411 = {{"<!ATTLIST doc a ( one | two | three ) #REQUIRED>\n"
1412 "]>"
1413 "<doc a='two'/>",
1414 XCS("doc"), XCS("a"),
1415 XCS("(one|two|three)"), /* Extraneous spaces will be removed */
1416 NULL, XML_TRUE},
1417 {"<!NOTATION foo SYSTEM 'http://example.org/foo'>\n"
1418 "<!ATTLIST doc a NOTATION (foo) #IMPLIED>\n"
1419 "]>"
1420 "<doc/>",
1421 XCS("doc"), XCS("a"), XCS("NOTATION(foo)"), NULL, XML_FALSE},
1422 {"<!ATTLIST doc a NOTATION (foo) 'bar'>\n"
1423 "]>"
1424 "<doc/>",
1425 XCS("doc"), XCS("a"), XCS("NOTATION(foo)"), XCS("bar"), XML_FALSE},
1426 {"<!ATTLIST doc a CDATA '\xdb\xb2'>\n"
1427 "]>"
1428 "<doc/>",
1429 XCS("doc"), XCS("a"), XCS("CDATA"),
1430 #ifdef XML_UNICODE
1431 XCS("\x06f2"),
1432 #else
1433 XCS("\xdb\xb2"),
1434 #endif
1435 XML_FALSE},
1436 {NULL, NULL, NULL, NULL, NULL, XML_FALSE}};
1437 AttTest *test;
1438
1439 for (test = attr_data; test->definition != NULL; test++) {
1440 set_subtest("%s", test->definition);
1441 XML_SetAttlistDeclHandler(g_parser, verify_attlist_decl_handler);
1442 XML_SetUserData(g_parser, test);
1443 if (_XML_Parse_SINGLE_BYTES(g_parser, prolog, (int)strlen(prolog),
1444 XML_FALSE)
1445 == XML_STATUS_ERROR)
1446 xml_failure(g_parser);
1447 if (_XML_Parse_SINGLE_BYTES(g_parser, test->definition,
1448 (int)strlen(test->definition), XML_TRUE)
1449 == XML_STATUS_ERROR)
1450 xml_failure(g_parser);
1451 XML_ParserReset(g_parser, NULL);
1452 }
1453 }
1454 END_TEST
1455
1456 /* See related SF bug #673791.
1457 When namespace processing is enabled, setting the namespace URI for
1458 a prefix is not allowed; this test ensures that it *is* allowed
1459 when namespace processing is not enabled.
1460 (See Namespaces in XML, section 2.)
1461 */
START_TEST(test_empty_ns_without_namespaces)1462 START_TEST(test_empty_ns_without_namespaces) {
1463 const char *text = "<doc xmlns:prefix='http://example.org/'>\n"
1464 " <e xmlns:prefix=''/>\n"
1465 "</doc>";
1466
1467 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1468 == XML_STATUS_ERROR)
1469 xml_failure(g_parser);
1470 }
1471 END_TEST
1472
1473 /* Regression test for SF bug #824420.
1474 Checks that an xmlns:prefix attribute set in an attribute's default
1475 value isn't misinterpreted.
1476 */
START_TEST(test_ns_in_attribute_default_without_namespaces)1477 START_TEST(test_ns_in_attribute_default_without_namespaces) {
1478 const char *text = "<!DOCTYPE e:element [\n"
1479 " <!ATTLIST e:element\n"
1480 " xmlns:e CDATA 'http://example.org/'>\n"
1481 " ]>\n"
1482 "<e:element/>";
1483
1484 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1485 == XML_STATUS_ERROR)
1486 xml_failure(g_parser);
1487 }
1488 END_TEST
1489
1490 /* Regression test for SF bug #1515266: missing check of stopped
1491 parser in doContext() 'for' loop. */
START_TEST(test_stop_parser_between_char_data_calls)1492 START_TEST(test_stop_parser_between_char_data_calls) {
1493 /* The sample data must be big enough that there are two calls to
1494 the character data handler from within the inner "for" loop of
1495 the XML_TOK_DATA_CHARS case in doContent(), and the character
1496 handler must stop the parser and clear the character data
1497 handler.
1498 */
1499 const char *text = long_character_data_text;
1500
1501 XML_SetCharacterDataHandler(g_parser, clearing_aborting_character_handler);
1502 g_resumable = XML_FALSE;
1503 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1504 != XML_STATUS_ERROR)
1505 xml_failure(g_parser);
1506 if (XML_GetErrorCode(g_parser) != XML_ERROR_ABORTED)
1507 xml_failure(g_parser);
1508 }
1509 END_TEST
1510
1511 /* Regression test for SF bug #1515266: missing check of stopped
1512 parser in doContext() 'for' loop. */
START_TEST(test_suspend_parser_between_char_data_calls)1513 START_TEST(test_suspend_parser_between_char_data_calls) {
1514 /* The sample data must be big enough that there are two calls to
1515 the character data handler from within the inner "for" loop of
1516 the XML_TOK_DATA_CHARS case in doContent(), and the character
1517 handler must stop the parser and clear the character data
1518 handler.
1519 */
1520 const char *text = long_character_data_text;
1521
1522 XML_SetCharacterDataHandler(g_parser, clearing_aborting_character_handler);
1523 g_resumable = XML_TRUE;
1524 // can't use SINGLE_BYTES here, because it'll return early on suspension, and
1525 // we won't know exactly how much input we actually managed to give Expat.
1526 if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE)
1527 != XML_STATUS_SUSPENDED)
1528 xml_failure(g_parser);
1529 if (XML_GetErrorCode(g_parser) != XML_ERROR_NONE)
1530 xml_failure(g_parser);
1531 /* Try parsing directly */
1532 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1533 != XML_STATUS_ERROR)
1534 fail("Attempt to continue parse while suspended not faulted");
1535 if (XML_GetErrorCode(g_parser) != XML_ERROR_SUSPENDED)
1536 fail("Suspended parse not faulted with correct error");
1537 }
1538 END_TEST
1539
1540 /* Test repeated calls to XML_StopParser are handled correctly */
START_TEST(test_repeated_stop_parser_between_char_data_calls)1541 START_TEST(test_repeated_stop_parser_between_char_data_calls) {
1542 const char *text = long_character_data_text;
1543
1544 XML_SetCharacterDataHandler(g_parser, parser_stop_character_handler);
1545 g_resumable = XML_FALSE;
1546 g_abortable = XML_FALSE;
1547 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1548 != XML_STATUS_ERROR)
1549 fail("Failed to double-stop parser");
1550
1551 XML_ParserReset(g_parser, NULL);
1552 XML_SetCharacterDataHandler(g_parser, parser_stop_character_handler);
1553 g_resumable = XML_TRUE;
1554 g_abortable = XML_FALSE;
1555 // can't use SINGLE_BYTES here, because it'll return early on suspension, and
1556 // we won't know exactly how much input we actually managed to give Expat.
1557 if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE)
1558 != XML_STATUS_SUSPENDED)
1559 fail("Failed to double-suspend parser");
1560
1561 XML_ParserReset(g_parser, NULL);
1562 XML_SetCharacterDataHandler(g_parser, parser_stop_character_handler);
1563 g_resumable = XML_TRUE;
1564 g_abortable = XML_TRUE;
1565 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1566 != XML_STATUS_ERROR)
1567 fail("Failed to suspend-abort parser");
1568 }
1569 END_TEST
1570
START_TEST(test_good_cdata_ascii)1571 START_TEST(test_good_cdata_ascii) {
1572 const char *text = "<a><![CDATA[<greeting>Hello, world!</greeting>]]></a>";
1573 const XML_Char *expected = XCS("<greeting>Hello, world!</greeting>");
1574
1575 CharData storage;
1576 CharData_Init(&storage);
1577 XML_SetUserData(g_parser, &storage);
1578 XML_SetCharacterDataHandler(g_parser, accumulate_characters);
1579 /* Add start and end handlers for coverage */
1580 XML_SetStartCdataSectionHandler(g_parser, dummy_start_cdata_handler);
1581 XML_SetEndCdataSectionHandler(g_parser, dummy_end_cdata_handler);
1582
1583 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1584 == XML_STATUS_ERROR)
1585 xml_failure(g_parser);
1586 CharData_CheckXMLChars(&storage, expected);
1587
1588 /* Try again, this time with a default handler */
1589 XML_ParserReset(g_parser, NULL);
1590 CharData_Init(&storage);
1591 XML_SetUserData(g_parser, &storage);
1592 XML_SetCharacterDataHandler(g_parser, accumulate_characters);
1593 XML_SetDefaultHandler(g_parser, dummy_default_handler);
1594
1595 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1596 == XML_STATUS_ERROR)
1597 xml_failure(g_parser);
1598 CharData_CheckXMLChars(&storage, expected);
1599 }
1600 END_TEST
1601
START_TEST(test_good_cdata_utf16)1602 START_TEST(test_good_cdata_utf16) {
1603 /* Test data is:
1604 * <?xml version='1.0' encoding='utf-16'?>
1605 * <a><![CDATA[hello]]></a>
1606 */
1607 const char text[]
1608 = "\0<\0?\0x\0m\0l\0"
1609 " \0v\0e\0r\0s\0i\0o\0n\0=\0'\0\x31\0.\0\x30\0'\0"
1610 " \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\0"
1611 "1\0"
1612 "6\0'"
1613 "\0?\0>\0\n"
1614 "\0<\0a\0>\0<\0!\0[\0C\0D\0A\0T\0A\0[\0h\0e\0l\0l\0o\0]\0]\0>\0<\0/\0a\0>";
1615 const XML_Char *expected = XCS("hello");
1616
1617 CharData storage;
1618 CharData_Init(&storage);
1619 XML_SetUserData(g_parser, &storage);
1620 XML_SetCharacterDataHandler(g_parser, accumulate_characters);
1621
1622 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
1623 == XML_STATUS_ERROR)
1624 xml_failure(g_parser);
1625 CharData_CheckXMLChars(&storage, expected);
1626 }
1627 END_TEST
1628
START_TEST(test_good_cdata_utf16_le)1629 START_TEST(test_good_cdata_utf16_le) {
1630 /* Test data is:
1631 * <?xml version='1.0' encoding='utf-16'?>
1632 * <a><![CDATA[hello]]></a>
1633 */
1634 const char text[]
1635 = "<\0?\0x\0m\0l\0"
1636 " \0v\0e\0r\0s\0i\0o\0n\0=\0'\0\x31\0.\0\x30\0'\0"
1637 " \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\0"
1638 "1\0"
1639 "6\0'"
1640 "\0?\0>\0\n"
1641 "\0<\0a\0>\0<\0!\0[\0C\0D\0A\0T\0A\0[\0h\0e\0l\0l\0o\0]\0]\0>\0<\0/\0a\0>\0";
1642 const XML_Char *expected = XCS("hello");
1643
1644 CharData storage;
1645 CharData_Init(&storage);
1646 XML_SetUserData(g_parser, &storage);
1647 XML_SetCharacterDataHandler(g_parser, accumulate_characters);
1648
1649 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
1650 == XML_STATUS_ERROR)
1651 xml_failure(g_parser);
1652 CharData_CheckXMLChars(&storage, expected);
1653 }
1654 END_TEST
1655
1656 /* Test UTF16 conversion of a long cdata string */
1657
1658 /* 16 characters: handy macro to reduce visual clutter */
1659 #define A_TO_P_IN_UTF16 "\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P"
1660
START_TEST(test_long_cdata_utf16)1661 START_TEST(test_long_cdata_utf16) {
1662 /* Test data is:
1663 * <?xlm version='1.0' encoding='utf-16'?>
1664 * <a><![CDATA[
1665 * ABCDEFGHIJKLMNOP
1666 * ]]></a>
1667 */
1668 const char text[]
1669 = "\0<\0?\0x\0m\0l\0 "
1670 "\0v\0e\0r\0s\0i\0o\0n\0=\0'\0\x31\0.\0\x30\0'\0 "
1671 "\0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\0\x31\0\x36\0'\0?\0>"
1672 "\0<\0a\0>\0<\0!\0[\0C\0D\0A\0T\0A\0["
1673 /* 64 characters per line */
1674 /* clang-format off */
1675 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1676 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1677 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1678 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1679 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1680 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1681 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1682 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1683 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1684 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1685 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1686 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1687 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1688 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1689 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1690 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16 A_TO_P_IN_UTF16
1691 A_TO_P_IN_UTF16
1692 /* clang-format on */
1693 "\0]\0]\0>\0<\0/\0a\0>";
1694 const XML_Char *expected =
1695 /* clang-format off */
1696 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1697 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1698 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1699 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1700 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1701 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1702 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1703 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1704 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1705 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1706 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1707 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1708 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1709 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1710 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1711 XCS("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")
1712 XCS("ABCDEFGHIJKLMNOP");
1713 /* clang-format on */
1714 CharData storage;
1715 void *buffer;
1716
1717 CharData_Init(&storage);
1718 XML_SetUserData(g_parser, &storage);
1719 XML_SetCharacterDataHandler(g_parser, accumulate_characters);
1720 buffer = XML_GetBuffer(g_parser, sizeof(text) - 1);
1721 if (buffer == NULL)
1722 fail("Could not allocate parse buffer");
1723 assert(buffer != NULL);
1724 memcpy(buffer, text, sizeof(text) - 1);
1725 if (XML_ParseBuffer(g_parser, sizeof(text) - 1, XML_TRUE) == XML_STATUS_ERROR)
1726 xml_failure(g_parser);
1727 CharData_CheckXMLChars(&storage, expected);
1728 }
1729 END_TEST
1730
1731 /* Test handling of multiple unit UTF-16 characters */
START_TEST(test_multichar_cdata_utf16)1732 START_TEST(test_multichar_cdata_utf16) {
1733 /* Test data is:
1734 * <?xml version='1.0' encoding='utf-16'?>
1735 * <a><![CDATA[{MINIM}{CROTCHET}]]></a>
1736 *
1737 * where {MINIM} is U+1d15e (a minim or half-note)
1738 * UTF-16: 0xd834 0xdd5e
1739 * UTF-8: 0xf0 0x9d 0x85 0x9e
1740 * and {CROTCHET} is U+1d15f (a crotchet or quarter-note)
1741 * UTF-16: 0xd834 0xdd5f
1742 * UTF-8: 0xf0 0x9d 0x85 0x9f
1743 */
1744 const char text[] = "\0<\0?\0x\0m\0l\0"
1745 " \0v\0e\0r\0s\0i\0o\0n\0=\0'\0\x31\0.\0\x30\0'\0"
1746 " \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\0"
1747 "1\0"
1748 "6\0'"
1749 "\0?\0>\0\n"
1750 "\0<\0a\0>\0<\0!\0[\0C\0D\0A\0T\0A\0["
1751 "\xd8\x34\xdd\x5e\xd8\x34\xdd\x5f"
1752 "\0]\0]\0>\0<\0/\0a\0>";
1753 #ifdef XML_UNICODE
1754 const XML_Char *expected = XCS("\xd834\xdd5e\xd834\xdd5f");
1755 #else
1756 const XML_Char *expected = XCS("\xf0\x9d\x85\x9e\xf0\x9d\x85\x9f");
1757 #endif
1758 CharData storage;
1759
1760 CharData_Init(&storage);
1761 XML_SetUserData(g_parser, &storage);
1762 XML_SetCharacterDataHandler(g_parser, accumulate_characters);
1763
1764 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
1765 == XML_STATUS_ERROR)
1766 xml_failure(g_parser);
1767 CharData_CheckXMLChars(&storage, expected);
1768 }
1769 END_TEST
1770
1771 /* Test that an element name with a UTF-16 surrogate pair is rejected */
START_TEST(test_utf16_bad_surrogate_pair)1772 START_TEST(test_utf16_bad_surrogate_pair) {
1773 /* Test data is:
1774 * <?xml version='1.0' encoding='utf-16'?>
1775 * <a><![CDATA[{BADLINB}]]></a>
1776 *
1777 * where {BADLINB} is U+10000 (the first Linear B character)
1778 * with the UTF-16 surrogate pair in the wrong order, i.e.
1779 * 0xdc00 0xd800
1780 */
1781 const char text[] = "\0<\0?\0x\0m\0l\0"
1782 " \0v\0e\0r\0s\0i\0o\0n\0=\0'\0\x31\0.\0\x30\0'\0"
1783 " \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\0"
1784 "1\0"
1785 "6\0'"
1786 "\0?\0>\0\n"
1787 "\0<\0a\0>\0<\0!\0[\0C\0D\0A\0T\0A\0["
1788 "\xdc\x00\xd8\x00"
1789 "\0]\0]\0>\0<\0/\0a\0>";
1790
1791 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
1792 != XML_STATUS_ERROR)
1793 fail("Reversed UTF-16 surrogate pair not faulted");
1794 if (XML_GetErrorCode(g_parser) != XML_ERROR_INVALID_TOKEN)
1795 xml_failure(g_parser);
1796 }
1797 END_TEST
1798
START_TEST(test_bad_cdata)1799 START_TEST(test_bad_cdata) {
1800 struct CaseData {
1801 const char *text;
1802 enum XML_Error expectedError;
1803 };
1804
1805 struct CaseData cases[]
1806 = {{"<a><", XML_ERROR_UNCLOSED_TOKEN},
1807 {"<a><!", XML_ERROR_UNCLOSED_TOKEN},
1808 {"<a><![", XML_ERROR_UNCLOSED_TOKEN},
1809 {"<a><![C", XML_ERROR_UNCLOSED_TOKEN},
1810 {"<a><![CD", XML_ERROR_UNCLOSED_TOKEN},
1811 {"<a><![CDA", XML_ERROR_UNCLOSED_TOKEN},
1812 {"<a><![CDAT", XML_ERROR_UNCLOSED_TOKEN},
1813 {"<a><![CDATA", XML_ERROR_UNCLOSED_TOKEN},
1814
1815 {"<a><![CDATA[", XML_ERROR_UNCLOSED_CDATA_SECTION},
1816 {"<a><![CDATA[]", XML_ERROR_UNCLOSED_CDATA_SECTION},
1817 {"<a><![CDATA[]]", XML_ERROR_UNCLOSED_CDATA_SECTION},
1818
1819 {"<a><!<a/>", XML_ERROR_INVALID_TOKEN},
1820 {"<a><![<a/>", XML_ERROR_UNCLOSED_TOKEN}, /* ?! */
1821 {"<a><![C<a/>", XML_ERROR_UNCLOSED_TOKEN}, /* ?! */
1822 {"<a><![CD<a/>", XML_ERROR_INVALID_TOKEN},
1823 {"<a><![CDA<a/>", XML_ERROR_INVALID_TOKEN},
1824 {"<a><![CDAT<a/>", XML_ERROR_INVALID_TOKEN},
1825 {"<a><![CDATA<a/>", XML_ERROR_INVALID_TOKEN},
1826
1827 {"<a><![CDATA[<a/>", XML_ERROR_UNCLOSED_CDATA_SECTION},
1828 {"<a><![CDATA[]<a/>", XML_ERROR_UNCLOSED_CDATA_SECTION},
1829 {"<a><![CDATA[]]<a/>", XML_ERROR_UNCLOSED_CDATA_SECTION}};
1830
1831 size_t i = 0;
1832 for (; i < sizeof(cases) / sizeof(struct CaseData); i++) {
1833 set_subtest("%s", cases[i].text);
1834 const enum XML_Status actualStatus = _XML_Parse_SINGLE_BYTES(
1835 g_parser, cases[i].text, (int)strlen(cases[i].text), XML_TRUE);
1836 const enum XML_Error actualError = XML_GetErrorCode(g_parser);
1837
1838 assert(actualStatus == XML_STATUS_ERROR);
1839
1840 if (actualError != cases[i].expectedError) {
1841 char message[100];
1842 snprintf(message, sizeof(message),
1843 "Expected error %d but got error %d for case %u: \"%s\"\n",
1844 cases[i].expectedError, actualError, (unsigned int)i + 1,
1845 cases[i].text);
1846 fail(message);
1847 }
1848
1849 XML_ParserReset(g_parser, NULL);
1850 }
1851 }
1852 END_TEST
1853
1854 /* Test failures in UTF-16 CDATA */
START_TEST(test_bad_cdata_utf16)1855 START_TEST(test_bad_cdata_utf16) {
1856 struct CaseData {
1857 size_t text_bytes;
1858 const char *text;
1859 enum XML_Error expected_error;
1860 };
1861
1862 const char prolog[] = "\0<\0?\0x\0m\0l\0"
1863 " \0v\0e\0r\0s\0i\0o\0n\0=\0'\0\x31\0.\0\x30\0'\0"
1864 " \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\0"
1865 "1\0"
1866 "6\0'"
1867 "\0?\0>\0\n"
1868 "\0<\0a\0>";
1869 struct CaseData cases[] = {
1870 {1, "\0", XML_ERROR_UNCLOSED_TOKEN},
1871 {2, "\0<", XML_ERROR_UNCLOSED_TOKEN},
1872 {3, "\0<\0", XML_ERROR_UNCLOSED_TOKEN},
1873 {4, "\0<\0!", XML_ERROR_UNCLOSED_TOKEN},
1874 {5, "\0<\0!\0", XML_ERROR_UNCLOSED_TOKEN},
1875 {6, "\0<\0!\0[", XML_ERROR_UNCLOSED_TOKEN},
1876 {7, "\0<\0!\0[\0", XML_ERROR_UNCLOSED_TOKEN},
1877 {8, "\0<\0!\0[\0C", XML_ERROR_UNCLOSED_TOKEN},
1878 {9, "\0<\0!\0[\0C\0", XML_ERROR_UNCLOSED_TOKEN},
1879 {10, "\0<\0!\0[\0C\0D", XML_ERROR_UNCLOSED_TOKEN},
1880 {11, "\0<\0!\0[\0C\0D\0", XML_ERROR_UNCLOSED_TOKEN},
1881 {12, "\0<\0!\0[\0C\0D\0A", XML_ERROR_UNCLOSED_TOKEN},
1882 {13, "\0<\0!\0[\0C\0D\0A\0", XML_ERROR_UNCLOSED_TOKEN},
1883 {14, "\0<\0!\0[\0C\0D\0A\0T", XML_ERROR_UNCLOSED_TOKEN},
1884 {15, "\0<\0!\0[\0C\0D\0A\0T\0", XML_ERROR_UNCLOSED_TOKEN},
1885 {16, "\0<\0!\0[\0C\0D\0A\0T\0A", XML_ERROR_UNCLOSED_TOKEN},
1886 {17, "\0<\0!\0[\0C\0D\0A\0T\0A\0", XML_ERROR_UNCLOSED_TOKEN},
1887 {18, "\0<\0!\0[\0C\0D\0A\0T\0A\0[", XML_ERROR_UNCLOSED_CDATA_SECTION},
1888 {19, "\0<\0!\0[\0C\0D\0A\0T\0A\0[\0", XML_ERROR_UNCLOSED_CDATA_SECTION},
1889 {20, "\0<\0!\0[\0C\0D\0A\0T\0A\0[\0Z", XML_ERROR_UNCLOSED_CDATA_SECTION},
1890 /* Now add a four-byte UTF-16 character */
1891 {21, "\0<\0!\0[\0C\0D\0A\0T\0A\0[\0Z\xd8",
1892 XML_ERROR_UNCLOSED_CDATA_SECTION},
1893 {22, "\0<\0!\0[\0C\0D\0A\0T\0A\0[\0Z\xd8\x34", XML_ERROR_PARTIAL_CHAR},
1894 {23, "\0<\0!\0[\0C\0D\0A\0T\0A\0[\0Z\xd8\x34\xdd",
1895 XML_ERROR_PARTIAL_CHAR},
1896 {24, "\0<\0!\0[\0C\0D\0A\0T\0A\0[\0Z\xd8\x34\xdd\x5e",
1897 XML_ERROR_UNCLOSED_CDATA_SECTION}};
1898 size_t i;
1899
1900 for (i = 0; i < sizeof(cases) / sizeof(struct CaseData); i++) {
1901 set_subtest("case %lu", (long unsigned)(i + 1));
1902 enum XML_Status actual_status;
1903 enum XML_Error actual_error;
1904
1905 if (_XML_Parse_SINGLE_BYTES(g_parser, prolog, (int)sizeof(prolog) - 1,
1906 XML_FALSE)
1907 == XML_STATUS_ERROR)
1908 xml_failure(g_parser);
1909 actual_status = _XML_Parse_SINGLE_BYTES(g_parser, cases[i].text,
1910 (int)cases[i].text_bytes, XML_TRUE);
1911 assert(actual_status == XML_STATUS_ERROR);
1912 actual_error = XML_GetErrorCode(g_parser);
1913 if (actual_error != cases[i].expected_error) {
1914 char message[1024];
1915
1916 snprintf(message, sizeof(message),
1917 "Expected error %d (%" XML_FMT_STR "), got %d (%" XML_FMT_STR
1918 ") for case %lu\n",
1919 cases[i].expected_error,
1920 XML_ErrorString(cases[i].expected_error), actual_error,
1921 XML_ErrorString(actual_error), (long unsigned)(i + 1));
1922 fail(message);
1923 }
1924 XML_ParserReset(g_parser, NULL);
1925 }
1926 }
1927 END_TEST
1928
1929 /* Test stopping the parser in cdata handler */
START_TEST(test_stop_parser_between_cdata_calls)1930 START_TEST(test_stop_parser_between_cdata_calls) {
1931 const char *text = long_cdata_text;
1932
1933 XML_SetCharacterDataHandler(g_parser, clearing_aborting_character_handler);
1934 g_resumable = XML_FALSE;
1935 expect_failure(text, XML_ERROR_ABORTED, "Parse not aborted in CDATA handler");
1936 }
1937 END_TEST
1938
1939 /* Test suspending the parser in cdata handler */
START_TEST(test_suspend_parser_between_cdata_calls)1940 START_TEST(test_suspend_parser_between_cdata_calls) {
1941 if (g_chunkSize != 0) {
1942 // this test does not use SINGLE_BYTES, because of suspension
1943 return;
1944 }
1945
1946 const char *text = long_cdata_text;
1947 enum XML_Status result;
1948
1949 XML_SetCharacterDataHandler(g_parser, clearing_aborting_character_handler);
1950 g_resumable = XML_TRUE;
1951 // can't use SINGLE_BYTES here, because it'll return early on suspension, and
1952 // we won't know exactly how much input we actually managed to give Expat.
1953 result = XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE);
1954 if (result != XML_STATUS_SUSPENDED) {
1955 if (result == XML_STATUS_ERROR)
1956 xml_failure(g_parser);
1957 fail("Parse not suspended in CDATA handler");
1958 }
1959 if (XML_GetErrorCode(g_parser) != XML_ERROR_NONE)
1960 xml_failure(g_parser);
1961 }
1962 END_TEST
1963
1964 /* Test memory allocation functions */
START_TEST(test_memory_allocation)1965 START_TEST(test_memory_allocation) {
1966 char *buffer = (char *)XML_MemMalloc(g_parser, 256);
1967 char *p;
1968
1969 if (buffer == NULL) {
1970 fail("Allocation failed");
1971 } else {
1972 /* Try writing to memory; some OSes try to cheat! */
1973 buffer[0] = 'T';
1974 buffer[1] = 'E';
1975 buffer[2] = 'S';
1976 buffer[3] = 'T';
1977 buffer[4] = '\0';
1978 if (strcmp(buffer, "TEST") != 0) {
1979 fail("Memory not writable");
1980 } else {
1981 p = (char *)XML_MemRealloc(g_parser, buffer, 512);
1982 if (p == NULL) {
1983 fail("Reallocation failed");
1984 } else {
1985 /* Write again, just to be sure */
1986 buffer = p;
1987 buffer[0] = 'V';
1988 if (strcmp(buffer, "VEST") != 0) {
1989 fail("Reallocated memory not writable");
1990 }
1991 }
1992 }
1993 XML_MemFree(g_parser, buffer);
1994 }
1995 }
1996 END_TEST
1997
1998 /* Test XML_DefaultCurrent() passes handling on correctly */
START_TEST(test_default_current)1999 START_TEST(test_default_current) {
2000 const char *text = "<doc>hell]</doc>";
2001 const char *entity_text = "<!DOCTYPE doc [\n"
2002 "<!ENTITY entity '%'>\n"
2003 "]>\n"
2004 "<doc>&entity;</doc>";
2005
2006 set_subtest("with defaulting");
2007 {
2008 struct handler_record_list storage;
2009 storage.count = 0;
2010 XML_SetDefaultHandler(g_parser, record_default_handler);
2011 XML_SetCharacterDataHandler(g_parser, record_cdata_handler);
2012 XML_SetUserData(g_parser, &storage);
2013 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2014 == XML_STATUS_ERROR)
2015 xml_failure(g_parser);
2016 int i = 0;
2017 assert_record_handler_called(&storage, i++, "record_default_handler", 5);
2018 // we should have gotten one or more cdata callbacks, totaling 5 chars
2019 int cdata_len_remaining = 5;
2020 while (cdata_len_remaining > 0) {
2021 const struct handler_record_entry *c_entry
2022 = handler_record_get(&storage, i++);
2023 assert_true(strcmp(c_entry->name, "record_cdata_handler") == 0);
2024 assert_true(c_entry->arg > 0);
2025 assert_true(c_entry->arg <= cdata_len_remaining);
2026 cdata_len_remaining -= c_entry->arg;
2027 // default handler must follow, with the exact same len argument.
2028 assert_record_handler_called(&storage, i++, "record_default_handler",
2029 c_entry->arg);
2030 }
2031 assert_record_handler_called(&storage, i++, "record_default_handler", 6);
2032 assert_true(storage.count == i);
2033 }
2034
2035 /* Again, without the defaulting */
2036 set_subtest("no defaulting");
2037 {
2038 struct handler_record_list storage;
2039 storage.count = 0;
2040 XML_ParserReset(g_parser, NULL);
2041 XML_SetDefaultHandler(g_parser, record_default_handler);
2042 XML_SetCharacterDataHandler(g_parser, record_cdata_nodefault_handler);
2043 XML_SetUserData(g_parser, &storage);
2044 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2045 == XML_STATUS_ERROR)
2046 xml_failure(g_parser);
2047 int i = 0;
2048 assert_record_handler_called(&storage, i++, "record_default_handler", 5);
2049 // we should have gotten one or more cdata callbacks, totaling 5 chars
2050 int cdata_len_remaining = 5;
2051 while (cdata_len_remaining > 0) {
2052 const struct handler_record_entry *c_entry
2053 = handler_record_get(&storage, i++);
2054 assert_true(strcmp(c_entry->name, "record_cdata_nodefault_handler") == 0);
2055 assert_true(c_entry->arg > 0);
2056 assert_true(c_entry->arg <= cdata_len_remaining);
2057 cdata_len_remaining -= c_entry->arg;
2058 }
2059 assert_record_handler_called(&storage, i++, "record_default_handler", 6);
2060 assert_true(storage.count == i);
2061 }
2062
2063 /* Now with an internal entity to complicate matters */
2064 set_subtest("with internal entity");
2065 {
2066 struct handler_record_list storage;
2067 storage.count = 0;
2068 XML_ParserReset(g_parser, NULL);
2069 XML_SetDefaultHandler(g_parser, record_default_handler);
2070 XML_SetCharacterDataHandler(g_parser, record_cdata_handler);
2071 XML_SetUserData(g_parser, &storage);
2072 if (_XML_Parse_SINGLE_BYTES(g_parser, entity_text, (int)strlen(entity_text),
2073 XML_TRUE)
2074 == XML_STATUS_ERROR)
2075 xml_failure(g_parser);
2076 /* The default handler suppresses the entity */
2077 assert_record_handler_called(&storage, 0, "record_default_handler", 9);
2078 assert_record_handler_called(&storage, 1, "record_default_handler", 1);
2079 assert_record_handler_called(&storage, 2, "record_default_handler", 3);
2080 assert_record_handler_called(&storage, 3, "record_default_handler", 1);
2081 assert_record_handler_called(&storage, 4, "record_default_handler", 1);
2082 assert_record_handler_called(&storage, 5, "record_default_handler", 1);
2083 assert_record_handler_called(&storage, 6, "record_default_handler", 8);
2084 assert_record_handler_called(&storage, 7, "record_default_handler", 1);
2085 assert_record_handler_called(&storage, 8, "record_default_handler", 6);
2086 assert_record_handler_called(&storage, 9, "record_default_handler", 1);
2087 assert_record_handler_called(&storage, 10, "record_default_handler", 7);
2088 assert_record_handler_called(&storage, 11, "record_default_handler", 1);
2089 assert_record_handler_called(&storage, 12, "record_default_handler", 1);
2090 assert_record_handler_called(&storage, 13, "record_default_handler", 1);
2091 assert_record_handler_called(&storage, 14, "record_default_handler", 1);
2092 assert_record_handler_called(&storage, 15, "record_default_handler", 1);
2093 assert_record_handler_called(&storage, 16, "record_default_handler", 5);
2094 assert_record_handler_called(&storage, 17, "record_default_handler", 8);
2095 assert_record_handler_called(&storage, 18, "record_default_handler", 6);
2096 assert_true(storage.count == 19);
2097 }
2098
2099 /* Again, with a skip handler */
2100 set_subtest("with skip handler");
2101 {
2102 struct handler_record_list storage;
2103 storage.count = 0;
2104 XML_ParserReset(g_parser, NULL);
2105 XML_SetDefaultHandler(g_parser, record_default_handler);
2106 XML_SetCharacterDataHandler(g_parser, record_cdata_handler);
2107 XML_SetSkippedEntityHandler(g_parser, record_skip_handler);
2108 XML_SetUserData(g_parser, &storage);
2109 if (_XML_Parse_SINGLE_BYTES(g_parser, entity_text, (int)strlen(entity_text),
2110 XML_TRUE)
2111 == XML_STATUS_ERROR)
2112 xml_failure(g_parser);
2113 /* The default handler suppresses the entity */
2114 assert_record_handler_called(&storage, 0, "record_default_handler", 9);
2115 assert_record_handler_called(&storage, 1, "record_default_handler", 1);
2116 assert_record_handler_called(&storage, 2, "record_default_handler", 3);
2117 assert_record_handler_called(&storage, 3, "record_default_handler", 1);
2118 assert_record_handler_called(&storage, 4, "record_default_handler", 1);
2119 assert_record_handler_called(&storage, 5, "record_default_handler", 1);
2120 assert_record_handler_called(&storage, 6, "record_default_handler", 8);
2121 assert_record_handler_called(&storage, 7, "record_default_handler", 1);
2122 assert_record_handler_called(&storage, 8, "record_default_handler", 6);
2123 assert_record_handler_called(&storage, 9, "record_default_handler", 1);
2124 assert_record_handler_called(&storage, 10, "record_default_handler", 7);
2125 assert_record_handler_called(&storage, 11, "record_default_handler", 1);
2126 assert_record_handler_called(&storage, 12, "record_default_handler", 1);
2127 assert_record_handler_called(&storage, 13, "record_default_handler", 1);
2128 assert_record_handler_called(&storage, 14, "record_default_handler", 1);
2129 assert_record_handler_called(&storage, 15, "record_default_handler", 1);
2130 assert_record_handler_called(&storage, 16, "record_default_handler", 5);
2131 assert_record_handler_called(&storage, 17, "record_skip_handler", 0);
2132 assert_record_handler_called(&storage, 18, "record_default_handler", 6);
2133 assert_true(storage.count == 19);
2134 }
2135
2136 /* This time, allow the entity through */
2137 set_subtest("allow entity");
2138 {
2139 struct handler_record_list storage;
2140 storage.count = 0;
2141 XML_ParserReset(g_parser, NULL);
2142 XML_SetDefaultHandlerExpand(g_parser, record_default_handler);
2143 XML_SetCharacterDataHandler(g_parser, record_cdata_handler);
2144 XML_SetUserData(g_parser, &storage);
2145 if (_XML_Parse_SINGLE_BYTES(g_parser, entity_text, (int)strlen(entity_text),
2146 XML_TRUE)
2147 == XML_STATUS_ERROR)
2148 xml_failure(g_parser);
2149 assert_record_handler_called(&storage, 0, "record_default_handler", 9);
2150 assert_record_handler_called(&storage, 1, "record_default_handler", 1);
2151 assert_record_handler_called(&storage, 2, "record_default_handler", 3);
2152 assert_record_handler_called(&storage, 3, "record_default_handler", 1);
2153 assert_record_handler_called(&storage, 4, "record_default_handler", 1);
2154 assert_record_handler_called(&storage, 5, "record_default_handler", 1);
2155 assert_record_handler_called(&storage, 6, "record_default_handler", 8);
2156 assert_record_handler_called(&storage, 7, "record_default_handler", 1);
2157 assert_record_handler_called(&storage, 8, "record_default_handler", 6);
2158 assert_record_handler_called(&storage, 9, "record_default_handler", 1);
2159 assert_record_handler_called(&storage, 10, "record_default_handler", 7);
2160 assert_record_handler_called(&storage, 11, "record_default_handler", 1);
2161 assert_record_handler_called(&storage, 12, "record_default_handler", 1);
2162 assert_record_handler_called(&storage, 13, "record_default_handler", 1);
2163 assert_record_handler_called(&storage, 14, "record_default_handler", 1);
2164 assert_record_handler_called(&storage, 15, "record_default_handler", 1);
2165 assert_record_handler_called(&storage, 16, "record_default_handler", 5);
2166 assert_record_handler_called(&storage, 17, "record_cdata_handler", 1);
2167 assert_record_handler_called(&storage, 18, "record_default_handler", 1);
2168 assert_record_handler_called(&storage, 19, "record_default_handler", 6);
2169 assert_true(storage.count == 20);
2170 }
2171
2172 /* Finally, without passing the cdata to the default handler */
2173 set_subtest("not passing cdata");
2174 {
2175 struct handler_record_list storage;
2176 storage.count = 0;
2177 XML_ParserReset(g_parser, NULL);
2178 XML_SetDefaultHandlerExpand(g_parser, record_default_handler);
2179 XML_SetCharacterDataHandler(g_parser, record_cdata_nodefault_handler);
2180 XML_SetUserData(g_parser, &storage);
2181 if (_XML_Parse_SINGLE_BYTES(g_parser, entity_text, (int)strlen(entity_text),
2182 XML_TRUE)
2183 == XML_STATUS_ERROR)
2184 xml_failure(g_parser);
2185 assert_record_handler_called(&storage, 0, "record_default_handler", 9);
2186 assert_record_handler_called(&storage, 1, "record_default_handler", 1);
2187 assert_record_handler_called(&storage, 2, "record_default_handler", 3);
2188 assert_record_handler_called(&storage, 3, "record_default_handler", 1);
2189 assert_record_handler_called(&storage, 4, "record_default_handler", 1);
2190 assert_record_handler_called(&storage, 5, "record_default_handler", 1);
2191 assert_record_handler_called(&storage, 6, "record_default_handler", 8);
2192 assert_record_handler_called(&storage, 7, "record_default_handler", 1);
2193 assert_record_handler_called(&storage, 8, "record_default_handler", 6);
2194 assert_record_handler_called(&storage, 9, "record_default_handler", 1);
2195 assert_record_handler_called(&storage, 10, "record_default_handler", 7);
2196 assert_record_handler_called(&storage, 11, "record_default_handler", 1);
2197 assert_record_handler_called(&storage, 12, "record_default_handler", 1);
2198 assert_record_handler_called(&storage, 13, "record_default_handler", 1);
2199 assert_record_handler_called(&storage, 14, "record_default_handler", 1);
2200 assert_record_handler_called(&storage, 15, "record_default_handler", 1);
2201 assert_record_handler_called(&storage, 16, "record_default_handler", 5);
2202 assert_record_handler_called(&storage, 17, "record_cdata_nodefault_handler",
2203 1);
2204 assert_record_handler_called(&storage, 18, "record_default_handler", 6);
2205 assert_true(storage.count == 19);
2206 }
2207 }
2208 END_TEST
2209
2210 /* Test DTD element parsing code paths */
START_TEST(test_dtd_elements)2211 START_TEST(test_dtd_elements) {
2212 const char *text = "<!DOCTYPE doc [\n"
2213 "<!ELEMENT doc (chapter)>\n"
2214 "<!ELEMENT chapter (#PCDATA)>\n"
2215 "]>\n"
2216 "<doc><chapter>Wombats are go</chapter></doc>";
2217
2218 XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
2219 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2220 == XML_STATUS_ERROR)
2221 xml_failure(g_parser);
2222 }
2223 END_TEST
2224
2225 static void XMLCALL
element_decl_check_model(void * userData,const XML_Char * name,XML_Content * model)2226 element_decl_check_model(void *userData, const XML_Char *name,
2227 XML_Content *model) {
2228 UNUSED_P(userData);
2229 uint32_t errorFlags = 0;
2230
2231 /* Expected model array structure is this:
2232 * [0] (type 6, quant 0)
2233 * [1] (type 5, quant 0)
2234 * [3] (type 4, quant 0, name "bar")
2235 * [4] (type 4, quant 0, name "foo")
2236 * [5] (type 4, quant 3, name "xyz")
2237 * [2] (type 4, quant 2, name "zebra")
2238 */
2239 errorFlags |= ((xcstrcmp(name, XCS("junk")) == 0) ? 0 : (1u << 0));
2240 errorFlags |= ((model != NULL) ? 0 : (1u << 1));
2241
2242 if (model != NULL) {
2243 errorFlags |= ((model[0].type == XML_CTYPE_SEQ) ? 0 : (1u << 2));
2244 errorFlags |= ((model[0].quant == XML_CQUANT_NONE) ? 0 : (1u << 3));
2245 errorFlags |= ((model[0].numchildren == 2) ? 0 : (1u << 4));
2246 errorFlags |= ((model[0].children == &model[1]) ? 0 : (1u << 5));
2247 errorFlags |= ((model[0].name == NULL) ? 0 : (1u << 6));
2248
2249 errorFlags |= ((model[1].type == XML_CTYPE_CHOICE) ? 0 : (1u << 7));
2250 errorFlags |= ((model[1].quant == XML_CQUANT_NONE) ? 0 : (1u << 8));
2251 errorFlags |= ((model[1].numchildren == 3) ? 0 : (1u << 9));
2252 errorFlags |= ((model[1].children == &model[3]) ? 0 : (1u << 10));
2253 errorFlags |= ((model[1].name == NULL) ? 0 : (1u << 11));
2254
2255 errorFlags |= ((model[2].type == XML_CTYPE_NAME) ? 0 : (1u << 12));
2256 errorFlags |= ((model[2].quant == XML_CQUANT_REP) ? 0 : (1u << 13));
2257 errorFlags |= ((model[2].numchildren == 0) ? 0 : (1u << 14));
2258 errorFlags |= ((model[2].children == NULL) ? 0 : (1u << 15));
2259 errorFlags
2260 |= ((xcstrcmp(model[2].name, XCS("zebra")) == 0) ? 0 : (1u << 16));
2261
2262 errorFlags |= ((model[3].type == XML_CTYPE_NAME) ? 0 : (1u << 17));
2263 errorFlags |= ((model[3].quant == XML_CQUANT_NONE) ? 0 : (1u << 18));
2264 errorFlags |= ((model[3].numchildren == 0) ? 0 : (1u << 19));
2265 errorFlags |= ((model[3].children == NULL) ? 0 : (1u << 20));
2266 errorFlags |= ((xcstrcmp(model[3].name, XCS("bar")) == 0) ? 0 : (1u << 21));
2267
2268 errorFlags |= ((model[4].type == XML_CTYPE_NAME) ? 0 : (1u << 22));
2269 errorFlags |= ((model[4].quant == XML_CQUANT_NONE) ? 0 : (1u << 23));
2270 errorFlags |= ((model[4].numchildren == 0) ? 0 : (1u << 24));
2271 errorFlags |= ((model[4].children == NULL) ? 0 : (1u << 25));
2272 errorFlags |= ((xcstrcmp(model[4].name, XCS("foo")) == 0) ? 0 : (1u << 26));
2273
2274 errorFlags |= ((model[5].type == XML_CTYPE_NAME) ? 0 : (1u << 27));
2275 errorFlags |= ((model[5].quant == XML_CQUANT_PLUS) ? 0 : (1u << 28));
2276 errorFlags |= ((model[5].numchildren == 0) ? 0 : (1u << 29));
2277 errorFlags |= ((model[5].children == NULL) ? 0 : (1u << 30));
2278 errorFlags |= ((xcstrcmp(model[5].name, XCS("xyz")) == 0) ? 0 : (1u << 31));
2279 }
2280
2281 XML_SetUserData(g_parser, (void *)(uintptr_t)errorFlags);
2282 XML_FreeContentModel(g_parser, model);
2283 }
2284
START_TEST(test_dtd_elements_nesting)2285 START_TEST(test_dtd_elements_nesting) {
2286 // Payload inspired by a test in Perl's XML::Parser
2287 const char *text = "<!DOCTYPE foo [\n"
2288 "<!ELEMENT junk ((bar|foo|xyz+), zebra*)>\n"
2289 "]>\n"
2290 "<foo/>";
2291
2292 XML_SetUserData(g_parser, (void *)(uintptr_t)-1);
2293
2294 XML_SetElementDeclHandler(g_parser, element_decl_check_model);
2295 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2296 == XML_STATUS_ERROR)
2297 xml_failure(g_parser);
2298
2299 if ((uint32_t)(uintptr_t)XML_GetUserData(g_parser) != 0)
2300 fail("Element declaration model regression detected");
2301 }
2302 END_TEST
2303
2304 /* Test foreign DTD handling */
START_TEST(test_set_foreign_dtd)2305 START_TEST(test_set_foreign_dtd) {
2306 const char *text1 = "<?xml version='1.0' encoding='us-ascii'?>\n";
2307 const char *text2 = "<doc>&entity;</doc>";
2308 ExtTest test_data = {"<!ELEMENT doc (#PCDATA)*>", NULL, NULL};
2309
2310 /* Check hash salt is passed through too */
2311 XML_SetHashSalt(g_parser, 0x12345678);
2312 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2313 XML_SetUserData(g_parser, &test_data);
2314 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
2315 /* Add a default handler to exercise more code paths */
2316 XML_SetDefaultHandler(g_parser, dummy_default_handler);
2317 if (XML_UseForeignDTD(g_parser, XML_TRUE) != XML_ERROR_NONE)
2318 fail("Could not set foreign DTD");
2319 if (_XML_Parse_SINGLE_BYTES(g_parser, text1, (int)strlen(text1), XML_FALSE)
2320 == XML_STATUS_ERROR)
2321 xml_failure(g_parser);
2322
2323 /* Ensure that trying to set the DTD after parsing has started
2324 * is faulted, even if it's the same setting.
2325 */
2326 if (XML_UseForeignDTD(g_parser, XML_TRUE)
2327 != XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING)
2328 fail("Failed to reject late foreign DTD setting");
2329 /* Ditto for the hash salt */
2330 if (XML_SetHashSalt(g_parser, 0x23456789))
2331 fail("Failed to reject late hash salt change");
2332
2333 /* Now finish the parse */
2334 if (_XML_Parse_SINGLE_BYTES(g_parser, text2, (int)strlen(text2), XML_TRUE)
2335 == XML_STATUS_ERROR)
2336 xml_failure(g_parser);
2337 }
2338 END_TEST
2339
2340 /* Test foreign DTD handling with a failing NotStandalone handler */
START_TEST(test_foreign_dtd_not_standalone)2341 START_TEST(test_foreign_dtd_not_standalone) {
2342 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
2343 "<doc>&entity;</doc>";
2344 ExtTest test_data = {"<!ELEMENT doc (#PCDATA)*>", NULL, NULL};
2345
2346 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2347 XML_SetUserData(g_parser, &test_data);
2348 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
2349 XML_SetNotStandaloneHandler(g_parser, reject_not_standalone_handler);
2350 if (XML_UseForeignDTD(g_parser, XML_TRUE) != XML_ERROR_NONE)
2351 fail("Could not set foreign DTD");
2352 expect_failure(text, XML_ERROR_NOT_STANDALONE,
2353 "NotStandalonehandler failed to reject");
2354 }
2355 END_TEST
2356
2357 /* Test invalid character in a foreign DTD is faulted */
START_TEST(test_invalid_foreign_dtd)2358 START_TEST(test_invalid_foreign_dtd) {
2359 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
2360 "<doc>&entity;</doc>";
2361 ExtFaults test_data
2362 = {"$", "Dollar not faulted", NULL, XML_ERROR_INVALID_TOKEN};
2363
2364 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2365 XML_SetUserData(g_parser, &test_data);
2366 XML_SetExternalEntityRefHandler(g_parser, external_entity_faulter);
2367 XML_UseForeignDTD(g_parser, XML_TRUE);
2368 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
2369 "Bad DTD should not have been accepted");
2370 }
2371 END_TEST
2372
2373 /* Test foreign DTD use with a doctype */
START_TEST(test_foreign_dtd_with_doctype)2374 START_TEST(test_foreign_dtd_with_doctype) {
2375 const char *text1 = "<?xml version='1.0' encoding='us-ascii'?>\n"
2376 "<!DOCTYPE doc [<!ENTITY entity 'hello world'>]>\n";
2377 const char *text2 = "<doc>&entity;</doc>";
2378 ExtTest test_data = {"<!ELEMENT doc (#PCDATA)*>", NULL, NULL};
2379
2380 /* Check hash salt is passed through too */
2381 XML_SetHashSalt(g_parser, 0x12345678);
2382 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2383 XML_SetUserData(g_parser, &test_data);
2384 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
2385 /* Add a default handler to exercise more code paths */
2386 XML_SetDefaultHandler(g_parser, dummy_default_handler);
2387 if (XML_UseForeignDTD(g_parser, XML_TRUE) != XML_ERROR_NONE)
2388 fail("Could not set foreign DTD");
2389 if (_XML_Parse_SINGLE_BYTES(g_parser, text1, (int)strlen(text1), XML_FALSE)
2390 == XML_STATUS_ERROR)
2391 xml_failure(g_parser);
2392
2393 /* Ensure that trying to set the DTD after parsing has started
2394 * is faulted, even if it's the same setting.
2395 */
2396 if (XML_UseForeignDTD(g_parser, XML_TRUE)
2397 != XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING)
2398 fail("Failed to reject late foreign DTD setting");
2399 /* Ditto for the hash salt */
2400 if (XML_SetHashSalt(g_parser, 0x23456789))
2401 fail("Failed to reject late hash salt change");
2402
2403 /* Now finish the parse */
2404 if (_XML_Parse_SINGLE_BYTES(g_parser, text2, (int)strlen(text2), XML_TRUE)
2405 == XML_STATUS_ERROR)
2406 xml_failure(g_parser);
2407 }
2408 END_TEST
2409
2410 /* Test XML_UseForeignDTD with no external subset present */
START_TEST(test_foreign_dtd_without_external_subset)2411 START_TEST(test_foreign_dtd_without_external_subset) {
2412 const char *text = "<!DOCTYPE doc [<!ENTITY foo 'bar'>]>\n"
2413 "<doc>&foo;</doc>";
2414
2415 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2416 XML_SetUserData(g_parser, NULL);
2417 XML_SetExternalEntityRefHandler(g_parser, external_entity_null_loader);
2418 XML_UseForeignDTD(g_parser, XML_TRUE);
2419 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2420 == XML_STATUS_ERROR)
2421 xml_failure(g_parser);
2422 }
2423 END_TEST
2424
START_TEST(test_empty_foreign_dtd)2425 START_TEST(test_empty_foreign_dtd) {
2426 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
2427 "<doc>&entity;</doc>";
2428
2429 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2430 XML_SetExternalEntityRefHandler(g_parser, external_entity_null_loader);
2431 XML_UseForeignDTD(g_parser, XML_TRUE);
2432 expect_failure(text, XML_ERROR_UNDEFINED_ENTITY,
2433 "Undefined entity not faulted");
2434 }
2435 END_TEST
2436
2437 /* Test XML Base is set and unset appropriately */
START_TEST(test_set_base)2438 START_TEST(test_set_base) {
2439 const XML_Char *old_base;
2440 const XML_Char *new_base = XCS("/local/file/name.xml");
2441
2442 old_base = XML_GetBase(g_parser);
2443 if (XML_SetBase(g_parser, new_base) != XML_STATUS_OK)
2444 fail("Unable to set base");
2445 if (xcstrcmp(XML_GetBase(g_parser), new_base) != 0)
2446 fail("Base setting not correct");
2447 if (XML_SetBase(g_parser, NULL) != XML_STATUS_OK)
2448 fail("Unable to NULL base");
2449 if (XML_GetBase(g_parser) != NULL)
2450 fail("Base setting not nulled");
2451 XML_SetBase(g_parser, old_base);
2452 }
2453 END_TEST
2454
2455 /* Test attribute counts, indexing, etc */
START_TEST(test_attributes)2456 START_TEST(test_attributes) {
2457 const char *text = "<!DOCTYPE doc [\n"
2458 "<!ELEMENT doc (tag)>\n"
2459 "<!ATTLIST doc id ID #REQUIRED>\n"
2460 "]>"
2461 "<doc a='1' id='one' b='2'>"
2462 "<tag c='3'/>"
2463 "</doc>";
2464 AttrInfo doc_info[] = {{XCS("a"), XCS("1")},
2465 {XCS("b"), XCS("2")},
2466 {XCS("id"), XCS("one")},
2467 {NULL, NULL}};
2468 AttrInfo tag_info[] = {{XCS("c"), XCS("3")}, {NULL, NULL}};
2469 ElementInfo info[] = {{XCS("doc"), 3, XCS("id"), NULL},
2470 {XCS("tag"), 1, NULL, NULL},
2471 {NULL, 0, NULL, NULL}};
2472 info[0].attributes = doc_info;
2473 info[1].attributes = tag_info;
2474
2475 XML_Parser parser = XML_ParserCreate(NULL);
2476 assert_true(parser != NULL);
2477 ParserAndElementInfo parserAndElementInfos = {
2478 parser,
2479 info,
2480 };
2481
2482 XML_SetStartElementHandler(parser, counting_start_element_handler);
2483 XML_SetUserData(parser, &parserAndElementInfos);
2484 if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
2485 == XML_STATUS_ERROR)
2486 xml_failure(parser);
2487
2488 XML_ParserFree(parser);
2489 }
2490 END_TEST
2491
2492 /* Test reset works correctly in the middle of processing an internal
2493 * entity. Exercises some obscure code in XML_ParserReset().
2494 */
START_TEST(test_reset_in_entity)2495 START_TEST(test_reset_in_entity) {
2496 if (g_chunkSize != 0) {
2497 // this test does not use SINGLE_BYTES, because of suspension
2498 return;
2499 }
2500
2501 const char *text = "<!DOCTYPE doc [\n"
2502 "<!ENTITY wombat 'wom'>\n"
2503 "<!ENTITY entity 'hi &wom; there'>\n"
2504 "]>\n"
2505 "<doc>&entity;</doc>";
2506 XML_ParsingStatus status;
2507
2508 g_resumable = XML_TRUE;
2509 XML_SetCharacterDataHandler(g_parser, clearing_aborting_character_handler);
2510 // can't use SINGLE_BYTES here, because it'll return early on suspension, and
2511 // we won't know exactly how much input we actually managed to give Expat.
2512 if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE)
2513 == XML_STATUS_ERROR)
2514 xml_failure(g_parser);
2515 XML_GetParsingStatus(g_parser, &status);
2516 if (status.parsing != XML_SUSPENDED)
2517 fail("Parsing status not SUSPENDED");
2518 XML_ParserReset(g_parser, NULL);
2519 XML_GetParsingStatus(g_parser, &status);
2520 if (status.parsing != XML_INITIALIZED)
2521 fail("Parsing status doesn't reset to INITIALIZED");
2522 }
2523 END_TEST
2524
2525 /* Test that resume correctly passes through parse errors */
START_TEST(test_resume_invalid_parse)2526 START_TEST(test_resume_invalid_parse) {
2527 const char *text = "<doc>Hello</doc"; /* Missing closing wedge */
2528
2529 g_resumable = XML_TRUE;
2530 XML_SetCharacterDataHandler(g_parser, clearing_aborting_character_handler);
2531 if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE)
2532 == XML_STATUS_ERROR)
2533 xml_failure(g_parser);
2534 if (XML_ResumeParser(g_parser) == XML_STATUS_OK)
2535 fail("Resumed invalid parse not faulted");
2536 if (XML_GetErrorCode(g_parser) != XML_ERROR_UNCLOSED_TOKEN)
2537 fail("Invalid parse not correctly faulted");
2538 }
2539 END_TEST
2540
2541 /* Test that re-suspended parses are correctly passed through */
START_TEST(test_resume_resuspended)2542 START_TEST(test_resume_resuspended) {
2543 const char *text = "<doc>Hello<meep/>world</doc>";
2544
2545 g_resumable = XML_TRUE;
2546 XML_SetCharacterDataHandler(g_parser, clearing_aborting_character_handler);
2547 if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE)
2548 == XML_STATUS_ERROR)
2549 xml_failure(g_parser);
2550 g_resumable = XML_TRUE;
2551 XML_SetCharacterDataHandler(g_parser, clearing_aborting_character_handler);
2552 if (XML_ResumeParser(g_parser) != XML_STATUS_SUSPENDED)
2553 fail("Resumption not suspended");
2554 /* This one should succeed and finish up */
2555 if (XML_ResumeParser(g_parser) != XML_STATUS_OK)
2556 xml_failure(g_parser);
2557 }
2558 END_TEST
2559
2560 /* Test that CDATA shows up correctly through a default handler */
START_TEST(test_cdata_default)2561 START_TEST(test_cdata_default) {
2562 const char *text = "<doc><![CDATA[Hello\nworld]]></doc>";
2563 const XML_Char *expected = XCS("<doc><![CDATA[Hello\nworld]]></doc>");
2564 CharData storage;
2565
2566 CharData_Init(&storage);
2567 XML_SetUserData(g_parser, &storage);
2568 XML_SetDefaultHandler(g_parser, accumulate_characters);
2569
2570 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2571 == XML_STATUS_ERROR)
2572 xml_failure(g_parser);
2573 CharData_CheckXMLChars(&storage, expected);
2574 }
2575 END_TEST
2576
2577 /* Test resetting a subordinate parser does exactly nothing */
START_TEST(test_subordinate_reset)2578 START_TEST(test_subordinate_reset) {
2579 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
2580 "<!DOCTYPE doc SYSTEM 'foo'>\n"
2581 "<doc>&entity;</doc>";
2582
2583 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2584 XML_SetExternalEntityRefHandler(g_parser, external_entity_resetter);
2585 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2586 == XML_STATUS_ERROR)
2587 xml_failure(g_parser);
2588 }
2589 END_TEST
2590
2591 /* Test suspending a subordinate parser */
START_TEST(test_subordinate_suspend)2592 START_TEST(test_subordinate_suspend) {
2593 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
2594 "<!DOCTYPE doc SYSTEM 'foo'>\n"
2595 "<doc>&entity;</doc>";
2596
2597 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2598 XML_SetExternalEntityRefHandler(g_parser, external_entity_suspender);
2599 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2600 == XML_STATUS_ERROR)
2601 xml_failure(g_parser);
2602 }
2603 END_TEST
2604
2605 /* Test suspending a subordinate parser from an XML declaration */
2606 /* Increases code coverage of the tests */
2607
START_TEST(test_subordinate_xdecl_suspend)2608 START_TEST(test_subordinate_xdecl_suspend) {
2609 const char *text
2610 = "<!DOCTYPE doc [\n"
2611 " <!ENTITY entity SYSTEM 'http://example.org/dummy.ent'>\n"
2612 "]>\n"
2613 "<doc>&entity;</doc>";
2614
2615 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2616 XML_SetExternalEntityRefHandler(g_parser, external_entity_suspend_xmldecl);
2617 g_resumable = XML_TRUE;
2618 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2619 == XML_STATUS_ERROR)
2620 xml_failure(g_parser);
2621 }
2622 END_TEST
2623
START_TEST(test_subordinate_xdecl_abort)2624 START_TEST(test_subordinate_xdecl_abort) {
2625 const char *text
2626 = "<!DOCTYPE doc [\n"
2627 " <!ENTITY entity SYSTEM 'http://example.org/dummy.ent'>\n"
2628 "]>\n"
2629 "<doc>&entity;</doc>";
2630
2631 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2632 XML_SetExternalEntityRefHandler(g_parser, external_entity_suspend_xmldecl);
2633 g_resumable = XML_FALSE;
2634 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2635 == XML_STATUS_ERROR)
2636 xml_failure(g_parser);
2637 }
2638 END_TEST
2639
2640 /* Test external entity fault handling with suspension */
START_TEST(test_ext_entity_invalid_suspended_parse)2641 START_TEST(test_ext_entity_invalid_suspended_parse) {
2642 const char *text = "<!DOCTYPE doc [\n"
2643 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
2644 "]>\n"
2645 "<doc>&en;</doc>";
2646 ExtFaults faults[]
2647 = {{"<?xml version='1.0' encoding='us-ascii'?><",
2648 "Incomplete element declaration not faulted", NULL,
2649 XML_ERROR_UNCLOSED_TOKEN},
2650 {/* First two bytes of a three-byte char */
2651 "<?xml version='1.0' encoding='utf-8'?>\xe2\x82",
2652 "Incomplete character not faulted", NULL, XML_ERROR_PARTIAL_CHAR},
2653 {NULL, NULL, NULL, XML_ERROR_NONE}};
2654 ExtFaults *fault;
2655
2656 for (fault = &faults[0]; fault->parse_text != NULL; fault++) {
2657 set_subtest("%s", fault->parse_text);
2658 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2659 XML_SetExternalEntityRefHandler(g_parser,
2660 external_entity_suspending_faulter);
2661 XML_SetUserData(g_parser, fault);
2662 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
2663 "Parser did not report external entity error");
2664 XML_ParserReset(g_parser, NULL);
2665 }
2666 }
2667 END_TEST
2668
2669 /* Test setting an explicit encoding */
START_TEST(test_explicit_encoding)2670 START_TEST(test_explicit_encoding) {
2671 const char *text1 = "<doc>Hello ";
2672 const char *text2 = " World</doc>";
2673
2674 /* Just check that we can set the encoding to NULL before starting */
2675 if (XML_SetEncoding(g_parser, NULL) != XML_STATUS_OK)
2676 fail("Failed to initialise encoding to NULL");
2677 /* Say we are UTF-8 */
2678 if (XML_SetEncoding(g_parser, XCS("utf-8")) != XML_STATUS_OK)
2679 fail("Failed to set explicit encoding");
2680 if (_XML_Parse_SINGLE_BYTES(g_parser, text1, (int)strlen(text1), XML_FALSE)
2681 == XML_STATUS_ERROR)
2682 xml_failure(g_parser);
2683 /* Try to switch encodings mid-parse */
2684 if (XML_SetEncoding(g_parser, XCS("us-ascii")) != XML_STATUS_ERROR)
2685 fail("Allowed encoding change");
2686 if (_XML_Parse_SINGLE_BYTES(g_parser, text2, (int)strlen(text2), XML_TRUE)
2687 == XML_STATUS_ERROR)
2688 xml_failure(g_parser);
2689 /* Try now the parse is over */
2690 if (XML_SetEncoding(g_parser, NULL) != XML_STATUS_OK)
2691 fail("Failed to unset encoding");
2692 }
2693 END_TEST
2694
2695 /* Test handling of trailing CR (rather than newline) */
START_TEST(test_trailing_cr)2696 START_TEST(test_trailing_cr) {
2697 const char *text = "<doc>\r";
2698 int found_cr;
2699
2700 /* Try with a character handler, for code coverage */
2701 XML_SetCharacterDataHandler(g_parser, cr_cdata_handler);
2702 XML_SetUserData(g_parser, &found_cr);
2703 found_cr = 0;
2704 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2705 == XML_STATUS_OK)
2706 fail("Failed to fault unclosed doc");
2707 if (found_cr == 0)
2708 fail("Did not catch the carriage return");
2709 XML_ParserReset(g_parser, NULL);
2710
2711 /* Now with a default handler instead */
2712 XML_SetDefaultHandler(g_parser, cr_cdata_handler);
2713 XML_SetUserData(g_parser, &found_cr);
2714 found_cr = 0;
2715 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2716 == XML_STATUS_OK)
2717 fail("Failed to fault unclosed doc");
2718 if (found_cr == 0)
2719 fail("Did not catch default carriage return");
2720 }
2721 END_TEST
2722
2723 /* Test trailing CR in an external entity parse */
START_TEST(test_ext_entity_trailing_cr)2724 START_TEST(test_ext_entity_trailing_cr) {
2725 const char *text = "<!DOCTYPE doc [\n"
2726 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
2727 "]>\n"
2728 "<doc>&en;</doc>";
2729 int found_cr;
2730
2731 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2732 XML_SetExternalEntityRefHandler(g_parser, external_entity_cr_catcher);
2733 XML_SetUserData(g_parser, &found_cr);
2734 found_cr = 0;
2735 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2736 != XML_STATUS_OK)
2737 xml_failure(g_parser);
2738 if (found_cr == 0)
2739 fail("No carriage return found");
2740 XML_ParserReset(g_parser, NULL);
2741
2742 /* Try again with a different trailing CR */
2743 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2744 XML_SetExternalEntityRefHandler(g_parser, external_entity_bad_cr_catcher);
2745 XML_SetUserData(g_parser, &found_cr);
2746 found_cr = 0;
2747 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2748 != XML_STATUS_OK)
2749 xml_failure(g_parser);
2750 if (found_cr == 0)
2751 fail("No carriage return found");
2752 }
2753 END_TEST
2754
2755 /* Test handling of trailing square bracket */
START_TEST(test_trailing_rsqb)2756 START_TEST(test_trailing_rsqb) {
2757 const char *text8 = "<doc>]";
2758 const char text16[] = "\xFF\xFE<\000d\000o\000c\000>\000]\000";
2759 int found_rsqb;
2760 int text8_len = (int)strlen(text8);
2761
2762 XML_SetCharacterDataHandler(g_parser, rsqb_handler);
2763 XML_SetUserData(g_parser, &found_rsqb);
2764 found_rsqb = 0;
2765 if (_XML_Parse_SINGLE_BYTES(g_parser, text8, text8_len, XML_TRUE)
2766 == XML_STATUS_OK)
2767 fail("Failed to fault unclosed doc");
2768 if (found_rsqb == 0)
2769 fail("Did not catch the right square bracket");
2770
2771 /* Try again with a different encoding */
2772 XML_ParserReset(g_parser, NULL);
2773 XML_SetCharacterDataHandler(g_parser, rsqb_handler);
2774 XML_SetUserData(g_parser, &found_rsqb);
2775 found_rsqb = 0;
2776 if (_XML_Parse_SINGLE_BYTES(g_parser, text16, (int)sizeof(text16) - 1,
2777 XML_TRUE)
2778 == XML_STATUS_OK)
2779 fail("Failed to fault unclosed doc");
2780 if (found_rsqb == 0)
2781 fail("Did not catch the right square bracket");
2782
2783 /* And finally with a default handler */
2784 XML_ParserReset(g_parser, NULL);
2785 XML_SetDefaultHandler(g_parser, rsqb_handler);
2786 XML_SetUserData(g_parser, &found_rsqb);
2787 found_rsqb = 0;
2788 if (_XML_Parse_SINGLE_BYTES(g_parser, text16, (int)sizeof(text16) - 1,
2789 XML_TRUE)
2790 == XML_STATUS_OK)
2791 fail("Failed to fault unclosed doc");
2792 if (found_rsqb == 0)
2793 fail("Did not catch the right square bracket");
2794 }
2795 END_TEST
2796
2797 /* Test trailing right square bracket in an external entity parse */
START_TEST(test_ext_entity_trailing_rsqb)2798 START_TEST(test_ext_entity_trailing_rsqb) {
2799 const char *text = "<!DOCTYPE doc [\n"
2800 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
2801 "]>\n"
2802 "<doc>&en;</doc>";
2803 int found_rsqb;
2804
2805 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2806 XML_SetExternalEntityRefHandler(g_parser, external_entity_rsqb_catcher);
2807 XML_SetUserData(g_parser, &found_rsqb);
2808 found_rsqb = 0;
2809 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2810 != XML_STATUS_OK)
2811 xml_failure(g_parser);
2812 if (found_rsqb == 0)
2813 fail("No right square bracket found");
2814 }
2815 END_TEST
2816
2817 /* Test CDATA handling in an external entity */
START_TEST(test_ext_entity_good_cdata)2818 START_TEST(test_ext_entity_good_cdata) {
2819 const char *text = "<!DOCTYPE doc [\n"
2820 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
2821 "]>\n"
2822 "<doc>&en;</doc>";
2823
2824 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2825 XML_SetExternalEntityRefHandler(g_parser, external_entity_good_cdata_ascii);
2826 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2827 != XML_STATUS_OK)
2828 xml_failure(g_parser);
2829 }
2830 END_TEST
2831
2832 /* Test user parameter settings */
START_TEST(test_user_parameters)2833 START_TEST(test_user_parameters) {
2834 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
2835 "<!-- Primary parse -->\n"
2836 "<!DOCTYPE doc SYSTEM 'foo'>\n"
2837 "<doc>&entity;";
2838 const char *epilog = "<!-- Back to primary parser -->\n"
2839 "</doc>";
2840
2841 g_comment_count = 0;
2842 g_skip_count = 0;
2843 g_xdecl_count = 0;
2844 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2845 XML_SetXmlDeclHandler(g_parser, xml_decl_handler);
2846 XML_SetExternalEntityRefHandler(g_parser, external_entity_param_checker);
2847 XML_SetCommentHandler(g_parser, data_check_comment_handler);
2848 XML_SetSkippedEntityHandler(g_parser, param_check_skip_handler);
2849 XML_UseParserAsHandlerArg(g_parser);
2850 XML_SetUserData(g_parser, (void *)1);
2851 g_handler_data = g_parser;
2852 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_FALSE)
2853 == XML_STATUS_ERROR)
2854 xml_failure(g_parser);
2855 /* Ensure we can't change policy mid-parse */
2856 if (XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_NEVER))
2857 fail("Changed param entity parsing policy while parsing");
2858 if (_XML_Parse_SINGLE_BYTES(g_parser, epilog, (int)strlen(epilog), XML_TRUE)
2859 == XML_STATUS_ERROR)
2860 xml_failure(g_parser);
2861 if (g_comment_count != 3)
2862 fail("Comment handler not invoked enough times");
2863 if (g_skip_count != 1)
2864 fail("Skip handler not invoked enough times");
2865 if (g_xdecl_count != 1)
2866 fail("XML declaration handler not invoked");
2867 }
2868 END_TEST
2869
2870 /* Test that an explicit external entity handler argument replaces
2871 * the parser as the first argument.
2872 *
2873 * We do not call the first parameter to the external entity handler
2874 * 'parser' for once, since the first time the handler is called it
2875 * will actually be a text string. We need to be able to access the
2876 * global 'parser' variable to create our external entity parser from,
2877 * since there are code paths we need to ensure get executed.
2878 */
START_TEST(test_ext_entity_ref_parameter)2879 START_TEST(test_ext_entity_ref_parameter) {
2880 const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
2881 "<!DOCTYPE doc SYSTEM 'foo'>\n"
2882 "<doc>&entity;</doc>";
2883
2884 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2885 XML_SetExternalEntityRefHandler(g_parser, external_entity_ref_param_checker);
2886 /* Set a handler arg that is not NULL and not parser (which is
2887 * what NULL would cause to be passed.
2888 */
2889 XML_SetExternalEntityRefHandlerArg(g_parser, (void *)text);
2890 g_handler_data = text;
2891 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2892 == XML_STATUS_ERROR)
2893 xml_failure(g_parser);
2894
2895 /* Now try again with unset args */
2896 XML_ParserReset(g_parser, NULL);
2897 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2898 XML_SetExternalEntityRefHandler(g_parser, external_entity_ref_param_checker);
2899 XML_SetExternalEntityRefHandlerArg(g_parser, NULL);
2900 g_handler_data = g_parser;
2901 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2902 == XML_STATUS_ERROR)
2903 xml_failure(g_parser);
2904 }
2905 END_TEST
2906
2907 /* Test the parsing of an empty string */
START_TEST(test_empty_parse)2908 START_TEST(test_empty_parse) {
2909 const char *text = "<doc></doc>";
2910 const char *partial = "<doc>";
2911
2912 if (XML_Parse(g_parser, NULL, 0, XML_FALSE) == XML_STATUS_ERROR)
2913 fail("Parsing empty string faulted");
2914 if (XML_Parse(g_parser, NULL, 0, XML_TRUE) != XML_STATUS_ERROR)
2915 fail("Parsing final empty string not faulted");
2916 if (XML_GetErrorCode(g_parser) != XML_ERROR_NO_ELEMENTS)
2917 fail("Parsing final empty string faulted for wrong reason");
2918
2919 /* Now try with valid text before the empty end */
2920 XML_ParserReset(g_parser, NULL);
2921 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_FALSE)
2922 == XML_STATUS_ERROR)
2923 xml_failure(g_parser);
2924 if (XML_Parse(g_parser, NULL, 0, XML_TRUE) == XML_STATUS_ERROR)
2925 fail("Parsing final empty string faulted");
2926
2927 /* Now try with invalid text before the empty end */
2928 XML_ParserReset(g_parser, NULL);
2929 if (_XML_Parse_SINGLE_BYTES(g_parser, partial, (int)strlen(partial),
2930 XML_FALSE)
2931 == XML_STATUS_ERROR)
2932 xml_failure(g_parser);
2933 if (XML_Parse(g_parser, NULL, 0, XML_TRUE) != XML_STATUS_ERROR)
2934 fail("Parsing final incomplete empty string not faulted");
2935 }
2936 END_TEST
2937
2938 /* Test XML_Parse for len < 0 */
START_TEST(test_negative_len_parse)2939 START_TEST(test_negative_len_parse) {
2940 const char *const doc = "<root/>";
2941 for (int isFinal = 0; isFinal < 2; isFinal++) {
2942 set_subtest("isFinal=%d", isFinal);
2943
2944 XML_Parser parser = XML_ParserCreate(NULL);
2945
2946 if (XML_GetErrorCode(parser) != XML_ERROR_NONE)
2947 fail("There was not supposed to be any initial parse error.");
2948
2949 const enum XML_Status status = XML_Parse(parser, doc, -1, isFinal);
2950
2951 if (status != XML_STATUS_ERROR)
2952 fail("Negative len was expected to fail the parse but did not.");
2953
2954 if (XML_GetErrorCode(parser) != XML_ERROR_INVALID_ARGUMENT)
2955 fail("Parse error does not match XML_ERROR_INVALID_ARGUMENT.");
2956
2957 XML_ParserFree(parser);
2958 }
2959 }
2960 END_TEST
2961
2962 /* Test XML_ParseBuffer for len < 0 */
START_TEST(test_negative_len_parse_buffer)2963 START_TEST(test_negative_len_parse_buffer) {
2964 const char *const doc = "<root/>";
2965 for (int isFinal = 0; isFinal < 2; isFinal++) {
2966 set_subtest("isFinal=%d", isFinal);
2967
2968 XML_Parser parser = XML_ParserCreate(NULL);
2969
2970 if (XML_GetErrorCode(parser) != XML_ERROR_NONE)
2971 fail("There was not supposed to be any initial parse error.");
2972
2973 void *const buffer = XML_GetBuffer(parser, (int)strlen(doc));
2974
2975 if (buffer == NULL)
2976 fail("XML_GetBuffer failed.");
2977
2978 memcpy(buffer, doc, strlen(doc));
2979
2980 const enum XML_Status status = XML_ParseBuffer(parser, -1, isFinal);
2981
2982 if (status != XML_STATUS_ERROR)
2983 fail("Negative len was expected to fail the parse but did not.");
2984
2985 if (XML_GetErrorCode(parser) != XML_ERROR_INVALID_ARGUMENT)
2986 fail("Parse error does not match XML_ERROR_INVALID_ARGUMENT.");
2987
2988 XML_ParserFree(parser);
2989 }
2990 }
2991 END_TEST
2992
2993 /* Test odd corners of the XML_GetBuffer interface */
2994 static enum XML_Status
get_feature(enum XML_FeatureEnum feature_id,long * presult)2995 get_feature(enum XML_FeatureEnum feature_id, long *presult) {
2996 const XML_Feature *feature = XML_GetFeatureList();
2997
2998 if (feature == NULL)
2999 return XML_STATUS_ERROR;
3000 for (; feature->feature != XML_FEATURE_END; feature++) {
3001 if (feature->feature == feature_id) {
3002 *presult = feature->value;
3003 return XML_STATUS_OK;
3004 }
3005 }
3006 return XML_STATUS_ERROR;
3007 }
3008
3009 /* Test odd corners of the XML_GetBuffer interface */
START_TEST(test_get_buffer_1)3010 START_TEST(test_get_buffer_1) {
3011 const char *text = get_buffer_test_text;
3012 void *buffer;
3013 long context_bytes;
3014
3015 /* Attempt to allocate a negative length buffer */
3016 if (XML_GetBuffer(g_parser, -12) != NULL)
3017 fail("Negative length buffer not failed");
3018
3019 /* Now get a small buffer and extend it past valid length */
3020 buffer = XML_GetBuffer(g_parser, 1536);
3021 if (buffer == NULL)
3022 fail("1.5K buffer failed");
3023 assert(buffer != NULL);
3024 memcpy(buffer, text, strlen(text));
3025 if (XML_ParseBuffer(g_parser, (int)strlen(text), XML_FALSE)
3026 == XML_STATUS_ERROR)
3027 xml_failure(g_parser);
3028 if (XML_GetBuffer(g_parser, INT_MAX) != NULL)
3029 fail("INT_MAX buffer not failed");
3030
3031 /* Now try extending it a more reasonable but still too large
3032 * amount. The allocator in XML_GetBuffer() doubles the buffer
3033 * size until it exceeds the requested amount or INT_MAX. If it
3034 * exceeds INT_MAX, it rejects the request, so we want a request
3035 * between INT_MAX and INT_MAX/2. A gap of 1K seems comfortable,
3036 * with an extra byte just to ensure that the request is off any
3037 * boundary. The request will be inflated internally by
3038 * XML_CONTEXT_BYTES (if >=1), so we subtract that from our
3039 * request.
3040 */
3041 if (get_feature(XML_FEATURE_CONTEXT_BYTES, &context_bytes) != XML_STATUS_OK)
3042 context_bytes = 0;
3043 if (XML_GetBuffer(g_parser, INT_MAX - (context_bytes + 1025)) != NULL)
3044 fail("INT_MAX- buffer not failed");
3045
3046 /* Now try extending it a carefully crafted amount */
3047 if (XML_GetBuffer(g_parser, 1000) == NULL)
3048 fail("1000 buffer failed");
3049 }
3050 END_TEST
3051
3052 /* Test more corners of the XML_GetBuffer interface */
START_TEST(test_get_buffer_2)3053 START_TEST(test_get_buffer_2) {
3054 const char *text = get_buffer_test_text;
3055 void *buffer;
3056
3057 /* Now get a decent buffer */
3058 buffer = XML_GetBuffer(g_parser, 1536);
3059 if (buffer == NULL)
3060 fail("1.5K buffer failed");
3061 assert(buffer != NULL);
3062 memcpy(buffer, text, strlen(text));
3063 if (XML_ParseBuffer(g_parser, (int)strlen(text), XML_FALSE)
3064 == XML_STATUS_ERROR)
3065 xml_failure(g_parser);
3066
3067 /* Extend it, to catch a different code path */
3068 if (XML_GetBuffer(g_parser, 1024) == NULL)
3069 fail("1024 buffer failed");
3070 }
3071 END_TEST
3072
3073 /* Test for signed integer overflow CVE-2022-23852 */
3074 #if XML_CONTEXT_BYTES > 0
START_TEST(test_get_buffer_3_overflow)3075 START_TEST(test_get_buffer_3_overflow) {
3076 XML_Parser parser = XML_ParserCreate(NULL);
3077 assert(parser != NULL);
3078
3079 const char *const text = "\n";
3080 const int expectedKeepValue = (int)strlen(text);
3081
3082 // After this call, variable "keep" in XML_GetBuffer will
3083 // have value expectedKeepValue
3084 if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text),
3085 XML_FALSE /* isFinal */)
3086 == XML_STATUS_ERROR)
3087 xml_failure(parser);
3088
3089 assert(expectedKeepValue > 0);
3090 if (XML_GetBuffer(parser, INT_MAX - expectedKeepValue + 1) != NULL)
3091 fail("enlarging buffer not failed");
3092
3093 XML_ParserFree(parser);
3094 }
3095 END_TEST
3096 #endif // XML_CONTEXT_BYTES > 0
3097
START_TEST(test_buffer_can_grow_to_max)3098 START_TEST(test_buffer_can_grow_to_max) {
3099 const char *const prefixes[] = {
3100 "",
3101 "<",
3102 "<x a='",
3103 "<doc><x a='",
3104 "<document><x a='",
3105 "<averylongelementnamesuchthatitwillhopefullystretchacrossmultiplelinesand"
3106 "lookprettyridiculousitsalsoveryhardtoreadandifyouredoingitihavetowonderif"
3107 "youreallydonthaveanythingbettertodoofcourseiguessicouldveputsomethingbadin"
3108 "herebutipromisethatididntheybtwhowgreatarespacesandpunctuationforhelping"
3109 "withreadabilityprettygreatithinkanywaysthisisprobablylongenoughbye><x a='"};
3110 const int num_prefixes = sizeof(prefixes) / sizeof(prefixes[0]);
3111 int maxbuf = INT_MAX / 2 + (INT_MAX & 1); // round up without overflow
3112 #if defined(__MINGW32__) && ! defined(__MINGW64__)
3113 // workaround for mingw/wine32 on GitHub CI not being able to reach 1GiB
3114 // Can we make a big allocation?
3115 void *big = malloc(maxbuf);
3116 if (! big) {
3117 // The big allocation failed. Let's be a little lenient.
3118 maxbuf = maxbuf / 2;
3119 }
3120 free(big);
3121 #endif
3122
3123 for (int i = 0; i < num_prefixes; ++i) {
3124 set_subtest("\"%s\"", prefixes[i]);
3125 XML_Parser parser = XML_ParserCreate(NULL);
3126 const int prefix_len = (int)strlen(prefixes[i]);
3127 const enum XML_Status s
3128 = _XML_Parse_SINGLE_BYTES(parser, prefixes[i], prefix_len, XML_FALSE);
3129 if (s != XML_STATUS_OK)
3130 xml_failure(parser);
3131
3132 // XML_CONTEXT_BYTES of the prefix may remain in the buffer;
3133 // subtracting the whole prefix is easiest, and close enough.
3134 assert_true(XML_GetBuffer(parser, maxbuf - prefix_len) != NULL);
3135 // The limit should be consistent; no prefix should allow us to
3136 // reach above the max buffer size.
3137 assert_true(XML_GetBuffer(parser, maxbuf + 1) == NULL);
3138 XML_ParserFree(parser);
3139 }
3140 }
3141 END_TEST
3142
START_TEST(test_getbuffer_allocates_on_zero_len)3143 START_TEST(test_getbuffer_allocates_on_zero_len) {
3144 for (int first_len = 1; first_len >= 0; first_len--) {
3145 set_subtest("with len=%d first", first_len);
3146 XML_Parser parser = XML_ParserCreate(NULL);
3147 assert_true(parser != NULL);
3148 assert_true(XML_GetBuffer(parser, first_len) != NULL);
3149 assert_true(XML_GetBuffer(parser, 0) != NULL);
3150 if (XML_ParseBuffer(parser, 0, XML_FALSE) != XML_STATUS_OK)
3151 xml_failure(parser);
3152 XML_ParserFree(parser);
3153 }
3154 }
3155 END_TEST
3156
3157 /* Test position information macros */
START_TEST(test_byte_info_at_end)3158 START_TEST(test_byte_info_at_end) {
3159 const char *text = "<doc></doc>";
3160
3161 if (XML_GetCurrentByteIndex(g_parser) != -1
3162 || XML_GetCurrentByteCount(g_parser) != 0)
3163 fail("Byte index/count incorrect at start of parse");
3164 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3165 == XML_STATUS_ERROR)
3166 xml_failure(g_parser);
3167 /* At end, the count will be zero and the index the end of string */
3168 if (XML_GetCurrentByteCount(g_parser) != 0)
3169 fail("Terminal byte count incorrect");
3170 if (XML_GetCurrentByteIndex(g_parser) != (XML_Index)strlen(text))
3171 fail("Terminal byte index incorrect");
3172 }
3173 END_TEST
3174
3175 /* Test position information from errors */
3176 #define PRE_ERROR_STR "<doc></"
3177 #define POST_ERROR_STR "wombat></doc>"
START_TEST(test_byte_info_at_error)3178 START_TEST(test_byte_info_at_error) {
3179 const char *text = PRE_ERROR_STR POST_ERROR_STR;
3180
3181 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3182 == XML_STATUS_OK)
3183 fail("Syntax error not faulted");
3184 if (XML_GetCurrentByteCount(g_parser) != 0)
3185 fail("Error byte count incorrect");
3186 if (XML_GetCurrentByteIndex(g_parser) != strlen(PRE_ERROR_STR))
3187 fail("Error byte index incorrect");
3188 }
3189 END_TEST
3190 #undef PRE_ERROR_STR
3191 #undef POST_ERROR_STR
3192
3193 /* Test position information in handler */
3194 #define START_ELEMENT "<e>"
3195 #define CDATA_TEXT "Hello"
3196 #define END_ELEMENT "</e>"
START_TEST(test_byte_info_at_cdata)3197 START_TEST(test_byte_info_at_cdata) {
3198 const char *text = START_ELEMENT CDATA_TEXT END_ELEMENT;
3199 int offset, size;
3200 ByteTestData data;
3201
3202 /* Check initial context is empty */
3203 if (XML_GetInputContext(g_parser, &offset, &size) != NULL)
3204 fail("Unexpected context at start of parse");
3205
3206 data.start_element_len = (int)strlen(START_ELEMENT);
3207 data.cdata_len = (int)strlen(CDATA_TEXT);
3208 data.total_string_len = (int)strlen(text);
3209 XML_SetCharacterDataHandler(g_parser, byte_character_handler);
3210 XML_SetUserData(g_parser, &data);
3211 if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE) != XML_STATUS_OK)
3212 xml_failure(g_parser);
3213 }
3214 END_TEST
3215 #undef START_ELEMENT
3216 #undef CDATA_TEXT
3217 #undef END_ELEMENT
3218
3219 /* Test predefined entities are correctly recognised */
START_TEST(test_predefined_entities)3220 START_TEST(test_predefined_entities) {
3221 const char *text = "<doc><>&"'</doc>";
3222 const XML_Char *expected = XCS("<doc><>&"'</doc>");
3223 const XML_Char *result = XCS("<>&\"'");
3224 CharData storage;
3225
3226 XML_SetDefaultHandler(g_parser, accumulate_characters);
3227 /* run_character_check uses XML_SetCharacterDataHandler(), which
3228 * unfortunately heads off a code path that we need to exercise.
3229 */
3230 CharData_Init(&storage);
3231 XML_SetUserData(g_parser, &storage);
3232 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3233 == XML_STATUS_ERROR)
3234 xml_failure(g_parser);
3235 /* The default handler doesn't translate the entities */
3236 CharData_CheckXMLChars(&storage, expected);
3237
3238 /* Now try again and check the translation */
3239 XML_ParserReset(g_parser, NULL);
3240 run_character_check(text, result);
3241 }
3242 END_TEST
3243
3244 /* Regression test that an invalid tag in an external parameter
3245 * reference in an external DTD is correctly faulted.
3246 *
3247 * Only a few specific tags are legal in DTDs ignoring comments and
3248 * processing instructions, all of which begin with an exclamation
3249 * mark. "<el/>" is not one of them, so the parser should raise an
3250 * error on encountering it.
3251 */
START_TEST(test_invalid_tag_in_dtd)3252 START_TEST(test_invalid_tag_in_dtd) {
3253 const char *text = "<!DOCTYPE doc SYSTEM '004-1.ent'>\n"
3254 "<doc></doc>\n";
3255
3256 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3257 XML_SetExternalEntityRefHandler(g_parser, external_entity_param);
3258 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
3259 "Invalid tag IN DTD external param not rejected");
3260 }
3261 END_TEST
3262
3263 /* Test entities not quite the predefined ones are not mis-recognised */
START_TEST(test_not_predefined_entities)3264 START_TEST(test_not_predefined_entities) {
3265 const char *text[] = {"<doc>&pt;</doc>", "<doc>&amo;</doc>",
3266 "<doc>&quid;</doc>", "<doc>&apod;</doc>", NULL};
3267 int i = 0;
3268
3269 while (text[i] != NULL) {
3270 expect_failure(text[i], XML_ERROR_UNDEFINED_ENTITY,
3271 "Undefined entity not rejected");
3272 XML_ParserReset(g_parser, NULL);
3273 i++;
3274 }
3275 }
3276 END_TEST
3277
3278 /* Test conditional inclusion (IGNORE) */
START_TEST(test_ignore_section)3279 START_TEST(test_ignore_section) {
3280 const char *text = "<!DOCTYPE doc SYSTEM 'foo'>\n"
3281 "<doc><e>&entity;</e></doc>";
3282 const XML_Char *expected
3283 = XCS("<![IGNORE[<!ELEMENT e (#PCDATA)*>]]>\n&entity;");
3284 CharData storage;
3285
3286 CharData_Init(&storage);
3287 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3288 XML_SetUserData(g_parser, &storage);
3289 XML_SetExternalEntityRefHandler(g_parser, external_entity_load_ignore);
3290 XML_SetDefaultHandler(g_parser, accumulate_characters);
3291 XML_SetStartDoctypeDeclHandler(g_parser, dummy_start_doctype_handler);
3292 XML_SetEndDoctypeDeclHandler(g_parser, dummy_end_doctype_handler);
3293 XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
3294 XML_SetStartElementHandler(g_parser, dummy_start_element);
3295 XML_SetEndElementHandler(g_parser, dummy_end_element);
3296 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3297 == XML_STATUS_ERROR)
3298 xml_failure(g_parser);
3299 CharData_CheckXMLChars(&storage, expected);
3300 }
3301 END_TEST
3302
START_TEST(test_ignore_section_utf16)3303 START_TEST(test_ignore_section_utf16) {
3304 const char text[] =
3305 /* <!DOCTYPE d SYSTEM 's'> */
3306 "<\0!\0D\0O\0C\0T\0Y\0P\0E\0 \0d\0 "
3307 "\0S\0Y\0S\0T\0E\0M\0 \0'\0s\0'\0>\0\n\0"
3308 /* <d><e>&en;</e></d> */
3309 "<\0d\0>\0<\0e\0>\0&\0e\0n\0;\0<\0/\0e\0>\0<\0/\0d\0>\0";
3310 const XML_Char *expected = XCS("<![IGNORE[<!ELEMENT e (#PCDATA)*>]]>\n&en;");
3311 CharData storage;
3312
3313 CharData_Init(&storage);
3314 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3315 XML_SetUserData(g_parser, &storage);
3316 XML_SetExternalEntityRefHandler(g_parser, external_entity_load_ignore_utf16);
3317 XML_SetDefaultHandler(g_parser, accumulate_characters);
3318 XML_SetStartDoctypeDeclHandler(g_parser, dummy_start_doctype_handler);
3319 XML_SetEndDoctypeDeclHandler(g_parser, dummy_end_doctype_handler);
3320 XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
3321 XML_SetStartElementHandler(g_parser, dummy_start_element);
3322 XML_SetEndElementHandler(g_parser, dummy_end_element);
3323 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
3324 == XML_STATUS_ERROR)
3325 xml_failure(g_parser);
3326 CharData_CheckXMLChars(&storage, expected);
3327 }
3328 END_TEST
3329
START_TEST(test_ignore_section_utf16_be)3330 START_TEST(test_ignore_section_utf16_be) {
3331 const char text[] =
3332 /* <!DOCTYPE d SYSTEM 's'> */
3333 "\0<\0!\0D\0O\0C\0T\0Y\0P\0E\0 \0d\0 "
3334 "\0S\0Y\0S\0T\0E\0M\0 \0'\0s\0'\0>\0\n"
3335 /* <d><e>&en;</e></d> */
3336 "\0<\0d\0>\0<\0e\0>\0&\0e\0n\0;\0<\0/\0e\0>\0<\0/\0d\0>";
3337 const XML_Char *expected = XCS("<![IGNORE[<!ELEMENT e (#PCDATA)*>]]>\n&en;");
3338 CharData storage;
3339
3340 CharData_Init(&storage);
3341 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3342 XML_SetUserData(g_parser, &storage);
3343 XML_SetExternalEntityRefHandler(g_parser,
3344 external_entity_load_ignore_utf16_be);
3345 XML_SetDefaultHandler(g_parser, accumulate_characters);
3346 XML_SetStartDoctypeDeclHandler(g_parser, dummy_start_doctype_handler);
3347 XML_SetEndDoctypeDeclHandler(g_parser, dummy_end_doctype_handler);
3348 XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
3349 XML_SetStartElementHandler(g_parser, dummy_start_element);
3350 XML_SetEndElementHandler(g_parser, dummy_end_element);
3351 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
3352 == XML_STATUS_ERROR)
3353 xml_failure(g_parser);
3354 CharData_CheckXMLChars(&storage, expected);
3355 }
3356 END_TEST
3357
3358 /* Test mis-formatted conditional exclusion */
START_TEST(test_bad_ignore_section)3359 START_TEST(test_bad_ignore_section) {
3360 const char *text = "<!DOCTYPE doc SYSTEM 'foo'>\n"
3361 "<doc><e>&entity;</e></doc>";
3362 ExtFaults faults[]
3363 = {{"<![IGNORE[<!ELEM", "Broken-off declaration not faulted", NULL,
3364 XML_ERROR_SYNTAX},
3365 {"<![IGNORE[\x01]]>", "Invalid XML character not faulted", NULL,
3366 XML_ERROR_INVALID_TOKEN},
3367 {/* FIrst two bytes of a three-byte char */
3368 "<![IGNORE[\xe2\x82", "Partial XML character not faulted", NULL,
3369 XML_ERROR_PARTIAL_CHAR},
3370 {NULL, NULL, NULL, XML_ERROR_NONE}};
3371 ExtFaults *fault;
3372
3373 for (fault = &faults[0]; fault->parse_text != NULL; fault++) {
3374 set_subtest("%s", fault->parse_text);
3375 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3376 XML_SetExternalEntityRefHandler(g_parser, external_entity_faulter);
3377 XML_SetUserData(g_parser, fault);
3378 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
3379 "Incomplete IGNORE section not failed");
3380 XML_ParserReset(g_parser, NULL);
3381 }
3382 }
3383 END_TEST
3384
3385 struct bom_testdata {
3386 const char *external;
3387 int split;
3388 XML_Bool nested_callback_happened;
3389 };
3390
3391 static int XMLCALL
external_bom_checker(XML_Parser parser,const XML_Char * context,const XML_Char * base,const XML_Char * systemId,const XML_Char * publicId)3392 external_bom_checker(XML_Parser parser, const XML_Char *context,
3393 const XML_Char *base, const XML_Char *systemId,
3394 const XML_Char *publicId) {
3395 const char *text;
3396 UNUSED_P(base);
3397 UNUSED_P(systemId);
3398 UNUSED_P(publicId);
3399
3400 XML_Parser ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
3401 if (ext_parser == NULL)
3402 fail("Could not create external entity parser");
3403
3404 if (! xcstrcmp(systemId, XCS("004-2.ent"))) {
3405 struct bom_testdata *const testdata
3406 = (struct bom_testdata *)XML_GetUserData(parser);
3407 const char *const external = testdata->external;
3408 const int split = testdata->split;
3409 testdata->nested_callback_happened = XML_TRUE;
3410
3411 if (_XML_Parse_SINGLE_BYTES(ext_parser, external, split, XML_FALSE)
3412 != XML_STATUS_OK) {
3413 xml_failure(ext_parser);
3414 }
3415 text = external + split; // the parse below will continue where we left off.
3416 } else if (! xcstrcmp(systemId, XCS("004-1.ent"))) {
3417 text = "<!ELEMENT doc EMPTY>\n"
3418 "<!ENTITY % e1 SYSTEM '004-2.ent'>\n"
3419 "<!ENTITY % e2 '%e1;'>\n";
3420 } else {
3421 fail("unknown systemId");
3422 }
3423
3424 if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
3425 != XML_STATUS_OK)
3426 xml_failure(ext_parser);
3427
3428 XML_ParserFree(ext_parser);
3429 return XML_STATUS_OK;
3430 }
3431
3432 /* regression test: BOM should be consumed when followed by a partial token. */
START_TEST(test_external_bom_consumed)3433 START_TEST(test_external_bom_consumed) {
3434 const char *const text = "<!DOCTYPE doc SYSTEM '004-1.ent'>\n"
3435 "<doc></doc>\n";
3436 const char *const external = "\xEF\xBB\xBF<!ATTLIST doc a1 CDATA 'value'>";
3437 const int len = (int)strlen(external);
3438 for (int split = 0; split <= len; ++split) {
3439 set_subtest("split at byte %d", split);
3440
3441 struct bom_testdata testdata;
3442 testdata.external = external;
3443 testdata.split = split;
3444 testdata.nested_callback_happened = XML_FALSE;
3445
3446 XML_Parser parser = XML_ParserCreate(NULL);
3447 if (parser == NULL) {
3448 fail("Couldn't create parser");
3449 }
3450 XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3451 XML_SetExternalEntityRefHandler(parser, external_bom_checker);
3452 XML_SetUserData(parser, &testdata);
3453 if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
3454 == XML_STATUS_ERROR)
3455 xml_failure(parser);
3456 if (! testdata.nested_callback_happened) {
3457 fail("ref handler not called");
3458 }
3459 XML_ParserFree(parser);
3460 }
3461 }
3462 END_TEST
3463
3464 /* Test recursive parsing */
START_TEST(test_external_entity_values)3465 START_TEST(test_external_entity_values) {
3466 const char *text = "<!DOCTYPE doc SYSTEM '004-1.ent'>\n"
3467 "<doc></doc>\n";
3468 ExtFaults data_004_2[] = {
3469 {"<!ATTLIST doc a1 CDATA 'value'>", NULL, NULL, XML_ERROR_NONE},
3470 {"<!ATTLIST $doc a1 CDATA 'value'>", "Invalid token not faulted", NULL,
3471 XML_ERROR_INVALID_TOKEN},
3472 {"'wombat", "Unterminated string not faulted", NULL,
3473 XML_ERROR_UNCLOSED_TOKEN},
3474 {"\xe2\x82", "Partial UTF-8 character not faulted", NULL,
3475 XML_ERROR_PARTIAL_CHAR},
3476 {"<?xml version='1.0' encoding='utf-8'?>\n", NULL, NULL, XML_ERROR_NONE},
3477 {"<?xml?>", "Malformed XML declaration not faulted", NULL,
3478 XML_ERROR_XML_DECL},
3479 {/* UTF-8 BOM */
3480 "\xEF\xBB\xBF<!ATTLIST doc a1 CDATA 'value'>", NULL, NULL,
3481 XML_ERROR_NONE},
3482 {"<?xml version='1.0' encoding='utf-8'?>\n$",
3483 "Invalid token after text declaration not faulted", NULL,
3484 XML_ERROR_INVALID_TOKEN},
3485 {"<?xml version='1.0' encoding='utf-8'?>\n'wombat",
3486 "Unterminated string after text decl not faulted", NULL,
3487 XML_ERROR_UNCLOSED_TOKEN},
3488 {"<?xml version='1.0' encoding='utf-8'?>\n\xe2\x82",
3489 "Partial UTF-8 character after text decl not faulted", NULL,
3490 XML_ERROR_PARTIAL_CHAR},
3491 {"%e1;", "Recursive parameter entity not faulted", NULL,
3492 XML_ERROR_RECURSIVE_ENTITY_REF},
3493 {NULL, NULL, NULL, XML_ERROR_NONE}};
3494 int i;
3495
3496 for (i = 0; data_004_2[i].parse_text != NULL; i++) {
3497 set_subtest("%s", data_004_2[i].parse_text);
3498 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3499 XML_SetExternalEntityRefHandler(g_parser, external_entity_valuer);
3500 XML_SetUserData(g_parser, &data_004_2[i]);
3501 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3502 == XML_STATUS_ERROR)
3503 xml_failure(g_parser);
3504 XML_ParserReset(g_parser, NULL);
3505 }
3506 }
3507 END_TEST
3508
3509 /* Test the recursive parse interacts with a not standalone handler */
START_TEST(test_ext_entity_not_standalone)3510 START_TEST(test_ext_entity_not_standalone) {
3511 const char *text = "<!DOCTYPE doc SYSTEM 'foo'>\n"
3512 "<doc></doc>";
3513
3514 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3515 XML_SetExternalEntityRefHandler(g_parser, external_entity_not_standalone);
3516 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
3517 "Standalone rejection not caught");
3518 }
3519 END_TEST
3520
START_TEST(test_ext_entity_value_abort)3521 START_TEST(test_ext_entity_value_abort) {
3522 const char *text = "<!DOCTYPE doc SYSTEM '004-1.ent'>\n"
3523 "<doc></doc>\n";
3524
3525 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3526 XML_SetExternalEntityRefHandler(g_parser, external_entity_value_aborter);
3527 g_resumable = XML_FALSE;
3528 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3529 == XML_STATUS_ERROR)
3530 xml_failure(g_parser);
3531 }
3532 END_TEST
3533
START_TEST(test_bad_public_doctype)3534 START_TEST(test_bad_public_doctype) {
3535 const char *text = "<?xml version='1.0' encoding='utf-8'?>\n"
3536 "<!DOCTYPE doc PUBLIC '{BadName}' 'test'>\n"
3537 "<doc></doc>";
3538
3539 /* Setting a handler provokes a particular code path */
3540 XML_SetDoctypeDeclHandler(g_parser, dummy_start_doctype_handler,
3541 dummy_end_doctype_handler);
3542 expect_failure(text, XML_ERROR_PUBLICID, "Bad Public ID not failed");
3543 }
3544 END_TEST
3545
3546 /* Test based on ibm/valid/P32/ibm32v04.xml */
START_TEST(test_attribute_enum_value)3547 START_TEST(test_attribute_enum_value) {
3548 const char *text = "<?xml version='1.0' standalone='no'?>\n"
3549 "<!DOCTYPE animal SYSTEM 'test.dtd'>\n"
3550 "<animal>This is a \n <a/> \n\nyellow tiger</animal>";
3551 ExtTest dtd_data
3552 = {"<!ELEMENT animal (#PCDATA|a)*>\n"
3553 "<!ELEMENT a EMPTY>\n"
3554 "<!ATTLIST animal xml:space (default|preserve) 'preserve'>",
3555 NULL, NULL};
3556 const XML_Char *expected = XCS("This is a \n \n\nyellow tiger");
3557
3558 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
3559 XML_SetUserData(g_parser, &dtd_data);
3560 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3561 /* An attribute list handler provokes a different code path */
3562 XML_SetAttlistDeclHandler(g_parser, dummy_attlist_decl_handler);
3563 run_ext_character_check(text, &dtd_data, expected);
3564 }
3565 END_TEST
3566
3567 /* Slightly bizarrely, the library seems to silently ignore entity
3568 * definitions for predefined entities, even when they are wrong. The
3569 * language of the XML 1.0 spec is somewhat unhelpful as to what ought
3570 * to happen, so this is currently treated as acceptable.
3571 */
START_TEST(test_predefined_entity_redefinition)3572 START_TEST(test_predefined_entity_redefinition) {
3573 const char *text = "<!DOCTYPE doc [\n"
3574 "<!ENTITY apos 'foo'>\n"
3575 "]>\n"
3576 "<doc>'</doc>";
3577 run_character_check(text, XCS("'"));
3578 }
3579 END_TEST
3580
3581 /* Test that the parser stops processing the DTD after an unresolved
3582 * parameter entity is encountered.
3583 */
START_TEST(test_dtd_stop_processing)3584 START_TEST(test_dtd_stop_processing) {
3585 const char *text = "<!DOCTYPE doc [\n"
3586 "%foo;\n"
3587 "<!ENTITY bar 'bas'>\n"
3588 "]><doc/>";
3589
3590 XML_SetEntityDeclHandler(g_parser, dummy_entity_decl_handler);
3591 init_dummy_handlers();
3592 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3593 == XML_STATUS_ERROR)
3594 xml_failure(g_parser);
3595 if (get_dummy_handler_flags() != 0)
3596 fail("DTD processing still going after undefined PE");
3597 }
3598 END_TEST
3599
3600 /* Test public notations with no system ID */
START_TEST(test_public_notation_no_sysid)3601 START_TEST(test_public_notation_no_sysid) {
3602 const char *text = "<!DOCTYPE doc [\n"
3603 "<!NOTATION note PUBLIC 'foo'>\n"
3604 "<!ELEMENT doc EMPTY>\n"
3605 "]>\n<doc/>";
3606
3607 init_dummy_handlers();
3608 XML_SetNotationDeclHandler(g_parser, dummy_notation_decl_handler);
3609 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3610 == XML_STATUS_ERROR)
3611 xml_failure(g_parser);
3612 if (get_dummy_handler_flags() != DUMMY_NOTATION_DECL_HANDLER_FLAG)
3613 fail("Notation declaration handler not called");
3614 }
3615 END_TEST
3616
START_TEST(test_nested_groups)3617 START_TEST(test_nested_groups) {
3618 const char *text
3619 = "<!DOCTYPE doc [\n"
3620 "<!ELEMENT doc "
3621 /* Sixteen elements per line */
3622 "(e,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,"
3623 "(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?"
3624 "))))))))))))))))))))))))))))))))>\n"
3625 "<!ELEMENT e EMPTY>"
3626 "]>\n"
3627 "<doc><e/></doc>";
3628 CharData storage;
3629
3630 CharData_Init(&storage);
3631 XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
3632 XML_SetStartElementHandler(g_parser, record_element_start_handler);
3633 XML_SetUserData(g_parser, &storage);
3634 init_dummy_handlers();
3635 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3636 == XML_STATUS_ERROR)
3637 xml_failure(g_parser);
3638 CharData_CheckXMLChars(&storage, XCS("doce"));
3639 if (get_dummy_handler_flags() != DUMMY_ELEMENT_DECL_HANDLER_FLAG)
3640 fail("Element handler not fired");
3641 }
3642 END_TEST
3643
START_TEST(test_group_choice)3644 START_TEST(test_group_choice) {
3645 const char *text = "<!DOCTYPE doc [\n"
3646 "<!ELEMENT doc (a|b|c)+>\n"
3647 "<!ELEMENT a EMPTY>\n"
3648 "<!ELEMENT b (#PCDATA)>\n"
3649 "<!ELEMENT c ANY>\n"
3650 "]>\n"
3651 "<doc>\n"
3652 "<a/>\n"
3653 "<b attr='foo'>This is a foo</b>\n"
3654 "<c></c>\n"
3655 "</doc>\n";
3656
3657 XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
3658 init_dummy_handlers();
3659 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3660 == XML_STATUS_ERROR)
3661 xml_failure(g_parser);
3662 if (get_dummy_handler_flags() != DUMMY_ELEMENT_DECL_HANDLER_FLAG)
3663 fail("Element handler flag not raised");
3664 }
3665 END_TEST
3666
START_TEST(test_standalone_parameter_entity)3667 START_TEST(test_standalone_parameter_entity) {
3668 const char *text = "<?xml version='1.0' standalone='yes'?>\n"
3669 "<!DOCTYPE doc SYSTEM 'http://example.org/' [\n"
3670 "<!ENTITY % entity '<!ELEMENT doc (#PCDATA)>'>\n"
3671 "%entity;\n"
3672 "]>\n"
3673 "<doc></doc>";
3674 char dtd_data[] = "<!ENTITY % e1 'foo'>\n";
3675
3676 XML_SetUserData(g_parser, dtd_data);
3677 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3678 XML_SetExternalEntityRefHandler(g_parser, external_entity_public);
3679 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3680 == XML_STATUS_ERROR)
3681 xml_failure(g_parser);
3682 }
3683 END_TEST
3684
3685 /* Test skipping of parameter entity in an external DTD */
3686 /* Derived from ibm/invalid/P69/ibm69i01.xml */
START_TEST(test_skipped_parameter_entity)3687 START_TEST(test_skipped_parameter_entity) {
3688 const char *text = "<?xml version='1.0'?>\n"
3689 "<!DOCTYPE root SYSTEM 'http://example.org/dtd.ent' [\n"
3690 "<!ELEMENT root (#PCDATA|a)* >\n"
3691 "]>\n"
3692 "<root></root>";
3693 ExtTest dtd_data = {"%pe2;", NULL, NULL};
3694
3695 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
3696 XML_SetUserData(g_parser, &dtd_data);
3697 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3698 XML_SetSkippedEntityHandler(g_parser, dummy_skip_handler);
3699 init_dummy_handlers();
3700 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3701 == XML_STATUS_ERROR)
3702 xml_failure(g_parser);
3703 if (get_dummy_handler_flags() != DUMMY_SKIP_HANDLER_FLAG)
3704 fail("Skip handler not executed");
3705 }
3706 END_TEST
3707
3708 /* Test recursive parameter entity definition rejected in external DTD */
START_TEST(test_recursive_external_parameter_entity)3709 START_TEST(test_recursive_external_parameter_entity) {
3710 const char *text = "<?xml version='1.0'?>\n"
3711 "<!DOCTYPE root SYSTEM 'http://example.org/dtd.ent' [\n"
3712 "<!ELEMENT root (#PCDATA|a)* >\n"
3713 "]>\n"
3714 "<root></root>";
3715 ExtFaults dtd_data = {"<!ENTITY % pe2 '%pe2;'>\n%pe2;",
3716 "Recursive external parameter entity not faulted", NULL,
3717 XML_ERROR_RECURSIVE_ENTITY_REF};
3718
3719 XML_SetExternalEntityRefHandler(g_parser, external_entity_faulter);
3720 XML_SetUserData(g_parser, &dtd_data);
3721 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3722 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
3723 "Recursive external parameter not spotted");
3724 }
3725 END_TEST
3726
3727 /* Test undefined parameter entity in external entity handler */
START_TEST(test_undefined_ext_entity_in_external_dtd)3728 START_TEST(test_undefined_ext_entity_in_external_dtd) {
3729 const char *text = "<!DOCTYPE doc SYSTEM 'foo'>\n"
3730 "<doc></doc>\n";
3731
3732 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3733 XML_SetExternalEntityRefHandler(g_parser, external_entity_devaluer);
3734 XML_SetUserData(g_parser, NULL);
3735 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3736 == XML_STATUS_ERROR)
3737 xml_failure(g_parser);
3738
3739 /* Now repeat without the external entity ref handler invoking
3740 * another copy of itself.
3741 */
3742 XML_ParserReset(g_parser, NULL);
3743 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3744 XML_SetExternalEntityRefHandler(g_parser, external_entity_devaluer);
3745 XML_SetUserData(g_parser, g_parser); /* Any non-NULL value will do */
3746 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3747 == XML_STATUS_ERROR)
3748 xml_failure(g_parser);
3749 }
3750 END_TEST
3751
3752 /* Test suspending the parse on receiving an XML declaration works */
START_TEST(test_suspend_xdecl)3753 START_TEST(test_suspend_xdecl) {
3754 const char *text = long_character_data_text;
3755
3756 XML_SetXmlDeclHandler(g_parser, entity_suspending_xdecl_handler);
3757 XML_SetUserData(g_parser, g_parser);
3758 g_resumable = XML_TRUE;
3759 // can't use SINGLE_BYTES here, because it'll return early on suspension, and
3760 // we won't know exactly how much input we actually managed to give Expat.
3761 if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE)
3762 != XML_STATUS_SUSPENDED)
3763 xml_failure(g_parser);
3764 if (XML_GetErrorCode(g_parser) != XML_ERROR_NONE)
3765 xml_failure(g_parser);
3766 /* Attempt to start a new parse while suspended */
3767 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3768 != XML_STATUS_ERROR)
3769 fail("Attempt to parse while suspended not faulted");
3770 if (XML_GetErrorCode(g_parser) != XML_ERROR_SUSPENDED)
3771 fail("Suspended parse not faulted with correct error");
3772 }
3773 END_TEST
3774
3775 /* Test aborting the parse in an epilog works */
START_TEST(test_abort_epilog)3776 START_TEST(test_abort_epilog) {
3777 const char *text = "<doc></doc>\n\r\n";
3778 XML_Char trigger_char = XCS('\r');
3779
3780 XML_SetDefaultHandler(g_parser, selective_aborting_default_handler);
3781 XML_SetUserData(g_parser, &trigger_char);
3782 g_resumable = XML_FALSE;
3783 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3784 != XML_STATUS_ERROR)
3785 fail("Abort not triggered");
3786 if (XML_GetErrorCode(g_parser) != XML_ERROR_ABORTED)
3787 xml_failure(g_parser);
3788 }
3789 END_TEST
3790
3791 /* Test a different code path for abort in the epilog */
START_TEST(test_abort_epilog_2)3792 START_TEST(test_abort_epilog_2) {
3793 const char *text = "<doc></doc>\n";
3794 XML_Char trigger_char = XCS('\n');
3795
3796 XML_SetDefaultHandler(g_parser, selective_aborting_default_handler);
3797 XML_SetUserData(g_parser, &trigger_char);
3798 g_resumable = XML_FALSE;
3799 expect_failure(text, XML_ERROR_ABORTED, "Abort not triggered");
3800 }
3801 END_TEST
3802
3803 /* Test suspension from the epilog */
START_TEST(test_suspend_epilog)3804 START_TEST(test_suspend_epilog) {
3805 const char *text = "<doc></doc>\n";
3806 XML_Char trigger_char = XCS('\n');
3807
3808 XML_SetDefaultHandler(g_parser, selective_aborting_default_handler);
3809 XML_SetUserData(g_parser, &trigger_char);
3810 g_resumable = XML_TRUE;
3811 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
3812 != XML_STATUS_SUSPENDED)
3813 xml_failure(g_parser);
3814 }
3815 END_TEST
3816
START_TEST(test_suspend_in_sole_empty_tag)3817 START_TEST(test_suspend_in_sole_empty_tag) {
3818 const char *text = "<doc/>";
3819 enum XML_Status rc;
3820
3821 XML_SetEndElementHandler(g_parser, suspending_end_handler);
3822 XML_SetUserData(g_parser, g_parser);
3823 rc = _XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE);
3824 if (rc == XML_STATUS_ERROR)
3825 xml_failure(g_parser);
3826 else if (rc != XML_STATUS_SUSPENDED)
3827 fail("Suspend not triggered");
3828 rc = XML_ResumeParser(g_parser);
3829 if (rc == XML_STATUS_ERROR)
3830 xml_failure(g_parser);
3831 else if (rc != XML_STATUS_OK)
3832 fail("Resume failed");
3833 }
3834 END_TEST
3835
START_TEST(test_unfinished_epilog)3836 START_TEST(test_unfinished_epilog) {
3837 const char *text = "<doc></doc><";
3838
3839 expect_failure(text, XML_ERROR_UNCLOSED_TOKEN,
3840 "Incomplete epilog entry not faulted");
3841 }
3842 END_TEST
3843
START_TEST(test_partial_char_in_epilog)3844 START_TEST(test_partial_char_in_epilog) {
3845 const char *text = "<doc></doc>\xe2\x82";
3846
3847 /* First check that no fault is raised if the parse is not finished */
3848 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_FALSE)
3849 == XML_STATUS_ERROR)
3850 xml_failure(g_parser);
3851 /* Now check that it is faulted once we finish */
3852 if (XML_ParseBuffer(g_parser, 0, XML_TRUE) != XML_STATUS_ERROR)
3853 fail("Partial character in epilog not faulted");
3854 if (XML_GetErrorCode(g_parser) != XML_ERROR_PARTIAL_CHAR)
3855 xml_failure(g_parser);
3856 }
3857 END_TEST
3858
3859 /* Test resuming a parse suspended in entity substitution */
START_TEST(test_suspend_resume_internal_entity)3860 START_TEST(test_suspend_resume_internal_entity) {
3861 const char *text
3862 = "<!DOCTYPE doc [\n"
3863 "<!ENTITY foo '<suspend>Hi<suspend>Ho</suspend></suspend>'>\n"
3864 "]>\n"
3865 "<doc>&foo;</doc>\n";
3866 const XML_Char *expected1 = XCS("Hi");
3867 const XML_Char *expected2 = XCS("HiHo");
3868 CharData storage;
3869
3870 CharData_Init(&storage);
3871 XML_SetStartElementHandler(g_parser, start_element_suspender);
3872 XML_SetCharacterDataHandler(g_parser, accumulate_characters);
3873 XML_SetUserData(g_parser, &storage);
3874 // can't use SINGLE_BYTES here, because it'll return early on suspension, and
3875 // we won't know exactly how much input we actually managed to give Expat.
3876 if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE)
3877 != XML_STATUS_SUSPENDED)
3878 xml_failure(g_parser);
3879 CharData_CheckXMLChars(&storage, XCS(""));
3880 if (XML_ResumeParser(g_parser) != XML_STATUS_SUSPENDED)
3881 xml_failure(g_parser);
3882 CharData_CheckXMLChars(&storage, expected1);
3883 if (XML_ResumeParser(g_parser) != XML_STATUS_OK)
3884 xml_failure(g_parser);
3885 CharData_CheckXMLChars(&storage, expected2);
3886 }
3887 END_TEST
3888
START_TEST(test_suspend_resume_internal_entity_issue_629)3889 START_TEST(test_suspend_resume_internal_entity_issue_629) {
3890 const char *const text
3891 = "<!DOCTYPE a [<!ENTITY e '<!--COMMENT-->a'>]><a>&e;<b>\n"
3892 "<"
3893 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3894 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3895 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3896 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3897 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3898 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3899 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3900 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3901 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3902 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3903 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3904 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3905 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3906 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3907 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3908 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3909 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3910 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3911 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3912 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3913 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3914 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3915 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3916 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3917 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3918 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3919 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3920 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3921 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3922 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3923 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3924 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3925 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3926 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3927 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3928 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3929 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3930 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3931 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3932 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3933 "/>"
3934 "</b></a>";
3935 const size_t firstChunkSizeBytes = 54;
3936
3937 XML_Parser parser = XML_ParserCreate(NULL);
3938 XML_SetUserData(parser, parser);
3939 XML_SetCommentHandler(parser, suspending_comment_handler);
3940
3941 if (XML_Parse(parser, text, (int)firstChunkSizeBytes, XML_FALSE)
3942 != XML_STATUS_SUSPENDED)
3943 xml_failure(parser);
3944 if (XML_ResumeParser(parser) != XML_STATUS_OK)
3945 xml_failure(parser);
3946 if (_XML_Parse_SINGLE_BYTES(parser, text + firstChunkSizeBytes,
3947 (int)(strlen(text) - firstChunkSizeBytes),
3948 XML_TRUE)
3949 != XML_STATUS_OK)
3950 xml_failure(parser);
3951 XML_ParserFree(parser);
3952 }
3953 END_TEST
3954
3955 /* Test syntax error is caught at parse resumption */
START_TEST(test_resume_entity_with_syntax_error)3956 START_TEST(test_resume_entity_with_syntax_error) {
3957 if (g_chunkSize != 0) {
3958 // this test does not use SINGLE_BYTES, because of suspension
3959 return;
3960 }
3961
3962 const char *text = "<!DOCTYPE doc [\n"
3963 "<!ENTITY foo '<suspend>Hi</wombat>'>\n"
3964 "]>\n"
3965 "<doc>&foo;</doc>\n";
3966
3967 XML_SetStartElementHandler(g_parser, start_element_suspender);
3968 // can't use SINGLE_BYTES here, because it'll return early on suspension, and
3969 // we won't know exactly how much input we actually managed to give Expat.
3970 if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE)
3971 != XML_STATUS_SUSPENDED)
3972 xml_failure(g_parser);
3973 if (XML_ResumeParser(g_parser) != XML_STATUS_ERROR)
3974 fail("Syntax error in entity not faulted");
3975 if (XML_GetErrorCode(g_parser) != XML_ERROR_TAG_MISMATCH)
3976 xml_failure(g_parser);
3977 }
3978 END_TEST
3979
3980 /* Test suspending and resuming in a parameter entity substitution */
START_TEST(test_suspend_resume_parameter_entity)3981 START_TEST(test_suspend_resume_parameter_entity) {
3982 const char *text = "<!DOCTYPE doc [\n"
3983 "<!ENTITY % foo '<!ELEMENT doc (#PCDATA)*>'>\n"
3984 "%foo;\n"
3985 "]>\n"
3986 "<doc>Hello, world</doc>";
3987 const XML_Char *expected = XCS("Hello, world");
3988 CharData storage;
3989
3990 CharData_Init(&storage);
3991 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
3992 XML_SetElementDeclHandler(g_parser, element_decl_suspender);
3993 XML_SetCharacterDataHandler(g_parser, accumulate_characters);
3994 XML_SetUserData(g_parser, &storage);
3995 if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE)
3996 != XML_STATUS_SUSPENDED)
3997 xml_failure(g_parser);
3998 CharData_CheckXMLChars(&storage, XCS(""));
3999 if (XML_ResumeParser(g_parser) != XML_STATUS_OK)
4000 xml_failure(g_parser);
4001 CharData_CheckXMLChars(&storage, expected);
4002 }
4003 END_TEST
4004
4005 /* Test attempting to use parser after an error is faulted */
START_TEST(test_restart_on_error)4006 START_TEST(test_restart_on_error) {
4007 const char *text = "<$doc><doc></doc>";
4008
4009 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4010 != XML_STATUS_ERROR)
4011 fail("Invalid tag name not faulted");
4012 if (XML_GetErrorCode(g_parser) != XML_ERROR_INVALID_TOKEN)
4013 xml_failure(g_parser);
4014 if (XML_Parse(g_parser, NULL, 0, XML_TRUE) != XML_STATUS_ERROR)
4015 fail("Restarting invalid parse not faulted");
4016 if (XML_GetErrorCode(g_parser) != XML_ERROR_INVALID_TOKEN)
4017 xml_failure(g_parser);
4018 }
4019 END_TEST
4020
4021 /* Test that angle brackets in an attribute default value are faulted */
START_TEST(test_reject_lt_in_attribute_value)4022 START_TEST(test_reject_lt_in_attribute_value) {
4023 const char *text = "<!DOCTYPE doc [<!ATTLIST doc a CDATA '<bar>'>]>\n"
4024 "<doc></doc>";
4025
4026 expect_failure(text, XML_ERROR_INVALID_TOKEN,
4027 "Bad attribute default not faulted");
4028 }
4029 END_TEST
4030
START_TEST(test_reject_unfinished_param_in_att_value)4031 START_TEST(test_reject_unfinished_param_in_att_value) {
4032 const char *text = "<!DOCTYPE doc [<!ATTLIST doc a CDATA '&foo'>]>\n"
4033 "<doc></doc>";
4034
4035 expect_failure(text, XML_ERROR_INVALID_TOKEN,
4036 "Bad attribute default not faulted");
4037 }
4038 END_TEST
4039
START_TEST(test_trailing_cr_in_att_value)4040 START_TEST(test_trailing_cr_in_att_value) {
4041 const char *text = "<doc a='value\r'/>";
4042
4043 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4044 == XML_STATUS_ERROR)
4045 xml_failure(g_parser);
4046 }
4047 END_TEST
4048
4049 /* Try parsing a general entity within a parameter entity in a
4050 * standalone internal DTD. Covers a corner case in the parser.
4051 */
START_TEST(test_standalone_internal_entity)4052 START_TEST(test_standalone_internal_entity) {
4053 const char *text = "<?xml version='1.0' standalone='yes' ?>\n"
4054 "<!DOCTYPE doc [\n"
4055 " <!ELEMENT doc (#PCDATA)>\n"
4056 " <!ENTITY % pe '<!ATTLIST doc att2 CDATA \"≥\">'>\n"
4057 " <!ENTITY ge 'AttDefaultValue'>\n"
4058 " %pe;\n"
4059 "]>\n"
4060 "<doc att2='any'/>";
4061
4062 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
4063 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4064 == XML_STATUS_ERROR)
4065 xml_failure(g_parser);
4066 }
4067 END_TEST
4068
4069 /* Test that a reference to an unknown external entity is skipped */
START_TEST(test_skipped_external_entity)4070 START_TEST(test_skipped_external_entity) {
4071 const char *text = "<!DOCTYPE doc SYSTEM 'http://example.org/'>\n"
4072 "<doc></doc>\n";
4073 ExtTest test_data = {"<!ELEMENT doc EMPTY>\n"
4074 "<!ENTITY % e2 '%e1;'>\n",
4075 NULL, NULL};
4076
4077 XML_SetUserData(g_parser, &test_data);
4078 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
4079 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
4080 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4081 == XML_STATUS_ERROR)
4082 xml_failure(g_parser);
4083 }
4084 END_TEST
4085
4086 /* Test a different form of unknown external entity */
START_TEST(test_skipped_null_loaded_ext_entity)4087 START_TEST(test_skipped_null_loaded_ext_entity) {
4088 const char *text = "<!DOCTYPE doc SYSTEM 'http://example.org/one.ent'>\n"
4089 "<doc />";
4090 ExtHdlrData test_data
4091 = {"<!ENTITY % pe1 SYSTEM 'http://example.org/two.ent'>\n"
4092 "<!ENTITY % pe2 '%pe1;'>\n"
4093 "%pe2;\n",
4094 external_entity_null_loader, NULL};
4095
4096 XML_SetUserData(g_parser, &test_data);
4097 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
4098 XML_SetExternalEntityRefHandler(g_parser, external_entity_oneshot_loader);
4099 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4100 == XML_STATUS_ERROR)
4101 xml_failure(g_parser);
4102 }
4103 END_TEST
4104
START_TEST(test_skipped_unloaded_ext_entity)4105 START_TEST(test_skipped_unloaded_ext_entity) {
4106 const char *text = "<!DOCTYPE doc SYSTEM 'http://example.org/one.ent'>\n"
4107 "<doc />";
4108 ExtHdlrData test_data
4109 = {"<!ENTITY % pe1 SYSTEM 'http://example.org/two.ent'>\n"
4110 "<!ENTITY % pe2 '%pe1;'>\n"
4111 "%pe2;\n",
4112 NULL, NULL};
4113
4114 XML_SetUserData(g_parser, &test_data);
4115 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
4116 XML_SetExternalEntityRefHandler(g_parser, external_entity_oneshot_loader);
4117 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4118 == XML_STATUS_ERROR)
4119 xml_failure(g_parser);
4120 }
4121 END_TEST
4122
4123 /* Test that a parameter entity value ending with a carriage return
4124 * has it translated internally into a newline.
4125 */
START_TEST(test_param_entity_with_trailing_cr)4126 START_TEST(test_param_entity_with_trailing_cr) {
4127 #define PARAM_ENTITY_NAME "pe"
4128 #define PARAM_ENTITY_CORE_VALUE "<!ATTLIST doc att CDATA \"default\">"
4129 const char *text = "<!DOCTYPE doc SYSTEM 'http://example.org/'>\n"
4130 "<doc/>";
4131 ExtTest test_data
4132 = {"<!ENTITY % " PARAM_ENTITY_NAME " '" PARAM_ENTITY_CORE_VALUE "\r'>\n"
4133 "%" PARAM_ENTITY_NAME ";\n",
4134 NULL, NULL};
4135
4136 XML_SetUserData(g_parser, &test_data);
4137 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
4138 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader);
4139 XML_SetEntityDeclHandler(g_parser, param_entity_match_handler);
4140 param_entity_match_init(XCS(PARAM_ENTITY_NAME),
4141 XCS(PARAM_ENTITY_CORE_VALUE) XCS("\n"));
4142 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4143 == XML_STATUS_ERROR)
4144 xml_failure(g_parser);
4145 int entity_match_flag = get_param_entity_match_flag();
4146 if (entity_match_flag == ENTITY_MATCH_FAIL)
4147 fail("Parameter entity CR->NEWLINE conversion failed");
4148 else if (entity_match_flag == ENTITY_MATCH_NOT_FOUND)
4149 fail("Parameter entity not parsed");
4150 }
4151 #undef PARAM_ENTITY_NAME
4152 #undef PARAM_ENTITY_CORE_VALUE
4153 END_TEST
4154
START_TEST(test_invalid_character_entity)4155 START_TEST(test_invalid_character_entity) {
4156 const char *text = "<!DOCTYPE doc [\n"
4157 " <!ENTITY entity '�'>\n"
4158 "]>\n"
4159 "<doc>&entity;</doc>";
4160
4161 expect_failure(text, XML_ERROR_BAD_CHAR_REF,
4162 "Out of range character reference not faulted");
4163 }
4164 END_TEST
4165
START_TEST(test_invalid_character_entity_2)4166 START_TEST(test_invalid_character_entity_2) {
4167 const char *text = "<!DOCTYPE doc [\n"
4168 " <!ENTITY entity '&#xg0;'>\n"
4169 "]>\n"
4170 "<doc>&entity;</doc>";
4171
4172 expect_failure(text, XML_ERROR_INVALID_TOKEN,
4173 "Out of range character reference not faulted");
4174 }
4175 END_TEST
4176
START_TEST(test_invalid_character_entity_3)4177 START_TEST(test_invalid_character_entity_3) {
4178 const char text[] =
4179 /* <!DOCTYPE doc [\n */
4180 "\0<\0!\0D\0O\0C\0T\0Y\0P\0E\0 \0d\0o\0c\0 \0[\0\n"
4181 /* U+0E04 = KHO KHWAI
4182 * U+0E08 = CHO CHAN */
4183 /* <!ENTITY entity '&\u0e04\u0e08;'>\n */
4184 "\0<\0!\0E\0N\0T\0I\0T\0Y\0 \0e\0n\0t\0i\0t\0y\0 "
4185 "\0'\0&\x0e\x04\x0e\x08\0;\0'\0>\0\n"
4186 /* ]>\n */
4187 "\0]\0>\0\n"
4188 /* <doc>&entity;</doc> */
4189 "\0<\0d\0o\0c\0>\0&\0e\0n\0t\0i\0t\0y\0;\0<\0/\0d\0o\0c\0>";
4190
4191 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
4192 != XML_STATUS_ERROR)
4193 fail("Invalid start of entity name not faulted");
4194 if (XML_GetErrorCode(g_parser) != XML_ERROR_UNDEFINED_ENTITY)
4195 xml_failure(g_parser);
4196 }
4197 END_TEST
4198
START_TEST(test_invalid_character_entity_4)4199 START_TEST(test_invalid_character_entity_4) {
4200 const char *text = "<!DOCTYPE doc [\n"
4201 " <!ENTITY entity '�'>\n" /* = � */
4202 "]>\n"
4203 "<doc>&entity;</doc>";
4204
4205 expect_failure(text, XML_ERROR_BAD_CHAR_REF,
4206 "Out of range character reference not faulted");
4207 }
4208 END_TEST
4209
4210 /* Test that processing instructions are picked up by a default handler */
START_TEST(test_pi_handled_in_default)4211 START_TEST(test_pi_handled_in_default) {
4212 const char *text = "<?test processing instruction?>\n<doc/>";
4213 const XML_Char *expected = XCS("<?test processing instruction?>\n<doc/>");
4214 CharData storage;
4215
4216 CharData_Init(&storage);
4217 XML_SetDefaultHandler(g_parser, accumulate_characters);
4218 XML_SetUserData(g_parser, &storage);
4219 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4220 == XML_STATUS_ERROR)
4221 xml_failure(g_parser);
4222 CharData_CheckXMLChars(&storage, expected);
4223 }
4224 END_TEST
4225
4226 /* Test that comments are picked up by a default handler */
START_TEST(test_comment_handled_in_default)4227 START_TEST(test_comment_handled_in_default) {
4228 const char *text = "<!-- This is a comment -->\n<doc/>";
4229 const XML_Char *expected = XCS("<!-- This is a comment -->\n<doc/>");
4230 CharData storage;
4231
4232 CharData_Init(&storage);
4233 XML_SetDefaultHandler(g_parser, accumulate_characters);
4234 XML_SetUserData(g_parser, &storage);
4235 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4236 == XML_STATUS_ERROR)
4237 xml_failure(g_parser);
4238 CharData_CheckXMLChars(&storage, expected);
4239 }
4240 END_TEST
4241
4242 /* Test PIs that look almost but not quite like XML declarations */
START_TEST(test_pi_yml)4243 START_TEST(test_pi_yml) {
4244 const char *text = "<?yml something like data?><doc/>";
4245 const XML_Char *expected = XCS("yml: something like data\n");
4246 CharData storage;
4247
4248 CharData_Init(&storage);
4249 XML_SetProcessingInstructionHandler(g_parser, accumulate_pi_characters);
4250 XML_SetUserData(g_parser, &storage);
4251 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4252 == XML_STATUS_ERROR)
4253 xml_failure(g_parser);
4254 CharData_CheckXMLChars(&storage, expected);
4255 }
4256 END_TEST
4257
START_TEST(test_pi_xnl)4258 START_TEST(test_pi_xnl) {
4259 const char *text = "<?xnl nothing like data?><doc/>";
4260 const XML_Char *expected = XCS("xnl: nothing like data\n");
4261 CharData storage;
4262
4263 CharData_Init(&storage);
4264 XML_SetProcessingInstructionHandler(g_parser, accumulate_pi_characters);
4265 XML_SetUserData(g_parser, &storage);
4266 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4267 == XML_STATUS_ERROR)
4268 xml_failure(g_parser);
4269 CharData_CheckXMLChars(&storage, expected);
4270 }
4271 END_TEST
4272
START_TEST(test_pi_xmm)4273 START_TEST(test_pi_xmm) {
4274 const char *text = "<?xmm everything like data?><doc/>";
4275 const XML_Char *expected = XCS("xmm: everything like data\n");
4276 CharData storage;
4277
4278 CharData_Init(&storage);
4279 XML_SetProcessingInstructionHandler(g_parser, accumulate_pi_characters);
4280 XML_SetUserData(g_parser, &storage);
4281 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4282 == XML_STATUS_ERROR)
4283 xml_failure(g_parser);
4284 CharData_CheckXMLChars(&storage, expected);
4285 }
4286 END_TEST
4287
START_TEST(test_utf16_pi)4288 START_TEST(test_utf16_pi) {
4289 const char text[] =
4290 /* <?{KHO KHWAI}{CHO CHAN}?>
4291 * where {KHO KHWAI} = U+0E04
4292 * and {CHO CHAN} = U+0E08
4293 */
4294 "<\0?\0\x04\x0e\x08\x0e?\0>\0"
4295 /* <q/> */
4296 "<\0q\0/\0>\0";
4297 #ifdef XML_UNICODE
4298 const XML_Char *expected = XCS("\x0e04\x0e08: \n");
4299 #else
4300 const XML_Char *expected = XCS("\xe0\xb8\x84\xe0\xb8\x88: \n");
4301 #endif
4302 CharData storage;
4303
4304 CharData_Init(&storage);
4305 XML_SetProcessingInstructionHandler(g_parser, accumulate_pi_characters);
4306 XML_SetUserData(g_parser, &storage);
4307 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
4308 == XML_STATUS_ERROR)
4309 xml_failure(g_parser);
4310 CharData_CheckXMLChars(&storage, expected);
4311 }
4312 END_TEST
4313
START_TEST(test_utf16_be_pi)4314 START_TEST(test_utf16_be_pi) {
4315 const char text[] =
4316 /* <?{KHO KHWAI}{CHO CHAN}?>
4317 * where {KHO KHWAI} = U+0E04
4318 * and {CHO CHAN} = U+0E08
4319 */
4320 "\0<\0?\x0e\x04\x0e\x08\0?\0>"
4321 /* <q/> */
4322 "\0<\0q\0/\0>";
4323 #ifdef XML_UNICODE
4324 const XML_Char *expected = XCS("\x0e04\x0e08: \n");
4325 #else
4326 const XML_Char *expected = XCS("\xe0\xb8\x84\xe0\xb8\x88: \n");
4327 #endif
4328 CharData storage;
4329
4330 CharData_Init(&storage);
4331 XML_SetProcessingInstructionHandler(g_parser, accumulate_pi_characters);
4332 XML_SetUserData(g_parser, &storage);
4333 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
4334 == XML_STATUS_ERROR)
4335 xml_failure(g_parser);
4336 CharData_CheckXMLChars(&storage, expected);
4337 }
4338 END_TEST
4339
4340 /* Test that comments can be picked up and translated */
START_TEST(test_utf16_be_comment)4341 START_TEST(test_utf16_be_comment) {
4342 const char text[] =
4343 /* <!-- Comment A --> */
4344 "\0<\0!\0-\0-\0 \0C\0o\0m\0m\0e\0n\0t\0 \0A\0 \0-\0-\0>\0\n"
4345 /* <doc/> */
4346 "\0<\0d\0o\0c\0/\0>";
4347 const XML_Char *expected = XCS(" Comment A ");
4348 CharData storage;
4349
4350 CharData_Init(&storage);
4351 XML_SetCommentHandler(g_parser, accumulate_comment);
4352 XML_SetUserData(g_parser, &storage);
4353 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
4354 == XML_STATUS_ERROR)
4355 xml_failure(g_parser);
4356 CharData_CheckXMLChars(&storage, expected);
4357 }
4358 END_TEST
4359
START_TEST(test_utf16_le_comment)4360 START_TEST(test_utf16_le_comment) {
4361 const char text[] =
4362 /* <!-- Comment B --> */
4363 "<\0!\0-\0-\0 \0C\0o\0m\0m\0e\0n\0t\0 \0B\0 \0-\0-\0>\0\n\0"
4364 /* <doc/> */
4365 "<\0d\0o\0c\0/\0>\0";
4366 const XML_Char *expected = XCS(" Comment B ");
4367 CharData storage;
4368
4369 CharData_Init(&storage);
4370 XML_SetCommentHandler(g_parser, accumulate_comment);
4371 XML_SetUserData(g_parser, &storage);
4372 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
4373 == XML_STATUS_ERROR)
4374 xml_failure(g_parser);
4375 CharData_CheckXMLChars(&storage, expected);
4376 }
4377 END_TEST
4378
4379 /* Test that the unknown encoding handler with map entries that expect
4380 * conversion but no conversion function is faulted
4381 */
START_TEST(test_missing_encoding_conversion_fn)4382 START_TEST(test_missing_encoding_conversion_fn) {
4383 const char *text = "<?xml version='1.0' encoding='no-conv'?>\n"
4384 "<doc>\x81</doc>";
4385
4386 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4387 /* MiscEncodingHandler sets up an encoding with every top-bit-set
4388 * character introducing a two-byte sequence. For this, it
4389 * requires a convert function. The above function call doesn't
4390 * pass one through, so when BadEncodingHandler actually gets
4391 * called it should supply an invalid encoding.
4392 */
4393 expect_failure(text, XML_ERROR_UNKNOWN_ENCODING,
4394 "Encoding with missing convert() not faulted");
4395 }
4396 END_TEST
4397
START_TEST(test_failing_encoding_conversion_fn)4398 START_TEST(test_failing_encoding_conversion_fn) {
4399 const char *text = "<?xml version='1.0' encoding='failing-conv'?>\n"
4400 "<doc>\x81</doc>";
4401
4402 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4403 /* BadEncodingHandler sets up an encoding with every top-bit-set
4404 * character introducing a two-byte sequence. For this, it
4405 * requires a convert function. The above function call passes
4406 * one that insists all possible sequences are invalid anyway.
4407 */
4408 expect_failure(text, XML_ERROR_INVALID_TOKEN,
4409 "Encoding with failing convert() not faulted");
4410 }
4411 END_TEST
4412
4413 /* Test unknown encoding conversions */
START_TEST(test_unknown_encoding_success)4414 START_TEST(test_unknown_encoding_success) {
4415 const char *text = "<?xml version='1.0' encoding='prefix-conv'?>\n"
4416 /* Equivalent to <eoc>Hello, world</eoc> */
4417 "<\x81\x64\x80oc>Hello, world</\x81\x64\x80oc>";
4418
4419 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4420 run_character_check(text, XCS("Hello, world"));
4421 }
4422 END_TEST
4423
4424 /* Test bad name character in unknown encoding */
START_TEST(test_unknown_encoding_bad_name)4425 START_TEST(test_unknown_encoding_bad_name) {
4426 const char *text = "<?xml version='1.0' encoding='prefix-conv'?>\n"
4427 "<\xff\x64oc>Hello, world</\xff\x64oc>";
4428
4429 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4430 expect_failure(text, XML_ERROR_INVALID_TOKEN,
4431 "Bad name start in unknown encoding not faulted");
4432 }
4433 END_TEST
4434
4435 /* Test bad mid-name character in unknown encoding */
START_TEST(test_unknown_encoding_bad_name_2)4436 START_TEST(test_unknown_encoding_bad_name_2) {
4437 const char *text = "<?xml version='1.0' encoding='prefix-conv'?>\n"
4438 "<d\xffoc>Hello, world</d\xffoc>";
4439
4440 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4441 expect_failure(text, XML_ERROR_INVALID_TOKEN,
4442 "Bad name in unknown encoding not faulted");
4443 }
4444 END_TEST
4445
4446 /* Test element name that is long enough to fill the conversion buffer
4447 * in an unknown encoding, finishing with an encoded character.
4448 */
START_TEST(test_unknown_encoding_long_name_1)4449 START_TEST(test_unknown_encoding_long_name_1) {
4450 const char *text = "<?xml version='1.0' encoding='prefix-conv'?>\n"
4451 "<abcdefghabcdefghabcdefghijkl\x80m\x80n\x80o\x80p>"
4452 "Hi"
4453 "</abcdefghabcdefghabcdefghijkl\x80m\x80n\x80o\x80p>";
4454 const XML_Char *expected = XCS("abcdefghabcdefghabcdefghijklmnop");
4455 CharData storage;
4456
4457 CharData_Init(&storage);
4458 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4459 XML_SetStartElementHandler(g_parser, record_element_start_handler);
4460 XML_SetUserData(g_parser, &storage);
4461 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4462 == XML_STATUS_ERROR)
4463 xml_failure(g_parser);
4464 CharData_CheckXMLChars(&storage, expected);
4465 }
4466 END_TEST
4467
4468 /* Test element name that is long enough to fill the conversion buffer
4469 * in an unknown encoding, finishing with an simple character.
4470 */
START_TEST(test_unknown_encoding_long_name_2)4471 START_TEST(test_unknown_encoding_long_name_2) {
4472 const char *text = "<?xml version='1.0' encoding='prefix-conv'?>\n"
4473 "<abcdefghabcdefghabcdefghijklmnop>"
4474 "Hi"
4475 "</abcdefghabcdefghabcdefghijklmnop>";
4476 const XML_Char *expected = XCS("abcdefghabcdefghabcdefghijklmnop");
4477 CharData storage;
4478
4479 CharData_Init(&storage);
4480 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4481 XML_SetStartElementHandler(g_parser, record_element_start_handler);
4482 XML_SetUserData(g_parser, &storage);
4483 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4484 == XML_STATUS_ERROR)
4485 xml_failure(g_parser);
4486 CharData_CheckXMLChars(&storage, expected);
4487 }
4488 END_TEST
4489
START_TEST(test_invalid_unknown_encoding)4490 START_TEST(test_invalid_unknown_encoding) {
4491 const char *text = "<?xml version='1.0' encoding='invalid-9'?>\n"
4492 "<doc>Hello world</doc>";
4493
4494 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4495 expect_failure(text, XML_ERROR_UNKNOWN_ENCODING,
4496 "Invalid unknown encoding not faulted");
4497 }
4498 END_TEST
4499
START_TEST(test_unknown_ascii_encoding_ok)4500 START_TEST(test_unknown_ascii_encoding_ok) {
4501 const char *text = "<?xml version='1.0' encoding='ascii-like'?>\n"
4502 "<doc>Hello, world</doc>";
4503
4504 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4505 run_character_check(text, XCS("Hello, world"));
4506 }
4507 END_TEST
4508
START_TEST(test_unknown_ascii_encoding_fail)4509 START_TEST(test_unknown_ascii_encoding_fail) {
4510 const char *text = "<?xml version='1.0' encoding='ascii-like'?>\n"
4511 "<doc>Hello, \x80 world</doc>";
4512
4513 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4514 expect_failure(text, XML_ERROR_INVALID_TOKEN,
4515 "Invalid character not faulted");
4516 }
4517 END_TEST
4518
START_TEST(test_unknown_encoding_invalid_length)4519 START_TEST(test_unknown_encoding_invalid_length) {
4520 const char *text = "<?xml version='1.0' encoding='invalid-len'?>\n"
4521 "<doc>Hello, world</doc>";
4522
4523 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4524 expect_failure(text, XML_ERROR_UNKNOWN_ENCODING,
4525 "Invalid unknown encoding not faulted");
4526 }
4527 END_TEST
4528
START_TEST(test_unknown_encoding_invalid_topbit)4529 START_TEST(test_unknown_encoding_invalid_topbit) {
4530 const char *text = "<?xml version='1.0' encoding='invalid-a'?>\n"
4531 "<doc>Hello, world</doc>";
4532
4533 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4534 expect_failure(text, XML_ERROR_UNKNOWN_ENCODING,
4535 "Invalid unknown encoding not faulted");
4536 }
4537 END_TEST
4538
START_TEST(test_unknown_encoding_invalid_surrogate)4539 START_TEST(test_unknown_encoding_invalid_surrogate) {
4540 const char *text = "<?xml version='1.0' encoding='invalid-surrogate'?>\n"
4541 "<doc>Hello, \x82 world</doc>";
4542
4543 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4544 expect_failure(text, XML_ERROR_INVALID_TOKEN,
4545 "Invalid unknown encoding not faulted");
4546 }
4547 END_TEST
4548
START_TEST(test_unknown_encoding_invalid_high)4549 START_TEST(test_unknown_encoding_invalid_high) {
4550 const char *text = "<?xml version='1.0' encoding='invalid-high'?>\n"
4551 "<doc>Hello, world</doc>";
4552
4553 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4554 expect_failure(text, XML_ERROR_UNKNOWN_ENCODING,
4555 "Invalid unknown encoding not faulted");
4556 }
4557 END_TEST
4558
START_TEST(test_unknown_encoding_invalid_attr_value)4559 START_TEST(test_unknown_encoding_invalid_attr_value) {
4560 const char *text = "<?xml version='1.0' encoding='prefix-conv'?>\n"
4561 "<doc attr='\xff\x30'/>";
4562
4563 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
4564 expect_failure(text, XML_ERROR_INVALID_TOKEN,
4565 "Invalid attribute valid not faulted");
4566 }
4567 END_TEST
4568
4569 /* Test an external entity parser set to use latin-1 detects UTF-16
4570 * BOMs correctly.
4571 */
4572 /* Test that UTF-16 BOM does not select UTF-16 given explicit encoding */
START_TEST(test_ext_entity_latin1_utf16le_bom)4573 START_TEST(test_ext_entity_latin1_utf16le_bom) {
4574 const char *text = "<!DOCTYPE doc [\n"
4575 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
4576 "]>\n"
4577 "<doc>&en;</doc>";
4578 ExtTest2 test_data
4579 = {/* If UTF-16, 0xfeff is the BOM and 0x204c is black left bullet */
4580 /* If Latin-1, 0xff = Y-diaeresis, 0xfe = lowercase thorn,
4581 * 0x4c = L and 0x20 is a space
4582 */
4583 "\xff\xfe\x4c\x20", 4, XCS("iso-8859-1"), NULL};
4584 #ifdef XML_UNICODE
4585 const XML_Char *expected = XCS("\x00ff\x00feL ");
4586 #else
4587 /* In UTF-8, y-diaeresis is 0xc3 0xbf, lowercase thorn is 0xc3 0xbe */
4588 const XML_Char *expected = XCS("\xc3\xbf\xc3\xbeL ");
4589 #endif
4590 CharData storage;
4591
4592 CharData_Init(&storage);
4593 test_data.storage = &storage;
4594 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader2);
4595 XML_SetUserData(g_parser, &test_data);
4596 XML_SetCharacterDataHandler(g_parser, ext2_accumulate_characters);
4597 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4598 == XML_STATUS_ERROR)
4599 xml_failure(g_parser);
4600 CharData_CheckXMLChars(&storage, expected);
4601 }
4602 END_TEST
4603
START_TEST(test_ext_entity_latin1_utf16be_bom)4604 START_TEST(test_ext_entity_latin1_utf16be_bom) {
4605 const char *text = "<!DOCTYPE doc [\n"
4606 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
4607 "]>\n"
4608 "<doc>&en;</doc>";
4609 ExtTest2 test_data
4610 = {/* If UTF-16, 0xfeff is the BOM and 0x204c is black left bullet */
4611 /* If Latin-1, 0xff = Y-diaeresis, 0xfe = lowercase thorn,
4612 * 0x4c = L and 0x20 is a space
4613 */
4614 "\xfe\xff\x20\x4c", 4, XCS("iso-8859-1"), NULL};
4615 #ifdef XML_UNICODE
4616 const XML_Char *expected = XCS("\x00fe\x00ff L");
4617 #else
4618 /* In UTF-8, y-diaeresis is 0xc3 0xbf, lowercase thorn is 0xc3 0xbe */
4619 const XML_Char *expected = XCS("\xc3\xbe\xc3\xbf L");
4620 #endif
4621 CharData storage;
4622
4623 CharData_Init(&storage);
4624 test_data.storage = &storage;
4625 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader2);
4626 XML_SetUserData(g_parser, &test_data);
4627 XML_SetCharacterDataHandler(g_parser, ext2_accumulate_characters);
4628 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4629 == XML_STATUS_ERROR)
4630 xml_failure(g_parser);
4631 CharData_CheckXMLChars(&storage, expected);
4632 }
4633 END_TEST
4634
4635 /* Parsing the full buffer rather than a byte at a time makes a
4636 * difference to the encoding scanning code, so repeat the above tests
4637 * without breaking them down by byte.
4638 */
START_TEST(test_ext_entity_latin1_utf16le_bom2)4639 START_TEST(test_ext_entity_latin1_utf16le_bom2) {
4640 const char *text = "<!DOCTYPE doc [\n"
4641 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
4642 "]>\n"
4643 "<doc>&en;</doc>";
4644 ExtTest2 test_data
4645 = {/* If UTF-16, 0xfeff is the BOM and 0x204c is black left bullet */
4646 /* If Latin-1, 0xff = Y-diaeresis, 0xfe = lowercase thorn,
4647 * 0x4c = L and 0x20 is a space
4648 */
4649 "\xff\xfe\x4c\x20", 4, XCS("iso-8859-1"), NULL};
4650 #ifdef XML_UNICODE
4651 const XML_Char *expected = XCS("\x00ff\x00feL ");
4652 #else
4653 /* In UTF-8, y-diaeresis is 0xc3 0xbf, lowercase thorn is 0xc3 0xbe */
4654 const XML_Char *expected = XCS("\xc3\xbf\xc3\xbeL ");
4655 #endif
4656 CharData storage;
4657
4658 CharData_Init(&storage);
4659 test_data.storage = &storage;
4660 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader2);
4661 XML_SetUserData(g_parser, &test_data);
4662 XML_SetCharacterDataHandler(g_parser, ext2_accumulate_characters);
4663 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4664 == XML_STATUS_ERROR)
4665 xml_failure(g_parser);
4666 CharData_CheckXMLChars(&storage, expected);
4667 }
4668 END_TEST
4669
START_TEST(test_ext_entity_latin1_utf16be_bom2)4670 START_TEST(test_ext_entity_latin1_utf16be_bom2) {
4671 const char *text = "<!DOCTYPE doc [\n"
4672 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
4673 "]>\n"
4674 "<doc>&en;</doc>";
4675 ExtTest2 test_data
4676 = {/* If UTF-16, 0xfeff is the BOM and 0x204c is black left bullet */
4677 /* If Latin-1, 0xff = Y-diaeresis, 0xfe = lowercase thorn,
4678 * 0x4c = L and 0x20 is a space
4679 */
4680 "\xfe\xff\x20\x4c", 4, XCS("iso-8859-1"), NULL};
4681 #ifdef XML_UNICODE
4682 const XML_Char *expected = XCS("\x00fe\x00ff L");
4683 #else
4684 /* In UTF-8, y-diaeresis is 0xc3 0xbf, lowercase thorn is 0xc3 0xbe */
4685 const XML_Char *expected = "\xc3\xbe\xc3\xbf L";
4686 #endif
4687 CharData storage;
4688
4689 CharData_Init(&storage);
4690 test_data.storage = &storage;
4691 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader2);
4692 XML_SetUserData(g_parser, &test_data);
4693 XML_SetCharacterDataHandler(g_parser, ext2_accumulate_characters);
4694 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4695 == XML_STATUS_ERROR)
4696 xml_failure(g_parser);
4697 CharData_CheckXMLChars(&storage, expected);
4698 }
4699 END_TEST
4700
4701 /* Test little-endian UTF-16 given an explicit big-endian encoding */
START_TEST(test_ext_entity_utf16_be)4702 START_TEST(test_ext_entity_utf16_be) {
4703 const char *text = "<!DOCTYPE doc [\n"
4704 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
4705 "]>\n"
4706 "<doc>&en;</doc>";
4707 ExtTest2 test_data = {"<\0e\0/\0>\0", 8, XCS("utf-16be"), NULL};
4708 #ifdef XML_UNICODE
4709 const XML_Char *expected = XCS("\x3c00\x6500\x2f00\x3e00");
4710 #else
4711 const XML_Char *expected = XCS("\xe3\xb0\x80" /* U+3C00 */
4712 "\xe6\x94\x80" /* U+6500 */
4713 "\xe2\xbc\x80" /* U+2F00 */
4714 "\xe3\xb8\x80"); /* U+3E00 */
4715 #endif
4716 CharData storage;
4717
4718 CharData_Init(&storage);
4719 test_data.storage = &storage;
4720 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader2);
4721 XML_SetUserData(g_parser, &test_data);
4722 XML_SetCharacterDataHandler(g_parser, ext2_accumulate_characters);
4723 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4724 == XML_STATUS_ERROR)
4725 xml_failure(g_parser);
4726 CharData_CheckXMLChars(&storage, expected);
4727 }
4728 END_TEST
4729
4730 /* Test big-endian UTF-16 given an explicit little-endian encoding */
START_TEST(test_ext_entity_utf16_le)4731 START_TEST(test_ext_entity_utf16_le) {
4732 const char *text = "<!DOCTYPE doc [\n"
4733 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
4734 "]>\n"
4735 "<doc>&en;</doc>";
4736 ExtTest2 test_data = {"\0<\0e\0/\0>", 8, XCS("utf-16le"), NULL};
4737 #ifdef XML_UNICODE
4738 const XML_Char *expected = XCS("\x3c00\x6500\x2f00\x3e00");
4739 #else
4740 const XML_Char *expected = XCS("\xe3\xb0\x80" /* U+3C00 */
4741 "\xe6\x94\x80" /* U+6500 */
4742 "\xe2\xbc\x80" /* U+2F00 */
4743 "\xe3\xb8\x80"); /* U+3E00 */
4744 #endif
4745 CharData storage;
4746
4747 CharData_Init(&storage);
4748 test_data.storage = &storage;
4749 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader2);
4750 XML_SetUserData(g_parser, &test_data);
4751 XML_SetCharacterDataHandler(g_parser, ext2_accumulate_characters);
4752 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4753 == XML_STATUS_ERROR)
4754 xml_failure(g_parser);
4755 CharData_CheckXMLChars(&storage, expected);
4756 }
4757 END_TEST
4758
4759 /* Test little-endian UTF-16 given no explicit encoding.
4760 * The existing default encoding (UTF-8) is assumed to hold without a
4761 * BOM to contradict it, so the entity value will in fact provoke an
4762 * error because 0x00 is not a valid XML character. We parse the
4763 * whole buffer in one go rather than feeding it in byte by byte to
4764 * exercise different code paths in the initial scanning routines.
4765 */
START_TEST(test_ext_entity_utf16_unknown)4766 START_TEST(test_ext_entity_utf16_unknown) {
4767 const char *text = "<!DOCTYPE doc [\n"
4768 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
4769 "]>\n"
4770 "<doc>&en;</doc>";
4771 ExtFaults2 test_data
4772 = {"a\0b\0c\0", 6, "Invalid character in entity not faulted", NULL,
4773 XML_ERROR_INVALID_TOKEN};
4774
4775 XML_SetExternalEntityRefHandler(g_parser, external_entity_faulter2);
4776 XML_SetUserData(g_parser, &test_data);
4777 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
4778 "Invalid character should not have been accepted");
4779 }
4780 END_TEST
4781
4782 /* Test not-quite-UTF-8 BOM (0xEF 0xBB 0xBF) */
START_TEST(test_ext_entity_utf8_non_bom)4783 START_TEST(test_ext_entity_utf8_non_bom) {
4784 const char *text = "<!DOCTYPE doc [\n"
4785 " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
4786 "]>\n"
4787 "<doc>&en;</doc>";
4788 ExtTest2 test_data
4789 = {"\xef\xbb\x80", /* Arabic letter DAD medial form, U+FEC0 */
4790 3, NULL, NULL};
4791 #ifdef XML_UNICODE
4792 const XML_Char *expected = XCS("\xfec0");
4793 #else
4794 const XML_Char *expected = XCS("\xef\xbb\x80");
4795 #endif
4796 CharData storage;
4797
4798 CharData_Init(&storage);
4799 test_data.storage = &storage;
4800 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader2);
4801 XML_SetUserData(g_parser, &test_data);
4802 XML_SetCharacterDataHandler(g_parser, ext2_accumulate_characters);
4803 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4804 == XML_STATUS_ERROR)
4805 xml_failure(g_parser);
4806 CharData_CheckXMLChars(&storage, expected);
4807 }
4808 END_TEST
4809
4810 /* Test that UTF-8 in a CDATA section is correctly passed through */
START_TEST(test_utf8_in_cdata_section)4811 START_TEST(test_utf8_in_cdata_section) {
4812 const char *text = "<doc><![CDATA[one \xc3\xa9 two]]></doc>";
4813 #ifdef XML_UNICODE
4814 const XML_Char *expected = XCS("one \x00e9 two");
4815 #else
4816 const XML_Char *expected = XCS("one \xc3\xa9 two");
4817 #endif
4818
4819 run_character_check(text, expected);
4820 }
4821 END_TEST
4822
4823 /* Test that little-endian UTF-16 in a CDATA section is handled */
START_TEST(test_utf8_in_cdata_section_2)4824 START_TEST(test_utf8_in_cdata_section_2) {
4825 const char *text = "<doc><![CDATA[\xc3\xa9]\xc3\xa9two]]></doc>";
4826 #ifdef XML_UNICODE
4827 const XML_Char *expected = XCS("\x00e9]\x00e9two");
4828 #else
4829 const XML_Char *expected = XCS("\xc3\xa9]\xc3\xa9two");
4830 #endif
4831
4832 run_character_check(text, expected);
4833 }
4834 END_TEST
4835
START_TEST(test_utf8_in_start_tags)4836 START_TEST(test_utf8_in_start_tags) {
4837 struct test_case {
4838 bool goodName;
4839 bool goodNameStart;
4840 const char *tagName;
4841 };
4842
4843 // The idea with the tests below is this:
4844 // We want to cover 1-, 2- and 3-byte sequences, 4-byte sequences
4845 // go to isNever and are hence not a concern.
4846 //
4847 // We start with a character that is a valid name character
4848 // (or even name-start character, see XML 1.0r4 spec) and then we flip
4849 // single bits at places where (1) the result leaves the UTF-8 encoding space
4850 // and (2) we stay in the same n-byte sequence family.
4851 //
4852 // The flipped bits are highlighted in angle brackets in comments,
4853 // e.g. "[<1>011 1001]" means we had [0011 1001] but we now flipped
4854 // the most significant bit to 1 to leave UTF-8 encoding space.
4855 struct test_case cases[] = {
4856 // 1-byte UTF-8: [0xxx xxxx]
4857 {true, true, "\x3A"}, // [0011 1010] = ASCII colon ':'
4858 {false, false, "\xBA"}, // [<1>011 1010]
4859 {true, false, "\x39"}, // [0011 1001] = ASCII nine '9'
4860 {false, false, "\xB9"}, // [<1>011 1001]
4861
4862 // 2-byte UTF-8: [110x xxxx] [10xx xxxx]
4863 {true, true, "\xDB\xA5"}, // [1101 1011] [1010 0101] =
4864 // Arabic small waw U+06E5
4865 {false, false, "\x9B\xA5"}, // [1<0>01 1011] [1010 0101]
4866 {false, false, "\xDB\x25"}, // [1101 1011] [<0>010 0101]
4867 {false, false, "\xDB\xE5"}, // [1101 1011] [1<1>10 0101]
4868 {true, false, "\xCC\x81"}, // [1100 1100] [1000 0001] =
4869 // combining char U+0301
4870 {false, false, "\x8C\x81"}, // [1<0>00 1100] [1000 0001]
4871 {false, false, "\xCC\x01"}, // [1100 1100] [<0>000 0001]
4872 {false, false, "\xCC\xC1"}, // [1100 1100] [1<1>00 0001]
4873
4874 // 3-byte UTF-8: [1110 xxxx] [10xx xxxx] [10xxxxxx]
4875 {true, true, "\xE0\xA4\x85"}, // [1110 0000] [1010 0100] [1000 0101] =
4876 // Devanagari Letter A U+0905
4877 {false, false, "\xA0\xA4\x85"}, // [1<0>10 0000] [1010 0100] [1000 0101]
4878 {false, false, "\xE0\x24\x85"}, // [1110 0000] [<0>010 0100] [1000 0101]
4879 {false, false, "\xE0\xE4\x85"}, // [1110 0000] [1<1>10 0100] [1000 0101]
4880 {false, false, "\xE0\xA4\x05"}, // [1110 0000] [1010 0100] [<0>000 0101]
4881 {false, false, "\xE0\xA4\xC5"}, // [1110 0000] [1010 0100] [1<1>00 0101]
4882 {true, false, "\xE0\xA4\x81"}, // [1110 0000] [1010 0100] [1000 0001] =
4883 // combining char U+0901
4884 {false, false, "\xA0\xA4\x81"}, // [1<0>10 0000] [1010 0100] [1000 0001]
4885 {false, false, "\xE0\x24\x81"}, // [1110 0000] [<0>010 0100] [1000 0001]
4886 {false, false, "\xE0\xE4\x81"}, // [1110 0000] [1<1>10 0100] [1000 0001]
4887 {false, false, "\xE0\xA4\x01"}, // [1110 0000] [1010 0100] [<0>000 0001]
4888 {false, false, "\xE0\xA4\xC1"}, // [1110 0000] [1010 0100] [1<1>00 0001]
4889 };
4890 const bool atNameStart[] = {true, false};
4891
4892 size_t i = 0;
4893 char doc[1024];
4894 size_t failCount = 0;
4895
4896 // we need all the bytes to be parsed, but we don't want the errors that can
4897 // trigger on isFinal=XML_TRUE, so we skip the test if the heuristic is on.
4898 if (g_reparseDeferralEnabledDefault) {
4899 return;
4900 }
4901
4902 for (; i < sizeof(cases) / sizeof(cases[0]); i++) {
4903 size_t j = 0;
4904 for (; j < sizeof(atNameStart) / sizeof(atNameStart[0]); j++) {
4905 const bool expectedSuccess
4906 = atNameStart[j] ? cases[i].goodNameStart : cases[i].goodName;
4907 snprintf(doc, sizeof(doc), "<%s%s><!--", atNameStart[j] ? "" : "a",
4908 cases[i].tagName);
4909 XML_Parser parser = XML_ParserCreate(NULL);
4910
4911 const enum XML_Status status = _XML_Parse_SINGLE_BYTES(
4912 parser, doc, (int)strlen(doc), /*isFinal=*/XML_FALSE);
4913
4914 bool success = true;
4915 if ((status == XML_STATUS_OK) != expectedSuccess) {
4916 success = false;
4917 }
4918 if ((status == XML_STATUS_ERROR)
4919 && (XML_GetErrorCode(parser) != XML_ERROR_INVALID_TOKEN)) {
4920 success = false;
4921 }
4922
4923 if (! success) {
4924 fprintf(
4925 stderr,
4926 "FAIL case %2u (%sat name start, %u-byte sequence, error code %d)\n",
4927 (unsigned)i + 1u, atNameStart[j] ? " " : "not ",
4928 (unsigned)strlen(cases[i].tagName), XML_GetErrorCode(parser));
4929 failCount++;
4930 }
4931
4932 XML_ParserFree(parser);
4933 }
4934 }
4935
4936 if (failCount > 0) {
4937 fail("UTF-8 regression detected");
4938 }
4939 }
4940 END_TEST
4941
4942 /* Test trailing spaces in elements are accepted */
START_TEST(test_trailing_spaces_in_elements)4943 START_TEST(test_trailing_spaces_in_elements) {
4944 const char *text = "<doc >Hi</doc >";
4945 const XML_Char *expected = XCS("doc/doc");
4946 CharData storage;
4947
4948 CharData_Init(&storage);
4949 XML_SetElementHandler(g_parser, record_element_start_handler,
4950 record_element_end_handler);
4951 XML_SetUserData(g_parser, &storage);
4952 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
4953 == XML_STATUS_ERROR)
4954 xml_failure(g_parser);
4955 CharData_CheckXMLChars(&storage, expected);
4956 }
4957 END_TEST
4958
START_TEST(test_utf16_attribute)4959 START_TEST(test_utf16_attribute) {
4960 const char text[] =
4961 /* <d {KHO KHWAI}{CHO CHAN}='a'/>
4962 * where {KHO KHWAI} = U+0E04 = 0xe0 0xb8 0x84 in UTF-8
4963 * and {CHO CHAN} = U+0E08 = 0xe0 0xb8 0x88 in UTF-8
4964 */
4965 "<\0d\0 \0\x04\x0e\x08\x0e=\0'\0a\0'\0/\0>\0";
4966 const XML_Char *expected = XCS("a");
4967 CharData storage;
4968
4969 CharData_Init(&storage);
4970 XML_SetStartElementHandler(g_parser, accumulate_attribute);
4971 XML_SetUserData(g_parser, &storage);
4972 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
4973 == XML_STATUS_ERROR)
4974 xml_failure(g_parser);
4975 CharData_CheckXMLChars(&storage, expected);
4976 }
4977 END_TEST
4978
START_TEST(test_utf16_second_attr)4979 START_TEST(test_utf16_second_attr) {
4980 /* <d a='1' {KHO KHWAI}{CHO CHAN}='2'/>
4981 * where {KHO KHWAI} = U+0E04 = 0xe0 0xb8 0x84 in UTF-8
4982 * and {CHO CHAN} = U+0E08 = 0xe0 0xb8 0x88 in UTF-8
4983 */
4984 const char text[] = "<\0d\0 \0a\0=\0'\0\x31\0'\0 \0"
4985 "\x04\x0e\x08\x0e=\0'\0\x32\0'\0/\0>\0";
4986 const XML_Char *expected = XCS("1");
4987 CharData storage;
4988
4989 CharData_Init(&storage);
4990 XML_SetStartElementHandler(g_parser, accumulate_attribute);
4991 XML_SetUserData(g_parser, &storage);
4992 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
4993 == XML_STATUS_ERROR)
4994 xml_failure(g_parser);
4995 CharData_CheckXMLChars(&storage, expected);
4996 }
4997 END_TEST
4998
START_TEST(test_attr_after_solidus)4999 START_TEST(test_attr_after_solidus) {
5000 const char *text = "<doc attr1='a' / attr2='b'>";
5001
5002 expect_failure(text, XML_ERROR_INVALID_TOKEN, "Misplaced / not faulted");
5003 }
5004 END_TEST
5005
START_TEST(test_utf16_pe)5006 START_TEST(test_utf16_pe) {
5007 /* <!DOCTYPE doc [
5008 * <!ENTITY % {KHO KHWAI}{CHO CHAN} '<!ELEMENT doc (#PCDATA)>'>
5009 * %{KHO KHWAI}{CHO CHAN};
5010 * ]>
5011 * <doc></doc>
5012 *
5013 * where {KHO KHWAI} = U+0E04 = 0xe0 0xb8 0x84 in UTF-8
5014 * and {CHO CHAN} = U+0E08 = 0xe0 0xb8 0x88 in UTF-8
5015 */
5016 const char text[] = "\0<\0!\0D\0O\0C\0T\0Y\0P\0E\0 \0d\0o\0c\0 \0[\0\n"
5017 "\0<\0!\0E\0N\0T\0I\0T\0Y\0 \0%\0 \x0e\x04\x0e\x08\0 "
5018 "\0'\0<\0!\0E\0L\0E\0M\0E\0N\0T\0 "
5019 "\0d\0o\0c\0 \0(\0#\0P\0C\0D\0A\0T\0A\0)\0>\0'\0>\0\n"
5020 "\0%\x0e\x04\x0e\x08\0;\0\n"
5021 "\0]\0>\0\n"
5022 "\0<\0d\0o\0c\0>\0<\0/\0d\0o\0c\0>";
5023 #ifdef XML_UNICODE
5024 const XML_Char *expected = XCS("\x0e04\x0e08=<!ELEMENT doc (#PCDATA)>\n");
5025 #else
5026 const XML_Char *expected
5027 = XCS("\xe0\xb8\x84\xe0\xb8\x88=<!ELEMENT doc (#PCDATA)>\n");
5028 #endif
5029 CharData storage;
5030
5031 CharData_Init(&storage);
5032 XML_SetUserData(g_parser, &storage);
5033 XML_SetEntityDeclHandler(g_parser, accumulate_entity_decl);
5034 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
5035 == XML_STATUS_ERROR)
5036 xml_failure(g_parser);
5037 CharData_CheckXMLChars(&storage, expected);
5038 }
5039 END_TEST
5040
5041 /* Test that duff attribute description keywords are rejected */
START_TEST(test_bad_attr_desc_keyword)5042 START_TEST(test_bad_attr_desc_keyword) {
5043 const char *text = "<!DOCTYPE doc [\n"
5044 " <!ATTLIST doc attr CDATA #!IMPLIED>\n"
5045 "]>\n"
5046 "<doc />";
5047
5048 expect_failure(text, XML_ERROR_INVALID_TOKEN,
5049 "Bad keyword !IMPLIED not faulted");
5050 }
5051 END_TEST
5052
5053 /* Test that an invalid attribute description keyword consisting of
5054 * UTF-16 characters with their top bytes non-zero are correctly
5055 * faulted
5056 */
START_TEST(test_bad_attr_desc_keyword_utf16)5057 START_TEST(test_bad_attr_desc_keyword_utf16) {
5058 /* <!DOCTYPE d [
5059 * <!ATTLIST d a CDATA #{KHO KHWAI}{CHO CHAN}>
5060 * ]><d/>
5061 *
5062 * where {KHO KHWAI} = U+0E04 = 0xe0 0xb8 0x84 in UTF-8
5063 * and {CHO CHAN} = U+0E08 = 0xe0 0xb8 0x88 in UTF-8
5064 */
5065 const char text[]
5066 = "\0<\0!\0D\0O\0C\0T\0Y\0P\0E\0 \0d\0 \0[\0\n"
5067 "\0<\0!\0A\0T\0T\0L\0I\0S\0T\0 \0d\0 \0a\0 \0C\0D\0A\0T\0A\0 "
5068 "\0#\x0e\x04\x0e\x08\0>\0\n"
5069 "\0]\0>\0<\0d\0/\0>";
5070
5071 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
5072 != XML_STATUS_ERROR)
5073 fail("Invalid UTF16 attribute keyword not faulted");
5074 if (XML_GetErrorCode(g_parser) != XML_ERROR_SYNTAX)
5075 xml_failure(g_parser);
5076 }
5077 END_TEST
5078
5079 /* Test that invalid syntax in a <!DOCTYPE> is rejected. Do this
5080 * using prefix-encoding (see above) to trigger specific code paths
5081 */
START_TEST(test_bad_doctype)5082 START_TEST(test_bad_doctype) {
5083 const char *text = "<?xml version='1.0' encoding='prefix-conv'?>\n"
5084 "<!DOCTYPE doc [ \x80\x44 ]><doc/>";
5085
5086 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
5087 expect_failure(text, XML_ERROR_SYNTAX,
5088 "Invalid bytes in DOCTYPE not faulted");
5089 }
5090 END_TEST
5091
START_TEST(test_bad_doctype_utf8)5092 START_TEST(test_bad_doctype_utf8) {
5093 const char *text = "<!DOCTYPE \xDB\x25"
5094 "doc><doc/>"; // [1101 1011] [<0>010 0101]
5095 expect_failure(text, XML_ERROR_INVALID_TOKEN,
5096 "Invalid UTF-8 in DOCTYPE not faulted");
5097 }
5098 END_TEST
5099
START_TEST(test_bad_doctype_utf16)5100 START_TEST(test_bad_doctype_utf16) {
5101 const char text[] =
5102 /* <!DOCTYPE doc [ \x06f2 ]><doc/>
5103 *
5104 * U+06F2 = EXTENDED ARABIC-INDIC DIGIT TWO, a valid number
5105 * (name character) but not a valid letter (name start character)
5106 */
5107 "\0<\0!\0D\0O\0C\0T\0Y\0P\0E\0 \0d\0o\0c\0 \0[\0 "
5108 "\x06\xf2"
5109 "\0 \0]\0>\0<\0d\0o\0c\0/\0>";
5110
5111 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
5112 != XML_STATUS_ERROR)
5113 fail("Invalid bytes in DOCTYPE not faulted");
5114 if (XML_GetErrorCode(g_parser) != XML_ERROR_SYNTAX)
5115 xml_failure(g_parser);
5116 }
5117 END_TEST
5118
START_TEST(test_bad_doctype_plus)5119 START_TEST(test_bad_doctype_plus) {
5120 const char *text = "<!DOCTYPE 1+ [ <!ENTITY foo 'bar'> ]>\n"
5121 "<1+>&foo;</1+>";
5122
5123 expect_failure(text, XML_ERROR_INVALID_TOKEN,
5124 "'+' in document name not faulted");
5125 }
5126 END_TEST
5127
START_TEST(test_bad_doctype_star)5128 START_TEST(test_bad_doctype_star) {
5129 const char *text = "<!DOCTYPE 1* [ <!ENTITY foo 'bar'> ]>\n"
5130 "<1*>&foo;</1*>";
5131
5132 expect_failure(text, XML_ERROR_INVALID_TOKEN,
5133 "'*' in document name not faulted");
5134 }
5135 END_TEST
5136
START_TEST(test_bad_doctype_query)5137 START_TEST(test_bad_doctype_query) {
5138 const char *text = "<!DOCTYPE 1? [ <!ENTITY foo 'bar'> ]>\n"
5139 "<1?>&foo;</1?>";
5140
5141 expect_failure(text, XML_ERROR_INVALID_TOKEN,
5142 "'?' in document name not faulted");
5143 }
5144 END_TEST
5145
START_TEST(test_unknown_encoding_bad_ignore)5146 START_TEST(test_unknown_encoding_bad_ignore) {
5147 const char *text = "<?xml version='1.0' encoding='prefix-conv'?>"
5148 "<!DOCTYPE doc SYSTEM 'foo'>"
5149 "<doc><e>&entity;</e></doc>";
5150 ExtFaults fault = {"<![IGNORE[<!ELEMENT \xffG (#PCDATA)*>]]>",
5151 "Invalid character not faulted", XCS("prefix-conv"),
5152 XML_ERROR_INVALID_TOKEN};
5153
5154 XML_SetUnknownEncodingHandler(g_parser, MiscEncodingHandler, NULL);
5155 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
5156 XML_SetExternalEntityRefHandler(g_parser, external_entity_faulter);
5157 XML_SetUserData(g_parser, &fault);
5158 expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
5159 "Bad IGNORE section with unknown encoding not failed");
5160 }
5161 END_TEST
5162
START_TEST(test_entity_in_utf16_be_attr)5163 START_TEST(test_entity_in_utf16_be_attr) {
5164 const char text[] =
5165 /* <e a='ä ä'></e> */
5166 "\0<\0e\0 \0a\0=\0'\0&\0#\0\x32\0\x32\0\x38\0;\0 "
5167 "\0&\0#\0x\0\x30\0\x30\0E\0\x34\0;\0'\0>\0<\0/\0e\0>";
5168 #ifdef XML_UNICODE
5169 const XML_Char *expected = XCS("\x00e4 \x00e4");
5170 #else
5171 const XML_Char *expected = XCS("\xc3\xa4 \xc3\xa4");
5172 #endif
5173 CharData storage;
5174
5175 CharData_Init(&storage);
5176 XML_SetUserData(g_parser, &storage);
5177 XML_SetStartElementHandler(g_parser, accumulate_attribute);
5178 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
5179 == XML_STATUS_ERROR)
5180 xml_failure(g_parser);
5181 CharData_CheckXMLChars(&storage, expected);
5182 }
5183 END_TEST
5184
START_TEST(test_entity_in_utf16_le_attr)5185 START_TEST(test_entity_in_utf16_le_attr) {
5186 const char text[] =
5187 /* <e a='ä ä'></e> */
5188 "<\0e\0 \0a\0=\0'\0&\0#\0\x32\0\x32\0\x38\0;\0 \0"
5189 "&\0#\0x\0\x30\0\x30\0E\0\x34\0;\0'\0>\0<\0/\0e\0>\0";
5190 #ifdef XML_UNICODE
5191 const XML_Char *expected = XCS("\x00e4 \x00e4");
5192 #else
5193 const XML_Char *expected = XCS("\xc3\xa4 \xc3\xa4");
5194 #endif
5195 CharData storage;
5196
5197 CharData_Init(&storage);
5198 XML_SetUserData(g_parser, &storage);
5199 XML_SetStartElementHandler(g_parser, accumulate_attribute);
5200 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
5201 == XML_STATUS_ERROR)
5202 xml_failure(g_parser);
5203 CharData_CheckXMLChars(&storage, expected);
5204 }
5205 END_TEST
5206
START_TEST(test_entity_public_utf16_be)5207 START_TEST(test_entity_public_utf16_be) {
5208 const char text[] =
5209 /* <!DOCTYPE d [ */
5210 "\0<\0!\0D\0O\0C\0T\0Y\0P\0E\0 \0d\0 \0[\0\n"
5211 /* <!ENTITY % e PUBLIC 'foo' 'bar.ent'> */
5212 "\0<\0!\0E\0N\0T\0I\0T\0Y\0 \0%\0 \0e\0 \0P\0U\0B\0L\0I\0C\0 "
5213 "\0'\0f\0o\0o\0'\0 \0'\0b\0a\0r\0.\0e\0n\0t\0'\0>\0\n"
5214 /* %e; */
5215 "\0%\0e\0;\0\n"
5216 /* ]> */
5217 "\0]\0>\0\n"
5218 /* <d>&j;</d> */
5219 "\0<\0d\0>\0&\0j\0;\0<\0/\0d\0>";
5220 ExtTest2 test_data
5221 = {/* <!ENTITY j 'baz'> */
5222 "\0<\0!\0E\0N\0T\0I\0T\0Y\0 \0j\0 \0'\0b\0a\0z\0'\0>", 34, NULL, NULL};
5223 const XML_Char *expected = XCS("baz");
5224 CharData storage;
5225
5226 CharData_Init(&storage);
5227 test_data.storage = &storage;
5228 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
5229 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader2);
5230 XML_SetUserData(g_parser, &test_data);
5231 XML_SetCharacterDataHandler(g_parser, ext2_accumulate_characters);
5232 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
5233 == XML_STATUS_ERROR)
5234 xml_failure(g_parser);
5235 CharData_CheckXMLChars(&storage, expected);
5236 }
5237 END_TEST
5238
START_TEST(test_entity_public_utf16_le)5239 START_TEST(test_entity_public_utf16_le) {
5240 const char text[] =
5241 /* <!DOCTYPE d [ */
5242 "<\0!\0D\0O\0C\0T\0Y\0P\0E\0 \0d\0 \0[\0\n\0"
5243 /* <!ENTITY % e PUBLIC 'foo' 'bar.ent'> */
5244 "<\0!\0E\0N\0T\0I\0T\0Y\0 \0%\0 \0e\0 \0P\0U\0B\0L\0I\0C\0 \0"
5245 "'\0f\0o\0o\0'\0 \0'\0b\0a\0r\0.\0e\0n\0t\0'\0>\0\n\0"
5246 /* %e; */
5247 "%\0e\0;\0\n\0"
5248 /* ]> */
5249 "]\0>\0\n\0"
5250 /* <d>&j;</d> */
5251 "<\0d\0>\0&\0j\0;\0<\0/\0d\0>\0";
5252 ExtTest2 test_data
5253 = {/* <!ENTITY j 'baz'> */
5254 "<\0!\0E\0N\0T\0I\0T\0Y\0 \0j\0 \0'\0b\0a\0z\0'\0>\0", 34, NULL, NULL};
5255 const XML_Char *expected = XCS("baz");
5256 CharData storage;
5257
5258 CharData_Init(&storage);
5259 test_data.storage = &storage;
5260 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
5261 XML_SetExternalEntityRefHandler(g_parser, external_entity_loader2);
5262 XML_SetUserData(g_parser, &test_data);
5263 XML_SetCharacterDataHandler(g_parser, ext2_accumulate_characters);
5264 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
5265 == XML_STATUS_ERROR)
5266 xml_failure(g_parser);
5267 CharData_CheckXMLChars(&storage, expected);
5268 }
5269 END_TEST
5270
5271 /* Test that a doctype with neither an internal nor external subset is
5272 * faulted
5273 */
START_TEST(test_short_doctype)5274 START_TEST(test_short_doctype) {
5275 const char *text = "<!DOCTYPE doc></doc>";
5276 expect_failure(text, XML_ERROR_INVALID_TOKEN,
5277 "DOCTYPE without subset not rejected");
5278 }
5279 END_TEST
5280
START_TEST(test_short_doctype_2)5281 START_TEST(test_short_doctype_2) {
5282 const char *text = "<!DOCTYPE doc PUBLIC></doc>";
5283 expect_failure(text, XML_ERROR_SYNTAX,
5284 "DOCTYPE without Public ID not rejected");
5285 }
5286 END_TEST
5287
START_TEST(test_short_doctype_3)5288 START_TEST(test_short_doctype_3) {
5289 const char *text = "<!DOCTYPE doc SYSTEM></doc>";
5290 expect_failure(text, XML_ERROR_SYNTAX,
5291 "DOCTYPE without System ID not rejected");
5292 }
5293 END_TEST
5294
START_TEST(test_long_doctype)5295 START_TEST(test_long_doctype) {
5296 const char *text = "<!DOCTYPE doc PUBLIC 'foo' 'bar' 'baz'></doc>";
5297 expect_failure(text, XML_ERROR_SYNTAX, "DOCTYPE with extra ID not rejected");
5298 }
5299 END_TEST
5300
START_TEST(test_bad_entity)5301 START_TEST(test_bad_entity) {
5302 const char *text = "<!DOCTYPE doc [\n"
5303 " <!ENTITY foo PUBLIC>\n"
5304 "]>\n"
5305 "<doc/>";
5306 expect_failure(text, XML_ERROR_SYNTAX,
5307 "ENTITY without Public ID is not rejected");
5308 }
5309 END_TEST
5310
5311 /* Test unquoted value is faulted */
START_TEST(test_bad_entity_2)5312 START_TEST(test_bad_entity_2) {
5313 const char *text = "<!DOCTYPE doc [\n"
5314 " <!ENTITY % foo bar>\n"
5315 "]>\n"
5316 "<doc/>";
5317 expect_failure(text, XML_ERROR_SYNTAX,
5318 "ENTITY without Public ID is not rejected");
5319 }
5320 END_TEST
5321
START_TEST(test_bad_entity_3)5322 START_TEST(test_bad_entity_3) {
5323 const char *text = "<!DOCTYPE doc [\n"
5324 " <!ENTITY % foo PUBLIC>\n"
5325 "]>\n"
5326 "<doc/>";
5327 expect_failure(text, XML_ERROR_SYNTAX,
5328 "Parameter ENTITY without Public ID is not rejected");
5329 }
5330 END_TEST
5331
START_TEST(test_bad_entity_4)5332 START_TEST(test_bad_entity_4) {
5333 const char *text = "<!DOCTYPE doc [\n"
5334 " <!ENTITY % foo SYSTEM>\n"
5335 "]>\n"
5336 "<doc/>";
5337 expect_failure(text, XML_ERROR_SYNTAX,
5338 "Parameter ENTITY without Public ID is not rejected");
5339 }
5340 END_TEST
5341
START_TEST(test_bad_notation)5342 START_TEST(test_bad_notation) {
5343 const char *text = "<!DOCTYPE doc [\n"
5344 " <!NOTATION n SYSTEM>\n"
5345 "]>\n"
5346 "<doc/>";
5347 expect_failure(text, XML_ERROR_SYNTAX,
5348 "Notation without System ID is not rejected");
5349 }
5350 END_TEST
5351
5352 /* Test for issue #11, wrongly suppressed default handler */
START_TEST(test_default_doctype_handler)5353 START_TEST(test_default_doctype_handler) {
5354 const char *text = "<!DOCTYPE doc PUBLIC 'pubname' 'test.dtd' [\n"
5355 " <!ENTITY foo 'bar'>\n"
5356 "]>\n"
5357 "<doc>&foo;</doc>";
5358 DefaultCheck test_data[] = {{XCS("'pubname'"), 9, XML_FALSE},
5359 {XCS("'test.dtd'"), 10, XML_FALSE},
5360 {NULL, 0, XML_FALSE}};
5361 int i;
5362
5363 XML_SetUserData(g_parser, &test_data);
5364 XML_SetDefaultHandler(g_parser, checking_default_handler);
5365 XML_SetEntityDeclHandler(g_parser, dummy_entity_decl_handler);
5366 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
5367 == XML_STATUS_ERROR)
5368 xml_failure(g_parser);
5369 for (i = 0; test_data[i].expected != NULL; i++)
5370 if (! test_data[i].seen)
5371 fail("Default handler not run for public !DOCTYPE");
5372 }
5373 END_TEST
5374
START_TEST(test_empty_element_abort)5375 START_TEST(test_empty_element_abort) {
5376 const char *text = "<abort/>";
5377
5378 XML_SetStartElementHandler(g_parser, start_element_suspender);
5379 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
5380 != XML_STATUS_ERROR)
5381 fail("Expected to error on abort");
5382 }
5383 END_TEST
5384
5385 /* Regression test for GH issue #612: unfinished m_declAttributeType
5386 * allocation in ->m_tempPool can corrupt following allocation.
5387 */
START_TEST(test_pool_integrity_with_unfinished_attr)5388 START_TEST(test_pool_integrity_with_unfinished_attr) {
5389 const char *text = "<?xml version='1.0' encoding='UTF-8'?>\n"
5390 "<!DOCTYPE foo [\n"
5391 "<!ELEMENT foo ANY>\n"
5392 "<!ENTITY % entp SYSTEM \"external.dtd\">\n"
5393 "%entp;\n"
5394 "]>\n"
5395 "<a></a>\n";
5396 const XML_Char *expected = XCS("COMMENT");
5397 CharData storage;
5398
5399 CharData_Init(&storage);
5400 XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
5401 XML_SetExternalEntityRefHandler(g_parser, external_entity_unfinished_attlist);
5402 XML_SetAttlistDeclHandler(g_parser, dummy_attlist_decl_handler);
5403 XML_SetCommentHandler(g_parser, accumulate_comment);
5404 XML_SetUserData(g_parser, &storage);
5405 if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
5406 == XML_STATUS_ERROR)
5407 xml_failure(g_parser);
5408 CharData_CheckXMLChars(&storage, expected);
5409 }
5410 END_TEST
5411
5412 /* Test a possible early return location in internalEntityProcessor */
START_TEST(test_entity_ref_no_elements)5413 START_TEST(test_entity_ref_no_elements) {
5414 const char *const text = "<!DOCTYPE foo [\n"
5415 "<!ENTITY e1 \"test\">\n"
5416 "]> <foo>&e1;"; // intentionally missing newline
5417
5418 XML_Parser parser = XML_ParserCreate(NULL);
5419 assert_true(_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
5420 == XML_STATUS_ERROR);
5421 assert_true(XML_GetErrorCode(parser) == XML_ERROR_NO_ELEMENTS);
5422 XML_ParserFree(parser);
5423 }
5424 END_TEST
5425
5426 /* Tests if chained entity references lead to unbounded recursion */
START_TEST(test_deep_nested_entity)5427 START_TEST(test_deep_nested_entity) {
5428 const size_t N_LINES = 60000;
5429 const size_t SIZE_PER_LINE = 50;
5430
5431 char *const text = (char *)malloc((N_LINES + 4) * SIZE_PER_LINE);
5432 if (text == NULL) {
5433 fail("malloc failed");
5434 }
5435
5436 char *textPtr = text;
5437
5438 // Create the XML
5439 textPtr += snprintf(textPtr, SIZE_PER_LINE,
5440 "<!DOCTYPE foo [\n"
5441 " <!ENTITY s0 'deepText'>\n");
5442
5443 for (size_t i = 1; i < N_LINES; ++i) {
5444 textPtr += snprintf(textPtr, SIZE_PER_LINE, " <!ENTITY s%lu '&s%lu;'>\n",
5445 (long unsigned)i, (long unsigned)(i - 1));
5446 }
5447
5448 snprintf(textPtr, SIZE_PER_LINE, "]> <foo>&s%lu;</foo>\n",
5449 (long unsigned)(N_LINES - 1));
5450
5451 const XML_Char *const expected = XCS("deepText");
5452
5453 CharData storage;
5454 CharData_Init(&storage);
5455
5456 XML_Parser parser = XML_ParserCreate(NULL);
5457
5458 XML_SetCharacterDataHandler(parser, accumulate_characters);
5459 XML_SetUserData(parser, &storage);
5460
5461 if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
5462 == XML_STATUS_ERROR)
5463 xml_failure(parser);
5464
5465 CharData_CheckXMLChars(&storage, expected);
5466 XML_ParserFree(parser);
5467 free(text);
5468 }
5469 END_TEST
5470
5471 /* Tests if chained entity references in attributes
5472 lead to unbounded recursion */
START_TEST(test_deep_nested_attribute_entity)5473 START_TEST(test_deep_nested_attribute_entity) {
5474 const size_t N_LINES = 60000;
5475 const size_t SIZE_PER_LINE = 100;
5476
5477 char *const text = (char *)malloc((N_LINES + 4) * SIZE_PER_LINE);
5478 if (text == NULL) {
5479 fail("malloc failed");
5480 }
5481
5482 char *textPtr = text;
5483
5484 // Create the XML
5485 textPtr += snprintf(textPtr, SIZE_PER_LINE,
5486 "<!DOCTYPE foo [\n"
5487 " <!ENTITY s0 'deepText'>\n");
5488
5489 for (size_t i = 1; i < N_LINES; ++i) {
5490 textPtr += snprintf(textPtr, SIZE_PER_LINE, " <!ENTITY s%lu '&s%lu;'>\n",
5491 (long unsigned)i, (long unsigned)(i - 1));
5492 }
5493
5494 snprintf(textPtr, SIZE_PER_LINE, "]> <foo name='&s%lu;'>mainText</foo>\n",
5495 (long unsigned)(N_LINES - 1));
5496
5497 AttrInfo doc_info[] = {{XCS("name"), XCS("deepText")}, {NULL, NULL}};
5498 ElementInfo info[] = {{XCS("foo"), 1, NULL, NULL}, {NULL, 0, NULL, NULL}};
5499 info[0].attributes = doc_info;
5500
5501 XML_Parser parser = XML_ParserCreate(NULL);
5502 ParserAndElementInfo parserPlusElemenInfo = {parser, info};
5503
5504 XML_SetStartElementHandler(parser, counting_start_element_handler);
5505 XML_SetUserData(parser, &parserPlusElemenInfo);
5506
5507 if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
5508 == XML_STATUS_ERROR)
5509 xml_failure(parser);
5510
5511 XML_ParserFree(parser);
5512 free(text);
5513 }
5514 END_TEST
5515
START_TEST(test_deep_nested_entity_delayed_interpretation)5516 START_TEST(test_deep_nested_entity_delayed_interpretation) {
5517 const size_t N_LINES = 70000;
5518 const size_t SIZE_PER_LINE = 100;
5519
5520 char *const text = (char *)malloc((N_LINES + 4) * SIZE_PER_LINE);
5521 if (text == NULL) {
5522 fail("malloc failed");
5523 }
5524
5525 char *textPtr = text;
5526
5527 // Create the XML
5528 textPtr += snprintf(textPtr, SIZE_PER_LINE,
5529 "<!DOCTYPE foo [\n"
5530 " <!ENTITY %% s0 'deepText'>\n");
5531
5532 for (size_t i = 1; i < N_LINES; ++i) {
5533 textPtr += snprintf(textPtr, SIZE_PER_LINE,
5534 " <!ENTITY %% s%lu '%s%lu;'>\n", (long unsigned)i,
5535 (long unsigned)(i - 1));
5536 }
5537
5538 snprintf(textPtr, SIZE_PER_LINE,
5539 " <!ENTITY %% define_g \"<!ENTITY g '%s%lu;'>\">\n"
5540 " %%define_g;\n"
5541 "]>\n"
5542 "<foo/>\n",
5543 (long unsigned)(N_LINES - 1));
5544
5545 XML_Parser parser = XML_ParserCreate(NULL);
5546
5547 XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
5548 if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
5549 == XML_STATUS_ERROR)
5550 xml_failure(parser);
5551
5552 XML_ParserFree(parser);
5553 free(text);
5554 }
5555 END_TEST
5556
START_TEST(test_nested_entity_suspend)5557 START_TEST(test_nested_entity_suspend) {
5558 const char *const text = "<!DOCTYPE a [\n"
5559 " <!ENTITY e1 '<!--e1-->'>\n"
5560 " <!ENTITY e2 '<!--e2 head-->&e1;<!--e2 tail-->'>\n"
5561 " <!ENTITY e3 '<!--e3 head-->&e2;<!--e3 tail-->'>\n"
5562 "]>\n"
5563 "<a><!--start-->&e3;<!--end--></a>";
5564 const XML_Char *const expected = XCS("start") XCS("e3 head") XCS("e2 head")
5565 XCS("e1") XCS("e2 tail") XCS("e3 tail") XCS("end");
5566 CharData storage;
5567 CharData_Init(&storage);
5568 XML_Parser parser = XML_ParserCreate(NULL);
5569 ParserPlusStorage parserPlusStorage = {parser, &storage};
5570
5571 XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
5572 XML_SetCommentHandler(parser, accumulate_and_suspend_comment_handler);
5573 XML_SetUserData(parser, &parserPlusStorage);
5574
5575 enum XML_Status status = XML_Parse(parser, text, (int)strlen(text), XML_TRUE);
5576 while (status == XML_STATUS_SUSPENDED) {
5577 status = XML_ResumeParser(parser);
5578 }
5579 if (status != XML_STATUS_OK)
5580 xml_failure(parser);
5581
5582 CharData_CheckXMLChars(&storage, expected);
5583 XML_ParserFree(parser);
5584 }
5585 END_TEST
5586
START_TEST(test_nested_entity_suspend_2)5587 START_TEST(test_nested_entity_suspend_2) {
5588 const char *const text = "<!DOCTYPE doc [\n"
5589 " <!ENTITY ge1 'head1Ztail1'>\n"
5590 " <!ENTITY ge2 'head2&ge1;tail2'>\n"
5591 " <!ENTITY ge3 'head3&ge2;tail3'>\n"
5592 "]>\n"
5593 "<doc>&ge3;</doc>";
5594 const XML_Char *const expected = XCS("head3") XCS("head2") XCS("head1")
5595 XCS("Z") XCS("tail1") XCS("tail2") XCS("tail3");
5596 CharData storage;
5597 CharData_Init(&storage);
5598 XML_Parser parser = XML_ParserCreate(NULL);
5599 ParserPlusStorage parserPlusStorage = {parser, &storage};
5600
5601 XML_SetCharacterDataHandler(parser, accumulate_char_data_and_suspend);
5602 XML_SetUserData(parser, &parserPlusStorage);
5603
5604 enum XML_Status status = XML_Parse(parser, text, (int)strlen(text), XML_TRUE);
5605 while (status == XML_STATUS_SUSPENDED) {
5606 status = XML_ResumeParser(parser);
5607 }
5608 if (status != XML_STATUS_OK)
5609 xml_failure(parser);
5610
5611 CharData_CheckXMLChars(&storage, expected);
5612 XML_ParserFree(parser);
5613 }
5614 END_TEST
5615
5616 /* Regression test for quadratic parsing on large tokens */
START_TEST(test_big_tokens_scale_linearly)5617 START_TEST(test_big_tokens_scale_linearly) {
5618 const struct {
5619 const char *pre;
5620 const char *post;
5621 } text[] = {
5622 {"<a>", "</a>"}, // assumed good, used as baseline
5623 {"<b><![CDATA[ value: ", " ]]></b>"}, // CDATA, performed OK before patch
5624 {"<c attr='", "'></c>"}, // big attribute, used to be O(N²)
5625 {"<d><!-- ", " --></d>"}, // long comment, used to be O(N²)
5626 {"<e><", "/></e>"}, // big elem name, used to be O(N²)
5627 };
5628 const int num_cases = sizeof(text) / sizeof(text[0]);
5629 char aaaaaa[4096];
5630 const int fillsize = (int)sizeof(aaaaaa);
5631 const int fillcount = 100;
5632 const unsigned approx_bytes = fillsize * fillcount; // ignore pre/post.
5633 const unsigned max_factor = 4;
5634 const unsigned max_scanned = max_factor * approx_bytes;
5635
5636 memset(aaaaaa, 'a', fillsize);
5637
5638 if (! g_reparseDeferralEnabledDefault) {
5639 return; // heuristic is disabled; we would get O(n^2) and fail.
5640 }
5641
5642 for (int i = 0; i < num_cases; ++i) {
5643 XML_Parser parser = XML_ParserCreate(NULL);
5644 assert_true(parser != NULL);
5645 enum XML_Status status;
5646 set_subtest("text=\"%saaaaaa%s\"", text[i].pre, text[i].post);
5647
5648 // parse the start text
5649 g_bytesScanned = 0;
5650 status = _XML_Parse_SINGLE_BYTES(parser, text[i].pre,
5651 (int)strlen(text[i].pre), XML_FALSE);
5652 if (status != XML_STATUS_OK) {
5653 xml_failure(parser);
5654 }
5655
5656 // parse lots of 'a', failing the test early if it takes too long
5657 unsigned past_max_count = 0;
5658 for (int f = 0; f < fillcount; ++f) {
5659 status = _XML_Parse_SINGLE_BYTES(parser, aaaaaa, fillsize, XML_FALSE);
5660 if (status != XML_STATUS_OK) {
5661 xml_failure(parser);
5662 }
5663 if (g_bytesScanned > max_scanned) {
5664 // We're not done, and have already passed the limit -- the test will
5665 // definitely fail. This block allows us to save time by failing early.
5666 const unsigned pushed
5667 = (unsigned)strlen(text[i].pre) + (f + 1) * fillsize;
5668 fprintf(
5669 stderr,
5670 "after %d/%d loops: pushed=%u scanned=%u (factor ~%.2f) max_scanned: %u (factor ~%u)\n",
5671 f + 1, fillcount, pushed, g_bytesScanned,
5672 g_bytesScanned / (double)pushed, max_scanned, max_factor);
5673 past_max_count++;
5674 // We are failing, but allow a few log prints first. If we don't reach
5675 // a count of five, the test will fail after the loop instead.
5676 assert_true(past_max_count < 5);
5677 }
5678 }
5679
5680 // parse the end text
5681 status = _XML_Parse_SINGLE_BYTES(parser, text[i].post,
5682 (int)strlen(text[i].post), XML_TRUE);
5683 if (status != XML_STATUS_OK) {
5684 xml_failure(parser);
5685 }
5686
5687 assert_true(g_bytesScanned > approx_bytes); // or the counter isn't working
5688 if (g_bytesScanned > max_scanned) {
5689 fprintf(
5690 stderr,
5691 "after all input: scanned=%u (factor ~%.2f) max_scanned: %u (factor ~%u)\n",
5692 g_bytesScanned, g_bytesScanned / (double)approx_bytes, max_scanned,
5693 max_factor);
5694 fail("scanned too many bytes");
5695 }
5696
5697 XML_ParserFree(parser);
5698 }
5699 }
5700 END_TEST
5701
START_TEST(test_set_reparse_deferral)5702 START_TEST(test_set_reparse_deferral) {
5703 const char *const pre = "<d>";
5704 const char *const start = "<x attr='";
5705 const char *const end = "'></x>";
5706 char eeeeee[100];
5707 const int fillsize = (int)sizeof(eeeeee);
5708 memset(eeeeee, 'e', fillsize);
5709
5710 for (int enabled = 0; enabled <= 1; enabled += 1) {
5711 set_subtest("deferral=%d", enabled);
5712
5713 XML_Parser parser = XML_ParserCreate(NULL);
5714 assert_true(parser != NULL);
5715 assert_true(XML_SetReparseDeferralEnabled(parser, enabled));
5716 // pre-grow the buffer to avoid reparsing due to almost-fullness
5717 assert_true(XML_GetBuffer(parser, fillsize * 10103) != NULL);
5718
5719 CharData storage;
5720 CharData_Init(&storage);
5721 XML_SetUserData(parser, &storage);
5722 XML_SetStartElementHandler(parser, start_element_event_handler);
5723
5724 enum XML_Status status;
5725 // parse the start text
5726 status = XML_Parse(parser, pre, (int)strlen(pre), XML_FALSE);
5727 if (status != XML_STATUS_OK) {
5728 xml_failure(parser);
5729 }
5730 CharData_CheckXMLChars(&storage, XCS("d")); // first element should be done
5731
5732 // ..and the start of the token
5733 status = XML_Parse(parser, start, (int)strlen(start), XML_FALSE);
5734 if (status != XML_STATUS_OK) {
5735 xml_failure(parser);
5736 }
5737 CharData_CheckXMLChars(&storage, XCS("d")); // still just the first one
5738
5739 // try to parse lots of 'e', but the token isn't finished
5740 for (int c = 0; c < 100; ++c) {
5741 status = XML_Parse(parser, eeeeee, fillsize, XML_FALSE);
5742 if (status != XML_STATUS_OK) {
5743 xml_failure(parser);
5744 }
5745 }
5746 CharData_CheckXMLChars(&storage, XCS("d")); // *still* just the first one
5747
5748 // end the <x> token.
5749 status = XML_Parse(parser, end, (int)strlen(end), XML_FALSE);
5750 if (status != XML_STATUS_OK) {
5751 xml_failure(parser);
5752 }
5753
5754 if (enabled) {
5755 // In general, we may need to push more data to trigger a reparse attempt,
5756 // but in this test, the data is constructed to always require it.
5757 CharData_CheckXMLChars(&storage, XCS("d")); // or the test is incorrect
5758 // 2x the token length should suffice; the +1 covers the start and end.
5759 for (int c = 0; c < 101; ++c) {
5760 status = XML_Parse(parser, eeeeee, fillsize, XML_FALSE);
5761 if (status != XML_STATUS_OK) {
5762 xml_failure(parser);
5763 }
5764 }
5765 }
5766 CharData_CheckXMLChars(&storage, XCS("dx")); // the <x> should be done
5767
5768 XML_ParserFree(parser);
5769 }
5770 }
5771 END_TEST
5772
5773 struct element_decl_data {
5774 XML_Parser parser;
5775 int count;
5776 };
5777
5778 static void
element_decl_counter(void * userData,const XML_Char * name,XML_Content * model)5779 element_decl_counter(void *userData, const XML_Char *name, XML_Content *model) {
5780 UNUSED_P(name);
5781 struct element_decl_data *testdata = (struct element_decl_data *)userData;
5782 testdata->count += 1;
5783 XML_FreeContentModel(testdata->parser, model);
5784 }
5785
5786 static int
external_inherited_parser(XML_Parser p,const XML_Char * context,const XML_Char * base,const XML_Char * systemId,const XML_Char * publicId)5787 external_inherited_parser(XML_Parser p, const XML_Char *context,
5788 const XML_Char *base, const XML_Char *systemId,
5789 const XML_Char *publicId) {
5790 UNUSED_P(base);
5791 UNUSED_P(systemId);
5792 UNUSED_P(publicId);
5793 const char *const pre = "<!ELEMENT document ANY>\n";
5794 const char *const start = "<!ELEMENT ";
5795 const char *const end = " ANY>\n";
5796 const char *const post = "<!ELEMENT xyz ANY>\n";
5797 const int enabled = *(int *)XML_GetUserData(p);
5798 char eeeeee[100];
5799 char spaces[100];
5800 const int fillsize = (int)sizeof(eeeeee);
5801 assert_true(fillsize == (int)sizeof(spaces));
5802 memset(eeeeee, 'e', fillsize);
5803 memset(spaces, ' ', fillsize);
5804
5805 XML_Parser parser = XML_ExternalEntityParserCreate(p, context, NULL);
5806 assert_true(parser != NULL);
5807 // pre-grow the buffer to avoid reparsing due to almost-fullness
5808 assert_true(XML_GetBuffer(parser, fillsize * 10103) != NULL);
5809
5810 struct element_decl_data testdata;
5811 testdata.parser = parser;
5812 testdata.count = 0;
5813 XML_SetUserData(parser, &testdata);
5814 XML_SetElementDeclHandler(parser, element_decl_counter);
5815
5816 enum XML_Status status;
5817 // parse the initial text
5818 status = XML_Parse(parser, pre, (int)strlen(pre), XML_FALSE);
5819 if (status != XML_STATUS_OK) {
5820 xml_failure(parser);
5821 }
5822 assert_true(testdata.count == 1); // first element should be done
5823
5824 // ..and the start of the big token
5825 status = XML_Parse(parser, start, (int)strlen(start), XML_FALSE);
5826 if (status != XML_STATUS_OK) {
5827 xml_failure(parser);
5828 }
5829 assert_true(testdata.count == 1); // still just the first one
5830
5831 // try to parse lots of 'e', but the token isn't finished
5832 for (int c = 0; c < 100; ++c) {
5833 status = XML_Parse(parser, eeeeee, fillsize, XML_FALSE);
5834 if (status != XML_STATUS_OK) {
5835 xml_failure(parser);
5836 }
5837 }
5838 assert_true(testdata.count == 1); // *still* just the first one
5839
5840 // end the big token.
5841 status = XML_Parse(parser, end, (int)strlen(end), XML_FALSE);
5842 if (status != XML_STATUS_OK) {
5843 xml_failure(parser);
5844 }
5845
5846 if (enabled) {
5847 // In general, we may need to push more data to trigger a reparse attempt,
5848 // but in this test, the data is constructed to always require it.
5849 assert_true(testdata.count == 1); // or the test is incorrect
5850 // 2x the token length should suffice; the +1 covers the start and end.
5851 for (int c = 0; c < 101; ++c) {
5852 status = XML_Parse(parser, spaces, fillsize, XML_FALSE);
5853 if (status != XML_STATUS_OK) {
5854 xml_failure(parser);
5855 }
5856 }
5857 }
5858 assert_true(testdata.count == 2); // the big token should be done
5859
5860 // parse the final text
5861 status = XML_Parse(parser, post, (int)strlen(post), XML_TRUE);
5862 if (status != XML_STATUS_OK) {
5863 xml_failure(parser);
5864 }
5865 assert_true(testdata.count == 3); // after isFinal=XML_TRUE, all must be done
5866
5867 XML_ParserFree(parser);
5868 return XML_STATUS_OK;
5869 }
5870
START_TEST(test_reparse_deferral_is_inherited)5871 START_TEST(test_reparse_deferral_is_inherited) {
5872 const char *const text
5873 = "<!DOCTYPE document SYSTEM 'something.ext'><document/>";
5874 for (int enabled = 0; enabled <= 1; ++enabled) {
5875 set_subtest("deferral=%d", enabled);
5876
5877 XML_Parser parser = XML_ParserCreate(NULL);
5878 assert_true(parser != NULL);
5879 XML_SetUserData(parser, (void *)&enabled);
5880 XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
5881 // this handler creates a sub-parser and checks that its deferral behavior
5882 // is what we expected, based on the value of `enabled` (in userdata).
5883 XML_SetExternalEntityRefHandler(parser, external_inherited_parser);
5884 assert_true(XML_SetReparseDeferralEnabled(parser, enabled));
5885 if (XML_Parse(parser, text, (int)strlen(text), XML_TRUE) != XML_STATUS_OK)
5886 xml_failure(parser);
5887
5888 XML_ParserFree(parser);
5889 }
5890 }
5891 END_TEST
5892
START_TEST(test_set_reparse_deferral_on_null_parser)5893 START_TEST(test_set_reparse_deferral_on_null_parser) {
5894 assert_true(XML_SetReparseDeferralEnabled(NULL, 0) == XML_FALSE);
5895 assert_true(XML_SetReparseDeferralEnabled(NULL, 1) == XML_FALSE);
5896 assert_true(XML_SetReparseDeferralEnabled(NULL, 10) == XML_FALSE);
5897 assert_true(XML_SetReparseDeferralEnabled(NULL, 100) == XML_FALSE);
5898 assert_true(XML_SetReparseDeferralEnabled(NULL, (XML_Bool)INT_MIN)
5899 == XML_FALSE);
5900 assert_true(XML_SetReparseDeferralEnabled(NULL, (XML_Bool)INT_MAX)
5901 == XML_FALSE);
5902 }
5903 END_TEST
5904
START_TEST(test_set_reparse_deferral_on_the_fly)5905 START_TEST(test_set_reparse_deferral_on_the_fly) {
5906 const char *const pre = "<d><x attr='";
5907 const char *const end = "'></x>";
5908 char iiiiii[100];
5909 const int fillsize = (int)sizeof(iiiiii);
5910 memset(iiiiii, 'i', fillsize);
5911
5912 XML_Parser parser = XML_ParserCreate(NULL);
5913 assert_true(parser != NULL);
5914 assert_true(XML_SetReparseDeferralEnabled(parser, XML_TRUE));
5915
5916 CharData storage;
5917 CharData_Init(&storage);
5918 XML_SetUserData(parser, &storage);
5919 XML_SetStartElementHandler(parser, start_element_event_handler);
5920
5921 enum XML_Status status;
5922 // parse the start text
5923 status = XML_Parse(parser, pre, (int)strlen(pre), XML_FALSE);
5924 if (status != XML_STATUS_OK) {
5925 xml_failure(parser);
5926 }
5927 CharData_CheckXMLChars(&storage, XCS("d")); // first element should be done
5928
5929 // try to parse some 'i', but the token isn't finished
5930 status = XML_Parse(parser, iiiiii, fillsize, XML_FALSE);
5931 if (status != XML_STATUS_OK) {
5932 xml_failure(parser);
5933 }
5934 CharData_CheckXMLChars(&storage, XCS("d")); // *still* just the first one
5935
5936 // end the <x> token.
5937 status = XML_Parse(parser, end, (int)strlen(end), XML_FALSE);
5938 if (status != XML_STATUS_OK) {
5939 xml_failure(parser);
5940 }
5941 CharData_CheckXMLChars(&storage, XCS("d")); // not yet.
5942
5943 // now change the heuristic setting and add *no* data
5944 assert_true(XML_SetReparseDeferralEnabled(parser, XML_FALSE));
5945 // we avoid isFinal=XML_TRUE, because that would force-bypass the heuristic.
5946 status = XML_Parse(parser, "", 0, XML_FALSE);
5947 if (status != XML_STATUS_OK) {
5948 xml_failure(parser);
5949 }
5950 CharData_CheckXMLChars(&storage, XCS("dx"));
5951
5952 XML_ParserFree(parser);
5953 }
5954 END_TEST
5955
START_TEST(test_set_bad_reparse_option)5956 START_TEST(test_set_bad_reparse_option) {
5957 XML_Parser parser = XML_ParserCreate(NULL);
5958 assert_true(XML_FALSE == XML_SetReparseDeferralEnabled(parser, 2));
5959 assert_true(XML_FALSE == XML_SetReparseDeferralEnabled(parser, 3));
5960 assert_true(XML_FALSE == XML_SetReparseDeferralEnabled(parser, 99));
5961 assert_true(XML_FALSE == XML_SetReparseDeferralEnabled(parser, 127));
5962 assert_true(XML_FALSE == XML_SetReparseDeferralEnabled(parser, 128));
5963 assert_true(XML_FALSE == XML_SetReparseDeferralEnabled(parser, 129));
5964 assert_true(XML_FALSE == XML_SetReparseDeferralEnabled(parser, 255));
5965 assert_true(XML_TRUE == XML_SetReparseDeferralEnabled(parser, 0));
5966 assert_true(XML_TRUE == XML_SetReparseDeferralEnabled(parser, 1));
5967 XML_ParserFree(parser);
5968 }
5969 END_TEST
5970
5971 static size_t g_totalAlloc = 0;
5972 static size_t g_biggestAlloc = 0;
5973
5974 static void *
counting_realloc(void * ptr,size_t size)5975 counting_realloc(void *ptr, size_t size) {
5976 g_totalAlloc += size;
5977 if (size > g_biggestAlloc) {
5978 g_biggestAlloc = size;
5979 }
5980 return realloc(ptr, size);
5981 }
5982
5983 static void *
counting_malloc(size_t size)5984 counting_malloc(size_t size) {
5985 return counting_realloc(NULL, size);
5986 }
5987
START_TEST(test_bypass_heuristic_when_close_to_bufsize)5988 START_TEST(test_bypass_heuristic_when_close_to_bufsize) {
5989 if (g_chunkSize != 0) {
5990 // this test does not use SINGLE_BYTES, because it depends on very precise
5991 // buffer fills.
5992 return;
5993 }
5994 if (! g_reparseDeferralEnabledDefault) {
5995 return; // this test is irrelevant when the deferral heuristic is disabled.
5996 }
5997
5998 const int document_length = 65536;
5999 char *const document = (char *)malloc(document_length);
6000
6001 const XML_Memory_Handling_Suite memfuncs = {
6002 counting_malloc,
6003 counting_realloc,
6004 free,
6005 };
6006
6007 const int leading_list[] = {0, 3, 61, 96, 400, 401, 4000, 4010, 4099, -1};
6008 const int bigtoken_list[] = {3000, 4000, 4001, 4096, 4099, 5000, 20000, -1};
6009 const int fillsize_list[] = {131, 256, 399, 400, 401, 1025, 4099, 4321, -1};
6010
6011 for (const int *leading = leading_list; *leading >= 0; leading++) {
6012 for (const int *bigtoken = bigtoken_list; *bigtoken >= 0; bigtoken++) {
6013 for (const int *fillsize = fillsize_list; *fillsize >= 0; fillsize++) {
6014 set_subtest("leading=%d bigtoken=%d fillsize=%d", *leading, *bigtoken,
6015 *fillsize);
6016 // start by checking that the test looks reasonably valid
6017 assert_true(*leading + *bigtoken <= document_length);
6018
6019 // put 'x' everywhere; some will be overwritten by elements.
6020 memset(document, 'x', document_length);
6021 // maybe add an initial tag
6022 if (*leading) {
6023 assert_true(*leading >= 3); // or the test case is invalid
6024 memcpy(document, "<a>", 3);
6025 }
6026 // add the large token
6027 document[*leading + 0] = '<';
6028 document[*leading + 1] = 'b';
6029 memset(&document[*leading + 2], ' ', *bigtoken - 2); // a spacy token
6030 document[*leading + *bigtoken - 1] = '>';
6031
6032 // 1 for 'b', plus 1 or 0 depending on the presence of 'a'
6033 const int expected_elem_total = 1 + (*leading ? 1 : 0);
6034
6035 XML_Parser parser = XML_ParserCreate_MM(NULL, &memfuncs, NULL);
6036 assert_true(parser != NULL);
6037
6038 CharData storage;
6039 CharData_Init(&storage);
6040 XML_SetUserData(parser, &storage);
6041 XML_SetStartElementHandler(parser, start_element_event_handler);
6042
6043 g_biggestAlloc = 0;
6044 g_totalAlloc = 0;
6045 int offset = 0;
6046 // fill data until the big token is covered (but not necessarily parsed)
6047 while (offset < *leading + *bigtoken) {
6048 assert_true(offset + *fillsize <= document_length);
6049 const enum XML_Status status
6050 = XML_Parse(parser, &document[offset], *fillsize, XML_FALSE);
6051 if (status != XML_STATUS_OK) {
6052 xml_failure(parser);
6053 }
6054 offset += *fillsize;
6055 }
6056 // Now, check that we've had a buffer allocation that could fit the
6057 // context bytes and our big token. In order to detect a special case,
6058 // we need to know how many bytes of our big token were included in the
6059 // first push that contained _any_ bytes of the big token:
6060 const int bigtok_first_chunk_bytes = *fillsize - (*leading % *fillsize);
6061 if (bigtok_first_chunk_bytes >= *bigtoken && XML_CONTEXT_BYTES == 0) {
6062 // Special case: we aren't saving any context, and the whole big token
6063 // was covered by a single fill, so Expat may have parsed directly
6064 // from our input pointer, without allocating an internal buffer.
6065 } else if (*leading < XML_CONTEXT_BYTES) {
6066 assert_true(g_biggestAlloc >= *leading + (size_t)*bigtoken);
6067 } else {
6068 assert_true(g_biggestAlloc >= XML_CONTEXT_BYTES + (size_t)*bigtoken);
6069 }
6070 // fill data until the big token is actually parsed
6071 while (storage.count < expected_elem_total) {
6072 const size_t alloc_before = g_totalAlloc;
6073 assert_true(offset + *fillsize <= document_length);
6074 const enum XML_Status status
6075 = XML_Parse(parser, &document[offset], *fillsize, XML_FALSE);
6076 if (status != XML_STATUS_OK) {
6077 xml_failure(parser);
6078 }
6079 offset += *fillsize;
6080 // since all the bytes of the big token are already in the buffer,
6081 // the bufsize ceiling should make us finish its parsing without any
6082 // further buffer allocations. We assume that there will be no other
6083 // large allocations in this test.
6084 assert_true(g_totalAlloc - alloc_before < 4096);
6085 }
6086 // test-the-test: was our alloc even called?
6087 assert_true(g_totalAlloc > 0);
6088 // test-the-test: there shouldn't be any extra start elements
6089 assert_true(storage.count == expected_elem_total);
6090
6091 XML_ParserFree(parser);
6092 }
6093 }
6094 }
6095 free(document);
6096 }
6097 END_TEST
6098
START_TEST(test_varying_buffer_fills)6099 START_TEST(test_varying_buffer_fills) {
6100 const int KiB = 1024;
6101 const int MiB = 1024 * KiB;
6102 const int document_length = 16 * MiB;
6103 const int big = 7654321; // arbitrarily chosen between 4 and 8 MiB
6104
6105 if (g_chunkSize != 0) {
6106 return; // this test is slow, and doesn't use _XML_Parse_SINGLE_BYTES().
6107 }
6108
6109 char *const document = (char *)malloc(document_length);
6110 assert_true(document != NULL);
6111 memset(document, 'x', document_length);
6112 document[0] = '<';
6113 document[1] = 't';
6114 memset(&document[2], ' ', big - 2); // a very spacy token
6115 document[big - 1] = '>';
6116
6117 // Each testcase is a list of buffer fill sizes, terminated by a value < 0.
6118 // When reparse deferral is enabled, the final (negated) value is the expected
6119 // maximum number of bytes scanned in parse attempts.
6120 const int testcases[][30] = {
6121 {8 * MiB, -8 * MiB},
6122 {4 * MiB, 4 * MiB, -12 * MiB}, // try at 4MB, then 8MB = 12 MB total
6123 // zero-size fills shouldn't trigger the bypass
6124 {4 * MiB, 0, 4 * MiB, -12 * MiB},
6125 {4 * MiB, 0, 0, 4 * MiB, -12 * MiB},
6126 {4 * MiB, 0, 1 * MiB, 0, 3 * MiB, -12 * MiB},
6127 // try to hit the buffer ceiling only once (at the end)
6128 {4 * MiB, 2 * MiB, 1 * MiB, 512 * KiB, 256 * KiB, 256 * KiB, -12 * MiB},
6129 // try to hit the same buffer ceiling multiple times
6130 {4 * MiB + 1, 2 * MiB, 1 * MiB, 512 * KiB, -25 * MiB},
6131
6132 // try to hit every ceiling, by always landing 1K shy of the buffer size
6133 {1 * KiB, 2 * KiB, 4 * KiB, 8 * KiB, 16 * KiB, 32 * KiB, 64 * KiB,
6134 128 * KiB, 256 * KiB, 512 * KiB, 1 * MiB, 2 * MiB, 4 * MiB, -16 * MiB},
6135
6136 // try to avoid every ceiling, by always landing 1B past the buffer size
6137 // the normal 2x heuristic threshold still forces parse attempts.
6138 {2 * KiB + 1, // will attempt 2KiB + 1 ==> total 2KiB + 1
6139 2 * KiB, 4 * KiB, // will attempt 8KiB + 1 ==> total 10KiB + 2
6140 8 * KiB, 16 * KiB, // will attempt 32KiB + 1 ==> total 42KiB + 3
6141 32 * KiB, 64 * KiB, // will attempt 128KiB + 1 ==> total 170KiB + 4
6142 128 * KiB, 256 * KiB, // will attempt 512KiB + 1 ==> total 682KiB + 5
6143 512 * KiB, 1 * MiB, // will attempt 2MiB + 1 ==> total 2M + 682K + 6
6144 2 * MiB, 4 * MiB, // will attempt 8MiB + 1 ==> total 10M + 682K + 7
6145 -(10 * MiB + 682 * KiB + 7)},
6146 // try to avoid every ceiling again, except on our last fill.
6147 {2 * KiB + 1, // will attempt 2KiB + 1 ==> total 2KiB + 1
6148 2 * KiB, 4 * KiB, // will attempt 8KiB + 1 ==> total 10KiB + 2
6149 8 * KiB, 16 * KiB, // will attempt 32KiB + 1 ==> total 42KiB + 3
6150 32 * KiB, 64 * KiB, // will attempt 128KiB + 1 ==> total 170KiB + 4
6151 128 * KiB, 256 * KiB, // will attempt 512KiB + 1 ==> total 682KiB + 5
6152 512 * KiB, 1 * MiB, // will attempt 2MiB + 1 ==> total 2M + 682K + 6
6153 2 * MiB, 4 * MiB - 1, // will attempt 8MiB ==> total 10M + 682K + 6
6154 -(10 * MiB + 682 * KiB + 6)},
6155
6156 // try to hit ceilings on the way multiple times
6157 {512 * KiB + 1, 256 * KiB, 128 * KiB, 128 * KiB - 1, // 1 MiB buffer
6158 512 * KiB + 1, 256 * KiB, 128 * KiB, 128 * KiB - 1, // 2 MiB buffer
6159 1 * MiB + 1, 512 * KiB, 256 * KiB, 256 * KiB - 1, // 4 MiB buffer
6160 2 * MiB + 1, 1 * MiB, 512 * KiB, // 8 MiB buffer
6161 // we'll make a parse attempt at every parse call
6162 -(45 * MiB + 12)},
6163 };
6164 const int testcount = sizeof(testcases) / sizeof(testcases[0]);
6165 for (int test_i = 0; test_i < testcount; test_i++) {
6166 const int *fillsize = testcases[test_i];
6167 set_subtest("#%d {%d %d %d %d ...}", test_i, fillsize[0], fillsize[1],
6168 fillsize[2], fillsize[3]);
6169 XML_Parser parser = XML_ParserCreate(NULL);
6170 assert_true(parser != NULL);
6171
6172 CharData storage;
6173 CharData_Init(&storage);
6174 XML_SetUserData(parser, &storage);
6175 XML_SetStartElementHandler(parser, start_element_event_handler);
6176
6177 g_bytesScanned = 0;
6178 int worstcase_bytes = 0; // sum of (buffered bytes at each XML_Parse call)
6179 int offset = 0;
6180 while (*fillsize >= 0) {
6181 assert_true(offset + *fillsize <= document_length); // or test is invalid
6182 const enum XML_Status status
6183 = XML_Parse(parser, &document[offset], *fillsize, XML_FALSE);
6184 if (status != XML_STATUS_OK) {
6185 xml_failure(parser);
6186 }
6187 offset += *fillsize;
6188 fillsize++;
6189 assert_true(offset <= INT_MAX - worstcase_bytes); // avoid overflow
6190 worstcase_bytes += offset; // we might've tried to parse all pending bytes
6191 }
6192 assert_true(storage.count == 1); // the big token should've been parsed
6193 assert_true(g_bytesScanned > 0); // test-the-test: does our counter work?
6194 if (g_reparseDeferralEnabledDefault) {
6195 // heuristic is enabled; some XML_Parse calls may have deferred reparsing
6196 const unsigned max_bytes_scanned = -*fillsize;
6197 if (g_bytesScanned > max_bytes_scanned) {
6198 fprintf(stderr,
6199 "bytes scanned in parse attempts: actual=%u limit=%u \n",
6200 g_bytesScanned, max_bytes_scanned);
6201 fail("too many bytes scanned in parse attempts");
6202 }
6203 }
6204 assert_true(g_bytesScanned <= (unsigned)worstcase_bytes);
6205
6206 XML_ParserFree(parser);
6207 }
6208 free(document);
6209 }
6210 END_TEST
6211
6212 void
make_basic_test_case(Suite * s)6213 make_basic_test_case(Suite *s) {
6214 TCase *tc_basic = tcase_create("basic tests");
6215
6216 suite_add_tcase(s, tc_basic);
6217 tcase_add_checked_fixture(tc_basic, basic_setup, basic_teardown);
6218
6219 tcase_add_test(tc_basic, test_nul_byte);
6220 tcase_add_test(tc_basic, test_u0000_char);
6221 tcase_add_test(tc_basic, test_siphash_self);
6222 tcase_add_test(tc_basic, test_siphash_spec);
6223 tcase_add_test(tc_basic, test_bom_utf8);
6224 tcase_add_test(tc_basic, test_bom_utf16_be);
6225 tcase_add_test(tc_basic, test_bom_utf16_le);
6226 tcase_add_test(tc_basic, test_nobom_utf16_le);
6227 tcase_add_test(tc_basic, test_hash_collision);
6228 tcase_add_test(tc_basic, test_illegal_utf8);
6229 tcase_add_test(tc_basic, test_utf8_auto_align);
6230 tcase_add_test(tc_basic, test_utf16);
6231 tcase_add_test(tc_basic, test_utf16_le_epilog_newline);
6232 tcase_add_test(tc_basic, test_not_utf16);
6233 tcase_add_test(tc_basic, test_bad_encoding);
6234 tcase_add_test(tc_basic, test_latin1_umlauts);
6235 tcase_add_test(tc_basic, test_long_utf8_character);
6236 tcase_add_test(tc_basic, test_long_latin1_attribute);
6237 tcase_add_test(tc_basic, test_long_ascii_attribute);
6238 /* Regression test for SF bug #491986. */
6239 tcase_add_test(tc_basic, test_danish_latin1);
6240 /* Regression test for SF bug #514281. */
6241 tcase_add_test(tc_basic, test_french_charref_hexidecimal);
6242 tcase_add_test(tc_basic, test_french_charref_decimal);
6243 tcase_add_test(tc_basic, test_french_latin1);
6244 tcase_add_test(tc_basic, test_french_utf8);
6245 tcase_add_test(tc_basic, test_utf8_false_rejection);
6246 tcase_add_test(tc_basic, test_line_number_after_parse);
6247 tcase_add_test(tc_basic, test_column_number_after_parse);
6248 tcase_add_test(tc_basic, test_line_and_column_numbers_inside_handlers);
6249 tcase_add_test(tc_basic, test_line_number_after_error);
6250 tcase_add_test(tc_basic, test_column_number_after_error);
6251 tcase_add_test(tc_basic, test_really_long_lines);
6252 tcase_add_test(tc_basic, test_really_long_encoded_lines);
6253 tcase_add_test(tc_basic, test_end_element_events);
6254 tcase_add_test(tc_basic, test_helper_is_whitespace_normalized);
6255 tcase_add_test(tc_basic, test_attr_whitespace_normalization);
6256 tcase_add_test(tc_basic, test_xmldecl_misplaced);
6257 tcase_add_test(tc_basic, test_xmldecl_invalid);
6258 tcase_add_test(tc_basic, test_xmldecl_missing_attr);
6259 tcase_add_test(tc_basic, test_xmldecl_missing_value);
6260 tcase_add_test__if_xml_ge(tc_basic, test_unknown_encoding_internal_entity);
6261 tcase_add_test(tc_basic, test_unrecognised_encoding_internal_entity);
6262 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_set_encoding);
6263 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_no_handler);
6264 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_set_bom);
6265 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_bad_encoding);
6266 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_bad_encoding_2);
6267 tcase_add_test(tc_basic, test_wfc_undeclared_entity_unread_external_subset);
6268 tcase_add_test(tc_basic, test_wfc_undeclared_entity_no_external_subset);
6269 tcase_add_test(tc_basic, test_wfc_undeclared_entity_standalone);
6270 tcase_add_test(tc_basic,
6271 test_wfc_undeclared_entity_with_external_subset_standalone);
6272 tcase_add_test(tc_basic, test_entity_with_external_subset_unless_standalone);
6273 tcase_add_test(tc_basic, test_wfc_undeclared_entity_with_external_subset);
6274 tcase_add_test(tc_basic, test_not_standalone_handler_reject);
6275 tcase_add_test(tc_basic, test_not_standalone_handler_accept);
6276 tcase_add_test(tc_basic, test_entity_start_tag_level_greater_than_one);
6277 tcase_add_test__if_xml_ge(tc_basic, test_wfc_no_recursive_entity_refs);
6278 tcase_add_test(tc_basic, test_no_indirectly_recursive_entity_refs);
6279 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_invalid_parse);
6280 tcase_add_test__if_xml_ge(tc_basic, test_dtd_default_handling);
6281 tcase_add_test(tc_basic, test_dtd_attr_handling);
6282 tcase_add_test(tc_basic, test_empty_ns_without_namespaces);
6283 tcase_add_test(tc_basic, test_ns_in_attribute_default_without_namespaces);
6284 tcase_add_test(tc_basic, test_stop_parser_between_char_data_calls);
6285 tcase_add_test(tc_basic, test_suspend_parser_between_char_data_calls);
6286 tcase_add_test(tc_basic, test_repeated_stop_parser_between_char_data_calls);
6287 tcase_add_test(tc_basic, test_good_cdata_ascii);
6288 tcase_add_test(tc_basic, test_good_cdata_utf16);
6289 tcase_add_test(tc_basic, test_good_cdata_utf16_le);
6290 tcase_add_test(tc_basic, test_long_cdata_utf16);
6291 tcase_add_test(tc_basic, test_multichar_cdata_utf16);
6292 tcase_add_test(tc_basic, test_utf16_bad_surrogate_pair);
6293 tcase_add_test(tc_basic, test_bad_cdata);
6294 tcase_add_test(tc_basic, test_bad_cdata_utf16);
6295 tcase_add_test(tc_basic, test_stop_parser_between_cdata_calls);
6296 tcase_add_test(tc_basic, test_suspend_parser_between_cdata_calls);
6297 tcase_add_test(tc_basic, test_memory_allocation);
6298 tcase_add_test__if_xml_ge(tc_basic, test_default_current);
6299 tcase_add_test(tc_basic, test_dtd_elements);
6300 tcase_add_test(tc_basic, test_dtd_elements_nesting);
6301 tcase_add_test__ifdef_xml_dtd(tc_basic, test_set_foreign_dtd);
6302 tcase_add_test__ifdef_xml_dtd(tc_basic, test_foreign_dtd_not_standalone);
6303 tcase_add_test__ifdef_xml_dtd(tc_basic, test_invalid_foreign_dtd);
6304 tcase_add_test__ifdef_xml_dtd(tc_basic, test_foreign_dtd_with_doctype);
6305 tcase_add_test__ifdef_xml_dtd(tc_basic,
6306 test_foreign_dtd_without_external_subset);
6307 tcase_add_test__ifdef_xml_dtd(tc_basic, test_empty_foreign_dtd);
6308 tcase_add_test(tc_basic, test_set_base);
6309 tcase_add_test(tc_basic, test_attributes);
6310 tcase_add_test__if_xml_ge(tc_basic, test_reset_in_entity);
6311 tcase_add_test(tc_basic, test_resume_invalid_parse);
6312 tcase_add_test(tc_basic, test_resume_resuspended);
6313 tcase_add_test(tc_basic, test_cdata_default);
6314 tcase_add_test(tc_basic, test_subordinate_reset);
6315 tcase_add_test(tc_basic, test_subordinate_suspend);
6316 tcase_add_test__if_xml_ge(tc_basic, test_subordinate_xdecl_suspend);
6317 tcase_add_test__if_xml_ge(tc_basic, test_subordinate_xdecl_abort);
6318 tcase_add_test__ifdef_xml_dtd(tc_basic,
6319 test_ext_entity_invalid_suspended_parse);
6320 tcase_add_test(tc_basic, test_explicit_encoding);
6321 tcase_add_test(tc_basic, test_trailing_cr);
6322 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_trailing_cr);
6323 tcase_add_test(tc_basic, test_trailing_rsqb);
6324 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_trailing_rsqb);
6325 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_good_cdata);
6326 tcase_add_test__ifdef_xml_dtd(tc_basic, test_user_parameters);
6327 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_ref_parameter);
6328 tcase_add_test(tc_basic, test_empty_parse);
6329 tcase_add_test(tc_basic, test_negative_len_parse);
6330 tcase_add_test(tc_basic, test_negative_len_parse_buffer);
6331 tcase_add_test(tc_basic, test_get_buffer_1);
6332 tcase_add_test(tc_basic, test_get_buffer_2);
6333 #if XML_CONTEXT_BYTES > 0
6334 tcase_add_test(tc_basic, test_get_buffer_3_overflow);
6335 #endif
6336 tcase_add_test(tc_basic, test_buffer_can_grow_to_max);
6337 tcase_add_test(tc_basic, test_getbuffer_allocates_on_zero_len);
6338 tcase_add_test(tc_basic, test_byte_info_at_end);
6339 tcase_add_test(tc_basic, test_byte_info_at_error);
6340 tcase_add_test(tc_basic, test_byte_info_at_cdata);
6341 tcase_add_test(tc_basic, test_predefined_entities);
6342 tcase_add_test__ifdef_xml_dtd(tc_basic, test_invalid_tag_in_dtd);
6343 tcase_add_test(tc_basic, test_not_predefined_entities);
6344 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ignore_section);
6345 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ignore_section_utf16);
6346 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ignore_section_utf16_be);
6347 tcase_add_test__ifdef_xml_dtd(tc_basic, test_bad_ignore_section);
6348 tcase_add_test__ifdef_xml_dtd(tc_basic, test_external_bom_consumed);
6349 tcase_add_test__ifdef_xml_dtd(tc_basic, test_external_entity_values);
6350 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_not_standalone);
6351 tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_value_abort);
6352 tcase_add_test(tc_basic, test_bad_public_doctype);
6353 tcase_add_test(tc_basic, test_attribute_enum_value);
6354 tcase_add_test(tc_basic, test_predefined_entity_redefinition);
6355 tcase_add_test__ifdef_xml_dtd(tc_basic, test_dtd_stop_processing);
6356 tcase_add_test(tc_basic, test_public_notation_no_sysid);
6357 tcase_add_test(tc_basic, test_nested_groups);
6358 tcase_add_test(tc_basic, test_group_choice);
6359 tcase_add_test(tc_basic, test_standalone_parameter_entity);
6360 tcase_add_test__ifdef_xml_dtd(tc_basic, test_skipped_parameter_entity);
6361 tcase_add_test__ifdef_xml_dtd(tc_basic,
6362 test_recursive_external_parameter_entity);
6363 tcase_add_test__ifdef_xml_dtd(tc_basic,
6364 test_recursive_external_parameter_entity_2);
6365 tcase_add_test(tc_basic, test_undefined_ext_entity_in_external_dtd);
6366 tcase_add_test(tc_basic, test_suspend_xdecl);
6367 tcase_add_test(tc_basic, test_abort_epilog);
6368 tcase_add_test(tc_basic, test_abort_epilog_2);
6369 tcase_add_test(tc_basic, test_suspend_epilog);
6370 tcase_add_test(tc_basic, test_suspend_in_sole_empty_tag);
6371 tcase_add_test(tc_basic, test_unfinished_epilog);
6372 tcase_add_test(tc_basic, test_partial_char_in_epilog);
6373 tcase_add_test__ifdef_xml_dtd(tc_basic, test_suspend_resume_internal_entity);
6374 tcase_add_test__ifdef_xml_dtd(tc_basic,
6375 test_suspend_resume_internal_entity_issue_629);
6376 tcase_add_test__ifdef_xml_dtd(tc_basic, test_resume_entity_with_syntax_error);
6377 tcase_add_test__ifdef_xml_dtd(tc_basic, test_suspend_resume_parameter_entity);
6378 tcase_add_test(tc_basic, test_restart_on_error);
6379 tcase_add_test(tc_basic, test_reject_lt_in_attribute_value);
6380 tcase_add_test(tc_basic, test_reject_unfinished_param_in_att_value);
6381 tcase_add_test(tc_basic, test_trailing_cr_in_att_value);
6382 tcase_add_test(tc_basic, test_standalone_internal_entity);
6383 tcase_add_test(tc_basic, test_skipped_external_entity);
6384 tcase_add_test(tc_basic, test_skipped_null_loaded_ext_entity);
6385 tcase_add_test(tc_basic, test_skipped_unloaded_ext_entity);
6386 tcase_add_test__ifdef_xml_dtd(tc_basic, test_param_entity_with_trailing_cr);
6387 tcase_add_test__if_xml_ge(tc_basic, test_invalid_character_entity);
6388 tcase_add_test__if_xml_ge(tc_basic, test_invalid_character_entity_2);
6389 tcase_add_test__if_xml_ge(tc_basic, test_invalid_character_entity_3);
6390 tcase_add_test__if_xml_ge(tc_basic, test_invalid_character_entity_4);
6391 tcase_add_test(tc_basic, test_pi_handled_in_default);
6392 tcase_add_test(tc_basic, test_comment_handled_in_default);
6393 tcase_add_test(tc_basic, test_pi_yml);
6394 tcase_add_test(tc_basic, test_pi_xnl);
6395 tcase_add_test(tc_basic, test_pi_xmm);
6396 tcase_add_test(tc_basic, test_utf16_pi);
6397 tcase_add_test(tc_basic, test_utf16_be_pi);
6398 tcase_add_test(tc_basic, test_utf16_be_comment);
6399 tcase_add_test(tc_basic, test_utf16_le_comment);
6400 tcase_add_test(tc_basic, test_missing_encoding_conversion_fn);
6401 tcase_add_test(tc_basic, test_failing_encoding_conversion_fn);
6402 tcase_add_test(tc_basic, test_unknown_encoding_success);
6403 tcase_add_test(tc_basic, test_unknown_encoding_bad_name);
6404 tcase_add_test(tc_basic, test_unknown_encoding_bad_name_2);
6405 tcase_add_test(tc_basic, test_unknown_encoding_long_name_1);
6406 tcase_add_test(tc_basic, test_unknown_encoding_long_name_2);
6407 tcase_add_test(tc_basic, test_invalid_unknown_encoding);
6408 tcase_add_test(tc_basic, test_unknown_ascii_encoding_ok);
6409 tcase_add_test(tc_basic, test_unknown_ascii_encoding_fail);
6410 tcase_add_test(tc_basic, test_unknown_encoding_invalid_length);
6411 tcase_add_test(tc_basic, test_unknown_encoding_invalid_topbit);
6412 tcase_add_test(tc_basic, test_unknown_encoding_invalid_surrogate);
6413 tcase_add_test(tc_basic, test_unknown_encoding_invalid_high);
6414 tcase_add_test(tc_basic, test_unknown_encoding_invalid_attr_value);
6415 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_latin1_utf16le_bom);
6416 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_latin1_utf16be_bom);
6417 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_latin1_utf16le_bom2);
6418 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_latin1_utf16be_bom2);
6419 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_utf16_be);
6420 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_utf16_le);
6421 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_utf16_unknown);
6422 tcase_add_test__if_xml_ge(tc_basic, test_ext_entity_utf8_non_bom);
6423 tcase_add_test(tc_basic, test_utf8_in_cdata_section);
6424 tcase_add_test(tc_basic, test_utf8_in_cdata_section_2);
6425 tcase_add_test(tc_basic, test_utf8_in_start_tags);
6426 tcase_add_test(tc_basic, test_trailing_spaces_in_elements);
6427 tcase_add_test(tc_basic, test_utf16_attribute);
6428 tcase_add_test(tc_basic, test_utf16_second_attr);
6429 tcase_add_test(tc_basic, test_attr_after_solidus);
6430 tcase_add_test__ifdef_xml_dtd(tc_basic, test_utf16_pe);
6431 tcase_add_test(tc_basic, test_bad_attr_desc_keyword);
6432 tcase_add_test(tc_basic, test_bad_attr_desc_keyword_utf16);
6433 tcase_add_test(tc_basic, test_bad_doctype);
6434 tcase_add_test(tc_basic, test_bad_doctype_utf8);
6435 tcase_add_test(tc_basic, test_bad_doctype_utf16);
6436 tcase_add_test(tc_basic, test_bad_doctype_plus);
6437 tcase_add_test(tc_basic, test_bad_doctype_star);
6438 tcase_add_test(tc_basic, test_bad_doctype_query);
6439 tcase_add_test__ifdef_xml_dtd(tc_basic, test_unknown_encoding_bad_ignore);
6440 tcase_add_test(tc_basic, test_entity_in_utf16_be_attr);
6441 tcase_add_test(tc_basic, test_entity_in_utf16_le_attr);
6442 tcase_add_test__ifdef_xml_dtd(tc_basic, test_entity_public_utf16_be);
6443 tcase_add_test__ifdef_xml_dtd(tc_basic, test_entity_public_utf16_le);
6444 tcase_add_test(tc_basic, test_short_doctype);
6445 tcase_add_test(tc_basic, test_short_doctype_2);
6446 tcase_add_test(tc_basic, test_short_doctype_3);
6447 tcase_add_test(tc_basic, test_long_doctype);
6448 tcase_add_test(tc_basic, test_bad_entity);
6449 tcase_add_test(tc_basic, test_bad_entity_2);
6450 tcase_add_test(tc_basic, test_bad_entity_3);
6451 tcase_add_test(tc_basic, test_bad_entity_4);
6452 tcase_add_test(tc_basic, test_bad_notation);
6453 tcase_add_test(tc_basic, test_default_doctype_handler);
6454 tcase_add_test(tc_basic, test_empty_element_abort);
6455 tcase_add_test__ifdef_xml_dtd(tc_basic,
6456 test_pool_integrity_with_unfinished_attr);
6457 tcase_add_test__if_xml_ge(tc_basic, test_entity_ref_no_elements);
6458 tcase_add_test__if_xml_ge(tc_basic, test_deep_nested_entity);
6459 tcase_add_test__if_xml_ge(tc_basic, test_deep_nested_attribute_entity);
6460 tcase_add_test__if_xml_ge(tc_basic,
6461 test_deep_nested_entity_delayed_interpretation);
6462 tcase_add_test__if_xml_ge(tc_basic, test_nested_entity_suspend);
6463 tcase_add_test__if_xml_ge(tc_basic, test_nested_entity_suspend_2);
6464 tcase_add_test(tc_basic, test_big_tokens_scale_linearly);
6465 tcase_add_test(tc_basic, test_set_reparse_deferral);
6466 tcase_add_test(tc_basic, test_reparse_deferral_is_inherited);
6467 tcase_add_test(tc_basic, test_set_reparse_deferral_on_null_parser);
6468 tcase_add_test(tc_basic, test_set_reparse_deferral_on_the_fly);
6469 tcase_add_test(tc_basic, test_set_bad_reparse_option);
6470 tcase_add_test(tc_basic, test_bypass_heuristic_when_close_to_bufsize);
6471 tcase_add_test(tc_basic, test_varying_buffer_fills);
6472 }
6473