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_util_file.c 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "strerror_override.h"
  2. #include "strerror_override_private.h"
  3. #ifdef WIN32
  4. #define WIN32_LEAN_AND_MEAN
  5. #include <io.h>
  6. #include <windows.h>
  7. #endif /* defined(WIN32) */
  8. #include <fcntl.h>
  9. #include <limits.h>
  10. #include <stddef.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #if HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif /* HAVE_UNISTD_H */
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #include <assert.h>
  20. #include "json.h"
  21. #include "json_util.h"
  22. #include "snprintf_compat.h"
  23. static void test_read_valid_with_fd(const char *testdir);
  24. static void test_read_valid_nested_with_fd(const char *testdir);
  25. static void test_read_nonexistant();
  26. static void test_read_closed(void);
  27. static void test_write_to_file();
  28. static void stat_and_cat(const char *file);
  29. static void test_read_fd_equal(const char *testdir);
  30. #ifndef PATH_MAX
  31. #define PATH_MAX 256
  32. #endif
  33. static void test_write_to_file()
  34. {
  35. json_object *jso;
  36. jso = json_tokener_parse("{"
  37. "\"foo\":1234,"
  38. "\"foo1\":\"abcdefghijklmnopqrstuvwxyz\","
  39. "\"foo2\":\"abcdefghijklmnopqrstuvwxyz\","
  40. "\"foo3\":\"abcdefghijklmnopqrstuvwxyz\","
  41. "\"foo4\":\"abcdefghijklmnopqrstuvwxyz\","
  42. "\"foo5\":\"abcdefghijklmnopqrstuvwxyz\","
  43. "\"foo6\":\"abcdefghijklmnopqrstuvwxyz\","
  44. "\"foo7\":\"abcdefghijklmnopqrstuvwxyz\","
  45. "\"foo8\":\"abcdefghijklmnopqrstuvwxyz\","
  46. "\"foo9\":\"abcdefghijklmnopqrstuvwxyz\""
  47. "}");
  48. const char *outfile = "json.out";
  49. int rv = json_object_to_file(outfile, jso);
  50. printf("%s: json_object_to_file(%s, jso)=%d\n", (rv == 0) ? "OK" : "FAIL", outfile, rv);
  51. if (rv == 0)
  52. stat_and_cat(outfile);
  53. putchar('\n');
  54. const char *outfile2 = "json2.out";
  55. rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY);
  56. printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
  57. (rv == 0) ? "OK" : "FAIL", outfile2, rv);
  58. if (rv == 0)
  59. stat_and_cat(outfile2);
  60. const char *outfile3 = "json3.out";
  61. int d = open(outfile3, O_WRONLY | O_CREAT, 0600);
  62. if (d < 0)
  63. {
  64. printf("FAIL: unable to open %s %s\n", outfile3, strerror(errno));
  65. return;
  66. }
  67. rv = json_object_to_fd(d, jso, JSON_C_TO_STRING_PRETTY);
  68. printf("%s: json_object_to_fd(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
  69. (rv == 0) ? "OK" : "FAIL", outfile3, rv);
  70. // Write the same object twice
  71. rv = json_object_to_fd(d, jso, JSON_C_TO_STRING_PLAIN);
  72. printf("%s: json_object_to_fd(%s, jso, JSON_C_TO_STRING_PLAIN)=%d\n",
  73. (rv == 0) ? "OK" : "FAIL", outfile3, rv);
  74. close(d);
  75. if (rv == 0)
  76. stat_and_cat(outfile3);
  77. const char *outfile4 = "./test_cast.test";
  78. json_object_to_file_ext(outfile4, jso, JSON_C_TO_STRING_PRETTY);
  79. json_object *new_jso = NULL;
  80. assert(-1 == json_object_to_file(outfile4, new_jso));
  81. d = open(outfile4, O_WRONLY|O_CREAT, 0600);
  82. if (d < 0)
  83. {
  84. printf("FAIL: unable to open %s %s\n", outfile4, strerror(errno));
  85. return;
  86. }
  87. assert(-1 == json_object_to_fd(d, new_jso, JSON_C_TO_STRING_PRETTY));
  88. close(d);
  89. json_object_put(jso);
  90. json_object_put(new_jso);
  91. }
  92. static void stat_and_cat(const char *file)
  93. {
  94. struct stat sb;
  95. int d = open(file, O_RDONLY, 0600);
  96. if (d < 0)
  97. {
  98. printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
  99. return;
  100. }
  101. if (fstat(d, &sb) < 0)
  102. {
  103. printf("FAIL: unable to stat %s: %s\n", file, strerror(errno));
  104. close(d);
  105. return;
  106. }
  107. char *buf = malloc(sb.st_size + 1);
  108. if (!buf)
  109. {
  110. printf("FAIL: unable to allocate memory\n");
  111. close(d);
  112. return;
  113. }
  114. if (read(d, buf, sb.st_size) < sb.st_size)
  115. {
  116. printf("FAIL: unable to read all of %s: %s\n", file, strerror(errno));
  117. free(buf);
  118. close(d);
  119. return;
  120. }
  121. buf[sb.st_size] = '\0';
  122. printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf);
  123. free(buf);
  124. close(d);
  125. }
  126. int main(int argc, char **argv)
  127. {
  128. // json_object_to_file(file, obj);
  129. // json_object_to_file_ext(file, obj, flags);
  130. _json_c_strerror(0);
  131. json_util_get_last_err();
  132. _json_c_strerror_enable = 1;
  133. const char *testdir;
  134. if (argc < 2)
  135. {
  136. fprintf(stderr,
  137. "Usage: %s <testdir>\n"
  138. " <testdir> is the location of input files\n",
  139. argv[0]);
  140. return EXIT_FAILURE;
  141. }
  142. testdir = argv[1];
  143. // Test json_c_version.c
  144. if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
  145. {
  146. printf("FAIL: Output from json_c_version(): %s "
  147. "does not match %s", json_c_version(), JSON_C_VERSION);
  148. return EXIT_FAILURE;
  149. }
  150. if (json_c_version_num() != JSON_C_VERSION_NUM)
  151. {
  152. printf("FAIL: Output from json_c_version_num(): %d "
  153. "does not match %d", json_c_version_num(), JSON_C_VERSION_NUM);
  154. return EXIT_FAILURE;
  155. }
  156. test_read_valid_with_fd(testdir);
  157. test_read_valid_nested_with_fd(testdir);
  158. test_read_nonexistant();
  159. test_read_closed();
  160. test_write_to_file();
  161. test_read_fd_equal(testdir);
  162. return EXIT_SUCCESS;
  163. }
  164. static void test_read_valid_with_fd(const char *testdir)
  165. {
  166. char filename[PATH_MAX];
  167. (void)snprintf(filename, sizeof(filename), "%s/valid.json", testdir);
  168. int d = open(filename, O_RDONLY, 0);
  169. if (d < 0)
  170. {
  171. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  172. exit(EXIT_FAILURE);
  173. }
  174. json_object *jso = json_object_from_fd(d);
  175. if (jso != NULL)
  176. {
  177. printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(jso));
  178. json_object_put(jso);
  179. }
  180. else
  181. {
  182. fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
  183. json_util_get_last_err());
  184. }
  185. close(d);
  186. }
  187. static void test_read_valid_nested_with_fd(const char *testdir)
  188. {
  189. char filename[PATH_MAX];
  190. (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
  191. int d = open(filename, O_RDONLY, 0);
  192. if (d < 0)
  193. {
  194. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  195. exit(EXIT_FAILURE);
  196. }
  197. assert(NULL == json_object_from_fd_ex(d, -2));
  198. json_object *jso = json_object_from_fd_ex(d, 20);
  199. if (jso != NULL)
  200. {
  201. printf("OK: json_object_from_fd_ex(valid_nested.json, 20)=%s\n",
  202. json_object_to_json_string(jso));
  203. json_object_put(jso);
  204. }
  205. else
  206. {
  207. fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
  208. json_util_get_last_err());
  209. }
  210. (void)lseek(d, SEEK_SET, 0);
  211. jso = json_object_from_fd_ex(d, 3);
  212. if (jso != NULL)
  213. {
  214. printf("FAIL: json_object_from_fd_ex(%s, 3)=%s\n", filename,
  215. json_object_to_json_string(jso));
  216. json_object_put(jso);
  217. }
  218. else
  219. {
  220. printf("OK: correctly unable to parse contents of valid_nested.json with low max "
  221. "depth: %s\n",
  222. json_util_get_last_err());
  223. }
  224. close(d);
  225. }
  226. static void test_read_nonexistant()
  227. {
  228. const char *filename = "./not_present.json";
  229. json_object *jso = json_object_from_file(filename);
  230. if (jso != NULL)
  231. {
  232. printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n", filename,
  233. (void *)jso);
  234. json_object_put(jso);
  235. }
  236. else
  237. {
  238. printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n", filename,
  239. json_util_get_last_err());
  240. }
  241. }
  242. static void test_read_closed()
  243. {
  244. // Test reading from a closed fd
  245. int d = open("/dev/null", O_RDONLY, 0);
  246. if (d < 0)
  247. {
  248. puts("FAIL: unable to open");
  249. }
  250. // Copy over to a fixed fd number so test output is consistent.
  251. int fixed_d = 10;
  252. if (dup2(d, fixed_d) < 0)
  253. {
  254. printf("FAIL: unable to dup to fd %d", fixed_d);
  255. }
  256. close(d);
  257. close(fixed_d);
  258. json_object *jso = json_object_from_fd(fixed_d);
  259. if (jso != NULL)
  260. {
  261. printf("FAIL: read from closed fd returning non-NULL: %p\n", (void *)jso);
  262. fflush(stdout);
  263. printf(" jso=%s\n", json_object_to_json_string(jso));
  264. json_object_put(jso);
  265. return;
  266. }
  267. printf("OK: json_object_from_fd(closed_fd), "
  268. "expecting NULL, EBADF, got:NULL, %s\n",
  269. json_util_get_last_err());
  270. }
  271. static void test_read_fd_equal(const char *testdir)
  272. {
  273. char filename[PATH_MAX];
  274. (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
  275. json_object *jso = json_object_from_file(filename);
  276. assert(NULL == json_type_to_name(20));
  277. int d = open(filename, O_RDONLY, 0);
  278. if (d < 0)
  279. {
  280. fprintf(stderr,
  281. "FAIL: unable to open %s: %s\n",
  282. filename, strerror(errno));
  283. exit(EXIT_FAILURE);
  284. }
  285. json_object *new_jso = json_object_from_fd(d);
  286. close(d);
  287. printf("OK: json_object_from_file(valid.json)=%s\n", json_object_to_json_string(jso));
  288. printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(new_jso));
  289. json_object_put(jso);
  290. json_object_put(new_jso);
  291. }