]> git.ozlabs.org Git - ccan/blob - ccan/json/test/run-decode-encode.c
json: new module for parsing and generating JSON
[ccan] / ccan / json / test / run-decode-encode.c
1 #include "common.h"
2
3 int main(void)
4 {
5         const char *strings_file = "test/test-strings";
6         const char *strings_reencoded_file = "test/test-strings-reencoded";
7         FILE *f, *f2;
8         char buffer[1024], buffer2[1024];
9         
10         plan_tests(90);
11         
12         f = fopen(strings_file, "rb");
13         if (f == NULL) {
14                 diag("Could not open %s: %s", strings_file, strerror(errno));
15                 return 1;
16         }
17         f2 = fopen(strings_reencoded_file, "rb");
18         if (f2 == NULL) {
19                 diag("Could not open %s: %s", strings_reencoded_file, strerror(errno));
20                 return 1;
21         }
22         
23         while (fgets(buffer, sizeof(buffer), f)) {
24                 const char *s = chomp(buffer);
25                 bool valid;
26                 JsonNode *node;
27                 
28                 if (expect_literal(&s, "valid ")) {
29                         valid = true;
30                 } else if (expect_literal(&s, "invalid ")) {
31                         valid = false;
32                 } else {
33                         fail("Invalid line in test-strings: %s", buffer);
34                         continue;
35                 }
36                 
37                 node = json_decode(s);
38                 
39                 if (valid) {
40                         char *reencoded;
41                         char errmsg[256];
42                         
43                         if (node == NULL) {
44                                 fail("%s is valid, but json_decode returned NULL", s);
45                                 continue;
46                         }
47                         
48                         if (!json_check(node, errmsg)) {
49                                 fail("Corrupt tree produced by json_decode: %s", errmsg);
50                                 continue;
51                         }
52                         
53                         reencoded = json_encode(node);
54                         
55                         if (!fgets(buffer2, sizeof(buffer2), f2)) {
56                                 fail("test-strings-reencoded is missing this line: %s", reencoded);
57                                 continue;
58                         }
59                         chomp(buffer2);
60                         
61                         ok(strcmp(reencoded, buffer2) == 0, "re-encode %s -> %s", s, reencoded);
62                         
63                         free(reencoded);
64                         json_delete(node);
65                 } else if (node != NULL) {
66                         fail("%s is invalid, but json_decode returned non-NULL", s);
67                         continue;
68                 }
69         }
70         
71         if (ferror(f) || fclose(f) != 0 || ferror(f2) || fclose(f2) != 0) {
72                 diag("I/O error reading test data.");
73                 return 1;
74         }
75         
76         return exit_status();
77 }