Skip to content

Commit fc3b9c9

Browse files
committed
tests: utils: Add test case for encoding raw UTF-8 strings
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent e362186 commit fc3b9c9

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

tests/internal/utils.c

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,19 @@ void test_url_split()
203203
}
204204

205205
/* test case loop for flb_utils_write_str */
206-
static void write_str_test_cases_w_buf_size(struct write_str_case *cases, int buf_size);
206+
static void write_str_test_cases_w_buf_size(struct write_str_case *cases, int buf_size,
207+
int escape_unicode);
207208
static void write_str_test_cases(struct write_str_case *cases) {
208-
write_str_test_cases_w_buf_size(cases, 100);
209+
write_str_test_cases_w_buf_size(cases, 100, FLB_TRUE);
210+
}
211+
212+
static void write_raw_str_test_cases(struct write_str_case *cases) {
213+
write_str_test_cases_w_buf_size(cases, 100, FLB_FALSE);
209214
}
210215

211216
/* test case loop for flb_utils_write_str */
212-
static void write_str_test_cases_w_buf_size(struct write_str_case *cases, int buf_size)
217+
static void write_str_test_cases_w_buf_size(struct write_str_case *cases, int buf_size,
218+
int escape_unicode)
213219
{
214220
char *buf = flb_calloc(buf_size + 1, sizeof(char));
215221
int size = buf_size + 1;
@@ -220,7 +226,8 @@ static void write_str_test_cases_w_buf_size(struct write_str_case *cases, int bu
220226
while (!(tcase->input == 0 && tcase->output == 0)) {
221227
memset(buf, 0, size);
222228
off = 0;
223-
ret = flb_utils_write_str(buf, &off, buf_size, tcase->input, tcase->input_len, FLB_TRUE);
229+
ret = flb_utils_write_str(buf, &off, buf_size, tcase->input, tcase->input_len,
230+
escape_unicode);
224231

225232
if(!TEST_CHECK(ret == tcase->ret)) {
226233
TEST_MSG("Input string: %s", tcase->input);
@@ -388,6 +395,46 @@ void test_write_str_special_bytes()
388395
write_str_test_cases(cases);
389396
}
390397

398+
void test_write_raw_str_special_bytes()
399+
{
400+
struct write_str_case cases[] = {
401+
/*
402+
* Input: "你好世界" (12 bytes)
403+
* Output: "你好世界" (raw)
404+
*/
405+
{
406+
"\xE4\xBD\xA0\xE5\xA5\xBD\xE4\xB8\x96\xE7\x95\x8C", 12,
407+
"\xE4\xBD\xA0\xE5\xA5\xBD\xE4\xB8\x96\xE7\x95\x8C",
408+
FLB_TRUE
409+
},
410+
/*
411+
* Input: "你好我来自一个汉字文化影响的地方" (48 bytes)
412+
* Output: "你好我来自一个汉字文化影响的地方" (raw)
413+
*/
414+
{
415+
"\xE4\xBD\xA0\xE5\xA5\xBD\xE6\x88\x91\xE6\x9D\xA5\xE8\x87\xAA" \
416+
"\xE4\xB8\x80\xE4\xB8\xAA\xE6\xB1\x89\xE5\xAD\x97\xE6\x96\x87" \
417+
"\xE5\x8C\x96\xE5\xBD\xB1\xE5\x93\x8D\xE7\x9A\x84\xE5\x9C\xB0" \
418+
"\xE6\x96\xB9",
419+
48,
420+
"\xE4\xBD\xA0\xE5\xA5\xBD\xE6\x88\x91\xE6\x9D\xA5\xE8\x87\xAA" \
421+
"\xE4\xB8\x80\xE4\xB8\xAA\xE6\xB1\x89\xE5\xAD\x97\xE6\x96\x87" \
422+
"\xE5\x8C\x96\xE5\xBD\xB1\xE5\x93\x8D\xE7\x9A\x84\xE5\x9C\xB0" \
423+
"\xE6\x96\xB9",
424+
FLB_TRUE
425+
},
426+
/* Test string with a quote */
427+
{
428+
"\"hello\"", 7,
429+
"\\\"hello\\\"",
430+
FLB_TRUE
431+
},
432+
{ 0 }
433+
};
434+
435+
write_raw_str_test_cases(cases);
436+
}
437+
391438
void test_write_str_invalid_leading_byte_case_2()
392439
{
393440

@@ -472,7 +519,7 @@ void test_write_str_buffer_overrun()
472519
},
473520
{ 0 }
474521
};
475-
write_str_test_cases_w_buf_size(cases, 5);
522+
write_str_test_cases_w_buf_size(cases, 5, FLB_TRUE);
476523
}
477524

478525
struct proxy_url_check {
@@ -810,6 +857,7 @@ TEST_LIST = {
810857
{ "url_split_sds", test_url_split_sds },
811858
{ "write_str", test_write_str },
812859
{ "write_str_special_bytes", test_write_str_special_bytes },
860+
{ "write_raw_str_special_bytes", test_write_raw_str_special_bytes },
813861
{ "test_write_str_invalid_trailing_bytes", test_write_str_invalid_trailing_bytes },
814862
{ "test_write_str_invalid_leading_byte", test_write_str_invalid_leading_byte },
815863
{ "test_write_str_edge_cases", test_write_str_edge_cases },

0 commit comments

Comments
 (0)