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
281 #ifdef __CJSON_USE_INT64
cjson_add_int64_number_should_add_int64_number(void)282 static void cjson_add_int64_number_should_add_int64_number(void)
283 {
284 cJSON *root = cJSON_CreateObject();
285 cJSON *number = NULL;
286
287 cJSON_AddInt64NumberToObject(root, "number", 42LL);
288
289 TEST_ASSERT_NOT_NULL(number = cJSON_GetObjectItemCaseSensitive(root, "number"));
290
291 TEST_ASSERT_EQUAL_INT(number->type, cJSON_Number | cJSON_IsInt64);
292 TEST_ASSERT_EQUAL_DOUBLE(number->valuedouble, 42);
293 TEST_ASSERT_EQUAL_INT64(number->valueint, 42LL);
294
295 cJSON_Delete(root);
296 }
297
cjson_add_int64_number_should_fail_with_null_pointers(void)298 static void cjson_add_int64_number_should_fail_with_null_pointers(void)
299 {
300 cJSON *root = cJSON_CreateObject();
301
302 TEST_ASSERT_NULL(cJSON_AddInt64NumberToObject(NULL, "number", 42LL));
303 TEST_ASSERT_NULL(cJSON_AddInt64NumberToObject(root, NULL, 42LL));
304
305 cJSON_Delete(root);
306 }
307
cjson_add_int64_number_should_fail_on_allocation_failure(void)308 static void cjson_add_int64_number_should_fail_on_allocation_failure(void)
309 {
310 cJSON *root = cJSON_CreateObject();
311
312 cJSON_InitHooks(&failing_hooks);
313
314 TEST_ASSERT_NULL(cJSON_AddInt64NumberToObject(root, "number", 42LL));
315
316 cJSON_InitHooks(NULL);
317
318 cJSON_Delete(root);
319 }
320 #endif /* __CJSON_USE_INT64 */
321
cjson_add_string_should_add_string(void)322 static void cjson_add_string_should_add_string(void)
323 {
324 cJSON *root = cJSON_CreateObject();
325 cJSON *string = NULL;
326
327 cJSON_AddStringToObject(root, "string", "Hello World!");
328
329 TEST_ASSERT_NOT_NULL(string = cJSON_GetObjectItemCaseSensitive(root, "string"));
330 TEST_ASSERT_EQUAL_INT(string->type, cJSON_String);
331 TEST_ASSERT_EQUAL_STRING(string->valuestring, "Hello World!");
332
333 cJSON_Delete(root);
334 }
335
cjson_add_string_should_fail_with_null_pointers(void)336 static void cjson_add_string_should_fail_with_null_pointers(void)
337 {
338 cJSON *root = cJSON_CreateObject();
339
340 TEST_ASSERT_NULL(cJSON_AddStringToObject(NULL, "string", "string"));
341 TEST_ASSERT_NULL(cJSON_AddStringToObject(root, NULL, "string"));
342
343 cJSON_Delete(root);
344 }
345
cjson_add_string_should_fail_on_allocation_failure(void)346 static void cjson_add_string_should_fail_on_allocation_failure(void)
347 {
348 cJSON *root = cJSON_CreateObject();
349
350 cJSON_InitHooks(&failing_hooks);
351
352 TEST_ASSERT_NULL(cJSON_AddStringToObject(root, "string", "string"));
353
354 cJSON_InitHooks(NULL);
355
356 cJSON_Delete(root);
357 }
358
cjson_add_raw_should_add_raw(void)359 static void cjson_add_raw_should_add_raw(void)
360 {
361 cJSON *root = cJSON_CreateObject();
362 cJSON *raw = NULL;
363
364 cJSON_AddRawToObject(root, "raw", "{}");
365
366 TEST_ASSERT_NOT_NULL(raw = cJSON_GetObjectItemCaseSensitive(root, "raw"));
367 TEST_ASSERT_EQUAL_INT(raw->type, cJSON_Raw);
368 TEST_ASSERT_EQUAL_STRING(raw->valuestring, "{}");
369
370 cJSON_Delete(root);
371 }
372
cjson_add_raw_should_fail_with_null_pointers(void)373 static void cjson_add_raw_should_fail_with_null_pointers(void)
374 {
375 cJSON *root = cJSON_CreateObject();
376
377 TEST_ASSERT_NULL(cJSON_AddRawToObject(NULL, "raw", "{}"));
378 TEST_ASSERT_NULL(cJSON_AddRawToObject(root, NULL, "{}"));
379
380 cJSON_Delete(root);
381 }
382
cjson_add_raw_should_fail_on_allocation_failure(void)383 static void cjson_add_raw_should_fail_on_allocation_failure(void)
384 {
385 cJSON *root = cJSON_CreateObject();
386
387 cJSON_InitHooks(&failing_hooks);
388
389 TEST_ASSERT_NULL(cJSON_AddRawToObject(root, "raw", "{}"));
390
391 cJSON_InitHooks(NULL);
392
393 cJSON_Delete(root);
394 }
395
cJSON_add_object_should_add_object(void)396 static void cJSON_add_object_should_add_object(void)
397 {
398 cJSON *root = cJSON_CreateObject();
399 cJSON *object = NULL;
400
401 cJSON_AddObjectToObject(root, "object");
402 TEST_ASSERT_NOT_NULL(object = cJSON_GetObjectItemCaseSensitive(root, "object"));
403 TEST_ASSERT_EQUAL_INT(object->type, cJSON_Object);
404
405 cJSON_Delete(root);
406 }
407
cjson_add_object_should_fail_with_null_pointers(void)408 static void cjson_add_object_should_fail_with_null_pointers(void)
409 {
410 cJSON *root = cJSON_CreateObject();
411
412 TEST_ASSERT_NULL(cJSON_AddObjectToObject(NULL, "object"));
413 TEST_ASSERT_NULL(cJSON_AddObjectToObject(root, NULL));
414
415 cJSON_Delete(root);
416 }
417
cjson_add_object_should_fail_on_allocation_failure(void)418 static void cjson_add_object_should_fail_on_allocation_failure(void)
419 {
420 cJSON *root = cJSON_CreateObject();
421
422 cJSON_InitHooks(&failing_hooks);
423
424 TEST_ASSERT_NULL(cJSON_AddObjectToObject(root, "object"));
425
426 cJSON_InitHooks(NULL);
427
428 cJSON_Delete(root);
429 }
430
cJSON_add_array_should_add_array(void)431 static void cJSON_add_array_should_add_array(void)
432 {
433 cJSON *root = cJSON_CreateObject();
434 cJSON *array = NULL;
435
436 cJSON_AddArrayToObject(root, "array");
437 TEST_ASSERT_NOT_NULL(array = cJSON_GetObjectItemCaseSensitive(root, "array"));
438 TEST_ASSERT_EQUAL_INT(array->type, cJSON_Array);
439
440 cJSON_Delete(root);
441 }
442
cjson_add_array_should_fail_with_null_pointers(void)443 static void cjson_add_array_should_fail_with_null_pointers(void)
444 {
445 cJSON *root = cJSON_CreateObject();
446
447 TEST_ASSERT_NULL(cJSON_AddArrayToObject(NULL, "array"));
448 TEST_ASSERT_NULL(cJSON_AddArrayToObject(root, NULL));
449
450 cJSON_Delete(root);
451 }
452
cjson_add_array_should_fail_on_allocation_failure(void)453 static void cjson_add_array_should_fail_on_allocation_failure(void)
454 {
455 cJSON *root = cJSON_CreateObject();
456
457 cJSON_InitHooks(&failing_hooks);
458
459 TEST_ASSERT_NULL(cJSON_AddArrayToObject(root, "array"));
460
461 cJSON_InitHooks(NULL);
462
463 cJSON_Delete(root);
464 }
465
main(void)466 int CJSON_CDECL main(void)
467 {
468 UNITY_BEGIN();
469
470 RUN_TEST(cjson_add_null_should_add_null);
471 RUN_TEST(cjson_add_null_should_fail_with_null_pointers);
472 RUN_TEST(cjson_add_null_should_fail_on_allocation_failure);
473
474 RUN_TEST(cjson_add_true_should_add_true);
475 RUN_TEST(cjson_add_true_should_fail_with_null_pointers);
476 RUN_TEST(cjson_add_true_should_fail_on_allocation_failure);
477
478 RUN_TEST(cjson_create_int_array_should_fail_on_allocation_failure);
479 RUN_TEST(cjson_create_float_array_should_fail_on_allocation_failure);
480 RUN_TEST(cjson_create_double_array_should_fail_on_allocation_failure);
481 RUN_TEST(cjson_create_string_array_should_fail_on_allocation_failure);
482
483 RUN_TEST(cjson_add_false_should_add_false);
484 RUN_TEST(cjson_add_false_should_fail_with_null_pointers);
485 RUN_TEST(cjson_add_false_should_fail_on_allocation_failure);
486
487 RUN_TEST(cjson_add_bool_should_add_bool);
488 RUN_TEST(cjson_add_bool_should_fail_with_null_pointers);
489 RUN_TEST(cjson_add_bool_should_fail_on_allocation_failure);
490
491 RUN_TEST(cjson_add_number_should_add_number);
492 RUN_TEST(cjson_add_number_should_fail_with_null_pointers);
493 RUN_TEST(cjson_add_number_should_fail_on_allocation_failure);
494
495 #ifdef __CJSON_USE_INT64
496 RUN_TEST(cjson_add_int64_number_should_add_int64_number);
497 RUN_TEST(cjson_add_int64_number_should_fail_with_null_pointers);
498 RUN_TEST(cjson_add_int64_number_should_fail_on_allocation_failure);
499 #endif /* __CJSON_USE_INT64 */
500
501 RUN_TEST(cjson_add_string_should_add_string);
502 RUN_TEST(cjson_add_string_should_fail_with_null_pointers);
503 RUN_TEST(cjson_add_string_should_fail_on_allocation_failure);
504
505 RUN_TEST(cjson_add_raw_should_add_raw);
506 RUN_TEST(cjson_add_raw_should_fail_with_null_pointers);
507 RUN_TEST(cjson_add_raw_should_fail_on_allocation_failure);
508
509 RUN_TEST(cJSON_add_object_should_add_object);
510 RUN_TEST(cjson_add_object_should_fail_with_null_pointers);
511 RUN_TEST(cjson_add_object_should_fail_on_allocation_failure);
512
513 RUN_TEST(cJSON_add_array_should_add_array);
514 RUN_TEST(cjson_add_array_should_fail_with_null_pointers);
515 RUN_TEST(cjson_add_array_should_fail_on_allocation_failure);
516
517 return UNITY_END();
518 }
519