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

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