test_util_file.c 5.0 KB

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