Skip to content

Commit 8bdcda8

Browse files
cosmo0920edsiper
authored andcommitted
pack: tests: Add test cases for INT_MAX and UINT_MAX cases for json packing
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 746624d commit 8bdcda8

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tests/internal/pack.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,64 @@ void test_json_invalid()
10451045
TEST_CHECK(buffer == NULL);
10461046
}
10471047

1048+
void test_json_pack_large_uint64()
1049+
{
1050+
int i;
1051+
int ret;
1052+
int type;
1053+
size_t off;
1054+
char *out_buf = NULL;
1055+
size_t out_size;
1056+
msgpack_unpacked result;
1057+
msgpack_object root;
1058+
msgpack_object val;
1059+
char *p_in;
1060+
size_t len_in = 0;
1061+
uint64_t expected = 0;
1062+
1063+
struct {
1064+
const char *json_str;
1065+
uint64_t expected_val;
1066+
} test_cases[] = {
1067+
{"{\"key\": 9223372036854775808}", 9223372036854775808ULL},
1068+
{"{\"key\": 18446744073709551615}", 18446744073709551615ULL}
1069+
};
1070+
1071+
for (i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
1072+
p_in = test_cases[i].json_str;
1073+
len_in = strlen(p_in);
1074+
expected = test_cases[i].expected_val;
1075+
1076+
ret = flb_pack_json(p_in, len_in, &out_buf, &out_size, &type, NULL);
1077+
TEST_CHECK(ret == 0);
1078+
if (!TEST_CHECK(out_buf != NULL)) {
1079+
continue;
1080+
}
1081+
1082+
off = 0;
1083+
msgpack_unpacked_init(&result);
1084+
ret = msgpack_unpack_next(&result, out_buf, out_size, &off);
1085+
TEST_CHECK(ret == MSGPACK_UNPACK_SUCCESS);
1086+
1087+
root = result.data;
1088+
TEST_CHECK(root.type == MSGPACK_OBJECT_MAP);
1089+
1090+
val = root.via.map.ptr[0].val;
1091+
if (!TEST_CHECK(val.type == MSGPACK_OBJECT_POSITIVE_INTEGER)) {
1092+
TEST_MSG("Test %d: Type mismatch, expected POSITIVE_INTEGER, got %d", i, val.type);
1093+
}
1094+
1095+
if (!TEST_CHECK(val.via.u64 == expected)) {
1096+
TEST_MSG("Test %d: Value mismatch.\nExpected: %"PRIu64"\nGot: %"PRIu64,
1097+
i, expected, val.via.u64);
1098+
}
1099+
1100+
msgpack_unpacked_destroy(&result);
1101+
flb_free(out_buf);
1102+
out_buf = NULL;
1103+
}
1104+
}
1105+
10481106
TEST_LIST = {
10491107
/* JSON maps iteration */
10501108
{ "json_pack" , test_json_pack },
@@ -1063,6 +1121,7 @@ TEST_LIST = {
10631121
{ "json_date_epoch" , test_json_date_epoch},
10641122
{ "json_date_epoch_ms" , test_json_date_epoch_ms},
10651123
{ "json_invalid", test_json_invalid},
1124+
{ "json_pack_large_uint64", test_json_pack_large_uint64},
10661125

10671126
/* Mixed bytes, check JSON encoding */
10681127
{ "utf8_to_json", test_utf8_to_json},

0 commit comments

Comments
 (0)