From e9fa31141d972f320c8c0df48f1a77c5e75c9573 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Wed, 9 Dec 2015 17:25:10 -0800 Subject: [PATCH 1/3] 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 a723c478192..94f4e89d7a7 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 70359a3f69a..131b5e7f65d 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(); From 5e0da5845e8409b3229a095e3bb2649ed8072bee Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Thu, 10 Dec 2015 18:27:32 -0800 Subject: [PATCH 2/3] More coverage --- src/core/compression/algorithm.c | 10 +-- src/core/compression/message_compress.c | 4 +- test/core/compression/compression_test.c | 89 ++++++++++++++++++- test/core/compression/message_compress_test.c | 40 ++++++++- 4 files changed, 130 insertions(+), 13 deletions(-) diff --git a/src/core/compression/algorithm.c b/src/core/compression/algorithm.c index 73d91fa8ea5..9624c52310e 100644 --- a/src/core/compression/algorithm.c +++ b/src/core/compression/algorithm.c @@ -84,7 +84,7 @@ int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm, case GRPC_COMPRESS_ALGORITHMS_COUNT: return 0; } - return 0; + GPR_UNREACHABLE_CODE(return 0); } grpc_compression_algorithm grpc_compression_algorithm_from_mdstr( @@ -139,10 +139,9 @@ grpc_compression_algorithm grpc_compression_algorithm_for_level( case GRPC_COMPRESS_LEVEL_HIGH: return GRPC_COMPRESS_DEFLATE; default: - /* we shouldn't be making it here */ - abort(); - return GRPC_COMPRESS_NONE; + break; } + GPR_UNREACHABLE_CODE(return GRPC_COMPRESS_NONE); } grpc_compression_level grpc_compression_level_for_algorithm( @@ -156,8 +155,7 @@ grpc_compression_level grpc_compression_level_for_algorithm( return clevel; } } - abort(); - return GRPC_COMPRESS_LEVEL_NONE; + GPR_UNREACHABLE_CODE(return GRPC_COMPRESS_LEVEL_NONE); } void grpc_compression_options_init(grpc_compression_options *opts) { diff --git a/src/core/compression/message_compress.c b/src/core/compression/message_compress.c index 94f4e89d7a7..e72347118f2 100644 --- a/src/core/compression/message_compress.c +++ b/src/core/compression/message_compress.c @@ -69,8 +69,8 @@ static int zlib_body(z_stream* zs, gpr_slice_buffer* input, zs->next_out = GPR_SLICE_START_PTR(outbuf); } r = flate(zs, flush); - if (r == Z_STREAM_ERROR) { - gpr_log(GPR_INFO, "zlib: stream error"); + if (r < 0 && r != Z_BUF_ERROR /* not fatal */) { + gpr_log(GPR_INFO, "zlib error (%d)", r); goto error; } } while (zs->avail_out == 0); diff --git a/test/core/compression/compression_test.c b/test/core/compression/compression_test.c index 35fadc00c05..85228fa47f0 100644 --- a/test/core/compression/compression_test.c +++ b/test/core/compression/compression_test.c @@ -53,9 +53,8 @@ static void test_compression_algorithm_parse(void) { for (i = 0; i < GPR_ARRAY_SIZE(valid_names); i++) { const char *valid_name = valid_names[i]; grpc_compression_algorithm algorithm; - int success; - success = grpc_compression_algorithm_parse(valid_name, strlen(valid_name), - &algorithm); + const int success = grpc_compression_algorithm_parse( + valid_name, strlen(valid_name), &algorithm); GPR_ASSERT(success != 0); GPR_ASSERT(algorithm == valid_algorithms[i]); } @@ -71,9 +70,93 @@ static void test_compression_algorithm_parse(void) { } } +static void test_compression_algorithm_name(void) { + int success; + char *name; + size_t i; + const char *valid_names[] = {"identity", "gzip", "deflate"}; + const grpc_compression_algorithm valid_algorithms[] = { + GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE}; + + gpr_log(GPR_DEBUG, "test_compression_algorithm_name"); + + for (i = 0; i < GPR_ARRAY_SIZE(valid_algorithms); i++) { + success = grpc_compression_algorithm_name(valid_algorithms[i], &name); + GPR_ASSERT(success != 0); + GPR_ASSERT(strcmp(name, valid_names[i]) == 0); + } + + success = + grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT, &name); + GPR_ASSERT(success == 0); + /* the value of "name" is undefined upon failure */ +} + + +static void test_compression_algorithm_for_level(void) { + size_t i; + grpc_compression_level levels[] = { + GRPC_COMPRESS_LEVEL_NONE, GRPC_COMPRESS_LEVEL_LOW, + GRPC_COMPRESS_LEVEL_MED, GRPC_COMPRESS_LEVEL_HIGH}; + grpc_compression_algorithm algorithms[] = {GRPC_COMPRESS_NONE, + GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_DEFLATE}; + gpr_log(GPR_DEBUG, "test_compression_algorithm_for_level"); + + for (i = 0; i < GPR_ARRAY_SIZE(levels); i++) { + GPR_ASSERT(algorithms[i] == + grpc_compression_algorithm_for_level(levels[i])); + } +} + +static void test_compression_level_for_algorithm(void) { + + size_t i; + grpc_compression_level levels[] = { + GRPC_COMPRESS_LEVEL_NONE, GRPC_COMPRESS_LEVEL_LOW, + GRPC_COMPRESS_LEVEL_LOW, GRPC_COMPRESS_LEVEL_LOW}; + grpc_compression_algorithm algorithms[] = {GRPC_COMPRESS_NONE, + GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_DEFLATE}; + gpr_log(GPR_DEBUG, "test_compression_level_for_algorithm"); + + for (i = 0; i < GPR_ARRAY_SIZE(algorithms); i++) { + GPR_ASSERT(levels[i] == + grpc_compression_level_for_algorithm(algorithms[i])); + } +} + +static void test_compression_enable_disable_algorithm(void) { + grpc_compression_options options; + grpc_compression_algorithm algorithm; + + gpr_log(GPR_DEBUG, "test_compression_enable_disable_algorithm"); + + grpc_compression_options_init(&options); + for (algorithm = GRPC_COMPRESS_NONE; algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; algorithm++) { + /* all algorithms are enabled by default */ + GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options, + algorithm) != 0); + } + /* disable one by one */ + for (algorithm = GRPC_COMPRESS_NONE; algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; algorithm++) { + grpc_compression_options_disable_algorithm(&options, algorithm); + GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options, + algorithm) == 0); + } + /* re-enable one by one */ + for (algorithm = GRPC_COMPRESS_NONE; algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; algorithm++) { + grpc_compression_options_enable_algorithm(&options, algorithm); + GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options, + algorithm) != 0); + } +} + int main(int argc, char **argv) { grpc_init(); test_compression_algorithm_parse(); + test_compression_algorithm_name(); + test_compression_algorithm_for_level(); + test_compression_level_for_algorithm(); + test_compression_enable_disable_algorithm(); grpc_shutdown(); return 0; diff --git a/test/core/compression/message_compress_test.c b/test/core/compression/message_compress_test.c index 131b5e7f65d..bd454099ce5 100644 --- a/test/core/compression/message_compress_test.c +++ b/test/core/compression/message_compress_test.c @@ -167,7 +167,7 @@ static void test_tiny_data_compress(void) { gpr_slice_buffer_destroy(&output); } -static void test_bad_data_decompress(void) { +static void test_bad_decompression_data_crc(void) { gpr_slice_buffer input; gpr_slice_buffer corrupted; gpr_slice_buffer output; @@ -195,6 +195,40 @@ static void test_bad_data_decompress(void) { gpr_slice_buffer_destroy(&output); } +static void test_bad_decompression_data_trailing_garbage(void) { + gpr_slice_buffer input; + gpr_slice_buffer output; + + gpr_slice_buffer_init(&input); + gpr_slice_buffer_init(&output); + /* append 0x99 to the end of an otherwise valid stream */ + gpr_slice_buffer_add( + &input, gpr_slice_from_copied_buffer( + "\x78\xda\x63\x60\x60\x60\x00\x00\x00\x04\x00\x01\x99", 13)); + + /* try (and fail) to decompress the invalid compresed buffer */ + GPR_ASSERT(0 == grpc_msg_decompress(GRPC_COMPRESS_DEFLATE, &input, &output)); + + gpr_slice_buffer_destroy(&input); + gpr_slice_buffer_destroy(&output); +} + +static void test_bad_decompression_data_stream(void) { + gpr_slice_buffer input; + gpr_slice_buffer output; + + gpr_slice_buffer_init(&input); + gpr_slice_buffer_init(&output); + gpr_slice_buffer_add(&input, + gpr_slice_from_copied_buffer("\x78\xda\xff\xff", 4)); + + /* try (and fail) to decompress the invalid compresed buffer */ + GPR_ASSERT(0 == grpc_msg_decompress(GRPC_COMPRESS_DEFLATE, &input, &output)); + + gpr_slice_buffer_destroy(&input); + gpr_slice_buffer_destroy(&output); +} + static void test_bad_compression_algorithm(void) { gpr_slice_buffer input; gpr_slice_buffer output; @@ -262,7 +296,9 @@ int main(int argc, char **argv) { } test_tiny_data_compress(); - test_bad_data_decompress(); + test_bad_decompression_data_crc(); + test_bad_decompression_data_stream(); + test_bad_decompression_data_trailing_garbage(); test_bad_compression_algorithm(); test_bad_decompression_algorithm(); grpc_shutdown(); From 48ec8062497ecece119d0df61d0bfdca93e11502 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Fri, 11 Dec 2015 14:04:59 -0800 Subject: [PATCH 3/3] Done! --- Makefile | 33 ++++ build.yaml | 10 + include/grpc/compression.h | 6 - src/core/compression/algorithm.c | 20 +- test/core/compression/algorithm_test.c | 104 ++++++++++ test/core/compression/compression_test.c | 17 -- tools/run_tests/sources_and_headers.json | 14 ++ tools/run_tests/tests.json | 18 ++ vsprojects/buildtests_c.sln | 27 +++ .../algorithm_test/algorithm_test.vcxproj | 184 ++++++++++++++++++ .../algorithm_test.vcxproj.filters | 21 ++ 11 files changed, 414 insertions(+), 40 deletions(-) create mode 100644 test/core/compression/algorithm_test.c create mode 100644 vsprojects/vcxproj/test/algorithm_test/algorithm_test.vcxproj create mode 100644 vsprojects/vcxproj/test/algorithm_test/algorithm_test.vcxproj.filters diff --git a/Makefile b/Makefile index f30fd1b6677..cd5a06eda09 100644 --- a/Makefile +++ b/Makefile @@ -777,6 +777,7 @@ systemtap_dep_error: stop: @false +algorithm_test: $(BINDIR)/$(CONFIG)/algorithm_test alloc_test: $(BINDIR)/$(CONFIG)/alloc_test alpn_test: $(BINDIR)/$(CONFIG)/alpn_test bin_encoder_test: $(BINDIR)/$(CONFIG)/bin_encoder_test @@ -1885,6 +1886,7 @@ endif buildtests: buildtests_c buildtests_cxx buildtests_zookeeper buildtests_c: privatelibs_c \ + $(BINDIR)/$(CONFIG)/algorithm_test \ $(BINDIR)/$(CONFIG)/alloc_test \ $(BINDIR)/$(CONFIG)/alpn_test \ $(BINDIR)/$(CONFIG)/bin_encoder_test \ @@ -2872,6 +2874,8 @@ test: test_c test_cxx test_zookeeper flaky_test: flaky_test_c flaky_test_cxx flaky_test_zookeeper test_c: buildtests_c + $(E) "[RUN] Testing algorithm_test" + $(Q) $(BINDIR)/$(CONFIG)/algorithm_test || ( echo test algorithm_test failed ; exit 1 ) $(E) "[RUN] Testing alloc_test" $(Q) $(BINDIR)/$(CONFIG)/alloc_test || ( echo test alloc_test failed ; exit 1 ) $(E) "[RUN] Testing alpn_test" @@ -9214,6 +9218,35 @@ endif # All of the test targets, and protoc plugins +ALGORITHM_TEST_SRC = \ + test/core/compression/algorithm_test.c \ + +ALGORITHM_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(ALGORITHM_TEST_SRC)))) +ifeq ($(NO_SECURE),true) + +# You can't build secure targets if you don't have OpenSSL. + +$(BINDIR)/$(CONFIG)/algorithm_test: openssl_dep_error + +else + +$(BINDIR)/$(CONFIG)/algorithm_test: $(ALGORITHM_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a + $(E) "[LD] Linking $@" + $(Q) mkdir -p `dirname $@` + $(Q) $(LD) $(LDFLAGS) $(ALGORITHM_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBS) $(LDLIBS_SECURE) -o $(BINDIR)/$(CONFIG)/algorithm_test + +endif + +$(OBJDIR)/$(CONFIG)/test/core/compression/algorithm_test.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +deps_algorithm_test: $(ALGORITHM_TEST_OBJS:.o=.dep) + +ifneq ($(NO_SECURE),true) +ifneq ($(NO_DEPS),true) +-include $(ALGORITHM_TEST_OBJS:.o=.dep) +endif +endif + + ALLOC_TEST_SRC = \ test/core/support/alloc_test.c \ diff --git a/build.yaml b/build.yaml index a67e7d0e71d..db04baaa083 100644 --- a/build.yaml +++ b/build.yaml @@ -809,6 +809,16 @@ libs: - winsock - global targets: +- name: algorithm_test + build: test + language: c + src: + - test/core/compression/algorithm_test.c + deps: + - grpc_test_util + - grpc + - gpr_test_util + - gpr - name: alloc_test build: test language: c diff --git a/include/grpc/compression.h b/include/grpc/compression.h index 30163be2ff4..3655a4f24c5 100644 --- a/include/grpc/compression.h +++ b/include/grpc/compression.h @@ -79,12 +79,6 @@ int grpc_compression_algorithm_parse(const char *name, size_t name_length, int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm, char **name); -/** Returns the compression level corresponding to \a algorithm. - * - * It abort()s for unknown algorithms. */ -grpc_compression_level grpc_compression_level_for_algorithm( - grpc_compression_algorithm algorithm); - /** Returns the compression algorithm corresponding to \a level. * * It abort()s for unknown levels . */ diff --git a/src/core/compression/algorithm.c b/src/core/compression/algorithm.c index 9624c52310e..8e4e5c91d44 100644 --- a/src/core/compression/algorithm.c +++ b/src/core/compression/algorithm.c @@ -84,7 +84,7 @@ int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm, case GRPC_COMPRESS_ALGORITHMS_COUNT: return 0; } - GPR_UNREACHABLE_CODE(return 0); + return 0; } grpc_compression_algorithm grpc_compression_algorithm_from_mdstr( @@ -119,8 +119,8 @@ grpc_mdelem *grpc_compression_encoding_mdelem( return GRPC_MDELEM_GRPC_ENCODING_DEFLATE; case GRPC_COMPRESS_GZIP: return GRPC_MDELEM_GRPC_ENCODING_GZIP; - case GRPC_COMPRESS_ALGORITHMS_COUNT: - return NULL; + default: + break; } return NULL; } @@ -144,20 +144,6 @@ grpc_compression_algorithm grpc_compression_algorithm_for_level( GPR_UNREACHABLE_CODE(return GRPC_COMPRESS_NONE); } -grpc_compression_level grpc_compression_level_for_algorithm( - grpc_compression_algorithm algorithm) { - grpc_compression_level clevel; - GRPC_API_TRACE("grpc_compression_level_for_algorithm(algorithm=%d)", 1, - ((int)algorithm)); - for (clevel = GRPC_COMPRESS_LEVEL_NONE; clevel < GRPC_COMPRESS_LEVEL_COUNT; - ++clevel) { - if (grpc_compression_algorithm_for_level(clevel) == algorithm) { - return clevel; - } - } - GPR_UNREACHABLE_CODE(return GRPC_COMPRESS_LEVEL_NONE); -} - void grpc_compression_options_init(grpc_compression_options *opts) { opts->enabled_algorithms_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; opts->default_compression_algorithm = GRPC_COMPRESS_NONE; diff --git a/test/core/compression/algorithm_test.c b/test/core/compression/algorithm_test.c new file mode 100644 index 00000000000..7de7e11a94d --- /dev/null +++ b/test/core/compression/algorithm_test.c @@ -0,0 +1,104 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "src/core/compression/algorithm_metadata.h" + +#include +#include + +#include +#include +#include + +#include "src/core/transport/static_metadata.h" +#include "test/core/util/test_config.h" + +static void test_algorithm_mesh(void) { + int i; + + gpr_log(GPR_DEBUG, "test_algorithm_mesh"); + + for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { + char *name; + grpc_compression_algorithm parsed; + grpc_mdstr *mdstr; + grpc_mdelem *mdelem; + GPR_ASSERT( + grpc_compression_algorithm_name((grpc_compression_algorithm)i, &name)); + GPR_ASSERT(grpc_compression_algorithm_parse(name, strlen(name), &parsed)); + GPR_ASSERT((int)parsed == i); + mdstr = grpc_mdstr_from_string(name); + GPR_ASSERT(mdstr == grpc_compression_algorithm_mdstr(parsed)); + GPR_ASSERT(parsed == grpc_compression_algorithm_from_mdstr(mdstr)); + mdelem = grpc_compression_encoding_mdelem(parsed); + GPR_ASSERT(mdelem->value == mdstr); + GPR_ASSERT(mdelem->key == GRPC_MDSTR_GRPC_ENCODING); + GRPC_MDSTR_UNREF(mdstr); + GRPC_MDELEM_UNREF(mdelem); + } + + /* test failure */ + GPR_ASSERT(NULL == + grpc_compression_encoding_mdelem(GRPC_COMPRESS_ALGORITHMS_COUNT)); +} + +static void test_algorithm_failure(void) { + grpc_mdstr *mdstr; + + gpr_log(GPR_DEBUG, "test_algorithm_failure"); + + GPR_ASSERT(grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT, + NULL) == 0); + GPR_ASSERT(grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT + 1, + NULL) == 0); + mdstr = grpc_mdstr_from_string("this-is-an-invalid-algorithm"); + GPR_ASSERT(grpc_compression_algorithm_from_mdstr(mdstr) == + GRPC_COMPRESS_ALGORITHMS_COUNT); + GPR_ASSERT(grpc_compression_algorithm_mdstr(GRPC_COMPRESS_ALGORITHMS_COUNT) == + NULL); + GPR_ASSERT(grpc_compression_algorithm_mdstr(GRPC_COMPRESS_ALGORITHMS_COUNT + + 1) == NULL); + GRPC_MDSTR_UNREF(mdstr); +} + +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + grpc_init(); + + test_algorithm_mesh(); + test_algorithm_failure(); + + grpc_shutdown(); + + return 0; +} diff --git a/test/core/compression/compression_test.c b/test/core/compression/compression_test.c index 85228fa47f0..78266357c46 100644 --- a/test/core/compression/compression_test.c +++ b/test/core/compression/compression_test.c @@ -108,22 +108,6 @@ static void test_compression_algorithm_for_level(void) { } } -static void test_compression_level_for_algorithm(void) { - - size_t i; - grpc_compression_level levels[] = { - GRPC_COMPRESS_LEVEL_NONE, GRPC_COMPRESS_LEVEL_LOW, - GRPC_COMPRESS_LEVEL_LOW, GRPC_COMPRESS_LEVEL_LOW}; - grpc_compression_algorithm algorithms[] = {GRPC_COMPRESS_NONE, - GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_DEFLATE}; - gpr_log(GPR_DEBUG, "test_compression_level_for_algorithm"); - - for (i = 0; i < GPR_ARRAY_SIZE(algorithms); i++) { - GPR_ASSERT(levels[i] == - grpc_compression_level_for_algorithm(algorithms[i])); - } -} - static void test_compression_enable_disable_algorithm(void) { grpc_compression_options options; grpc_compression_algorithm algorithm; @@ -155,7 +139,6 @@ int main(int argc, char **argv) { test_compression_algorithm_parse(); test_compression_algorithm_name(); test_compression_algorithm_for_level(); - test_compression_level_for_algorithm(); test_compression_enable_disable_algorithm(); grpc_shutdown(); diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json index 704131d5bea..a25de323527 100644 --- a/tools/run_tests/sources_and_headers.json +++ b/tools/run_tests/sources_and_headers.json @@ -1,6 +1,20 @@ [ + { + "deps": [ + "gpr", + "gpr_test_util", + "grpc", + "grpc_test_util" + ], + "headers": [], + "language": "c", + "name": "algorithm_test", + "src": [ + "test/core/compression/algorithm_test.c" + ] + }, { "deps": [ "gpr", diff --git a/tools/run_tests/tests.json b/tools/run_tests/tests.json index 289a13d8c4b..81b15df3908 100644 --- a/tools/run_tests/tests.json +++ b/tools/run_tests/tests.json @@ -1,6 +1,24 @@ [ + { + "ci_platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "exclude_configs": [], + "flaky": false, + "language": "c", + "name": "algorithm_test", + "platforms": [ + "linux", + "mac", + "posix", + "windows" + ] + }, { "ci_platforms": [ "linux", diff --git a/vsprojects/buildtests_c.sln b/vsprojects/buildtests_c.sln index 8ca27b02157..7528229f84f 100644 --- a/vsprojects/buildtests_c.sln +++ b/vsprojects/buildtests_c.sln @@ -1135,6 +1135,17 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bad_client_test", "vcxproj\ {B23D3D1A-9438-4EDA-BEB6-9A0A03D17792} = {B23D3D1A-9438-4EDA-BEB6-9A0A03D17792} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "algorithm_test", "vcxproj\test\algorithm_test\algorithm_test.vcxproj", "{216FDCB2-9D93-0D86-F0F1-12E16312A191}" + ProjectSection(myProperties) = preProject + lib = "False" + EndProjectSection + ProjectSection(ProjectDependencies) = postProject + {17BCAFC0-5FDC-4C94-AEB9-95F3E220614B} = {17BCAFC0-5FDC-4C94-AEB9-95F3E220614B} + {29D16885-7228-4C31-81ED-5F9187C7F2A9} = {29D16885-7228-4C31-81ED-5F9187C7F2A9} + {EAB0A629-17A9-44DB-B5FF-E91A721FE037} = {EAB0A629-17A9-44DB-B5FF-E91A721FE037} + {B23D3D1A-9438-4EDA-BEB6-9A0A03D17792} = {B23D3D1A-9438-4EDA-BEB6-9A0A03D17792} + EndProjectSection +EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "alloc_test", "vcxproj\test\alloc_test\alloc_test.vcxproj", "{DD37D527-9DFF-1F53-B97F-50CF80AE0650}" ProjectSection(myProperties) = preProject lib = "False" @@ -11752,6 +11763,22 @@ Global {BA67B418-B699-E41A-9CC4-0279C49481A5}.Release-DLL|Win32.Build.0 = Release|Win32 {BA67B418-B699-E41A-9CC4-0279C49481A5}.Release-DLL|x64.ActiveCfg = Release|x64 {BA67B418-B699-E41A-9CC4-0279C49481A5}.Release-DLL|x64.Build.0 = Release|x64 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Debug|Win32.ActiveCfg = Debug|Win32 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Debug|x64.ActiveCfg = Debug|x64 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Release|Win32.ActiveCfg = Release|Win32 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Release|x64.ActiveCfg = Release|x64 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Debug|Win32.Build.0 = Debug|Win32 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Debug|x64.Build.0 = Debug|x64 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Release|Win32.Build.0 = Release|Win32 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Release|x64.Build.0 = Release|x64 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Debug-DLL|Win32.ActiveCfg = Debug|Win32 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Debug-DLL|Win32.Build.0 = Debug|Win32 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Debug-DLL|x64.ActiveCfg = Debug|x64 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Debug-DLL|x64.Build.0 = Debug|x64 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Release-DLL|Win32.ActiveCfg = Release|Win32 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Release-DLL|Win32.Build.0 = Release|Win32 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Release-DLL|x64.ActiveCfg = Release|x64 + {216FDCB2-9D93-0D86-F0F1-12E16312A191}.Release-DLL|x64.Build.0 = Release|x64 {DD37D527-9DFF-1F53-B97F-50CF80AE0650}.Debug|Win32.ActiveCfg = Debug|Win32 {DD37D527-9DFF-1F53-B97F-50CF80AE0650}.Debug|x64.ActiveCfg = Debug|x64 {DD37D527-9DFF-1F53-B97F-50CF80AE0650}.Release|Win32.ActiveCfg = Release|Win32 diff --git a/vsprojects/vcxproj/test/algorithm_test/algorithm_test.vcxproj b/vsprojects/vcxproj/test/algorithm_test/algorithm_test.vcxproj new file mode 100644 index 00000000000..d6bccbfc829 --- /dev/null +++ b/vsprojects/vcxproj/test/algorithm_test/algorithm_test.vcxproj @@ -0,0 +1,184 @@ + + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {216FDCB2-9D93-0D86-F0F1-12E16312A191} + + + + v100 + + + v110 + + + v120 + + + Application + true + Unicode + + + Application + false + true + Unicode + + + + + + + + + + + + + + algorithm_test + static + Debug + Debug + + + algorithm_test + static + Debug + Debug + + + + NotUsing + Level3 + Disabled + WIN32;_DEBUG;_LIB;_USE_32BIT_TIME_T;%(PreprocessorDefinitions) + true + MultiThreadedDebug + true + None + + + Console + true + false + + + + + NotUsing + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + MultiThreadedDebug + true + None + + + Console + true + false + + + + + Level3 + NotUsing + MaxSpeed + true + true + WIN32;NDEBUG;_LIB;_USE_32BIT_TIME_T;%(PreprocessorDefinitions) + true + MultiThreaded + true + None + + + Console + true + false + true + true + + + + + Level3 + NotUsing + MaxSpeed + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + MultiThreaded + true + None + + + Console + true + false + true + true + + + + + + + + + {17BCAFC0-5FDC-4C94-AEB9-95F3E220614B} + + + {29D16885-7228-4C31-81ED-5F9187C7F2A9} + + + {EAB0A629-17A9-44DB-B5FF-E91A721FE037} + + + {B23D3D1A-9438-4EDA-BEB6-9A0A03D17792} + + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + + + diff --git a/vsprojects/vcxproj/test/algorithm_test/algorithm_test.vcxproj.filters b/vsprojects/vcxproj/test/algorithm_test/algorithm_test.vcxproj.filters new file mode 100644 index 00000000000..07c4882b501 --- /dev/null +++ b/vsprojects/vcxproj/test/algorithm_test/algorithm_test.vcxproj.filters @@ -0,0 +1,21 @@ + + + + + test\core\compression + + + + + + {578848dc-8093-ced0-747d-7506e3daa00a} + + + {2428bb9e-2466-0c4c-0bfe-0be54b35d2b7} + + + {27e6f7a4-cb96-47a1-9328-614b672c3124} + + + +