1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* util/support/t_json.c - JSON test program */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (c) 2010 Kungliga Tekniska Högskolan
5*7f2fe78bSCy Schubert * (Royal Institute of Technology, Stockholm, Sweden).
6*7f2fe78bSCy Schubert * All rights reserved.
7*7f2fe78bSCy Schubert *
8*7f2fe78bSCy Schubert * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
9*7f2fe78bSCy Schubert *
10*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
11*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions
12*7f2fe78bSCy Schubert * are met:
13*7f2fe78bSCy Schubert *
14*7f2fe78bSCy Schubert * 1. Redistributions of source code must retain the above copyright
15*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
16*7f2fe78bSCy Schubert *
17*7f2fe78bSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
18*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in the
19*7f2fe78bSCy Schubert * documentation and/or other materials provided with the distribution.
20*7f2fe78bSCy Schubert *
21*7f2fe78bSCy Schubert * 3. Neither the name of the Institute nor the names of its contributors
22*7f2fe78bSCy Schubert * may be used to endorse or promote products derived from this software
23*7f2fe78bSCy Schubert * without specific prior written permission.
24*7f2fe78bSCy Schubert *
25*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26*7f2fe78bSCy Schubert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*7f2fe78bSCy Schubert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*7f2fe78bSCy Schubert * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29*7f2fe78bSCy Schubert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*7f2fe78bSCy Schubert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*7f2fe78bSCy Schubert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*7f2fe78bSCy Schubert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*7f2fe78bSCy Schubert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*7f2fe78bSCy Schubert * SUCH DAMAGE.
36*7f2fe78bSCy Schubert */
37*7f2fe78bSCy Schubert /*
38*7f2fe78bSCy Schubert * Copyright (C) 2012 by the Massachusetts Institute of Technology.
39*7f2fe78bSCy Schubert * All rights reserved.
40*7f2fe78bSCy Schubert *
41*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
42*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions
43*7f2fe78bSCy Schubert * are met:
44*7f2fe78bSCy Schubert *
45*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright
46*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
47*7f2fe78bSCy Schubert *
48*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright
49*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in
50*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the
51*7f2fe78bSCy Schubert * distribution.
52*7f2fe78bSCy Schubert *
53*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54*7f2fe78bSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55*7f2fe78bSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
56*7f2fe78bSCy Schubert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
57*7f2fe78bSCy Schubert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
58*7f2fe78bSCy Schubert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
59*7f2fe78bSCy Schubert * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
60*7f2fe78bSCy Schubert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
62*7f2fe78bSCy Schubert * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63*7f2fe78bSCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
64*7f2fe78bSCy Schubert * OF THE POSSIBILITY OF SUCH DAMAGE.
65*7f2fe78bSCy Schubert */
66*7f2fe78bSCy Schubert
67*7f2fe78bSCy Schubert #include <errno.h>
68*7f2fe78bSCy Schubert #include <stdio.h>
69*7f2fe78bSCy Schubert #include <stdlib.h>
70*7f2fe78bSCy Schubert #include <string.h>
71*7f2fe78bSCy Schubert
72*7f2fe78bSCy Schubert #include <k5-json.h>
73*7f2fe78bSCy Schubert
74*7f2fe78bSCy Schubert static void
err(const char * str)75*7f2fe78bSCy Schubert err(const char *str)
76*7f2fe78bSCy Schubert {
77*7f2fe78bSCy Schubert fprintf(stderr, "%s\n", str);
78*7f2fe78bSCy Schubert exit(1);
79*7f2fe78bSCy Schubert }
80*7f2fe78bSCy Schubert
81*7f2fe78bSCy Schubert static void
check(int pred,const char * str)82*7f2fe78bSCy Schubert check(int pred, const char *str)
83*7f2fe78bSCy Schubert {
84*7f2fe78bSCy Schubert if (!pred)
85*7f2fe78bSCy Schubert err(str);
86*7f2fe78bSCy Schubert }
87*7f2fe78bSCy Schubert
88*7f2fe78bSCy Schubert static void
test_array()89*7f2fe78bSCy Schubert test_array()
90*7f2fe78bSCy Schubert {
91*7f2fe78bSCy Schubert k5_json_string v1;
92*7f2fe78bSCy Schubert k5_json_number v2;
93*7f2fe78bSCy Schubert k5_json_null v3;
94*7f2fe78bSCy Schubert k5_json_array a;
95*7f2fe78bSCy Schubert k5_json_value v;
96*7f2fe78bSCy Schubert
97*7f2fe78bSCy Schubert k5_json_array_create(&a);
98*7f2fe78bSCy Schubert k5_json_string_create("abc", &v1);
99*7f2fe78bSCy Schubert k5_json_array_add(a, v1);
100*7f2fe78bSCy Schubert k5_json_number_create(2, &v2);
101*7f2fe78bSCy Schubert k5_json_array_add(a, v2);
102*7f2fe78bSCy Schubert k5_json_null_create(&v3);
103*7f2fe78bSCy Schubert k5_json_array_add(a, v3);
104*7f2fe78bSCy Schubert
105*7f2fe78bSCy Schubert check(k5_json_array_length(a) == 3, "array length");
106*7f2fe78bSCy Schubert v = k5_json_array_get(a, 2);
107*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_NULL, "array[2] tid");
108*7f2fe78bSCy Schubert v = k5_json_array_get(a, 1);
109*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_NUMBER, "array[1] tid");
110*7f2fe78bSCy Schubert check(k5_json_number_value(v) == 2, "array[1] value");
111*7f2fe78bSCy Schubert v = k5_json_array_get(a, 0);
112*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_STRING, "array[0] tid");
113*7f2fe78bSCy Schubert check(strcmp(k5_json_string_utf8(v), "abc") == 0, "array[0] value");
114*7f2fe78bSCy Schubert
115*7f2fe78bSCy Schubert k5_json_release(v1);
116*7f2fe78bSCy Schubert k5_json_release(v2);
117*7f2fe78bSCy Schubert k5_json_release(a);
118*7f2fe78bSCy Schubert
119*7f2fe78bSCy Schubert k5_json_array_fmt(&a, "vnbiLssB", v3, 1, 9, (long long)-6, "def", NULL,
120*7f2fe78bSCy Schubert (void *)"ghij", (size_t)4);
121*7f2fe78bSCy Schubert v = k5_json_array_get(a, 0);
122*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_NULL, "fmt array[0] tid");
123*7f2fe78bSCy Schubert v = k5_json_array_get(a, 1);
124*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_NULL, "fmt array[1] tid");
125*7f2fe78bSCy Schubert v = k5_json_array_get(a, 2);
126*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_BOOL, "fmt array[2] tid");
127*7f2fe78bSCy Schubert check(k5_json_bool_value(v), "fmt array[2] value");
128*7f2fe78bSCy Schubert v = k5_json_array_get(a, 3);
129*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_NUMBER, "fmt array[3] tid");
130*7f2fe78bSCy Schubert check(k5_json_number_value(v) == 9, "fmt array[3] value");
131*7f2fe78bSCy Schubert v = k5_json_array_get(a, 4);
132*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_NUMBER, "fmt array[4] tid");
133*7f2fe78bSCy Schubert check(k5_json_number_value(v) == -6, "fmt array[4] value");
134*7f2fe78bSCy Schubert v = k5_json_array_get(a, 5);
135*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_STRING, "fmt array[5] tid");
136*7f2fe78bSCy Schubert check(strcmp(k5_json_string_utf8(v), "def") == 0, "fmt array[5] value");
137*7f2fe78bSCy Schubert v = k5_json_array_get(a, 6);
138*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_NULL, "fmt array[6] tid");
139*7f2fe78bSCy Schubert v = k5_json_array_get(a, 7);
140*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_STRING, "fmt array[7] tid");
141*7f2fe78bSCy Schubert check(strcmp(k5_json_string_utf8(v), "Z2hpag==") == 0,
142*7f2fe78bSCy Schubert "fmt array[7] value");
143*7f2fe78bSCy Schubert k5_json_release(v3);
144*7f2fe78bSCy Schubert k5_json_release(a);
145*7f2fe78bSCy Schubert }
146*7f2fe78bSCy Schubert
147*7f2fe78bSCy Schubert static void
test_object(void)148*7f2fe78bSCy Schubert test_object(void)
149*7f2fe78bSCy Schubert {
150*7f2fe78bSCy Schubert k5_json_object object;
151*7f2fe78bSCy Schubert k5_json_number n, v1;
152*7f2fe78bSCy Schubert k5_json_string s, v2;
153*7f2fe78bSCy Schubert
154*7f2fe78bSCy Schubert k5_json_object_create(&object);
155*7f2fe78bSCy Schubert k5_json_number_create(1, &v1);
156*7f2fe78bSCy Schubert k5_json_object_set(object, "key1", v1);
157*7f2fe78bSCy Schubert k5_json_string_create("hejsan", &v2);
158*7f2fe78bSCy Schubert k5_json_object_set(object, "key2", v2);
159*7f2fe78bSCy Schubert
160*7f2fe78bSCy Schubert n = k5_json_object_get(object, "key1");
161*7f2fe78bSCy Schubert if (k5_json_number_value(n) != 1)
162*7f2fe78bSCy Schubert err("Retrieving key1 from object failed");
163*7f2fe78bSCy Schubert
164*7f2fe78bSCy Schubert s = k5_json_object_get(object, "key2");
165*7f2fe78bSCy Schubert if (strcmp(k5_json_string_utf8(s), "hejsan") != 0)
166*7f2fe78bSCy Schubert err("Retrieving key2 from object failed");
167*7f2fe78bSCy Schubert
168*7f2fe78bSCy Schubert check(k5_json_object_get(object, "key3") == NULL,
169*7f2fe78bSCy Schubert "object nonexistent key");
170*7f2fe78bSCy Schubert
171*7f2fe78bSCy Schubert k5_json_object_set(object, "key1", NULL);
172*7f2fe78bSCy Schubert check(k5_json_object_get(object, "key1") == NULL,
173*7f2fe78bSCy Schubert "object removed key");
174*7f2fe78bSCy Schubert check(k5_json_object_get(object, "key2") != NULL,
175*7f2fe78bSCy Schubert "object remaining key");
176*7f2fe78bSCy Schubert
177*7f2fe78bSCy Schubert k5_json_release(v1);
178*7f2fe78bSCy Schubert k5_json_release(v2);
179*7f2fe78bSCy Schubert k5_json_release(object);
180*7f2fe78bSCy Schubert }
181*7f2fe78bSCy Schubert
182*7f2fe78bSCy Schubert static void
test_string(void)183*7f2fe78bSCy Schubert test_string(void)
184*7f2fe78bSCy Schubert {
185*7f2fe78bSCy Schubert k5_json_string s1, s2, s3;
186*7f2fe78bSCy Schubert unsigned char *data;
187*7f2fe78bSCy Schubert size_t len;
188*7f2fe78bSCy Schubert
189*7f2fe78bSCy Schubert k5_json_string_create("hejsan", &s1);
190*7f2fe78bSCy Schubert k5_json_string_create("hejsan", &s2);
191*7f2fe78bSCy Schubert k5_json_string_create_base64("55555", 5, &s3);
192*7f2fe78bSCy Schubert
193*7f2fe78bSCy Schubert if (strcmp(k5_json_string_utf8(s1), k5_json_string_utf8(s2)) != 0)
194*7f2fe78bSCy Schubert err("Identical strings are not identical");
195*7f2fe78bSCy Schubert if (strcmp(k5_json_string_utf8(s3), "NTU1NTU=") != 0)
196*7f2fe78bSCy Schubert err("base64 string has incorrect value");
197*7f2fe78bSCy Schubert k5_json_string_unbase64(s3, &data, &len);
198*7f2fe78bSCy Schubert if (len != 5 || memcmp(data, "55555", 5) != 0)
199*7f2fe78bSCy Schubert err("base64 string doesn't decode to correct value");
200*7f2fe78bSCy Schubert free(data);
201*7f2fe78bSCy Schubert
202*7f2fe78bSCy Schubert k5_json_release(s1);
203*7f2fe78bSCy Schubert k5_json_release(s2);
204*7f2fe78bSCy Schubert k5_json_release(s3);
205*7f2fe78bSCy Schubert }
206*7f2fe78bSCy Schubert
207*7f2fe78bSCy Schubert static void
test_json(void)208*7f2fe78bSCy Schubert test_json(void)
209*7f2fe78bSCy Schubert {
210*7f2fe78bSCy Schubert static char *tests[] = {
211*7f2fe78bSCy Schubert "{\"k1\":\"s1\",\"k2\":\"s2\"}",
212*7f2fe78bSCy Schubert "{\"k1\":[\"s1\",\"s2\",\"s3\"],\"k2\":\"s3\"}",
213*7f2fe78bSCy Schubert "{\"k1\":{\"k2\":\"s1\",\"k3\":\"s2\",\"k4\":\"s3\"},\"k5\":\"s4\"}",
214*7f2fe78bSCy Schubert "[\"v1\",\"v2\",[\"v3\",\"v4\",[\"v 5\",\" v 7 \"]],-123456789,"
215*7f2fe78bSCy Schubert "null,true,false,123456789,\"\"]",
216*7f2fe78bSCy Schubert "-1",
217*7f2fe78bSCy Schubert "\"\\\\abc\\\"\\nde\\b\\r/\\ff\\tghi\\u0001\\u001F\"",
218*7f2fe78bSCy Schubert "9223372036854775807",
219*7f2fe78bSCy Schubert "-9223372036854775808",
220*7f2fe78bSCy Schubert NULL
221*7f2fe78bSCy Schubert };
222*7f2fe78bSCy Schubert char **tptr, *s, *enc, *p, orig;
223*7f2fe78bSCy Schubert int i;
224*7f2fe78bSCy Schubert k5_json_value v, v2;
225*7f2fe78bSCy Schubert
226*7f2fe78bSCy Schubert check(k5_json_decode("\"string\"", &v) == 0, "string1");
227*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_STRING, "string1 tid");
228*7f2fe78bSCy Schubert check(strcmp(k5_json_string_utf8(v), "string") == 0, "string1 utf8");
229*7f2fe78bSCy Schubert k5_json_release(v);
230*7f2fe78bSCy Schubert
231*7f2fe78bSCy Schubert check(k5_json_decode("\t \"foo\\\"bar\" ", &v) == 0, "string2");
232*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_STRING, "string2 tid");
233*7f2fe78bSCy Schubert check(strcmp(k5_json_string_utf8(v), "foo\"bar") == 0, "string2 utf8");
234*7f2fe78bSCy Schubert k5_json_release(v);
235*7f2fe78bSCy Schubert
236*7f2fe78bSCy Schubert check(k5_json_decode(" { \"key\" : \"value\" }", &v) == 0, "object1");
237*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_OBJECT, "object1 tid");
238*7f2fe78bSCy Schubert v2 = k5_json_object_get(v, "key");
239*7f2fe78bSCy Schubert check(v2 != NULL, "object[key]");
240*7f2fe78bSCy Schubert check(k5_json_get_tid(v2) == K5_JSON_TID_STRING, "object1[key] tid");
241*7f2fe78bSCy Schubert check(strcmp(k5_json_string_utf8(v2), "value") == 0, "object1[key] utf8");
242*7f2fe78bSCy Schubert k5_json_release(v);
243*7f2fe78bSCy Schubert
244*7f2fe78bSCy Schubert check(k5_json_decode("{ \"k1\" : { \"k2\" : \"s2\", \"k3\" : \"s3\" }, "
245*7f2fe78bSCy Schubert "\"k4\" : \"s4\" }", &v) == 0, "object2");
246*7f2fe78bSCy Schubert v2 = k5_json_object_get(v, "k1");
247*7f2fe78bSCy Schubert check(v2 != NULL, "object2[k1]");
248*7f2fe78bSCy Schubert check(k5_json_get_tid(v2) == K5_JSON_TID_OBJECT, "object2[k1] tid");
249*7f2fe78bSCy Schubert v2 = k5_json_object_get(v2, "k3");
250*7f2fe78bSCy Schubert check(v2 != NULL, "object2[k1][k3]");
251*7f2fe78bSCy Schubert check(k5_json_get_tid(v2) == K5_JSON_TID_STRING, "object2[k1][k3] tid");
252*7f2fe78bSCy Schubert check(strcmp(k5_json_string_utf8(v2), "s3") == 0, "object2[k1][k3] utf8");
253*7f2fe78bSCy Schubert k5_json_release(v);
254*7f2fe78bSCy Schubert
255*7f2fe78bSCy Schubert check(k5_json_decode("{ \"k1\" : 1 }", &v) == 0, "object3");
256*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_OBJECT, "object3 id");
257*7f2fe78bSCy Schubert v2 = k5_json_object_get(v, "k1");
258*7f2fe78bSCy Schubert check(k5_json_get_tid(v2) == K5_JSON_TID_NUMBER, "object3[k1] tid");
259*7f2fe78bSCy Schubert check(k5_json_number_value(v2) == 1, "object3[k1] value");
260*7f2fe78bSCy Schubert k5_json_release(v);
261*7f2fe78bSCy Schubert
262*7f2fe78bSCy Schubert check(k5_json_decode("-10", &v) == 0, "number1");
263*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_NUMBER, "number1 tid");
264*7f2fe78bSCy Schubert check(k5_json_number_value(v) == -10, "number1 value");
265*7f2fe78bSCy Schubert k5_json_release(v);
266*7f2fe78bSCy Schubert
267*7f2fe78bSCy Schubert check(k5_json_decode("99", &v) == 0, "number2");
268*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_NUMBER, "number2 tid");
269*7f2fe78bSCy Schubert check(k5_json_number_value(v) == 99, "number2 value");
270*7f2fe78bSCy Schubert k5_json_release(v);
271*7f2fe78bSCy Schubert
272*7f2fe78bSCy Schubert check(k5_json_decode(" [ 1 ]", &v) == 0, "array1");
273*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_ARRAY, "array1 tid");
274*7f2fe78bSCy Schubert check(k5_json_array_length(v) == 1, "array1 len");
275*7f2fe78bSCy Schubert v2 = k5_json_array_get(v, 0);
276*7f2fe78bSCy Schubert check(v2 != NULL, "array1[0]");
277*7f2fe78bSCy Schubert check(k5_json_get_tid(v2) == K5_JSON_TID_NUMBER, "array1[0] tid");
278*7f2fe78bSCy Schubert check(k5_json_number_value(v2) == 1, "array1[0] value");
279*7f2fe78bSCy Schubert k5_json_release(v);
280*7f2fe78bSCy Schubert
281*7f2fe78bSCy Schubert check(k5_json_decode(" [ -1 ]", &v) == 0, "array2");
282*7f2fe78bSCy Schubert check(k5_json_get_tid(v) == K5_JSON_TID_ARRAY, "array2 tid");
283*7f2fe78bSCy Schubert check(k5_json_array_length(v) == 1, "array2 len");
284*7f2fe78bSCy Schubert v2 = k5_json_array_get(v, 0);
285*7f2fe78bSCy Schubert check(v2 != NULL, "array2[0]");
286*7f2fe78bSCy Schubert check(k5_json_get_tid(v2) == K5_JSON_TID_NUMBER, "array2[0] tid");
287*7f2fe78bSCy Schubert check(k5_json_number_value(v2) == -1, "array2[0] value");
288*7f2fe78bSCy Schubert k5_json_release(v);
289*7f2fe78bSCy Schubert
290*7f2fe78bSCy Schubert check(k5_json_decode("18446744073709551616", &v) == EOVERFLOW,
291*7f2fe78bSCy Schubert "unsigned 64-bit overflow");
292*7f2fe78bSCy Schubert check(k5_json_decode("9223372036854775808", &v) == EOVERFLOW,
293*7f2fe78bSCy Schubert "signed 64-bit positive overflow");
294*7f2fe78bSCy Schubert check(k5_json_decode("-9223372036854775809", &v) == EOVERFLOW,
295*7f2fe78bSCy Schubert "signed 64-bit negative overflow");
296*7f2fe78bSCy Schubert
297*7f2fe78bSCy Schubert for (tptr = tests; *tptr != NULL; tptr++) {
298*7f2fe78bSCy Schubert s = strdup(*tptr);
299*7f2fe78bSCy Schubert if (k5_json_decode(s, &v))
300*7f2fe78bSCy Schubert err(s);
301*7f2fe78bSCy Schubert if (k5_json_encode(v, &enc) || strcmp(enc, s) != 0)
302*7f2fe78bSCy Schubert err(s);
303*7f2fe78bSCy Schubert free(enc);
304*7f2fe78bSCy Schubert k5_json_release(v);
305*7f2fe78bSCy Schubert
306*7f2fe78bSCy Schubert /* Fuzz bytes. Parsing may succeed or fail; we're just looking for
307*7f2fe78bSCy Schubert * memory access bugs. */
308*7f2fe78bSCy Schubert for (p = s; *p != '\0'; p++) {
309*7f2fe78bSCy Schubert orig = *p;
310*7f2fe78bSCy Schubert for (i = 0; i <= 255; i++) {
311*7f2fe78bSCy Schubert *p = i;
312*7f2fe78bSCy Schubert k5_json_decode(s, &v);
313*7f2fe78bSCy Schubert k5_json_release(v);
314*7f2fe78bSCy Schubert }
315*7f2fe78bSCy Schubert *p = orig;
316*7f2fe78bSCy Schubert }
317*7f2fe78bSCy Schubert free(s);
318*7f2fe78bSCy Schubert }
319*7f2fe78bSCy Schubert }
320*7f2fe78bSCy Schubert
321*7f2fe78bSCy Schubert int
main(int argc,char ** argv)322*7f2fe78bSCy Schubert main(int argc, char **argv)
323*7f2fe78bSCy Schubert {
324*7f2fe78bSCy Schubert test_array();
325*7f2fe78bSCy Schubert test_object();
326*7f2fe78bSCy Schubert test_string();
327*7f2fe78bSCy Schubert test_json();
328*7f2fe78bSCy Schubert return 0;
329*7f2fe78bSCy Schubert }
330