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