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

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