Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(504)

Side by Side Diff: net/spdy/hpack/hpack_huffman_table_test.cc

Issue 1492403002: Remove kuint32max. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: http security header file Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/http/http_security_headers.cc ('k') | sandbox/win/src/crosscall_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
8
7 #include <bitset> 9 #include <bitset>
10 #include <limits>
8 #include <string> 11 #include <string>
9 12
10 #include "base/logging.h" 13 #include "base/logging.h"
11 #include "net/spdy/hpack/hpack_constants.h" 14 #include "net/spdy/hpack/hpack_constants.h"
12 #include "net/spdy/hpack/hpack_input_stream.h" 15 #include "net/spdy/hpack/hpack_input_stream.h"
13 #include "net/spdy/hpack/hpack_output_stream.h" 16 #include "net/spdy/hpack/hpack_output_stream.h"
14 #include "net/spdy/spdy_test_utils.h" 17 #include "net/spdy/spdy_test_utils.h"
15 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
17 20
18 using base::StringPiece; 21 using base::StringPiece;
19 using std::string; 22 using std::string;
20 using testing::ElementsAreArray; 23 using testing::ElementsAreArray;
21 using testing::Pointwise; 24 using testing::Pointwise;
22 25
23 namespace net { 26 namespace net {
24 27
25 namespace test { 28 namespace test {
26 29
27 typedef HpackHuffmanTable::DecodeEntry DecodeEntry; 30 typedef HpackHuffmanTable::DecodeEntry DecodeEntry;
28 typedef HpackHuffmanTable::DecodeTable DecodeTable; 31 typedef HpackHuffmanTable::DecodeTable DecodeTable;
29 32
30 class HpackHuffmanTablePeer { 33 class HpackHuffmanTablePeer {
31 public: 34 public:
32 explicit HpackHuffmanTablePeer(const HpackHuffmanTable& table) 35 explicit HpackHuffmanTablePeer(const HpackHuffmanTable& table)
33 : table_(table) {} 36 : table_(table) {}
34 37
35 const std::vector<uint32>& code_by_id() const { return table_.code_by_id_; } 38 const std::vector<uint32_t>& code_by_id() const { return table_.code_by_id_; }
36 const std::vector<uint8>& length_by_id() const { 39 const std::vector<uint8_t>& length_by_id() const {
37 return table_.length_by_id_; 40 return table_.length_by_id_;
38 } 41 }
39 const std::vector<DecodeTable>& decode_tables() const { 42 const std::vector<DecodeTable>& decode_tables() const {
40 return table_.decode_tables_; 43 return table_.decode_tables_;
41 } 44 }
42 char pad_bits() const { 45 char pad_bits() const {
43 // Cast to match signed-ness of bits8(). 46 // Cast to match signed-ness of bits8().
44 return static_cast<char>(table_.pad_bits_); 47 return static_cast<char>(table_.pad_bits_);
45 } 48 }
46 uint16 failed_symbol_id() const { return table_.failed_symbol_id_; } 49 uint16_t failed_symbol_id() const { return table_.failed_symbol_id_; }
47 std::vector<DecodeEntry> decode_entries(const DecodeTable& decode_table) { 50 std::vector<DecodeEntry> decode_entries(const DecodeTable& decode_table) {
48 std::vector<DecodeEntry>::const_iterator begin = 51 std::vector<DecodeEntry>::const_iterator begin =
49 table_.decode_entries_.begin() + decode_table.entries_offset; 52 table_.decode_entries_.begin() + decode_table.entries_offset;
50 return std::vector<DecodeEntry>(begin, begin + decode_table.size()); 53 return std::vector<DecodeEntry>(begin, begin + decode_table.size());
51 } 54 }
52 55
53 private: 56 private:
54 const HpackHuffmanTable& table_; 57 const HpackHuffmanTable& table_;
55 }; 58 };
56 59
(...skipping 18 matching lines...) Expand all
75 HpackHuffmanTablePeer peer_; 78 HpackHuffmanTablePeer peer_;
76 }; 79 };
77 80
78 MATCHER(DecodeEntryEq, "") { 81 MATCHER(DecodeEntryEq, "") {
79 const DecodeEntry& lhs = std::tr1::get<0>(arg); 82 const DecodeEntry& lhs = std::tr1::get<0>(arg);
80 const DecodeEntry& rhs = std::tr1::get<1>(arg); 83 const DecodeEntry& rhs = std::tr1::get<1>(arg);
81 return lhs.next_table_index == rhs.next_table_index && 84 return lhs.next_table_index == rhs.next_table_index &&
82 lhs.length == rhs.length && lhs.symbol_id == rhs.symbol_id; 85 lhs.length == rhs.length && lhs.symbol_id == rhs.symbol_id;
83 } 86 }
84 87
85 uint32 bits32(const string& bitstring) { 88 uint32_t bits32(const string& bitstring) {
86 return std::bitset<32>(bitstring).to_ulong(); 89 return std::bitset<32>(bitstring).to_ulong();
87 } 90 }
88 char bits8(const string& bitstring) { 91 char bits8(const string& bitstring) {
89 return static_cast<char>(std::bitset<8>(bitstring).to_ulong()); 92 return static_cast<char>(std::bitset<8>(bitstring).to_ulong());
90 } 93 }
91 94
92 TEST_F(HpackHuffmanTableTest, InitializeHpackCode) { 95 TEST_F(HpackHuffmanTableTest, InitializeHpackCode) {
93 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); 96 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode();
94 EXPECT_TRUE(table_.Initialize(&code[0], code.size())); 97 EXPECT_TRUE(table_.Initialize(&code[0], code.size()));
95 EXPECT_TRUE(table_.IsInitialized()); 98 EXPECT_TRUE(table_.IsInitialized());
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 StringPiece input(input_storage, arraysize(input_storage)); 235 StringPiece input(input_storage, arraysize(input_storage));
233 // By symbol: (2) 00 (3) 010 (2) 00 (7) 10010 (4) 10000 (6 as pad) 1001100. 236 // By symbol: (2) 00 (3) 010 (2) 00 (7) 10010 (4) 10000 (6 as pad) 1001100.
234 char expect_storage[] = {bits8("00010001"), bits8("00101000"), 237 char expect_storage[] = {bits8("00010001"), bits8("00101000"),
235 bits8("01001100")}; 238 bits8("01001100")};
236 StringPiece expect(expect_storage, arraysize(expect_storage)); 239 StringPiece expect(expect_storage, arraysize(expect_storage));
237 240
238 string buffer_in = EncodeString(input); 241 string buffer_in = EncodeString(input);
239 EXPECT_EQ(expect, buffer_in); 242 EXPECT_EQ(expect, buffer_in);
240 243
241 string buffer_out; 244 string buffer_out;
242 HpackInputStream input_stream(kuint32max, buffer_in); 245 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(),
246 buffer_in);
243 EXPECT_TRUE(table_.DecodeString(&input_stream, input.size(), &buffer_out)); 247 EXPECT_TRUE(table_.DecodeString(&input_stream, input.size(), &buffer_out));
244 EXPECT_EQ(buffer_out, input); 248 EXPECT_EQ(buffer_out, input);
245 } 249 }
246 250
247 TEST_F(HpackHuffmanTableTest, ValidateMultiLevelDecodeTables) { 251 TEST_F(HpackHuffmanTableTest, ValidateMultiLevelDecodeTables) {
248 HpackHuffmanSymbol code[] = { 252 HpackHuffmanSymbol code[] = {
249 {bits32("00000000000000000000000000000000"), 6, 0}, 253 {bits32("00000000000000000000000000000000"), 6, 0},
250 {bits32("00000100000000000000000000000000"), 6, 1}, 254 {bits32("00000100000000000000000000000000"), 6, 1},
251 {bits32("00001000000000000000000000000000"), 11, 2}, 255 {bits32("00001000000000000000000000000000"), 11, 2},
252 {bits32("00001000001000000000000000000000"), 11, 3}, 256 {bits32("00001000001000000000000000000000"), 11, 3},
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 {bits32("10011100000000000000000000000000"), 16, 8}}; 301 {bits32("10011100000000000000000000000000"), 16, 8}};
298 EXPECT_TRUE(table_.Initialize(code, arraysize(code))); 302 EXPECT_TRUE(table_.Initialize(code, arraysize(code)));
299 303
300 string buffer; 304 string buffer;
301 const size_t capacity = 4; 305 const size_t capacity = 4;
302 { 306 {
303 // This example works: (2) 00 (3) 010 (2) 00 (6) 100110 (pad) 100. 307 // This example works: (2) 00 (3) 010 (2) 00 (6) 100110 (pad) 100.
304 char input_storage[] = {bits8("00010001"), bits8("00110100")}; 308 char input_storage[] = {bits8("00010001"), bits8("00110100")};
305 StringPiece input(input_storage, arraysize(input_storage)); 309 StringPiece input(input_storage, arraysize(input_storage));
306 310
307 HpackInputStream input_stream(kuint32max, input); 311 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), input);
308 EXPECT_TRUE(table_.DecodeString(&input_stream, capacity, &buffer)); 312 EXPECT_TRUE(table_.DecodeString(&input_stream, capacity, &buffer));
309 EXPECT_EQ(buffer, "\x02\x03\x02\x06"); 313 EXPECT_EQ(buffer, "\x02\x03\x02\x06");
310 } 314 }
311 { 315 {
312 // Expect to fail on an invalid code prefix. 316 // Expect to fail on an invalid code prefix.
313 // (2) 00 (3) 010 (2) 00 (too-large) 101000 (pad) 100. 317 // (2) 00 (3) 010 (2) 00 (too-large) 101000 (pad) 100.
314 char input_storage[] = {bits8("00010001"), bits8("01000111")}; 318 char input_storage[] = {bits8("00010001"), bits8("01000111")};
315 StringPiece input(input_storage, arraysize(input_storage)); 319 StringPiece input(input_storage, arraysize(input_storage));
316 320
317 HpackInputStream input_stream(kuint32max, input); 321 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), input);
318 EXPECT_FALSE(table_.DecodeString(&input_stream, capacity, &buffer)); 322 EXPECT_FALSE(table_.DecodeString(&input_stream, capacity, &buffer));
319 EXPECT_EQ(buffer, "\x02\x03\x02"); 323 EXPECT_EQ(buffer, "\x02\x03\x02");
320 } 324 }
321 { 325 {
322 // Repeat the shortest 0b00 code to overflow |buffer|. Expect to fail. 326 // Repeat the shortest 0b00 code to overflow |buffer|. Expect to fail.
323 std::vector<char> input_storage(1 + capacity / 4, '\0'); 327 std::vector<char> input_storage(1 + capacity / 4, '\0');
324 StringPiece input(&input_storage[0], input_storage.size()); 328 StringPiece input(&input_storage[0], input_storage.size());
325 329
326 HpackInputStream input_stream(kuint32max, input); 330 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), input);
327 EXPECT_FALSE(table_.DecodeString(&input_stream, capacity, &buffer)); 331 EXPECT_FALSE(table_.DecodeString(&input_stream, capacity, &buffer));
328 332
329 std::vector<char> expected(capacity, '\x02'); 333 std::vector<char> expected(capacity, '\x02');
330 EXPECT_THAT(buffer, ElementsAreArray(expected)); 334 EXPECT_THAT(buffer, ElementsAreArray(expected));
331 EXPECT_EQ(capacity, buffer.size()); 335 EXPECT_EQ(capacity, buffer.size());
332 } 336 }
333 { 337 {
334 // Expect to fail if more than a byte of unconsumed input remains. 338 // Expect to fail if more than a byte of unconsumed input remains.
335 // (6) 100110 (8 truncated) 1001110000 339 // (6) 100110 (8 truncated) 1001110000
336 char input_storage[] = {bits8("10011010"), bits8("01110000")}; 340 char input_storage[] = {bits8("10011010"), bits8("01110000")};
337 StringPiece input(input_storage, arraysize(input_storage)); 341 StringPiece input(input_storage, arraysize(input_storage));
338 342
339 HpackInputStream input_stream(kuint32max, input); 343 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), input);
340 EXPECT_FALSE(table_.DecodeString(&input_stream, capacity, &buffer)); 344 EXPECT_FALSE(table_.DecodeString(&input_stream, capacity, &buffer));
341 EXPECT_EQ(buffer, "\x06"); 345 EXPECT_EQ(buffer, "\x06");
342 } 346 }
343 } 347 }
344 348
345 TEST_F(HpackHuffmanTableTest, SpecRequestExamples) { 349 TEST_F(HpackHuffmanTableTest, SpecRequestExamples) {
346 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); 350 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode();
347 EXPECT_TRUE(table_.Initialize(&code[0], code.size())); 351 EXPECT_TRUE(table_.Initialize(&code[0], code.size()));
348 352
349 string buffer; 353 string buffer;
350 string test_table[] = { 354 string test_table[] = {
351 a2b_hex("f1e3c2e5f23a6ba0ab90f4ff"), 355 a2b_hex("f1e3c2e5f23a6ba0ab90f4ff"),
352 "www.example.com", 356 "www.example.com",
353 a2b_hex("a8eb10649cbf"), 357 a2b_hex("a8eb10649cbf"),
354 "no-cache", 358 "no-cache",
355 a2b_hex("25a849e95ba97d7f"), 359 a2b_hex("25a849e95ba97d7f"),
356 "custom-key", 360 "custom-key",
357 a2b_hex("25a849e95bb8e8b4bf"), 361 a2b_hex("25a849e95bb8e8b4bf"),
358 "custom-value", 362 "custom-value",
359 }; 363 };
360 // Round-trip each test example. 364 // Round-trip each test example.
361 for (size_t i = 0; i != arraysize(test_table); i += 2) { 365 for (size_t i = 0; i != arraysize(test_table); i += 2) {
362 const string& encodedFixture(test_table[i]); 366 const string& encodedFixture(test_table[i]);
363 const string& decodedFixture(test_table[i + 1]); 367 const string& decodedFixture(test_table[i + 1]);
364 HpackInputStream input_stream(kuint32max, encodedFixture); 368 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(),
369 encodedFixture);
365 370
366 EXPECT_TRUE( 371 EXPECT_TRUE(
367 table_.DecodeString(&input_stream, decodedFixture.size(), &buffer)); 372 table_.DecodeString(&input_stream, decodedFixture.size(), &buffer));
368 EXPECT_EQ(decodedFixture, buffer); 373 EXPECT_EQ(decodedFixture, buffer);
369 buffer = EncodeString(decodedFixture); 374 buffer = EncodeString(decodedFixture);
370 EXPECT_EQ(encodedFixture, buffer); 375 EXPECT_EQ(encodedFixture, buffer);
371 } 376 }
372 } 377 }
373 378
374 TEST_F(HpackHuffmanTableTest, SpecResponseExamples) { 379 TEST_F(HpackHuffmanTableTest, SpecResponseExamples) {
(...skipping 10 matching lines...) Expand all
385 "d3"), 390 "d3"),
386 "https://www.example.com", a2b_hex("94e7821dd7f2e6c7b335dfdfcd5b3960" 391 "https://www.example.com", a2b_hex("94e7821dd7f2e6c7b335dfdfcd5b3960"
387 "d5af27087f3672c1ab270fb5291f9587" 392 "d5af27087f3672c1ab270fb5291f9587"
388 "316065c003ed4ee5b1063d5007"), 393 "316065c003ed4ee5b1063d5007"),
389 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", 394 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1",
390 }; 395 };
391 // Round-trip each test example. 396 // Round-trip each test example.
392 for (size_t i = 0; i != arraysize(test_table); i += 2) { 397 for (size_t i = 0; i != arraysize(test_table); i += 2) {
393 const string& encodedFixture(test_table[i]); 398 const string& encodedFixture(test_table[i]);
394 const string& decodedFixture(test_table[i + 1]); 399 const string& decodedFixture(test_table[i + 1]);
395 HpackInputStream input_stream(kuint32max, encodedFixture); 400 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(),
401 encodedFixture);
396 402
397 EXPECT_TRUE( 403 EXPECT_TRUE(
398 table_.DecodeString(&input_stream, decodedFixture.size(), &buffer)); 404 table_.DecodeString(&input_stream, decodedFixture.size(), &buffer));
399 EXPECT_EQ(decodedFixture, buffer); 405 EXPECT_EQ(decodedFixture, buffer);
400 buffer = EncodeString(decodedFixture); 406 buffer = EncodeString(decodedFixture);
401 EXPECT_EQ(encodedFixture, buffer); 407 EXPECT_EQ(encodedFixture, buffer);
402 } 408 }
403 } 409 }
404 410
405 TEST_F(HpackHuffmanTableTest, RoundTripIndvidualSymbols) { 411 TEST_F(HpackHuffmanTableTest, RoundTripIndvidualSymbols) {
406 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); 412 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode();
407 EXPECT_TRUE(table_.Initialize(&code[0], code.size())); 413 EXPECT_TRUE(table_.Initialize(&code[0], code.size()));
408 414
409 for (size_t i = 0; i != 256; i++) { 415 for (size_t i = 0; i != 256; i++) {
410 char c = static_cast<char>(i); 416 char c = static_cast<char>(i);
411 char storage[3] = {c, c, c}; 417 char storage[3] = {c, c, c};
412 StringPiece input(storage, arraysize(storage)); 418 StringPiece input(storage, arraysize(storage));
413 419
414 string buffer_in = EncodeString(input); 420 string buffer_in = EncodeString(input);
415 string buffer_out; 421 string buffer_out;
416 422
417 HpackInputStream input_stream(kuint32max, buffer_in); 423 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(),
424 buffer_in);
418 EXPECT_TRUE(table_.DecodeString(&input_stream, input.size(), &buffer_out)); 425 EXPECT_TRUE(table_.DecodeString(&input_stream, input.size(), &buffer_out));
419 EXPECT_EQ(input, buffer_out); 426 EXPECT_EQ(input, buffer_out);
420 } 427 }
421 } 428 }
422 429
423 TEST_F(HpackHuffmanTableTest, RoundTripSymbolSequence) { 430 TEST_F(HpackHuffmanTableTest, RoundTripSymbolSequence) {
424 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); 431 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode();
425 EXPECT_TRUE(table_.Initialize(&code[0], code.size())); 432 EXPECT_TRUE(table_.Initialize(&code[0], code.size()));
426 433
427 char storage[512]; 434 char storage[512];
428 for (size_t i = 0; i != 256; i++) { 435 for (size_t i = 0; i != 256; i++) {
429 storage[i] = static_cast<char>(i); 436 storage[i] = static_cast<char>(i);
430 storage[511 - i] = static_cast<char>(i); 437 storage[511 - i] = static_cast<char>(i);
431 } 438 }
432 StringPiece input(storage, arraysize(storage)); 439 StringPiece input(storage, arraysize(storage));
433 440
434 string buffer_in = EncodeString(input); 441 string buffer_in = EncodeString(input);
435 string buffer_out; 442 string buffer_out;
436 443
437 HpackInputStream input_stream(kuint32max, buffer_in); 444 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(),
445 buffer_in);
438 EXPECT_TRUE(table_.DecodeString(&input_stream, input.size(), &buffer_out)); 446 EXPECT_TRUE(table_.DecodeString(&input_stream, input.size(), &buffer_out));
439 EXPECT_EQ(input, buffer_out); 447 EXPECT_EQ(input, buffer_out);
440 } 448 }
441 449
442 TEST_F(HpackHuffmanTableTest, EncodedSizeAgreesWithEncodeString) { 450 TEST_F(HpackHuffmanTableTest, EncodedSizeAgreesWithEncodeString) {
443 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode(); 451 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode();
444 EXPECT_TRUE(table_.Initialize(&code[0], code.size())); 452 EXPECT_TRUE(table_.Initialize(&code[0], code.size()));
445 453
446 string test_table[] = { 454 string test_table[] = {
447 "", 455 "",
(...skipping 16 matching lines...) Expand all
464 output_stream.TakeString(&encoding); 472 output_stream.TakeString(&encoding);
465 EXPECT_EQ(encoding.size(), table_.EncodedSize(test_table[i])); 473 EXPECT_EQ(encoding.size(), table_.EncodedSize(test_table[i]));
466 } 474 }
467 } 475 }
468 476
469 } // namespace 477 } // namespace
470 478
471 } // namespace test 479 } // namespace test
472 480
473 } // namespace net 481 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_security_headers.cc ('k') | sandbox/win/src/crosscall_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698