test_load.c 6.0 KB

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