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