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

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