From e9fa31141d972f320c8c0df48f1a77c5e75c9573 Mon Sep 17 00:00:00 2001
From: David Garcia Quintas <dgq@google.com>
Date: Wed, 9 Dec 2015 17:25:10 -0800
Subject: [PATCH] Further coverage for compression

---
 src/core/compression/message_compress.c       | 10 +----
 test/core/compression/message_compress_test.c | 40 ++++++++++++++++---
 2 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/src/core/compression/message_compress.c b/src/core/compression/message_compress.c
index a723c47819..94f4e89d7a 100644
--- a/src/core/compression/message_compress.c
+++ b/src/core/compression/message_compress.c
@@ -111,10 +111,7 @@ static int zlib_compress(gpr_slice_buffer* input, gpr_slice_buffer* output,
   zs.zfree = zfree_gpr;
   r = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | (gzip ? 16 : 0),
                    8, Z_DEFAULT_STRATEGY);
-  if (r != Z_OK) {
-    gpr_log(GPR_ERROR, "deflateInit2 returns %d", r);
-    return 0;
-  }
+  GPR_ASSERT(r == Z_OK);
   r = zlib_body(&zs, input, output, deflate) && output->length < input->length;
   if (!r) {
     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.zfree = zfree_gpr;
   r = inflateInit2(&zs, 15 | (gzip ? 16 : 0));
-  if (r != Z_OK) {
-    gpr_log(GPR_ERROR, "inflateInit2 returns %d", r);
-    return 0;
-  }
+  GPR_ASSERT(r == Z_OK);
   r = zlib_body(&zs, input, output, inflate);
   if (!r) {
     for (i = count_before; i < output->count; i++) {
diff --git a/test/core/compression/message_compress_test.c b/test/core/compression/message_compress_test.c
index 70359a3f69..131b5e7f65 100644
--- a/test/core/compression/message_compress_test.c
+++ b/test/core/compression/message_compress_test.c
@@ -148,26 +148,53 @@ static gpr_slice create_test_value(test_value id) {
   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 output;
   grpc_compression_algorithm i;
 
   gpr_slice_buffer_init(&input);
   gpr_slice_buffer_init(&output);
-  gpr_slice_buffer_add(&input, gpr_slice_from_copied_string(
-                                   "this is not valid compressed input"));
+  gpr_slice_buffer_add(&input, create_test_value(ONE_A));
 
   for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
     if (i == GRPC_COMPRESS_NONE) continue;
-    GPR_ASSERT(0 == grpc_msg_decompress(i, &input, &output));
-    GPR_ASSERT(0 == output.count);
+    GPR_ASSERT(0 == grpc_msg_compress(i, &input, &output));
+    GPR_ASSERT(1 == output.count);
   }
 
   gpr_slice_buffer_destroy(&input);
   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) {
   gpr_slice_buffer input;
   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_decompression_algorithm();
   grpc_shutdown();
-- 
GitLab