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/hpack_input_stream.h" | 5 #include "net/spdy/hpack/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 |
(...skipping 27 matching lines...) Expand all Loading... |
38 // Hex representation of encoded length and Huffman string. | 38 // Hex representation of encoded length and Huffman string. |
39 const char kEncodedHuffmanFixture[] = | 39 const char kEncodedHuffmanFixture[] = |
40 "2d" // Length prefix. | 40 "2d" // Length prefix. |
41 "94e7821dd7f2e6c7b335dfdfcd5b3960" | 41 "94e7821dd7f2e6c7b335dfdfcd5b3960" |
42 "d5af27087f3672c1ab270fb5291f9587" | 42 "d5af27087f3672c1ab270fb5291f9587" |
43 "316065c003ed4ee5b1063d5007"; | 43 "316065c003ed4ee5b1063d5007"; |
44 | 44 |
45 const char kDecodedHuffmanFixture[] = | 45 const char kDecodedHuffmanFixture[] = |
46 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"; | 46 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"; |
47 | 47 |
48 // Utility function to decode an assumed-valid uint32 with an N-bit | 48 // Utility function to decode an assumed-valid uint32_t with an N-bit |
49 // prefix. | 49 // prefix. |
50 uint32 DecodeValidUint32(uint8 N, StringPiece str) { | 50 uint32_t DecodeValidUint32(uint8_t N, StringPiece str) { |
51 EXPECT_GT(N, 0); | 51 EXPECT_GT(N, 0); |
52 EXPECT_LE(N, 8); | 52 EXPECT_LE(N, 8); |
53 HpackInputStream input_stream(kLiteralBound, str); | 53 HpackInputStream input_stream(kLiteralBound, str); |
54 input_stream.SetBitOffsetForTest(8 - N); | 54 input_stream.SetBitOffsetForTest(8 - N); |
55 uint32 I; | 55 uint32_t I; |
56 EXPECT_TRUE(input_stream.DecodeNextUint32(&I)); | 56 EXPECT_TRUE(input_stream.DecodeNextUint32(&I)); |
57 return I; | 57 return I; |
58 } | 58 } |
59 | 59 |
60 // Utility function to decode an assumed-invalid uint32 with an N-bit | 60 // Utility function to decode an assumed-invalid uint32_t with an N-bit |
61 // prefix. | 61 // prefix. |
62 void ExpectDecodeUint32Invalid(uint8 N, StringPiece str) { | 62 void ExpectDecodeUint32Invalid(uint8_t N, StringPiece str) { |
63 EXPECT_GT(N, 0); | 63 EXPECT_GT(N, 0); |
64 EXPECT_LE(N, 8); | 64 EXPECT_LE(N, 8); |
65 HpackInputStream input_stream(kLiteralBound, str); | 65 HpackInputStream input_stream(kLiteralBound, str); |
66 input_stream.SetBitOffsetForTest(8 - N); | 66 input_stream.SetBitOffsetForTest(8 - N); |
67 uint32 I; | 67 uint32_t I; |
68 EXPECT_FALSE(input_stream.DecodeNextUint32(&I)); | 68 EXPECT_FALSE(input_stream.DecodeNextUint32(&I)); |
69 } | 69 } |
70 | 70 |
71 uint32 bits32(const string& bitstring) { | 71 uint32_t bits32(const string& bitstring) { |
72 return std::bitset<32>(bitstring).to_ulong(); | 72 return std::bitset<32>(bitstring).to_ulong(); |
73 } | 73 } |
74 | 74 |
75 // The {Number}ByteIntegersEightBitPrefix tests below test that | 75 // The {Number}ByteIntegersEightBitPrefix tests below test that |
76 // certain integers are decoded correctly with an 8-bit prefix in | 76 // certain integers are decoded correctly with an 8-bit prefix in |
77 // exactly {Number} bytes. | 77 // exactly {Number} bytes. |
78 | 78 |
79 TEST_F(HpackInputStreamTest, OneByteIntegersEightBitPrefix) { | 79 TEST_F(HpackInputStreamTest, OneByteIntegersEightBitPrefix) { |
80 // Minimum. | 80 // Minimum. |
81 EXPECT_EQ(0x00u, DecodeValidUint32(8, string("\x00", 1))); | 81 EXPECT_EQ(0x00u, DecodeValidUint32(8, string("\x00", 1))); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // Minimum. | 140 // Minimum. |
141 EXPECT_EQ(0x100000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x80\x01")); | 141 EXPECT_EQ(0x100000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x80\x01")); |
142 // Maximum. | 142 // Maximum. |
143 EXPECT_EQ(0xffffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\xff\x0f")); | 143 EXPECT_EQ(0xffffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\xff\x0f")); |
144 // Invalid. | 144 // Invalid. |
145 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x00"); | 145 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x00"); |
146 ExpectDecodeUint32Invalid(8, "\xff\x80\xfe\xff\xff\x10"); | 146 ExpectDecodeUint32Invalid(8, "\xff\x80\xfe\xff\xff\x10"); |
147 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff"); | 147 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff"); |
148 } | 148 } |
149 | 149 |
150 // There are no valid uint32 encodings that are greater than six | 150 // There are no valid uint32_t encodings that are greater than six |
151 // bytes. | 151 // bytes. |
152 TEST_F(HpackInputStreamTest, SevenByteIntegersEightBitPrefix) { | 152 TEST_F(HpackInputStreamTest, SevenByteIntegersEightBitPrefix) { |
153 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x00"); | 153 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x00"); |
154 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x01"); | 154 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x01"); |
155 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff\xff"); | 155 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff\xff"); |
156 } | 156 } |
157 | 157 |
158 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that | 158 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that |
159 // certain integers are encoded correctly with an N-bit prefix in | 159 // certain integers are encoded correctly with an N-bit prefix in |
160 // exactly {Number} bytes for N in {1, 2, ..., 7}. | 160 // exactly {Number} bytes for N in {1, 2, ..., 7}. |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 ExpectDecodeUint32Invalid(4, "\x0f\xf0\xff\xff\xff\x10"); | 452 ExpectDecodeUint32Invalid(4, "\x0f\xf0\xff\xff\xff\x10"); |
453 ExpectDecodeUint32Invalid(4, "\xff\xf0\xff\xff\xff\xff"); | 453 ExpectDecodeUint32Invalid(4, "\xff\xf0\xff\xff\xff\xff"); |
454 ExpectDecodeUint32Invalid(3, "\x07\xf8\xff\xff\xff\x10"); | 454 ExpectDecodeUint32Invalid(3, "\x07\xf8\xff\xff\xff\x10"); |
455 ExpectDecodeUint32Invalid(3, "\xff\xf8\xff\xff\xff\xff"); | 455 ExpectDecodeUint32Invalid(3, "\xff\xf8\xff\xff\xff\xff"); |
456 ExpectDecodeUint32Invalid(2, "\x03\xfc\xff\xff\xff\x10"); | 456 ExpectDecodeUint32Invalid(2, "\x03\xfc\xff\xff\xff\x10"); |
457 ExpectDecodeUint32Invalid(2, "\xff\xfc\xff\xff\xff\xff"); | 457 ExpectDecodeUint32Invalid(2, "\xff\xfc\xff\xff\xff\xff"); |
458 ExpectDecodeUint32Invalid(1, "\x01\xfe\xff\xff\xff\x10"); | 458 ExpectDecodeUint32Invalid(1, "\x01\xfe\xff\xff\xff\x10"); |
459 ExpectDecodeUint32Invalid(1, "\xff\xfe\xff\xff\xff\xff"); | 459 ExpectDecodeUint32Invalid(1, "\xff\xfe\xff\xff\xff\xff"); |
460 } | 460 } |
461 | 461 |
462 // There are no valid uint32 encodings that are greater than six | 462 // There are no valid uint32_t encodings that are greater than six |
463 // bytes. | 463 // bytes. |
464 TEST_F(HpackInputStreamTest, SevenByteIntegersOneToSevenBitPrefixes) { | 464 TEST_F(HpackInputStreamTest, SevenByteIntegersOneToSevenBitPrefixes) { |
465 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x00"); | 465 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x00"); |
466 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x01"); | 466 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x01"); |
467 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff\xff\xff"); | 467 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff\xff\xff"); |
468 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x00"); | 468 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x00"); |
469 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x01"); | 469 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x01"); |
470 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff\xff\xff"); | 470 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff\xff\xff"); |
471 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x00"); | 471 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x00"); |
472 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x01"); | 472 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x01"); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture) - 1, input); | 543 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture) - 1, input); |
544 | 544 |
545 // Not enough buffer for declared encoded length. | 545 // Not enough buffer for declared encoded length. |
546 EXPECT_TRUE(input_stream.HasMoreData()); | 546 EXPECT_TRUE(input_stream.HasMoreData()); |
547 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table_, &output)); | 547 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table_, &output)); |
548 } | 548 } |
549 | 549 |
550 TEST_F(HpackInputStreamTest, PeekBitsAndConsume) { | 550 TEST_F(HpackInputStreamTest, PeekBitsAndConsume) { |
551 HpackInputStream input_stream(kLiteralBound, "\xad\xab\xad\xab\xad"); | 551 HpackInputStream input_stream(kLiteralBound, "\xad\xab\xad\xab\xad"); |
552 | 552 |
553 uint32 bits = 0; | 553 uint32_t bits = 0; |
554 size_t peeked_count = 0; | 554 size_t peeked_count = 0; |
555 | 555 |
556 // Read 0xad. | 556 // Read 0xad. |
557 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | 557 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); |
558 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits); | 558 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits); |
559 EXPECT_EQ(8u, peeked_count); | 559 EXPECT_EQ(8u, peeked_count); |
560 | 560 |
561 // Read 0xab. | 561 // Read 0xab. |
562 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | 562 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); |
563 EXPECT_EQ(bits32("10101101101010110000000000000000"), bits); | 563 EXPECT_EQ(bits32("10101101101010110000000000000000"), bits); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 | 621 |
622 input_stream.ConsumeBits(6); | 622 input_stream.ConsumeBits(6); |
623 EXPECT_TRUE(input_stream.HasMoreData()); | 623 EXPECT_TRUE(input_stream.HasMoreData()); |
624 input_stream.ConsumeByteRemainder(); | 624 input_stream.ConsumeByteRemainder(); |
625 EXPECT_FALSE(input_stream.HasMoreData()); | 625 EXPECT_FALSE(input_stream.HasMoreData()); |
626 } | 626 } |
627 | 627 |
628 } // namespace | 628 } // namespace |
629 | 629 |
630 } // namespace net | 630 } // namespace net |
OLD | NEW |