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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "strerror_override.h"
  2. #include "strerror_override_private.h"
  3. #ifdef WIN32
  4. #define WIN32_LEAN_AND_MEAN
  5. #include <windows.h>
  6. #include <io.h>
  7. #endif /* defined(WIN32) */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stddef.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <limits.h>
  14. #if HAVE_UNISTD_H
  15. # include <unistd.h>
  16. #endif /* HAVE_UNISTD_H */
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include "json.h"
  20. #include "json_util.h"
  21. static void test_read_valid_with_fd(const char *testdir);
  22. static void test_read_valid_nested_with_fd(const char *testdir);
  23. static void test_read_nonexistant();
  24. static void test_read_closed(void);
  25. static void test_write_to_file();
  26. static void stat_and_cat(const char *file);
  27. #ifndef PATH_MAX
  28. #define PATH_MAX 256
  29. #endif
  30. static void test_write_to_file()
  31. {
  32. json_object *jso;
  33. jso = json_tokener_parse("{"
  34. "\"foo\":1234,"
  35. "\"foo1\":\"abcdefghijklmnopqrstuvwxyz\","
  36. "\"foo2\":\"abcdefghijklmnopqrstuvwxyz\","
  37. "\"foo3\":\"abcdefghijklmnopqrstuvwxyz\","
  38. "\"foo4\":\"abcdefghijklmnopqrstuvwxyz\","
  39. "\"foo5\":\"abcdefghijklmnopqrstuvwxyz\","
  40. "\"foo6\":\"abcdefghijklmnopqrstuvwxyz\","
  41. "\"foo7\":\"abcdefghijklmnopqrstuvwxyz\","
  42. "\"foo8\":\"abcdefghijklmnopqrstuvwxyz\","
  43. "\"foo9\":\"abcdefghijklmnopqrstuvwxyz\""
  44. "}");
  45. const char *outfile = "json.out";
  46. int rv = json_object_to_file(outfile, jso);
  47. printf("%s: json_object_to_file(%s, jso)=%d\n",
  48. (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",
  84. file, strerror(errno));
  85. return;
  86. }
  87. if (fstat(d, &sb) < 0)
  88. {
  89. printf("FAIL: unable to stat %s: %s\n",
  90. 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",
  104. file, strerror(errno));
  105. free(buf);
  106. close(d);
  107. return;
  108. }
  109. buf[sb.st_size] = '\0';
  110. printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf);
  111. free(buf);
  112. close(d);
  113. }
  114. int main(int argc, char **argv)
  115. {
  116. // json_object_to_file(file, obj);
  117. // json_object_to_file_ext(file, obj, flags);
  118. _json_c_strerror_enable = 1;
  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_read_valid_with_fd(testdir);
  130. test_read_valid_nested_with_fd(testdir);
  131. test_read_nonexistant();
  132. test_read_closed();
  133. test_write_to_file();
  134. return EXIT_SUCCESS;
  135. }
  136. static void test_read_valid_with_fd(const char *testdir)
  137. {
  138. char filename[PATH_MAX];
  139. (void)snprintf(filename, sizeof(filename), "%s/valid.json", testdir);
  140. int d = open(filename, O_RDONLY, 0);
  141. if (d < 0)
  142. {
  143. fprintf(stderr,
  144. "FAIL: unable to open %s: %s\n",
  145. filename, strerror(errno));
  146. exit(EXIT_FAILURE);
  147. }
  148. json_object *jso = json_object_from_fd(d);
  149. if (jso != NULL)
  150. {
  151. printf("OK: json_object_from_fd(valid.json)=%s\n",
  152. json_object_to_json_string(jso));
  153. json_object_put(jso);
  154. }
  155. else
  156. {
  157. fprintf(stderr,
  158. "FAIL: unable to parse contents of %s: %s\n",
  159. filename, json_util_get_last_err());
  160. }
  161. close(d);
  162. }
  163. static void test_read_valid_nested_with_fd(const char *testdir)
  164. {
  165. char filename[PATH_MAX];
  166. (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
  167. int d = open(filename, O_RDONLY, 0);
  168. if (d < 0)
  169. {
  170. fprintf(stderr,
  171. "FAIL: unable to open %s: %s\n",
  172. filename, strerror(errno));
  173. exit(EXIT_FAILURE);
  174. }
  175. json_object *jso = json_object_from_fd_ex(d, 20);
  176. if (jso != NULL)
  177. {
  178. printf("OK: json_object_from_fd_ex(valid_nested.json, 20)=%s\n",
  179. json_object_to_json_string(jso));
  180. json_object_put(jso);
  181. }
  182. else
  183. {
  184. fprintf(stderr,
  185. "FAIL: unable to parse contents of %s: %s\n",
  186. filename, json_util_get_last_err());
  187. }
  188. (void)lseek(d, SEEK_SET, 0);
  189. jso = json_object_from_fd_ex(d, 3);
  190. if (jso != NULL)
  191. {
  192. printf("FAIL: json_object_from_fd_ex(%s, 3)=%s\n",
  193. filename, json_object_to_json_string(jso));
  194. json_object_put(jso);
  195. }
  196. else
  197. {
  198. printf("OK: correctly unable to parse contents of valid_nested.json with low max depth: %s\n",
  199. json_util_get_last_err());
  200. }
  201. close(d);
  202. }
  203. static void test_read_nonexistant()
  204. {
  205. const char *filename = "./not_present.json";
  206. json_object *jso = json_object_from_file(filename);
  207. if (jso != NULL)
  208. {
  209. printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n",
  210. filename, (void *)jso);
  211. json_object_put(jso);
  212. }
  213. else
  214. {
  215. printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n",
  216. filename, json_util_get_last_err());
  217. }
  218. }
  219. static void test_read_closed()
  220. {
  221. // Test reading from a closed fd
  222. int d = open("/dev/null", O_RDONLY, 0);
  223. if(d < 0)
  224. {
  225. puts("FAIL: unable to open");
  226. }
  227. // Copy over to a fixed fd number so test output is consistent.
  228. int fixed_d = 10;
  229. if (dup2(d, fixed_d) < 0)
  230. {
  231. printf("FAIL: unable to dup to fd %d", fixed_d);
  232. }
  233. close(d);
  234. close(fixed_d);
  235. json_object *jso = json_object_from_fd(fixed_d);
  236. if (jso != NULL)
  237. {
  238. printf("FAIL: read from closed fd returning non-NULL: %p\n",
  239. (void *)jso);
  240. fflush(stdout);
  241. printf(" jso=%s\n", json_object_to_json_string(jso));
  242. json_object_put(jso);
  243. return;
  244. }
  245. printf("OK: json_object_from_fd(closed_fd), "
  246. "expecting NULL, EBADF, got:NULL, %s\n",
  247. json_util_get_last_err());
  248. }