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