• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/json-c/tests/test_util_file.c

  1 #include "strerror_override.h"
  2 #include "strerror_override_private.h"
  3 #ifdef WIN32
  4 #define WIN32_LEAN_AND_MEAN
  5 #include <io.h>
  6 #include <windows.h>
  7 #endif /* defined(WIN32) */
  8 #include <fcntl.h>
  9 #include <limits.h>
 10 #include <stddef.h>
 11 #include <stdio.h>
 12 #include <stdlib.h>
 13 #include <string.h>
 14 #if HAVE_UNISTD_H
 15 #include <unistd.h>
 16 #endif /* HAVE_UNISTD_H */
 17 #include <assert.h>
 18 #include <sys/stat.h>
 19 #include <sys/types.h>
 20 
 21 #include "json.h"
 22 #include "json_util.h"
 23 #include "snprintf_compat.h"
 24 
 25 static void test_read_valid_with_fd(const char *testdir);
 26 static void test_read_valid_nested_with_fd(const char *testdir);
 27 static void test_read_nonexistant(void);
 28 static void test_read_closed(void);
 29 
 30 static void test_write_to_file(void);
 31 static void stat_and_cat(const char *file);
 32 static void test_read_fd_equal(const char *testdir);
 33 
 34 #ifndef PATH_MAX
 35 #define PATH_MAX 256
 36 #endif
 37 
 38 static void test_write_to_file()
 39 {
 40         json_object *jso;
 41 
 42         jso = json_tokener_parse("{"
 43                                  "\"foo\":1234,"
 44                                  "\"foo1\":\"abcdefghijklmnopqrstuvwxyz\","
 45                                  "\"foo2\":\"abcdefghijklmnopqrstuvwxyz\","
 46                                  "\"foo3\":\"abcdefghijklmnopqrstuvwxyz\","
 47                                  "\"foo4\":\"abcdefghijklmnopqrstuvwxyz\","
 48                                  "\"foo5\":\"abcdefghijklmnopqrstuvwxyz\","
 49                                  "\"foo6\":\"abcdefghijklmnopqrstuvwxyz\","
 50                                  "\"foo7\":\"abcdefghijklmnopqrstuvwxyz\","
 51                                  "\"foo8\":\"abcdefghijklmnopqrstuvwxyz\","
 52                                  "\"foo9\":\"abcdefghijklmnopqrstuvwxyz\""
 53                                  "}");
 54         const char *outfile = "json.out";
 55         int rv = json_object_to_file(outfile, jso);
 56         printf("%s: json_object_to_file(%s, jso)=%d\n", (rv == 0) ? "OK" : "FAIL", outfile, rv);
 57         if (rv == 0)
 58                 stat_and_cat(outfile);
 59 
 60         putchar('\n');
 61 
 62         const char *outfile2 = "json2.out";
 63         rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY);
 64         printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
 65                (rv == 0) ? "OK" : "FAIL", outfile2, rv);
 66         if (rv == 0)
 67                 stat_and_cat(outfile2);
 68 
 69         const char *outfile3 = "json3.out";
 70         int d = open(outfile3, O_WRONLY | O_CREAT, 0600);
 71         if (d < 0)
 72         {
 73                 printf("FAIL: unable to open %s %s\n", outfile3, strerror(errno));
 74                 return;
 75         }
 76         rv = json_object_to_fd(d, jso, JSON_C_TO_STRING_PRETTY);
 77         printf("%s: json_object_to_fd(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
 78                (rv == 0) ? "OK" : "FAIL", outfile3, rv);
 79         // Write the same object twice
 80         rv = json_object_to_fd(d, jso, JSON_C_TO_STRING_PLAIN);
 81         printf("%s: json_object_to_fd(%s, jso, JSON_C_TO_STRING_PLAIN)=%d\n",
 82                (rv == 0) ? "OK" : "FAIL", outfile3, rv);
 83         close(d);
 84         if (rv == 0)
 85                 stat_and_cat(outfile3);
 86 
 87         json_object_put(jso);
 88 }
 89 
 90 static void stat_and_cat(const char *file)
 91 {
 92         struct stat sb;
 93         int d = open(file, O_RDONLY, 0600);
 94         if (d < 0)
 95         {
 96                 printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
 97                 return;
 98         }
 99         if (fstat(d, &sb) < 0)
100         {
101                 printf("FAIL: unable to stat %s: %s\n", file, strerror(errno));
102                 close(d);
103                 return;
104         }
105         char *buf = malloc(sb.st_size + 1);
106         if (!buf)
107         {
108                 printf("FAIL: unable to allocate memory\n");
109                 close(d);
110                 return;
111         }
112         if (read(d, buf, sb.st_size) < sb.st_size)
113         {
114                 printf("FAIL: unable to read all of %s: %s\n", file, strerror(errno));
115                 free(buf);
116                 close(d);
117                 return;
118         }
119         buf[sb.st_size] = '\0';
120         printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf);
121         free(buf);
122         close(d);
123 }
124 
125 int main(int argc, char **argv)
126 {
127         //      json_object_to_file(file, obj);
128         //      json_object_to_file_ext(file, obj, flags);
129 
130         const char *testdir;
131         if (argc < 2)
132         {
133                 fprintf(stderr,
134                         "Usage: %s <testdir>\n"
135                         "  <testdir> is the location of input files\n",
136                         argv[0]);
137                 return EXIT_FAILURE;
138         }
139         testdir = argv[1];
140 
141         //      Test json_c_version.c
142         if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
143         {
144                 printf("FAIL: Output from json_c_version(): %s "
145                        "does not match %s",
146                        json_c_version(), JSON_C_VERSION);
147                 return EXIT_FAILURE;
148         }
149         if (json_c_version_num() != JSON_C_VERSION_NUM)
150         {
151                 printf("FAIL: Output from json_c_version_num(): %d "
152                        "does not match %d",
153                        json_c_version_num(), JSON_C_VERSION_NUM);
154                 return EXIT_FAILURE;
155         }
156 
157         test_read_valid_with_fd(testdir);
158         test_read_valid_nested_with_fd(testdir);
159         test_read_nonexistant();
160         test_read_closed();
161         test_write_to_file();
162         test_read_fd_equal(testdir);
163         return EXIT_SUCCESS;
164 }
165 
166 static void test_read_valid_with_fd(const char *testdir)
167 {
168         char filename[PATH_MAX];
169         (void)snprintf(filename, sizeof(filename), "%s/valid.json", testdir);
170 
171         int d = open(filename, O_RDONLY, 0);
172         if (d < 0)
173         {
174                 fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
175                 exit(EXIT_FAILURE);
176         }
177         json_object *jso = json_object_from_fd(d);
178         if (jso != NULL)
179         {
180                 printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(jso));
181                 json_object_put(jso);
182         }
183         else
184         {
185                 fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
186                         json_util_get_last_err());
187         }
188         close(d);
189 }
190 
191 static void test_read_valid_nested_with_fd(const char *testdir)
192 {
193         char filename[PATH_MAX];
194         (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
195 
196         int d = open(filename, O_RDONLY, 0);
197         if (d < 0)
198         {
199                 fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
200                 exit(EXIT_FAILURE);
201         }
202         assert(NULL == json_object_from_fd_ex(d, -2));
203         json_object *jso = json_object_from_fd_ex(d, 20);
204         if (jso != NULL)
205         {
206                 printf("OK: json_object_from_fd_ex(valid_nested.json, 20)=%s\n",
207                        json_object_to_json_string(jso));
208                 json_object_put(jso);
209         }
210         else
211         {
212                 fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
213                         json_util_get_last_err());
214         }
215 
216         (void)lseek(d, SEEK_SET, 0);
217 
218         jso = json_object_from_fd_ex(d, 3);
219         if (jso != NULL)
220         {
221                 printf("FAIL: json_object_from_fd_ex(%s, 3)=%s\n", filename,
222                        json_object_to_json_string(jso));
223                 json_object_put(jso);
224         }
225         else
226         {
227                 printf("OK: correctly unable to parse contents of valid_nested.json with low max "
228                        "depth: %s\n",
229                        json_util_get_last_err());
230         }
231         close(d);
232 }
233 
234 static void test_read_nonexistant()
235 {
236         const char *filename = "./not_present.json";
237 
238         json_object *jso = json_object_from_file(filename);
239         if (jso != NULL)
240         {
241                 printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n", filename,
242                        (void *)jso);
243                 json_object_put(jso);
244         }
245         else
246         {
247                 printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n", filename,
248                        json_util_get_last_err());
249         }
250 }
251 
252 static void test_read_closed()
253 {
254         // Test reading from a closed fd
255         int d = open("/dev/null", O_RDONLY, 0);
256         if (d < 0)
257         {
258                 puts("FAIL: unable to open");
259         }
260         // Copy over to a fixed fd number so test output is consistent.
261         int fixed_d = 10;
262         if (dup2(d, fixed_d) < 0)
263         {
264                 printf("FAIL: unable to dup to fd %d", fixed_d);
265         }
266         close(d);
267         close(fixed_d);
268 
269         json_object *jso = json_object_from_fd(fixed_d);
270         if (jso != NULL)
271         {
272                 printf("FAIL: read from closed fd returning non-NULL: %p\n", (void *)jso);
273                 fflush(stdout);
274                 printf("  jso=%s\n", json_object_to_json_string(jso));
275                 json_object_put(jso);
276                 return;
277         }
278         printf("OK: json_object_from_fd(closed_fd), "
279                "expecting NULL, EBADF, got:NULL, %s\n",
280                json_util_get_last_err());
281 }
282 
283 static void test_read_fd_equal(const char *testdir)
284 {
285         char filename[PATH_MAX];
286         (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
287 
288         json_object *jso = json_object_from_file(filename);
289 
290         int d = open(filename, O_RDONLY, 0);
291         if (d < 0)
292         {
293                 fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
294                 exit(EXIT_FAILURE);
295         }
296         json_object *new_jso = json_object_from_fd(d);
297         close(d);
298 
299         printf("OK: json_object_from_file(valid.json)=%s\n", json_object_to_json_string(jso));
300         printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(new_jso));
301         json_object_put(jso);
302         json_object_put(new_jso);
303 }
304 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt