diff options
Diffstat (limited to 'test/core/transport/metadata_test.c')
-rw-r--r-- | test/core/transport/metadata_test.c | 325 |
1 files changed, 131 insertions, 194 deletions
diff --git a/test/core/transport/metadata_test.c b/test/core/transport/metadata_test.c index 92e58f2375..3625043d07 100644 --- a/test/core/transport/metadata_test.c +++ b/test/core/transport/metadata_test.c @@ -47,51 +47,55 @@ #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) + /* a large number */ #define MANY 10000 static void test_no_op(void) { - gpr_log(GPR_INFO, "test_no_op"); + LOG_TEST("test_no_op"); grpc_init(); grpc_shutdown(); } -static grpc_slice maybe_intern(grpc_slice in, bool intern) { - grpc_slice out = intern ? grpc_slice_intern(in) : grpc_slice_ref(in); - grpc_slice_unref(in); - return out; -} +static void test_create_string(void) { + grpc_mdstr *s1, *s2, *s3; + + LOG_TEST("test_create_string"); -static grpc_slice maybe_dup(grpc_slice in, bool dup) { - grpc_slice out = dup ? grpc_slice_dup(in) : grpc_slice_ref(in); - grpc_slice_unref(in); - return out; + grpc_init(); + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + s1 = grpc_mdstr_from_string("hello"); + s2 = grpc_mdstr_from_string("hello"); + s3 = grpc_mdstr_from_string("very much not hello"); + GPR_ASSERT(s1 == s2); + GPR_ASSERT(s3 != s1); + GPR_ASSERT(grpc_slice_str_cmp(s1->slice, "hello") == 0); + GPR_ASSERT(grpc_slice_str_cmp(s3->slice, "very much not hello") == 0); + GRPC_MDSTR_UNREF(&exec_ctx, s1); + GRPC_MDSTR_UNREF(&exec_ctx, s2); + GRPC_MDSTR_UNREF(&exec_ctx, s3); + grpc_exec_ctx_finish(&exec_ctx); + grpc_shutdown(); } -static void test_create_metadata(bool intern_keys, bool intern_values) { - grpc_mdelem m1, m2, m3; +static void test_create_metadata(void) { + grpc_mdelem *m1, *m2, *m3; - gpr_log(GPR_INFO, "test_create_metadata: intern_keys=%d intern_values=%d", - intern_keys, intern_values); + LOG_TEST("test_create_metadata"); grpc_init(); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - m1 = grpc_mdelem_from_slices( - &exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys), - maybe_intern(grpc_slice_from_static_string("b"), intern_values)); - m2 = grpc_mdelem_from_slices( - &exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys), - maybe_intern(grpc_slice_from_static_string("b"), intern_values)); - m3 = grpc_mdelem_from_slices( - &exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys), - maybe_intern(grpc_slice_from_static_string("c"), intern_values)); - GPR_ASSERT(grpc_mdelem_eq(m1, m2)); - GPR_ASSERT(!grpc_mdelem_eq(m3, m1)); - GPR_ASSERT(grpc_slice_eq(GRPC_MDKEY(m3), GRPC_MDKEY(m1))); - GPR_ASSERT(!grpc_slice_eq(GRPC_MDVALUE(m3), GRPC_MDVALUE(m1))); - GPR_ASSERT(grpc_slice_str_cmp(GRPC_MDKEY(m1), "a") == 0); - GPR_ASSERT(grpc_slice_str_cmp(GRPC_MDVALUE(m1), "b") == 0); - GPR_ASSERT(grpc_slice_str_cmp(GRPC_MDVALUE(m3), "c") == 0); + m1 = grpc_mdelem_from_strings(&exec_ctx, "a", "b"); + m2 = grpc_mdelem_from_strings(&exec_ctx, "a", "b"); + m3 = grpc_mdelem_from_strings(&exec_ctx, "a", "c"); + GPR_ASSERT(m1 == m2); + GPR_ASSERT(m3 != m1); + GPR_ASSERT(m3->key == m1->key); + GPR_ASSERT(m3->value != m1->value); + GPR_ASSERT(grpc_slice_str_cmp(m1->key->slice, "a") == 0); + GPR_ASSERT(grpc_slice_str_cmp(m1->value->slice, "b") == 0); + GPR_ASSERT(grpc_slice_str_cmp(m3->value->slice, "c") == 0); GRPC_MDELEM_UNREF(&exec_ctx, m1); GRPC_MDELEM_UNREF(&exec_ctx, m2); GRPC_MDELEM_UNREF(&exec_ctx, m3); @@ -99,28 +103,19 @@ static void test_create_metadata(bool intern_keys, bool intern_values) { grpc_shutdown(); } -static void test_create_many_ephemeral_metadata(bool intern_keys, - bool intern_values) { +static void test_create_many_ephemeral_metadata(void) { char buffer[GPR_LTOA_MIN_BUFSIZE]; long i; - gpr_log( - GPR_INFO, - "test_create_many_ephemeral_metadata: intern_keys=%d intern_values=%d", - intern_keys, intern_values); + LOG_TEST("test_create_many_ephemeral_metadata"); grpc_init(); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; /* add, and immediately delete a bunch of different elements */ for (i = 0; i < MANY; i++) { gpr_ltoa(i, buffer); - GRPC_MDELEM_UNREF( - &exec_ctx, - grpc_mdelem_from_slices( - &exec_ctx, - maybe_intern(grpc_slice_from_static_string("a"), intern_keys), - maybe_intern(grpc_slice_from_copied_string(buffer), - intern_values))); + GRPC_MDELEM_UNREF(&exec_ctx, + grpc_mdelem_from_strings(&exec_ctx, "a", buffer)); } grpc_exec_ctx_finish(&exec_ctx); grpc_shutdown(); @@ -129,27 +124,23 @@ static void test_create_many_ephemeral_metadata(bool intern_keys, static void test_create_many_persistant_metadata(void) { char buffer[GPR_LTOA_MIN_BUFSIZE]; long i; - grpc_mdelem *created = gpr_malloc(sizeof(grpc_mdelem) * MANY); - grpc_mdelem md; + grpc_mdelem **created = gpr_malloc(sizeof(grpc_mdelem *) * MANY); + grpc_mdelem *md; - gpr_log(GPR_INFO, "test_create_many_persistant_metadata"); + LOG_TEST("test_create_many_persistant_metadata"); grpc_init(); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; /* add phase */ for (i = 0; i < MANY; i++) { gpr_ltoa(i, buffer); - created[i] = grpc_mdelem_from_slices( - &exec_ctx, grpc_slice_intern(grpc_slice_from_static_string("a")), - grpc_slice_intern(grpc_slice_from_static_string(buffer))); + created[i] = grpc_mdelem_from_strings(&exec_ctx, "a", buffer); } /* verify phase */ for (i = 0; i < MANY; i++) { gpr_ltoa(i, buffer); - md = grpc_mdelem_from_slices( - &exec_ctx, grpc_slice_intern(grpc_slice_from_static_string("a")), - grpc_slice_intern(grpc_slice_from_static_string(buffer))); - GPR_ASSERT(grpc_mdelem_eq(md, created[i])); + md = grpc_mdelem_from_strings(&exec_ctx, "a", buffer); + GPR_ASSERT(md == created[i]); GRPC_MDELEM_UNREF(&exec_ctx, md); } /* cleanup phase */ @@ -162,77 +153,14 @@ static void test_create_many_persistant_metadata(void) { gpr_free(created); } -static void test_spin_creating_the_same_thing(bool intern_keys, - bool intern_values) { - gpr_log(GPR_INFO, - "test_spin_creating_the_same_thing: intern_keys=%d intern_values=%d", - intern_keys, intern_values); - - grpc_init(); - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - grpc_mdelem a, b, c; - GRPC_MDELEM_UNREF( - &exec_ctx, - a = grpc_mdelem_from_slices( - &exec_ctx, - maybe_intern(grpc_slice_from_static_string("a"), intern_keys), - maybe_intern(grpc_slice_from_static_string("b"), intern_values))); - GRPC_MDELEM_UNREF( - &exec_ctx, - b = grpc_mdelem_from_slices( - &exec_ctx, - maybe_intern(grpc_slice_from_static_string("a"), intern_keys), - maybe_intern(grpc_slice_from_static_string("b"), intern_values))); - GRPC_MDELEM_UNREF( - &exec_ctx, - c = grpc_mdelem_from_slices( - &exec_ctx, - maybe_intern(grpc_slice_from_static_string("a"), intern_keys), - maybe_intern(grpc_slice_from_static_string("b"), intern_values))); - if (intern_keys && intern_values) { - GPR_ASSERT(a.payload == b.payload); - GPR_ASSERT(a.payload == c.payload); - } - grpc_exec_ctx_finish(&exec_ctx); - grpc_shutdown(); -} - -static void test_identity_laws(bool intern_keys, bool intern_values) { - gpr_log(GPR_INFO, "test_identity_laws: intern_keys=%d intern_values=%d", - intern_keys, intern_values); +static void test_spin_creating_the_same_thing(void) { + LOG_TEST("test_spin_creating_the_same_thing"); grpc_init(); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - grpc_mdelem a, b, c; - a = grpc_mdelem_from_slices( - &exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys), - maybe_intern(grpc_slice_from_static_string("b"), intern_values)); - b = grpc_mdelem_from_slices( - &exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys), - maybe_intern(grpc_slice_from_static_string("b"), intern_values)); - c = grpc_mdelem_from_slices( - &exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys), - maybe_intern(grpc_slice_from_static_string("b"), intern_values)); - GPR_ASSERT(grpc_mdelem_eq(a, a)); - GPR_ASSERT(grpc_mdelem_eq(b, b)); - GPR_ASSERT(grpc_mdelem_eq(c, c)); - GPR_ASSERT(grpc_mdelem_eq(a, b)); - GPR_ASSERT(grpc_mdelem_eq(b, c)); - GPR_ASSERT(grpc_mdelem_eq(a, c)); - GPR_ASSERT(grpc_mdelem_eq(b, a)); - GPR_ASSERT(grpc_mdelem_eq(c, b)); - GPR_ASSERT(grpc_mdelem_eq(c, a)); - if (intern_keys && intern_values) { - GPR_ASSERT(a.payload == b.payload); - GPR_ASSERT(a.payload == c.payload); - } else { - GPR_ASSERT(a.payload != b.payload); - GPR_ASSERT(a.payload != c.payload); - GPR_ASSERT(b.payload != c.payload); - } - GRPC_MDELEM_UNREF(&exec_ctx, a); - GRPC_MDELEM_UNREF(&exec_ctx, b); - GRPC_MDELEM_UNREF(&exec_ctx, c); + GRPC_MDELEM_UNREF(&exec_ctx, grpc_mdelem_from_strings(&exec_ctx, "a", "b")); + GRPC_MDELEM_UNREF(&exec_ctx, grpc_mdelem_from_strings(&exec_ctx, "a", "b")); + GRPC_MDELEM_UNREF(&exec_ctx, grpc_mdelem_from_strings(&exec_ctx, "a", "b")); grpc_exec_ctx_finish(&exec_ctx); grpc_shutdown(); } @@ -241,25 +169,25 @@ static void test_things_stick_around(void) { size_t i, j; char *buffer; size_t nstrs = 1000; - grpc_slice *strs = gpr_malloc(sizeof(grpc_slice) * nstrs); + grpc_mdstr **strs = gpr_malloc(sizeof(grpc_mdstr *) * nstrs); size_t *shuf = gpr_malloc(sizeof(size_t) * nstrs); - grpc_slice test; + grpc_mdstr *test; - gpr_log(GPR_INFO, "test_things_stick_around"); + LOG_TEST("test_things_stick_around"); grpc_init(); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; for (i = 0; i < nstrs; i++) { gpr_asprintf(&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%" PRIuPTR "x", i); - strs[i] = grpc_slice_intern(grpc_slice_from_static_string(buffer)); + strs[i] = grpc_mdstr_from_string(buffer); shuf[i] = i; gpr_free(buffer); } for (i = 0; i < nstrs; i++) { - grpc_slice_ref_internal(strs[i]); - grpc_slice_unref_internal(&exec_ctx, strs[i]); + GRPC_MDSTR_REF(strs[i]); + GRPC_MDSTR_UNREF(&exec_ctx, strs[i]); } for (i = 0; i < nstrs; i++) { @@ -271,13 +199,13 @@ static void test_things_stick_around(void) { } for (i = 0; i < nstrs; i++) { - grpc_slice_unref_internal(&exec_ctx, strs[shuf[i]]); + GRPC_MDSTR_UNREF(&exec_ctx, strs[shuf[i]]); for (j = i + 1; j < nstrs; j++) { gpr_asprintf(&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%" PRIuPTR "x", shuf[j]); - test = grpc_slice_intern(grpc_slice_from_static_string(buffer)); - GPR_ASSERT(grpc_slice_is_equivalent(test, strs[shuf[j]])); - grpc_slice_unref_internal(&exec_ctx, test); + test = grpc_mdstr_from_string(buffer); + GPR_ASSERT(test == strs[shuf[j]]); + GRPC_MDSTR_UNREF(&exec_ctx, test); gpr_free(buffer); } } @@ -288,11 +216,57 @@ static void test_things_stick_around(void) { gpr_free(shuf); } +static void test_slices_work(void) { + /* ensure no memory leaks when switching representation from mdstr to slice */ + grpc_mdstr *str; + grpc_slice slice; + + LOG_TEST("test_slices_work"); + + grpc_init(); + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + + str = grpc_mdstr_from_string( + "123456789012345678901234567890123456789012345678901234567890"); + slice = grpc_slice_ref(str->slice); + GRPC_MDSTR_UNREF(&exec_ctx, str); + grpc_slice_unref_internal(&exec_ctx, slice); + + str = grpc_mdstr_from_string( + "123456789012345678901234567890123456789012345678901234567890"); + slice = grpc_slice_ref(str->slice); + grpc_slice_unref_internal(&exec_ctx, slice); + GRPC_MDSTR_UNREF(&exec_ctx, str); + + grpc_exec_ctx_finish(&exec_ctx); + grpc_shutdown(); +} + +static void test_base64_and_huffman_works(void) { + grpc_mdstr *str; + grpc_slice slice1; + grpc_slice slice2; + + LOG_TEST("test_base64_and_huffman_works"); + + grpc_init(); + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + str = grpc_mdstr_from_string("abcdefg"); + slice1 = grpc_mdstr_as_base64_encoded_and_huffman_compressed(str); + slice2 = grpc_chttp2_base64_encode_and_huffman_compress(str->slice); + GPR_ASSERT(0 == grpc_slice_cmp(slice1, slice2)); + + grpc_slice_unref_internal(&exec_ctx, slice2); + GRPC_MDSTR_UNREF(&exec_ctx, str); + grpc_exec_ctx_finish(&exec_ctx); + grpc_shutdown(); +} + static void test_user_data_works(void) { int *ud1; int *ud2; - grpc_mdelem md; - gpr_log(GPR_INFO, "test_user_data_works"); + grpc_mdelem *md; + LOG_TEST("test_user_data_works"); grpc_init(); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; @@ -300,9 +274,7 @@ static void test_user_data_works(void) { *ud1 = 1; ud2 = gpr_malloc(sizeof(int)); *ud2 = 2; - md = grpc_mdelem_from_slices( - &exec_ctx, grpc_slice_intern(grpc_slice_from_static_string("abc")), - grpc_slice_intern(grpc_slice_from_static_string("123"))); + md = grpc_mdelem_from_strings(&exec_ctx, "abc", "123"); grpc_mdelem_set_user_data(md, gpr_free, ud1); grpc_mdelem_set_user_data(md, gpr_free, ud2); GPR_ASSERT(grpc_mdelem_get_user_data(md, gpr_free) == ud1); @@ -312,11 +284,8 @@ static void test_user_data_works(void) { } static void verify_ascii_header_size(grpc_exec_ctx *exec_ctx, const char *key, - const char *value, bool intern_key, - bool intern_value) { - grpc_mdelem elem = grpc_mdelem_from_slices( - exec_ctx, maybe_intern(grpc_slice_from_static_string(key), intern_key), - maybe_intern(grpc_slice_from_static_string(value), intern_value)); + const char *value) { + grpc_mdelem *elem = grpc_mdelem_from_strings(exec_ctx, 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); @@ -324,13 +293,10 @@ static void verify_ascii_header_size(grpc_exec_ctx *exec_ctx, const char *key, } static void verify_binary_header_size(grpc_exec_ctx *exec_ctx, const char *key, - const uint8_t *value, size_t value_len, - bool intern_key, bool intern_value) { - grpc_mdelem elem = grpc_mdelem_from_slices( - exec_ctx, maybe_intern(grpc_slice_from_static_string(key), intern_key), - maybe_intern(grpc_slice_from_static_buffer(value, value_len), - intern_value)); - GPR_ASSERT(grpc_is_binary_header(GRPC_MDKEY(elem))); + const uint8_t *value, size_t value_len) { + grpc_mdelem *elem = + grpc_mdelem_from_string_and_buffer(exec_ctx, 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); grpc_slice value_slice = grpc_slice_from_copied_buffer((const char *)value, value_len); @@ -343,9 +309,8 @@ static void verify_binary_header_size(grpc_exec_ctx *exec_ctx, const char *key, } #define BUFFER_SIZE 64 -static void test_mdelem_sizes_in_hpack(bool intern_key, bool intern_value) { - gpr_log(GPR_INFO, "test_mdelem_size: intern_key=%d intern_value=%d", - intern_key, intern_value); +static void test_mdelem_sizes_in_hpack(void) { + LOG_TEST("test_mdelem_size"); grpc_init(); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; @@ -354,44 +319,19 @@ static void test_mdelem_sizes_in_hpack(bool intern_key, bool intern_value) { binary_value[i] = i; } - verify_ascii_header_size(&exec_ctx, "hello", "world", intern_key, - intern_value); + verify_ascii_header_size(&exec_ctx, "hello", "world"); verify_ascii_header_size(&exec_ctx, "hello", - "worldxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", intern_key, - intern_value); - verify_ascii_header_size(&exec_ctx, ":scheme", "http", intern_key, - intern_value); + "worldxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); + verify_ascii_header_size(&exec_ctx, ":scheme", "http"); for (uint8_t i = 0; i < BUFFER_SIZE; i++) { - verify_binary_header_size(&exec_ctx, "hello-bin", binary_value, i, - intern_key, intern_value); + verify_binary_header_size(&exec_ctx, "hello-bin", binary_value, i); } - grpc_exec_ctx_finish(&exec_ctx); - grpc_shutdown(); -} - -static void test_copied_static_metadata(bool dup_key, bool dup_value) { - gpr_log(GPR_INFO, "test_static_metadata: dup_key=%d dup_value=%d", dup_key, - dup_value); - grpc_init(); - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - - for (size_t i = 0; i < GRPC_STATIC_MDELEM_COUNT; i++) { - grpc_mdelem p = GRPC_MAKE_MDELEM(&grpc_static_mdelem_table[i], - GRPC_MDELEM_STORAGE_STATIC); - grpc_mdelem q = - grpc_mdelem_from_slices(&exec_ctx, maybe_dup(GRPC_MDKEY(p), dup_key), - maybe_dup(GRPC_MDVALUE(p), dup_value)); - GPR_ASSERT(grpc_mdelem_eq(p, q)); - if (dup_key || dup_value) { - GPR_ASSERT(p.payload != q.payload); - } else { - GPR_ASSERT(p.payload == q.payload); - } - GRPC_MDELEM_UNREF(&exec_ctx, p); - GRPC_MDELEM_UNREF(&exec_ctx, q); - } + const char *static_metadata = grpc_static_metadata_strings[0]; + memcpy(binary_value, static_metadata, strlen(static_metadata)); + verify_binary_header_size(&exec_ctx, "hello-bin", binary_value, + strlen(static_metadata)); grpc_exec_ctx_finish(&exec_ctx); grpc_shutdown(); @@ -400,18 +340,15 @@ static void test_copied_static_metadata(bool dup_key, bool dup_value) { int main(int argc, char **argv) { grpc_test_init(argc, argv); test_no_op(); - for (int k = 0; k <= 1; k++) { - for (int v = 0; v <= 1; v++) { - test_create_metadata(k, v); - test_create_many_ephemeral_metadata(k, v); - test_identity_laws(k, v); - test_spin_creating_the_same_thing(k, v); - test_mdelem_sizes_in_hpack(k, v); - test_copied_static_metadata(k, v); - } - } + test_create_string(); + test_create_metadata(); + test_create_many_ephemeral_metadata(); test_create_many_persistant_metadata(); + test_spin_creating_the_same_thing(); test_things_stick_around(); + test_slices_work(); + test_base64_and_huffman_works(); test_user_data_works(); + test_mdelem_sizes_in_hpack(); return 0; } |