Further coverage for compression

pull/4381/head
David Garcia Quintas 9 years ago
parent 3ff5d2dd6b
commit e9fa31141d
  1. 10
      src/core/compression/message_compress.c
  2. 40
      test/core/compression/message_compress_test.c

@ -111,10 +111,7 @@ static int zlib_compress(gpr_slice_buffer* input, gpr_slice_buffer* output,
zs.zfree = zfree_gpr; zs.zfree = zfree_gpr;
r = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | (gzip ? 16 : 0), r = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | (gzip ? 16 : 0),
8, Z_DEFAULT_STRATEGY); 8, Z_DEFAULT_STRATEGY);
if (r != Z_OK) { GPR_ASSERT(r == Z_OK);
gpr_log(GPR_ERROR, "deflateInit2 returns %d", r);
return 0;
}
r = zlib_body(&zs, input, output, deflate) && output->length < input->length; r = zlib_body(&zs, input, output, deflate) && output->length < input->length;
if (!r) { if (!r) {
for (i = count_before; i < output->count; i++) { for (i = count_before; i < output->count; i++) {
@ -138,10 +135,7 @@ static int zlib_decompress(gpr_slice_buffer* input, gpr_slice_buffer* output,
zs.zalloc = zalloc_gpr; zs.zalloc = zalloc_gpr;
zs.zfree = zfree_gpr; zs.zfree = zfree_gpr;
r = inflateInit2(&zs, 15 | (gzip ? 16 : 0)); r = inflateInit2(&zs, 15 | (gzip ? 16 : 0));
if (r != Z_OK) { GPR_ASSERT(r == Z_OK);
gpr_log(GPR_ERROR, "inflateInit2 returns %d", r);
return 0;
}
r = zlib_body(&zs, input, output, inflate); r = zlib_body(&zs, input, output, inflate);
if (!r) { if (!r) {
for (i = count_before; i < output->count; i++) { for (i = count_before; i < output->count; i++) {

@ -148,26 +148,53 @@ static gpr_slice create_test_value(test_value id) {
return gpr_slice_from_copied_string("bad value"); return gpr_slice_from_copied_string("bad value");
} }
static void test_bad_data(void) { static void test_tiny_data_compress(void) {
gpr_slice_buffer input; gpr_slice_buffer input;
gpr_slice_buffer output; gpr_slice_buffer output;
grpc_compression_algorithm i; grpc_compression_algorithm i;
gpr_slice_buffer_init(&input); gpr_slice_buffer_init(&input);
gpr_slice_buffer_init(&output); gpr_slice_buffer_init(&output);
gpr_slice_buffer_add(&input, gpr_slice_from_copied_string( gpr_slice_buffer_add(&input, create_test_value(ONE_A));
"this is not valid compressed input"));
for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
if (i == GRPC_COMPRESS_NONE) continue; if (i == GRPC_COMPRESS_NONE) continue;
GPR_ASSERT(0 == grpc_msg_decompress(i, &input, &output)); GPR_ASSERT(0 == grpc_msg_compress(i, &input, &output));
GPR_ASSERT(0 == output.count); GPR_ASSERT(1 == output.count);
} }
gpr_slice_buffer_destroy(&input); gpr_slice_buffer_destroy(&input);
gpr_slice_buffer_destroy(&output); gpr_slice_buffer_destroy(&output);
} }
static void test_bad_data_decompress(void) {
gpr_slice_buffer input;
gpr_slice_buffer corrupted;
gpr_slice_buffer output;
size_t idx;
const gpr_uint32 bad = 0xdeadbeef;
gpr_slice_buffer_init(&input);
gpr_slice_buffer_init(&corrupted);
gpr_slice_buffer_init(&output);
gpr_slice_buffer_add(&input, create_test_value(ONE_MB_A));
/* compress it */
grpc_msg_compress(GRPC_COMPRESS_GZIP, &input, &corrupted);
/* corrupt the output by smashing the CRC */
GPR_ASSERT(corrupted.count > 1);
GPR_ASSERT(GPR_SLICE_LENGTH(corrupted.slices[1]) > 8);
idx = GPR_SLICE_LENGTH(corrupted.slices[1]) - 8;
memcpy(GPR_SLICE_START_PTR(corrupted.slices[1]) + idx, &bad, 4);
/* try (and fail) to decompress the corrupted compresed buffer */
GPR_ASSERT(0 == grpc_msg_decompress(GRPC_COMPRESS_GZIP, &corrupted, &output));
gpr_slice_buffer_destroy(&input);
gpr_slice_buffer_destroy(&corrupted);
gpr_slice_buffer_destroy(&output);
}
static void test_bad_compression_algorithm(void) { static void test_bad_compression_algorithm(void) {
gpr_slice_buffer input; gpr_slice_buffer input;
gpr_slice_buffer output; gpr_slice_buffer output;
@ -234,7 +261,8 @@ int main(int argc, char **argv) {
} }
} }
test_bad_data(); test_tiny_data_compress();
test_bad_data_decompress();
test_bad_compression_algorithm(); test_bad_compression_algorithm();
test_bad_decompression_algorithm(); test_bad_decompression_algorithm();
grpc_shutdown(); grpc_shutdown();

Loading…
Cancel
Save