| 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_huffman_table.h" | 5 #include "net/spdy/hpack_huffman_table.h" |
| 6 | 6 |
| 7 #include <bitset> | 7 #include <bitset> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 char input_storage[] = {2, 3, 2, 7, 4}; | 253 char input_storage[] = {2, 3, 2, 7, 4}; |
| 254 StringPiece input(input_storage, arraysize(input_storage)); | 254 StringPiece input(input_storage, arraysize(input_storage)); |
| 255 // By symbol: (2) 00 (3) 010 (2) 00 (7) 10010 (4) 10000 (6 as pad) 1001100. | 255 // By symbol: (2) 00 (3) 010 (2) 00 (7) 10010 (4) 10000 (6 as pad) 1001100. |
| 256 char expect_storage[] = { | 256 char expect_storage[] = { |
| 257 bits8("00010001"), | 257 bits8("00010001"), |
| 258 bits8("00101000"), | 258 bits8("00101000"), |
| 259 bits8("01001100")}; | 259 bits8("01001100")}; |
| 260 StringPiece expect(expect_storage, arraysize(expect_storage)); | 260 StringPiece expect(expect_storage, arraysize(expect_storage)); |
| 261 | 261 |
| 262 string buffer_in, buffer_out; | 262 string buffer_in, buffer_out; |
| 263 HpackOutputStream output_stream(kuint32max); | 263 HpackOutputStream output_stream; |
| 264 table.EncodeString(input, &output_stream); | 264 table.EncodeString(input, &output_stream); |
| 265 output_stream.TakeString(&buffer_in); | 265 output_stream.TakeString(&buffer_in); |
| 266 EXPECT_EQ(buffer_in, expect); | 266 EXPECT_EQ(buffer_in, expect); |
| 267 | 267 |
| 268 HpackInputStream input_stream(kuint32max, buffer_in); | 268 HpackInputStream input_stream(kuint32max, buffer_in); |
| 269 EXPECT_TRUE(table.DecodeString(&input_stream, input.size(), &buffer_out)); | 269 EXPECT_TRUE(table.DecodeString(&input_stream, input.size(), &buffer_out)); |
| 270 EXPECT_EQ(buffer_out, input); | 270 EXPECT_EQ(buffer_out, input); |
| 271 } | 271 } |
| 272 | 272 |
| 273 TEST(HpackHuffmanTableTest, ValidateMultiLevelDecodeTables) { | 273 TEST(HpackHuffmanTableTest, ValidateMultiLevelDecodeTables) { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 "\x4e\xb0\x8b\x74\x97\x90\xfa\x7f", | 383 "\x4e\xb0\x8b\x74\x97\x90\xfa\x7f", |
| 384 "custom-key", | 384 "custom-key", |
| 385 "\x4e\xb0\x8b\x74\x97\x9a\x17\xa8\xff", | 385 "\x4e\xb0\x8b\x74\x97\x9a\x17\xa8\xff", |
| 386 "custom-value", | 386 "custom-value", |
| 387 }; | 387 }; |
| 388 // Round-trip each test example. | 388 // Round-trip each test example. |
| 389 for (size_t i = 0; i != arraysize(test_table); i += 2) { | 389 for (size_t i = 0; i != arraysize(test_table); i += 2) { |
| 390 const string& encoded(test_table[i]); | 390 const string& encoded(test_table[i]); |
| 391 const string& decoded(test_table[i+1]); | 391 const string& decoded(test_table[i+1]); |
| 392 HpackInputStream input_stream(kuint32max, encoded); | 392 HpackInputStream input_stream(kuint32max, encoded); |
| 393 HpackOutputStream output_stream(kuint32max); | 393 HpackOutputStream output_stream; |
| 394 | 394 |
| 395 buffer.reserve(decoded.size()); | 395 buffer.reserve(decoded.size()); |
| 396 EXPECT_TRUE(table.DecodeString(&input_stream, decoded.size(), &buffer)); | 396 EXPECT_TRUE(table.DecodeString(&input_stream, decoded.size(), &buffer)); |
| 397 EXPECT_EQ(decoded, buffer); | 397 EXPECT_EQ(decoded, buffer); |
| 398 table.EncodeString(decoded, &output_stream); | 398 table.EncodeString(decoded, &output_stream); |
| 399 output_stream.TakeString(&buffer); | 399 output_stream.TakeString(&buffer); |
| 400 EXPECT_EQ(encoded, buffer); | 400 EXPECT_EQ(encoded, buffer); |
| 401 } | 401 } |
| 402 } | 402 } |
| 403 | 403 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 419 "\xfb\x7e\xbb\xbe\x9f\x5f\x87\xe3\x7f\xef\xed\xfa\xee\xfa\x7c\x3f" | 419 "\xfb\x7e\xbb\xbe\x9f\x5f\x87\xe3\x7f\xef\xed\xfa\xee\xfa\x7c\x3f" |
| 420 "\x1d\x5d\x1a\x23\xce\x54\x64\x36\xcd\x49\x4b\xd5\xd1\xcc\x5f\x05" | 420 "\x1d\x5d\x1a\x23\xce\x54\x64\x36\xcd\x49\x4b\xd5\xd1\xcc\x5f\x05" |
| 421 "\x35\x96\x9b", | 421 "\x35\x96\x9b", |
| 422 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", | 422 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", |
| 423 }; | 423 }; |
| 424 // Round-trip each test example. | 424 // Round-trip each test example. |
| 425 for (size_t i = 0; i != arraysize(test_table); i += 2) { | 425 for (size_t i = 0; i != arraysize(test_table); i += 2) { |
| 426 const string& encoded(test_table[i]); | 426 const string& encoded(test_table[i]); |
| 427 const string& decoded(test_table[i+1]); | 427 const string& decoded(test_table[i+1]); |
| 428 HpackInputStream input_stream(kuint32max, encoded); | 428 HpackInputStream input_stream(kuint32max, encoded); |
| 429 HpackOutputStream output_stream(kuint32max); | 429 HpackOutputStream output_stream; |
| 430 | 430 |
| 431 buffer.reserve(decoded.size()); | 431 buffer.reserve(decoded.size()); |
| 432 EXPECT_TRUE(table.DecodeString(&input_stream, decoded.size(), &buffer)); | 432 EXPECT_TRUE(table.DecodeString(&input_stream, decoded.size(), &buffer)); |
| 433 EXPECT_EQ(decoded, buffer); | 433 EXPECT_EQ(decoded, buffer); |
| 434 table.EncodeString(decoded, &output_stream); | 434 table.EncodeString(decoded, &output_stream); |
| 435 output_stream.TakeString(&buffer); | 435 output_stream.TakeString(&buffer); |
| 436 EXPECT_EQ(encoded, buffer); | 436 EXPECT_EQ(encoded, buffer); |
| 437 } | 437 } |
| 438 } | 438 } |
| 439 | 439 |
| 440 TEST(HpackHuffmanTableTest, RoundTripIndvidualSymbols) { | 440 TEST(HpackHuffmanTableTest, RoundTripIndvidualSymbols) { |
| 441 const HpackHuffmanTable& table(ObtainHpackHuffmanTable()); | 441 const HpackHuffmanTable& table(ObtainHpackHuffmanTable()); |
| 442 | 442 |
| 443 for (size_t i = 0; i != 256; i++) { | 443 for (size_t i = 0; i != 256; i++) { |
| 444 char c = static_cast<char>(i); | 444 char c = static_cast<char>(i); |
| 445 char storage[3] = {c, c, c}; | 445 char storage[3] = {c, c, c}; |
| 446 StringPiece input(storage, arraysize(storage)); | 446 StringPiece input(storage, arraysize(storage)); |
| 447 | 447 |
| 448 string buffer_in, buffer_out(input.size(), '\0'); | 448 string buffer_in, buffer_out(input.size(), '\0'); |
| 449 HpackOutputStream output_stream(kuint32max); | 449 HpackOutputStream output_stream; |
| 450 table.EncodeString(input, &output_stream); | 450 table.EncodeString(input, &output_stream); |
| 451 output_stream.TakeString(&buffer_in); | 451 output_stream.TakeString(&buffer_in); |
| 452 | 452 |
| 453 HpackInputStream input_stream(kuint32max, buffer_in); | 453 HpackInputStream input_stream(kuint32max, buffer_in); |
| 454 EXPECT_TRUE(table.DecodeString(&input_stream, input.size(), &buffer_out)); | 454 EXPECT_TRUE(table.DecodeString(&input_stream, input.size(), &buffer_out)); |
| 455 EXPECT_EQ(input, buffer_out); | 455 EXPECT_EQ(input, buffer_out); |
| 456 } | 456 } |
| 457 } | 457 } |
| 458 | 458 |
| 459 TEST(HpackHuffmanTableTest, RoundTripSymbolSequence) { | 459 TEST(HpackHuffmanTableTest, RoundTripSymbolSequence) { |
| 460 const HpackHuffmanTable& table(ObtainHpackHuffmanTable()); | 460 const HpackHuffmanTable& table(ObtainHpackHuffmanTable()); |
| 461 | 461 |
| 462 char storage[512]; | 462 char storage[512]; |
| 463 for (size_t i = 0; i != 256; i++) { | 463 for (size_t i = 0; i != 256; i++) { |
| 464 storage[i] = static_cast<char>(i); | 464 storage[i] = static_cast<char>(i); |
| 465 storage[511 - i] = static_cast<char>(i); | 465 storage[511 - i] = static_cast<char>(i); |
| 466 } | 466 } |
| 467 StringPiece input(storage, arraysize(storage)); | 467 StringPiece input(storage, arraysize(storage)); |
| 468 | 468 |
| 469 string buffer_in, buffer_out(input.size(), '\0'); | 469 string buffer_in, buffer_out(input.size(), '\0'); |
| 470 HpackOutputStream output_stream(kuint32max); | 470 HpackOutputStream output_stream; |
| 471 table.EncodeString(input, &output_stream); | 471 table.EncodeString(input, &output_stream); |
| 472 output_stream.TakeString(&buffer_in); | 472 output_stream.TakeString(&buffer_in); |
| 473 | 473 |
| 474 HpackInputStream input_stream(kuint32max, buffer_in); | 474 HpackInputStream input_stream(kuint32max, buffer_in); |
| 475 EXPECT_TRUE(table.DecodeString(&input_stream, input.size(), &buffer_out)); | 475 EXPECT_TRUE(table.DecodeString(&input_stream, input.size(), &buffer_out)); |
| 476 EXPECT_EQ(input, buffer_out); | 476 EXPECT_EQ(input, buffer_out); |
| 477 } | 477 } |
| 478 | 478 |
| 479 } // namespace | 479 } // namespace |
| 480 | 480 |
| 481 } // namespace test | 481 } // namespace test |
| 482 | 482 |
| 483 } // namespace net | 483 } // namespace net |
| OLD | NEW |