misc_tests.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /* Tests in the "miscellaneous" test case for the Expat test suite
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 2001-2006 Fred L. Drake, Jr. <[email protected]>
  9. Copyright (c) 2003 Greg Stein <[email protected]>
  10. Copyright (c) 2005-2007 Steven Solie <[email protected]>
  11. Copyright (c) 2005-2012 Karl Waclawek <[email protected]>
  12. Copyright (c) 2016-2025 Sebastian Pipping <[email protected]>
  13. Copyright (c) 2017-2022 Rhodri James <[email protected]>
  14. Copyright (c) 2017 Joe Orton <[email protected]>
  15. Copyright (c) 2017 José Gutiérrez de la Concha <[email protected]>
  16. Copyright (c) 2018 Marco Maggi <[email protected]>
  17. Copyright (c) 2019 David Loffredo <[email protected]>
  18. Copyright (c) 2020 Tim Gates <[email protected]>
  19. Copyright (c) 2021 Donghee Na <[email protected]>
  20. Copyright (c) 2023 Sony Corporation / Snild Dolkow <[email protected]>
  21. Licensed under the MIT license:
  22. Permission is hereby granted, free of charge, to any person obtaining
  23. a copy of this software and associated documentation files (the
  24. "Software"), to deal in the Software without restriction, including
  25. without limitation the rights to use, copy, modify, merge, publish,
  26. distribute, sublicense, and/or sell copies of the Software, and to permit
  27. persons to whom the Software is furnished to do so, subject to the
  28. following conditions:
  29. The above copyright notice and this permission notice shall be included
  30. in all copies or substantial portions of the Software.
  31. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  34. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  35. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  36. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  37. USE OR OTHER DEALINGS IN THE SOFTWARE.
  38. */
  39. #if defined(NDEBUG)
  40. # undef NDEBUG /* because test suite relies on assert(...) at the moment */
  41. #endif
  42. #include <assert.h>
  43. #include <string.h>
  44. #include "expat_config.h"
  45. #include "expat.h"
  46. #include "internal.h"
  47. #include "minicheck.h"
  48. #include "memcheck.h"
  49. #include "common.h"
  50. #include "ascii.h" /* for ASCII_xxx */
  51. #include "handlers.h"
  52. #include "misc_tests.h"
  53. void XMLCALL accumulate_characters_ext_handler(void *userData,
  54. const XML_Char *s, int len);
  55. /* Test that a failure to allocate the parser structure fails gracefully */
  56. START_TEST(test_misc_alloc_create_parser) {
  57. XML_Memory_Handling_Suite memsuite = {duff_allocator, realloc, free};
  58. unsigned int i;
  59. const unsigned int max_alloc_count = 10;
  60. /* Something this simple shouldn't need more than 10 allocations */
  61. for (i = 0; i < max_alloc_count; i++) {
  62. g_allocation_count = (int)i;
  63. g_parser = XML_ParserCreate_MM(NULL, &memsuite, NULL);
  64. if (g_parser != NULL)
  65. break;
  66. }
  67. if (i == 0)
  68. fail("Parser unexpectedly ignored failing allocator");
  69. else if (i == max_alloc_count)
  70. fail("Parser not created with max allocation count");
  71. }
  72. END_TEST
  73. /* Test memory allocation failures for a parser with an encoding */
  74. START_TEST(test_misc_alloc_create_parser_with_encoding) {
  75. XML_Memory_Handling_Suite memsuite = {duff_allocator, realloc, free};
  76. unsigned int i;
  77. const unsigned int max_alloc_count = 10;
  78. /* Try several levels of allocation */
  79. for (i = 0; i < max_alloc_count; i++) {
  80. g_allocation_count = (int)i;
  81. g_parser = XML_ParserCreate_MM(XCS("us-ascii"), &memsuite, NULL);
  82. if (g_parser != NULL)
  83. break;
  84. }
  85. if (i == 0)
  86. fail("Parser ignored failing allocator");
  87. else if (i == max_alloc_count)
  88. fail("Parser not created with max allocation count");
  89. }
  90. END_TEST
  91. /* Test that freeing a NULL parser doesn't cause an explosion.
  92. * (Not actually tested anywhere else)
  93. */
  94. START_TEST(test_misc_null_parser) {
  95. XML_ParserFree(NULL);
  96. }
  97. END_TEST
  98. #if defined(__has_feature)
  99. # if __has_feature(undefined_behavior_sanitizer)
  100. # define EXPAT_TESTS_UBSAN 1
  101. # else
  102. # define EXPAT_TESTS_UBSAN 0
  103. # endif
  104. #else
  105. # define EXPAT_TESTS_UBSAN 0
  106. #endif
  107. /* Test that XML_ErrorString rejects out-of-range codes */
  108. START_TEST(test_misc_error_string) {
  109. #if ! EXPAT_TESTS_UBSAN // because this would trigger UBSan
  110. union {
  111. enum XML_Error xml_error;
  112. int integer;
  113. } trickery;
  114. assert_true(sizeof(enum XML_Error) == sizeof(int)); // self-test
  115. trickery.integer = -1;
  116. if (XML_ErrorString(trickery.xml_error) != NULL)
  117. fail("Negative error code not rejected");
  118. trickery.integer = 100;
  119. if (XML_ErrorString(trickery.xml_error) != NULL)
  120. fail("Large error code not rejected");
  121. #endif
  122. }
  123. END_TEST
  124. /* Test the version information is consistent */
  125. /* Since we are working in XML_LChars (potentially 16-bits), we
  126. * can't use the standard C library functions for character
  127. * manipulation and have to roll our own.
  128. */
  129. static int
  130. parse_version(const XML_LChar *version_text,
  131. XML_Expat_Version *version_struct) {
  132. if (! version_text)
  133. return XML_FALSE;
  134. while (*version_text != 0x00) {
  135. if (*version_text >= ASCII_0 && *version_text <= ASCII_9)
  136. break;
  137. version_text++;
  138. }
  139. if (*version_text == 0x00)
  140. return XML_FALSE;
  141. /* version_struct->major = strtoul(version_text, 10, &version_text) */
  142. version_struct->major = 0;
  143. while (*version_text >= ASCII_0 && *version_text <= ASCII_9) {
  144. version_struct->major
  145. = 10 * version_struct->major + (*version_text++ - ASCII_0);
  146. }
  147. if (*version_text++ != ASCII_PERIOD)
  148. return XML_FALSE;
  149. /* Now for the minor version number */
  150. version_struct->minor = 0;
  151. while (*version_text >= ASCII_0 && *version_text <= ASCII_9) {
  152. version_struct->minor
  153. = 10 * version_struct->minor + (*version_text++ - ASCII_0);
  154. }
  155. if (*version_text++ != ASCII_PERIOD)
  156. return XML_FALSE;
  157. /* Finally the micro version number */
  158. version_struct->micro = 0;
  159. while (*version_text >= ASCII_0 && *version_text <= ASCII_9) {
  160. version_struct->micro
  161. = 10 * version_struct->micro + (*version_text++ - ASCII_0);
  162. }
  163. if (*version_text != 0x00)
  164. return XML_FALSE;
  165. return XML_TRUE;
  166. }
  167. static int
  168. versions_equal(const XML_Expat_Version *first,
  169. const XML_Expat_Version *second) {
  170. return (first->major == second->major && first->minor == second->minor
  171. && first->micro == second->micro);
  172. }
  173. START_TEST(test_misc_version) {
  174. XML_Expat_Version read_version = XML_ExpatVersionInfo();
  175. /* Silence compiler warning with the following assignment */
  176. XML_Expat_Version parsed_version = {0, 0, 0};
  177. const XML_LChar *version_text = XML_ExpatVersion();
  178. if (version_text == NULL)
  179. fail("Could not obtain version text");
  180. assert(version_text != NULL);
  181. if (! parse_version(version_text, &parsed_version))
  182. fail("Unable to parse version text");
  183. if (! versions_equal(&read_version, &parsed_version))
  184. fail("Version mismatch");
  185. if (xcstrcmp(version_text, XCS("expat_2.7.3"))
  186. != 0) /* needs bump on releases */
  187. fail("XML_*_VERSION in expat.h out of sync?\n");
  188. }
  189. END_TEST
  190. /* Test feature information */
  191. START_TEST(test_misc_features) {
  192. const XML_Feature *features = XML_GetFeatureList();
  193. /* Prevent problems with double-freeing parsers */
  194. g_parser = NULL;
  195. if (features == NULL) {
  196. fail("Failed to get feature information");
  197. } else {
  198. /* Loop through the features checking what we can */
  199. while (features->feature != XML_FEATURE_END) {
  200. switch (features->feature) {
  201. case XML_FEATURE_SIZEOF_XML_CHAR:
  202. if (features->value != sizeof(XML_Char))
  203. fail("Incorrect size of XML_Char");
  204. break;
  205. case XML_FEATURE_SIZEOF_XML_LCHAR:
  206. if (features->value != sizeof(XML_LChar))
  207. fail("Incorrect size of XML_LChar");
  208. break;
  209. default:
  210. break;
  211. }
  212. features++;
  213. }
  214. }
  215. }
  216. END_TEST
  217. /* Regression test for GitHub Issue #17: memory leak parsing attribute
  218. * values with mixed bound and unbound namespaces.
  219. */
  220. START_TEST(test_misc_attribute_leak) {
  221. const char *text = "<D xmlns:L=\"D\" l:a='' L:a=''/>";
  222. XML_Memory_Handling_Suite memsuite
  223. = {tracking_malloc, tracking_realloc, tracking_free};
  224. g_parser = XML_ParserCreate_MM(XCS("UTF-8"), &memsuite, XCS("\n"));
  225. expect_failure(text, XML_ERROR_UNBOUND_PREFIX, "Unbound prefixes not found");
  226. XML_ParserFree(g_parser);
  227. /* Prevent the teardown trying to double free */
  228. g_parser = NULL;
  229. if (! tracking_report())
  230. fail("Memory leak found");
  231. }
  232. END_TEST
  233. /* Test parser created for UTF-16LE is successful */
  234. START_TEST(test_misc_utf16le) {
  235. const char text[] =
  236. /* <?xml version='1.0'?><q>Hi</q> */
  237. "<\0?\0x\0m\0l\0 \0"
  238. "v\0e\0r\0s\0i\0o\0n\0=\0'\0\x31\0.\0\x30\0'\0?\0>\0"
  239. "<\0q\0>\0H\0i\0<\0/\0q\0>\0";
  240. const XML_Char *expected = XCS("Hi");
  241. CharData storage;
  242. g_parser = XML_ParserCreate(XCS("UTF-16LE"));
  243. if (g_parser == NULL)
  244. fail("Parser not created");
  245. CharData_Init(&storage);
  246. XML_SetUserData(g_parser, &storage);
  247. XML_SetCharacterDataHandler(g_parser, accumulate_characters);
  248. if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
  249. == XML_STATUS_ERROR)
  250. xml_failure(g_parser);
  251. CharData_CheckXMLChars(&storage, expected);
  252. }
  253. END_TEST
  254. START_TEST(test_misc_stop_during_end_handler_issue_240_1) {
  255. XML_Parser parser;
  256. DataIssue240 *mydata;
  257. enum XML_Status result;
  258. const char *const doc1 = "<doc><e1/><e><foo/></e></doc>";
  259. parser = XML_ParserCreate(NULL);
  260. XML_SetElementHandler(parser, start_element_issue_240, end_element_issue_240);
  261. mydata = (DataIssue240 *)malloc(sizeof(DataIssue240));
  262. assert_true(mydata != NULL);
  263. mydata->parser = parser;
  264. mydata->deep = 0;
  265. XML_SetUserData(parser, mydata);
  266. result = _XML_Parse_SINGLE_BYTES(parser, doc1, (int)strlen(doc1), 1);
  267. XML_ParserFree(parser);
  268. free(mydata);
  269. if (result != XML_STATUS_ERROR)
  270. fail("Stopping the parser did not work as expected");
  271. }
  272. END_TEST
  273. START_TEST(test_misc_stop_during_end_handler_issue_240_2) {
  274. XML_Parser parser;
  275. DataIssue240 *mydata;
  276. enum XML_Status result;
  277. const char *const doc2 = "<doc><elem/></doc>";
  278. parser = XML_ParserCreate(NULL);
  279. XML_SetElementHandler(parser, start_element_issue_240, end_element_issue_240);
  280. mydata = (DataIssue240 *)malloc(sizeof(DataIssue240));
  281. assert_true(mydata != NULL);
  282. mydata->parser = parser;
  283. mydata->deep = 0;
  284. XML_SetUserData(parser, mydata);
  285. result = _XML_Parse_SINGLE_BYTES(parser, doc2, (int)strlen(doc2), 1);
  286. XML_ParserFree(parser);
  287. free(mydata);
  288. if (result != XML_STATUS_ERROR)
  289. fail("Stopping the parser did not work as expected");
  290. }
  291. END_TEST
  292. START_TEST(test_misc_deny_internal_entity_closing_doctype_issue_317) {
  293. const char *const inputOne
  294. = "<!DOCTYPE d [\n"
  295. "<!ENTITY % element_d '<!ELEMENT d (#PCDATA)*>'>\n"
  296. "%element_d;\n"
  297. "<!ENTITY % e ']><d/>'>\n"
  298. "\n"
  299. "%e;";
  300. const char *const inputTwo
  301. = "<!DOCTYPE d [\n"
  302. "<!ENTITY % element_d '<!ELEMENT d (#PCDATA)*>'>\n"
  303. "%element_d;\n"
  304. "<!ENTITY % e1 ']><d/>'><!ENTITY % e2 '&#37;e1;'>\n"
  305. "\n"
  306. "%e2;";
  307. const char *const inputThree
  308. = "<!DOCTYPE d [\n"
  309. "<!ENTITY % element_d '<!ELEMENT d (#PCDATA)*>'>\n"
  310. "%element_d;\n"
  311. "<!ENTITY % e ']><d'>\n"
  312. "\n"
  313. "%e;/>";
  314. const char *const inputIssue317
  315. = "<!DOCTYPE doc [\n"
  316. "<!ENTITY % element_doc '<!ELEMENT doc (#PCDATA)*>'>\n"
  317. "%element_doc;\n"
  318. "<!ENTITY % foo ']>\n"
  319. "<doc>Hell<oc (#PCDATA)*>'>\n"
  320. "%foo;\n"
  321. "]>\n"
  322. "<doc>Hello, world</dVc>";
  323. const char *const inputs[] = {inputOne, inputTwo, inputThree, inputIssue317};
  324. const XML_Bool suspendOrNot[] = {XML_FALSE, XML_TRUE};
  325. size_t inputIndex = 0;
  326. for (; inputIndex < sizeof(inputs) / sizeof(inputs[0]); inputIndex++) {
  327. for (size_t suspendOrNotIndex = 0;
  328. suspendOrNotIndex < sizeof(suspendOrNot) / sizeof(suspendOrNot[0]);
  329. suspendOrNotIndex++) {
  330. const char *const input = inputs[inputIndex];
  331. const XML_Bool suspend = suspendOrNot[suspendOrNotIndex];
  332. if (suspend && (g_chunkSize > 0)) {
  333. // We cannot use _XML_Parse_SINGLE_BYTES below due to suspension, and
  334. // so chunk sizes >0 would only repeat the very same test
  335. // due to use of plain XML_Parse; we are saving upon that runtime:
  336. return;
  337. }
  338. set_subtest("[input=%d suspend=%s] %s", (int)inputIndex,
  339. suspend ? "true" : "false", input);
  340. XML_Parser parser;
  341. enum XML_Status parseResult;
  342. int setParamEntityResult;
  343. XML_Size lineNumber;
  344. XML_Size columnNumber;
  345. parser = XML_ParserCreate(NULL);
  346. setParamEntityResult
  347. = XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
  348. if (setParamEntityResult != 1)
  349. fail("Failed to set XML_PARAM_ENTITY_PARSING_ALWAYS.");
  350. if (suspend) {
  351. XML_SetUserData(parser, parser);
  352. XML_SetElementDeclHandler(parser, suspend_after_element_declaration);
  353. }
  354. if (suspend) {
  355. // can't use SINGLE_BYTES here, because it'll return early on
  356. // suspension, and we won't know exactly how much input we actually
  357. // managed to give Expat.
  358. parseResult = XML_Parse(parser, input, (int)strlen(input), 0);
  359. while (parseResult == XML_STATUS_SUSPENDED) {
  360. parseResult = XML_ResumeParser(parser);
  361. }
  362. if (parseResult != XML_STATUS_ERROR) {
  363. // can't use SINGLE_BYTES here, because it'll return early on
  364. // suspension, and we won't know exactly how much input we actually
  365. // managed to give Expat.
  366. parseResult = XML_Parse(parser, "", 0, 1);
  367. }
  368. while (parseResult == XML_STATUS_SUSPENDED) {
  369. parseResult = XML_ResumeParser(parser);
  370. }
  371. } else {
  372. parseResult
  373. = _XML_Parse_SINGLE_BYTES(parser, input, (int)strlen(input), 0);
  374. if (parseResult != XML_STATUS_ERROR) {
  375. parseResult = _XML_Parse_SINGLE_BYTES(parser, "", 0, 1);
  376. }
  377. }
  378. if (parseResult != XML_STATUS_ERROR) {
  379. fail("Parsing was expected to fail but succeeded.");
  380. }
  381. if (XML_GetErrorCode(parser) != XML_ERROR_INVALID_TOKEN)
  382. fail("Error code does not match XML_ERROR_INVALID_TOKEN");
  383. lineNumber = XML_GetCurrentLineNumber(parser);
  384. if (lineNumber != 6)
  385. fail("XML_GetCurrentLineNumber does not work as expected.");
  386. columnNumber = XML_GetCurrentColumnNumber(parser);
  387. if (columnNumber != 0)
  388. fail("XML_GetCurrentColumnNumber does not work as expected.");
  389. XML_ParserFree(parser);
  390. }
  391. }
  392. }
  393. END_TEST
  394. START_TEST(test_misc_tag_mismatch_reset_leak) {
  395. #ifdef XML_NS
  396. const char *const text = "<open xmlns='https://namespace1.test'></close>";
  397. XML_Parser parser = XML_ParserCreateNS(NULL, XCS('\n'));
  398. if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
  399. != XML_STATUS_ERROR)
  400. fail("Call to parse was expected to fail");
  401. if (XML_GetErrorCode(parser) != XML_ERROR_TAG_MISMATCH)
  402. fail("Call to parse was expected to fail from a closing tag mismatch");
  403. XML_ParserReset(parser, NULL);
  404. if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
  405. != XML_STATUS_ERROR)
  406. fail("Call to parse was expected to fail");
  407. if (XML_GetErrorCode(parser) != XML_ERROR_TAG_MISMATCH)
  408. fail("Call to parse was expected to fail from a closing tag mismatch");
  409. XML_ParserFree(parser);
  410. #endif
  411. }
  412. END_TEST
  413. START_TEST(test_misc_create_external_entity_parser_with_null_context) {
  414. // With XML_DTD undefined, the only supported case of external entities
  415. // is pattern "<!ENTITY entity123 SYSTEM 'filename123'>". A NULL context
  416. // was causing a segfault through a null pointer dereference in function
  417. // setContext, previously.
  418. XML_Parser parser = XML_ParserCreate(NULL);
  419. XML_Parser ext_parser = XML_ExternalEntityParserCreate(parser, NULL, NULL);
  420. #ifdef XML_DTD
  421. assert_true(ext_parser != NULL);
  422. XML_ParserFree(ext_parser);
  423. #else
  424. assert_true(ext_parser == NULL);
  425. #endif /* XML_DTD */
  426. XML_ParserFree(parser);
  427. }
  428. END_TEST
  429. START_TEST(test_misc_general_entities_support) {
  430. const char *const doc
  431. = "<!DOCTYPE r [\n"
  432. "<!ENTITY e1 'v1'>\n"
  433. "<!ENTITY e2 SYSTEM 'v2'>\n"
  434. "]>\n"
  435. "<r a1='[&e1;]'>[&e1;][&e2;][&amp;&apos;&gt;&lt;&quot;]</r>";
  436. CharData storage;
  437. CharData_Init(&storage);
  438. XML_Parser parser = XML_ParserCreate(NULL);
  439. XML_SetUserData(parser, &storage);
  440. XML_SetStartElementHandler(parser, accumulate_start_element);
  441. XML_SetExternalEntityRefHandler(parser,
  442. external_entity_failer__if_not_xml_ge);
  443. XML_SetEntityDeclHandler(parser, accumulate_entity_decl);
  444. XML_SetCharacterDataHandler(parser, accumulate_characters);
  445. if (_XML_Parse_SINGLE_BYTES(parser, doc, (int)strlen(doc), XML_TRUE)
  446. != XML_STATUS_OK) {
  447. xml_failure(parser);
  448. }
  449. XML_ParserFree(parser);
  450. CharData_CheckXMLChars(&storage,
  451. /* clang-format off */
  452. #if XML_GE == 1
  453. XCS("e1=v1\n")
  454. XCS("e2=(null)\n")
  455. XCS("(r(a1=[v1]))\n")
  456. XCS("[v1][][&'><\"]")
  457. #else
  458. XCS("e1=&amp;e1;\n")
  459. XCS("e2=(null)\n")
  460. XCS("(r(a1=[&e1;]))\n")
  461. XCS("[&e1;][&e2;][&'><\"]")
  462. #endif
  463. );
  464. /* clang-format on */
  465. }
  466. END_TEST
  467. static void XMLCALL
  468. resumable_stopping_character_handler(void *userData, const XML_Char *s,
  469. int len) {
  470. UNUSED_P(s);
  471. UNUSED_P(len);
  472. XML_Parser parser = (XML_Parser)userData;
  473. XML_StopParser(parser, XML_TRUE);
  474. }
  475. // NOTE: This test needs active LeakSanitizer to be of actual use
  476. START_TEST(test_misc_char_handler_stop_without_leak) {
  477. const char *const data
  478. = "<!DOCTYPE t1[<!ENTITY e1 'angle<'><!ENTITY e2 '&e1;'>]><t1>&e2;";
  479. XML_Parser parser = XML_ParserCreate(NULL);
  480. assert_true(parser != NULL);
  481. XML_SetUserData(parser, parser);
  482. XML_SetCharacterDataHandler(parser, resumable_stopping_character_handler);
  483. _XML_Parse_SINGLE_BYTES(parser, data, (int)strlen(data), XML_FALSE);
  484. XML_ParserFree(parser);
  485. }
  486. END_TEST
  487. START_TEST(test_misc_resumeparser_not_crashing) {
  488. XML_Parser parser = XML_ParserCreate(NULL);
  489. XML_GetBuffer(parser, 1);
  490. XML_StopParser(parser, /*resumable=*/XML_TRUE);
  491. XML_ResumeParser(parser); // could crash here, previously
  492. XML_ParserFree(parser);
  493. }
  494. END_TEST
  495. START_TEST(test_misc_stopparser_rejects_unstarted_parser) {
  496. const XML_Bool cases[] = {XML_TRUE, XML_FALSE};
  497. for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
  498. const XML_Bool resumable = cases[i];
  499. XML_Parser parser = XML_ParserCreate(NULL);
  500. assert_true(XML_GetErrorCode(parser) == XML_ERROR_NONE);
  501. assert_true(XML_StopParser(parser, resumable) == XML_STATUS_ERROR);
  502. assert_true(XML_GetErrorCode(parser) == XML_ERROR_NOT_STARTED);
  503. XML_ParserFree(parser);
  504. }
  505. }
  506. END_TEST
  507. /* Adaptation of accumulate_characters that takes ExtHdlrData input to work with
  508. * test_renter_loop_finite_content below */
  509. void XMLCALL
  510. accumulate_characters_ext_handler(void *userData, const XML_Char *s, int len) {
  511. ExtHdlrData *const test_data = (ExtHdlrData *)userData;
  512. CharData_AppendXMLChars(test_data->storage, s, len);
  513. }
  514. /* Test that internalEntityProcessor does not re-enter forever;
  515. * based on files tests/xmlconf/xmltest/valid/ext-sa/012.{xml,ent} */
  516. START_TEST(test_renter_loop_finite_content) {
  517. CharData storage;
  518. CharData_Init(&storage);
  519. const char *const text = "<!DOCTYPE doc [\n"
  520. "<!ENTITY e1 '&e2;'>\n"
  521. "<!ENTITY e2 '&e3;'>\n"
  522. "<!ENTITY e3 SYSTEM '012.ent'>\n"
  523. "<!ENTITY e4 '&e5;'>\n"
  524. "<!ENTITY e5 '(e5)'>\n"
  525. "<!ELEMENT doc (#PCDATA)>\n"
  526. "]>\n"
  527. "<doc>&e1;</doc>\n";
  528. ExtHdlrData test_data = {"&e4;\n", external_entity_null_loader, &storage};
  529. const XML_Char *const expected = XCS("(e5)\n");
  530. XML_Parser parser = XML_ParserCreate(NULL);
  531. assert_true(parser != NULL);
  532. XML_SetUserData(parser, &test_data);
  533. XML_SetExternalEntityRefHandler(parser, external_entity_oneshot_loader);
  534. XML_SetCharacterDataHandler(parser, accumulate_characters_ext_handler);
  535. if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
  536. == XML_STATUS_ERROR)
  537. xml_failure(parser);
  538. CharData_CheckXMLChars(&storage, expected);
  539. XML_ParserFree(parser);
  540. }
  541. END_TEST
  542. // Inspired by function XML_OriginalString of Perl's XML::Parser
  543. static char *
  544. dup_original_string(XML_Parser parser) {
  545. const int byte_count = XML_GetCurrentByteCount(parser);
  546. assert_true(byte_count >= 0);
  547. int offset = -1;
  548. int size = -1;
  549. const char *const context = XML_GetInputContext(parser, &offset, &size);
  550. #if XML_CONTEXT_BYTES > 0
  551. assert_true(context != NULL);
  552. assert_true(offset >= 0);
  553. assert_true(size >= 0);
  554. return portable_strndup(context + offset, byte_count);
  555. #else
  556. assert_true(context == NULL);
  557. return NULL;
  558. #endif
  559. }
  560. static void
  561. on_characters_issue_980(void *userData, const XML_Char *s, int len) {
  562. (void)s;
  563. (void)len;
  564. XML_Parser parser = (XML_Parser)userData;
  565. char *const original_string = dup_original_string(parser);
  566. #if XML_CONTEXT_BYTES > 0
  567. assert_true(original_string != NULL);
  568. assert_true(strcmp(original_string, "&draft.day;") == 0);
  569. free(original_string);
  570. #else
  571. assert_true(original_string == NULL);
  572. #endif
  573. }
  574. START_TEST(test_misc_expected_event_ptr_issue_980) {
  575. // NOTE: This is a tiny subset of sample "REC-xml-19980210.xml"
  576. // from Perl's XML::Parser
  577. const char *const doc = "<!DOCTYPE day [\n"
  578. " <!ENTITY draft.day '10'>\n"
  579. "]>\n"
  580. "<day>&draft.day;</day>\n";
  581. XML_Parser parser = XML_ParserCreate(NULL);
  582. XML_SetUserData(parser, parser);
  583. XML_SetCharacterDataHandler(parser, on_characters_issue_980);
  584. assert_true(_XML_Parse_SINGLE_BYTES(parser, doc, (int)strlen(doc),
  585. /*isFinal=*/XML_TRUE)
  586. == XML_STATUS_OK);
  587. XML_ParserFree(parser);
  588. }
  589. END_TEST
  590. START_TEST(test_misc_sync_entity_tolerated) {
  591. const char *const doc = "<!DOCTYPE t0 [\n"
  592. " <!ENTITY a '<t1></t1>'>\n"
  593. " <!ENTITY b '<t2>two</t2>'>\n"
  594. " <!ENTITY c '<t3>three<t4>four</t4>three</t3>'>\n"
  595. " <!ENTITY d '<t5>&b;</t5>'>\n"
  596. "]>\n"
  597. "<t0>&a;&b;&c;&d;</t0>\n";
  598. XML_Parser parser = XML_ParserCreate(NULL);
  599. assert_true(_XML_Parse_SINGLE_BYTES(parser, doc, (int)strlen(doc),
  600. /*isFinal=*/XML_TRUE)
  601. == XML_STATUS_OK);
  602. XML_ParserFree(parser);
  603. }
  604. END_TEST
  605. START_TEST(test_misc_async_entity_rejected) {
  606. struct test_case {
  607. const char *doc;
  608. enum XML_Status expectedStatusNoGE;
  609. enum XML_Error expectedErrorNoGE;
  610. XML_Size expectedErrorLine;
  611. XML_Size expectedErrorColumn;
  612. };
  613. const struct test_case cases[] = {
  614. // Opened by one entity, closed by another
  615. {"<!DOCTYPE t0 [\n"
  616. " <!ENTITY open '<t1>'>\n"
  617. " <!ENTITY close '</t1>'>\n"
  618. "]>\n"
  619. "<t0>&open;&close;</t0>\n",
  620. XML_STATUS_OK, XML_ERROR_NONE, 5, 4},
  621. // Opened by tag, closed by entity (non-root case)
  622. {"<!DOCTYPE t0 [\n"
  623. " <!ENTITY g0 ''>\n"
  624. " <!ENTITY g1 '&g0;</t1>'>\n"
  625. "]>\n"
  626. "<t0><t1>&g1;</t0>\n",
  627. XML_STATUS_ERROR, XML_ERROR_TAG_MISMATCH, 5, 8},
  628. // Opened by tag, closed by entity (root case)
  629. {"<!DOCTYPE t0 [\n"
  630. " <!ENTITY g0 ''>\n"
  631. " <!ENTITY g1 '&g0;</t0>'>\n"
  632. "]>\n"
  633. "<t0>&g1;\n",
  634. XML_STATUS_ERROR, XML_ERROR_NO_ELEMENTS, 5, 4},
  635. // Opened by entity, closed by tag <-- regression from 2.7.0
  636. {"<!DOCTYPE t0 [\n"
  637. " <!ENTITY g0 ''>\n"
  638. " <!ENTITY g1 '<t1>&g0;'>\n"
  639. "]>\n"
  640. "<t0>&g1;</t1></t0>\n",
  641. XML_STATUS_ERROR, XML_ERROR_TAG_MISMATCH, 5, 4},
  642. // Opened by tag, closed by entity; then the other way around
  643. {"<!DOCTYPE t0 [\n"
  644. " <!ENTITY open '<t1>'>\n"
  645. " <!ENTITY close '</t1>'>\n"
  646. "]>\n"
  647. "<t0><t1>&close;&open;</t1></t0>\n",
  648. XML_STATUS_OK, XML_ERROR_NONE, 5, 8},
  649. };
  650. for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
  651. const struct test_case testCase = cases[i];
  652. set_subtest("cases[%d]", (int)i);
  653. const char *const doc = testCase.doc;
  654. #if XML_GE == 1
  655. const enum XML_Status expectedStatus = XML_STATUS_ERROR;
  656. const enum XML_Error expectedError = XML_ERROR_ASYNC_ENTITY;
  657. #else
  658. const enum XML_Status expectedStatus = testCase.expectedStatusNoGE;
  659. const enum XML_Error expectedError = testCase.expectedErrorNoGE;
  660. #endif
  661. XML_Parser parser = XML_ParserCreate(NULL);
  662. assert_true(_XML_Parse_SINGLE_BYTES(parser, doc, (int)strlen(doc),
  663. /*isFinal=*/XML_TRUE)
  664. == expectedStatus);
  665. assert_true(XML_GetErrorCode(parser) == expectedError);
  666. #if XML_GE == 1
  667. assert_true(XML_GetCurrentLineNumber(parser) == testCase.expectedErrorLine);
  668. assert_true(XML_GetCurrentColumnNumber(parser)
  669. == testCase.expectedErrorColumn);
  670. #endif
  671. XML_ParserFree(parser);
  672. }
  673. }
  674. END_TEST
  675. void
  676. make_miscellaneous_test_case(Suite *s) {
  677. TCase *tc_misc = tcase_create("miscellaneous tests");
  678. suite_add_tcase(s, tc_misc);
  679. tcase_add_checked_fixture(tc_misc, NULL, basic_teardown);
  680. tcase_add_test(tc_misc, test_misc_alloc_create_parser);
  681. tcase_add_test(tc_misc, test_misc_alloc_create_parser_with_encoding);
  682. tcase_add_test(tc_misc, test_misc_null_parser);
  683. tcase_add_test(tc_misc, test_misc_error_string);
  684. tcase_add_test(tc_misc, test_misc_version);
  685. tcase_add_test(tc_misc, test_misc_features);
  686. tcase_add_test(tc_misc, test_misc_attribute_leak);
  687. tcase_add_test(tc_misc, test_misc_utf16le);
  688. tcase_add_test(tc_misc, test_misc_stop_during_end_handler_issue_240_1);
  689. tcase_add_test(tc_misc, test_misc_stop_during_end_handler_issue_240_2);
  690. tcase_add_test__ifdef_xml_dtd(
  691. tc_misc, test_misc_deny_internal_entity_closing_doctype_issue_317);
  692. tcase_add_test(tc_misc, test_misc_tag_mismatch_reset_leak);
  693. tcase_add_test(tc_misc,
  694. test_misc_create_external_entity_parser_with_null_context);
  695. tcase_add_test(tc_misc, test_misc_general_entities_support);
  696. tcase_add_test(tc_misc, test_misc_char_handler_stop_without_leak);
  697. tcase_add_test(tc_misc, test_misc_resumeparser_not_crashing);
  698. tcase_add_test(tc_misc, test_misc_stopparser_rejects_unstarted_parser);
  699. tcase_add_test__if_xml_ge(tc_misc, test_renter_loop_finite_content);
  700. tcase_add_test(tc_misc, test_misc_expected_event_ptr_issue_980);
  701. tcase_add_test(tc_misc, test_misc_sync_entity_tolerated);
  702. tcase_add_test(tc_misc, test_misc_async_entity_rejected);
  703. }