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

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