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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. _json_c_strerror_enable = 1;
  118. const char *testdir;
  119. if (argc < 2)
  120. {
  121. fprintf(stderr,
  122. "Usage: %s <testdir>\n"
  123. " <testdir> is the location of input files\n",
  124. argv[0]);
  125. return EXIT_FAILURE;
  126. }
  127. testdir = argv[1];
  128. // Test json_c_version.c
  129. if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
  130. {
  131. printf("FAIL: Output from json_c_version(): %s "
  132. "does not match %s",
  133. json_c_version(), JSON_C_VERSION);
  134. return EXIT_FAILURE;
  135. }
  136. if (json_c_version_num() != JSON_C_VERSION_NUM)
  137. {
  138. printf("FAIL: Output from json_c_version_num(): %d "
  139. "does not match %d",
  140. json_c_version_num(), JSON_C_VERSION_NUM);
  141. return EXIT_FAILURE;
  142. }
  143. test_read_valid_with_fd(testdir);
  144. test_read_valid_nested_with_fd(testdir);
  145. test_read_nonexistant();
  146. test_read_closed();
  147. test_write_to_file();
  148. test_read_fd_equal(testdir);
  149. return EXIT_SUCCESS;
  150. }
  151. static void test_read_valid_with_fd(const char *testdir)
  152. {
  153. char filename[PATH_MAX];
  154. (void)snprintf(filename, sizeof(filename), "%s/valid.json", testdir);
  155. int d = open(filename, O_RDONLY, 0);
  156. if (d < 0)
  157. {
  158. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  159. exit(EXIT_FAILURE);
  160. }
  161. json_object *jso = json_object_from_fd(d);
  162. if (jso != NULL)
  163. {
  164. printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(jso));
  165. json_object_put(jso);
  166. }
  167. else
  168. {
  169. fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
  170. json_util_get_last_err());
  171. }
  172. close(d);
  173. }
  174. static void test_read_valid_nested_with_fd(const char *testdir)
  175. {
  176. char filename[PATH_MAX];
  177. (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
  178. int d = open(filename, O_RDONLY, 0);
  179. if (d < 0)
  180. {
  181. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  182. exit(EXIT_FAILURE);
  183. }
  184. assert(NULL == json_object_from_fd_ex(d, -2));
  185. json_object *jso = json_object_from_fd_ex(d, 20);
  186. if (jso != NULL)
  187. {
  188. printf("OK: json_object_from_fd_ex(valid_nested.json, 20)=%s\n",
  189. json_object_to_json_string(jso));
  190. json_object_put(jso);
  191. }
  192. else
  193. {
  194. fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
  195. json_util_get_last_err());
  196. }
  197. (void)lseek(d, SEEK_SET, 0);
  198. jso = json_object_from_fd_ex(d, 3);
  199. if (jso != NULL)
  200. {
  201. printf("FAIL: json_object_from_fd_ex(%s, 3)=%s\n", filename,
  202. json_object_to_json_string(jso));
  203. json_object_put(jso);
  204. }
  205. else
  206. {
  207. printf("OK: correctly unable to parse contents of valid_nested.json with low max "
  208. "depth: %s\n",
  209. json_util_get_last_err());
  210. }
  211. close(d);
  212. }
  213. static void test_read_nonexistant()
  214. {
  215. const char *filename = "./not_present.json";
  216. json_object *jso = json_object_from_file(filename);
  217. if (jso != NULL)
  218. {
  219. printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n", filename,
  220. (void *)jso);
  221. json_object_put(jso);
  222. }
  223. else
  224. {
  225. printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n", filename,
  226. json_util_get_last_err());
  227. }
  228. }
  229. static void test_read_closed()
  230. {
  231. // Test reading from a closed fd
  232. int d = open("/dev/null", O_RDONLY, 0);
  233. if (d < 0)
  234. {
  235. puts("FAIL: unable to open");
  236. }
  237. // Copy over to a fixed fd number so test output is consistent.
  238. int fixed_d = 10;
  239. if (dup2(d, fixed_d) < 0)
  240. {
  241. printf("FAIL: unable to dup to fd %d", fixed_d);
  242. }
  243. close(d);
  244. close(fixed_d);
  245. json_object *jso = json_object_from_fd(fixed_d);
  246. if (jso != NULL)
  247. {
  248. printf("FAIL: read from closed fd returning non-NULL: %p\n", (void *)jso);
  249. fflush(stdout);
  250. printf(" jso=%s\n", json_object_to_json_string(jso));
  251. json_object_put(jso);
  252. return;
  253. }
  254. printf("OK: json_object_from_fd(closed_fd), "
  255. "expecting NULL, EBADF, got:NULL, %s\n",
  256. json_util_get_last_err());
  257. }
  258. static void test_read_fd_equal(const char *testdir)
  259. {
  260. char filename[PATH_MAX];
  261. (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
  262. json_object *jso = json_object_from_file(filename);
  263. int d = open(filename, O_RDONLY, 0);
  264. if (d < 0)
  265. {
  266. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  267. exit(EXIT_FAILURE);
  268. }
  269. json_object *new_jso = json_object_from_fd(d);
  270. close(d);
  271. printf("OK: json_object_from_file(valid.json)=%s\n", json_object_to_json_string(jso));
  272. printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(new_jso));
  273. json_object_put(jso);
  274. json_object_put(new_jso);
  275. }