TSG-20527 http2解析层支持zstd编解码

This commit is contained in:
fengweihao
2024-04-28 15:56:01 +08:00
parent 93c626de6d
commit 397a6aba93
9 changed files with 824 additions and 264 deletions

View File

@@ -337,13 +337,13 @@ TEST(UI_TEST_INFLATE_GZIP, inflate_01)
{
char *uncompr = NULL;
int ret = 0, uncompr_len = 0;
struct z_stream_st *inflate = NULL;
ret = inflate_read(ut_gip_01, sizeof(ut_gip_01), &uncompr, &uncompr_len, &inflate, 2);
struct http2_codec_ctx *inflate = NULL;
ret = http2_decompress_stream(ut_gip_01, sizeof(ut_gip_01), &uncompr, &uncompr_len, &inflate, 2);
EXPECT_EQ(ret, Z_STREAM_END);
EXPECT_EQ(uncompr_len, sizeof(ut_ungip_01));
EXPECT_EQ(memcmp(uncompr, ut_ungip_01, sizeof(ut_ungip_01)), 0);
inflate_finished(&inflate);
http2_decompress_finished(&inflate);
free(uncompr);
uncompr = NULL;
@@ -351,17 +351,17 @@ TEST(UI_TEST_INFLATE_GZIP, inflate_01)
TEST(UI_TEST_INFLATE_GZIP, inflate_02)
{
struct z_stream_st *inflate = NULL;
struct http2_codec_ctx *inflate = NULL;
int ret = 0, half = 0;
int size = sizeof(ut_gip_01);
half = size / 2;
char *uncompr1 = NULL; int uncompr_len1 = 0;
ret = inflate_read(ut_gip_01, half, &uncompr1, &uncompr_len1, &inflate, 2);
ret = http2_decompress_stream(ut_gip_01, half, &uncompr1, &uncompr_len1, &inflate, 2);
EXPECT_EQ(ret, Z_OK);
char *uncompr2 = NULL; int uncompr_len2 = 0;
ret = inflate_read(ut_gip_01 + half, size - half, &uncompr2, &uncompr_len2, &inflate, 2);
ret = http2_decompress_stream(ut_gip_01 + half, size - half, &uncompr2, &uncompr_len2, &inflate, 2);
EXPECT_EQ(ret, Z_STREAM_END);
char uncompr[1024] = {0};
@@ -370,7 +370,7 @@ TEST(UI_TEST_INFLATE_GZIP, inflate_02)
EXPECT_EQ(uncompr_len1 + uncompr_len2, sizeof(ut_ungip_01));
EXPECT_EQ(memcmp(uncompr, ut_ungip_01, sizeof(ut_ungip_01)), 0);
inflate_finished(&inflate);
http2_decompress_finished(&inflate);
free(uncompr1);
uncompr1 = NULL;
free(uncompr2);
@@ -384,23 +384,23 @@ TEST(UI_TEST_DEFLATE_GZIP, deflate_01)
struct evbuffer * buf = evbuffer_new();
int size = sizeof(ut_ungip_01);
struct z_stream_st *deflate = NULL;
ret = deflate_write(&deflate, ut_ungip_01, size, buf, 2, 1);
struct http2_codec_ctx *deflate = NULL;
ret = http2_compress_stream(&deflate, ut_ungip_01, size, buf, 2, 1);
EXPECT_EQ(ret, Z_OK);
deflate_finished(&deflate);
http2_compress_finished(&deflate);
dest = evbuffer_pullup(buf, -1);
dlen = evbuffer_get_length(buf);
char *uncompr = NULL;
int uncompr_len = 0;
struct z_stream_st *inflate = NULL;
ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &inflate, 2);
struct http2_codec_ctx *inflate = NULL;
ret = http2_decompress_stream(dest, dlen, &uncompr, &uncompr_len, &inflate, 2);
EXPECT_EQ(ret, Z_STREAM_END);
EXPECT_EQ(uncompr_len, sizeof(ut_ungip_01));
EXPECT_EQ(memcmp(uncompr, ut_ungip_01, sizeof(ut_ungip_01)), 0);
inflate_finished(&inflate);
http2_decompress_finished(&inflate);
free(uncompr);
uncompr = NULL;
evbuffer_free(buf);
@@ -415,31 +415,31 @@ TEST(UI_TEST_DEFLATE_GZIP, deflate_02)
half = size / 2;
struct evbuffer * buf = evbuffer_new();
struct z_stream_st *deflate = NULL;
struct http2_codec_ctx *deflate = NULL;
/* First frag */
ret = deflate_write(&deflate, ut_ungip_01, half, buf, 2, 0);
ret = http2_compress_stream(&deflate, ut_ungip_01, half, buf, 2, 0);
EXPECT_EQ(ret, Z_OK);
/* Last frag */
ret = deflate_write(&deflate, ut_ungip_01 + half, size - half, buf, 2, 0);
ret = http2_compress_stream(&deflate, ut_ungip_01 + half, size - half, buf, 2, 0);
EXPECT_EQ(ret, Z_OK);
/* End frag */
ret = deflate_write(&deflate, NULL, 0, buf, 2, 1);
ret = http2_compress_stream(&deflate, NULL, 0, buf, 2, 1);
EXPECT_EQ(ret, Z_OK);
deflate_finished(&deflate);
http2_compress_finished(&deflate);
dest = evbuffer_pullup(buf, -1);
dlen = evbuffer_get_length(buf);
char *uncompr = NULL;
int uncompr_len = 0;
struct z_stream_st *inflate = NULL;
ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &inflate, 2);
struct http2_codec_ctx *inflate = NULL;
ret = http2_decompress_stream(dest, dlen, &uncompr, &uncompr_len, &inflate, 2);
EXPECT_EQ(ret, Z_STREAM_END);
EXPECT_EQ(uncompr_len, sizeof(ut_ungip_01));
EXPECT_EQ(memcmp(uncompr, ut_ungip_01, sizeof(ut_ungip_01)), 0);
inflate_finished(&inflate);
http2_decompress_finished(&inflate);
free(uncompr);
uncompr = NULL;
@@ -453,22 +453,22 @@ TEST(UI_TEST_DEFLATE_BR, deflate_01)
struct evbuffer * buf = evbuffer_new();
int size = sizeof(ut_ungip_01);
struct z_stream_st *deflate = NULL;
ret = deflate_write(&deflate, ut_ungip_01, size, buf, HTTP2_CONTENT_ENCODING_BR, 1);
struct http2_codec_ctx *deflate = NULL;
ret = http2_compress_stream(&deflate, ut_ungip_01, size, buf, HTTP2_CONTENT_ENCODING_BR, 1);
EXPECT_EQ(ret, Z_OK);
deflate_finished(&deflate);
http2_compress_finished(&deflate);
dest = evbuffer_pullup(buf, -1);
dlen = evbuffer_get_length(buf);
char *uncompr = NULL;
int uncompr_len = 0;
struct z_stream_st *inflate = NULL;
ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &inflate, HTTP2_CONTENT_ENCODING_BR);
struct http2_codec_ctx *inflate = NULL;
ret = http2_decompress_stream(dest, dlen, &uncompr, &uncompr_len, &inflate, HTTP2_CONTENT_ENCODING_BR);
EXPECT_EQ(ret, Z_STREAM_END);
EXPECT_EQ(uncompr_len, sizeof(ut_ungip_01));
EXPECT_EQ(memcmp(uncompr, ut_ungip_01, sizeof(ut_ungip_01)), 0);
inflate_finished(&inflate);
http2_decompress_finished(&inflate);
free(uncompr);
uncompr = NULL;
@@ -484,37 +484,272 @@ TEST(UI_TEST_DEFLATE_BR, deflate_02)
half = size / 2;
struct evbuffer * buf = evbuffer_new();
struct z_stream_st *deflate = NULL;
struct http2_codec_ctx *deflate = NULL;
/* First frag */
ret = deflate_write(&deflate, ut_ungip_01, half, buf, HTTP2_CONTENT_ENCODING_BR, 0);
ret = http2_compress_stream(&deflate, ut_ungip_01, half, buf, HTTP2_CONTENT_ENCODING_BR, 0);
EXPECT_EQ(ret, Z_OK);
/* Last frag */
ret = deflate_write(&deflate, ut_ungip_01 + half, size - half, buf, HTTP2_CONTENT_ENCODING_BR, 0);
ret = http2_compress_stream(&deflate, ut_ungip_01 + half, size - half, buf, HTTP2_CONTENT_ENCODING_BR, 0);
EXPECT_EQ(ret, Z_OK);
/* End frag */
ret = deflate_write(&deflate, NULL, 0, buf, HTTP2_CONTENT_ENCODING_BR, 1);
ret = http2_compress_stream(&deflate, NULL, 0, buf, HTTP2_CONTENT_ENCODING_BR, 1);
EXPECT_EQ(ret, Z_OK);
deflate_finished(&deflate);
http2_compress_finished(&deflate);
dest = evbuffer_pullup(buf, -1);
dlen = evbuffer_get_length(buf);
char *uncompr = NULL;
int uncompr_len = 0;
struct z_stream_st *inflate = NULL;
ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &inflate, HTTP2_CONTENT_ENCODING_BR);
struct http2_codec_ctx *inflate = NULL;
ret = http2_decompress_stream(dest, dlen, &uncompr, &uncompr_len, &inflate, HTTP2_CONTENT_ENCODING_BR);
EXPECT_EQ(ret, Z_STREAM_END);
EXPECT_EQ(uncompr_len, sizeof(ut_ungip_01));
EXPECT_EQ(memcmp(uncompr, ut_ungip_01, sizeof(ut_ungip_01)), 0);
inflate_finished(&inflate);
http2_decompress_finished(&inflate);
free(uncompr);
uncompr = NULL;
evbuffer_free(buf);
}
TEST(UI_TEST_INFLATE_ZSTD, Decompress_Facebook_Manifest)
{
char *output = NULL;
int ret = 0, output_len = 0;
struct http2_codec_ctx *codec_ctx = NULL;
ret = http2_decompress_stream(facebook_response_zstd_body, sizeof(facebook_response_zstd_body), &output, &output_len, &codec_ctx, HTTP2_CONTENT_ENCODING_ZSTD);
printf("output = %s\n", output);
EXPECT_EQ(ret, 1);
EXPECT_EQ(output_len, strlen(facebook_response_text_body));
EXPECT_EQ(memcmp(output, facebook_response_text_body, strlen(facebook_response_text_body)), 0);
http2_decompress_finished(&codec_ctx);
free(output);
output = NULL;
}
#if 0
TEST(UI_TEST_INFLATE_ZSTD, Decompress_Facebook_Index)
{
char *output = NULL;
int ret = 0, output_len = 0;
struct http2_codec_ctx *codec_ctx = NULL;
ret = http2_decompress_stream(facebook_index_zstd_body, sizeof(facebook_index_zstd_body), &output, &output_len, &codec_ctx, HTTP2_CONTENT_ENCODING_ZSTD);
printf("output = %s\n", output);
EXPECT_EQ(ret, 1);
//EXPECT_EQ(output_len, strlen(facebook_response_text_body));
//EXPECT_EQ(memcmp(output, facebook_response_text_body, strlen(facebook_response_text_body)), 0);
http2_compress_finished(&codec_ctx);
free(output);
output = NULL;
}
#endif
TEST(UI_TEST_INFLATE_ZSTD, Compress_AND_Decompress_Facebook_Manifest)
{
unsigned char *encode_str = NULL;
int ret = 0, encode_len = 0;
struct evbuffer *encode_buf = evbuffer_new();
int input_len = strlen(facebook_response_text_body);
struct http2_codec_ctx *codec_ctx_ecode = NULL;
ret = http2_compress_stream(&codec_ctx_ecode, (const uint8_t *)facebook_response_text_body, input_len, encode_buf, HTTP2_CONTENT_ENCODING_ZSTD, 1);
EXPECT_EQ(ret, Z_OK);
http2_compress_finished(&codec_ctx_ecode);
encode_str = evbuffer_pullup(encode_buf, -1);
encode_len = evbuffer_get_length(encode_buf) + 1;
char *output = NULL;
int output_len = 0;
struct http2_codec_ctx *codec_ctx_decode = NULL;
ret = http2_decompress_stream(encode_str, encode_len, &output, &output_len, &codec_ctx_decode, HTTP2_CONTENT_ENCODING_ZSTD);
printf("output = %s\n", output);
EXPECT_EQ(ret, 1);
EXPECT_EQ(output_len, strlen(facebook_response_text_body));
EXPECT_EQ(memcmp(output, facebook_response_text_body, strlen(facebook_response_text_body)), 0);
http2_decompress_finished(&codec_ctx_decode);
free(output);
output = NULL;
evbuffer_free(encode_buf);
}
TEST(UI_TEST_INFLATE_ZSTD, Compress_Facebook_Manifest_MORE_TIMES)
{
unsigned char *encode_str = NULL;
int ret = 0, half_len = 0, encode_len = 0;
int text_len = strlen(facebook_response_text_body);
half_len = text_len / 2;
struct evbuffer *encode_buf = evbuffer_new();
struct http2_codec_ctx *codec_ctx = NULL;
/* First frag */
ret = http2_compress_stream(&codec_ctx, (const uint8_t *)facebook_response_text_body, half_len, encode_buf, HTTP2_CONTENT_ENCODING_ZSTD, 0);
EXPECT_EQ(ret, 0);
/* Last frag */
ret = http2_compress_stream(&codec_ctx, (const uint8_t *)facebook_response_text_body + half_len, text_len - half_len, encode_buf, HTTP2_CONTENT_ENCODING_ZSTD, 0);
EXPECT_EQ(ret, 0);
/* End frag */
ret = http2_compress_stream(&codec_ctx, NULL, 0, encode_buf, HTTP2_CONTENT_ENCODING_ZSTD, 1);
EXPECT_EQ(ret, 0);
http2_compress_finished(&codec_ctx);
encode_str = evbuffer_pullup(encode_buf, -1);
encode_len = evbuffer_get_length(encode_buf);
printf("encode_len = %d\n", encode_len);
char *output = NULL;
int output_len = 0;
ret = http2_decompress_stream(encode_str, encode_len, &output, &output_len, &codec_ctx, HTTP2_CONTENT_ENCODING_ZSTD);
printf("output = %s, ret =%d\n", output, ret);
EXPECT_EQ(ret, 1);
EXPECT_EQ(output_len, strlen(facebook_response_text_body));
EXPECT_EQ(memcmp(output, facebook_response_text_body, strlen(facebook_response_text_body)), 0);
http2_decompress_finished(&codec_ctx);
free(output);
output = NULL;
evbuffer_free(encode_buf);
}
static int read_file(const char *filename, char **input)
{
FILE* fp=NULL;
struct stat file_info;
stat(filename, &file_info);
size_t input_sz=file_info.st_size;
fp=fopen(filename,"r");
if(fp==NULL)
{
return 0;
}
*input=(char*)malloc(input_sz);
fread(*input,1,input_sz,fp);
fclose(fp);
return input_sz;
}
static int write_file(const char *outfile, char *output, int out_len)
{
FILE* fp=NULL;
fp=fopen(outfile,"wb");
if(fp==NULL)
{
return 0;
}
size_t writtenSize = fwrite(output, 1, out_len, fp);
fclose(fp);
return writtenSize;
}
TEST(UI_TEST_INFLATE_ZSTD, Compress_Manual_Html_Simple)
{
int ret=0, encode_len=0;
char *input=0; size_t input_sz=0;
unsigned char *encode_str = NULL;
struct http2_codec_ctx *codec_ctx = NULL;
struct evbuffer *encode_buf = evbuffer_new();
const char* filename="./zstd_manual_simple.html";
const char *outfile="./zstd_manual_simple_test.html";
input_sz = read_file(filename, &input);
if(input_sz !=0 || input != NULL)
{
ret = http2_compress_stream(&codec_ctx, (const uint8_t *)input, input_sz, encode_buf, HTTP2_CONTENT_ENCODING_ZSTD, 1);
EXPECT_EQ(ret, 0);
http2_compress_finished(&codec_ctx);
encode_str = evbuffer_pullup(encode_buf, -1);
encode_len = evbuffer_get_length(encode_buf);
printf("encode_len = %d\n", encode_len);
char *output = NULL;
int output_len = 0;
ret = http2_decompress_stream(encode_str, encode_len, &output, &output_len, &codec_ctx, HTTP2_CONTENT_ENCODING_ZSTD);
printf("output_len = %d, ret =%d\n", output_len, ret);
write_file(outfile, output, output_len);
http2_decompress_finished(&codec_ctx);
free(input);
free(output);
evbuffer_free(encode_buf);
}
}
TEST(UI_TEST_INFLATE_ZSTD, Compress_Manual_Html_Large)
{
int ret=0, encode_len=0;
char *input=0; size_t input_sz=0;
unsigned char *encode_str = NULL;
struct http2_codec_ctx *codec_ctx = NULL;
struct evbuffer *encode_buf = evbuffer_new();
const char *inputfile="./zstd_manual_large.html";
const char *outfile="./zstd_manual_large_test.html";
input_sz = read_file(inputfile, &input);
if(input_sz !=0 || input != NULL)
{
ret = http2_compress_stream(&codec_ctx, (const uint8_t *)input, input_sz, encode_buf, HTTP2_CONTENT_ENCODING_ZSTD, 1);
EXPECT_EQ(ret, 0);
http2_compress_finished(&codec_ctx);
encode_str = evbuffer_pullup(encode_buf, -1);
encode_len = evbuffer_get_length(encode_buf);
printf("encode_len = %d\n", encode_len);
char *output = NULL;
int output_len = 0;
ret = http2_decompress_stream(encode_str, encode_len, &output, &output_len, &codec_ctx, HTTP2_CONTENT_ENCODING_ZSTD);
printf("output_len = %d, ret =%d\n", output_len, ret);
write_file(outfile, output, output_len);
http2_decompress_finished(&codec_ctx);
free(input);
free(output);
evbuffer_free(encode_buf);
}
}
#if 0
TEST(UI_TEST_INFLATE_ZSTD, Decompress_Facebook_Manifest_Half)
{
struct http2_codec_ctx *codec_ctx = NULL;
int ret = 0, half = 0;
int size = sizeof(facebook_response_zstd_body);
half = size / 2;
char *output1 = NULL; int output_len1 = 0;
ret = http2_decompress_stream(ut_gip_01, half, &output1, &output_len1, &codec_ctx, HTTP2_CONTENT_ENCODING_ZSTD);
EXPECT_EQ(ret, 1);
char *output2 = NULL; int output_len2 = 0;
ret = http2_decompress_stream(ut_gip_01 + half, size - half, &output2, &output_len2, &codec_ctx, HTTP2_CONTENT_ENCODING_ZSTD);
EXPECT_EQ(ret, 1);
char output[4096] = {0};
memcpy(output, output1, output_len1);
memcpy(output + output_len1, output2, output_len2);
EXPECT_EQ(output_len1 + output_len2, strlen(facebook_response_text_body));
EXPECT_EQ(memcmp(output, facebook_response_text_body, strlen(facebook_response_text_body)), 0);
http2_compress_finished(&codec_ctx);
free(output1);
output1 = NULL;
free(output2);
output2 = NULL;
}
#endif
int main(int argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);