| 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_huffman_table.h" | 5 #include "net/spdy/hpack/hpack_huffman_table.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <bitset> | 9 #include <bitset> |
| 10 #include <limits> | |
| 11 #include <string> | 10 #include <string> |
| 12 #include <utility> | 11 #include <utility> |
| 13 | 12 |
| 14 #include "base/logging.h" | 13 #include "base/logging.h" |
| 15 #include "base/macros.h" | 14 #include "base/macros.h" |
| 16 #include "net/spdy/hpack/hpack_constants.h" | 15 #include "net/spdy/hpack/hpack_constants.h" |
| 17 #include "net/spdy/hpack/hpack_huffman_decoder.h" | 16 #include "net/spdy/hpack/hpack_huffman_decoder.h" |
| 18 #include "net/spdy/hpack/hpack_input_stream.h" | 17 #include "net/spdy/hpack/hpack_input_stream.h" |
| 19 #include "net/spdy/hpack/hpack_output_stream.h" | 18 #include "net/spdy/hpack/hpack_output_stream.h" |
| 20 #include "net/spdy/spdy_test_utils.h" | 19 #include "net/spdy/spdy_test_utils.h" |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 StringPiece input(input_storage, arraysize(input_storage)); | 232 StringPiece input(input_storage, arraysize(input_storage)); |
| 234 // By symbol: (2) 00 (3) 010 (2) 00 (7) 10010 (4) 10000 (6 as pad) 1001100. | 233 // By symbol: (2) 00 (3) 010 (2) 00 (7) 10010 (4) 10000 (6 as pad) 1001100. |
| 235 char expect_storage[] = {bits8("00010001"), bits8("00101000"), | 234 char expect_storage[] = {bits8("00010001"), bits8("00101000"), |
| 236 bits8("01001100")}; | 235 bits8("01001100")}; |
| 237 StringPiece expect(expect_storage, arraysize(expect_storage)); | 236 StringPiece expect(expect_storage, arraysize(expect_storage)); |
| 238 | 237 |
| 239 string buffer_in = EncodeString(input); | 238 string buffer_in = EncodeString(input); |
| 240 EXPECT_EQ(expect, buffer_in); | 239 EXPECT_EQ(expect, buffer_in); |
| 241 | 240 |
| 242 string buffer_out; | 241 string buffer_out; |
| 243 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), | 242 HpackInputStream input_stream(buffer_in); |
| 244 buffer_in); | |
| 245 EXPECT_TRUE( | 243 EXPECT_TRUE( |
| 246 table_.GenericDecodeString(&input_stream, input.size(), &buffer_out)); | 244 table_.GenericDecodeString(&input_stream, input.size(), &buffer_out)); |
| 247 EXPECT_EQ(buffer_out, input); | 245 EXPECT_EQ(buffer_out, input); |
| 248 } | 246 } |
| 249 | 247 |
| 250 TEST_F(GenericHuffmanTableTest, ValidateMultiLevelDecodeTables) { | 248 TEST_F(GenericHuffmanTableTest, ValidateMultiLevelDecodeTables) { |
| 251 HpackHuffmanSymbol code[] = { | 249 HpackHuffmanSymbol code[] = { |
| 252 {bits32("00000000000000000000000000000000"), 6, 0}, | 250 {bits32("00000000000000000000000000000000"), 6, 0}, |
| 253 {bits32("00000100000000000000000000000000"), 6, 1}, | 251 {bits32("00000100000000000000000000000000"), 6, 1}, |
| 254 {bits32("00001000000000000000000000000000"), 11, 2}, | 252 {bits32("00001000000000000000000000000000"), 11, 2}, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 {bits32("10011100000000000000000000000000"), 16, 8}}; | 298 {bits32("10011100000000000000000000000000"), 16, 8}}; |
| 301 EXPECT_TRUE(table_.Initialize(code, arraysize(code))); | 299 EXPECT_TRUE(table_.Initialize(code, arraysize(code))); |
| 302 | 300 |
| 303 string buffer; | 301 string buffer; |
| 304 const size_t capacity = 4; | 302 const size_t capacity = 4; |
| 305 { | 303 { |
| 306 // This example works: (2) 00 (3) 010 (2) 00 (6) 100110 (pad) 100. | 304 // This example works: (2) 00 (3) 010 (2) 00 (6) 100110 (pad) 100. |
| 307 char input_storage[] = {bits8("00010001"), bits8("00110100")}; | 305 char input_storage[] = {bits8("00010001"), bits8("00110100")}; |
| 308 StringPiece input(input_storage, arraysize(input_storage)); | 306 StringPiece input(input_storage, arraysize(input_storage)); |
| 309 | 307 |
| 310 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), input); | 308 HpackInputStream input_stream(input); |
| 311 EXPECT_TRUE(table_.GenericDecodeString(&input_stream, capacity, &buffer)); | 309 EXPECT_TRUE(table_.GenericDecodeString(&input_stream, capacity, &buffer)); |
| 312 EXPECT_EQ(buffer, "\x02\x03\x02\x06"); | 310 EXPECT_EQ(buffer, "\x02\x03\x02\x06"); |
| 313 } | 311 } |
| 314 { | 312 { |
| 315 // Expect to fail on an invalid code prefix. | 313 // Expect to fail on an invalid code prefix. |
| 316 // (2) 00 (3) 010 (2) 00 (too-large) 101000 (pad) 100. | 314 // (2) 00 (3) 010 (2) 00 (too-large) 101000 (pad) 100. |
| 317 char input_storage[] = {bits8("00010001"), bits8("01000111")}; | 315 char input_storage[] = {bits8("00010001"), bits8("01000111")}; |
| 318 StringPiece input(input_storage, arraysize(input_storage)); | 316 StringPiece input(input_storage, arraysize(input_storage)); |
| 319 | 317 |
| 320 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), input); | 318 HpackInputStream input_stream(input); |
| 321 EXPECT_FALSE(table_.GenericDecodeString(&input_stream, capacity, &buffer)); | 319 EXPECT_FALSE(table_.GenericDecodeString(&input_stream, capacity, &buffer)); |
| 322 EXPECT_EQ(buffer, "\x02\x03\x02"); | 320 EXPECT_EQ(buffer, "\x02\x03\x02"); |
| 323 } | 321 } |
| 324 { | 322 { |
| 325 // Repeat the shortest 0b00 code to overflow |buffer|. Expect to fail. | 323 // Repeat the shortest 0b00 code to overflow |buffer|. Expect to fail. |
| 326 std::vector<char> input_storage(1 + capacity / 4, '\0'); | 324 std::vector<char> input_storage(1 + capacity / 4, '\0'); |
| 327 StringPiece input(&input_storage[0], input_storage.size()); | 325 StringPiece input(&input_storage[0], input_storage.size()); |
| 328 | 326 |
| 329 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), input); | 327 HpackInputStream input_stream(input); |
| 330 EXPECT_FALSE(table_.GenericDecodeString(&input_stream, capacity, &buffer)); | 328 EXPECT_FALSE(table_.GenericDecodeString(&input_stream, capacity, &buffer)); |
| 331 | 329 |
| 332 std::vector<char> expected(capacity, '\x02'); | 330 std::vector<char> expected(capacity, '\x02'); |
| 333 EXPECT_THAT(buffer, ElementsAreArray(expected)); | 331 EXPECT_THAT(buffer, ElementsAreArray(expected)); |
| 334 EXPECT_EQ(capacity, buffer.size()); | 332 EXPECT_EQ(capacity, buffer.size()); |
| 335 } | 333 } |
| 336 { | 334 { |
| 337 // Expect to fail if more than a byte of unconsumed input remains. | 335 // Expect to fail if more than a byte of unconsumed input remains. |
| 338 // (6) 100110 (8 truncated) 1001110000 | 336 // (6) 100110 (8 truncated) 1001110000 |
| 339 char input_storage[] = {bits8("10011010"), bits8("01110000")}; | 337 char input_storage[] = {bits8("10011010"), bits8("01110000")}; |
| 340 StringPiece input(input_storage, arraysize(input_storage)); | 338 StringPiece input(input_storage, arraysize(input_storage)); |
| 341 | 339 |
| 342 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), input); | 340 HpackInputStream input_stream(input); |
| 343 EXPECT_FALSE(table_.GenericDecodeString(&input_stream, capacity, &buffer)); | 341 EXPECT_FALSE(table_.GenericDecodeString(&input_stream, capacity, &buffer)); |
| 344 EXPECT_EQ(buffer, "\x06"); | 342 EXPECT_EQ(buffer, "\x06"); |
| 345 } | 343 } |
| 346 } | 344 } |
| 347 | 345 |
| 348 // Tests of the ability to decode the HPACK Huffman Code, defined in: | 346 // Tests of the ability to decode the HPACK Huffman Code, defined in: |
| 349 // https://httpwg.github.io/specs/rfc7541.html#huffman.code | 347 // https://httpwg.github.io/specs/rfc7541.html#huffman.code |
| 350 class HpackHuffmanTableTest : public GenericHuffmanTableTest { | 348 class HpackHuffmanTableTest : public GenericHuffmanTableTest { |
| 351 protected: | 349 protected: |
| 352 void SetUp() override { | 350 void SetUp() override { |
| 353 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); | 351 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); |
| 354 EXPECT_TRUE(table_.Initialize(&code[0], code.size())); | 352 EXPECT_TRUE(table_.Initialize(&code[0], code.size())); |
| 355 EXPECT_TRUE(table_.IsInitialized()); | 353 EXPECT_TRUE(table_.IsInitialized()); |
| 356 } | 354 } |
| 357 | 355 |
| 358 void DecodeStringTwice(const string& encoded, | 356 void DecodeStringTwice(const string& encoded, |
| 359 size_t out_capacity, | 357 size_t out_capacity, |
| 360 string* out) { | 358 string* out) { |
| 361 // First decode with HpackHuffmanTable. | 359 // First decode with HpackHuffmanTable. |
| 362 { | 360 { |
| 363 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), | 361 HpackInputStream input_stream(encoded); |
| 364 encoded); | |
| 365 EXPECT_TRUE(table_.GenericDecodeString(&input_stream, out_capacity, out)); | 362 EXPECT_TRUE(table_.GenericDecodeString(&input_stream, out_capacity, out)); |
| 366 } | 363 } |
| 367 // And decode again with the fixed decoder, confirming that the result is | 364 // And decode again with the fixed decoder, confirming that the result is |
| 368 // the same. | 365 // the same. |
| 369 { | 366 { |
| 370 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), | 367 HpackInputStream input_stream(encoded); |
| 371 encoded); | |
| 372 string buf; | 368 string buf; |
| 373 EXPECT_TRUE( | 369 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &buf)); |
| 374 HpackHuffmanDecoder::DecodeString(&input_stream, out_capacity, &buf)); | |
| 375 EXPECT_EQ(*out, buf); | 370 EXPECT_EQ(*out, buf); |
| 376 } | 371 } |
| 377 } | 372 } |
| 378 }; | 373 }; |
| 379 | 374 |
| 380 TEST_F(HpackHuffmanTableTest, InitializeHpackCode) { | 375 TEST_F(HpackHuffmanTableTest, InitializeHpackCode) { |
| 381 EXPECT_EQ(peer_.pad_bits(), '\xFF'); // First 8 bits of EOS. | 376 EXPECT_EQ(peer_.pad_bits(), '\xFF'); // First 8 bits of EOS. |
| 382 } | 377 } |
| 383 | 378 |
| 384 TEST_F(HpackHuffmanTableTest, SpecRequestExamples) { | 379 TEST_F(HpackHuffmanTableTest, SpecRequestExamples) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 output_stream.TakeString(&encoding); | 473 output_stream.TakeString(&encoding); |
| 479 EXPECT_EQ(encoding.size(), table_.EncodedSize(test_table[i])); | 474 EXPECT_EQ(encoding.size(), table_.EncodedSize(test_table[i])); |
| 480 } | 475 } |
| 481 } | 476 } |
| 482 | 477 |
| 483 } // namespace | 478 } // namespace |
| 484 | 479 |
| 485 } // namespace test | 480 } // namespace test |
| 486 | 481 |
| 487 } // namespace net | 482 } // namespace net |
| OLD | NEW |