blob: 98b7e85f41f1ae053aa1b47ac38481e0bdd4178c [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright 2013 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/http/quic_spdy_stream.h"
6
7#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include <utility>
10
fayanga45ee8a2020-03-20 08:56:11 -070011#include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/http/http_encoder.h"
13#include "net/third_party/quiche/src/quic/core/http/spdy_utils.h"
14#include "net/third_party/quiche/src/quic/core/quic_connection.h"
15#include "net/third_party/quiche/src/quic/core/quic_stream_sequencer_buffer.h"
16#include "net/third_party/quiche/src/quic/core/quic_utils.h"
17#include "net/third_party/quiche/src/quic/core/quic_versions.h"
18#include "net/third_party/quiche/src/quic/core/quic_write_blocked_list.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050019#include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h"
20#include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h"
21#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050022#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
bnc7e9155d2019-11-21 17:50:40 -080023#include "net/third_party/quiche/src/quic/test_tools/qpack/qpack_test_utils.h"
renjietangbb2e22a2019-09-12 15:46:39 -070024#include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h"
fayang8790d292020-03-18 13:17:23 -070025#include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050026#include "net/third_party/quiche/src/quic/test_tools/quic_flow_controller_peer.h"
27#include "net/third_party/quiche/src/quic/test_tools/quic_session_peer.h"
28#include "net/third_party/quiche/src/quic/test_tools/quic_spdy_session_peer.h"
29#include "net/third_party/quiche/src/quic/test_tools/quic_spdy_stream_peer.h"
30#include "net/third_party/quiche/src/quic/test_tools/quic_stream_peer.h"
31#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
bnc4e9283d2019-12-17 07:08:57 -080032#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
dmcardleba2fb7e2019-12-13 07:44:34 -080033#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
34#include "net/third_party/quiche/src/common/platform/api/quiche_text_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050035
36using spdy::kV3HighestPriority;
37using spdy::kV3LowestPriority;
38using spdy::SpdyHeaderBlock;
39using spdy::SpdyPriority;
40using testing::_;
41using testing::AtLeast;
bnc8d041302019-06-10 10:19:04 -070042using testing::ElementsAre;
QUICHE teama6ef0a62019-03-07 20:34:33 -050043using testing::Invoke;
bnc519216c2019-07-09 05:03:48 -070044using testing::InvokeWithoutArgs;
bncbdd303e2019-07-09 05:33:17 -070045using testing::MatchesRegex;
bnc8d041302019-06-10 10:19:04 -070046using testing::Pair;
QUICHE teama6ef0a62019-03-07 20:34:33 -050047using testing::Return;
48using testing::StrictMock;
49
50namespace quic {
51namespace test {
52namespace {
53
54const bool kShouldProcessData = true;
bnc9f977e42019-06-07 11:36:34 -070055const char kDataFramePayload[] = "some data";
QUICHE teama6ef0a62019-03-07 20:34:33 -050056
fayang8790d292020-03-18 13:17:23 -070057class TestCryptoStream : public QuicCryptoStream, public QuicCryptoHandshaker {
58 public:
59 explicit TestCryptoStream(QuicSession* session)
60 : QuicCryptoStream(session),
61 QuicCryptoHandshaker(this, session),
62 encryption_established_(false),
63 one_rtt_keys_available_(false),
64 params_(new QuicCryptoNegotiatedParameters) {
65 // Simulate a negotiated cipher_suite with a fake value.
66 params_->cipher_suite = 1;
67 }
68
69 void OnHandshakeMessage(const CryptoHandshakeMessage& /*message*/) override {
70 encryption_established_ = true;
71 one_rtt_keys_available_ = true;
72 QuicErrorCode error;
73 std::string error_details;
74 session()->config()->SetInitialStreamFlowControlWindowToSend(
75 kInitialStreamFlowControlWindowForTest);
76 session()->config()->SetInitialSessionFlowControlWindowToSend(
77 kInitialSessionFlowControlWindowForTest);
78 if (session()->connection()->version().handshake_protocol ==
79 PROTOCOL_TLS1_3) {
80 TransportParameters transport_parameters;
81 EXPECT_TRUE(
82 session()->config()->FillTransportParameters(&transport_parameters));
83 error = session()->config()->ProcessTransportParameters(
84 transport_parameters, CLIENT, &error_details);
85 } else {
86 CryptoHandshakeMessage msg;
87 session()->config()->ToHandshakeMessage(&msg, transport_version());
88 error =
89 session()->config()->ProcessPeerHello(msg, CLIENT, &error_details);
90 }
91 EXPECT_THAT(error, IsQuicNoError());
fayanga45ee8a2020-03-20 08:56:11 -070092 session()->OnNewEncryptionKeyAvailable(
93 ENCRYPTION_FORWARD_SECURE,
94 std::make_unique<NullEncrypter>(session()->perspective()));
fayang8790d292020-03-18 13:17:23 -070095 session()->OnConfigNegotiated();
fayangd18bfb92020-03-19 17:24:21 -070096 if (session()->connection()->version().handshake_protocol ==
97 PROTOCOL_TLS1_3) {
98 session()->OnOneRttKeysAvailable();
99 } else {
100 session()->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE);
101 }
fayang8790d292020-03-18 13:17:23 -0700102 session()->DiscardOldEncryptionKey(ENCRYPTION_INITIAL);
103 }
104
105 // QuicCryptoStream implementation
106 bool encryption_established() const override {
107 return encryption_established_;
108 }
109 bool one_rtt_keys_available() const override {
110 return one_rtt_keys_available_;
111 }
112 HandshakeState GetHandshakeState() const override {
113 return one_rtt_keys_available() ? HANDSHAKE_COMPLETE : HANDSHAKE_START;
114 }
115 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
116 const override {
117 return *params_;
118 }
119 CryptoMessageParser* crypto_message_parser() override {
120 return QuicCryptoHandshaker::crypto_message_parser();
121 }
122 void OnPacketDecrypted(EncryptionLevel /*level*/) override {}
123 void OnOneRttPacketAcknowledged() override {}
124 void OnHandshakeDoneReceived() override {}
125
126 MOCK_METHOD0(OnCanWrite, void());
127
128 bool HasPendingCryptoRetransmission() const override { return false; }
129
130 MOCK_CONST_METHOD0(HasPendingRetransmission, bool());
131
132 private:
133 using QuicCryptoStream::session;
134
135 bool encryption_established_;
136 bool one_rtt_keys_available_;
137 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> params_;
138};
139
QUICHE teama6ef0a62019-03-07 20:34:33 -0500140class TestStream : public QuicSpdyStream {
141 public:
142 TestStream(QuicStreamId id,
143 QuicSpdySession* session,
144 bool should_process_data)
145 : QuicSpdyStream(id, session, BIDIRECTIONAL),
146 should_process_data_(should_process_data) {}
147 ~TestStream() override = default;
148
149 using QuicSpdyStream::set_ack_listener;
150 using QuicStream::CloseWriteSide;
151 using QuicStream::WriteOrBufferData;
152
153 void OnBodyAvailable() override {
154 if (!should_process_data_) {
155 return;
156 }
157 char buffer[2048];
158 struct iovec vec;
159 vec.iov_base = buffer;
bnc4e9283d2019-12-17 07:08:57 -0800160 vec.iov_len = QUICHE_ARRAYSIZE(buffer);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500161 size_t bytes_read = Readv(&vec, 1);
vasilvvc48c8712019-03-11 13:38:16 -0700162 data_ += std::string(buffer, bytes_read);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500163 }
164
165 MOCK_METHOD1(WriteHeadersMock, void(bool fin));
166
167 size_t WriteHeadersImpl(spdy::SpdyHeaderBlock header_block,
168 bool fin,
169 QuicReferenceCountedPointer<QuicAckListenerInterface>
dschinazi17d42422019-06-18 16:35:07 -0700170 /*ack_listener*/) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500171 saved_headers_ = std::move(header_block);
172 WriteHeadersMock(fin);
renjietanga29a96a2019-10-10 12:47:50 -0700173 if (VersionUsesHttp3(transport_version())) {
renjietang2abedac2019-05-20 14:04:50 -0700174 // In this case, call QuicSpdyStream::WriteHeadersImpl() that does the
175 // actual work of closing the stream.
176 QuicSpdyStream::WriteHeadersImpl(saved_headers_.Clone(), fin, nullptr);
177 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500178 return 0;
179 }
180
vasilvvc48c8712019-03-11 13:38:16 -0700181 const std::string& data() const { return data_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500182 const spdy::SpdyHeaderBlock& saved_headers() const { return saved_headers_; }
183
bnc8d041302019-06-10 10:19:04 -0700184 // Expose protected accessor.
185 const QuicStreamSequencer* sequencer() const {
186 return QuicStream::sequencer();
187 }
188
QUICHE teama6ef0a62019-03-07 20:34:33 -0500189 private:
190 bool should_process_data_;
191 spdy::SpdyHeaderBlock saved_headers_;
vasilvvc48c8712019-03-11 13:38:16 -0700192 std::string data_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500193};
194
fayang8790d292020-03-18 13:17:23 -0700195class TestSession : public MockQuicSpdySession {
196 public:
197 explicit TestSession(QuicConnection* connection)
198 : MockQuicSpdySession(connection, /*create_mock_crypto_stream=*/false),
199 crypto_stream_(this) {}
200
201 TestCryptoStream* GetMutableCryptoStream() override {
202 return &crypto_stream_;
203 }
204
205 const TestCryptoStream* GetCryptoStream() const override {
206 return &crypto_stream_;
207 }
208
209 private:
210 StrictMock<TestCryptoStream> crypto_stream_;
211};
212
QUICHE teama6ef0a62019-03-07 20:34:33 -0500213class TestMockUpdateStreamSession : public MockQuicSpdySession {
214 public:
215 explicit TestMockUpdateStreamSession(QuicConnection* connection)
fayang476683a2019-07-25 12:42:16 -0700216 : MockQuicSpdySession(connection),
217 expected_precedence_(
218 spdy::SpdyStreamPrecedence(QuicStream::kDefaultPriority)) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500219
fayang476683a2019-07-25 12:42:16 -0700220 void UpdateStreamPriority(
221 QuicStreamId id,
222 const spdy::SpdyStreamPrecedence& precedence) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500223 EXPECT_EQ(id, expected_stream_->id());
fayang476683a2019-07-25 12:42:16 -0700224 EXPECT_EQ(expected_precedence_, precedence);
225 EXPECT_EQ(expected_precedence_, expected_stream_->precedence());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500226 }
227
228 void SetExpectedStream(QuicSpdyStream* stream) { expected_stream_ = stream; }
fayang476683a2019-07-25 12:42:16 -0700229 void SetExpectedPriority(const spdy::SpdyStreamPrecedence& precedence) {
230 expected_precedence_ = precedence;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500231 }
232
233 private:
234 QuicSpdyStream* expected_stream_;
fayang476683a2019-07-25 12:42:16 -0700235 spdy::SpdyStreamPrecedence expected_precedence_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500236};
237
238class QuicSpdyStreamTest : public QuicTestWithParam<ParsedQuicVersion> {
bnc9f977e42019-06-07 11:36:34 -0700239 protected:
QUICHE teama6ef0a62019-03-07 20:34:33 -0500240 QuicSpdyStreamTest() {
241 headers_[":host"] = "www.google.com";
242 headers_[":path"] = "/index.hml";
243 headers_[":scheme"] = "https";
244 headers_["cookie"] =
245 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
246 "__utmc=160408618; "
247 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
248 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
249 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
250 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
251 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
252 "1zFMi5vzcns38-8_Sns; "
253 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
254 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
255 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
256 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
257 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
258 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
259 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
260 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
261 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
262 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
263 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
264 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
265 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
266 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
267 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
268 }
269
bnc8d041302019-06-10 10:19:04 -0700270 ~QuicSpdyStreamTest() override = default;
271
bnc2e963aa2019-07-31 18:03:17 -0700272 // Return QPACK-encoded header block without using the dynamic table.
273 std::string EncodeQpackHeaders(
dmcardleba2fb7e2019-12-13 07:44:34 -0800274 std::vector<std::pair<quiche::QuicheStringPiece,
275 quiche::QuicheStringPiece>> headers) {
bnc2e963aa2019-07-31 18:03:17 -0700276 SpdyHeaderBlock header_block;
277 for (const auto& header_field : headers) {
278 header_block.AppendValueOrAddHeader(header_field.first,
279 header_field.second);
280 }
281
282 return EncodeQpackHeaders(header_block);
283 }
284
285 // Return QPACK-encoded header block without using the dynamic table.
286 std::string EncodeQpackHeaders(const SpdyHeaderBlock& header) {
renjietangc2aa5cb2019-06-20 12:22:53 -0700287 NoopQpackStreamSenderDelegate encoder_stream_sender_delegate;
vasilvv0fc587f2019-09-06 13:33:08 -0700288 auto qpack_encoder = std::make_unique<QpackEncoder>(session_.get());
renjietang8a2df8f2019-08-07 10:43:52 -0700289 qpack_encoder->set_qpack_stream_sender_delegate(
290 &encoder_stream_sender_delegate);
bnc2e963aa2019-07-31 18:03:17 -0700291 // QpackEncoder does not use the dynamic table by default,
292 // therefore the value of |stream_id| does not matter.
bnc609c24e2019-09-10 05:24:32 -0700293 return qpack_encoder->EncodeHeaderList(/* stream_id = */ 0, header,
294 nullptr);
renjietangbd1a0392019-05-31 11:36:24 -0700295 }
296
QUICHE teama6ef0a62019-03-07 20:34:33 -0500297 void Initialize(bool stream_should_process_data) {
renjietang7498c8c2019-07-02 19:28:42 -0700298 InitializeWithPerspective(stream_should_process_data,
299 Perspective::IS_SERVER);
300 }
301
302 void InitializeWithPerspective(bool stream_should_process_data,
303 Perspective perspective) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500304 connection_ = new StrictMock<MockQuicConnection>(
renjietang7498c8c2019-07-02 19:28:42 -0700305 &helper_, &alarm_factory_, perspective, SupportedVersions(GetParam()));
fayang8790d292020-03-18 13:17:23 -0700306 session_ = std::make_unique<StrictMock<TestSession>>(connection_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500307 session_->Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800308 ON_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800309 .WillByDefault(
310 Invoke(session_.get(), &MockQuicSpdySession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500311
312 stream_ =
313 new StrictMock<TestStream>(GetNthClientInitiatedBidirectionalId(0),
314 session_.get(), stream_should_process_data);
315 session_->ActivateStream(QuicWrapUnique(stream_));
316 stream2_ =
317 new StrictMock<TestStream>(GetNthClientInitiatedBidirectionalId(1),
318 session_.get(), stream_should_process_data);
319 session_->ActivateStream(QuicWrapUnique(stream2_));
renjietangbb2e22a2019-09-12 15:46:39 -0700320 QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow(
321 session_->config(), kMinimumFlowControlSendWindow);
dschinazi18cdf132019-10-09 16:08:18 -0700322 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesUnidirectional(
323 session_->config(), kMinimumFlowControlSendWindow);
324 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional(
325 session_->config(), kMinimumFlowControlSendWindow);
326 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesOutgoingBidirectional(
327 session_->config(), kMinimumFlowControlSendWindow);
renjietange6d94672020-01-07 10:30:10 -0800328 QuicConfigPeer::SetReceivedMaxUnidirectionalStreams(session_->config(), 10);
renjietangbb2e22a2019-09-12 15:46:39 -0700329 session_->OnConfigNegotiated();
rchee322322019-10-11 14:56:03 -0700330 if (UsesHttp3()) {
renjietangd6e461b2020-02-04 16:16:35 -0800331 // The control stream will write the stream type, a greased frame, and
332 // SETTINGS frame.
333 int num_control_stream_writes = 3;
rch4cd745d2019-12-03 13:19:41 -0800334 if (session_->perspective() == Perspective::IS_CLIENT) {
335 // The control stream also writes the max push id frame.
336 num_control_stream_writes++;
337 }
bnccd3c4b52020-01-10 11:13:18 -0800338 auto send_control_stream =
339 QuicSpdySessionPeer::GetSendControlStream(session_.get());
renjietang41a1b412020-02-27 15:05:14 -0800340 EXPECT_CALL(*session_,
341 WritevData(send_control_stream->id(), _, _, _, _, _))
rch4cd745d2019-12-03 13:19:41 -0800342 .Times(num_control_stream_writes);
rchee322322019-10-11 14:56:03 -0700343 auto qpack_decoder_stream =
344 QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get());
renjietang7c239172020-02-21 13:50:39 -0800345 EXPECT_CALL(*session_,
renjietang41a1b412020-02-27 15:05:14 -0800346 WritevData(qpack_decoder_stream->id(), 1, 0, _, _, _));
renjietang296cfa32019-11-27 12:49:46 -0800347 auto qpack_encoder_stream =
348 QuicSpdySessionPeer::GetQpackEncoderSendStream(session_.get());
renjietang7c239172020-02-21 13:50:39 -0800349 EXPECT_CALL(*session_,
renjietang41a1b412020-02-27 15:05:14 -0800350 WritevData(qpack_encoder_stream->id(), 1, 0, _, _, _));
rchee322322019-10-11 14:56:03 -0700351 }
fayang8790d292020-03-18 13:17:23 -0700352 TestCryptoStream* crypto_stream = session_->GetMutableCryptoStream();
353 EXPECT_CALL(*crypto_stream, HasPendingRetransmission())
354 .Times(testing::AnyNumber());
355
356 if (connection_->version().HasHandshakeDone() &&
357 session_->perspective() == Perspective::IS_SERVER) {
358 EXPECT_CALL(*connection_, SendControlFrame(_))
359 .WillOnce(Invoke(&ClearControlFrame));
360 }
361 CryptoHandshakeMessage message;
362 session_->GetMutableCryptoStream()->OnHandshakeMessage(message);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500363 }
364
365 QuicHeaderList ProcessHeaders(bool fin, const SpdyHeaderBlock& headers) {
366 QuicHeaderList h = AsHeaderList(headers);
367 stream_->OnStreamHeaderList(fin, h.uncompressed_header_bytes(), h);
368 return h;
369 }
370
371 QuicStreamId GetNthClientInitiatedBidirectionalId(int n) {
372 return GetNthClientInitiatedBidirectionalStreamId(
373 connection_->transport_version(), n);
374 }
375
renjietanga29a96a2019-10-10 12:47:50 -0700376 bool UsesHttp3() const {
377 return VersionUsesHttp3(GetParam().transport_version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500378 }
379
bnc2e963aa2019-07-31 18:03:17 -0700380 // Construct HEADERS frame with QPACK-encoded |headers| without using the
381 // dynamic table.
382 std::string HeadersFrame(
dmcardleba2fb7e2019-12-13 07:44:34 -0800383 std::vector<std::pair<quiche::QuicheStringPiece,
384 quiche::QuicheStringPiece>> headers) {
bnc2e963aa2019-07-31 18:03:17 -0700385 return HeadersFrame(EncodeQpackHeaders(headers));
386 }
387
388 // Construct HEADERS frame with QPACK-encoded |headers| without using the
389 // dynamic table.
390 std::string HeadersFrame(const SpdyHeaderBlock& headers) {
391 return HeadersFrame(EncodeQpackHeaders(headers));
392 }
393
394 // Construct HEADERS frame with given payload.
dmcardleba2fb7e2019-12-13 07:44:34 -0800395 std::string HeadersFrame(quiche::QuicheStringPiece payload) {
bnc9f977e42019-06-07 11:36:34 -0700396 std::unique_ptr<char[]> headers_buffer;
397 QuicByteCount headers_frame_header_length =
bnc46942722019-10-29 11:56:21 -0700398 HttpEncoder::SerializeHeadersFrameHeader(payload.length(),
399 &headers_buffer);
dmcardleba2fb7e2019-12-13 07:44:34 -0800400 quiche::QuicheStringPiece headers_frame_header(headers_buffer.get(),
401 headers_frame_header_length);
402 return quiche::QuicheStrCat(headers_frame_header, payload);
bnc9f977e42019-06-07 11:36:34 -0700403 }
404
dmcardleba2fb7e2019-12-13 07:44:34 -0800405 std::string DataFrame(quiche::QuicheStringPiece payload) {
bnc9f977e42019-06-07 11:36:34 -0700406 std::unique_ptr<char[]> data_buffer;
407 QuicByteCount data_frame_header_length =
bnc46942722019-10-29 11:56:21 -0700408 HttpEncoder::SerializeDataFrameHeader(payload.length(), &data_buffer);
dmcardleba2fb7e2019-12-13 07:44:34 -0800409 quiche::QuicheStringPiece data_frame_header(data_buffer.get(),
410 data_frame_header_length);
411 return quiche::QuicheStrCat(data_frame_header, payload);
bnc9f977e42019-06-07 11:36:34 -0700412 }
413
dmcardleba2fb7e2019-12-13 07:44:34 -0800414 std::string UnknownFrame(uint64_t frame_type,
415 quiche::QuicheStringPiece payload) {
bnc25827c42019-07-29 08:57:24 -0700416 std::string frame;
417 const size_t length = QuicDataWriter::GetVarInt62Len(frame_type) +
418 QuicDataWriter::GetVarInt62Len(payload.size()) +
419 payload.size();
420 frame.resize(length);
421
fayangf0041ed2019-07-31 02:39:45 -0700422 QuicDataWriter writer(length, const_cast<char*>(frame.data()));
bnc25827c42019-07-29 08:57:24 -0700423 writer.WriteVarInt62(frame_type);
424 writer.WriteStringPieceVarInt62(payload);
425 // Even though integers can be encoded with different lengths,
426 // QuicDataWriter is expected to produce an encoding in Write*() of length
427 // promised in GetVarInt62Len().
428 DCHECK_EQ(length, writer.length());
429
430 return frame;
431 }
432
QUICHE teama6ef0a62019-03-07 20:34:33 -0500433 MockQuicConnectionHelper helper_;
434 MockAlarmFactory alarm_factory_;
435 MockQuicConnection* connection_;
fayang8790d292020-03-18 13:17:23 -0700436 std::unique_ptr<TestSession> session_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500437
438 // Owned by the |session_|.
439 TestStream* stream_;
440 TestStream* stream2_;
441
442 SpdyHeaderBlock headers_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500443};
444
vasilvvc48c8712019-03-11 13:38:16 -0700445INSTANTIATE_TEST_SUITE_P(Tests,
446 QuicSpdyStreamTest,
dschinazi142051a2019-09-18 18:17:29 -0700447 ::testing::ValuesIn(AllSupportedVersions()),
448 ::testing::PrintToStringParamName());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500449
450TEST_P(QuicSpdyStreamTest, ProcessHeaderList) {
451 Initialize(kShouldProcessData);
452
fayang476683a2019-07-25 12:42:16 -0700453 stream_->OnStreamHeadersPriority(
454 spdy::SpdyStreamPrecedence(kV3HighestPriority));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500455 ProcessHeaders(false, headers_);
456 EXPECT_EQ("", stream_->data());
457 EXPECT_FALSE(stream_->header_list().empty());
458 EXPECT_FALSE(stream_->IsDoneReading());
459}
460
461TEST_P(QuicSpdyStreamTest, ProcessTooLargeHeaderList) {
462 Initialize(kShouldProcessData);
463
bnc446887e2019-11-27 13:08:28 -0800464 if (!UsesHttp3()) {
465 QuicHeaderList headers;
466 stream_->OnStreamHeadersPriority(
467 spdy::SpdyStreamPrecedence(kV3HighestPriority));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500468
renjietang2abedac2019-05-20 14:04:50 -0700469 EXPECT_CALL(*session_,
470 SendRstStream(stream_->id(), QUIC_HEADERS_TOO_LARGE, 0));
bnc446887e2019-11-27 13:08:28 -0800471 stream_->OnStreamHeaderList(false, 1 << 20, headers);
renjietang2abedac2019-05-20 14:04:50 -0700472
bncc5769502019-11-27 10:01:44 -0800473 EXPECT_THAT(stream_->stream_error(), IsStreamError(QUIC_HEADERS_TOO_LARGE));
bnc446887e2019-11-27 13:08:28 -0800474
475 return;
renjietang2abedac2019-05-20 14:04:50 -0700476 }
bnc446887e2019-11-27 13:08:28 -0800477
478 // Header list size includes 32 bytes for overhead per header field.
479 session_->set_max_inbound_header_list_size(40);
480 std::string headers =
481 HeadersFrame({std::make_pair("foo", "too long headers")});
482
483 QuicStreamFrame frame(stream_->id(), false, 0, headers);
484
485 EXPECT_CALL(
486 *connection_,
487 CloseConnection(QUIC_HEADERS_STREAM_DATA_DECOMPRESS_FAILURE,
488 MatchesRegex("Too large headers received on stream \\d+"),
489 _));
490
491 stream_->OnStreamFrame(frame);
492
493 EXPECT_TRUE(stream_->header_list().empty());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500494}
495
496TEST_P(QuicSpdyStreamTest, ProcessHeaderListWithFin) {
497 Initialize(kShouldProcessData);
498
499 size_t total_bytes = 0;
500 QuicHeaderList headers;
501 for (auto p : headers_) {
502 headers.OnHeader(p.first, p.second);
503 total_bytes += p.first.size() + p.second.size();
504 }
fayang476683a2019-07-25 12:42:16 -0700505 stream_->OnStreamHeadersPriority(
506 spdy::SpdyStreamPrecedence(kV3HighestPriority));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500507 stream_->OnStreamHeaderList(true, total_bytes, headers);
508 EXPECT_EQ("", stream_->data());
509 EXPECT_FALSE(stream_->header_list().empty());
510 EXPECT_FALSE(stream_->IsDoneReading());
renjietang6c066562019-11-04 17:05:59 -0800511 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500512}
513
bnc677451a2019-06-07 10:13:30 -0700514// A valid status code should be 3-digit integer. The first digit should be in
515// the range of [1, 5]. All the others are invalid.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500516TEST_P(QuicSpdyStreamTest, ParseHeaderStatusCode) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500517 Initialize(kShouldProcessData);
518 int status_code = 0;
519
520 // Valid status codes.
521 headers_[":status"] = "404";
522 EXPECT_TRUE(stream_->ParseHeaderStatusCode(headers_, &status_code));
523 EXPECT_EQ(404, status_code);
524
525 headers_[":status"] = "100";
526 EXPECT_TRUE(stream_->ParseHeaderStatusCode(headers_, &status_code));
527 EXPECT_EQ(100, status_code);
528
529 headers_[":status"] = "599";
530 EXPECT_TRUE(stream_->ParseHeaderStatusCode(headers_, &status_code));
531 EXPECT_EQ(599, status_code);
532
533 // Invalid status codes.
534 headers_[":status"] = "010";
535 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
536
537 headers_[":status"] = "600";
538 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
539
540 headers_[":status"] = "200 ok";
541 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
542
543 headers_[":status"] = "2000";
544 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
545
546 headers_[":status"] = "+200";
547 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
548
549 headers_[":status"] = "+20";
550 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
551
552 headers_[":status"] = "-10";
553 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
554
555 headers_[":status"] = "-100";
556 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
557
558 // Leading or trailing spaces are also invalid.
559 headers_[":status"] = " 200";
560 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
561
562 headers_[":status"] = "200 ";
563 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
564
565 headers_[":status"] = " 200 ";
566 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
567
568 headers_[":status"] = " ";
569 EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code));
570}
571
572TEST_P(QuicSpdyStreamTest, MarkHeadersConsumed) {
573 Initialize(kShouldProcessData);
574
vasilvvc48c8712019-03-11 13:38:16 -0700575 std::string body = "this is the body";
QUICHE teama6ef0a62019-03-07 20:34:33 -0500576 QuicHeaderList headers = ProcessHeaders(false, headers_);
577 EXPECT_EQ(headers, stream_->header_list());
578
579 stream_->ConsumeHeaderList();
580 EXPECT_EQ(QuicHeaderList(), stream_->header_list());
581}
582
QUICHE team396d1092019-03-20 10:21:07 -0700583TEST_P(QuicSpdyStreamTest, ProcessWrongFramesOnSpdyStream) {
renjietanga29a96a2019-10-10 12:47:50 -0700584 if (!UsesHttp3()) {
QUICHE team396d1092019-03-20 10:21:07 -0700585 return;
586 }
bnc677451a2019-06-07 10:13:30 -0700587
bnc677451a2019-06-07 10:13:30 -0700588 Initialize(kShouldProcessData);
fayang8790d292020-03-18 13:17:23 -0700589 testing::InSequence s;
QUICHE team396d1092019-03-20 10:21:07 -0700590 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
591 GoAwayFrame goaway;
592 goaway.stream_id = 0x1;
593 std::unique_ptr<char[]> buffer;
bnc46942722019-10-29 11:56:21 -0700594 QuicByteCount header_length =
595 HttpEncoder::SerializeGoAwayFrame(goaway, &buffer);
QUICHE team396d1092019-03-20 10:21:07 -0700596 std::string data = std::string(buffer.get(), header_length);
597
598 EXPECT_EQ("", stream_->data());
599 QuicHeaderList headers = ProcessHeaders(false, headers_);
600 EXPECT_EQ(headers, stream_->header_list());
601 stream_->ConsumeHeaderList();
602 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800603 quiche::QuicheStringPiece(data));
QUICHE team396d1092019-03-20 10:21:07 -0700604
bncdfabdfb2020-01-17 17:46:40 -0800605 EXPECT_CALL(*connection_,
606 CloseConnection(QUIC_HTTP_FRAME_UNEXPECTED_ON_SPDY_STREAM, _, _))
QUICHE team396d1092019-03-20 10:21:07 -0700607 .WillOnce(
608 (Invoke([this](QuicErrorCode error, const std::string& error_details,
609 ConnectionCloseBehavior connection_close_behavior) {
610 connection_->ReallyCloseConnection(error, error_details,
611 connection_close_behavior);
612 })));
ianswettdc1e7ab2019-05-03 16:10:44 -0700613 EXPECT_CALL(*connection_, SendConnectionClosePacket(_, _));
fkastenholz5d880a92019-06-21 09:01:56 -0700614 EXPECT_CALL(*session_, OnConnectionClosed(_, _))
615 .WillOnce(Invoke([this](const QuicConnectionCloseFrame& frame,
616 ConnectionCloseSource source) {
617 session_->ReallyOnConnectionClosed(frame, source);
618 }));
QUICHE team396d1092019-03-20 10:21:07 -0700619 EXPECT_CALL(*session_, SendRstStream(_, _, _));
620 EXPECT_CALL(*session_, SendRstStream(_, _, _));
621
622 stream_->OnStreamFrame(frame);
623}
624
bncdfabdfb2020-01-17 17:46:40 -0800625TEST_P(QuicSpdyStreamTest, Http3FrameError) {
626 if (!UsesHttp3()) {
627 return;
628 }
629
630 Initialize(kShouldProcessData);
631
632 // PUSH_PROMISE frame with empty payload is considered invalid.
633 std::string invalid_http3_frame = quiche::QuicheTextUtils::HexDecode("0500");
634 QuicStreamFrame stream_frame(stream_->id(), /* fin = */ false,
635 /* offset = */ 0, invalid_http3_frame);
636
637 EXPECT_CALL(*connection_, CloseConnection(QUIC_HTTP_FRAME_ERROR, _, _));
638 stream_->OnStreamFrame(stream_frame);
639}
640
641TEST_P(QuicSpdyStreamTest, UnexpectedHttp3Frame) {
642 if (!UsesHttp3()) {
643 return;
644 }
645
646 Initialize(kShouldProcessData);
647
648 // SETTINGS frame with empty payload.
649 std::string settings = quiche::QuicheTextUtils::HexDecode("0400");
650 QuicStreamFrame stream_frame(stream_->id(), /* fin = */ false,
651 /* offset = */ 0, settings);
652
653 EXPECT_CALL(*connection_,
654 CloseConnection(QUIC_HTTP_FRAME_UNEXPECTED_ON_SPDY_STREAM, _, _));
655 stream_->OnStreamFrame(stream_frame);
656}
657
QUICHE teama6ef0a62019-03-07 20:34:33 -0500658TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBody) {
659 Initialize(kShouldProcessData);
660
vasilvvc48c8712019-03-11 13:38:16 -0700661 std::string body = "this is the body";
renjietanga29a96a2019-10-10 12:47:50 -0700662 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500663
664 EXPECT_EQ("", stream_->data());
665 QuicHeaderList headers = ProcessHeaders(false, headers_);
666 EXPECT_EQ(headers, stream_->header_list());
667 stream_->ConsumeHeaderList();
668 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800669 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500670 stream_->OnStreamFrame(frame);
671 EXPECT_EQ(QuicHeaderList(), stream_->header_list());
672 EXPECT_EQ(body, stream_->data());
673}
674
675TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBodyFragments) {
vasilvvc48c8712019-03-11 13:38:16 -0700676 std::string body = "this is the body";
renjietanga29a96a2019-10-10 12:47:50 -0700677 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500678
679 for (size_t fragment_size = 1; fragment_size < data.size(); ++fragment_size) {
680 Initialize(kShouldProcessData);
681 QuicHeaderList headers = ProcessHeaders(false, headers_);
682 ASSERT_EQ(headers, stream_->header_list());
683 stream_->ConsumeHeaderList();
684 for (size_t offset = 0; offset < data.size(); offset += fragment_size) {
685 size_t remaining_data = data.size() - offset;
dmcardleba2fb7e2019-12-13 07:44:34 -0800686 quiche::QuicheStringPiece fragment(
687 data.data() + offset, std::min(fragment_size, remaining_data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500688 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false,
dmcardleba2fb7e2019-12-13 07:44:34 -0800689 offset, quiche::QuicheStringPiece(fragment));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500690 stream_->OnStreamFrame(frame);
691 }
692 ASSERT_EQ(body, stream_->data()) << "fragment_size: " << fragment_size;
693 }
694}
695
696TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBodyFragmentsSplit) {
vasilvvc48c8712019-03-11 13:38:16 -0700697 std::string body = "this is the body";
renjietanga29a96a2019-10-10 12:47:50 -0700698 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500699
700 for (size_t split_point = 1; split_point < data.size() - 1; ++split_point) {
701 Initialize(kShouldProcessData);
702 QuicHeaderList headers = ProcessHeaders(false, headers_);
703 ASSERT_EQ(headers, stream_->header_list());
704 stream_->ConsumeHeaderList();
705
dmcardleba2fb7e2019-12-13 07:44:34 -0800706 quiche::QuicheStringPiece fragment1(data.data(), split_point);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500707 QuicStreamFrame frame1(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800708 quiche::QuicheStringPiece(fragment1));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500709 stream_->OnStreamFrame(frame1);
710
dmcardleba2fb7e2019-12-13 07:44:34 -0800711 quiche::QuicheStringPiece fragment2(data.data() + split_point,
712 data.size() - split_point);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500713 QuicStreamFrame frame2(GetNthClientInitiatedBidirectionalId(0), false,
dmcardleba2fb7e2019-12-13 07:44:34 -0800714 split_point, quiche::QuicheStringPiece(fragment2));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500715 stream_->OnStreamFrame(frame2);
716
717 ASSERT_EQ(body, stream_->data()) << "split_point: " << split_point;
718 }
719}
720
721TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBodyReadv) {
722 Initialize(!kShouldProcessData);
723
vasilvvc48c8712019-03-11 13:38:16 -0700724 std::string body = "this is the body";
renjietanga29a96a2019-10-10 12:47:50 -0700725 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500726
727 ProcessHeaders(false, headers_);
728 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800729 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500730 stream_->OnStreamFrame(frame);
731 stream_->ConsumeHeaderList();
732
733 char buffer[2048];
bnc4e9283d2019-12-17 07:08:57 -0800734 ASSERT_LT(data.length(), QUICHE_ARRAYSIZE(buffer));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500735 struct iovec vec;
736 vec.iov_base = buffer;
bnc4e9283d2019-12-17 07:08:57 -0800737 vec.iov_len = QUICHE_ARRAYSIZE(buffer);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500738
739 size_t bytes_read = stream_->Readv(&vec, 1);
QUICHE team396d1092019-03-20 10:21:07 -0700740 QuicStreamPeer::CloseReadSide(stream_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500741 EXPECT_EQ(body.length(), bytes_read);
vasilvvc48c8712019-03-11 13:38:16 -0700742 EXPECT_EQ(body, std::string(buffer, bytes_read));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500743}
744
745TEST_P(QuicSpdyStreamTest, ProcessHeadersAndLargeBodySmallReadv) {
746 Initialize(kShouldProcessData);
vasilvvc48c8712019-03-11 13:38:16 -0700747 std::string body(12 * 1024, 'a');
renjietanga29a96a2019-10-10 12:47:50 -0700748 std::string data = UsesHttp3() ? DataFrame(body) : body;
bnc9f977e42019-06-07 11:36:34 -0700749
QUICHE teama6ef0a62019-03-07 20:34:33 -0500750 ProcessHeaders(false, headers_);
751 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800752 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500753 stream_->OnStreamFrame(frame);
754 stream_->ConsumeHeaderList();
755 char buffer[2048];
756 char buffer2[2048];
757 struct iovec vec[2];
758 vec[0].iov_base = buffer;
bnc4e9283d2019-12-17 07:08:57 -0800759 vec[0].iov_len = QUICHE_ARRAYSIZE(buffer);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500760 vec[1].iov_base = buffer2;
bnc4e9283d2019-12-17 07:08:57 -0800761 vec[1].iov_len = QUICHE_ARRAYSIZE(buffer2);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500762 size_t bytes_read = stream_->Readv(vec, 2);
763 EXPECT_EQ(2048u * 2, bytes_read);
vasilvvc48c8712019-03-11 13:38:16 -0700764 EXPECT_EQ(body.substr(0, 2048), std::string(buffer, 2048));
765 EXPECT_EQ(body.substr(2048, 2048), std::string(buffer2, 2048));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500766}
767
768TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBodyMarkConsumed) {
769 Initialize(!kShouldProcessData);
770
vasilvvc48c8712019-03-11 13:38:16 -0700771 std::string body = "this is the body";
renjietanga29a96a2019-10-10 12:47:50 -0700772 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500773
774 ProcessHeaders(false, headers_);
775 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800776 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500777 stream_->OnStreamFrame(frame);
778 stream_->ConsumeHeaderList();
779
780 struct iovec vec;
781
782 EXPECT_EQ(1, stream_->GetReadableRegions(&vec, 1));
783 EXPECT_EQ(body.length(), vec.iov_len);
vasilvvc48c8712019-03-11 13:38:16 -0700784 EXPECT_EQ(body, std::string(static_cast<char*>(vec.iov_base), vec.iov_len));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500785
786 stream_->MarkConsumed(body.length());
787 EXPECT_EQ(data.length(), stream_->flow_controller()->bytes_consumed());
788}
789
790TEST_P(QuicSpdyStreamTest, ProcessHeadersAndConsumeMultipleBody) {
791 Initialize(!kShouldProcessData);
vasilvvc48c8712019-03-11 13:38:16 -0700792 std::string body1 = "this is body 1";
renjietanga29a96a2019-10-10 12:47:50 -0700793 std::string data1 = UsesHttp3() ? DataFrame(body1) : body1;
vasilvvc48c8712019-03-11 13:38:16 -0700794 std::string body2 = "body 2";
renjietanga29a96a2019-10-10 12:47:50 -0700795 std::string data2 = UsesHttp3() ? DataFrame(body2) : body2;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500796
797 ProcessHeaders(false, headers_);
798 QuicStreamFrame frame1(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800799 quiche::QuicheStringPiece(data1));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500800 QuicStreamFrame frame2(GetNthClientInitiatedBidirectionalId(0), false,
dmcardleba2fb7e2019-12-13 07:44:34 -0800801 data1.length(), quiche::QuicheStringPiece(data2));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500802 stream_->OnStreamFrame(frame1);
803 stream_->OnStreamFrame(frame2);
804 stream_->ConsumeHeaderList();
805
806 stream_->MarkConsumed(body1.length() + body2.length());
807 EXPECT_EQ(data1.length() + data2.length(),
808 stream_->flow_controller()->bytes_consumed());
809}
810
811TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBodyIncrementalReadv) {
812 Initialize(!kShouldProcessData);
813
vasilvvc48c8712019-03-11 13:38:16 -0700814 std::string body = "this is the body";
renjietanga29a96a2019-10-10 12:47:50 -0700815 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500816
817 ProcessHeaders(false, headers_);
818 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800819 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500820 stream_->OnStreamFrame(frame);
821 stream_->ConsumeHeaderList();
822
823 char buffer[1];
824 struct iovec vec;
825 vec.iov_base = buffer;
bnc4e9283d2019-12-17 07:08:57 -0800826 vec.iov_len = QUICHE_ARRAYSIZE(buffer);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500827
828 for (size_t i = 0; i < body.length(); ++i) {
829 size_t bytes_read = stream_->Readv(&vec, 1);
830 ASSERT_EQ(1u, bytes_read);
831 EXPECT_EQ(body.data()[i], buffer[0]);
832 }
833}
834
835TEST_P(QuicSpdyStreamTest, ProcessHeadersUsingReadvWithMultipleIovecs) {
836 Initialize(!kShouldProcessData);
837
vasilvvc48c8712019-03-11 13:38:16 -0700838 std::string body = "this is the body";
renjietanga29a96a2019-10-10 12:47:50 -0700839 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500840
841 ProcessHeaders(false, headers_);
842 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800843 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500844 stream_->OnStreamFrame(frame);
845 stream_->ConsumeHeaderList();
846
847 char buffer1[1];
848 char buffer2[1];
849 struct iovec vec[2];
850 vec[0].iov_base = buffer1;
bnc4e9283d2019-12-17 07:08:57 -0800851 vec[0].iov_len = QUICHE_ARRAYSIZE(buffer1);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500852 vec[1].iov_base = buffer2;
bnc4e9283d2019-12-17 07:08:57 -0800853 vec[1].iov_len = QUICHE_ARRAYSIZE(buffer2);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500854
855 for (size_t i = 0; i < body.length(); i += 2) {
856 size_t bytes_read = stream_->Readv(vec, 2);
857 ASSERT_EQ(2u, bytes_read) << i;
858 ASSERT_EQ(body.data()[i], buffer1[0]) << i;
859 ASSERT_EQ(body.data()[i + 1], buffer2[0]) << i;
860 }
861}
862
bnc677451a2019-06-07 10:13:30 -0700863// Tests that we send a BLOCKED frame to the peer when we attempt to write, but
864// are flow control blocked.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500865TEST_P(QuicSpdyStreamTest, StreamFlowControlBlocked) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500866 Initialize(kShouldProcessData);
fayang8790d292020-03-18 13:17:23 -0700867 testing::InSequence seq;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500868
869 // Set a small flow control limit.
870 const uint64_t kWindow = 36;
871 QuicFlowControllerPeer::SetSendWindowOffset(stream_->flow_controller(),
872 kWindow);
873 EXPECT_EQ(kWindow, QuicFlowControllerPeer::SendWindowOffset(
874 stream_->flow_controller()));
875
876 // Try to send more data than the flow control limit allows.
877 const uint64_t kOverflow = 15;
vasilvvc48c8712019-03-11 13:38:16 -0700878 std::string body(kWindow + kOverflow, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500879
renjietanga29a96a2019-10-10 12:47:50 -0700880 const uint64_t kHeaderLength = UsesHttp3() ? 2 : 0;
881 if (UsesHttp3()) {
renjietang41a1b412020-02-27 15:05:14 -0800882 EXPECT_CALL(*session_, WritevData(_, kHeaderLength, _, NO_FIN, _, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500883 }
renjietang41a1b412020-02-27 15:05:14 -0800884 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500885 .WillOnce(Return(QuicConsumedData(kWindow - kHeaderLength, true)));
886 EXPECT_CALL(*connection_, SendControlFrame(_));
887 stream_->WriteOrBufferBody(body, false);
888
889 // Should have sent as much as possible, resulting in no send window left.
890 EXPECT_EQ(0u,
891 QuicFlowControllerPeer::SendWindowSize(stream_->flow_controller()));
892
893 // And we should have queued the overflowed data.
bncc7d9e0c2019-04-16 10:22:15 -0700894 EXPECT_EQ(kOverflow + kHeaderLength, stream_->BufferedDataBytes());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500895}
896
bnc677451a2019-06-07 10:13:30 -0700897// The flow control receive window decreases whenever we add new bytes to the
898// sequencer, whether they are consumed immediately or buffered. However we only
899// send WINDOW_UPDATE frames based on increasing number of bytes consumed.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500900TEST_P(QuicSpdyStreamTest, StreamFlowControlNoWindowUpdateIfNotConsumed) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500901 // Don't process data - it will be buffered instead.
902 Initialize(!kShouldProcessData);
903
904 // Expect no WINDOW_UPDATE frames to be sent.
905 EXPECT_CALL(*connection_, SendWindowUpdate(_, _)).Times(0);
906
907 // Set a small flow control receive window.
908 const uint64_t kWindow = 36;
909 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
910 kWindow);
911 QuicFlowControllerPeer::SetMaxReceiveWindow(stream_->flow_controller(),
912 kWindow);
913 EXPECT_EQ(kWindow, QuicFlowControllerPeer::ReceiveWindowOffset(
914 stream_->flow_controller()));
915
916 // Stream receives enough data to fill a fraction of the receive window.
vasilvvc48c8712019-03-11 13:38:16 -0700917 std::string body(kWindow / 3, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500918 QuicByteCount header_length = 0;
vasilvvc48c8712019-03-11 13:38:16 -0700919 std::string data;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500920
renjietanga29a96a2019-10-10 12:47:50 -0700921 if (UsesHttp3()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500922 std::unique_ptr<char[]> buffer;
bnc46942722019-10-29 11:56:21 -0700923 header_length =
924 HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer);
vasilvvc48c8712019-03-11 13:38:16 -0700925 std::string header = std::string(buffer.get(), header_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500926 data = header + body;
927 } else {
928 data = body;
929 }
930
931 ProcessHeaders(false, headers_);
932
933 QuicStreamFrame frame1(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800934 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500935 stream_->OnStreamFrame(frame1);
936 EXPECT_EQ(
937 kWindow - (kWindow / 3) - header_length,
938 QuicFlowControllerPeer::ReceiveWindowSize(stream_->flow_controller()));
939
940 // Now receive another frame which results in the receive window being over
941 // half full. This should all be buffered, decreasing the receive window but
942 // not sending WINDOW_UPDATE.
943 QuicStreamFrame frame2(GetNthClientInitiatedBidirectionalId(0), false,
dmcardleba2fb7e2019-12-13 07:44:34 -0800944 kWindow / 3 + header_length,
945 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500946 stream_->OnStreamFrame(frame2);
947 EXPECT_EQ(
948 kWindow - (2 * kWindow / 3) - 2 * header_length,
949 QuicFlowControllerPeer::ReceiveWindowSize(stream_->flow_controller()));
950}
951
bnc677451a2019-06-07 10:13:30 -0700952// Tests that on receipt of data, the stream updates its receive window offset
953// appropriately, and sends WINDOW_UPDATE frames when its receive window drops
954// too low.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500955TEST_P(QuicSpdyStreamTest, StreamFlowControlWindowUpdate) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500956 Initialize(kShouldProcessData);
957
958 // Set a small flow control limit.
959 const uint64_t kWindow = 36;
960 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
961 kWindow);
962 QuicFlowControllerPeer::SetMaxReceiveWindow(stream_->flow_controller(),
963 kWindow);
964 EXPECT_EQ(kWindow, QuicFlowControllerPeer::ReceiveWindowOffset(
965 stream_->flow_controller()));
966
967 // Stream receives enough data to fill a fraction of the receive window.
vasilvvc48c8712019-03-11 13:38:16 -0700968 std::string body(kWindow / 3, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500969 QuicByteCount header_length = 0;
vasilvvc48c8712019-03-11 13:38:16 -0700970 std::string data;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500971
renjietanga29a96a2019-10-10 12:47:50 -0700972 if (UsesHttp3()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500973 std::unique_ptr<char[]> buffer;
bnc46942722019-10-29 11:56:21 -0700974 header_length =
975 HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer);
vasilvvc48c8712019-03-11 13:38:16 -0700976 std::string header = std::string(buffer.get(), header_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500977 data = header + body;
978 } else {
979 data = body;
980 }
981
982 ProcessHeaders(false, headers_);
983 stream_->ConsumeHeaderList();
984
985 QuicStreamFrame frame1(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -0800986 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500987 stream_->OnStreamFrame(frame1);
988 EXPECT_EQ(
989 kWindow - (kWindow / 3) - header_length,
990 QuicFlowControllerPeer::ReceiveWindowSize(stream_->flow_controller()));
991
992 // Now receive another frame which results in the receive window being over
993 // half full. This will trigger the stream to increase its receive window
994 // offset and send a WINDOW_UPDATE. The result will be again an available
995 // window of kWindow bytes.
996 QuicStreamFrame frame2(GetNthClientInitiatedBidirectionalId(0), false,
dmcardleba2fb7e2019-12-13 07:44:34 -0800997 kWindow / 3 + header_length,
998 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500999 EXPECT_CALL(*connection_, SendControlFrame(_));
1000 stream_->OnStreamFrame(frame2);
1001 EXPECT_EQ(kWindow, QuicFlowControllerPeer::ReceiveWindowSize(
1002 stream_->flow_controller()));
1003}
1004
bnc677451a2019-06-07 10:13:30 -07001005// Tests that on receipt of data, the connection updates its receive window
1006// offset appropriately, and sends WINDOW_UPDATE frames when its receive window
1007// drops too low.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001008TEST_P(QuicSpdyStreamTest, ConnectionFlowControlWindowUpdate) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001009 Initialize(kShouldProcessData);
1010
1011 // Set a small flow control limit for streams and connection.
1012 const uint64_t kWindow = 36;
1013 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
1014 kWindow);
1015 QuicFlowControllerPeer::SetMaxReceiveWindow(stream_->flow_controller(),
1016 kWindow);
1017 QuicFlowControllerPeer::SetReceiveWindowOffset(stream2_->flow_controller(),
1018 kWindow);
1019 QuicFlowControllerPeer::SetMaxReceiveWindow(stream2_->flow_controller(),
1020 kWindow);
1021 QuicFlowControllerPeer::SetReceiveWindowOffset(session_->flow_controller(),
1022 kWindow);
1023 QuicFlowControllerPeer::SetMaxReceiveWindow(session_->flow_controller(),
1024 kWindow);
1025
1026 // Supply headers to both streams so that they are happy to receive data.
1027 auto headers = AsHeaderList(headers_);
1028 stream_->OnStreamHeaderList(false, headers.uncompressed_header_bytes(),
1029 headers);
1030 stream_->ConsumeHeaderList();
1031 stream2_->OnStreamHeaderList(false, headers.uncompressed_header_bytes(),
1032 headers);
1033 stream2_->ConsumeHeaderList();
1034
1035 // Each stream gets a quarter window of data. This should not trigger a
1036 // WINDOW_UPDATE for either stream, nor for the connection.
1037 QuicByteCount header_length = 0;
vasilvvc48c8712019-03-11 13:38:16 -07001038 std::string body;
1039 std::string data;
1040 std::string data2;
1041 std::string body2(1, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001042
renjietanga29a96a2019-10-10 12:47:50 -07001043 if (UsesHttp3()) {
vasilvvc48c8712019-03-11 13:38:16 -07001044 body = std::string(kWindow / 4 - 2, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001045 std::unique_ptr<char[]> buffer;
bnc46942722019-10-29 11:56:21 -07001046 header_length =
1047 HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer);
vasilvvc48c8712019-03-11 13:38:16 -07001048 std::string header = std::string(buffer.get(), header_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001049 data = header + body;
1050 std::unique_ptr<char[]> buffer2;
1051 QuicByteCount header_length2 =
bnc46942722019-10-29 11:56:21 -07001052 HttpEncoder::SerializeDataFrameHeader(body2.length(), &buffer2);
vasilvvc48c8712019-03-11 13:38:16 -07001053 std::string header2 = std::string(buffer2.get(), header_length2);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001054 data2 = header2 + body2;
1055 } else {
vasilvvc48c8712019-03-11 13:38:16 -07001056 body = std::string(kWindow / 4, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001057 data = body;
1058 data2 = body2;
1059 }
1060
1061 QuicStreamFrame frame1(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -08001062 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001063 stream_->OnStreamFrame(frame1);
1064 QuicStreamFrame frame2(GetNthClientInitiatedBidirectionalId(1), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -08001065 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001066 stream2_->OnStreamFrame(frame2);
1067
1068 // Now receive a further single byte on one stream - again this does not
1069 // trigger a stream WINDOW_UPDATE, but now the connection flow control window
1070 // is over half full and thus a connection WINDOW_UPDATE is sent.
1071 EXPECT_CALL(*connection_, SendControlFrame(_));
1072 QuicStreamFrame frame3(GetNthClientInitiatedBidirectionalId(0), false,
dmcardleba2fb7e2019-12-13 07:44:34 -08001073 body.length() + header_length,
1074 quiche::QuicheStringPiece(data2));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001075 stream_->OnStreamFrame(frame3);
1076}
1077
bnc677451a2019-06-07 10:13:30 -07001078// Tests that on if the peer sends too much data (i.e. violates the flow control
1079// protocol), then we terminate the connection.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001080TEST_P(QuicSpdyStreamTest, StreamFlowControlViolation) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001081 // Stream should not process data, so that data gets buffered in the
1082 // sequencer, triggering flow control limits.
1083 Initialize(!kShouldProcessData);
1084
1085 // Set a small flow control limit.
1086 const uint64_t kWindow = 50;
1087 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
1088 kWindow);
1089
1090 ProcessHeaders(false, headers_);
1091
1092 // Receive data to overflow the window, violating flow control.
vasilvvc48c8712019-03-11 13:38:16 -07001093 std::string body(kWindow + 1, 'a');
renjietanga29a96a2019-10-10 12:47:50 -07001094 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001095 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -08001096 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001097 EXPECT_CALL(*connection_,
1098 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _));
1099 stream_->OnStreamFrame(frame);
1100}
1101
1102TEST_P(QuicSpdyStreamTest, TestHandlingQuicRstStreamNoError) {
1103 Initialize(kShouldProcessData);
1104 ProcessHeaders(false, headers_);
1105
1106 stream_->OnStreamReset(QuicRstStreamFrame(
1107 kInvalidControlFrameId, stream_->id(), QUIC_STREAM_NO_ERROR, 0));
1108 EXPECT_TRUE(stream_->write_side_closed());
1109 EXPECT_FALSE(stream_->reading_stopped());
1110}
1111
bnc677451a2019-06-07 10:13:30 -07001112// Tests that on if the peer sends too much data (i.e. violates the flow control
1113// protocol), at the connection level (rather than the stream level) then we
1114// terminate the connection.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001115TEST_P(QuicSpdyStreamTest, ConnectionFlowControlViolation) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001116 // Stream should not process data, so that data gets buffered in the
1117 // sequencer, triggering flow control limits.
1118 Initialize(!kShouldProcessData);
1119
1120 // Set a small flow control window on streams, and connection.
1121 const uint64_t kStreamWindow = 50;
1122 const uint64_t kConnectionWindow = 10;
1123 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
1124 kStreamWindow);
1125 QuicFlowControllerPeer::SetReceiveWindowOffset(session_->flow_controller(),
1126 kConnectionWindow);
1127
1128 ProcessHeaders(false, headers_);
1129
1130 // Send enough data to overflow the connection level flow control window.
vasilvvc48c8712019-03-11 13:38:16 -07001131 std::string body(kConnectionWindow + 1, 'a');
renjietanga29a96a2019-10-10 12:47:50 -07001132 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001133
1134 EXPECT_LT(data.size(), kStreamWindow);
1135 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0,
dmcardleba2fb7e2019-12-13 07:44:34 -08001136 quiche::QuicheStringPiece(data));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001137
1138 EXPECT_CALL(*connection_,
1139 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _));
1140 stream_->OnStreamFrame(frame);
1141}
1142
bnc677451a2019-06-07 10:13:30 -07001143// An attempt to write a FIN with no data should not be flow control blocked,
1144// even if the send window is 0.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001145TEST_P(QuicSpdyStreamTest, StreamFlowControlFinNotBlocked) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001146 Initialize(kShouldProcessData);
1147
1148 // Set a flow control limit of zero.
1149 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(), 0);
1150 EXPECT_EQ(0u, QuicFlowControllerPeer::ReceiveWindowOffset(
1151 stream_->flow_controller()));
1152
1153 // Send a frame with a FIN but no data. This should not be blocked.
vasilvvc48c8712019-03-11 13:38:16 -07001154 std::string body = "";
QUICHE teama6ef0a62019-03-07 20:34:33 -05001155 bool fin = true;
1156
1157 EXPECT_CALL(*connection_,
1158 SendBlocked(GetNthClientInitiatedBidirectionalId(0)))
1159 .Times(0);
renjietang41a1b412020-02-27 15:05:14 -08001160 EXPECT_CALL(*session_, WritevData(_, 0, _, FIN, _, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001161
1162 stream_->WriteOrBufferBody(body, fin);
1163}
1164
bnc677451a2019-06-07 10:13:30 -07001165// Test that receiving trailing headers from the peer via OnStreamHeaderList()
1166// works, and can be read from the stream and consumed.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001167TEST_P(QuicSpdyStreamTest, ReceivingTrailersViaHeaderList) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001168 Initialize(kShouldProcessData);
1169
1170 // Receive initial headers.
1171 size_t total_bytes = 0;
1172 QuicHeaderList headers;
1173 for (const auto& p : headers_) {
1174 headers.OnHeader(p.first, p.second);
1175 total_bytes += p.first.size() + p.second.size();
1176 }
1177
fayang476683a2019-07-25 12:42:16 -07001178 stream_->OnStreamHeadersPriority(
1179 spdy::SpdyStreamPrecedence(kV3HighestPriority));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001180 stream_->OnStreamHeaderList(/*fin=*/false, total_bytes, headers);
1181 stream_->ConsumeHeaderList();
1182
1183 // Receive trailing headers.
1184 SpdyHeaderBlock trailers_block;
1185 trailers_block["key1"] = "value1";
1186 trailers_block["key2"] = "value2";
1187 trailers_block["key3"] = "value3";
1188 SpdyHeaderBlock trailers_block_with_final_offset = trailers_block.Clone();
renjietange47e15f2019-10-29 14:19:30 -07001189 if (!UsesHttp3()) {
renjietang2abedac2019-05-20 14:04:50 -07001190 // :final-offset pseudo-header is only added if trailers are sent
1191 // on the headers stream.
1192 trailers_block_with_final_offset[kFinalOffsetHeaderKey] = "0";
1193 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001194 total_bytes = 0;
1195 QuicHeaderList trailers;
1196 for (const auto& p : trailers_block_with_final_offset) {
1197 trailers.OnHeader(p.first, p.second);
1198 total_bytes += p.first.size() + p.second.size();
1199 }
1200 stream_->OnStreamHeaderList(/*fin=*/true, total_bytes, trailers);
1201
1202 // The trailers should be decompressed, and readable from the stream.
1203 EXPECT_TRUE(stream_->trailers_decompressed());
1204 EXPECT_EQ(trailers_block, stream_->received_trailers());
1205
1206 // IsDoneReading() returns false until trailers marked consumed.
1207 EXPECT_FALSE(stream_->IsDoneReading());
1208 stream_->MarkTrailersConsumed();
1209 EXPECT_TRUE(stream_->IsDoneReading());
1210}
1211
bnc677451a2019-06-07 10:13:30 -07001212// Test that when receiving trailing headers with an offset before response
1213// body, stream is closed at the right offset.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001214TEST_P(QuicSpdyStreamTest, ReceivingTrailersWithOffset) {
renjietang2abedac2019-05-20 14:04:50 -07001215 // kFinalOffsetHeaderKey is not used when HEADERS are sent on the
1216 // request/response stream.
renjietange47e15f2019-10-29 14:19:30 -07001217 if (UsesHttp3()) {
renjietang2abedac2019-05-20 14:04:50 -07001218 return;
1219 }
1220
bnc677451a2019-06-07 10:13:30 -07001221 Initialize(kShouldProcessData);
1222
QUICHE teama6ef0a62019-03-07 20:34:33 -05001223 // Receive initial headers.
1224 QuicHeaderList headers = ProcessHeaders(false, headers_);
1225 stream_->ConsumeHeaderList();
1226
vasilvvc48c8712019-03-11 13:38:16 -07001227 const std::string body = "this is the body";
renjietanga29a96a2019-10-10 12:47:50 -07001228 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001229
1230 // Receive trailing headers.
1231 SpdyHeaderBlock trailers_block;
1232 trailers_block["key1"] = "value1";
1233 trailers_block["key2"] = "value2";
1234 trailers_block["key3"] = "value3";
1235 trailers_block[kFinalOffsetHeaderKey] =
dmcardleba2fb7e2019-12-13 07:44:34 -08001236 quiche::QuicheTextUtils::Uint64ToString(data.size());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001237
1238 QuicHeaderList trailers = ProcessHeaders(true, trailers_block);
1239
1240 // The trailers should be decompressed, and readable from the stream.
1241 EXPECT_TRUE(stream_->trailers_decompressed());
1242
1243 // The final offset trailer will be consumed by QUIC.
1244 trailers_block.erase(kFinalOffsetHeaderKey);
1245 EXPECT_EQ(trailers_block, stream_->received_trailers());
1246
1247 // Consuming the trailers erases them from the stream.
1248 stream_->MarkTrailersConsumed();
1249 EXPECT_TRUE(stream_->FinishedReadingTrailers());
1250
1251 EXPECT_FALSE(stream_->IsDoneReading());
1252 // Receive and consume body.
1253 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), /*fin=*/false,
1254 0, data);
1255 stream_->OnStreamFrame(frame);
1256 EXPECT_EQ(body, stream_->data());
1257 EXPECT_TRUE(stream_->IsDoneReading());
1258}
1259
bnc677451a2019-06-07 10:13:30 -07001260// Test that receiving trailers without a final offset field is an error.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001261TEST_P(QuicSpdyStreamTest, ReceivingTrailersWithoutOffset) {
renjietang2abedac2019-05-20 14:04:50 -07001262 // kFinalOffsetHeaderKey is not used when HEADERS are sent on the
1263 // request/response stream.
renjietange47e15f2019-10-29 14:19:30 -07001264 if (UsesHttp3()) {
renjietang2abedac2019-05-20 14:04:50 -07001265 return;
1266 }
1267
bnc677451a2019-06-07 10:13:30 -07001268 Initialize(kShouldProcessData);
1269
QUICHE teama6ef0a62019-03-07 20:34:33 -05001270 // Receive initial headers.
1271 ProcessHeaders(false, headers_);
1272 stream_->ConsumeHeaderList();
1273
1274 // Receive trailing headers, without kFinalOffsetHeaderKey.
1275 SpdyHeaderBlock trailers_block;
1276 trailers_block["key1"] = "value1";
1277 trailers_block["key2"] = "value2";
1278 trailers_block["key3"] = "value3";
1279 auto trailers = AsHeaderList(trailers_block);
1280
1281 // Verify that the trailers block didn't contain a final offset.
1282 EXPECT_EQ("", trailers_block[kFinalOffsetHeaderKey].as_string());
1283
1284 // Receipt of the malformed trailers will close the connection.
1285 EXPECT_CALL(*connection_,
1286 CloseConnection(QUIC_INVALID_HEADERS_STREAM_DATA, _, _))
1287 .Times(1);
1288 stream_->OnStreamHeaderList(/*fin=*/true,
1289 trailers.uncompressed_header_bytes(), trailers);
1290}
1291
bnc677451a2019-06-07 10:13:30 -07001292// Test that received Trailers must always have the FIN set.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001293TEST_P(QuicSpdyStreamTest, ReceivingTrailersWithoutFin) {
renjietang2abedac2019-05-20 14:04:50 -07001294 // In IETF QUIC, there is no such thing as FIN flag on HTTP/3 frames like the
1295 // HEADERS frame.
renjietange47e15f2019-10-29 14:19:30 -07001296 if (UsesHttp3()) {
renjietang2abedac2019-05-20 14:04:50 -07001297 return;
1298 }
1299
bnc677451a2019-06-07 10:13:30 -07001300 Initialize(kShouldProcessData);
1301
QUICHE teama6ef0a62019-03-07 20:34:33 -05001302 // Receive initial headers.
1303 auto headers = AsHeaderList(headers_);
1304 stream_->OnStreamHeaderList(/*fin=*/false,
1305 headers.uncompressed_header_bytes(), headers);
1306 stream_->ConsumeHeaderList();
1307
1308 // Receive trailing headers with FIN deliberately set to false.
1309 SpdyHeaderBlock trailers_block;
1310 trailers_block["foo"] = "bar";
1311 auto trailers = AsHeaderList(trailers_block);
1312
1313 EXPECT_CALL(*connection_,
1314 CloseConnection(QUIC_INVALID_HEADERS_STREAM_DATA, _, _))
1315 .Times(1);
1316 stream_->OnStreamHeaderList(/*fin=*/false,
1317 trailers.uncompressed_header_bytes(), trailers);
1318}
1319
1320TEST_P(QuicSpdyStreamTest, ReceivingTrailersAfterHeadersWithFin) {
1321 // If headers are received with a FIN, no trailers should then arrive.
1322 Initialize(kShouldProcessData);
1323
bnc8d041302019-06-10 10:19:04 -07001324 // If HEADERS frames are sent on the request/response stream, then the
1325 // sequencer will signal an error if any stream data arrives after a FIN,
1326 // so QuicSpdyStream does not need to.
renjietange47e15f2019-10-29 14:19:30 -07001327 if (UsesHttp3()) {
bnc8d041302019-06-10 10:19:04 -07001328 return;
1329 }
1330
QUICHE teama6ef0a62019-03-07 20:34:33 -05001331 // Receive initial headers with FIN set.
1332 ProcessHeaders(true, headers_);
1333 stream_->ConsumeHeaderList();
1334
1335 // Receive trailing headers after FIN already received.
1336 SpdyHeaderBlock trailers_block;
1337 trailers_block["foo"] = "bar";
1338 EXPECT_CALL(*connection_,
1339 CloseConnection(QUIC_INVALID_HEADERS_STREAM_DATA, _, _))
1340 .Times(1);
1341 ProcessHeaders(true, trailers_block);
1342}
1343
bnc677451a2019-06-07 10:13:30 -07001344// If body data are received with a FIN, no trailers should then arrive.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001345TEST_P(QuicSpdyStreamTest, ReceivingTrailersAfterBodyWithFin) {
renjietang2abedac2019-05-20 14:04:50 -07001346 // If HEADERS frames are sent on the request/response stream,
1347 // then the sequencer will block them from reaching QuicSpdyStream
1348 // after the stream is closed.
renjietange47e15f2019-10-29 14:19:30 -07001349 if (UsesHttp3()) {
renjietang2abedac2019-05-20 14:04:50 -07001350 return;
1351 }
1352
bnc677451a2019-06-07 10:13:30 -07001353 Initialize(kShouldProcessData);
1354
QUICHE teama6ef0a62019-03-07 20:34:33 -05001355 // Receive initial headers without FIN set.
1356 ProcessHeaders(false, headers_);
1357 stream_->ConsumeHeaderList();
1358
1359 // Receive body data, with FIN.
1360 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), /*fin=*/true,
1361 0, "body");
1362 stream_->OnStreamFrame(frame);
1363
1364 // Receive trailing headers after FIN already received.
1365 SpdyHeaderBlock trailers_block;
1366 trailers_block["foo"] = "bar";
1367 EXPECT_CALL(*connection_,
1368 CloseConnection(QUIC_INVALID_HEADERS_STREAM_DATA, _, _))
1369 .Times(1);
1370 ProcessHeaders(true, trailers_block);
1371}
1372
1373TEST_P(QuicSpdyStreamTest, ClosingStreamWithNoTrailers) {
1374 // Verify that a stream receiving headers, body, and no trailers is correctly
1375 // marked as done reading on consumption of headers and body.
1376 Initialize(kShouldProcessData);
1377
1378 // Receive and consume initial headers with FIN not set.
1379 auto h = AsHeaderList(headers_);
1380 stream_->OnStreamHeaderList(/*fin=*/false, h.uncompressed_header_bytes(), h);
1381 stream_->ConsumeHeaderList();
1382
1383 // Receive and consume body with FIN set, and no trailers.
vasilvvc48c8712019-03-11 13:38:16 -07001384 std::string body(1024, 'x');
renjietanga29a96a2019-10-10 12:47:50 -07001385 std::string data = UsesHttp3() ? DataFrame(body) : body;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001386
1387 QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), /*fin=*/true,
1388 0, data);
1389 stream_->OnStreamFrame(frame);
1390
1391 EXPECT_TRUE(stream_->IsDoneReading());
1392}
1393
bnc677451a2019-06-07 10:13:30 -07001394// Test that writing trailers will send a FIN, as Trailers are the last thing to
1395// be sent on a stream.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001396TEST_P(QuicSpdyStreamTest, WritingTrailersSendsAFin) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001397 Initialize(kShouldProcessData);
1398
renjietange47e15f2019-10-29 14:19:30 -07001399 if (UsesHttp3()) {
renjietang2abedac2019-05-20 14:04:50 -07001400 // In this case, TestStream::WriteHeadersImpl() does not prevent writes.
bnccd3c4b52020-01-10 11:13:18 -08001401 // Four writes on the request stream: HEADERS frame header and payload both
1402 // for headers and trailers.
renjietang41a1b412020-02-27 15:05:14 -08001403 EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)).Times(4);
renjietang2abedac2019-05-20 14:04:50 -07001404 }
1405
QUICHE teama6ef0a62019-03-07 20:34:33 -05001406 // Write the initial headers, without a FIN.
1407 EXPECT_CALL(*stream_, WriteHeadersMock(false));
1408 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr);
1409
1410 // Writing trailers implicitly sends a FIN.
1411 SpdyHeaderBlock trailers;
1412 trailers["trailer key"] = "trailer value";
1413 EXPECT_CALL(*stream_, WriteHeadersMock(true));
1414 stream_->WriteTrailers(std::move(trailers), nullptr);
1415 EXPECT_TRUE(stream_->fin_sent());
1416}
1417
bncb8abf382020-01-30 11:26:12 -08001418TEST_P(QuicSpdyStreamTest, DoNotSendPriorityUpdateWithDefaultUrgency) {
1419 if (!UsesHttp3()) {
1420 return;
1421 }
1422
1423 InitializeWithPerspective(kShouldProcessData, Perspective::IS_CLIENT);
1424
1425 // Four writes on the request stream: HEADERS frame header and payload both
1426 // for headers and trailers.
renjietang41a1b412020-02-27 15:05:14 -08001427 EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)).Times(4);
bncb8abf382020-01-30 11:26:12 -08001428
bnc5f202512020-02-01 18:43:02 -08001429 // No PRIORITY_UPDATE frames on the control stream,
1430 // because the stream has default priority.
bncb8abf382020-01-30 11:26:12 -08001431 auto send_control_stream =
1432 QuicSpdySessionPeer::GetSendControlStream(session_.get());
renjietang41a1b412020-02-27 15:05:14 -08001433 EXPECT_CALL(*session_, WritevData(send_control_stream->id(), _, _, _, _, _))
bncb8abf382020-01-30 11:26:12 -08001434 .Times(0);
1435
1436 // Write the initial headers, without a FIN.
1437 EXPECT_CALL(*stream_, WriteHeadersMock(false));
1438 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr);
1439
1440 // Writing trailers implicitly sends a FIN.
1441 SpdyHeaderBlock trailers;
1442 trailers["trailer key"] = "trailer value";
1443 EXPECT_CALL(*stream_, WriteHeadersMock(true));
1444 stream_->WriteTrailers(std::move(trailers), nullptr);
1445 EXPECT_TRUE(stream_->fin_sent());
1446}
1447
bnccf09f952020-01-30 17:35:59 -08001448TEST_P(QuicSpdyStreamTest, ChangePriority) {
1449 if (!UsesHttp3()) {
1450 return;
1451 }
1452
1453 InitializeWithPerspective(kShouldProcessData, Perspective::IS_CLIENT);
1454
1455 // Two writes on the request stream: HEADERS frame header and payload.
renjietang41a1b412020-02-27 15:05:14 -08001456 EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)).Times(2);
bnccf09f952020-01-30 17:35:59 -08001457 EXPECT_CALL(*stream_, WriteHeadersMock(false));
bnc5f202512020-02-01 18:43:02 -08001458 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr);
1459
bnccf09f952020-01-30 17:35:59 -08001460 // PRIORITY_UPDATE frame on the control stream.
1461 auto send_control_stream =
1462 QuicSpdySessionPeer::GetSendControlStream(session_.get());
renjietang41a1b412020-02-27 15:05:14 -08001463 EXPECT_CALL(*session_, WritevData(send_control_stream->id(), _, _, _, _, _));
bnccf09f952020-01-30 17:35:59 -08001464 stream_->SetPriority(spdy::SpdyStreamPrecedence(kV3HighestPriority));
1465}
1466
1467TEST_P(QuicSpdyStreamTest, ChangePriorityBeforeWritingHeaders) {
1468 if (!UsesHttp3()) {
1469 return;
1470 }
1471
1472 InitializeWithPerspective(kShouldProcessData, Perspective::IS_CLIENT);
1473
1474 // PRIORITY_UPDATE frame sent on the control stream as soon as SetPriority()
1475 // is called, before HEADERS frame is sent.
1476 auto send_control_stream =
1477 QuicSpdySessionPeer::GetSendControlStream(session_.get());
renjietang41a1b412020-02-27 15:05:14 -08001478 EXPECT_CALL(*session_, WritevData(send_control_stream->id(), _, _, _, _, _));
bnccf09f952020-01-30 17:35:59 -08001479
1480 stream_->SetPriority(spdy::SpdyStreamPrecedence(kV3HighestPriority));
1481 testing::Mock::VerifyAndClearExpectations(session_.get());
1482
1483 // Two writes on the request stream: HEADERS frame header and payload.
1484 // PRIORITY_UPDATE frame is not sent this time, because one is already sent.
renjietang41a1b412020-02-27 15:05:14 -08001485 EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)).Times(2);
bnccf09f952020-01-30 17:35:59 -08001486 EXPECT_CALL(*stream_, WriteHeadersMock(true));
1487 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/true, nullptr);
1488}
1489
bnc677451a2019-06-07 10:13:30 -07001490// Test that when writing trailers, the trailers that are actually sent to the
1491// peer contain the final offset field indicating last byte of data.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001492TEST_P(QuicSpdyStreamTest, WritingTrailersFinalOffset) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001493 Initialize(kShouldProcessData);
1494
renjietange47e15f2019-10-29 14:19:30 -07001495 if (UsesHttp3()) {
renjietang2abedac2019-05-20 14:04:50 -07001496 // In this case, TestStream::WriteHeadersImpl() does not prevent writes.
bnccd3c4b52020-01-10 11:13:18 -08001497 // HEADERS frame header and payload on the request stream.
renjietang41a1b412020-02-27 15:05:14 -08001498 EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)).Times(2);
renjietang2abedac2019-05-20 14:04:50 -07001499 }
1500
QUICHE teama6ef0a62019-03-07 20:34:33 -05001501 // Write the initial headers.
1502 EXPECT_CALL(*stream_, WriteHeadersMock(false));
1503 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr);
1504
1505 // Write non-zero body data to force a non-zero final offset.
renjietang41a1b412020-02-27 15:05:14 -08001506 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1));
vasilvvc48c8712019-03-11 13:38:16 -07001507 std::string body(1024, 'x'); // 1 kB
QUICHE teama6ef0a62019-03-07 20:34:33 -05001508 QuicByteCount header_length = 0;
renjietanga29a96a2019-10-10 12:47:50 -07001509 if (UsesHttp3()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001510 std::unique_ptr<char[]> buf;
bnc46942722019-10-29 11:56:21 -07001511 header_length = HttpEncoder::SerializeDataFrameHeader(body.length(), &buf);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001512 }
1513
1514 stream_->WriteOrBufferBody(body, false);
1515
1516 // The final offset field in the trailing headers is populated with the
1517 // number of body bytes written (including queued bytes).
1518 SpdyHeaderBlock trailers;
1519 trailers["trailer key"] = "trailer value";
renjietang2abedac2019-05-20 14:04:50 -07001520
1521 SpdyHeaderBlock expected_trailers(trailers.Clone());
1522 // :final-offset pseudo-header is only added if trailers are sent
1523 // on the headers stream.
renjietange47e15f2019-10-29 14:19:30 -07001524 if (!UsesHttp3()) {
renjietang2abedac2019-05-20 14:04:50 -07001525 expected_trailers[kFinalOffsetHeaderKey] =
dmcardleba2fb7e2019-12-13 07:44:34 -08001526 quiche::QuicheTextUtils::Uint64ToString(body.length() + header_length);
renjietang2abedac2019-05-20 14:04:50 -07001527 }
1528
QUICHE teama6ef0a62019-03-07 20:34:33 -05001529 EXPECT_CALL(*stream_, WriteHeadersMock(true));
1530 stream_->WriteTrailers(std::move(trailers), nullptr);
renjietang2abedac2019-05-20 14:04:50 -07001531 EXPECT_EQ(expected_trailers, stream_->saved_headers());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001532}
1533
bnc677451a2019-06-07 10:13:30 -07001534// Test that if trailers are written after all other data has been written
1535// (headers and body), that this closes the stream for writing.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001536TEST_P(QuicSpdyStreamTest, WritingTrailersClosesWriteSide) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001537 Initialize(kShouldProcessData);
1538
renjietang2abedac2019-05-20 14:04:50 -07001539 // Expect data being written on the stream. In addition to that, headers are
1540 // also written on the stream in case of IETF QUIC.
renjietang41a1b412020-02-27 15:05:14 -08001541 EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _))
renjietang2abedac2019-05-20 14:04:50 -07001542 .Times(AtLeast(1));
1543
QUICHE teama6ef0a62019-03-07 20:34:33 -05001544 // Write the initial headers.
1545 EXPECT_CALL(*stream_, WriteHeadersMock(false));
1546 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr);
1547
1548 // Write non-zero body data.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001549 const int kBodySize = 1 * 1024; // 1 kB
vasilvvc48c8712019-03-11 13:38:16 -07001550 stream_->WriteOrBufferBody(std::string(kBodySize, 'x'), false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001551 EXPECT_EQ(0u, stream_->BufferedDataBytes());
1552
1553 // Headers and body have been fully written, there is no queued data. Writing
1554 // trailers marks the end of this stream, and thus the write side is closed.
1555 EXPECT_CALL(*stream_, WriteHeadersMock(true));
1556 stream_->WriteTrailers(SpdyHeaderBlock(), nullptr);
1557 EXPECT_TRUE(stream_->write_side_closed());
1558}
1559
bnc677451a2019-06-07 10:13:30 -07001560// Test that the stream is not closed for writing when trailers are sent while
1561// there are still body bytes queued.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001562TEST_P(QuicSpdyStreamTest, WritingTrailersWithQueuedBytes) {
renjietang2abedac2019-05-20 14:04:50 -07001563 // This test exercises sending trailers on the headers stream while data is
1564 // still queued on the response/request stream. In IETF QUIC, data and
1565 // trailers are sent on the same stream, so this test does not apply.
renjietange47e15f2019-10-29 14:19:30 -07001566 if (UsesHttp3()) {
renjietang2abedac2019-05-20 14:04:50 -07001567 return;
1568 }
1569
QUICHE teama6ef0a62019-03-07 20:34:33 -05001570 testing::InSequence seq;
1571 Initialize(kShouldProcessData);
1572
1573 // Write the initial headers.
1574 EXPECT_CALL(*stream_, WriteHeadersMock(false));
1575 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr);
1576
1577 // Write non-zero body data, but only consume partially, ensuring queueing.
1578 const int kBodySize = 1 * 1024; // 1 kB
renjietanga29a96a2019-10-10 12:47:50 -07001579 if (UsesHttp3()) {
renjietang41a1b412020-02-27 15:05:14 -08001580 EXPECT_CALL(*session_, WritevData(_, 3, _, NO_FIN, _, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001581 }
renjietang41a1b412020-02-27 15:05:14 -08001582 EXPECT_CALL(*session_, WritevData(_, kBodySize, _, NO_FIN, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001583 .WillOnce(Return(QuicConsumedData(kBodySize - 1, false)));
vasilvvc48c8712019-03-11 13:38:16 -07001584 stream_->WriteOrBufferBody(std::string(kBodySize, 'x'), false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001585 EXPECT_EQ(1u, stream_->BufferedDataBytes());
1586
1587 // Writing trailers will send a FIN, but not close the write side of the
1588 // stream as there are queued bytes.
1589 EXPECT_CALL(*stream_, WriteHeadersMock(true));
1590 stream_->WriteTrailers(SpdyHeaderBlock(), nullptr);
1591 EXPECT_TRUE(stream_->fin_sent());
1592 EXPECT_FALSE(stream_->write_side_closed());
1593
1594 // Writing the queued bytes will close the write side of the stream.
renjietang41a1b412020-02-27 15:05:14 -08001595 EXPECT_CALL(*session_, WritevData(_, 1, _, NO_FIN, _, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001596 stream_->OnCanWrite();
1597 EXPECT_TRUE(stream_->write_side_closed());
1598}
1599
bnc677451a2019-06-07 10:13:30 -07001600// Test that it is not possible to write Trailers after a FIN has been sent.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001601TEST_P(QuicSpdyStreamTest, WritingTrailersAfterFIN) {
renjietang2abedac2019-05-20 14:04:50 -07001602 // In IETF QUIC, there is no such thing as FIN flag on HTTP/3 frames like the
bnc01de0b82020-02-25 11:36:51 -08001603 // HEADERS frame.
1604 if (UsesHttp3()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001605 return;
1606 }
1607
QUICHE teama6ef0a62019-03-07 20:34:33 -05001608 Initialize(kShouldProcessData);
1609
1610 // Write the initial headers, with a FIN.
1611 EXPECT_CALL(*stream_, WriteHeadersMock(true));
1612 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/true, nullptr);
1613 EXPECT_TRUE(stream_->fin_sent());
1614
1615 // Writing Trailers should fail, as the FIN has already been sent.
1616 // populated with the number of body bytes written.
1617 EXPECT_QUIC_BUG(stream_->WriteTrailers(SpdyHeaderBlock(), nullptr),
1618 "Trailers cannot be sent after a FIN");
1619}
1620
1621TEST_P(QuicSpdyStreamTest, HeaderStreamNotiferCorrespondingSpdyStream) {
bnc9f977e42019-06-07 11:36:34 -07001622 // There is no headers stream if QPACK is used.
renjietange47e15f2019-10-29 14:19:30 -07001623 if (UsesHttp3()) {
bnc9f977e42019-06-07 11:36:34 -07001624 return;
1625 }
1626
bnc9f977e42019-06-07 11:36:34 -07001627 const char kHeader1[] = "Header1";
1628 const char kHeader2[] = "Header2";
1629 const char kBody1[] = "Test1";
1630 const char kBody2[] = "Test2";
1631
QUICHE teama6ef0a62019-03-07 20:34:33 -05001632 Initialize(kShouldProcessData);
renjietang41a1b412020-02-27 15:05:14 -08001633 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001634 testing::InSequence s;
1635 QuicReferenceCountedPointer<MockAckListener> ack_listener1(
1636 new MockAckListener());
1637 QuicReferenceCountedPointer<MockAckListener> ack_listener2(
1638 new MockAckListener());
1639 stream_->set_ack_listener(ack_listener1);
1640 stream2_->set_ack_listener(ack_listener2);
1641
bnc9f977e42019-06-07 11:36:34 -07001642 session_->headers_stream()->WriteOrBufferData(kHeader1, false, ack_listener1);
1643 stream_->WriteOrBufferBody(kBody1, true);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001644
bnc9f977e42019-06-07 11:36:34 -07001645 session_->headers_stream()->WriteOrBufferData(kHeader2, false, ack_listener2);
1646 stream2_->WriteOrBufferBody(kBody2, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001647
1648 QuicStreamFrame frame1(
1649 QuicUtils::GetHeadersStreamId(connection_->transport_version()), false, 0,
bnc9f977e42019-06-07 11:36:34 -07001650 kHeader1);
1651
renjietanga29a96a2019-10-10 12:47:50 -07001652 std::string data1 = UsesHttp3() ? DataFrame(kBody1) : kBody1;
bnc9f977e42019-06-07 11:36:34 -07001653 QuicStreamFrame frame2(stream_->id(), true, 0, data1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001654 QuicStreamFrame frame3(
1655 QuicUtils::GetHeadersStreamId(connection_->transport_version()), false, 7,
bnc9f977e42019-06-07 11:36:34 -07001656 kHeader2);
renjietanga29a96a2019-10-10 12:47:50 -07001657 std::string data2 = UsesHttp3() ? DataFrame(kBody2) : kBody2;
bnc9f977e42019-06-07 11:36:34 -07001658 QuicStreamFrame frame4(stream2_->id(), false, 0, data2);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001659
1660 EXPECT_CALL(*ack_listener1, OnPacketRetransmitted(7));
1661 session_->OnStreamFrameRetransmitted(frame1);
1662
1663 EXPECT_CALL(*ack_listener1, OnPacketAcked(7, _));
QUICHE team9467db02019-05-30 09:38:45 -07001664 EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame1), QuicTime::Delta::Zero(),
1665 QuicTime::Zero()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001666 EXPECT_CALL(*ack_listener1, OnPacketAcked(5, _));
QUICHE team9467db02019-05-30 09:38:45 -07001667 EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame2), QuicTime::Delta::Zero(),
1668 QuicTime::Zero()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001669 EXPECT_CALL(*ack_listener2, OnPacketAcked(7, _));
QUICHE team9467db02019-05-30 09:38:45 -07001670 EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame3), QuicTime::Delta::Zero(),
1671 QuicTime::Zero()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001672 EXPECT_CALL(*ack_listener2, OnPacketAcked(5, _));
QUICHE team9467db02019-05-30 09:38:45 -07001673 EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame4), QuicTime::Delta::Zero(),
1674 QuicTime::Zero()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001675}
1676
1677TEST_P(QuicSpdyStreamTest, StreamBecomesZombieWithWriteThatCloses) {
1678 Initialize(kShouldProcessData);
renjietang41a1b412020-02-27 15:05:14 -08001679 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001680 QuicStreamPeer::CloseReadSide(stream_);
1681 // This write causes stream to be closed.
1682 stream_->WriteOrBufferBody("Test1", true);
1683 // stream_ has unacked data and should become zombie.
1684 EXPECT_TRUE(QuicContainsKey(QuicSessionPeer::zombie_streams(session_.get()),
1685 stream_->id()));
1686 EXPECT_TRUE(QuicSessionPeer::closed_streams(session_.get()).empty());
1687}
1688
1689TEST_P(QuicSpdyStreamTest, OnPriorityFrame) {
1690 Initialize(kShouldProcessData);
fayang476683a2019-07-25 12:42:16 -07001691 stream_->OnPriorityFrame(spdy::SpdyStreamPrecedence(kV3HighestPriority));
1692 EXPECT_EQ(spdy::SpdyStreamPrecedence(kV3HighestPriority),
1693 stream_->precedence());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001694}
1695
1696TEST_P(QuicSpdyStreamTest, OnPriorityFrameAfterSendingData) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001697 Initialize(kShouldProcessData);
fayang8790d292020-03-18 13:17:23 -07001698 testing::InSequence seq;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001699
renjietanga29a96a2019-10-10 12:47:50 -07001700 if (UsesHttp3()) {
renjietang41a1b412020-02-27 15:05:14 -08001701 EXPECT_CALL(*session_, WritevData(_, 2, _, NO_FIN, _, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001702 }
renjietang41a1b412020-02-27 15:05:14 -08001703 EXPECT_CALL(*session_, WritevData(_, 4, _, FIN, _, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001704 stream_->WriteOrBufferBody("data", true);
fayang476683a2019-07-25 12:42:16 -07001705 stream_->OnPriorityFrame(spdy::SpdyStreamPrecedence(kV3HighestPriority));
1706 EXPECT_EQ(spdy::SpdyStreamPrecedence(kV3HighestPriority),
1707 stream_->precedence());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001708}
1709
1710TEST_P(QuicSpdyStreamTest, SetPriorityBeforeUpdateStreamPriority) {
1711 MockQuicConnection* connection = new StrictMock<MockQuicConnection>(
1712 &helper_, &alarm_factory_, Perspective::IS_SERVER,
1713 SupportedVersions(GetParam()));
1714 std::unique_ptr<TestMockUpdateStreamSession> session(
1715 new StrictMock<TestMockUpdateStreamSession>(connection));
renjietangd1d00852019-09-06 10:43:12 -07001716 auto stream =
1717 new StrictMock<TestStream>(GetNthClientInitiatedBidirectionalStreamId(
1718 session->transport_version(), 0),
1719 session.get(),
1720 /*should_process_data=*/true);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001721 session->ActivateStream(QuicWrapUnique(stream));
1722
1723 // QuicSpdyStream::SetPriority() should eventually call UpdateStreamPriority()
1724 // on the session. Make sure stream->priority() returns the updated priority
1725 // if called within UpdateStreamPriority(). This expectation is enforced in
1726 // TestMockUpdateStreamSession::UpdateStreamPriority().
1727 session->SetExpectedStream(stream);
fayang476683a2019-07-25 12:42:16 -07001728 session->SetExpectedPriority(spdy::SpdyStreamPrecedence(kV3HighestPriority));
1729 stream->SetPriority(spdy::SpdyStreamPrecedence(kV3HighestPriority));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001730
fayang476683a2019-07-25 12:42:16 -07001731 session->SetExpectedPriority(spdy::SpdyStreamPrecedence(kV3LowestPriority));
1732 stream->SetPriority(spdy::SpdyStreamPrecedence(kV3LowestPriority));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001733}
1734
1735TEST_P(QuicSpdyStreamTest, StreamWaitsForAcks) {
1736 Initialize(kShouldProcessData);
1737 QuicReferenceCountedPointer<MockAckListener> mock_ack_listener(
1738 new StrictMock<MockAckListener>);
1739 stream_->set_ack_listener(mock_ack_listener);
renjietang41a1b412020-02-27 15:05:14 -08001740 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001741 // Stream is not waiting for acks initially.
1742 EXPECT_FALSE(stream_->IsWaitingForAcks());
1743 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1744
1745 // Send kData1.
1746 stream_->WriteOrBufferData("FooAndBar", false, nullptr);
1747 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1748 EXPECT_TRUE(stream_->IsWaitingForAcks());
1749 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(9, _));
1750 QuicByteCount newly_acked_length = 0;
1751 EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001752 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001753 &newly_acked_length));
1754 // Stream is not waiting for acks as all sent data is acked.
1755 EXPECT_FALSE(stream_->IsWaitingForAcks());
1756 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1757
1758 // Send kData2.
1759 stream_->WriteOrBufferData("FooAndBar", false, nullptr);
1760 EXPECT_TRUE(stream_->IsWaitingForAcks());
1761 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1762 // Send FIN.
1763 stream_->WriteOrBufferData("", true, nullptr);
1764 // Fin only frame is not stored in send buffer.
1765 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1766
1767 // kData2 is retransmitted.
1768 EXPECT_CALL(*mock_ack_listener, OnPacketRetransmitted(9));
1769 stream_->OnStreamFrameRetransmitted(9, 9, false);
1770
1771 // kData2 is acked.
1772 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(9, _));
1773 EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001774 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001775 &newly_acked_length));
1776 // Stream is waiting for acks as FIN is not acked.
1777 EXPECT_TRUE(stream_->IsWaitingForAcks());
1778 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1779
1780 // FIN is acked.
1781 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(0, _));
1782 EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 0, true, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001783 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001784 &newly_acked_length));
1785 EXPECT_FALSE(stream_->IsWaitingForAcks());
1786 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1787}
1788
1789TEST_P(QuicSpdyStreamTest, StreamDataGetAckedMultipleTimes) {
1790 Initialize(kShouldProcessData);
1791 QuicReferenceCountedPointer<MockAckListener> mock_ack_listener(
1792 new StrictMock<MockAckListener>);
1793 stream_->set_ack_listener(mock_ack_listener);
renjietang41a1b412020-02-27 15:05:14 -08001794 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001795 // Send [0, 27) and fin.
1796 stream_->WriteOrBufferData("FooAndBar", false, nullptr);
1797 stream_->WriteOrBufferData("FooAndBar", false, nullptr);
1798 stream_->WriteOrBufferData("FooAndBar", true, nullptr);
1799
1800 // Ack [0, 9), [5, 22) and [18, 26)
1801 // Verify [0, 9) 9 bytes are acked.
1802 QuicByteCount newly_acked_length = 0;
1803 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(9, _));
1804 EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001805 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001806 &newly_acked_length));
1807 EXPECT_EQ(2u, QuicStreamPeer::SendBuffer(stream_).size());
1808 // Verify [9, 22) 13 bytes are acked.
1809 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(13, _));
1810 EXPECT_TRUE(stream_->OnStreamFrameAcked(5, 17, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001811 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001812 &newly_acked_length));
1813 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1814 // Verify [22, 26) 4 bytes are acked.
1815 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(4, _));
1816 EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 8, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001817 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001818 &newly_acked_length));
1819 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1820 EXPECT_TRUE(stream_->IsWaitingForAcks());
1821
1822 // Ack [0, 27).
1823 // Verify [26, 27) 1 byte is acked.
1824 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(1, _));
1825 EXPECT_TRUE(stream_->OnStreamFrameAcked(26, 1, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001826 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001827 &newly_acked_length));
1828 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1829 EXPECT_TRUE(stream_->IsWaitingForAcks());
1830
1831 // Ack Fin. Verify OnPacketAcked is called.
1832 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(0, _));
1833 EXPECT_TRUE(stream_->OnStreamFrameAcked(27, 0, true, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001834 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001835 &newly_acked_length));
1836 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1837 EXPECT_FALSE(stream_->IsWaitingForAcks());
1838
1839 // Ack [10, 27) and fin.
1840 // No new data is acked, verify OnPacketAcked is not called.
1841 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(_, _)).Times(0);
QUICHE team2f5f30b2020-02-18 08:52:28 -08001842 EXPECT_FALSE(
1843 stream_->OnStreamFrameAcked(10, 17, true, QuicTime::Delta::Zero(),
1844 QuicTime::Zero(), &newly_acked_length));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001845 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1846 EXPECT_FALSE(stream_->IsWaitingForAcks());
1847}
1848
1849// HTTP/3 only.
1850TEST_P(QuicSpdyStreamTest, HeadersAckNotReportedWriteOrBufferBody) {
renjietanga29a96a2019-10-10 12:47:50 -07001851 if (!UsesHttp3()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001852 return;
1853 }
bnc677451a2019-06-07 10:13:30 -07001854
1855 Initialize(kShouldProcessData);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001856 QuicReferenceCountedPointer<MockAckListener> mock_ack_listener(
1857 new StrictMock<MockAckListener>);
1858 stream_->set_ack_listener(mock_ack_listener);
vasilvvc48c8712019-03-11 13:38:16 -07001859 std::string body = "Test1";
1860 std::string body2(100, 'x');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001861
renjietang41a1b412020-02-27 15:05:14 -08001862 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001863 stream_->WriteOrBufferBody(body, false);
1864 stream_->WriteOrBufferBody(body2, true);
1865
1866 std::unique_ptr<char[]> buffer;
1867 QuicByteCount header_length =
bnc46942722019-10-29 11:56:21 -07001868 HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer);
vasilvvc48c8712019-03-11 13:38:16 -07001869 std::string header = std::string(buffer.get(), header_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001870
bnc46942722019-10-29 11:56:21 -07001871 header_length =
1872 HttpEncoder::SerializeDataFrameHeader(body2.length(), &buffer);
vasilvvc48c8712019-03-11 13:38:16 -07001873 std::string header2 = std::string(buffer.get(), header_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001874
1875 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(body.length(), _));
1876 QuicStreamFrame frame(stream_->id(), false, 0, header + body);
QUICHE team9467db02019-05-30 09:38:45 -07001877 EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame), QuicTime::Delta::Zero(),
1878 QuicTime::Zero()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001879
1880 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(0, _));
bnc9f977e42019-06-07 11:36:34 -07001881 QuicStreamFrame frame2(stream_->id(), false, header.length() + body.length(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001882 header2);
QUICHE team9467db02019-05-30 09:38:45 -07001883 EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame2), QuicTime::Delta::Zero(),
1884 QuicTime::Zero()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001885
1886 EXPECT_CALL(*mock_ack_listener, OnPacketAcked(body2.length(), _));
1887 QuicStreamFrame frame3(stream_->id(), true,
bnc9f977e42019-06-07 11:36:34 -07001888 header.length() + body.length() + header2.length(),
1889 body2);
QUICHE team9467db02019-05-30 09:38:45 -07001890 EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame3), QuicTime::Delta::Zero(),
1891 QuicTime::Zero()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001892
1893 EXPECT_TRUE(
1894 QuicSpdyStreamPeer::unacked_frame_headers_offsets(stream_).Empty());
1895}
1896
1897// HTTP/3 only.
1898TEST_P(QuicSpdyStreamTest, HeadersAckNotReportedWriteBodySlices) {
renjietanga29a96a2019-10-10 12:47:50 -07001899 if (!UsesHttp3()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001900 return;
1901 }
bnc677451a2019-06-07 10:13:30 -07001902
1903 Initialize(kShouldProcessData);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001904 QuicReferenceCountedPointer<MockAckListener> mock_ack_listener(
1905 new StrictMock<MockAckListener>);
1906 stream_->set_ack_listener(mock_ack_listener);
bnc9f977e42019-06-07 11:36:34 -07001907 std::string body1 = "Test1";
vasilvvc48c8712019-03-11 13:38:16 -07001908 std::string body2(100, 'x');
bnc9f977e42019-06-07 11:36:34 -07001909 struct iovec body1_iov = {const_cast<char*>(body1.data()), body1.length()};
QUICHE teama6ef0a62019-03-07 20:34:33 -05001910 struct iovec body2_iov = {const_cast<char*>(body2.data()), body2.length()};
1911 QuicMemSliceStorage storage(&body1_iov, 1,
1912 helper_.GetStreamSendBufferAllocator(), 1024);
1913 QuicMemSliceStorage storage2(&body2_iov, 1,
1914 helper_.GetStreamSendBufferAllocator(), 1024);
renjietang41a1b412020-02-27 15:05:14 -08001915 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001916 stream_->WriteBodySlices(storage.ToSpan(), false);
1917 stream_->WriteBodySlices(storage2.ToSpan(), true);
1918
bnc9f977e42019-06-07 11:36:34 -07001919 std::string data1 = DataFrame(body1);
1920 std::string data2 = DataFrame(body2);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001921
1922 EXPECT_CALL(*mock_ack_listener,
bnc9f977e42019-06-07 11:36:34 -07001923 OnPacketAcked(body1.length() + body2.length(), _));
1924 QuicStreamFrame frame(stream_->id(), true, 0, data1 + data2);
QUICHE team9467db02019-05-30 09:38:45 -07001925 EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame), QuicTime::Delta::Zero(),
1926 QuicTime::Zero()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001927
1928 EXPECT_TRUE(
1929 QuicSpdyStreamPeer::unacked_frame_headers_offsets(stream_).Empty());
1930}
1931
1932// HTTP/3 only.
1933TEST_P(QuicSpdyStreamTest, HeaderBytesNotReportedOnRetransmission) {
renjietanga29a96a2019-10-10 12:47:50 -07001934 if (!UsesHttp3()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001935 return;
1936 }
bnc677451a2019-06-07 10:13:30 -07001937
1938 Initialize(kShouldProcessData);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001939 QuicReferenceCountedPointer<MockAckListener> mock_ack_listener(
1940 new StrictMock<MockAckListener>);
1941 stream_->set_ack_listener(mock_ack_listener);
bnc9f977e42019-06-07 11:36:34 -07001942 std::string body1 = "Test1";
vasilvvc48c8712019-03-11 13:38:16 -07001943 std::string body2(100, 'x');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001944
renjietang41a1b412020-02-27 15:05:14 -08001945 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1));
bnc9f977e42019-06-07 11:36:34 -07001946 stream_->WriteOrBufferBody(body1, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001947 stream_->WriteOrBufferBody(body2, true);
1948
bnc9f977e42019-06-07 11:36:34 -07001949 std::string data1 = DataFrame(body1);
1950 std::string data2 = DataFrame(body2);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001951
bnc9f977e42019-06-07 11:36:34 -07001952 EXPECT_CALL(*mock_ack_listener, OnPacketRetransmitted(body1.length()));
1953 QuicStreamFrame frame(stream_->id(), false, 0, data1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001954 session_->OnStreamFrameRetransmitted(frame);
1955
1956 EXPECT_CALL(*mock_ack_listener, OnPacketRetransmitted(body2.length()));
bnc9f977e42019-06-07 11:36:34 -07001957 QuicStreamFrame frame2(stream_->id(), true, data1.length(), data2);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001958 session_->OnStreamFrameRetransmitted(frame2);
1959
1960 EXPECT_FALSE(
1961 QuicSpdyStreamPeer::unacked_frame_headers_offsets(stream_).Empty());
1962}
1963
renjietang2abedac2019-05-20 14:04:50 -07001964TEST_P(QuicSpdyStreamTest, HeadersFrameOnRequestStream) {
renjietange47e15f2019-10-29 14:19:30 -07001965 if (!UsesHttp3()) {
renjietang2abedac2019-05-20 14:04:50 -07001966 return;
1967 }
1968
1969 Initialize(kShouldProcessData);
1970
fayang7e3c1a12019-08-02 08:07:13 -07001971 std::string headers = HeadersFrame({std::make_pair("foo", "bar")});
bnc9f977e42019-06-07 11:36:34 -07001972 std::string data = DataFrame(kDataFramePayload);
fayang7e3c1a12019-08-02 08:07:13 -07001973 std::string trailers =
1974 HeadersFrame({std::make_pair("custom-key", "custom-value")});
renjietang2abedac2019-05-20 14:04:50 -07001975
dmcardleba2fb7e2019-12-13 07:44:34 -08001976 std::string stream_frame_payload =
1977 quiche::QuicheStrCat(headers, data, trailers);
renjietang2abedac2019-05-20 14:04:50 -07001978 QuicStreamFrame frame(stream_->id(), false, 0, stream_frame_payload);
1979 stream_->OnStreamFrame(frame);
1980
bnc8d041302019-06-10 10:19:04 -07001981 EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar")));
renjietang2abedac2019-05-20 14:04:50 -07001982
1983 // QuicSpdyStream only calls OnBodyAvailable()
1984 // after the header list has been consumed.
1985 EXPECT_EQ("", stream_->data());
1986 stream_->ConsumeHeaderList();
bnc9f977e42019-06-07 11:36:34 -07001987 EXPECT_EQ(kDataFramePayload, stream_->data());
renjietang2abedac2019-05-20 14:04:50 -07001988
bnc8d041302019-06-10 10:19:04 -07001989 EXPECT_THAT(stream_->received_trailers(),
1990 ElementsAre(Pair("custom-key", "custom-value")));
renjietang2abedac2019-05-20 14:04:50 -07001991}
1992
renjietangbd1a0392019-05-31 11:36:24 -07001993TEST_P(QuicSpdyStreamTest, ProcessBodyAfterTrailers) {
renjietange47e15f2019-10-29 14:19:30 -07001994 if (!UsesHttp3()) {
renjietangbd1a0392019-05-31 11:36:24 -07001995 return;
1996 }
bnc677451a2019-06-07 10:13:30 -07001997
1998 Initialize(!kShouldProcessData);
renjietangbd1a0392019-05-31 11:36:24 -07001999
fayang7e3c1a12019-08-02 08:07:13 -07002000 std::string headers = HeadersFrame({std::make_pair("foo", "bar")});
bnc9f977e42019-06-07 11:36:34 -07002001 std::string data = DataFrame(kDataFramePayload);
renjietangbd1a0392019-05-31 11:36:24 -07002002
2003 // A header block that will take more than one block of sequencer buffer.
2004 // This ensures that when the trailers are consumed, some buffer buckets will
2005 // be freed.
2006 SpdyHeaderBlock trailers_block;
2007 trailers_block["key1"] = std::string(10000, 'x');
bnc2e963aa2019-07-31 18:03:17 -07002008 std::string trailers = HeadersFrame(trailers_block);
renjietangbd1a0392019-05-31 11:36:24 -07002009
bncd48a92e2019-06-18 19:24:58 -07002010 // Feed all three HTTP/3 frames in a single stream frame.
dmcardleba2fb7e2019-12-13 07:44:34 -08002011 std::string stream_frame_payload =
2012 quiche::QuicheStrCat(headers, data, trailers);
renjietangbd1a0392019-05-31 11:36:24 -07002013 QuicStreamFrame frame(stream_->id(), false, 0, stream_frame_payload);
2014 stream_->OnStreamFrame(frame);
2015
2016 stream_->ConsumeHeaderList();
2017 stream_->MarkTrailersConsumed();
bncd48a92e2019-06-18 19:24:58 -07002018
2019 EXPECT_TRUE(stream_->trailers_decompressed());
2020 EXPECT_EQ(trailers_block, stream_->received_trailers());
2021
bncab33c712019-06-28 15:42:15 -07002022 EXPECT_TRUE(stream_->HasBytesToRead());
bncd48a92e2019-06-18 19:24:58 -07002023
2024 // Consume data.
renjietangbd1a0392019-05-31 11:36:24 -07002025 char buffer[2048];
2026 struct iovec vec;
2027 vec.iov_base = buffer;
bnc4e9283d2019-12-17 07:08:57 -08002028 vec.iov_len = QUICHE_ARRAYSIZE(buffer);
renjietangbd1a0392019-05-31 11:36:24 -07002029 size_t bytes_read = stream_->Readv(&vec, 1);
dmcardleba2fb7e2019-12-13 07:44:34 -08002030 EXPECT_EQ(kDataFramePayload, quiche::QuicheStringPiece(buffer, bytes_read));
bncd48a92e2019-06-18 19:24:58 -07002031
bncab33c712019-06-28 15:42:15 -07002032 EXPECT_FALSE(stream_->HasBytesToRead());
renjietangbd1a0392019-05-31 11:36:24 -07002033}
2034
bnc677451a2019-06-07 10:13:30 -07002035// The test stream will receive a stream frame containing malformed headers and
2036// normal body. Make sure the http decoder stops processing body after the
2037// connection shuts down.
renjietang546a6282019-06-03 10:21:21 -07002038TEST_P(QuicSpdyStreamTest, MalformedHeadersStopHttpDecoder) {
renjietange47e15f2019-10-29 14:19:30 -07002039 if (!UsesHttp3()) {
renjietang546a6282019-06-03 10:21:21 -07002040 return;
2041 }
2042
2043 Initialize(kShouldProcessData);
fayang8790d292020-03-18 13:17:23 -07002044 testing::InSequence s;
renjietang546a6282019-06-03 10:21:21 -07002045 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
2046
2047 // Random bad headers.
bnc9f977e42019-06-07 11:36:34 -07002048 std::string headers =
dmcardleba2fb7e2019-12-13 07:44:34 -08002049 HeadersFrame(quiche::QuicheTextUtils::HexDecode("00002a94e7036261"));
bnc9f977e42019-06-07 11:36:34 -07002050 std::string data = DataFrame(kDataFramePayload);
renjietang546a6282019-06-03 10:21:21 -07002051
dmcardleba2fb7e2019-12-13 07:44:34 -08002052 std::string stream_frame_payload = quiche::QuicheStrCat(headers, data);
renjietang546a6282019-06-03 10:21:21 -07002053 QuicStreamFrame frame(stream_->id(), false, 0, stream_frame_payload);
2054
dschinazi552accc2019-06-17 17:07:34 -07002055 EXPECT_CALL(
2056 *connection_,
bnc232ff9b2019-11-13 18:38:22 -08002057 CloseConnection(QUIC_QPACK_DECOMPRESSION_FAILED,
2058 MatchesRegex("Error decoding headers on stream \\d+: "
2059 "Incomplete header block."),
2060 _))
renjietang546a6282019-06-03 10:21:21 -07002061 .WillOnce(
2062 (Invoke([this](QuicErrorCode error, const std::string& error_details,
2063 ConnectionCloseBehavior connection_close_behavior) {
2064 connection_->ReallyCloseConnection(error, error_details,
2065 connection_close_behavior);
2066 })));
2067 EXPECT_CALL(*connection_, SendConnectionClosePacket(_, _));
fkastenholz5d880a92019-06-21 09:01:56 -07002068 EXPECT_CALL(*session_, OnConnectionClosed(_, _))
2069 .WillOnce(Invoke([this](const QuicConnectionCloseFrame& frame,
2070 ConnectionCloseSource source) {
2071 session_->ReallyOnConnectionClosed(frame, source);
2072 }));
renjietang546a6282019-06-03 10:21:21 -07002073 EXPECT_CALL(*session_, SendRstStream(_, _, _));
2074 EXPECT_CALL(*session_, SendRstStream(_, _, _));
2075 stream_->OnStreamFrame(frame);
2076}
2077
bncf9ff46b2019-11-26 10:32:09 -08002078// Regression test for https://crbug.com/1027895: a HEADERS frame triggers an
2079// error in QuicSpdyStream::OnHeadersFramePayload(). This closes the
2080// connection, freeing the buffer of QuicStreamSequencer. Therefore
2081// QuicStreamSequencer::MarkConsumed() must not be called from
2082// QuicSpdyStream::OnHeadersFramePayload().
2083TEST_P(QuicSpdyStreamTest, DoNotMarkConsumedAfterQpackDecodingError) {
2084 if (!UsesHttp3()) {
2085 return;
2086 }
2087
2088 Initialize(kShouldProcessData);
2089 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
2090
2091 testing::InSequence s;
2092 EXPECT_CALL(
2093 *connection_,
2094 CloseConnection(QUIC_QPACK_DECOMPRESSION_FAILED,
2095 MatchesRegex("Error decoding headers on stream \\d+: "
2096 "Invalid relative index."),
2097 _))
2098 .WillOnce(
2099 (Invoke([this](QuicErrorCode error, const std::string& error_details,
2100 ConnectionCloseBehavior connection_close_behavior) {
2101 connection_->ReallyCloseConnection(error, error_details,
2102 connection_close_behavior);
2103 })));
2104 EXPECT_CALL(*connection_, SendConnectionClosePacket(_, _));
2105 EXPECT_CALL(*session_, OnConnectionClosed(_, _))
2106 .WillOnce(Invoke([this](const QuicConnectionCloseFrame& frame,
2107 ConnectionCloseSource source) {
2108 session_->ReallyOnConnectionClosed(frame, source);
2109 }));
2110 EXPECT_CALL(*session_, SendRstStream(stream_->id(), _, _));
2111 EXPECT_CALL(*session_, SendRstStream(stream2_->id(), _, _));
2112
2113 // Invalid headers: Required Insert Count is zero, but the header block
2114 // contains a dynamic table reference.
dmcardleba2fb7e2019-12-13 07:44:34 -08002115 std::string headers =
2116 HeadersFrame(quiche::QuicheTextUtils::HexDecode("000080"));
bncf9ff46b2019-11-26 10:32:09 -08002117 QuicStreamFrame frame(stream_->id(), false, 0, headers);
2118 stream_->OnStreamFrame(frame);
2119}
2120
bncbdd303e2019-07-09 05:33:17 -07002121TEST_P(QuicSpdyStreamTest, ImmediateHeaderDecodingWithDynamicTableEntries) {
renjietange47e15f2019-10-29 14:19:30 -07002122 if (!UsesHttp3()) {
bncbdd303e2019-07-09 05:33:17 -07002123 return;
2124 }
2125
2126 Initialize(kShouldProcessData);
fayang8790d292020-03-18 13:17:23 -07002127 testing::InSequence s;
bnc40e9a7b2019-08-30 05:27:08 -07002128 session_->qpack_decoder()->OnSetDynamicTableCapacity(1024);
bncbdd303e2019-07-09 05:33:17 -07002129
renjietang87cd7de2019-08-16 08:35:10 -07002130 auto decoder_send_stream =
2131 QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get());
2132
2133 // The stream byte will be written in the first byte.
renjietang41a1b412020-02-27 15:05:14 -08002134 EXPECT_CALL(*session_, WritevData(decoder_send_stream->id(), _, _, _, _, _));
bncbdd303e2019-07-09 05:33:17 -07002135 // Deliver dynamic table entry to decoder.
2136 session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar");
2137
2138 // HEADERS frame referencing first dynamic table entry.
dmcardleba2fb7e2019-12-13 07:44:34 -08002139 std::string headers =
2140 HeadersFrame(quiche::QuicheTextUtils::HexDecode("020080"));
bncbdd303e2019-07-09 05:33:17 -07002141 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, headers));
2142
2143 // Headers can be decoded immediately.
2144 EXPECT_TRUE(stream_->headers_decompressed());
2145
2146 // Verify headers.
2147 EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar")));
2148 stream_->ConsumeHeaderList();
2149
2150 // DATA frame.
2151 std::string data = DataFrame(kDataFramePayload);
2152 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, /* offset = */
2153 headers.length(), data));
2154 EXPECT_EQ(kDataFramePayload, stream_->data());
2155
renjietang41a1b412020-02-27 15:05:14 -08002156 EXPECT_CALL(*session_, WritevData(decoder_send_stream->id(), _, _, _, _, _));
bncbdd303e2019-07-09 05:33:17 -07002157 // Deliver second dynamic table entry to decoder.
2158 session_->qpack_decoder()->OnInsertWithoutNameReference("trailing", "foobar");
2159
2160 // Trailing HEADERS frame referencing second dynamic table entry.
dmcardleba2fb7e2019-12-13 07:44:34 -08002161 std::string trailers =
2162 HeadersFrame(quiche::QuicheTextUtils::HexDecode("030080"));
bncbdd303e2019-07-09 05:33:17 -07002163 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), true, /* offset = */
2164 headers.length() + data.length(),
2165 trailers));
2166
2167 // Trailers can be decoded immediately.
2168 EXPECT_TRUE(stream_->trailers_decompressed());
2169
2170 // Verify trailers.
2171 EXPECT_THAT(stream_->received_trailers(),
2172 ElementsAre(Pair("trailing", "foobar")));
2173 stream_->MarkTrailersConsumed();
2174}
2175
bncc057c012019-07-02 11:13:22 -07002176TEST_P(QuicSpdyStreamTest, BlockedHeaderDecoding) {
renjietange47e15f2019-10-29 14:19:30 -07002177 if (!UsesHttp3()) {
bncc057c012019-07-02 11:13:22 -07002178 return;
2179 }
2180
2181 Initialize(kShouldProcessData);
fayang8790d292020-03-18 13:17:23 -07002182 testing::InSequence s;
bnc40e9a7b2019-08-30 05:27:08 -07002183 session_->qpack_decoder()->OnSetDynamicTableCapacity(1024);
bncc057c012019-07-02 11:13:22 -07002184
bncbdd303e2019-07-09 05:33:17 -07002185 // HEADERS frame referencing first dynamic table entry.
dmcardleba2fb7e2019-12-13 07:44:34 -08002186 std::string headers =
2187 HeadersFrame(quiche::QuicheTextUtils::HexDecode("020080"));
bncc057c012019-07-02 11:13:22 -07002188 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, headers));
2189
bncbdd303e2019-07-09 05:33:17 -07002190 // Decoding is blocked because dynamic table entry has not been received yet.
bncc057c012019-07-02 11:13:22 -07002191 EXPECT_FALSE(stream_->headers_decompressed());
2192
renjietang87cd7de2019-08-16 08:35:10 -07002193 auto decoder_send_stream =
2194 QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get());
2195
2196 // The stream byte will be written in the first byte.
renjietang41a1b412020-02-27 15:05:14 -08002197 EXPECT_CALL(*session_, WritevData(decoder_send_stream->id(), _, _, _, _, _));
bncbdd303e2019-07-09 05:33:17 -07002198 // Deliver dynamic table entry to decoder.
2199 session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar");
bncc057c012019-07-02 11:13:22 -07002200 EXPECT_TRUE(stream_->headers_decompressed());
2201
bncbdd303e2019-07-09 05:33:17 -07002202 // Verify headers.
bncc057c012019-07-02 11:13:22 -07002203 EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar")));
2204 stream_->ConsumeHeaderList();
2205
bncbdd303e2019-07-09 05:33:17 -07002206 // DATA frame.
bncc057c012019-07-02 11:13:22 -07002207 std::string data = DataFrame(kDataFramePayload);
2208 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, /* offset = */
2209 headers.length(), data));
2210 EXPECT_EQ(kDataFramePayload, stream_->data());
2211
bncbdd303e2019-07-09 05:33:17 -07002212 // Trailing HEADERS frame referencing second dynamic table entry.
dmcardleba2fb7e2019-12-13 07:44:34 -08002213 std::string trailers =
2214 HeadersFrame(quiche::QuicheTextUtils::HexDecode("030080"));
bncc057c012019-07-02 11:13:22 -07002215 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), true, /* offset = */
2216 headers.length() + data.length(),
2217 trailers));
2218
bncbdd303e2019-07-09 05:33:17 -07002219 // Decoding is blocked because dynamic table entry has not been received yet.
bncc057c012019-07-02 11:13:22 -07002220 EXPECT_FALSE(stream_->trailers_decompressed());
2221
renjietang41a1b412020-02-27 15:05:14 -08002222 EXPECT_CALL(*session_, WritevData(decoder_send_stream->id(), _, _, _, _, _));
bncbdd303e2019-07-09 05:33:17 -07002223 // Deliver second dynamic table entry to decoder.
2224 session_->qpack_decoder()->OnInsertWithoutNameReference("trailing", "foobar");
bncc057c012019-07-02 11:13:22 -07002225 EXPECT_TRUE(stream_->trailers_decompressed());
2226
2227 // Verify trailers.
2228 EXPECT_THAT(stream_->received_trailers(),
bncbdd303e2019-07-09 05:33:17 -07002229 ElementsAre(Pair("trailing", "foobar")));
bncc057c012019-07-02 11:13:22 -07002230 stream_->MarkTrailersConsumed();
2231}
2232
2233TEST_P(QuicSpdyStreamTest, AsyncErrorDecodingHeaders) {
renjietange47e15f2019-10-29 14:19:30 -07002234 if (!UsesHttp3()) {
bncc057c012019-07-02 11:13:22 -07002235 return;
2236 }
2237
2238 Initialize(kShouldProcessData);
bnc40e9a7b2019-08-30 05:27:08 -07002239 session_->qpack_decoder()->OnSetDynamicTableCapacity(1024);
bncc057c012019-07-02 11:13:22 -07002240
bncbdd303e2019-07-09 05:33:17 -07002241 // HEADERS frame only referencing entry with absolute index 0 but with
2242 // Required Insert Count = 2, which is incorrect.
dmcardleba2fb7e2019-12-13 07:44:34 -08002243 std::string headers =
2244 HeadersFrame(quiche::QuicheTextUtils::HexDecode("030081"));
bncc057c012019-07-02 11:13:22 -07002245 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, headers));
2246
bncbdd303e2019-07-09 05:33:17 -07002247 // Even though entire header block is received and every referenced entry is
2248 // available, decoding is blocked until insert count reaches the Required
2249 // Insert Count value advertised in the header block prefix.
bncc057c012019-07-02 11:13:22 -07002250 EXPECT_FALSE(stream_->headers_decompressed());
2251
bncc057c012019-07-02 11:13:22 -07002252 EXPECT_CALL(
2253 *connection_,
bnc232ff9b2019-11-13 18:38:22 -08002254 CloseConnection(QUIC_QPACK_DECOMPRESSION_FAILED,
2255 MatchesRegex("Error decoding headers on stream \\d+: "
2256 "Required Insert Count too large."),
2257 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET));
bncbdd303e2019-07-09 05:33:17 -07002258
2259 // Deliver two dynamic table entries to decoder
2260 // to trigger decoding of header block.
2261 session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar");
2262 session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar");
bncc057c012019-07-02 11:13:22 -07002263}
2264
bncfb4f4fc2019-11-18 17:14:56 -08002265// Regression test for https://crbug.com/1024263 and for
2266// https://crbug.com/1025209#c11.
2267TEST_P(QuicSpdyStreamTest, BlockedHeaderDecodingUnblockedWithBufferedError) {
2268 if (!UsesHttp3()) {
2269 return;
2270 }
2271
2272 Initialize(kShouldProcessData);
2273 session_->qpack_decoder()->OnSetDynamicTableCapacity(1024);
2274
2275 // Relative index 2 is invalid because it is larger than or equal to the Base.
dmcardleba2fb7e2019-12-13 07:44:34 -08002276 std::string headers =
2277 HeadersFrame(quiche::QuicheTextUtils::HexDecode("020082"));
bncfb4f4fc2019-11-18 17:14:56 -08002278 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, headers));
2279
2280 // Decoding is blocked.
2281 EXPECT_FALSE(stream_->headers_decompressed());
2282
2283 EXPECT_CALL(
2284 *connection_,
2285 CloseConnection(QUIC_QPACK_DECOMPRESSION_FAILED,
2286 MatchesRegex("Error decoding headers on stream \\d+: "
2287 "Invalid relative index."),
2288 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET));
2289
2290 // Deliver one dynamic table entry to decoder
2291 // to trigger decoding of header block.
2292 session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar");
2293}
2294
bncc057c012019-07-02 11:13:22 -07002295TEST_P(QuicSpdyStreamTest, AsyncErrorDecodingTrailers) {
renjietange47e15f2019-10-29 14:19:30 -07002296 if (!UsesHttp3()) {
bncc057c012019-07-02 11:13:22 -07002297 return;
2298 }
2299
2300 Initialize(kShouldProcessData);
fayang8790d292020-03-18 13:17:23 -07002301 testing::InSequence s;
bnc40e9a7b2019-08-30 05:27:08 -07002302 session_->qpack_decoder()->OnSetDynamicTableCapacity(1024);
bncc057c012019-07-02 11:13:22 -07002303
bncbdd303e2019-07-09 05:33:17 -07002304 // HEADERS frame referencing first dynamic table entry.
dmcardleba2fb7e2019-12-13 07:44:34 -08002305 std::string headers =
2306 HeadersFrame(quiche::QuicheTextUtils::HexDecode("020080"));
bncc057c012019-07-02 11:13:22 -07002307 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, headers));
2308
bncbdd303e2019-07-09 05:33:17 -07002309 // Decoding is blocked because dynamic table entry has not been received yet.
bncc057c012019-07-02 11:13:22 -07002310 EXPECT_FALSE(stream_->headers_decompressed());
2311
renjietang87cd7de2019-08-16 08:35:10 -07002312 auto decoder_send_stream =
2313 QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get());
2314
2315 // The stream byte will be written in the first byte.
renjietang41a1b412020-02-27 15:05:14 -08002316 EXPECT_CALL(*session_, WritevData(decoder_send_stream->id(), _, _, _, _, _));
bncbdd303e2019-07-09 05:33:17 -07002317 // Deliver dynamic table entry to decoder.
2318 session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar");
bncc057c012019-07-02 11:13:22 -07002319 EXPECT_TRUE(stream_->headers_decompressed());
2320
bncbdd303e2019-07-09 05:33:17 -07002321 // Verify headers.
bncc057c012019-07-02 11:13:22 -07002322 EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar")));
2323 stream_->ConsumeHeaderList();
2324
bncbdd303e2019-07-09 05:33:17 -07002325 // DATA frame.
bncc057c012019-07-02 11:13:22 -07002326 std::string data = DataFrame(kDataFramePayload);
2327 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, /* offset = */
2328 headers.length(), data));
2329 EXPECT_EQ(kDataFramePayload, stream_->data());
2330
bncbdd303e2019-07-09 05:33:17 -07002331 // Trailing HEADERS frame only referencing entry with absolute index 0 but
2332 // with Required Insert Count = 2, which is incorrect.
dmcardleba2fb7e2019-12-13 07:44:34 -08002333 std::string trailers =
2334 HeadersFrame(quiche::QuicheTextUtils::HexDecode("030081"));
bncc057c012019-07-02 11:13:22 -07002335 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), true, /* offset = */
2336 headers.length() + data.length(),
2337 trailers));
2338
bncbdd303e2019-07-09 05:33:17 -07002339 // Even though entire header block is received and every referenced entry is
2340 // available, decoding is blocked until insert count reaches the Required
2341 // Insert Count value advertised in the header block prefix.
bncc057c012019-07-02 11:13:22 -07002342 EXPECT_FALSE(stream_->trailers_decompressed());
2343
bnc232ff9b2019-11-13 18:38:22 -08002344 EXPECT_CALL(
2345 *connection_,
2346 CloseConnection(QUIC_QPACK_DECOMPRESSION_FAILED,
2347 MatchesRegex("Error decoding trailers on stream \\d+: "
2348 "Required Insert Count too large."),
2349 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET));
bncbdd303e2019-07-09 05:33:17 -07002350
2351 // Deliver second dynamic table entry to decoder
2352 // to trigger decoding of trailing header block.
2353 session_->qpack_decoder()->OnInsertWithoutNameReference("trailing", "foobar");
bncc057c012019-07-02 11:13:22 -07002354}
2355
bnc8d041302019-06-10 10:19:04 -07002356class QuicSpdyStreamIncrementalConsumptionTest : public QuicSpdyStreamTest {
2357 protected:
2358 QuicSpdyStreamIncrementalConsumptionTest() : offset_(0), consumed_bytes_(0) {}
2359 ~QuicSpdyStreamIncrementalConsumptionTest() override = default;
2360
2361 // Create QuicStreamFrame with |payload|
2362 // and pass it to stream_->OnStreamFrame().
dmcardleba2fb7e2019-12-13 07:44:34 -08002363 void OnStreamFrame(quiche::QuicheStringPiece payload) {
bnc8d041302019-06-10 10:19:04 -07002364 QuicStreamFrame frame(stream_->id(), /* fin = */ false, offset_, payload);
2365 stream_->OnStreamFrame(frame);
2366 offset_ += payload.size();
2367 }
2368
2369 // Return number of bytes marked consumed with sequencer
2370 // since last NewlyConsumedBytes() call.
2371 QuicStreamOffset NewlyConsumedBytes() {
2372 QuicStreamOffset previously_consumed_bytes = consumed_bytes_;
2373 consumed_bytes_ = stream_->sequencer()->NumBytesConsumed();
2374 return consumed_bytes_ - previously_consumed_bytes;
2375 }
2376
2377 // Read |size| bytes from the stream.
2378 std::string ReadFromStream(QuicByteCount size) {
2379 std::string buffer;
2380 buffer.resize(size);
2381
2382 struct iovec vec;
2383 vec.iov_base = const_cast<char*>(buffer.data());
2384 vec.iov_len = size;
2385
2386 size_t bytes_read = stream_->Readv(&vec, 1);
2387 EXPECT_EQ(bytes_read, size);
2388
2389 return buffer;
2390 }
2391
2392 private:
2393 QuicStreamOffset offset_;
2394 QuicStreamOffset consumed_bytes_;
2395};
2396
2397INSTANTIATE_TEST_SUITE_P(Tests,
2398 QuicSpdyStreamIncrementalConsumptionTest,
dschinazi38cc1ee2020-02-28 14:33:58 -08002399 ::testing::ValuesIn(AllSupportedVersions()),
dschinazi142051a2019-09-18 18:17:29 -07002400 ::testing::PrintToStringParamName());
bnc8d041302019-06-10 10:19:04 -07002401
2402// Test that stream bytes are consumed (by calling
2403// sequencer()->MarkConsumed()) incrementally, as soon as possible.
bnc25827c42019-07-29 08:57:24 -07002404TEST_P(QuicSpdyStreamIncrementalConsumptionTest, OnlyKnownFrames) {
renjietange47e15f2019-10-29 14:19:30 -07002405 if (!UsesHttp3()) {
bnc8d041302019-06-10 10:19:04 -07002406 return;
2407 }
2408
2409 Initialize(!kShouldProcessData);
2410
fayang7e3c1a12019-08-02 08:07:13 -07002411 std::string headers = HeadersFrame({std::make_pair("foo", "bar")});
bnc8d041302019-06-10 10:19:04 -07002412
2413 // All HEADERS frame bytes are consumed even if the frame is not received
bnc7deaf372019-07-25 18:00:40 -07002414 // completely.
dmcardleba2fb7e2019-12-13 07:44:34 -08002415 OnStreamFrame(
2416 quiche::QuicheStringPiece(headers).substr(0, headers.size() - 1));
bnc8d041302019-06-10 10:19:04 -07002417 EXPECT_EQ(headers.size() - 1, NewlyConsumedBytes());
2418
2419 // The rest of the HEADERS frame is also consumed immediately.
dmcardleba2fb7e2019-12-13 07:44:34 -08002420 OnStreamFrame(quiche::QuicheStringPiece(headers).substr(headers.size() - 1));
bnc8d041302019-06-10 10:19:04 -07002421 EXPECT_EQ(1u, NewlyConsumedBytes());
2422
2423 // Verify headers.
2424 EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar")));
2425 stream_->ConsumeHeaderList();
2426
2427 // DATA frame.
dmcardleba2fb7e2019-12-13 07:44:34 -08002428 quiche::QuicheStringPiece data_payload(kDataFramePayload);
bnc8d041302019-06-10 10:19:04 -07002429 std::string data_frame = DataFrame(data_payload);
bnce5f9c032019-07-25 11:30:40 -07002430 QuicByteCount data_frame_header_length =
2431 data_frame.size() - data_payload.size();
bnc8d041302019-06-10 10:19:04 -07002432
bnce5f9c032019-07-25 11:30:40 -07002433 // DATA frame header is consumed.
2434 // DATA frame payload is not consumed because payload has to be buffered.
bnc8d041302019-06-10 10:19:04 -07002435 OnStreamFrame(data_frame);
bnce5f9c032019-07-25 11:30:40 -07002436 EXPECT_EQ(data_frame_header_length, NewlyConsumedBytes());
bnc8d041302019-06-10 10:19:04 -07002437
2438 // Consume all but last byte of data.
2439 EXPECT_EQ(data_payload.substr(0, data_payload.size() - 1),
2440 ReadFromStream(data_payload.size() - 1));
bnce5f9c032019-07-25 11:30:40 -07002441 EXPECT_EQ(data_payload.size() - 1, NewlyConsumedBytes());
bnc8d041302019-06-10 10:19:04 -07002442
fayang7e3c1a12019-08-02 08:07:13 -07002443 std::string trailers =
2444 HeadersFrame({std::make_pair("custom-key", "custom-value")});
bnc8d041302019-06-10 10:19:04 -07002445
2446 // No bytes are consumed, because last byte of DATA payload is still buffered.
dmcardleba2fb7e2019-12-13 07:44:34 -08002447 OnStreamFrame(
2448 quiche::QuicheStringPiece(trailers).substr(0, trailers.size() - 1));
bnc8d041302019-06-10 10:19:04 -07002449 EXPECT_EQ(0u, NewlyConsumedBytes());
2450
2451 // Reading last byte of DATA payload triggers consumption of all data received
2452 // so far, even though last HEADERS frame has not been received completely.
2453 EXPECT_EQ(data_payload.substr(data_payload.size() - 1), ReadFromStream(1));
2454 EXPECT_EQ(1 + trailers.size() - 1, NewlyConsumedBytes());
2455
2456 // Last byte of trailers is immediately consumed.
dmcardleba2fb7e2019-12-13 07:44:34 -08002457 OnStreamFrame(
2458 quiche::QuicheStringPiece(trailers).substr(trailers.size() - 1));
bnc8d041302019-06-10 10:19:04 -07002459 EXPECT_EQ(1u, NewlyConsumedBytes());
2460
2461 // Verify trailers.
2462 EXPECT_THAT(stream_->received_trailers(),
2463 ElementsAre(Pair("custom-key", "custom-value")));
2464}
2465
bnc25827c42019-07-29 08:57:24 -07002466TEST_P(QuicSpdyStreamIncrementalConsumptionTest, UnknownFramesInterleaved) {
renjietange47e15f2019-10-29 14:19:30 -07002467 if (!UsesHttp3()) {
bnc25827c42019-07-29 08:57:24 -07002468 return;
2469 }
2470
2471 Initialize(!kShouldProcessData);
2472
2473 // Unknown frame of reserved type before HEADERS is consumed immediately.
2474 std::string unknown_frame1 = UnknownFrame(0x21, "foo");
2475 OnStreamFrame(unknown_frame1);
2476 EXPECT_EQ(unknown_frame1.size(), NewlyConsumedBytes());
2477
fayang7e3c1a12019-08-02 08:07:13 -07002478 std::string headers = HeadersFrame({std::make_pair("foo", "bar")});
bnc25827c42019-07-29 08:57:24 -07002479
2480 // All HEADERS frame bytes are consumed even if the frame is not received
2481 // completely.
dmcardleba2fb7e2019-12-13 07:44:34 -08002482 OnStreamFrame(
2483 quiche::QuicheStringPiece(headers).substr(0, headers.size() - 1));
bnc25827c42019-07-29 08:57:24 -07002484 EXPECT_EQ(headers.size() - 1, NewlyConsumedBytes());
2485
2486 // The rest of the HEADERS frame is also consumed immediately.
dmcardleba2fb7e2019-12-13 07:44:34 -08002487 OnStreamFrame(quiche::QuicheStringPiece(headers).substr(headers.size() - 1));
bnc25827c42019-07-29 08:57:24 -07002488 EXPECT_EQ(1u, NewlyConsumedBytes());
2489
2490 // Verify headers.
2491 EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar")));
2492 stream_->ConsumeHeaderList();
2493
2494 // Frame of unknown, not reserved type between HEADERS and DATA is consumed
2495 // immediately.
2496 std::string unknown_frame2 = UnknownFrame(0x3a, "");
2497 OnStreamFrame(unknown_frame2);
2498 EXPECT_EQ(unknown_frame2.size(), NewlyConsumedBytes());
2499
2500 // DATA frame.
dmcardleba2fb7e2019-12-13 07:44:34 -08002501 quiche::QuicheStringPiece data_payload(kDataFramePayload);
bnc25827c42019-07-29 08:57:24 -07002502 std::string data_frame = DataFrame(data_payload);
2503 QuicByteCount data_frame_header_length =
2504 data_frame.size() - data_payload.size();
2505
2506 // DATA frame header is consumed.
2507 // DATA frame payload is not consumed because payload has to be buffered.
2508 OnStreamFrame(data_frame);
2509 EXPECT_EQ(data_frame_header_length, NewlyConsumedBytes());
2510
2511 // Frame of unknown, not reserved type is not consumed because DATA payload is
2512 // still buffered.
2513 std::string unknown_frame3 = UnknownFrame(0x39, "bar");
2514 OnStreamFrame(unknown_frame3);
2515 EXPECT_EQ(0u, NewlyConsumedBytes());
2516
2517 // Consume all but last byte of data.
2518 EXPECT_EQ(data_payload.substr(0, data_payload.size() - 1),
2519 ReadFromStream(data_payload.size() - 1));
2520 EXPECT_EQ(data_payload.size() - 1, NewlyConsumedBytes());
2521
fayang7e3c1a12019-08-02 08:07:13 -07002522 std::string trailers =
2523 HeadersFrame({std::make_pair("custom-key", "custom-value")});
bnc25827c42019-07-29 08:57:24 -07002524
2525 // No bytes are consumed, because last byte of DATA payload is still buffered.
dmcardleba2fb7e2019-12-13 07:44:34 -08002526 OnStreamFrame(
2527 quiche::QuicheStringPiece(trailers).substr(0, trailers.size() - 1));
bnc25827c42019-07-29 08:57:24 -07002528 EXPECT_EQ(0u, NewlyConsumedBytes());
2529
2530 // Reading last byte of DATA payload triggers consumption of all data received
2531 // so far, even though last HEADERS frame has not been received completely.
2532 EXPECT_EQ(data_payload.substr(data_payload.size() - 1), ReadFromStream(1));
2533 EXPECT_EQ(1 + unknown_frame3.size() + trailers.size() - 1,
2534 NewlyConsumedBytes());
2535
2536 // Last byte of trailers is immediately consumed.
dmcardleba2fb7e2019-12-13 07:44:34 -08002537 OnStreamFrame(
2538 quiche::QuicheStringPiece(trailers).substr(trailers.size() - 1));
bnc25827c42019-07-29 08:57:24 -07002539 EXPECT_EQ(1u, NewlyConsumedBytes());
2540
2541 // Verify trailers.
2542 EXPECT_THAT(stream_->received_trailers(),
2543 ElementsAre(Pair("custom-key", "custom-value")));
2544
2545 // Unknown frame of reserved type after trailers is consumed immediately.
2546 std::string unknown_frame4 = UnknownFrame(0x40, "");
2547 OnStreamFrame(unknown_frame4);
2548 EXPECT_EQ(unknown_frame4.size(), NewlyConsumedBytes());
2549}
2550
renjietang3c3dfb72019-07-26 11:55:52 -07002551TEST_P(QuicSpdyStreamTest, PushPromiseOnDataStream) {
QUICHE team6c579462019-06-21 14:06:09 -07002552 Initialize(kShouldProcessData);
renjietanga29a96a2019-10-10 12:47:50 -07002553 if (!UsesHttp3()) {
QUICHE team6c579462019-06-21 14:06:09 -07002554 return;
2555 }
renjietang3c3dfb72019-07-26 11:55:52 -07002556
bnc2e963aa2019-07-31 18:03:17 -07002557 std::string headers = EncodeQpackHeaders({{"foo", "bar"}});
renjietang3c3dfb72019-07-26 11:55:52 -07002558
QUICHE team6c579462019-06-21 14:06:09 -07002559 PushPromiseFrame push_promise;
2560 push_promise.push_id = 0x01;
renjietang3c3dfb72019-07-26 11:55:52 -07002561 push_promise.headers = headers;
QUICHE team6c579462019-06-21 14:06:09 -07002562 std::unique_ptr<char[]> buffer;
bnc46942722019-10-29 11:56:21 -07002563 uint64_t length = HttpEncoder::SerializePushPromiseFrameWithOnlyPushId(
2564 push_promise, &buffer);
renjietang3c3dfb72019-07-26 11:55:52 -07002565 std::string data = std::string(buffer.get(), length) + headers;
2566 QuicStreamFrame frame(stream_->id(), false, 0, data);
2567
2568 EXPECT_CALL(*session_,
2569 OnPromiseHeaderList(stream_->id(), push_promise.push_id,
2570 headers.length(), _));
QUICHE team6c579462019-06-21 14:06:09 -07002571 stream_->OnStreamFrame(frame);
2572}
2573
bnc519216c2019-07-09 05:03:48 -07002574// Close connection if a DATA frame is received before a HEADERS frame.
2575TEST_P(QuicSpdyStreamTest, DataBeforeHeaders) {
renjietange47e15f2019-10-29 14:19:30 -07002576 if (!UsesHttp3()) {
bnc519216c2019-07-09 05:03:48 -07002577 return;
2578 }
2579
2580 Initialize(kShouldProcessData);
2581
2582 // Closing the connection is mocked out in tests. Instead, simply stop
2583 // reading data at the stream level to prevent QuicSpdyStream from blowing up.
bnc519216c2019-07-09 05:03:48 -07002584 EXPECT_CALL(
2585 *connection_,
bnc3e8fe7c2020-03-10 17:44:58 -07002586 CloseConnection(QUIC_HTTP_INVALID_FRAME_SEQUENCE_ON_SPDY_STREAM,
bnc519216c2019-07-09 05:03:48 -07002587 "Unexpected DATA frame received.",
2588 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET))
2589 .WillOnce(InvokeWithoutArgs([this]() { stream_->StopReading(); }));
2590
2591 std::string data = DataFrame(kDataFramePayload);
2592 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, data));
2593}
2594
2595// Close connection if a HEADERS frame is received after the trailing HEADERS.
2596TEST_P(QuicSpdyStreamTest, TrailersAfterTrailers) {
renjietange47e15f2019-10-29 14:19:30 -07002597 if (!UsesHttp3()) {
bnc519216c2019-07-09 05:03:48 -07002598 return;
2599 }
2600
2601 Initialize(kShouldProcessData);
2602
bnc2e963aa2019-07-31 18:03:17 -07002603 // Receive and consume headers.
fayang7e3c1a12019-08-02 08:07:13 -07002604 std::string headers = HeadersFrame({std::make_pair("foo", "bar")});
bnc519216c2019-07-09 05:03:48 -07002605 QuicStreamOffset offset = 0;
2606 stream_->OnStreamFrame(
2607 QuicStreamFrame(stream_->id(), false, offset, headers));
2608 offset += headers.size();
2609
2610 EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar")));
2611 stream_->ConsumeHeaderList();
2612
2613 // Receive data. It is consumed by TestStream.
2614 std::string data = DataFrame(kDataFramePayload);
2615 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, offset, data));
2616 offset += data.size();
2617
2618 EXPECT_EQ(kDataFramePayload, stream_->data());
2619
bnc2e963aa2019-07-31 18:03:17 -07002620 // Receive and consume trailers.
fayang7e3c1a12019-08-02 08:07:13 -07002621 std::string trailers1 =
2622 HeadersFrame({std::make_pair("custom-key", "custom-value")});
bnc519216c2019-07-09 05:03:48 -07002623 stream_->OnStreamFrame(
2624 QuicStreamFrame(stream_->id(), false, offset, trailers1));
2625 offset += trailers1.size();
2626
2627 EXPECT_TRUE(stream_->trailers_decompressed());
2628 EXPECT_THAT(stream_->received_trailers(),
2629 ElementsAre(Pair("custom-key", "custom-value")));
2630
2631 // Closing the connection is mocked out in tests. Instead, simply stop
2632 // reading data at the stream level to prevent QuicSpdyStream from blowing up.
bnc519216c2019-07-09 05:03:48 -07002633 EXPECT_CALL(
2634 *connection_,
bnc3e8fe7c2020-03-10 17:44:58 -07002635 CloseConnection(QUIC_HTTP_INVALID_FRAME_SEQUENCE_ON_SPDY_STREAM,
bnc519216c2019-07-09 05:03:48 -07002636 "HEADERS frame received after trailing HEADERS.",
2637 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET))
2638 .WillOnce(InvokeWithoutArgs([this]() { stream_->StopReading(); }));
2639
2640 // Receive another HEADERS frame, with no header fields.
bnc2e963aa2019-07-31 18:03:17 -07002641 std::string trailers2 = HeadersFrame(SpdyHeaderBlock());
bnc519216c2019-07-09 05:03:48 -07002642 stream_->OnStreamFrame(
2643 QuicStreamFrame(stream_->id(), false, offset, trailers2));
2644}
2645
2646// Regression test for https://crbug.com/978733.
2647// Close connection if a DATA frame is received after the trailing HEADERS.
2648TEST_P(QuicSpdyStreamTest, DataAfterTrailers) {
renjietange47e15f2019-10-29 14:19:30 -07002649 if (!UsesHttp3()) {
bnc519216c2019-07-09 05:03:48 -07002650 return;
2651 }
2652
2653 Initialize(kShouldProcessData);
2654
bnc2e963aa2019-07-31 18:03:17 -07002655 // Receive and consume headers.
fayang7e3c1a12019-08-02 08:07:13 -07002656 std::string headers = HeadersFrame({std::make_pair("foo", "bar")});
bnc519216c2019-07-09 05:03:48 -07002657 QuicStreamOffset offset = 0;
2658 stream_->OnStreamFrame(
2659 QuicStreamFrame(stream_->id(), false, offset, headers));
2660 offset += headers.size();
2661
2662 EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar")));
2663 stream_->ConsumeHeaderList();
2664
2665 // Receive data. It is consumed by TestStream.
2666 std::string data1 = DataFrame(kDataFramePayload);
2667 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, offset, data1));
2668 offset += data1.size();
2669 EXPECT_EQ(kDataFramePayload, stream_->data());
2670
2671 // Receive trailers, with single header field "custom-key: custom-value".
fayang7e3c1a12019-08-02 08:07:13 -07002672 std::string trailers =
2673 HeadersFrame({std::make_pair("custom-key", "custom-value")});
bnc519216c2019-07-09 05:03:48 -07002674 stream_->OnStreamFrame(
2675 QuicStreamFrame(stream_->id(), false, offset, trailers));
2676 offset += trailers.size();
2677
2678 EXPECT_THAT(stream_->received_trailers(),
2679 ElementsAre(Pair("custom-key", "custom-value")));
2680
2681 // Closing the connection is mocked out in tests. Instead, simply stop
2682 // reading data at the stream level to prevent QuicSpdyStream from blowing up.
bnc519216c2019-07-09 05:03:48 -07002683 EXPECT_CALL(
2684 *connection_,
bnc3e8fe7c2020-03-10 17:44:58 -07002685 CloseConnection(QUIC_HTTP_INVALID_FRAME_SEQUENCE_ON_SPDY_STREAM,
bnc519216c2019-07-09 05:03:48 -07002686 "Unexpected DATA frame received.",
2687 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET))
2688 .WillOnce(InvokeWithoutArgs([this]() { stream_->StopReading(); }));
2689
2690 // Receive more data.
2691 std::string data2 = DataFrame("This payload should not be proccessed.");
2692 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, offset, data2));
2693}
2694
bncef518152019-07-18 05:36:44 -07002695// SETTINGS frames are invalid on bidirectional streams. If one is received,
2696// the connection is closed. No more data should be processed.
2697TEST_P(QuicSpdyStreamTest, StopProcessingIfConnectionClosed) {
renjietange47e15f2019-10-29 14:19:30 -07002698 if (!UsesHttp3()) {
bncef518152019-07-18 05:36:44 -07002699 return;
2700 }
2701
2702 Initialize(kShouldProcessData);
2703
2704 // SETTINGS frame with empty payload.
dmcardleba2fb7e2019-12-13 07:44:34 -08002705 std::string settings = quiche::QuicheTextUtils::HexDecode("0400");
bnc2e963aa2019-07-31 18:03:17 -07002706
2707 // HEADERS frame.
bncef518152019-07-18 05:36:44 -07002708 // Since it arrives after a SETTINGS frame, it should never be read.
fayang7e3c1a12019-08-02 08:07:13 -07002709 std::string headers = HeadersFrame({std::make_pair("foo", "bar")});
bncef518152019-07-18 05:36:44 -07002710
2711 // Combine the two frames to make sure they are processed in a single
2712 // QuicSpdyStream::OnDataAvailable() call.
dmcardleba2fb7e2019-12-13 07:44:34 -08002713 std::string frames = quiche::QuicheStrCat(settings, headers);
bncef518152019-07-18 05:36:44 -07002714
2715 EXPECT_EQ(0u, stream_->sequencer()->NumBytesConsumed());
2716
bncdfabdfb2020-01-17 17:46:40 -08002717 EXPECT_CALL(*connection_,
2718 CloseConnection(QUIC_HTTP_FRAME_UNEXPECTED_ON_SPDY_STREAM, _, _))
bncef518152019-07-18 05:36:44 -07002719 .WillOnce(
2720 Invoke(connection_, &MockQuicConnection::ReallyCloseConnection));
2721 EXPECT_CALL(*connection_, SendConnectionClosePacket(_, _));
2722 EXPECT_CALL(*session_, OnConnectionClosed(_, _));
2723
2724 stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), /* fin = */ false,
2725 /* offset = */ 0, frames));
2726
2727 EXPECT_EQ(0u, stream_->sequencer()->NumBytesConsumed());
2728}
2729
bnc6f18a822019-11-27 17:50:38 -08002730// Stream Cancellation instruction is sent on QPACK decoder stream
2731// when stream is reset.
bnc84aa7e52019-12-13 05:25:21 -08002732TEST_P(QuicSpdyStreamTest, StreamCancellationWhenStreamReset) {
bnc6f18a822019-11-27 17:50:38 -08002733 if (!UsesHttp3()) {
2734 return;
2735 }
2736
2737 Initialize(kShouldProcessData);
2738
2739 auto qpack_decoder_stream =
2740 QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get());
renjietang41a1b412020-02-27 15:05:14 -08002741 EXPECT_CALL(*session_, WritevData(qpack_decoder_stream->id(), 1, 1, _, _, _));
bnc6f18a822019-11-27 17:50:38 -08002742 EXPECT_CALL(*session_,
2743 SendRstStream(stream_->id(), QUIC_STREAM_CANCELLED, 0));
2744
2745 stream_->Reset(QUIC_STREAM_CANCELLED);
2746}
2747
2748// Stream Cancellation instruction is sent on QPACK decoder stream
2749// when RESET_STREAM frame is received.
bnc84aa7e52019-12-13 05:25:21 -08002750TEST_P(QuicSpdyStreamTest, StreamCancellationOnResetReceived) {
bnc6f18a822019-11-27 17:50:38 -08002751 if (!UsesHttp3()) {
2752 return;
2753 }
2754
2755 Initialize(kShouldProcessData);
2756
2757 auto qpack_decoder_stream =
2758 QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get());
renjietang41a1b412020-02-27 15:05:14 -08002759 EXPECT_CALL(*session_, WritevData(qpack_decoder_stream->id(), 1, 1, _, _, _));
bnc6f18a822019-11-27 17:50:38 -08002760
2761 stream_->OnStreamReset(QuicRstStreamFrame(
2762 kInvalidControlFrameId, stream_->id(), QUIC_STREAM_CANCELLED, 0));
2763}
2764
QUICHE teama6ef0a62019-03-07 20:34:33 -05002765} // namespace
2766} // namespace test
2767} // namespace quic