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 31static 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 46static 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 109static void file_test1_should_be_parsed_and_printed(void) 110{ 111 do_test("test1"); 112} 113 114static void file_test2_should_be_parsed_and_printed(void) 115{ 116 do_test("test2"); 117} 118 119static void file_test3_should_be_parsed_and_printed(void) 120{ 121 do_test("test3"); 122} 123 124static void file_test4_should_be_parsed_and_printed(void) 125{ 126 do_test("test4"); 127} 128 129static void file_test5_should_be_parsed_and_printed(void) 130{ 131 do_test("test5"); 132} 133 134static 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 157static void file_test7_should_be_parsed_and_printed(void) 158{ 159 do_test("test7"); 160} 161 162static void file_test8_should_be_parsed_and_printed(void) 163{ 164 do_test("test8"); 165} 166 167static void file_test9_should_be_parsed_and_printed(void) 168{ 169 do_test("test9"); 170} 171 172static void file_test10_should_be_parsed_and_printed(void) 173{ 174 do_test("test10"); 175} 176 177static void file_test11_should_be_parsed_and_printed(void) 178{ 179 do_test("test11"); 180} 181 182static 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 198static 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 227static 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 253int CJSON_CDECL main(void) 254{ 255 UNITY_BEGIN(); 256 RUN_TEST(file_test1_should_be_parsed_and_printed); 257 RUN_TEST(file_test2_should_be_parsed_and_printed); 258 RUN_TEST(file_test3_should_be_parsed_and_printed); 259 RUN_TEST(file_test4_should_be_parsed_and_printed); 260 RUN_TEST(file_test5_should_be_parsed_and_printed); 261 RUN_TEST(file_test6_should_not_be_parsed); 262 RUN_TEST(file_test7_should_be_parsed_and_printed); 263 RUN_TEST(file_test8_should_be_parsed_and_printed); 264 RUN_TEST(file_test9_should_be_parsed_and_printed); 265 RUN_TEST(file_test10_should_be_parsed_and_printed); 266 RUN_TEST(file_test11_should_be_parsed_and_printed); 267 RUN_TEST(test12_should_not_be_parsed); 268 RUN_TEST(test13_should_be_parsed_without_null_termination); 269 RUN_TEST(test14_should_not_be_parsed); 270 return UNITY_END(); 271} 272