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
cJSON / tests / parse_with_opts.c
| 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 "unity/examples/unity_config.h" |
| 24 | #include "unity/src/unity.h" |
| 25 | #include "common.h" |
| 26 | |
| 27 | static void parse_with_opts_should_handle_null(void) |
| 28 | { |
| 29 | const char *error_pointer = NULL; |
| 30 | cJSON *item = NULL; |
| 31 | TEST_ASSERT_NULL_MESSAGE(cJSON_ParseWithOpts(NULL, &error_pointer, false), "Failed to handle NULL input."); |
| 32 | item = cJSON_ParseWithOpts("{}", NULL, false); |
| 33 | TEST_ASSERT_NOT_NULL_MESSAGE(item, "Failed to handle NULL error pointer."); |
| 34 | cJSON_Delete(item); |
| 35 | TEST_ASSERT_NULL_MESSAGE(cJSON_ParseWithOpts(NULL, NULL, false), "Failed to handle both NULL."); |
| 36 | TEST_ASSERT_NULL_MESSAGE(cJSON_ParseWithOpts("{", NULL, false), "Failed to handle NULL error pointer with parse error."); |
| 37 | } |
| 38 | |
| 39 | static void parse_with_opts_should_handle_empty_strings(void) |
| 40 | { |
| 41 | const char empty_string[] = ""; |
| 42 | const char *error_pointer = NULL; |
| 43 | |
| 44 | TEST_ASSERT_NULL(cJSON_ParseWithOpts(empty_string, NULL, false)); |
| 45 | TEST_ASSERT_EQUAL_PTR(empty_string, cJSON_GetErrorPtr()); |
| 46 | |
| 47 | TEST_ASSERT_NULL(cJSON_ParseWithOpts(empty_string, &error_pointer, false)); |
| 48 | TEST_ASSERT_EQUAL_PTR(empty_string, error_pointer); |
| 49 | TEST_ASSERT_EQUAL_PTR(empty_string, cJSON_GetErrorPtr()); |
| 50 | } |
| 51 | |
| 52 | static void parse_with_opts_should_handle_incomplete_json(void) |
| 53 | { |
| 54 | const char json[] = "{ \"name\": "; |
| 55 | const char *parse_end = NULL; |
| 56 | |
| 57 | TEST_ASSERT_NULL(cJSON_ParseWithOpts(json, &parse_end, false)); |
| 58 | TEST_ASSERT_EQUAL_PTR(json + strlen(json), parse_end); |
| 59 | TEST_ASSERT_EQUAL_PTR(json + strlen(json), cJSON_GetErrorPtr()); |
| 60 | } |
| 61 | |
| 62 | static void parse_with_opts_should_require_null_if_requested(void) |
| 63 | { |
| 64 | cJSON *item = cJSON_ParseWithOpts("{}", NULL, true); |
| 65 | TEST_ASSERT_NOT_NULL(item); |
| 66 | cJSON_Delete(item); |
| 67 | item = cJSON_ParseWithOpts("{} \n", NULL, true); |
| 68 | TEST_ASSERT_NOT_NULL(item); |
| 69 | cJSON_Delete(item); |
| 70 | TEST_ASSERT_NULL(cJSON_ParseWithOpts("{}x", NULL, true)); |
| 71 | } |
| 72 | |
| 73 | static void parse_with_opts_should_return_parse_end(void) |
| 74 | { |
| 75 | const char json[] = "[] empty array XD"; |
| 76 | const char *parse_end = NULL; |
| 77 | |
| 78 | cJSON *item = cJSON_ParseWithOpts(json, &parse_end, false); |
| 79 | TEST_ASSERT_NOT_NULL(item); |
| 80 | TEST_ASSERT_EQUAL_PTR(json + 2, parse_end); |
| 81 | cJSON_Delete(item); |
| 82 | } |
| 83 | |
| 84 | static void parse_with_opts_should_parse_utf8_bom(void) |
| 85 | { |
| 86 | cJSON *with_bom = NULL; |
| 87 | cJSON *without_bom = NULL; |
| 88 | |
| 89 | with_bom = cJSON_ParseWithOpts("\xEF\xBB\xBF{}", NULL, true); |
| 90 | TEST_ASSERT_NOT_NULL(with_bom); |
| 91 | without_bom = cJSON_ParseWithOpts("{}", NULL, true); |
| 92 | TEST_ASSERT_NOT_NULL(with_bom); |
| 93 | |
| 94 | TEST_ASSERT_TRUE(cJSON_Compare(with_bom, without_bom, true)); |
| 95 | |
| 96 | cJSON_Delete(with_bom); |
| 97 | cJSON_Delete(without_bom); |
| 98 | } |
| 99 | |
| 100 | int CJSON_CDECL main(void) |
| 101 | { |
| 102 | UNITY_BEGIN(); |
| 103 | |
| 104 | RUN_TEST(parse_with_opts_should_handle_null); |
| 105 | RUN_TEST(parse_with_opts_should_handle_empty_strings); |
| 106 | RUN_TEST(parse_with_opts_should_handle_incomplete_json); |
| 107 | RUN_TEST(parse_with_opts_should_require_null_if_requested); |
| 108 | RUN_TEST(parse_with_opts_should_return_parse_end); |
| 109 | RUN_TEST(parse_with_opts_should_parse_utf8_bom); |
| 110 | |
| 111 | return UNITY_END(); |
| 112 | } |