QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // 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> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 9 | #include <utility> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | |
renjietang | 15afba3 | 2019-10-23 14:32:35 -0700 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/core/frames/quic_rst_stream_frame.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/quic_connection.h" |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/core/quic_constants.h" |
renjietang | 15afba3 | 2019-10-23 14:32:35 -0700 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/quic_write_blocked_list.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 19 | #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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 24 | #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" |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 33 | #include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h" |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 34 | #include "net/third_party/quiche/src/common/platform/api/quiche_optional.h" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 35 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 36 | |
| 37 | using testing::_; |
| 38 | using testing::AnyNumber; |
| 39 | using testing::AtLeast; |
| 40 | using testing::InSequence; |
| 41 | using testing::Invoke; |
| 42 | using testing::InvokeWithoutArgs; |
| 43 | using testing::Return; |
| 44 | using testing::StrictMock; |
| 45 | |
| 46 | namespace quic { |
| 47 | namespace test { |
| 48 | namespace { |
| 49 | |
| 50 | const char kData1[] = "FooAndBar"; |
| 51 | const char kData2[] = "EepAndBaz"; |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 52 | const QuicByteCount kDataLen = 9; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 53 | |
| 54 | class TestStream : public QuicStream { |
| 55 | public: |
| 56 | TestStream(QuicStreamId id, QuicSession* session, StreamType type) |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 57 | : QuicStream(id, session, /*is_static=*/false, type) { |
| 58 | sequencer()->set_level_triggered(true); |
| 59 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 60 | |
renjietang | 49aec63 | 2020-08-31 10:56:28 -0700 | [diff] [blame] | 61 | TestStream(PendingStream* pending, |
| 62 | QuicSession* session, |
| 63 | StreamType type, |
| 64 | bool is_static) |
| 65 | : QuicStream(pending, session, type, is_static) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 66 | |
wub | 713afae | 2020-04-27 07:48:31 -0700 | [diff] [blame] | 67 | MOCK_METHOD(void, OnDataAvailable, (), (override)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 68 | |
wub | 713afae | 2020-04-27 07:48:31 -0700 | [diff] [blame] | 69 | MOCK_METHOD(void, OnCanWriteNewData, (), (override)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 70 | |
| 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 78 | |
| 79 | private: |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 80 | std::string data_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 81 | }; |
| 82 | |
renjietang | 9bf2564 | 2019-10-10 10:21:15 -0700 | [diff] [blame] | 83 | class QuicStreamTest : public QuicTestWithParam<ParsedQuicVersion> { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 84 | public: |
renjietang | 9bf2564 | 2019-10-10 10:21:15 -0700 | [diff] [blame] | 85 | QuicStreamTest() |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 86 | : zero_(QuicTime::Delta::Zero()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 87 | 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)); |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 95 | session_ = std::make_unique<StrictMock<MockQuicSession>>(connection_); |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 96 | session_->Initialize(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 97 | |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 98 | QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow( |
| 99 | session_->config(), kMinimumFlowControlSendWindow); |
dschinazi | 18cdf13 | 2019-10-09 16:08:18 -0700 | [diff] [blame] | 100 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesUnidirectional( |
| 101 | session_->config(), kMinimumFlowControlSendWindow); |
| 102 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional( |
| 103 | session_->config(), kMinimumFlowControlSendWindow); |
| 104 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesOutgoingBidirectional( |
| 105 | session_->config(), kMinimumFlowControlSendWindow); |
renjietang | e6d9467 | 2020-01-07 10:30:10 -0800 | [diff] [blame] | 106 | QuicConfigPeer::SetReceivedMaxUnidirectionalStreams(session_->config(), 10); |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 107 | session_->OnConfigNegotiated(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 108 | |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 109 | stream_ = new StrictMock<TestStream>(kTestStreamId, session_.get(), |
| 110 | BIDIRECTIONAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 111 | EXPECT_NE(nullptr, stream_); |
| 112 | // session_ now owns stream_. |
| 113 | session_->ActivateStream(QuicWrapUnique(stream_)); |
| 114 | // Ignore resetting when session_ is terminated. |
renjietang | 9946bc0 | 2020-07-16 15:14:27 -0700 | [diff] [blame] | 115 | EXPECT_CALL(*session_, SendRstStream(kTestStreamId, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 116 | .Times(AnyNumber()); |
| 117 | write_blocked_list_ = |
| 118 | QuicSessionPeer::GetWriteBlockedStreams(session_.get()); |
| 119 | } |
| 120 | |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 121 | bool fin_sent() { return stream_->fin_sent(); } |
| 122 | bool rst_sent() { return stream_->rst_sent(); } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 123 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 124 | bool HasWriteBlockedStreams() { |
| 125 | return write_blocked_list_->HasWriteBlockedSpecialStream() || |
| 126 | write_blocked_list_->HasWriteBlockedDataStreams(); |
| 127 | } |
| 128 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 129 | QuicConsumedData CloseStreamOnWriteError( |
| 130 | QuicStreamId id, |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 131 | QuicByteCount /*write_length*/, |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 132 | QuicStreamOffset /*offset*/, |
| 133 | StreamSendingState /*state*/, |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 134 | TransmissionType /*type*/, |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 135 | quiche::QuicheOptional<EncryptionLevel> /*level*/) { |
renjietang | ff4240d | 2020-07-01 06:19:55 -0700 | [diff] [blame] | 136 | session_->ResetStream(id, QUIC_STREAM_CANCELLED); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 137 | return QuicConsumedData(1, false); |
| 138 | } |
| 139 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 140 | 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_; |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 157 | StrictMock<TestStream>* stream_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 158 | QuicWriteBlockedList* write_blocked_list_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 159 | QuicTime::Delta zero_; |
| 160 | ParsedQuicVersionVector supported_versions_; |
renjietang | 940a532 | 2019-08-02 16:17:51 -0700 | [diff] [blame] | 161 | QuicStreamId kTestStreamId = |
| 162 | GetNthClientInitiatedBidirectionalStreamId(GetParam().transport_version, |
| 163 | 1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 164 | }; |
| 165 | |
renjietang | 9bf2564 | 2019-10-10 10:21:15 -0700 | [diff] [blame] | 166 | INSTANTIATE_TEST_SUITE_P(QuicStreamTests, |
| 167 | QuicStreamTest, |
dschinazi | 142051a | 2019-09-18 18:17:29 -0700 | [diff] [blame] | 168 | ::testing::ValuesIn(AllSupportedVersions()), |
| 169 | ::testing::PrintToStringParamName()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 170 | |
renjietang | 3544899 | 2019-05-08 17:08:57 -0700 | [diff] [blame] | 171 | TEST_P(QuicStreamTest, PendingStreamStaticness) { |
| 172 | Initialize(); |
| 173 | |
| 174 | PendingStream pending(kTestStreamId + 2, session_.get()); |
renjietang | 49aec63 | 2020-08-31 10:56:28 -0700 | [diff] [blame] | 175 | TestStream stream(&pending, session_.get(), StreamType::BIDIRECTIONAL, false); |
renjietang | 3544899 | 2019-05-08 17:08:57 -0700 | [diff] [blame] | 176 | EXPECT_FALSE(stream.is_static()); |
| 177 | |
| 178 | PendingStream pending2(kTestStreamId + 3, session_.get()); |
renjietang | 49aec63 | 2020-08-31 10:56:28 -0700 | [diff] [blame] | 179 | TestStream stream2(&pending2, session_.get(), StreamType::BIDIRECTIONAL, |
| 180 | true); |
renjietang | 3544899 | 2019-05-08 17:08:57 -0700 | [diff] [blame] | 181 | EXPECT_TRUE(stream2.is_static()); |
| 182 | } |
| 183 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 184 | TEST_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, |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 191 | kInitialSessionFlowControlWindowForTest + 1, "."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 192 | |
| 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 | |
| 199 | TEST_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 | |
| 216 | TEST_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 | |
| 229 | TEST_P(QuicStreamTest, FromPendingStream) { |
| 230 | Initialize(); |
| 231 | |
| 232 | PendingStream pending(kTestStreamId + 2, session_.get()); |
| 233 | |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 234 | QuicStreamFrame frame(kTestStreamId + 2, false, 2, "."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 235 | pending.OnStreamFrame(frame); |
| 236 | pending.OnStreamFrame(frame); |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 237 | QuicStreamFrame frame2(kTestStreamId + 2, true, 3, "."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 238 | pending.OnStreamFrame(frame2); |
| 239 | |
renjietang | 49aec63 | 2020-08-31 10:56:28 -0700 | [diff] [blame] | 240 | TestStream stream(&pending, session_.get(), StreamType::READ_UNIDIRECTIONAL, |
| 241 | false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 242 | 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()); |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 246 | EXPECT_EQ(frame2.offset + 1, stream.highest_received_byte_offset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 247 | EXPECT_EQ(frame2.offset + 1, |
| 248 | session_->flow_controller()->highest_received_byte_offset()); |
| 249 | } |
| 250 | |
| 251 | TEST_P(QuicStreamTest, FromPendingStreamThenData) { |
| 252 | Initialize(); |
| 253 | |
| 254 | PendingStream pending(kTestStreamId + 2, session_.get()); |
| 255 | |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 256 | QuicStreamFrame frame(kTestStreamId + 2, false, 2, "."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 257 | pending.OnStreamFrame(frame); |
| 258 | |
renjietang | 49aec63 | 2020-08-31 10:56:28 -0700 | [diff] [blame] | 259 | auto stream = new TestStream(&pending, session_.get(), |
| 260 | StreamType::READ_UNIDIRECTIONAL, false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 261 | session_->ActivateStream(QuicWrapUnique(stream)); |
| 262 | |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 263 | QuicStreamFrame frame2(kTestStreamId + 2, true, 3, "."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 264 | 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()); |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 269 | EXPECT_EQ(frame2.offset + 1, stream->highest_received_byte_offset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 270 | EXPECT_EQ(frame2.offset + 1, |
| 271 | session_->flow_controller()->highest_received_byte_offset()); |
| 272 | } |
| 273 | |
| 274 | TEST_P(QuicStreamTest, WriteAllData) { |
| 275 | Initialize(); |
| 276 | |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 277 | QuicByteCount length = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 278 | 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 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 286 | EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 287 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 288 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 289 | EXPECT_FALSE(HasWriteBlockedStreams()); |
| 290 | } |
| 291 | |
| 292 | TEST_P(QuicStreamTest, NoBlockingIfNoDataOrFin) { |
| 293 | Initialize(); |
| 294 | |
| 295 | // Write no data and no fin. If we consume nothing we should not be write |
| 296 | // blocked. |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 297 | EXPECT_QUIC_BUG( |
| 298 | stream_->WriteOrBufferData(quiche::QuicheStringPiece(), false, nullptr), |
| 299 | ""); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 300 | EXPECT_FALSE(HasWriteBlockedStreams()); |
| 301 | } |
| 302 | |
| 303 | TEST_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. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 308 | EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 309 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 310 | return session_->ConsumeData(stream_->id(), 1u, 0u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 311 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 312 | })); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 313 | stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), false, |
| 314 | nullptr); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 315 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 316 | ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams()); |
| 317 | EXPECT_EQ(1u, stream_->BufferedDataBytes()); |
| 318 | } |
| 319 | |
| 320 | TEST_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) |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 327 | EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 328 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 329 | return session_->ConsumeData(stream_->id(), 2u, 0u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 330 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 331 | })); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 332 | stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), true, |
| 333 | nullptr); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 334 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 335 | ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams()); |
| 336 | } |
| 337 | |
| 338 | TEST_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. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 343 | EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 344 | .WillOnce(Return(QuicConsumedData(0, false))); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 345 | stream_->WriteOrBufferData(quiche::QuicheStringPiece(), true, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 346 | ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams()); |
| 347 | } |
| 348 | |
| 349 | TEST_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. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 355 | EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 356 | .WillOnce(Invoke(this, &QuicStreamTest::CloseStreamOnWriteError)); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 357 | stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), false, |
| 358 | nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 359 | ASSERT_EQ(0u, write_blocked_list_->NumBlockedStreams()); |
| 360 | } |
| 361 | |
| 362 | TEST_P(QuicStreamTest, WriteOrBufferData) { |
| 363 | Initialize(); |
| 364 | |
| 365 | EXPECT_FALSE(HasWriteBlockedStreams()); |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 366 | QuicByteCount length = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 367 | 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 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 375 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 376 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 377 | return session_->ConsumeData(stream_->id(), kDataLen - 1, 0u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 378 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 379 | })); |
| 380 | stream_->WriteOrBufferData(kData1, false, nullptr); |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 381 | |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 382 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 383 | 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; |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 391 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 392 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 393 | return session_->ConsumeData(stream_->id(), kDataLen - 1, kDataLen - 1, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 394 | NO_FIN, NOT_RETRANSMISSION, |
| 395 | QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 396 | })); |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 397 | EXPECT_CALL(*stream_, OnCanWriteNewData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 398 | stream_->OnCanWrite(); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 399 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 400 | |
| 401 | // And finally the end of the bytes_consumed. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 402 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 403 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 404 | return session_->ConsumeData(stream_->id(), 2u, 2 * kDataLen - 2, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 405 | NO_FIN, NOT_RETRANSMISSION, |
| 406 | QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 407 | })); |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 408 | EXPECT_CALL(*stream_, OnCanWriteNewData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 409 | stream_->OnCanWrite(); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 410 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | TEST_P(QuicStreamTest, WriteOrBufferDataReachStreamLimit) { |
| 414 | Initialize(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 415 | std::string data("aaaaa"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 416 | QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - data.length(), |
| 417 | stream_); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 418 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 419 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 420 | stream_->WriteOrBufferData(data, false, nullptr); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 421 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 422 | 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 | |
| 427 | TEST_P(QuicStreamTest, ConnectionCloseAfterStreamClose) { |
| 428 | Initialize(); |
| 429 | |
| 430 | QuicStreamPeer::CloseReadSide(stream_); |
| 431 | stream_->CloseWriteSide(); |
bnc | f6f82b1 | 2019-10-30 07:01:01 -0700 | [diff] [blame] | 432 | EXPECT_THAT(stream_->stream_error(), IsQuicStreamNoError()); |
| 433 | EXPECT_THAT(stream_->connection_error(), IsQuicNoError()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 434 | stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR, |
| 435 | ConnectionCloseSource::FROM_SELF); |
bnc | f6f82b1 | 2019-10-30 07:01:01 -0700 | [diff] [blame] | 436 | EXPECT_THAT(stream_->stream_error(), IsQuicStreamNoError()); |
| 437 | EXPECT_THAT(stream_->connection_error(), IsQuicNoError()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | TEST_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. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 450 | EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 451 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 452 | return session_->ConsumeData(stream_->id(), 1u, 0u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 453 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 454 | })); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 455 | stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 1), false, |
| 456 | nullptr); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 457 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 458 | EXPECT_FALSE(fin_sent()); |
| 459 | EXPECT_FALSE(rst_sent()); |
| 460 | |
| 461 | // Now close the stream, and expect that we send a RST. |
renjietang | 9946bc0 | 2020-07-16 15:14:27 -0700 | [diff] [blame] | 462 | EXPECT_CALL(*session_, SendRstStream(_, _, _, _)); |
renjietang | f00e1e1 | 2020-08-26 10:51:15 -0700 | [diff] [blame] | 463 | QuicStreamPeer::CloseReadSide(stream_); |
fayang | b8f8344 | 2020-06-01 12:09:17 -0700 | [diff] [blame] | 464 | stream_->CloseWriteSide(); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 465 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 466 | EXPECT_FALSE(fin_sent()); |
| 467 | EXPECT_TRUE(rst_sent()); |
| 468 | } |
| 469 | |
| 470 | TEST_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. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 480 | EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 481 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 482 | return session_->ConsumeData(stream_->id(), 1u, 0u, FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 483 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 484 | })); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 485 | stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 1), true, |
| 486 | nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 487 | EXPECT_TRUE(fin_sent()); |
| 488 | EXPECT_FALSE(rst_sent()); |
| 489 | |
| 490 | // Now close the stream, and expect that we do not send a RST. |
renjietang | f00e1e1 | 2020-08-26 10:51:15 -0700 | [diff] [blame] | 491 | QuicStreamPeer::CloseReadSide(stream_); |
fayang | b8f8344 | 2020-06-01 12:09:17 -0700 | [diff] [blame] | 492 | stream_->CloseWriteSide(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 493 | EXPECT_TRUE(fin_sent()); |
| 494 | EXPECT_FALSE(rst_sent()); |
| 495 | } |
| 496 | |
| 497 | TEST_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; |
renjietang | 9946bc0 | 2020-07-16 15:14:27 -0700 | [diff] [blame] | 509 | EXPECT_CALL(*session_, SendRstStream(_, _, _, _)).Times(expected_resets); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 510 | 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). |
renjietang | f00e1e1 | 2020-08-26 10:51:15 -0700 | [diff] [blame] | 516 | QuicStreamPeer::CloseReadSide(stream_); |
fayang | b8f8344 | 2020-06-01 12:09:17 -0700 | [diff] [blame] | 517 | stream_->CloseWriteSide(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 518 | EXPECT_FALSE(fin_sent()); |
| 519 | EXPECT_TRUE(rst_sent()); |
| 520 | } |
| 521 | |
| 522 | TEST_P(QuicStreamTest, StreamFlowControlMultipleWindowUpdates) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 523 | 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. |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 529 | EXPECT_EQ(kMinimumFlowControlSendWindow, |
| 530 | QuicStreamPeer::SendWindowOffset(stream_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 531 | |
| 532 | // Check a single WINDOW_UPDATE results in correct offset. |
| 533 | QuicWindowUpdateFrame window_update_1(kInvalidControlFrameId, stream_->id(), |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 534 | kMinimumFlowControlSendWindow + 5); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 535 | stream_->OnWindowUpdateFrame(window_update_1); |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 536 | EXPECT_EQ(window_update_1.max_data, |
| 537 | QuicStreamPeer::SendWindowOffset(stream_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 538 | |
| 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(), |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 544 | kMinimumFlowControlSendWindow + 10); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 545 | 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); |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 550 | EXPECT_EQ(window_update_3.max_data, |
| 551 | QuicStreamPeer::SendWindowOffset(stream_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | TEST_P(QuicStreamTest, FrameStats) { |
| 555 | Initialize(); |
| 556 | |
| 557 | EXPECT_EQ(0, stream_->num_frames_received()); |
| 558 | EXPECT_EQ(0, stream_->num_duplicate_frames_received()); |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 559 | QuicStreamFrame frame(stream_->id(), false, 0, "."); |
| 560 | EXPECT_CALL(*stream_, OnDataAvailable()).Times(2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 561 | 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()); |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 567 | QuicStreamFrame frame2(stream_->id(), false, 1, "abc"); |
| 568 | stream_->OnStreamFrame(frame2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 569 | } |
| 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. |
| 574 | TEST_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, |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 580 | kInitialSessionFlowControlWindowForTest + 1, "."); |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 581 | EXPECT_GT(frame.offset, QuicStreamPeer::ReceiveWindowOffset(stream_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 582 | |
| 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. |
| 591 | TEST_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)) |
bnc | 5b3c3be | 2019-06-25 10:37:09 -0700 | [diff] [blame] | 602 | .WillRepeatedly(Invoke(&ClearControlFrame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 603 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 604 | std::string data(1000, 'x'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 605 | 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 | } |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 611 | EXPECT_LT(kInitialStreamFlowControlWindowForTest, |
| 612 | QuicStreamPeer::ReceiveWindowOffset(stream_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | TEST_P(QuicStreamTest, FinalByteOffsetFromFin) { |
| 616 | Initialize(); |
| 617 | |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 618 | EXPECT_FALSE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 619 | |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 620 | QuicStreamFrame stream_frame_no_fin(stream_->id(), false, 1234, "."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 621 | stream_->OnStreamFrame(stream_frame_no_fin); |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 622 | EXPECT_FALSE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 623 | |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 624 | QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, "."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 625 | stream_->OnStreamFrame(stream_frame_with_fin); |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 626 | EXPECT_TRUE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | TEST_P(QuicStreamTest, FinalByteOffsetFromRst) { |
| 630 | Initialize(); |
| 631 | |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 632 | EXPECT_FALSE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 633 | QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(), |
| 634 | QUIC_STREAM_CANCELLED, 1234); |
| 635 | stream_->OnStreamReset(rst_frame); |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 636 | EXPECT_TRUE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | TEST_P(QuicStreamTest, InvalidFinalByteOffsetFromRst) { |
| 640 | Initialize(); |
| 641 | |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 642 | EXPECT_FALSE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 643 | 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); |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 649 | EXPECT_TRUE(stream_->HasReceivedFinalOffset()); |
renjietang | f00e1e1 | 2020-08-26 10:51:15 -0700 | [diff] [blame] | 650 | QuicStreamPeer::CloseReadSide(stream_); |
fayang | b8f8344 | 2020-06-01 12:09:17 -0700 | [diff] [blame] | 651 | stream_->CloseWriteSide(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | TEST_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 | |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 663 | EXPECT_FALSE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 664 | const QuicStreamOffset kByteOffsetExceedingFlowControlWindow = |
| 665 | kInitialSessionFlowControlWindowForTest + 1; |
| 666 | const QuicStreamOffset current_stream_flow_control_offset = |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 667 | QuicStreamPeer::ReceiveWindowOffset(stream_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 668 | 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, |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 676 | quiche::QuicheStringPiece()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 677 | 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); |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 681 | EXPECT_TRUE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 682 | |
| 683 | // The flow control receive offset values should not have changed. |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 684 | EXPECT_EQ(current_stream_flow_control_offset, |
| 685 | QuicStreamPeer::ReceiveWindowOffset(stream_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 686 | EXPECT_EQ( |
| 687 | current_connection_flow_control_offset, |
| 688 | QuicFlowControllerPeer::ReceiveWindowOffset(session_->flow_controller())); |
| 689 | } |
| 690 | |
| 691 | TEST_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 | |
| 699 | TEST_P(QuicStreamTest, OnStreamFrameUpperLimit) { |
| 700 | Initialize(); |
| 701 | |
| 702 | // Modify receive window offset and sequencer buffer total_bytes_read_ to |
| 703 | // avoid flow control violation. |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 704 | QuicStreamPeer::SetReceiveWindowOffset(stream_, kMaxStreamLength + 5u); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 705 | 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); |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 712 | QuicStreamFrame stream_frame(stream_->id(), false, kMaxStreamLength - 1, "."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 713 | stream_->OnStreamFrame(stream_frame); |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 714 | QuicStreamFrame stream_frame2(stream_->id(), true, kMaxStreamLength, ""); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 715 | stream_->OnStreamFrame(stream_frame2); |
| 716 | } |
| 717 | |
| 718 | TEST_P(QuicStreamTest, StreamTooLong) { |
| 719 | Initialize(); |
| 720 | EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _)) |
| 721 | .Times(1); |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 722 | QuicStreamFrame stream_frame(stream_->id(), false, kMaxStreamLength, "."); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 723 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 727 | } |
| 728 | |
renjietang | 9bf2564 | 2019-10-10 10:21:15 -0700 | [diff] [blame] | 729 | TEST_P(QuicStreamTest, SetDrainingIncomingOutgoing) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 730 | // Don't have incoming data consumed. |
| 731 | Initialize(); |
| 732 | |
| 733 | // Incoming data with FIN. |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 734 | QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, "."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 735 | stream_->OnStreamFrame(stream_frame_with_fin); |
| 736 | // The FIN has been received but not consumed. |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 737 | EXPECT_TRUE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 738 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 739 | EXPECT_FALSE(stream_->reading_stopped()); |
| 740 | |
fayang | 01591ae | 2020-04-23 14:14:56 -0700 | [diff] [blame] | 741 | EXPECT_EQ(1u, QuicSessionPeer::GetNumOpenDynamicStreams(session_.get())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 742 | |
| 743 | // Outgoing data with FIN. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 744 | EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 745 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 746 | return session_->ConsumeData(stream_->id(), 2u, 0u, FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 747 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 748 | })); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 749 | stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), true, |
| 750 | nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 751 | EXPECT_TRUE(stream_->write_side_closed()); |
| 752 | |
fayang | 116fa4c | 2020-06-02 08:10:49 -0700 | [diff] [blame] | 753 | EXPECT_EQ(1u, QuicSessionPeer::GetNumDrainingStreams(session_.get())); |
fayang | 01591ae | 2020-04-23 14:14:56 -0700 | [diff] [blame] | 754 | EXPECT_EQ(0u, QuicSessionPeer::GetNumOpenDynamicStreams(session_.get())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 755 | } |
| 756 | |
renjietang | 9bf2564 | 2019-10-10 10:21:15 -0700 | [diff] [blame] | 757 | TEST_P(QuicStreamTest, SetDrainingOutgoingIncoming) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 758 | // Don't have incoming data consumed. |
| 759 | Initialize(); |
| 760 | |
| 761 | // Outgoing data with FIN. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 762 | EXPECT_CALL(*session_, WritevData(kTestStreamId, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 763 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 764 | return session_->ConsumeData(stream_->id(), 2u, 0u, FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 765 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 766 | })); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 767 | stream_->WriteOrBufferData(quiche::QuicheStringPiece(kData1, 2), true, |
| 768 | nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 769 | EXPECT_TRUE(stream_->write_side_closed()); |
| 770 | |
fayang | 01591ae | 2020-04-23 14:14:56 -0700 | [diff] [blame] | 771 | EXPECT_EQ(1u, QuicSessionPeer::GetNumOpenDynamicStreams(session_.get())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 772 | |
| 773 | // Incoming data with FIN. |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 774 | QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, "."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 775 | stream_->OnStreamFrame(stream_frame_with_fin); |
| 776 | // The FIN has been received but not consumed. |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 777 | EXPECT_TRUE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 778 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 779 | EXPECT_FALSE(stream_->reading_stopped()); |
| 780 | |
fayang | 116fa4c | 2020-06-02 08:10:49 -0700 | [diff] [blame] | 781 | EXPECT_EQ(1u, QuicSessionPeer::GetNumDrainingStreams(session_.get())); |
fayang | 01591ae | 2020-04-23 14:14:56 -0700 | [diff] [blame] | 782 | EXPECT_EQ(0u, QuicSessionPeer::GetNumOpenDynamicStreams(session_.get())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | TEST_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); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 791 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 792 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 793 | |
| 794 | // Receive data for the request. |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 795 | EXPECT_CALL(*stream_, OnDataAvailable()).Times(1); |
| 796 | QuicStreamFrame frame1(stream_->id(), false, 0, "Start"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 797 | 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. |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 805 | QuicStreamFrame frame2(stream_->id(), true, 0, "End"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 806 | stream_->OnStreamFrame(frame2); |
| 807 | EXPECT_TRUE(stream_->fin_received()); |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 808 | EXPECT_TRUE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | TEST_P(QuicStreamTest, StreamWaitsForAcks) { |
| 812 | Initialize(); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 813 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 814 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 815 | // Stream is not waiting for acks initially. |
| 816 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 817 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 818 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 819 | |
| 820 | // Send kData1. |
| 821 | stream_->WriteOrBufferData(kData1, false, nullptr); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 822 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 823 | 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 team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 827 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 828 | &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()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 832 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 833 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 834 | |
| 835 | // Send kData2. |
| 836 | stream_->WriteOrBufferData(kData2, false, nullptr); |
| 837 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 838 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 839 | 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 team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 850 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 851 | &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()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 855 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 856 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 857 | |
| 858 | // FIN is acked. |
| 859 | EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 0, true, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 860 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 861 | &newly_acked_length)); |
| 862 | EXPECT_EQ(0u, newly_acked_length); |
| 863 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 864 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 865 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 866 | } |
| 867 | |
| 868 | TEST_P(QuicStreamTest, StreamDataGetAckedOutOfOrder) { |
| 869 | Initialize(); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 870 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 871 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 872 | // 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()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 879 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 880 | QuicByteCount newly_acked_length = 0; |
| 881 | EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 882 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 883 | &newly_acked_length)); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 884 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 885 | 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 team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 888 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 889 | &newly_acked_length)); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 890 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 891 | 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 team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 894 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 895 | &newly_acked_length)); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 896 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 897 | 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()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 901 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 902 | EXPECT_TRUE(stream_->OnStreamFrameAcked(27, 0, true, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 903 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 904 | &newly_acked_length)); |
| 905 | EXPECT_EQ(0u, newly_acked_length); |
| 906 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 907 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | TEST_P(QuicStreamTest, CancelStream) { |
| 911 | Initialize(); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 912 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 913 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 914 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 915 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 916 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 917 | |
| 918 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 919 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 920 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 921 | 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()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 927 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 928 | EXPECT_CALL(*connection_, |
| 929 | OnStreamReset(stream_->id(), QUIC_STREAM_CANCELLED)); |
renjietang | eab9285 | 2019-10-25 12:16:14 -0700 | [diff] [blame] | 930 | EXPECT_CALL(*connection_, SendControlFrame(_)) |
| 931 | .Times(AtLeast(1)) |
| 932 | .WillRepeatedly(Invoke(&ClearControlFrame)); |
renjietang | 9946bc0 | 2020-07-16 15:14:27 -0700 | [diff] [blame] | 933 | EXPECT_CALL(*session_, |
| 934 | SendRstStream(stream_->id(), QUIC_STREAM_CANCELLED, 9, _)) |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 935 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 61cc245 | 2019-11-26 10:57:10 -0800 | [diff] [blame] | 936 | session_->ReallySendRstStream(stream_->id(), QUIC_STREAM_CANCELLED, |
renjietang | 9946bc0 | 2020-07-16 15:14:27 -0700 | [diff] [blame] | 937 | stream_->stream_bytes_written(), false); |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 938 | })); |
| 939 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 940 | 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()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 944 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | TEST_P(QuicStreamTest, RstFrameReceivedStreamNotFinishSending) { |
renjietang | 9bf2564 | 2019-10-10 10:21:15 -0700 | [diff] [blame] | 948 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 954 | Initialize(); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 955 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 956 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 957 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 958 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 959 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 960 | |
| 961 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 962 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 963 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 964 | 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_, |
renjietang | 9946bc0 | 2020-07-16 15:14:27 -0700 | [diff] [blame] | 970 | SendRstStream(stream_->id(), QUIC_RST_ACKNOWLEDGEMENT, 9, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 971 | 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()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 976 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | TEST_P(QuicStreamTest, RstFrameReceivedStreamFinishSending) { |
| 980 | Initialize(); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 981 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 982 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 983 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 984 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 985 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 986 | |
| 987 | stream_->WriteOrBufferData(kData1, true, nullptr); |
| 988 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 989 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 990 | |
| 991 | // RST_STREAM received. |
renjietang | 9946bc0 | 2020-07-16 15:14:27 -0700 | [diff] [blame] | 992 | EXPECT_CALL(*session_, SendRstStream(_, _, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 993 | 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()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 998 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 999 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1000 | } |
| 1001 | |
| 1002 | TEST_P(QuicStreamTest, ConnectionClosed) { |
| 1003 | Initialize(); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1004 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1005 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1006 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 1007 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1008 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1009 | |
| 1010 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 1011 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 1012 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1013 | EXPECT_CALL(*session_, |
renjietang | 9946bc0 | 2020-07-16 15:14:27 -0700 | [diff] [blame] | 1014 | SendRstStream(stream_->id(), QUIC_RST_ACKNOWLEDGEMENT, 9, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1015 | 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()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 1020 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | TEST_P(QuicStreamTest, CanWriteNewDataAfterData) { |
wub | 4985598 | 2019-05-01 14:16:26 -0700 | [diff] [blame] | 1024 | SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1025 | Initialize(); |
| 1026 | EXPECT_TRUE(stream_->CanWriteNewDataAfterData(99)); |
| 1027 | EXPECT_FALSE(stream_->CanWriteNewDataAfterData(100)); |
| 1028 | } |
| 1029 | |
| 1030 | TEST_P(QuicStreamTest, WriteBufferedData) { |
| 1031 | // Set buffered data low water mark to be 100. |
wub | 4985598 | 2019-05-01 14:16:26 -0700 | [diff] [blame] | 1032 | SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1033 | |
| 1034 | Initialize(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1035 | std::string data(1024, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1036 | EXPECT_TRUE(stream_->CanWriteNewData()); |
| 1037 | |
| 1038 | // Testing WriteOrBufferData. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1039 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1040 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1041 | return session_->ConsumeData(stream_->id(), 100u, 0u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 1042 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1043 | })); |
| 1044 | stream_->WriteOrBufferData(data, false, nullptr); |
| 1045 | stream_->WriteOrBufferData(data, false, nullptr); |
| 1046 | stream_->WriteOrBufferData(data, false, nullptr); |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 1047 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 1048 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1049 | // Verify all data is saved. |
| 1050 | EXPECT_EQ(3 * data.length() - 100, stream_->BufferedDataBytes()); |
| 1051 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1052 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1053 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1054 | return session_->ConsumeData(stream_->id(), 100, 100u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 1055 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1056 | })); |
| 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. |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 1064 | QuicByteCount data_to_write = |
| 1065 | 3 * data.length() - 200 - |
| 1066 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1; |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1067 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1068 | .WillOnce(InvokeWithoutArgs([this, data_to_write]() { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1069 | return session_->ConsumeData(stream_->id(), data_to_write, 200u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 1070 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1071 | })); |
| 1072 | // Buffered data size < threshold, ask upper layer for more data. |
| 1073 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1); |
| 1074 | stream_->OnCanWrite(); |
dschinazi | 696ee60 | 2019-04-25 16:05:35 -0700 | [diff] [blame] | 1075 | EXPECT_EQ(static_cast<uint64_t>( |
| 1076 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1077 | stream_->BufferedDataBytes()); |
| 1078 | EXPECT_TRUE(stream_->CanWriteNewData()); |
| 1079 | |
| 1080 | // Flush all buffered data. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1081 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1082 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1083 | 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. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1090 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1091 | .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 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1105 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1106 | 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; |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1117 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1118 | .WillOnce(InvokeWithoutArgs([this, data_to_write]() { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1119 | return session_->ConsumeData(stream_->id(), data_to_write, 0u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 1120 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1121 | })); |
| 1122 | |
| 1123 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1); |
| 1124 | stream_->OnCanWrite(); |
dschinazi | 696ee60 | 2019-04-25 16:05:35 -0700 | [diff] [blame] | 1125 | EXPECT_EQ(static_cast<uint64_t>( |
| 1126 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1127 | stream_->BufferedDataBytes()); |
| 1128 | EXPECT_TRUE(stream_->CanWriteNewData()); |
| 1129 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1130 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1131 | // 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 | |
| 1143 | TEST_P(QuicStreamTest, WritevDataReachStreamLimit) { |
| 1144 | Initialize(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1145 | std::string data("aaaaa"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1146 | QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - data.length(), |
| 1147 | stream_); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1148 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1149 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1150 | 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 | |
| 1165 | TEST_P(QuicStreamTest, WriteMemSlices) { |
| 1166 | // Set buffered data low water mark to be 100. |
wub | 4985598 | 2019-05-01 14:16:26 -0700 | [diff] [blame] | 1167 | SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1168 | |
| 1169 | Initialize(); |
| 1170 | char data[1024]; |
nharper | 6b7aa8b | 2020-04-30 17:23:35 -0700 | [diff] [blame] | 1171 | std::vector<std::pair<char*, size_t>> buffers; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 1172 | buffers.push_back(std::make_pair(data, QUICHE_ARRAYSIZE(data))); |
| 1173 | buffers.push_back(std::make_pair(data, QUICHE_ARRAYSIZE(data))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1174 | QuicTestMemSliceVector vector1(buffers); |
| 1175 | QuicTestMemSliceVector vector2(buffers); |
| 1176 | QuicMemSliceSpan span1 = vector1.span(); |
| 1177 | QuicMemSliceSpan span2 = vector2.span(); |
| 1178 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1179 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1180 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1181 | return session_->ConsumeData(stream_->id(), 100u, 0u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 1182 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1183 | })); |
| 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); |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 1188 | EXPECT_EQ(2 * QUICHE_ARRAYSIZE(data) - 100, stream_->BufferedDataBytes()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1189 | EXPECT_FALSE(stream_->fin_buffered()); |
| 1190 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1191 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1192 | // 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); |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 1196 | EXPECT_EQ(2 * QUICHE_ARRAYSIZE(data) - 100, stream_->BufferedDataBytes()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1197 | EXPECT_FALSE(stream_->fin_buffered()); |
| 1198 | |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 1199 | QuicByteCount data_to_write = |
| 1200 | 2 * QUICHE_ARRAYSIZE(data) - 100 - |
| 1201 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1; |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1202 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1203 | .WillOnce(InvokeWithoutArgs([this, data_to_write]() { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1204 | return session_->ConsumeData(stream_->id(), data_to_write, 100u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 1205 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1206 | })); |
| 1207 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1); |
| 1208 | stream_->OnCanWrite(); |
dschinazi | 696ee60 | 2019-04-25 16:05:35 -0700 | [diff] [blame] | 1209 | EXPECT_EQ(static_cast<uint64_t>( |
| 1210 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1211 | stream_->BufferedDataBytes()); |
| 1212 | // Try to write slices2 again. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1213 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1214 | consumed = stream_->WriteMemSlices(span2, true); |
| 1215 | EXPECT_EQ(2048u, consumed.bytes_consumed); |
| 1216 | EXPECT_TRUE(consumed.fin_consumed); |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 1217 | EXPECT_EQ(2 * QUICHE_ARRAYSIZE(data) + |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1218 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1, |
| 1219 | stream_->BufferedDataBytes()); |
| 1220 | EXPECT_TRUE(stream_->fin_buffered()); |
| 1221 | |
| 1222 | // Flush all buffered data. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1223 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1224 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1225 | stream_->OnCanWrite(); |
| 1226 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(0); |
| 1227 | EXPECT_FALSE(stream_->HasBufferedData()); |
| 1228 | EXPECT_TRUE(stream_->write_side_closed()); |
| 1229 | } |
| 1230 | |
| 1231 | TEST_P(QuicStreamTest, WriteMemSlicesReachStreamLimit) { |
| 1232 | Initialize(); |
| 1233 | QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - 5u, stream_); |
| 1234 | char data[5]; |
nharper | 6b7aa8b | 2020-04-30 17:23:35 -0700 | [diff] [blame] | 1235 | std::vector<std::pair<char*, size_t>> buffers; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 1236 | buffers.push_back(std::make_pair(data, QUICHE_ARRAYSIZE(data))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1237 | QuicTestMemSliceVector vector1(buffers); |
| 1238 | QuicMemSliceSpan span1 = vector1.span(); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1239 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1240 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1241 | return session_->ConsumeData(stream_->id(), 5u, 0u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 1242 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1243 | })); |
| 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 | |
nharper | 6b7aa8b | 2020-04-30 17:23:35 -0700 | [diff] [blame] | 1248 | std::vector<std::pair<char*, size_t>> buffers2; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1249 | 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 | |
| 1257 | TEST_P(QuicStreamTest, StreamDataGetAckedMultipleTimes) { |
| 1258 | Initialize(); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1259 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1260 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 1261 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 1262 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 1263 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1264 | // 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()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 1270 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1271 | // 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 team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1275 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1276 | &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 team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1281 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1282 | &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 team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1287 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1288 | &newly_acked_length)); |
| 1289 | EXPECT_EQ(4u, newly_acked_length); |
| 1290 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1291 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 1292 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1293 | |
| 1294 | // Ack [0, 27). Verify [26, 27) 1 byte is acked. |
| 1295 | EXPECT_TRUE(stream_->OnStreamFrameAcked(26, 1, false, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1296 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1297 | &newly_acked_length)); |
| 1298 | EXPECT_EQ(1u, newly_acked_length); |
| 1299 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1300 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 1301 | EXPECT_TRUE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1302 | |
| 1303 | // Ack Fin. |
| 1304 | EXPECT_TRUE(stream_->OnStreamFrameAcked(27, 0, true, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1305 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1306 | &newly_acked_length)); |
| 1307 | EXPECT_EQ(0u, newly_acked_length); |
| 1308 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1309 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 1310 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1311 | |
| 1312 | // Ack [10, 27) and fin. No new data is acked. |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1313 | EXPECT_FALSE( |
| 1314 | stream_->OnStreamFrameAcked(10, 17, true, QuicTime::Delta::Zero(), |
| 1315 | QuicTime::Zero(), &newly_acked_length)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1316 | EXPECT_EQ(0u, newly_acked_length); |
| 1317 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1318 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 1319 | EXPECT_FALSE(session_->HasUnackedStreamData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | TEST_P(QuicStreamTest, OnStreamFrameLost) { |
| 1323 | Initialize(); |
| 1324 | |
| 1325 | // Send [0, 9). |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1326 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1327 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1328 | 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. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1333 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1334 | .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()); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1344 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1345 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 1346 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1347 | stream_->OnCanWrite(); |
| 1348 | EXPECT_FALSE(stream_->HasPendingRetransmission()); |
| 1349 | EXPECT_TRUE(stream_->HasBufferedData()); |
| 1350 | |
| 1351 | // This OnCanWrite causes [9, 27) to be sent. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1352 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1353 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1354 | stream_->OnCanWrite(); |
| 1355 | EXPECT_FALSE(stream_->HasBufferedData()); |
| 1356 | |
| 1357 | // Send a fin only frame. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1358 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1359 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1360 | 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 team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1370 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1371 | &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. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1377 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1378 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1379 | return session_->ConsumeData(stream_->id(), 9u, 18u, FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 1380 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1381 | })); |
| 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 | |
| 1391 | TEST_P(QuicStreamTest, CannotBundleLostFin) { |
| 1392 | Initialize(); |
| 1393 | |
| 1394 | // Send [0, 18) and fin. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1395 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1396 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1397 | 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; |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1407 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1408 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1409 | return session_->ConsumeData(stream_->id(), 9u, 0u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 1410 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1411 | })); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1412 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1413 | .WillOnce(Return(QuicConsumedData(0, true))); |
| 1414 | stream_->OnCanWrite(); |
| 1415 | } |
| 1416 | |
| 1417 | TEST_P(QuicStreamTest, MarkConnectionLevelWriteBlockedOnWindowUpdateFrame) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1418 | Initialize(); |
| 1419 | |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 1420 | // 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); |
renjietang | 9bf2564 | 2019-10-10 10:21:15 -0700 | [diff] [blame] | 1424 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional( |
| 1425 | session_->config(), 100); |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 1426 | auto stream = new TestStream(GetNthClientInitiatedBidirectionalStreamId( |
| 1427 | GetParam().transport_version, 2), |
| 1428 | session_.get(), BIDIRECTIONAL); |
| 1429 | session_->ActivateStream(QuicWrapUnique(stream)); |
| 1430 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1431 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1432 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1433 | EXPECT_CALL(*connection_, SendControlFrame(_)) |
bnc | 5b3c3be | 2019-06-25 10:37:09 -0700 | [diff] [blame] | 1434 | .WillOnce(Invoke(&ClearControlFrame)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1435 | std::string data(1024, '.'); |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 1436 | stream->WriteOrBufferData(data, false, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1437 | EXPECT_FALSE(HasWriteBlockedStreams()); |
| 1438 | |
| 1439 | QuicWindowUpdateFrame window_update(kInvalidControlFrameId, stream_->id(), |
| 1440 | 1234); |
| 1441 | |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 1442 | stream->OnWindowUpdateFrame(window_update); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1443 | // Verify stream is marked connection level write blocked. |
| 1444 | EXPECT_TRUE(HasWriteBlockedStreams()); |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 1445 | EXPECT_TRUE(stream->HasBufferedData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | // Regression test for b/73282665. |
| 1449 | TEST_P(QuicStreamTest, |
| 1450 | MarkConnectionLevelWriteBlockedOnWindowUpdateFrameWithNoBufferedData) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1451 | Initialize(); |
| 1452 | |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 1453 | // 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); |
renjietang | 9bf2564 | 2019-10-10 10:21:15 -0700 | [diff] [blame] | 1457 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional( |
| 1458 | session_->config(), 100); |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 1459 | 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, '.'); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1465 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1466 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1467 | EXPECT_CALL(*connection_, SendControlFrame(_)) |
bnc | 5b3c3be | 2019-06-25 10:37:09 -0700 | [diff] [blame] | 1468 | .WillOnce(Invoke(&ClearControlFrame)); |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 1469 | stream->WriteOrBufferData(data, false, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1470 | EXPECT_FALSE(HasWriteBlockedStreams()); |
| 1471 | |
| 1472 | QuicWindowUpdateFrame window_update(kInvalidControlFrameId, stream_->id(), |
| 1473 | 120); |
renjietang | 39979d3 | 2019-09-13 10:02:13 -0700 | [diff] [blame] | 1474 | stream->OnWindowUpdateFrame(window_update); |
| 1475 | EXPECT_FALSE(stream->HasBufferedData()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1476 | // Verify stream is marked as blocked although there is no buffered data. |
| 1477 | EXPECT_TRUE(HasWriteBlockedStreams()); |
| 1478 | } |
| 1479 | |
| 1480 | TEST_P(QuicStreamTest, RetransmitStreamData) { |
| 1481 | Initialize(); |
| 1482 | InSequence s; |
| 1483 | |
| 1484 | // Send [0, 18) with fin. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1485 | EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1486 | .Times(2) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1487 | .WillRepeatedly(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1488 | 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 team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1493 | QuicTime::Zero(), &newly_acked_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1494 | EXPECT_EQ(3u, newly_acked_length); |
| 1495 | // Retransmit [0, 18) with fin, and only [0, 8) is consumed. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1496 | EXPECT_CALL(*session_, WritevData(stream_->id(), 10, 0, NO_FIN, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1497 | .WillOnce(InvokeWithoutArgs([this]() { |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1498 | return session_->ConsumeData(stream_->id(), 8, 0u, NO_FIN, |
vasilvv | 9efbb91 | 2020-06-05 08:59:26 -0700 | [diff] [blame] | 1499 | NOT_RETRANSMISSION, QUICHE_NULLOPT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1500 | })); |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1501 | EXPECT_FALSE(stream_->RetransmitStreamData(0, 18, true, PTO_RETRANSMISSION)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1502 | |
| 1503 | // Retransmit [0, 18) with fin, and all is consumed. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1504 | EXPECT_CALL(*session_, WritevData(stream_->id(), 10, 0, NO_FIN, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1505 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1506 | EXPECT_CALL(*session_, WritevData(stream_->id(), 5, 13, FIN, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1507 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1508 | EXPECT_TRUE(stream_->RetransmitStreamData(0, 18, true, PTO_RETRANSMISSION)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1509 | |
| 1510 | // Retransmit [0, 8) with fin, and all is consumed. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1511 | EXPECT_CALL(*session_, WritevData(stream_->id(), 8, 0, NO_FIN, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1512 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1513 | EXPECT_CALL(*session_, WritevData(stream_->id(), 0, 18, FIN, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1514 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1515 | EXPECT_TRUE(stream_->RetransmitStreamData(0, 8, true, PTO_RETRANSMISSION)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | TEST_P(QuicStreamTest, ResetStreamOnTtlExpiresRetransmitLostData) { |
| 1519 | Initialize(); |
| 1520 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1521 | EXPECT_CALL(*session_, WritevData(stream_->id(), 200, 0, FIN, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1522 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1523 | std::string body(200, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1524 | 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. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1530 | EXPECT_CALL(*session_, WritevData(stream_->id(), 100, 0, NO_FIN, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1531 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1532 | EXPECT_TRUE(stream_->RetransmitStreamData(0, 100, false, PTO_RETRANSMISSION)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1533 | 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. |
renjietang | 9946bc0 | 2020-07-16 15:14:27 -0700 | [diff] [blame] | 1538 | EXPECT_CALL(*session_, SendRstStream(_, QUIC_STREAM_TTL_EXPIRED, _, _)) |
| 1539 | .Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1540 | stream_->OnCanWrite(); |
| 1541 | } |
| 1542 | |
| 1543 | TEST_P(QuicStreamTest, ResetStreamOnTtlExpiresEarlyRetransmitData) { |
| 1544 | Initialize(); |
| 1545 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1546 | EXPECT_CALL(*session_, WritevData(stream_->id(), 200, 0, FIN, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 1547 | .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1548 | std::string body(200, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1549 | 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. |
renjietang | 9946bc0 | 2020-07-16 15:14:27 -0700 | [diff] [blame] | 1557 | EXPECT_CALL(*session_, SendRstStream(_, QUIC_STREAM_TTL_EXPIRED, _, _)) |
| 1558 | .Times(1); |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 1559 | stream_->RetransmitStreamData(0, 100, false, PTO_RETRANSMISSION); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1560 | } |
| 1561 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1562 | // Test that OnStreamReset does one-way (read) closes if version 99, two way |
| 1563 | // (read and write) if not version 99. |
| 1564 | TEST_P(QuicStreamTest, OnStreamResetReadOrReadWrite) { |
| 1565 | Initialize(); |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1566 | EXPECT_FALSE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1567 | 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); |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 1572 | if (VersionHasIetfQuicFrames(connection_->transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1573 | // Version 99/IETF QUIC should close just the read side. |
| 1574 | EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_)); |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1575 | EXPECT_FALSE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1576 | } else { |
| 1577 | // Google QUIC should close both sides of the stream. |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1578 | EXPECT_TRUE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1579 | EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_)); |
| 1580 | } |
| 1581 | } |
| 1582 | |
renjietang | 28c04b7 | 2019-07-01 15:08:09 -0700 | [diff] [blame] | 1583 | TEST_P(QuicStreamTest, WindowUpdateForReadOnlyStream) { |
renjietang | 28c04b7 | 2019-07-01 15:08:09 -0700 | [diff] [blame] | 1584 | 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 | |
renjietang | 15afba3 | 2019-10-23 14:32:35 -0700 | [diff] [blame] | 1599 | TEST_P(QuicStreamTest, RstStreamFrameChangesCloseOffset) { |
renjietang | 15afba3 | 2019-10-23 14:32:35 -0700 | [diff] [blame] | 1600 | Initialize(); |
| 1601 | |
| 1602 | QuicStreamFrame stream_frame(stream_->id(), true, 0, "abc"); |
renjietang | b38e635 | 2019-10-24 09:38:20 -0700 | [diff] [blame] | 1603 | EXPECT_CALL(*stream_, OnDataAvailable()); |
renjietang | 15afba3 | 2019-10-23 14:32:35 -0700 | [diff] [blame] | 1604 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1612 | } // namespace |
| 1613 | } // namespace test |
| 1614 | } // namespace quic |