Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/cJSON/blob/master/tests/parse_examples.c # cJSON [Ported from github.com/DaveGamble/cJSON] Fix: heap-use-after-free in merge_patch when patch is subtree of target (#1065) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `cJSON / tests / parse_examples.c` 299 lines ยท 8699 bytes Raw Blame History 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 24 #include 25 #include 26 27 #include "unity/examples/unity_config.h" 28 #include "unity/src/unity.h" 29 #include "common.h" 30 31 static cJSON *parse_file(const char *filename) 32 { 33 cJSON *parsed = NULL; 34 char *content = read_file(filename); 35 36 parsed = cJSON_Parse(content); 37 38 if (content != NULL) 39 { 40 free(content); 41 } 42 43 return parsed; 44 } 45 46 static void do_test(const char *test_name) 47 { 48 char *expected = NULL; 49 char *actual = NULL; 50 cJSON *tree = NULL; 51 52 size_t test_name_length = 0; 53 /* path of the test input */ 54 char *test_path = NULL; 55 /* path of the expected output */ 56 char *expected_path = NULL; 57 58 test_name_length = strlen(test_name); 59 60 /* allocate file paths */ 61 #define TEST_DIR_PATH "inputs/" 62 test_path = (char*)malloc(sizeof(TEST_DIR_PATH) + test_name_length); 63 TEST_ASSERT_NOT_NULL_MESSAGE(test_path, "Failed to allocate test_path buffer."); 64 expected_path = (char*)malloc(sizeof(TEST_DIR_PATH) + test_name_length + sizeof(".expected")); 65 TEST_ASSERT_NOT_NULL_MESSAGE(expected_path, "Failed to allocate expected_path buffer."); 66 67 /* create file paths */ 68 sprintf(test_path, TEST_DIR_PATH"%s", test_name); 69 sprintf(expected_path, TEST_DIR_PATH"%s.expected", test_name); 70 71 /* read expected output */ 72 expected = read_file(expected_path); 73 TEST_ASSERT_NOT_NULL_MESSAGE(expected, "Failed to read expected output."); 74 75 /* read and parse test */ 76 tree = parse_file(test_path); 77 TEST_ASSERT_NOT_NULL_MESSAGE(tree, "Failed to read of parse test."); 78 79 /* print the parsed tree */ 80 actual = cJSON_Print(tree); 81 TEST_ASSERT_NOT_NULL_MESSAGE(actual, "Failed to print tree back to JSON."); 82 83 84 TEST_ASSERT_EQUAL_STRING(expected, actual); 85 86 /* cleanup resources */ 87 if (expected != NULL) 88 { 89 free(expected); 90 } 91 if (tree != NULL) 92 { 93 cJSON_Delete(tree); 94 } 95 if (actual != NULL) 96 { 97 free(actual); 98 } 99 if (test_path != NULL) 100 { 101 free(test_path); 102 } 103 if (expected_path != NULL) 104 { 105 free(expected_path); 106 } 107 } 108 109 static void file_test1_should_be_parsed_and_printed(void) 110 { 111 do_test("test1"); 112 } 113 114 static void file_test2_should_be_parsed_and_printed(void) 115 { 116 do_test("test2"); 117 } 118 119 static void file_test3_should_be_parsed_and_printed(void) 120 { 121 do_test("test3"); 122 } 123 124 static void file_test4_should_be_parsed_and_printed(void) 125 { 126 do_test("test4"); 127 } 128 129 static void file_test5_should_be_parsed_and_printed(void) 130 { 131 do_test("test5"); 132 } 133 134 static void file_test6_should_not_be_parsed(void) 135 { 136 char *test6 = NULL; 137 cJSON *tree = NULL; 138 139 test6 = read_file("inputs/test6"); 140 TEST_ASSERT_NOT_NULL_MESSAGE(test6, "Failed to read test6 data."); 141 142 tree = cJSON_Parse(test6); 143 TEST_ASSERT_NULL_MESSAGE(tree, "Should fail to parse what is not JSON."); 144 145 TEST_ASSERT_EQUAL_PTR_MESSAGE(test6, cJSON_GetErrorPtr(), "Error pointer is incorrect."); 146 147 if (test6 != NULL) 148 { 149 free(test6); 150 } 151 if (tree != NULL) 152 { 153 cJSON_Delete(tree); 154 } 155 } 156 157 static void file_test7_should_be_parsed_and_printed(void) 158 { 159 do_test("test7"); 160 } 161 162 static void file_test8_should_be_parsed_and_printed(void) 163 { 164 do_test("test8"); 165 } 166 167 static void file_test9_should_be_parsed_and_printed(void) 168 { 169 do_test("test9"); 170 } 171 172 static void file_test10_should_be_parsed_and_printed(void) 173 { 174 do_test("test10"); 175 } 176 177 static void file_test11_should_be_parsed_and_printed(void) 178 { 179 do_test("test11"); 180 } 181 182 static void test12_should_not_be_parsed(void) 183 { 184 const char *test12 = "{ \"name\": "; 185 cJSON *tree = NULL; 186 187 tree = cJSON_Parse(test12); 188 TEST_ASSERT_NULL_MESSAGE(tree, "Should fail to parse incomplete JSON."); 189 190 TEST_ASSERT_EQUAL_PTR_MESSAGE(test12 + strlen(test12), cJSON_GetErrorPtr(), "Error pointer is incorrect."); 191 192 if (tree != NULL) 193 { 194 cJSON_Delete(tree); 195 } 196 } 197 198 static void test13_should_be_parsed_without_null_termination(void) 199 { 200 cJSON *tree = NULL; 201 const char test_13[] = "{" \ 202 "\"Image\":{" \ 203 "\"Width\":800," \ 204 "\"Height\":600," \ 205 "\"Title\":\"Viewfrom15thFloor\"," \ 206 "\"Thumbnail\":{" \ 207 "\"Url\":\"http:/*www.example.com/image/`481989943`\"," \ 208 "\"Height\":125," \ 209 "\"Width\":\"100\"" \ 210 "}," \ 211 "\"IDs\":[116,943,234,38793]" \ 212 "}" \ 213 "}"; 214 215 char test_13_wo_null[sizeof(test_13) - 1]; 216 memcpy(test_13_wo_null, test_13, sizeof(test_13) - 1); 217 218 tree = cJSON_ParseWithLength(test_13_wo_null, sizeof(test_13) - 1); 219 TEST_ASSERT_NOT_NULL_MESSAGE(tree, "Failed to parse valid json."); 220 221 if (tree != NULL) 222 { 223 cJSON_Delete(tree); 224 } 225 } 226 227 static void test14_should_not_be_parsed(void) 228 { 229 cJSON *tree = NULL; 230 const char test_14[] = "{" \ 231 "\"Image\":{" \ 232 "\"Width\":800," \ 233 "\"Height\":600," \ 234 "\"Title\":\"Viewfrom15thFloor\"," \ 235 "\"Thumbnail\":{" \ 236 "\"Url\":\"http:/*www.example.com/image/`481989943`\"," \ 237 "\"Height\":125," \ 238 "\"Width\":\"100\"" \ 239 "}," \ 240 "\"IDs\":[116,943,234,38793]" \ 241 "}" \ 242 "}"; 243 244 tree = cJSON_ParseWithLength(test_14, sizeof(test_14) - 2); 245 TEST_ASSERT_NULL_MESSAGE(tree, "Should not continue after buffer_length is reached."); 246 247 if (tree != NULL) 248 { 249 cJSON_Delete(tree); 250 } 251 } 252 253 /* Address Sanitizer */ 254 static void test15_should_not_heap_buffer_overflow(void) 255 { 256 const char *strings[] = { 257 "{\"1\":1,", 258 "{\"1\":1, ", 259 }; 260 261 size_t i; 262 263 for (i = 0; i < sizeof(strings) / sizeof(strings[0]); i+=1) 264 { 265 const char *json_string = strings[i]; 266 size_t len = strlen(json_string); 267 cJSON *json = NULL; 268 269 char *exact_size_heap = (char*)malloc(len); 270 TEST_ASSERT_NOT_NULL(exact_size_heap); 271 272 memcpy(exact_size_heap, json_string, len); 273 json = cJSON_ParseWithLength(exact_size_heap, len); 274 275 cJSON_Delete(json); 276 free(exact_size_heap); 277 } 278 } 279 280 int CJSON_CDECL main(void) 281 { 282 UNITY_BEGIN(); 283 RUN_TEST(file_test1_should_be_parsed_and_printed); 284 RUN_TEST(file_test2_should_be_parsed_and_printed); 285 RUN_TEST(file_test3_should_be_parsed_and_printed); 286 RUN_TEST(file_test4_should_be_parsed_and_printed); 287 RUN_TEST(file_test5_should_be_parsed_and_printed); 288 RUN_TEST(file_test6_should_not_be_parsed); 289 RUN_TEST(file_test7_should_be_parsed_and_printed); 290 RUN_TEST(file_test8_should_be_parsed_and_printed); 291 RUN_TEST(file_test9_should_be_parsed_and_printed); 292 RUN_TEST(file_test10_should_be_parsed_and_printed); 293 RUN_TEST(file_test11_should_be_parsed_and_printed); 294 RUN_TEST(test12_should_not_be_parsed); 295 RUN_TEST(test13_should_be_parsed_without_null_termination); 296 RUN_TEST(test14_should_not_be_parsed); 297 RUN_TEST(test15_should_not_heap_buffer_overflow); 298 return UNITY_END(); 299 }