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

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