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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 json_c_version.c
  127. if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
  128. {
  129. printf("FAIL: Output from json_c_version(): %s "
  130. "does not match %s", json_c_version(), JSON_C_VERSION);
  131. return EXIT_FAILURE;
  132. }
  133. if (json_c_version_num() != JSON_C_VERSION_NUM)
  134. {
  135. printf("FAIL: Output from json_c_version_num(): %d "
  136. "does not match %d", json_c_version_num(), JSON_C_VERSION_NUM);
  137. return EXIT_FAILURE;
  138. }
  139. test_read_valid_with_fd(testdir);
  140. test_read_valid_nested_with_fd(testdir);
  141. test_read_nonexistant();
  142. test_read_closed();
  143. test_write_to_file();
  144. return EXIT_SUCCESS;
  145. }
  146. static void test_read_valid_with_fd(const char *testdir)
  147. {
  148. char filename[PATH_MAX];
  149. (void)snprintf(filename, sizeof(filename), "%s/valid.json", testdir);
  150. int d = open(filename, O_RDONLY, 0);
  151. if (d < 0)
  152. {
  153. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  154. exit(EXIT_FAILURE);
  155. }
  156. json_object *jso = json_object_from_fd(d);
  157. if (jso != NULL)
  158. {
  159. printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(jso));
  160. json_object_put(jso);
  161. }
  162. else
  163. {
  164. fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
  165. json_util_get_last_err());
  166. }
  167. close(d);
  168. }
  169. static void test_read_valid_nested_with_fd(const char *testdir)
  170. {
  171. char filename[PATH_MAX];
  172. (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
  173. int d = open(filename, O_RDONLY, 0);
  174. if (d < 0)
  175. {
  176. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  177. exit(EXIT_FAILURE);
  178. }
  179. json_object *jso = json_object_from_fd_ex(d, 20);
  180. if (jso != NULL)
  181. {
  182. printf("OK: json_object_from_fd_ex(valid_nested.json, 20)=%s\n",
  183. json_object_to_json_string(jso));
  184. json_object_put(jso);
  185. }
  186. else
  187. {
  188. fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
  189. json_util_get_last_err());
  190. }
  191. (void)lseek(d, SEEK_SET, 0);
  192. jso = json_object_from_fd_ex(d, 3);
  193. if (jso != NULL)
  194. {
  195. printf("FAIL: json_object_from_fd_ex(%s, 3)=%s\n", filename,
  196. json_object_to_json_string(jso));
  197. json_object_put(jso);
  198. }
  199. else
  200. {
  201. printf("OK: correctly unable to parse contents of valid_nested.json with low max "
  202. "depth: %s\n",
  203. json_util_get_last_err());
  204. }
  205. close(d);
  206. }
  207. static void test_read_nonexistant()
  208. {
  209. const char *filename = "./not_present.json";
  210. json_object *jso = json_object_from_file(filename);
  211. if (jso != NULL)
  212. {
  213. printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n", filename,
  214. (void *)jso);
  215. json_object_put(jso);
  216. }
  217. else
  218. {
  219. printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n", filename,
  220. json_util_get_last_err());
  221. }
  222. }
  223. static void test_read_closed()
  224. {
  225. // Test reading from a closed fd
  226. int d = open("/dev/null", O_RDONLY, 0);
  227. if (d < 0)
  228. {
  229. puts("FAIL: unable to open");
  230. }
  231. // Copy over to a fixed fd number so test output is consistent.
  232. int fixed_d = 10;
  233. if (dup2(d, fixed_d) < 0)
  234. {
  235. printf("FAIL: unable to dup to fd %d", fixed_d);
  236. }
  237. close(d);
  238. close(fixed_d);
  239. json_object *jso = json_object_from_fd(fixed_d);
  240. if (jso != NULL)
  241. {
  242. printf("FAIL: read from closed fd returning non-NULL: %p\n", (void *)jso);
  243. fflush(stdout);
  244. printf(" jso=%s\n", json_object_to_json_string(jso));
  245. json_object_put(jso);
  246. return;
  247. }
  248. printf("OK: json_object_from_fd(closed_fd), "
  249. "expecting NULL, EBADF, got:NULL, %s\n",
  250. json_util_get_last_err());
  251. }