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

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