You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_parse.c 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. #include <assert.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "json.h"
  7. #include "json_tokener.h"
  8. #include "json_visit.h"
  9. static void test_basic_parse(void);
  10. static void test_utf8_parse(void);
  11. static void test_verbose_parse(void);
  12. static void test_incremental_parse(void);
  13. int main(void)
  14. {
  15. MC_SET_DEBUG(1);
  16. static const char separator[] = "==================================";
  17. test_basic_parse();
  18. puts(separator);
  19. test_utf8_parse();
  20. puts(separator);
  21. test_verbose_parse();
  22. puts(separator);
  23. test_incremental_parse();
  24. puts(separator);
  25. return 0;
  26. }
  27. static json_c_visit_userfunc clear_serializer;
  28. static void do_clear_serializer(json_object *jso);
  29. static void single_incremental_parse(const char *test_string, int clear_serializer)
  30. {
  31. int ii;
  32. int chunksize = atoi(getenv("TEST_PARSE_CHUNKSIZE"));
  33. struct json_tokener *tok;
  34. enum json_tokener_error jerr;
  35. json_object *all_at_once_obj, *new_obj;
  36. const char *all_at_once_str, *new_str;
  37. all_at_once_obj = json_tokener_parse(test_string);
  38. if (clear_serializer)
  39. do_clear_serializer(all_at_once_obj);
  40. all_at_once_str = json_object_to_json_string(all_at_once_obj);
  41. tok = json_tokener_new();
  42. int test_string_len = strlen(test_string) + 1; // Including '\0' !
  43. for (ii = 0; ii < test_string_len; ii += chunksize)
  44. {
  45. int len_to_parse = chunksize;
  46. if (ii + chunksize > test_string_len)
  47. len_to_parse = test_string_len - ii;
  48. if (getenv("TEST_PARSE_DEBUG") != NULL)
  49. printf(" chunk: %.*s\n", len_to_parse, &test_string[ii]);
  50. new_obj = json_tokener_parse_ex(tok, &test_string[ii], len_to_parse);
  51. jerr = json_tokener_get_error(tok);
  52. if (jerr != json_tokener_continue || new_obj)
  53. break;
  54. }
  55. if (clear_serializer && new_obj)
  56. do_clear_serializer(new_obj);
  57. new_str = json_object_to_json_string(new_obj);
  58. if (strcmp(all_at_once_str, new_str) != 0)
  59. {
  60. printf("ERROR: failed to parse (%s) in %d byte chunks: %s != %s\n",
  61. test_string, chunksize, all_at_once_str, new_str);
  62. }
  63. json_tokener_free(tok);
  64. }
  65. static void single_basic_parse(const char *test_string, int clear_serializer)
  66. {
  67. json_object *new_obj;
  68. new_obj = json_tokener_parse(test_string);
  69. if (clear_serializer)
  70. do_clear_serializer(new_obj);
  71. printf("new_obj.to_string(%s)=%s\n", test_string, json_object_to_json_string(new_obj));
  72. json_object_put(new_obj);
  73. if (getenv("TEST_PARSE_CHUNKSIZE") != NULL)
  74. single_incremental_parse(test_string, clear_serializer);
  75. }
  76. static void test_basic_parse()
  77. {
  78. single_basic_parse("\"\003\"", 0);
  79. single_basic_parse("/* hello */\"foo\"", 0);
  80. single_basic_parse("// hello\n\"foo\"", 0);
  81. single_basic_parse("\"foo\"blue", 0);
  82. single_basic_parse("\'foo\'", 0);
  83. single_basic_parse("\"\\u0041\\u0042\\u0043\"", 0);
  84. single_basic_parse("\"\\u4e16\\u754c\\u00df\"", 0);
  85. single_basic_parse("\"\\u4E16\"", 0);
  86. single_basic_parse("\"\\u4e1\"", 0);
  87. single_basic_parse("\"\\u4e1@\"", 0);
  88. single_basic_parse("\"\\ud840\\u4e16\"", 0);
  89. single_basic_parse("\"\\ud840\"", 0);
  90. single_basic_parse("\"\\udd27\"", 0);
  91. // Test with a "short" high surrogate
  92. single_basic_parse("[9,'\\uDAD", 0);
  93. single_basic_parse("null", 0);
  94. single_basic_parse("NaN", 0);
  95. single_basic_parse("-NaN", 0); /* non-sensical, returns null */
  96. single_basic_parse("Inf", 0); /* must use full string, returns null */
  97. single_basic_parse("inf", 0); /* must use full string, returns null */
  98. single_basic_parse("Infinity", 0);
  99. single_basic_parse("infinity", 0);
  100. single_basic_parse("-Infinity", 0);
  101. single_basic_parse("-infinity", 0);
  102. single_basic_parse("{ \"min\": Infinity, \"max\": -Infinity}", 0);
  103. single_basic_parse("Infinity!", 0);
  104. single_basic_parse("Infinitynull", 0);
  105. single_basic_parse("InfinityXXXX", 0);
  106. single_basic_parse("-Infinitynull", 0);
  107. single_basic_parse("-InfinityXXXX", 0);
  108. single_basic_parse("Infinoodle", 0);
  109. single_basic_parse("InfinAAA", 0);
  110. single_basic_parse("-Infinoodle", 0);
  111. single_basic_parse("-InfinAAA", 0);
  112. single_basic_parse("True", 0);
  113. single_basic_parse("False", 0);
  114. /* not case sensitive */
  115. single_basic_parse("tRue", 0);
  116. single_basic_parse("fAlse", 0);
  117. single_basic_parse("nAn", 0);
  118. single_basic_parse("iNfinity", 0);
  119. single_basic_parse("12", 0);
  120. single_basic_parse("12.3", 0);
  121. single_basic_parse("12.3.4", 0); /* non-sensical, returns null */
  122. /* was returning (int)2015 before patch, should return null */
  123. single_basic_parse("2015-01-15", 0);
  124. /* ...but this works. It's rather inconsistent, and a future major release
  125. * should change the behavior so it either always returns null when extra
  126. * bytes are present (preferred), or always return object created from as much
  127. * as was able to be parsed.
  128. */
  129. single_basic_parse("12.3xxx", 0);
  130. single_basic_parse("{\"FoO\" : -12.3E512}", 0);
  131. single_basic_parse("{\"FoO\" : -12.3e512}", 0);
  132. single_basic_parse("{\"FoO\" : -12.3E51.2}", 0); /* non-sensical, returns null */
  133. single_basic_parse("{\"FoO\" : -12.3E512E12}", 0); /* non-sensical, returns null */
  134. single_basic_parse("[\"\\n\"]", 0);
  135. single_basic_parse("[\"\\nabc\\n\"]", 0);
  136. single_basic_parse("[null]", 0);
  137. single_basic_parse("[]", 0);
  138. single_basic_parse("[false]", 0);
  139. single_basic_parse("[\"abc\",null,\"def\",12]", 0);
  140. single_basic_parse("{}", 0);
  141. single_basic_parse("{ \"foo\": \"bar\" }", 0);
  142. single_basic_parse("{ \'foo\': \'bar\' }", 0);
  143. single_basic_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }", 0);
  144. single_basic_parse("{ \"foo\": [null, \"foo\"] }", 0);
  145. single_basic_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, "
  146. "\"arr\": [ 1, 2, 3, null, 5 ] }",
  147. 0);
  148. single_basic_parse("{ \"abc\": \"blue\nred\\ngreen\" }", 0);
  149. // Clear serializer for these tests so we see the actual parsed value.
  150. single_basic_parse("null", 1);
  151. single_basic_parse("false", 1);
  152. single_basic_parse("[0e]", 1);
  153. single_basic_parse("[0e+]", 1);
  154. single_basic_parse("[0e+-1]", 1);
  155. single_basic_parse("\"hello world!\"", 1);
  156. // uint64/int64 range test
  157. single_basic_parse("[9223372036854775806]", 1);
  158. single_basic_parse("[9223372036854775807]", 1);
  159. single_basic_parse("[9223372036854775808]", 1);
  160. single_basic_parse("[-9223372036854775807]", 1);
  161. single_basic_parse("[-9223372036854775808]", 1);
  162. single_basic_parse("[-9223372036854775809]", 1);
  163. single_basic_parse("[18446744073709551614]", 1);
  164. single_basic_parse("[18446744073709551615]", 1);
  165. single_basic_parse("[18446744073709551616]", 1);
  166. }
  167. static void test_utf8_parse()
  168. {
  169. // json_tokener_parse doesn't support checking for byte order marks.
  170. // It's the responsibility of the caller to detect and skip a BOM.
  171. // Both of these checks return null.
  172. char* utf8_bom = "\xEF\xBB\xBF";
  173. char* utf8_bom_and_chars = "\xEF\xBB\xBF{}";
  174. single_basic_parse(utf8_bom, 0);
  175. single_basic_parse(utf8_bom_and_chars, 0);
  176. }
  177. // Clear the re-serialization information that the tokener
  178. // saves to ensure that the output reflects the actual
  179. // values we parsed, rather than just the original input.
  180. static void do_clear_serializer(json_object *jso)
  181. {
  182. json_c_visit(jso, 0, clear_serializer, NULL);
  183. }
  184. static int clear_serializer(json_object *jso, int flags, json_object *parent_jso,
  185. const char *jso_key, size_t *jso_index, void *userarg)
  186. {
  187. if (jso)
  188. json_object_set_serializer(jso, NULL, NULL, NULL);
  189. return JSON_C_VISIT_RETURN_CONTINUE;
  190. }
  191. static void test_verbose_parse()
  192. {
  193. json_object *new_obj;
  194. enum json_tokener_error error = json_tokener_success;
  195. new_obj = json_tokener_parse_verbose("{ foo }", &error);
  196. assert(error == json_tokener_error_parse_object_key_name);
  197. assert(new_obj == NULL);
  198. new_obj = json_tokener_parse("{ foo }");
  199. assert(new_obj == NULL);
  200. new_obj = json_tokener_parse("foo");
  201. assert(new_obj == NULL);
  202. new_obj = json_tokener_parse_verbose("foo", &error);
  203. assert(new_obj == NULL);
  204. /* b/c the string starts with 'f' parsing return a boolean error */
  205. assert(error == json_tokener_error_parse_boolean);
  206. puts("json_tokener_parse_verbose() OK");
  207. }
  208. struct incremental_step
  209. {
  210. const char *string_to_parse;
  211. int length;
  212. int char_offset;
  213. enum json_tokener_error expected_error;
  214. int reset_tokener; /* Set to 1 to call json_tokener_reset() after parsing */
  215. int tok_flags; /* JSON_TOKENER_* flags to pass to json_tokener_set_flags() */
  216. } incremental_steps[] = {
  217. /* Check that full json messages can be parsed, both w/ and w/o a reset */
  218. {"{ \"foo\": 123 }", -1, -1, json_tokener_success, 0},
  219. {"{ \"foo\": 456 }", -1, -1, json_tokener_success, 1},
  220. {"{ \"foo\": 789 }", -1, -1, json_tokener_success, 1},
  221. /* Check the comment parse*/
  222. {"/* hello */{ \"foo\"", -1, -1, json_tokener_continue, 0},
  223. {"/* hello */:/* hello */", -1, -1, json_tokener_continue, 0},
  224. {"\"bar\"/* hello */", -1, -1, json_tokener_continue, 0},
  225. {"}/* hello */", -1, -1, json_tokener_success, 1},
  226. {"/ hello ", -1, 1, json_tokener_error_parse_comment, 1},
  227. {"/* hello\"foo\"", -1, -1, json_tokener_continue, 1},
  228. {"/* hello*\"foo\"", -1, -1, json_tokener_continue, 1},
  229. {"// hello\"foo\"", -1, -1, json_tokener_continue, 1},
  230. /* Check a basic incremental parse */
  231. {"{ \"foo", -1, -1, json_tokener_continue, 0},
  232. {"\": {\"bar", -1, -1, json_tokener_continue, 0},
  233. {"\":13}}", -1, -1, json_tokener_success, 1},
  234. /* Check the UTF-16 surrogate pair */
  235. /* parse one char at every time */
  236. {"\"\\", -1, -1, json_tokener_continue, 0},
  237. {"u", -1, -1, json_tokener_continue, 0},
  238. {"d", -1, -1, json_tokener_continue, 0},
  239. {"8", -1, -1, json_tokener_continue, 0},
  240. {"3", -1, -1, json_tokener_continue, 0},
  241. {"4", -1, -1, json_tokener_continue, 0},
  242. {"\\", -1, -1, json_tokener_continue, 0},
  243. {"u", -1, -1, json_tokener_continue, 0},
  244. {"d", -1, -1, json_tokener_continue, 0},
  245. {"d", -1, -1, json_tokener_continue, 0},
  246. {"1", -1, -1, json_tokener_continue, 0},
  247. {"e\"", -1, -1, json_tokener_success, 1},
  248. /* parse two char at every time */
  249. {"\"\\u", -1, -1, json_tokener_continue, 0},
  250. {"d8", -1, -1, json_tokener_continue, 0},
  251. {"34", -1, -1, json_tokener_continue, 0},
  252. {"\\u", -1, -1, json_tokener_continue, 0},
  253. {"dd", -1, -1, json_tokener_continue, 0},
  254. {"1e\"", -1, -1, json_tokener_success, 1},
  255. /* check the low surrogate pair */
  256. {"\"\\ud834", -1, -1, json_tokener_continue, 0},
  257. {"\\udd1e\"", -1, -1, json_tokener_success, 1},
  258. {"\"\\ud834\\", -1, -1, json_tokener_continue, 0},
  259. {"udd1e\"", -1, -1, json_tokener_success, 1},
  260. {"\"\\ud834\\u", -1, -1, json_tokener_continue, 0},
  261. {"dd1e\"", -1, -1, json_tokener_success, 1},
  262. /* Check that json_tokener_reset actually resets */
  263. {"{ \"foo", -1, -1, json_tokener_continue, 1},
  264. {": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1},
  265. /* Check incremental parsing with trailing characters */
  266. {"{ \"foo", -1, -1, json_tokener_continue, 0},
  267. {"\": {\"bar", -1, -1, json_tokener_continue, 0},
  268. {"\":13}}XXXX", 10, 6, json_tokener_success, 0},
  269. {"XXXX", 4, 0, json_tokener_error_parse_unexpected, 1},
  270. /* Check that trailing characters can change w/o a reset */
  271. {"{\"x\": 123 }\"X\"", -1, 11, json_tokener_success, 0},
  272. {"\"Y\"", -1, -1, json_tokener_success, 1},
  273. /* Trailing characters should cause a failure in strict mode */
  274. {"{\"foo\":9}{\"bar\":8}", -1, 9, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
  275. /* ... unless explicitly allowed. */
  276. {"{\"foo\":9}{\"bar\":8}", -1, 9, json_tokener_success, 0,
  277. JSON_TOKENER_STRICT | JSON_TOKENER_ALLOW_TRAILING_CHARS},
  278. {"{\"b\":8}ignored garbage", -1, 7, json_tokener_success, 1,
  279. JSON_TOKENER_STRICT | JSON_TOKENER_ALLOW_TRAILING_CHARS},
  280. /* To stop parsing a number we need to reach a non-digit, e.g. a \0 */
  281. {"1", 1, 1, json_tokener_continue, 0},
  282. /* This should parse as the number 12, since it continues the "1" */
  283. {"2", 2, 1, json_tokener_success, 0},
  284. {"12{", 3, 2, json_tokener_success, 1},
  285. /* Parse number in strict model */
  286. {"[02]", -1, 3, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
  287. /* Similar tests for other kinds of objects: */
  288. /* These could all return success immediately, since regardless of
  289. what follows the false/true/null token we *will* return a json object,
  290. but it currently doesn't work that way. hmm... */
  291. {"false", 5, 5, json_tokener_continue, 1},
  292. {"false", 6, 5, json_tokener_success, 1},
  293. {"true", 4, 4, json_tokener_continue, 1},
  294. {"true", 5, 4, json_tokener_success, 1},
  295. {"null", 4, 4, json_tokener_continue, 1},
  296. {"null", 5, 4, json_tokener_success, 1},
  297. {"Infinity", 9, 8, json_tokener_success, 1},
  298. {"infinity", 9, 8, json_tokener_success, 1},
  299. {"-infinity", 10, 9, json_tokener_success, 1},
  300. {"infinity", 9, 0, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
  301. {"-infinity", 10, 1, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
  302. {"inf", 3, 3, json_tokener_continue, 0},
  303. {"inity", 6, 5, json_tokener_success, 1},
  304. {"-inf", 4, 4, json_tokener_continue, 0},
  305. {"inity", 6, 5, json_tokener_success, 1},
  306. {"i", 1, 1, json_tokener_continue, 0},
  307. {"n", 1, 1, json_tokener_continue, 0},
  308. {"f", 1, 1, json_tokener_continue, 0},
  309. {"i", 1, 1, json_tokener_continue, 0},
  310. {"n", 1, 1, json_tokener_continue, 0},
  311. {"i", 1, 1, json_tokener_continue, 0},
  312. {"t", 1, 1, json_tokener_continue, 0},
  313. {"y", 1, 1, json_tokener_continue, 0},
  314. {"", 1, 0, json_tokener_success, 1},
  315. {"-", 1, 1, json_tokener_continue, 0},
  316. {"inf", 3, 3, json_tokener_continue, 0},
  317. {"ini", 3, 3, json_tokener_continue, 0},
  318. {"ty", 3, 2, json_tokener_success, 1},
  319. {"-", 1, 1, json_tokener_continue, 0},
  320. {"i", 1, 1, json_tokener_continue, 0},
  321. {"nfini", 5, 5, json_tokener_continue, 0},
  322. {"ty", 3, 2, json_tokener_success, 1},
  323. {"-i", 2, 2, json_tokener_continue, 0},
  324. {"nfinity", 8, 7, json_tokener_success, 1},
  325. {"InfinityX", 10, 8, json_tokener_success, 0},
  326. {"X", 1, 0, json_tokener_error_parse_unexpected, 1},
  327. {"Infinity1234", 13, 8, json_tokener_success, 0},
  328. {"1234", 5, 4, json_tokener_success, 1},
  329. {"Infinity9999", 8, 8, json_tokener_continue, 0},
  330. /* returns the Infinity loaded up by the previous call: */
  331. {"1234", 5, 0, json_tokener_success, 0},
  332. {"1234", 5, 4, json_tokener_success, 1},
  333. /* offset=1 because "n" is the start of "null". hmm... */
  334. {"noodle", 7, 1, json_tokener_error_parse_null, 1},
  335. /* offset=2 because "na" is the start of "nan". hmm... */
  336. {"naodle", 7, 2, json_tokener_error_parse_null, 1},
  337. /* offset=2 because "tr" is the start of "true". hmm... */
  338. {"track", 6, 2, json_tokener_error_parse_boolean, 1},
  339. {"fail", 5, 2, json_tokener_error_parse_boolean, 1},
  340. /* Although they may initially look like they should fail,
  341. * the next few tests check that parsing multiple sequential
  342. * json objects in the input works as expected
  343. */
  344. {"null123", 8, 4, json_tokener_success, 0},
  345. {&"null123"[4], 4, 3, json_tokener_success, 1},
  346. {"nullx", 6, 4, json_tokener_success, 0},
  347. {&"nullx"[4], 2, 0, json_tokener_error_parse_unexpected, 1},
  348. {"{\"a\":1}{\"b\":2}", 15, 7, json_tokener_success, 0},
  349. {&"{\"a\":1}{\"b\":2}"[7], 8, 7, json_tokener_success, 1},
  350. /* Some bad formatting. Check we get the correct error status
  351. * XXX this means we can't have two numbers in the incremental parse
  352. * XXX stream with the second one being a negative number!
  353. */
  354. {"2015-01-15", 10, 4, json_tokener_error_parse_number, 1},
  355. /* Strings have a well defined end point, so we can stop at the quote */
  356. {"\"blue\"", -1, -1, json_tokener_success, 0},
  357. /* Check each of the escape sequences defined by the spec */
  358. {"\"\\\"\"", -1, -1, json_tokener_success, 0},
  359. {"\"\\\\\"", -1, -1, json_tokener_success, 0},
  360. {"\"\\b\"", -1, -1, json_tokener_success, 0},
  361. {"\"\\f\"", -1, -1, json_tokener_success, 0},
  362. {"\"\\n\"", -1, -1, json_tokener_success, 0},
  363. {"\"\\r\"", -1, -1, json_tokener_success, 0},
  364. {"\"\\t\"", -1, -1, json_tokener_success, 0},
  365. {"\"\\/\"", -1, -1, json_tokener_success, 0},
  366. // Escaping a forward slash is optional
  367. {"\"/\"", -1, -1, json_tokener_success, 0},
  368. /* Check wrong escape sequences */
  369. {"\"\\a\"", -1, 2, json_tokener_error_parse_string, 1},
  370. /* Check '\'' in strict model */
  371. {"\'foo\'", -1, 0, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
  372. /* Parse array/object */
  373. {"[1,2,3]", -1, -1, json_tokener_success, 0},
  374. {"[1,2,3}", -1, 6, json_tokener_error_parse_array, 1},
  375. {"{\"a\"}", -1, 4, json_tokener_error_parse_object_key_sep, 1},
  376. {"{\"a\":1]", -1, 6, json_tokener_error_parse_object_value_sep, 1},
  377. {"{\"a\"::1}", -1, 5, json_tokener_error_parse_unexpected, 1},
  378. {"{\"a\":}", -1, 5, json_tokener_error_parse_unexpected, 1},
  379. {"{\"a\":1,\"a\":2}", -1, -1, json_tokener_success, 1},
  380. {"\"a\":1}", -1, 3, json_tokener_success, 1},
  381. {"{\"a\":1", -1, -1, json_tokener_continue, 1},
  382. {"[,]", -1, 1, json_tokener_error_parse_unexpected, 1},
  383. {"[,1]", -1, 1, json_tokener_error_parse_unexpected, 1},
  384. /* This behaviour doesn't entirely follow the json spec, but until we have
  385. * a way to specify how strict to be we follow Postel's Law and be liberal
  386. * in what we accept (up to a point).
  387. */
  388. {"[1,2,3,]", -1, -1, json_tokener_success, 0},
  389. {"[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0},
  390. {"[1,2,3,]", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
  391. {"{\"a\":1,}", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
  392. // utf-8 test
  393. // acsll encoding
  394. {"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1,
  395. JSON_TOKENER_VALIDATE_UTF8},
  396. {"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1},
  397. // utf-8 encoding
  398. {"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1,
  399. JSON_TOKENER_VALIDATE_UTF8},
  400. {"\x22\xe4\xb8", -1, 3, json_tokener_error_parse_utf8_string, 0, JSON_TOKENER_VALIDATE_UTF8},
  401. {"\x96\xe7\x95\x8c\x22", -1, 0, json_tokener_error_parse_utf8_string, 1,
  402. JSON_TOKENER_VALIDATE_UTF8},
  403. {"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1},
  404. {"\x22\xcf\x80\xcf\x86\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
  405. {"\x22\xf0\xa5\x91\x95\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
  406. // wrong utf-8 encoding
  407. {"\x22\xe6\x9d\x4e\x22", -1, 3, json_tokener_error_parse_utf8_string, 1,
  408. JSON_TOKENER_VALIDATE_UTF8},
  409. {"\x22\xe6\x9d\x4e\x22", -1, 5, json_tokener_success, 1},
  410. // GBK encoding
  411. {"\x22\xc0\xee\xc5\xf4\x22", -1, 2, json_tokener_error_parse_utf8_string, 1,
  412. JSON_TOKENER_VALIDATE_UTF8},
  413. {"\x22\xc0\xee\xc5\xf4\x22", -1, 6, json_tokener_success, 1},
  414. // char after space
  415. {"\x20\x20\x22\xe4\xb8\x96\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
  416. {"\x20\x20\x81\x22\xe4\xb8\x96\x22", -1, 2, json_tokener_error_parse_utf8_string, 1,
  417. JSON_TOKENER_VALIDATE_UTF8},
  418. {"\x5b\x20\x81\x31\x5d", -1, 2, json_tokener_error_parse_utf8_string, 1,
  419. JSON_TOKENER_VALIDATE_UTF8},
  420. // char in state inf
  421. {"\x49\x6e\x66\x69\x6e\x69\x74\x79", 9, 8, json_tokener_success, 1},
  422. {"\x49\x6e\x66\x81\x6e\x69\x74\x79", -1, 3, json_tokener_error_parse_utf8_string, 1,
  423. JSON_TOKENER_VALIDATE_UTF8},
  424. // char in escape unicode
  425. {"\x22\x5c\x75\x64\x38\x35\x35\x5c\x75\x64\x63\x35\x35\x22", 15, 14, json_tokener_success, 1,
  426. JSON_TOKENER_VALIDATE_UTF8},
  427. {"\x22\x5c\x75\x64\x38\x35\x35\xc0\x75\x64\x63\x35\x35\x22", -1, 8,
  428. json_tokener_error_parse_utf8_string, 1, JSON_TOKENER_VALIDATE_UTF8},
  429. {"\x22\x5c\x75\x64\x30\x30\x33\x31\xc0\x22", -1, 9, json_tokener_error_parse_utf8_string, 1,
  430. JSON_TOKENER_VALIDATE_UTF8},
  431. // char in number
  432. {"\x31\x31\x81\x31\x31", -1, 2, json_tokener_error_parse_utf8_string, 1,
  433. JSON_TOKENER_VALIDATE_UTF8},
  434. // char in object
  435. {"\x7b\x22\x31\x81\x22\x3a\x31\x7d", -1, 3, json_tokener_error_parse_utf8_string, 1,
  436. JSON_TOKENER_VALIDATE_UTF8},
  437. {NULL, -1, -1, json_tokener_success, 0},
  438. };
  439. static void test_incremental_parse()
  440. {
  441. json_object *new_obj;
  442. enum json_tokener_error jerr;
  443. struct json_tokener *tok;
  444. const char *string_to_parse;
  445. int ii;
  446. int num_ok, num_error;
  447. num_ok = 0;
  448. num_error = 0;
  449. printf("Starting incremental tests.\n");
  450. printf("Note: quotes and backslashes seen in the output here are literal values passed\n");
  451. printf(" to the parse functions. e.g. this is 4 characters: \"\\f\"\n");
  452. string_to_parse = "{ \"foo"; /* } */
  453. printf("json_tokener_parse(%s) ... ", string_to_parse);
  454. new_obj = json_tokener_parse(string_to_parse);
  455. if (new_obj == NULL)
  456. puts("got error as expected");
  457. /* test incremental parsing in various forms */
  458. tok = json_tokener_new();
  459. for (ii = 0; incremental_steps[ii].string_to_parse != NULL; ii++)
  460. {
  461. int this_step_ok = 0;
  462. struct incremental_step *step = &incremental_steps[ii];
  463. int length = step->length;
  464. size_t expected_char_offset;
  465. json_tokener_set_flags(tok, step->tok_flags);
  466. if (length == -1)
  467. length = (int)strlen(step->string_to_parse);
  468. if (step->char_offset == -1)
  469. expected_char_offset = length;
  470. else
  471. expected_char_offset = step->char_offset;
  472. printf("json_tokener_parse_ex(tok, %-12s, %3d) ... ", step->string_to_parse,
  473. length);
  474. new_obj = json_tokener_parse_ex(tok, step->string_to_parse, length);
  475. jerr = json_tokener_get_error(tok);
  476. if (step->expected_error != json_tokener_success)
  477. {
  478. if (new_obj != NULL)
  479. printf("ERROR: invalid object returned: %s\n",
  480. json_object_to_json_string(new_obj));
  481. else if (jerr != step->expected_error)
  482. printf("ERROR: got wrong error: %s\n",
  483. json_tokener_error_desc(jerr));
  484. else if (json_tokener_get_parse_end(tok) != expected_char_offset)
  485. printf("ERROR: wrong char_offset %zu != expected %zu\n",
  486. json_tokener_get_parse_end(tok), expected_char_offset);
  487. else
  488. {
  489. printf("OK: got correct error: %s\n",
  490. json_tokener_error_desc(jerr));
  491. this_step_ok = 1;
  492. }
  493. }
  494. else
  495. {
  496. if (new_obj == NULL &&
  497. !(step->length >= 4 && strncmp(step->string_to_parse, "null", 4) == 0))
  498. printf("ERROR: expected valid object, instead: %s\n",
  499. json_tokener_error_desc(jerr));
  500. else if (json_tokener_get_parse_end(tok) != expected_char_offset)
  501. printf("ERROR: wrong char_offset %zu != expected %zu\n",
  502. json_tokener_get_parse_end(tok), expected_char_offset);
  503. else
  504. {
  505. printf("OK: got object of type [%s]: %s\n",
  506. json_type_to_name(json_object_get_type(new_obj)),
  507. json_object_to_json_string(new_obj));
  508. this_step_ok = 1;
  509. }
  510. }
  511. if (new_obj)
  512. json_object_put(new_obj);
  513. if (step->reset_tokener & 1)
  514. json_tokener_reset(tok);
  515. if (this_step_ok)
  516. num_ok++;
  517. else
  518. num_error++;
  519. }
  520. json_tokener_free(tok);
  521. printf("End Incremental Tests OK=%d ERROR=%d\n", num_ok, num_error);
  522. }