hpack_parser_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <gtest/gtest.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/slice.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 
33 
35  grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
36  "test"));
37 
38 struct TestInput {
39  const char* input;
40  const char* expected_parse;
41 };
42 
43 struct Test {
45  std::vector<TestInput> inputs;
46 };
47 
48 class ParseTest : public ::testing::TestWithParam<Test> {
49  public:
51  grpc_init();
52  parser_ = absl::make_unique<grpc_core::HPackParser>();
53  }
54 
55  ~ParseTest() override {
56  {
58  parser_.reset();
59  }
60 
61  grpc_shutdown();
62  }
63 
64  void SetUp() override {
65  if (GetParam().table_size.has_value()) {
66  parser_->hpack_table()->SetMaxBytes(GetParam().table_size.value());
67  EXPECT_EQ(parser_->hpack_table()->SetCurrentTableSize(
68  GetParam().table_size.value()),
70  }
71  }
72 
73  void TestVector(grpc_slice_split_mode mode, const char* hexstring,
74  std::string expect) {
77  grpc_slice input = parse_hexstring(hexstring);
79  size_t nslices;
80  size_t i;
81 
83 
84  parser_->BeginFrame(
89 
90  grpc_split_slices(mode, &input, 1, &slices, &nslices);
92 
93  for (i = 0; i < nslices; i++) {
95  auto err = parser_->Parse(slices[i], i == nslices - 1);
96  if (!GRPC_ERROR_IS_NONE(err)) {
97  gpr_log(GPR_ERROR, "Unexpected parse error: %s",
99  abort();
100  }
101  }
102 
103  for (i = 0; i < nslices; i++) {
105  }
106  gpr_free(slices);
107 
108  TestEncoder encoder;
109  b.Encode(&encoder);
110  EXPECT_EQ(encoder.result(), expect);
111  }
112 
113  private:
114  class TestEncoder {
115  public:
116  std::string result() { return out_; }
117 
119  out_.append(absl::StrCat(key.as_string_view(), ": ",
120  value.as_string_view(), "\n"));
121  }
122 
123  template <typename T, typename V>
124  void Encode(T, const V& v) {
125  out_.append(
126  absl::StrCat(T::key(), ": ", T::Encode(v).as_string_view(), "\n"));
127  }
128 
129  private:
131  };
132 
133  std::unique_ptr<grpc_core::HPackParser> parser_;
134 };
135 
136 TEST_P(ParseTest, WholeSlices) {
137  for (const auto& input : GetParam().inputs) {
138  TestVector(GRPC_SLICE_SPLIT_MERGE_ALL, input.input, input.expected_parse);
139  }
140 }
141 
142 TEST_P(ParseTest, OneByteAtATime) {
143  for (const auto& input : GetParam().inputs) {
144  TestVector(GRPC_SLICE_SPLIT_ONE_BYTE, input.input, input.expected_parse);
145  }
146 }
147 
150  ::testing::Values(
151  Test{
152  {},
153  {
154  /* D.2.1 */
155  {"400a 6375 7374 6f6d 2d6b 6579 0d63 7573"
156  "746f 6d2d 6865 6164 6572",
157  "custom-key: custom-header\n"},
158  /* D.2.2 */
159  {"040c 2f73 616d 706c 652f 7061 7468", ":path: /sample/path\n"},
160  /* D.2.3 */
161  {"1008 7061 7373 776f 7264 0673 6563 7265"
162  "74",
163  "password: secret\n"},
164  /* D.2.4 */
165  {"82", ":method: GET\n"},
166  }},
167  Test{{},
168  {
169  /* D.3.1 */
170  {"8286 8441 0f77 7777 2e65 7861 6d70 6c65"
171  "2e63 6f6d",
172  ":path: /\n"
173  ":authority: www.example.com\n"
174  ":method: GET\n"
175  ":scheme: http\n"},
176  /* D.3.2 */
177  {"8286 84be 5808 6e6f 2d63 6163 6865",
178  ":path: /\n"
179  ":authority: www.example.com\n"
180  ":method: GET\n"
181  ":scheme: http\n"
182  "cache-control: no-cache\n"},
183  /* D.3.3 */
184  {"8287 85bf 400a 6375 7374 6f6d 2d6b 6579"
185  "0c63 7573 746f 6d2d 7661 6c75 65",
186  ":path: /index.html\n"
187  ":authority: www.example.com\n"
188  ":method: GET\n"
189  ":scheme: https\n"
190  "custom-key: custom-value\n"},
191  }},
192  Test{{},
193  {
194  /* D.4.1 */
195  {"8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4"
196  "ff",
197  ":path: /\n"
198  ":authority: www.example.com\n"
199  ":method: GET\n"
200  ":scheme: http\n"},
201  /* D.4.2 */
202  {"8286 84be 5886 a8eb 1064 9cbf",
203  ":path: /\n"
204  ":authority: www.example.com\n"
205  ":method: GET\n"
206  ":scheme: http\n"
207  "cache-control: no-cache\n"},
208  /* D.4.3 */
209  {"8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925"
210  "a849 e95b b8e8 b4bf",
211  ":path: /index.html\n"
212  ":authority: www.example.com\n"
213  ":method: GET\n"
214  ":scheme: https\n"
215  "custom-key: custom-value\n"},
216  }},
217  Test{{256},
218  {
219  /* D.5.1 */
220  {"4803 3330 3258 0770 7269 7661 7465 611d"
221  "4d6f 6e2c 2032 3120 4f63 7420 3230 3133"
222  "2032 303a 3133 3a32 3120 474d 546e 1768"
223  "7474 7073 3a2f 2f77 7777 2e65 7861 6d70"
224  "6c65 2e63 6f6d",
225  ":status: 302\n"
226  "cache-control: private\n"
227  "date: Mon, 21 Oct 2013 20:13:21 GMT\n"
228  "location: https://www.example.com\n"},
229  /* D.5.2 */
230  {"4803 3330 37c1 c0bf",
231  ":status: 307\n"
232  "cache-control: private\n"
233  "date: Mon, 21 Oct 2013 20:13:21 GMT\n"
234  "location: https://www.example.com\n"},
235  /* D.5.3 */
236  {"88c1 611d 4d6f 6e2c 2032 3120 4f63 7420"
237  "3230 3133 2032 303a 3133 3a32 3220 474d"
238  "54c0 5a04 677a 6970 7738 666f 6f3d 4153"
239  "444a 4b48 514b 425a 584f 5157 454f 5049"
240  "5541 5851 5745 4f49 553b 206d 6178 2d61"
241  "6765 3d33 3630 303b 2076 6572 7369 6f6e"
242  "3d31",
243  ":status: 200\n"
244  "cache-control: private\n"
245  "date: Mon, 21 Oct 2013 20:13:22 GMT\n"
246  "location: https://www.example.com\n"
247  "content-encoding: gzip\n"
248  "set-cookie: foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; "
249  "version=1\n"},
250  }},
251  Test{{256},
252  {
253  /* D.6.1 */
254  {"4882 6402 5885 aec3 771a 4b61 96d0 7abe"
255  "9410 54d4 44a8 2005 9504 0b81 66e0 82a6"
256  "2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8"
257  "e9ae 82ae 43d3",
258  ":status: 302\n"
259  "cache-control: private\n"
260  "date: Mon, 21 Oct 2013 20:13:21 GMT\n"
261  "location: https://www.example.com\n"},
262  /* D.6.2 */
263  {"4883 640e ffc1 c0bf",
264  ":status: 307\n"
265  "cache-control: private\n"
266  "date: Mon, 21 Oct 2013 20:13:21 GMT\n"
267  "location: https://www.example.com\n"},
268  /* D.6.3 */
269  {"88c1 6196 d07a be94 1054 d444 a820 0595"
270  "040b 8166 e084 a62d 1bff c05a 839b d9ab"
271  "77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b"
272  "3960 d5af 2708 7f36 72c1 ab27 0fb5 291f"
273  "9587 3160 65c0 03ed 4ee5 b106 3d50 07",
274  ":status: 200\n"
275  "cache-control: private\n"
276  "date: Mon, 21 Oct 2013 20:13:22 GMT\n"
277  "location: https://www.example.com\n"
278  "content-encoding: gzip\n"
279  "set-cookie: foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; "
280  "version=1\n"},
281  }},
282  Test{{},
283  {
284  // Binary metadata: created using:
285  // tools/codegen/core/gen_header_frame.py
286  // --compression inc --no_framing --hex
287  // < test/core/transport/chttp2/binary-metadata.headers
288  {"40 09 61 2e 62 2e 63 2d 62 69 6e 0c 62 32 31 6e 4d 6a 41 79 "
289  "4d 51 3d 3d",
290  "a.b.c-bin: omg2021\n"},
291  }}));
292 
293 int main(int argc, char** argv) {
294  grpc::testing::TestEnvironment env(&argc, argv);
295  ::testing::InitGoogleTest(&argc, argv);
296  return RUN_ALL_TESTS();
297 }
grpc_core::HPackParser::LogInfo
Definition: hpack_parser.h:59
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
grpc_core::MakeScopedArena
ScopedArenaPtr MakeScopedArena(size_t initial_size, MemoryAllocator *memory_allocator)
Definition: src/core/lib/resource_quota/arena.h:130
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
generate.env
env
Definition: generate.py:37
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
TestInput::expected_parse
const char * expected_parse
Definition: hpack_parser_test.cc:40
slice.h
ParseTest::TestEncoder::Encode
void Encode(const grpc_core::Slice &key, const grpc_core::Slice &value)
Definition: hpack_parser_test.cc:118
grpc_core::Slice
Definition: src/core/lib/slice/slice.h:282
grpc_slice_split_mode
grpc_slice_split_mode
Definition: slice_splitter.h:28
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error_ref_leak.err
err
Definition: error_ref_leak.py:35
ParseTest::parser_
std::unique_ptr< grpc_core::HPackParser > parser_
Definition: hpack_parser_test.cc:133
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
ParseTest::TestEncoder
Definition: hpack_parser_test.cc:114
ParseTest::TestEncoder::Encode
void Encode(T, const V &v)
Definition: hpack_parser_test.cc:124
Test::table_size
absl::optional< size_t > table_size
Definition: hpack_parser_test.cc:44
TEST_P
TEST_P(ParseTest, WholeSlices)
Definition: hpack_parser_test.cc:136
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_core::HPackParser::Boundary::None
@ None
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
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
Test::inputs
std::vector< TestInput > inputs
Definition: hpack_parser_test.cc:45
GRPC_SLICE_SPLIT_MERGE_ALL
@ GRPC_SLICE_SPLIT_MERGE_ALL
Definition: slice_splitter.h:30
slice_splitter.h
hpack_parser.h
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
ParseTest::TestEncoder::result
std::string result()
Definition: hpack_parser_test.cc:116
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
grpc.h
absl::optional< size_t >
ParseTest
Definition: hpack_parser_test.cc:48
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_core::ResourceQuota::Default
static ResourceQuotaRefPtr Default()
Definition: resource_quota.cc:27
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
resource_quota.h
grpc_core::ExecCtx
Definition: exec_ctx.h:97
ParseTest::SetUp
void SetUp() override
Definition: hpack_parser_test.cc:64
g_memory_allocator
static auto * g_memory_allocator
Definition: hpack_parser_test.cc:34
main
int main(int argc, char **argv)
Definition: hpack_parser_test.cc:293
test_config.h
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
key
const char * key
Definition: hpack_parser_table.cc:164
testing::Values
internal::ValueArray< T... > Values(T... v)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:335
grpc_core::HPackParser::Priority::None
@ None
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
grpc_split_slices
void grpc_split_slices(grpc_slice_split_mode mode, grpc_slice *src_slices, size_t src_slice_count, grpc_slice **dst_slices, size_t *dst_slice_count)
Definition: slice_splitter.cc:41
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
ParseTest::TestEncoder::out_
std::string out_
Definition: hpack_parser_test.cc:130
slices
SliceBuffer * slices
Definition: retry_filter.cc:631
testing::WithParamInterface< Test >::GetParam
static const ParamType & GetParam()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1855
TestInput::input
const char * input
Definition: hpack_parser_test.cc:39
exec_ctx.h
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
ParseTest::ParseTest
ParseTest()
Definition: hpack_parser_test.cc:50
TestInput
Definition: hpack_parser_test.cc:38
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
parse_hexstring.h
grpc_core::HPackParser::LogInfo::kHeaders
@ kHeaders
Definition: hpack_parser.h:64
INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(ParseTest, ParseTest, ::testing::Values(Test{ {}, { {"400a 6375 7374 6f6d 2d6b 6579 0d63 7573" "746f 6d2d 6865 6164 6572", "custom-key: custom-header\n"}, {"040c 2f73 616d 706c 652f 7061 7468", ":path: /sample/path\n"}, {"1008 7061 7373 776f 7264 0673 6563 7265" "74", "password: secret\n"}, {"82", ":method: GET\n"}, }}, Test{{}, { {"8286 8441 0f77 7777 2e65 7861 6d70 6c65" "2e63 6f6d", ":path: /\n" ":authority: www.example.com\n" ":method: GET\n" ":scheme: http\n"}, {"8286 84be 5808 6e6f 2d63 6163 6865", ":path: /\n" ":authority: www.example.com\n" ":method: GET\n" ":scheme: http\n" "cache-control: no-cache\n"}, {"8287 85bf 400a 6375 7374 6f6d 2d6b 6579" "0c63 7573 746f 6d2d 7661 6c75 65", ":path: /index.html\n" ":authority: www.example.com\n" ":method: GET\n" ":scheme: https\n" "custom-key: custom-value\n"}, }}, Test{{}, { {"8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4" "ff", ":path: /\n" ":authority: www.example.com\n" ":method: GET\n" ":scheme: http\n"}, {"8286 84be 5886 a8eb 1064 9cbf", ":path: /\n" ":authority: www.example.com\n" ":method: GET\n" ":scheme: http\n" "cache-control: no-cache\n"}, {"8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925" "a849 e95b b8e8 b4bf", ":path: /index.html\n" ":authority: www.example.com\n" ":method: GET\n" ":scheme: https\n" "custom-key: custom-value\n"}, }}, Test{{256}, { {"4803 3330 3258 0770 7269 7661 7465 611d" "4d6f 6e2c 2032 3120 4f63 7420 3230 3133" "2032 303a 3133 3a32 3120 474d 546e 1768" "7474 7073 3a2f 2f77 7777 2e65 7861 6d70" "6c65 2e63 6f6d", ":status: 302\n" "cache-control: private\n" "date: Mon, 21 Oct 2013 20:13:21 GMT\n" "location: https://www.example.com\n"}, {"4803 3330 37c1 c0bf", ":status: 307\n" "cache-control: private\n" "date: Mon, 21 Oct 2013 20:13:21 GMT\n" "location: https://www.example.com\n"}, {"88c1 611d 4d6f 6e2c 2032 3120 4f63 7420" "3230 3133 2032 303a 3133 3a32 3220 474d" "54c0 5a04 677a 6970 7738 666f 6f3d 4153" "444a 4b48 514b 425a 584f 5157 454f 5049" "5541 5851 5745 4f49 553b 206d 6178 2d61" "6765 3d33 3630 303b 2076 6572 7369 6f6e" "3d31", ":status: 200\n" "cache-control: private\n" "date: Mon, 21 Oct 2013 20:13:22 GMT\n" "location: https://www.example.com\n" "content-encoding: gzip\n" "set-cookie: foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; " "version=1\n"}, }}, Test{{256}, { {"4882 6402 5885 aec3 771a 4b61 96d0 7abe" "9410 54d4 44a8 2005 9504 0b81 66e0 82a6" "2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8" "e9ae 82ae 43d3", ":status: 302\n" "cache-control: private\n" "date: Mon, 21 Oct 2013 20:13:21 GMT\n" "location: https://www.example.com\n"}, {"4883 640e ffc1 c0bf", ":status: 307\n" "cache-control: private\n" "date: Mon, 21 Oct 2013 20:13:21 GMT\n" "location: https://www.example.com\n"}, {"88c1 6196 d07a be94 1054 d444 a820 0595" "040b 8166 e084 a62d 1bff c05a 839b d9ab" "77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b" "3960 d5af 2708 7f36 72c1 ab27 0fb5 291f" "9587 3160 65c0 03ed 4ee5 b106 3d50 07", ":status: 200\n" "cache-control: private\n" "date: Mon, 21 Oct 2013 20:13:22 GMT\n" "location: https://www.example.com\n" "content-encoding: gzip\n" "set-cookie: foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; " "version=1\n"}, }}, Test{{}, { {"40 09 61 2e 62 2e 63 2d 62 69 6e 0c 62 32 31 6e 4d 6a 41 79 " "4d 51 3d 3d", "a.b.c-bin: omg2021\n"}, }}))
grpc_metadata_batch
Definition: metadata_batch.h:1259
ParseTest::TestVector
void TestVector(grpc_slice_split_mode mode, const char *hexstring, std::string expect)
Definition: hpack_parser_test.cc:73
GRPC_SLICE_SPLIT_ONE_BYTE
@ GRPC_SLICE_SPLIT_ONE_BYTE
Definition: slice_splitter.h:34
parse_hexstring
grpc_slice parse_hexstring(const char *hexstring)
Definition: parse_hexstring.cc:23
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
Test
Definition: hpack_parser_test.cc:43
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
ParseTest::~ParseTest
~ParseTest() override
Definition: hpack_parser_test.cc:55


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