Skip to content

Commit

Permalink
tests: internal: utils: adjusts expected utf8 encoded bytes
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Dec 4, 2024
1 parent ca70432 commit d06b3fd
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions tests/internal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
},
/*
Expand All @@ -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
},
/*
Expand All @@ -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 }
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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 },
Expand Down

0 comments on commit d06b3fd

Please sign in to comment.