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 6.8 kB

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