• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
3 
4   Permission is hereby granted, free of charge, to any person obtaining a copy
5   of this software and associated documentation files (the "Software"), to deal
6   in the Software without restriction, including without limitation the rights
7   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8   copies of the Software, and to permit persons to whom the Software is
9   furnished to do so, subject to the following conditions:
10 
11   The above copyright notice and this permission notice shall be included in
12   all copies or substantial portions of the Software.
13 
14   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   THE SOFTWARE.
21 */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "unity/examples/unity_config.h"
28 #include "unity/src/unity.h"
29 #include "common.h"
30 
failing_malloc(size_t size)31 static void * CJSON_CDECL failing_malloc(size_t size)
32 {
33     (void)size;
34     return NULL;
35 }
36 
37 /* work around MSVC error C2322: '...' address of dillimport '...' is not static */
normal_free(void * pointer)38 static void CJSON_CDECL normal_free(void *pointer)
39 {
40     free(pointer);
41 }
42 
43 static cJSON_Hooks failing_hooks = {
44     failing_malloc,
45     normal_free
46 };
47 
cjson_add_null_should_add_null(void)48 static void cjson_add_null_should_add_null(void)
49 {
50     cJSON *root = cJSON_CreateObject();
51     cJSON *null = NULL;
52 
53     cJSON_AddNullToObject(root, "null");
54 
55     TEST_ASSERT_NOT_NULL(null = cJSON_GetObjectItemCaseSensitive(root, "null"));
56     TEST_ASSERT_EQUAL_INT(null->type, cJSON_NULL);
57 
58     cJSON_Delete(root);
59 }
60 
cjson_add_null_should_fail_with_null_pointers(void)61 static void cjson_add_null_should_fail_with_null_pointers(void)
62 {
63     cJSON *root = cJSON_CreateObject();
64 
65     TEST_ASSERT_NULL(cJSON_AddNullToObject(NULL, "null"));
66     TEST_ASSERT_NULL(cJSON_AddNullToObject(root, NULL));
67 
68     cJSON_Delete(root);
69 }
70 
cjson_add_null_should_fail_on_allocation_failure(void)71 static void cjson_add_null_should_fail_on_allocation_failure(void)
72 {
73     cJSON *root = cJSON_CreateObject();
74 
75     cJSON_InitHooks(&failing_hooks);
76 
77     TEST_ASSERT_NULL(cJSON_AddNullToObject(root, "null"));
78 
79     cJSON_InitHooks(NULL);
80 
81     cJSON_Delete(root);
82 }
83 
cjson_add_true_should_add_true(void)84 static void cjson_add_true_should_add_true(void)
85 {
86     cJSON *root = cJSON_CreateObject();
87     cJSON *true_item = NULL;
88 
89     cJSON_AddTrueToObject(root, "true");
90 
91     TEST_ASSERT_NOT_NULL(true_item = cJSON_GetObjectItemCaseSensitive(root, "true"));
92     TEST_ASSERT_EQUAL_INT(true_item->type, cJSON_True);
93 
94     cJSON_Delete(root);
95 }
96 
cjson_add_true_should_fail_with_null_pointers(void)97 static void cjson_add_true_should_fail_with_null_pointers(void)
98 {
99     cJSON *root = cJSON_CreateObject();
100 
101     TEST_ASSERT_NULL(cJSON_AddTrueToObject(NULL, "true"));
102     TEST_ASSERT_NULL(cJSON_AddTrueToObject(root, NULL));
103 
104     cJSON_Delete(root);
105 }
106 
cjson_add_true_should_fail_on_allocation_failure(void)107 static void cjson_add_true_should_fail_on_allocation_failure(void)
108 {
109     cJSON *root = cJSON_CreateObject();
110 
111     cJSON_InitHooks(&failing_hooks);
112 
113     TEST_ASSERT_NULL(cJSON_AddTrueToObject(root, "true"));
114 
115     cJSON_InitHooks(NULL);
116 
117     cJSON_Delete(root);
118 }
119 
cjson_create_int_array_should_fail_on_allocation_failure(void)120 static void cjson_create_int_array_should_fail_on_allocation_failure(void)
121 {
122     int numbers[] = {1, 2, 3};
123 
124     cJSON_InitHooks(&failing_hooks);
125 
126     TEST_ASSERT_NULL(cJSON_CreateIntArray(numbers, 3));
127 
128     cJSON_InitHooks(NULL);
129 }
130 
cjson_create_float_array_should_fail_on_allocation_failure(void)131 static void cjson_create_float_array_should_fail_on_allocation_failure(void)
132 {
133     float numbers[] = {1.0f, 2.0f, 3.0f};
134 
135     cJSON_InitHooks(&failing_hooks);
136 
137     TEST_ASSERT_NULL(cJSON_CreateFloatArray(numbers, 3));
138 
139     cJSON_InitHooks(NULL);
140 }
141 
cjson_create_double_array_should_fail_on_allocation_failure(void)142 static void cjson_create_double_array_should_fail_on_allocation_failure(void)
143 {
144     double numbers[] = {1.0, 2.0, 3.0};
145 
146     cJSON_InitHooks(&failing_hooks);
147 
148     TEST_ASSERT_NULL(cJSON_CreateDoubleArray(numbers, 3));
149 
150     cJSON_InitHooks(NULL);
151 }
152 
cjson_create_string_array_should_fail_on_allocation_failure(void)153 static void cjson_create_string_array_should_fail_on_allocation_failure(void)
154 {
155     const char* strings[] = {"1", "2", "3"};
156 
157     cJSON_InitHooks(&failing_hooks);
158 
159     TEST_ASSERT_NULL(cJSON_CreateStringArray(strings, 3));
160 
161     cJSON_InitHooks(NULL);
162 }
163 
cjson_add_false_should_add_false(void)164 static void cjson_add_false_should_add_false(void)
165 {
166     cJSON *root = cJSON_CreateObject();
167     cJSON *false_item = NULL;
168 
169     cJSON_AddFalseToObject(root, "false");
170 
171     TEST_ASSERT_NOT_NULL(false_item = cJSON_GetObjectItemCaseSensitive(root, "false"));
172     TEST_ASSERT_EQUAL_INT(false_item->type, cJSON_False);
173 
174     cJSON_Delete(root);
175 }
176 
cjson_add_false_should_fail_with_null_pointers(void)177 static void cjson_add_false_should_fail_with_null_pointers(void)
178 {
179     cJSON *root = cJSON_CreateObject();
180 
181     TEST_ASSERT_NULL(cJSON_AddFalseToObject(NULL, "false"));
182     TEST_ASSERT_NULL(cJSON_AddFalseToObject(root, NULL));
183 
184     cJSON_Delete(root);
185 }
186 
cjson_add_false_should_fail_on_allocation_failure(void)187 static void cjson_add_false_should_fail_on_allocation_failure(void)
188 {
189     cJSON *root = cJSON_CreateObject();
190 
191     cJSON_InitHooks(&failing_hooks);
192 
193     TEST_ASSERT_NULL(cJSON_AddFalseToObject(root, "false"));
194 
195     cJSON_InitHooks(NULL);
196 
197     cJSON_Delete(root);
198 }
199 
cjson_add_bool_should_add_bool(void)200 static void cjson_add_bool_should_add_bool(void)
201 {
202     cJSON *root = cJSON_CreateObject();
203     cJSON *true_item = NULL;
204     cJSON *false_item = NULL;
205 
206     /* true */
207     cJSON_AddBoolToObject(root, "true", true);
208     TEST_ASSERT_NOT_NULL(true_item = cJSON_GetObjectItemCaseSensitive(root, "true"));
209     TEST_ASSERT_EQUAL_INT(true_item->type, cJSON_True);
210 
211     /* false */
212     cJSON_AddBoolToObject(root, "false", false);
213     TEST_ASSERT_NOT_NULL(false_item = cJSON_GetObjectItemCaseSensitive(root, "false"));
214     TEST_ASSERT_EQUAL_INT(false_item->type, cJSON_False);
215 
216     cJSON_Delete(root);
217 }
218 
cjson_add_bool_should_fail_with_null_pointers(void)219 static void cjson_add_bool_should_fail_with_null_pointers(void)
220 {
221     cJSON *root = cJSON_CreateObject();
222 
223     TEST_ASSERT_NULL(cJSON_AddBoolToObject(NULL, "false", false));
224     TEST_ASSERT_NULL(cJSON_AddBoolToObject(root, NULL, false));
225 
226     cJSON_Delete(root);
227 }
228 
cjson_add_bool_should_fail_on_allocation_failure(void)229 static void cjson_add_bool_should_fail_on_allocation_failure(void)
230 {
231     cJSON *root = cJSON_CreateObject();
232 
233     cJSON_InitHooks(&failing_hooks);
234 
235     TEST_ASSERT_NULL(cJSON_AddBoolToObject(root, "false", false));
236 
237     cJSON_InitHooks(NULL);
238 
239     cJSON_Delete(root);
240 }
241 
cjson_add_number_should_add_number(void)242 static void cjson_add_number_should_add_number(void)
243 {
244     cJSON *root = cJSON_CreateObject();
245     cJSON *number = NULL;
246 
247     cJSON_AddNumberToObject(root, "number", 42);
248 
249     TEST_ASSERT_NOT_NULL(number = cJSON_GetObjectItemCaseSensitive(root, "number"));
250 
251     TEST_ASSERT_EQUAL_INT(number->type, cJSON_Number);
252     TEST_ASSERT_EQUAL_DOUBLE(number->valuedouble, 42);
253     TEST_ASSERT_EQUAL_INT(number->valueint, 42);
254 
255     cJSON_Delete(root);
256 }
257 
cjson_add_number_should_fail_with_null_pointers(void)258 static void cjson_add_number_should_fail_with_null_pointers(void)
259 {
260     cJSON *root = cJSON_CreateObject();
261 
262     TEST_ASSERT_NULL(cJSON_AddNumberToObject(NULL, "number", 42));
263     TEST_ASSERT_NULL(cJSON_AddNumberToObject(root, NULL, 42));
264 
265     cJSON_Delete(root);
266 }
267 
cjson_add_number_should_fail_on_allocation_failure(void)268 static void cjson_add_number_should_fail_on_allocation_failure(void)
269 {
270     cJSON *root = cJSON_CreateObject();
271 
272     cJSON_InitHooks(&failing_hooks);
273 
274     TEST_ASSERT_NULL(cJSON_AddNumberToObject(root, "number", 42));
275 
276     cJSON_InitHooks(NULL);
277 
278     cJSON_Delete(root);
279 }
280 
cjson_add_string_should_add_string(void)281 static void cjson_add_string_should_add_string(void)
282 {
283     cJSON *root = cJSON_CreateObject();
284     cJSON *string = NULL;
285 
286     cJSON_AddStringToObject(root, "string", "Hello World!");
287 
288     TEST_ASSERT_NOT_NULL(string = cJSON_GetObjectItemCaseSensitive(root, "string"));
289     TEST_ASSERT_EQUAL_INT(string->type, cJSON_String);
290     TEST_ASSERT_EQUAL_STRING(string->valuestring, "Hello World!");
291 
292     cJSON_Delete(root);
293 }
294 
cjson_add_string_should_fail_with_null_pointers(void)295 static void cjson_add_string_should_fail_with_null_pointers(void)
296 {
297     cJSON *root = cJSON_CreateObject();
298 
299     TEST_ASSERT_NULL(cJSON_AddStringToObject(NULL, "string", "string"));
300     TEST_ASSERT_NULL(cJSON_AddStringToObject(root, NULL, "string"));
301 
302     cJSON_Delete(root);
303 }
304 
cjson_add_string_should_fail_on_allocation_failure(void)305 static void cjson_add_string_should_fail_on_allocation_failure(void)
306 {
307     cJSON *root = cJSON_CreateObject();
308 
309     cJSON_InitHooks(&failing_hooks);
310 
311     TEST_ASSERT_NULL(cJSON_AddStringToObject(root, "string", "string"));
312 
313     cJSON_InitHooks(NULL);
314 
315     cJSON_Delete(root);
316 }
317 
cjson_add_raw_should_add_raw(void)318 static void cjson_add_raw_should_add_raw(void)
319 {
320     cJSON *root = cJSON_CreateObject();
321     cJSON *raw = NULL;
322 
323     cJSON_AddRawToObject(root, "raw", "{}");
324 
325     TEST_ASSERT_NOT_NULL(raw = cJSON_GetObjectItemCaseSensitive(root, "raw"));
326     TEST_ASSERT_EQUAL_INT(raw->type, cJSON_Raw);
327     TEST_ASSERT_EQUAL_STRING(raw->valuestring, "{}");
328 
329     cJSON_Delete(root);
330 }
331 
cjson_add_raw_should_fail_with_null_pointers(void)332 static void cjson_add_raw_should_fail_with_null_pointers(void)
333 {
334     cJSON *root = cJSON_CreateObject();
335 
336     TEST_ASSERT_NULL(cJSON_AddRawToObject(NULL, "raw", "{}"));
337     TEST_ASSERT_NULL(cJSON_AddRawToObject(root, NULL, "{}"));
338 
339     cJSON_Delete(root);
340 }
341 
cjson_add_raw_should_fail_on_allocation_failure(void)342 static void cjson_add_raw_should_fail_on_allocation_failure(void)
343 {
344     cJSON *root = cJSON_CreateObject();
345 
346     cJSON_InitHooks(&failing_hooks);
347 
348     TEST_ASSERT_NULL(cJSON_AddRawToObject(root, "raw", "{}"));
349 
350     cJSON_InitHooks(NULL);
351 
352     cJSON_Delete(root);
353 }
354 
cJSON_add_object_should_add_object(void)355 static void cJSON_add_object_should_add_object(void)
356 {
357     cJSON *root = cJSON_CreateObject();
358     cJSON *object = NULL;
359 
360     cJSON_AddObjectToObject(root, "object");
361     TEST_ASSERT_NOT_NULL(object = cJSON_GetObjectItemCaseSensitive(root, "object"));
362     TEST_ASSERT_EQUAL_INT(object->type, cJSON_Object);
363 
364     cJSON_Delete(root);
365 }
366 
cjson_add_object_should_fail_with_null_pointers(void)367 static void cjson_add_object_should_fail_with_null_pointers(void)
368 {
369     cJSON *root = cJSON_CreateObject();
370 
371     TEST_ASSERT_NULL(cJSON_AddObjectToObject(NULL, "object"));
372     TEST_ASSERT_NULL(cJSON_AddObjectToObject(root, NULL));
373 
374     cJSON_Delete(root);
375 }
376 
cjson_add_object_should_fail_on_allocation_failure(void)377 static void cjson_add_object_should_fail_on_allocation_failure(void)
378 {
379     cJSON *root = cJSON_CreateObject();
380 
381     cJSON_InitHooks(&failing_hooks);
382 
383     TEST_ASSERT_NULL(cJSON_AddObjectToObject(root, "object"));
384 
385     cJSON_InitHooks(NULL);
386 
387     cJSON_Delete(root);
388 }
389 
cJSON_add_array_should_add_array(void)390 static void cJSON_add_array_should_add_array(void)
391 {
392     cJSON *root = cJSON_CreateObject();
393     cJSON *array = NULL;
394 
395     cJSON_AddArrayToObject(root, "array");
396     TEST_ASSERT_NOT_NULL(array = cJSON_GetObjectItemCaseSensitive(root, "array"));
397     TEST_ASSERT_EQUAL_INT(array->type, cJSON_Array);
398 
399     cJSON_Delete(root);
400 }
401 
cjson_add_array_should_fail_with_null_pointers(void)402 static void cjson_add_array_should_fail_with_null_pointers(void)
403 {
404     cJSON *root = cJSON_CreateObject();
405 
406     TEST_ASSERT_NULL(cJSON_AddArrayToObject(NULL, "array"));
407     TEST_ASSERT_NULL(cJSON_AddArrayToObject(root, NULL));
408 
409     cJSON_Delete(root);
410 }
411 
cjson_add_array_should_fail_on_allocation_failure(void)412 static void cjson_add_array_should_fail_on_allocation_failure(void)
413 {
414     cJSON *root = cJSON_CreateObject();
415 
416     cJSON_InitHooks(&failing_hooks);
417 
418     TEST_ASSERT_NULL(cJSON_AddArrayToObject(root, "array"));
419 
420     cJSON_InitHooks(NULL);
421 
422     cJSON_Delete(root);
423 }
424 
main(void)425 int CJSON_CDECL main(void)
426 {
427     UNITY_BEGIN();
428 
429     RUN_TEST(cjson_add_null_should_add_null);
430     RUN_TEST(cjson_add_null_should_fail_with_null_pointers);
431     RUN_TEST(cjson_add_null_should_fail_on_allocation_failure);
432 
433     RUN_TEST(cjson_add_true_should_add_true);
434     RUN_TEST(cjson_add_true_should_fail_with_null_pointers);
435     RUN_TEST(cjson_add_true_should_fail_on_allocation_failure);
436 
437     RUN_TEST(cjson_create_int_array_should_fail_on_allocation_failure);
438     RUN_TEST(cjson_create_float_array_should_fail_on_allocation_failure);
439     RUN_TEST(cjson_create_double_array_should_fail_on_allocation_failure);
440     RUN_TEST(cjson_create_string_array_should_fail_on_allocation_failure);
441 
442     RUN_TEST(cjson_add_false_should_add_false);
443     RUN_TEST(cjson_add_false_should_fail_with_null_pointers);
444     RUN_TEST(cjson_add_false_should_fail_on_allocation_failure);
445 
446     RUN_TEST(cjson_add_bool_should_add_bool);
447     RUN_TEST(cjson_add_bool_should_fail_with_null_pointers);
448     RUN_TEST(cjson_add_bool_should_fail_on_allocation_failure);
449 
450     RUN_TEST(cjson_add_number_should_add_number);
451     RUN_TEST(cjson_add_number_should_fail_with_null_pointers);
452     RUN_TEST(cjson_add_number_should_fail_on_allocation_failure);
453 
454     RUN_TEST(cjson_add_string_should_add_string);
455     RUN_TEST(cjson_add_string_should_fail_with_null_pointers);
456     RUN_TEST(cjson_add_string_should_fail_on_allocation_failure);
457 
458     RUN_TEST(cjson_add_raw_should_add_raw);
459     RUN_TEST(cjson_add_raw_should_fail_with_null_pointers);
460     RUN_TEST(cjson_add_raw_should_fail_on_allocation_failure);
461 
462     RUN_TEST(cJSON_add_object_should_add_object);
463     RUN_TEST(cjson_add_object_should_fail_with_null_pointers);
464     RUN_TEST(cjson_add_object_should_fail_on_allocation_failure);
465 
466     RUN_TEST(cJSON_add_array_should_add_array);
467     RUN_TEST(cjson_add_array_should_fail_with_null_pointers);
468     RUN_TEST(cjson_add_array_should_fail_on_allocation_failure);
469 
470     return UNITY_END();
471 }
472