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.4 kB

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