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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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();
  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();
  82. test_read_closed();
  83. test_write_to_file();
  84. }
  85. static void test_read_valid_with_fd(const char *testdir)
  86. {
  87. const char *filename = "./valid.json";
  88. int d = open(filename, O_RDONLY, 0);
  89. if (d < 0)
  90. {
  91. fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
  92. exit(1);
  93. }
  94. json_object *jso = json_object_from_fd(d);
  95. if (jso != NULL)
  96. {
  97. printf("OK: json_object_from_fd(%s)=%s\n", filename, json_object_to_json_string(jso));
  98. json_object_put(jso);
  99. }
  100. else
  101. {
  102. fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename, json_util_get_last_err());
  103. }
  104. close(d);
  105. }
  106. static void test_read_nonexistant()
  107. {
  108. const char *filename = "./not_present.json";
  109. json_object *jso = json_object_from_file(filename);
  110. if (jso != NULL)
  111. {
  112. printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n", filename, jso);
  113. json_object_put(jso);
  114. }
  115. else
  116. {
  117. printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n",
  118. filename, json_util_get_last_err());
  119. }
  120. }
  121. static void test_read_closed()
  122. {
  123. // Test reading from a closed fd
  124. int d = open("/dev/null", O_RDONLY, 0);
  125. close(d);
  126. json_object *jso = json_object_from_fd(d);
  127. if (jso != NULL)
  128. {
  129. printf("FAIL: read from closed fd returning non-NULL: %p\n", jso);
  130. fflush(stdout);
  131. printf(" jso=%s\n", json_object_to_json_string(jso));
  132. json_object_put(jso);
  133. return;
  134. }
  135. printf("OK: json_object_from_fd(closed_fd), expecting NULL, EBADF, got:%p, %s\n", jso, json_util_get_last_err());
  136. }