blob: f2b5563344b5ecb839c87c0b2cc718a3d2e49090 [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";
dschinazif1e7b422020-04-30 12:21:28 -070052const QuicByteCount kDataLen = 9;
QUICHE teama6ef0a62019-03-07 20:34:33 -050053
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
renjietang49aec632020-08-31 10:56:28 -070061 TestStream(PendingStream* pending,
62 QuicSession* session,
63 StreamType type,
64 bool is_static)
65 : QuicStream(pending, session, type, is_static) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050066
wub713afae2020-04-27 07:48:31 -070067 MOCK_METHOD(void, OnDataAvailable, (), (override));
QUICHE teama6ef0a62019-03-07 20:34:33 -050068
wub713afae2020-04-27 07:48:31 -070069 MOCK_METHOD(void, OnCanWriteNewData, (), (override));
QUICHE teama6ef0a62019-03-07 20:34:33 -050070
71 using QuicStream::CanWriteNewData;
72 using QuicStream::CanWriteNewDataAfterData;
73 using QuicStream::CloseWriteSide;
74 using QuicStream::fin_buffered;
75 using QuicStream::OnClose;
76 using QuicStream::WriteMemSlices;
77 using QuicStream::WriteOrBufferData;
QUICHE teama6ef0a62019-03-07 20:34:33 -050078
79 private:
vasilvvc48c8712019-03-11 13:38:16 -070080 std::string data_;
QUICHE teama6ef0a62019-03-07 20:34:33 -050081};
82
renjietang9bf25642019-10-10 10:21:15 -070083class QuicStreamTest : public QuicTestWithParam<ParsedQuicVersion> {
QUICHE teama6ef0a62019-03-07 20:34:33 -050084 public:
renjietang9bf25642019-10-10 10:21:15 -070085 QuicStreamTest()
renjietang39979d32019-09-13 10:02:13 -070086 : zero_(QuicTime::Delta::Zero()),
QUICHE teama6ef0a62019-03-07 20:34:33 -050087 supported_versions_(AllSupportedVersions()) {}
88
89 void Initialize() {
90 ParsedQuicVersionVector version_vector;
91 version_vector.push_back(GetParam());
92 connection_ = new StrictMock<MockQuicConnection>(
93 &helper_, &alarm_factory_, Perspective::IS_SERVER, version_vector);
94 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
vasilvv0fc587f2019-09-06 13:33:08 -070095 session_ = std::make_unique<StrictMock<MockQuicSession>>(connection_);
renjietang39979d32019-09-13 10:02:13 -070096 session_->Initialize();
QUICHE teama6ef0a62019-03-07 20:34:33 -050097
renjietang39979d32019-09-13 10:02:13 -070098 QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow(
99 session_->config(), kMinimumFlowControlSendWindow);
dschinazi18cdf132019-10-09 16:08:18 -0700100 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesUnidirectional(
101 session_->config(), kMinimumFlowControlSendWindow);
102 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional(
103 session_->config(), kMinimumFlowControlSendWindow);
104 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesOutgoingBidirectional(
105 session_->config(), kMinimumFlowControlSendWindow);
renjietange6d94672020-01-07 10:30:10 -0800106 QuicConfigPeer::SetReceivedMaxUnidirectionalStreams(session_->config(), 10);
renjietang39979d32019-09-13 10:02:13 -0700107 session_->OnConfigNegotiated();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500108
renjietangb38e6352019-10-24 09:38:20 -0700109 stream_ = new StrictMock<TestStream>(kTestStreamId, session_.get(),
110 BIDIRECTIONAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500111 EXPECT_NE(nullptr, stream_);
112 // session_ now owns stream_.
113 session_->ActivateStream(QuicWrapUnique(stream_));
114 // Ignore resetting when session_ is terminated.
renjietang9946bc02020-07-16 15:14:27 -0700115 EXPECT_CALL(*session_, SendRstStream(kTestStreamId, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500116 .Times(AnyNumber());
117 write_blocked_list_ =
118 QuicSessionPeer::GetWriteBlockedStreams(session_.get());
119 }
120
bncc7d9e0c2019-04-16 10:22:15 -0700121 bool fin_sent() { return stream_->fin_sent(); }
122 bool rst_sent() { return stream_->rst_sent(); }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500123
QUICHE teama6ef0a62019-03-07 20:34:33 -0500124 bool HasWriteBlockedStreams() {
125 return write_blocked_list_->HasWriteBlockedSpecialStream() ||
126 write_blocked_list_->HasWriteBlockedDataStreams();
127 }
128
renjietang41a1b412020-02-27 15:05:14 -0800129 QuicConsumedData CloseStreamOnWriteError(
130 QuicStreamId id,
dschinazif1e7b422020-04-30 12:21:28 -0700131 QuicByteCount /*write_length*/,
renjietang41a1b412020-02-27 15:05:14 -0800132 QuicStreamOffset /*offset*/,
133 StreamSendingState /*state*/,
renjietang4d992bf2020-03-03 13:01:55 -0800134 TransmissionType /*type*/,
renjietang41a1b412020-02-27 15:05:14 -0800135 quiche::QuicheOptional<EncryptionLevel> /*level*/) {
renjietangff4240d2020-07-01 06:19:55 -0700136 session_->ResetStream(id, QUIC_STREAM_CANCELLED);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500137 return QuicConsumedData(1, false);
138 }
139
QUICHE teama6ef0a62019-03-07 20:34:33 -0500140 bool ClearResetStreamFrame(const QuicFrame& frame) {
141 EXPECT_EQ(RST_STREAM_FRAME, frame.type);
142 DeleteFrame(&const_cast<QuicFrame&>(frame));
143 return true;
144 }
145
146 bool ClearStopSendingFrame(const QuicFrame& frame) {
147 EXPECT_EQ(STOP_SENDING_FRAME, frame.type);
148 DeleteFrame(&const_cast<QuicFrame&>(frame));
149 return true;
150 }
151
152 protected:
153 MockQuicConnectionHelper helper_;
154 MockAlarmFactory alarm_factory_;
155 MockQuicConnection* connection_;
156 std::unique_ptr<MockQuicSession> session_;
renjietangb38e6352019-10-24 09:38:20 -0700157 StrictMock<TestStream>* stream_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500158 QuicWriteBlockedList* write_blocked_list_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500159 QuicTime::Delta zero_;
160 ParsedQuicVersionVector supported_versions_;
renjietang940a5322019-08-02 16:17:51 -0700161 QuicStreamId kTestStreamId =
162 GetNthClientInitiatedBidirectionalStreamId(GetParam().transport_version,
163 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500164};
165
renjietang9bf25642019-10-10 10:21:15 -0700166INSTANTIATE_TEST_SUITE_P(QuicStreamTests,
167 QuicStreamTest,
dschinazi142051a2019-09-18 18:17:29 -0700168 ::testing::ValuesIn(AllSupportedVersions()),
169 ::testing::PrintToStringParamName());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500170
renjietang35448992019-05-08 17:08:57 -0700171TEST_P(QuicStreamTest, PendingStreamStaticness) {
172 Initialize();
173
174 PendingStream pending(kTestStreamId + 2, session_.get());
renjietang49aec632020-08-31 10:56:28 -0700175 TestStream stream(&pending, session_.get(), StreamType::BIDIRECTIONAL, false);
renjietang35448992019-05-08 17:08:57 -0700176 EXPECT_FALSE(stream.is_static());
177
178 PendingStream pending2(kTestStreamId + 3, session_.get());
renjietang49aec632020-08-31 10:56:28 -0700179 TestStream stream2(&pending2, session_.get(), StreamType::BIDIRECTIONAL,
180 true);
renjietang35448992019-05-08 17:08:57 -0700181 EXPECT_TRUE(stream2.is_static());
182}
183
QUICHE teama6ef0a62019-03-07 20:34:33 -0500184TEST_P(QuicStreamTest, PendingStreamTooMuchData) {
185 Initialize();
186
187 PendingStream pending(kTestStreamId + 2, session_.get());
188 // Receive a stream frame that violates flow control: the byte offset is
189 // higher than the receive window offset.
190 QuicStreamFrame frame(kTestStreamId + 2, false,
renjietangb38e6352019-10-24 09:38:20 -0700191 kInitialSessionFlowControlWindowForTest + 1, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500192
193 // Stream should not accept the frame, and the connection should be closed.
194 EXPECT_CALL(*connection_,
195 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _));
196 pending.OnStreamFrame(frame);
197}
198
199TEST_P(QuicStreamTest, PendingStreamTooMuchDataInRstStream) {
200 Initialize();
201
202 PendingStream pending(kTestStreamId + 2, session_.get());
203 // Receive a rst stream frame that violates flow control: the byte offset is
204 // higher than the receive window offset.
205 QuicRstStreamFrame frame(kInvalidControlFrameId, kTestStreamId + 2,
206 QUIC_STREAM_CANCELLED,
207 kInitialSessionFlowControlWindowForTest + 1);
208
209 // Pending stream should not accept the frame, and the connection should be
210 // closed.
211 EXPECT_CALL(*connection_,
212 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _));
213 pending.OnRstStreamFrame(frame);
214}
215
216TEST_P(QuicStreamTest, PendingStreamRstStream) {
217 Initialize();
218
219 PendingStream pending(kTestStreamId + 2, session_.get());
220 QuicStreamOffset final_byte_offset = 7;
221 QuicRstStreamFrame frame(kInvalidControlFrameId, kTestStreamId + 2,
222 QUIC_STREAM_CANCELLED, final_byte_offset);
223
224 // Pending stream should accept the frame and not close the connection.
225 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
226 pending.OnRstStreamFrame(frame);
227}
228
229TEST_P(QuicStreamTest, FromPendingStream) {
230 Initialize();
231
232 PendingStream pending(kTestStreamId + 2, session_.get());
233
renjietangb38e6352019-10-24 09:38:20 -0700234 QuicStreamFrame frame(kTestStreamId + 2, false, 2, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500235 pending.OnStreamFrame(frame);
236 pending.OnStreamFrame(frame);
renjietangb38e6352019-10-24 09:38:20 -0700237 QuicStreamFrame frame2(kTestStreamId + 2, true, 3, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500238 pending.OnStreamFrame(frame2);
239
renjietang49aec632020-08-31 10:56:28 -0700240 TestStream stream(&pending, session_.get(), StreamType::READ_UNIDIRECTIONAL,
241 false);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500242 EXPECT_EQ(3, stream.num_frames_received());
243 EXPECT_EQ(3u, stream.stream_bytes_read());
244 EXPECT_EQ(1, stream.num_duplicate_frames_received());
245 EXPECT_EQ(true, stream.fin_received());
renjietang07b2e8d2020-08-10 15:18:24 -0700246 EXPECT_EQ(frame2.offset + 1, stream.highest_received_byte_offset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500247 EXPECT_EQ(frame2.offset + 1,
248 session_->flow_controller()->highest_received_byte_offset());
249}
250
251TEST_P(QuicStreamTest, FromPendingStreamThenData) {
252 Initialize();
253
254 PendingStream pending(kTestStreamId + 2, session_.get());
255
renjietangb38e6352019-10-24 09:38:20 -0700256 QuicStreamFrame frame(kTestStreamId + 2, false, 2, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500257 pending.OnStreamFrame(frame);
258
renjietang49aec632020-08-31 10:56:28 -0700259 auto stream = new TestStream(&pending, session_.get(),
260 StreamType::READ_UNIDIRECTIONAL, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500261 session_->ActivateStream(QuicWrapUnique(stream));
262
renjietangb38e6352019-10-24 09:38:20 -0700263 QuicStreamFrame frame2(kTestStreamId + 2, true, 3, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500264 stream->OnStreamFrame(frame2);
265
266 EXPECT_EQ(2, stream->num_frames_received());
267 EXPECT_EQ(2u, stream->stream_bytes_read());
268 EXPECT_EQ(true, stream->fin_received());
renjietang07b2e8d2020-08-10 15:18:24 -0700269 EXPECT_EQ(frame2.offset + 1, stream->highest_received_byte_offset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500270 EXPECT_EQ(frame2.offset + 1,
271 session_->flow_controller()->highest_received_byte_offset());
272}
273
274TEST_P(QuicStreamTest, WriteAllData) {
275 Initialize();
276
dschinazif1e7b422020-04-30 12:21:28 -0700277 QuicByteCount length =
QUICHE teama6ef0a62019-03-07 20:34:33 -0500278 1 + QuicPacketCreator::StreamFramePacketOverhead(
279 connection_->transport_version(), PACKET_8BYTE_CONNECTION_ID,
280 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
281 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
282 VARIABLE_LENGTH_INTEGER_LENGTH_0,
283 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0u);
284 connection_->SetMaxPacketLength(length);
285
renjietang41a1b412020-02-27 15:05:14 -0800286 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800287 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500288 stream_->WriteOrBufferData(kData1, false, nullptr);
289 EXPECT_FALSE(HasWriteBlockedStreams());
290}
291
292TEST_P(QuicStreamTest, NoBlockingIfNoDataOrFin) {
293 Initialize();
294
295 // Write no data and no fin. If we consume nothing we should not be write
296 // blocked.
dmcardlecf0bfcf2019-12-13 08:08:21 -0800297 EXPECT_QUIC_BUG(
298 stream_->WriteOrBufferData(quiche::QuicheStringPiece(), false, nullptr),
299 "");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500300 EXPECT_FALSE(HasWriteBlockedStreams());
301}
302
303TEST_P(QuicStreamTest, BlockIfOnlySomeDataConsumed) {
304 Initialize();
305
306 // Write some data and no fin. If we consume some but not all of the data,
307 // we should be write blocked a not all the data was consumed.
renjietang41a1b412020-02-27 15:05:14 -0800308 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500309 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -0800310 return session_->ConsumeData(stream_->id(), 1u, 0u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -0700311 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500312 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800313 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), false,
314 nullptr);
fayang8265a2a2019-10-16 11:23:51 -0700315 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500316 ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
317 EXPECT_EQ(1u, stream_->BufferedDataBytes());
318}
319
320TEST_P(QuicStreamTest, BlockIfFinNotConsumedWithData) {
321 Initialize();
322
323 // Write some data and no fin. If we consume all the data but not the fin,
324 // we should be write blocked because the fin was not consumed.
325 // (This should never actually happen as the fin should be sent out with the
326 // last data)
renjietang41a1b412020-02-27 15:05:14 -0800327 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500328 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -0800329 return session_->ConsumeData(stream_->id(), 2u, 0u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -0700330 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500331 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800332 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), true,
333 nullptr);
fayang8265a2a2019-10-16 11:23:51 -0700334 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500335 ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
336}
337
338TEST_P(QuicStreamTest, BlockIfSoloFinNotConsumed) {
339 Initialize();
340
341 // Write no data and a fin. If we consume nothing we should be write blocked,
342 // as the fin was not consumed.
renjietang41a1b412020-02-27 15:05:14 -0800343 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500344 .WillOnce(Return(QuicConsumedData(0, false)));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800345 stream_->WriteOrBufferData(quiche::QuicheStringPiece(), true, nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500346 ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
347}
348
349TEST_P(QuicStreamTest, CloseOnPartialWrite) {
350 Initialize();
351
352 // Write some data and no fin. However, while writing the data
353 // close the stream and verify that MarkConnectionLevelWriteBlocked does not
354 // crash with an unknown stream.
renjietang41a1b412020-02-27 15:05:14 -0800355 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500356 .WillOnce(Invoke(this, &QuicStreamTest::CloseStreamOnWriteError));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800357 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), false,
358 nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500359 ASSERT_EQ(0u, write_blocked_list_->NumBlockedStreams());
360}
361
362TEST_P(QuicStreamTest, WriteOrBufferData) {
363 Initialize();
364
365 EXPECT_FALSE(HasWriteBlockedStreams());
dschinazif1e7b422020-04-30 12:21:28 -0700366 QuicByteCount length =
QUICHE teama6ef0a62019-03-07 20:34:33 -0500367 1 + QuicPacketCreator::StreamFramePacketOverhead(
368 connection_->transport_version(), PACKET_8BYTE_CONNECTION_ID,
369 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
370 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
371 VARIABLE_LENGTH_INTEGER_LENGTH_0,
372 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0u);
373 connection_->SetMaxPacketLength(length);
374
renjietang41a1b412020-02-27 15:05:14 -0800375 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500376 .WillOnce(InvokeWithoutArgs([this]() {
renjietang7c239172020-02-21 13:50:39 -0800377 return session_->ConsumeData(stream_->id(), kDataLen - 1, 0u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -0700378 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500379 }));
380 stream_->WriteOrBufferData(kData1, false, nullptr);
zhongyi1b2f7832019-06-14 13:31:34 -0700381
fayang8265a2a2019-10-16 11:23:51 -0700382 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500383 EXPECT_EQ(1u, stream_->BufferedDataBytes());
384 EXPECT_TRUE(HasWriteBlockedStreams());
385
386 // Queue a bytes_consumed write.
387 stream_->WriteOrBufferData(kData2, false, nullptr);
388 EXPECT_EQ(10u, stream_->BufferedDataBytes());
389 // Make sure we get the tail of the first write followed by the bytes_consumed
390 InSequence s;
renjietang41a1b412020-02-27 15:05:14 -0800391 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500392 .WillOnce(InvokeWithoutArgs([this]() {
renjietang7c239172020-02-21 13:50:39 -0800393 return session_->ConsumeData(stream_->id(), kDataLen - 1, kDataLen - 1,
vasilvv9efbb912020-06-05 08:59:26 -0700394 NO_FIN, NOT_RETRANSMISSION,
395 QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500396 }));
renjietangb38e6352019-10-24 09:38:20 -0700397 EXPECT_CALL(*stream_, OnCanWriteNewData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500398 stream_->OnCanWrite();
fayang8265a2a2019-10-16 11:23:51 -0700399 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500400
401 // And finally the end of the bytes_consumed.
renjietang41a1b412020-02-27 15:05:14 -0800402 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500403 .WillOnce(InvokeWithoutArgs([this]() {
renjietang7c239172020-02-21 13:50:39 -0800404 return session_->ConsumeData(stream_->id(), 2u, 2 * kDataLen - 2,
vasilvv9efbb912020-06-05 08:59:26 -0700405 NO_FIN, NOT_RETRANSMISSION,
406 QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500407 }));
renjietangb38e6352019-10-24 09:38:20 -0700408 EXPECT_CALL(*stream_, OnCanWriteNewData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500409 stream_->OnCanWrite();
fayang8265a2a2019-10-16 11:23:51 -0700410 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500411}
412
413TEST_P(QuicStreamTest, WriteOrBufferDataReachStreamLimit) {
414 Initialize();
vasilvvc48c8712019-03-11 13:38:16 -0700415 std::string data("aaaaa");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500416 QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - data.length(),
417 stream_);
renjietang41a1b412020-02-27 15:05:14 -0800418 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800419 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500420 stream_->WriteOrBufferData(data, false, nullptr);
fayang8265a2a2019-10-16 11:23:51 -0700421 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500422 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
423 EXPECT_QUIC_BUG(stream_->WriteOrBufferData("a", false, nullptr),
424 "Write too many data via stream");
425}
426
427TEST_P(QuicStreamTest, ConnectionCloseAfterStreamClose) {
428 Initialize();
429
430 QuicStreamPeer::CloseReadSide(stream_);
431 stream_->CloseWriteSide();
bncf6f82b12019-10-30 07:01:01 -0700432 EXPECT_THAT(stream_->stream_error(), IsQuicStreamNoError());
433 EXPECT_THAT(stream_->connection_error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500434 stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR,
435 ConnectionCloseSource::FROM_SELF);
bncf6f82b12019-10-30 07:01:01 -0700436 EXPECT_THAT(stream_->stream_error(), IsQuicStreamNoError());
437 EXPECT_THAT(stream_->connection_error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500438}
439
440TEST_P(QuicStreamTest, RstAlwaysSentIfNoFinSent) {
441 // For flow control accounting, a stream must send either a FIN or a RST frame
442 // before termination.
443 // Test that if no FIN has been sent, we send a RST.
444
445 Initialize();
446 EXPECT_FALSE(fin_sent());
447 EXPECT_FALSE(rst_sent());
448
449 // Write some data, with no FIN.
renjietang41a1b412020-02-27 15:05:14 -0800450 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500451 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -0800452 return session_->ConsumeData(stream_->id(), 1u, 0u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -0700453 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500454 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800455 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 1), false,
456 nullptr);
fayang8265a2a2019-10-16 11:23:51 -0700457 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500458 EXPECT_FALSE(fin_sent());
459 EXPECT_FALSE(rst_sent());
460
461 // Now close the stream, and expect that we send a RST.
renjietang9946bc02020-07-16 15:14:27 -0700462 EXPECT_CALL(*session_, SendRstStream(_, _, _, _));
renjietangf00e1e12020-08-26 10:51:15 -0700463 QuicStreamPeer::CloseReadSide(stream_);
fayangb8f83442020-06-01 12:09:17 -0700464 stream_->CloseWriteSide();
zhongyi9fa2be32019-09-10 12:39:01 -0700465 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500466 EXPECT_FALSE(fin_sent());
467 EXPECT_TRUE(rst_sent());
468}
469
470TEST_P(QuicStreamTest, RstNotSentIfFinSent) {
471 // For flow control accounting, a stream must send either a FIN or a RST frame
472 // before termination.
473 // Test that if a FIN has been sent, we don't also send a RST.
474
475 Initialize();
476 EXPECT_FALSE(fin_sent());
477 EXPECT_FALSE(rst_sent());
478
479 // Write some data, with FIN.
renjietang41a1b412020-02-27 15:05:14 -0800480 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500481 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -0800482 return session_->ConsumeData(stream_->id(), 1u, 0u, FIN,
vasilvv9efbb912020-06-05 08:59:26 -0700483 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500484 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800485 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 1), true,
486 nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500487 EXPECT_TRUE(fin_sent());
488 EXPECT_FALSE(rst_sent());
489
490 // Now close the stream, and expect that we do not send a RST.
renjietangf00e1e12020-08-26 10:51:15 -0700491 QuicStreamPeer::CloseReadSide(stream_);
fayangb8f83442020-06-01 12:09:17 -0700492 stream_->CloseWriteSide();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500493 EXPECT_TRUE(fin_sent());
494 EXPECT_FALSE(rst_sent());
495}
496
497TEST_P(QuicStreamTest, OnlySendOneRst) {
498 // For flow control accounting, a stream must send either a FIN or a RST frame
499 // before termination.
500 // Test that if a stream sends a RST, it doesn't send an additional RST during
501 // OnClose() (this shouldn't be harmful, but we shouldn't do it anyway...)
502
503 Initialize();
504 EXPECT_FALSE(fin_sent());
505 EXPECT_FALSE(rst_sent());
506
507 // Reset the stream.
508 const int expected_resets = 1;
renjietang9946bc02020-07-16 15:14:27 -0700509 EXPECT_CALL(*session_, SendRstStream(_, _, _, _)).Times(expected_resets);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500510 stream_->Reset(QUIC_STREAM_CANCELLED);
511 EXPECT_FALSE(fin_sent());
512 EXPECT_TRUE(rst_sent());
513
514 // Now close the stream (any further resets being sent would break the
515 // expectation above).
renjietangf00e1e12020-08-26 10:51:15 -0700516 QuicStreamPeer::CloseReadSide(stream_);
fayangb8f83442020-06-01 12:09:17 -0700517 stream_->CloseWriteSide();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500518 EXPECT_FALSE(fin_sent());
519 EXPECT_TRUE(rst_sent());
520}
521
522TEST_P(QuicStreamTest, StreamFlowControlMultipleWindowUpdates) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500523 Initialize();
524
525 // If we receive multiple WINDOW_UPDATES (potentially out of order), then we
526 // want to make sure we latch the largest offset we see.
527
528 // Initially should be default.
renjietang07b2e8d2020-08-10 15:18:24 -0700529 EXPECT_EQ(kMinimumFlowControlSendWindow,
530 QuicStreamPeer::SendWindowOffset(stream_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500531
532 // Check a single WINDOW_UPDATE results in correct offset.
533 QuicWindowUpdateFrame window_update_1(kInvalidControlFrameId, stream_->id(),
renjietang39979d32019-09-13 10:02:13 -0700534 kMinimumFlowControlSendWindow + 5);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500535 stream_->OnWindowUpdateFrame(window_update_1);
renjietang07b2e8d2020-08-10 15:18:24 -0700536 EXPECT_EQ(window_update_1.max_data,
537 QuicStreamPeer::SendWindowOffset(stream_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500538
539 // Now send a few more WINDOW_UPDATES and make sure that only the largest is
540 // remembered.
541 QuicWindowUpdateFrame window_update_2(kInvalidControlFrameId, stream_->id(),
542 1);
543 QuicWindowUpdateFrame window_update_3(kInvalidControlFrameId, stream_->id(),
renjietang39979d32019-09-13 10:02:13 -0700544 kMinimumFlowControlSendWindow + 10);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500545 QuicWindowUpdateFrame window_update_4(kInvalidControlFrameId, stream_->id(),
546 5678);
547 stream_->OnWindowUpdateFrame(window_update_2);
548 stream_->OnWindowUpdateFrame(window_update_3);
549 stream_->OnWindowUpdateFrame(window_update_4);
renjietang07b2e8d2020-08-10 15:18:24 -0700550 EXPECT_EQ(window_update_3.max_data,
551 QuicStreamPeer::SendWindowOffset(stream_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500552}
553
554TEST_P(QuicStreamTest, FrameStats) {
555 Initialize();
556
557 EXPECT_EQ(0, stream_->num_frames_received());
558 EXPECT_EQ(0, stream_->num_duplicate_frames_received());
renjietangb38e6352019-10-24 09:38:20 -0700559 QuicStreamFrame frame(stream_->id(), false, 0, ".");
560 EXPECT_CALL(*stream_, OnDataAvailable()).Times(2);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500561 stream_->OnStreamFrame(frame);
562 EXPECT_EQ(1, stream_->num_frames_received());
563 EXPECT_EQ(0, stream_->num_duplicate_frames_received());
564 stream_->OnStreamFrame(frame);
565 EXPECT_EQ(2, stream_->num_frames_received());
566 EXPECT_EQ(1, stream_->num_duplicate_frames_received());
renjietangb38e6352019-10-24 09:38:20 -0700567 QuicStreamFrame frame2(stream_->id(), false, 1, "abc");
568 stream_->OnStreamFrame(frame2);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500569}
570
571// Verify that when we receive a packet which violates flow control (i.e. sends
572// too much data on the stream) that the stream sequencer never sees this frame,
573// as we check for violation and close the connection early.
574TEST_P(QuicStreamTest, StreamSequencerNeverSeesPacketsViolatingFlowControl) {
575 Initialize();
576
577 // Receive a stream frame that violates flow control: the byte offset is
578 // higher than the receive window offset.
579 QuicStreamFrame frame(stream_->id(), false,
renjietangb38e6352019-10-24 09:38:20 -0700580 kInitialSessionFlowControlWindowForTest + 1, ".");
renjietang07b2e8d2020-08-10 15:18:24 -0700581 EXPECT_GT(frame.offset, QuicStreamPeer::ReceiveWindowOffset(stream_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500582
583 // Stream should not accept the frame, and the connection should be closed.
584 EXPECT_CALL(*connection_,
585 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _));
586 stream_->OnStreamFrame(frame);
587}
588
589// Verify that after the consumer calls StopReading(), the stream still sends
590// flow control updates.
591TEST_P(QuicStreamTest, StopReadingSendsFlowControl) {
592 Initialize();
593
594 stream_->StopReading();
595
596 // Connection should not get terminated due to flow control errors.
597 EXPECT_CALL(*connection_,
598 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _))
599 .Times(0);
600 EXPECT_CALL(*connection_, SendControlFrame(_))
601 .Times(AtLeast(1))
bnc5b3c3be2019-06-25 10:37:09 -0700602 .WillRepeatedly(Invoke(&ClearControlFrame));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500603
vasilvvc48c8712019-03-11 13:38:16 -0700604 std::string data(1000, 'x');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500605 for (QuicStreamOffset offset = 0;
606 offset < 2 * kInitialStreamFlowControlWindowForTest;
607 offset += data.length()) {
608 QuicStreamFrame frame(stream_->id(), false, offset, data);
609 stream_->OnStreamFrame(frame);
610 }
renjietang07b2e8d2020-08-10 15:18:24 -0700611 EXPECT_LT(kInitialStreamFlowControlWindowForTest,
612 QuicStreamPeer::ReceiveWindowOffset(stream_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500613}
614
615TEST_P(QuicStreamTest, FinalByteOffsetFromFin) {
616 Initialize();
617
renjietang6c066562019-11-04 17:05:59 -0800618 EXPECT_FALSE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500619
renjietangb38e6352019-10-24 09:38:20 -0700620 QuicStreamFrame stream_frame_no_fin(stream_->id(), false, 1234, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500621 stream_->OnStreamFrame(stream_frame_no_fin);
renjietang6c066562019-11-04 17:05:59 -0800622 EXPECT_FALSE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500623
renjietangb38e6352019-10-24 09:38:20 -0700624 QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500625 stream_->OnStreamFrame(stream_frame_with_fin);
renjietang6c066562019-11-04 17:05:59 -0800626 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500627}
628
629TEST_P(QuicStreamTest, FinalByteOffsetFromRst) {
630 Initialize();
631
renjietang6c066562019-11-04 17:05:59 -0800632 EXPECT_FALSE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500633 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
634 QUIC_STREAM_CANCELLED, 1234);
635 stream_->OnStreamReset(rst_frame);
renjietang6c066562019-11-04 17:05:59 -0800636 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500637}
638
639TEST_P(QuicStreamTest, InvalidFinalByteOffsetFromRst) {
640 Initialize();
641
renjietang6c066562019-11-04 17:05:59 -0800642 EXPECT_FALSE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500643 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
644 QUIC_STREAM_CANCELLED, 0xFFFFFFFFFFFF);
645 // Stream should not accept the frame, and the connection should be closed.
646 EXPECT_CALL(*connection_,
647 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _));
648 stream_->OnStreamReset(rst_frame);
renjietang6c066562019-11-04 17:05:59 -0800649 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
renjietangf00e1e12020-08-26 10:51:15 -0700650 QuicStreamPeer::CloseReadSide(stream_);
fayangb8f83442020-06-01 12:09:17 -0700651 stream_->CloseWriteSide();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500652}
653
654TEST_P(QuicStreamTest, FinalByteOffsetFromZeroLengthStreamFrame) {
655 // When receiving Trailers, an empty stream frame is created with the FIN set,
656 // and is passed to OnStreamFrame. The Trailers may be sent in advance of
657 // queued body bytes being sent, and thus the final byte offset may exceed
658 // current flow control limits. Flow control should only be concerned with
659 // data that has actually been sent/received, so verify that flow control
660 // ignores such a stream frame.
661 Initialize();
662
renjietang6c066562019-11-04 17:05:59 -0800663 EXPECT_FALSE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500664 const QuicStreamOffset kByteOffsetExceedingFlowControlWindow =
665 kInitialSessionFlowControlWindowForTest + 1;
666 const QuicStreamOffset current_stream_flow_control_offset =
renjietang07b2e8d2020-08-10 15:18:24 -0700667 QuicStreamPeer::ReceiveWindowOffset(stream_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500668 const QuicStreamOffset current_connection_flow_control_offset =
669 QuicFlowControllerPeer::ReceiveWindowOffset(session_->flow_controller());
670 ASSERT_GT(kByteOffsetExceedingFlowControlWindow,
671 current_stream_flow_control_offset);
672 ASSERT_GT(kByteOffsetExceedingFlowControlWindow,
673 current_connection_flow_control_offset);
674 QuicStreamFrame zero_length_stream_frame_with_fin(
675 stream_->id(), /*fin=*/true, kByteOffsetExceedingFlowControlWindow,
dmcardlecf0bfcf2019-12-13 08:08:21 -0800676 quiche::QuicheStringPiece());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500677 EXPECT_EQ(0, zero_length_stream_frame_with_fin.data_length);
678
679 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
680 stream_->OnStreamFrame(zero_length_stream_frame_with_fin);
renjietang6c066562019-11-04 17:05:59 -0800681 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500682
683 // The flow control receive offset values should not have changed.
renjietang07b2e8d2020-08-10 15:18:24 -0700684 EXPECT_EQ(current_stream_flow_control_offset,
685 QuicStreamPeer::ReceiveWindowOffset(stream_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500686 EXPECT_EQ(
687 current_connection_flow_control_offset,
688 QuicFlowControllerPeer::ReceiveWindowOffset(session_->flow_controller()));
689}
690
691TEST_P(QuicStreamTest, OnStreamResetOffsetOverflow) {
692 Initialize();
693 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
694 QUIC_STREAM_CANCELLED, kMaxStreamLength + 1);
695 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
696 stream_->OnStreamReset(rst_frame);
697}
698
699TEST_P(QuicStreamTest, OnStreamFrameUpperLimit) {
700 Initialize();
701
702 // Modify receive window offset and sequencer buffer total_bytes_read_ to
703 // avoid flow control violation.
renjietang07b2e8d2020-08-10 15:18:24 -0700704 QuicStreamPeer::SetReceiveWindowOffset(stream_, kMaxStreamLength + 5u);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500705 QuicFlowControllerPeer::SetReceiveWindowOffset(session_->flow_controller(),
706 kMaxStreamLength + 5u);
707 QuicStreamSequencerPeer::SetFrameBufferTotalBytesRead(
708 QuicStreamPeer::sequencer(stream_), kMaxStreamLength - 10u);
709
710 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _))
711 .Times(0);
renjietangb38e6352019-10-24 09:38:20 -0700712 QuicStreamFrame stream_frame(stream_->id(), false, kMaxStreamLength - 1, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500713 stream_->OnStreamFrame(stream_frame);
renjietangb38e6352019-10-24 09:38:20 -0700714 QuicStreamFrame stream_frame2(stream_->id(), true, kMaxStreamLength, "");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500715 stream_->OnStreamFrame(stream_frame2);
716}
717
718TEST_P(QuicStreamTest, StreamTooLong) {
719 Initialize();
720 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _))
721 .Times(1);
renjietangb38e6352019-10-24 09:38:20 -0700722 QuicStreamFrame stream_frame(stream_->id(), false, kMaxStreamLength, ".");
dmcardlecf0bfcf2019-12-13 08:08:21 -0800723 EXPECT_QUIC_PEER_BUG(
724 stream_->OnStreamFrame(stream_frame),
725 quiche::QuicheStrCat("Receive stream frame on stream ", stream_->id(),
726 " reaches max stream length"));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500727}
728
renjietang9bf25642019-10-10 10:21:15 -0700729TEST_P(QuicStreamTest, SetDrainingIncomingOutgoing) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500730 // Don't have incoming data consumed.
731 Initialize();
732
733 // Incoming data with FIN.
renjietangb38e6352019-10-24 09:38:20 -0700734 QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500735 stream_->OnStreamFrame(stream_frame_with_fin);
736 // The FIN has been received but not consumed.
renjietang6c066562019-11-04 17:05:59 -0800737 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500738 EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_));
739 EXPECT_FALSE(stream_->reading_stopped());
740
fayang01591ae2020-04-23 14:14:56 -0700741 EXPECT_EQ(1u, QuicSessionPeer::GetNumOpenDynamicStreams(session_.get()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500742
743 // Outgoing data with FIN.
renjietang41a1b412020-02-27 15:05:14 -0800744 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500745 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -0800746 return session_->ConsumeData(stream_->id(), 2u, 0u, FIN,
vasilvv9efbb912020-06-05 08:59:26 -0700747 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500748 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800749 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), true,
750 nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500751 EXPECT_TRUE(stream_->write_side_closed());
752
fayang116fa4c2020-06-02 08:10:49 -0700753 EXPECT_EQ(1u, QuicSessionPeer::GetNumDrainingStreams(session_.get()));
fayang01591ae2020-04-23 14:14:56 -0700754 EXPECT_EQ(0u, QuicSessionPeer::GetNumOpenDynamicStreams(session_.get()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500755}
756
renjietang9bf25642019-10-10 10:21:15 -0700757TEST_P(QuicStreamTest, SetDrainingOutgoingIncoming) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500758 // Don't have incoming data consumed.
759 Initialize();
760
761 // Outgoing data with FIN.
renjietang41a1b412020-02-27 15:05:14 -0800762 EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500763 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -0800764 return session_->ConsumeData(stream_->id(), 2u, 0u, FIN,
vasilvv9efbb912020-06-05 08:59:26 -0700765 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500766 }));
dmcardlecf0bfcf2019-12-13 08:08:21 -0800767 stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), true,
768 nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500769 EXPECT_TRUE(stream_->write_side_closed());
770
fayang01591ae2020-04-23 14:14:56 -0700771 EXPECT_EQ(1u, QuicSessionPeer::GetNumOpenDynamicStreams(session_.get()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500772
773 // Incoming data with FIN.
renjietangb38e6352019-10-24 09:38:20 -0700774 QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, ".");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500775 stream_->OnStreamFrame(stream_frame_with_fin);
776 // The FIN has been received but not consumed.
renjietang6c066562019-11-04 17:05:59 -0800777 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500778 EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_));
779 EXPECT_FALSE(stream_->reading_stopped());
780
fayang116fa4c2020-06-02 08:10:49 -0700781 EXPECT_EQ(1u, QuicSessionPeer::GetNumDrainingStreams(session_.get()));
fayang01591ae2020-04-23 14:14:56 -0700782 EXPECT_EQ(0u, QuicSessionPeer::GetNumOpenDynamicStreams(session_.get()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500783}
784
785TEST_P(QuicStreamTest, EarlyResponseFinHandling) {
786 // Verify that if the server completes the response before reading the end of
787 // the request, the received FIN is recorded.
788
789 Initialize();
790 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
renjietang41a1b412020-02-27 15:05:14 -0800791 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800792 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500793
794 // Receive data for the request.
renjietangb38e6352019-10-24 09:38:20 -0700795 EXPECT_CALL(*stream_, OnDataAvailable()).Times(1);
796 QuicStreamFrame frame1(stream_->id(), false, 0, "Start");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500797 stream_->OnStreamFrame(frame1);
798 // When QuicSimpleServerStream sends the response, it calls
799 // QuicStream::CloseReadSide() first.
800 QuicStreamPeer::CloseReadSide(stream_);
801 // Send data and FIN for the response.
802 stream_->WriteOrBufferData(kData1, false, nullptr);
803 EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_));
804 // Receive remaining data and FIN for the request.
renjietangb38e6352019-10-24 09:38:20 -0700805 QuicStreamFrame frame2(stream_->id(), true, 0, "End");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500806 stream_->OnStreamFrame(frame2);
807 EXPECT_TRUE(stream_->fin_received());
renjietang6c066562019-11-04 17:05:59 -0800808 EXPECT_TRUE(stream_->HasReceivedFinalOffset());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500809}
810
811TEST_P(QuicStreamTest, StreamWaitsForAcks) {
812 Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800813 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800814 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500815 // Stream is not waiting for acks initially.
816 EXPECT_FALSE(stream_->IsWaitingForAcks());
817 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
zhongyi9fa2be32019-09-10 12:39:01 -0700818 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500819
820 // Send kData1.
821 stream_->WriteOrBufferData(kData1, false, nullptr);
fayang8265a2a2019-10-16 11:23:51 -0700822 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500823 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
824 EXPECT_TRUE(stream_->IsWaitingForAcks());
825 QuicByteCount newly_acked_length = 0;
826 EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800827 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500828 &newly_acked_length));
829 EXPECT_EQ(9u, newly_acked_length);
830 // Stream is not waiting for acks as all sent data is acked.
831 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700832 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500833 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
834
835 // Send kData2.
836 stream_->WriteOrBufferData(kData2, false, nullptr);
837 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700838 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500839 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
840 // Send FIN.
841 stream_->WriteOrBufferData("", true, nullptr);
842 // Fin only frame is not stored in send buffer.
843 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
844
845 // kData2 is retransmitted.
846 stream_->OnStreamFrameRetransmitted(9, 9, false);
847
848 // kData2 is acked.
849 EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800850 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500851 &newly_acked_length));
852 EXPECT_EQ(9u, newly_acked_length);
853 // Stream is waiting for acks as FIN is not acked.
854 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700855 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500856 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
857
858 // FIN is acked.
859 EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 0, true, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800860 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500861 &newly_acked_length));
862 EXPECT_EQ(0u, newly_acked_length);
863 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700864 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500865 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
866}
867
868TEST_P(QuicStreamTest, StreamDataGetAckedOutOfOrder) {
869 Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800870 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800871 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500872 // Send data.
873 stream_->WriteOrBufferData(kData1, false, nullptr);
874 stream_->WriteOrBufferData(kData1, false, nullptr);
875 stream_->WriteOrBufferData(kData1, false, nullptr);
876 stream_->WriteOrBufferData("", true, nullptr);
877 EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size());
878 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700879 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500880 QuicByteCount newly_acked_length = 0;
881 EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800882 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500883 &newly_acked_length));
fayang8265a2a2019-10-16 11:23:51 -0700884 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500885 EXPECT_EQ(9u, newly_acked_length);
886 EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size());
887 EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800888 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500889 &newly_acked_length));
fayang8265a2a2019-10-16 11:23:51 -0700890 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500891 EXPECT_EQ(9u, newly_acked_length);
892 EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size());
893 EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800894 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500895 &newly_acked_length));
fayang8265a2a2019-10-16 11:23:51 -0700896 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500897 EXPECT_EQ(9u, newly_acked_length);
898 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
899 // FIN is not acked yet.
900 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700901 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500902 EXPECT_TRUE(stream_->OnStreamFrameAcked(27, 0, true, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -0800903 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500904 &newly_acked_length));
905 EXPECT_EQ(0u, newly_acked_length);
906 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700907 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500908}
909
910TEST_P(QuicStreamTest, CancelStream) {
911 Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800912 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800913 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500914 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700915 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500916 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
917
918 stream_->WriteOrBufferData(kData1, false, nullptr);
919 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700920 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500921 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
922 // Cancel stream.
923 stream_->Reset(QUIC_STREAM_NO_ERROR);
924 // stream still waits for acks as the error code is QUIC_STREAM_NO_ERROR, and
925 // data is going to be retransmitted.
926 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700927 EXPECT_TRUE(session_->HasUnackedStreamData());
zhongyi1b2f7832019-06-14 13:31:34 -0700928 EXPECT_CALL(*connection_,
929 OnStreamReset(stream_->id(), QUIC_STREAM_CANCELLED));
renjietangeab92852019-10-25 12:16:14 -0700930 EXPECT_CALL(*connection_, SendControlFrame(_))
931 .Times(AtLeast(1))
932 .WillRepeatedly(Invoke(&ClearControlFrame));
renjietang9946bc02020-07-16 15:14:27 -0700933 EXPECT_CALL(*session_,
934 SendRstStream(stream_->id(), QUIC_STREAM_CANCELLED, 9, _))
zhongyi1b2f7832019-06-14 13:31:34 -0700935 .WillOnce(InvokeWithoutArgs([this]() {
renjietang61cc2452019-11-26 10:57:10 -0800936 session_->ReallySendRstStream(stream_->id(), QUIC_STREAM_CANCELLED,
renjietang9946bc02020-07-16 15:14:27 -0700937 stream_->stream_bytes_written(), false);
zhongyi1b2f7832019-06-14 13:31:34 -0700938 }));
939
QUICHE teama6ef0a62019-03-07 20:34:33 -0500940 stream_->Reset(QUIC_STREAM_CANCELLED);
941 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
942 // Stream stops waiting for acks as data is not going to be retransmitted.
943 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700944 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500945}
946
947TEST_P(QuicStreamTest, RstFrameReceivedStreamNotFinishSending) {
renjietang9bf25642019-10-10 10:21:15 -0700948 if (VersionHasIetfQuicFrames(GetParam().transport_version)) {
949 // In IETF QUIC, receiving a RESET_STREAM will only close the read side. The
950 // stream itself is not closed and will not send reset.
951 return;
952 }
953
QUICHE teama6ef0a62019-03-07 20:34:33 -0500954 Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800955 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800956 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500957 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700958 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500959 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
960
961 stream_->WriteOrBufferData(kData1, false, nullptr);
962 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700963 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500964 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
965
966 // RST_STREAM received.
967 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
968 QUIC_STREAM_CANCELLED, 9);
969 EXPECT_CALL(*session_,
renjietang9946bc02020-07-16 15:14:27 -0700970 SendRstStream(stream_->id(), QUIC_RST_ACKNOWLEDGEMENT, 9, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500971 stream_->OnStreamReset(rst_frame);
972 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
973 // Stream stops waiting for acks as it does not finish sending and rst is
974 // sent.
975 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700976 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500977}
978
979TEST_P(QuicStreamTest, RstFrameReceivedStreamFinishSending) {
980 Initialize();
renjietang41a1b412020-02-27 15:05:14 -0800981 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -0800982 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500983 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -0700984 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500985 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
986
987 stream_->WriteOrBufferData(kData1, true, nullptr);
988 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700989 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500990
991 // RST_STREAM received.
renjietang9946bc02020-07-16 15:14:27 -0700992 EXPECT_CALL(*session_, SendRstStream(_, _, _, _)).Times(0);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500993 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
994 QUIC_STREAM_CANCELLED, 1234);
995 stream_->OnStreamReset(rst_frame);
996 // Stream still waits for acks as it finishes sending and has unacked data.
997 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -0700998 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500999 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1000}
1001
1002TEST_P(QuicStreamTest, ConnectionClosed) {
1003 Initialize();
renjietang41a1b412020-02-27 15:05:14 -08001004 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001005 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001006 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -07001007 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001008 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1009
1010 stream_->WriteOrBufferData(kData1, false, nullptr);
1011 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -07001012 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001013 EXPECT_CALL(*session_,
renjietang9946bc02020-07-16 15:14:27 -07001014 SendRstStream(stream_->id(), QUIC_RST_ACKNOWLEDGEMENT, 9, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001015 stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR,
1016 ConnectionCloseSource::FROM_SELF);
1017 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1018 // Stream stops waiting for acks as connection is going to close.
1019 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -07001020 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001021}
1022
1023TEST_P(QuicStreamTest, CanWriteNewDataAfterData) {
wub49855982019-05-01 14:16:26 -07001024 SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001025 Initialize();
1026 EXPECT_TRUE(stream_->CanWriteNewDataAfterData(99));
1027 EXPECT_FALSE(stream_->CanWriteNewDataAfterData(100));
1028}
1029
1030TEST_P(QuicStreamTest, WriteBufferedData) {
1031 // Set buffered data low water mark to be 100.
wub49855982019-05-01 14:16:26 -07001032 SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001033
1034 Initialize();
vasilvvc48c8712019-03-11 13:38:16 -07001035 std::string data(1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001036 EXPECT_TRUE(stream_->CanWriteNewData());
1037
1038 // Testing WriteOrBufferData.
renjietang41a1b412020-02-27 15:05:14 -08001039 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001040 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -08001041 return session_->ConsumeData(stream_->id(), 100u, 0u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -07001042 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001043 }));
1044 stream_->WriteOrBufferData(data, false, nullptr);
1045 stream_->WriteOrBufferData(data, false, nullptr);
1046 stream_->WriteOrBufferData(data, false, nullptr);
zhongyi1b2f7832019-06-14 13:31:34 -07001047 EXPECT_TRUE(stream_->IsWaitingForAcks());
1048
QUICHE teama6ef0a62019-03-07 20:34:33 -05001049 // Verify all data is saved.
1050 EXPECT_EQ(3 * data.length() - 100, stream_->BufferedDataBytes());
1051
renjietang41a1b412020-02-27 15:05:14 -08001052 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001053 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -08001054 return session_->ConsumeData(stream_->id(), 100, 100u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -07001055 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001056 }));
1057 // Buffered data size > threshold, do not ask upper layer for more data.
1058 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(0);
1059 stream_->OnCanWrite();
1060 EXPECT_EQ(3 * data.length() - 200, stream_->BufferedDataBytes());
1061 EXPECT_FALSE(stream_->CanWriteNewData());
1062
1063 // Send buffered data to make buffered data size < threshold.
dschinazif1e7b422020-04-30 12:21:28 -07001064 QuicByteCount data_to_write =
1065 3 * data.length() - 200 -
1066 GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1;
renjietang41a1b412020-02-27 15:05:14 -08001067 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001068 .WillOnce(InvokeWithoutArgs([this, data_to_write]() {
renjietang7c239172020-02-21 13:50:39 -08001069 return session_->ConsumeData(stream_->id(), data_to_write, 200u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -07001070 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001071 }));
1072 // Buffered data size < threshold, ask upper layer for more data.
1073 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1);
1074 stream_->OnCanWrite();
dschinazi696ee602019-04-25 16:05:35 -07001075 EXPECT_EQ(static_cast<uint64_t>(
1076 GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001077 stream_->BufferedDataBytes());
1078 EXPECT_TRUE(stream_->CanWriteNewData());
1079
1080 // Flush all buffered data.
renjietang41a1b412020-02-27 15:05:14 -08001081 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001082 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001083 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1);
1084 stream_->OnCanWrite();
1085 EXPECT_EQ(0u, stream_->BufferedDataBytes());
1086 EXPECT_FALSE(stream_->HasBufferedData());
1087 EXPECT_TRUE(stream_->CanWriteNewData());
1088
1089 // Testing Writev.
renjietang41a1b412020-02-27 15:05:14 -08001090 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001091 .WillOnce(Return(QuicConsumedData(0, false)));
1092 struct iovec iov = {const_cast<char*>(data.data()), data.length()};
1093 QuicMemSliceStorage storage(
1094 &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(),
1095 1024);
1096 QuicConsumedData consumed = stream_->WriteMemSlices(storage.ToSpan(), false);
1097
1098 // There is no buffered data before, all data should be consumed without
1099 // respecting buffered data upper limit.
1100 EXPECT_EQ(data.length(), consumed.bytes_consumed);
1101 EXPECT_FALSE(consumed.fin_consumed);
1102 EXPECT_EQ(data.length(), stream_->BufferedDataBytes());
1103 EXPECT_FALSE(stream_->CanWriteNewData());
1104
renjietang41a1b412020-02-27 15:05:14 -08001105 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001106 QuicMemSliceStorage storage2(
1107 &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(),
1108 1024);
1109 consumed = stream_->WriteMemSlices(storage2.ToSpan(), false);
1110 // No Data can be consumed as buffered data is beyond upper limit.
1111 EXPECT_EQ(0u, consumed.bytes_consumed);
1112 EXPECT_FALSE(consumed.fin_consumed);
1113 EXPECT_EQ(data.length(), stream_->BufferedDataBytes());
1114
1115 data_to_write =
1116 data.length() - GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1;
renjietang41a1b412020-02-27 15:05:14 -08001117 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001118 .WillOnce(InvokeWithoutArgs([this, data_to_write]() {
renjietang7c239172020-02-21 13:50:39 -08001119 return session_->ConsumeData(stream_->id(), data_to_write, 0u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -07001120 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001121 }));
1122
1123 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1);
1124 stream_->OnCanWrite();
dschinazi696ee602019-04-25 16:05:35 -07001125 EXPECT_EQ(static_cast<uint64_t>(
1126 GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001127 stream_->BufferedDataBytes());
1128 EXPECT_TRUE(stream_->CanWriteNewData());
1129
renjietang41a1b412020-02-27 15:05:14 -08001130 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001131 // All data can be consumed as buffered data is below upper limit.
1132 QuicMemSliceStorage storage3(
1133 &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(),
1134 1024);
1135 consumed = stream_->WriteMemSlices(storage3.ToSpan(), false);
1136 EXPECT_EQ(data.length(), consumed.bytes_consumed);
1137 EXPECT_FALSE(consumed.fin_consumed);
1138 EXPECT_EQ(data.length() + GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1,
1139 stream_->BufferedDataBytes());
1140 EXPECT_FALSE(stream_->CanWriteNewData());
1141}
1142
1143TEST_P(QuicStreamTest, WritevDataReachStreamLimit) {
1144 Initialize();
vasilvvc48c8712019-03-11 13:38:16 -07001145 std::string data("aaaaa");
QUICHE teama6ef0a62019-03-07 20:34:33 -05001146 QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - data.length(),
1147 stream_);
renjietang41a1b412020-02-27 15:05:14 -08001148 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001149 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001150 struct iovec iov = {const_cast<char*>(data.data()), 5u};
1151 QuicMemSliceStorage storage(
1152 &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(),
1153 1024);
1154 QuicConsumedData consumed = stream_->WriteMemSlices(storage.ToSpan(), false);
1155 EXPECT_EQ(data.length(), consumed.bytes_consumed);
1156 struct iovec iov2 = {const_cast<char*>(data.data()), 1u};
1157 QuicMemSliceStorage storage2(
1158 &iov2, 1,
1159 session_->connection()->helper()->GetStreamSendBufferAllocator(), 1024);
1160 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
1161 EXPECT_QUIC_BUG(stream_->WriteMemSlices(storage2.ToSpan(), false),
1162 "Write too many data via stream");
1163}
1164
1165TEST_P(QuicStreamTest, WriteMemSlices) {
1166 // Set buffered data low water mark to be 100.
wub49855982019-05-01 14:16:26 -07001167 SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001168
1169 Initialize();
1170 char data[1024];
nharper6b7aa8b2020-04-30 17:23:35 -07001171 std::vector<std::pair<char*, size_t>> buffers;
bnc4e9283d2019-12-17 07:08:57 -08001172 buffers.push_back(std::make_pair(data, QUICHE_ARRAYSIZE(data)));
1173 buffers.push_back(std::make_pair(data, QUICHE_ARRAYSIZE(data)));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001174 QuicTestMemSliceVector vector1(buffers);
1175 QuicTestMemSliceVector vector2(buffers);
1176 QuicMemSliceSpan span1 = vector1.span();
1177 QuicMemSliceSpan span2 = vector2.span();
1178
renjietang41a1b412020-02-27 15:05:14 -08001179 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001180 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -08001181 return session_->ConsumeData(stream_->id(), 100u, 0u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -07001182 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001183 }));
1184 // There is no buffered data before, all data should be consumed.
1185 QuicConsumedData consumed = stream_->WriteMemSlices(span1, false);
1186 EXPECT_EQ(2048u, consumed.bytes_consumed);
1187 EXPECT_FALSE(consumed.fin_consumed);
bnc4e9283d2019-12-17 07:08:57 -08001188 EXPECT_EQ(2 * QUICHE_ARRAYSIZE(data) - 100, stream_->BufferedDataBytes());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001189 EXPECT_FALSE(stream_->fin_buffered());
1190
renjietang41a1b412020-02-27 15:05:14 -08001191 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001192 // No Data can be consumed as buffered data is beyond upper limit.
1193 consumed = stream_->WriteMemSlices(span2, true);
1194 EXPECT_EQ(0u, consumed.bytes_consumed);
1195 EXPECT_FALSE(consumed.fin_consumed);
bnc4e9283d2019-12-17 07:08:57 -08001196 EXPECT_EQ(2 * QUICHE_ARRAYSIZE(data) - 100, stream_->BufferedDataBytes());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001197 EXPECT_FALSE(stream_->fin_buffered());
1198
dschinazif1e7b422020-04-30 12:21:28 -07001199 QuicByteCount data_to_write =
1200 2 * QUICHE_ARRAYSIZE(data) - 100 -
1201 GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1;
renjietang41a1b412020-02-27 15:05:14 -08001202 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001203 .WillOnce(InvokeWithoutArgs([this, data_to_write]() {
renjietang7c239172020-02-21 13:50:39 -08001204 return session_->ConsumeData(stream_->id(), data_to_write, 100u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -07001205 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001206 }));
1207 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1);
1208 stream_->OnCanWrite();
dschinazi696ee602019-04-25 16:05:35 -07001209 EXPECT_EQ(static_cast<uint64_t>(
1210 GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001211 stream_->BufferedDataBytes());
1212 // Try to write slices2 again.
renjietang41a1b412020-02-27 15:05:14 -08001213 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001214 consumed = stream_->WriteMemSlices(span2, true);
1215 EXPECT_EQ(2048u, consumed.bytes_consumed);
1216 EXPECT_TRUE(consumed.fin_consumed);
bnc4e9283d2019-12-17 07:08:57 -08001217 EXPECT_EQ(2 * QUICHE_ARRAYSIZE(data) +
QUICHE teama6ef0a62019-03-07 20:34:33 -05001218 GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1,
1219 stream_->BufferedDataBytes());
1220 EXPECT_TRUE(stream_->fin_buffered());
1221
1222 // Flush all buffered data.
renjietang41a1b412020-02-27 15:05:14 -08001223 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001224 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001225 stream_->OnCanWrite();
1226 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(0);
1227 EXPECT_FALSE(stream_->HasBufferedData());
1228 EXPECT_TRUE(stream_->write_side_closed());
1229}
1230
1231TEST_P(QuicStreamTest, WriteMemSlicesReachStreamLimit) {
1232 Initialize();
1233 QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - 5u, stream_);
1234 char data[5];
nharper6b7aa8b2020-04-30 17:23:35 -07001235 std::vector<std::pair<char*, size_t>> buffers;
bnc4e9283d2019-12-17 07:08:57 -08001236 buffers.push_back(std::make_pair(data, QUICHE_ARRAYSIZE(data)));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001237 QuicTestMemSliceVector vector1(buffers);
1238 QuicMemSliceSpan span1 = vector1.span();
renjietang41a1b412020-02-27 15:05:14 -08001239 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001240 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -08001241 return session_->ConsumeData(stream_->id(), 5u, 0u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -07001242 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001243 }));
1244 // There is no buffered data before, all data should be consumed.
1245 QuicConsumedData consumed = stream_->WriteMemSlices(span1, false);
1246 EXPECT_EQ(5u, consumed.bytes_consumed);
1247
nharper6b7aa8b2020-04-30 17:23:35 -07001248 std::vector<std::pair<char*, size_t>> buffers2;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001249 buffers2.push_back(std::make_pair(data, 1u));
1250 QuicTestMemSliceVector vector2(buffers);
1251 QuicMemSliceSpan span2 = vector2.span();
1252 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
1253 EXPECT_QUIC_BUG(stream_->WriteMemSlices(span2, false),
1254 "Write too many data via stream");
1255}
1256
1257TEST_P(QuicStreamTest, StreamDataGetAckedMultipleTimes) {
1258 Initialize();
renjietang41a1b412020-02-27 15:05:14 -08001259 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001260 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
zhongyi1b2f7832019-06-14 13:31:34 -07001261 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -07001262 EXPECT_FALSE(session_->HasUnackedStreamData());
zhongyi1b2f7832019-06-14 13:31:34 -07001263
QUICHE teama6ef0a62019-03-07 20:34:33 -05001264 // Send [0, 27) and fin.
1265 stream_->WriteOrBufferData(kData1, false, nullptr);
1266 stream_->WriteOrBufferData(kData1, false, nullptr);
1267 stream_->WriteOrBufferData(kData1, true, nullptr);
1268 EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size());
1269 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -07001270 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001271 // Ack [0, 9), [5, 22) and [18, 26)
1272 // Verify [0, 9) 9 bytes are acked.
1273 QuicByteCount newly_acked_length = 0;
1274 EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001275 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001276 &newly_acked_length));
1277 EXPECT_EQ(9u, newly_acked_length);
1278 EXPECT_EQ(2u, QuicStreamPeer::SendBuffer(stream_).size());
1279 // Verify [9, 22) 13 bytes are acked.
1280 EXPECT_TRUE(stream_->OnStreamFrameAcked(5, 17, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001281 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001282 &newly_acked_length));
1283 EXPECT_EQ(13u, newly_acked_length);
1284 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1285 // Verify [22, 26) 4 bytes are acked.
1286 EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 8, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001287 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001288 &newly_acked_length));
1289 EXPECT_EQ(4u, newly_acked_length);
1290 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size());
1291 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -07001292 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001293
1294 // Ack [0, 27). Verify [26, 27) 1 byte is acked.
1295 EXPECT_TRUE(stream_->OnStreamFrameAcked(26, 1, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001296 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001297 &newly_acked_length));
1298 EXPECT_EQ(1u, newly_acked_length);
1299 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1300 EXPECT_TRUE(stream_->IsWaitingForAcks());
fayang8265a2a2019-10-16 11:23:51 -07001301 EXPECT_TRUE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001302
1303 // Ack Fin.
1304 EXPECT_TRUE(stream_->OnStreamFrameAcked(27, 0, true, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001305 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001306 &newly_acked_length));
1307 EXPECT_EQ(0u, newly_acked_length);
1308 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1309 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -07001310 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001311
1312 // Ack [10, 27) and fin. No new data is acked.
QUICHE team2f5f30b2020-02-18 08:52:28 -08001313 EXPECT_FALSE(
1314 stream_->OnStreamFrameAcked(10, 17, true, QuicTime::Delta::Zero(),
1315 QuicTime::Zero(), &newly_acked_length));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001316 EXPECT_EQ(0u, newly_acked_length);
1317 EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size());
1318 EXPECT_FALSE(stream_->IsWaitingForAcks());
zhongyi9fa2be32019-09-10 12:39:01 -07001319 EXPECT_FALSE(session_->HasUnackedStreamData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001320}
1321
1322TEST_P(QuicStreamTest, OnStreamFrameLost) {
1323 Initialize();
1324
1325 // Send [0, 9).
renjietang41a1b412020-02-27 15:05:14 -08001326 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001327 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001328 stream_->WriteOrBufferData(kData1, false, nullptr);
1329 EXPECT_FALSE(stream_->HasBufferedData());
1330 EXPECT_TRUE(stream_->IsStreamFrameOutstanding(0, 9, false));
1331
1332 // Try to send [9, 27), but connection is blocked.
renjietang41a1b412020-02-27 15:05:14 -08001333 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001334 .WillOnce(Return(QuicConsumedData(0, false)));
1335 stream_->WriteOrBufferData(kData2, false, nullptr);
1336 stream_->WriteOrBufferData(kData2, false, nullptr);
1337 EXPECT_TRUE(stream_->HasBufferedData());
1338 EXPECT_FALSE(stream_->HasPendingRetransmission());
1339
1340 // Lost [0, 9). When stream gets a chance to write, only lost data is
1341 // transmitted.
1342 stream_->OnStreamFrameLost(0, 9, false);
1343 EXPECT_TRUE(stream_->HasPendingRetransmission());
renjietang41a1b412020-02-27 15:05:14 -08001344 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001345 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
renjietangb38e6352019-10-24 09:38:20 -07001346 EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001347 stream_->OnCanWrite();
1348 EXPECT_FALSE(stream_->HasPendingRetransmission());
1349 EXPECT_TRUE(stream_->HasBufferedData());
1350
1351 // This OnCanWrite causes [9, 27) to be sent.
renjietang41a1b412020-02-27 15:05:14 -08001352 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001353 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001354 stream_->OnCanWrite();
1355 EXPECT_FALSE(stream_->HasBufferedData());
1356
1357 // Send a fin only frame.
renjietang41a1b412020-02-27 15:05:14 -08001358 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001359 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001360 stream_->WriteOrBufferData("", true, nullptr);
1361
1362 // Lost [9, 27) and fin.
1363 stream_->OnStreamFrameLost(9, 18, false);
1364 stream_->OnStreamFrameLost(27, 0, true);
1365 EXPECT_TRUE(stream_->HasPendingRetransmission());
1366
1367 // Ack [9, 18).
1368 QuicByteCount newly_acked_length = 0;
1369 EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001370 QuicTime::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001371 &newly_acked_length));
1372 EXPECT_EQ(9u, newly_acked_length);
1373 EXPECT_FALSE(stream_->IsStreamFrameOutstanding(9, 3, false));
1374 EXPECT_TRUE(stream_->HasPendingRetransmission());
1375 // This OnCanWrite causes [18, 27) and fin to be retransmitted. Verify fin can
1376 // be bundled with data.
renjietang41a1b412020-02-27 15:05:14 -08001377 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001378 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -08001379 return session_->ConsumeData(stream_->id(), 9u, 18u, FIN,
vasilvv9efbb912020-06-05 08:59:26 -07001380 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001381 }));
1382 stream_->OnCanWrite();
1383 EXPECT_FALSE(stream_->HasPendingRetransmission());
1384 // Lost [9, 18) again, but it is not considered as lost because kData2
1385 // has been acked.
1386 stream_->OnStreamFrameLost(9, 9, false);
1387 EXPECT_FALSE(stream_->HasPendingRetransmission());
1388 EXPECT_TRUE(stream_->IsStreamFrameOutstanding(27, 0, true));
1389}
1390
1391TEST_P(QuicStreamTest, CannotBundleLostFin) {
1392 Initialize();
1393
1394 // Send [0, 18) and fin.
renjietang41a1b412020-02-27 15:05:14 -08001395 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001396 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001397 stream_->WriteOrBufferData(kData1, false, nullptr);
1398 stream_->WriteOrBufferData(kData2, true, nullptr);
1399
1400 // Lost [0, 9) and fin.
1401 stream_->OnStreamFrameLost(0, 9, false);
1402 stream_->OnStreamFrameLost(18, 0, true);
1403
1404 // Retransmit lost data. Verify [0, 9) and fin are retransmitted in two
1405 // frames.
1406 InSequence s;
renjietang41a1b412020-02-27 15:05:14 -08001407 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001408 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -08001409 return session_->ConsumeData(stream_->id(), 9u, 0u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -07001410 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001411 }));
renjietang41a1b412020-02-27 15:05:14 -08001412 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001413 .WillOnce(Return(QuicConsumedData(0, true)));
1414 stream_->OnCanWrite();
1415}
1416
1417TEST_P(QuicStreamTest, MarkConnectionLevelWriteBlockedOnWindowUpdateFrame) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001418 Initialize();
1419
renjietang39979d32019-09-13 10:02:13 -07001420 // Set the config to a small value so that a newly created stream has small
1421 // send flow control window.
1422 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(session_->config(),
1423 100);
renjietang9bf25642019-10-10 10:21:15 -07001424 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional(
1425 session_->config(), 100);
renjietang39979d32019-09-13 10:02:13 -07001426 auto stream = new TestStream(GetNthClientInitiatedBidirectionalStreamId(
1427 GetParam().transport_version, 2),
1428 session_.get(), BIDIRECTIONAL);
1429 session_->ActivateStream(QuicWrapUnique(stream));
1430
renjietang41a1b412020-02-27 15:05:14 -08001431 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001432 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001433 EXPECT_CALL(*connection_, SendControlFrame(_))
bnc5b3c3be2019-06-25 10:37:09 -07001434 .WillOnce(Invoke(&ClearControlFrame));
vasilvvc48c8712019-03-11 13:38:16 -07001435 std::string data(1024, '.');
renjietang39979d32019-09-13 10:02:13 -07001436 stream->WriteOrBufferData(data, false, nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001437 EXPECT_FALSE(HasWriteBlockedStreams());
1438
1439 QuicWindowUpdateFrame window_update(kInvalidControlFrameId, stream_->id(),
1440 1234);
1441
renjietang39979d32019-09-13 10:02:13 -07001442 stream->OnWindowUpdateFrame(window_update);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001443 // Verify stream is marked connection level write blocked.
1444 EXPECT_TRUE(HasWriteBlockedStreams());
renjietang39979d32019-09-13 10:02:13 -07001445 EXPECT_TRUE(stream->HasBufferedData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001446}
1447
1448// Regression test for b/73282665.
1449TEST_P(QuicStreamTest,
1450 MarkConnectionLevelWriteBlockedOnWindowUpdateFrameWithNoBufferedData) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001451 Initialize();
1452
renjietang39979d32019-09-13 10:02:13 -07001453 // Set the config to a small value so that a newly created stream has small
1454 // send flow control window.
1455 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(session_->config(),
1456 100);
renjietang9bf25642019-10-10 10:21:15 -07001457 QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional(
1458 session_->config(), 100);
renjietang39979d32019-09-13 10:02:13 -07001459 auto stream = new TestStream(GetNthClientInitiatedBidirectionalStreamId(
1460 GetParam().transport_version, 2),
1461 session_.get(), BIDIRECTIONAL);
1462 session_->ActivateStream(QuicWrapUnique(stream));
1463
1464 std::string data(100, '.');
renjietang41a1b412020-02-27 15:05:14 -08001465 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _))
renjietang7c239172020-02-21 13:50:39 -08001466 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001467 EXPECT_CALL(*connection_, SendControlFrame(_))
bnc5b3c3be2019-06-25 10:37:09 -07001468 .WillOnce(Invoke(&ClearControlFrame));
renjietang39979d32019-09-13 10:02:13 -07001469 stream->WriteOrBufferData(data, false, nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001470 EXPECT_FALSE(HasWriteBlockedStreams());
1471
1472 QuicWindowUpdateFrame window_update(kInvalidControlFrameId, stream_->id(),
1473 120);
renjietang39979d32019-09-13 10:02:13 -07001474 stream->OnWindowUpdateFrame(window_update);
1475 EXPECT_FALSE(stream->HasBufferedData());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001476 // Verify stream is marked as blocked although there is no buffered data.
1477 EXPECT_TRUE(HasWriteBlockedStreams());
1478}
1479
1480TEST_P(QuicStreamTest, RetransmitStreamData) {
1481 Initialize();
1482 InSequence s;
1483
1484 // Send [0, 18) with fin.
renjietang41a1b412020-02-27 15:05:14 -08001485 EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001486 .Times(2)
renjietang7c239172020-02-21 13:50:39 -08001487 .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001488 stream_->WriteOrBufferData(kData1, false, nullptr);
1489 stream_->WriteOrBufferData(kData1, true, nullptr);
1490 // Ack [10, 13).
1491 QuicByteCount newly_acked_length = 0;
1492 stream_->OnStreamFrameAcked(10, 3, false, QuicTime::Delta::Zero(),
QUICHE team2f5f30b2020-02-18 08:52:28 -08001493 QuicTime::Zero(), &newly_acked_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001494 EXPECT_EQ(3u, newly_acked_length);
1495 // Retransmit [0, 18) with fin, and only [0, 8) is consumed.
renjietang41a1b412020-02-27 15:05:14 -08001496 EXPECT_CALL(*session_, WritevData(stream_->id(), 10, 0, NO_FIN, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -05001497 .WillOnce(InvokeWithoutArgs([this]() {
renjietang4d992bf2020-03-03 13:01:55 -08001498 return session_->ConsumeData(stream_->id(), 8, 0u, NO_FIN,
vasilvv9efbb912020-06-05 08:59:26 -07001499 NOT_RETRANSMISSION, QUICHE_NULLOPT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001500 }));
renjietang4d992bf2020-03-03 13:01:55 -08001501 EXPECT_FALSE(stream_->RetransmitStreamData(0, 18, true, PTO_RETRANSMISSION));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001502
1503 // Retransmit [0, 18) with fin, and all is consumed.
renjietang41a1b412020-02-27 15:05:14 -08001504 EXPECT_CALL(*session_, WritevData(stream_->id(), 10, 0, NO_FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001505 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
renjietang41a1b412020-02-27 15:05:14 -08001506 EXPECT_CALL(*session_, WritevData(stream_->id(), 5, 13, FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001507 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
renjietang4d992bf2020-03-03 13:01:55 -08001508 EXPECT_TRUE(stream_->RetransmitStreamData(0, 18, true, PTO_RETRANSMISSION));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001509
1510 // Retransmit [0, 8) with fin, and all is consumed.
renjietang41a1b412020-02-27 15:05:14 -08001511 EXPECT_CALL(*session_, WritevData(stream_->id(), 8, 0, NO_FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001512 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
renjietang41a1b412020-02-27 15:05:14 -08001513 EXPECT_CALL(*session_, WritevData(stream_->id(), 0, 18, FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001514 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
renjietang4d992bf2020-03-03 13:01:55 -08001515 EXPECT_TRUE(stream_->RetransmitStreamData(0, 8, true, PTO_RETRANSMISSION));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001516}
1517
1518TEST_P(QuicStreamTest, ResetStreamOnTtlExpiresRetransmitLostData) {
1519 Initialize();
1520
renjietang41a1b412020-02-27 15:05:14 -08001521 EXPECT_CALL(*session_, WritevData(stream_->id(), 200, 0, FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001522 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
vasilvvc48c8712019-03-11 13:38:16 -07001523 std::string body(200, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001524 stream_->WriteOrBufferData(body, true, nullptr);
1525
1526 // Set TTL to be 1 s.
1527 QuicTime::Delta ttl = QuicTime::Delta::FromSeconds(1);
1528 ASSERT_TRUE(stream_->MaybeSetTtl(ttl));
1529 // Verify data gets retransmitted because TTL does not expire.
renjietang41a1b412020-02-27 15:05:14 -08001530 EXPECT_CALL(*session_, WritevData(stream_->id(), 100, 0, NO_FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001531 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
renjietang4d992bf2020-03-03 13:01:55 -08001532 EXPECT_TRUE(stream_->RetransmitStreamData(0, 100, false, PTO_RETRANSMISSION));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001533 stream_->OnStreamFrameLost(100, 100, true);
1534 EXPECT_TRUE(stream_->HasPendingRetransmission());
1535
1536 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
1537 // Verify stream gets reset because TTL expires.
renjietang9946bc02020-07-16 15:14:27 -07001538 EXPECT_CALL(*session_, SendRstStream(_, QUIC_STREAM_TTL_EXPIRED, _, _))
1539 .Times(1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001540 stream_->OnCanWrite();
1541}
1542
1543TEST_P(QuicStreamTest, ResetStreamOnTtlExpiresEarlyRetransmitData) {
1544 Initialize();
1545
renjietang41a1b412020-02-27 15:05:14 -08001546 EXPECT_CALL(*session_, WritevData(stream_->id(), 200, 0, FIN, _, _))
renjietang7c239172020-02-21 13:50:39 -08001547 .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
vasilvvc48c8712019-03-11 13:38:16 -07001548 std::string body(200, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001549 stream_->WriteOrBufferData(body, true, nullptr);
1550
1551 // Set TTL to be 1 s.
1552 QuicTime::Delta ttl = QuicTime::Delta::FromSeconds(1);
1553 ASSERT_TRUE(stream_->MaybeSetTtl(ttl));
1554
1555 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
1556 // Verify stream gets reset because TTL expires.
renjietang9946bc02020-07-16 15:14:27 -07001557 EXPECT_CALL(*session_, SendRstStream(_, QUIC_STREAM_TTL_EXPIRED, _, _))
1558 .Times(1);
renjietang4d992bf2020-03-03 13:01:55 -08001559 stream_->RetransmitStreamData(0, 100, false, PTO_RETRANSMISSION);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001560}
1561
QUICHE teama6ef0a62019-03-07 20:34:33 -05001562// Test that OnStreamReset does one-way (read) closes if version 99, two way
1563// (read and write) if not version 99.
1564TEST_P(QuicStreamTest, OnStreamResetReadOrReadWrite) {
1565 Initialize();
bncc7d9e0c2019-04-16 10:22:15 -07001566 EXPECT_FALSE(stream_->write_side_closed());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001567 EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_));
1568
1569 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(),
1570 QUIC_STREAM_CANCELLED, 1234);
1571 stream_->OnStreamReset(rst_frame);
fkastenholz305e1732019-06-18 05:01:22 -07001572 if (VersionHasIetfQuicFrames(connection_->transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001573 // Version 99/IETF QUIC should close just the read side.
1574 EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_));
bncc7d9e0c2019-04-16 10:22:15 -07001575 EXPECT_FALSE(stream_->write_side_closed());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001576 } else {
1577 // Google QUIC should close both sides of the stream.
bncc7d9e0c2019-04-16 10:22:15 -07001578 EXPECT_TRUE(stream_->write_side_closed());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001579 EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_));
1580 }
1581}
1582
renjietang28c04b72019-07-01 15:08:09 -07001583TEST_P(QuicStreamTest, WindowUpdateForReadOnlyStream) {
renjietang28c04b72019-07-01 15:08:09 -07001584 Initialize();
1585
1586 QuicStreamId stream_id = QuicUtils::GetFirstUnidirectionalStreamId(
1587 connection_->transport_version(), Perspective::IS_CLIENT);
1588 TestStream stream(stream_id, session_.get(), READ_UNIDIRECTIONAL);
1589 QuicWindowUpdateFrame window_update_frame(kInvalidControlFrameId, stream_id,
1590 0);
1591 EXPECT_CALL(
1592 *connection_,
1593 CloseConnection(
1594 QUIC_WINDOW_UPDATE_RECEIVED_ON_READ_UNIDIRECTIONAL_STREAM,
1595 "WindowUpdateFrame received on READ_UNIDIRECTIONAL stream.", _));
1596 stream.OnWindowUpdateFrame(window_update_frame);
1597}
1598
renjietang15afba32019-10-23 14:32:35 -07001599TEST_P(QuicStreamTest, RstStreamFrameChangesCloseOffset) {
renjietang15afba32019-10-23 14:32:35 -07001600 Initialize();
1601
1602 QuicStreamFrame stream_frame(stream_->id(), true, 0, "abc");
renjietangb38e6352019-10-24 09:38:20 -07001603 EXPECT_CALL(*stream_, OnDataAvailable());
renjietang15afba32019-10-23 14:32:35 -07001604 stream_->OnStreamFrame(stream_frame);
1605 QuicRstStreamFrame rst(kInvalidControlFrameId, stream_->id(),
1606 QUIC_STREAM_CANCELLED, 0u);
1607
1608 EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_MULTIPLE_OFFSET, _, _));
1609 stream_->OnStreamReset(rst);
1610}
1611
QUICHE teama6ef0a62019-03-07 20:34:33 -05001612} // namespace
1613} // namespace test
1614} // namespace quic