parse_examples.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 cJSON *parse_file(const char *filename)
  26. {
  27. cJSON *parsed = NULL;
  28. char *content = read_file(filename);
  29. parsed = cJSON_Parse(content);
  30. if (content != NULL)
  31. {
  32. free(content);
  33. }
  34. return parsed;
  35. }
  36. static void do_test(const char *test_name)
  37. {
  38. char *expected = NULL;
  39. char *actual = NULL;
  40. cJSON *tree = NULL;
  41. size_t test_name_length = 0;
  42. /* path of the test input */
  43. char *test_path = NULL;
  44. /* path of the expected output */
  45. char *expected_path = NULL;
  46. test_name_length = strlen(test_name);
  47. /* allocate file paths */
  48. #define TEST_DIR_PATH "inputs/"
  49. test_path = (char*)malloc(sizeof(TEST_DIR_PATH) + test_name_length);
  50. TEST_ASSERT_NOT_NULL_MESSAGE(test_path, "Failed to allocate test_path buffer.");
  51. expected_path = (char*)malloc(sizeof(TEST_DIR_PATH) + test_name_length + sizeof(".expected"));
  52. TEST_ASSERT_NOT_NULL_MESSAGE(expected_path, "Failed to allocate expected_path buffer.");
  53. /* create file paths */
  54. sprintf(test_path, TEST_DIR_PATH"%s", test_name);
  55. sprintf(expected_path, TEST_DIR_PATH"%s.expected", test_name);
  56. /* read expected output */
  57. expected = read_file(expected_path);
  58. TEST_ASSERT_NOT_NULL_MESSAGE(expected, "Failed to read expected output.");
  59. /* read and parse test */
  60. tree = parse_file(test_path);
  61. TEST_ASSERT_NOT_NULL_MESSAGE(tree, "Failed to read of parse test.");
  62. /* print the parsed tree */
  63. actual = cJSON_Print(tree);
  64. TEST_ASSERT_NOT_NULL_MESSAGE(actual, "Failed to print tree back to JSON.");
  65. TEST_ASSERT_EQUAL_STRING(expected, actual);
  66. /* cleanup resources */
  67. if (expected != NULL)
  68. {
  69. free(expected);
  70. }
  71. if (tree != NULL)
  72. {
  73. cJSON_Delete(tree);
  74. }
  75. if (actual != NULL)
  76. {
  77. free(actual);
  78. }
  79. if (test_path != NULL)
  80. {
  81. free(test_path);
  82. }
  83. if (expected_path != NULL)
  84. {
  85. free(expected_path);
  86. }
  87. }
  88. static void file_test1_should_be_parsed_and_printed(void)
  89. {
  90. do_test("test1");
  91. }
  92. static void file_test2_should_be_parsed_and_printed(void)
  93. {
  94. do_test("test2");
  95. }
  96. static void file_test3_should_be_parsed_and_printed(void)
  97. {
  98. do_test("test3");
  99. }
  100. static void file_test4_should_be_parsed_and_printed(void)
  101. {
  102. do_test("test4");
  103. }
  104. static void file_test5_should_be_parsed_and_printed(void)
  105. {
  106. do_test("test5");
  107. }
  108. static void file_test6_should_not_be_parsed(void)
  109. {
  110. char *test6 = NULL;
  111. cJSON *tree = NULL;
  112. test6 = read_file("inputs/test6");
  113. TEST_ASSERT_NOT_NULL_MESSAGE(test6, "Failed to read test6 data.");
  114. tree = cJSON_Parse(test6);
  115. TEST_ASSERT_NULL_MESSAGE(tree, "Should fail to parse what is not JSON.");
  116. TEST_ASSERT_EQUAL_PTR_MESSAGE(test6, cJSON_GetErrorPtr(), "Error pointer is incorrect.");
  117. if (test6 != NULL)
  118. {
  119. free(test6);
  120. }
  121. if (tree != NULL)
  122. {
  123. cJSON_Delete(tree);
  124. }
  125. }
  126. static void file_test7_should_be_parsed_and_printed(void)
  127. {
  128. do_test("test7");
  129. }
  130. static void file_test8_should_be_parsed_and_printed(void)
  131. {
  132. do_test("test8");
  133. }
  134. static void file_test9_should_be_parsed_and_printed(void)
  135. {
  136. do_test("test9");
  137. }
  138. static void file_test10_should_be_parsed_and_printed(void)
  139. {
  140. do_test("test10");
  141. }
  142. static void file_test11_should_be_parsed_and_printed(void)
  143. {
  144. do_test("test11");
  145. }
  146. static void test12_should_not_be_parsed(void)
  147. {
  148. const char *test12 = "{ \"name\": ";
  149. cJSON *tree = NULL;
  150. tree = cJSON_Parse(test12);
  151. TEST_ASSERT_NULL_MESSAGE(tree, "Should fail to parse incomplete JSON.");
  152. TEST_ASSERT_EQUAL_PTR_MESSAGE(test12 + strlen(test12), cJSON_GetErrorPtr(), "Error pointer is incorrect.");
  153. if (tree != NULL)
  154. {
  155. cJSON_Delete(tree);
  156. }
  157. }
  158. static void test13_should_be_parsed_without_null_termination(void)
  159. {
  160. cJSON *tree = NULL;
  161. const char test_13[] = "{" \
  162. "\"Image\":{" \
  163. "\"Width\":800," \
  164. "\"Height\":600," \
  165. "\"Title\":\"Viewfrom15thFloor\"," \
  166. "\"Thumbnail\":{" \
  167. "\"Url\":\"http:/*www.example.com/image/481989943\"," \
  168. "\"Height\":125," \
  169. "\"Width\":\"100\"" \
  170. "}," \
  171. "\"IDs\":[116,943,234,38793]" \
  172. "}" \
  173. "}";
  174. char test_13_wo_null[sizeof(test_13) - 1];
  175. memcpy(test_13_wo_null, test_13, sizeof(test_13) - 1);
  176. tree = cJSON_ParseWithLength(test_13_wo_null, sizeof(test_13) - 1);
  177. TEST_ASSERT_NOT_NULL_MESSAGE(tree, "Failed to parse valid json.");
  178. if (tree != NULL)
  179. {
  180. cJSON_Delete(tree);
  181. }
  182. }
  183. static void test14_should_not_be_parsed(void)
  184. {
  185. cJSON *tree = NULL;
  186. const char test_14[] = "{" \
  187. "\"Image\":{" \
  188. "\"Width\":800," \
  189. "\"Height\":600," \
  190. "\"Title\":\"Viewfrom15thFloor\"," \
  191. "\"Thumbnail\":{" \
  192. "\"Url\":\"http:/*www.example.com/image/481989943\"," \
  193. "\"Height\":125," \
  194. "\"Width\":\"100\"" \
  195. "}," \
  196. "\"IDs\":[116,943,234,38793]" \
  197. "}" \
  198. "}";
  199. tree = cJSON_ParseWithLength(test_14, sizeof(test_14) - 2);
  200. TEST_ASSERT_NULL_MESSAGE(tree, "Should not continue after buffer_length is reached.");
  201. if (tree != NULL)
  202. {
  203. cJSON_Delete(tree);
  204. }
  205. }
  206. int CJSON_CDECL main(void)
  207. {
  208. UNITY_BEGIN();
  209. RUN_TEST(file_test1_should_be_parsed_and_printed);
  210. RUN_TEST(file_test2_should_be_parsed_and_printed);
  211. RUN_TEST(file_test3_should_be_parsed_and_printed);
  212. RUN_TEST(file_test4_should_be_parsed_and_printed);
  213. RUN_TEST(file_test5_should_be_parsed_and_printed);
  214. RUN_TEST(file_test6_should_not_be_parsed);
  215. RUN_TEST(file_test7_should_be_parsed_and_printed);
  216. RUN_TEST(file_test8_should_be_parsed_and_printed);
  217. RUN_TEST(file_test9_should_be_parsed_and_printed);
  218. RUN_TEST(file_test10_should_be_parsed_and_printed);
  219. RUN_TEST(file_test11_should_be_parsed_and_printed);
  220. RUN_TEST(test12_should_not_be_parsed);
  221. RUN_TEST(test13_should_be_parsed_without_null_termination);
  222. RUN_TEST(test14_should_not_be_parsed);
  223. return UNITY_END();
  224. }