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

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