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

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