hpack_parser_table_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 <stdio.h>
22 #include <string.h>
23 
24 #include <string>
25 
26 #include <gtest/gtest.h>
27 
28 #include "absl/strings/str_cat.h"
29 
30 #include <grpc/grpc.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 
38 
39 namespace grpc_core {
40 namespace {
41 void AssertIndex(const HPackTable* tbl, uint32_t idx, const char* key,
42  const char* value) {
43  const auto* md = tbl->Lookup(idx);
44  ASSERT_NE(md, nullptr);
45  EXPECT_EQ(md->DebugString(), absl::StrCat(key, ": ", value));
46 }
47 } // namespace
48 
49 TEST(HpackParserTableTest, StaticTable) {
51  HPackTable tbl;
52 
53  AssertIndex(&tbl, 1, ":authority", "");
54  AssertIndex(&tbl, 2, ":method", "GET");
55  AssertIndex(&tbl, 3, ":method", "POST");
56  AssertIndex(&tbl, 4, ":path", "/");
57  AssertIndex(&tbl, 5, ":path", "/index.html");
58  AssertIndex(&tbl, 6, ":scheme", "http");
59  AssertIndex(&tbl, 7, ":scheme", "https");
60  AssertIndex(&tbl, 8, ":status", "200");
61  AssertIndex(&tbl, 9, ":status", "204");
62  AssertIndex(&tbl, 10, ":status", "206");
63  AssertIndex(&tbl, 11, ":status", "304");
64  AssertIndex(&tbl, 12, ":status", "400");
65  AssertIndex(&tbl, 13, ":status", "404");
66  AssertIndex(&tbl, 14, ":status", "500");
67  AssertIndex(&tbl, 15, "accept-charset", "");
68  AssertIndex(&tbl, 16, "accept-encoding", "gzip, deflate");
69  AssertIndex(&tbl, 17, "accept-language", "");
70  AssertIndex(&tbl, 18, "accept-ranges", "");
71  AssertIndex(&tbl, 19, "accept", "");
72  AssertIndex(&tbl, 20, "access-control-allow-origin", "");
73  AssertIndex(&tbl, 21, "age", "");
74  AssertIndex(&tbl, 22, "allow", "");
75  AssertIndex(&tbl, 23, "authorization", "");
76  AssertIndex(&tbl, 24, "cache-control", "");
77  AssertIndex(&tbl, 25, "content-disposition", "");
78  AssertIndex(&tbl, 26, "content-encoding", "");
79  AssertIndex(&tbl, 27, "content-language", "");
80  AssertIndex(&tbl, 28, "content-length", "");
81  AssertIndex(&tbl, 29, "content-location", "");
82  AssertIndex(&tbl, 30, "content-range", "");
83  AssertIndex(&tbl, 31, "content-type", "");
84  AssertIndex(&tbl, 32, "cookie", "");
85  AssertIndex(&tbl, 33, "date", "");
86  AssertIndex(&tbl, 34, "etag", "");
87  AssertIndex(&tbl, 35, "expect", "");
88  AssertIndex(&tbl, 36, "expires", "");
89  AssertIndex(&tbl, 37, "from", "");
90  AssertIndex(&tbl, 38, "host", "");
91  AssertIndex(&tbl, 39, "if-match", "");
92  AssertIndex(&tbl, 40, "if-modified-since", "");
93  AssertIndex(&tbl, 41, "if-none-match", "");
94  AssertIndex(&tbl, 42, "if-range", "");
95  AssertIndex(&tbl, 43, "if-unmodified-since", "");
96  AssertIndex(&tbl, 44, "last-modified", "");
97  AssertIndex(&tbl, 45, "link", "");
98  AssertIndex(&tbl, 46, "location", "");
99  AssertIndex(&tbl, 47, "max-forwards", "");
100  AssertIndex(&tbl, 48, "proxy-authenticate", "");
101  AssertIndex(&tbl, 49, "proxy-authorization", "");
102  AssertIndex(&tbl, 50, "range", "");
103  AssertIndex(&tbl, 51, "referer", "");
104  AssertIndex(&tbl, 52, "refresh", "");
105  AssertIndex(&tbl, 53, "retry-after", "");
106  AssertIndex(&tbl, 54, "server", "");
107  AssertIndex(&tbl, 55, "set-cookie", "");
108  AssertIndex(&tbl, 56, "strict-transport-security", "");
109  AssertIndex(&tbl, 57, "transfer-encoding", "");
110  AssertIndex(&tbl, 58, "user-agent", "");
111  AssertIndex(&tbl, 59, "vary", "");
112  AssertIndex(&tbl, 60, "via", "");
113  AssertIndex(&tbl, 61, "www-authenticate", "");
114 }
115 
116 TEST(HpackParserTableTest, ManyAdditions) {
117  HPackTable tbl;
118  int i;
119 
121 
122  for (i = 0; i < 100000; i++) {
123  std::string key = absl::StrCat("K.", i);
124  std::string value = absl::StrCat("VALUE.", i);
125  auto key_slice = Slice::FromCopiedString(key);
126  auto value_slice = Slice::FromCopiedString(value);
127  auto memento =
128  HPackTable::Memento(std::move(key_slice), std::move(value_slice));
129  auto add_err = tbl.Add(std::move(memento));
130  ASSERT_EQ(add_err, GRPC_ERROR_NONE);
131  AssertIndex(&tbl, 1 + hpack_constants::kLastStaticEntry, key.c_str(),
132  value.c_str());
133  if (i) {
134  std::string key = absl::StrCat("K.", i - 1);
135  std::string value = absl::StrCat("VALUE.", i - 1);
136  AssertIndex(&tbl, 2 + hpack_constants::kLastStaticEntry, key.c_str(),
137  value.c_str());
138  }
139  }
140 }
141 
142 } // namespace grpc_core
143 
144 int main(int argc, char** argv) {
145  ::testing::InitGoogleTest(&argc, argv);
146  grpc::testing::TestEnvironment env(&argc, argv);
147  grpc_init();
148  int r = RUN_ALL_TESTS();
149  grpc_shutdown();
150  return r;
151 }
ASSERT_NE
#define ASSERT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2060
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_core
Definition: call_metric_recorder.h:31
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
grpc_core::HPackTable::Memento
ParsedMetadata< grpc_metadata_batch > Memento
Definition: hpack_parser_table.h:47
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::slice_detail::CopyConstructors< Slice >::FromCopiedString
static Slice FromCopiedString(const char *s)
Definition: src/core/lib/slice/slice.h:173
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::HPackTable::Add
grpc_error_handle Add(Memento md) GRPC_MUST_USE_RESULT
Definition: hpack_parser_table.cc:127
grpc.h
slice_internal.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::ExecCtx
Definition: exec_ctx.h:97
setup.idx
idx
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:197
grpc_core::HPackTable
Definition: hpack_parser_table.h:36
grpc_core::hpack_constants::kLastStaticEntry
static constexpr uint32_t kLastStaticEntry
Definition: hpack_constants.h:30
test_config.h
value
const char * value
Definition: hpack_parser_table.cc:165
hpack_parser_table.h
benchmark.md
md
Definition: benchmark.py:86
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
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
alloc.h
fix_build_deps.r
r
Definition: fix_build_deps.py:491
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
exec_ctx.h
main
int main(int argc, char **argv)
Definition: hpack_parser_table_test.cc:144
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
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:01