diff options
author | Craig Tiller <ctiller@google.com> | 2016-04-07 07:57:47 -0700 |
---|---|---|
committer | Craig Tiller <ctiller@google.com> | 2016-04-07 07:57:47 -0700 |
commit | 74142a29e942432e17c6bf33e4d7e7d41d5e770f (patch) | |
tree | b8edc8502dca9c5f87b2ab46b0fcb09435b9ef00 /test | |
parent | 5db5e132934404ae64bcfd34f0e70dd4193daee7 (diff) | |
parent | 41d124fe66bd08c3e00a082f282b78ca16eb8903 (diff) |
Merge github.com:grpc/grpc into strong-includes
Diffstat (limited to 'test')
-rw-r--r-- | test/core/transport/chttp2/hpack_encoder_test.c | 32 | ||||
-rw-r--r-- | test/core/transport/metadata_test.c | 51 |
2 files changed, 83 insertions, 0 deletions
diff --git a/test/core/transport/chttp2/hpack_encoder_test.c b/test/core/transport/chttp2/hpack_encoder_test.c index 5ff592c21c..186bb6406f 100644 --- a/test/core/transport/chttp2/hpack_encoder_test.c +++ b/test/core/transport/chttp2/hpack_encoder_test.c @@ -184,6 +184,37 @@ static void test_decode_table_overflow(void) { verify(0, 0, 0, "000007 0104 deadbeef 40 026161 026261", 1, "aa", "ba"); } +static void verify_table_size_change_match_elem_size(const char *key, + const char *value) { + gpr_slice_buffer output; + grpc_mdelem *elem = grpc_mdelem_from_strings(key, value); + size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem); + size_t initial_table_size = g_compressor.table_size; + grpc_linked_mdelem *e = gpr_malloc(sizeof(*e)); + grpc_metadata_batch b; + grpc_metadata_batch_init(&b); + e[0].md = elem; + e[0].prev = NULL; + e[0].next = NULL; + b.list.head = &e[0]; + b.list.tail = &e[0]; + gpr_slice_buffer_init(&output); + + grpc_transport_one_way_stats stats; + memset(&stats, 0, sizeof(stats)); + grpc_chttp2_encode_header(&g_compressor, 0xdeadbeef, &b, 0, &stats, &output); + gpr_slice_buffer_destroy(&output); + grpc_metadata_batch_destroy(&b); + + GPR_ASSERT(g_compressor.table_size == elem_size + initial_table_size); + gpr_free(e); +} + +static void test_encode_header_size(void) { + verify_table_size_change_match_elem_size("hello", "world"); + verify_table_size_change_match_elem_size("hello-bin", "world"); +} + static void run_test(void (*test)(), const char *name) { gpr_log(GPR_INFO, "RUN TEST: %s", name); grpc_chttp2_hpack_compressor_init(&g_compressor); @@ -198,6 +229,7 @@ int main(int argc, char **argv) { grpc_init(); TEST(test_basic_headers); TEST(test_decode_table_overflow); + TEST(test_encode_header_size); grpc_shutdown(); for (i = 0; i < num_to_delete; i++) { gpr_free(to_delete[i]); diff --git a/test/core/transport/metadata_test.c b/test/core/transport/metadata_test.c index 809fa875dd..a4e9694a93 100644 --- a/test/core/transport/metadata_test.c +++ b/test/core/transport/metadata_test.c @@ -34,6 +34,7 @@ #include "src/core/lib/transport/metadata.h" #include <stdio.h> +#include <string.h> #include <grpc/grpc.h> #include <grpc/support/alloc.h> @@ -42,6 +43,7 @@ #include "src/core/ext/transport/chttp2/transport/bin_encoder.h" #include "src/core/lib/support/string.h" +#include "src/core/lib/transport/static_metadata.h" #include "test/core/util/test_config.h" #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x) @@ -260,6 +262,54 @@ static void test_user_data_works(void) { grpc_shutdown(); } +static void verify_ascii_header_size(const char *key, const char *value) { + grpc_mdelem *elem = grpc_mdelem_from_strings(key, value); + size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem); + size_t expected_size = 32 + strlen(key) + strlen(value); + GPR_ASSERT(expected_size == elem_size); + GRPC_MDELEM_UNREF(elem); +} + +static void verify_binary_header_size(const char *key, const uint8_t *value, + size_t value_len) { + grpc_mdelem *elem = grpc_mdelem_from_string_and_buffer(key, value, value_len); + GPR_ASSERT(grpc_is_binary_header(key, strlen(key))); + size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem); + gpr_slice value_slice = + gpr_slice_from_copied_buffer((const char *)value, value_len); + gpr_slice base64_encoded = grpc_chttp2_base64_encode(value_slice); + size_t expected_size = 32 + strlen(key) + GPR_SLICE_LENGTH(base64_encoded); + GPR_ASSERT(expected_size == elem_size); + gpr_slice_unref(value_slice); + gpr_slice_unref(base64_encoded); + GRPC_MDELEM_UNREF(elem); +} + +#define BUFFER_SIZE 64 +static void test_mdelem_sizes_in_hpack(void) { + LOG_TEST("test_mdelem_size"); + grpc_init(); + + uint8_t binary_value[BUFFER_SIZE] = {0}; + for (uint8_t i = 0; i < BUFFER_SIZE; i++) { + binary_value[i] = i; + } + + verify_ascii_header_size("hello", "world"); + verify_ascii_header_size("hello", "worldxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); + verify_ascii_header_size(":scheme", "http"); + + for (uint8_t i = 0; i < BUFFER_SIZE; i++) { + verify_binary_header_size("hello-bin", binary_value, i); + } + + const char *static_metadata = grpc_static_metadata_strings[0]; + memcpy(binary_value, static_metadata, strlen(static_metadata)); + verify_binary_header_size("hello-bin", binary_value, strlen(static_metadata)); + + grpc_shutdown(); +} + int main(int argc, char **argv) { grpc_test_init(argc, argv); test_no_op(); @@ -272,5 +322,6 @@ int main(int argc, char **argv) { test_slices_work(); test_base64_and_huffman_works(); test_user_data_works(); + test_mdelem_sizes_in_hpack(); return 0; } |