• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 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 "quiche/quic/core/http/http_decoder.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "absl/base/macros.h"
11 #include "absl/strings/escaping.h"
12 #include "absl/strings/str_cat.h"
13 #include "absl/strings/string_view.h"
14 #include "quiche/quic/core/http/http_encoder.h"
15 #include "quiche/quic/core/http/http_frames.h"
16 #include "quiche/quic/core/quic_data_writer.h"
17 #include "quiche/quic/core/quic_versions.h"
18 #include "quiche/quic/platform/api/quic_expect_bug.h"
19 #include "quiche/quic/platform/api/quic_flags.h"
20 #include "quiche/quic/platform/api/quic_test.h"
21 #include "quiche/quic/test_tools/quic_test_utils.h"
22 
23 using ::testing::_;
24 using ::testing::AnyNumber;
25 using ::testing::Eq;
26 using ::testing::InSequence;
27 using ::testing::Return;
28 
29 namespace quic {
30 namespace test {
31 
32 class HttpDecoderPeer {
33  public:
current_frame_type(HttpDecoder * decoder)34   static uint64_t current_frame_type(HttpDecoder* decoder) {
35     return decoder->current_frame_type_;
36   }
37 };
38 
39 namespace {
40 
41 class HttpDecoderTest : public QuicTest {
42  public:
HttpDecoderTest()43   HttpDecoderTest() : decoder_(&visitor_) {
44     ON_CALL(visitor_, OnMaxPushIdFrame()).WillByDefault(Return(true));
45     ON_CALL(visitor_, OnGoAwayFrame(_)).WillByDefault(Return(true));
46     ON_CALL(visitor_, OnSettingsFrameStart(_)).WillByDefault(Return(true));
47     ON_CALL(visitor_, OnSettingsFrame(_)).WillByDefault(Return(true));
48     ON_CALL(visitor_, OnDataFrameStart(_, _)).WillByDefault(Return(true));
49     ON_CALL(visitor_, OnDataFramePayload(_)).WillByDefault(Return(true));
50     ON_CALL(visitor_, OnDataFrameEnd()).WillByDefault(Return(true));
51     ON_CALL(visitor_, OnHeadersFrameStart(_, _)).WillByDefault(Return(true));
52     ON_CALL(visitor_, OnHeadersFramePayload(_)).WillByDefault(Return(true));
53     ON_CALL(visitor_, OnHeadersFrameEnd()).WillByDefault(Return(true));
54     ON_CALL(visitor_, OnPriorityUpdateFrameStart(_))
55         .WillByDefault(Return(true));
56     ON_CALL(visitor_, OnPriorityUpdateFrame(_)).WillByDefault(Return(true));
57     ON_CALL(visitor_, OnAcceptChFrameStart(_)).WillByDefault(Return(true));
58     ON_CALL(visitor_, OnAcceptChFrame(_)).WillByDefault(Return(true));
59     ON_CALL(visitor_, OnUnknownFrameStart(_, _, _)).WillByDefault(Return(true));
60     ON_CALL(visitor_, OnUnknownFramePayload(_)).WillByDefault(Return(true));
61     ON_CALL(visitor_, OnUnknownFrameEnd()).WillByDefault(Return(true));
62   }
63   ~HttpDecoderTest() override = default;
64 
current_frame_type()65   uint64_t current_frame_type() {
66     return HttpDecoderPeer::current_frame_type(&decoder_);
67   }
68 
69   // Process |input| in a single call to HttpDecoder::ProcessInput().
ProcessInput(absl::string_view input)70   QuicByteCount ProcessInput(absl::string_view input) {
71     return decoder_.ProcessInput(input.data(), input.size());
72   }
73 
74   // Feed |input| to |decoder_| one character at a time,
75   // verifying that each character gets processed.
ProcessInputCharByChar(absl::string_view input)76   void ProcessInputCharByChar(absl::string_view input) {
77     for (char c : input) {
78       EXPECT_EQ(1u, decoder_.ProcessInput(&c, 1));
79     }
80   }
81 
82   // Append garbage to |input|, then process it in a single call to
83   // HttpDecoder::ProcessInput().  Verify that garbage is not read.
ProcessInputWithGarbageAppended(absl::string_view input)84   QuicByteCount ProcessInputWithGarbageAppended(absl::string_view input) {
85     std::string input_with_garbage_appended = absl::StrCat(input, "blahblah");
86     QuicByteCount processed_bytes = ProcessInput(input_with_garbage_appended);
87 
88     // Guaranteed by HttpDecoder::ProcessInput() contract.
89     QUICHE_DCHECK_LE(processed_bytes, input_with_garbage_appended.size());
90 
91     // Caller should set up visitor to pause decoding
92     // before HttpDecoder would read garbage.
93     EXPECT_LE(processed_bytes, input.size());
94 
95     return processed_bytes;
96   }
97 
98   testing::StrictMock<MockHttpDecoderVisitor> visitor_;
99   HttpDecoder decoder_;
100 };
101 
TEST_F(HttpDecoderTest,InitialState)102 TEST_F(HttpDecoderTest, InitialState) {
103   EXPECT_THAT(decoder_.error(), IsQuicNoError());
104   EXPECT_EQ("", decoder_.error_detail());
105 }
106 
TEST_F(HttpDecoderTest,UnknownFrame)107 TEST_F(HttpDecoderTest, UnknownFrame) {
108   std::unique_ptr<char[]> input;
109 
110   const QuicByteCount payload_lengths[] = {0, 14, 100};
111   const uint64_t frame_types[] = {
112       0x21, 0x40, 0x5f, 0x7e, 0x9d,  // some reserved frame types
113       0x6f, 0x14                     // some unknown, not reserved frame types
114   };
115 
116   for (auto payload_length : payload_lengths) {
117     std::string data(payload_length, 'a');
118 
119     for (auto frame_type : frame_types) {
120       const QuicByteCount total_length =
121           QuicDataWriter::GetVarInt62Len(frame_type) +
122           QuicDataWriter::GetVarInt62Len(payload_length) + payload_length;
123       input = std::make_unique<char[]>(total_length);
124 
125       QuicDataWriter writer(total_length, input.get());
126       writer.WriteVarInt62(frame_type);
127       writer.WriteVarInt62(payload_length);
128       const QuicByteCount header_length = writer.length();
129       if (payload_length > 0) {
130         writer.WriteStringPiece(data);
131       }
132 
133       EXPECT_CALL(visitor_, OnUnknownFrameStart(frame_type, header_length,
134                                                 payload_length));
135       if (payload_length > 0) {
136         EXPECT_CALL(visitor_, OnUnknownFramePayload(Eq(data)));
137       }
138       EXPECT_CALL(visitor_, OnUnknownFrameEnd());
139 
140       EXPECT_EQ(total_length, decoder_.ProcessInput(input.get(), total_length));
141 
142       EXPECT_THAT(decoder_.error(), IsQuicNoError());
143       ASSERT_EQ("", decoder_.error_detail());
144       EXPECT_EQ(frame_type, current_frame_type());
145     }
146   }
147 }
148 
TEST_F(HttpDecoderTest,CancelPush)149 TEST_F(HttpDecoderTest, CancelPush) {
150   InSequence s;
151   std::string input = absl::HexStringToBytes(
152       "03"    // type (CANCEL_PUSH)
153       "01"    // length
154       "01");  // Push Id
155 
156   EXPECT_CALL(visitor_, OnError(&decoder_));
157   EXPECT_EQ(1u, ProcessInput(input));
158   EXPECT_THAT(decoder_.error(), IsError(QUIC_HTTP_FRAME_ERROR));
159   EXPECT_EQ("CANCEL_PUSH frame received.", decoder_.error_detail());
160 }
161 
TEST_F(HttpDecoderTest,PushPromiseFrame)162 TEST_F(HttpDecoderTest, PushPromiseFrame) {
163   InSequence s;
164   std::string input =
165       absl::StrCat(absl::HexStringToBytes("05"    // type (PUSH PROMISE)
166                                           "08"    // length
167                                           "1f"),  // push id 31
168                    "Headers");                    // headers
169 
170   EXPECT_CALL(visitor_, OnError(&decoder_));
171   EXPECT_EQ(1u, ProcessInput(input));
172   EXPECT_THAT(decoder_.error(), IsError(QUIC_HTTP_FRAME_ERROR));
173   EXPECT_EQ("PUSH_PROMISE frame received.", decoder_.error_detail());
174 }
175 
TEST_F(HttpDecoderTest,MaxPushId)176 TEST_F(HttpDecoderTest, MaxPushId) {
177   InSequence s;
178   std::string input = absl::HexStringToBytes(
179       "0D"    // type (MAX_PUSH_ID)
180       "01"    // length
181       "01");  // Push Id
182 
183   // Visitor pauses processing.
184   EXPECT_CALL(visitor_, OnMaxPushIdFrame()).WillOnce(Return(false));
185   EXPECT_EQ(input.size(), ProcessInputWithGarbageAppended(input));
186   EXPECT_THAT(decoder_.error(), IsQuicNoError());
187   EXPECT_EQ("", decoder_.error_detail());
188 
189   // Process the full frame.
190   EXPECT_CALL(visitor_, OnMaxPushIdFrame());
191   EXPECT_EQ(input.size(), ProcessInput(input));
192   EXPECT_THAT(decoder_.error(), IsQuicNoError());
193   EXPECT_EQ("", decoder_.error_detail());
194 
195   // Process the frame incrementally.
196   EXPECT_CALL(visitor_, OnMaxPushIdFrame());
197   ProcessInputCharByChar(input);
198   EXPECT_THAT(decoder_.error(), IsQuicNoError());
199   EXPECT_EQ("", decoder_.error_detail());
200 }
201 
TEST_F(HttpDecoderTest,SettingsFrame)202 TEST_F(HttpDecoderTest, SettingsFrame) {
203   InSequence s;
204   std::string input = absl::HexStringToBytes(
205       "04"    // type (SETTINGS)
206       "07"    // length
207       "01"    // identifier (SETTINGS_QPACK_MAX_TABLE_CAPACITY)
208       "02"    // content
209       "06"    // identifier (SETTINGS_MAX_HEADER_LIST_SIZE)
210       "05"    // content
211       "4100"  // identifier, encoded on 2 bytes (0x40), value is 256 (0x100)
212       "04");  // content
213 
214   SettingsFrame frame;
215   frame.values[1] = 2;
216   frame.values[6] = 5;
217   frame.values[256] = 4;
218 
219   // Visitor pauses processing.
220   absl::string_view remaining_input(input);
221   EXPECT_CALL(visitor_, OnSettingsFrameStart(2)).WillOnce(Return(false));
222   QuicByteCount processed_bytes =
223       ProcessInputWithGarbageAppended(remaining_input);
224   EXPECT_EQ(2u, processed_bytes);
225   remaining_input = remaining_input.substr(processed_bytes);
226 
227   EXPECT_CALL(visitor_, OnSettingsFrame(frame)).WillOnce(Return(false));
228   processed_bytes = ProcessInputWithGarbageAppended(remaining_input);
229   EXPECT_EQ(remaining_input.size(), processed_bytes);
230   EXPECT_THAT(decoder_.error(), IsQuicNoError());
231   EXPECT_EQ("", decoder_.error_detail());
232 
233   // Process the full frame.
234   EXPECT_CALL(visitor_, OnSettingsFrameStart(2));
235   EXPECT_CALL(visitor_, OnSettingsFrame(frame));
236   EXPECT_EQ(input.size(), ProcessInput(input));
237   EXPECT_THAT(decoder_.error(), IsQuicNoError());
238   EXPECT_EQ("", decoder_.error_detail());
239 
240   // Process the frame incrementally.
241   EXPECT_CALL(visitor_, OnSettingsFrameStart(2));
242   EXPECT_CALL(visitor_, OnSettingsFrame(frame));
243   ProcessInputCharByChar(input);
244   EXPECT_THAT(decoder_.error(), IsQuicNoError());
245   EXPECT_EQ("", decoder_.error_detail());
246 }
247 
TEST_F(HttpDecoderTest,CorruptSettingsFrame)248 TEST_F(HttpDecoderTest, CorruptSettingsFrame) {
249   const char* const kPayload =
250       "\x42\x11"                           // two-byte id
251       "\x80\x22\x33\x44"                   // four-byte value
252       "\x58\x39"                           // two-byte id
253       "\xf0\x22\x33\x44\x55\x66\x77\x88";  // eight-byte value
254   struct {
255     size_t payload_length;
256     const char* const error_message;
257   } kTestData[] = {
258       {1, "Unable to read setting identifier."},
259       {5, "Unable to read setting value."},
260       {7, "Unable to read setting identifier."},
261       {12, "Unable to read setting value."},
262   };
263 
264   for (const auto& test_data : kTestData) {
265     std::string input;
266     input.push_back(4u);  // type SETTINGS
267     input.push_back(test_data.payload_length);
268     const size_t header_length = input.size();
269     input.append(kPayload, test_data.payload_length);
270 
271     HttpDecoder decoder(&visitor_);
272     EXPECT_CALL(visitor_, OnSettingsFrameStart(header_length));
273     EXPECT_CALL(visitor_, OnError(&decoder));
274 
275     QuicByteCount processed_bytes =
276         decoder.ProcessInput(input.data(), input.size());
277     EXPECT_EQ(input.size(), processed_bytes);
278     EXPECT_THAT(decoder.error(), IsError(QUIC_HTTP_FRAME_ERROR));
279     EXPECT_EQ(test_data.error_message, decoder.error_detail());
280   }
281 }
282 
TEST_F(HttpDecoderTest,DuplicateSettingsIdentifier)283 TEST_F(HttpDecoderTest, DuplicateSettingsIdentifier) {
284   std::string input = absl::HexStringToBytes(
285       "04"    // type (SETTINGS)
286       "04"    // length
287       "01"    // identifier
288       "01"    // content
289       "01"    // identifier
290       "02");  // content
291 
292   EXPECT_CALL(visitor_, OnSettingsFrameStart(2));
293   EXPECT_CALL(visitor_, OnError(&decoder_));
294 
295   EXPECT_EQ(input.size(), ProcessInput(input));
296 
297   EXPECT_THAT(decoder_.error(),
298               IsError(QUIC_HTTP_DUPLICATE_SETTING_IDENTIFIER));
299   EXPECT_EQ("Duplicate setting identifier.", decoder_.error_detail());
300 }
301 
TEST_F(HttpDecoderTest,DataFrame)302 TEST_F(HttpDecoderTest, DataFrame) {
303   InSequence s;
304   std::string input = absl::StrCat(absl::HexStringToBytes("00"    // type (DATA)
305                                                           "05"),  // length
306                                    "Data!");                      // data
307 
308   // Visitor pauses processing.
309   EXPECT_CALL(visitor_, OnDataFrameStart(2, 5)).WillOnce(Return(false));
310   absl::string_view remaining_input(input);
311   QuicByteCount processed_bytes =
312       ProcessInputWithGarbageAppended(remaining_input);
313   EXPECT_EQ(2u, processed_bytes);
314   remaining_input = remaining_input.substr(processed_bytes);
315 
316   EXPECT_CALL(visitor_, OnDataFramePayload(absl::string_view("Data!")))
317       .WillOnce(Return(false));
318   processed_bytes = ProcessInputWithGarbageAppended(remaining_input);
319   EXPECT_EQ(remaining_input.size(), processed_bytes);
320 
321   EXPECT_CALL(visitor_, OnDataFrameEnd()).WillOnce(Return(false));
322   EXPECT_EQ(0u, ProcessInputWithGarbageAppended(""));
323   EXPECT_THAT(decoder_.error(), IsQuicNoError());
324   EXPECT_EQ("", decoder_.error_detail());
325 
326   // Process the full frame.
327   EXPECT_CALL(visitor_, OnDataFrameStart(2, 5));
328   EXPECT_CALL(visitor_, OnDataFramePayload(absl::string_view("Data!")));
329   EXPECT_CALL(visitor_, OnDataFrameEnd());
330   EXPECT_EQ(input.size(), ProcessInput(input));
331   EXPECT_THAT(decoder_.error(), IsQuicNoError());
332   EXPECT_EQ("", decoder_.error_detail());
333 
334   // Process the frame incrementally.
335   EXPECT_CALL(visitor_, OnDataFrameStart(2, 5));
336   EXPECT_CALL(visitor_, OnDataFramePayload(absl::string_view("D")));
337   EXPECT_CALL(visitor_, OnDataFramePayload(absl::string_view("a")));
338   EXPECT_CALL(visitor_, OnDataFramePayload(absl::string_view("t")));
339   EXPECT_CALL(visitor_, OnDataFramePayload(absl::string_view("a")));
340   EXPECT_CALL(visitor_, OnDataFramePayload(absl::string_view("!")));
341   EXPECT_CALL(visitor_, OnDataFrameEnd());
342   ProcessInputCharByChar(input);
343   EXPECT_THAT(decoder_.error(), IsQuicNoError());
344   EXPECT_EQ("", decoder_.error_detail());
345 }
346 
TEST_F(HttpDecoderTest,FrameHeaderPartialDelivery)347 TEST_F(HttpDecoderTest, FrameHeaderPartialDelivery) {
348   InSequence s;
349   // A large input that will occupy more than 1 byte in the length field.
350   std::string input(2048, 'x');
351   quiche::QuicheBuffer header = HttpEncoder::SerializeDataFrameHeader(
352       input.length(), quiche::SimpleBufferAllocator::Get());
353   // Partially send only 1 byte of the header to process.
354   EXPECT_EQ(1u, decoder_.ProcessInput(header.data(), 1));
355   EXPECT_THAT(decoder_.error(), IsQuicNoError());
356   EXPECT_EQ("", decoder_.error_detail());
357 
358   // Send the rest of the header.
359   EXPECT_CALL(visitor_, OnDataFrameStart(3, input.length()));
360   EXPECT_EQ(header.size() - 1,
361             decoder_.ProcessInput(header.data() + 1, header.size() - 1));
362   EXPECT_THAT(decoder_.error(), IsQuicNoError());
363   EXPECT_EQ("", decoder_.error_detail());
364 
365   // Send data.
366   EXPECT_CALL(visitor_, OnDataFramePayload(absl::string_view(input)));
367   EXPECT_CALL(visitor_, OnDataFrameEnd());
368   EXPECT_EQ(2048u, decoder_.ProcessInput(input.data(), 2048));
369   EXPECT_THAT(decoder_.error(), IsQuicNoError());
370   EXPECT_EQ("", decoder_.error_detail());
371 }
372 
TEST_F(HttpDecoderTest,PartialDeliveryOfLargeFrameType)373 TEST_F(HttpDecoderTest, PartialDeliveryOfLargeFrameType) {
374   // Use a reserved type that takes four bytes as a varint.
375   const uint64_t frame_type = 0x1f * 0x222 + 0x21;
376   const QuicByteCount payload_length = 0;
377   const QuicByteCount header_length =
378       QuicDataWriter::GetVarInt62Len(frame_type) +
379       QuicDataWriter::GetVarInt62Len(payload_length);
380 
381   auto input = std::make_unique<char[]>(header_length);
382   QuicDataWriter writer(header_length, input.get());
383   writer.WriteVarInt62(frame_type);
384   writer.WriteVarInt62(payload_length);
385 
386   EXPECT_CALL(visitor_,
387               OnUnknownFrameStart(frame_type, header_length, payload_length));
388   EXPECT_CALL(visitor_, OnUnknownFrameEnd());
389 
390   auto raw_input = input.get();
391   for (uint64_t i = 0; i < header_length; ++i) {
392     char c = raw_input[i];
393     EXPECT_EQ(1u, decoder_.ProcessInput(&c, 1));
394   }
395 
396   EXPECT_THAT(decoder_.error(), IsQuicNoError());
397   EXPECT_EQ("", decoder_.error_detail());
398   EXPECT_EQ(frame_type, current_frame_type());
399 }
400 
TEST_F(HttpDecoderTest,GoAway)401 TEST_F(HttpDecoderTest, GoAway) {
402   InSequence s;
403   std::string input = absl::HexStringToBytes(
404       "07"    // type (GOAWAY)
405       "01"    // length
406       "01");  // ID
407 
408   // Visitor pauses processing.
409   EXPECT_CALL(visitor_, OnGoAwayFrame(GoAwayFrame({1})))
410       .WillOnce(Return(false));
411   EXPECT_EQ(input.size(), ProcessInputWithGarbageAppended(input));
412   EXPECT_THAT(decoder_.error(), IsQuicNoError());
413   EXPECT_EQ("", decoder_.error_detail());
414 
415   // Process the full frame.
416   EXPECT_CALL(visitor_, OnGoAwayFrame(GoAwayFrame({1})));
417   EXPECT_EQ(input.size(), ProcessInput(input));
418   EXPECT_THAT(decoder_.error(), IsQuicNoError());
419   EXPECT_EQ("", decoder_.error_detail());
420 
421   // Process the frame incrementally.
422   EXPECT_CALL(visitor_, OnGoAwayFrame(GoAwayFrame({1})));
423   ProcessInputCharByChar(input);
424   EXPECT_THAT(decoder_.error(), IsQuicNoError());
425   EXPECT_EQ("", decoder_.error_detail());
426 }
427 
TEST_F(HttpDecoderTest,HeadersFrame)428 TEST_F(HttpDecoderTest, HeadersFrame) {
429   InSequence s;
430   std::string input =
431       absl::StrCat(absl::HexStringToBytes("01"    // type (HEADERS)
432                                           "07"),  // length
433                    "Headers");                    // headers
434 
435   // Visitor pauses processing.
436   EXPECT_CALL(visitor_, OnHeadersFrameStart(2, 7)).WillOnce(Return(false));
437   absl::string_view remaining_input(input);
438   QuicByteCount processed_bytes =
439       ProcessInputWithGarbageAppended(remaining_input);
440   EXPECT_EQ(2u, processed_bytes);
441   remaining_input = remaining_input.substr(processed_bytes);
442 
443   EXPECT_CALL(visitor_, OnHeadersFramePayload(absl::string_view("Headers")))
444       .WillOnce(Return(false));
445   processed_bytes = ProcessInputWithGarbageAppended(remaining_input);
446   EXPECT_EQ(remaining_input.size(), processed_bytes);
447 
448   EXPECT_CALL(visitor_, OnHeadersFrameEnd()).WillOnce(Return(false));
449   EXPECT_EQ(0u, ProcessInputWithGarbageAppended(""));
450   EXPECT_THAT(decoder_.error(), IsQuicNoError());
451   EXPECT_EQ("", decoder_.error_detail());
452 
453   // Process the full frame.
454   EXPECT_CALL(visitor_, OnHeadersFrameStart(2, 7));
455   EXPECT_CALL(visitor_, OnHeadersFramePayload(absl::string_view("Headers")));
456   EXPECT_CALL(visitor_, OnHeadersFrameEnd());
457   EXPECT_EQ(input.size(), ProcessInput(input));
458   EXPECT_THAT(decoder_.error(), IsQuicNoError());
459   EXPECT_EQ("", decoder_.error_detail());
460 
461   // Process the frame incrementally.
462   EXPECT_CALL(visitor_, OnHeadersFrameStart(2, 7));
463   EXPECT_CALL(visitor_, OnHeadersFramePayload(absl::string_view("H")));
464   EXPECT_CALL(visitor_, OnHeadersFramePayload(absl::string_view("e")));
465   EXPECT_CALL(visitor_, OnHeadersFramePayload(absl::string_view("a")));
466   EXPECT_CALL(visitor_, OnHeadersFramePayload(absl::string_view("d")));
467   EXPECT_CALL(visitor_, OnHeadersFramePayload(absl::string_view("e")));
468   EXPECT_CALL(visitor_, OnHeadersFramePayload(absl::string_view("r")));
469   EXPECT_CALL(visitor_, OnHeadersFramePayload(absl::string_view("s")));
470   EXPECT_CALL(visitor_, OnHeadersFrameEnd());
471   ProcessInputCharByChar(input);
472   EXPECT_THAT(decoder_.error(), IsQuicNoError());
473   EXPECT_EQ("", decoder_.error_detail());
474 }
475 
TEST_F(HttpDecoderTest,EmptyDataFrame)476 TEST_F(HttpDecoderTest, EmptyDataFrame) {
477   InSequence s;
478   std::string input = absl::HexStringToBytes(
479       "00"    // type (DATA)
480       "00");  // length
481 
482   // Visitor pauses processing.
483   EXPECT_CALL(visitor_, OnDataFrameStart(2, 0)).WillOnce(Return(false));
484   EXPECT_EQ(input.size(), ProcessInputWithGarbageAppended(input));
485 
486   EXPECT_CALL(visitor_, OnDataFrameEnd()).WillOnce(Return(false));
487   EXPECT_EQ(0u, ProcessInputWithGarbageAppended(""));
488   EXPECT_THAT(decoder_.error(), IsQuicNoError());
489   EXPECT_EQ("", decoder_.error_detail());
490 
491   // Process the full frame.
492   EXPECT_CALL(visitor_, OnDataFrameStart(2, 0));
493   EXPECT_CALL(visitor_, OnDataFrameEnd());
494   EXPECT_EQ(input.size(), ProcessInput(input));
495   EXPECT_THAT(decoder_.error(), IsQuicNoError());
496   EXPECT_EQ("", decoder_.error_detail());
497 
498   // Process the frame incrementally.
499   EXPECT_CALL(visitor_, OnDataFrameStart(2, 0));
500   EXPECT_CALL(visitor_, OnDataFrameEnd());
501   ProcessInputCharByChar(input);
502   EXPECT_THAT(decoder_.error(), IsQuicNoError());
503   EXPECT_EQ("", decoder_.error_detail());
504 }
505 
TEST_F(HttpDecoderTest,EmptyHeadersFrame)506 TEST_F(HttpDecoderTest, EmptyHeadersFrame) {
507   InSequence s;
508   std::string input = absl::HexStringToBytes(
509       "01"    // type (HEADERS)
510       "00");  // length
511 
512   // Visitor pauses processing.
513   EXPECT_CALL(visitor_, OnHeadersFrameStart(2, 0)).WillOnce(Return(false));
514   EXPECT_EQ(input.size(), ProcessInputWithGarbageAppended(input));
515 
516   EXPECT_CALL(visitor_, OnHeadersFrameEnd()).WillOnce(Return(false));
517   EXPECT_EQ(0u, ProcessInputWithGarbageAppended(""));
518   EXPECT_THAT(decoder_.error(), IsQuicNoError());
519   EXPECT_EQ("", decoder_.error_detail());
520 
521   // Process the full frame.
522   EXPECT_CALL(visitor_, OnHeadersFrameStart(2, 0));
523   EXPECT_CALL(visitor_, OnHeadersFrameEnd());
524   EXPECT_EQ(input.size(), ProcessInput(input));
525   EXPECT_THAT(decoder_.error(), IsQuicNoError());
526   EXPECT_EQ("", decoder_.error_detail());
527 
528   // Process the frame incrementally.
529   EXPECT_CALL(visitor_, OnHeadersFrameStart(2, 0));
530   EXPECT_CALL(visitor_, OnHeadersFrameEnd());
531   ProcessInputCharByChar(input);
532   EXPECT_THAT(decoder_.error(), IsQuicNoError());
533   EXPECT_EQ("", decoder_.error_detail());
534 }
535 
TEST_F(HttpDecoderTest,GoawayWithOverlyLargePayload)536 TEST_F(HttpDecoderTest, GoawayWithOverlyLargePayload) {
537   std::string input = absl::HexStringToBytes(
538       "07"    // type (GOAWAY)
539       "10");  // length exceeding the maximum possible length for GOAWAY frame
540   // Process all data at once.
541   EXPECT_CALL(visitor_, OnError(&decoder_));
542   EXPECT_EQ(2u, ProcessInput(input));
543   EXPECT_THAT(decoder_.error(), IsError(QUIC_HTTP_FRAME_TOO_LARGE));
544   EXPECT_EQ("Frame is too large.", decoder_.error_detail());
545 }
546 
TEST_F(HttpDecoderTest,MaxPushIdWithOverlyLargePayload)547 TEST_F(HttpDecoderTest, MaxPushIdWithOverlyLargePayload) {
548   std::string input = absl::HexStringToBytes(
549       "0d"    // type (MAX_PUSH_ID)
550       "10");  // length exceeding the maximum possible length for MAX_PUSH_ID
551               // frame
552   // Process all data at once.
553   EXPECT_CALL(visitor_, OnError(&decoder_));
554   EXPECT_EQ(2u, ProcessInput(input));
555   EXPECT_THAT(decoder_.error(), IsError(QUIC_HTTP_FRAME_TOO_LARGE));
556   EXPECT_EQ("Frame is too large.", decoder_.error_detail());
557 }
558 
TEST_F(HttpDecoderTest,FrameWithOverlyLargePayload)559 TEST_F(HttpDecoderTest, FrameWithOverlyLargePayload) {
560   // Regression test for b/193919867: Ensure that reading frames with incredibly
561   // large payload lengths does not lead to allocating unbounded memory.
562   constexpr size_t max_input_length =
563       /*max frame type varint length*/ sizeof(uint64_t) +
564       /*max frame length varint length*/ sizeof(uint64_t) +
565       /*one byte of payload*/ sizeof(uint8_t);
566   char input[max_input_length];
567   for (uint64_t frame_type = 0; frame_type < 1025; frame_type++) {
568     ::testing::NiceMock<MockHttpDecoderVisitor> visitor;
569     HttpDecoder decoder(&visitor);
570     QuicDataWriter writer(max_input_length, input);
571     ASSERT_TRUE(writer.WriteVarInt62(frame_type));         // frame type.
572     ASSERT_TRUE(
573         writer.WriteVarInt62(quiche::kVarInt62MaxValue));  // frame length.
574     ASSERT_TRUE(writer.WriteUInt8(0x00));  // one byte of payload.
575     EXPECT_NE(decoder.ProcessInput(input, writer.length()), 0u) << frame_type;
576   }
577 }
578 
TEST_F(HttpDecoderTest,MalformedSettingsFrame)579 TEST_F(HttpDecoderTest, MalformedSettingsFrame) {
580   char input[30];
581   QuicDataWriter writer(30, input);
582   // Write type SETTINGS.
583   writer.WriteUInt8(0x04);
584   // Write length.
585   writer.WriteVarInt62(2048 * 1024);
586 
587   writer.WriteStringPiece("Malformed payload");
588   EXPECT_CALL(visitor_, OnError(&decoder_));
589   EXPECT_EQ(5u, decoder_.ProcessInput(input, ABSL_ARRAYSIZE(input)));
590   EXPECT_THAT(decoder_.error(), IsError(QUIC_HTTP_FRAME_TOO_LARGE));
591   EXPECT_EQ("Frame is too large.", decoder_.error_detail());
592 }
593 
TEST_F(HttpDecoderTest,Http2Frame)594 TEST_F(HttpDecoderTest, Http2Frame) {
595   std::string input = absl::HexStringToBytes(
596       "06"    // PING in HTTP/2 but not supported in HTTP/3.
597       "05"    // length
598       "15");  // random payload
599 
600   // Process the full frame.
601   EXPECT_CALL(visitor_, OnError(&decoder_));
602   EXPECT_EQ(1u, ProcessInput(input));
603   EXPECT_THAT(decoder_.error(), IsError(QUIC_HTTP_RECEIVE_SPDY_FRAME));
604   EXPECT_EQ("HTTP/2 frame received in a HTTP/3 connection: 6",
605             decoder_.error_detail());
606 }
607 
TEST_F(HttpDecoderTest,HeadersPausedThenData)608 TEST_F(HttpDecoderTest, HeadersPausedThenData) {
609   InSequence s;
610   std::string input =
611       absl::StrCat(absl::HexStringToBytes("01"    // type (HEADERS)
612                                           "07"),  // length
613                    "Headers",                     // headers
614                    absl::HexStringToBytes("00"    // type (DATA)
615                                           "05"),  // length
616                    "Data!");                      // data
617 
618   // Visitor pauses processing, maybe because header decompression is blocked.
619   EXPECT_CALL(visitor_, OnHeadersFrameStart(2, 7));
620   EXPECT_CALL(visitor_, OnHeadersFramePayload(absl::string_view("Headers")));
621   EXPECT_CALL(visitor_, OnHeadersFrameEnd()).WillOnce(Return(false));
622   absl::string_view remaining_input(input);
623   QuicByteCount processed_bytes =
624       ProcessInputWithGarbageAppended(remaining_input);
625   EXPECT_EQ(9u, processed_bytes);
626   remaining_input = remaining_input.substr(processed_bytes);
627 
628   // Process DATA frame.
629   EXPECT_CALL(visitor_, OnDataFrameStart(2, 5));
630   EXPECT_CALL(visitor_, OnDataFramePayload(absl::string_view("Data!")));
631   EXPECT_CALL(visitor_, OnDataFrameEnd());
632 
633   processed_bytes = ProcessInput(remaining_input);
634   EXPECT_EQ(remaining_input.size(), processed_bytes);
635 
636   EXPECT_THAT(decoder_.error(), IsQuicNoError());
637   EXPECT_EQ("", decoder_.error_detail());
638 }
639 
TEST_F(HttpDecoderTest,CorruptFrame)640 TEST_F(HttpDecoderTest, CorruptFrame) {
641   InSequence s;
642 
643   struct {
644     const char* const input;
645     const char* const error_message;
646   } kTestData[] = {{"\x0D"   // type (MAX_PUSH_ID)
647                     "\x01"   // length
648                     "\x40",  // first byte of two-byte varint push id
649                     "Unable to read MAX_PUSH_ID push_id."},
650                    {"\x0D"  // type (MAX_PUSH_ID)
651                     "\x04"  // length
652                     "\x05"  // valid push id
653                     "foo",  // superfluous data
654                     "Superfluous data in MAX_PUSH_ID frame."},
655                    {"\x07"   // type (GOAWAY)
656                     "\x01"   // length
657                     "\x40",  // first byte of two-byte varint stream id
658                     "Unable to read GOAWAY ID."},
659                    {"\x07"  // type (GOAWAY)
660                     "\x04"  // length
661                     "\x05"  // valid stream id
662                     "foo",  // superfluous data
663                     "Superfluous data in GOAWAY frame."},
664                    {"\x40\x89"  // type (ACCEPT_CH)
665                     "\x01"      // length
666                     "\x40",     // first byte of two-byte varint origin length
667                     "Unable to read ACCEPT_CH origin."},
668                    {"\x40\x89"  // type (ACCEPT_CH)
669                     "\x01"      // length
670                     "\x05",     // valid origin length but no origin string
671                     "Unable to read ACCEPT_CH origin."},
672                    {"\x40\x89"  // type (ACCEPT_CH)
673                     "\x04"      // length
674                     "\x05"      // valid origin length
675                     "foo",      // payload ends before origin ends
676                     "Unable to read ACCEPT_CH origin."},
677                    {"\x40\x89"  // type (ACCEPT_CH)
678                     "\x04"      // length
679                     "\x03"      // valid origin length
680                     "foo",      // payload ends at end of origin: no value
681                     "Unable to read ACCEPT_CH value."},
682                    {"\x40\x89"  // type (ACCEPT_CH)
683                     "\x05"      // length
684                     "\x03"      // valid origin length
685                     "foo"       // payload ends at end of origin: no value
686                     "\x40",     // first byte of two-byte varint value length
687                     "Unable to read ACCEPT_CH value."},
688                    {"\x40\x89"  // type (ACCEPT_CH)
689                     "\x08"      // length
690                     "\x03"      // valid origin length
691                     "foo"       // origin
692                     "\x05"      // valid value length
693                     "bar",      // payload ends before value ends
694                     "Unable to read ACCEPT_CH value."}};
695 
696   for (const auto& test_data : kTestData) {
697     {
698       HttpDecoder decoder(&visitor_);
699       EXPECT_CALL(visitor_, OnAcceptChFrameStart(_)).Times(AnyNumber());
700       EXPECT_CALL(visitor_, OnError(&decoder));
701 
702       absl::string_view input(test_data.input);
703       decoder.ProcessInput(input.data(), input.size());
704       EXPECT_THAT(decoder.error(), IsError(QUIC_HTTP_FRAME_ERROR));
705       EXPECT_EQ(test_data.error_message, decoder.error_detail());
706     }
707     {
708       HttpDecoder decoder(&visitor_);
709       EXPECT_CALL(visitor_, OnAcceptChFrameStart(_)).Times(AnyNumber());
710       EXPECT_CALL(visitor_, OnError(&decoder));
711 
712       absl::string_view input(test_data.input);
713       for (auto c : input) {
714         decoder.ProcessInput(&c, 1);
715       }
716       EXPECT_THAT(decoder.error(), IsError(QUIC_HTTP_FRAME_ERROR));
717       EXPECT_EQ(test_data.error_message, decoder.error_detail());
718     }
719   }
720 }
721 
TEST_F(HttpDecoderTest,EmptySettingsFrame)722 TEST_F(HttpDecoderTest, EmptySettingsFrame) {
723   std::string input = absl::HexStringToBytes(
724       "04"    // type (SETTINGS)
725       "00");  // frame length
726 
727   EXPECT_CALL(visitor_, OnSettingsFrameStart(2));
728 
729   SettingsFrame empty_frame;
730   EXPECT_CALL(visitor_, OnSettingsFrame(empty_frame));
731 
732   EXPECT_EQ(input.size(), ProcessInput(input));
733   EXPECT_THAT(decoder_.error(), IsQuicNoError());
734   EXPECT_EQ("", decoder_.error_detail());
735 }
736 
TEST_F(HttpDecoderTest,EmptyGoAwayFrame)737 TEST_F(HttpDecoderTest, EmptyGoAwayFrame) {
738   std::string input = absl::HexStringToBytes(
739       "07"    // type (GOAWAY)
740       "00");  // frame length
741 
742   EXPECT_CALL(visitor_, OnError(&decoder_));
743   EXPECT_EQ(input.size(), ProcessInput(input));
744   EXPECT_THAT(decoder_.error(), IsError(QUIC_HTTP_FRAME_ERROR));
745   EXPECT_EQ("Unable to read GOAWAY ID.", decoder_.error_detail());
746 }
747 
TEST_F(HttpDecoderTest,EmptyMaxPushIdFrame)748 TEST_F(HttpDecoderTest, EmptyMaxPushIdFrame) {
749   std::string input = absl::HexStringToBytes(
750       "0d"    // type (MAX_PUSH_ID)
751       "00");  // frame length
752 
753   EXPECT_CALL(visitor_, OnError(&decoder_));
754   EXPECT_EQ(input.size(), ProcessInput(input));
755   EXPECT_THAT(decoder_.error(), IsError(QUIC_HTTP_FRAME_ERROR));
756   EXPECT_EQ("Unable to read MAX_PUSH_ID push_id.", decoder_.error_detail());
757 }
758 
TEST_F(HttpDecoderTest,LargeStreamIdInGoAway)759 TEST_F(HttpDecoderTest, LargeStreamIdInGoAway) {
760   GoAwayFrame frame;
761   frame.id = 1ull << 60;
762   std::string goaway = HttpEncoder::SerializeGoAwayFrame(frame);
763   EXPECT_CALL(visitor_, OnGoAwayFrame(frame));
764   EXPECT_GT(goaway.length(), 0u);
765   EXPECT_EQ(goaway.length(),
766             decoder_.ProcessInput(goaway.data(), goaway.length()));
767   EXPECT_THAT(decoder_.error(), IsQuicNoError());
768   EXPECT_EQ("", decoder_.error_detail());
769 }
770 
771 // Old PRIORITY_UPDATE frame is parsed as unknown frame.
TEST_F(HttpDecoderTest,ObsoletePriorityUpdateFrame)772 TEST_F(HttpDecoderTest, ObsoletePriorityUpdateFrame) {
773   const QuicByteCount header_length = 2;
774   const QuicByteCount payload_length = 3;
775   InSequence s;
776   std::string input = absl::HexStringToBytes(
777       "0f"        // type (obsolete PRIORITY_UPDATE)
778       "03"        // length
779       "666f6f");  // payload "foo"
780 
781   // Process frame as a whole.
782   EXPECT_CALL(visitor_,
783               OnUnknownFrameStart(0x0f, header_length, payload_length));
784   EXPECT_CALL(visitor_, OnUnknownFramePayload(Eq("foo")));
785   EXPECT_CALL(visitor_, OnUnknownFrameEnd()).WillOnce(Return(false));
786 
787   EXPECT_EQ(header_length + payload_length,
788             ProcessInputWithGarbageAppended(input));
789   EXPECT_THAT(decoder_.error(), IsQuicNoError());
790   EXPECT_EQ("", decoder_.error_detail());
791 
792   // Process frame byte by byte.
793   EXPECT_CALL(visitor_,
794               OnUnknownFrameStart(0x0f, header_length, payload_length));
795   EXPECT_CALL(visitor_, OnUnknownFramePayload(Eq("f")));
796   EXPECT_CALL(visitor_, OnUnknownFramePayload(Eq("o")));
797   EXPECT_CALL(visitor_, OnUnknownFramePayload(Eq("o")));
798   EXPECT_CALL(visitor_, OnUnknownFrameEnd());
799 
800   ProcessInputCharByChar(input);
801   EXPECT_THAT(decoder_.error(), IsQuicNoError());
802   EXPECT_EQ("", decoder_.error_detail());
803 }
804 
TEST_F(HttpDecoderTest,PriorityUpdateFrame)805 TEST_F(HttpDecoderTest, PriorityUpdateFrame) {
806   InSequence s;
807   std::string input1 = absl::HexStringToBytes(
808       "800f0700"  // type (PRIORITY_UPDATE)
809       "01"        // length
810       "03");      // prioritized element id
811 
812   PriorityUpdateFrame priority_update1;
813   priority_update1.prioritized_element_id = 0x03;
814 
815   // Visitor pauses processing.
816   EXPECT_CALL(visitor_, OnPriorityUpdateFrameStart(5)).WillOnce(Return(false));
817   absl::string_view remaining_input(input1);
818   QuicByteCount processed_bytes =
819       ProcessInputWithGarbageAppended(remaining_input);
820   EXPECT_EQ(5u, processed_bytes);
821   remaining_input = remaining_input.substr(processed_bytes);
822 
823   EXPECT_CALL(visitor_, OnPriorityUpdateFrame(priority_update1))
824       .WillOnce(Return(false));
825   processed_bytes = ProcessInputWithGarbageAppended(remaining_input);
826   EXPECT_EQ(remaining_input.size(), processed_bytes);
827   EXPECT_THAT(decoder_.error(), IsQuicNoError());
828   EXPECT_EQ("", decoder_.error_detail());
829 
830   // Process the full frame.
831   EXPECT_CALL(visitor_, OnPriorityUpdateFrameStart(5));
832   EXPECT_CALL(visitor_, OnPriorityUpdateFrame(priority_update1));
833   EXPECT_EQ(input1.size(), ProcessInput(input1));
834   EXPECT_THAT(decoder_.error(), IsQuicNoError());
835   EXPECT_EQ("", decoder_.error_detail());
836 
837   // Process the frame incrementally.
838   EXPECT_CALL(visitor_, OnPriorityUpdateFrameStart(5));
839   EXPECT_CALL(visitor_, OnPriorityUpdateFrame(priority_update1));
840   ProcessInputCharByChar(input1);
841   EXPECT_THAT(decoder_.error(), IsQuicNoError());
842   EXPECT_EQ("", decoder_.error_detail());
843 
844   std::string input2 = absl::HexStringToBytes(
845       "800f0700"  // type (PRIORITY_UPDATE)
846       "04"        // length
847       "05"        // prioritized element id
848       "666f6f");  // priority field value: "foo"
849 
850   PriorityUpdateFrame priority_update2;
851   priority_update2.prioritized_element_id = 0x05;
852   priority_update2.priority_field_value = "foo";
853 
854   // Visitor pauses processing.
855   EXPECT_CALL(visitor_, OnPriorityUpdateFrameStart(5)).WillOnce(Return(false));
856   remaining_input = input2;
857   processed_bytes = ProcessInputWithGarbageAppended(remaining_input);
858   EXPECT_EQ(5u, processed_bytes);
859   remaining_input = remaining_input.substr(processed_bytes);
860 
861   EXPECT_CALL(visitor_, OnPriorityUpdateFrame(priority_update2))
862       .WillOnce(Return(false));
863   processed_bytes = ProcessInputWithGarbageAppended(remaining_input);
864   EXPECT_EQ(remaining_input.size(), processed_bytes);
865   EXPECT_THAT(decoder_.error(), IsQuicNoError());
866   EXPECT_EQ("", decoder_.error_detail());
867 
868   // Process the full frame.
869   EXPECT_CALL(visitor_, OnPriorityUpdateFrameStart(5));
870   EXPECT_CALL(visitor_, OnPriorityUpdateFrame(priority_update2));
871   EXPECT_EQ(input2.size(), ProcessInput(input2));
872   EXPECT_THAT(decoder_.error(), IsQuicNoError());
873   EXPECT_EQ("", decoder_.error_detail());
874 
875   // Process the frame incrementally.
876   EXPECT_CALL(visitor_, OnPriorityUpdateFrameStart(5));
877   EXPECT_CALL(visitor_, OnPriorityUpdateFrame(priority_update2));
878   ProcessInputCharByChar(input2);
879   EXPECT_THAT(decoder_.error(), IsQuicNoError());
880   EXPECT_EQ("", decoder_.error_detail());
881 }
882 
TEST_F(HttpDecoderTest,CorruptPriorityUpdateFrame)883 TEST_F(HttpDecoderTest, CorruptPriorityUpdateFrame) {
884   std::string payload =
885       absl::HexStringToBytes("4005");  // prioritized element id
886   struct {
887     size_t payload_length;
888     const char* const error_message;
889   } kTestData[] = {
890       {0, "Unable to read prioritized element id."},
891       {1, "Unable to read prioritized element id."},
892   };
893 
894   for (const auto& test_data : kTestData) {
895     std::string input =
896         absl::HexStringToBytes("800f0700");  // type PRIORITY_UPDATE
897     input.push_back(test_data.payload_length);
898     size_t header_length = input.size();
899     input.append(payload.data(), test_data.payload_length);
900 
901     HttpDecoder decoder(&visitor_);
902     EXPECT_CALL(visitor_, OnPriorityUpdateFrameStart(header_length));
903     EXPECT_CALL(visitor_, OnError(&decoder));
904 
905     QuicByteCount processed_bytes =
906         decoder.ProcessInput(input.data(), input.size());
907     EXPECT_EQ(input.size(), processed_bytes);
908     EXPECT_THAT(decoder.error(), IsError(QUIC_HTTP_FRAME_ERROR));
909     EXPECT_EQ(test_data.error_message, decoder.error_detail());
910   }
911 }
912 
TEST_F(HttpDecoderTest,AcceptChFrame)913 TEST_F(HttpDecoderTest, AcceptChFrame) {
914   InSequence s;
915   std::string input1 = absl::HexStringToBytes(
916       "4089"  // type (ACCEPT_CH)
917       "00");  // length
918 
919   AcceptChFrame accept_ch1;
920 
921   // Visitor pauses processing.
922   EXPECT_CALL(visitor_, OnAcceptChFrameStart(3)).WillOnce(Return(false));
923   absl::string_view remaining_input(input1);
924   QuicByteCount processed_bytes =
925       ProcessInputWithGarbageAppended(remaining_input);
926   EXPECT_EQ(3u, processed_bytes);
927   remaining_input = remaining_input.substr(processed_bytes);
928 
929   EXPECT_CALL(visitor_, OnAcceptChFrame(accept_ch1)).WillOnce(Return(false));
930   processed_bytes = ProcessInputWithGarbageAppended(remaining_input);
931   EXPECT_EQ(remaining_input.size(), processed_bytes);
932   EXPECT_THAT(decoder_.error(), IsQuicNoError());
933   EXPECT_EQ("", decoder_.error_detail());
934 
935   // Process the full frame.
936   EXPECT_CALL(visitor_, OnAcceptChFrameStart(3));
937   EXPECT_CALL(visitor_, OnAcceptChFrame(accept_ch1));
938   EXPECT_EQ(input1.size(), ProcessInput(input1));
939   EXPECT_THAT(decoder_.error(), IsQuicNoError());
940   EXPECT_EQ("", decoder_.error_detail());
941 
942   // Process the frame incrementally.
943   EXPECT_CALL(visitor_, OnAcceptChFrameStart(3));
944   EXPECT_CALL(visitor_, OnAcceptChFrame(accept_ch1));
945   ProcessInputCharByChar(input1);
946   EXPECT_THAT(decoder_.error(), IsQuicNoError());
947   EXPECT_EQ("", decoder_.error_detail());
948 
949   std::string input2 = absl::HexStringToBytes(
950       "4089"      // type (ACCEPT_CH)
951       "08"        // length
952       "03"        // length of origin
953       "666f6f"    // origin "foo"
954       "03"        // length of value
955       "626172");  // value "bar"
956 
957   AcceptChFrame accept_ch2;
958   accept_ch2.entries.push_back({"foo", "bar"});
959 
960   // Visitor pauses processing.
961   EXPECT_CALL(visitor_, OnAcceptChFrameStart(3)).WillOnce(Return(false));
962   remaining_input = input2;
963   processed_bytes = ProcessInputWithGarbageAppended(remaining_input);
964   EXPECT_EQ(3u, processed_bytes);
965   remaining_input = remaining_input.substr(processed_bytes);
966 
967   EXPECT_CALL(visitor_, OnAcceptChFrame(accept_ch2)).WillOnce(Return(false));
968   processed_bytes = ProcessInputWithGarbageAppended(remaining_input);
969   EXPECT_EQ(remaining_input.size(), processed_bytes);
970   EXPECT_THAT(decoder_.error(), IsQuicNoError());
971   EXPECT_EQ("", decoder_.error_detail());
972 
973   // Process the full frame.
974   EXPECT_CALL(visitor_, OnAcceptChFrameStart(3));
975   EXPECT_CALL(visitor_, OnAcceptChFrame(accept_ch2));
976   EXPECT_EQ(input2.size(), ProcessInput(input2));
977   EXPECT_THAT(decoder_.error(), IsQuicNoError());
978   EXPECT_EQ("", decoder_.error_detail());
979 
980   // Process the frame incrementally.
981   EXPECT_CALL(visitor_, OnAcceptChFrameStart(3));
982   EXPECT_CALL(visitor_, OnAcceptChFrame(accept_ch2));
983   ProcessInputCharByChar(input2);
984   EXPECT_THAT(decoder_.error(), IsQuicNoError());
985   EXPECT_EQ("", decoder_.error_detail());
986 }
987 
TEST_F(HttpDecoderTest,WebTransportStreamDisabled)988 TEST_F(HttpDecoderTest, WebTransportStreamDisabled) {
989   InSequence s;
990 
991   // Unknown frame of type 0x41 and length 0x104.
992   std::string input = absl::HexStringToBytes("40414104");
993   EXPECT_CALL(visitor_, OnUnknownFrameStart(0x41, input.size(), 0x104));
994   EXPECT_EQ(ProcessInput(input), input.size());
995 }
996 
TEST(HttpDecoderTestNoFixture,WebTransportStream)997 TEST(HttpDecoderTestNoFixture, WebTransportStream) {
998   testing::StrictMock<MockHttpDecoderVisitor> visitor;
999   HttpDecoder decoder(&visitor);
1000   decoder.EnableWebTransportStreamParsing();
1001 
1002   // WebTransport stream for session ID 0x104, with four bytes of extra data.
1003   std::string input = absl::HexStringToBytes("40414104ffffffff");
1004   EXPECT_CALL(visitor, OnWebTransportStreamFrameType(4, 0x104));
1005   QuicByteCount bytes = decoder.ProcessInput(input.data(), input.size());
1006   EXPECT_EQ(bytes, 4u);
1007 }
1008 
TEST(HttpDecoderTestNoFixture,WebTransportStreamError)1009 TEST(HttpDecoderTestNoFixture, WebTransportStreamError) {
1010   testing::StrictMock<MockHttpDecoderVisitor> visitor;
1011   HttpDecoder decoder(&visitor);
1012   decoder.EnableWebTransportStreamParsing();
1013 
1014   std::string input = absl::HexStringToBytes("404100");
1015   EXPECT_CALL(visitor, OnWebTransportStreamFrameType(_, _));
1016   decoder.ProcessInput(input.data(), input.size());
1017 
1018   EXPECT_QUIC_BUG(
1019       {
1020         EXPECT_CALL(visitor, OnError(_));
1021         decoder.ProcessInput(input.data(), input.size());
1022       },
1023       "HttpDecoder called after an indefinite-length frame");
1024 }
1025 
TEST_F(HttpDecoderTest,DecodeSettings)1026 TEST_F(HttpDecoderTest, DecodeSettings) {
1027   std::string input = absl::HexStringToBytes(
1028       "04"    // type (SETTINGS)
1029       "07"    // length
1030       "01"    // identifier (SETTINGS_QPACK_MAX_TABLE_CAPACITY)
1031       "02"    // content
1032       "06"    // identifier (SETTINGS_MAX_HEADER_LIST_SIZE)
1033       "05"    // content
1034       "4100"  // identifier, encoded on 2 bytes (0x40), value is 256 (0x100)
1035       "04");  // content
1036 
1037   SettingsFrame frame;
1038   frame.values[1] = 2;
1039   frame.values[6] = 5;
1040   frame.values[256] = 4;
1041 
1042   SettingsFrame out;
1043   EXPECT_TRUE(HttpDecoder::DecodeSettings(input.data(), input.size(), &out));
1044   EXPECT_EQ(frame, out);
1045 
1046   // non-settings frame.
1047   input = absl::HexStringToBytes(
1048       "0D"    // type (MAX_PUSH_ID)
1049       "01"    // length
1050       "01");  // Push Id
1051 
1052   EXPECT_FALSE(HttpDecoder::DecodeSettings(input.data(), input.size(), &out));
1053 
1054   // Corrupt SETTINGS.
1055   input = absl::HexStringToBytes(
1056       "04"    // type (SETTINGS)
1057       "01"    // length
1058       "42");  // First byte of setting identifier, indicating a 2-byte varint62.
1059 
1060   EXPECT_FALSE(HttpDecoder::DecodeSettings(input.data(), input.size(), &out));
1061 }
1062 
1063 }  // namespace
1064 }  // namespace test
1065 }  // namespace quic
1066