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_string.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_string(const char *expected, const char *input) |
| 28 | { |
| 29 | unsigned char printed[1024]; |
| 30 | printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; |
| 31 | buffer.buffer = printed; |
| 32 | buffer.length = sizeof(printed); |
| 33 | buffer.offset = 0; |
| 34 | buffer.noalloc = true; |
| 35 | buffer.hooks = global_hooks; |
| 36 | |
| 37 | TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer), "Failed to print string."); |
| 38 | TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "The printed string isn't as expected."); |
| 39 | } |
| 40 | |
| 41 | static void print_string_should_print_empty_strings(void) |
| 42 | { |
| 43 | assert_print_string("\"\"", ""); |
| 44 | assert_print_string("\"\"", NULL); |
| 45 | } |
| 46 | |
| 47 | static void print_string_should_print_ascii(void) |
| 48 | { |
| 49 | char ascii[0x7F]; |
| 50 | size_t i = 1; |
| 51 | |
| 52 | /* create ascii table */ |
| 53 | for (i = 1; i < 0x7F; i++) |
| 54 | { |
| 55 | ascii[i-1] = (char)i; |
| 56 | } |
| 57 | ascii[0x7F-1] = '\0'; |
| 58 | |
| 59 | assert_print_string("\"\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\"", |
| 60 | ascii); |
| 61 | } |
| 62 | |
| 63 | static void print_string_should_print_utf8(void) |
| 64 | { |
| 65 | assert_print_string("\"ü猫慕\"", "ü猫慕"); |
| 66 | } |
| 67 | |
| 68 | int CJSON_CDECL main(void) |
| 69 | { |
| 70 | /* initialize cJSON item */ |
| 71 | UNITY_BEGIN(); |
| 72 | |
| 73 | RUN_TEST(print_string_should_print_empty_strings); |
| 74 | RUN_TEST(print_string_should_print_ascii); |
| 75 | RUN_TEST(print_string_should_print_utf8); |
| 76 | |
| 77 | return UNITY_END(); |
| 78 | } |