Add lots of tests for pack/unpack code, fix bugs found
[jansson.git] / test / suites / api / test_unpack.c
1 /*
2  * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
3  * Copyright (c) 2010-2011 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
4  *
5  * Jansson is free software; you can redistribute it and/or modify
6  * it under the terms of the MIT license. See LICENSE for details.
7  */
8
9 #include <string.h>
10 #include <jansson.h>
11 #include <stdio.h>
12 #include "util.h"
13
14 int main()
15 {
16     json_t *j, *j2;
17     int i1, i2, i3;
18     json_int_t I1;
19     int rv;
20     double f;
21     char *s;
22
23     json_error_t error;
24
25     /*
26      * Simple, valid json_pack cases
27      */
28
29     /* true */
30     rv = json_unpack(json_true(), "b", &i1);
31     if(rv || !i1)
32         fail("json_unpack boolean failed");
33
34     /* false */
35     rv = json_unpack(json_false(), "b", &i1);
36     if(rv || i1)
37         fail("json_unpack boolean failed");
38
39     /* null */
40     if(json_unpack(json_null(), "n"))
41         fail("json_unpack null failed");
42
43     /* integer */
44     j = json_integer(42);
45     rv = json_unpack(j, "i", &i1);
46     if(rv || i1 != 42)
47         fail("json_unpack integer failed");
48     json_decref(j);
49
50     /* json_int_t */
51     j = json_integer(5555555);
52     rv = json_unpack(j, "I", &I1);
53     if(rv || I1 != 5555555)
54         fail("json_unpack json_int_t failed");
55     json_decref(j);
56
57     /* real */
58     j = json_real(1.7);
59     rv = json_unpack(j, "f", &f);
60     if(rv || f != 1.7)
61         fail("json_unpack real failed");
62     json_decref(j);
63
64     /* number */
65     j = json_integer(12345);
66     rv = json_unpack(j, "F", &f);
67     if(rv || f != 12345.0)
68         fail("json_unpack (real or) integer failed");
69     json_decref(j);
70
71     j = json_real(1.7);
72     rv = json_unpack(j, "F", &f);
73     if(rv || f != 1.7)
74         fail("json_unpack real (or integer) failed");
75     json_decref(j);
76
77     /* string */
78     j = json_string("foo");
79     rv = json_unpack(j, "s", &s);
80     if(rv || strcmp(s, "foo"))
81         fail("json_unpack string failed");
82     json_decref(j);
83
84     /* empty object */
85     j = json_object();
86     if(json_unpack(j, "{}"))
87         fail("json_unpack empty object failed");
88     json_decref(j);
89
90     /* empty list */
91     j = json_array();
92     if(json_unpack(j, "[]"))
93         fail("json_unpack empty list failed");
94     json_decref(j);
95
96     /* non-incref'd object */
97     j = json_object();
98     rv = json_unpack(j, "o", &j2);
99     if(j2 != j || j->refcount != 1)
100         fail("json_unpack object failed");
101     json_decref(j);
102
103     /* incref'd object */
104     j = json_object();
105     rv = json_unpack(j, "O", &j2);
106     if(j2 != j || j->refcount != 2)
107         fail("json_unpack object failed");
108     json_decref(j);
109     json_decref(j);
110
111     /* simple object */
112     j = json_pack("{s:i}", "foo", 42);
113     rv = json_unpack(j, "{s:i}", "foo", &i1);
114     if(rv || i1 != 42)
115         fail("json_unpack simple object failed");
116     json_decref(j);
117
118     /* simple array */
119     j = json_pack("[iii]", 1, 2, 3);
120     rv = json_unpack(j, "[i,i,i]", &i1, &i2, &i3);
121     if(rv || i1 != 1 || i2 != 2 || i3 != 3)
122         fail("json_unpack simple array failed");
123     json_decref(j);
124
125     /*
126      * Invalid cases
127      */
128
129     j = json_integer(42);
130     if(!json_unpack_ex(j, &error, 0, "z"))
131         fail("json_unpack succeeded with invalid format character");
132     check_error("Unexpected format character 'z'", "<format>", 1, 1, 1);
133
134     if(!json_unpack_ex(NULL, &error, 0, "[i]"))
135         fail("json_unpack succeeded with NULL root");
136     check_error("NULL root value", "<root>", -1, -1, 0);
137
138     /* mismatched open/close array/object */
139     j = json_pack("[]");
140     if(!json_unpack_ex(j, &error, 0, "[}"))
141         fail("json_unpack failed to catch mismatched ']'");
142     check_error("Unexpected format character '}'", "<format>", 1, 2, 2);
143     json_decref(j);
144
145     j = json_pack("{}");
146     if(!json_unpack_ex(j, &error, 0, "{]"))
147         fail("json_unpack failed to catch mismatched '}'");
148     check_error("Expected format 's', got ']'", "<format>", 1, 2, 2);
149     json_decref(j);
150
151     /* missing close array */
152     j = json_pack("[]");
153     if(!json_unpack_ex(j, &error, 0, "["))
154         fail("json_unpack failed to catch missing ']'");
155     check_error("Unexpected end of format string", "<format>", 1, 2, 2);
156     json_decref(j);
157
158     /* missing close object */
159     j = json_pack("{}");
160     if(!json_unpack_ex(j, &error, 0, "{"))
161         fail("json_unpack failed to catch missing '}'");
162     check_error("Unexpected end of format string", "<format>", 1, 2, 2);
163     json_decref(j);
164
165     /* garbage after format string */
166     j = json_pack("[i]", 42);
167     if(!json_unpack_ex(j, &error, 0, "[i]a", &i1))
168         fail("json_unpack failed to catch garbage after format string");
169     check_error("Garbage after format string", "<format>", 1, 4, 4);
170
171     j = json_integer(12345);
172     if(!json_unpack_ex(j, &error, 0, "ia", &i1))
173         fail("json_unpack failed to catch garbage after format string");
174     check_error("Garbage after format string", "<format>", 1, 2, 2);
175
176     /* NULL format string */
177     j = json_pack("[]");
178     if(!json_unpack_ex(j, &error, 0, NULL))
179         fail("json_unpack failed to catch null format string");
180     check_error("NULL or empty format string", "<format>", -1, -1, 0);
181     json_decref(j);
182
183     /* NULL string pointer */
184     j = json_string("foobie");
185     if(!json_unpack_ex(j, &error, 0, "s", NULL))
186         fail("json_unpack failed to catch null string pointer");
187     check_error("NULL string argument", "<args>", 1, 1, 1);
188     json_decref(j);
189
190     /* invalid types */
191     j = json_integer(42);
192     j2 = json_string("foo");
193     if(!json_unpack_ex(j, &error, 0, "s"))
194         fail("json_unpack failed to catch invalid type");
195     check_error("Expected string, got integer", "<validation>", 1, 1, 1);
196
197     if(!json_unpack_ex(j, &error, 0, "n"))
198         fail("json_unpack failed to catch invalid type");
199     check_error("Expected null, got integer", "<validation>", 1, 1, 1);
200
201     if(!json_unpack_ex(j, &error, 0, "b"))
202         fail("json_unpack failed to catch invalid type");
203     check_error("Expected true or false, got integer", "<validation>", 1, 1, 1);
204
205     if(!json_unpack_ex(j2, &error, 0, "i"))
206         fail("json_unpack failed to catch invalid type");
207     check_error("Expected integer, got string", "<validation>", 1, 1, 1);
208
209     if(!json_unpack_ex(j2, &error, 0, "I"))
210         fail("json_unpack failed to catch invalid type");
211     check_error("Expected integer, got string", "<validation>", 1, 1, 1);
212
213     if(!json_unpack_ex(j, &error, 0, "f"))
214         fail("json_unpack failed to catch invalid type");
215     check_error("Expected real, got integer", "<validation>", 1, 1, 1);
216
217     if(!json_unpack_ex(j2, &error, 0, "F"))
218         fail("json_unpack failed to catch invalid type");
219     check_error("Expected real or integer, got string", "<validation>", 1, 1, 1);
220
221     if(!json_unpack_ex(j, &error, 0, "[i]"))
222         fail("json_unpack failed to catch invalid type");
223     check_error("Expected array, got integer", "<validation>", 1, 1, 1);
224
225     if(!json_unpack_ex(j, &error, 0, "{si}", "foo"))
226         fail("json_unpack failed to catch invalid type");
227     check_error("Expected object, got integer", "<validation>", 1, 1, 1);
228
229     json_decref(j);
230     json_decref(j2);
231
232     /* Array index out of range */
233     j = json_pack("[i]", 1);
234     if(!json_unpack_ex(j, &error, 0, "[ii]", &i1, &i2))
235         fail("json_unpack failed to catch index out of array bounds");
236     check_error("Array index 1 out of range", "<validation>", 1, 3, 3);
237     json_decref(j);
238
239     /* NULL object key */
240     j = json_pack("{si}", "foo", 42);
241     if(!json_unpack_ex(j, &error, 0, "{si}", NULL, &i1))
242         fail("json_unpack failed to catch null string pointer");
243     check_error("NULL object key", "<args>", 1, 2, 2);
244     json_decref(j);
245
246     /* Object key not found */
247     j = json_pack("{si}", "foo", 42);
248     if(!json_unpack_ex(j, &error, 0, "{si}", "baz", &i1))
249         fail("json_unpack failed to catch null string pointer");
250     check_error("Object item not found: baz", "<validation>", 1, 3, 3);
251     json_decref(j);
252
253     /*
254      * Strict validation
255      */
256
257     j = json_pack("[iii]", 1, 2, 3);
258     rv = json_unpack(j, "[iii!]", &i1, &i2, &i3);
259     if(rv || i1 != 1 || i2 != 2 || i3 != 3)
260         fail("json_unpack array with strict validation failed");
261     json_decref(j);
262
263     j = json_pack("[iii]", 1, 2, 3);
264     if(!json_unpack_ex(j, &error, 0, "[ii!]", &i1, &i2))
265         fail("json_unpack array with strict validation failed");
266     check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
267     json_decref(j);
268
269     /* Like above, but with JSON_STRICT instead of '!' format */
270     j = json_pack("[iii]", 1, 2, 3);
271     if(!json_unpack_ex(j, &error, JSON_STRICT, "[ii]", &i1, &i2))
272         fail("json_unpack array with strict validation failed");
273     check_error("1 array item(s) left unpacked", "<validation>", 1, 4, 4);
274     json_decref(j);
275
276     j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
277     rv = json_unpack(j, "{sssi!}", "foo", &s, "baz", &i1);
278     if(rv || strcmp(s, "bar") != 0 || i1 != 42)
279         fail("json_unpack object with strict validation failed");
280     json_decref(j);
281
282     /* Unpack the same item twice */
283     j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
284     if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s))
285        fail("json_unpack object with strict validation failed");
286     check_error("1 object item(s) left unpacked", "<validation>", 1, 10, 10);
287     json_decref(j);
288
289     j = json_pack("[i,{s:i,s:n},[i,i]]", 1, "foo", 2, "bar", 3, 4);
290     if(json_unpack_ex(j, NULL, JSON_STRICT | JSON_VALIDATE_ONLY,
291                       "[i{sisn}[ii]]", "foo", "bar"))
292         fail("json_unpack complex value with strict validation failed");
293     json_decref(j);
294
295     /* ! and * must be last */
296     j = json_pack("[ii]", 1, 2);
297     if(!json_unpack_ex(j, &error, 0, "[i!i]", &i1, &i2))
298         fail("json_unpack failed to catch ! in the middle of an array");
299     check_error("Expected ']' after '!', got 'i'", "<format>", 1, 4, 4);
300
301     if(!json_unpack_ex(j, &error, 0, "[i*i]", &i1, &i2))
302         fail("json_unpack failed to catch * in the middle of an array");
303     check_error("Expected ']' after '*', got 'i'", "<format>", 1, 4, 4);
304     json_decref(j);
305
306     j = json_pack("{sssi}", "foo", "bar", "baz", 42);
307     if(!json_unpack_ex(j, &error, 0, "{ss!si}", "foo", &s, "baz", &i1))
308         fail("json_unpack failed to catch ! in the middle of an object");
309     check_error("Expected '}' after '!', got 's'", "<format>", 1, 5, 5);
310
311     if(!json_unpack_ex(j, &error, 0, "{ss*si}", "foo", &s, "baz", &i1))
312         fail("json_unpack failed to catch ! in the middle of an object");
313     check_error("Expected '}' after '*', got 's'", "<format>", 1, 5, 5);
314     json_decref(j);
315
316     /* Error in nested object */
317     j = json_pack("{s{snsn}}", "foo", "bar", "baz");
318     if(!json_unpack_ex(j, &error, 0, "{s{sn!}}", "foo", "bar"))
319         fail("json_unpack nested object with strict validation failed");
320     check_error("1 object item(s) left unpacked", "<validation>", 1, 7, 7);
321     json_decref(j);
322
323     /* Error in nested array */
324     j = json_pack("[[ii]]", 1, 2);
325     if(!json_unpack_ex(j, &error, 0, "[[i!]]", &i1))
326         fail("json_unpack nested array with strict validation failed");
327     check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
328     json_decref(j);
329
330     return 0;
331 }