OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/hpack_input_stream.h" | 5 #include "net/spdy/hpack_input_stream.h" |
6 | 6 |
7 #include <bitset> | 7 #include <bitset> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
13 #include "net/spdy/hpack_constants.h" | 13 #include "net/spdy/hpack_constants.h" |
14 #include "net/spdy/spdy_test_utils.h" | 14 #include "net/spdy/spdy_test_utils.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 using base::StringPiece; | 21 using base::StringPiece; |
22 using std::string; | 22 using std::string; |
23 using test::a2b_hex; | 23 using test::a2b_hex; |
24 | 24 |
25 const size_t kLiteralBound = 1024; | 25 const size_t kLiteralBound = 1024; |
26 | 26 |
27 class HpackInputStreamTest : public ::testing::Test { | 27 class HpackInputStreamTest : public ::testing::Test { |
28 protected: | 28 public: |
29 virtual void SetUp() { | 29 virtual void SetUp() { |
30 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); | 30 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); |
31 EXPECT_TRUE(huffman_table.Initialize(&code[0], code.size())); | 31 EXPECT_TRUE(huffman_table_.Initialize(&code[0], code.size())); |
32 } | 32 } |
33 | 33 |
34 HpackHuffmanTable huffman_table; | 34 protected: |
| 35 HpackHuffmanTable huffman_table_; |
35 }; | 36 }; |
36 | 37 |
37 // Hex representation of encoded length and Huffman string. | 38 // Hex representation of encoded length and Huffman string. |
38 const char kEncodedHuffmanFixture[] = "31" // Length prefix. | 39 const char kEncodedHuffmanFixture[] = "2d" // Length prefix. |
39 "e0d6cf9f6e8f9fd3e5f6fa76fefd3c7e" | 40 "94e7821dd7f2e6c7b335dfdfcd5b3960" |
40 "df9eff1f2f0f3cfe9f6fcf7f8f879f61" | 41 "d5af27087f3672c1ab270fb5291f9587" |
41 "ad4f4cc9a973a2200ec3725e18b1b74e" | 42 "316065c003ed4ee5b1063d5007"; |
42 "3f"; | |
43 | 43 |
44 const char kDecodedHuffmanFixture[] = | 44 const char kDecodedHuffmanFixture[] = |
45 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"; | 45 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"; |
46 | 46 |
47 // Utility function to decode an assumed-valid uint32 with an N-bit | 47 // Utility function to decode an assumed-valid uint32 with an N-bit |
48 // prefix. | 48 // prefix. |
49 uint32 DecodeValidUint32(uint8 N, StringPiece str) { | 49 uint32 DecodeValidUint32(uint8 N, StringPiece str) { |
50 EXPECT_GT(N, 0); | 50 EXPECT_GT(N, 0); |
51 EXPECT_LE(N, 8); | 51 EXPECT_LE(N, 8); |
52 HpackInputStream input_stream(kLiteralBound, str); | 52 HpackInputStream input_stream(kLiteralBound, str); |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 EXPECT_TRUE(input_stream.HasMoreData()); | 514 EXPECT_TRUE(input_stream.HasMoreData()); |
515 StringPiece string_piece; | 515 StringPiece string_piece; |
516 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece)); | 516 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece)); |
517 } | 517 } |
518 | 518 |
519 TEST_F(HpackInputStreamTest, DecodeNextHuffmanString) { | 519 TEST_F(HpackInputStreamTest, DecodeNextHuffmanString) { |
520 string output, input(a2b_hex(kEncodedHuffmanFixture)); | 520 string output, input(a2b_hex(kEncodedHuffmanFixture)); |
521 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-1, input); | 521 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-1, input); |
522 | 522 |
523 EXPECT_TRUE(input_stream.HasMoreData()); | 523 EXPECT_TRUE(input_stream.HasMoreData()); |
524 EXPECT_TRUE(input_stream.DecodeNextHuffmanString(huffman_table, &output)); | 524 EXPECT_TRUE(input_stream.DecodeNextHuffmanString(huffman_table_, &output)); |
525 EXPECT_EQ(kDecodedHuffmanFixture, output); | 525 EXPECT_EQ(kDecodedHuffmanFixture, output); |
526 EXPECT_FALSE(input_stream.HasMoreData()); | 526 EXPECT_FALSE(input_stream.HasMoreData()); |
527 } | 527 } |
528 | 528 |
529 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringSizeLimit) { | 529 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringSizeLimit) { |
530 string output, input(a2b_hex(kEncodedHuffmanFixture)); | 530 string output, input(a2b_hex(kEncodedHuffmanFixture)); |
531 // Max string literal is one byte shorter than the decoded fixture. | 531 // Max string literal is one byte shorter than the decoded fixture. |
532 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-2, input); | 532 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-2, input); |
533 | 533 |
534 // Decoded string overflows the max string literal. | 534 // Decoded string overflows the max string literal. |
535 EXPECT_TRUE(input_stream.HasMoreData()); | 535 EXPECT_TRUE(input_stream.HasMoreData()); |
536 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table, &output)); | 536 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table_, &output)); |
537 } | 537 } |
538 | 538 |
539 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringNotEnoughInput) { | 539 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringNotEnoughInput) { |
540 string output, input(a2b_hex(kEncodedHuffmanFixture)); | 540 string output, input(a2b_hex(kEncodedHuffmanFixture)); |
541 input[0]++; // Input prefix is one byte larger than available input. | 541 input[0]++; // Input prefix is one byte larger than available input. |
542 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-1, input); | 542 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-1, input); |
543 | 543 |
544 // Not enough buffer for declared encoded length. | 544 // Not enough buffer for declared encoded length. |
545 EXPECT_TRUE(input_stream.HasMoreData()); | 545 EXPECT_TRUE(input_stream.HasMoreData()); |
546 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table, &output)); | 546 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table_, &output)); |
547 } | 547 } |
548 | 548 |
549 TEST_F(HpackInputStreamTest, PeekBitsAndConsume) { | 549 TEST_F(HpackInputStreamTest, PeekBitsAndConsume) { |
550 HpackInputStream input_stream(kLiteralBound, "\xad\xab\xad\xab\xad"); | 550 HpackInputStream input_stream(kLiteralBound, "\xad\xab\xad\xab\xad"); |
551 | 551 |
552 uint32 bits = 0; | 552 uint32 bits = 0; |
553 size_t peeked_count = 0; | 553 size_t peeked_count = 0; |
554 | 554 |
555 // Read 0xad. | 555 // Read 0xad. |
556 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | 556 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 | 620 |
621 input_stream.ConsumeBits(6); | 621 input_stream.ConsumeBits(6); |
622 EXPECT_TRUE(input_stream.HasMoreData()); | 622 EXPECT_TRUE(input_stream.HasMoreData()); |
623 input_stream.ConsumeByteRemainder(); | 623 input_stream.ConsumeByteRemainder(); |
624 EXPECT_FALSE(input_stream.HasMoreData()); | 624 EXPECT_FALSE(input_stream.HasMoreData()); |
625 } | 625 } |
626 | 626 |
627 } // namespace | 627 } // namespace |
628 | 628 |
629 } // namespace net | 629 } // namespace net |
OLD | NEW |