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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "json.h"
  9. #include "json_util.h"
  10. static void test_read_valid_with_fd(const char *testdir);
  11. static void test_read_nonexistant(const char *testdir);
  12. static void test_read_closed(void);
  13. static void test_write_to_file();
  14. static void stat_and_cat(const char *file);
  15. static void test_write_to_file()
  16. {
  17. json_object *jso;
  18. jso = json_tokener_parse("{"
  19. "\"foo\":1234,"
  20. "\"foo1\":\"abcdefghijklmnopqrstuvwxyz\","
  21. "\"foo2\":\"abcdefghijklmnopqrstuvwxyz\","
  22. "\"foo3\":\"abcdefghijklmnopqrstuvwxyz\","
  23. "\"foo4\":\"abcdefghijklmnopqrstuvwxyz\","
  24. "\"foo5\":\"abcdefghijklmnopqrstuvwxyz\","
  25. "\"foo6\":\"abcdefghijklmnopqrstuvwxyz\","
  26. "\"foo7\":\"abcdefghijklmnopqrstuvwxyz\","
  27. "\"foo8\":\"abcdefghijklmnopqrstuvwxyz\","
  28. "\"foo9\":\"abcdefghijklmnopqrstuvwxyz\""
  29. "}");
  30. const char *outfile = "json.out";
  31. int rv = json_object_to_file(outfile, jso);
  32. printf("%s: json_object_to_file(%s, jso)=%d\n",
  33. (rv == 0) ? "OK" : "FAIL", outfile, rv);
  34. if (rv == 0)
  35. stat_and_cat(outfile);
  36. printf("\n");
  37. const char *outfile2 = "json2.out";
  38. rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY);
  39. printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
  40. (rv == 0) ? "OK" : "FAIL", outfile2, rv);
  41. if (rv == 0)
  42. stat_and_cat(outfile2);
  43. }
  44. static void stat_and_cat(const char *file)
  45. {
  46. struct stat sb;
  47. int d = open(file, O_RDONLY, 0600);
  48. if (d < 0)
  49. {
  50. printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
  51. return;
  52. }
  53. if (fstat(d, &sb) < 0)
  54. {
  55. printf("FAIL: unable to stat %s: %s\n", file, strerror(errno));
  56. close(d);
  57. return;
  58. }
  59. char *buf = malloc(sb.st_size + 1);
  60. if (read(d, buf, sb.st_size) < sb.st_size)
  61. {
  62. printf("FAIL: unable to read all of %s: %s\n", file, strerror(errno));
  63. close(d);
  64. return;
  65. }
  66. buf[sb.st_size] = '\0';
  67. printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf);
  68. }
  69. int main(int argc, char **argv)
  70. {
  71. // json_object_to_file(file, obj);
  72. // json_object_to_file_ext(file, obj, flags);
  73. const char *testdir;
  74. if (argc < 2)
  75. {
  76. fprintf(stderr, "Usage: %s <testdir>\n <testdir> is the location of input files\n", argv[0]);
  77. exit(1);
  78. }
  79. testdir = argv[1];
  80. test_read_valid_with_fd(testdir);
  81. test_read_nonexistant(testdir);
  82. test_read_closed();
  83. test_write_to_file();
  84. }
  85. static void test_read_valid_with_fd(const char *testdir)
  86. {
  87. char file_buf[4096];
  88. (void)snprintf(file_buf, sizeof(file_buf), "%s/valid.json", testdir);
  89. int d = open(file_buf, O_RDONLY, 0);
  90. if (d < 0)
  91. {
  92. fprintf(stderr, "FAIL: unable to open %s: %s\n", file_buf, strerror(errno));
  93. exit(1);
  94. }
  95. json_object *jso = json_object_from_fd(d);
  96. if (jso != NULL)
  97. {
  98. printf("OK: json_object_from_fd(%s)=%s\n", file_buf, json_object_to_json_string(jso));
  99. json_object_put(jso);
  100. }
  101. else
  102. {
  103. fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", file_buf, json_util_get_last_err());
  104. }
  105. close(d);
  106. }
  107. static void test_read_nonexistant(const char *testdir)
  108. {
  109. char file_buf[4096];
  110. const char *filename = "not_present.json";
  111. (void)snprintf(file_buf, sizeof(file_buf), "%s/%s", testdir, filename);
  112. json_object *jso = json_object_from_file(file_buf);
  113. if (jso != NULL)
  114. {
  115. printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n", filename, jso);
  116. json_object_put(jso);
  117. }
  118. else
  119. {
  120. printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n",
  121. filename, json_util_get_last_err());
  122. }
  123. }
  124. static void test_read_closed()
  125. {
  126. // Test reading from a closed fd
  127. int d = open("/dev/null", O_RDONLY, 0);
  128. close(d);
  129. json_object *jso = json_object_from_fd(d);
  130. if (jso != NULL)
  131. {
  132. printf("FAIL: read from closed fd returning non-NULL: %p\n", jso);
  133. fflush(stdout);
  134. printf(" jso=%s\n", json_object_to_json_string(jso));
  135. json_object_put(jso);
  136. return;
  137. }
  138. printf("OK: json_object_from_fd(closed_fd), expecting NULL, EBADF, got:%p, %s\n", jso, json_util_get_last_err());
  139. }