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_add_false_should_add_false(void)120 static void cjson_add_false_should_add_false(void)
121 {
122 cJSON *root = cJSON_CreateObject();
123 cJSON *false_item = NULL;
124
125 cJSON_AddFalseToObject(root, "false");
126
127 TEST_ASSERT_NOT_NULL(false_item = cJSON_GetObjectItemCaseSensitive(root, "false"));
128 TEST_ASSERT_EQUAL_INT(false_item->type, cJSON_False);
129
130 cJSON_Delete(root);
131 }
132
cjson_add_false_should_fail_with_null_pointers(void)133 static void cjson_add_false_should_fail_with_null_pointers(void)
134 {
135 cJSON *root = cJSON_CreateObject();
136
137 TEST_ASSERT_NULL(cJSON_AddFalseToObject(NULL, "false"));
138 TEST_ASSERT_NULL(cJSON_AddFalseToObject(root, NULL));
139
140 cJSON_Delete(root);
141 }
142
cjson_add_false_should_fail_on_allocation_failure(void)143 static void cjson_add_false_should_fail_on_allocation_failure(void)
144 {
145 cJSON *root = cJSON_CreateObject();
146
147 cJSON_InitHooks(&failing_hooks);
148
149 TEST_ASSERT_NULL(cJSON_AddFalseToObject(root, "false"));
150
151 cJSON_InitHooks(NULL);
152
153 cJSON_Delete(root);
154 }
155
cjson_add_bool_should_add_bool(void)156 static void cjson_add_bool_should_add_bool(void)
157 {
158 cJSON *root = cJSON_CreateObject();
159 cJSON *true_item = NULL;
160 cJSON *false_item = NULL;
161
162 /* true */
163 cJSON_AddBoolToObject(root, "true", true);
164 TEST_ASSERT_NOT_NULL(true_item = cJSON_GetObjectItemCaseSensitive(root, "true"));
165 TEST_ASSERT_EQUAL_INT(true_item->type, cJSON_True);
166
167 /* false */
168 cJSON_AddBoolToObject(root, "false", false);
169 TEST_ASSERT_NOT_NULL(false_item = cJSON_GetObjectItemCaseSensitive(root, "false"));
170 TEST_ASSERT_EQUAL_INT(false_item->type, cJSON_False);
171
172 cJSON_Delete(root);
173 }
174
cjson_add_bool_should_fail_with_null_pointers(void)175 static void cjson_add_bool_should_fail_with_null_pointers(void)
176 {
177 cJSON *root = cJSON_CreateObject();
178
179 TEST_ASSERT_NULL(cJSON_AddBoolToObject(NULL, "false", false));
180 TEST_ASSERT_NULL(cJSON_AddBoolToObject(root, NULL, false));
181
182 cJSON_Delete(root);
183 }
184
cjson_add_bool_should_fail_on_allocation_failure(void)185 static void cjson_add_bool_should_fail_on_allocation_failure(void)
186 {
187 cJSON *root = cJSON_CreateObject();
188
189 cJSON_InitHooks(&failing_hooks);
190
191 TEST_ASSERT_NULL(cJSON_AddBoolToObject(root, "false", false));
192
193 cJSON_InitHooks(NULL);
194
195 cJSON_Delete(root);
196 }
197
cjson_add_number_should_add_number(void)198 static void cjson_add_number_should_add_number(void)
199 {
200 cJSON *root = cJSON_CreateObject();
201 cJSON *number = NULL;
202
203 cJSON_AddNumberToObject(root, "number", 42);
204
205 TEST_ASSERT_NOT_NULL(number = cJSON_GetObjectItemCaseSensitive(root, "number"));
206
207 TEST_ASSERT_EQUAL_INT(number->type, cJSON_Number);
208 TEST_ASSERT_EQUAL_DOUBLE(number->valuedouble, 42);
209 TEST_ASSERT_EQUAL_INT(number->valueint, 42);
210
211 cJSON_Delete(root);
212 }
213
cjson_add_number_should_fail_with_null_pointers(void)214 static void cjson_add_number_should_fail_with_null_pointers(void)
215 {
216 cJSON *root = cJSON_CreateObject();
217
218 TEST_ASSERT_NULL(cJSON_AddNumberToObject(NULL, "number", 42));
219 TEST_ASSERT_NULL(cJSON_AddNumberToObject(root, NULL, 42));
220
221 cJSON_Delete(root);
222 }
223
cjson_add_number_should_fail_on_allocation_failure(void)224 static void cjson_add_number_should_fail_on_allocation_failure(void)
225 {
226 cJSON *root = cJSON_CreateObject();
227
228 cJSON_InitHooks(&failing_hooks);
229
230 TEST_ASSERT_NULL(cJSON_AddNumberToObject(root, "number", 42));
231
232 cJSON_InitHooks(NULL);
233
234 cJSON_Delete(root);
235 }
236
cjson_add_string_should_add_string(void)237 static void cjson_add_string_should_add_string(void)
238 {
239 cJSON *root = cJSON_CreateObject();
240 cJSON *string = NULL;
241
242 cJSON_AddStringToObject(root, "string", "Hello World!");
243
244 TEST_ASSERT_NOT_NULL(string = cJSON_GetObjectItemCaseSensitive(root, "string"));
245 TEST_ASSERT_EQUAL_INT(string->type, cJSON_String);
246 TEST_ASSERT_EQUAL_STRING(string->valuestring, "Hello World!");
247
248 cJSON_Delete(root);
249 }
250
cjson_add_string_should_fail_with_null_pointers(void)251 static void cjson_add_string_should_fail_with_null_pointers(void)
252 {
253 cJSON *root = cJSON_CreateObject();
254
255 TEST_ASSERT_NULL(cJSON_AddStringToObject(NULL, "string", "string"));
256 TEST_ASSERT_NULL(cJSON_AddStringToObject(root, NULL, "string"));
257
258 cJSON_Delete(root);
259 }
260
cjson_add_string_should_fail_on_allocation_failure(void)261 static void cjson_add_string_should_fail_on_allocation_failure(void)
262 {
263 cJSON *root = cJSON_CreateObject();
264
265 cJSON_InitHooks(&failing_hooks);
266
267 TEST_ASSERT_NULL(cJSON_AddStringToObject(root, "string", "string"));
268
269 cJSON_InitHooks(NULL);
270
271 cJSON_Delete(root);
272 }
273
cjson_add_raw_should_add_raw(void)274 static void cjson_add_raw_should_add_raw(void)
275 {
276 cJSON *root = cJSON_CreateObject();
277 cJSON *raw = NULL;
278
279 cJSON_AddRawToObject(root, "raw", "{}");
280
281 TEST_ASSERT_NOT_NULL(raw = cJSON_GetObjectItemCaseSensitive(root, "raw"));
282 TEST_ASSERT_EQUAL_INT(raw->type, cJSON_Raw);
283 TEST_ASSERT_EQUAL_STRING(raw->valuestring, "{}");
284
285 cJSON_Delete(root);
286 }
287
cjson_add_raw_should_fail_with_null_pointers(void)288 static void cjson_add_raw_should_fail_with_null_pointers(void)
289 {
290 cJSON *root = cJSON_CreateObject();
291
292 TEST_ASSERT_NULL(cJSON_AddRawToObject(NULL, "raw", "{}"));
293 TEST_ASSERT_NULL(cJSON_AddRawToObject(root, NULL, "{}"));
294
295 cJSON_Delete(root);
296 }
297
cjson_add_raw_should_fail_on_allocation_failure(void)298 static void cjson_add_raw_should_fail_on_allocation_failure(void)
299 {
300 cJSON *root = cJSON_CreateObject();
301
302 cJSON_InitHooks(&failing_hooks);
303
304 TEST_ASSERT_NULL(cJSON_AddRawToObject(root, "raw", "{}"));
305
306 cJSON_InitHooks(NULL);
307
308 cJSON_Delete(root);
309 }
310
cJSON_add_object_should_add_object(void)311 static void cJSON_add_object_should_add_object(void)
312 {
313 cJSON *root = cJSON_CreateObject();
314 cJSON *object = NULL;
315
316 cJSON_AddObjectToObject(root, "object");
317 TEST_ASSERT_NOT_NULL(object = cJSON_GetObjectItemCaseSensitive(root, "object"));
318 TEST_ASSERT_EQUAL_INT(object->type, cJSON_Object);
319
320 cJSON_Delete(root);
321 }
322
cjson_add_object_should_fail_with_null_pointers(void)323 static void cjson_add_object_should_fail_with_null_pointers(void)
324 {
325 cJSON *root = cJSON_CreateObject();
326
327 TEST_ASSERT_NULL(cJSON_AddObjectToObject(NULL, "object"));
328 TEST_ASSERT_NULL(cJSON_AddObjectToObject(root, NULL));
329
330 cJSON_Delete(root);
331 }
332
cjson_add_object_should_fail_on_allocation_failure(void)333 static void cjson_add_object_should_fail_on_allocation_failure(void)
334 {
335 cJSON *root = cJSON_CreateObject();
336
337 cJSON_InitHooks(&failing_hooks);
338
339 TEST_ASSERT_NULL(cJSON_AddObjectToObject(root, "object"));
340
341 cJSON_InitHooks(NULL);
342
343 cJSON_Delete(root);
344 }
345
cJSON_add_array_should_add_array(void)346 static void cJSON_add_array_should_add_array(void)
347 {
348 cJSON *root = cJSON_CreateObject();
349 cJSON *array = NULL;
350
351 cJSON_AddArrayToObject(root, "array");
352 TEST_ASSERT_NOT_NULL(array = cJSON_GetObjectItemCaseSensitive(root, "array"));
353 TEST_ASSERT_EQUAL_INT(array->type, cJSON_Array);
354
355 cJSON_Delete(root);
356 }
357
cjson_add_array_should_fail_with_null_pointers(void)358 static void cjson_add_array_should_fail_with_null_pointers(void)
359 {
360 cJSON *root = cJSON_CreateObject();
361
362 TEST_ASSERT_NULL(cJSON_AddArrayToObject(NULL, "array"));
363 TEST_ASSERT_NULL(cJSON_AddArrayToObject(root, NULL));
364
365 cJSON_Delete(root);
366 }
367
cjson_add_array_should_fail_on_allocation_failure(void)368 static void cjson_add_array_should_fail_on_allocation_failure(void)
369 {
370 cJSON *root = cJSON_CreateObject();
371
372 cJSON_InitHooks(&failing_hooks);
373
374 TEST_ASSERT_NULL(cJSON_AddArrayToObject(root, "array"));
375
376 cJSON_InitHooks(NULL);
377
378 cJSON_Delete(root);
379 }
380
main(void)381 int CJSON_CDECL main(void)
382 {
383 UNITY_BEGIN();
384
385 RUN_TEST(cjson_add_null_should_add_null);
386 RUN_TEST(cjson_add_null_should_fail_with_null_pointers);
387 RUN_TEST(cjson_add_null_should_fail_on_allocation_failure);
388
389 RUN_TEST(cjson_add_true_should_add_true);
390 RUN_TEST(cjson_add_true_should_fail_with_null_pointers);
391 RUN_TEST(cjson_add_true_should_fail_on_allocation_failure);
392
393 RUN_TEST(cjson_add_false_should_add_false);
394 RUN_TEST(cjson_add_false_should_fail_with_null_pointers);
395 RUN_TEST(cjson_add_false_should_fail_on_allocation_failure);
396
397 RUN_TEST(cjson_add_bool_should_add_bool);
398 RUN_TEST(cjson_add_bool_should_fail_with_null_pointers);
399 RUN_TEST(cjson_add_bool_should_fail_on_allocation_failure);
400
401 RUN_TEST(cjson_add_number_should_add_number);
402 RUN_TEST(cjson_add_number_should_fail_with_null_pointers);
403 RUN_TEST(cjson_add_number_should_fail_on_allocation_failure);
404
405 RUN_TEST(cjson_add_string_should_add_string);
406 RUN_TEST(cjson_add_string_should_fail_with_null_pointers);
407 RUN_TEST(cjson_add_string_should_fail_on_allocation_failure);
408
409 RUN_TEST(cjson_add_raw_should_add_raw);
410 RUN_TEST(cjson_add_raw_should_fail_with_null_pointers);
411 RUN_TEST(cjson_add_raw_should_fail_on_allocation_failure);
412
413 RUN_TEST(cJSON_add_object_should_add_object);
414 RUN_TEST(cjson_add_object_should_fail_with_null_pointers);
415 RUN_TEST(cjson_add_object_should_fail_on_allocation_failure);
416
417 RUN_TEST(cJSON_add_array_should_add_array);
418 RUN_TEST(cjson_add_array_should_fail_with_null_pointers);
419 RUN_TEST(cjson_add_array_should_fail_on_allocation_failure);
420
421 return UNITY_END();
422 }
423