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 / print_object.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 assert_print_object(const char * const expected, const char * const input) |
| 28 | { |
| 29 | unsigned char printed_unformatted[1024]; |
| 30 | unsigned char printed_formatted[1024]; |
| 31 | |
| 32 | cJSON item[1]; |
| 33 | |
| 34 | printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; |
| 35 | printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; |
| 36 | parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; |
| 37 | |
| 38 | /* buffer for parsing */ |
| 39 | parsebuffer.content = (const unsigned char*)input; |
| 40 | parsebuffer.length = strlen(input) + sizeof(""); |
| 41 | parsebuffer.hooks = global_hooks; |
| 42 | |
| 43 | /* buffer for formatted printing */ |
| 44 | formatted_buffer.buffer = printed_formatted; |
| 45 | formatted_buffer.length = sizeof(printed_formatted); |
| 46 | formatted_buffer.offset = 0; |
| 47 | formatted_buffer.noalloc = true; |
| 48 | formatted_buffer.hooks = global_hooks; |
| 49 | |
| 50 | /* buffer for unformatted printing */ |
| 51 | unformatted_buffer.buffer = printed_unformatted; |
| 52 | unformatted_buffer.length = sizeof(printed_unformatted); |
| 53 | unformatted_buffer.offset = 0; |
| 54 | unformatted_buffer.noalloc = true; |
| 55 | unformatted_buffer.hooks = global_hooks; |
| 56 | |
| 57 | memset(item, 0, sizeof(item)); |
| 58 | TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer), "Failed to parse object."); |
| 59 | |
| 60 | unformatted_buffer.format = false; |
| 61 | TEST_ASSERT_TRUE_MESSAGE(print_object(item, &unformatted_buffer), "Failed to print unformatted string."); |
| 62 | TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted object is not correct."); |
| 63 | |
| 64 | formatted_buffer.format = true; |
| 65 | TEST_ASSERT_TRUE_MESSAGE(print_object(item, &formatted_buffer), "Failed to print formatted string."); |
| 66 | TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted object is not correct."); |
| 67 | |
| 68 | reset(item); |
| 69 | } |
| 70 | |
| 71 | static void print_object_should_print_empty_objects(void) |
| 72 | { |
| 73 | assert_print_object("{\n}", "{}"); |
| 74 | } |
| 75 | |
| 76 | static void print_object_should_print_objects_with_one_element(void) |
| 77 | { |
| 78 | |
| 79 | assert_print_object("{\n\t\"one\":\t1\n}", "{\"one\":1}"); |
| 80 | assert_print_object("{\n\t\"hello\":\t\"world!\"\n}", "{\"hello\":\"world!\"}"); |
| 81 | assert_print_object("{\n\t\"array\":\t[]\n}", "{\"array\":[]}"); |
| 82 | assert_print_object("{\n\t\"null\":\tnull\n}", "{\"null\":null}"); |
| 83 | } |
| 84 | |
| 85 | static void print_object_should_print_objects_with_multiple_elements(void) |
| 86 | { |
| 87 | assert_print_object("{\n\t\"one\":\t1,\n\t\"two\":\t2,\n\t\"three\":\t3\n}", "{\"one\":1,\"two\":2,\"three\":3}"); |
| 88 | assert_print_object("{\n\t\"one\":\t1,\n\t\"NULL\":\tnull,\n\t\"TRUE\":\ttrue,\n\t\"FALSE\":\tfalse,\n\t\"array\":\t[],\n\t\"world\":\t\"hello\",\n\t\"object\":\t{\n\t}\n}", "{\"one\":1,\"NULL\":null,\"TRUE\":true,\"FALSE\":false,\"array\":[],\"world\":\"hello\",\"object\":{}}"); |
| 89 | } |
| 90 | |
| 91 | int CJSON_CDECL main(void) |
| 92 | { |
| 93 | /* initialize cJSON item */ |
| 94 | UNITY_BEGIN(); |
| 95 | |
| 96 | RUN_TEST(print_object_should_print_empty_objects); |
| 97 | RUN_TEST(print_object_should_print_objects_with_one_element); |
| 98 | RUN_TEST(print_object_should_print_objects_with_multiple_elements); |
| 99 | |
| 100 | return UNITY_END(); |
| 101 | } |