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.3 kB

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