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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. json_object_put(jso);
  46. }
  47. static void stat_and_cat(const char *file)
  48. {
  49. struct stat sb;
  50. int d = open(file, O_RDONLY, 0600);
  51. if (d < 0)
  52. {
  53. printf("FAIL: unable to open %s: %s\n",
  54. file, strerror(errno));
  55. return;
  56. }
  57. if (fstat(d, &sb) < 0)
  58. {
  59. printf("FAIL: unable to stat %s: %s\n",
  60. file, strerror(errno));
  61. close(d);
  62. return;
  63. }
  64. char *buf = malloc(sb.st_size + 1);
  65. if(!buf)
  66. {
  67. printf("FAIL: unable to allocate memory\n");
  68. close(d);
  69. return;
  70. }
  71. if (read(d, buf, sb.st_size) < sb.st_size)
  72. {
  73. printf("FAIL: unable to read all of %s: %s\n",
  74. file, strerror(errno));
  75. free(buf);
  76. close(d);
  77. return;
  78. }
  79. buf[sb.st_size] = '\0';
  80. printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf);
  81. free(buf);
  82. }
  83. int main(int argc, char **argv)
  84. {
  85. // json_object_to_file(file, obj);
  86. // json_object_to_file_ext(file, obj, flags);
  87. const char *testdir;
  88. if (argc < 2)
  89. {
  90. fprintf(stderr,
  91. "Usage: %s <testdir>\n"
  92. " <testdir> is the location of input files\n",
  93. argv[0]);
  94. return EXIT_FAILURE;
  95. }
  96. testdir = argv[1];
  97. test_read_valid_with_fd(testdir);
  98. test_read_nonexistant();
  99. test_read_closed();
  100. test_write_to_file();
  101. return EXIT_SUCCESS;
  102. }
  103. static void test_read_valid_with_fd(const char *testdir)
  104. {
  105. const char *filename = "./valid.json";
  106. int d = open(filename, O_RDONLY, 0);
  107. if (d < 0)
  108. {
  109. fprintf(stderr,
  110. "FAIL: unable to open %s: %s\n",
  111. filename, strerror(errno));
  112. exit(EXIT_FAILURE);
  113. }
  114. json_object *jso = json_object_from_fd(d);
  115. if (jso != NULL)
  116. {
  117. printf("OK: json_object_from_fd(%s)=%s\n",
  118. filename, json_object_to_json_string(jso));
  119. json_object_put(jso);
  120. }
  121. else
  122. {
  123. fprintf(stderr,
  124. "FAIL: unable to parse contents of %s: %s\n",
  125. filename, json_util_get_last_err());
  126. }
  127. close(d);
  128. }
  129. static void test_read_nonexistant()
  130. {
  131. const char *filename = "./not_present.json";
  132. json_object *jso = json_object_from_file(filename);
  133. if (jso != NULL)
  134. {
  135. printf("FAIL: json_object_from_file(%s) returned 0x%lx when NULL expected\n",
  136. filename, (unsigned long)jso);
  137. json_object_put(jso);
  138. }
  139. else
  140. {
  141. printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n",
  142. filename, json_util_get_last_err());
  143. }
  144. }
  145. static void test_read_closed()
  146. {
  147. // Test reading from a closed fd
  148. int d = open("/dev/null", O_RDONLY, 0);
  149. if(d < 0)
  150. {
  151. puts("FAIL: unable to open");
  152. }
  153. // Copy over to a fixed fd number so test output is consistent.
  154. int fixed_d = 10;
  155. if (dup2(d, fixed_d) < 0)
  156. {
  157. printf("FAIL: unable to dup to fd %d", fixed_d);
  158. }
  159. close(d);
  160. close(fixed_d);
  161. json_object *jso = json_object_from_fd(fixed_d);
  162. if (jso != NULL)
  163. {
  164. printf("FAIL: read from closed fd returning non-NULL: 0x%lx\n",
  165. (unsigned long) jso);
  166. fflush(stdout);
  167. printf(" jso=%s\n", json_object_to_json_string(jso));
  168. json_object_put(jso);
  169. return;
  170. }
  171. printf("OK: json_object_from_fd(closed_fd), "
  172. "expecting NULL, EBADF, got:0x%lx, %s\n",
  173. (unsigned long)jso, json_util_get_last_err());
  174. }