misc_tests.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "unity/examples/unity_config.h"
  23. #include "unity/src/unity.h"
  24. #include "common.h"
  25. static void cjson_array_foreach_should_loop_over_arrays(void)
  26. {
  27. cJSON array[1];
  28. cJSON elements[10];
  29. cJSON *element_pointer = NULL;
  30. size_t i = 0;
  31. memset(array, 0, sizeof(array));
  32. memset(elements, 0, sizeof(elements));
  33. /* create array */
  34. array[0].child = &elements[0];
  35. elements[0].prev = NULL;
  36. elements[9].next = NULL;
  37. for (i = 0; i < 9; i++)
  38. {
  39. elements[i].next = &elements[i + 1];
  40. elements[i + 1].prev = &elements[i];
  41. }
  42. i = 0;
  43. cJSON_ArrayForEach(element_pointer, array)
  44. {
  45. TEST_ASSERT_TRUE_MESSAGE(element_pointer == &elements[i], "Not iterating over array properly");
  46. i++;
  47. }
  48. }
  49. static void cjson_array_foreach_should_not_dereference_null_pointer(void)
  50. {
  51. cJSON *array = NULL;
  52. cJSON *element = NULL;
  53. cJSON_ArrayForEach(element, array);
  54. }
  55. static void cjson_get_object_item_should_get_object_items(void)
  56. {
  57. cJSON *item = NULL;
  58. cJSON *found = NULL;
  59. item = cJSON_Parse("{\"one\":1, \"Two\":2, \"tHree\":3}");
  60. found = cJSON_GetObjectItem(NULL, "test");
  61. TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL pointer.");
  62. found = cJSON_GetObjectItem(item, NULL);
  63. TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
  64. found = cJSON_GetObjectItem(item, "one");
  65. TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
  66. TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
  67. found = cJSON_GetObjectItem(item, "tWo");
  68. TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
  69. TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 2);
  70. found = cJSON_GetObjectItem(item, "three");
  71. TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find item.");
  72. TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 3);
  73. found = cJSON_GetObjectItem(item, "four");
  74. TEST_ASSERT_NULL_MESSAGE(found, "Should not find something that isn't there.");
  75. cJSON_Delete(item);
  76. }
  77. static void cjson_get_object_item_case_sensitive_should_get_object_items(void)
  78. {
  79. cJSON *item = NULL;
  80. cJSON *found = NULL;
  81. item = cJSON_Parse("{\"one\":1, \"Two\":2, \"tHree\":3}");
  82. found = cJSON_GetObjectItemCaseSensitive(NULL, "test");
  83. TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL pointer.");
  84. found = cJSON_GetObjectItemCaseSensitive(item, NULL);
  85. TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
  86. found = cJSON_GetObjectItemCaseSensitive(item, "one");
  87. TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
  88. TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
  89. found = cJSON_GetObjectItemCaseSensitive(item, "Two");
  90. TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
  91. TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 2);
  92. found = cJSON_GetObjectItemCaseSensitive(item, "tHree");
  93. TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find item.");
  94. TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 3);
  95. found = cJSON_GetObjectItemCaseSensitive(item, "One");
  96. TEST_ASSERT_NULL_MESSAGE(found, "Should not find something that isn't there.");
  97. cJSON_Delete(item);
  98. }
  99. static void cjson_get_object_item_should_not_crash_with_array(void) {
  100. cJSON *array = NULL;
  101. cJSON *found = NULL;
  102. array = cJSON_Parse("[1]");
  103. found = cJSON_GetObjectItem(array, "name");
  104. TEST_ASSERT_NULL(found);
  105. cJSON_Delete(array);
  106. }
  107. static void cjson_get_object_item_case_sensitive_should_not_crash_with_array(void) {
  108. cJSON *array = NULL;
  109. cJSON *found = NULL;
  110. array = cJSON_Parse("[1]");
  111. found = cJSON_GetObjectItemCaseSensitive(array, "name");
  112. TEST_ASSERT_NULL(found);
  113. cJSON_Delete(array);
  114. }
  115. static void typecheck_functions_should_check_type(void)
  116. {
  117. cJSON invalid[1];
  118. cJSON item[1];
  119. invalid->type = cJSON_Invalid;
  120. invalid->type |= cJSON_StringIsConst;
  121. item->type = cJSON_False;
  122. item->type |= cJSON_StringIsConst;
  123. TEST_ASSERT_FALSE(cJSON_IsInvalid(NULL));
  124. TEST_ASSERT_FALSE(cJSON_IsInvalid(item));
  125. TEST_ASSERT_TRUE(cJSON_IsInvalid(invalid));
  126. item->type = cJSON_False | cJSON_StringIsConst;
  127. TEST_ASSERT_FALSE(cJSON_IsFalse(NULL));
  128. TEST_ASSERT_FALSE(cJSON_IsFalse(invalid));
  129. TEST_ASSERT_TRUE(cJSON_IsFalse(item));
  130. TEST_ASSERT_TRUE(cJSON_IsBool(item));
  131. item->type = cJSON_True | cJSON_StringIsConst;
  132. TEST_ASSERT_FALSE(cJSON_IsTrue(NULL));
  133. TEST_ASSERT_FALSE(cJSON_IsTrue(invalid));
  134. TEST_ASSERT_TRUE(cJSON_IsTrue(item));
  135. TEST_ASSERT_TRUE(cJSON_IsBool(item));
  136. item->type = cJSON_NULL | cJSON_StringIsConst;
  137. TEST_ASSERT_FALSE(cJSON_IsNull(NULL));
  138. TEST_ASSERT_FALSE(cJSON_IsNull(invalid));
  139. TEST_ASSERT_TRUE(cJSON_IsNull(item));
  140. item->type = cJSON_Number | cJSON_StringIsConst;
  141. TEST_ASSERT_FALSE(cJSON_IsNumber(NULL));
  142. TEST_ASSERT_FALSE(cJSON_IsNumber(invalid));
  143. TEST_ASSERT_TRUE(cJSON_IsNumber(item));
  144. item->type = cJSON_String | cJSON_StringIsConst;
  145. TEST_ASSERT_FALSE(cJSON_IsString(NULL));
  146. TEST_ASSERT_FALSE(cJSON_IsString(invalid));
  147. TEST_ASSERT_TRUE(cJSON_IsString(item));
  148. item->type = cJSON_Array | cJSON_StringIsConst;
  149. TEST_ASSERT_FALSE(cJSON_IsArray(NULL));
  150. TEST_ASSERT_FALSE(cJSON_IsArray(invalid));
  151. TEST_ASSERT_TRUE(cJSON_IsArray(item));
  152. item->type = cJSON_Object | cJSON_StringIsConst;
  153. TEST_ASSERT_FALSE(cJSON_IsObject(NULL));
  154. TEST_ASSERT_FALSE(cJSON_IsObject(invalid));
  155. TEST_ASSERT_TRUE(cJSON_IsObject(item));
  156. item->type = cJSON_Raw | cJSON_StringIsConst;
  157. TEST_ASSERT_FALSE(cJSON_IsRaw(NULL));
  158. TEST_ASSERT_FALSE(cJSON_IsRaw(invalid));
  159. TEST_ASSERT_TRUE(cJSON_IsRaw(item));
  160. }
  161. static void cjson_should_not_parse_to_deeply_nested_jsons(void)
  162. {
  163. char deep_json[CJSON_NESTING_LIMIT + 1];
  164. size_t position = 0;
  165. for (position = 0; position < sizeof(deep_json); position++)
  166. {
  167. deep_json[position] = '[';
  168. }
  169. deep_json[sizeof(deep_json) - 1] = '\0';
  170. TEST_ASSERT_NULL_MESSAGE(cJSON_Parse(deep_json), "To deep JSONs should not be parsed.");
  171. }
  172. static void cjson_set_number_value_should_set_numbers(void)
  173. {
  174. cJSON number[1] = {{NULL, NULL, NULL, cJSON_Number, NULL, 0, 0, NULL}};
  175. cJSON_SetNumberValue(number, 1.5);
  176. TEST_ASSERT_EQUAL(1, number->valueint);
  177. TEST_ASSERT_EQUAL_DOUBLE(1.5, number->valuedouble);
  178. cJSON_SetNumberValue(number, -1.5);
  179. TEST_ASSERT_EQUAL(-1, number->valueint);
  180. TEST_ASSERT_EQUAL_DOUBLE(-1.5, number->valuedouble);
  181. cJSON_SetNumberValue(number, 1 + (double)INT_MAX);
  182. TEST_ASSERT_EQUAL(INT_MAX, number->valueint);
  183. TEST_ASSERT_EQUAL_DOUBLE(1 + (double)INT_MAX, number->valuedouble);
  184. cJSON_SetNumberValue(number, -1 + (double)INT_MIN);
  185. TEST_ASSERT_EQUAL(INT_MIN, number->valueint);
  186. TEST_ASSERT_EQUAL_DOUBLE(-1 + (double)INT_MIN, number->valuedouble);
  187. }
  188. static void cjson_detach_item_via_pointer_should_detach_items(void)
  189. {
  190. cJSON list[4];
  191. cJSON parent[1];
  192. memset(list, '\0', sizeof(list));
  193. /* link the list */
  194. list[0].next = &(list[1]);
  195. list[1].next = &(list[2]);
  196. list[2].next = &(list[3]);
  197. list[3].prev = &(list[2]);
  198. list[2].prev = &(list[1]);
  199. list[1].prev = &(list[0]);
  200. list[0].prev = &(list[3]);
  201. parent->child = &list[0];
  202. /* detach in the middle (list[1]) */
  203. TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[1])) == &(list[1]), "Failed to detach in the middle.");
  204. TEST_ASSERT_TRUE_MESSAGE((list[1].prev == NULL) && (list[1].next == NULL), "Didn't set pointers of detached item to NULL.");
  205. TEST_ASSERT_TRUE((list[0].next == &(list[2])) && (list[2].prev == &(list[0])));
  206. /* detach beginning (list[0]) */
  207. TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[0])) == &(list[0]), "Failed to detach beginning.");
  208. TEST_ASSERT_TRUE_MESSAGE((list[0].prev == NULL) && (list[0].next == NULL), "Didn't set pointers of detached item to NULL.");
  209. TEST_ASSERT_TRUE_MESSAGE((list[2].prev == &(list[3])) && (parent->child == &(list[2])), "Didn't set the new beginning.");
  210. /* detach end (list[3])*/
  211. TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[3])) == &(list[3]), "Failed to detach end.");
  212. TEST_ASSERT_TRUE_MESSAGE((list[3].prev == NULL) && (list[3].next == NULL), "Didn't set pointers of detached item to NULL.");
  213. TEST_ASSERT_TRUE_MESSAGE((list[2].next == NULL) && (parent->child == &(list[2])), "Didn't set the new end");
  214. /* detach single item (list[2]) */
  215. TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &list[2]) == &list[2], "Failed to detach single item.");
  216. TEST_ASSERT_TRUE_MESSAGE((list[2].prev == NULL) && (list[2].next == NULL), "Didn't set pointers of detached item to NULL.");
  217. TEST_ASSERT_NULL_MESSAGE(parent->child, "Child of the parent wasn't set to NULL.");
  218. }
  219. static void cjson_replace_item_via_pointer_should_replace_items(void)
  220. {
  221. cJSON replacements[3];
  222. cJSON *beginning = NULL;
  223. cJSON *middle = NULL;
  224. cJSON *end = NULL;
  225. cJSON *array = NULL;
  226. beginning = cJSON_CreateNull();
  227. TEST_ASSERT_NOT_NULL(beginning);
  228. middle = cJSON_CreateNull();
  229. TEST_ASSERT_NOT_NULL(middle);
  230. end = cJSON_CreateNull();
  231. TEST_ASSERT_NOT_NULL(end);
  232. array = cJSON_CreateArray();
  233. TEST_ASSERT_NOT_NULL(array);
  234. cJSON_AddItemToArray(array, beginning);
  235. cJSON_AddItemToArray(array, middle);
  236. cJSON_AddItemToArray(array, end);
  237. memset(replacements, '\0', sizeof(replacements));
  238. /* replace beginning */
  239. TEST_ASSERT_TRUE(cJSON_ReplaceItemViaPointer(array, beginning, &(replacements[0])));
  240. TEST_ASSERT_TRUE(replacements[0].prev == end);
  241. TEST_ASSERT_TRUE(replacements[0].next == middle);
  242. TEST_ASSERT_TRUE(middle->prev == &(replacements[0]));
  243. TEST_ASSERT_TRUE(array->child == &(replacements[0]));
  244. /* replace middle */
  245. TEST_ASSERT_TRUE(cJSON_ReplaceItemViaPointer(array, middle, &(replacements[1])));
  246. TEST_ASSERT_TRUE(replacements[1].prev == &(replacements[0]));
  247. TEST_ASSERT_TRUE(replacements[1].next == end);
  248. TEST_ASSERT_TRUE(end->prev == &(replacements[1]));
  249. /* replace end */
  250. TEST_ASSERT_TRUE(cJSON_ReplaceItemViaPointer(array, end, &(replacements[2])));
  251. TEST_ASSERT_TRUE(replacements[2].prev == &(replacements[1]));
  252. TEST_ASSERT_NULL(replacements[2].next);
  253. TEST_ASSERT_TRUE(replacements[1].next == &(replacements[2]));
  254. cJSON_free(array);
  255. }
  256. static void cjson_replace_item_in_object_should_preserve_name(void)
  257. {
  258. cJSON root[1] = {{ NULL, NULL, NULL, 0, NULL, 0, 0, NULL }};
  259. cJSON *child = NULL;
  260. cJSON *replacement = NULL;
  261. cJSON_bool flag = false;
  262. child = cJSON_CreateNumber(1);
  263. TEST_ASSERT_NOT_NULL(child);
  264. replacement = cJSON_CreateNumber(2);
  265. TEST_ASSERT_NOT_NULL(replacement);
  266. flag = cJSON_AddItemToObject(root, "child", child);
  267. TEST_ASSERT_TRUE_MESSAGE(flag, "add item to object failed");
  268. cJSON_ReplaceItemInObject(root, "child", replacement);
  269. TEST_ASSERT_TRUE(root->child == replacement);
  270. TEST_ASSERT_EQUAL_STRING("child", replacement->string);
  271. cJSON_Delete(replacement);
  272. }
  273. static void cjson_functions_should_not_crash_with_null_pointers(void)
  274. {
  275. char buffer[10];
  276. cJSON *item = cJSON_CreateString("item");
  277. cJSON_InitHooks(NULL);
  278. TEST_ASSERT_NULL(cJSON_Parse(NULL));
  279. TEST_ASSERT_NULL(cJSON_ParseWithOpts(NULL, NULL, true));
  280. TEST_ASSERT_NULL(cJSON_Print(NULL));
  281. TEST_ASSERT_NULL(cJSON_PrintUnformatted(NULL));
  282. TEST_ASSERT_NULL(cJSON_PrintBuffered(NULL, 10, true));
  283. TEST_ASSERT_FALSE(cJSON_PrintPreallocated(NULL, buffer, sizeof(buffer), true));
  284. TEST_ASSERT_FALSE(cJSON_PrintPreallocated(item, NULL, 1, true));
  285. cJSON_Delete(NULL);
  286. cJSON_GetArraySize(NULL);
  287. TEST_ASSERT_NULL(cJSON_GetArrayItem(NULL, 0));
  288. TEST_ASSERT_NULL(cJSON_GetObjectItem(NULL, "item"));
  289. TEST_ASSERT_NULL(cJSON_GetObjectItem(item, NULL));
  290. TEST_ASSERT_NULL(cJSON_GetObjectItemCaseSensitive(NULL, "item"));
  291. TEST_ASSERT_NULL(cJSON_GetObjectItemCaseSensitive(item, NULL));
  292. TEST_ASSERT_FALSE(cJSON_HasObjectItem(NULL, "item"));
  293. TEST_ASSERT_FALSE(cJSON_HasObjectItem(item, NULL));
  294. TEST_ASSERT_FALSE(cJSON_IsInvalid(NULL));
  295. TEST_ASSERT_FALSE(cJSON_IsFalse(NULL));
  296. TEST_ASSERT_FALSE(cJSON_IsTrue(NULL));
  297. TEST_ASSERT_FALSE(cJSON_IsBool(NULL));
  298. TEST_ASSERT_FALSE(cJSON_IsNull(NULL));
  299. TEST_ASSERT_FALSE(cJSON_IsNumber(NULL));
  300. TEST_ASSERT_FALSE(cJSON_IsString(NULL));
  301. TEST_ASSERT_FALSE(cJSON_IsArray(NULL));
  302. TEST_ASSERT_FALSE(cJSON_IsObject(NULL));
  303. TEST_ASSERT_FALSE(cJSON_IsRaw(NULL));
  304. TEST_ASSERT_NULL(cJSON_CreateString(NULL));
  305. TEST_ASSERT_NULL(cJSON_CreateRaw(NULL));
  306. TEST_ASSERT_NULL(cJSON_CreateIntArray(NULL, 10));
  307. TEST_ASSERT_NULL(cJSON_CreateFloatArray(NULL, 10));
  308. TEST_ASSERT_NULL(cJSON_CreateDoubleArray(NULL, 10));
  309. TEST_ASSERT_NULL(cJSON_CreateStringArray(NULL, 10));
  310. cJSON_AddItemToArray(NULL, item);
  311. cJSON_AddItemToArray(item, NULL);
  312. cJSON_AddItemToObject(item, "item", NULL);
  313. cJSON_AddItemToObject(item, NULL, item);
  314. cJSON_AddItemToObject(NULL, "item", item);
  315. cJSON_AddItemToObjectCS(item, "item", NULL);
  316. cJSON_AddItemToObjectCS(item, NULL, item);
  317. cJSON_AddItemToObjectCS(NULL, "item", item);
  318. cJSON_AddItemReferenceToArray(NULL, item);
  319. cJSON_AddItemReferenceToArray(item, NULL);
  320. cJSON_AddItemReferenceToObject(item, "item", NULL);
  321. cJSON_AddItemReferenceToObject(item, NULL, item);
  322. cJSON_AddItemReferenceToObject(NULL, "item", item);
  323. TEST_ASSERT_NULL(cJSON_DetachItemViaPointer(NULL, item));
  324. TEST_ASSERT_NULL(cJSON_DetachItemViaPointer(item, NULL));
  325. TEST_ASSERT_NULL(cJSON_DetachItemFromArray(NULL, 0));
  326. cJSON_DeleteItemFromArray(NULL, 0);
  327. TEST_ASSERT_NULL(cJSON_DetachItemFromObject(NULL, "item"));
  328. TEST_ASSERT_NULL(cJSON_DetachItemFromObject(item, NULL));
  329. TEST_ASSERT_NULL(cJSON_DetachItemFromObjectCaseSensitive(NULL, "item"));
  330. TEST_ASSERT_NULL(cJSON_DetachItemFromObjectCaseSensitive(item, NULL));
  331. cJSON_DeleteItemFromObject(NULL, "item");
  332. cJSON_DeleteItemFromObject(item, NULL);
  333. cJSON_DeleteItemFromObjectCaseSensitive(NULL, "item");
  334. cJSON_DeleteItemFromObjectCaseSensitive(item, NULL);
  335. TEST_ASSERT_FALSE(cJSON_InsertItemInArray(NULL, 0, item));
  336. TEST_ASSERT_FALSE(cJSON_InsertItemInArray(item, 0, NULL));
  337. TEST_ASSERT_FALSE(cJSON_ReplaceItemViaPointer(NULL, item, item));
  338. TEST_ASSERT_FALSE(cJSON_ReplaceItemViaPointer(item, NULL, item));
  339. TEST_ASSERT_FALSE(cJSON_ReplaceItemViaPointer(item, item, NULL));
  340. TEST_ASSERT_FALSE(cJSON_ReplaceItemInArray(item, 0, NULL));
  341. TEST_ASSERT_FALSE(cJSON_ReplaceItemInArray(NULL, 0, item));
  342. TEST_ASSERT_FALSE(cJSON_ReplaceItemInObject(NULL, "item", item));
  343. TEST_ASSERT_FALSE(cJSON_ReplaceItemInObject(item, NULL, item));
  344. TEST_ASSERT_FALSE(cJSON_ReplaceItemInObject(item, "item", NULL));
  345. TEST_ASSERT_FALSE(cJSON_ReplaceItemInObjectCaseSensitive(NULL, "item", item));
  346. TEST_ASSERT_FALSE(cJSON_ReplaceItemInObjectCaseSensitive(item, NULL, item));
  347. TEST_ASSERT_FALSE(cJSON_ReplaceItemInObjectCaseSensitive(item, "item", NULL));
  348. TEST_ASSERT_NULL(cJSON_Duplicate(NULL, true));
  349. TEST_ASSERT_FALSE(cJSON_Compare(item, NULL, false));
  350. TEST_ASSERT_FALSE(cJSON_Compare(NULL, item, false));
  351. cJSON_Minify(NULL);
  352. /* skipped because it is only used via a macro that checks for NULL */
  353. /* cJSON_SetNumberHelper(NULL, 0); */
  354. cJSON_Delete(item);
  355. }
  356. static void * CJSON_CDECL failing_realloc(void *pointer, size_t size)
  357. {
  358. (void)size;
  359. (void)pointer;
  360. return NULL;
  361. }
  362. static void ensure_should_fail_on_failed_realloc(void)
  363. {
  364. printbuffer buffer = {NULL, 10, 0, 0, false, false, {&malloc, &free, &failing_realloc}};
  365. buffer.buffer = (unsigned char*)malloc(100);
  366. TEST_ASSERT_NOT_NULL(buffer.buffer);
  367. TEST_ASSERT_NULL_MESSAGE(ensure(&buffer, 200), "Ensure didn't fail with failing realloc.");
  368. }
  369. static void skip_utf8_bom_should_skip_bom(void)
  370. {
  371. const unsigned char string[] = "\xEF\xBB\xBF{}";
  372. parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
  373. buffer.content = string;
  374. buffer.length = sizeof(string);
  375. buffer.hooks = global_hooks;
  376. TEST_ASSERT_TRUE(skip_utf8_bom(&buffer) == &buffer);
  377. TEST_ASSERT_EQUAL_UINT(3U, (unsigned int)buffer.offset);
  378. }
  379. static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void)
  380. {
  381. const unsigned char string[] = " \xEF\xBB\xBF{}";
  382. parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
  383. buffer.content = string;
  384. buffer.length = sizeof(string);
  385. buffer.hooks = global_hooks;
  386. buffer.offset = 1;
  387. TEST_ASSERT_NULL(skip_utf8_bom(&buffer));
  388. }
  389. static void cjson_get_string_value_should_get_a_string(void)
  390. {
  391. cJSON *string = cJSON_CreateString("test");
  392. cJSON *number = cJSON_CreateNumber(1);
  393. TEST_ASSERT_TRUE(cJSON_GetStringValue(string) == string->valuestring);
  394. TEST_ASSERT_NULL(cJSON_GetStringValue(number));
  395. TEST_ASSERT_NULL(cJSON_GetStringValue(NULL));
  396. cJSON_Delete(number);
  397. cJSON_Delete(string);
  398. }
  399. static void cjson_get_number_value_should_get_a_number(void)
  400. {
  401. cJSON *string = cJSON_CreateString("test");
  402. cJSON *number = cJSON_CreateNumber(1);
  403. TEST_ASSERT_EQUAL_DOUBLE(cJSON_GetNumberValue(number), number->valuedouble);
  404. TEST_ASSERT_DOUBLE_IS_NAN(cJSON_GetNumberValue(string));
  405. TEST_ASSERT_DOUBLE_IS_NAN(cJSON_GetNumberValue(NULL));
  406. cJSON_Delete(number);
  407. cJSON_Delete(string);
  408. }
  409. static void cjson_create_string_reference_should_create_a_string_reference(void) {
  410. const char *string = "I am a string!";
  411. cJSON *string_reference = cJSON_CreateStringReference(string);
  412. TEST_ASSERT_TRUE(string_reference->valuestring == string);
  413. TEST_ASSERT_EQUAL_INT(cJSON_IsReference | cJSON_String, string_reference->type);
  414. cJSON_Delete(string_reference);
  415. }
  416. static void cjson_create_object_reference_should_create_an_object_reference(void) {
  417. cJSON *number_reference = NULL;
  418. cJSON *number_object = cJSON_CreateObject();
  419. cJSON *number = cJSON_CreateNumber(42);
  420. const char key[] = "number";
  421. TEST_ASSERT_TRUE(cJSON_IsNumber(number));
  422. TEST_ASSERT_TRUE(cJSON_IsObject(number_object));
  423. cJSON_AddItemToObjectCS(number_object, key, number);
  424. number_reference = cJSON_CreateObjectReference(number);
  425. TEST_ASSERT_TRUE(number_reference->child == number);
  426. TEST_ASSERT_EQUAL_INT(cJSON_Object | cJSON_IsReference, number_reference->type);
  427. cJSON_Delete(number_object);
  428. cJSON_Delete(number_reference);
  429. }
  430. static void cjson_create_array_reference_should_create_an_array_reference(void) {
  431. cJSON *number_reference = NULL;
  432. cJSON *number_array = cJSON_CreateArray();
  433. cJSON *number = cJSON_CreateNumber(42);
  434. TEST_ASSERT_TRUE(cJSON_IsNumber(number));
  435. TEST_ASSERT_TRUE(cJSON_IsArray(number_array));
  436. cJSON_AddItemToArray(number_array, number);
  437. number_reference = cJSON_CreateArrayReference(number);
  438. TEST_ASSERT_TRUE(number_reference->child == number);
  439. TEST_ASSERT_EQUAL_INT(cJSON_Array | cJSON_IsReference, number_reference->type);
  440. cJSON_Delete(number_array);
  441. cJSON_Delete(number_reference);
  442. }
  443. static void cjson_add_item_to_object_or_array_should_not_add_itself(void)
  444. {
  445. cJSON *object = cJSON_CreateObject();
  446. cJSON *array = cJSON_CreateArray();
  447. cJSON_bool flag = false;
  448. flag = cJSON_AddItemToObject(object, "key", object);
  449. TEST_ASSERT_FALSE_MESSAGE(flag, "add an object to itself should fail");
  450. flag = cJSON_AddItemToArray(array, array);
  451. TEST_ASSERT_FALSE_MESSAGE(flag, "add an array to itself should fail");
  452. cJSON_Delete(object);
  453. cJSON_Delete(array);
  454. }
  455. static void cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased(void)
  456. {
  457. cJSON *object = cJSON_CreateObject();
  458. cJSON *number = cJSON_CreateNumber(42);
  459. char *name = (char*)cJSON_strdup((const unsigned char*)"number", &global_hooks);
  460. TEST_ASSERT_NOT_NULL(object);
  461. TEST_ASSERT_NOT_NULL(number);
  462. TEST_ASSERT_NOT_NULL(name);
  463. number->string = name;
  464. /* The following should not have a use after free
  465. * that would show up in valgrind or with AddressSanitizer */
  466. cJSON_AddItemToObject(object, number->string, number);
  467. cJSON_Delete(object);
  468. }
  469. static void cjson_delete_item_from_array_should_not_broken_list_structure(void)
  470. {
  471. const char expected_json1[] = "{\"rd\":[{\"a\":\"123\"}]}";
  472. const char expected_json2[] = "{\"rd\":[{\"a\":\"123\"},{\"b\":\"456\"}]}";
  473. const char expected_json3[] = "{\"rd\":[{\"b\":\"456\"}]}";
  474. char *str1 = NULL;
  475. char *str2 = NULL;
  476. char *str3 = NULL;
  477. cJSON *root = cJSON_Parse("{}");
  478. cJSON *array = cJSON_AddArrayToObject(root, "rd");
  479. cJSON *item1 = cJSON_Parse("{\"a\":\"123\"}");
  480. cJSON *item2 = cJSON_Parse("{\"b\":\"456\"}");
  481. cJSON_AddItemToArray(array, item1);
  482. str1 = cJSON_PrintUnformatted(root);
  483. TEST_ASSERT_EQUAL_STRING(expected_json1, str1);
  484. free(str1);
  485. cJSON_AddItemToArray(array, item2);
  486. str2 = cJSON_PrintUnformatted(root);
  487. TEST_ASSERT_EQUAL_STRING(expected_json2, str2);
  488. free(str2);
  489. /* this should not broken list structure */
  490. cJSON_DeleteItemFromArray(array, 0);
  491. str3 = cJSON_PrintUnformatted(root);
  492. TEST_ASSERT_EQUAL_STRING(expected_json3, str3);
  493. free(str3);
  494. cJSON_Delete(root);
  495. }
  496. static void cjson_set_valuestring_to_object_should_not_leak_memory(void)
  497. {
  498. cJSON *root = cJSON_Parse("{}");
  499. const char *stringvalue = "valuestring could be changed safely";
  500. const char *reference_valuestring = "reference item should be freed by yourself";
  501. const char *short_valuestring = "shorter valuestring";
  502. const char *long_valuestring = "new valuestring which much longer than previous should be changed safely";
  503. cJSON *item1 = cJSON_CreateString(stringvalue);
  504. cJSON *item2 = cJSON_CreateStringReference(reference_valuestring);
  505. char *ptr1 = NULL;
  506. char *return_value = NULL;
  507. cJSON_AddItemToObject(root, "one", item1);
  508. cJSON_AddItemToObject(root, "two", item2);
  509. ptr1 = item1->valuestring;
  510. return_value = cJSON_SetValuestring(cJSON_GetObjectItem(root, "one"), short_valuestring);
  511. TEST_ASSERT_NOT_NULL(return_value);
  512. TEST_ASSERT_EQUAL_PTR_MESSAGE(ptr1, return_value, "new valuestring shorter than old should not reallocate memory");
  513. TEST_ASSERT_EQUAL_STRING(short_valuestring, cJSON_GetObjectItem(root, "one")->valuestring);
  514. /* we needn't to free the original valuestring manually */
  515. ptr1 = item1->valuestring;
  516. return_value = cJSON_SetValuestring(cJSON_GetObjectItem(root, "one"), long_valuestring);
  517. TEST_ASSERT_NOT_NULL(return_value);
  518. TEST_ASSERT_NOT_EQUAL_MESSAGE(ptr1, return_value, "new valuestring longer than old should reallocate memory")
  519. TEST_ASSERT_EQUAL_STRING(long_valuestring, cJSON_GetObjectItem(root, "one")->valuestring);
  520. return_value = cJSON_SetValuestring(cJSON_GetObjectItem(root, "two"), long_valuestring);
  521. TEST_ASSERT_NULL_MESSAGE(return_value, "valuestring of reference object should not be changed");
  522. TEST_ASSERT_EQUAL_STRING(reference_valuestring, cJSON_GetObjectItem(root, "two")->valuestring);
  523. cJSON_Delete(root);
  524. }
  525. int CJSON_CDECL main(void)
  526. {
  527. UNITY_BEGIN();
  528. RUN_TEST(cjson_array_foreach_should_loop_over_arrays);
  529. RUN_TEST(cjson_array_foreach_should_not_dereference_null_pointer);
  530. RUN_TEST(cjson_get_object_item_should_get_object_items);
  531. RUN_TEST(cjson_get_object_item_case_sensitive_should_get_object_items);
  532. RUN_TEST(cjson_get_object_item_should_not_crash_with_array);
  533. RUN_TEST(cjson_get_object_item_case_sensitive_should_not_crash_with_array);
  534. RUN_TEST(typecheck_functions_should_check_type);
  535. RUN_TEST(cjson_should_not_parse_to_deeply_nested_jsons);
  536. RUN_TEST(cjson_set_number_value_should_set_numbers);
  537. RUN_TEST(cjson_detach_item_via_pointer_should_detach_items);
  538. RUN_TEST(cjson_replace_item_via_pointer_should_replace_items);
  539. RUN_TEST(cjson_replace_item_in_object_should_preserve_name);
  540. RUN_TEST(cjson_functions_should_not_crash_with_null_pointers);
  541. RUN_TEST(ensure_should_fail_on_failed_realloc);
  542. RUN_TEST(skip_utf8_bom_should_skip_bom);
  543. RUN_TEST(skip_utf8_bom_should_not_skip_bom_if_not_at_beginning);
  544. RUN_TEST(cjson_get_string_value_should_get_a_string);
  545. RUN_TEST(cjson_get_number_value_should_get_a_number);
  546. RUN_TEST(cjson_create_string_reference_should_create_a_string_reference);
  547. RUN_TEST(cjson_create_object_reference_should_create_an_object_reference);
  548. RUN_TEST(cjson_create_array_reference_should_create_an_array_reference);
  549. RUN_TEST(cjson_add_item_to_object_or_array_should_not_add_itself);
  550. RUN_TEST(cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased);
  551. RUN_TEST(cjson_delete_item_from_array_should_not_broken_list_structure);
  552. RUN_TEST(cjson_set_valuestring_to_object_should_not_leak_memory);
  553. return UNITY_END();
  554. }