test_load.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2009-2016 Petri Lehtinen <[email protected]>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #include "util.h"
  8. #include <jansson.h>
  9. #include <string.h>
  10. static void file_not_found() {
  11. json_t *json;
  12. json_error_t error;
  13. char *pos;
  14. json = json_load_file("/path/to/nonexistent/file.json", 0, &error);
  15. if (json)
  16. fail("json_load_file returned non-NULL for a nonexistent file");
  17. if (error.line != -1)
  18. fail("json_load_file returned an invalid line number");
  19. /* The error message is locale specific, only check the beginning
  20. of the error message. */
  21. pos = strchr(error.text, ':');
  22. if (!pos)
  23. fail("json_load_file returne an invalid error message");
  24. *pos = '\0';
  25. if (strcmp(error.text, "unable to open /path/to/nonexistent/file.json") != 0)
  26. fail("json_load_file returned an invalid error message");
  27. if (json_error_code(&error) != json_error_cannot_open_file)
  28. fail("json_load_file returned an invalid error code");
  29. }
  30. static void very_long_file_name() {
  31. json_t *json;
  32. json_error_t error;
  33. json = json_load_file("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  34. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  35. 0, &error);
  36. if (json)
  37. fail("json_load_file returned non-NULL for a nonexistent file");
  38. if (error.line != -1)
  39. fail("json_load_file returned an invalid line number");
  40. if (strncmp(error.source, "...aaa", 6) != 0)
  41. fail("error source was set incorrectly");
  42. if (json_error_code(&error) != json_error_cannot_open_file)
  43. fail("error code was set incorrectly");
  44. }
  45. static void reject_duplicates() {
  46. json_error_t error;
  47. if (json_loads("{\"foo\": 1, \"foo\": 2}", JSON_REJECT_DUPLICATES, &error))
  48. fail("json_loads did not detect a duplicate key");
  49. check_error(json_error_duplicate_key, "duplicate object key near '\"foo\"'",
  50. "<string>", 1, 16, 16);
  51. }
  52. static void disable_eof_check() {
  53. json_error_t error;
  54. json_t *json;
  55. const char *text = "{\"foo\": 1} garbage";
  56. if (json_loads(text, 0, &error))
  57. fail("json_loads did not detect garbage after JSON text");
  58. check_error(json_error_end_of_input_expected, "end of file expected near 'garbage'",
  59. "<string>", 1, 18, 18);
  60. json = json_loads(text, JSON_DISABLE_EOF_CHECK, &error);
  61. if (!json)
  62. fail("json_loads failed with JSON_DISABLE_EOF_CHECK");
  63. json_decref(json);
  64. }
  65. static void decode_any() {
  66. json_t *json;
  67. json_error_t error;
  68. json = json_loads("\"foo\"", JSON_DECODE_ANY, &error);
  69. if (!json || !json_is_string(json))
  70. fail("json_load decoded any failed - string");
  71. json_decref(json);
  72. json = json_loads("42", JSON_DECODE_ANY, &error);
  73. if (!json || !json_is_integer(json))
  74. fail("json_load decoded any failed - integer");
  75. json_decref(json);
  76. json = json_loads("true", JSON_DECODE_ANY, &error);
  77. if (!json || !json_is_true(json))
  78. fail("json_load decoded any failed - boolean");
  79. json_decref(json);
  80. json = json_loads("null", JSON_DECODE_ANY, &error);
  81. if (!json || !json_is_null(json))
  82. fail("json_load decoded any failed - null");
  83. json_decref(json);
  84. }
  85. static void decode_int_as_real() {
  86. json_t *json;
  87. json_error_t error;
  88. #if JSON_INTEGER_IS_LONG_LONG
  89. const char *imprecise;
  90. json_int_t expected;
  91. #endif
  92. char big[311];
  93. json = json_loads("42", JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error);
  94. if (!json || !json_is_real(json) || json_real_value(json) != 42.0)
  95. fail("json_load decode int as real failed - int");
  96. json_decref(json);
  97. #if JSON_INTEGER_IS_LONG_LONG
  98. /* This number cannot be represented exactly by a double */
  99. imprecise = "9007199254740993";
  100. expected = 9007199254740992ll;
  101. json = json_loads(imprecise, JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error);
  102. if (!json || !json_is_real(json) || expected != (json_int_t)json_real_value(json))
  103. fail("json_load decode int as real failed - expected imprecision");
  104. json_decref(json);
  105. #endif
  106. /* 1E309 overflows. Here we create 1E309 as a decimal number, i.e.
  107. 1000...(309 zeroes)...0. */
  108. big[0] = '1';
  109. memset(big + 1, '0', 309);
  110. big[310] = '\0';
  111. json = json_loads(big, JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error);
  112. if (json || strcmp(error.text, "real number overflow") != 0 ||
  113. json_error_code(&error) != json_error_numeric_overflow)
  114. fail("json_load decode int as real failed - expected overflow");
  115. json_decref(json);
  116. }
  117. static void allow_nul() {
  118. const char *text = "\"nul byte \\u0000 in string\"";
  119. const char *expected = "nul byte \0 in string";
  120. size_t len = 20;
  121. json_t *json;
  122. json = json_loads(text, JSON_ALLOW_NUL | JSON_DECODE_ANY, NULL);
  123. if (!json || !json_is_string(json))
  124. fail("unable to decode embedded NUL byte");
  125. if (json_string_length(json) != len)
  126. fail("decoder returned wrong string length");
  127. if (memcmp(json_string_value(json), expected, len + 1))
  128. fail("decoder returned wrong string content");
  129. json_decref(json);
  130. }
  131. static void load_wrong_args() {
  132. json_t *json;
  133. json_error_t error;
  134. json = json_loads(NULL, 0, &error);
  135. if (json)
  136. fail("json_loads should return NULL if the first argument is NULL");
  137. json = json_loadb(NULL, 0, 0, &error);
  138. if (json)
  139. fail("json_loadb should return NULL if the first argument is NULL");
  140. json = json_loadf(NULL, 0, &error);
  141. if (json)
  142. fail("json_loadf should return NULL if the first argument is NULL");
  143. json = json_loadfd(-1, 0, &error);
  144. if (json)
  145. fail("json_loadfd should return NULL if the first argument is < 0");
  146. json = json_load_file(NULL, 0, &error);
  147. if (json)
  148. fail("json_load_file should return NULL if the first argument is NULL");
  149. }
  150. static void position() {
  151. json_t *json;
  152. size_t flags = JSON_DISABLE_EOF_CHECK;
  153. json_error_t error;
  154. json = json_loads("{\"foo\": \"bar\"}", 0, &error);
  155. if (error.position != 14)
  156. fail("json_loads returned a wrong position");
  157. json_decref(json);
  158. json = json_loads("{\"foo\": \"bar\"} baz quux", flags, &error);
  159. if (error.position != 14)
  160. fail("json_loads returned a wrong position");
  161. json_decref(json);
  162. }
  163. static void error_code() {
  164. json_error_t error;
  165. json_t *json = json_loads("[123] garbage", 0, &error);
  166. if (json != NULL)
  167. fail("json_loads returned not NULL");
  168. if (strlen(error.text) >= JSON_ERROR_TEXT_LENGTH)
  169. fail("error.text longer than expected");
  170. if (json_error_code(&error) != json_error_end_of_input_expected)
  171. fail("json_loads returned incorrect error code");
  172. json = json_loads("{\"foo\": ", 0, &error);
  173. if (json != NULL)
  174. fail("json_loads returned not NULL");
  175. if (strlen(error.text) >= JSON_ERROR_TEXT_LENGTH)
  176. fail("error.text longer than expected");
  177. if (json_error_code(&error) != json_error_premature_end_of_input)
  178. fail("json_loads returned incorrect error code");
  179. }
  180. static void run_tests() {
  181. file_not_found();
  182. very_long_file_name();
  183. reject_duplicates();
  184. disable_eof_check();
  185. decode_any();
  186. decode_int_as_real();
  187. allow_nul();
  188. load_wrong_args();
  189. position();
  190. error_code();
  191. }