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