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

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