1 #include "strerror_override.h"
2 #include "strerror_override_private.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stddef.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11
12 #include "json.h"
13 #include "json_util.h"
14
15 static void test_read_valid_with_fd(const char *testdir);
16 static void test_read_nonexistant();
17 static void test_read_closed(void);
18
19 static void test_write_to_file();
20 static void stat_and_cat(const char *file);
21
test_write_to_file()22 static void test_write_to_file()
23 {
24 json_object *jso;
25
26 jso = json_tokener_parse("{"
27 "\"foo\":1234,"
28 "\"foo1\":\"abcdefghijklmnopqrstuvwxyz\","
29 "\"foo2\":\"abcdefghijklmnopqrstuvwxyz\","
30 "\"foo3\":\"abcdefghijklmnopqrstuvwxyz\","
31 "\"foo4\":\"abcdefghijklmnopqrstuvwxyz\","
32 "\"foo5\":\"abcdefghijklmnopqrstuvwxyz\","
33 "\"foo6\":\"abcdefghijklmnopqrstuvwxyz\","
34 "\"foo7\":\"abcdefghijklmnopqrstuvwxyz\","
35 "\"foo8\":\"abcdefghijklmnopqrstuvwxyz\","
36 "\"foo9\":\"abcdefghijklmnopqrstuvwxyz\""
37 "}");
38 const char *outfile = "json.out";
39 int rv = json_object_to_file(outfile, jso);
40 printf("%s: json_object_to_file(%s, jso)=%d\n",
41 (rv == 0) ? "OK" : "FAIL", outfile, rv);
42 if (rv == 0)
43 stat_and_cat(outfile);
44
45 putchar('\n');
46
47 const char *outfile2 = "json2.out";
48 rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY);
49 printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
50 (rv == 0) ? "OK" : "FAIL", outfile2, rv);
51 if (rv == 0)
52 stat_and_cat(outfile2);
53
54 const char *outfile3 = "json3.out";
55 int d = open(outfile3, O_WRONLY|O_CREAT, 0600);
56 if (d < 0)
57 {
58 printf("FAIL: unable to open %s %s\n", outfile3, strerror(errno));
59 return;
60 }
61 rv = json_object_to_fd(d, jso, JSON_C_TO_STRING_PRETTY);
62 printf("%s: json_object_to_fd(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
63 (rv == 0) ? "OK" : "FAIL", outfile3, rv);
64 // Write the same object twice
65 rv = json_object_to_fd(d, jso, JSON_C_TO_STRING_PLAIN);
66 printf("%s: json_object_to_fd(%s, jso, JSON_C_TO_STRING_PLAIN)=%d\n",
67 (rv == 0) ? "OK" : "FAIL", outfile3, rv);
68 close(d);
69 if (rv == 0)
70 stat_and_cat(outfile3);
71
72 json_object_put(jso);
73 }
74
stat_and_cat(const char * file)75 static void stat_and_cat(const char *file)
76 {
77 struct stat sb;
78 int d = open(file, O_RDONLY, 0600);
79 if (d < 0)
80 {
81 printf("FAIL: unable to open %s: %s\n",
82 file, strerror(errno));
83 return;
84 }
85 if (fstat(d, &sb) < 0)
86 {
87 printf("FAIL: unable to stat %s: %s\n",
88 file, strerror(errno));
89 close(d);
90 return;
91 }
92 char *buf = malloc(sb.st_size + 1);
93 if(!buf)
94 {
95 printf("FAIL: unable to allocate memory\n");
96 close(d);
97 return;
98 }
99 if (read(d, buf, sb.st_size) < sb.st_size)
100 {
101 printf("FAIL: unable to read all of %s: %s\n",
102 file, strerror(errno));
103 free(buf);
104 close(d);
105 return;
106 }
107 buf[sb.st_size] = '\0';
108 printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf);
109 free(buf);
110 close(d);
111 }
112
main(int argc,char ** argv)113 int main(int argc, char **argv)
114 {
115 // json_object_to_file(file, obj);
116 // json_object_to_file_ext(file, obj, flags);
117
118 _json_c_strerror_enable = 1;
119
120 const char *testdir;
121 if (argc < 2)
122 {
123 fprintf(stderr,
124 "Usage: %s <testdir>\n"
125 " <testdir> is the location of input files\n",
126 argv[0]);
127 return EXIT_FAILURE;
128 }
129 testdir = argv[1];
130
131 test_read_valid_with_fd(testdir);
132 test_read_nonexistant();
133 test_read_closed();
134 test_write_to_file();
135 return EXIT_SUCCESS;
136 }
137
test_read_valid_with_fd(const char * testdir)138 static void test_read_valid_with_fd(const char *testdir)
139 {
140 const char *filename = "./valid.json";
141
142 int d = open(filename, O_RDONLY, 0);
143 if (d < 0)
144 {
145 fprintf(stderr,
146 "FAIL: unable to open %s: %s\n",
147 filename, strerror(errno));
148 exit(EXIT_FAILURE);
149 }
150 json_object *jso = json_object_from_fd(d);
151 if (jso != NULL)
152 {
153 printf("OK: json_object_from_fd(%s)=%s\n",
154 filename, json_object_to_json_string(jso));
155 json_object_put(jso);
156 }
157 else
158 {
159 fprintf(stderr,
160 "FAIL: unable to parse contents of %s: %s\n",
161 filename, json_util_get_last_err());
162 }
163 close(d);
164 }
165
test_read_nonexistant()166 static void test_read_nonexistant()
167 {
168 const char *filename = "./not_present.json";
169
170 json_object *jso = json_object_from_file(filename);
171 if (jso != NULL)
172 {
173 printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n",
174 filename, (void *)jso);
175 json_object_put(jso);
176 }
177 else
178 {
179 printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n",
180 filename, json_util_get_last_err());
181 }
182 }
183
test_read_closed()184 static void test_read_closed()
185 {
186 // Test reading from a closed fd
187 int d = open("/dev/null", O_RDONLY, 0);
188 if(d < 0)
189 {
190 puts("FAIL: unable to open");
191 }
192 // Copy over to a fixed fd number so test output is consistent.
193 int fixed_d = 10;
194 if (dup2(d, fixed_d) < 0)
195 {
196 printf("FAIL: unable to dup to fd %d", fixed_d);
197 }
198 close(d);
199 close(fixed_d);
200
201 json_object *jso = json_object_from_fd(fixed_d);
202 if (jso != NULL)
203 {
204 printf("FAIL: read from closed fd returning non-NULL: %p\n",
205 (void *)jso);
206 fflush(stdout);
207 printf(" jso=%s\n", json_object_to_json_string(jso));
208 json_object_put(jso);
209 return;
210 }
211 printf("OK: json_object_from_fd(closed_fd), "
212 "expecting NULL, EBADF, got:NULL, %s\n",
213 json_util_get_last_err());
214 }
215