QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/qpack/qpack_instruction_decoder.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | |
bnc | 3b58cfc | 2019-11-27 12:52:39 -0800 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/core/qpack/qpack_instructions.h" |
vasilvv | 0fb4443 | 2019-03-13 22:47:36 -0700 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
bnc | e42f7ad | 2019-10-25 17:46:31 -0700 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/test_tools/qpack/qpack_test_utils.h" |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
| 14 | #include "net/third_party/quiche/src/common/platform/api/quiche_text_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | |
| 16 | using ::testing::_; |
| 17 | using ::testing::Eq; |
| 18 | using ::testing::Expectation; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 19 | using ::testing::Invoke; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 20 | using ::testing::Return; |
| 21 | using ::testing::StrictMock; |
| 22 | using ::testing::Values; |
| 23 | |
| 24 | namespace quic { |
| 25 | namespace test { |
| 26 | namespace { |
| 27 | |
| 28 | // This instruction has three fields: an S bit and two varints. |
| 29 | const QpackInstruction* TestInstruction1() { |
| 30 | static const QpackInstruction* const instruction = |
| 31 | new QpackInstruction{QpackInstructionOpcode{0x00, 0x80}, |
| 32 | {{QpackInstructionFieldType::kSbit, 0x40}, |
| 33 | {QpackInstructionFieldType::kVarint, 6}, |
| 34 | {QpackInstructionFieldType::kVarint2, 8}}}; |
| 35 | return instruction; |
| 36 | } |
| 37 | |
| 38 | // This instruction has two fields: a header name with a 6-bit prefix, and a |
| 39 | // header value with a 7-bit prefix, both preceded by a Huffman bit. |
| 40 | const QpackInstruction* TestInstruction2() { |
| 41 | static const QpackInstruction* const instruction = |
| 42 | new QpackInstruction{QpackInstructionOpcode{0x80, 0x80}, |
| 43 | {{QpackInstructionFieldType::kName, 6}, |
| 44 | {QpackInstructionFieldType::kValue, 7}}}; |
| 45 | return instruction; |
| 46 | } |
| 47 | |
| 48 | const QpackLanguage* TestLanguage() { |
| 49 | static const QpackLanguage* const language = |
| 50 | new QpackLanguage{TestInstruction1(), TestInstruction2()}; |
| 51 | return language; |
| 52 | } |
| 53 | |
| 54 | class MockDelegate : public QpackInstructionDecoder::Delegate { |
| 55 | public: |
| 56 | MockDelegate() { |
| 57 | ON_CALL(*this, OnInstructionDecoded(_)).WillByDefault(Return(true)); |
| 58 | } |
| 59 | |
| 60 | MockDelegate(const MockDelegate&) = delete; |
| 61 | MockDelegate& operator=(const MockDelegate&) = delete; |
| 62 | ~MockDelegate() override = default; |
| 63 | |
| 64 | MOCK_METHOD1(OnInstructionDecoded, bool(const QpackInstruction* instruction)); |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 65 | MOCK_METHOD1(OnError, void(quiche::QuicheStringPiece error_message)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | class QpackInstructionDecoderTest : public QuicTestWithParam<FragmentMode> { |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 69 | protected: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 70 | QpackInstructionDecoderTest() |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 71 | : decoder_(std::make_unique<QpackInstructionDecoder>(TestLanguage(), |
| 72 | &delegate_)), |
| 73 | fragment_mode_(GetParam()) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 74 | ~QpackInstructionDecoderTest() override = default; |
| 75 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 76 | void SetUp() override { |
| 77 | // Destroy QpackInstructionDecoder on error to test that it does not crash. |
| 78 | // See https://crbug.com/1025209. |
| 79 | ON_CALL(delegate_, OnError(_)) |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 80 | .WillByDefault( |
| 81 | Invoke([this](quiche::QuicheStringPiece /* error_message */) { |
| 82 | decoder_.reset(); |
| 83 | })); |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 84 | } |
| 85 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | // Decode one full instruction with fragment sizes dictated by |
| 87 | // |fragment_mode_|. |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 88 | // Assumes that |data| is a single complete instruction, and accordingly |
| 89 | // verifies that AtInstructionBoundary() returns true before and after the |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 90 | // instruction, and returns false while decoding is in progress. |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 91 | // Assumes that delegate methods destroy |decoder_| if they return false. |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 92 | void DecodeInstruction(quiche::QuicheStringPiece data) { |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 93 | EXPECT_TRUE(decoder_->AtInstructionBoundary()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 94 | |
| 95 | FragmentSizeGenerator fragment_size_generator = |
| 96 | FragmentModeToFragmentSizeGenerator(fragment_mode_); |
| 97 | |
| 98 | while (!data.empty()) { |
| 99 | size_t fragment_size = std::min(fragment_size_generator(), data.size()); |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 100 | bool success = decoder_->Decode(data.substr(0, fragment_size)); |
| 101 | if (!decoder_) { |
| 102 | EXPECT_FALSE(success); |
| 103 | return; |
| 104 | } |
| 105 | EXPECT_TRUE(success); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 106 | data = data.substr(fragment_size); |
| 107 | if (!data.empty()) { |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 108 | EXPECT_FALSE(decoder_->AtInstructionBoundary()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 112 | EXPECT_TRUE(decoder_->AtInstructionBoundary()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | StrictMock<MockDelegate> delegate_; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 116 | std::unique_ptr<QpackInstructionDecoder> decoder_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | const FragmentMode fragment_mode_; |
| 120 | }; |
| 121 | |
QUICHE team | 1bfe0d7 | 2019-09-23 04:50:47 -0700 | [diff] [blame] | 122 | INSTANTIATE_TEST_SUITE_P(All, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 123 | QpackInstructionDecoderTest, |
| 124 | Values(FragmentMode::kSingleChunk, |
| 125 | FragmentMode::kOctetByOctet)); |
| 126 | |
| 127 | TEST_P(QpackInstructionDecoderTest, SBitAndVarint2) { |
| 128 | EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction1())); |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 129 | DecodeInstruction(quiche::QuicheTextUtils::HexDecode("7f01ff65")); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 130 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 131 | EXPECT_TRUE(decoder_->s_bit()); |
| 132 | EXPECT_EQ(64u, decoder_->varint()); |
| 133 | EXPECT_EQ(356u, decoder_->varint2()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 134 | |
| 135 | EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction1())); |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 136 | DecodeInstruction(quiche::QuicheTextUtils::HexDecode("05c8")); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 137 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 138 | EXPECT_FALSE(decoder_->s_bit()); |
| 139 | EXPECT_EQ(5u, decoder_->varint()); |
| 140 | EXPECT_EQ(200u, decoder_->varint2()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | TEST_P(QpackInstructionDecoderTest, NameAndValue) { |
| 144 | EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction2())); |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 145 | DecodeInstruction(quiche::QuicheTextUtils::HexDecode("83666f6f03626172")); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 146 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 147 | EXPECT_EQ("foo", decoder_->name()); |
| 148 | EXPECT_EQ("bar", decoder_->value()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 149 | |
| 150 | EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction2())); |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 151 | DecodeInstruction(quiche::QuicheTextUtils::HexDecode("8000")); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 152 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 153 | EXPECT_EQ("", decoder_->name()); |
| 154 | EXPECT_EQ("", decoder_->value()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 155 | |
| 156 | EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction2())); |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 157 | DecodeInstruction(quiche::QuicheTextUtils::HexDecode("c294e7838c767f")); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 158 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 159 | EXPECT_EQ("foo", decoder_->name()); |
| 160 | EXPECT_EQ("bar", decoder_->value()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | TEST_P(QpackInstructionDecoderTest, InvalidHuffmanEncoding) { |
| 164 | EXPECT_CALL(delegate_, OnError(Eq("Error in Huffman-encoded string."))); |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 165 | DecodeInstruction(quiche::QuicheTextUtils::HexDecode("c1ff")); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TEST_P(QpackInstructionDecoderTest, InvalidVarintEncoding) { |
| 169 | EXPECT_CALL(delegate_, OnError(Eq("Encoded integer too large."))); |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 170 | DecodeInstruction( |
| 171 | quiche::QuicheTextUtils::HexDecode("ffffffffffffffffffffff")); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | TEST_P(QpackInstructionDecoderTest, DelegateSignalsError) { |
| 175 | // First instruction is valid. |
| 176 | Expectation first_call = |
| 177 | EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction1())) |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 178 | .WillOnce(Invoke( |
| 179 | [this](const QpackInstruction * /* instruction */) -> bool { |
| 180 | EXPECT_EQ(1u, decoder_->varint()); |
| 181 | return true; |
| 182 | })); |
| 183 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 184 | // Second instruction is invalid. Decoding must halt. |
| 185 | EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction1())) |
| 186 | .After(first_call) |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 187 | .WillOnce( |
| 188 | Invoke([this](const QpackInstruction * /* instruction */) -> bool { |
| 189 | EXPECT_EQ(2u, decoder_->varint()); |
| 190 | return false; |
| 191 | })); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 192 | |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 193 | EXPECT_FALSE(decoder_->Decode( |
| 194 | quiche::QuicheTextUtils::HexDecode("01000200030004000500"))); |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // QpackInstructionDecoder must not crash if it is destroyed from a |
| 198 | // Delegate::OnInstructionDecoded() call as long as it returns false. |
| 199 | TEST_P(QpackInstructionDecoderTest, DelegateSignalsErrorAndDestroysDecoder) { |
| 200 | EXPECT_CALL(delegate_, OnInstructionDecoded(TestInstruction1())) |
| 201 | .WillOnce( |
| 202 | Invoke([this](const QpackInstruction * /* instruction */) -> bool { |
| 203 | EXPECT_EQ(1u, decoder_->varint()); |
| 204 | decoder_.reset(); |
| 205 | return false; |
| 206 | })); |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 207 | DecodeInstruction(quiche::QuicheTextUtils::HexDecode("0100")); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | } // namespace |
| 211 | } // namespace test |
| 212 | } // namespace quic |