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 "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 using base::StringPiece; | 21 using base::StringPiece; |
21 using std::string; | 22 using std::string; |
| 23 using test::a2b_hex; |
22 | 24 |
23 const size_t kLiteralBound = 1024; | 25 const size_t kLiteralBound = 1024; |
24 | 26 |
25 class HpackInputStreamTest : public ::testing::Test { | 27 class HpackInputStreamTest : public ::testing::Test { |
| 28 protected: |
26 virtual void SetUp() { | 29 virtual void SetUp() { |
27 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); | 30 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); |
28 EXPECT_TRUE(huffman_table.Initialize(&code[0], code.size())); | 31 EXPECT_TRUE(huffman_table.Initialize(&code[0], code.size())); |
29 } | 32 } |
30 | 33 |
31 protected: | |
32 HpackHuffmanTable huffman_table; | 34 HpackHuffmanTable huffman_table; |
33 }; | 35 }; |
34 | 36 |
35 const char kEncodedFixture[] = "\x33" // Length prefix. | 37 // Hex representation of encoded length and Huffman string. |
36 "\xc5\xad\xb7\x7f\x87\x6f\xc7\xfb\xf7\xfd\xbf\xbe\xbf\xf3\xf7\xf4" | 38 const char kEncodedHuffmanFixture[] = "31" // Length prefix. |
37 "\xfb\x7e\xbb\xbe\x9f\x5f\x87\xe3\x7f\xef\xed\xfa\xee\xfa\x7c\x3f" | 39 "e0d6cf9f6e8f9fd3e5f6fa76fefd3c7e" |
38 "\x1d\x5d\x1a\x23\xce\x54\x64\x36\xcd\x49\x4b\xd5\xd1\xcc\x5f\x05" | 40 "df9eff1f2f0f3cfe9f6fcf7f8f879f61" |
39 "\x35\x96\x9b"; | 41 "ad4f4cc9a973a2200ec3725e18b1b74e" |
| 42 "3f"; |
40 | 43 |
41 const char kDecodedFixture[] = | 44 const char kDecodedHuffmanFixture[] = |
42 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"; | 45 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"; |
43 | 46 |
44 // 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 |
45 // prefix. | 48 // prefix. |
46 uint32 DecodeValidUint32(uint8 N, StringPiece str) { | 49 uint32 DecodeValidUint32(uint8 N, StringPiece str) { |
47 EXPECT_GT(N, 0); | 50 EXPECT_GT(N, 0); |
48 EXPECT_LE(N, 8); | 51 EXPECT_LE(N, 8); |
49 HpackInputStream input_stream(kLiteralBound, str); | 52 HpackInputStream input_stream(kLiteralBound, str); |
50 input_stream.SetBitOffsetForTest(8 - N); | 53 input_stream.SetBitOffsetForTest(8 - N); |
51 uint32 I; | 54 uint32 I; |
52 EXPECT_TRUE(input_stream.DecodeNextUint32ForTest(&I)); | 55 EXPECT_TRUE(input_stream.DecodeNextUint32(&I)); |
53 return I; | 56 return I; |
54 } | 57 } |
55 | 58 |
56 // Utility function to decode an assumed-invalid uint32 with an N-bit | 59 // Utility function to decode an assumed-invalid uint32 with an N-bit |
57 // prefix. | 60 // prefix. |
58 void ExpectDecodeUint32Invalid(uint8 N, StringPiece str) { | 61 void ExpectDecodeUint32Invalid(uint8 N, StringPiece str) { |
59 EXPECT_GT(N, 0); | 62 EXPECT_GT(N, 0); |
60 EXPECT_LE(N, 8); | 63 EXPECT_LE(N, 8); |
61 HpackInputStream input_stream(kLiteralBound, str); | 64 HpackInputStream input_stream(kLiteralBound, str); |
62 input_stream.SetBitOffsetForTest(8 - N); | 65 input_stream.SetBitOffsetForTest(8 - N); |
63 uint32 I; | 66 uint32 I; |
64 EXPECT_FALSE(input_stream.DecodeNextUint32ForTest(&I)); | 67 EXPECT_FALSE(input_stream.DecodeNextUint32(&I)); |
65 } | 68 } |
66 | 69 |
67 uint32 bits32(const string& bitstring) { | 70 uint32 bits32(const string& bitstring) { |
68 return std::bitset<32>(bitstring).to_ulong(); | 71 return std::bitset<32>(bitstring).to_ulong(); |
69 } | 72 } |
70 | 73 |
71 // The {Number}ByteIntegersEightBitPrefix tests below test that | 74 // The {Number}ByteIntegersEightBitPrefix tests below test that |
72 // certain integers are decoded correctly with an 8-bit prefix in | 75 // certain integers are decoded correctly with an 8-bit prefix in |
73 // exactly {Number} bytes. | 76 // exactly {Number} bytes. |
74 | 77 |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 TEST_F(HpackInputStreamTest, DecodeNextIdentityStringNotEnoughInput) { | 510 TEST_F(HpackInputStreamTest, DecodeNextIdentityStringNotEnoughInput) { |
508 // Set the length to be one more than it should be. | 511 // Set the length to be one more than it should be. |
509 HpackInputStream input_stream(kLiteralBound, "\x0fstring literal"); | 512 HpackInputStream input_stream(kLiteralBound, "\x0fstring literal"); |
510 | 513 |
511 EXPECT_TRUE(input_stream.HasMoreData()); | 514 EXPECT_TRUE(input_stream.HasMoreData()); |
512 StringPiece string_piece; | 515 StringPiece string_piece; |
513 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece)); | 516 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece)); |
514 } | 517 } |
515 | 518 |
516 TEST_F(HpackInputStreamTest, DecodeNextHuffmanString) { | 519 TEST_F(HpackInputStreamTest, DecodeNextHuffmanString) { |
517 string output, input(kEncodedFixture, arraysize(kEncodedFixture)-1); | 520 string output, input(a2b_hex(kEncodedHuffmanFixture)); |
518 HpackInputStream input_stream(arraysize(kDecodedFixture)-1, input); | 521 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-1, input); |
519 | 522 |
520 EXPECT_TRUE(input_stream.HasMoreData()); | 523 EXPECT_TRUE(input_stream.HasMoreData()); |
521 EXPECT_TRUE(input_stream.DecodeNextHuffmanString(huffman_table, &output)); | 524 EXPECT_TRUE(input_stream.DecodeNextHuffmanString(huffman_table, &output)); |
522 EXPECT_EQ(kDecodedFixture, output); | 525 EXPECT_EQ(kDecodedHuffmanFixture, output); |
523 EXPECT_FALSE(input_stream.HasMoreData()); | 526 EXPECT_FALSE(input_stream.HasMoreData()); |
524 } | 527 } |
525 | 528 |
526 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringSizeLimit) { | 529 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringSizeLimit) { |
527 string output, input(kEncodedFixture, arraysize(kEncodedFixture)-1); | 530 string output, input(a2b_hex(kEncodedHuffmanFixture)); |
528 // Max string literal is one byte shorter than the decoded fixture. | 531 // Max string literal is one byte shorter than the decoded fixture. |
529 HpackInputStream input_stream(arraysize(kDecodedFixture)-2, input); | 532 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-2, input); |
530 | 533 |
531 // Decoded string overflows the max string literal. | 534 // Decoded string overflows the max string literal. |
532 EXPECT_TRUE(input_stream.HasMoreData()); | 535 EXPECT_TRUE(input_stream.HasMoreData()); |
533 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table, &output)); | 536 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table, &output)); |
534 } | 537 } |
535 | 538 |
536 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringNotEnoughInput) { | 539 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringNotEnoughInput) { |
537 string output, input(kEncodedFixture, arraysize(kEncodedFixture)-1); | 540 string output, input(a2b_hex(kEncodedHuffmanFixture)); |
538 input[0]++; // Input prefix is one byte larger than available input. | 541 input[0]++; // Input prefix is one byte larger than available input. |
539 HpackInputStream input_stream(arraysize(kDecodedFixture)-1, input); | 542 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-1, input); |
540 | 543 |
541 // Not enough buffer for declared encoded length. | 544 // Not enough buffer for declared encoded length. |
542 EXPECT_TRUE(input_stream.HasMoreData()); | 545 EXPECT_TRUE(input_stream.HasMoreData()); |
543 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table, &output)); | 546 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table, &output)); |
544 } | 547 } |
545 | 548 |
546 TEST_F(HpackInputStreamTest, PeekBitsAndConsume) { | 549 TEST_F(HpackInputStreamTest, PeekBitsAndConsume) { |
547 HpackInputStream input_stream(kLiteralBound, "\xad\xab\xad\xab\xad"); | 550 HpackInputStream input_stream(kLiteralBound, "\xad\xab\xad\xab\xad"); |
548 | 551 |
549 uint32 bits = 0; | 552 uint32 bits = 0; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 | 620 |
618 input_stream.ConsumeBits(6); | 621 input_stream.ConsumeBits(6); |
619 EXPECT_TRUE(input_stream.HasMoreData()); | 622 EXPECT_TRUE(input_stream.HasMoreData()); |
620 input_stream.ConsumeByteRemainder(); | 623 input_stream.ConsumeByteRemainder(); |
621 EXPECT_FALSE(input_stream.HasMoreData()); | 624 EXPECT_FALSE(input_stream.HasMoreData()); |
622 } | 625 } |
623 | 626 |
624 } // namespace | 627 } // namespace |
625 | 628 |
626 } // namespace net | 629 } // namespace net |
OLD | NEW |