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