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 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <assert.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_basic_parse(const char *test_string, int clear_serializer)
  30. {
  31. json_object *new_obj;
  32. new_obj = json_tokener_parse(test_string);
  33. if (clear_serializer)
  34. do_clear_serializer(new_obj);
  35. printf("new_obj.to_string(%s)=%s\n", test_string, json_object_to_json_string(new_obj));
  36. json_object_put(new_obj);
  37. }
  38. static void test_basic_parse()
  39. {
  40. single_basic_parse("\"\003\"", 0);
  41. single_basic_parse("/* hello */\"foo\"", 0);
  42. single_basic_parse("// hello\n\"foo\"", 0);
  43. single_basic_parse("\"foo\"blue", 0);
  44. single_basic_parse("\'foo\'", 0);
  45. single_basic_parse("\"\\u0041\\u0042\\u0043\"", 0);
  46. single_basic_parse("\"\\u4e16\\u754c\\u00df\"", 0);
  47. single_basic_parse("\"\\u4E16\"", 0);
  48. single_basic_parse("\"\\u4e1\"", 0);
  49. single_basic_parse("\"\\u4e1@\"", 0);
  50. single_basic_parse("\"\\ud840\\u4e16\"", 0);
  51. single_basic_parse("\"\\ud840\"", 0);
  52. single_basic_parse("\"\\udd27\"", 0);
  53. // Test with a "short" high surrogate
  54. single_basic_parse("[9,'\\uDAD", 0);
  55. single_basic_parse("null", 0);
  56. single_basic_parse("NaN", 0);
  57. single_basic_parse("-NaN", 0); /* non-sensical, returns null */
  58. single_basic_parse("Inf", 0); /* must use full string, returns null */
  59. single_basic_parse("inf", 0); /* must use full string, returns null */
  60. single_basic_parse("Infinity", 0);
  61. single_basic_parse("infinity", 0);
  62. single_basic_parse("-Infinity", 0);
  63. single_basic_parse("-infinity", 0);
  64. single_basic_parse("{ \"min\": Infinity, \"max\": -Infinity}", 0);
  65. single_basic_parse("Infinity!", 0);
  66. single_basic_parse("Infinitynull", 0);
  67. single_basic_parse("InfinityXXXX", 0);
  68. single_basic_parse("-Infinitynull", 0);
  69. single_basic_parse("-InfinityXXXX", 0);
  70. single_basic_parse("Infinoodle", 0);
  71. single_basic_parse("InfinAAA", 0);
  72. single_basic_parse("-Infinoodle", 0);
  73. single_basic_parse("-InfinAAA", 0);
  74. single_basic_parse("True", 0);
  75. single_basic_parse("False", 0);
  76. /* not case sensitive */
  77. single_basic_parse("tRue", 0);
  78. single_basic_parse("fAlse", 0);
  79. single_basic_parse("nAn", 0);
  80. single_basic_parse("iNfinity", 0);
  81. single_basic_parse("12", 0);
  82. single_basic_parse("12.3", 0);
  83. single_basic_parse("12.3.4", 0); /* non-sensical, returns null */
  84. /* was returning (int)2015 before patch, should return null */
  85. single_basic_parse("2015-01-15", 0);
  86. /* ...but this works. It's rather inconsistent, and a future major release
  87. * should change the behavior so it either always returns null when extra
  88. * bytes are present (preferred), or always return object created from as much
  89. * as was able to be parsed.
  90. */
  91. single_basic_parse("12.3xxx", 0);
  92. single_basic_parse("{\"FoO\" : -12.3E512}", 0);
  93. single_basic_parse("{\"FoO\" : -12.3e512}", 0);
  94. single_basic_parse("{\"FoO\" : -12.3E51.2}", 0); /* non-sensical, returns null */
  95. single_basic_parse("{\"FoO\" : -12.3E512E12}", 0); /* non-sensical, returns null */
  96. single_basic_parse("[\"\\n\"]", 0);
  97. single_basic_parse("[\"\\nabc\\n\"]", 0);
  98. single_basic_parse("[null]", 0);
  99. single_basic_parse("[]", 0);
  100. single_basic_parse("[false]", 0);
  101. single_basic_parse("[\"abc\",null,\"def\",12]", 0);
  102. single_basic_parse("{}", 0);
  103. single_basic_parse("{ \"foo\": \"bar\" }", 0);
  104. single_basic_parse("{ \'foo\': \'bar\' }", 0);
  105. single_basic_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }", 0);
  106. single_basic_parse("{ \"foo\": [null, \"foo\"] }", 0);
  107. single_basic_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }", 0);
  108. single_basic_parse("{ \"abc\": \"blue\nred\\ngreen\" }", 0);
  109. // Clear serializer for these tests so we see the actual parsed value.
  110. single_basic_parse("null", 1);
  111. single_basic_parse("false", 1);
  112. single_basic_parse("[0e]", 1);
  113. single_basic_parse("[0e+]", 1);
  114. single_basic_parse("[0e+-1]", 1);
  115. single_basic_parse("[18446744073709551616]", 1);
  116. single_basic_parse("\"hello world!\"", 1);
  117. }
  118. static void test_utf8_parse()
  119. {
  120. // json_tokener_parse doesn't support checking for byte order marks.
  121. // It's the responsibility of the caller to detect and skip a BOM.
  122. // Both of these checks return null.
  123. char utf8_bom[] = { 0xEF, 0xBB, 0xBF, 0x00 };
  124. char utf8_bom_and_chars[] = { 0xEF, 0xBB, 0xBF, '{', '}', 0x00 };
  125. single_basic_parse(utf8_bom, 0);
  126. single_basic_parse(utf8_bom_and_chars, 0);
  127. }
  128. // Clear the re-serialization information that the tokener
  129. // saves to ensure that the output reflects the actual
  130. // values we parsed, rather than just the original input.
  131. static void do_clear_serializer(json_object *jso)
  132. {
  133. json_c_visit(jso, 0, clear_serializer, NULL);
  134. }
  135. static int clear_serializer(json_object *jso, int flags,
  136. json_object *parent_jso,
  137. const char *jso_key,
  138. size_t *jso_index, void *userarg)
  139. {
  140. if (jso)
  141. json_object_set_serializer(jso, NULL, NULL, NULL);
  142. return JSON_C_VISIT_RETURN_CONTINUE;
  143. }
  144. static void test_verbose_parse()
  145. {
  146. json_object *new_obj;
  147. enum json_tokener_error error = json_tokener_success;
  148. new_obj = json_tokener_parse_verbose("{ foo }", &error);
  149. assert (error == json_tokener_error_parse_object_key_name);
  150. assert (new_obj == NULL);
  151. new_obj = json_tokener_parse("{ foo }");
  152. assert (new_obj == NULL);
  153. new_obj = json_tokener_parse("foo");
  154. assert (new_obj == NULL);
  155. new_obj = json_tokener_parse_verbose("foo", &error);
  156. assert (new_obj == NULL);
  157. /* b/c the string starts with 'f' parsing return a boolean error */
  158. assert (error == json_tokener_error_parse_boolean);
  159. puts("json_tokener_parse_verbose() OK");
  160. }
  161. struct incremental_step {
  162. const char *string_to_parse;
  163. int length;
  164. int char_offset;
  165. enum json_tokener_error expected_error;
  166. int reset_tokener;
  167. } incremental_steps[] = {
  168. /* Check that full json messages can be parsed, both w/ and w/o a reset */
  169. { "{ \"foo\": 123 }", -1, -1, json_tokener_success, 0 },
  170. { "{ \"foo\": 456 }", -1, -1, json_tokener_success, 1 },
  171. { "{ \"foo\": 789 }", -1, -1, json_tokener_success, 1 },
  172. /* Check the comment parse*/
  173. { "/* hello */{ \"foo\"", -1, -1, json_tokener_continue, 0 },
  174. { "/* hello */:/* hello */", -1, -1, json_tokener_continue, 0 },
  175. { "\"bar\"/* hello */", -1, -1, json_tokener_continue, 0 },
  176. { "}/* hello */", -1, -1, json_tokener_success, 1 },
  177. { "/ hello ", -1, 1, json_tokener_error_parse_comment, 1 },
  178. { "/* hello\"foo\"", -1, -1, json_tokener_continue, 1 },
  179. { "/* hello*\"foo\"", -1, -1, json_tokener_continue, 1 },
  180. { "// hello\"foo\"", -1, -1, json_tokener_continue, 1 },
  181. /* Check a basic incremental parse */
  182. { "{ \"foo", -1, -1, json_tokener_continue, 0 },
  183. { "\": {\"bar", -1, -1, json_tokener_continue, 0 },
  184. { "\":13}}", -1, -1, json_tokener_success, 1 },
  185. /* Check that json_tokener_reset actually resets */
  186. { "{ \"foo", -1, -1, json_tokener_continue, 1 },
  187. { ": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1 },
  188. /* Check incremental parsing with trailing characters */
  189. { "{ \"foo", -1, -1, json_tokener_continue, 0 },
  190. { "\": {\"bar", -1, -1, json_tokener_continue, 0 },
  191. { "\":13}}XXXX", 10, 6, json_tokener_success, 0 },
  192. { "XXXX", 4, 0, json_tokener_error_parse_unexpected, 1 },
  193. /* Check that trailing characters can change w/o a reset */
  194. { "{\"x\": 123 }\"X\"", -1, 11, json_tokener_success, 0 },
  195. { "\"Y\"", -1, -1, json_tokener_success, 1 },
  196. /* To stop parsing a number we need to reach a non-digit, e.g. a \0 */
  197. { "1", 1, 1, json_tokener_continue, 0 },
  198. /* This should parse as the number 12, since it continues the "1" */
  199. { "2", 2, 1, json_tokener_success, 0 },
  200. { "12{", 3, 2, json_tokener_success, 1 },
  201. /* Parse number in strict model */
  202. { "[02]", -1, 3, json_tokener_error_parse_number, 3 },
  203. /* Similar tests for other kinds of objects: */
  204. /* These could all return success immediately, since regardless of
  205. what follows the false/true/null token we *will* return a json object,
  206. but it currently doesn't work that way. hmm... */
  207. { "false", 5, 5, json_tokener_continue, 1 },
  208. { "false", 6, 5, json_tokener_success, 1 },
  209. { "true", 4, 4, json_tokener_continue, 1 },
  210. { "true", 5, 4, json_tokener_success, 1 },
  211. { "null", 4, 4, json_tokener_continue, 1 },
  212. { "null", 5, 4, json_tokener_success, 1 },
  213. { "Infinity", 9, 8, json_tokener_success, 1 },
  214. { "infinity", 9, 8, json_tokener_success, 1 },
  215. { "-infinity", 10, 9, json_tokener_success, 1 },
  216. { "infinity", 9, 0, json_tokener_error_parse_unexpected, 3 },
  217. { "-infinity", 10, 1, json_tokener_error_parse_unexpected, 3 },
  218. { "inf", 3, 3, json_tokener_continue, 0 },
  219. { "inity", 6, 5, json_tokener_success, 1 },
  220. { "-inf", 4, 4, json_tokener_continue, 0 },
  221. { "inity", 6, 5, json_tokener_success, 1 },
  222. { "i", 1, 1, json_tokener_continue, 0 },
  223. { "n", 1, 1, json_tokener_continue, 0 },
  224. { "f", 1, 1, json_tokener_continue, 0 },
  225. { "i", 1, 1, json_tokener_continue, 0 },
  226. { "n", 1, 1, json_tokener_continue, 0 },
  227. { "i", 1, 1, json_tokener_continue, 0 },
  228. { "t", 1, 1, json_tokener_continue, 0 },
  229. { "y", 1, 1, json_tokener_continue, 0 },
  230. { "", 1, 0, json_tokener_success, 1 },
  231. { "-", 1, 1, json_tokener_continue, 0 },
  232. { "inf", 3, 3, json_tokener_continue, 0 },
  233. { "ini", 3, 3, json_tokener_continue, 0 },
  234. { "ty", 3, 2, json_tokener_success, 1 },
  235. { "-", 1, 1, json_tokener_continue, 0 },
  236. { "i", 1, 1, json_tokener_continue, 0 },
  237. { "nfini", 5, 5, json_tokener_continue, 0 },
  238. { "ty", 3, 2, json_tokener_success, 1 },
  239. { "-i", 2, 2, json_tokener_continue, 0 },
  240. { "nfinity", 8, 7, json_tokener_success, 1 },
  241. { "InfinityX", 10, 8, json_tokener_success, 0 },
  242. { "X", 1, 0, json_tokener_error_parse_unexpected, 1 },
  243. { "Infinity1234", 13, 8, json_tokener_success, 0 },
  244. { "1234", 5, 4, json_tokener_success, 1 },
  245. { "Infinity9999", 8, 8, json_tokener_continue, 0 },
  246. /* returns the Infinity loaded up by the previous call: */
  247. { "1234", 5, 0, json_tokener_success, 0 },
  248. { "1234", 5, 4, json_tokener_success, 1 },
  249. /* offset=1 because "n" is the start of "null". hmm... */
  250. { "noodle", 7, 1, json_tokener_error_parse_null, 1 },
  251. /* offset=2 because "na" is the start of "nan". hmm... */
  252. { "naodle", 7, 2, json_tokener_error_parse_null, 1 },
  253. /* offset=2 because "tr" is the start of "true". hmm... */
  254. { "track", 6, 2, json_tokener_error_parse_boolean, 1 },
  255. { "fail", 5, 2, json_tokener_error_parse_boolean, 1 },
  256. /* Although they may initially look like they should fail,
  257. the next few tests check that parsing multiple sequential
  258. json objects in the input works as expected */
  259. { "null123", 9, 4, json_tokener_success, 0 },
  260. { &"null123"[4], 4, 3, json_tokener_success, 1 },
  261. { "nullx", 5, 4, json_tokener_success, 0 },
  262. { &"nullx"[4], 2, 0, json_tokener_error_parse_unexpected, 1 },
  263. { "{\"a\":1}{\"b\":2}",15, 7, json_tokener_success, 0 },
  264. { &"{\"a\":1}{\"b\":2}"[7],
  265. 8, 7, json_tokener_success, 1 },
  266. /* Some bad formatting. Check we get the correct error status */
  267. { "2015-01-15", 10, 4, json_tokener_error_parse_number, 1 },
  268. /* Strings have a well defined end point, so we can stop at the quote */
  269. { "\"blue\"", -1, -1, json_tokener_success, 0 },
  270. /* Check each of the escape sequences defined by the spec */
  271. { "\"\\\"\"", -1, -1, json_tokener_success, 0 },
  272. { "\"\\\\\"", -1, -1, json_tokener_success, 0 },
  273. { "\"\\b\"", -1, -1, json_tokener_success, 0 },
  274. { "\"\\f\"", -1, -1, json_tokener_success, 0 },
  275. { "\"\\n\"", -1, -1, json_tokener_success, 0 },
  276. { "\"\\r\"", -1, -1, json_tokener_success, 0 },
  277. { "\"\\t\"", -1, -1, json_tokener_success, 0 },
  278. { "\"\\/\"", -1, -1, json_tokener_success, 0 },
  279. // Escaping a forward slash is optional
  280. { "\"/\"", -1, -1, json_tokener_success, 0 },
  281. /* Check wrong escape sequences */
  282. { "\"\\a\"", -1, 2, json_tokener_error_parse_string, 1 },
  283. /* Check '\'' in strict model */
  284. { "\'foo\'", -1, 0, json_tokener_error_parse_unexpected, 3 },
  285. /* Parse array/object */
  286. { "[1,2,3]", -1, -1, json_tokener_success, 0 },
  287. { "[1,2,3}", -1, 6, json_tokener_error_parse_array, 1 },
  288. { "{\"a\"}", -1, 4, json_tokener_error_parse_object_key_sep, 1 },
  289. { "{\"a\":1]", -1, 6, json_tokener_error_parse_object_value_sep, 1 },
  290. { "{\"a\"::1}", -1, 5, json_tokener_error_parse_unexpected, 1 },
  291. { "{\"a\":}", -1, 5, json_tokener_error_parse_unexpected, 1 },
  292. { "{\"a\":1,\"a\":2}",-1, -1, json_tokener_success, 1 },
  293. { "\"a\":1}", -1, 3, json_tokener_success, 1 },
  294. { "{\"a\":1", -1, -1, json_tokener_continue, 1 },
  295. { "[,]", -1, 1, json_tokener_error_parse_unexpected, 1 },
  296. { "[,1]", -1, 1, json_tokener_error_parse_unexpected, 1 },
  297. /* This behaviour doesn't entirely follow the json spec, but until we have
  298. a way to specify how strict to be we follow Postel's Law and be liberal
  299. in what we accept (up to a point). */
  300. { "[1,2,3,]", -1, -1, json_tokener_success, 0 },
  301. { "[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0 },
  302. { "[1,2,3,]", -1, 7, json_tokener_error_parse_unexpected, 3 },
  303. { "{\"a\":1,}", -1, 7, json_tokener_error_parse_unexpected, 3 },
  304. { NULL, -1, -1, json_tokener_success, 0 },
  305. };
  306. static void test_incremental_parse()
  307. {
  308. json_object *new_obj;
  309. enum json_tokener_error jerr;
  310. struct json_tokener *tok;
  311. const char *string_to_parse;
  312. int ii;
  313. int num_ok, num_error;
  314. num_ok = 0;
  315. num_error = 0;
  316. printf("Starting incremental tests.\n");
  317. printf("Note: quotes and backslashes seen in the output here are literal values passed\n");
  318. printf(" to the parse functions. e.g. this is 4 characters: \"\\f\"\n");
  319. string_to_parse = "{ \"foo"; /* } */
  320. printf("json_tokener_parse(%s) ... ", string_to_parse);
  321. new_obj = json_tokener_parse(string_to_parse);
  322. if (new_obj == NULL) puts("got error as expected");
  323. /* test incremental parsing in various forms */
  324. tok = json_tokener_new();
  325. for (ii = 0; incremental_steps[ii].string_to_parse != NULL; ii++)
  326. {
  327. int this_step_ok = 0;
  328. struct incremental_step *step = &incremental_steps[ii];
  329. int length = step->length;
  330. size_t expected_char_offset;
  331. if (step->reset_tokener & 2)
  332. json_tokener_set_flags(tok, JSON_TOKENER_STRICT);
  333. else
  334. json_tokener_set_flags(tok, 0);
  335. if (length == -1)
  336. length = strlen(step->string_to_parse);
  337. if (step->char_offset == -1)
  338. expected_char_offset = length;
  339. else
  340. expected_char_offset = step->char_offset;
  341. printf("json_tokener_parse_ex(tok, %-12s, %3d) ... ",
  342. step->string_to_parse, length);
  343. new_obj = json_tokener_parse_ex(tok, step->string_to_parse, length);
  344. jerr = json_tokener_get_error(tok);
  345. if (step->expected_error != json_tokener_success)
  346. {
  347. if (new_obj != NULL)
  348. printf("ERROR: invalid object returned: %s\n",
  349. json_object_to_json_string(new_obj));
  350. else if (jerr != step->expected_error)
  351. printf("ERROR: got wrong error: %s\n",
  352. json_tokener_error_desc(jerr));
  353. else if (json_tokener_get_parse_end(tok) != expected_char_offset)
  354. printf("ERROR: wrong char_offset %zu != expected %zu\n",
  355. json_tokener_get_parse_end(tok),
  356. expected_char_offset);
  357. else
  358. {
  359. printf("OK: got correct error: %s\n",
  360. json_tokener_error_desc(jerr));
  361. this_step_ok = 1;
  362. }
  363. }
  364. else
  365. {
  366. if (new_obj == NULL &&
  367. !(step->length >= 4 &&
  368. strncmp(step->string_to_parse, "null", 4) == 0))
  369. printf("ERROR: expected valid object, instead: %s\n",
  370. json_tokener_error_desc(jerr));
  371. else if (json_tokener_get_parse_end(tok) != expected_char_offset)
  372. printf("ERROR: wrong char_offset %zu != expected %zu\n",
  373. json_tokener_get_parse_end(tok),
  374. expected_char_offset);
  375. else
  376. {
  377. printf("OK: got object of type [%s]: %s\n",
  378. json_type_to_name(json_object_get_type(new_obj)),
  379. json_object_to_json_string(new_obj));
  380. this_step_ok = 1;
  381. }
  382. }
  383. if (new_obj)
  384. json_object_put(new_obj);
  385. if (step->reset_tokener & 1)
  386. json_tokener_reset(tok);
  387. if (this_step_ok)
  388. num_ok++;
  389. else
  390. num_error++;
  391. }
  392. json_tokener_free(tok);
  393. printf("End Incremental Tests OK=%d ERROR=%d\n", num_ok, num_error);
  394. }