blob: 4055b4c4c09fb5f84c9805c071e49331c2c20beb [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2012 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/quic_stream.h"
6
7#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
bnc463f2352019-10-10 04:49:34 -07009#include <utility>
QUICHE teama6ef0a62019-03-07 20:34:33 -050010
renjietang15afba32019-10-23 14:32:35 -070011#include "net/third_party/quiche/src/quic/core/frames/quic_rst_stream_frame.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/quic_connection.h"
renjietang39979d32019-09-13 10:02:13 -070013#include "net/third_party/quiche/src/quic/core/quic_constants.h"
renjietang15afba32019-10-23 14:32:35 -070014#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
renjietang39979d32019-09-13 10:02:13 -070015#include "net/third_party/quiche/src/quic/core/quic_types.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050016#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_flags.h"
21#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
22#include "net/third_party/quiche/src/quic/platform/api/quic_mem_slice_storage.h"
23#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050024#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
25#include "net/third_party/quiche/src/quic/platform/api/quic_test_mem_slice_vector.h"
26#include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h"
27#include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h"
28#include "net/third_party/quiche/src/quic/test_tools/quic_flow_controller_peer.h"
29#include "net/third_party/quiche/src/quic/test_tools/quic_session_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_stream_sequencer_peer.h"
32#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
bnc4e9283d2019-12-17 07:08:57 -080033#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
renjietang41a1b412020-02-27 15:05:14 -080034#include "net/third_party/quiche/src/common/platform/api/quiche_optional.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080035#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050036
37using testing::_;
38using testing::AnyNumber;
39using testing::AtLeast;
40using testing::InSequence;
41using testing::Invoke;
42using testing::InvokeWithoutArgs;
43using testing::Return;
44using testing::StrictMock;
45
46namespace quic {
47namespace test {
48namespace {
49
50const char kData1[] = "FooAndBar";
51const char kData2[] = "EepAndBaz";
52const size_t kDataLen = 9;
53
54class TestStream : public QuicStream {
55 public:
56 TestStream(QuicStreamId id, QuicSession* session, StreamType type)
renjietangb38e6352019-10-24 09:38:20 -070057 : QuicStream(id, session, /*is_static=*/false, type) {
58 sequencer()->set_level_triggered(true);
59 }
QUICHE teama6ef0a62019-03-07 20:34:33 -050060
renjietangbaea59c2019-05-29 15:08:14 -070061 TestStream(PendingStream* pending, StreamType type, bool is_static)
62 : QuicStream(pending, type, is_static) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050063
renjietangb38e6352019-10-24 09:38:20 -070064 MOCK_METHOD0(OnDataAvailable, void());
QUICHE teama6ef0a62019-03-07 20:34:33 -050065
66 MOCK_METHOD0(OnCanWriteNewData, void());
67
68 using QuicStream::CanWriteNewData;
69 using QuicStream::CanWriteNewDataAfterData;
70 using QuicStream::CloseWriteSide;
71 using QuicStream::fin_buffered;
72 using QuicStream::OnClose;
73 using QuicStream::WriteMemSlices;
74 using QuicStream::WriteOrBufferData;
QUICHE teama6ef0a62019-03-07 20:34:33 -050075
76 private:
vasilvvc48c8712019-03-11 13:38:16 -070077 std::string data_;
QUICHE teama6ef0a62019-03-07 20:34:33 -050078};
79
renjietang9bf25642019-10-10 10:21:15 -070080class QuicStreamTest : public QuicTestWithParam<ParsedQuicVersion> {
QUICHE teama6ef0a62019-03-07 20:34:33 -050081 public:
renjietang9bf25642019-10-10 10:21:15 -070082 QuicStreamTest()
renjietang39979d32019-09-13 10:02:13 -070083 : zero_(QuicTime::Delta::Zero()),
QUICHE teama6ef0a62019-03-07 20:34:33 -050084 supported_versions_(AllSupportedVersions()) {}
85
86 void Initialize() {
87 ParsedQuicVersionVector version_vector;
88 version_vector.push_back(GetParam());
89 connection_ = new StrictMock<MockQuicConnection>(
90 &helper_, &alarm_factory_, Perspective::IS_SERVER, version_vector);
91 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
vasilvv0fc587f2019-09-06 13:33:08 -070092 session_ = std::make_unique<StrictMock<MockQuicSession>>(connection_);
renjietang39979d32019-09-13 10:02:13 -070093 session_->Initialize();
QUICHE teama6ef0a62019-03-07 20:34:33 -050094
renjietang39979d32019-09-13 10:02:13 -070095 QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow(
96 session_->config(), kMinimumFlowControlSendWindow);
dschinazi18cdf132019-10-09 16:08:18 -070097 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesUnidirectional(
98 session_->config(), kMinimumFlowControlSendWindow);
99 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional(
100 session_->config(), kMinimumFlowControlSendWindow);
101 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesOutgoingBidirectional(
102 session_->config(), kMinimumFlowControlSendWindow);
renjietange6d94672020-01-07 10:30:10 -0800103 QuicConfigPeer::SetReceivedMaxUnidirectionalStreams(session_->config(), 10);
renjietang39979d32019-09-13 10:02:13 -0700104 session_->OnConfigNegotiated();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500105
renjietangb38e6352019-10-24 09:38:20 -0700106 stream_ = new StrictMock<TestStream>(kTestStreamId, session_.get(),
107 BIDIRECTIONAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500108 EXPECT_NE(nullptr, stream_);
109 // session_ now owns stream_.
110 session_->ActivateStream(QuicWrapUnique(stream_));
111 // Ignore resetting when session_ is terminated.
112 EXPECT_CALL(*session_, SendRstStream(kTestStreamId, _, _))
113 .Times(AnyNumber());
114 write_blocked_list_ =
115 QuicSessionPeer::GetWriteBlockedStreams(session_.get());
116 }
117
bncc7d9e0c2019-04-16 10:22:15 -0700118 bool fin_sent() { return stream_->fin_sent(); }
119 bool rst_sent() { return stream_->rst_sent(); }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500120
QUICHE teama6ef0a62019-03-07 20:34:33 -0500121 bool HasWriteBlockedStreams() {
122 return write_blocked_list_->HasWriteBlockedSpecialStream() ||
123 write_blocked_list_->HasWriteBlockedDataStreams();
124 }
125
renjietang41a1b412020-02-27 15:05:14 -0800126 QuicConsumedData CloseStreamOnWriteError(
127 QuicStreamId id,
128 size_t /*write_length*/,
129 QuicStreamOffset /*offset*/,
130 StreamSendingState /*state*/,
131 QuicByteCount /*bytes_written*/,
132 quiche::QuicheOptional<EncryptionLevel> /*level*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500133 session_->CloseStream(id);
134 return QuicConsumedData(1, false);
135 }
136
QUICHE teama6ef0a62019-03-07 20:34:33 -0500137 bool ClearResetStreamFrame(const QuicFrame& frame) {
138 EXPECT_EQ(RST_STREAM_FRAME, frame.type);
139 DeleteFrame(&const_cast<QuicFrame&>(frame));
140 return true;
141 }
142
143 bool ClearStopSendingFrame(const QuicFrame& frame) {
144 EXPECT_EQ(STOP_SENDING_FRAME, frame.type);
145 DeleteFrame(&const_cast<QuicFrame&>(frame));
146 return true;
147 }
148
149 protected:
150 MockQuicConnectionHelper helper_;
151 MockAlarmFactory alarm_factory_;
152 MockQuicConnection* connection_;
153 std::unique_ptr<MockQuicSession> session_;
renjietangb38e6352019-10-24 09:38:20 -0700154 StrictMock<TestStream>* stream_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500155 QuicWriteBlockedList* write_blocked_list_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500156 QuicTime::Delta zero_;
157 ParsedQuicVersionVector supported_versions_;
renjietang940a5322019-08-02 16:17:51 -0700158 QuicStreamId kTestStreamId =
159 GetNthClientInitiatedBidirectionalStreamId(GetParam().transport_version,
160 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500161};
162
renjietang9bf25642019-10-10 10:21:15 -0700163INSTANTIATE_TEST_SUITE_P(QuicStreamTests,
164 QuicStreamTest,
dschinazi142051a2019-09-18 18:17:29 -0700165 ::testing::ValuesIn(AllSupportedVersions()),
166 ::testing::PrintToStringParamName());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500167
renjietang35448992019-05-08 17:08:57 -0700168TEST_P(QuicStreamTest, PendingStreamStaticness) {
169 Initialize();
170
171 PendingStream pending(kTestStreamId + 2, session_.get());
renjietangbaea59c2019-05-29 15:08:14 -0700172 TestStream stream(&pending, StreamType::BIDIRECTIONAL, false);
renjietang35448992019-05-08 17:08:57 -0700173 EXPECT_FALSE(stream.is_static());
174
175 PendingStream pending2(kTestStreamId + 3, session_.get());
renjietangbaea59c2019-05-29 15:08:14 -0700176 TestStream stream2(&pending2, StreamType::BIDIRECTIONAL, true);
renjietang35448992019-05-08 17:08:57 -0700177 EXPECT_TRUE(stream2.is_static());
178}
179
QUICHE teama6ef0a62019-03-07 20:34:33 -0500180TEST_P(QuicStreamTest, PendingStreamTooMuchData) {
181 Initialize();
182
183 PendingStream pending(kTestStreamId + 2, session_.get());
184 // Receive a stream frame that violates flow control: the byte offset is
185 // higher than the receive window offset.
186 QuicStreamFrame frame(kTestStreamId + 2, false,
renjietangb38e6352019-10-24 09:38:20 -0700187 kInitialSessionFlowControlWindowForTest + 1, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500188
189 // Stream should not accept the frame, and the connection should be closed.
190 EXPECT_CALL(*connection_,
191 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _));
192 pending.OnStreamFrame(frame);
193}
194
195TEST_P(QuicStreamTest, PendingStreamTooMuchDataInRstStream) {
196 Initialize();
197
198 PendingStream pending(kTestStreamId + 2, session_.get());
199 // Receive a rst stream frame that violates flow control: the byte offset is
200 // higher than the receive window offset.
201 QuicRstStreamFrame frame(kInvalidControlFrameId, kTestStreamId + 2,
202 QUIC_STREAM_CANCELLED,
203 kInitialSessionFlowControlWindowForTest + 1);
204
205 // Pending stream should not accept the frame, and the connection should be
206 // closed.
207 EXPECT_CALL(*connection_,
208 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _));
209 pending.OnRstStreamFrame(frame);
210}
211
212TEST_P(QuicStreamTest, PendingStreamRstStream) {
213 Initialize();
214
215 PendingStream pending(kTestStreamId + 2, session_.get());
216 QuicStreamOffset final_byte_offset = 7;
217 QuicRstStreamFrame frame(kInvalidControlFrameId, kTestStreamId + 2,
218 QUIC_STREAM_CANCELLED, final_byte_offset);
219
220 // Pending stream should accept the frame and not close the connection.
221 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
222 pending.OnRstStreamFrame(frame);
223}
224
225TEST_P(QuicStreamTest, FromPendingStream) {
226 Initialize();
227
228 PendingStream pending(kTestStreamId + 2, session_.get());
229
renjietangb38e6352019-10-24 09:38:20 -0700230 QuicStreamFrame frame(kTestStreamId + 2, false, 2, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500231 pending.OnStreamFrame(frame);
232 pending.OnStreamFrame(frame);
renjietangb38e6352019-10-24 09:38:20 -0700233 QuicStreamFrame frame2(kTestStreamId + 2, true, 3, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500234 pending.OnStreamFrame(frame2);
235
renjietangbaea59c2019-05-29 15:08:14 -0700236 TestStream stream(&pending, StreamType::READ_UNIDIRECTIONAL, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500237 EXPECT_EQ(3, stream.num_frames_received());
238 EXPECT_EQ(3u, stream.stream_bytes_read());
239 EXPECT_EQ(1, stream.num_duplicate_frames_received());
240 EXPECT_EQ(true, stream.fin_received());
241 EXPECT_EQ(frame2.offset + 1,
242 stream.flow_controller()->highest_received_byte_offset());
243 EXPECT_EQ(frame2.offset + 1,
244 session_->flow_controller()->highest_received_byte_offset());
245}
246
247TEST_P(QuicStreamTest, FromPendingStreamThenData) {
248 Initialize();
249
250 PendingStream pending(kTestStreamId + 2, session_.get());
251
renjietangb38e6352019-10-24 09:38:20 -0700252 QuicStreamFrame frame(kTestStreamId + 2, false, 2, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500253 pending.OnStreamFrame(frame);
254
renjietangbaea59c2019-05-29 15:08:14 -0700255 auto stream =
256 new TestStream(&pending, StreamType::READ_UNIDIRECTIONAL, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500257 session_->ActivateStream(QuicWrapUnique(stream));
258
renjietangb38e6352019-10-24 09:38:20 -0700259 QuicStreamFrame frame2(kTestStreamId + 2, true, 3, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500260 stream->OnStreamFrame(frame2);
261
262 EXPECT_EQ(2, stream->num_frames_received());
263 EXPECT_EQ(2u, stream->stream_bytes_read());
264 EXPECT_EQ(true, stream->fin_received());
265 EXPECT_EQ(frame2.offset + 1,
266 stream->flow_controller()->highest_received_byte_offset());
267 EXPECT_EQ(frame2.offset + 1,
268 session_->flow_controller()->highest_received_byte_offset());
269}
270
271TEST_P(QuicStreamTest, WriteAllData) {
272 Initialize();
273
274 size_t length =
275 1 + QuicPacketCreator::StreamFramePacketOverhead(
276 connection_->transport_version(), PACKET_8BYTE_CONNECTION_ID,
277 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
278 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
279 VARIABLE_LENGTH_INTEGER_LENGTH_0,
280 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0u);
281 connection_->SetMaxPacketLength(length);
282
renjietang41a1b412020-02-27 15:05:14 -0800283 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800284 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500285 stream_->WriteOrBufferData(kData1, false, nullptr);
286 EXPECT_FALSE(HasWriteBlockedStreams());
287}
288
289TEST_P(QuicStreamTest, NoBlockingIfNoDataOrFin) {
290 Initialize();
291
292 // Write no data and no fin. If we consume nothing we should not be write
293 // blocked.
dmcardlecf0bfcf2019-12-13 08:08:21 -0800294 EXPECT_QUIC_BUG(
295 stream_->WriteOrBufferData(quiche::QuicheStringPiece(), false, nullptr),
296 "");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500297 EXPECT_FALSE(HasWriteBlockedStreams());
298}
299
300TEST_P(QuicStreamTest, BlockIfOnlySomeDataConsumed) {
301 Initialize();
302
303 // Write some data and no fin. If we consume some but not all of the data,
304 // we should be write blocked a not all the data was consumed.
renjietang41a1b412020-02-27 15:05:14 -0800305 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500306 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -0800307 return session_->ConsumeData(stream_->id(), 1u, 0u, NO_FIN, false,
308 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500309 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800310 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), false,
311 nullptr);
fayang8265a2a2019-10-16 11:23:51 -0700312 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500313 ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
314 EXPECT_EQ(1u, stream_->BufferedDataBytes());
315}
316
317TEST_P(QuicStreamTest, BlockIfFinNotConsumedWithData) {
318 Initialize();
319
320 // Write some data and no fin. If we consume all the data but not the fin,
321 // we should be write blocked because the fin was not consumed.
322 // (This should never actually happen as the fin should be sent out with the
323 // last data)
renjietang41a1b412020-02-27 15:05:14 -0800324 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500325 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -0800326 return session_->ConsumeData(stream_->id(), 2u, 0u, NO_FIN, false,
327 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500328 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800329 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), true,
330 nullptr);
fayang8265a2a2019-10-16 11:23:51 -0700331 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500332 ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
333}
334
335TEST_P(QuicStreamTest, BlockIfSoloFinNotConsumed) {
336 Initialize();
337
338 // Write no data and a fin. If we consume nothing we should be write blocked,
339 // as the fin was not consumed.
renjietang41a1b412020-02-27 15:05:14 -0800340 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500341 .WillOnce(Return(QuicConsumedData(0, false)));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800342 stream_->WriteOrBufferData(quiche::QuicheStringPiece(), true, nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500343 ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
344}
345
346TEST_P(QuicStreamTest, CloseOnPartialWrite) {
347 Initialize();
348
349 // Write some data and no fin. However, while writing the data
350 // close the stream and verify that MarkConnectionLevelWriteBlocked does not
351 // crash with an unknown stream.
renjietang41a1b412020-02-27 15:05:14 -0800352 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500353 .WillOnce(Invoke(this, &QuicStreamTest::CloseStreamOnWriteError));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800354 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), false,
355 nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500356 ASSERT_EQ(0u, write_blocked_list_->NumBlockedStreams());
357}
358
359TEST_P(QuicStreamTest, WriteOrBufferData) {
360 Initialize();
361
362 EXPECT_FALSE(HasWriteBlockedStreams());
363 size_t length =
364 1 + QuicPacketCreator::StreamFramePacketOverhead(
365 connection_->transport_version(), PACKET_8BYTE_CONNECTION_ID,
366 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
367 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
368 VARIABLE_LENGTH_INTEGER_LENGTH_0,
369 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0u);
370 connection_->SetMaxPacketLength(length);
371
renjietang41a1b412020-02-27 15:05:14 -0800372 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500373 .WillOnce(InvokeWithoutArgs([this]() {
renjietang7c239172020-02-21 13:50:39 -0800374 return session_->ConsumeData(stream_->id(), kDataLen - 1, 0u, NO_FIN,
renjietang41a1b412020-02-27 15:05:14 -0800375 false, QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500376 }));
377 stream_->WriteOrBufferData(kData1, false, nullptr);
zhongyi1b2f7832019-06-14 13:31:34 -0700378
fayang8265a2a2019-10-16 11:23:51 -0700379 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500380 EXPECT_EQ(1u, stream_->BufferedDataBytes());
381 EXPECT_TRUE(HasWriteBlockedStreams());
382
383 // Queue a bytes_consumed write.
384 stream_->WriteOrBufferData(kData2, false, nullptr);
385 EXPECT_EQ(10u, stream_->BufferedDataBytes());
386 // Make sure we get the tail of the first write followed by the bytes_consumed
387 InSequence s;
renjietang41a1b412020-02-27 15:05:14 -0800388 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500389 .WillOnce(InvokeWithoutArgs([this]() {
renjietang7c239172020-02-21 13:50:39 -0800390 return session_->ConsumeData(stream_->id(), kDataLen - 1, kDataLen - 1,
renjietang41a1b412020-02-27 15:05:14 -0800391 NO_FIN, false, QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500392 }));
renjietangb38e6352019-10-24 09:38:20 -0700393 EXPECT_CALL(*stream_, OnCanWriteNewData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500394 stream_->OnCanWrite();
fayang8265a2a2019-10-16 11:23:51 -0700395 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500396
397 // And finally the end of the bytes_consumed.
renjietang41a1b412020-02-27 15:05:14 -0800398 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500399 .WillOnce(InvokeWithoutArgs([this]() {
renjietang7c239172020-02-21 13:50:39 -0800400 return session_->ConsumeData(stream_->id(), 2u, 2 * kDataLen - 2,
renjietang41a1b412020-02-27 15:05:14 -0800401 NO_FIN, false, QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500402 }));
renjietangb38e6352019-10-24 09:38:20 -0700403 EXPECT_CALL(*stream_, OnCanWriteNewData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500404 stream_->OnCanWrite();
fayang8265a2a2019-10-16 11:23:51 -0700405 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500406}
407
408TEST_P(QuicStreamTest, WriteOrBufferDataReachStreamLimit) {
409 Initialize();
vasilvvc48c8712019-03-11 13:38:16 -0700410 std::string data("aaaaa");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500411 QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - data.length(),
412 stream_);
renjietang41a1b412020-02-27 15:05:14 -0800413 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800414 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500415 stream_->WriteOrBufferData(data, false, nullptr);
fayang8265a2a2019-10-16 11:23:51 -0700416 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500417 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
418 EXPECT_QUIC_BUG(stream_->WriteOrBufferData("a", false, nullptr),
419 "Write too many data via stream");
420}
421
422TEST_P(QuicStreamTest, ConnectionCloseAfterStreamClose) {
423 Initialize();
424
425 QuicStreamPeer::CloseReadSide(stream_);
426 stream_->CloseWriteSide();
bncf6f82b12019-10-30 07:01:01 -0700427 EXPECT_THAT(stream_->stream_error(), IsQuicStreamNoError());
428 EXPECT_THAT(stream_->connection_error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500429 stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR,
430 ConnectionCloseSource::FROM_SELF);
bncf6f82b12019-10-30 07:01:01 -0700431 EXPECT_THAT(stream_->stream_error(), IsQuicStreamNoError());
432 EXPECT_THAT(stream_->connection_error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500433}
434
435TEST_P(QuicStreamTest, RstAlwaysSentIfNoFinSent) {
436 // For flow control accounting, a stream must send either a FIN or a RST frame
437 // before termination.
438 // Test that if no FIN has been sent, we send a RST.
439
440 Initialize();
441 EXPECT_FALSE(fin_sent());
442 EXPECT_FALSE(rst_sent());
443
444 // Write some data, with no FIN.
renjietang41a1b412020-02-27 15:05:14 -0800445 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500446 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -0800447 return session_->ConsumeData(stream_->id(), 1u, 0u, NO_FIN, false,
448 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500449 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800450 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 1), false,
451 nullptr);
fayang8265a2a2019-10-16 11:23:51 -0700452 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500453 EXPECT_FALSE(fin_sent());
454 EXPECT_FALSE(rst_sent());
455
456 // Now close the stream, and expect that we send a RST.
457 EXPECT_CALL(*session_, SendRstStream(_, _, _));
458 stream_->OnClose();
zhongyi9fa2be32019-09-10 12:39:01 -0700459 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500460 EXPECT_FALSE(fin_sent());
461 EXPECT_TRUE(rst_sent());
462}
463
464TEST_P(QuicStreamTest, RstNotSentIfFinSent) {
465 // For flow control accounting, a stream must send either a FIN or a RST frame
466 // before termination.
467 // Test that if a FIN has been sent, we don't also send a RST.
468
469 Initialize();
470 EXPECT_FALSE(fin_sent());
471 EXPECT_FALSE(rst_sent());
472
473 // Write some data, with FIN.
renjietang41a1b412020-02-27 15:05:14 -0800474 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500475 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -0800476 return session_->ConsumeData(stream_->id(), 1u, 0u, FIN, false,
477 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500478 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800479 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 1), true,
480 nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500481 EXPECT_TRUE(fin_sent());
482 EXPECT_FALSE(rst_sent());
483
484 // Now close the stream, and expect that we do not send a RST.
485 stream_->OnClose();
486 EXPECT_TRUE(fin_sent());
487 EXPECT_FALSE(rst_sent());
488}
489
490TEST_P(QuicStreamTest, OnlySendOneRst) {
491 // For flow control accounting, a stream must send either a FIN or a RST frame
492 // before termination.
493 // Test that if a stream sends a RST, it doesn't send an additional RST during
494 // OnClose() (this shouldn't be harmful, but we shouldn't do it anyway...)
495
496 Initialize();
497 EXPECT_FALSE(fin_sent());
498 EXPECT_FALSE(rst_sent());
499
500 // Reset the stream.
501 const int expected_resets = 1;
502 EXPECT_CALL(*session_, SendRstStream(_, _, _)).Times(expected_resets);
503 stream_->Reset(QUIC_STREAM_CANCELLED);
504 EXPECT_FALSE(fin_sent());
505 EXPECT_TRUE(rst_sent());
506
507 // Now close the stream (any further resets being sent would break the
508 // expectation above).
509 stream_->OnClose();
510 EXPECT_FALSE(fin_sent());
511 EXPECT_TRUE(rst_sent());
512}
513
514TEST_P(QuicStreamTest, StreamFlowControlMultipleWindowUpdates) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500515 Initialize();
516
517 // If we receive multiple WINDOW_UPDATES (potentially out of order), then we
518 // want to make sure we latch the largest offset we see.
519
520 // Initially should be default.
521 EXPECT_EQ(
renjietang39979d32019-09-13 10:02:13 -0700522 kMinimumFlowControlSendWindow,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500523 QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller()));
524
525 // Check a single WINDOW_UPDATE results in correct offset.
526 QuicWindowUpdateFrame window_update_1(kInvalidControlFrameId, stream_->id(),
renjietang39979d32019-09-13 10:02:13 -0700527 kMinimumFlowControlSendWindow + 5);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500528 stream_->OnWindowUpdateFrame(window_update_1);
renjietangd088eab2019-11-21 14:54:41 -0800529 EXPECT_EQ(window_update_1.max_data, QuicFlowControllerPeer::SendWindowOffset(
530 stream_->flow_controller()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500531
532 // Now send a few more WINDOW_UPDATES and make sure that only the largest is
533 // remembered.
534 QuicWindowUpdateFrame window_update_2(kInvalidControlFrameId, stream_->id(),
535 1);
536 QuicWindowUpdateFrame window_update_3(kInvalidControlFrameId, stream_->id(),
renjietang39979d32019-09-13 10:02:13 -0700537 kMinimumFlowControlSendWindow + 10);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500538 QuicWindowUpdateFrame window_update_4(kInvalidControlFrameId, stream_->id(),
539 5678);
540 stream_->OnWindowUpdateFrame(window_update_2);
541 stream_->OnWindowUpdateFrame(window_update_3);
542 stream_->OnWindowUpdateFrame(window_update_4);
renjietangd088eab2019-11-21 14:54:41 -0800543 EXPECT_EQ(window_update_3.max_data, QuicFlowControllerPeer::SendWindowOffset(
544 stream_->flow_controller()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500545}
546
547TEST_P(QuicStreamTest, FrameStats) {
548 Initialize();
549
550 EXPECT_EQ(0, stream_->num_frames_received());
551 EXPECT_EQ(0, stream_->num_duplicate_frames_received());
renjietangb38e6352019-10-24 09:38:20 -0700552 QuicStreamFrame frame(stream_->id(), false, 0, ".");
553 EXPECT_CALL(*stream_, OnDataAvailable()).Times(2);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500554 stream_->OnStreamFrame(frame);
555 EXPECT_EQ(1, stream_->num_frames_received());
556 EXPECT_EQ(0, stream_->num_duplicate_frames_received());
557 stream_->OnStreamFrame(frame);
558 EXPECT_EQ(2, stream_->num_frames_received());
559 EXPECT_EQ(1, stream_->num_duplicate_frames_received());
renjietangb38e6352019-10-24 09:38:20 -0700560 QuicStreamFrame frame2(stream_->id(), false, 1, "abc");
561 stream_->OnStreamFrame(frame2);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500562}
563
564// Verify that when we receive a packet which violates flow control (i.e. sends
565// too much data on the stream) that the stream sequencer never sees this frame,
566// as we check for violation and close the connection early.
567TEST_P(QuicStreamTest, StreamSequencerNeverSeesPacketsViolatingFlowControl) {
568 Initialize();
569
570 // Receive a stream frame that violates flow control: the byte offset is
571 // higher than the receive window offset.
572 QuicStreamFrame frame(stream_->id(), false,
renjietangb38e6352019-10-24 09:38:20 -0700573 kInitialSessionFlowControlWindowForTest + 1, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500574 EXPECT_GT(frame.offset, QuicFlowControllerPeer::ReceiveWindowOffset(
575 stream_->flow_controller()));
576
577 // Stream should not accept the frame, and the connection should be closed.
578 EXPECT_CALL(*connection_,
579 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _));
580 stream_->OnStreamFrame(frame);
581}
582
583// Verify that after the consumer calls StopReading(), the stream still sends
584// flow control updates.
585TEST_P(QuicStreamTest, StopReadingSendsFlowControl) {
586 Initialize();
587
588 stream_->StopReading();
589
590 // Connection should not get terminated due to flow control errors.
591 EXPECT_CALL(*connection_,
592 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _))
593 .Times(0);
594 EXPECT_CALL(*connection_, SendControlFrame(_))
595 .Times(AtLeast(1))
bnc5b3c3be2019-06-25 10:37:09 -0700596 .WillRepeatedly(Invoke(&ClearControlFrame));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500597
vasilvvc48c8712019-03-11 13:38:16 -0700598 std::string data(1000, 'x');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500599 for (QuicStreamOffset offset = 0;
600 offset < 2 * kInitialStreamFlowControlWindowForTest;
601 offset += data.length()) {
602 QuicStreamFrame frame(stream_->id(), false, offset, data);
603 stream_->OnStreamFrame(frame);
604 }
605 EXPECT_LT(
606 kInitialStreamFlowControlWindowForTest,
607 QuicFlowControllerPeer::ReceiveWindowOffset(stream_->flow_controller()));
608}
609
610TEST_P(QuicStreamTest, FinalByteOffsetFromFin) {
611 Initialize();
612
renjietang6c066562019-11-04 17:05:59 -0800613 EXPECT_FALSE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500614
renjietangb38e6352019-10-24 09:38:20 -0700615 QuicStreamFrame stream_frame_no_fin(stream_->id(), false, 1234, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500616 stream_->OnStreamFrame(stream_frame_no_fin);
renjietang6c066562019-11-04 17:05:59 -0800617 EXPECT_FALSE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500618
renjietangb38e6352019-10-24 09:38:20 -0700619 QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500620 stream_->OnStreamFrame(stream_frame_with_fin);
renjietang6c066562019-11-04 17:05:59 -0800621 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500622}
623
624TEST_P(QuicStreamTest, FinalByteOffsetFromRst) {
625 Initialize();
626
renjietang6c066562019-11-04 17:05:59 -0800627 EXPECT_FALSE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500628 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
629 QUIC_STREAM_CANCELLED, 1234);
630 stream_->OnStreamReset(rst_frame);
renjietang6c066562019-11-04 17:05:59 -0800631 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500632}
633
634TEST_P(QuicStreamTest, InvalidFinalByteOffsetFromRst) {
635 Initialize();
636
renjietang6c066562019-11-04 17:05:59 -0800637 EXPECT_FALSE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500638 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
639 QUIC_STREAM_CANCELLED, 0xFFFFFFFFFFFF);
640 // Stream should not accept the frame, and the connection should be closed.
641 EXPECT_CALL(*connection_,
642 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _));
643 stream_->OnStreamReset(rst_frame);
renjietang6c066562019-11-04 17:05:59 -0800644 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500645 stream_->OnClose();
646}
647
648TEST_P(QuicStreamTest, FinalByteOffsetFromZeroLengthStreamFrame) {
649 // When receiving Trailers, an empty stream frame is created with the FIN set,
650 // and is passed to OnStreamFrame. The Trailers may be sent in advance of
651 // queued body bytes being sent, and thus the final byte offset may exceed
652 // current flow control limits. Flow control should only be concerned with
653 // data that has actually been sent/received, so verify that flow control
654 // ignores such a stream frame.
655 Initialize();
656
renjietang6c066562019-11-04 17:05:59 -0800657 EXPECT_FALSE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500658 const QuicStreamOffset kByteOffsetExceedingFlowControlWindow =
659 kInitialSessionFlowControlWindowForTest + 1;
660 const QuicStreamOffset current_stream_flow_control_offset =
661 QuicFlowControllerPeer::ReceiveWindowOffset(stream_->flow_controller());
662 const QuicStreamOffset current_connection_flow_control_offset =
663 QuicFlowControllerPeer::ReceiveWindowOffset(session_->flow_controller());
664 ASSERT_GT(kByteOffsetExceedingFlowControlWindow,
665 current_stream_flow_control_offset);
666 ASSERT_GT(kByteOffsetExceedingFlowControlWindow,
667 current_connection_flow_control_offset);
668 QuicStreamFrame zero_length_stream_frame_with_fin(
669 stream_->id(), /*fin=*/true, kByteOffsetExceedingFlowControlWindow,
dmcardlecf0bfcf2019-12-13 08:08:21 -0800670 quiche::QuicheStringPiece());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500671 EXPECT_EQ(0, zero_length_stream_frame_with_fin.data_length);
672
673 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
674 stream_->OnStreamFrame(zero_length_stream_frame_with_fin);
renjietang6c066562019-11-04 17:05:59 -0800675 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500676
677 // The flow control receive offset values should not have changed.
678 EXPECT_EQ(
679 current_stream_flow_control_offset,
680 QuicFlowControllerPeer::ReceiveWindowOffset(stream_->flow_controller()));
681 EXPECT_EQ(
682 current_connection_flow_control_offset,
683 QuicFlowControllerPeer::ReceiveWindowOffset(session_->flow_controller()));
684}
685
686TEST_P(QuicStreamTest, OnStreamResetOffsetOverflow) {
687 Initialize();
688 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
689 QUIC_STREAM_CANCELLED, kMaxStreamLength + 1);
690 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
691 stream_->OnStreamReset(rst_frame);
692}
693
694TEST_P(QuicStreamTest, OnStreamFrameUpperLimit) {
695 Initialize();
696
697 // Modify receive window offset and sequencer buffer total_bytes_read_ to
698 // avoid flow control violation.
699 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
700 kMaxStreamLength + 5u);
701 QuicFlowControllerPeer::SetReceiveWindowOffset(session_->flow_controller(),
702 kMaxStreamLength + 5u);
703 QuicStreamSequencerPeer::SetFrameBufferTotalBytesRead(
704 QuicStreamPeer::sequencer(stream_), kMaxStreamLength - 10u);
705
706 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _))
707 .Times(0);
renjietangb38e6352019-10-24 09:38:20 -0700708 QuicStreamFrame stream_frame(stream_->id(), false, kMaxStreamLength - 1, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500709 stream_->OnStreamFrame(stream_frame);
renjietangb38e6352019-10-24 09:38:20 -0700710 QuicStreamFrame stream_frame2(stream_->id(), true, kMaxStreamLength, "");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500711 stream_->OnStreamFrame(stream_frame2);
712}
713
714TEST_P(QuicStreamTest, StreamTooLong) {
715 Initialize();
716 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _))
717 .Times(1);
renjietangb38e6352019-10-24 09:38:20 -0700718 QuicStreamFrame stream_frame(stream_->id(), false, kMaxStreamLength, ".");
dmcardlecf0bfcf2019-12-13 08:08:21 -0800719 EXPECT_QUIC_PEER_BUG(
720 stream_->OnStreamFrame(stream_frame),
721 quiche::QuicheStrCat("Receive stream frame on stream ", stream_->id(),
722 " reaches max stream length"));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500723}
724
renjietang9bf25642019-10-10 10:21:15 -0700725TEST_P(QuicStreamTest, SetDrainingIncomingOutgoing) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500726 // Don't have incoming data consumed.
727 Initialize();
728
729 // Incoming data with FIN.
renjietangb38e6352019-10-24 09:38:20 -0700730 QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500731 stream_->OnStreamFrame(stream_frame_with_fin);
732 // The FIN has been received but not consumed.
renjietang6c066562019-11-04 17:05:59 -0800733 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500734 EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_));
735 EXPECT_FALSE(stream_->reading_stopped());
736
737 EXPECT_EQ(1u, session_->GetNumOpenIncomingStreams());
738
739 // Outgoing data with FIN.
renjietang41a1b412020-02-27 15:05:14 -0800740 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500741 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -0800742 return session_->ConsumeData(stream_->id(), 2u, 0u, FIN, false,
743 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500744 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800745 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), true,
746 nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500747 EXPECT_TRUE(stream_->write_side_closed());
748
749 EXPECT_EQ(1u, QuicSessionPeer::GetDrainingStreams(session_.get())
750 ->count(kTestStreamId));
751 EXPECT_EQ(0u, session_->GetNumOpenIncomingStreams());
752}
753
renjietang9bf25642019-10-10 10:21:15 -0700754TEST_P(QuicStreamTest, SetDrainingOutgoingIncoming) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500755 // Don't have incoming data consumed.
756 Initialize();
757
758 // Outgoing data with FIN.
renjietang41a1b412020-02-27 15:05:14 -0800759 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500760 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -0800761 return session_->ConsumeData(stream_->id(), 2u, 0u, FIN, false,
762 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500763 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800764 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), true,
765 nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500766 EXPECT_TRUE(stream_->write_side_closed());
767
768 EXPECT_EQ(1u, session_->GetNumOpenIncomingStreams());
769
770 // Incoming data with FIN.
renjietangb38e6352019-10-24 09:38:20 -0700771 QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500772 stream_->OnStreamFrame(stream_frame_with_fin);
773 // The FIN has been received but not consumed.
renjietang6c066562019-11-04 17:05:59 -0800774 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500775 EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_));
776 EXPECT_FALSE(stream_->reading_stopped());
777
778 EXPECT_EQ(1u, QuicSessionPeer::GetDrainingStreams(session_.get())
779 ->count(kTestStreamId));
780 EXPECT_EQ(0u, session_->GetNumOpenIncomingStreams());
781}
782
783TEST_P(QuicStreamTest, EarlyResponseFinHandling) {
784 // Verify that if the server completes the response before reading the end of
785 // the request, the received FIN is recorded.
786
787 Initialize();
788 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
renjietang41a1b412020-02-27 15:05:14 -0800789 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800790 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500791
792 // Receive data for the request.
renjietangb38e6352019-10-24 09:38:20 -0700793 EXPECT_CALL(*stream_, OnDataAvailable()).Times(1);
794 QuicStreamFrame frame1(stream_->id(), false, 0, "Start");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500795 stream_->OnStreamFrame(frame1);
796 // When QuicSimpleServerStream sends the response, it calls
797 // QuicStream::CloseReadSide() first.
798 QuicStreamPeer::CloseReadSide(stream_);
799 // Send data and FIN for the response.
800 stream_->WriteOrBufferData(kData1, false, nullptr);
801 EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_));
802 // Receive remaining data and FIN for the request.
renjietangb38e6352019-10-24 09:38:20 -0700803 QuicStreamFrame frame2(stream_->id(), true, 0, "End");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500804 stream_->OnStreamFrame(frame2);
805 EXPECT_TRUE(stream_->fin_received());
renjietang6c066562019-11-04 17:05:59 -0800806 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500807}
808
809TEST_P(QuicStreamTest, StreamWaitsForAcks) {
810 Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800811 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800812 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500813 // Stream is not waiting for acks initially.
814 EXPECT_FALSE(stream_->IsWaitingForAcks());
815 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
zhongyi9fa2be32019-09-10 12:39:01 -0700816 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500817
818 // Send kData1.
819 stream_->WriteOrBufferData(kData1, false, nullptr);
fayang8265a2a2019-10-16 11:23:51 -0700820 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500821 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
822 EXPECT_TRUE(stream_->IsWaitingForAcks());
823 QuicByteCount newly_acked_length = 0;
824 EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800825 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500826 &newly_acked_length));
827 EXPECT_EQ(9u, newly_acked_length);
828 // Stream is not waiting for acks as all sent data is acked.
829 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700830 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500831 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
832
833 // Send kData2.
834 stream_->WriteOrBufferData(kData2, false, nullptr);
835 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700836 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500837 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
838 // Send FIN.
839 stream_->WriteOrBufferData("", true, nullptr);
840 // Fin only frame is not stored in send buffer.
841 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
842
843 // kData2 is retransmitted.
844 stream_->OnStreamFrameRetransmitted(9, 9, false);
845
846 // kData2 is acked.
847 EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800848 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500849 &newly_acked_length));
850 EXPECT_EQ(9u, newly_acked_length);
851 // Stream is waiting for acks as FIN is not acked.
852 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700853 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500854 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
855
856 // FIN is acked.
857 EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 0, true, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800858 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500859 &newly_acked_length));
860 EXPECT_EQ(0u, newly_acked_length);
861 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700862 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500863 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
864}
865
866TEST_P(QuicStreamTest, StreamDataGetAckedOutOfOrder) {
867 Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800868 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800869 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500870 // Send data.
871 stream_->WriteOrBufferData(kData1, false, nullptr);
872 stream_->WriteOrBufferData(kData1, false, nullptr);
873 stream_->WriteOrBufferData(kData1, false, nullptr);
874 stream_->WriteOrBufferData("", true, nullptr);
875 EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size());
876 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700877 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500878 QuicByteCount newly_acked_length = 0;
879 EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800880 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500881 &newly_acked_length));
fayang8265a2a2019-10-16 11:23:51 -0700882 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500883 EXPECT_EQ(9u, newly_acked_length);
884 EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size());
885 EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800886 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500887 &newly_acked_length));
fayang8265a2a2019-10-16 11:23:51 -0700888 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500889 EXPECT_EQ(9u, newly_acked_length);
890 EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size());
891 EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800892 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500893 &newly_acked_length));
fayang8265a2a2019-10-16 11:23:51 -0700894 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500895 EXPECT_EQ(9u, newly_acked_length);
896 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
897 // FIN is not acked yet.
898 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700899 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500900 EXPECT_TRUE(stream_->OnStreamFrameAcked(27, 0, true, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800901 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500902 &newly_acked_length));
903 EXPECT_EQ(0u, newly_acked_length);
904 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700905 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500906}
907
908TEST_P(QuicStreamTest, CancelStream) {
909 Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800910 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800911 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500912 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700913 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500914 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
915
916 stream_->WriteOrBufferData(kData1, false, nullptr);
917 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700918 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500919 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
920 // Cancel stream.
921 stream_->Reset(QUIC_STREAM_NO_ERROR);
922 // stream still waits for acks as the error code is QUIC_STREAM_NO_ERROR, and
923 // data is going to be retransmitted.
924 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700925 EXPECT_TRUE(session_->HasUnackedStreamData());
zhongyi1b2f7832019-06-14 13:31:34 -0700926 EXPECT_CALL(*connection_,
927 OnStreamReset(stream_->id(), QUIC_STREAM_CANCELLED));
renjietangeab92852019-10-25 12:16:14 -0700928 EXPECT_CALL(*connection_, SendControlFrame(_))
929 .Times(AtLeast(1))
930 .WillRepeatedly(Invoke(&ClearControlFrame));
zhongyi1b2f7832019-06-14 13:31:34 -0700931 EXPECT_CALL(*session_, SendRstStream(stream_->id(), QUIC_STREAM_CANCELLED, 9))
932 .WillOnce(InvokeWithoutArgs([this]() {
renjietang61cc2452019-11-26 10:57:10 -0800933 session_->ReallySendRstStream(stream_->id(), QUIC_STREAM_CANCELLED,
934 stream_->stream_bytes_written());
zhongyi1b2f7832019-06-14 13:31:34 -0700935 }));
936
QUICHE teama6ef0a62019-03-07 20:34:33 -0500937 stream_->Reset(QUIC_STREAM_CANCELLED);
938 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
939 // Stream stops waiting for acks as data is not going to be retransmitted.
940 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700941 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500942}
943
944TEST_P(QuicStreamTest, RstFrameReceivedStreamNotFinishSending) {
renjietang9bf25642019-10-10 10:21:15 -0700945 if (VersionHasIetfQuicFrames(GetParam().transport_version)) {
946 // In IETF QUIC, receiving a RESET_STREAM will only close the read side. The
947 // stream itself is not closed and will not send reset.
948 return;
949 }
950
QUICHE teama6ef0a62019-03-07 20:34:33 -0500951 Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800952 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800953 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500954 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700955 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500956 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
957
958 stream_->WriteOrBufferData(kData1, false, nullptr);
959 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700960 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500961 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
962
963 // RST_STREAM received.
964 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
965 QUIC_STREAM_CANCELLED, 9);
966 EXPECT_CALL(*session_,
967 SendRstStream(stream_->id(), QUIC_RST_ACKNOWLEDGEMENT, 9));
968 stream_->OnStreamReset(rst_frame);
969 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
970 // Stream stops waiting for acks as it does not finish sending and rst is
971 // sent.
972 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700973 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500974}
975
976TEST_P(QuicStreamTest, RstFrameReceivedStreamFinishSending) {
977 Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800978 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800979 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500980 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700981 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500982 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
983
984 stream_->WriteOrBufferData(kData1, true, nullptr);
985 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700986 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500987
988 // RST_STREAM received.
989 EXPECT_CALL(*session_, SendRstStream(_, _, _)).Times(0);
990 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
991 QUIC_STREAM_CANCELLED, 1234);
992 stream_->OnStreamReset(rst_frame);
993 // Stream still waits for acks as it finishes sending and has unacked data.
994 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700995 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500996 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
997}
998
999TEST_P(QuicStreamTest, ConnectionClosed) {
1000 Initialize();
renjietang41a1b412020-02-27 15:05:14 -08001001 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001002 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001003 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -07001004 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001005 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1006
1007 stream_->WriteOrBufferData(kData1, false, nullptr);
1008 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -07001009 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001010 EXPECT_CALL(*session_,
1011 SendRstStream(stream_->id(), QUIC_RST_ACKNOWLEDGEMENT, 9));
1012 stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR,
1013 ConnectionCloseSource::FROM_SELF);
1014 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1015 // Stream stops waiting for acks as connection is going to close.
1016 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -07001017 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001018}
1019
1020TEST_P(QuicStreamTest, CanWriteNewDataAfterData) {
wub49855982019-05-01 14:16:26 -07001021 SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001022 Initialize();
1023 EXPECT_TRUE(stream_->CanWriteNewDataAfterData(99));
1024 EXPECT_FALSE(stream_->CanWriteNewDataAfterData(100));
1025}
1026
1027TEST_P(QuicStreamTest, WriteBufferedData) {
1028 // Set buffered data low water mark to be 100.
wub49855982019-05-01 14:16:26 -07001029 SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001030
1031 Initialize();
vasilvvc48c8712019-03-11 13:38:16 -07001032 std::string data(1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001033 EXPECT_TRUE(stream_->CanWriteNewData());
1034
1035 // Testing WriteOrBufferData.
renjietang41a1b412020-02-27 15:05:14 -08001036 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001037 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -08001038 return session_->ConsumeData(stream_->id(), 100u, 0u, NO_FIN, false,
1039 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001040 }));
1041 stream_->WriteOrBufferData(data, false, nullptr);
1042 stream_->WriteOrBufferData(data, false, nullptr);
1043 stream_->WriteOrBufferData(data, false, nullptr);
zhongyi1b2f7832019-06-14 13:31:34 -07001044 EXPECT_TRUE(stream_->IsWaitingForAcks());
1045
QUICHE teama6ef0a62019-03-07 20:34:33 -05001046 // Verify all data is saved.
1047 EXPECT_EQ(3 * data.length() - 100, stream_->BufferedDataBytes());
1048
renjietang41a1b412020-02-27 15:05:14 -08001049 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001050 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -08001051 return session_->ConsumeData(stream_->id(), 100, 100u, NO_FIN, false,
1052 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001053 }));
1054 // Buffered data size > threshold, do not ask upper layer for more data.
1055 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(0);
1056 stream_->OnCanWrite();
1057 EXPECT_EQ(3 * data.length() - 200, stream_->BufferedDataBytes());
1058 EXPECT_FALSE(stream_->CanWriteNewData());
1059
1060 // Send buffered data to make buffered data size < threshold.
1061 size_t data_to_write = 3 * data.length() - 200 -
1062 GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1;
renjietang41a1b412020-02-27 15:05:14 -08001063 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001064 .WillOnce(InvokeWithoutArgs([this, data_to_write]() {
renjietang7c239172020-02-21 13:50:39 -08001065 return session_->ConsumeData(stream_->id(), data_to_write, 200u, NO_FIN,
renjietang41a1b412020-02-27 15:05:14 -08001066 false, QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001067 }));
1068 // Buffered data size < threshold, ask upper layer for more data.
1069 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1);
1070 stream_->OnCanWrite();
dschinazi696ee602019-04-25 16:05:35 -07001071 EXPECT_EQ(static_cast<uint64_t>(
1072 GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001073 stream_->BufferedDataBytes());
1074 EXPECT_TRUE(stream_->CanWriteNewData());
1075
1076 // Flush all buffered data.
renjietang41a1b412020-02-27 15:05:14 -08001077 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001078 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001079 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1);
1080 stream_->OnCanWrite();
1081 EXPECT_EQ(0u, stream_->BufferedDataBytes());
1082 EXPECT_FALSE(stream_->HasBufferedData());
1083 EXPECT_TRUE(stream_->CanWriteNewData());
1084
1085 // Testing Writev.
renjietang41a1b412020-02-27 15:05:14 -08001086 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001087 .WillOnce(Return(QuicConsumedData(0, false)));
1088 struct iovec iov = {const_cast<char*>(data.data()), data.length()};
1089 QuicMemSliceStorage storage(
1090 &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(),
1091 1024);
1092 QuicConsumedData consumed = stream_->WriteMemSlices(storage.ToSpan(), false);
1093
1094 // There is no buffered data before, all data should be consumed without
1095 // respecting buffered data upper limit.
1096 EXPECT_EQ(data.length(), consumed.bytes_consumed);
1097 EXPECT_FALSE(consumed.fin_consumed);
1098 EXPECT_EQ(data.length(), stream_->BufferedDataBytes());
1099 EXPECT_FALSE(stream_->CanWriteNewData());
1100
renjietang41a1b412020-02-27 15:05:14 -08001101 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001102 QuicMemSliceStorage storage2(
1103 &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(),
1104 1024);
1105 consumed = stream_->WriteMemSlices(storage2.ToSpan(), false);
1106 // No Data can be consumed as buffered data is beyond upper limit.
1107 EXPECT_EQ(0u, consumed.bytes_consumed);
1108 EXPECT_FALSE(consumed.fin_consumed);
1109 EXPECT_EQ(data.length(), stream_->BufferedDataBytes());
1110
1111 data_to_write =
1112 data.length() - GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1;
renjietang41a1b412020-02-27 15:05:14 -08001113 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001114 .WillOnce(InvokeWithoutArgs([this, data_to_write]() {
renjietang7c239172020-02-21 13:50:39 -08001115 return session_->ConsumeData(stream_->id(), data_to_write, 0u, NO_FIN,
renjietang41a1b412020-02-27 15:05:14 -08001116 false, QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001117 }));
1118
1119 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1);
1120 stream_->OnCanWrite();
dschinazi696ee602019-04-25 16:05:35 -07001121 EXPECT_EQ(static_cast<uint64_t>(
1122 GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001123 stream_->BufferedDataBytes());
1124 EXPECT_TRUE(stream_->CanWriteNewData());
1125
renjietang41a1b412020-02-27 15:05:14 -08001126 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001127 // All data can be consumed as buffered data is below upper limit.
1128 QuicMemSliceStorage storage3(
1129 &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(),
1130 1024);
1131 consumed = stream_->WriteMemSlices(storage3.ToSpan(), false);
1132 EXPECT_EQ(data.length(), consumed.bytes_consumed);
1133 EXPECT_FALSE(consumed.fin_consumed);
1134 EXPECT_EQ(data.length() + GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1,
1135 stream_->BufferedDataBytes());
1136 EXPECT_FALSE(stream_->CanWriteNewData());
1137}
1138
1139TEST_P(QuicStreamTest, WritevDataReachStreamLimit) {
1140 Initialize();
vasilvvc48c8712019-03-11 13:38:16 -07001141 std::string data("aaaaa");
QUICHE teama6ef0a62019-03-07 20:34:33 -05001142 QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - data.length(),
1143 stream_);
renjietang41a1b412020-02-27 15:05:14 -08001144 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001145 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001146 struct iovec iov = {const_cast<char*>(data.data()), 5u};
1147 QuicMemSliceStorage storage(
1148 &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(),
1149 1024);
1150 QuicConsumedData consumed = stream_->WriteMemSlices(storage.ToSpan(), false);
1151 EXPECT_EQ(data.length(), consumed.bytes_consumed);
1152 struct iovec iov2 = {const_cast<char*>(data.data()), 1u};
1153 QuicMemSliceStorage storage2(
1154 &iov2, 1,
1155 session_->connection()->helper()->GetStreamSendBufferAllocator(), 1024);
1156 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
1157 EXPECT_QUIC_BUG(stream_->WriteMemSlices(storage2.ToSpan(), false),
1158 "Write too many data via stream");
1159}
1160
1161TEST_P(QuicStreamTest, WriteMemSlices) {
1162 // Set buffered data low water mark to be 100.
wub49855982019-05-01 14:16:26 -07001163 SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001164
1165 Initialize();
1166 char data[1024];
1167 std::vector<std::pair<char*, size_t>> buffers;
bnc4e9283d2019-12-17 07:08:57 -08001168 buffers.push_back(std::make_pair(data, QUICHE_ARRAYSIZE(data)));
1169 buffers.push_back(std::make_pair(data, QUICHE_ARRAYSIZE(data)));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001170 QuicTestMemSliceVector vector1(buffers);
1171 QuicTestMemSliceVector vector2(buffers);
1172 QuicMemSliceSpan span1 = vector1.span();
1173 QuicMemSliceSpan span2 = vector2.span();
1174
renjietang41a1b412020-02-27 15:05:14 -08001175 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001176 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -08001177 return session_->ConsumeData(stream_->id(), 100u, 0u, NO_FIN, false,
1178 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001179 }));
1180 // There is no buffered data before, all data should be consumed.
1181 QuicConsumedData consumed = stream_->WriteMemSlices(span1, false);
1182 EXPECT_EQ(2048u, consumed.bytes_consumed);
1183 EXPECT_FALSE(consumed.fin_consumed);
bnc4e9283d2019-12-17 07:08:57 -08001184 EXPECT_EQ(2 * QUICHE_ARRAYSIZE(data) - 100, stream_->BufferedDataBytes());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001185 EXPECT_FALSE(stream_->fin_buffered());
1186
renjietang41a1b412020-02-27 15:05:14 -08001187 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001188 // No Data can be consumed as buffered data is beyond upper limit.
1189 consumed = stream_->WriteMemSlices(span2, true);
1190 EXPECT_EQ(0u, consumed.bytes_consumed);
1191 EXPECT_FALSE(consumed.fin_consumed);
bnc4e9283d2019-12-17 07:08:57 -08001192 EXPECT_EQ(2 * QUICHE_ARRAYSIZE(data) - 100, stream_->BufferedDataBytes());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001193 EXPECT_FALSE(stream_->fin_buffered());
1194
bnc4e9283d2019-12-17 07:08:57 -08001195 size_t data_to_write = 2 * QUICHE_ARRAYSIZE(data) - 100 -
QUICHE teama6ef0a62019-03-07 20:34:33 -05001196 GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1;
renjietang41a1b412020-02-27 15:05:14 -08001197 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001198 .WillOnce(InvokeWithoutArgs([this, data_to_write]() {
renjietang7c239172020-02-21 13:50:39 -08001199 return session_->ConsumeData(stream_->id(), data_to_write, 100u, NO_FIN,
renjietang41a1b412020-02-27 15:05:14 -08001200 false, QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001201 }));
1202 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1);
1203 stream_->OnCanWrite();
dschinazi696ee602019-04-25 16:05:35 -07001204 EXPECT_EQ(static_cast<uint64_t>(
1205 GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001206 stream_->BufferedDataBytes());
1207 // Try to write slices2 again.
renjietang41a1b412020-02-27 15:05:14 -08001208 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001209 consumed = stream_->WriteMemSlices(span2, true);
1210 EXPECT_EQ(2048u, consumed.bytes_consumed);
1211 EXPECT_TRUE(consumed.fin_consumed);
bnc4e9283d2019-12-17 07:08:57 -08001212 EXPECT_EQ(2 * QUICHE_ARRAYSIZE(data) +
QUICHE teama6ef0a62019-03-07 20:34:33 -05001213 GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1,
1214 stream_->BufferedDataBytes());
1215 EXPECT_TRUE(stream_->fin_buffered());
1216
1217 // Flush all buffered data.
renjietang41a1b412020-02-27 15:05:14 -08001218 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001219 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001220 stream_->OnCanWrite();
1221 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(0);
1222 EXPECT_FALSE(stream_->HasBufferedData());
1223 EXPECT_TRUE(stream_->write_side_closed());
1224}
1225
1226TEST_P(QuicStreamTest, WriteMemSlicesReachStreamLimit) {
1227 Initialize();
1228 QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - 5u, stream_);
1229 char data[5];
1230 std::vector<std::pair<char*, size_t>> buffers;
bnc4e9283d2019-12-17 07:08:57 -08001231 buffers.push_back(std::make_pair(data, QUICHE_ARRAYSIZE(data)));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001232 QuicTestMemSliceVector vector1(buffers);
1233 QuicMemSliceSpan span1 = vector1.span();
renjietang41a1b412020-02-27 15:05:14 -08001234 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001235 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -08001236 return session_->ConsumeData(stream_->id(), 5u, 0u, NO_FIN, false,
1237 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001238 }));
1239 // There is no buffered data before, all data should be consumed.
1240 QuicConsumedData consumed = stream_->WriteMemSlices(span1, false);
1241 EXPECT_EQ(5u, consumed.bytes_consumed);
1242
1243 std::vector<std::pair<char*, size_t>> buffers2;
1244 buffers2.push_back(std::make_pair(data, 1u));
1245 QuicTestMemSliceVector vector2(buffers);
1246 QuicMemSliceSpan span2 = vector2.span();
1247 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
1248 EXPECT_QUIC_BUG(stream_->WriteMemSlices(span2, false),
1249 "Write too many data via stream");
1250}
1251
1252TEST_P(QuicStreamTest, StreamDataGetAckedMultipleTimes) {
1253 Initialize();
renjietang41a1b412020-02-27 15:05:14 -08001254 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001255 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
zhongyi1b2f7832019-06-14 13:31:34 -07001256 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -07001257 EXPECT_FALSE(session_->HasUnackedStreamData());
zhongyi1b2f7832019-06-14 13:31:34 -07001258
QUICHE teama6ef0a62019-03-07 20:34:33 -05001259 // Send [0, 27) and fin.
1260 stream_->WriteOrBufferData(kData1, false, nullptr);
1261 stream_->WriteOrBufferData(kData1, false, nullptr);
1262 stream_->WriteOrBufferData(kData1, true, nullptr);
1263 EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size());
1264 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -07001265 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001266 // Ack [0, 9), [5, 22) and [18, 26)
1267 // Verify [0, 9) 9 bytes are acked.
1268 QuicByteCount newly_acked_length = 0;
1269 EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001270 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001271 &newly_acked_length));
1272 EXPECT_EQ(9u, newly_acked_length);
1273 EXPECT_EQ(2u, QuicStreamPeer::SendBuffer(stream_).size());
1274 // Verify [9, 22) 13 bytes are acked.
1275 EXPECT_TRUE(stream_->OnStreamFrameAcked(5, 17, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001276 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001277 &newly_acked_length));
1278 EXPECT_EQ(13u, newly_acked_length);
1279 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1280 // Verify [22, 26) 4 bytes are acked.
1281 EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 8, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001282 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001283 &newly_acked_length));
1284 EXPECT_EQ(4u, newly_acked_length);
1285 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1286 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -07001287 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001288
1289 // Ack [0, 27). Verify [26, 27) 1 byte is acked.
1290 EXPECT_TRUE(stream_->OnStreamFrameAcked(26, 1, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001291 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001292 &newly_acked_length));
1293 EXPECT_EQ(1u, newly_acked_length);
1294 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1295 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -07001296 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001297
1298 // Ack Fin.
1299 EXPECT_TRUE(stream_->OnStreamFrameAcked(27, 0, true, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001300 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001301 &newly_acked_length));
1302 EXPECT_EQ(0u, newly_acked_length);
1303 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1304 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -07001305 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001306
1307 // Ack [10, 27) and fin. No new data is acked.
QUICHE team2f5f30b2020-02-18 08:52:28 -08001308 EXPECT_FALSE(
1309 stream_->OnStreamFrameAcked(10, 17, true, QuicTime::Delta::Zero(),
1310 QuicTime::Zero(), &newly_acked_length));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001311 EXPECT_EQ(0u, newly_acked_length);
1312 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1313 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -07001314 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001315}
1316
1317TEST_P(QuicStreamTest, OnStreamFrameLost) {
1318 Initialize();
1319
1320 // Send [0, 9).
renjietang41a1b412020-02-27 15:05:14 -08001321 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001322 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001323 stream_->WriteOrBufferData(kData1, false, nullptr);
1324 EXPECT_FALSE(stream_->HasBufferedData());
1325 EXPECT_TRUE(stream_->IsStreamFrameOutstanding(0, 9, false));
1326
1327 // Try to send [9, 27), but connection is blocked.
renjietang41a1b412020-02-27 15:05:14 -08001328 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001329 .WillOnce(Return(QuicConsumedData(0, false)));
1330 stream_->WriteOrBufferData(kData2, false, nullptr);
1331 stream_->WriteOrBufferData(kData2, false, nullptr);
1332 EXPECT_TRUE(stream_->HasBufferedData());
1333 EXPECT_FALSE(stream_->HasPendingRetransmission());
1334
1335 // Lost [0, 9). When stream gets a chance to write, only lost data is
1336 // transmitted.
1337 stream_->OnStreamFrameLost(0, 9, false);
1338 EXPECT_TRUE(stream_->HasPendingRetransmission());
renjietang41a1b412020-02-27 15:05:14 -08001339 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001340 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
renjietangb38e6352019-10-24 09:38:20 -07001341 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001342 stream_->OnCanWrite();
1343 EXPECT_FALSE(stream_->HasPendingRetransmission());
1344 EXPECT_TRUE(stream_->HasBufferedData());
1345
1346 // This OnCanWrite causes [9, 27) to be sent.
renjietang41a1b412020-02-27 15:05:14 -08001347 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001348 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001349 stream_->OnCanWrite();
1350 EXPECT_FALSE(stream_->HasBufferedData());
1351
1352 // Send a fin only frame.
renjietang41a1b412020-02-27 15:05:14 -08001353 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001354 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001355 stream_->WriteOrBufferData("", true, nullptr);
1356
1357 // Lost [9, 27) and fin.
1358 stream_->OnStreamFrameLost(9, 18, false);
1359 stream_->OnStreamFrameLost(27, 0, true);
1360 EXPECT_TRUE(stream_->HasPendingRetransmission());
1361
1362 // Ack [9, 18).
1363 QuicByteCount newly_acked_length = 0;
1364 EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001365 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001366 &newly_acked_length));
1367 EXPECT_EQ(9u, newly_acked_length);
1368 EXPECT_FALSE(stream_->IsStreamFrameOutstanding(9, 3, false));
1369 EXPECT_TRUE(stream_->HasPendingRetransmission());
1370 // This OnCanWrite causes [18, 27) and fin to be retransmitted. Verify fin can
1371 // be bundled with data.
renjietang41a1b412020-02-27 15:05:14 -08001372 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001373 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -08001374 return session_->ConsumeData(stream_->id(), 9u, 18u, FIN, false,
1375 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001376 }));
1377 stream_->OnCanWrite();
1378 EXPECT_FALSE(stream_->HasPendingRetransmission());
1379 // Lost [9, 18) again, but it is not considered as lost because kData2
1380 // has been acked.
1381 stream_->OnStreamFrameLost(9, 9, false);
1382 EXPECT_FALSE(stream_->HasPendingRetransmission());
1383 EXPECT_TRUE(stream_->IsStreamFrameOutstanding(27, 0, true));
1384}
1385
1386TEST_P(QuicStreamTest, CannotBundleLostFin) {
1387 Initialize();
1388
1389 // Send [0, 18) and fin.
renjietang41a1b412020-02-27 15:05:14 -08001390 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001391 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001392 stream_->WriteOrBufferData(kData1, false, nullptr);
1393 stream_->WriteOrBufferData(kData2, true, nullptr);
1394
1395 // Lost [0, 9) and fin.
1396 stream_->OnStreamFrameLost(0, 9, false);
1397 stream_->OnStreamFrameLost(18, 0, true);
1398
1399 // Retransmit lost data. Verify [0, 9) and fin are retransmitted in two
1400 // frames.
1401 InSequence s;
renjietang41a1b412020-02-27 15:05:14 -08001402 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001403 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -08001404 return session_->ConsumeData(stream_->id(), 9u, 0u, NO_FIN, false,
1405 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001406 }));
renjietang41a1b412020-02-27 15:05:14 -08001407 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001408 .WillOnce(Return(QuicConsumedData(0, true)));
1409 stream_->OnCanWrite();
1410}
1411
1412TEST_P(QuicStreamTest, MarkConnectionLevelWriteBlockedOnWindowUpdateFrame) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001413 Initialize();
1414
renjietang39979d32019-09-13 10:02:13 -07001415 // Set the config to a small value so that a newly created stream has small
1416 // send flow control window.
1417 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(session_->config(),
1418 100);
renjietang9bf25642019-10-10 10:21:15 -07001419 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional(
1420 session_->config(), 100);
renjietang39979d32019-09-13 10:02:13 -07001421 auto stream = new TestStream(GetNthClientInitiatedBidirectionalStreamId(
1422 GetParam().transport_version, 2),
1423 session_.get(), BIDIRECTIONAL);
1424 session_->ActivateStream(QuicWrapUnique(stream));
1425
renjietang41a1b412020-02-27 15:05:14 -08001426 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001427 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001428 EXPECT_CALL(*connection_, SendControlFrame(_))
bnc5b3c3be2019-06-25 10:37:09 -07001429 .WillOnce(Invoke(&ClearControlFrame));
vasilvvc48c8712019-03-11 13:38:16 -07001430 std::string data(1024, '.');
renjietang39979d32019-09-13 10:02:13 -07001431 stream->WriteOrBufferData(data, false, nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001432 EXPECT_FALSE(HasWriteBlockedStreams());
1433
1434 QuicWindowUpdateFrame window_update(kInvalidControlFrameId, stream_->id(),
1435 1234);
1436
renjietang39979d32019-09-13 10:02:13 -07001437 stream->OnWindowUpdateFrame(window_update);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001438 // Verify stream is marked connection level write blocked.
1439 EXPECT_TRUE(HasWriteBlockedStreams());
renjietang39979d32019-09-13 10:02:13 -07001440 EXPECT_TRUE(stream->HasBufferedData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001441}
1442
1443// Regression test for b/73282665.
1444TEST_P(QuicStreamTest,
1445 MarkConnectionLevelWriteBlockedOnWindowUpdateFrameWithNoBufferedData) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001446 Initialize();
1447
renjietang39979d32019-09-13 10:02:13 -07001448 // Set the config to a small value so that a newly created stream has small
1449 // send flow control window.
1450 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(session_->config(),
1451 100);
renjietang9bf25642019-10-10 10:21:15 -07001452 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional(
1453 session_->config(), 100);
renjietang39979d32019-09-13 10:02:13 -07001454 auto stream = new TestStream(GetNthClientInitiatedBidirectionalStreamId(
1455 GetParam().transport_version, 2),
1456 session_.get(), BIDIRECTIONAL);
1457 session_->ActivateStream(QuicWrapUnique(stream));
1458
1459 std::string data(100, '.');
renjietang41a1b412020-02-27 15:05:14 -08001460 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001461 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001462 EXPECT_CALL(*connection_, SendControlFrame(_))
bnc5b3c3be2019-06-25 10:37:09 -07001463 .WillOnce(Invoke(&ClearControlFrame));
renjietang39979d32019-09-13 10:02:13 -07001464 stream->WriteOrBufferData(data, false, nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001465 EXPECT_FALSE(HasWriteBlockedStreams());
1466
1467 QuicWindowUpdateFrame window_update(kInvalidControlFrameId, stream_->id(),
1468 120);
renjietang39979d32019-09-13 10:02:13 -07001469 stream->OnWindowUpdateFrame(window_update);
1470 EXPECT_FALSE(stream->HasBufferedData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001471 // Verify stream is marked as blocked although there is no buffered data.
1472 EXPECT_TRUE(HasWriteBlockedStreams());
1473}
1474
1475TEST_P(QuicStreamTest, RetransmitStreamData) {
1476 Initialize();
1477 InSequence s;
1478
1479 // Send [0, 18) with fin.
renjietang41a1b412020-02-27 15:05:14 -08001480 EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001481 .Times(2)
renjietang7c239172020-02-21 13:50:39 -08001482 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001483 stream_->WriteOrBufferData(kData1, false, nullptr);
1484 stream_->WriteOrBufferData(kData1, true, nullptr);
1485 // Ack [10, 13).
1486 QuicByteCount newly_acked_length = 0;
1487 stream_->OnStreamFrameAcked(10, 3, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001488 QuicTime::Zero(), &newly_acked_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001489 EXPECT_EQ(3u, newly_acked_length);
1490 // Retransmit [0, 18) with fin, and only [0, 8) is consumed.
renjietang41a1b412020-02-27 15:05:14 -08001491 EXPECT_CALL(*session_, WritevData(stream_->id(), 10, 0, NO_FIN, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001492 .WillOnce(InvokeWithoutArgs([this]() {
renjietang41a1b412020-02-27 15:05:14 -08001493 return session_->ConsumeData(stream_->id(), 8, 0u, NO_FIN, false,
1494 QuicheNullOpt);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001495 }));
1496 EXPECT_FALSE(stream_->RetransmitStreamData(0, 18, true));
1497
1498 // Retransmit [0, 18) with fin, and all is consumed.
renjietang41a1b412020-02-27 15:05:14 -08001499 EXPECT_CALL(*session_, WritevData(stream_->id(), 10, 0, NO_FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001500 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
renjietang41a1b412020-02-27 15:05:14 -08001501 EXPECT_CALL(*session_, WritevData(stream_->id(), 5, 13, FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001502 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001503 EXPECT_TRUE(stream_->RetransmitStreamData(0, 18, true));
1504
1505 // Retransmit [0, 8) with fin, and all is consumed.
renjietang41a1b412020-02-27 15:05:14 -08001506 EXPECT_CALL(*session_, WritevData(stream_->id(), 8, 0, NO_FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001507 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
renjietang41a1b412020-02-27 15:05:14 -08001508 EXPECT_CALL(*session_, WritevData(stream_->id(), 0, 18, FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001509 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001510 EXPECT_TRUE(stream_->RetransmitStreamData(0, 8, true));
1511}
1512
1513TEST_P(QuicStreamTest, ResetStreamOnTtlExpiresRetransmitLostData) {
1514 Initialize();
1515
renjietang41a1b412020-02-27 15:05:14 -08001516 EXPECT_CALL(*session_, WritevData(stream_->id(), 200, 0, FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001517 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
vasilvvc48c8712019-03-11 13:38:16 -07001518 std::string body(200, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001519 stream_->WriteOrBufferData(body, true, nullptr);
1520
1521 // Set TTL to be 1 s.
1522 QuicTime::Delta ttl = QuicTime::Delta::FromSeconds(1);
1523 ASSERT_TRUE(stream_->MaybeSetTtl(ttl));
1524 // Verify data gets retransmitted because TTL does not expire.
renjietang41a1b412020-02-27 15:05:14 -08001525 EXPECT_CALL(*session_, WritevData(stream_->id(), 100, 0, NO_FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001526 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001527 EXPECT_TRUE(stream_->RetransmitStreamData(0, 100, false));
1528 stream_->OnStreamFrameLost(100, 100, true);
1529 EXPECT_TRUE(stream_->HasPendingRetransmission());
1530
1531 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
1532 // Verify stream gets reset because TTL expires.
1533 EXPECT_CALL(*session_, SendRstStream(_, QUIC_STREAM_TTL_EXPIRED, _)).Times(1);
1534 stream_->OnCanWrite();
1535}
1536
1537TEST_P(QuicStreamTest, ResetStreamOnTtlExpiresEarlyRetransmitData) {
1538 Initialize();
1539
renjietang41a1b412020-02-27 15:05:14 -08001540 EXPECT_CALL(*session_, WritevData(stream_->id(), 200, 0, FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001541 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
vasilvvc48c8712019-03-11 13:38:16 -07001542 std::string body(200, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001543 stream_->WriteOrBufferData(body, true, nullptr);
1544
1545 // Set TTL to be 1 s.
1546 QuicTime::Delta ttl = QuicTime::Delta::FromSeconds(1);
1547 ASSERT_TRUE(stream_->MaybeSetTtl(ttl));
1548
1549 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
1550 // Verify stream gets reset because TTL expires.
1551 EXPECT_CALL(*session_, SendRstStream(_, QUIC_STREAM_TTL_EXPIRED, _)).Times(1);
1552 stream_->RetransmitStreamData(0, 100, false);
1553}
1554
1555// Test that QuicStream::StopSending A) is a no-op if the connection is not in
1556// version 99, B) that it properly invokes QuicSession::StopSending, and C) that
1557// the correct data is passed along, including getting the stream ID.
renjietang9bf25642019-10-10 10:21:15 -07001558TEST_P(QuicStreamTest, CheckStopSending) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001559 Initialize();
1560 const int kStopSendingCode = 123;
1561 // These must start as false.
bncc7d9e0c2019-04-16 10:22:15 -07001562 EXPECT_FALSE(stream_->write_side_closed());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001563 EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_));
1564 // Expect to actually see a stop sending if and only if we are in version 99.
fkastenholz305e1732019-06-18 05:01:22 -07001565 if (VersionHasIetfQuicFrames(connection_->transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001566 EXPECT_CALL(*session_, SendStopSending(kStopSendingCode, stream_->id()))
1567 .Times(1);
1568 } else {
1569 EXPECT_CALL(*session_, SendStopSending(_, _)).Times(0);
1570 }
1571 stream_->SendStopSending(kStopSendingCode);
1572 // Sending a STOP_SENDING does not actually close the local stream.
1573 // Our implementation waits for the responding RESET_STREAM to effect the
1574 // closes. Therefore, read- and write-side closes should both be false.
bncc7d9e0c2019-04-16 10:22:15 -07001575 EXPECT_FALSE(stream_->write_side_closed());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001576 EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_));
1577}
1578
1579// Test that OnStreamReset does one-way (read) closes if version 99, two way
1580// (read and write) if not version 99.
1581TEST_P(QuicStreamTest, OnStreamResetReadOrReadWrite) {
1582 Initialize();
bncc7d9e0c2019-04-16 10:22:15 -07001583 EXPECT_FALSE(stream_->write_side_closed());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001584 EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_));
1585
1586 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
1587 QUIC_STREAM_CANCELLED, 1234);
1588 stream_->OnStreamReset(rst_frame);
fkastenholz305e1732019-06-18 05:01:22 -07001589 if (VersionHasIetfQuicFrames(connection_->transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001590 // Version 99/IETF QUIC should close just the read side.
1591 EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_));
bncc7d9e0c2019-04-16 10:22:15 -07001592 EXPECT_FALSE(stream_->write_side_closed());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001593 } else {
1594 // Google QUIC should close both sides of the stream.
bncc7d9e0c2019-04-16 10:22:15 -07001595 EXPECT_TRUE(stream_->write_side_closed());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001596 EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_));
1597 }
1598}
1599
renjietang28c04b72019-07-01 15:08:09 -07001600TEST_P(QuicStreamTest, WindowUpdateForReadOnlyStream) {
renjietang28c04b72019-07-01 15:08:09 -07001601 Initialize();
1602
1603 QuicStreamId stream_id = QuicUtils::GetFirstUnidirectionalStreamId(
1604 connection_->transport_version(), Perspective::IS_CLIENT);
1605 TestStream stream(stream_id, session_.get(), READ_UNIDIRECTIONAL);
1606 QuicWindowUpdateFrame window_update_frame(kInvalidControlFrameId, stream_id,
1607 0);
1608 EXPECT_CALL(
1609 *connection_,
1610 CloseConnection(
1611 QUIC_WINDOW_UPDATE_RECEIVED_ON_READ_UNIDIRECTIONAL_STREAM,
1612 "WindowUpdateFrame received on READ_UNIDIRECTIONAL stream.", _));
1613 stream.OnWindowUpdateFrame(window_update_frame);
1614}
1615
renjietang15afba32019-10-23 14:32:35 -07001616TEST_P(QuicStreamTest, RstStreamFrameChangesCloseOffset) {
renjietang15afba32019-10-23 14:32:35 -07001617 Initialize();
1618
1619 QuicStreamFrame stream_frame(stream_->id(), true, 0, "abc");
renjietangb38e6352019-10-24 09:38:20 -07001620 EXPECT_CALL(*stream_, OnDataAvailable());
renjietang15afba32019-10-23 14:32:35 -07001621 stream_->OnStreamFrame(stream_frame);
1622 QuicRstStreamFrame rst(kInvalidControlFrameId, stream_->id(),
1623 QUIC_STREAM_CANCELLED, 0u);
1624
1625 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_MULTIPLE_OFFSET, _, _));
1626 stream_->OnStreamReset(rst);
1627}
1628
QUICHE teama6ef0a62019-03-07 20:34:33 -05001629} // namespace
1630} // namespace test
1631} // namespace quic