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

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