增加GZIP压缩功能及对应的单元测试

This commit is contained in:
Lu Qiuwen
2018-09-28 15:05:54 +08:00
parent 5bf18848c1
commit a14b665f90
5 changed files with 208 additions and 53 deletions

View File

@@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include <http_convert.h>
#include <event.h>
unsigned char __64x[] = {
0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58,
@@ -157,7 +158,7 @@ protected:
__ctx->data = new unsigned char[2048];
__ctx->sz_data = 0;
cv_object = hf_content_converter_create(HF_CONTENT_CONV_UNCOMPRASS,
cv_object = hf_content_uncompress_create(
HTTP_ACCEPT_ENCODING_GZIP, __gzip_64x_data_callback, __ctx);
ASSERT_TRUE(cv_object != NULL);
@@ -167,18 +168,18 @@ protected:
{
delete[] __ctx->data;
delete __ctx;
hf_content_converter_destroy(cv_object);
hf_content_uncompress_destroy(cv_object);
}
protected:
struct callback_ctx * __ctx{};
struct hf_content_converter * cv_object{};
struct hf_content_uncompress * cv_object{};
};
TEST_F(HttpConvertUncompress, Gzip64x)
{
int ret = hf_content_converter_write(cv_object, NULL, EV_HTTP_REQ_BODY_CONT,
(const unsigned char *)__64x_gz, (size_t)__64x_gz_len);
int ret = hf_content_uncompress_write(cv_object, NULL, EV_HTTP_REQ_BODY_CONT,
(const unsigned char *) __64x_gz, (size_t) __64x_gz_len);
EXPECT_EQ(ret, 1);
EXPECT_EQ(__ctx->sz_data, __64x_len);
@@ -187,8 +188,8 @@ TEST_F(HttpConvertUncompress, Gzip64x)
TEST_F(HttpConvertUncompress, GzipMonkey)
{
int ret = hf_content_converter_write(cv_object, NULL, EV_HTTP_REQ_BODY_CONT,
(const unsigned char *)monkey_gz, (size_t)monkey_gz_len);
int ret = hf_content_uncompress_write(cv_object, NULL, EV_HTTP_REQ_BODY_CONT,
(const unsigned char *) monkey_gz, (size_t) monkey_gz_len);
EXPECT_EQ(ret, 1);
EXPECT_EQ(__ctx->sz_data, monkey_len);
@@ -200,18 +201,91 @@ TEST_F(HttpConvertUncompress, GzipMonkeyFrag)
unsigned frag_length = monkey_gz_len / 2;
int ret = 0;
ret = hf_content_converter_write(cv_object, NULL, EV_HTTP_REQ_BODY_CONT,
(const unsigned char *)monkey_gz, frag_length);
ret = hf_content_uncompress_write(cv_object, NULL, EV_HTTP_REQ_BODY_CONT,
(const unsigned char *) monkey_gz, frag_length);
EXPECT_EQ(ret, 0);
ret = hf_content_converter_write(cv_object, NULL, EV_HTTP_REQ_BODY_CONT,
(const unsigned char *)monkey_gz + frag_length, monkey_gz_len - frag_length);
ret = hf_content_uncompress_write(cv_object, NULL, EV_HTTP_REQ_BODY_CONT,
(const unsigned char *) monkey_gz + frag_length, monkey_gz_len - frag_length);
EXPECT_EQ(ret, 1);
EXPECT_EQ(__ctx->sz_data, monkey_len);
EXPECT_EQ(memcmp(__ctx->data, monkey, monkey_len), 0);
}
class HttpConvertCompress : public ::testing::Test
{
protected:
static constexpr unsigned int __encode = HTTP_ACCEPT_ENCODING_GZIP;
void SetUp() override
{
cv_compress_object = hf_content_compress_create(__encode);
cv_uncompress_object = hf_content_uncompress_create(__encode, __gzip_uncompress_callback, &uncompress_buf);
ASSERT_TRUE(cv_compress_object != NULL);
}
void TearDown() override
{
hf_content_compress_destroy(cv_compress_object);
evbuffer_free(buf);
}
protected:
struct hf_content_compress * cv_compress_object{};
struct hf_content_uncompress * cv_uncompress_object{};
struct evbuffer * buf{evbuffer_new()};
std::vector<char> uncompress_buf;
protected:
static int __gzip_uncompress_callback(struct http_half_private * hf_private,
tfe_http_event ev, const unsigned char * data, size_t len, void * user)
{
auto * __uncompress_buf = static_cast<std::vector<char> *>(user);
__uncompress_buf->insert(__uncompress_buf->end(), data, data + len);
}
};
TEST_F(HttpConvertCompress, MonkeyToGzip)
{
int ret = hf_content_compress_write(cv_compress_object, monkey, sizeof(monkey), buf, 1);
ASSERT_EQ(ret, 0);
unsigned char * __raw_buf_ptr = evbuffer_pullup(buf, -1);
size_t __raw_buf_length = evbuffer_get_length(buf);
ret = hf_content_uncompress_write(cv_uncompress_object, NULL, EV_HTTP_REQ_BODY_CONT,
__raw_buf_ptr, __raw_buf_length);
ASSERT_TRUE(__raw_buf_ptr != NULL);
ASSERT_EQ(ret, 1);
EXPECT_EQ(uncompress_buf.size(), sizeof(monkey));
EXPECT_EQ(memcmp(uncompress_buf.data(), monkey, sizeof(monkey)), 0);
}
TEST_F(HttpConvertCompress, MonkeyToGzipStrem)
{
unsigned frag_length = sizeof(monkey) / 2;
/* First frag */
int ret = hf_content_compress_write(cv_compress_object, monkey, frag_length, buf, 0);
ASSERT_EQ(ret, 0);
/* Last frag */
ret = hf_content_compress_write(cv_compress_object, monkey + frag_length, sizeof(monkey) - frag_length, buf, 1);
ASSERT_EQ(ret, 0);
unsigned char * __raw_buf_ptr = evbuffer_pullup(buf, -1);
size_t __raw_buf_length = evbuffer_get_length(buf);
ret = hf_content_uncompress_write(cv_uncompress_object, NULL, EV_HTTP_REQ_BODY_CONT,
__raw_buf_ptr, __raw_buf_length);
ASSERT_TRUE(__raw_buf_ptr != NULL);
ASSERT_EQ(ret, 1);
EXPECT_EQ(uncompress_buf.size(), sizeof(monkey));
EXPECT_EQ(memcmp(uncompress_buf.data(), monkey, sizeof(monkey)), 0);
}
void tfe_stream_write_access_log(const struct tfe_stream * stream, int level, const char * fmt, ...)
{
return;