1 /*
2  * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
3  *
4  * Jansson is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #include "util.h"
9 #include <jansson.h>
10 #include <string.h>
11 
test_bad_args(void)12 static void test_bad_args(void) {
13     json_t *num = json_integer(1);
14     json_t *txt = json_string("test");
15 
16     if (!num || !txt)
17         fail("failed to allocate test objects");
18 
19     if (json_string_nocheck(NULL) != NULL)
20         fail("json_string_nocheck with NULL argument did not return NULL");
21     if (json_stringn_nocheck(NULL, 0) != NULL)
22         fail("json_stringn_nocheck with NULL argument did not return NULL");
23     if (json_string(NULL) != NULL)
24         fail("json_string with NULL argument did not return NULL");
25     if (json_stringn(NULL, 0) != NULL)
26         fail("json_stringn with NULL argument did not return NULL");
27 
28     if (json_string_length(NULL) != 0)
29         fail("json_string_length with non-string argument did not return 0");
30     if (json_string_length(num) != 0)
31         fail("json_string_length with non-string argument did not return 0");
32 
33     if (json_string_value(NULL) != NULL)
34         fail("json_string_value with non-string argument did not return NULL");
35     if (json_string_value(num) != NULL)
36         fail("json_string_value with non-string argument did not return NULL");
37 
38     if (!json_string_setn_nocheck(NULL, "", 0))
39         fail("json_string_setn with non-string argument did not return error");
40     if (!json_string_setn_nocheck(num, "", 0))
41         fail("json_string_setn with non-string argument did not return error");
42     if (!json_string_setn_nocheck(txt, NULL, 0))
43         fail("json_string_setn_nocheck with NULL value did not return error");
44 
45     if (!json_string_set_nocheck(txt, NULL))
46         fail("json_string_set_nocheck with NULL value did not return error");
47     if (!json_string_set(txt, NULL))
48         fail("json_string_set with NULL value did not return error");
49     if (!json_string_setn(txt, NULL, 0))
50         fail("json_string_setn with NULL value did not return error");
51 
52     if (num->refcount != 1)
53         fail("unexpected reference count for num");
54     if (txt->refcount != 1)
55         fail("unexpected reference count for txt");
56 
57     json_decref(num);
58     json_decref(txt);
59 }
60 
61 /* Call the simple functions not covered by other tests of the public API */
run_tests()62 static void run_tests() {
63     json_t *value;
64 
65     value = json_boolean(1);
66     if (!json_is_true(value))
67         fail("json_boolean(1) failed");
68     json_decref(value);
69 
70     value = json_boolean(-123);
71     if (!json_is_true(value))
72         fail("json_boolean(-123) failed");
73     json_decref(value);
74 
75     value = json_boolean(0);
76     if (!json_is_false(value))
77         fail("json_boolean(0) failed");
78     if (json_boolean_value(value) != 0)
79         fail("json_boolean_value failed");
80     json_decref(value);
81 
82     value = json_integer(1);
83     if (json_typeof(value) != JSON_INTEGER)
84         fail("json_typeof failed");
85 
86     if (json_is_object(value))
87         fail("json_is_object failed");
88 
89     if (json_is_array(value))
90         fail("json_is_array failed");
91 
92     if (json_is_string(value))
93         fail("json_is_string failed");
94 
95     if (!json_is_integer(value))
96         fail("json_is_integer failed");
97 
98     if (json_is_real(value))
99         fail("json_is_real failed");
100 
101     if (!json_is_number(value))
102         fail("json_is_number failed");
103 
104     if (json_is_true(value))
105         fail("json_is_true failed");
106 
107     if (json_is_false(value))
108         fail("json_is_false failed");
109 
110     if (json_is_boolean(value))
111         fail("json_is_boolean failed");
112 
113     if (json_is_null(value))
114         fail("json_is_null failed");
115 
116     json_decref(value);
117 
118     value = json_string("foo");
119     if (!value)
120         fail("json_string failed");
121     if (strcmp(json_string_value(value), "foo"))
122         fail("invalid string value");
123     if (json_string_length(value) != 3)
124         fail("invalid string length");
125 
126     if (json_string_set(value, "barr"))
127         fail("json_string_set failed");
128     if (strcmp(json_string_value(value), "barr"))
129         fail("invalid string value");
130     if (json_string_length(value) != 4)
131         fail("invalid string length");
132 
133     if (json_string_setn(value, "hi\0ho", 5))
134         fail("json_string_set failed");
135     if (memcmp(json_string_value(value), "hi\0ho\0", 6))
136         fail("invalid string value");
137     if (json_string_length(value) != 5)
138         fail("invalid string length");
139 
140     json_decref(value);
141 
142     value = json_string(NULL);
143     if (value)
144         fail("json_string(NULL) failed");
145 
146     /* invalid UTF-8  */
147     value = json_string("a\xefz");
148     if (value)
149         fail("json_string(<invalid utf-8>) failed");
150 
151     value = json_string_nocheck("foo");
152     if (!value)
153         fail("json_string_nocheck failed");
154     if (strcmp(json_string_value(value), "foo"))
155         fail("invalid string value");
156     if (json_string_length(value) != 3)
157         fail("invalid string length");
158 
159     if (json_string_set_nocheck(value, "barr"))
160         fail("json_string_set_nocheck failed");
161     if (strcmp(json_string_value(value), "barr"))
162         fail("invalid string value");
163     if (json_string_length(value) != 4)
164         fail("invalid string length");
165 
166     if (json_string_setn_nocheck(value, "hi\0ho", 5))
167         fail("json_string_set failed");
168     if (memcmp(json_string_value(value), "hi\0ho\0", 6))
169         fail("invalid string value");
170     if (json_string_length(value) != 5)
171         fail("invalid string length");
172 
173     json_decref(value);
174 
175     /* invalid UTF-8 */
176     value = json_string_nocheck("qu\xff");
177     if (!value)
178         fail("json_string_nocheck failed");
179     if (strcmp(json_string_value(value), "qu\xff"))
180         fail("invalid string value");
181     if (json_string_length(value) != 3)
182         fail("invalid string length");
183 
184     if (json_string_set_nocheck(value, "\xfd\xfe\xff"))
185         fail("json_string_set_nocheck failed");
186     if (strcmp(json_string_value(value), "\xfd\xfe\xff"))
187         fail("invalid string value");
188     if (json_string_length(value) != 3)
189         fail("invalid string length");
190 
191     json_decref(value);
192 
193     value = json_integer(123);
194     if (!value)
195         fail("json_integer failed");
196     if (json_integer_value(value) != 123)
197         fail("invalid integer value");
198     if (json_number_value(value) != 123.0)
199         fail("invalid number value");
200 
201     if (json_integer_set(value, 321))
202         fail("json_integer_set failed");
203     if (json_integer_value(value) != 321)
204         fail("invalid integer value");
205     if (json_number_value(value) != 321.0)
206         fail("invalid number value");
207 
208     json_decref(value);
209 
210     value = json_real(123.123);
211     if (!value)
212         fail("json_real failed");
213     if (json_real_value(value) != 123.123)
214         fail("invalid integer value");
215     if (json_number_value(value) != 123.123)
216         fail("invalid number value");
217 
218     if (json_real_set(value, 321.321))
219         fail("json_real_set failed");
220     if (json_real_value(value) != 321.321)
221         fail("invalid real value");
222     if (json_number_value(value) != 321.321)
223         fail("invalid number value");
224 
225     json_decref(value);
226 
227     value = json_true();
228     if (!value)
229         fail("json_true failed");
230     json_decref(value);
231 
232     value = json_false();
233     if (!value)
234         fail("json_false failed");
235     json_decref(value);
236 
237     value = json_null();
238     if (!value)
239         fail("json_null failed");
240     json_decref(value);
241 
242     /* Test reference counting on singletons (true, false, null) */
243     value = json_true();
244     if (value->refcount != (size_t)-1)
245         fail("refcounting true works incorrectly");
246     json_decref(value);
247     if (value->refcount != (size_t)-1)
248         fail("refcounting true works incorrectly");
249     json_incref(value);
250     if (value->refcount != (size_t)-1)
251         fail("refcounting true works incorrectly");
252 
253     value = json_false();
254     if (value->refcount != (size_t)-1)
255         fail("refcounting false works incorrectly");
256     json_decref(value);
257     if (value->refcount != (size_t)-1)
258         fail("refcounting false works incorrectly");
259     json_incref(value);
260     if (value->refcount != (size_t)-1)
261         fail("refcounting false works incorrectly");
262 
263     value = json_null();
264     if (value->refcount != (size_t)-1)
265         fail("refcounting null works incorrectly");
266     json_decref(value);
267     if (value->refcount != (size_t)-1)
268         fail("refcounting null works incorrectly");
269     json_incref(value);
270     if (value->refcount != (size_t)-1)
271         fail("refcounting null works incorrectly");
272 
273 #ifdef json_auto_t
274     value = json_string("foo");
275     {
276         json_auto_t *test = json_incref(value);
277         /* Use test so GCC doesn't complain it is unused. */
278         if (!json_is_string(test))
279             fail("value type check failed");
280     }
281     if (value->refcount != 1)
282         fail("automatic decrement failed");
283     json_decref(value);
284 #endif
285 
286     test_bad_args();
287 }
288