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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <assert.h>
  18. #include <sys/stat.h>
  19. #include <sys/types.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(void);
  26. static void test_read_closed(void);
  27. static void test_write_to_file(void);
  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. json_object_put(jso);
  78. }
  79. static void stat_and_cat(const char *file)
  80. {
  81. struct stat sb;
  82. int d = open(file, O_RDONLY, 0600);
  83. if (d < 0)
  84. {
  85. printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
  86. return;
  87. }
  88. if (fstat(d, &sb) < 0)
  89. {
  90. printf("FAIL: unable to stat %s: %s\n", file, strerror(errno));
  91. close(d);
  92. return;
  93. }
  94. char *buf = malloc(sb.st_size + 1);
  95. if (!buf)
  96. {
  97. printf("FAIL: unable to allocate memory\n");
  98. close(d);
  99. return;
  100. }
  101. if (read(d, buf, sb.st_size) < sb.st_size)
  102. {
  103. printf("FAIL: unable to read all of %s: %s\n", file, strerror(errno));
  104. free(buf);
  105. close(d);
  106. return;
  107. }
  108. buf[sb.st_size] = '\0';
  109. printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf);
  110. free(buf);
  111. close(d);
  112. }
  113. int main(int argc, char **argv)
  114. {
  115. // json_object_to_file(file, obj);
  116. // json_object_to_file_ext(file, obj, flags);
  117. const char *testdir;
  118. if (argc < 2)
  119. {
  120. fprintf(stderr,
  121. "Usage: %s <testdir>\n"
  122. " <testdir> is the location of input files\n",
  123. argv[0]);
  124. return EXIT_FAILURE;
  125. }
  126. testdir = argv[1];
  127. // Test json_c_version.c
  128. if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
  129. {
  130. printf("FAIL: Output from json_c_version(): %s "
  131. "does not match %s",
  132. json_c_version(), JSON_C_VERSION);
  133. return EXIT_FAILURE;
  134. }
  135. if (json_c_version_num() != JSON_C_VERSION_NUM)
  136. {
  137. printf("FAIL: Output from json_c_version_num(): %d "
  138. "does not match %d",
  139. json_c_version_num(), JSON_C_VERSION_NUM);
  140. return EXIT_FAILURE;
  141. }
  142. test_read_valid_with_fd(testdir);
  143. test_read_valid_nested_with_fd(testdir);
  144. test_read_nonexistant();
  145. test_read_closed();
  146. test_write_to_file();
  147. test_read_fd_equal(testdir);
  148. return EXIT_SUCCESS;
  149. }
  150. static void test_read_valid_with_fd(const char *testdir)
  151. {
  152. char filename[PATH_MAX];
  153. (void)snprintf(filename, sizeof(filename), "%s/valid.json", testdir);
  154. int d = open(filename, O_RDONLY, 0);
  155. if (d < 0)
  156. {
  157. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  158. exit(EXIT_FAILURE);
  159. }
  160. json_object *jso = json_object_from_fd(d);
  161. if (jso != NULL)
  162. {
  163. printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(jso));
  164. json_object_put(jso);
  165. }
  166. else
  167. {
  168. fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
  169. json_util_get_last_err());
  170. }
  171. close(d);
  172. }
  173. static void test_read_valid_nested_with_fd(const char *testdir)
  174. {
  175. char filename[PATH_MAX];
  176. (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
  177. int d = open(filename, O_RDONLY, 0);
  178. if (d < 0)
  179. {
  180. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  181. exit(EXIT_FAILURE);
  182. }
  183. assert(NULL == json_object_from_fd_ex(d, -2));
  184. json_object *jso = json_object_from_fd_ex(d, 20);
  185. if (jso != NULL)
  186. {
  187. printf("OK: json_object_from_fd_ex(valid_nested.json, 20)=%s\n",
  188. json_object_to_json_string(jso));
  189. json_object_put(jso);
  190. }
  191. else
  192. {
  193. fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
  194. json_util_get_last_err());
  195. }
  196. (void)lseek(d, SEEK_SET, 0);
  197. jso = json_object_from_fd_ex(d, 3);
  198. if (jso != NULL)
  199. {
  200. printf("FAIL: json_object_from_fd_ex(%s, 3)=%s\n", filename,
  201. json_object_to_json_string(jso));
  202. json_object_put(jso);
  203. }
  204. else
  205. {
  206. printf("OK: correctly unable to parse contents of valid_nested.json with low max "
  207. "depth: %s\n",
  208. json_util_get_last_err());
  209. }
  210. close(d);
  211. }
  212. static void test_read_nonexistant()
  213. {
  214. const char *filename = "./not_present.json";
  215. json_object *jso = json_object_from_file(filename);
  216. if (jso != NULL)
  217. {
  218. printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n", filename,
  219. (void *)jso);
  220. json_object_put(jso);
  221. }
  222. else
  223. {
  224. printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n", filename,
  225. json_util_get_last_err());
  226. }
  227. }
  228. static void test_read_closed()
  229. {
  230. // Test reading from a closed fd
  231. int d = open("/dev/null", O_RDONLY, 0);
  232. if (d < 0)
  233. {
  234. puts("FAIL: unable to open");
  235. }
  236. // Copy over to a fixed fd number so test output is consistent.
  237. int fixed_d = 10;
  238. if (dup2(d, fixed_d) < 0)
  239. {
  240. printf("FAIL: unable to dup to fd %d", fixed_d);
  241. }
  242. close(d);
  243. close(fixed_d);
  244. json_object *jso = json_object_from_fd(fixed_d);
  245. if (jso != NULL)
  246. {
  247. printf("FAIL: read from closed fd returning non-NULL: %p\n", (void *)jso);
  248. fflush(stdout);
  249. printf(" jso=%s\n", json_object_to_json_string(jso));
  250. json_object_put(jso);
  251. return;
  252. }
  253. printf("OK: json_object_from_fd(closed_fd), "
  254. "expecting NULL, EBADF, got:NULL, %s\n",
  255. json_util_get_last_err());
  256. }
  257. static void test_read_fd_equal(const char *testdir)
  258. {
  259. char filename[PATH_MAX];
  260. (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
  261. json_object *jso = json_object_from_file(filename);
  262. int d = open(filename, O_RDONLY, 0);
  263. if (d < 0)
  264. {
  265. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  266. exit(EXIT_FAILURE);
  267. }
  268. json_object *new_jso = json_object_from_fd(d);
  269. close(d);
  270. printf("OK: json_object_from_file(valid.json)=%s\n", json_object_to_json_string(jso));
  271. printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(new_jso));
  272. json_object_put(jso);
  273. json_object_put(new_jso);
  274. }