interplane.Get Omega
Read in your language

cJSON

[Ported from github.com/DaveGamble/cJSON] Fix: heap-use-after-free in merge_patch when patch is subtree of target (#1065)

Brought by drakestapleton

branch master Branches Tags History

cJSON / tests / misc_tests.c

838 lines · 32086 bytes Raw Blame History
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#include <math.h>
27
28#include "unity/examples/unity_config.h"
29#include "unity/src/unity.h"
30#include "common.h"
31
32static void cjson_array_foreach_should_loop_over_arrays(void)
33{
34 cJSON array[1];
35 cJSON elements[10];
36 cJSON *element_pointer = NULL;
37 size_t i = 0;
38
39 memset(array, 0, sizeof(array));
40 memset(elements, 0, sizeof(elements));
41
42 /* create array */
43 array[0].child = &elements[0];
44 elements[0].prev = NULL;
45 elements[9].next = NULL;
46 for (i = 0; i < 9; i++)
47 {
48 elements[i].next = &elements[i + 1];
49 elements[i + 1].prev = &elements[i];
50 }
51
52 i = 0;
53 cJSON_ArrayForEach(element_pointer, array)
54 {
55 TEST_ASSERT_TRUE_MESSAGE(element_pointer == &elements[i], "Not iterating over array properly");
56 i++;
57 }
58}
59
60static void cjson_array_foreach_should_not_dereference_null_pointer(void)
61{
62 cJSON *array = NULL;
63 cJSON *element = NULL;
64 cJSON_ArrayForEach(element, array);
65}
66
67static void cjson_get_object_item_should_get_object_items(void)
68{
69 cJSON *item = NULL;
70 cJSON *found = NULL;
71
72 item = cJSON_Parse("{\"one\":1, \"Two\":2, \"tHree\":3}");
73
74 found = cJSON_GetObjectItem(NULL, "test");
75 TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL pointer.");
76
77 found = cJSON_GetObjectItem(item, NULL);
78 TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
79
80 found = cJSON_GetObjectItem(item, "one");
81 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
82 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
83
84 found = cJSON_GetObjectItem(item, "tWo");
85 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
86 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 2);
87
88 found = cJSON_GetObjectItem(item, "three");
89 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find item.");
90 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 3);
91
92 found = cJSON_GetObjectItem(item, "four");
93 TEST_ASSERT_NULL_MESSAGE(found, "Should not find something that isn't there.");
94
95 cJSON_Delete(item);
96}
97
98static void cjson_get_object_item_case_sensitive_should_get_object_items(void)
99{
100 cJSON *item = NULL;
101 cJSON *found = NULL;
102
103 item = cJSON_Parse("{\"one\":1, \"Two\":2, \"tHree\":3}");
104
105 found = cJSON_GetObjectItemCaseSensitive(NULL, "test");
106 TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL pointer.");
107
108 found = cJSON_GetObjectItemCaseSensitive(item, NULL);
109 TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
110
111 found = cJSON_GetObjectItemCaseSensitive(item, "one");
112 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
113 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
114
115 found = cJSON_GetObjectItemCaseSensitive(item, "Two");
116 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
117 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 2);
118
119 found = cJSON_GetObjectItemCaseSensitive(item, "tHree");
120 TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find item.");
121 TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 3);
122
123 found = cJSON_GetObjectItemCaseSensitive(item, "One");
124 TEST_ASSERT_NULL_MESSAGE(found, "Should not find something that isn't there.");
125
126 cJSON_Delete(item);
127}
128
129static void cjson_get_object_item_should_not_crash_with_array(void)
130{
131 cJSON *array = NULL;
132 cJSON *found = NULL;
133 array = cJSON_Parse("[1]");
134
135 found = cJSON_GetObjectItem(array, "name");
136 TEST_ASSERT_NULL(found);
137
138 cJSON_Delete(array);
139}
140
141static void cjson_get_object_item_case_sensitive_should_not_crash_with_array(void)
142{
143 cJSON *array = NULL;
144 cJSON *found = NULL;
145 array = cJSON_Parse("[1]");
146
147 found = cJSON_GetObjectItemCaseSensitive(array, "name");
148 TEST_ASSERT_NULL(found);
149
150 cJSON_Delete(array);
151}
152
153static void typecheck_functions_should_check_type(void)
154{
155 cJSON invalid[1];
156 cJSON item[1];
157 invalid->type = cJSON_Invalid;
158 invalid->type |= cJSON_StringIsConst;
159 item->type = cJSON_False;
160 item->type |= cJSON_StringIsConst;
161
162 TEST_ASSERT_FALSE(cJSON_IsInvalid(NULL));
163 TEST_ASSERT_FALSE(cJSON_IsInvalid(item));
164 TEST_ASSERT_TRUE(cJSON_IsInvalid(invalid));
165
166 item->type = cJSON_False | cJSON_StringIsConst;
167 TEST_ASSERT_FALSE(cJSON_IsFalse(NULL));
168 TEST_ASSERT_FALSE(cJSON_IsFalse(invalid));
169 TEST_ASSERT_TRUE(cJSON_IsFalse(item));
170 TEST_ASSERT_TRUE(cJSON_IsBool(item));
171
172 item->type = cJSON_True | cJSON_StringIsConst;
173 TEST_ASSERT_FALSE(cJSON_IsTrue(NULL));
174 TEST_ASSERT_FALSE(cJSON_IsTrue(invalid));
175 TEST_ASSERT_TRUE(cJSON_IsTrue(item));
176 TEST_ASSERT_TRUE(cJSON_IsBool(item));
177
178 item->type = cJSON_NULL | cJSON_StringIsConst;
179 TEST_ASSERT_FALSE(cJSON_IsNull(NULL));
180 TEST_ASSERT_FALSE(cJSON_IsNull(invalid));
181 TEST_ASSERT_TRUE(cJSON_IsNull(item));
182
183 item->type = cJSON_Number | cJSON_StringIsConst;
184 TEST_ASSERT_FALSE(cJSON_IsNumber(NULL));
185 TEST_ASSERT_FALSE(cJSON_IsNumber(invalid));
186 TEST_ASSERT_TRUE(cJSON_IsNumber(item));
187
188 item->type = cJSON_String | cJSON_StringIsConst;
189 TEST_ASSERT_FALSE(cJSON_IsString(NULL));
190 TEST_ASSERT_FALSE(cJSON_IsString(invalid));
191 TEST_ASSERT_TRUE(cJSON_IsString(item));
192
193 item->type = cJSON_Array | cJSON_StringIsConst;
194 TEST_ASSERT_FALSE(cJSON_IsArray(NULL));
195 TEST_ASSERT_FALSE(cJSON_IsArray(invalid));
196 TEST_ASSERT_TRUE(cJSON_IsArray(item));
197
198 item->type = cJSON_Object | cJSON_StringIsConst;
199 TEST_ASSERT_FALSE(cJSON_IsObject(NULL));
200 TEST_ASSERT_FALSE(cJSON_IsObject(invalid));
201 TEST_ASSERT_TRUE(cJSON_IsObject(item));
202
203 item->type = cJSON_Raw | cJSON_StringIsConst;
204 TEST_ASSERT_FALSE(cJSON_IsRaw(NULL));
205 TEST_ASSERT_FALSE(cJSON_IsRaw(invalid));
206 TEST_ASSERT_TRUE(cJSON_IsRaw(item));
207}
208
209static void cjson_should_not_parse_to_deeply_nested_jsons(void)
210{
211 char deep_json[CJSON_NESTING_LIMIT + 1];
212 size_t position = 0;
213
214 for (position = 0; position < sizeof(deep_json); position++)
215 {
216 deep_json[position] = '[';
217 }
218 deep_json[sizeof(deep_json) - 1] = '\0';
219
220 TEST_ASSERT_NULL_MESSAGE(cJSON_Parse(deep_json), "To deep JSONs should not be parsed.");
221}
222
223static void cjson_should_not_follow_too_deep_circular_references(void)
224{
225 cJSON *o = cJSON_CreateArray();
226 cJSON *a = cJSON_CreateArray();
227 cJSON *b = cJSON_CreateArray();
228 cJSON *x;
229
230 cJSON_AddItemToArray(o, a);
231 cJSON_AddItemToArray(a, b);
232 cJSON_AddItemToArray(b, o);
233
234 x = cJSON_Duplicate(o, 1);
235 TEST_ASSERT_NULL(x);
236 cJSON_DetachItemFromArray(b, 0);
237 cJSON_Delete(o);
238}
239
240static void cjson_set_number_value_should_set_numbers(void)
241{
242 cJSON number[1] = {{NULL, NULL, NULL, cJSON_Number, NULL, 0, 0, NULL}};
243
244 cJSON_SetNumberValue(number, 1.5);
245 TEST_ASSERT_EQUAL(1, number->valueint);
246 TEST_ASSERT_EQUAL_DOUBLE(1.5, number->valuedouble);
247
248 cJSON_SetNumberValue(number, -1.5);
249 TEST_ASSERT_EQUAL(-1, number->valueint);
250 TEST_ASSERT_EQUAL_DOUBLE(-1.5, number->valuedouble);
251
252 cJSON_SetNumberValue(number, 1 + (double)INT_MAX);
253 TEST_ASSERT_EQUAL(INT_MAX, number->valueint);
254 TEST_ASSERT_EQUAL_DOUBLE(1 + (double)INT_MAX, number->valuedouble);
255
256 cJSON_SetNumberValue(number, -1 + (double)INT_MIN);
257 TEST_ASSERT_EQUAL(INT_MIN, number->valueint);
258 TEST_ASSERT_EQUAL_DOUBLE(-1 + (double)INT_MIN, number->valuedouble);
259}
260
261static void cjson_detach_item_via_pointer_should_detach_items(void)
262{
263 cJSON list[4];
264 cJSON parent[1];
265
266 memset(list, '\0', sizeof(list));
267
268 /* link the list */
269 list[0].next = &(list[1]);
270 list[1].next = &(list[2]);
271 list[2].next = &(list[3]);
272
273 list[3].prev = &(list[2]);
274 list[2].prev = &(list[1]);
275 list[1].prev = &(list[0]);
276 list[0].prev = &(list[3]);
277
278 parent->child = &list[0];
279
280 /* detach in the middle (list[1]) */
281 TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[1])) == &(list[1]), "Failed to detach in the middle.");
282 TEST_ASSERT_TRUE_MESSAGE((list[1].prev == NULL) && (list[1].next == NULL), "Didn't set pointers of detached item to NULL.");
283 TEST_ASSERT_TRUE((list[0].next == &(list[2])) && (list[2].prev == &(list[0])));
284
285 /* detach beginning (list[0]) */
286 TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[0])) == &(list[0]), "Failed to detach beginning.");
287 TEST_ASSERT_TRUE_MESSAGE((list[0].prev == NULL) && (list[0].next == NULL), "Didn't set pointers of detached item to NULL.");
288 TEST_ASSERT_TRUE_MESSAGE((list[2].prev == &(list[3])) && (parent->child == &(list[2])), "Didn't set the new beginning.");
289
290 /* detach end (list[3])*/
291 TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[3])) == &(list[3]), "Failed to detach end.");
292 TEST_ASSERT_TRUE_MESSAGE((list[3].prev == NULL) && (list[3].next == NULL), "Didn't set pointers of detached item to NULL.");
293 TEST_ASSERT_TRUE_MESSAGE((list[2].next == NULL) && (parent->child == &(list[2])), "Didn't set the new end");
294
295 /* detach single item (list[2]) */
296 TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &list[2]) == &list[2], "Failed to detach single item.");
297 TEST_ASSERT_TRUE_MESSAGE((list[2].prev == NULL) && (list[2].next == NULL), "Didn't set pointers of detached item to NULL.");
298 TEST_ASSERT_NULL_MESSAGE(parent->child, "Child of the parent wasn't set to NULL.");
299}
300
301static void cjson_detach_item_via_pointer_should_return_null_if_item_prev_is_null(void)
302{
303 cJSON list[2];
304 cJSON parent[1];
305
306 memset(list, '\0', sizeof(list));
307
308 /* link the list */
309 list[0].next = &(list[1]);
310
311 parent->child = &list[0];
312 TEST_ASSERT_NULL_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[1])), "Failed to detach in the middle.");
313 TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[0])) == &(list[0]), "Failed to detach in the middle.");
314}
315
316static void cjson_replace_item_via_pointer_should_replace_items(void)
317{
318 cJSON replacements[3];
319 cJSON *beginning = NULL;
320 cJSON *middle = NULL;
321 cJSON *end = NULL;
322 cJSON *array = NULL;
323
324 beginning = cJSON_CreateNull();
325 TEST_ASSERT_NOT_NULL(beginning);
326 middle = cJSON_CreateNull();
327 TEST_ASSERT_NOT_NULL(middle);
328 end = cJSON_CreateNull();
329 TEST_ASSERT_NOT_NULL(end);
330
331 array = cJSON_CreateArray();
332 TEST_ASSERT_NOT_NULL(array);
333
334 cJSON_AddItemToArray(array, beginning);
335 cJSON_AddItemToArray(array, middle);
336 cJSON_AddItemToArray(array, end);
337
338 memset(replacements, '\0', sizeof(replacements));
339
340 /* replace beginning */
341 TEST_ASSERT_TRUE(cJSON_ReplaceItemViaPointer(array, beginning, &(replacements[0])));
342 TEST_ASSERT_TRUE(replacements[0].prev == end);
343 TEST_ASSERT_TRUE(replacements[0].next == middle);
344 TEST_ASSERT_TRUE(middle->prev == &(replacements[0]));
345 TEST_ASSERT_TRUE(array->child == &(replacements[0]));
346
347 /* replace middle */
348 TEST_ASSERT_TRUE(cJSON_ReplaceItemViaPointer(array, middle, &(replacements[1])));
349 TEST_ASSERT_TRUE(replacements[1].prev == &(replacements[0]));
350 TEST_ASSERT_TRUE(replacements[1].next == end);
351 TEST_ASSERT_TRUE(end->prev == &(replacements[1]));
352
353 /* replace end */
354 TEST_ASSERT_TRUE(cJSON_ReplaceItemViaPointer(array, end, &(replacements[2])));
355 TEST_ASSERT_TRUE(replacements[2].prev == &(replacements[1]));
356 TEST_ASSERT_NULL(replacements[2].next);
357 TEST_ASSERT_TRUE(replacements[1].next == &(replacements[2]));
358
359 cJSON_free(array);
360}
361
362static void cjson_replace_item_in_object_should_preserve_name(void)
363{
364 cJSON root[1] = {{NULL, NULL, NULL, 0, NULL, 0, 0, NULL}};
365 cJSON *child = NULL;
366 cJSON *replacement = NULL;
367 cJSON_bool flag = false;
368
369 child = cJSON_CreateNumber(1);
370 TEST_ASSERT_NOT_NULL(child);
371 replacement = cJSON_CreateNumber(2);
372 TEST_ASSERT_NOT_NULL(replacement);
373
374 flag = cJSON_AddItemToObject(root, "child", child);
375 TEST_ASSERT_TRUE_MESSAGE(flag, "add item to object failed");
376 cJSON_ReplaceItemInObject(root, "child", replacement);
377
378 TEST_ASSERT_TRUE(root->child == replacement);
379 TEST_ASSERT_EQUAL_STRING("child", replacement->string);
380
381 cJSON_Delete(replacement);
382}
383
384static void cjson_functions_should_not_crash_with_null_pointers(void)
385{
386 char buffer[10];
387 cJSON *item = cJSON_CreateString("item");
388 cJSON *array = cJSON_CreateArray();
389 cJSON *item1 = cJSON_CreateString("item1");
390 cJSON *item2 = cJSON_CreateString("corrupted array item3");
391 cJSON *corruptedString = cJSON_CreateString("corrupted");
392 struct cJSON *originalPrev;
393
394 add_item_to_array(array, item1);
395 add_item_to_array(array, item2);
396
397 originalPrev = item2->prev;
398 item2->prev = NULL;
399 free(corruptedString->valuestring);
400 corruptedString->valuestring = NULL;
401
402 cJSON_InitHooks(NULL);
403 TEST_ASSERT_NULL(cJSON_Parse(NULL));
404 TEST_ASSERT_NULL(cJSON_ParseWithOpts(NULL, NULL, true));
405 TEST_ASSERT_NULL(cJSON_Print(NULL));
406 TEST_ASSERT_NULL(cJSON_PrintUnformatted(NULL));
407 TEST_ASSERT_NULL(cJSON_PrintBuffered(NULL, 10, true));
408 TEST_ASSERT_FALSE(cJSON_PrintPreallocated(NULL, buffer, sizeof(buffer), true));
409 TEST_ASSERT_FALSE(cJSON_PrintPreallocated(item, NULL, 1, true));
410 cJSON_Delete(NULL);
411 cJSON_GetArraySize(NULL);
412 TEST_ASSERT_NULL(cJSON_GetArrayItem(NULL, 0));
413 TEST_ASSERT_NULL(cJSON_GetObjectItem(NULL, "item"));
414 TEST_ASSERT_NULL(cJSON_GetObjectItem(item, NULL));
415 TEST_ASSERT_NULL(cJSON_GetObjectItemCaseSensitive(NULL, "item"));
416 TEST_ASSERT_NULL(cJSON_GetObjectItemCaseSensitive(item, NULL));
417 TEST_ASSERT_FALSE(cJSON_HasObjectItem(NULL, "item"));
418 TEST_ASSERT_FALSE(cJSON_HasObjectItem(item, NULL));
419 TEST_ASSERT_FALSE(cJSON_IsInvalid(NULL));
420 TEST_ASSERT_FALSE(cJSON_IsFalse(NULL));
421 TEST_ASSERT_FALSE(cJSON_IsTrue(NULL));
422 TEST_ASSERT_FALSE(cJSON_IsBool(NULL));
423 TEST_ASSERT_FALSE(cJSON_IsNull(NULL));
424 TEST_ASSERT_FALSE(cJSON_IsNumber(NULL));
425 TEST_ASSERT_FALSE(cJSON_IsString(NULL));
426 TEST_ASSERT_FALSE(cJSON_IsArray(NULL));
427 TEST_ASSERT_FALSE(cJSON_IsObject(NULL));
428 TEST_ASSERT_FALSE(cJSON_IsRaw(NULL));
429 TEST_ASSERT_NULL(cJSON_CreateString(NULL));
430 TEST_ASSERT_NULL(cJSON_CreateRaw(NULL));
431 TEST_ASSERT_NULL(cJSON_CreateIntArray(NULL, 10));
432 TEST_ASSERT_NULL(cJSON_CreateFloatArray(NULL, 10));
433 TEST_ASSERT_NULL(cJSON_CreateDoubleArray(NULL, 10));
434 TEST_ASSERT_NULL(cJSON_CreateStringArray(NULL, 10));
435 cJSON_AddItemToArray(NULL, item);
436 cJSON_AddItemToArray(item, NULL);
437 cJSON_AddItemToObject(item, "item", NULL);
438 cJSON_AddItemToObject(item, NULL, item);
439 cJSON_AddItemToObject(NULL, "item", item);
440 cJSON_AddItemToObjectCS(item, "item", NULL);
441 cJSON_AddItemToObjectCS(item, NULL, item);
442 cJSON_AddItemToObjectCS(NULL, "item", item);
443 cJSON_AddItemReferenceToArray(NULL, item);
444 cJSON_AddItemReferenceToArray(item, NULL);
445 cJSON_AddItemReferenceToObject(item, "item", NULL);
446 cJSON_AddItemReferenceToObject(item, NULL, item);
447 cJSON_AddItemReferenceToObject(NULL, "item", item);
448 TEST_ASSERT_NULL(cJSON_DetachItemViaPointer(NULL, item));
449 TEST_ASSERT_NULL(cJSON_DetachItemViaPointer(item, NULL));
450 TEST_ASSERT_NULL(cJSON_DetachItemFromArray(NULL, 0));
451 cJSON_DeleteItemFromArray(NULL, 0);
452 TEST_ASSERT_NULL(cJSON_DetachItemFromObject(NULL, "item"));
453 TEST_ASSERT_NULL(cJSON_DetachItemFromObject(item, NULL));
454 TEST_ASSERT_NULL(cJSON_DetachItemFromObjectCaseSensitive(NULL, "item"));
455 TEST_ASSERT_NULL(cJSON_DetachItemFromObjectCaseSensitive(item, NULL));
456 cJSON_DeleteItemFromObject(NULL, "item");
457 cJSON_DeleteItemFromObject(item, NULL);
458 cJSON_DeleteItemFromObjectCaseSensitive(NULL, "item");
459 cJSON_DeleteItemFromObjectCaseSensitive(item, NULL);
460 TEST_ASSERT_FALSE(cJSON_InsertItemInArray(array, 0, NULL));
461 TEST_ASSERT_FALSE(cJSON_InsertItemInArray(array, 1, item));
462 TEST_ASSERT_FALSE(cJSON_InsertItemInArray(NULL, 0, item));
463 TEST_ASSERT_FALSE(cJSON_InsertItemInArray(item, 0, NULL));
464 TEST_ASSERT_FALSE(cJSON_ReplaceItemViaPointer(NULL, item, item));
465 TEST_ASSERT_FALSE(cJSON_ReplaceItemViaPointer(item, NULL, item));
466 TEST_ASSERT_FALSE(cJSON_ReplaceItemViaPointer(item, item, NULL));
467 TEST_ASSERT_FALSE(cJSON_ReplaceItemInArray(item, 0, NULL));
468 TEST_ASSERT_FALSE(cJSON_ReplaceItemInArray(NULL, 0, item));
469 TEST_ASSERT_FALSE(cJSON_ReplaceItemInObject(NULL, "item", item));
470 TEST_ASSERT_FALSE(cJSON_ReplaceItemInObject(item, NULL, item));
471 TEST_ASSERT_FALSE(cJSON_ReplaceItemInObject(item, "item", NULL));
472 TEST_ASSERT_FALSE(cJSON_ReplaceItemInObjectCaseSensitive(NULL, "item", item));
473 TEST_ASSERT_FALSE(cJSON_ReplaceItemInObjectCaseSensitive(item, NULL, item));
474 TEST_ASSERT_FALSE(cJSON_ReplaceItemInObjectCaseSensitive(item, "item", NULL));
475 TEST_ASSERT_NULL(cJSON_Duplicate(NULL, true));
476 TEST_ASSERT_FALSE(cJSON_Compare(item, NULL, false));
477 TEST_ASSERT_FALSE(cJSON_Compare(NULL, item, false));
478 TEST_ASSERT_NULL(cJSON_SetValuestring(NULL, "test"));
479 TEST_ASSERT_NULL(cJSON_SetValuestring(corruptedString, "test"));
480 TEST_ASSERT_NULL(cJSON_SetValuestring(item, NULL));
481 cJSON_Minify(NULL);
482 /* cJSON_SetNumberHelper should handle NULL gracefully */
483 TEST_ASSERT_TRUE(isnan(cJSON_SetNumberHelper(NULL, 0)));
484
485 /* restore corrupted item2 to delete it */
486 item2->prev = originalPrev;
487 cJSON_Delete(corruptedString);
488 cJSON_Delete(array);
489 cJSON_Delete(item);
490}
491
492static void cjson_set_valuestring_should_return_null_if_strings_overlap(void)
493{
494 cJSON *obj;
495 char* str;
496 char* str2;
497
498 obj = cJSON_Parse("\"foo0z\"");
499
500 str = cJSON_SetValuestring(obj, "abcde");
501 str += 1;
502 /* The string passed to strcpy overlap which is not allowed.*/
503 str2 = cJSON_SetValuestring(obj, str);
504 /* If it overlaps, the string will be messed up.*/
505 TEST_ASSERT_TRUE(strcmp(str, "bcde") == 0);
506 TEST_ASSERT_NULL(str2);
507 cJSON_Delete(obj);
508}
509
510static void *CJSON_CDECL failing_realloc(void *pointer, size_t size)
511{
512 (void)size;
513 (void)pointer;
514 return NULL;
515}
516
517static void ensure_should_fail_on_failed_realloc(void)
518{
519 printbuffer buffer = {NULL, 10, 0, 0, false, false, {&malloc, &free, &failing_realloc}};
520 buffer.buffer = (unsigned char *)malloc(100);
521 TEST_ASSERT_NOT_NULL(buffer.buffer);
522
523 TEST_ASSERT_NULL_MESSAGE(ensure(&buffer, 200), "Ensure didn't fail with failing realloc.");
524}
525
526static void skip_utf8_bom_should_skip_bom(void)
527{
528 const unsigned char string[] = "\xEF\xBB\xBF{}";
529 parse_buffer buffer = {0, 0, 0, 0, {0, 0, 0}};
530 buffer.content = string;
531 buffer.length = sizeof(string);
532 buffer.hooks = global_hooks;
533
534 TEST_ASSERT_TRUE(skip_utf8_bom(&buffer) == &buffer);
535 TEST_ASSERT_EQUAL_UINT(3U, (unsigned int)buffer.offset);
536}
537
538static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void)
539{
540 const unsigned char string[] = " \xEF\xBB\xBF{}";
541 parse_buffer buffer = {0, 0, 0, 0, {0, 0, 0}};
542 buffer.content = string;
543 buffer.length = sizeof(string);
544 buffer.hooks = global_hooks;
545 buffer.offset = 1;
546
547 TEST_ASSERT_NULL(skip_utf8_bom(&buffer));
548}
549
550static void cjson_get_string_value_should_get_a_string(void)
551{
552 cJSON *string = cJSON_CreateString("test");
553 cJSON *number = cJSON_CreateNumber(1);
554
555 TEST_ASSERT_TRUE(cJSON_GetStringValue(string) == string->valuestring);
556 TEST_ASSERT_NULL(cJSON_GetStringValue(number));
557 TEST_ASSERT_NULL(cJSON_GetStringValue(NULL));
558
559 cJSON_Delete(number);
560 cJSON_Delete(string);
561}
562
563static void cjson_get_number_value_should_get_a_number(void)
564{
565 cJSON *string = cJSON_CreateString("test");
566 cJSON *number = cJSON_CreateNumber(1);
567
568 TEST_ASSERT_EQUAL_DOUBLE(cJSON_GetNumberValue(number), number->valuedouble);
569 TEST_ASSERT_DOUBLE_IS_NAN(cJSON_GetNumberValue(string));
570 TEST_ASSERT_DOUBLE_IS_NAN(cJSON_GetNumberValue(NULL));
571
572 cJSON_Delete(number);
573 cJSON_Delete(string);
574}
575
576static void cjson_create_string_reference_should_create_a_string_reference(void)
577{
578 const char *string = "I am a string!";
579
580 cJSON *string_reference = cJSON_CreateStringReference(string);
581 TEST_ASSERT_TRUE(string_reference->valuestring == string);
582 TEST_ASSERT_EQUAL_INT(cJSON_IsReference | cJSON_String, string_reference->type);
583
584 cJSON_Delete(string_reference);
585}
586
587static void cjson_create_object_reference_should_create_an_object_reference(void)
588{
589 cJSON *number_reference = NULL;
590 cJSON *number_object = cJSON_CreateObject();
591 cJSON *number = cJSON_CreateNumber(42);
592 const char key[] = "number";
593
594 TEST_ASSERT_TRUE(cJSON_IsNumber(number));
595 TEST_ASSERT_TRUE(cJSON_IsObject(number_object));
596 cJSON_AddItemToObjectCS(number_object, key, number);
597
598 number_reference = cJSON_CreateObjectReference(number);
599 TEST_ASSERT_TRUE(number_reference->child == number);
600 TEST_ASSERT_EQUAL_INT(cJSON_Object | cJSON_IsReference, number_reference->type);
601
602 cJSON_Delete(number_object);
603 cJSON_Delete(number_reference);
604}
605
606static void cjson_create_array_reference_should_create_an_array_reference(void)
607{
608 cJSON *number_reference = NULL;
609 cJSON *number_array = cJSON_CreateArray();
610 cJSON *number = cJSON_CreateNumber(42);
611
612 TEST_ASSERT_TRUE(cJSON_IsNumber(number));
613 TEST_ASSERT_TRUE(cJSON_IsArray(number_array));
614 cJSON_AddItemToArray(number_array, number);
615
616 number_reference = cJSON_CreateArrayReference(number);
617 TEST_ASSERT_TRUE(number_reference->child == number);
618 TEST_ASSERT_EQUAL_INT(cJSON_Array | cJSON_IsReference, number_reference->type);
619
620 cJSON_Delete(number_array);
621 cJSON_Delete(number_reference);
622}
623
624static void cjson_add_item_to_object_or_array_should_not_add_itself(void)
625{
626 cJSON *object = cJSON_CreateObject();
627 cJSON *array = cJSON_CreateArray();
628 cJSON_bool flag = false;
629
630 flag = cJSON_AddItemToObject(object, "key", object);
631 TEST_ASSERT_FALSE_MESSAGE(flag, "add an object to itself should fail");
632
633 flag = cJSON_AddItemToArray(array, array);
634 TEST_ASSERT_FALSE_MESSAGE(flag, "add an array to itself should fail");
635
636 cJSON_Delete(object);
637 cJSON_Delete(array);
638}
639
640static void cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased(void)
641{
642 cJSON *object = cJSON_CreateObject();
643 cJSON *number = cJSON_CreateNumber(42);
644 char *name = (char *)cJSON_strdup((const unsigned char *)"number", &global_hooks);
645
646 TEST_ASSERT_NOT_NULL(object);
647 TEST_ASSERT_NOT_NULL(number);
648 TEST_ASSERT_NOT_NULL(name);
649
650 number->string = name;
651
652 /* The following should not have a use after free
653 * that would show up in valgrind or with AddressSanitizer */
654 cJSON_AddItemToObject(object, number->string, number);
655
656 cJSON_Delete(object);
657}
658
659static void cjson_delete_item_from_array_should_not_broken_list_structure(void)
660{
661 const char expected_json1[] = "{\"rd\":[{\"a\":\"123\"}]}";
662 const char expected_json2[] = "{\"rd\":[{\"a\":\"123\"},{\"b\":\"456\"}]}";
663 const char expected_json3[] = "{\"rd\":[{\"b\":\"456\"}]}";
664 char *str1 = NULL;
665 char *str2 = NULL;
666 char *str3 = NULL;
667
668 cJSON *root = cJSON_Parse("{}");
669
670 cJSON *array = cJSON_AddArrayToObject(root, "rd");
671 cJSON *item1 = cJSON_Parse("{\"a\":\"123\"}");
672 cJSON *item2 = cJSON_Parse("{\"b\":\"456\"}");
673
674 cJSON_AddItemToArray(array, item1);
675 str1 = cJSON_PrintUnformatted(root);
676 TEST_ASSERT_EQUAL_STRING(expected_json1, str1);
677 free(str1);
678
679 cJSON_AddItemToArray(array, item2);
680 str2 = cJSON_PrintUnformatted(root);
681 TEST_ASSERT_EQUAL_STRING(expected_json2, str2);
682 free(str2);
683
684 /* this should not broken list structure */
685 cJSON_DeleteItemFromArray(array, 0);
686 str3 = cJSON_PrintUnformatted(root);
687 TEST_ASSERT_EQUAL_STRING(expected_json3, str3);
688 free(str3);
689
690 cJSON_Delete(root);
691}
692
693static void cjson_set_valuestring_to_object_should_not_leak_memory(void)
694{
695 cJSON *root = cJSON_Parse("{}");
696 const char *stringvalue = "valuestring could be changed safely";
697 const char *reference_valuestring = "reference item should be freed by yourself";
698 const char *short_valuestring = "shorter valuestring";
699 const char *long_valuestring = "new valuestring which much longer than previous should be changed safely";
700 cJSON *item1 = cJSON_CreateString(stringvalue);
701 cJSON *item2 = cJSON_CreateStringReference(reference_valuestring);
702 char *ptr1 = NULL;
703 char *return_value = NULL;
704
705 cJSON_AddItemToObject(root, "one", item1);
706 cJSON_AddItemToObject(root, "two", item2);
707
708 ptr1 = item1->valuestring;
709 return_value = cJSON_SetValuestring(cJSON_GetObjectItem(root, "one"), short_valuestring);
710 TEST_ASSERT_NOT_NULL(return_value);
711 TEST_ASSERT_EQUAL_PTR_MESSAGE(ptr1, return_value, "new valuestring shorter than old should not reallocate memory");
712 TEST_ASSERT_EQUAL_STRING(short_valuestring, cJSON_GetObjectItem(root, "one")->valuestring);
713
714 /* we needn't to free the original valuestring manually */
715 ptr1 = item1->valuestring;
716 return_value = cJSON_SetValuestring(cJSON_GetObjectItem(root, "one"), long_valuestring);
717 TEST_ASSERT_NOT_NULL(return_value);
718 TEST_ASSERT_NOT_EQUAL_MESSAGE(ptr1, return_value, "new valuestring longer than old should reallocate memory")
719 TEST_ASSERT_EQUAL_STRING(long_valuestring, cJSON_GetObjectItem(root, "one")->valuestring);
720
721 return_value = cJSON_SetValuestring(cJSON_GetObjectItem(root, "two"), long_valuestring);
722 TEST_ASSERT_NULL_MESSAGE(return_value, "valuestring of reference object should not be changed");
723 TEST_ASSERT_EQUAL_STRING(reference_valuestring, cJSON_GetObjectItem(root, "two")->valuestring);
724
725 cJSON_Delete(root);
726}
727
728static void cjson_set_bool_value_must_not_break_objects(void)
729{
730 cJSON *bobj, *sobj, *oobj, *refobj = NULL;
731
732 TEST_ASSERT_TRUE((cJSON_SetBoolValue(refobj, 1) == cJSON_Invalid));
733
734 bobj = cJSON_CreateFalse();
735 TEST_ASSERT_TRUE(cJSON_IsFalse(bobj));
736 TEST_ASSERT_TRUE((cJSON_SetBoolValue(bobj, 1) == cJSON_True));
737 TEST_ASSERT_TRUE(cJSON_IsTrue(bobj));
738 cJSON_SetBoolValue(bobj, 1);
739 TEST_ASSERT_TRUE(cJSON_IsTrue(bobj));
740 TEST_ASSERT_TRUE((cJSON_SetBoolValue(bobj, 0) == cJSON_False));
741 TEST_ASSERT_TRUE(cJSON_IsFalse(bobj));
742 cJSON_SetBoolValue(bobj, 0);
743 TEST_ASSERT_TRUE(cJSON_IsFalse(bobj));
744
745 sobj = cJSON_CreateString("test");
746 TEST_ASSERT_TRUE(cJSON_IsString(sobj));
747 cJSON_SetBoolValue(sobj, 1);
748 TEST_ASSERT_TRUE(cJSON_IsString(sobj));
749 cJSON_SetBoolValue(sobj, 0);
750 TEST_ASSERT_TRUE(cJSON_IsString(sobj));
751
752 oobj = cJSON_CreateObject();
753 TEST_ASSERT_TRUE(cJSON_IsObject(oobj));
754 cJSON_SetBoolValue(oobj, 1);
755 TEST_ASSERT_TRUE(cJSON_IsObject(oobj));
756 cJSON_SetBoolValue(oobj, 0);
757 TEST_ASSERT_TRUE(cJSON_IsObject(oobj));
758
759 refobj = cJSON_CreateStringReference("conststring");
760 TEST_ASSERT_TRUE(cJSON_IsString(refobj));
761 TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
762 cJSON_SetBoolValue(refobj, 1);
763 TEST_ASSERT_TRUE(cJSON_IsString(refobj));
764 TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
765 cJSON_SetBoolValue(refobj, 0);
766 TEST_ASSERT_TRUE(cJSON_IsString(refobj));
767 TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
768 cJSON_Delete(refobj);
769
770 refobj = cJSON_CreateObjectReference(oobj);
771 TEST_ASSERT_TRUE(cJSON_IsObject(refobj));
772 TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
773 cJSON_SetBoolValue(refobj, 1);
774 TEST_ASSERT_TRUE(cJSON_IsObject(refobj));
775 TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
776 cJSON_SetBoolValue(refobj, 0);
777 TEST_ASSERT_TRUE(cJSON_IsObject(refobj));
778 TEST_ASSERT_TRUE(refobj->type & cJSON_IsReference);
779 cJSON_Delete(refobj);
780
781 cJSON_Delete(oobj);
782 cJSON_Delete(bobj);
783 cJSON_Delete(sobj);
784}
785
786static void cjson_parse_big_numbers_should_not_report_error(void)
787{
788 cJSON *valid_big_number_json_object1 = cJSON_Parse("{\"a\": true, \"b\": [ null,9999999999999999999999999999999999999999999999912345678901234567]}");
789 cJSON *valid_big_number_json_object2 = cJSON_Parse("{\"a\": true, \"b\": [ null,999999999999999999999999999999999999999999999991234567890.1234567E3]}");
790 const char *invalid_big_number_json1 = "{\"a\": true, \"b\": [ null,99999999999999999999999999999999999999999999999.1234567890.1234567]}";
791 const char *invalid_big_number_json2 = "{\"a\": true, \"b\": [ null,99999999999999999999999999999999999999999999999E1234567890e1234567]}";
792
793 TEST_ASSERT_NOT_NULL(valid_big_number_json_object1);
794 TEST_ASSERT_NOT_NULL(valid_big_number_json_object2);
795 TEST_ASSERT_NULL_MESSAGE(cJSON_Parse(invalid_big_number_json1), "Invalid big number JSONs should not be parsed.");
796 TEST_ASSERT_NULL_MESSAGE(cJSON_Parse(invalid_big_number_json2), "Invalid big number JSONs should not be parsed.");
797
798 cJSON_Delete(valid_big_number_json_object1);
799 cJSON_Delete(valid_big_number_json_object2);
800}
801
802int CJSON_CDECL main(void)
803{
804 UNITY_BEGIN();
805
806 RUN_TEST(cjson_array_foreach_should_loop_over_arrays);
807 RUN_TEST(cjson_array_foreach_should_not_dereference_null_pointer);
808 RUN_TEST(cjson_get_object_item_should_get_object_items);
809 RUN_TEST(cjson_get_object_item_case_sensitive_should_get_object_items);
810 RUN_TEST(cjson_get_object_item_should_not_crash_with_array);
811 RUN_TEST(cjson_get_object_item_case_sensitive_should_not_crash_with_array);
812 RUN_TEST(typecheck_functions_should_check_type);
813 RUN_TEST(cjson_should_not_parse_to_deeply_nested_jsons);
814 RUN_TEST(cjson_should_not_follow_too_deep_circular_references);
815 RUN_TEST(cjson_set_number_value_should_set_numbers);
816 RUN_TEST(cjson_detach_item_via_pointer_should_detach_items);
817 RUN_TEST(cjson_detach_item_via_pointer_should_return_null_if_item_prev_is_null);
818 RUN_TEST(cjson_replace_item_via_pointer_should_replace_items);
819 RUN_TEST(cjson_replace_item_in_object_should_preserve_name);
820 RUN_TEST(cjson_functions_should_not_crash_with_null_pointers);
821 RUN_TEST(cjson_set_valuestring_should_return_null_if_strings_overlap);
822 RUN_TEST(ensure_should_fail_on_failed_realloc);
823 RUN_TEST(skip_utf8_bom_should_skip_bom);
824 RUN_TEST(skip_utf8_bom_should_not_skip_bom_if_not_at_beginning);
825 RUN_TEST(cjson_get_string_value_should_get_a_string);
826 RUN_TEST(cjson_get_number_value_should_get_a_number);
827 RUN_TEST(cjson_create_string_reference_should_create_a_string_reference);
828 RUN_TEST(cjson_create_object_reference_should_create_an_object_reference);
829 RUN_TEST(cjson_create_array_reference_should_create_an_array_reference);
830 RUN_TEST(cjson_add_item_to_object_or_array_should_not_add_itself);
831 RUN_TEST(cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased);
832 RUN_TEST(cjson_delete_item_from_array_should_not_broken_list_structure);
833 RUN_TEST(cjson_set_valuestring_to_object_should_not_leak_memory);
834 RUN_TEST(cjson_set_bool_value_must_not_break_objects);
835 RUN_TEST(cjson_parse_big_numbers_should_not_report_error);
836
837 return UNITY_END();
838}