OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/spdy/hpack/hpack_decoder2.h" |
| 6 |
| 7 // Tests of HpackDecoder2. |
| 8 |
| 9 #include <string> |
| 10 #include <tuple> |
| 11 #include <utility> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/logging.h" |
| 15 #include "base/strings/string_piece.h" |
| 16 #include "net/http2/hpack/tools/hpack_block_builder.h" |
| 17 #include "net/http2/tools/http2_random.h" |
| 18 #include "net/spdy/hpack/hpack_encoder.h" |
| 19 #include "net/spdy/hpack/hpack_entry.h" |
| 20 #include "net/spdy/hpack/hpack_huffman_table.h" |
| 21 #include "net/spdy/hpack/hpack_output_stream.h" |
| 22 #include "net/spdy/spdy_test_utils.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 using base::StringPiece; |
| 26 using std::string; |
| 27 |
| 28 namespace net { |
| 29 namespace test { |
| 30 |
| 31 class HpackDecoder2Peer { |
| 32 public: |
| 33 explicit HpackDecoder2Peer(HpackDecoder2* decoder) : decoder_(decoder) {} |
| 34 |
| 35 void HandleHeaderRepresentation(StringPiece name, StringPiece value) { |
| 36 decoder_->HandleHeaderRepresentation(name, value); |
| 37 } |
| 38 HpackHeaderTable* header_table() { return &decoder_->header_table_; } |
| 39 |
| 40 private: |
| 41 HpackDecoder2* decoder_; |
| 42 }; |
| 43 |
| 44 namespace { |
| 45 |
| 46 using testing::ElementsAre; |
| 47 using testing::Pair; |
| 48 |
| 49 // Is HandleControlFrameHeadersStart to be called, and with what value? |
| 50 enum StartChoice { START_WITH_HANDLER, START_WITHOUT_HANDLER, NO_START }; |
| 51 |
| 52 class HpackDecoder2Test |
| 53 : public ::testing::TestWithParam<std::tuple<StartChoice, bool>> { |
| 54 protected: |
| 55 HpackDecoder2Test() : decoder_(), decoder_peer_(&decoder_) {} |
| 56 |
| 57 void SetUp() override { |
| 58 std::tie(start_choice_, randomly_split_input_buffer_) = GetParam(); |
| 59 } |
| 60 |
| 61 void HandleControlFrameHeadersStart() { |
| 62 switch (start_choice_) { |
| 63 case START_WITH_HANDLER: |
| 64 decoder_.HandleControlFrameHeadersStart(&handler_); |
| 65 break; |
| 66 case START_WITHOUT_HANDLER: |
| 67 decoder_.HandleControlFrameHeadersStart(nullptr); |
| 68 break; |
| 69 case NO_START: |
| 70 break; |
| 71 } |
| 72 } |
| 73 |
| 74 bool HandleControlFrameHeadersData(StringPiece str) { |
| 75 return decoder_.HandleControlFrameHeadersData(str.data(), str.size()); |
| 76 } |
| 77 |
| 78 bool HandleControlFrameHeadersComplete(size_t* size) { |
| 79 return decoder_.HandleControlFrameHeadersComplete(size); |
| 80 } |
| 81 |
| 82 bool DecodeHeaderBlock(StringPiece str) { |
| 83 // Don't call this again if HandleControlFrameHeadersData failed previously. |
| 84 EXPECT_FALSE(decode_has_failed_); |
| 85 HandleControlFrameHeadersStart(); |
| 86 if (randomly_split_input_buffer_) { |
| 87 do { |
| 88 // Decode some fragment of the remaining bytes. |
| 89 size_t bytes = str.length(); |
| 90 if (!str.empty()) { |
| 91 bytes = (random_.Rand8() % str.length()) + 1; |
| 92 } |
| 93 EXPECT_LE(bytes, str.length()); |
| 94 if (!HandleControlFrameHeadersData(str.substr(0, bytes))) { |
| 95 decode_has_failed_ = true; |
| 96 return false; |
| 97 } |
| 98 str.remove_prefix(bytes); |
| 99 } while (!str.empty()); |
| 100 } else if (!HandleControlFrameHeadersData(str)) { |
| 101 decode_has_failed_ = true; |
| 102 return false; |
| 103 } |
| 104 if (!HandleControlFrameHeadersComplete(nullptr)) { |
| 105 decode_has_failed_ = true; |
| 106 return false; |
| 107 } |
| 108 return true; |
| 109 } |
| 110 |
| 111 const SpdyHeaderBlock& decoded_block() const { |
| 112 if (start_choice_ == START_WITH_HANDLER) { |
| 113 return handler_.decoded_block(); |
| 114 } else { |
| 115 return decoder_.decoded_block(); |
| 116 } |
| 117 } |
| 118 |
| 119 const SpdyHeaderBlock& DecodeBlockExpectingSuccess(StringPiece str) { |
| 120 EXPECT_TRUE(DecodeHeaderBlock(str)); |
| 121 return decoded_block(); |
| 122 } |
| 123 |
| 124 void expectEntry(size_t index, |
| 125 size_t size, |
| 126 const string& name, |
| 127 const string& value) { |
| 128 const HpackEntry* entry = decoder_peer_.header_table()->GetByIndex(index); |
| 129 EXPECT_EQ(name, entry->name()) << "index " << index; |
| 130 EXPECT_EQ(value, entry->value()); |
| 131 EXPECT_EQ(size, entry->Size()); |
| 132 EXPECT_EQ(index, decoder_peer_.header_table()->IndexOf(entry)); |
| 133 } |
| 134 |
| 135 SpdyHeaderBlock MakeHeaderBlock( |
| 136 const std::vector<std::pair<string, string>>& headers) { |
| 137 SpdyHeaderBlock result; |
| 138 for (const auto& kv : headers) { |
| 139 result.AppendValueOrAddHeader(kv.first, kv.second); |
| 140 } |
| 141 return result; |
| 142 } |
| 143 |
| 144 Http2Random random_; |
| 145 HpackDecoder2 decoder_; |
| 146 test::HpackDecoder2Peer decoder_peer_; |
| 147 TestHeadersHandler handler_; |
| 148 StartChoice start_choice_; |
| 149 bool randomly_split_input_buffer_; |
| 150 bool decode_has_failed_ = false; |
| 151 }; |
| 152 |
| 153 INSTANTIATE_TEST_CASE_P( |
| 154 StartChoiceAndRandomlySplitChoice, |
| 155 HpackDecoder2Test, |
| 156 ::testing::Combine( |
| 157 ::testing::Values(START_WITH_HANDLER, START_WITHOUT_HANDLER, NO_START), |
| 158 ::testing::Bool())); |
| 159 |
| 160 TEST_P(HpackDecoder2Test, AddHeaderDataWithHandleControlFrameHeadersData) { |
| 161 // The hpack decode buffer size is limited in size. This test verifies that |
| 162 // adding encoded data under that limit is accepted, and data that exceeds the |
| 163 // limit is rejected. |
| 164 HandleControlFrameHeadersStart(); |
| 165 const size_t kMaxBufferSizeBytes = 50; |
| 166 const string a_value = string(49, 'x'); |
| 167 decoder_.set_max_decode_buffer_size_bytes(kMaxBufferSizeBytes); |
| 168 { |
| 169 HpackBlockBuilder hbb; |
| 170 hbb.AppendLiteralNameAndValue(HpackEntryType::kNeverIndexedLiteralHeader, |
| 171 false, "a", false, a_value); |
| 172 const auto& s = hbb.buffer(); |
| 173 EXPECT_TRUE(decoder_.HandleControlFrameHeadersData(s.data(), s.size())); |
| 174 } |
| 175 { |
| 176 HpackBlockBuilder hbb; |
| 177 hbb.AppendLiteralNameAndValue(HpackEntryType::kNeverIndexedLiteralHeader, |
| 178 false, "b", false, string(51, 'x')); |
| 179 const auto& s = hbb.buffer(); |
| 180 EXPECT_FALSE(decoder_.HandleControlFrameHeadersData(s.data(), s.size())); |
| 181 } |
| 182 |
| 183 SpdyHeaderBlock expected_block = MakeHeaderBlock({{"a", a_value}}); |
| 184 EXPECT_EQ(expected_block, decoded_block()); |
| 185 } |
| 186 |
| 187 TEST_P(HpackDecoder2Test, NameTooLong) { |
| 188 // Verify that a name longer than the allowed size generates an error. |
| 189 const size_t kMaxBufferSizeBytes = 50; |
| 190 const string name = string(2 * kMaxBufferSizeBytes, 'x'); |
| 191 const string value = "abc"; |
| 192 |
| 193 decoder_.set_max_decode_buffer_size_bytes(kMaxBufferSizeBytes); |
| 194 |
| 195 HpackBlockBuilder hbb; |
| 196 hbb.AppendLiteralNameAndValue(HpackEntryType::kNeverIndexedLiteralHeader, |
| 197 false, name, false, value); |
| 198 |
| 199 const size_t fragment_size = (3 * kMaxBufferSizeBytes) / 2; |
| 200 const string fragment = hbb.buffer().substr(0, fragment_size); |
| 201 |
| 202 HandleControlFrameHeadersStart(); |
| 203 EXPECT_FALSE(HandleControlFrameHeadersData(fragment)); |
| 204 } |
| 205 |
| 206 TEST_P(HpackDecoder2Test, HeaderTooLongToBuffer) { |
| 207 // Verify that a header longer than the allowed size generates an error if |
| 208 // it isn't all in one input buffer. |
| 209 const string name = "some-key"; |
| 210 const string value = "some-value"; |
| 211 const size_t kMaxBufferSizeBytes = name.size() + value.size() - 2; |
| 212 decoder_.set_max_decode_buffer_size_bytes(kMaxBufferSizeBytes); |
| 213 |
| 214 HpackBlockBuilder hbb; |
| 215 hbb.AppendLiteralNameAndValue(HpackEntryType::kNeverIndexedLiteralHeader, |
| 216 false, name, false, value); |
| 217 const size_t fragment_size = hbb.size() - 1; |
| 218 const string fragment = hbb.buffer().substr(0, fragment_size); |
| 219 |
| 220 HandleControlFrameHeadersStart(); |
| 221 EXPECT_FALSE(HandleControlFrameHeadersData(fragment)); |
| 222 } |
| 223 |
| 224 // Decode with incomplete data in buffer. |
| 225 TEST_P(HpackDecoder2Test, DecodeWithIncompleteData) { |
| 226 HandleControlFrameHeadersStart(); |
| 227 |
| 228 // No need to wait for more data. |
| 229 EXPECT_TRUE(HandleControlFrameHeadersData("\x82\x85\x82")); |
| 230 std::vector<std::pair<string, string>> expected_headers = { |
| 231 {":method", "GET"}, {":path", "/index.html"}, {":method", "GET"}}; |
| 232 |
| 233 SpdyHeaderBlock expected_block1 = MakeHeaderBlock(expected_headers); |
| 234 EXPECT_EQ(expected_block1, decoded_block()); |
| 235 |
| 236 // Full and partial headers, won't add partial to the headers. |
| 237 EXPECT_TRUE( |
| 238 HandleControlFrameHeadersData("\x40\x03goo" |
| 239 "\x03gar\xbe\x40\x04spam")); |
| 240 expected_headers.push_back({"goo", "gar"}); |
| 241 expected_headers.push_back({"goo", "gar"}); |
| 242 |
| 243 SpdyHeaderBlock expected_block2 = MakeHeaderBlock(expected_headers); |
| 244 EXPECT_EQ(expected_block2, decoded_block()); |
| 245 |
| 246 // Add the needed data. |
| 247 EXPECT_TRUE(HandleControlFrameHeadersData("\x04gggs")); |
| 248 |
| 249 size_t size = 0; |
| 250 EXPECT_TRUE(HandleControlFrameHeadersComplete(&size)); |
| 251 EXPECT_EQ(24u, size); |
| 252 |
| 253 expected_headers.push_back({"spam", "gggs"}); |
| 254 |
| 255 SpdyHeaderBlock expected_block3 = MakeHeaderBlock(expected_headers); |
| 256 EXPECT_EQ(expected_block3, decoded_block()); |
| 257 } |
| 258 |
| 259 TEST_P(HpackDecoder2Test, HandleHeaderRepresentation) { |
| 260 // Make sure the decoder is properly initialized. |
| 261 HandleControlFrameHeadersStart(); |
| 262 HandleControlFrameHeadersData(""); |
| 263 |
| 264 // All cookie crumbs are joined. |
| 265 decoder_peer_.HandleHeaderRepresentation("cookie", " part 1"); |
| 266 decoder_peer_.HandleHeaderRepresentation("cookie", "part 2 "); |
| 267 decoder_peer_.HandleHeaderRepresentation("cookie", "part3"); |
| 268 |
| 269 // Already-delimited headers are passed through. |
| 270 decoder_peer_.HandleHeaderRepresentation("passed-through", |
| 271 string("foo\0baz", 7)); |
| 272 |
| 273 // Other headers are joined on \0. Case matters. |
| 274 decoder_peer_.HandleHeaderRepresentation("joined", "not joined"); |
| 275 decoder_peer_.HandleHeaderRepresentation("joineD", "value 1"); |
| 276 decoder_peer_.HandleHeaderRepresentation("joineD", "value 2"); |
| 277 |
| 278 // Empty headers remain empty. |
| 279 decoder_peer_.HandleHeaderRepresentation("empty", ""); |
| 280 |
| 281 // Joined empty headers work as expected. |
| 282 decoder_peer_.HandleHeaderRepresentation("empty-joined", ""); |
| 283 decoder_peer_.HandleHeaderRepresentation("empty-joined", "foo"); |
| 284 decoder_peer_.HandleHeaderRepresentation("empty-joined", ""); |
| 285 decoder_peer_.HandleHeaderRepresentation("empty-joined", ""); |
| 286 |
| 287 // Non-contiguous cookie crumb. |
| 288 decoder_peer_.HandleHeaderRepresentation("cookie", " fin!"); |
| 289 |
| 290 // Finish and emit all headers. |
| 291 decoder_.HandleControlFrameHeadersComplete(nullptr); |
| 292 |
| 293 // Resulting decoded headers are in the same order as the inputs. |
| 294 EXPECT_THAT(decoded_block(), |
| 295 ElementsAre(Pair("cookie", " part 1; part 2 ; part3; fin!"), |
| 296 Pair("passed-through", StringPiece("foo\0baz", 7)), |
| 297 Pair("joined", "not joined"), |
| 298 Pair("joineD", StringPiece("value 1\0value 2", 15)), |
| 299 Pair("empty", ""), |
| 300 Pair("empty-joined", StringPiece("\0foo\0\0", 6)))); |
| 301 } |
| 302 |
| 303 // Decoding indexed static table field should work. |
| 304 TEST_P(HpackDecoder2Test, IndexedHeaderStatic) { |
| 305 // Reference static table entries #2 and #5. |
| 306 const SpdyHeaderBlock& header_set1 = DecodeBlockExpectingSuccess("\x82\x85"); |
| 307 SpdyHeaderBlock expected_header_set1; |
| 308 expected_header_set1[":method"] = "GET"; |
| 309 expected_header_set1[":path"] = "/index.html"; |
| 310 EXPECT_EQ(expected_header_set1, header_set1); |
| 311 |
| 312 // Reference static table entry #2. |
| 313 const SpdyHeaderBlock& header_set2 = DecodeBlockExpectingSuccess("\x82"); |
| 314 SpdyHeaderBlock expected_header_set2; |
| 315 expected_header_set2[":method"] = "GET"; |
| 316 EXPECT_EQ(expected_header_set2, header_set2); |
| 317 } |
| 318 |
| 319 TEST_P(HpackDecoder2Test, IndexedHeaderDynamic) { |
| 320 // First header block: add an entry to header table. |
| 321 const SpdyHeaderBlock& header_set1 = DecodeBlockExpectingSuccess( |
| 322 "\x40\x03" |
| 323 "foo" |
| 324 "\x03" |
| 325 "bar"); |
| 326 SpdyHeaderBlock expected_header_set1; |
| 327 expected_header_set1["foo"] = "bar"; |
| 328 EXPECT_EQ(expected_header_set1, header_set1); |
| 329 |
| 330 // Second header block: add another entry to header table. |
| 331 const SpdyHeaderBlock& header_set2 = DecodeBlockExpectingSuccess( |
| 332 "\xbe\x40\x04" |
| 333 "spam" |
| 334 "\x04" |
| 335 "eggs"); |
| 336 SpdyHeaderBlock expected_header_set2; |
| 337 expected_header_set2["foo"] = "bar"; |
| 338 expected_header_set2["spam"] = "eggs"; |
| 339 EXPECT_EQ(expected_header_set2, header_set2); |
| 340 |
| 341 // Third header block: refer to most recently added entry. |
| 342 const SpdyHeaderBlock& header_set3 = DecodeBlockExpectingSuccess("\xbe"); |
| 343 SpdyHeaderBlock expected_header_set3; |
| 344 expected_header_set3["spam"] = "eggs"; |
| 345 EXPECT_EQ(expected_header_set3, header_set3); |
| 346 } |
| 347 |
| 348 // Test a too-large indexed header. |
| 349 TEST_P(HpackDecoder2Test, InvalidIndexedHeader) { |
| 350 // High-bit set, and a prefix of one more than the number of static entries. |
| 351 EXPECT_FALSE(DecodeHeaderBlock("\xbe")); |
| 352 } |
| 353 |
| 354 TEST_P(HpackDecoder2Test, ContextUpdateMaximumSize) { |
| 355 EXPECT_EQ(kDefaultHeaderTableSizeSetting, |
| 356 decoder_peer_.header_table()->max_size()); |
| 357 string input; |
| 358 { |
| 359 // Maximum-size update with size 126. Succeeds. |
| 360 HpackOutputStream output_stream; |
| 361 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 362 output_stream.AppendUint32(126); |
| 363 |
| 364 output_stream.TakeString(&input); |
| 365 EXPECT_TRUE(DecodeHeaderBlock(StringPiece(input))); |
| 366 EXPECT_EQ(126u, decoder_peer_.header_table()->max_size()); |
| 367 } |
| 368 { |
| 369 // Maximum-size update with kDefaultHeaderTableSizeSetting. Succeeds. |
| 370 HpackOutputStream output_stream; |
| 371 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 372 output_stream.AppendUint32(kDefaultHeaderTableSizeSetting); |
| 373 |
| 374 output_stream.TakeString(&input); |
| 375 EXPECT_TRUE(DecodeHeaderBlock(StringPiece(input))); |
| 376 EXPECT_EQ(kDefaultHeaderTableSizeSetting, |
| 377 decoder_peer_.header_table()->max_size()); |
| 378 } |
| 379 { |
| 380 // Maximum-size update with kDefaultHeaderTableSizeSetting + 1. Fails. |
| 381 HpackOutputStream output_stream; |
| 382 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 383 output_stream.AppendUint32(kDefaultHeaderTableSizeSetting + 1); |
| 384 |
| 385 output_stream.TakeString(&input); |
| 386 EXPECT_FALSE(DecodeHeaderBlock(StringPiece(input))); |
| 387 EXPECT_EQ(kDefaultHeaderTableSizeSetting, |
| 388 decoder_peer_.header_table()->max_size()); |
| 389 } |
| 390 } |
| 391 |
| 392 // Two HeaderTableSizeUpdates may appear at the beginning of the block |
| 393 TEST_P(HpackDecoder2Test, TwoTableSizeUpdates) { |
| 394 string input; |
| 395 { |
| 396 // Should accept two table size updates, update to second one |
| 397 HpackOutputStream output_stream; |
| 398 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 399 output_stream.AppendUint32(0); |
| 400 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 401 output_stream.AppendUint32(122); |
| 402 |
| 403 output_stream.TakeString(&input); |
| 404 EXPECT_TRUE(DecodeHeaderBlock(StringPiece(input))); |
| 405 EXPECT_EQ(122u, decoder_peer_.header_table()->max_size()); |
| 406 } |
| 407 } |
| 408 |
| 409 // Three HeaderTableSizeUpdates should result in an error |
| 410 TEST_P(HpackDecoder2Test, ThreeTableSizeUpdatesError) { |
| 411 string input; |
| 412 { |
| 413 // Should reject three table size updates, update to second one |
| 414 HpackOutputStream output_stream; |
| 415 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 416 output_stream.AppendUint32(5); |
| 417 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 418 output_stream.AppendUint32(10); |
| 419 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 420 output_stream.AppendUint32(15); |
| 421 |
| 422 output_stream.TakeString(&input); |
| 423 |
| 424 EXPECT_FALSE(DecodeHeaderBlock(StringPiece(input))); |
| 425 EXPECT_EQ(10u, decoder_peer_.header_table()->max_size()); |
| 426 } |
| 427 } |
| 428 |
| 429 // HeaderTableSizeUpdates may only appear at the beginning of the block |
| 430 // Any other updates should result in an error |
| 431 TEST_P(HpackDecoder2Test, TableSizeUpdateSecondError) { |
| 432 string input; |
| 433 { |
| 434 // Should reject a table size update appearing after a different entry |
| 435 // The table size should remain as the default |
| 436 HpackOutputStream output_stream; |
| 437 output_stream.AppendBytes("\x82\x85"); |
| 438 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 439 output_stream.AppendUint32(123); |
| 440 |
| 441 output_stream.TakeString(&input); |
| 442 |
| 443 EXPECT_FALSE(DecodeHeaderBlock(StringPiece(input))); |
| 444 EXPECT_EQ(kDefaultHeaderTableSizeSetting, |
| 445 decoder_peer_.header_table()->max_size()); |
| 446 } |
| 447 } |
| 448 |
| 449 // HeaderTableSizeUpdates may only appear at the beginning of the block |
| 450 // Any other updates should result in an error |
| 451 TEST_P(HpackDecoder2Test, TableSizeUpdateFirstThirdError) { |
| 452 string input; |
| 453 { |
| 454 // Should reject the second table size update |
| 455 // if a different entry appears after the first update |
| 456 // The table size should update to the first but not the second |
| 457 HpackOutputStream output_stream; |
| 458 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 459 output_stream.AppendUint32(60); |
| 460 output_stream.AppendBytes("\x82\x85"); |
| 461 output_stream.AppendPrefix(kHeaderTableSizeUpdateOpcode); |
| 462 output_stream.AppendUint32(125); |
| 463 |
| 464 output_stream.TakeString(&input); |
| 465 |
| 466 EXPECT_FALSE(DecodeHeaderBlock(StringPiece(input))); |
| 467 EXPECT_EQ(60u, decoder_peer_.header_table()->max_size()); |
| 468 } |
| 469 } |
| 470 |
| 471 // Decoding two valid encoded literal headers with no indexing should |
| 472 // work. |
| 473 TEST_P(HpackDecoder2Test, LiteralHeaderNoIndexing) { |
| 474 // First header with indexed name, second header with string literal |
| 475 // name. |
| 476 const char input[] = "\x04\x0c/sample/path\x00\x06:path2\x0e/sample/path/2"; |
| 477 const SpdyHeaderBlock& header_set = |
| 478 DecodeBlockExpectingSuccess(StringPiece(input, arraysize(input) - 1)); |
| 479 |
| 480 SpdyHeaderBlock expected_header_set; |
| 481 expected_header_set[":path"] = "/sample/path"; |
| 482 expected_header_set[":path2"] = "/sample/path/2"; |
| 483 EXPECT_EQ(expected_header_set, header_set); |
| 484 } |
| 485 |
| 486 // Decoding two valid encoded literal headers with incremental |
| 487 // indexing and string literal names should work. |
| 488 TEST_P(HpackDecoder2Test, LiteralHeaderIncrementalIndexing) { |
| 489 const char input[] = "\x44\x0c/sample/path\x40\x06:path2\x0e/sample/path/2"; |
| 490 const SpdyHeaderBlock& header_set = |
| 491 DecodeBlockExpectingSuccess(StringPiece(input, arraysize(input) - 1)); |
| 492 |
| 493 SpdyHeaderBlock expected_header_set; |
| 494 expected_header_set[":path"] = "/sample/path"; |
| 495 expected_header_set[":path2"] = "/sample/path/2"; |
| 496 EXPECT_EQ(expected_header_set, header_set); |
| 497 } |
| 498 |
| 499 TEST_P(HpackDecoder2Test, LiteralHeaderWithIndexingInvalidNameIndex) { |
| 500 decoder_.ApplyHeaderTableSizeSetting(0); |
| 501 |
| 502 // Name is the last static index. Works. |
| 503 EXPECT_TRUE(DecodeHeaderBlock(StringPiece("\x7d\x03ooo"))); |
| 504 // Name is one beyond the last static index. Fails. |
| 505 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\x7e\x03ooo"))); |
| 506 } |
| 507 |
| 508 TEST_P(HpackDecoder2Test, LiteralHeaderNoIndexingInvalidNameIndex) { |
| 509 // Name is the last static index. Works. |
| 510 EXPECT_TRUE(DecodeHeaderBlock(StringPiece("\x0f\x2e\x03ooo"))); |
| 511 // Name is one beyond the last static index. Fails. |
| 512 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\x0f\x2f\x03ooo"))); |
| 513 } |
| 514 |
| 515 TEST_P(HpackDecoder2Test, LiteralHeaderNeverIndexedInvalidNameIndex) { |
| 516 // Name is the last static index. Works. |
| 517 EXPECT_TRUE(DecodeHeaderBlock(StringPiece("\x1f\x2e\x03ooo"))); |
| 518 // Name is one beyond the last static index. Fails. |
| 519 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\x1f\x2f\x03ooo"))); |
| 520 } |
| 521 |
| 522 TEST_P(HpackDecoder2Test, TruncatedIndex) { |
| 523 // Indexed Header, varint for index requires multiple bytes, |
| 524 // but only one provided. |
| 525 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\xff", 1))); |
| 526 } |
| 527 |
| 528 TEST_P(HpackDecoder2Test, TruncatedHuffmanLiteral) { |
| 529 // Literal value, Huffman encoded, but with the last byte missing (i.e. |
| 530 // drop the final ff shown below). |
| 531 // |
| 532 // 41 | == Literal indexed == |
| 533 // | Indexed name (idx = 1) |
| 534 // | :authority |
| 535 // 8c | Literal value (len = 12) |
| 536 // | Huffman encoded: |
| 537 // f1e3 c2e5 f23a 6ba0 ab90 f4ff | .....:k..... |
| 538 // | Decoded: |
| 539 // | www.example.com |
| 540 // | -> :authority: www.example.com |
| 541 |
| 542 string first = a2b_hex("418cf1e3c2e5f23a6ba0ab90f4ff"); |
| 543 EXPECT_TRUE(DecodeHeaderBlock(first)); |
| 544 first = a2b_hex("418cf1e3c2e5f23a6ba0ab90f4"); |
| 545 EXPECT_FALSE(DecodeHeaderBlock(first)); |
| 546 } |
| 547 |
| 548 TEST_P(HpackDecoder2Test, HuffmanEOSError) { |
| 549 // Literal value, Huffman encoded, but with an additional ff byte at the end |
| 550 // of the string, i.e. an EOS that is longer than permitted. |
| 551 // |
| 552 // 41 | == Literal indexed == |
| 553 // | Indexed name (idx = 1) |
| 554 // | :authority |
| 555 // 8d | Literal value (len = 13) |
| 556 // | Huffman encoded: |
| 557 // f1e3 c2e5 f23a 6ba0 ab90 f4ff | .....:k..... |
| 558 // | Decoded: |
| 559 // | www.example.com |
| 560 // | -> :authority: www.example.com |
| 561 |
| 562 string first = a2b_hex("418cf1e3c2e5f23a6ba0ab90f4ff"); |
| 563 EXPECT_TRUE(DecodeHeaderBlock(first)); |
| 564 first = a2b_hex("418df1e3c2e5f23a6ba0ab90f4ffff"); |
| 565 EXPECT_FALSE(DecodeHeaderBlock(first)); |
| 566 } |
| 567 |
| 568 // Round-tripping the header set from E.2.1 should work. |
| 569 TEST_P(HpackDecoder2Test, BasicE21) { |
| 570 HpackEncoder encoder(ObtainHpackHuffmanTable()); |
| 571 |
| 572 SpdyHeaderBlock expected_header_set; |
| 573 expected_header_set[":method"] = "GET"; |
| 574 expected_header_set[":scheme"] = "http"; |
| 575 expected_header_set[":path"] = "/"; |
| 576 expected_header_set[":authority"] = "www.example.com"; |
| 577 |
| 578 string encoded_header_set; |
| 579 EXPECT_TRUE( |
| 580 encoder.EncodeHeaderSet(expected_header_set, &encoded_header_set)); |
| 581 |
| 582 EXPECT_TRUE(DecodeHeaderBlock(encoded_header_set)); |
| 583 EXPECT_EQ(expected_header_set, decoded_block()); |
| 584 } |
| 585 |
| 586 TEST_P(HpackDecoder2Test, SectionD4RequestHuffmanExamples) { |
| 587 // TODO(jamessynge): Use net/http2/hpack/tools/hpack_example.h to parse the |
| 588 // example directly, instead of having it as a comment. |
| 589 // 82 | == Indexed - Add == |
| 590 // | idx = 2 |
| 591 // | -> :method: GET |
| 592 // 86 | == Indexed - Add == |
| 593 // | idx = 6 |
| 594 // | -> :scheme: http |
| 595 // 84 | == Indexed - Add == |
| 596 // | idx = 4 |
| 597 // | -> :path: / |
| 598 // 41 | == Literal indexed == |
| 599 // | Indexed name (idx = 1) |
| 600 // | :authority |
| 601 // 8c | Literal value (len = 12) |
| 602 // | Huffman encoded: |
| 603 // f1e3 c2e5 f23a 6ba0 ab90 f4ff | .....:k..... |
| 604 // | Decoded: |
| 605 // | www.example.com |
| 606 // | -> :authority: www.example.com |
| 607 string first = a2b_hex("828684418cf1e3c2e5f23a6ba0ab90f4ff"); |
| 608 const SpdyHeaderBlock& first_header_set = DecodeBlockExpectingSuccess(first); |
| 609 |
| 610 EXPECT_THAT(first_header_set, |
| 611 ElementsAre( |
| 612 // clang-format off |
| 613 Pair(":method", "GET"), |
| 614 Pair(":scheme", "http"), |
| 615 Pair(":path", "/"), |
| 616 Pair(":authority", "www.example.com"))); |
| 617 // clang-format on |
| 618 |
| 619 expectEntry(62, 57, ":authority", "www.example.com"); |
| 620 EXPECT_EQ(57u, decoder_peer_.header_table()->size()); |
| 621 |
| 622 // 82 | == Indexed - Add == |
| 623 // | idx = 2 |
| 624 // | -> :method: GET |
| 625 // 86 | == Indexed - Add == |
| 626 // | idx = 6 |
| 627 // | -> :scheme: http |
| 628 // 84 | == Indexed - Add == |
| 629 // | idx = 4 |
| 630 // | -> :path: / |
| 631 // be | == Indexed - Add == |
| 632 // | idx = 62 |
| 633 // | -> :authority: www.example.com |
| 634 // 58 | == Literal indexed == |
| 635 // | Indexed name (idx = 24) |
| 636 // | cache-control |
| 637 // 86 | Literal value (len = 8) |
| 638 // | Huffman encoded: |
| 639 // a8eb 1064 9cbf | ...d.. |
| 640 // | Decoded: |
| 641 // | no-cache |
| 642 // | -> cache-control: no-cache |
| 643 |
| 644 string second = a2b_hex("828684be5886a8eb10649cbf"); |
| 645 const SpdyHeaderBlock& second_header_set = |
| 646 DecodeBlockExpectingSuccess(second); |
| 647 |
| 648 EXPECT_THAT(second_header_set, |
| 649 ElementsAre( |
| 650 // clang-format off |
| 651 Pair(":method", "GET"), |
| 652 Pair(":scheme", "http"), |
| 653 Pair(":path", "/"), |
| 654 Pair(":authority", "www.example.com"), |
| 655 Pair("cache-control", "no-cache"))); |
| 656 // clang-format on |
| 657 |
| 658 expectEntry(62, 53, "cache-control", "no-cache"); |
| 659 expectEntry(63, 57, ":authority", "www.example.com"); |
| 660 EXPECT_EQ(110u, decoder_peer_.header_table()->size()); |
| 661 |
| 662 // 82 | == Indexed - Add == |
| 663 // | idx = 2 |
| 664 // | -> :method: GET |
| 665 // 87 | == Indexed - Add == |
| 666 // | idx = 7 |
| 667 // | -> :scheme: https |
| 668 // 85 | == Indexed - Add == |
| 669 // | idx = 5 |
| 670 // | -> :path: /index.html |
| 671 // bf | == Indexed - Add == |
| 672 // | idx = 63 |
| 673 // | -> :authority: www.example.com |
| 674 // 40 | == Literal indexed == |
| 675 // 88 | Literal name (len = 10) |
| 676 // | Huffman encoded: |
| 677 // 25a8 49e9 5ba9 7d7f | %.I.[.}. |
| 678 // | Decoded: |
| 679 // | custom-key |
| 680 // 89 | Literal value (len = 12) |
| 681 // | Huffman encoded: |
| 682 // 25a8 49e9 5bb8 e8b4 bf | %.I.[.... |
| 683 // | Decoded: |
| 684 // | custom-value |
| 685 // | -> custom-key: custom-value |
| 686 string third = a2b_hex("828785bf408825a849e95ba97d7f8925a849e95bb8e8b4bf"); |
| 687 const SpdyHeaderBlock& third_header_set = DecodeBlockExpectingSuccess(third); |
| 688 |
| 689 EXPECT_THAT( |
| 690 third_header_set, |
| 691 ElementsAre( |
| 692 // clang-format off |
| 693 Pair(":method", "GET"), |
| 694 Pair(":scheme", "https"), |
| 695 Pair(":path", "/index.html"), |
| 696 Pair(":authority", "www.example.com"), |
| 697 Pair("custom-key", "custom-value"))); |
| 698 // clang-format on |
| 699 |
| 700 expectEntry(62, 54, "custom-key", "custom-value"); |
| 701 expectEntry(63, 53, "cache-control", "no-cache"); |
| 702 expectEntry(64, 57, ":authority", "www.example.com"); |
| 703 EXPECT_EQ(164u, decoder_peer_.header_table()->size()); |
| 704 } |
| 705 |
| 706 TEST_P(HpackDecoder2Test, SectionD6ResponseHuffmanExamples) { |
| 707 decoder_.ApplyHeaderTableSizeSetting(256); |
| 708 |
| 709 // 48 | == Literal indexed == |
| 710 // | Indexed name (idx = 8) |
| 711 // | :status |
| 712 // 82 | Literal value (len = 3) |
| 713 // | Huffman encoded: |
| 714 // 6402 | d. |
| 715 // | Decoded: |
| 716 // | 302 |
| 717 // | -> :status: 302 |
| 718 // 58 | == Literal indexed == |
| 719 // | Indexed name (idx = 24) |
| 720 // | cache-control |
| 721 // 85 | Literal value (len = 7) |
| 722 // | Huffman encoded: |
| 723 // aec3 771a 4b | ..w.K |
| 724 // | Decoded: |
| 725 // | private |
| 726 // | -> cache-control: private |
| 727 // 61 | == Literal indexed == |
| 728 // | Indexed name (idx = 33) |
| 729 // | date |
| 730 // 96 | Literal value (len = 29) |
| 731 // | Huffman encoded: |
| 732 // d07a be94 1054 d444 a820 0595 040b 8166 | .z...T.D. .....f |
| 733 // e082 a62d 1bff | ...-.. |
| 734 // | Decoded: |
| 735 // | Mon, 21 Oct 2013 20:13:21 |
| 736 // | GMT |
| 737 // | -> date: Mon, 21 Oct 2013 |
| 738 // | 20:13:21 GMT |
| 739 // 6e | == Literal indexed == |
| 740 // | Indexed name (idx = 46) |
| 741 // | location |
| 742 // 91 | Literal value (len = 23) |
| 743 // | Huffman encoded: |
| 744 // 9d29 ad17 1863 c78f 0b97 c8e9 ae82 ae43 | .)...c.........C |
| 745 // d3 | . |
| 746 // | Decoded: |
| 747 // | https://www.example.com |
| 748 // | -> location: https://www.e |
| 749 // | xample.com |
| 750 |
| 751 string first = a2b_hex( |
| 752 "488264025885aec3771a4b6196d07abe" |
| 753 "941054d444a8200595040b8166e082a6" |
| 754 "2d1bff6e919d29ad171863c78f0b97c8" |
| 755 "e9ae82ae43d3"); |
| 756 const SpdyHeaderBlock& first_header_set = DecodeBlockExpectingSuccess(first); |
| 757 |
| 758 EXPECT_THAT(first_header_set, |
| 759 ElementsAre( |
| 760 // clang-format off |
| 761 Pair(":status", "302"), |
| 762 Pair("cache-control", "private"), |
| 763 Pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"), |
| 764 Pair("location", "https://www.example.com"))); |
| 765 // clang-format on |
| 766 |
| 767 expectEntry(62, 63, "location", "https://www.example.com"); |
| 768 expectEntry(63, 65, "date", "Mon, 21 Oct 2013 20:13:21 GMT"); |
| 769 expectEntry(64, 52, "cache-control", "private"); |
| 770 expectEntry(65, 42, ":status", "302"); |
| 771 EXPECT_EQ(222u, decoder_peer_.header_table()->size()); |
| 772 |
| 773 // 48 | == Literal indexed == |
| 774 // | Indexed name (idx = 8) |
| 775 // | :status |
| 776 // 83 | Literal value (len = 3) |
| 777 // | Huffman encoded: |
| 778 // 640e ff | d.. |
| 779 // | Decoded: |
| 780 // | 307 |
| 781 // | - evict: :status: 302 |
| 782 // | -> :status: 307 |
| 783 // c1 | == Indexed - Add == |
| 784 // | idx = 65 |
| 785 // | -> cache-control: private |
| 786 // c0 | == Indexed - Add == |
| 787 // | idx = 64 |
| 788 // | -> date: Mon, 21 Oct 2013 |
| 789 // | 20:13:21 GMT |
| 790 // bf | == Indexed - Add == |
| 791 // | idx = 63 |
| 792 // | -> location: |
| 793 // | https://www.example.com |
| 794 string second = a2b_hex("4883640effc1c0bf"); |
| 795 const SpdyHeaderBlock& second_header_set = |
| 796 DecodeBlockExpectingSuccess(second); |
| 797 |
| 798 EXPECT_THAT(second_header_set, |
| 799 ElementsAre( |
| 800 // clang-format off |
| 801 Pair(":status", "307"), |
| 802 Pair("cache-control", "private"), |
| 803 Pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"), |
| 804 Pair("location", "https://www.example.com"))); |
| 805 // clang-format on |
| 806 |
| 807 expectEntry(62, 42, ":status", "307"); |
| 808 expectEntry(63, 63, "location", "https://www.example.com"); |
| 809 expectEntry(64, 65, "date", "Mon, 21 Oct 2013 20:13:21 GMT"); |
| 810 expectEntry(65, 52, "cache-control", "private"); |
| 811 EXPECT_EQ(222u, decoder_peer_.header_table()->size()); |
| 812 |
| 813 // 88 | == Indexed - Add == |
| 814 // | idx = 8 |
| 815 // | -> :status: 200 |
| 816 // c1 | == Indexed - Add == |
| 817 // | idx = 65 |
| 818 // | -> cache-control: private |
| 819 // 61 | == Literal indexed == |
| 820 // | Indexed name (idx = 33) |
| 821 // | date |
| 822 // 96 | Literal value (len = 22) |
| 823 // | Huffman encoded: |
| 824 // d07a be94 1054 d444 a820 0595 040b 8166 | .z...T.D. .....f |
| 825 // e084 a62d 1bff | ...-.. |
| 826 // | Decoded: |
| 827 // | Mon, 21 Oct 2013 20:13:22 |
| 828 // | GMT |
| 829 // | - evict: cache-control: |
| 830 // | private |
| 831 // | -> date: Mon, 21 Oct 2013 |
| 832 // | 20:13:22 GMT |
| 833 // c0 | == Indexed - Add == |
| 834 // | idx = 64 |
| 835 // | -> location: |
| 836 // | https://www.example.com |
| 837 // 5a | == Literal indexed == |
| 838 // | Indexed name (idx = 26) |
| 839 // | content-encoding |
| 840 // 83 | Literal value (len = 3) |
| 841 // | Huffman encoded: |
| 842 // 9bd9 ab | ... |
| 843 // | Decoded: |
| 844 // | gzip |
| 845 // | - evict: date: Mon, 21 Oct |
| 846 // | 2013 20:13:21 GMT |
| 847 // | -> content-encoding: gzip |
| 848 // 77 | == Literal indexed == |
| 849 // | Indexed name (idx = 55) |
| 850 // | set-cookie |
| 851 // ad | Literal value (len = 45) |
| 852 // | Huffman encoded: |
| 853 // 94e7 821d d7f2 e6c7 b335 dfdf cd5b 3960 | .........5...[9` |
| 854 // d5af 2708 7f36 72c1 ab27 0fb5 291f 9587 | ..'..6r..'..)... |
| 855 // 3160 65c0 03ed 4ee5 b106 3d50 07 | 1`e...N...=P. |
| 856 // | Decoded: |
| 857 // | foo=ASDJKHQKBZXOQWEOPIUAXQ |
| 858 // | WEOIU; max-age=3600; versi |
| 859 // | on=1 |
| 860 // | - evict: location: |
| 861 // | https://www.example.com |
| 862 // | - evict: :status: 307 |
| 863 // | -> set-cookie: foo=ASDJKHQ |
| 864 // | KBZXOQWEOPIUAXQWEOIU; |
| 865 // | max-age=3600; version=1 |
| 866 string third = a2b_hex( |
| 867 "88c16196d07abe941054d444a8200595" |
| 868 "040b8166e084a62d1bffc05a839bd9ab" |
| 869 "77ad94e7821dd7f2e6c7b335dfdfcd5b" |
| 870 "3960d5af27087f3672c1ab270fb5291f" |
| 871 "9587316065c003ed4ee5b1063d5007"); |
| 872 const SpdyHeaderBlock& third_header_set = DecodeBlockExpectingSuccess(third); |
| 873 |
| 874 EXPECT_THAT(third_header_set, |
| 875 ElementsAre( |
| 876 // clang-format off |
| 877 Pair(":status", "200"), |
| 878 Pair("cache-control", "private"), |
| 879 Pair("date", "Mon, 21 Oct 2013 20:13:22 GMT"), |
| 880 Pair("location", "https://www.example.com"), |
| 881 Pair("content-encoding", "gzip"), |
| 882 Pair("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU;" |
| 883 " max-age=3600; version=1"))); |
| 884 // clang-format on |
| 885 |
| 886 expectEntry(62, 98, "set-cookie", |
| 887 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU;" |
| 888 " max-age=3600; version=1"); |
| 889 expectEntry(63, 52, "content-encoding", "gzip"); |
| 890 expectEntry(64, 65, "date", "Mon, 21 Oct 2013 20:13:22 GMT"); |
| 891 EXPECT_EQ(215u, decoder_peer_.header_table()->size()); |
| 892 } |
| 893 |
| 894 // Regression test: Found that entries with dynamic indexed names and literal |
| 895 // values caused "use after free" MSAN failures if the name was evicted as it |
| 896 // was being re-used. |
| 897 TEST_P(HpackDecoder2Test, ReuseNameOfEvictedEntry) { |
| 898 // Each entry is measured as 32 bytes plus the sum of the lengths of the name |
| 899 // and the value. Set the size big enough for at most one entry, and a fairly |
| 900 // small one at that (31 ASCII characters). |
| 901 decoder_.ApplyHeaderTableSizeSetting(63); |
| 902 |
| 903 HpackBlockBuilder hbb; |
| 904 |
| 905 const StringPiece name("some-name"); |
| 906 const StringPiece value1("some-value"); |
| 907 const StringPiece value2("another-value"); |
| 908 const StringPiece value3("yet-another-value"); |
| 909 |
| 910 // Add an entry that will become the first in the dynamic table, entry 62. |
| 911 hbb.AppendLiteralNameAndValue(HpackEntryType::kIndexedLiteralHeader, false, |
| 912 name, false, value1); |
| 913 |
| 914 // Confirm that entry has been added by re-using it. |
| 915 hbb.AppendIndexedHeader(62); |
| 916 |
| 917 // Add another entry referring to the name of the first. This will evict the |
| 918 // first. |
| 919 hbb.AppendNameIndexAndLiteralValue(HpackEntryType::kIndexedLiteralHeader, 62, |
| 920 false, value2); |
| 921 |
| 922 // Confirm that entry has been added by re-using it. |
| 923 hbb.AppendIndexedHeader(62); |
| 924 |
| 925 // Add another entry referring to the name of the second. This will evict the |
| 926 // second. |
| 927 hbb.AppendNameIndexAndLiteralValue(HpackEntryType::kIndexedLiteralHeader, 62, |
| 928 false, value3); |
| 929 |
| 930 // Confirm that entry has been added by re-using it. |
| 931 hbb.AppendIndexedHeader(62); |
| 932 |
| 933 EXPECT_TRUE(DecodeHeaderBlock(hbb.buffer())); |
| 934 |
| 935 SpdyHeaderBlock expected_header_set; |
| 936 expected_header_set.AppendValueOrAddHeader(name, value1); |
| 937 expected_header_set.AppendValueOrAddHeader(name, value1); |
| 938 expected_header_set.AppendValueOrAddHeader(name, value2); |
| 939 expected_header_set.AppendValueOrAddHeader(name, value2); |
| 940 expected_header_set.AppendValueOrAddHeader(name, value3); |
| 941 expected_header_set.AppendValueOrAddHeader(name, value3); |
| 942 |
| 943 // SpdyHeaderBlock stores these 6 strings as '\0' separated values. |
| 944 // Make sure that is what happened. |
| 945 string joined_values = expected_header_set[name].as_string(); |
| 946 EXPECT_EQ(joined_values.size(), |
| 947 2 * value1.size() + 2 * value2.size() + 2 * value3.size() + 5); |
| 948 |
| 949 EXPECT_EQ(expected_header_set, decoded_block()); |
| 950 } |
| 951 |
| 952 } // namespace |
| 953 } // namespace test |
| 954 } // namespace net |
OLD | NEW |