• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/grpc.h>
22 
23 #include <string>
24 #include <utility>
25 
26 #include "absl/strings/str_cat.h"
27 #include "gtest/gtest.h"
28 #include "src/core/lib/iomgr/exec_ctx.h"
29 #include "src/core/lib/slice/slice.h"
30 #include "src/core/telemetry/stats.h"
31 #include "test/core/test_util/test_config.h"
32 
33 namespace grpc_core {
34 namespace {
AssertIndex(HPackTable * tbl,uint32_t idx,const char * key,const char * value)35 void AssertIndex(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   auto stats_before = global_stats().Collect();
117 
118   for (i = 0; i < 100000; i++) {
119     std::string key = absl::StrCat("K.", i);
120     std::string value = absl::StrCat("VALUE.", i);
121     auto key_slice = Slice::FromCopiedString(key);
122     auto value_slice = Slice::FromCopiedString(value);
123     auto memento = HPackTable::Memento{
124         ParsedMetadata<grpc_metadata_batch>(
125             ParsedMetadata<grpc_metadata_batch>::FromSlicePair{},
126             std::move(key_slice), std::move(value_slice),
127             key.length() + value.length() + 32),
128         nullptr};
129     ASSERT_TRUE(tbl.Add(std::move(memento)));
130     AssertIndex(&tbl, 1 + hpack_constants::kLastStaticEntry, key.c_str(),
131                 value.c_str());
132     if (i) {
133       std::string key = absl::StrCat("K.", i - 1);
134       std::string value = absl::StrCat("VALUE.", i - 1);
135       AssertIndex(&tbl, 2 + hpack_constants::kLastStaticEntry, key.c_str(),
136                   value.c_str());
137     }
138   }
139 
140   auto stats_after = global_stats().Collect();
141 
142   EXPECT_EQ(stats_after->http2_hpack_hits - stats_before->http2_hpack_hits,
143             100000);
144   EXPECT_EQ(stats_after->http2_hpack_misses, stats_before->http2_hpack_misses);
145 }
146 
TEST(HpackParserTableTest,ManyUnusedAdditions)147 TEST(HpackParserTableTest, ManyUnusedAdditions) {
148   auto tbl = std::make_unique<HPackTable>();
149   int i;
150 
151   ExecCtx exec_ctx;
152 
153   auto stats_before = global_stats().Collect();
154   const Timestamp start = Timestamp::Now();
155 
156   for (i = 0; i < 100000; i++) {
157     std::string key = absl::StrCat("K.", i);
158     std::string value = absl::StrCat("VALUE.", i);
159     auto key_slice = Slice::FromCopiedString(key);
160     auto value_slice = Slice::FromCopiedString(value);
161     auto memento = HPackTable::Memento{
162         ParsedMetadata<grpc_metadata_batch>(
163             ParsedMetadata<grpc_metadata_batch>::FromSlicePair{},
164             std::move(key_slice), std::move(value_slice),
165             key.length() + value.length() + 32),
166         nullptr};
167     ASSERT_TRUE(tbl->Add(std::move(memento)));
168   }
169 
170   tbl.reset();
171 
172   auto stats_after = global_stats().Collect();
173   const Timestamp end = Timestamp::Now();
174 
175   EXPECT_EQ(stats_after->http2_hpack_hits, stats_before->http2_hpack_hits);
176   EXPECT_EQ(stats_after->http2_hpack_misses - stats_before->http2_hpack_misses,
177             100000);
178 
179   size_t num_buckets_changed = 0;
180   const auto& lifetime_before = stats_before->http2_hpack_entry_lifetime;
181   const auto& lifetime_after = stats_after->http2_hpack_entry_lifetime;
182   for (size_t i = 0; i < lifetime_before.bucket_count(); i++) {
183     if (lifetime_before.buckets()[i] != lifetime_after.buckets()[i]) {
184       EXPECT_LE(i, lifetime_before.BucketFor((end - start).millis()));
185       num_buckets_changed++;
186     }
187   }
188   EXPECT_GT(num_buckets_changed, 0);
189 }
190 
191 }  // namespace grpc_core
192 
main(int argc,char ** argv)193 int main(int argc, char** argv) {
194   ::testing::InitGoogleTest(&argc, argv);
195   grpc::testing::TestEnvironment env(&argc, argv);
196   grpc_init();
197   int r = RUN_ALL_TESTS();
198   grpc_shutdown();
199   return r;
200 }
201