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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. static void test_basic_parse(void);
  9. static void test_verbose_parse(void);
  10. static void test_incremental_parse(void);
  11. int main(void)
  12. {
  13. MC_SET_DEBUG(1);
  14. static const char separator[] = "==================================";
  15. test_basic_parse();
  16. puts(separator);
  17. test_verbose_parse();
  18. puts(separator);
  19. test_incremental_parse();
  20. puts(separator);
  21. }
  22. static void test_basic_parse()
  23. {
  24. json_object *new_obj;
  25. new_obj = json_tokener_parse("\"\003\"");
  26. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  27. json_object_put(new_obj);
  28. new_obj = json_tokener_parse("/* hello */\"foo\"");
  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\n\"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("\"\\u0041\\u0042\\u0043\"");
  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("null");
  38. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  39. json_object_put(new_obj);
  40. new_obj = json_tokener_parse("NaN");
  41. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  42. json_object_put(new_obj);
  43. new_obj = json_tokener_parse("-NaN"); /* non-sensical, returns null */
  44. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  45. json_object_put(new_obj);
  46. new_obj = json_tokener_parse("Inf"); /* must use full string, returns null */
  47. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  48. json_object_put(new_obj);
  49. new_obj = json_tokener_parse("inf"); /* must use full string, returns null */
  50. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  51. json_object_put(new_obj);
  52. new_obj = json_tokener_parse("Infinity");
  53. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  54. json_object_put(new_obj);
  55. new_obj = json_tokener_parse("infinity");
  56. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  57. json_object_put(new_obj);
  58. new_obj = json_tokener_parse("-Infinity");
  59. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  60. json_object_put(new_obj);
  61. new_obj = json_tokener_parse("-infinity");
  62. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  63. json_object_put(new_obj);
  64. new_obj = json_tokener_parse("True");
  65. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  66. json_object_put(new_obj);
  67. new_obj = json_tokener_parse("12");
  68. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  69. json_object_put(new_obj);
  70. new_obj = json_tokener_parse("12.3");
  71. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  72. json_object_put(new_obj);
  73. new_obj = json_tokener_parse("12.3.4"); /* non-sensical, returns null */
  74. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  75. json_object_put(new_obj);
  76. /* was returning (int)2015 before patch, should return null */
  77. new_obj = json_tokener_parse("2015-01-15");
  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("{\"FoO\" : -12.3E512}");
  81. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  82. json_object_put(new_obj);
  83. new_obj = json_tokener_parse("{\"FoO\" : -12.3E51.2}"); /* non-sensical, returns null */
  84. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  85. json_object_put(new_obj);
  86. new_obj = json_tokener_parse("[\"\\n\"]");
  87. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  88. json_object_put(new_obj);
  89. new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
  90. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  91. json_object_put(new_obj);
  92. new_obj = json_tokener_parse("[null]");
  93. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  94. json_object_put(new_obj);
  95. new_obj = json_tokener_parse("[]");
  96. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  97. json_object_put(new_obj);
  98. new_obj = json_tokener_parse("[false]");
  99. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  100. json_object_put(new_obj);
  101. new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
  102. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  103. json_object_put(new_obj);
  104. new_obj = json_tokener_parse("{}");
  105. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  106. json_object_put(new_obj);
  107. new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
  108. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  109. json_object_put(new_obj);
  110. new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
  111. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  112. json_object_put(new_obj);
  113. new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
  114. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  115. json_object_put(new_obj);
  116. new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
  117. printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  118. json_object_put(new_obj);
  119. }
  120. static void test_verbose_parse()
  121. {
  122. json_object *new_obj;
  123. enum json_tokener_error error = json_tokener_success;
  124. new_obj = json_tokener_parse_verbose("{ foo }", &error);
  125. assert (error == json_tokener_error_parse_object_key_name);
  126. assert (new_obj == NULL);
  127. new_obj = json_tokener_parse("{ foo }");
  128. assert (new_obj == NULL);
  129. new_obj = json_tokener_parse("foo");
  130. assert (new_obj == NULL);
  131. new_obj = json_tokener_parse_verbose("foo", &error);
  132. assert (new_obj == NULL);
  133. /* b/c the string starts with 'f' parsing return a boolean error */
  134. assert (error == json_tokener_error_parse_boolean);
  135. puts("json_tokener_parse_versbose() OK");
  136. }
  137. struct incremental_step {
  138. const char *string_to_parse;
  139. int length;
  140. int char_offset;
  141. enum json_tokener_error expected_error;
  142. int reset_tokener;
  143. } incremental_steps[] = {
  144. /* Check that full json messages can be parsed, both w/ and w/o a reset */
  145. { "{ \"foo\": 123 }", -1, -1, json_tokener_success, 0 },
  146. { "{ \"foo\": 456 }", -1, -1, json_tokener_success, 1 },
  147. { "{ \"foo\": 789 }", -1, -1, json_tokener_success, 1 },
  148. /* Check a basic incremental parse */
  149. { "{ \"foo", -1, -1, json_tokener_continue, 0 },
  150. { "\": {\"bar", -1, -1, json_tokener_continue, 0 },
  151. { "\":13}}", -1, -1, json_tokener_success, 1 },
  152. /* Check that json_tokener_reset actually resets */
  153. { "{ \"foo", -1, -1, json_tokener_continue, 1 },
  154. { ": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1 },
  155. /* Check incremental parsing with trailing characters */
  156. { "{ \"foo", -1, -1, json_tokener_continue, 0 },
  157. { "\": {\"bar", -1, -1, json_tokener_continue, 0 },
  158. { "\":13}}XXXX", 10, 6, json_tokener_success, 0 },
  159. { "XXXX", 4, 0, json_tokener_error_parse_unexpected, 1 },
  160. /* Check that trailing characters can change w/o a reset */
  161. { "{\"x\": 123 }\"X\"", -1, 11, json_tokener_success, 0 },
  162. { "\"Y\"", -1, -1, json_tokener_success, 1 },
  163. /* To stop parsing a number we need to reach a non-digit, e.g. a \0 */
  164. { "1", 1, 1, json_tokener_continue, 0 },
  165. { "2", 2, 1, json_tokener_success, 0 },
  166. /* Some bad formatting. Check we get the correct error status */
  167. { "2015-01-15", 10, 4, json_tokener_error_parse_number, 1 },
  168. /* Strings have a well defined end point, so we can stop at the quote */
  169. { "\"blue\"", -1, -1, json_tokener_success, 0 },
  170. /* Check each of the escape sequences defined by the spec */
  171. { "\"\\\"\"", -1, -1, json_tokener_success, 0 },
  172. { "\"\\\\\"", -1, -1, json_tokener_success, 0 },
  173. { "\"\\b\"", -1, -1, json_tokener_success, 0 },
  174. { "\"\\f\"", -1, -1, json_tokener_success, 0 },
  175. { "\"\\n\"", -1, -1, json_tokener_success, 0 },
  176. { "\"\\r\"", -1, -1, json_tokener_success, 0 },
  177. { "\"\\t\"", -1, -1, json_tokener_success, 0 },
  178. { "\"\\/\"", -1, -1, json_tokener_success, 0 },
  179. // Escaping a forward slash is optional
  180. { "\"/\"", -1, -1, json_tokener_success, 0 },
  181. { "[1,2,3]", -1, -1, json_tokener_success, 0 },
  182. /* This behaviour doesn't entirely follow the json spec, but until we have
  183. a way to specify how strict to be we follow Postel's Law and be liberal
  184. in what we accept (up to a point). */
  185. { "[1,2,3,]", -1, -1, json_tokener_success, 0 },
  186. { "[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0 },
  187. { "[1,2,3,]", -1, 7, json_tokener_error_parse_unexpected, 3 },
  188. { "{\"a\":1,}", -1, 7, json_tokener_error_parse_unexpected, 3 },
  189. { NULL, -1, -1, json_tokener_success, 0 },
  190. };
  191. static void test_incremental_parse()
  192. {
  193. json_object *new_obj;
  194. enum json_tokener_error jerr;
  195. json_tokener *tok;
  196. const char *string_to_parse;
  197. int ii;
  198. int num_ok, num_error;
  199. num_ok = 0;
  200. num_error = 0;
  201. printf("Starting incremental tests.\n");
  202. printf("Note: quotes and backslashes seen in the output here are literal values passed\n");
  203. printf(" to the parse functions. e.g. this is 4 characters: \"\\f\"\n");
  204. string_to_parse = "{ \"foo"; /* } */
  205. printf("json_tokener_parse(%s) ... ", string_to_parse);
  206. new_obj = json_tokener_parse(string_to_parse);
  207. if (new_obj == NULL) puts("got error as expected");
  208. /* test incremental parsing in various forms */
  209. tok = json_tokener_new();
  210. for (ii = 0; incremental_steps[ii].string_to_parse != NULL; ii++)
  211. {
  212. int this_step_ok = 0;
  213. struct incremental_step *step = &incremental_steps[ii];
  214. int length = step->length;
  215. int expected_char_offset = step->char_offset;
  216. if (step->reset_tokener & 2)
  217. json_tokener_set_flags(tok, JSON_TOKENER_STRICT);
  218. else
  219. json_tokener_set_flags(tok, 0);
  220. if (length == -1)
  221. length = strlen(step->string_to_parse);
  222. if (expected_char_offset == -1)
  223. expected_char_offset = length;
  224. printf("json_tokener_parse_ex(tok, %-12s, %3d) ... ",
  225. step->string_to_parse, length);
  226. new_obj = json_tokener_parse_ex(tok, step->string_to_parse, length);
  227. jerr = json_tokener_get_error(tok);
  228. if (step->expected_error != json_tokener_success)
  229. {
  230. if (new_obj != NULL)
  231. printf("ERROR: invalid object returned: %s\n",
  232. json_object_to_json_string(new_obj));
  233. else if (jerr != step->expected_error)
  234. printf("ERROR: got wrong error: %s\n",
  235. json_tokener_error_desc(jerr));
  236. else if (tok->char_offset != expected_char_offset)
  237. printf("ERROR: wrong char_offset %d != expected %d\n",
  238. tok->char_offset,
  239. expected_char_offset);
  240. else
  241. {
  242. printf("OK: got correct error: %s\n",
  243. json_tokener_error_desc(jerr));
  244. this_step_ok = 1;
  245. }
  246. }
  247. else
  248. {
  249. if (new_obj == NULL)
  250. printf("ERROR: expected valid object, instead: %s\n",
  251. json_tokener_error_desc(jerr));
  252. else if (tok->char_offset != expected_char_offset)
  253. printf("ERROR: wrong char_offset %d != expected %d\n",
  254. tok->char_offset,
  255. expected_char_offset);
  256. else
  257. {
  258. printf("OK: got object of type [%s]: %s\n",
  259. json_type_to_name(json_object_get_type(new_obj)),
  260. json_object_to_json_string(new_obj));
  261. this_step_ok = 1;
  262. }
  263. }
  264. if (new_obj)
  265. json_object_put(new_obj);
  266. if (step->reset_tokener & 1)
  267. json_tokener_reset(tok);
  268. if (this_step_ok)
  269. num_ok++;
  270. else
  271. num_error++;
  272. }
  273. json_tokener_free(tok);
  274. printf("End Incremental Tests OK=%d ERROR=%d\n", num_ok, num_error);
  275. }