common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* Commonly used functions 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-2024 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. #include <assert.h>
  40. #include <errno.h>
  41. #include <stdint.h> // for SIZE_MAX
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include "expat_config.h"
  45. #include "expat.h"
  46. #include "internal.h"
  47. #include "chardata.h"
  48. #include "minicheck.h"
  49. #include "common.h"
  50. #include "handlers.h"
  51. /* Common test data */
  52. const char *long_character_data_text
  53. = "<?xml version='1.0' encoding='iso-8859-1'?><s>"
  54. "012345678901234567890123456789012345678901234567890123456789"
  55. "012345678901234567890123456789012345678901234567890123456789"
  56. "012345678901234567890123456789012345678901234567890123456789"
  57. "012345678901234567890123456789012345678901234567890123456789"
  58. "012345678901234567890123456789012345678901234567890123456789"
  59. "012345678901234567890123456789012345678901234567890123456789"
  60. "012345678901234567890123456789012345678901234567890123456789"
  61. "012345678901234567890123456789012345678901234567890123456789"
  62. "012345678901234567890123456789012345678901234567890123456789"
  63. "012345678901234567890123456789012345678901234567890123456789"
  64. "012345678901234567890123456789012345678901234567890123456789"
  65. "012345678901234567890123456789012345678901234567890123456789"
  66. "012345678901234567890123456789012345678901234567890123456789"
  67. "012345678901234567890123456789012345678901234567890123456789"
  68. "012345678901234567890123456789012345678901234567890123456789"
  69. "012345678901234567890123456789012345678901234567890123456789"
  70. "012345678901234567890123456789012345678901234567890123456789"
  71. "012345678901234567890123456789012345678901234567890123456789"
  72. "012345678901234567890123456789012345678901234567890123456789"
  73. "012345678901234567890123456789012345678901234567890123456789"
  74. "</s>";
  75. const char *long_cdata_text
  76. = "<s><![CDATA["
  77. "012345678901234567890123456789012345678901234567890123456789"
  78. "012345678901234567890123456789012345678901234567890123456789"
  79. "012345678901234567890123456789012345678901234567890123456789"
  80. "012345678901234567890123456789012345678901234567890123456789"
  81. "012345678901234567890123456789012345678901234567890123456789"
  82. "012345678901234567890123456789012345678901234567890123456789"
  83. "012345678901234567890123456789012345678901234567890123456789"
  84. "012345678901234567890123456789012345678901234567890123456789"
  85. "012345678901234567890123456789012345678901234567890123456789"
  86. "012345678901234567890123456789012345678901234567890123456789"
  87. "012345678901234567890123456789012345678901234567890123456789"
  88. "012345678901234567890123456789012345678901234567890123456789"
  89. "012345678901234567890123456789012345678901234567890123456789"
  90. "012345678901234567890123456789012345678901234567890123456789"
  91. "012345678901234567890123456789012345678901234567890123456789"
  92. "012345678901234567890123456789012345678901234567890123456789"
  93. "012345678901234567890123456789012345678901234567890123456789"
  94. "012345678901234567890123456789012345678901234567890123456789"
  95. "012345678901234567890123456789012345678901234567890123456789"
  96. "012345678901234567890123456789012345678901234567890123456789"
  97. "]]></s>";
  98. /* Having an element name longer than 1024 characters exercises some
  99. * of the pool allocation code in the parser that otherwise does not
  100. * get executed. The count at the end of the line is the number of
  101. * characters (bytes) in the element name by that point.x
  102. */
  103. const char *get_buffer_test_text
  104. = "<documentwitharidiculouslylongelementnametotease" /* 0x030 */
  105. "aparticularcorneroftheallocationinXML_GetBuffers" /* 0x060 */
  106. "othatwecanimprovethecoverageyetagain012345678901" /* 0x090 */
  107. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x0c0 */
  108. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x0f0 */
  109. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x120 */
  110. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x150 */
  111. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x180 */
  112. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x1b0 */
  113. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x1e0 */
  114. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x210 */
  115. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x240 */
  116. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x270 */
  117. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x2a0 */
  118. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x2d0 */
  119. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x300 */
  120. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x330 */
  121. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x360 */
  122. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x390 */
  123. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x3c0 */
  124. "123456789abcdef0123456789abcdef0123456789abcdef0" /* 0x3f0 */
  125. "123456789abcdef0123456789abcdef0123456789>\n<ef0"; /* 0x420 */
  126. /* Test control globals */
  127. /* Used as the "resumable" parameter to XML_StopParser by some tests */
  128. XML_Bool g_resumable = XML_FALSE;
  129. /* Used to control abort checks in some tests */
  130. XML_Bool g_abortable = XML_FALSE;
  131. /* Used to control _XML_Parse_SINGLE_BYTES() chunk size */
  132. int g_chunkSize = 1;
  133. /* Common test functions */
  134. void
  135. tcase_add_test__ifdef_xml_dtd(TCase *tc, tcase_test_function test) {
  136. #ifdef XML_DTD
  137. tcase_add_test(tc, test);
  138. #else
  139. UNUSED_P(tc);
  140. UNUSED_P(test);
  141. #endif
  142. }
  143. void
  144. tcase_add_test__if_xml_ge(TCase *tc, tcase_test_function test) {
  145. #if XML_GE == 1
  146. tcase_add_test(tc, test);
  147. #else
  148. UNUSED_P(tc);
  149. UNUSED_P(test);
  150. #endif
  151. }
  152. void
  153. basic_teardown(void) {
  154. if (g_parser != NULL) {
  155. XML_ParserFree(g_parser);
  156. g_parser = NULL;
  157. }
  158. }
  159. /* Generate a failure using the parser state to create an error message;
  160. this should be used when the parser reports an error we weren't
  161. expecting.
  162. */
  163. void
  164. _xml_failure(XML_Parser parser, const char *file, int line) {
  165. char buffer[1024];
  166. enum XML_Error err = XML_GetErrorCode(parser);
  167. snprintf(buffer, sizeof(buffer),
  168. " %d: %" XML_FMT_STR " (line %" XML_FMT_INT_MOD
  169. "u, offset %" XML_FMT_INT_MOD "u)\n reported from %s, line %d\n",
  170. err, XML_ErrorString(err), XML_GetCurrentLineNumber(parser),
  171. XML_GetCurrentColumnNumber(parser), file, line);
  172. _fail(file, line, buffer);
  173. }
  174. enum XML_Status
  175. _XML_Parse_SINGLE_BYTES(XML_Parser parser, const char *s, int len,
  176. int isFinal) {
  177. // This ensures that tests have to run pathological parse cases
  178. // (e.g. when `s` is NULL) against plain XML_Parse rather than
  179. // chunking _XML_Parse_SINGLE_BYTES.
  180. assert((parser != NULL) && (s != NULL) && (len >= 0));
  181. const int chunksize = g_chunkSize;
  182. if (chunksize > 0) {
  183. // parse in chunks of `chunksize` bytes as long as not exhausting
  184. for (; len > chunksize; len -= chunksize, s += chunksize) {
  185. enum XML_Status res = XML_Parse(parser, s, chunksize, XML_FALSE);
  186. if (res != XML_STATUS_OK) {
  187. if ((res == XML_STATUS_SUSPENDED) && (len > chunksize)) {
  188. fail("Use of function _XML_Parse_SINGLE_BYTES with a chunk size "
  189. "greater than 0 (from g_chunkSize) does not work well with "
  190. "suspension. Please consider use of plain XML_Parse at this "
  191. "place in your test, instead.");
  192. }
  193. return res;
  194. }
  195. }
  196. }
  197. // parse the final chunk, the size of which will be <= chunksize
  198. return XML_Parse(parser, s, len, isFinal);
  199. }
  200. void
  201. _expect_failure(const char *text, enum XML_Error errorCode,
  202. const char *errorMessage, const char *file, int lineno) {
  203. if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
  204. == XML_STATUS_OK)
  205. /* Hackish use of _fail() macro, but lets us report
  206. the right filename and line number. */
  207. _fail(file, lineno, errorMessage);
  208. if (XML_GetErrorCode(g_parser) != errorCode)
  209. _xml_failure(g_parser, file, lineno);
  210. }
  211. void
  212. _run_character_check(const char *text, const XML_Char *expected,
  213. const char *file, int line) {
  214. CharData storage;
  215. CharData_Init(&storage);
  216. XML_SetUserData(g_parser, &storage);
  217. XML_SetCharacterDataHandler(g_parser, accumulate_characters);
  218. if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
  219. == XML_STATUS_ERROR)
  220. _xml_failure(g_parser, file, line);
  221. CharData_CheckXMLChars(&storage, expected);
  222. }
  223. void
  224. _run_attribute_check(const char *text, const XML_Char *expected,
  225. const char *file, int line) {
  226. CharData storage;
  227. CharData_Init(&storage);
  228. XML_SetUserData(g_parser, &storage);
  229. XML_SetStartElementHandler(g_parser, accumulate_attribute);
  230. if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
  231. == XML_STATUS_ERROR)
  232. _xml_failure(g_parser, file, line);
  233. CharData_CheckXMLChars(&storage, expected);
  234. }
  235. void
  236. _run_ext_character_check(const char *text, ExtTest *test_data,
  237. const XML_Char *expected, const char *file, int line) {
  238. CharData *const storage = (CharData *)malloc(sizeof(CharData));
  239. CharData_Init(storage);
  240. test_data->storage = storage;
  241. XML_SetUserData(g_parser, test_data);
  242. XML_SetCharacterDataHandler(g_parser, ext_accumulate_characters);
  243. if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
  244. == XML_STATUS_ERROR)
  245. _xml_failure(g_parser, file, line);
  246. CharData_CheckXMLChars(storage, expected);
  247. free(storage);
  248. }
  249. /* Control variable; the number of times duff_allocator() will successfully
  250. * allocate */
  251. #define ALLOC_ALWAYS_SUCCEED (-1)
  252. #define REALLOC_ALWAYS_SUCCEED (-1)
  253. int g_allocation_count = ALLOC_ALWAYS_SUCCEED;
  254. int g_reallocation_count = REALLOC_ALWAYS_SUCCEED;
  255. /* Crocked allocator for allocation failure tests */
  256. void *
  257. duff_allocator(size_t size) {
  258. if (g_allocation_count == 0)
  259. return NULL;
  260. if (g_allocation_count != ALLOC_ALWAYS_SUCCEED)
  261. g_allocation_count--;
  262. return malloc(size);
  263. }
  264. /* Crocked reallocator for allocation failure tests */
  265. void *
  266. duff_reallocator(void *ptr, size_t size) {
  267. if (g_reallocation_count == 0)
  268. return NULL;
  269. if (g_reallocation_count != REALLOC_ALWAYS_SUCCEED)
  270. g_reallocation_count--;
  271. return realloc(ptr, size);
  272. }
  273. // Portable remake of strndup(3) for C99; does not care about space efficiency
  274. char *
  275. portable_strndup(const char *s, size_t n) {
  276. if ((s == NULL) || (n == SIZE_MAX)) {
  277. errno = EINVAL;
  278. return NULL;
  279. }
  280. char *const buffer = (char *)malloc(n + 1);
  281. if (buffer == NULL) {
  282. errno = ENOMEM;
  283. return NULL;
  284. }
  285. errno = 0;
  286. memcpy(buffer, s, n);
  287. buffer[n] = '\0';
  288. return buffer;
  289. }