| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/http2/hpack/decoder/hpack_varint_decoder.h" | 5 #include "net/http2/hpack/decoder/hpack_varint_decoder.h" |
| 6 | 6 |
| 7 // Tests of HpackVarintDecoder. | 7 // Tests of HpackVarintDecoder. |
| 8 | 8 |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| 11 #include <ios> | 11 #include <ios> |
| 12 #include <iterator> | 12 #include <iterator> |
| 13 #include <ostream> | 13 #include <ostream> |
| 14 #include <set> | 14 #include <set> |
| 15 #include <sstream> | 15 #include <sstream> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/bind.h" | |
| 19 #include "base/bind_helpers.h" | |
| 20 #include "base/logging.h" | 18 #include "base/logging.h" |
| 21 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/strings/string_piece.h" | 20 #include "base/strings/string_piece.h" |
| 23 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 24 #include "net/http2/hpack/tools/hpack_block_builder.h" | 22 #include "net/http2/hpack/tools/hpack_block_builder.h" |
| 25 #include "net/http2/tools/failure.h" | 23 #include "net/http2/tools/failure.h" |
| 26 #include "net/http2/tools/http2_random.h" | 24 #include "net/http2/tools/http2_random.h" |
| 27 #include "net/http2/tools/random_decoder_test.h" | 25 #include "net/http2/tools/random_decoder_test.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 27 |
| 30 using ::testing::AssertionFailure; | 28 using ::testing::AssertionFailure; |
| 31 using ::testing::AssertionSuccess; | 29 using ::testing::AssertionSuccess; |
| 32 using base::StringPiece; | 30 using base::StringPiece; |
| 33 using base::StringPrintf; | 31 using base::StringPrintf; |
| 34 using std::string; | 32 using std::string; |
| 35 | 33 |
| 36 namespace net { | 34 namespace net { |
| 37 namespace test { | 35 namespace test { |
| 38 namespace { | 36 namespace { |
| 39 | 37 |
| 40 class HpackVarintDecoderTest : public RandomDecoderTest { | 38 class HpackVarintDecoderTest : public RandomDecoderTest { |
| 41 public: | |
| 42 AssertionResult ValidatorForValueTooLarge(bool* validated, | |
| 43 uint32_t expected_offset, | |
| 44 const DecodeBuffer& db, | |
| 45 DecodeStatus status) { | |
| 46 *validated = true; | |
| 47 VERIFY_EQ(DecodeStatus::kDecodeError, status); | |
| 48 VERIFY_EQ(expected_offset, db.Offset()); | |
| 49 return AssertionSuccess(); | |
| 50 } | |
| 51 | 39 |
| 52 protected: | 40 protected: |
| 53 DecodeStatus StartDecoding(DecodeBuffer* b) override { | 41 DecodeStatus StartDecoding(DecodeBuffer* b) override { |
| 54 CHECK_LT(0u, b->Remaining()); | 42 CHECK_LT(0u, b->Remaining()); |
| 55 CHECK_NE(0, prefix_mask_); | 43 CHECK_NE(0, prefix_mask_); |
| 56 uint8_t prefix = b->DecodeUInt8(); | 44 uint8_t prefix = b->DecodeUInt8(); |
| 57 return decoder_.Start(prefix, prefix_mask_, b); | 45 return decoder_.Start(prefix, prefix_mask_, b); |
| 58 } | 46 } |
| 59 | 47 |
| 60 DecodeStatus ResumeDecoding(DecodeBuffer* b) override { | 48 DecodeStatus ResumeDecoding(DecodeBuffer* b) override { |
| 61 return decoder_.Resume(b); | 49 return decoder_.Resume(b); |
| 62 } | 50 } |
| 63 | 51 |
| 64 AssertionResult ValidatorForDecodeSeveralWays(uint32_t expected_value, | |
| 65 const DecodeBuffer& db, | |
| 66 DecodeStatus status) { | |
| 67 if (decoder_.value() != expected_value) { | |
| 68 return AssertionFailure() | |
| 69 << "Value doesn't match expected: " << decoder_.value() | |
| 70 << " != " << expected_value; | |
| 71 } | |
| 72 return AssertionSuccess(); | |
| 73 } | |
| 74 | |
| 75 void DecodeSeveralWays(uint32_t expected_value, uint32_t expected_offset) { | 52 void DecodeSeveralWays(uint32_t expected_value, uint32_t expected_offset) { |
| 76 // The validator is called after each of the several times that the input | 53 // The validator is called after each of the several times that the input |
| 77 // DecodeBuffer is decoded, each with a different segmentation of the input. | 54 // DecodeBuffer is decoded, each with a different segmentation of the input. |
| 78 // Validate that decoder_.value() matches the expected value. | 55 // Validate that decoder_.value() matches the expected value. |
| 79 Validator validator = | 56 Validator validator = [expected_value, this]( |
| 80 base::Bind(&HpackVarintDecoderTest::ValidatorForDecodeSeveralWays, | 57 const DecodeBuffer& db, DecodeStatus status) -> AssertionResult { |
| 81 base::Unretained(this), expected_value); | 58 if (decoder_.value() != expected_value) { |
| 59 return AssertionFailure() |
| 60 << "Value doesn't match expected: " << decoder_.value() |
| 61 << " != " << expected_value; |
| 62 } |
| 63 return AssertionSuccess(); |
| 64 }; |
| 82 | 65 |
| 83 // First validate that decoding is done and that we've advanced the cursor | 66 // First validate that decoding is done and that we've advanced the cursor |
| 84 // the expected amount. | 67 // the expected amount. |
| 85 validator = ValidateDoneAndOffset(expected_offset, validator); | 68 validator = ValidateDoneAndOffset(expected_offset, validator); |
| 86 | 69 |
| 87 // StartDecoding, above, requires the DecodeBuffer be non-empty so that it | 70 // StartDecoding, above, requires the DecodeBuffer be non-empty so that it |
| 88 // can call Start with the prefix byte. | 71 // can call Start with the prefix byte. |
| 89 bool return_non_zero_on_first = true; | 72 bool return_non_zero_on_first = true; |
| 90 | 73 |
| 91 DecodeBuffer b(buffer_); | 74 DecodeBuffer b(buffer_); |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 uint64_t too_large = HpackVarintDecoder::HiValueOfExtensionBytes( | 351 uint64_t too_large = HpackVarintDecoder::HiValueOfExtensionBytes( |
| 369 HpackVarintDecoder::MaxExtensionBytes() + 3, prefix_length_); | 352 HpackVarintDecoder::MaxExtensionBytes() + 3, prefix_length_); |
| 370 HpackBlockBuilder bb; | 353 HpackBlockBuilder bb; |
| 371 bb.AppendHighBitsAndVarint(0, prefix_length_, too_large); | 354 bb.AppendHighBitsAndVarint(0, prefix_length_, too_large); |
| 372 buffer_ = bb.buffer(); | 355 buffer_ = bb.buffer(); |
| 373 | 356 |
| 374 // The validator is called after each of the several times that the input | 357 // The validator is called after each of the several times that the input |
| 375 // DecodeBuffer is decoded, each with a different segmentation of the input. | 358 // DecodeBuffer is decoded, each with a different segmentation of the input. |
| 376 // Validate that decoder_.value() matches the expected value. | 359 // Validate that decoder_.value() matches the expected value. |
| 377 bool validated = false; | 360 bool validated = false; |
| 378 Validator validator = | 361 Validator validator = [&validated, expected_offset]( |
| 379 base::Bind(&HpackVarintDecoderTest::ValidatorForValueTooLarge, | 362 const DecodeBuffer& db, DecodeStatus status) -> AssertionResult { |
| 380 base::Unretained(this), &validated, expected_offset); | 363 validated = true; |
| 364 VERIFY_EQ(DecodeStatus::kDecodeError, status); |
| 365 VERIFY_EQ(expected_offset, db.Offset()); |
| 366 return AssertionSuccess(); |
| 367 }; |
| 381 | 368 |
| 382 // StartDecoding, above, requires the DecodeBuffer be non-empty so that it | 369 // StartDecoding, above, requires the DecodeBuffer be non-empty so that it |
| 383 // can call Start with the prefix byte. | 370 // can call Start with the prefix byte. |
| 384 bool return_non_zero_on_first = true; | 371 bool return_non_zero_on_first = true; |
| 385 DecodeBuffer b(buffer_); | 372 DecodeBuffer b(buffer_); |
| 386 EXPECT_TRUE( | 373 EXPECT_TRUE( |
| 387 DecodeAndValidateSeveralWays(&b, return_non_zero_on_first, validator)); | 374 DecodeAndValidateSeveralWays(&b, return_non_zero_on_first, validator)); |
| 388 EXPECT_EQ(expected_offset, b.Offset()); | 375 EXPECT_EQ(expected_offset, b.Offset()); |
| 389 EXPECT_TRUE(validated); | 376 EXPECT_TRUE(validated); |
| 390 } | 377 } |
| 391 } | 378 } |
| 392 | 379 |
| 393 } // namespace | 380 } // namespace |
| 394 } // namespace test | 381 } // namespace test |
| 395 } // namespace net | 382 } // namespace net |
| OLD | NEW |