cjson_add.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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_CDECL failing_malloc(size_t size)
  26. {
  27. (void)size;
  28. return NULL;
  29. }
  30. /* work around MSVC error C2322: '...' address of dillimport '...' is not static */
  31. static void CJSON_CDECL normal_free(void *pointer)
  32. {
  33. free(pointer);
  34. }
  35. static cJSON_Hooks failing_hooks = {
  36. failing_malloc,
  37. normal_free
  38. };
  39. static void cjson_add_null_should_add_null(void)
  40. {
  41. cJSON *root = cJSON_CreateObject();
  42. cJSON *null = NULL;
  43. cJSON_AddNullToObject(root, "null");
  44. TEST_ASSERT_NOT_NULL(null = cJSON_GetObjectItemCaseSensitive(root, "null"));
  45. TEST_ASSERT_EQUAL_INT(null->type, cJSON_NULL);
  46. cJSON_Delete(root);
  47. }
  48. static void cjson_add_null_should_fail_with_null_pointers(void)
  49. {
  50. cJSON *root = cJSON_CreateObject();
  51. TEST_ASSERT_NULL(cJSON_AddNullToObject(NULL, "null"));
  52. TEST_ASSERT_NULL(cJSON_AddNullToObject(root, NULL));
  53. cJSON_Delete(root);
  54. }
  55. static void cjson_add_null_should_fail_on_allocation_failure(void)
  56. {
  57. cJSON *root = cJSON_CreateObject();
  58. cJSON_InitHooks(&failing_hooks);
  59. TEST_ASSERT_NULL(cJSON_AddNullToObject(root, "null"));
  60. cJSON_InitHooks(NULL);
  61. cJSON_Delete(root);
  62. }
  63. static void cjson_add_true_should_add_true(void)
  64. {
  65. cJSON *root = cJSON_CreateObject();
  66. cJSON *true_item = NULL;
  67. cJSON_AddTrueToObject(root, "true");
  68. TEST_ASSERT_NOT_NULL(true_item = cJSON_GetObjectItemCaseSensitive(root, "true"));
  69. TEST_ASSERT_EQUAL_INT(true_item->type, cJSON_True);
  70. cJSON_Delete(root);
  71. }
  72. static void cjson_add_true_should_fail_with_null_pointers(void)
  73. {
  74. cJSON *root = cJSON_CreateObject();
  75. TEST_ASSERT_NULL(cJSON_AddTrueToObject(NULL, "true"));
  76. TEST_ASSERT_NULL(cJSON_AddTrueToObject(root, NULL));
  77. cJSON_Delete(root);
  78. }
  79. static void cjson_add_true_should_fail_on_allocation_failure(void)
  80. {
  81. cJSON *root = cJSON_CreateObject();
  82. cJSON_InitHooks(&failing_hooks);
  83. TEST_ASSERT_NULL(cJSON_AddTrueToObject(root, "true"));
  84. cJSON_InitHooks(NULL);
  85. cJSON_Delete(root);
  86. }
  87. static void cjson_create_int_array_should_fail_on_allocation_failure(void)
  88. {
  89. int numbers[] = {1, 2, 3};
  90. cJSON_InitHooks(&failing_hooks);
  91. TEST_ASSERT_NULL(cJSON_CreateIntArray(numbers, 3));
  92. cJSON_InitHooks(NULL);
  93. }
  94. static void cjson_create_float_array_should_fail_on_allocation_failure(void)
  95. {
  96. float numbers[] = {1.0f, 2.0f, 3.0f};
  97. cJSON_InitHooks(&failing_hooks);
  98. TEST_ASSERT_NULL(cJSON_CreateFloatArray(numbers, 3));
  99. cJSON_InitHooks(NULL);
  100. }
  101. static void cjson_create_double_array_should_fail_on_allocation_failure(void)
  102. {
  103. double numbers[] = {1.0, 2.0, 3.0};
  104. cJSON_InitHooks(&failing_hooks);
  105. TEST_ASSERT_NULL(cJSON_CreateDoubleArray(numbers, 3));
  106. cJSON_InitHooks(NULL);
  107. }
  108. static void cjson_create_string_array_should_fail_on_allocation_failure(void)
  109. {
  110. const char* strings[] = {"1", "2", "3"};
  111. cJSON_InitHooks(&failing_hooks);
  112. TEST_ASSERT_NULL(cJSON_CreateStringArray(strings, 3));
  113. cJSON_InitHooks(NULL);
  114. }
  115. static void cjson_add_false_should_add_false(void)
  116. {
  117. cJSON *root = cJSON_CreateObject();
  118. cJSON *false_item = NULL;
  119. cJSON_AddFalseToObject(root, "false");
  120. TEST_ASSERT_NOT_NULL(false_item = cJSON_GetObjectItemCaseSensitive(root, "false"));
  121. TEST_ASSERT_EQUAL_INT(false_item->type, cJSON_False);
  122. cJSON_Delete(root);
  123. }
  124. static void cjson_add_false_should_fail_with_null_pointers(void)
  125. {
  126. cJSON *root = cJSON_CreateObject();
  127. TEST_ASSERT_NULL(cJSON_AddFalseToObject(NULL, "false"));
  128. TEST_ASSERT_NULL(cJSON_AddFalseToObject(root, NULL));
  129. cJSON_Delete(root);
  130. }
  131. static void cjson_add_false_should_fail_on_allocation_failure(void)
  132. {
  133. cJSON *root = cJSON_CreateObject();
  134. cJSON_InitHooks(&failing_hooks);
  135. TEST_ASSERT_NULL(cJSON_AddFalseToObject(root, "false"));
  136. cJSON_InitHooks(NULL);
  137. cJSON_Delete(root);
  138. }
  139. static void cjson_add_bool_should_add_bool(void)
  140. {
  141. cJSON *root = cJSON_CreateObject();
  142. cJSON *true_item = NULL;
  143. cJSON *false_item = NULL;
  144. /* true */
  145. cJSON_AddBoolToObject(root, "true", true);
  146. TEST_ASSERT_NOT_NULL(true_item = cJSON_GetObjectItemCaseSensitive(root, "true"));
  147. TEST_ASSERT_EQUAL_INT(true_item->type, cJSON_True);
  148. /* false */
  149. cJSON_AddBoolToObject(root, "false", false);
  150. TEST_ASSERT_NOT_NULL(false_item = cJSON_GetObjectItemCaseSensitive(root, "false"));
  151. TEST_ASSERT_EQUAL_INT(false_item->type, cJSON_False);
  152. cJSON_Delete(root);
  153. }
  154. static void cjson_add_bool_should_fail_with_null_pointers(void)
  155. {
  156. cJSON *root = cJSON_CreateObject();
  157. TEST_ASSERT_NULL(cJSON_AddBoolToObject(NULL, "false", false));
  158. TEST_ASSERT_NULL(cJSON_AddBoolToObject(root, NULL, false));
  159. cJSON_Delete(root);
  160. }
  161. static void cjson_add_bool_should_fail_on_allocation_failure(void)
  162. {
  163. cJSON *root = cJSON_CreateObject();
  164. cJSON_InitHooks(&failing_hooks);
  165. TEST_ASSERT_NULL(cJSON_AddBoolToObject(root, "false", false));
  166. cJSON_InitHooks(NULL);
  167. cJSON_Delete(root);
  168. }
  169. static void cjson_add_number_should_add_number(void)
  170. {
  171. cJSON *root = cJSON_CreateObject();
  172. cJSON *number = NULL;
  173. cJSON_AddNumberToObject(root, "number", 42);
  174. TEST_ASSERT_NOT_NULL(number = cJSON_GetObjectItemCaseSensitive(root, "number"));
  175. TEST_ASSERT_EQUAL_INT(number->type, cJSON_Number);
  176. TEST_ASSERT_EQUAL_DOUBLE(number->valuedouble, 42);
  177. TEST_ASSERT_EQUAL_INT(number->valueint, 42);
  178. cJSON_Delete(root);
  179. }
  180. static void cjson_add_number_should_fail_with_null_pointers(void)
  181. {
  182. cJSON *root = cJSON_CreateObject();
  183. TEST_ASSERT_NULL(cJSON_AddNumberToObject(NULL, "number", 42));
  184. TEST_ASSERT_NULL(cJSON_AddNumberToObject(root, NULL, 42));
  185. cJSON_Delete(root);
  186. }
  187. static void cjson_add_number_should_fail_on_allocation_failure(void)
  188. {
  189. cJSON *root = cJSON_CreateObject();
  190. cJSON_InitHooks(&failing_hooks);
  191. TEST_ASSERT_NULL(cJSON_AddNumberToObject(root, "number", 42));
  192. cJSON_InitHooks(NULL);
  193. cJSON_Delete(root);
  194. }
  195. static void cjson_add_string_should_add_string(void)
  196. {
  197. cJSON *root = cJSON_CreateObject();
  198. cJSON *string = NULL;
  199. cJSON_AddStringToObject(root, "string", "Hello World!");
  200. TEST_ASSERT_NOT_NULL(string = cJSON_GetObjectItemCaseSensitive(root, "string"));
  201. TEST_ASSERT_EQUAL_INT(string->type, cJSON_String);
  202. TEST_ASSERT_EQUAL_STRING(string->valuestring, "Hello World!");
  203. cJSON_Delete(root);
  204. }
  205. static void cjson_add_string_should_fail_with_null_pointers(void)
  206. {
  207. cJSON *root = cJSON_CreateObject();
  208. TEST_ASSERT_NULL(cJSON_AddStringToObject(NULL, "string", "string"));
  209. TEST_ASSERT_NULL(cJSON_AddStringToObject(root, NULL, "string"));
  210. cJSON_Delete(root);
  211. }
  212. static void cjson_add_string_should_fail_on_allocation_failure(void)
  213. {
  214. cJSON *root = cJSON_CreateObject();
  215. cJSON_InitHooks(&failing_hooks);
  216. TEST_ASSERT_NULL(cJSON_AddStringToObject(root, "string", "string"));
  217. cJSON_InitHooks(NULL);
  218. cJSON_Delete(root);
  219. }
  220. static void cjson_add_raw_should_add_raw(void)
  221. {
  222. cJSON *root = cJSON_CreateObject();
  223. cJSON *raw = NULL;
  224. cJSON_AddRawToObject(root, "raw", "{}");
  225. TEST_ASSERT_NOT_NULL(raw = cJSON_GetObjectItemCaseSensitive(root, "raw"));
  226. TEST_ASSERT_EQUAL_INT(raw->type, cJSON_Raw);
  227. TEST_ASSERT_EQUAL_STRING(raw->valuestring, "{}");
  228. cJSON_Delete(root);
  229. }
  230. static void cjson_add_raw_should_fail_with_null_pointers(void)
  231. {
  232. cJSON *root = cJSON_CreateObject();
  233. TEST_ASSERT_NULL(cJSON_AddRawToObject(NULL, "raw", "{}"));
  234. TEST_ASSERT_NULL(cJSON_AddRawToObject(root, NULL, "{}"));
  235. cJSON_Delete(root);
  236. }
  237. static void cjson_add_raw_should_fail_on_allocation_failure(void)
  238. {
  239. cJSON *root = cJSON_CreateObject();
  240. cJSON_InitHooks(&failing_hooks);
  241. TEST_ASSERT_NULL(cJSON_AddRawToObject(root, "raw", "{}"));
  242. cJSON_InitHooks(NULL);
  243. cJSON_Delete(root);
  244. }
  245. static void cJSON_add_object_should_add_object(void)
  246. {
  247. cJSON *root = cJSON_CreateObject();
  248. cJSON *object = NULL;
  249. cJSON_AddObjectToObject(root, "object");
  250. TEST_ASSERT_NOT_NULL(object = cJSON_GetObjectItemCaseSensitive(root, "object"));
  251. TEST_ASSERT_EQUAL_INT(object->type, cJSON_Object);
  252. cJSON_Delete(root);
  253. }
  254. static void cjson_add_object_should_fail_with_null_pointers(void)
  255. {
  256. cJSON *root = cJSON_CreateObject();
  257. TEST_ASSERT_NULL(cJSON_AddObjectToObject(NULL, "object"));
  258. TEST_ASSERT_NULL(cJSON_AddObjectToObject(root, NULL));
  259. cJSON_Delete(root);
  260. }
  261. static void cjson_add_object_should_fail_on_allocation_failure(void)
  262. {
  263. cJSON *root = cJSON_CreateObject();
  264. cJSON_InitHooks(&failing_hooks);
  265. TEST_ASSERT_NULL(cJSON_AddObjectToObject(root, "object"));
  266. cJSON_InitHooks(NULL);
  267. cJSON_Delete(root);
  268. }
  269. static void cJSON_add_array_should_add_array(void)
  270. {
  271. cJSON *root = cJSON_CreateObject();
  272. cJSON *array = NULL;
  273. cJSON_AddArrayToObject(root, "array");
  274. TEST_ASSERT_NOT_NULL(array = cJSON_GetObjectItemCaseSensitive(root, "array"));
  275. TEST_ASSERT_EQUAL_INT(array->type, cJSON_Array);
  276. cJSON_Delete(root);
  277. }
  278. static void cjson_add_array_should_fail_with_null_pointers(void)
  279. {
  280. cJSON *root = cJSON_CreateObject();
  281. TEST_ASSERT_NULL(cJSON_AddArrayToObject(NULL, "array"));
  282. TEST_ASSERT_NULL(cJSON_AddArrayToObject(root, NULL));
  283. cJSON_Delete(root);
  284. }
  285. static void cjson_add_array_should_fail_on_allocation_failure(void)
  286. {
  287. cJSON *root = cJSON_CreateObject();
  288. cJSON_InitHooks(&failing_hooks);
  289. TEST_ASSERT_NULL(cJSON_AddArrayToObject(root, "array"));
  290. cJSON_InitHooks(NULL);
  291. cJSON_Delete(root);
  292. }
  293. int CJSON_CDECL main(void)
  294. {
  295. UNITY_BEGIN();
  296. RUN_TEST(cjson_add_null_should_add_null);
  297. RUN_TEST(cjson_add_null_should_fail_with_null_pointers);
  298. RUN_TEST(cjson_add_null_should_fail_on_allocation_failure);
  299. RUN_TEST(cjson_add_true_should_add_true);
  300. RUN_TEST(cjson_add_true_should_fail_with_null_pointers);
  301. RUN_TEST(cjson_add_true_should_fail_on_allocation_failure);
  302. RUN_TEST(cjson_create_int_array_should_fail_on_allocation_failure);
  303. RUN_TEST(cjson_create_float_array_should_fail_on_allocation_failure);
  304. RUN_TEST(cjson_create_double_array_should_fail_on_allocation_failure);
  305. RUN_TEST(cjson_create_string_array_should_fail_on_allocation_failure);
  306. RUN_TEST(cjson_add_false_should_add_false);
  307. RUN_TEST(cjson_add_false_should_fail_with_null_pointers);
  308. RUN_TEST(cjson_add_false_should_fail_on_allocation_failure);
  309. RUN_TEST(cjson_add_bool_should_add_bool);
  310. RUN_TEST(cjson_add_bool_should_fail_with_null_pointers);
  311. RUN_TEST(cjson_add_bool_should_fail_on_allocation_failure);
  312. RUN_TEST(cjson_add_number_should_add_number);
  313. RUN_TEST(cjson_add_number_should_fail_with_null_pointers);
  314. RUN_TEST(cjson_add_number_should_fail_on_allocation_failure);
  315. RUN_TEST(cjson_add_string_should_add_string);
  316. RUN_TEST(cjson_add_string_should_fail_with_null_pointers);
  317. RUN_TEST(cjson_add_string_should_fail_on_allocation_failure);
  318. RUN_TEST(cjson_add_raw_should_add_raw);
  319. RUN_TEST(cjson_add_raw_should_fail_with_null_pointers);
  320. RUN_TEST(cjson_add_raw_should_fail_on_allocation_failure);
  321. RUN_TEST(cJSON_add_object_should_add_object);
  322. RUN_TEST(cjson_add_object_should_fail_with_null_pointers);
  323. RUN_TEST(cjson_add_object_should_fail_on_allocation_failure);
  324. RUN_TEST(cJSON_add_array_should_add_array);
  325. RUN_TEST(cjson_add_array_should_fail_with_null_pointers);
  326. RUN_TEST(cjson_add_array_should_fail_on_allocation_failure);
  327. return UNITY_END();
  328. }