diff --git a/tests/internal/utils.c b/tests/internal/utils.c index f55a3672c37..fb9f8c1f2a1 100644 --- a/tests/internal/utils.c +++ b/tests/internal/utils.c @@ -127,7 +127,8 @@ static void write_str_test_cases(struct write_str_case *cases) { } /* test case loop for flb_utils_write_str */ -static void write_str_test_cases_w_buf_size(struct write_str_case *cases, int buf_size) { +static void write_str_test_cases_w_buf_size(struct write_str_case *cases, int buf_size) +{ char *buf = flb_calloc(buf_size + 1, sizeof(char)); int size = buf_size + 1; int off; @@ -170,11 +171,13 @@ static void write_str_test_cases_w_buf_size(struct write_str_case *cases, int bu void test_write_str() { - char buf[10]; - char japanese_a[4] = {0xe3, 0x81, 0x82}; - int size = sizeof(buf); int off; int ret; + char buf[10] = {0}; + int size = sizeof(buf); + + /* escaped Unicode representation of あ */ + char jp_expected_output[] = "\\u3042"; off = 0; ret = flb_utils_write_str(buf, &off, size, "a", 1); @@ -189,15 +192,16 @@ void test_write_str() off = 0; ret = flb_utils_write_str(buf, &off, size, "\xe3\x81\x82", 3); TEST_CHECK(ret == FLB_TRUE); - TEST_CHECK(memcmp(buf, japanese_a, off) == 0); + TEST_CHECK(memcmp(buf, jp_expected_output, off) == 0); - // Truncated bytes + /* Truncated bytes: 'buf' should not be touched and off == 0 */ off = 0; ret = flb_utils_write_str(buf, &off, size, "\xe3\x81\x82\xe3", 1); TEST_CHECK(ret == FLB_TRUE); - TEST_CHECK(memcmp(buf, japanese_a, off) == 0); + TEST_CHECK(off == 0); + TEST_CHECK(memcmp(buf, jp_expected_output, off) == 0); - // Error: buffer too small + /* Error: buffer too small */ off = 0; ret = flb_utils_write_str(buf, &off, size, "aaaaaaaaaaa", 11); TEST_CHECK(ret == FLB_FALSE); @@ -238,7 +242,7 @@ void test_write_str_invalid_leading_byte() */ { "\x00\x01\xe3\x81\x82""abc", 8, /* note that 0x01 is an invalid byte */ - "\\u0000\\u0001""\xe3\x81\x82""abc", /* escape hex */ + "\\u0000\\u0001\\u3042""abc", /* escape hex */ FLB_TRUE }, /* @@ -252,7 +256,7 @@ void test_write_str_invalid_leading_byte() "\xee\x83\xb3" /* f3 fragment */ /* replace invalid unicode */ "\xee\x82\x81" /* 81 fragment */ "\xee\x82\x81" /* 81 fragment */ - "\xe3\x81\x82""abc", /* valid unicode */ + "\\u3042""abc", /* valid unicode */ FLB_TRUE }, /* @@ -263,7 +267,25 @@ void test_write_str_invalid_leading_byte() "\xf3\x81\x01\xe3\x81\x82""abc", 9, /* note that 0x01 is an invalid byte */ "\xee\x83\xb3" /* f3 fragment */ /* replace invalid unicode */ "\xee\x82\x81" /* 81 fragment */ - "\\u0001""\xe3\x81\x82""abc", + "\\u0001""\\u3042""abc", + FLB_TRUE + }, + { 0 } + }; + + write_str_test_cases(cases); +} + +void test_write_str_special_bytes() +{ + + struct write_str_case cases[] = { + /* + * Escaped leading hex (two hex, one valid unicode) + */ + { + "你好世界", 12, + "\\u4f60\\u597d\\u4e16\\u754c", FLB_TRUE }, { 0 } @@ -342,10 +364,10 @@ void test_write_str_buffer_overrun() FLB_FALSE }, { - "\"" + "a" "\xe3\x81\x82", 4, /* valid unicode */ - "\\\"""\xe3\x81\x82", /* just enough space for valid unicode */ - FLB_TRUE + "a", /* just enough space for valid ascii */ + FLB_FALSE /* no space for \u3042 */ }, { "\x81" @@ -662,7 +684,7 @@ struct size_to_bytes_check size_to_bytes_checks[] = { {"9223372036.78G", -1}, }; -void test_size_to_bytes() +void test_size_to_bytes() { int i; int size; @@ -682,6 +704,7 @@ TEST_LIST = { /* JSON maps iteration */ { "url_split", test_url_split }, { "write_str", test_write_str }, + { "write_str_special_bytes", test_write_str_special_bytes }, { "test_write_str_invalid_trailing_bytes", test_write_str_invalid_trailing_bytes }, { "test_write_str_invalid_leading_byte", test_write_str_invalid_leading_byte }, { "test_write_str_edge_cases", test_write_str_edge_cases },