common.c 12 KB

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