Skip to content

Commit

Permalink
pack: tests: Add unescaping test cases for surrogate pairs mainly emojis
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
  • Loading branch information
cosmo0920 committed Jan 6, 2025
1 parent 2d2fe7d commit 27a363e
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/internal/pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,77 @@ void test_utf8_to_json()
utf8_tests_destroy(n_tests);
}

void test_json_pack_surrogate_pairs()
{
int i;
int ret;
int len;
int type;
int items;
char *p_in;
char *p_unescaped;
size_t len_in;
char *out_buf;
size_t out_size;
char *data_in[] = {
"{\"text\":\"\\ud83e\\udd17\"}",
"{\"text\":\"thinking...\\ud83e\\uddd0\"}",
"{\"text\":\"\\ud83e\\udee1\"}",
};
char *data_unescaped[] = {
"🤗",
"thinking...🧐",
"🫡",
};
flb_sds_t d;
char tmp[32];
msgpack_unpacked result;
msgpack_object root;
msgpack_object val;
size_t off = 0;

items = sizeof(data_in) / sizeof(char *);
for (i = 0; i < items; i++) {
p_in = data_in[i];
p_unescaped = data_unescaped[i];

/* Pack raw JSON as msgpack */
ret = flb_pack_json(p_in, len_in, &out_buf, &out_size, &type, NULL);
TEST_CHECK(ret == 0);

/* Unpack 'log' value and compare it to the original raw */
off = 0;
msgpack_unpacked_init(&result);
ret = msgpack_unpack_next(&result, out_buf, out_size, &off);
TEST_CHECK(ret == MSGPACK_UNPACK_SUCCESS);

/* Check parent type is a map */
root = result.data;
TEST_CHECK(root.type == MSGPACK_OBJECT_MAP);

/* Get map value */
val = root.via.map.ptr[0].val;
TEST_CHECK(val.type == MSGPACK_OBJECT_STR);

/* Compare bytes length */
len = strlen(p_unescaped);
TEST_CHECK(len == val.via.str.size);
if (len != val.via.str.size) {
printf("failed comparing string length\n");
}

/* Compare raw bytes */
ret = memcmp(val.via.str.ptr, p_unescaped, len);
TEST_CHECK(ret == 0);
if (ret != 0) {
printf("failed comparing to original value\n");
}

msgpack_unpacked_destroy(&result);
flb_free(out_buf);
}
}

void test_json_pack_bug1278()
{
int i;
Expand Down Expand Up @@ -910,5 +981,6 @@ TEST_LIST = {

/* Mixed bytes, check JSON encoding */
{ "utf8_to_json", test_utf8_to_json},
{ "json_pack_surrogate_pairs", test_json_pack_surrogate_pairs},
{ 0 }
};

0 comments on commit 27a363e

Please sign in to comment.