MCPcopy Create free account
hub / github.com/ElementsProject/lightning / main

Function main

common/test/run-json_scan.c:125–252  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

123}
124
125int main(int argc, char *argv[])
126{
127 const jsmntok_t *toks, *t;
128 char *buf;
129 char *s;
130 u32 u32val;
131 u8 *hex;
132 const char *err;
133
134 common_setup(argv[0]);
135
136 buf = tal_strdup(tmpctx, "{\"1\":\"one\", \"2\":\"two\", \"3\":{\"three\": {\"deeper\": 17}}, \"arr\": [{\"1\": \"arrone\"}, 2, [3, 4]]}");
137 toks = json_parse_simple(tmpctx, buf, strlen(buf));
138 assert(toks);
139 assert(toks->size == 4);
140
141 /* These are direct matches, and they work. */
142 assert(!json_scan(tmpctx, buf, toks, "{1:one}"));
143 assert(!json_scan(tmpctx, buf, toks, "{1:one,2:two}"));
144 assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one}"));
145 assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{three:{deeper:17}}}"));
146 assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{three:{deeper:17}},arr:[0:{1:arrone}]}"));
147 assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{three:{deeper:17}},arr:[1:2]}"));
148 assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{three:{deeper:17}},arr:[1:2,2:[0:3,1:4]]}"));
149
150 /* Optional fields which are present are fine. */
151 assert(!json_scan(tmpctx, buf, toks, "{1?:one}"));
152 assert(!json_scan(tmpctx, buf, toks, "{1?:one,2?:two}"));
153 assert(!json_scan(tmpctx, buf, toks, "{2?:two,1?:one}"));
154 assert(!json_scan(tmpctx, buf, toks, "{2?:two,1?:one,3?:{three?:{deeper?:17}}}"));
155 assert(!json_scan(tmpctx, buf, toks, "{2?:two,1?:one,3?:{three?:{deeper?:17}},arr?:[0:{1?:arrone}]}"));
156 assert(!json_scan(tmpctx, buf, toks, "{2?:two,1?:one,3?:{three?:{deeper?:17}},arr?:[1:2]}"));
157 assert(!json_scan(tmpctx, buf, toks, "{2?:two,1?:one,3?:{three?:{deeper?:17}},arr?:[1:2,2:[0:3,1:4]]}"));
158
159 /* Optional field which are missing are fine too */
160 assert(!json_scan(tmpctx, buf, toks, "{5?:one}"));
161 assert(!json_scan(tmpctx, buf, toks, "{1:one,5?:two}"));
162 assert(!json_scan(tmpctx, buf, toks, "{2:two,5?:one}"));
163 assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,5?:{three:{deeper:17}}}"));
164 assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{five?:{deeper:17}},arr:[0:{1:arrone}]}"));
165 assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{three:{notdeeper?:17}},arr:[1:2]}"));
166 assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{three:{deeper:17}},notarr?:[1:2,2:[0:3,1:4]]}"));
167
168 /* These do not match */
169 err = json_scan(tmpctx, buf, toks, "{2:one}");
170 assert(streq(err, "Parsing '{2:one': \"two\" does not match expected one"));
171 err = json_scan(tmpctx, buf, toks, "{1:one,2:tw}");
172 assert(streq(err, "Parsing '{1:one,2:tw': \"two\" does not match expected tw"));
173 err = json_scan(tmpctx, buf, toks, "{1:one,2:twoo}");
174 assert(streq(err, "Parsing '{1:one,2:twoo': \"two\" does not match expected twoo"));
175 err = json_scan(tmpctx, buf, toks, "{4:one}");
176 assert(streq(err, "Parsing '{4:': object does not have member 4"));
177 err = json_scan(tmpctx, buf, toks, "{2:two,1:one,3:three}");
178 assert(streq(err, "Parsing '{2:two,1:one,3:three': {\"three\": {\"deeper\": 17}} does not match expected three"));
179 err = json_scan(tmpctx, buf, toks, "{3:{three:deeper}}");
180 assert(streq(err, "Parsing '{3:{three:deeper': {\"deeper\": 17} does not match expected deeper"));
181 err = json_scan(tmpctx, buf, toks, "{3:{three:{deeper:{}}}}");
182 assert(streq(err, "Parsing '{3:{three:{deeper:{': token is not an object: 17"));

Callers

nothing calls this directly

Calls 5

common_setupFunction · 0.85
json_parse_simpleFunction · 0.85
tal_parentFunction · 0.85
common_shutdownFunction · 0.85
json_scanFunction · 0.70

Tested by

no test coverage detected