| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include <grpc/impl/codegen/port_platform.h> |
| 16 |
|
| 17 |
#include <random> |
| 18 |
#include <unordered_map> |
| 19 |
|
| 20 |
#include <gtest/gtest.h> |
| 21 |
|
| 22 |
#include "src/core/ext/transport/chttp2/transport/hpack_encoder_index.h" |
| 23 |
|
| 24 |
namespace grpc_core { |
| 25 |
namespace testing { |
| 26 |
|
| 27 |
static void VerifyAsciiHeaderSize(const char* key, const char* value, |
| 28 |
bool intern_key, bool intern_value) { |
| 29 |
grpc_mdelem elem = grpc_mdelem_from_slices( |
| 30 |
maybe_intern(grpc_slice_from_static_string(key), intern_key), |
| 31 |
maybe_intern(grpc_slice_from_static_string(value), intern_value)); |
| 32 |
size_t elem_size = MetadataSizeInHPackTable(elem, false); |
| 33 |
size_t expected_size = 32 + strlen(key) + strlen(value); |
| 34 |
GPR_ASSERT(expected_size == elem_size); |
| 35 |
GRPC_MDELEM_UNREF(elem); |
| 36 |
} |
| 37 |
|
| 38 |
static void VerifyBinaryHeaderSize(const char* key, const uint8_t* value, |
| 39 |
size_t value_len, bool intern_key, |
| 40 |
bool intern_value) { |
| 41 |
grpc_mdelem elem = grpc_mdelem_from_slices( |
| 42 |
maybe_intern(grpc_slice_from_static_string(key), intern_key), |
| 43 |
maybe_intern(grpc_slice_from_static_buffer(value, value_len), |
| 44 |
intern_value)); |
| 45 |
GPR_ASSERT(grpc_is_binary_header(GRPC_MDKEY(elem))); |
| 46 |
size_t elem_size = MetadataSizeInHPackTable(elem, false); |
| 47 |
grpc_slice value_slice = grpc_slice_from_copied_buffer( |
| 48 |
reinterpret_cast<const char*>(value), value_len); |
| 49 |
grpc_slice base64_encoded = grpc_chttp2_base64_encode(value_slice); |
| 50 |
size_t expected_size = 32 + strlen(key) + GRPC_SLICE_LENGTH(base64_encoded); |
| 51 |
GPR_ASSERT(expected_size == elem_size); |
| 52 |
grpc_slice_unref(value_slice); |
| 53 |
grpc_slice_unref(base64_encoded); |
| 54 |
GRPC_MDELEM_UNREF(elem); |
| 55 |
} |
| 56 |
|
| 57 |
struct Param { |
| 58 |
bool intern_key; |
| 59 |
bool intern_value; |
| 60 |
} |
| 61 |
|
| 62 |
class MetadataTest : public ::testing::TestWithParam<Param> { |
| 63 |
}; |
| 64 |
|
| 65 |
#define BUFFER_SIZE 64 |
| 66 |
TEST_P(MetadataTest, MetadataSize) { |
| 67 |
const bool intern_key = GetParam().intern_key; |
| 68 |
const bool intern_value = GetParam().intern_value; |
| 69 |
gpr_log(GPR_INFO, "test_mdelem_size: intern_key=%d intern_value=%d", |
| 70 |
intern_key, intern_value); |
| 71 |
grpc_init(); |
| 72 |
ExecCtx exec_ctx; |
| 73 |
|
| 74 |
uint8_t binary_value[BUFFER_SIZE] = {0}; |
| 75 |
for (uint8_t i = 0; i < BUFFER_SIZE; i++) { |
| 76 |
binary_value[i] = i; |
| 77 |
} |
| 78 |
|
| 79 |
verify_ascii_header_size("hello", "world", intern_key, intern_value); |
| 80 |
verify_ascii_header_size("hello", "worldxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 81 |
intern_key, intern_value); |
| 82 |
verify_ascii_header_size(":scheme", "http", intern_key, intern_value); |
| 83 |
|
| 84 |
for (uint8_t i = 0; i < BUFFER_SIZE; i++) { |
| 85 |
verify_binary_header_size("hello-bin", binary_value, i, intern_key, |
| 86 |
intern_value); |
| 87 |
} |
| 88 |
|
| 89 |
grpc_shutdown(); |
| 90 |
} |
| 91 |
|
| 92 |
INSTANTIATE_TEST_SUITE_P(MetadataTestSuite, MetadataTest, |
| 93 |
::testing::Values(Param{false, false}, |
| 94 |
Param{false, true}, |
| 95 |
Param{true, false}, |
| 96 |
Param{true, true})); |
| 97 |
|
| 98 |
TEST(HPackEncoderIndexTest, SetAndGet) { |
| 99 |
HPackEncoderIndex<TestKey, 64> index; |
| 100 |
std::default_random_engine rng; |
| 101 |
std::unordered_map<uint32_t, uint32_t> last_index; |
| 102 |
for (uint32_t i = 0; i < 10000; i++) { |
| 103 |
uint32_t key = rng(); |
| 104 |
index.Insert({key}, i); |
| 105 |
EXPECT_EQ(index.Lookup({key}), i); |
| 106 |
last_index[key] = i; |
| 107 |
} |
| 108 |
for (auto p : last_index) { |
| 109 |
auto r = index.Lookup({p.first}); |
| 110 |
if (r.has_value()) { |
| 111 |
EXPECT_EQ(*r, p.second); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
} // namespace testing |
| 117 |
} // namespace grpc_core |
| 118 |
|
| 119 |
int main(int argc, char** argv) { |
| 120 |
::testing::InitGoogleTest(&argc, argv); |
| 121 |
return RUN_ALL_TESTS(); |
| 122 |
} |