| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/spdy/hpack/hpack_input_stream.h" | |
| 6 | |
| 7 #include <bitset> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "net/spdy/hpack/hpack_constants.h" | |
| 12 #include "net/spdy/spdy_test_utils.h" | |
| 13 #include "net/test/gtest_util.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace net { | |
| 17 | |
| 18 namespace test { | |
| 19 | |
| 20 using test::a2b_hex; | |
| 21 | |
| 22 // Hex representation of encoded length and Huffman string. | |
| 23 const char kEncodedHuffmanFixture[] = | |
| 24 "2d" // Length prefix. | |
| 25 "94e7821dd7f2e6c7b335dfdfcd5b3960" | |
| 26 "d5af27087f3672c1ab270fb5291f9587" | |
| 27 "316065c003ed4ee5b1063d5007"; | |
| 28 | |
| 29 const char kDecodedHuffmanFixture[] = | |
| 30 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"; | |
| 31 | |
| 32 class HpackInputStreamPeer { | |
| 33 public: | |
| 34 explicit HpackInputStreamPeer(HpackInputStream* input_stream) | |
| 35 : input_stream_(input_stream) {} | |
| 36 | |
| 37 void SetBitOffsetForTest(size_t bit_offset) { | |
| 38 input_stream_->bit_offset_ = bit_offset; | |
| 39 } | |
| 40 | |
| 41 uint32_t ParsedBytesCurrent() { return input_stream_->parsed_bytes_current_; } | |
| 42 | |
| 43 private: | |
| 44 HpackInputStream* input_stream_; | |
| 45 }; | |
| 46 | |
| 47 // Utility function to decode an assumed-valid uint32_t with an N-bit | |
| 48 // prefix. | |
| 49 uint32_t DecodeValidUint32(uint8_t N, SpdyStringPiece str) { | |
| 50 EXPECT_GT(N, 0); | |
| 51 EXPECT_LE(N, 8); | |
| 52 HpackInputStream input_stream(str); | |
| 53 HpackInputStreamPeer input_stream_peer(&input_stream); | |
| 54 input_stream_peer.SetBitOffsetForTest(8 - N); | |
| 55 uint32_t I; | |
| 56 EXPECT_TRUE(input_stream.DecodeNextUint32(&I)); | |
| 57 EXPECT_EQ(str.size(), input_stream_peer.ParsedBytesCurrent()); | |
| 58 EXPECT_FALSE(input_stream.NeedMoreData()); | |
| 59 return I; | |
| 60 } | |
| 61 | |
| 62 // Utility function to decode an assumed-invalid uint32_t with an N-bit | |
| 63 // prefix. | |
| 64 void ExpectDecodeUint32Invalid(uint8_t N, SpdyStringPiece str) { | |
| 65 EXPECT_GT(N, 0); | |
| 66 EXPECT_LE(N, 8); | |
| 67 HpackInputStream input_stream(str); | |
| 68 HpackInputStreamPeer input_stream_peer(&input_stream); | |
| 69 input_stream_peer.SetBitOffsetForTest(8 - N); | |
| 70 uint32_t I; | |
| 71 EXPECT_FALSE(input_stream.DecodeNextUint32(&I)); | |
| 72 } | |
| 73 | |
| 74 uint32_t bits32(const SpdyString& bitstring) { | |
| 75 return std::bitset<32>(bitstring).to_ulong(); | |
| 76 } | |
| 77 | |
| 78 // The {Number}ByteIntegersEightBitPrefix tests below test that | |
| 79 // certain integers are decoded correctly with an 8-bit prefix in | |
| 80 // exactly {Number} bytes. | |
| 81 | |
| 82 TEST(HpackInputStreamTest, OneByteIntegersEightBitPrefix) { | |
| 83 // Minimum. | |
| 84 EXPECT_EQ(0x00u, DecodeValidUint32(8, SpdyString("\x00", 1))); | |
| 85 EXPECT_EQ(0x7fu, DecodeValidUint32(8, "\x7f")); | |
| 86 // Maximum. | |
| 87 EXPECT_EQ(0xfeu, DecodeValidUint32(8, "\xfe")); | |
| 88 // Invalid. | |
| 89 ExpectDecodeUint32Invalid(8, "\xff"); | |
| 90 } | |
| 91 | |
| 92 TEST(HpackInputStreamTest, TwoByteIntegersEightBitPrefix) { | |
| 93 // Minimum. | |
| 94 EXPECT_EQ(0xffu, DecodeValidUint32(8, SpdyString("\xff\x00", 2))); | |
| 95 EXPECT_EQ(0x0100u, DecodeValidUint32(8, "\xff\x01")); | |
| 96 // Maximum. | |
| 97 EXPECT_EQ(0x017eu, DecodeValidUint32(8, "\xff\x7f")); | |
| 98 // Invalid. | |
| 99 ExpectDecodeUint32Invalid(8, "\xff\x80"); | |
| 100 ExpectDecodeUint32Invalid(8, "\xff\xff"); | |
| 101 } | |
| 102 | |
| 103 TEST(HpackInputStreamTest, ThreeByteIntegersEightBitPrefix) { | |
| 104 // Minimum. | |
| 105 EXPECT_EQ(0x017fu, DecodeValidUint32(8, "\xff\x80\x01")); | |
| 106 EXPECT_EQ(0x0fffu, DecodeValidUint32(8, "\xff\x80\x1e")); | |
| 107 // Maximum. | |
| 108 EXPECT_EQ(0x40feu, DecodeValidUint32(8, "\xff\xff\x7f")); | |
| 109 // Invalid. | |
| 110 ExpectDecodeUint32Invalid(8, "\xff\x80\x00"); | |
| 111 ExpectDecodeUint32Invalid(8, "\xff\xff\x00"); | |
| 112 ExpectDecodeUint32Invalid(8, "\xff\xff\x80"); | |
| 113 ExpectDecodeUint32Invalid(8, "\xff\xff\xff"); | |
| 114 } | |
| 115 | |
| 116 TEST(HpackInputStreamTest, FourByteIntegersEightBitPrefix) { | |
| 117 // Minimum. | |
| 118 EXPECT_EQ(0x40ffu, DecodeValidUint32(8, "\xff\x80\x80\x01")); | |
| 119 EXPECT_EQ(0xffffu, DecodeValidUint32(8, "\xff\x80\xfe\x03")); | |
| 120 // Maximum. | |
| 121 EXPECT_EQ(0x002000feu, DecodeValidUint32(8, "\xff\xff\xff\x7f")); | |
| 122 // Invalid. | |
| 123 ExpectDecodeUint32Invalid(8, "\xff\xff\x80\x00"); | |
| 124 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x00"); | |
| 125 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80"); | |
| 126 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff"); | |
| 127 } | |
| 128 | |
| 129 TEST(HpackInputStreamTest, FiveByteIntegersEightBitPrefix) { | |
| 130 // Minimum. | |
| 131 EXPECT_EQ(0x002000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x01")); | |
| 132 EXPECT_EQ(0x00ffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\x07")); | |
| 133 // Maximum. | |
| 134 EXPECT_EQ(0x100000feu, DecodeValidUint32(8, "\xff\xff\xff\xff\x7f")); | |
| 135 // Invalid. | |
| 136 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80\x00"); | |
| 137 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x00"); | |
| 138 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x80"); | |
| 139 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff"); | |
| 140 } | |
| 141 | |
| 142 TEST(HpackInputStreamTest, SixByteIntegersEightBitPrefix) { | |
| 143 // Minimum. | |
| 144 EXPECT_EQ(0x100000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x80\x01")); | |
| 145 // Maximum. | |
| 146 EXPECT_EQ(0xffffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\xff\x0f")); | |
| 147 // Invalid. | |
| 148 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x00"); | |
| 149 ExpectDecodeUint32Invalid(8, "\xff\x80\xfe\xff\xff\x10"); | |
| 150 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff"); | |
| 151 } | |
| 152 | |
| 153 // There are no valid uint32_t encodings that are greater than six | |
| 154 // bytes. | |
| 155 TEST(HpackInputStreamTest, SevenByteIntegersEightBitPrefix) { | |
| 156 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x00"); | |
| 157 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x01"); | |
| 158 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff\xff"); | |
| 159 } | |
| 160 | |
| 161 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that | |
| 162 // certain integers are encoded correctly with an N-bit prefix in | |
| 163 // exactly {Number} bytes for N in {1, 2, ..., 7}. | |
| 164 | |
| 165 TEST(HpackInputStreamTest, OneByteIntegersOneToSevenBitPrefixes) { | |
| 166 // Minimums. | |
| 167 EXPECT_EQ(0x00u, DecodeValidUint32(7, SpdyString("\x00", 1))); | |
| 168 EXPECT_EQ(0x00u, DecodeValidUint32(7, "\x80")); | |
| 169 EXPECT_EQ(0x00u, DecodeValidUint32(6, SpdyString("\x00", 1))); | |
| 170 EXPECT_EQ(0x00u, DecodeValidUint32(6, "\xc0")); | |
| 171 EXPECT_EQ(0x00u, DecodeValidUint32(5, SpdyString("\x00", 1))); | |
| 172 EXPECT_EQ(0x00u, DecodeValidUint32(5, "\xe0")); | |
| 173 EXPECT_EQ(0x00u, DecodeValidUint32(4, SpdyString("\x00", 1))); | |
| 174 EXPECT_EQ(0x00u, DecodeValidUint32(4, "\xf0")); | |
| 175 EXPECT_EQ(0x00u, DecodeValidUint32(3, SpdyString("\x00", 1))); | |
| 176 EXPECT_EQ(0x00u, DecodeValidUint32(3, "\xf8")); | |
| 177 EXPECT_EQ(0x00u, DecodeValidUint32(2, SpdyString("\x00", 1))); | |
| 178 EXPECT_EQ(0x00u, DecodeValidUint32(2, "\xfc")); | |
| 179 EXPECT_EQ(0x00u, DecodeValidUint32(1, SpdyString("\x00", 1))); | |
| 180 EXPECT_EQ(0x00u, DecodeValidUint32(1, "\xfe")); | |
| 181 | |
| 182 // Maximums. | |
| 183 EXPECT_EQ(0x7eu, DecodeValidUint32(7, "\x7e")); | |
| 184 EXPECT_EQ(0x7eu, DecodeValidUint32(7, "\xfe")); | |
| 185 EXPECT_EQ(0x3eu, DecodeValidUint32(6, "\x3e")); | |
| 186 EXPECT_EQ(0x3eu, DecodeValidUint32(6, "\xfe")); | |
| 187 EXPECT_EQ(0x1eu, DecodeValidUint32(5, "\x1e")); | |
| 188 EXPECT_EQ(0x1eu, DecodeValidUint32(5, "\xfe")); | |
| 189 EXPECT_EQ(0x0eu, DecodeValidUint32(4, "\x0e")); | |
| 190 EXPECT_EQ(0x0eu, DecodeValidUint32(4, "\xfe")); | |
| 191 EXPECT_EQ(0x06u, DecodeValidUint32(3, "\x06")); | |
| 192 EXPECT_EQ(0x06u, DecodeValidUint32(3, "\xfe")); | |
| 193 EXPECT_EQ(0x02u, DecodeValidUint32(2, "\x02")); | |
| 194 EXPECT_EQ(0x02u, DecodeValidUint32(2, "\xfe")); | |
| 195 EXPECT_EQ(0x00u, DecodeValidUint32(1, SpdyString("\x00", 1))); | |
| 196 EXPECT_EQ(0x00u, DecodeValidUint32(1, "\xfe")); | |
| 197 | |
| 198 // Invalid. | |
| 199 ExpectDecodeUint32Invalid(7, "\x7f"); | |
| 200 ExpectDecodeUint32Invalid(7, "\xff"); | |
| 201 ExpectDecodeUint32Invalid(6, "\x3f"); | |
| 202 ExpectDecodeUint32Invalid(6, "\xff"); | |
| 203 ExpectDecodeUint32Invalid(5, "\x1f"); | |
| 204 ExpectDecodeUint32Invalid(5, "\xff"); | |
| 205 ExpectDecodeUint32Invalid(4, "\x0f"); | |
| 206 ExpectDecodeUint32Invalid(4, "\xff"); | |
| 207 ExpectDecodeUint32Invalid(3, "\x07"); | |
| 208 ExpectDecodeUint32Invalid(3, "\xff"); | |
| 209 ExpectDecodeUint32Invalid(2, "\x03"); | |
| 210 ExpectDecodeUint32Invalid(2, "\xff"); | |
| 211 ExpectDecodeUint32Invalid(1, "\x01"); | |
| 212 ExpectDecodeUint32Invalid(1, "\xff"); | |
| 213 } | |
| 214 | |
| 215 TEST(HpackInputStreamTest, TwoByteIntegersOneToSevenBitPrefixes) { | |
| 216 // Minimums. | |
| 217 EXPECT_EQ(0x7fu, DecodeValidUint32(7, SpdyString("\x7f\x00", 2))); | |
| 218 EXPECT_EQ(0x7fu, DecodeValidUint32(7, SpdyString("\xff\x00", 2))); | |
| 219 EXPECT_EQ(0x3fu, DecodeValidUint32(6, SpdyString("\x3f\x00", 2))); | |
| 220 EXPECT_EQ(0x3fu, DecodeValidUint32(6, SpdyString("\xff\x00", 2))); | |
| 221 EXPECT_EQ(0x1fu, DecodeValidUint32(5, SpdyString("\x1f\x00", 2))); | |
| 222 EXPECT_EQ(0x1fu, DecodeValidUint32(5, SpdyString("\xff\x00", 2))); | |
| 223 EXPECT_EQ(0x0fu, DecodeValidUint32(4, SpdyString("\x0f\x00", 2))); | |
| 224 EXPECT_EQ(0x0fu, DecodeValidUint32(4, SpdyString("\xff\x00", 2))); | |
| 225 EXPECT_EQ(0x07u, DecodeValidUint32(3, SpdyString("\x07\x00", 2))); | |
| 226 EXPECT_EQ(0x07u, DecodeValidUint32(3, SpdyString("\xff\x00", 2))); | |
| 227 EXPECT_EQ(0x03u, DecodeValidUint32(2, SpdyString("\x03\x00", 2))); | |
| 228 EXPECT_EQ(0x03u, DecodeValidUint32(2, SpdyString("\xff\x00", 2))); | |
| 229 EXPECT_EQ(0x01u, DecodeValidUint32(1, SpdyString("\x01\x00", 2))); | |
| 230 EXPECT_EQ(0x01u, DecodeValidUint32(1, SpdyString("\xff\x00", 2))); | |
| 231 | |
| 232 // Maximums. | |
| 233 EXPECT_EQ(0xfeu, DecodeValidUint32(7, "\x7f\x7f")); | |
| 234 EXPECT_EQ(0xfeu, DecodeValidUint32(7, "\xff\x7f")); | |
| 235 EXPECT_EQ(0xbeu, DecodeValidUint32(6, "\x3f\x7f")); | |
| 236 EXPECT_EQ(0xbeu, DecodeValidUint32(6, "\xff\x7f")); | |
| 237 EXPECT_EQ(0x9eu, DecodeValidUint32(5, "\x1f\x7f")); | |
| 238 EXPECT_EQ(0x9eu, DecodeValidUint32(5, "\xff\x7f")); | |
| 239 EXPECT_EQ(0x8eu, DecodeValidUint32(4, "\x0f\x7f")); | |
| 240 EXPECT_EQ(0x8eu, DecodeValidUint32(4, "\xff\x7f")); | |
| 241 EXPECT_EQ(0x86u, DecodeValidUint32(3, "\x07\x7f")); | |
| 242 EXPECT_EQ(0x86u, DecodeValidUint32(3, "\xff\x7f")); | |
| 243 EXPECT_EQ(0x82u, DecodeValidUint32(2, "\x03\x7f")); | |
| 244 EXPECT_EQ(0x82u, DecodeValidUint32(2, "\xff\x7f")); | |
| 245 EXPECT_EQ(0x80u, DecodeValidUint32(1, "\x01\x7f")); | |
| 246 EXPECT_EQ(0x80u, DecodeValidUint32(1, "\xff\x7f")); | |
| 247 | |
| 248 // Invalid. | |
| 249 ExpectDecodeUint32Invalid(7, "\x7f\x80"); | |
| 250 ExpectDecodeUint32Invalid(7, "\xff\xff"); | |
| 251 ExpectDecodeUint32Invalid(6, "\x3f\x80"); | |
| 252 ExpectDecodeUint32Invalid(6, "\xff\xff"); | |
| 253 ExpectDecodeUint32Invalid(5, "\x1f\x80"); | |
| 254 ExpectDecodeUint32Invalid(5, "\xff\xff"); | |
| 255 ExpectDecodeUint32Invalid(4, "\x0f\x80"); | |
| 256 ExpectDecodeUint32Invalid(4, "\xff\xff"); | |
| 257 ExpectDecodeUint32Invalid(3, "\x07\x80"); | |
| 258 ExpectDecodeUint32Invalid(3, "\xff\xff"); | |
| 259 ExpectDecodeUint32Invalid(2, "\x03\x80"); | |
| 260 ExpectDecodeUint32Invalid(2, "\xff\xff"); | |
| 261 ExpectDecodeUint32Invalid(1, "\x01\x80"); | |
| 262 ExpectDecodeUint32Invalid(1, "\xff\xff"); | |
| 263 } | |
| 264 | |
| 265 TEST(HpackInputStreamTest, ThreeByteIntegersOneToSevenBitPrefixes) { | |
| 266 // Minimums. | |
| 267 EXPECT_EQ(0xffu, DecodeValidUint32(7, "\x7f\x80\x01")); | |
| 268 EXPECT_EQ(0xffu, DecodeValidUint32(7, "\xff\x80\x01")); | |
| 269 EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\x3f\x80\x01")); | |
| 270 EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\xff\x80\x01")); | |
| 271 EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\x1f\x80\x01")); | |
| 272 EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\xff\x80\x01")); | |
| 273 EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\x0f\x80\x01")); | |
| 274 EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\xff\x80\x01")); | |
| 275 EXPECT_EQ(0x87u, DecodeValidUint32(3, "\x07\x80\x01")); | |
| 276 EXPECT_EQ(0x87u, DecodeValidUint32(3, "\xff\x80\x01")); | |
| 277 EXPECT_EQ(0x83u, DecodeValidUint32(2, "\x03\x80\x01")); | |
| 278 EXPECT_EQ(0x83u, DecodeValidUint32(2, "\xff\x80\x01")); | |
| 279 EXPECT_EQ(0x81u, DecodeValidUint32(1, "\x01\x80\x01")); | |
| 280 EXPECT_EQ(0x81u, DecodeValidUint32(1, "\xff\x80\x01")); | |
| 281 | |
| 282 // Maximums. | |
| 283 EXPECT_EQ(0x407eu, DecodeValidUint32(7, "\x7f\xff\x7f")); | |
| 284 EXPECT_EQ(0x407eu, DecodeValidUint32(7, "\xff\xff\x7f")); | |
| 285 EXPECT_EQ(0x403eu, DecodeValidUint32(6, "\x3f\xff\x7f")); | |
| 286 EXPECT_EQ(0x403eu, DecodeValidUint32(6, "\xff\xff\x7f")); | |
| 287 EXPECT_EQ(0x401eu, DecodeValidUint32(5, "\x1f\xff\x7f")); | |
| 288 EXPECT_EQ(0x401eu, DecodeValidUint32(5, "\xff\xff\x7f")); | |
| 289 EXPECT_EQ(0x400eu, DecodeValidUint32(4, "\x0f\xff\x7f")); | |
| 290 EXPECT_EQ(0x400eu, DecodeValidUint32(4, "\xff\xff\x7f")); | |
| 291 EXPECT_EQ(0x4006u, DecodeValidUint32(3, "\x07\xff\x7f")); | |
| 292 EXPECT_EQ(0x4006u, DecodeValidUint32(3, "\xff\xff\x7f")); | |
| 293 EXPECT_EQ(0x4002u, DecodeValidUint32(2, "\x03\xff\x7f")); | |
| 294 EXPECT_EQ(0x4002u, DecodeValidUint32(2, "\xff\xff\x7f")); | |
| 295 EXPECT_EQ(0x4000u, DecodeValidUint32(1, "\x01\xff\x7f")); | |
| 296 EXPECT_EQ(0x4000u, DecodeValidUint32(1, "\xff\xff\x7f")); | |
| 297 | |
| 298 // Invalid. | |
| 299 ExpectDecodeUint32Invalid(7, "\x7f\xff\x80"); | |
| 300 ExpectDecodeUint32Invalid(7, "\xff\xff\xff"); | |
| 301 ExpectDecodeUint32Invalid(6, "\x3f\xff\x80"); | |
| 302 ExpectDecodeUint32Invalid(6, "\xff\xff\xff"); | |
| 303 ExpectDecodeUint32Invalid(5, "\x1f\xff\x80"); | |
| 304 ExpectDecodeUint32Invalid(5, "\xff\xff\xff"); | |
| 305 ExpectDecodeUint32Invalid(4, "\x0f\xff\x80"); | |
| 306 ExpectDecodeUint32Invalid(4, "\xff\xff\xff"); | |
| 307 ExpectDecodeUint32Invalid(3, "\x07\xff\x80"); | |
| 308 ExpectDecodeUint32Invalid(3, "\xff\xff\xff"); | |
| 309 ExpectDecodeUint32Invalid(2, "\x03\xff\x80"); | |
| 310 ExpectDecodeUint32Invalid(2, "\xff\xff\xff"); | |
| 311 ExpectDecodeUint32Invalid(1, "\x01\xff\x80"); | |
| 312 ExpectDecodeUint32Invalid(1, "\xff\xff\xff"); | |
| 313 } | |
| 314 | |
| 315 TEST(HpackInputStreamTest, FourByteIntegersOneToSevenBitPrefixes) { | |
| 316 // Minimums. | |
| 317 EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\x7f\x80\x80\x01")); | |
| 318 EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\xff\x80\x80\x01")); | |
| 319 EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\x3f\x80\x80\x01")); | |
| 320 EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\xff\x80\x80\x01")); | |
| 321 EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\x1f\x80\x80\x01")); | |
| 322 EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\xff\x80\x80\x01")); | |
| 323 EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\x0f\x80\x80\x01")); | |
| 324 EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\xff\x80\x80\x01")); | |
| 325 EXPECT_EQ(0x4007u, DecodeValidUint32(3, "\x07\x80\x80\x01")); | |
| 326 EXPECT_EQ(0x4007u, DecodeValidUint32(3, "\xff\x80\x80\x01")); | |
| 327 EXPECT_EQ(0x4003u, DecodeValidUint32(2, "\x03\x80\x80\x01")); | |
| 328 EXPECT_EQ(0x4003u, DecodeValidUint32(2, "\xff\x80\x80\x01")); | |
| 329 EXPECT_EQ(0x4001u, DecodeValidUint32(1, "\x01\x80\x80\x01")); | |
| 330 EXPECT_EQ(0x4001u, DecodeValidUint32(1, "\xff\x80\x80\x01")); | |
| 331 | |
| 332 // Maximums. | |
| 333 EXPECT_EQ(0x20007eu, DecodeValidUint32(7, "\x7f\xff\xff\x7f")); | |
| 334 EXPECT_EQ(0x20007eu, DecodeValidUint32(7, "\xff\xff\xff\x7f")); | |
| 335 EXPECT_EQ(0x20003eu, DecodeValidUint32(6, "\x3f\xff\xff\x7f")); | |
| 336 EXPECT_EQ(0x20003eu, DecodeValidUint32(6, "\xff\xff\xff\x7f")); | |
| 337 EXPECT_EQ(0x20001eu, DecodeValidUint32(5, "\x1f\xff\xff\x7f")); | |
| 338 EXPECT_EQ(0x20001eu, DecodeValidUint32(5, "\xff\xff\xff\x7f")); | |
| 339 EXPECT_EQ(0x20000eu, DecodeValidUint32(4, "\x0f\xff\xff\x7f")); | |
| 340 EXPECT_EQ(0x20000eu, DecodeValidUint32(4, "\xff\xff\xff\x7f")); | |
| 341 EXPECT_EQ(0x200006u, DecodeValidUint32(3, "\x07\xff\xff\x7f")); | |
| 342 EXPECT_EQ(0x200006u, DecodeValidUint32(3, "\xff\xff\xff\x7f")); | |
| 343 EXPECT_EQ(0x200002u, DecodeValidUint32(2, "\x03\xff\xff\x7f")); | |
| 344 EXPECT_EQ(0x200002u, DecodeValidUint32(2, "\xff\xff\xff\x7f")); | |
| 345 EXPECT_EQ(0x200000u, DecodeValidUint32(1, "\x01\xff\xff\x7f")); | |
| 346 EXPECT_EQ(0x200000u, DecodeValidUint32(1, "\xff\xff\xff\x7f")); | |
| 347 | |
| 348 // Invalid. | |
| 349 ExpectDecodeUint32Invalid(7, "\x7f\xff\xff\x80"); | |
| 350 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff"); | |
| 351 ExpectDecodeUint32Invalid(6, "\x3f\xff\xff\x80"); | |
| 352 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff"); | |
| 353 ExpectDecodeUint32Invalid(5, "\x1f\xff\xff\x80"); | |
| 354 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff"); | |
| 355 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\x80"); | |
| 356 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff"); | |
| 357 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\x80"); | |
| 358 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff"); | |
| 359 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\x80"); | |
| 360 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff"); | |
| 361 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\x80"); | |
| 362 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff"); | |
| 363 } | |
| 364 | |
| 365 TEST(HpackInputStreamTest, FiveByteIntegersOneToSevenBitPrefixes) { | |
| 366 // Minimums. | |
| 367 EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x01")); | |
| 368 EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x01")); | |
| 369 EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x01")); | |
| 370 EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x01")); | |
| 371 EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x01")); | |
| 372 EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x01")); | |
| 373 EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x01")); | |
| 374 EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x01")); | |
| 375 EXPECT_EQ(0x200007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x01")); | |
| 376 EXPECT_EQ(0x200007u, DecodeValidUint32(3, "\xff\x80\x80\x80\x01")); | |
| 377 EXPECT_EQ(0x200003u, DecodeValidUint32(2, "\x03\x80\x80\x80\x01")); | |
| 378 EXPECT_EQ(0x200003u, DecodeValidUint32(2, "\xff\x80\x80\x80\x01")); | |
| 379 EXPECT_EQ(0x200001u, DecodeValidUint32(1, "\x01\x80\x80\x80\x01")); | |
| 380 EXPECT_EQ(0x200001u, DecodeValidUint32(1, "\xff\x80\x80\x80\x01")); | |
| 381 | |
| 382 // Maximums. | |
| 383 EXPECT_EQ(0x1000007eu, DecodeValidUint32(7, "\x7f\xff\xff\xff\x7f")); | |
| 384 EXPECT_EQ(0x1000007eu, DecodeValidUint32(7, "\xff\xff\xff\xff\x7f")); | |
| 385 EXPECT_EQ(0x1000003eu, DecodeValidUint32(6, "\x3f\xff\xff\xff\x7f")); | |
| 386 EXPECT_EQ(0x1000003eu, DecodeValidUint32(6, "\xff\xff\xff\xff\x7f")); | |
| 387 EXPECT_EQ(0x1000001eu, DecodeValidUint32(5, "\x1f\xff\xff\xff\x7f")); | |
| 388 EXPECT_EQ(0x1000001eu, DecodeValidUint32(5, "\xff\xff\xff\xff\x7f")); | |
| 389 EXPECT_EQ(0x1000000eu, DecodeValidUint32(4, "\x0f\xff\xff\xff\x7f")); | |
| 390 EXPECT_EQ(0x1000000eu, DecodeValidUint32(4, "\xff\xff\xff\xff\x7f")); | |
| 391 EXPECT_EQ(0x10000006u, DecodeValidUint32(3, "\x07\xff\xff\xff\x7f")); | |
| 392 EXPECT_EQ(0x10000006u, DecodeValidUint32(3, "\xff\xff\xff\xff\x7f")); | |
| 393 EXPECT_EQ(0x10000002u, DecodeValidUint32(2, "\x03\xff\xff\xff\x7f")); | |
| 394 EXPECT_EQ(0x10000002u, DecodeValidUint32(2, "\xff\xff\xff\xff\x7f")); | |
| 395 EXPECT_EQ(0x10000000u, DecodeValidUint32(1, "\x01\xff\xff\xff\x7f")); | |
| 396 EXPECT_EQ(0x10000000u, DecodeValidUint32(1, "\xff\xff\xff\xff\x7f")); | |
| 397 | |
| 398 // Invalid. | |
| 399 ExpectDecodeUint32Invalid(7, "\x7f\xff\xff\xff\x80"); | |
| 400 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff"); | |
| 401 ExpectDecodeUint32Invalid(6, "\x3f\xff\xff\xff\x80"); | |
| 402 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff"); | |
| 403 ExpectDecodeUint32Invalid(5, "\x1f\xff\xff\xff\x80"); | |
| 404 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff"); | |
| 405 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\xff\x80"); | |
| 406 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff"); | |
| 407 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\xff\x80"); | |
| 408 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff"); | |
| 409 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\xff\x80"); | |
| 410 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff"); | |
| 411 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\xff\x80"); | |
| 412 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff"); | |
| 413 } | |
| 414 | |
| 415 TEST(HpackInputStreamTest, SixByteIntegersOneToSevenBitPrefixes) { | |
| 416 // Minimums. | |
| 417 EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x80\x01")); | |
| 418 EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x80\x01")); | |
| 419 EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x80\x01")); | |
| 420 EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x80\x01")); | |
| 421 EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x80\x01")); | |
| 422 EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x80\x01")); | |
| 423 EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x80\x01")); | |
| 424 EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x80\x01")); | |
| 425 EXPECT_EQ(0x10000007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x80\x01")); | |
| 426 EXPECT_EQ(0x10000007u, DecodeValidUint32(3, "\xff\x80\x80\x80\x80\x01")); | |
| 427 EXPECT_EQ(0x10000003u, DecodeValidUint32(2, "\x03\x80\x80\x80\x80\x01")); | |
| 428 EXPECT_EQ(0x10000003u, DecodeValidUint32(2, "\xff\x80\x80\x80\x80\x01")); | |
| 429 EXPECT_EQ(0x10000001u, DecodeValidUint32(1, "\x01\x80\x80\x80\x80\x01")); | |
| 430 EXPECT_EQ(0x10000001u, DecodeValidUint32(1, "\xff\x80\x80\x80\x80\x01")); | |
| 431 | |
| 432 // Maximums. | |
| 433 EXPECT_EQ(0xffffffffu, DecodeValidUint32(7, "\x7f\x80\xff\xff\xff\x0f")); | |
| 434 EXPECT_EQ(0xffffffffu, DecodeValidUint32(7, "\xff\x80\xff\xff\xff\x0f")); | |
| 435 EXPECT_EQ(0xffffffffu, DecodeValidUint32(6, "\x3f\xc0\xff\xff\xff\x0f")); | |
| 436 EXPECT_EQ(0xffffffffu, DecodeValidUint32(6, "\xff\xc0\xff\xff\xff\x0f")); | |
| 437 EXPECT_EQ(0xffffffffu, DecodeValidUint32(5, "\x1f\xe0\xff\xff\xff\x0f")); | |
| 438 EXPECT_EQ(0xffffffffu, DecodeValidUint32(5, "\xff\xe0\xff\xff\xff\x0f")); | |
| 439 EXPECT_EQ(0xffffffffu, DecodeValidUint32(4, "\x0f\xf0\xff\xff\xff\x0f")); | |
| 440 EXPECT_EQ(0xffffffffu, DecodeValidUint32(4, "\xff\xf0\xff\xff\xff\x0f")); | |
| 441 EXPECT_EQ(0xffffffffu, DecodeValidUint32(3, "\x07\xf8\xff\xff\xff\x0f")); | |
| 442 EXPECT_EQ(0xffffffffu, DecodeValidUint32(3, "\xff\xf8\xff\xff\xff\x0f")); | |
| 443 EXPECT_EQ(0xffffffffu, DecodeValidUint32(2, "\x03\xfc\xff\xff\xff\x0f")); | |
| 444 EXPECT_EQ(0xffffffffu, DecodeValidUint32(2, "\xff\xfc\xff\xff\xff\x0f")); | |
| 445 EXPECT_EQ(0xffffffffu, DecodeValidUint32(1, "\x01\xfe\xff\xff\xff\x0f")); | |
| 446 EXPECT_EQ(0xffffffffu, DecodeValidUint32(1, "\xff\xfe\xff\xff\xff\x0f")); | |
| 447 | |
| 448 // Invalid. | |
| 449 ExpectDecodeUint32Invalid(7, "\x7f\x80\xff\xff\xff\x10"); | |
| 450 ExpectDecodeUint32Invalid(7, "\xff\x80\xff\xff\xff\xff"); | |
| 451 ExpectDecodeUint32Invalid(6, "\x3f\xc0\xff\xff\xff\x10"); | |
| 452 ExpectDecodeUint32Invalid(6, "\xff\xc0\xff\xff\xff\xff"); | |
| 453 ExpectDecodeUint32Invalid(5, "\x1f\xe0\xff\xff\xff\x10"); | |
| 454 ExpectDecodeUint32Invalid(5, "\xff\xe0\xff\xff\xff\xff"); | |
| 455 ExpectDecodeUint32Invalid(4, "\x0f\xf0\xff\xff\xff\x10"); | |
| 456 ExpectDecodeUint32Invalid(4, "\xff\xf0\xff\xff\xff\xff"); | |
| 457 ExpectDecodeUint32Invalid(3, "\x07\xf8\xff\xff\xff\x10"); | |
| 458 ExpectDecodeUint32Invalid(3, "\xff\xf8\xff\xff\xff\xff"); | |
| 459 ExpectDecodeUint32Invalid(2, "\x03\xfc\xff\xff\xff\x10"); | |
| 460 ExpectDecodeUint32Invalid(2, "\xff\xfc\xff\xff\xff\xff"); | |
| 461 ExpectDecodeUint32Invalid(1, "\x01\xfe\xff\xff\xff\x10"); | |
| 462 ExpectDecodeUint32Invalid(1, "\xff\xfe\xff\xff\xff\xff"); | |
| 463 } | |
| 464 | |
| 465 // There are no valid uint32_t encodings that are greater than six | |
| 466 // bytes. | |
| 467 TEST(HpackInputStreamTest, SevenByteIntegersOneToSevenBitPrefixes) { | |
| 468 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x00"); | |
| 469 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x01"); | |
| 470 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff\xff\xff"); | |
| 471 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x00"); | |
| 472 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x01"); | |
| 473 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff\xff\xff"); | |
| 474 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x00"); | |
| 475 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x01"); | |
| 476 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff\xff\xff"); | |
| 477 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x00"); | |
| 478 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x01"); | |
| 479 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff\xff\xff"); | |
| 480 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x00"); | |
| 481 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x01"); | |
| 482 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff\xff\xff"); | |
| 483 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x00"); | |
| 484 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x01"); | |
| 485 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff\xff\xff"); | |
| 486 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x00"); | |
| 487 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x01"); | |
| 488 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff\xff\xff"); | |
| 489 } | |
| 490 | |
| 491 // Decoding a valid encoded string literal should work. | |
| 492 TEST(HpackInputStreamTest, DecodeNextIdentityString) { | |
| 493 HpackInputStream input_stream("\x0estring literal"); | |
| 494 HpackInputStreamPeer input_stream_peer(&input_stream); | |
| 495 | |
| 496 EXPECT_TRUE(input_stream.HasMoreData()); | |
| 497 SpdyStringPiece string_piece; | |
| 498 EXPECT_TRUE(input_stream.DecodeNextIdentityString(&string_piece)); | |
| 499 EXPECT_EQ("string literal", string_piece); | |
| 500 EXPECT_FALSE(input_stream.HasMoreData()); | |
| 501 EXPECT_EQ(string_piece.size() + 1, input_stream_peer.ParsedBytesCurrent()); | |
| 502 EXPECT_FALSE(input_stream.NeedMoreData()); | |
| 503 } | |
| 504 | |
| 505 // Decoding an encoded string literal with size larger than the | |
| 506 // remainder of the buffer should fail. | |
| 507 TEST(HpackInputStreamTest, DecodeNextIdentityStringNotEnoughInput) { | |
| 508 // Set the length to be one more than it should be. | |
| 509 HpackInputStream input_stream("\x0fstring literal"); | |
| 510 | |
| 511 EXPECT_TRUE(input_stream.HasMoreData()); | |
| 512 SpdyStringPiece string_piece; | |
| 513 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece)); | |
| 514 EXPECT_TRUE(input_stream.NeedMoreData()); | |
| 515 } | |
| 516 | |
| 517 TEST(HpackInputStreamTest, DecodeNextHuffmanString) { | |
| 518 SpdyString output, input(a2b_hex(kEncodedHuffmanFixture)); | |
| 519 HpackInputStream input_stream(input); | |
| 520 HpackInputStreamPeer input_stream_peer(&input_stream); | |
| 521 | |
| 522 EXPECT_TRUE(input_stream.HasMoreData()); | |
| 523 EXPECT_TRUE(input_stream.DecodeNextHuffmanString(&output)); | |
| 524 EXPECT_EQ(kDecodedHuffmanFixture, output); | |
| 525 EXPECT_FALSE(input_stream.HasMoreData()); | |
| 526 EXPECT_FALSE(input_stream.NeedMoreData()); | |
| 527 EXPECT_EQ(46u, input_stream_peer.ParsedBytesCurrent()); | |
| 528 } | |
| 529 | |
| 530 TEST(HpackInputStreamTest, DecodeNextHuffmanStringNotEnoughInput) { | |
| 531 SpdyString output, input(a2b_hex(kEncodedHuffmanFixture)); | |
| 532 input[0]++; // Input prefix is one byte larger than available input. | |
| 533 HpackInputStream input_stream(input); | |
| 534 | |
| 535 // Not enough buffer for declared encoded length. | |
| 536 EXPECT_TRUE(input_stream.HasMoreData()); | |
| 537 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(&output)); | |
| 538 EXPECT_TRUE(input_stream.NeedMoreData()); | |
| 539 } | |
| 540 | |
| 541 TEST(HpackInputStreamTest, PeekBitsAndConsume) { | |
| 542 HpackInputStream input_stream("\xad\xab\xad\xab\xad"); | |
| 543 | |
| 544 uint32_t bits = 0; | |
| 545 size_t peeked_count = 0; | |
| 546 | |
| 547 // Read 0xad. | |
| 548 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | |
| 549 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits); | |
| 550 EXPECT_EQ(8u, peeked_count); | |
| 551 | |
| 552 // Read 0xab. | |
| 553 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | |
| 554 EXPECT_EQ(bits32("10101101101010110000000000000000"), bits); | |
| 555 EXPECT_EQ(16u, peeked_count); | |
| 556 | |
| 557 input_stream.ConsumeBits(5); | |
| 558 bits = bits << 5; | |
| 559 peeked_count -= 5; | |
| 560 EXPECT_EQ(bits32("10110101011000000000000000000000"), bits); | |
| 561 EXPECT_EQ(11u, peeked_count); | |
| 562 | |
| 563 // Read 0xad. | |
| 564 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | |
| 565 EXPECT_EQ(bits32("10110101011101011010000000000000"), bits); | |
| 566 EXPECT_EQ(19u, peeked_count); | |
| 567 | |
| 568 // Read 0xab. | |
| 569 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | |
| 570 EXPECT_EQ(bits32("10110101011101011011010101100000"), bits); | |
| 571 EXPECT_EQ(27u, peeked_count); | |
| 572 | |
| 573 // Read 0xa, and 1 bit of 0xd | |
| 574 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | |
| 575 EXPECT_EQ(bits32("10110101011101011011010101110101"), bits); | |
| 576 EXPECT_EQ(32u, peeked_count); | |
| 577 | |
| 578 // |bits| is full, and doesn't change. | |
| 579 EXPECT_FALSE(input_stream.PeekBits(&peeked_count, &bits)); | |
| 580 EXPECT_EQ(bits32("10110101011101011011010101110101"), bits); | |
| 581 EXPECT_EQ(32u, peeked_count); | |
| 582 | |
| 583 input_stream.ConsumeBits(27); | |
| 584 bits = bits << 27; | |
| 585 peeked_count -= 27; | |
| 586 EXPECT_EQ(bits32("10101000000000000000000000000000"), bits); | |
| 587 EXPECT_EQ(5u, peeked_count); | |
| 588 | |
| 589 // Read remaining 3 bits of 0xd. | |
| 590 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | |
| 591 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits); | |
| 592 EXPECT_EQ(8u, peeked_count); | |
| 593 | |
| 594 // EOF. | |
| 595 EXPECT_FALSE(input_stream.PeekBits(&peeked_count, &bits)); | |
| 596 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits); | |
| 597 EXPECT_EQ(8u, peeked_count); | |
| 598 | |
| 599 input_stream.ConsumeBits(8); | |
| 600 EXPECT_FALSE(input_stream.HasMoreData()); | |
| 601 } | |
| 602 | |
| 603 TEST(HpackInputStreamTest, InitializePeekBits) { | |
| 604 { | |
| 605 // Empty input, peeked_count == 0 and bits == 0. | |
| 606 HpackInputStream input_stream(""); | |
| 607 auto peeked_count_and_bits = input_stream.InitializePeekBits(); | |
| 608 size_t peeked_count = peeked_count_and_bits.first; | |
| 609 uint32_t bits = peeked_count_and_bits.second; | |
| 610 EXPECT_EQ(0u, peeked_count); | |
| 611 EXPECT_EQ(0u, bits); | |
| 612 } | |
| 613 { | |
| 614 // One input byte, returns peeked_count == 8 and bits | |
| 615 // has the input byte in its high order bits. | |
| 616 HpackInputStream input_stream("\xfe"); | |
| 617 auto peeked_count_and_bits = input_stream.InitializePeekBits(); | |
| 618 size_t peeked_count = peeked_count_and_bits.first; | |
| 619 uint32_t bits = peeked_count_and_bits.second; | |
| 620 EXPECT_EQ(8u, peeked_count); | |
| 621 EXPECT_EQ(0xfe000000, bits); | |
| 622 input_stream.ConsumeBits(8); | |
| 623 EXPECT_FALSE(input_stream.HasMoreData()); | |
| 624 } | |
| 625 { | |
| 626 // Two input bytes, returns peeked_count == 16 and bits | |
| 627 // has the two input bytes in its high order bits. | |
| 628 HpackInputStream input_stream("\xfe\xdc"); | |
| 629 auto peeked_count_and_bits = input_stream.InitializePeekBits(); | |
| 630 size_t peeked_count = peeked_count_and_bits.first; | |
| 631 uint32_t bits = peeked_count_and_bits.second; | |
| 632 EXPECT_EQ(16u, peeked_count); | |
| 633 EXPECT_EQ(0xfedc0000, bits); | |
| 634 input_stream.ConsumeBits(16); | |
| 635 EXPECT_FALSE(input_stream.HasMoreData()); | |
| 636 } | |
| 637 { | |
| 638 // Three input bytes, returns peeked_count == 24 and bits | |
| 639 // has the three input bytes in its high order bits. | |
| 640 HpackInputStream input_stream("\xab\xcd\xef"); | |
| 641 auto peeked_count_and_bits = input_stream.InitializePeekBits(); | |
| 642 size_t peeked_count = peeked_count_and_bits.first; | |
| 643 uint32_t bits = peeked_count_and_bits.second; | |
| 644 EXPECT_EQ(24u, peeked_count); | |
| 645 EXPECT_EQ(0xabcdef00, bits); | |
| 646 input_stream.ConsumeBits(24); | |
| 647 EXPECT_FALSE(input_stream.HasMoreData()); | |
| 648 } | |
| 649 { | |
| 650 // Four input bytes, returns peeked_count == 32 and bits | |
| 651 // contains the four input bytes. | |
| 652 HpackInputStream input_stream("\xfe\xed\xdc\xcb"); | |
| 653 auto peeked_count_and_bits = input_stream.InitializePeekBits(); | |
| 654 size_t peeked_count = peeked_count_and_bits.first; | |
| 655 uint32_t bits = peeked_count_and_bits.second; | |
| 656 EXPECT_EQ(32u, peeked_count); | |
| 657 EXPECT_EQ(0xfeeddccb, bits); | |
| 658 input_stream.ConsumeBits(32); | |
| 659 EXPECT_FALSE(input_stream.HasMoreData()); | |
| 660 } | |
| 661 { | |
| 662 // Five input bytes, returns peeked_count == 32 and bits | |
| 663 // contains the first four input bytes. | |
| 664 HpackInputStream input_stream("\xfe\xed\xdc\xcb\xba"); | |
| 665 auto peeked_count_and_bits = input_stream.InitializePeekBits(); | |
| 666 size_t peeked_count = peeked_count_and_bits.first; | |
| 667 uint32_t bits = peeked_count_and_bits.second; | |
| 668 EXPECT_EQ(32u, peeked_count); | |
| 669 EXPECT_EQ(0xfeeddccb, bits); | |
| 670 EXPECT_TRUE(input_stream.HasMoreData()); | |
| 671 | |
| 672 // If we consume some bits, then InitializePeekBits will return no bits. | |
| 673 input_stream.ConsumeBits(28); | |
| 674 peeked_count -= 28; | |
| 675 bits <<= 28; | |
| 676 EXPECT_EQ(0xb0000000, bits); | |
| 677 | |
| 678 EXPECT_SPDY_BUG(peeked_count_and_bits = input_stream.InitializePeekBits(), | |
| 679 "bit_offset_"); | |
| 680 EXPECT_EQ(0u, peeked_count_and_bits.first); | |
| 681 EXPECT_EQ(0u, peeked_count_and_bits.second); | |
| 682 EXPECT_TRUE(input_stream.HasMoreData()); | |
| 683 | |
| 684 // Can PeekBits, which will get us the last byte's bits. | |
| 685 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | |
| 686 EXPECT_EQ(12u, peeked_count); | |
| 687 EXPECT_EQ(0xbba00000, bits); | |
| 688 input_stream.ConsumeBits(12); | |
| 689 EXPECT_FALSE(input_stream.HasMoreData()); | |
| 690 } | |
| 691 } | |
| 692 | |
| 693 TEST(HpackInputStreamTest, ConsumeByteRemainder) { | |
| 694 HpackInputStream input_stream("\xad\xab"); | |
| 695 // Does nothing. | |
| 696 input_stream.ConsumeByteRemainder(); | |
| 697 | |
| 698 // Consumes one byte. | |
| 699 input_stream.ConsumeBits(3); | |
| 700 input_stream.ConsumeByteRemainder(); | |
| 701 EXPECT_TRUE(input_stream.HasMoreData()); | |
| 702 | |
| 703 input_stream.ConsumeBits(6); | |
| 704 EXPECT_TRUE(input_stream.HasMoreData()); | |
| 705 input_stream.ConsumeByteRemainder(); | |
| 706 EXPECT_FALSE(input_stream.HasMoreData()); | |
| 707 } | |
| 708 | |
| 709 TEST(HpackInputStreamTest, IncompleteHeaderMatchPrefixAndConsume) { | |
| 710 HpackInputStream input_stream(""); | |
| 711 HpackInputStreamPeer input_stream_peer(&input_stream); | |
| 712 EXPECT_FALSE(input_stream.MatchPrefixAndConsume(kIndexedOpcode)); | |
| 713 EXPECT_EQ(0u, input_stream_peer.ParsedBytesCurrent()); | |
| 714 EXPECT_TRUE(input_stream.NeedMoreData()); | |
| 715 } | |
| 716 | |
| 717 TEST(HpackInputStreamTest, IncompleteHeaderDecodeNextUint32) { | |
| 718 // First byte only | |
| 719 HpackInputStream input_stream1("\xff"); | |
| 720 HpackInputStreamPeer input_stream1_peer(&input_stream1); | |
| 721 EXPECT_TRUE(input_stream1.MatchPrefixAndConsume(kIndexedOpcode)); | |
| 722 uint32_t result; | |
| 723 EXPECT_FALSE(input_stream1.DecodeNextUint32(&result)); | |
| 724 EXPECT_TRUE(input_stream1.NeedMoreData()); | |
| 725 EXPECT_EQ(1u, input_stream1_peer.ParsedBytesCurrent()); | |
| 726 | |
| 727 // No last byte | |
| 728 HpackInputStream input_stream2("\xff\x80\x80\x80"); | |
| 729 HpackInputStreamPeer input_stream2_peer(&input_stream2); | |
| 730 EXPECT_TRUE(input_stream2.MatchPrefixAndConsume(kIndexedOpcode)); | |
| 731 EXPECT_FALSE(input_stream2.DecodeNextUint32(&result)); | |
| 732 EXPECT_TRUE(input_stream2.NeedMoreData()); | |
| 733 EXPECT_EQ(4u, input_stream2_peer.ParsedBytesCurrent()); | |
| 734 | |
| 735 // Error happens before finishing parsing. | |
| 736 HpackInputStream input_stream3("\xff\xff\xff\xff\xff\xff\xff"); | |
| 737 HpackInputStreamPeer input_stream3_peer(&input_stream3); | |
| 738 EXPECT_TRUE(input_stream3.MatchPrefixAndConsume(kIndexedOpcode)); | |
| 739 EXPECT_FALSE(input_stream3.DecodeNextUint32(&result)); | |
| 740 EXPECT_FALSE(input_stream3.NeedMoreData()); | |
| 741 EXPECT_EQ(6u, input_stream3_peer.ParsedBytesCurrent()); | |
| 742 } | |
| 743 | |
| 744 TEST(HpackInputStreamTest, IncompleteHeaderDecodeNextIdentityString) { | |
| 745 HpackInputStream input_stream1("\x0estring litera"); | |
| 746 HpackInputStreamPeer input_stream1_peer(&input_stream1); | |
| 747 SpdyStringPiece string_piece; | |
| 748 EXPECT_FALSE(input_stream1.DecodeNextIdentityString(&string_piece)); | |
| 749 // Only parsed first byte. | |
| 750 EXPECT_EQ(1u, input_stream1_peer.ParsedBytesCurrent()); | |
| 751 EXPECT_TRUE(input_stream1.NeedMoreData()); | |
| 752 | |
| 753 HpackInputStream input_stream2("\x0e"); | |
| 754 HpackInputStreamPeer input_stream2_peer(&input_stream2); | |
| 755 EXPECT_FALSE(input_stream2.DecodeNextIdentityString(&string_piece)); | |
| 756 // Only parsed first byte. | |
| 757 EXPECT_EQ(1u, input_stream2_peer.ParsedBytesCurrent()); | |
| 758 EXPECT_TRUE(input_stream2.NeedMoreData()); | |
| 759 } | |
| 760 | |
| 761 TEST(HpackInputStreamTest, IncompleteHeaderDecodeNextHuffmanString) { | |
| 762 SpdyString output, input(a2b_hex(kEncodedHuffmanFixture)); | |
| 763 input.resize(input.size() - 1); // Remove last byte. | |
| 764 HpackInputStream input_stream1(input); | |
| 765 HpackInputStreamPeer input_stream1_peer(&input_stream1); | |
| 766 EXPECT_FALSE(input_stream1.DecodeNextHuffmanString(&output)); | |
| 767 EXPECT_EQ(1u, input_stream1_peer.ParsedBytesCurrent()); | |
| 768 EXPECT_TRUE(input_stream1.NeedMoreData()); | |
| 769 | |
| 770 input.erase(1, input.size()); // Remove all bytes except the first one. | |
| 771 HpackInputStream input_stream2(input); | |
| 772 HpackInputStreamPeer input_stream2_peer(&input_stream2); | |
| 773 EXPECT_FALSE(input_stream2.DecodeNextHuffmanString(&output)); | |
| 774 EXPECT_EQ(1u, input_stream2_peer.ParsedBytesCurrent()); | |
| 775 EXPECT_TRUE(input_stream2.NeedMoreData()); | |
| 776 } | |
| 777 | |
| 778 } // namespace test | |
| 779 | |
| 780 } // namespace net | |
| OLD | NEW |