| 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.h" |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
|
| 23 |
#include <memory> |
| 24 |
#include <string> |
| 25 |
|
| 26 |
#include "absl/status/status.h" |
| 27 |
#include "absl/strings/str_cat.h" |
| 28 |
#include "absl/types/optional.h" |
| 29 |
#include "gtest/gtest.h" |
| 30 |
|
| 31 |
#include <grpc/event_engine/memory_allocator.h> |
| 32 |
#include <grpc/grpc.h> |
| 33 |
#include <grpc/slice.h> |
| 34 |
#include <grpc/status.h> |
| 35 |
#include <grpc/support/alloc.h> |
| 36 |
#include <grpc/support/log.h> |
| 37 |
|
| 38 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 39 |
#include "src/core/lib/gprpp/status_helper.h" |
| 40 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 41 |
#include "src/core/lib/resource_quota/arena.h" |
| 42 |
#include "src/core/lib/resource_quota/memory_quota.h" |
| 43 |
#include "src/core/lib/resource_quota/resource_quota.h" |
| 44 |
#include "src/core/lib/slice/slice.h" |
| 45 |
#include "test/core/util/parse_hexstring.h" |
| 46 |
#include "test/core/util/slice_splitter.h" |
| 47 |
#include "test/core/util/test_config.h" |
| 48 |
|
| 49 |
static auto* g_memory_allocator = new grpc_core::MemoryAllocator( |
| 50 |
grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator( |
| 51 |
"test")); |
| 52 |
|
| 53 |
struct TestInput { |
| 54 |
const char* input; |
| 55 |
const char* expected_parse; |
| 56 |
}; |
| 57 |
|
| 58 |
struct Test { |
| 59 |
absl::optional<size_t> table_size; |
| 60 |
std::vector<TestInput> inputs; |
| 61 |
}; |
| 62 |
|
| 63 |
class ParseTest : public ::testing::TestWithParam<Test> { |
| 64 |
public: |
| 65 |
ParseTest() { |
| 66 |
grpc_init(); |
| 67 |
parser_ = std::make_unique<grpc_core::HPackParser>(); |
| 68 |
} |
| 69 |
|
| 70 |
~ParseTest() override { |
| 71 |
{ |
| 72 |
grpc_core::ExecCtx exec_ctx; |
| 73 |
parser_.reset(); |
| 74 |
} |
| 75 |
|
| 76 |
grpc_shutdown(); |
| 77 |
} |
| 78 |
|
| 79 |
void SetUp() override { |
| 80 |
if (GetParam().table_size.has_value()) { |
| 81 |
parser_->hpack_table()->SetMaxBytes(GetParam().table_size.value()); |
| 82 |
EXPECT_EQ(parser_->hpack_table()->SetCurrentTableSize( |
| 83 |
GetParam().table_size.value()), |
| 84 |
absl::OkStatus()); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void TestVector(grpc_slice_split_mode mode, const char* hexstring, |
| 89 |
std::string expect) { |
| 90 |
auto arena = grpc_core::MakeScopedArena(1024, g_memory_allocator); |
| 91 |
grpc_core::ExecCtx exec_ctx; |
| 92 |
grpc_slice input = parse_hexstring(hexstring); |
| 93 |
grpc_slice* slices; |
| 94 |
size_t nslices; |
| 95 |
size_t i; |
| 96 |
|
| 97 |
grpc_metadata_batch b(arena.get()); |
| 98 |
|
| 99 |
parser_->BeginFrame( |
| 100 |
&b, 4096, grpc_core::HPackParser::Boundary::None, |
| 101 |
grpc_core::HPackParser::Priority::None, |
| 102 |
grpc_core::HPackParser::LogInfo{ |
| 103 |
1, grpc_core::HPackParser::LogInfo::kHeaders, false}); |
| 104 |
|
| 105 |
grpc_split_slices(mode, &input, 1, &slices, &nslices); |
| 106 |
grpc_slice_unref(input); |
| 107 |
|
| 108 |
for (i = 0; i < nslices; i++) { |
| 109 |
grpc_core::ExecCtx exec_ctx; |
| 110 |
auto err = parser_->Parse(slices[i], i == nslices - 1); |
| 111 |
if (!err.ok()) { |
| 112 |
gpr_log(GPR_ERROR, "Unexpected parse error: %s", |
| 113 |
grpc_core::StatusToString(err).c_str()); |
| 114 |
abort(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
for (i = 0; i < nslices; i++) { |
| 119 |
grpc_slice_unref(slices[i]); |
| 120 |
} |
| 121 |
gpr_free(slices); |
| 122 |
|
| 123 |
TestEncoder encoder; |
| 124 |
b.Encode(&encoder); |
| 125 |
EXPECT_EQ(encoder.result(), expect); |
| 126 |
} |
| 127 |
|
| 128 |
private: |
| 129 |
class TestEncoder { |
| 130 |
public: |
| 131 |
std::string result() { return out_; } |
| 132 |
|
| 133 |
void Encode(const grpc_core::Slice& key, const grpc_core::Slice& value) { |
| 134 |
out_.append(absl::StrCat(key.as_string_view(), ": ", |
| 135 |
value.as_string_view(), "\n")); |
| 136 |
} |
| 137 |
|
| 138 |
template <typename T, typename V> |
| 139 |
void Encode(T, const V& v) { |
| 140 |
out_.append( |
| 141 |
absl::StrCat(T::key(), ": ", T::Encode(v).as_string_view(), "\n")); |
| 142 |
} |
| 143 |
|
| 144 |
private: |
| 145 |
std::string out_; |
| 146 |
}; |
| 147 |
|
| 148 |
std::unique_ptr<grpc_core::HPackParser> parser_; |
| 149 |
}; |
| 150 |
|
| 151 |
TEST_P(ParseTest, WholeSlices) { |
| 152 |
for (const auto& input : GetParam().inputs) { |
| 153 |
TestVector(GRPC_SLICE_SPLIT_MERGE_ALL, input.input, input.expected_parse); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
TEST_P(ParseTest, OneByteAtATime) { |
| 158 |
for (const auto& input : GetParam().inputs) { |
| 159 |
TestVector(GRPC_SLICE_SPLIT_ONE_BYTE, input.input, input.expected_parse); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
INSTANTIATE_TEST_SUITE_P( |
| 164 |
ParseTest, ParseTest, |
| 165 |
::testing::Values( |
| 166 |
Test{ |
| 167 |
{}, |
| 168 |
{ |
| 169 |
/* D.2.1 */ |
| 170 |
{"400a 6375 7374 6f6d 2d6b 6579 0d63 7573" |
| 171 |
"746f 6d2d 6865 6164 6572", |
| 172 |
"custom-key: custom-header\n"}, |
| 173 |
/* D.2.2 */ |
| 174 |
{"040c 2f73 616d 706c 652f 7061 7468", ":path: /sample/path\n"}, |
| 175 |
/* D.2.3 */ |
| 176 |
{"1008 7061 7373 776f 7264 0673 6563 7265" |
| 177 |
"74", |
| 178 |
"password: secret\n"}, |
| 179 |
/* D.2.4 */ |
| 180 |
{"82", ":method: GET\n"}, |
| 181 |
}}, |
| 182 |
Test{{}, |
| 183 |
{ |
| 184 |
/* D.3.1 */ |
| 185 |
{"8286 8441 0f77 7777 2e65 7861 6d70 6c65" |
| 186 |
"2e63 6f6d", |
| 187 |
":path: /\n" |
| 188 |
":authority: www.example.com\n" |
| 189 |
":method: GET\n" |
| 190 |
":scheme: http\n"}, |
| 191 |
/* D.3.2 */ |
| 192 |
{"8286 84be 5808 6e6f 2d63 6163 6865", |
| 193 |
":path: /\n" |
| 194 |
":authority: www.example.com\n" |
| 195 |
":method: GET\n" |
| 196 |
":scheme: http\n" |
| 197 |
"cache-control: no-cache\n"}, |
| 198 |
/* D.3.3 */ |
| 199 |
{"8287 85bf 400a 6375 7374 6f6d 2d6b 6579" |
| 200 |
"0c63 7573 746f 6d2d 7661 6c75 65", |
| 201 |
":path: /index.html\n" |
| 202 |
":authority: www.example.com\n" |
| 203 |
":method: GET\n" |
| 204 |
":scheme: https\n" |
| 205 |
"custom-key: custom-value\n"}, |
| 206 |
}}, |
| 207 |
Test{{}, |
| 208 |
{ |
| 209 |
/* D.4.1 */ |
| 210 |
{"8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4" |
| 211 |
"ff", |
| 212 |
":path: /\n" |
| 213 |
":authority: www.example.com\n" |
| 214 |
":method: GET\n" |
| 215 |
":scheme: http\n"}, |
| 216 |
/* D.4.2 */ |
| 217 |
{"8286 84be 5886 a8eb 1064 9cbf", |
| 218 |
":path: /\n" |
| 219 |
":authority: www.example.com\n" |
| 220 |
":method: GET\n" |
| 221 |
":scheme: http\n" |
| 222 |
"cache-control: no-cache\n"}, |
| 223 |
/* D.4.3 */ |
| 224 |
{"8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925" |
| 225 |
"a849 e95b b8e8 b4bf", |
| 226 |
":path: /index.html\n" |
| 227 |
":authority: www.example.com\n" |
| 228 |
":method: GET\n" |
| 229 |
":scheme: https\n" |
| 230 |
"custom-key: custom-value\n"}, |
| 231 |
}}, |
| 232 |
Test{{256}, |
| 233 |
{ |
| 234 |
/* D.5.1 */ |
| 235 |
{"4803 3330 3258 0770 7269 7661 7465 611d" |
| 236 |
"4d6f 6e2c 2032 3120 4f63 7420 3230 3133" |
| 237 |
"2032 303a 3133 3a32 3120 474d 546e 1768" |
| 238 |
"7474 7073 3a2f 2f77 7777 2e65 7861 6d70" |
| 239 |
"6c65 2e63 6f6d", |
| 240 |
":status: 302\n" |
| 241 |
"cache-control: private\n" |
| 242 |
"date: Mon, 21 Oct 2013 20:13:21 GMT\n" |
| 243 |
"location: https://www.example.com\n"}, |
| 244 |
/* D.5.2 */ |
| 245 |
{"4803 3330 37c1 c0bf", |
| 246 |
":status: 307\n" |
| 247 |
"cache-control: private\n" |
| 248 |
"date: Mon, 21 Oct 2013 20:13:21 GMT\n" |
| 249 |
"location: https://www.example.com\n"}, |
| 250 |
/* D.5.3 */ |
| 251 |
{"88c1 611d 4d6f 6e2c 2032 3120 4f63 7420" |
| 252 |
"3230 3133 2032 303a 3133 3a32 3220 474d" |
| 253 |
"54c0 5a04 677a 6970 7738 666f 6f3d 4153" |
| 254 |
"444a 4b48 514b 425a 584f 5157 454f 5049" |
| 255 |
"5541 5851 5745 4f49 553b 206d 6178 2d61" |
| 256 |
"6765 3d33 3630 303b 2076 6572 7369 6f6e" |
| 257 |
"3d31", |
| 258 |
":status: 200\n" |
| 259 |
"cache-control: private\n" |
| 260 |
"date: Mon, 21 Oct 2013 20:13:22 GMT\n" |
| 261 |
"location: https://www.example.com\n" |
| 262 |
"content-encoding: gzip\n" |
| 263 |
"set-cookie: foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; " |
| 264 |
"version=1\n"}, |
| 265 |
}}, |
| 266 |
Test{{256}, |
| 267 |
{ |
| 268 |
/* D.6.1 */ |
| 269 |
{"4882 6402 5885 aec3 771a 4b61 96d0 7abe" |
| 270 |
"9410 54d4 44a8 2005 9504 0b81 66e0 82a6" |
| 271 |
"2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8" |
| 272 |
"e9ae 82ae 43d3", |
| 273 |
":status: 302\n" |
| 274 |
"cache-control: private\n" |
| 275 |
"date: Mon, 21 Oct 2013 20:13:21 GMT\n" |
| 276 |
"location: https://www.example.com\n"}, |
| 277 |
/* D.6.2 */ |
| 278 |
{"4883 640e ffc1 c0bf", |
| 279 |
":status: 307\n" |
| 280 |
"cache-control: private\n" |
| 281 |
"date: Mon, 21 Oct 2013 20:13:21 GMT\n" |
| 282 |
"location: https://www.example.com\n"}, |
| 283 |
/* D.6.3 */ |
| 284 |
{"88c1 6196 d07a be94 1054 d444 a820 0595" |
| 285 |
"040b 8166 e084 a62d 1bff c05a 839b d9ab" |
| 286 |
"77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b" |
| 287 |
"3960 d5af 2708 7f36 72c1 ab27 0fb5 291f" |
| 288 |
"9587 3160 65c0 03ed 4ee5 b106 3d50 07", |
| 289 |
":status: 200\n" |
| 290 |
"cache-control: private\n" |
| 291 |
"date: Mon, 21 Oct 2013 20:13:22 GMT\n" |
| 292 |
"location: https://www.example.com\n" |
| 293 |
"content-encoding: gzip\n" |
| 294 |
"set-cookie: foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; " |
| 295 |
"version=1\n"}, |
| 296 |
}}, |
| 297 |
Test{{}, |
| 298 |
{ |
| 299 |
// Binary metadata: created using: |
| 300 |
// tools/codegen/core/gen_header_frame.py |
| 301 |
// --compression inc --no_framing --hex |
| 302 |
// < test/core/transport/chttp2/binary-metadata.headers |
| 303 |
{"40 09 61 2e 62 2e 63 2d 62 69 6e 0c 62 32 31 6e 4d 6a 41 79 " |
| 304 |
"4d 51 3d 3d", |
| 305 |
"a.b.c-bin: omg2021\n"}, |
| 306 |
}})); |
| 307 |
|
| 308 |
int main(int argc, char** argv) { |
| 309 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 310 |
::testing::InitGoogleTest(&argc, argv); |
| 311 |
return RUN_ALL_TESTS(); |
| 312 |
} |