hpack_utils_test.cc
Go to the documentation of this file.
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 
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));
46  size_t elem_size = MetadataSizeInHPackTable(elem, false);
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_internal(value_slice);
53  grpc_slice_unref_internal(base64_encoded);
54  GRPC_MDELEM_UNREF(elem);
55 }
56 
57 struct Param {
58  bool intern_key;
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();
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 }
grpc_core::testing::Param::intern_key
bool intern_key
Definition: hpack_utils_test.cc:58
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
testing
Definition: aws_request_signer_test.cc:25
grpc_core
Definition: call_metric_recorder.h:31
elem
Timer elem
Definition: event_engine/iomgr_event_engine/timer_heap_test.cc:109
grpc_core::testing::Param
struct grpc_core::testing::Param Param
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
testing::TestWithParam
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1883
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_core::testing::Param
Definition: hpack_utils_test.cc:57
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_slice_from_static_buffer
GPRAPI grpc_slice grpc_slice_from_static_buffer(const void *source, size_t len)
Definition: slice/slice.cc:85
main
int main(int argc, char **argv)
Definition: hpack_utils_test.cc:119
grpc_core::testing::VerifyBinaryHeaderSize
static void VerifyBinaryHeaderSize(const char *key, const uint8_t *value, size_t value_len, bool intern_key, bool intern_value)
Definition: hpack_utils_test.cc:38
grpc_slice_from_static_string
GPRAPI grpc_slice grpc_slice_from_static_string(const char *source)
Definition: slice/slice.cc:89
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::testing::Param::intern_value
bool intern_value
Definition: hpack_utils_test.cc:59
grpc_core::ExecCtx
Definition: exec_ctx.h:97
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
value
const char * value
Definition: hpack_parser_table.cc:165
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_slice_from_copied_buffer
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Definition: slice/slice.cc:170
key
const char * key
Definition: hpack_parser_table.cc:164
grpc_chttp2_base64_encode
grpc_slice grpc_chttp2_base64_encode(const grpc_slice &input)
Definition: bin_encoder.cc:52
testing::Values
internal::ValueArray< T... > Values(T... v)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:335
BUFFER_SIZE
#define BUFFER_SIZE
Definition: hpack_utils_test.cc:65
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
port_platform.h
fix_build_deps.r
r
Definition: fix_build_deps.py:491
grpc_core::testing::VerifyAsciiHeaderSize
static void VerifyAsciiHeaderSize(const char *key, const char *value, bool intern_key, bool intern_value)
Definition: hpack_utils_test.cc:27
grpc_core::testing::INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(TooManyNames, BdpEstimatorRandomTest, ::testing::Values(3, 4, 6, 9, 13, 19, 28, 42, 63, 94, 141, 211, 316, 474, 711))
grpc_is_binary_header
GRPCAPI int grpc_is_binary_header(grpc_slice slice)
Definition: validate_metadata.cc:131
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
grpc_core::testing::TEST_P
TEST_P(BdpEstimatorRandomTest, GetEstimateRandomValues)
Definition: bdp_estimator_test.cc:124
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_slice_unref_internal
void grpc_slice_unref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:39


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:13