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> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | |
| 10 | #include "net/third_party/quiche/src/quic/core/quic_connection.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_write_blocked_list.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_mem_slice_storage.h" |
| 19 | #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] | 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 21 | #include "net/third_party/quiche/src/quic/platform/api/quic_test_mem_slice_vector.h" |
| 22 | #include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h" |
| 23 | #include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h" |
| 24 | #include "net/third_party/quiche/src/quic/test_tools/quic_flow_controller_peer.h" |
| 25 | #include "net/third_party/quiche/src/quic/test_tools/quic_session_peer.h" |
| 26 | #include "net/third_party/quiche/src/quic/test_tools/quic_stream_peer.h" |
| 27 | #include "net/third_party/quiche/src/quic/test_tools/quic_stream_sequencer_peer.h" |
| 28 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 29 | |
| 30 | using testing::_; |
| 31 | using testing::AnyNumber; |
| 32 | using testing::AtLeast; |
| 33 | using testing::InSequence; |
| 34 | using testing::Invoke; |
| 35 | using testing::InvokeWithoutArgs; |
| 36 | using testing::Return; |
| 37 | using testing::StrictMock; |
| 38 | |
| 39 | namespace quic { |
| 40 | namespace test { |
| 41 | namespace { |
| 42 | |
| 43 | const char kData1[] = "FooAndBar"; |
| 44 | const char kData2[] = "EepAndBaz"; |
| 45 | const size_t kDataLen = 9; |
| 46 | |
| 47 | class TestStream : public QuicStream { |
| 48 | public: |
| 49 | TestStream(QuicStreamId id, QuicSession* session, StreamType type) |
| 50 | : QuicStream(id, session, /*is_static=*/false, type) {} |
| 51 | |
renjietang | 3544899 | 2019-05-08 17:08:57 -0700 | [diff] [blame] | 52 | TestStream(PendingStream pending, StreamType type, bool is_static) |
| 53 | : QuicStream(std::move(pending), type, is_static) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 54 | |
| 55 | void OnDataAvailable() override {} |
| 56 | |
| 57 | MOCK_METHOD0(OnCanWriteNewData, void()); |
| 58 | |
| 59 | using QuicStream::CanWriteNewData; |
| 60 | using QuicStream::CanWriteNewDataAfterData; |
| 61 | using QuicStream::CloseWriteSide; |
| 62 | using QuicStream::fin_buffered; |
| 63 | using QuicStream::OnClose; |
| 64 | using QuicStream::WriteMemSlices; |
| 65 | using QuicStream::WriteOrBufferData; |
| 66 | using QuicStream::WritevData; |
| 67 | |
| 68 | private: |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 69 | std::string data_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | class QuicStreamTestBase : public QuicTestWithParam<ParsedQuicVersion> { |
| 73 | public: |
| 74 | QuicStreamTestBase() |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 75 | : initial_flow_control_window_bytes_(kMaxOutgoingPacketSize), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 76 | zero_(QuicTime::Delta::Zero()), |
| 77 | supported_versions_(AllSupportedVersions()) {} |
| 78 | |
| 79 | void Initialize() { |
| 80 | ParsedQuicVersionVector version_vector; |
| 81 | version_vector.push_back(GetParam()); |
| 82 | connection_ = new StrictMock<MockQuicConnection>( |
| 83 | &helper_, &alarm_factory_, Perspective::IS_SERVER, version_vector); |
| 84 | connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 85 | session_ = QuicMakeUnique<StrictMock<MockQuicSession>>(connection_); |
| 86 | |
| 87 | // New streams rely on having the peer's flow control receive window |
| 88 | // negotiated in the config. |
| 89 | QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow( |
| 90 | session_->config(), initial_flow_control_window_bytes_); |
| 91 | |
| 92 | stream_ = new TestStream(kTestStreamId, session_.get(), BIDIRECTIONAL); |
| 93 | EXPECT_NE(nullptr, stream_); |
| 94 | // session_ now owns stream_. |
| 95 | session_->ActivateStream(QuicWrapUnique(stream_)); |
| 96 | // Ignore resetting when session_ is terminated. |
| 97 | EXPECT_CALL(*session_, SendRstStream(kTestStreamId, _, _)) |
| 98 | .Times(AnyNumber()); |
| 99 | write_blocked_list_ = |
| 100 | QuicSessionPeer::GetWriteBlockedStreams(session_.get()); |
| 101 | } |
| 102 | |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 103 | bool fin_sent() { return stream_->fin_sent(); } |
| 104 | bool rst_sent() { return stream_->rst_sent(); } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 105 | |
| 106 | void set_initial_flow_control_window_bytes(uint32_t val) { |
| 107 | initial_flow_control_window_bytes_ = val; |
| 108 | } |
| 109 | |
| 110 | bool HasWriteBlockedStreams() { |
| 111 | return write_blocked_list_->HasWriteBlockedSpecialStream() || |
| 112 | write_blocked_list_->HasWriteBlockedDataStreams(); |
| 113 | } |
| 114 | |
| 115 | QuicConsumedData CloseStreamOnWriteError(QuicStream* /*stream*/, |
| 116 | QuicStreamId id, |
| 117 | size_t /*write_length*/, |
| 118 | QuicStreamOffset /*offset*/, |
| 119 | StreamSendingState /*state*/) { |
| 120 | session_->CloseStream(id); |
| 121 | return QuicConsumedData(1, false); |
| 122 | } |
| 123 | |
| 124 | bool ClearControlFrame(const QuicFrame& frame) { |
| 125 | DeleteFrame(&const_cast<QuicFrame&>(frame)); |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | bool ClearResetStreamFrame(const QuicFrame& frame) { |
| 130 | EXPECT_EQ(RST_STREAM_FRAME, frame.type); |
| 131 | DeleteFrame(&const_cast<QuicFrame&>(frame)); |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | bool ClearStopSendingFrame(const QuicFrame& frame) { |
| 136 | EXPECT_EQ(STOP_SENDING_FRAME, frame.type); |
| 137 | DeleteFrame(&const_cast<QuicFrame&>(frame)); |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | protected: |
| 142 | MockQuicConnectionHelper helper_; |
| 143 | MockAlarmFactory alarm_factory_; |
| 144 | MockQuicConnection* connection_; |
| 145 | std::unique_ptr<MockQuicSession> session_; |
| 146 | TestStream* stream_; |
| 147 | QuicWriteBlockedList* write_blocked_list_; |
| 148 | uint32_t initial_flow_control_window_bytes_; |
| 149 | QuicTime::Delta zero_; |
| 150 | ParsedQuicVersionVector supported_versions_; |
| 151 | const QuicStreamId kTestStreamId = |
| 152 | QuicUtils::GetHeadersStreamId(GetParam().transport_version) + |
| 153 | QuicUtils::StreamIdDelta(GetParam().transport_version); |
| 154 | }; |
| 155 | |
| 156 | // Non parameterized QuicStreamTest used for tests that do not |
| 157 | // have any dependencies on the quic version. |
| 158 | class QuicStreamTest : public QuicStreamTestBase {}; |
| 159 | |
| 160 | // Index value of 1 has the test run with supported-version[1], which is some |
| 161 | // version OTHER than 99. |
| 162 | INSTANTIATE_TEST_SUITE_P( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 163 | QuicStreamTests, |
| 164 | QuicStreamTest, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 165 | ::testing::ValuesIn(ParsedVersionOfIndex(AllSupportedVersions(), 1))); |
| 166 | |
| 167 | // Make a parameterized version of the QuicStreamTest for those tests |
| 168 | // that need to differentiate based on version number. |
| 169 | class QuicParameterizedStreamTest : public QuicStreamTestBase {}; |
| 170 | INSTANTIATE_TEST_SUITE_P(QuicParameterizedStreamTests, |
| 171 | QuicParameterizedStreamTest, |
| 172 | ::testing::ValuesIn(AllSupportedVersions())); |
| 173 | |
renjietang | 3544899 | 2019-05-08 17:08:57 -0700 | [diff] [blame] | 174 | TEST_P(QuicStreamTest, PendingStreamStaticness) { |
| 175 | Initialize(); |
| 176 | |
| 177 | PendingStream pending(kTestStreamId + 2, session_.get()); |
| 178 | TestStream stream(std::move(pending), StreamType::BIDIRECTIONAL, false); |
| 179 | EXPECT_FALSE(stream.is_static()); |
| 180 | |
| 181 | PendingStream pending2(kTestStreamId + 3, session_.get()); |
| 182 | TestStream stream2(std::move(pending2), StreamType::BIDIRECTIONAL, true); |
| 183 | EXPECT_TRUE(stream2.is_static()); |
| 184 | } |
| 185 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 186 | TEST_P(QuicStreamTest, PendingStreamTooMuchData) { |
| 187 | Initialize(); |
| 188 | |
| 189 | PendingStream pending(kTestStreamId + 2, session_.get()); |
| 190 | // Receive a stream frame that violates flow control: the byte offset is |
| 191 | // higher than the receive window offset. |
| 192 | QuicStreamFrame frame(kTestStreamId + 2, false, |
| 193 | kInitialSessionFlowControlWindowForTest + 1, |
| 194 | QuicStringPiece(".")); |
| 195 | |
| 196 | // Stream should not accept the frame, and the connection should be closed. |
| 197 | EXPECT_CALL(*connection_, |
| 198 | CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _)); |
| 199 | pending.OnStreamFrame(frame); |
| 200 | } |
| 201 | |
| 202 | TEST_P(QuicStreamTest, PendingStreamTooMuchDataInRstStream) { |
| 203 | Initialize(); |
| 204 | |
| 205 | PendingStream pending(kTestStreamId + 2, session_.get()); |
| 206 | // Receive a rst stream frame that violates flow control: the byte offset is |
| 207 | // higher than the receive window offset. |
| 208 | QuicRstStreamFrame frame(kInvalidControlFrameId, kTestStreamId + 2, |
| 209 | QUIC_STREAM_CANCELLED, |
| 210 | kInitialSessionFlowControlWindowForTest + 1); |
| 211 | |
| 212 | // Pending stream should not accept the frame, and the connection should be |
| 213 | // closed. |
| 214 | EXPECT_CALL(*connection_, |
| 215 | CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _)); |
| 216 | pending.OnRstStreamFrame(frame); |
| 217 | } |
| 218 | |
| 219 | TEST_P(QuicStreamTest, PendingStreamRstStream) { |
| 220 | Initialize(); |
| 221 | |
| 222 | PendingStream pending(kTestStreamId + 2, session_.get()); |
| 223 | QuicStreamOffset final_byte_offset = 7; |
| 224 | QuicRstStreamFrame frame(kInvalidControlFrameId, kTestStreamId + 2, |
| 225 | QUIC_STREAM_CANCELLED, final_byte_offset); |
| 226 | |
| 227 | // Pending stream should accept the frame and not close the connection. |
| 228 | EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0); |
| 229 | pending.OnRstStreamFrame(frame); |
| 230 | } |
| 231 | |
| 232 | TEST_P(QuicStreamTest, FromPendingStream) { |
| 233 | Initialize(); |
| 234 | |
| 235 | PendingStream pending(kTestStreamId + 2, session_.get()); |
| 236 | |
| 237 | QuicStreamFrame frame(kTestStreamId + 2, false, 2, QuicStringPiece(".")); |
| 238 | pending.OnStreamFrame(frame); |
| 239 | pending.OnStreamFrame(frame); |
| 240 | QuicStreamFrame frame2(kTestStreamId + 2, true, 3, QuicStringPiece(".")); |
| 241 | pending.OnStreamFrame(frame2); |
| 242 | |
renjietang | 3544899 | 2019-05-08 17:08:57 -0700 | [diff] [blame] | 243 | TestStream stream(std::move(pending), StreamType::READ_UNIDIRECTIONAL, false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 244 | EXPECT_EQ(3, stream.num_frames_received()); |
| 245 | EXPECT_EQ(3u, stream.stream_bytes_read()); |
| 246 | EXPECT_EQ(1, stream.num_duplicate_frames_received()); |
| 247 | EXPECT_EQ(true, stream.fin_received()); |
| 248 | EXPECT_EQ(frame2.offset + 1, |
| 249 | stream.flow_controller()->highest_received_byte_offset()); |
| 250 | EXPECT_EQ(frame2.offset + 1, |
| 251 | session_->flow_controller()->highest_received_byte_offset()); |
| 252 | } |
| 253 | |
| 254 | TEST_P(QuicStreamTest, FromPendingStreamThenData) { |
| 255 | Initialize(); |
| 256 | |
| 257 | PendingStream pending(kTestStreamId + 2, session_.get()); |
| 258 | |
| 259 | QuicStreamFrame frame(kTestStreamId + 2, false, 2, QuicStringPiece(".")); |
| 260 | pending.OnStreamFrame(frame); |
| 261 | |
renjietang | 3544899 | 2019-05-08 17:08:57 -0700 | [diff] [blame] | 262 | auto stream = new TestStream(std::move(pending), |
| 263 | StreamType::READ_UNIDIRECTIONAL, false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 264 | session_->ActivateStream(QuicWrapUnique(stream)); |
| 265 | |
| 266 | QuicStreamFrame frame2(kTestStreamId + 2, true, 3, QuicStringPiece(".")); |
| 267 | stream->OnStreamFrame(frame2); |
| 268 | |
| 269 | EXPECT_EQ(2, stream->num_frames_received()); |
| 270 | EXPECT_EQ(2u, stream->stream_bytes_read()); |
| 271 | EXPECT_EQ(true, stream->fin_received()); |
| 272 | EXPECT_EQ(frame2.offset + 1, |
| 273 | stream->flow_controller()->highest_received_byte_offset()); |
| 274 | EXPECT_EQ(frame2.offset + 1, |
| 275 | session_->flow_controller()->highest_received_byte_offset()); |
| 276 | } |
| 277 | |
| 278 | TEST_P(QuicStreamTest, WriteAllData) { |
| 279 | Initialize(); |
| 280 | |
| 281 | size_t length = |
| 282 | 1 + QuicPacketCreator::StreamFramePacketOverhead( |
| 283 | connection_->transport_version(), PACKET_8BYTE_CONNECTION_ID, |
| 284 | PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion, |
| 285 | !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER, |
| 286 | VARIABLE_LENGTH_INTEGER_LENGTH_0, |
| 287 | VARIABLE_LENGTH_INTEGER_LENGTH_0, 0u); |
| 288 | connection_->SetMaxPacketLength(length); |
| 289 | |
| 290 | EXPECT_CALL(*session_, WritevData(stream_, kTestStreamId, _, _, _)) |
| 291 | .WillOnce(Invoke(&(MockQuicSession::ConsumeData))); |
| 292 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 293 | EXPECT_FALSE(HasWriteBlockedStreams()); |
| 294 | } |
| 295 | |
| 296 | TEST_P(QuicStreamTest, NoBlockingIfNoDataOrFin) { |
| 297 | Initialize(); |
| 298 | |
| 299 | // Write no data and no fin. If we consume nothing we should not be write |
| 300 | // blocked. |
| 301 | EXPECT_QUIC_BUG(stream_->WriteOrBufferData(QuicStringPiece(), false, nullptr), |
| 302 | ""); |
| 303 | EXPECT_FALSE(HasWriteBlockedStreams()); |
| 304 | } |
| 305 | |
| 306 | TEST_P(QuicStreamTest, BlockIfOnlySomeDataConsumed) { |
| 307 | Initialize(); |
| 308 | |
| 309 | // Write some data and no fin. If we consume some but not all of the data, |
| 310 | // we should be write blocked a not all the data was consumed. |
| 311 | EXPECT_CALL(*session_, WritevData(stream_, kTestStreamId, _, _, _)) |
| 312 | .WillOnce(InvokeWithoutArgs([this]() { |
| 313 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 1u, 0u, |
| 314 | NO_FIN); |
| 315 | })); |
| 316 | stream_->WriteOrBufferData(QuicStringPiece(kData1, 2), false, nullptr); |
| 317 | ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams()); |
| 318 | EXPECT_EQ(1u, stream_->BufferedDataBytes()); |
| 319 | } |
| 320 | |
| 321 | TEST_P(QuicStreamTest, BlockIfFinNotConsumedWithData) { |
| 322 | Initialize(); |
| 323 | |
| 324 | // Write some data and no fin. If we consume all the data but not the fin, |
| 325 | // we should be write blocked because the fin was not consumed. |
| 326 | // (This should never actually happen as the fin should be sent out with the |
| 327 | // last data) |
| 328 | EXPECT_CALL(*session_, WritevData(stream_, kTestStreamId, _, _, _)) |
| 329 | .WillOnce(InvokeWithoutArgs([this]() { |
| 330 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 2u, 0u, |
| 331 | NO_FIN); |
| 332 | })); |
| 333 | stream_->WriteOrBufferData(QuicStringPiece(kData1, 2), true, nullptr); |
| 334 | ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams()); |
| 335 | } |
| 336 | |
| 337 | TEST_P(QuicStreamTest, BlockIfSoloFinNotConsumed) { |
| 338 | Initialize(); |
| 339 | |
| 340 | // Write no data and a fin. If we consume nothing we should be write blocked, |
| 341 | // as the fin was not consumed. |
| 342 | EXPECT_CALL(*session_, WritevData(stream_, kTestStreamId, _, _, _)) |
| 343 | .WillOnce(Return(QuicConsumedData(0, false))); |
| 344 | stream_->WriteOrBufferData(QuicStringPiece(), true, nullptr); |
| 345 | ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams()); |
| 346 | } |
| 347 | |
| 348 | TEST_P(QuicStreamTest, CloseOnPartialWrite) { |
| 349 | Initialize(); |
| 350 | |
| 351 | // Write some data and no fin. However, while writing the data |
| 352 | // close the stream and verify that MarkConnectionLevelWriteBlocked does not |
| 353 | // crash with an unknown stream. |
| 354 | EXPECT_CALL(*session_, WritevData(stream_, kTestStreamId, _, _, _)) |
| 355 | .WillOnce(Invoke(this, &QuicStreamTest::CloseStreamOnWriteError)); |
| 356 | stream_->WriteOrBufferData(QuicStringPiece(kData1, 2), false, nullptr); |
| 357 | ASSERT_EQ(0u, write_blocked_list_->NumBlockedStreams()); |
| 358 | } |
| 359 | |
| 360 | TEST_P(QuicStreamTest, WriteOrBufferData) { |
| 361 | Initialize(); |
| 362 | |
| 363 | EXPECT_FALSE(HasWriteBlockedStreams()); |
| 364 | size_t length = |
| 365 | 1 + QuicPacketCreator::StreamFramePacketOverhead( |
| 366 | connection_->transport_version(), PACKET_8BYTE_CONNECTION_ID, |
| 367 | PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion, |
| 368 | !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER, |
| 369 | VARIABLE_LENGTH_INTEGER_LENGTH_0, |
| 370 | VARIABLE_LENGTH_INTEGER_LENGTH_0, 0u); |
| 371 | connection_->SetMaxPacketLength(length); |
| 372 | |
| 373 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 374 | .WillOnce(InvokeWithoutArgs([this]() { |
| 375 | return MockQuicSession::ConsumeData(stream_, stream_->id(), |
| 376 | kDataLen - 1, 0u, NO_FIN); |
| 377 | })); |
| 378 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 379 | EXPECT_EQ(1u, stream_->BufferedDataBytes()); |
| 380 | EXPECT_TRUE(HasWriteBlockedStreams()); |
| 381 | |
| 382 | // Queue a bytes_consumed write. |
| 383 | stream_->WriteOrBufferData(kData2, false, nullptr); |
| 384 | EXPECT_EQ(10u, stream_->BufferedDataBytes()); |
| 385 | // Make sure we get the tail of the first write followed by the bytes_consumed |
| 386 | InSequence s; |
| 387 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 388 | .WillOnce(InvokeWithoutArgs([this]() { |
| 389 | return MockQuicSession::ConsumeData(stream_, stream_->id(), |
| 390 | kDataLen - 1, kDataLen - 1, NO_FIN); |
| 391 | })); |
| 392 | stream_->OnCanWrite(); |
| 393 | |
| 394 | // And finally the end of the bytes_consumed. |
| 395 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 396 | .WillOnce(InvokeWithoutArgs([this]() { |
| 397 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 2u, |
| 398 | 2 * kDataLen - 2, NO_FIN); |
| 399 | })); |
| 400 | stream_->OnCanWrite(); |
| 401 | } |
| 402 | |
| 403 | TEST_P(QuicStreamTest, WriteOrBufferDataReachStreamLimit) { |
| 404 | Initialize(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 405 | std::string data("aaaaa"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 406 | QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - data.length(), |
| 407 | stream_); |
| 408 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 409 | .WillOnce(Invoke(&(MockQuicSession::ConsumeData))); |
| 410 | stream_->WriteOrBufferData(data, false, nullptr); |
| 411 | EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _)); |
| 412 | EXPECT_QUIC_BUG(stream_->WriteOrBufferData("a", false, nullptr), |
| 413 | "Write too many data via stream"); |
| 414 | } |
| 415 | |
| 416 | TEST_P(QuicStreamTest, ConnectionCloseAfterStreamClose) { |
| 417 | Initialize(); |
| 418 | |
| 419 | QuicStreamPeer::CloseReadSide(stream_); |
| 420 | stream_->CloseWriteSide(); |
| 421 | EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error()); |
| 422 | EXPECT_EQ(QUIC_NO_ERROR, stream_->connection_error()); |
| 423 | stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR, |
| 424 | ConnectionCloseSource::FROM_SELF); |
| 425 | EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error()); |
| 426 | EXPECT_EQ(QUIC_NO_ERROR, stream_->connection_error()); |
| 427 | } |
| 428 | |
| 429 | TEST_P(QuicStreamTest, RstAlwaysSentIfNoFinSent) { |
| 430 | // For flow control accounting, a stream must send either a FIN or a RST frame |
| 431 | // before termination. |
| 432 | // Test that if no FIN has been sent, we send a RST. |
| 433 | |
| 434 | Initialize(); |
| 435 | EXPECT_FALSE(fin_sent()); |
| 436 | EXPECT_FALSE(rst_sent()); |
| 437 | |
| 438 | // Write some data, with no FIN. |
| 439 | EXPECT_CALL(*session_, WritevData(stream_, kTestStreamId, _, _, _)) |
| 440 | .WillOnce(InvokeWithoutArgs([this]() { |
| 441 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 1u, 0u, |
| 442 | NO_FIN); |
| 443 | })); |
| 444 | stream_->WriteOrBufferData(QuicStringPiece(kData1, 1), false, nullptr); |
| 445 | EXPECT_FALSE(fin_sent()); |
| 446 | EXPECT_FALSE(rst_sent()); |
| 447 | |
| 448 | // Now close the stream, and expect that we send a RST. |
| 449 | EXPECT_CALL(*session_, SendRstStream(_, _, _)); |
| 450 | stream_->OnClose(); |
| 451 | EXPECT_FALSE(fin_sent()); |
| 452 | EXPECT_TRUE(rst_sent()); |
| 453 | } |
| 454 | |
| 455 | TEST_P(QuicStreamTest, RstNotSentIfFinSent) { |
| 456 | // For flow control accounting, a stream must send either a FIN or a RST frame |
| 457 | // before termination. |
| 458 | // Test that if a FIN has been sent, we don't also send a RST. |
| 459 | |
| 460 | Initialize(); |
| 461 | EXPECT_FALSE(fin_sent()); |
| 462 | EXPECT_FALSE(rst_sent()); |
| 463 | |
| 464 | // Write some data, with FIN. |
| 465 | EXPECT_CALL(*session_, WritevData(stream_, kTestStreamId, _, _, _)) |
| 466 | .WillOnce(InvokeWithoutArgs([this]() { |
| 467 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 1u, 0u, |
| 468 | FIN); |
| 469 | })); |
| 470 | stream_->WriteOrBufferData(QuicStringPiece(kData1, 1), true, nullptr); |
| 471 | EXPECT_TRUE(fin_sent()); |
| 472 | EXPECT_FALSE(rst_sent()); |
| 473 | |
| 474 | // Now close the stream, and expect that we do not send a RST. |
| 475 | stream_->OnClose(); |
| 476 | EXPECT_TRUE(fin_sent()); |
| 477 | EXPECT_FALSE(rst_sent()); |
| 478 | } |
| 479 | |
| 480 | TEST_P(QuicStreamTest, OnlySendOneRst) { |
| 481 | // For flow control accounting, a stream must send either a FIN or a RST frame |
| 482 | // before termination. |
| 483 | // Test that if a stream sends a RST, it doesn't send an additional RST during |
| 484 | // OnClose() (this shouldn't be harmful, but we shouldn't do it anyway...) |
| 485 | |
| 486 | Initialize(); |
| 487 | EXPECT_FALSE(fin_sent()); |
| 488 | EXPECT_FALSE(rst_sent()); |
| 489 | |
| 490 | // Reset the stream. |
| 491 | const int expected_resets = 1; |
| 492 | EXPECT_CALL(*session_, SendRstStream(_, _, _)).Times(expected_resets); |
| 493 | stream_->Reset(QUIC_STREAM_CANCELLED); |
| 494 | EXPECT_FALSE(fin_sent()); |
| 495 | EXPECT_TRUE(rst_sent()); |
| 496 | |
| 497 | // Now close the stream (any further resets being sent would break the |
| 498 | // expectation above). |
| 499 | stream_->OnClose(); |
| 500 | EXPECT_FALSE(fin_sent()); |
| 501 | EXPECT_TRUE(rst_sent()); |
| 502 | } |
| 503 | |
| 504 | TEST_P(QuicStreamTest, StreamFlowControlMultipleWindowUpdates) { |
| 505 | set_initial_flow_control_window_bytes(1000); |
| 506 | |
| 507 | Initialize(); |
| 508 | |
| 509 | // If we receive multiple WINDOW_UPDATES (potentially out of order), then we |
| 510 | // want to make sure we latch the largest offset we see. |
| 511 | |
| 512 | // Initially should be default. |
| 513 | EXPECT_EQ( |
| 514 | initial_flow_control_window_bytes_, |
| 515 | QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller())); |
| 516 | |
| 517 | // Check a single WINDOW_UPDATE results in correct offset. |
| 518 | QuicWindowUpdateFrame window_update_1(kInvalidControlFrameId, stream_->id(), |
| 519 | 1234); |
| 520 | stream_->OnWindowUpdateFrame(window_update_1); |
| 521 | EXPECT_EQ( |
| 522 | window_update_1.byte_offset, |
| 523 | QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller())); |
| 524 | |
| 525 | // Now send a few more WINDOW_UPDATES and make sure that only the largest is |
| 526 | // remembered. |
| 527 | QuicWindowUpdateFrame window_update_2(kInvalidControlFrameId, stream_->id(), |
| 528 | 1); |
| 529 | QuicWindowUpdateFrame window_update_3(kInvalidControlFrameId, stream_->id(), |
| 530 | 9999); |
| 531 | QuicWindowUpdateFrame window_update_4(kInvalidControlFrameId, stream_->id(), |
| 532 | 5678); |
| 533 | stream_->OnWindowUpdateFrame(window_update_2); |
| 534 | stream_->OnWindowUpdateFrame(window_update_3); |
| 535 | stream_->OnWindowUpdateFrame(window_update_4); |
| 536 | EXPECT_EQ( |
| 537 | window_update_3.byte_offset, |
| 538 | QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller())); |
| 539 | } |
| 540 | |
| 541 | TEST_P(QuicStreamTest, FrameStats) { |
| 542 | Initialize(); |
| 543 | |
| 544 | EXPECT_EQ(0, stream_->num_frames_received()); |
| 545 | EXPECT_EQ(0, stream_->num_duplicate_frames_received()); |
| 546 | QuicStreamFrame frame(stream_->id(), false, 0, QuicStringPiece(".")); |
| 547 | stream_->OnStreamFrame(frame); |
| 548 | EXPECT_EQ(1, stream_->num_frames_received()); |
| 549 | EXPECT_EQ(0, stream_->num_duplicate_frames_received()); |
| 550 | stream_->OnStreamFrame(frame); |
| 551 | EXPECT_EQ(2, stream_->num_frames_received()); |
| 552 | EXPECT_EQ(1, stream_->num_duplicate_frames_received()); |
| 553 | } |
| 554 | |
| 555 | // Verify that when we receive a packet which violates flow control (i.e. sends |
| 556 | // too much data on the stream) that the stream sequencer never sees this frame, |
| 557 | // as we check for violation and close the connection early. |
| 558 | TEST_P(QuicStreamTest, StreamSequencerNeverSeesPacketsViolatingFlowControl) { |
| 559 | Initialize(); |
| 560 | |
| 561 | // Receive a stream frame that violates flow control: the byte offset is |
| 562 | // higher than the receive window offset. |
| 563 | QuicStreamFrame frame(stream_->id(), false, |
| 564 | kInitialSessionFlowControlWindowForTest + 1, |
| 565 | QuicStringPiece(".")); |
| 566 | EXPECT_GT(frame.offset, QuicFlowControllerPeer::ReceiveWindowOffset( |
| 567 | stream_->flow_controller())); |
| 568 | |
| 569 | // Stream should not accept the frame, and the connection should be closed. |
| 570 | EXPECT_CALL(*connection_, |
| 571 | CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _)); |
| 572 | stream_->OnStreamFrame(frame); |
| 573 | } |
| 574 | |
| 575 | // Verify that after the consumer calls StopReading(), the stream still sends |
| 576 | // flow control updates. |
| 577 | TEST_P(QuicStreamTest, StopReadingSendsFlowControl) { |
| 578 | Initialize(); |
| 579 | |
| 580 | stream_->StopReading(); |
| 581 | |
| 582 | // Connection should not get terminated due to flow control errors. |
| 583 | EXPECT_CALL(*connection_, |
| 584 | CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _)) |
| 585 | .Times(0); |
| 586 | EXPECT_CALL(*connection_, SendControlFrame(_)) |
| 587 | .Times(AtLeast(1)) |
| 588 | .WillRepeatedly(Invoke(this, &QuicStreamTest::ClearControlFrame)); |
| 589 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 590 | std::string data(1000, 'x'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 591 | for (QuicStreamOffset offset = 0; |
| 592 | offset < 2 * kInitialStreamFlowControlWindowForTest; |
| 593 | offset += data.length()) { |
| 594 | QuicStreamFrame frame(stream_->id(), false, offset, data); |
| 595 | stream_->OnStreamFrame(frame); |
| 596 | } |
| 597 | EXPECT_LT( |
| 598 | kInitialStreamFlowControlWindowForTest, |
| 599 | QuicFlowControllerPeer::ReceiveWindowOffset(stream_->flow_controller())); |
| 600 | } |
| 601 | |
| 602 | TEST_P(QuicStreamTest, FinalByteOffsetFromFin) { |
| 603 | Initialize(); |
| 604 | |
| 605 | EXPECT_FALSE(stream_->HasFinalReceivedByteOffset()); |
| 606 | |
| 607 | QuicStreamFrame stream_frame_no_fin(stream_->id(), false, 1234, |
| 608 | QuicStringPiece(".")); |
| 609 | stream_->OnStreamFrame(stream_frame_no_fin); |
| 610 | EXPECT_FALSE(stream_->HasFinalReceivedByteOffset()); |
| 611 | |
| 612 | QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, |
| 613 | QuicStringPiece(".")); |
| 614 | stream_->OnStreamFrame(stream_frame_with_fin); |
| 615 | EXPECT_TRUE(stream_->HasFinalReceivedByteOffset()); |
| 616 | } |
| 617 | |
| 618 | TEST_P(QuicStreamTest, FinalByteOffsetFromRst) { |
| 619 | Initialize(); |
| 620 | |
| 621 | EXPECT_FALSE(stream_->HasFinalReceivedByteOffset()); |
| 622 | QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(), |
| 623 | QUIC_STREAM_CANCELLED, 1234); |
| 624 | stream_->OnStreamReset(rst_frame); |
| 625 | EXPECT_TRUE(stream_->HasFinalReceivedByteOffset()); |
| 626 | } |
| 627 | |
| 628 | TEST_P(QuicStreamTest, InvalidFinalByteOffsetFromRst) { |
| 629 | Initialize(); |
| 630 | |
| 631 | EXPECT_FALSE(stream_->HasFinalReceivedByteOffset()); |
| 632 | QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(), |
| 633 | QUIC_STREAM_CANCELLED, 0xFFFFFFFFFFFF); |
| 634 | // Stream should not accept the frame, and the connection should be closed. |
| 635 | EXPECT_CALL(*connection_, |
| 636 | CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _)); |
| 637 | stream_->OnStreamReset(rst_frame); |
| 638 | EXPECT_TRUE(stream_->HasFinalReceivedByteOffset()); |
| 639 | stream_->OnClose(); |
| 640 | } |
| 641 | |
| 642 | TEST_P(QuicStreamTest, FinalByteOffsetFromZeroLengthStreamFrame) { |
| 643 | // When receiving Trailers, an empty stream frame is created with the FIN set, |
| 644 | // and is passed to OnStreamFrame. The Trailers may be sent in advance of |
| 645 | // queued body bytes being sent, and thus the final byte offset may exceed |
| 646 | // current flow control limits. Flow control should only be concerned with |
| 647 | // data that has actually been sent/received, so verify that flow control |
| 648 | // ignores such a stream frame. |
| 649 | Initialize(); |
| 650 | |
| 651 | EXPECT_FALSE(stream_->HasFinalReceivedByteOffset()); |
| 652 | const QuicStreamOffset kByteOffsetExceedingFlowControlWindow = |
| 653 | kInitialSessionFlowControlWindowForTest + 1; |
| 654 | const QuicStreamOffset current_stream_flow_control_offset = |
| 655 | QuicFlowControllerPeer::ReceiveWindowOffset(stream_->flow_controller()); |
| 656 | const QuicStreamOffset current_connection_flow_control_offset = |
| 657 | QuicFlowControllerPeer::ReceiveWindowOffset(session_->flow_controller()); |
| 658 | ASSERT_GT(kByteOffsetExceedingFlowControlWindow, |
| 659 | current_stream_flow_control_offset); |
| 660 | ASSERT_GT(kByteOffsetExceedingFlowControlWindow, |
| 661 | current_connection_flow_control_offset); |
| 662 | QuicStreamFrame zero_length_stream_frame_with_fin( |
| 663 | stream_->id(), /*fin=*/true, kByteOffsetExceedingFlowControlWindow, |
| 664 | QuicStringPiece()); |
| 665 | EXPECT_EQ(0, zero_length_stream_frame_with_fin.data_length); |
| 666 | |
| 667 | EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0); |
| 668 | stream_->OnStreamFrame(zero_length_stream_frame_with_fin); |
| 669 | EXPECT_TRUE(stream_->HasFinalReceivedByteOffset()); |
| 670 | |
| 671 | // The flow control receive offset values should not have changed. |
| 672 | EXPECT_EQ( |
| 673 | current_stream_flow_control_offset, |
| 674 | QuicFlowControllerPeer::ReceiveWindowOffset(stream_->flow_controller())); |
| 675 | EXPECT_EQ( |
| 676 | current_connection_flow_control_offset, |
| 677 | QuicFlowControllerPeer::ReceiveWindowOffset(session_->flow_controller())); |
| 678 | } |
| 679 | |
| 680 | TEST_P(QuicStreamTest, OnStreamResetOffsetOverflow) { |
| 681 | Initialize(); |
| 682 | QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(), |
| 683 | QUIC_STREAM_CANCELLED, kMaxStreamLength + 1); |
| 684 | EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _)); |
| 685 | stream_->OnStreamReset(rst_frame); |
| 686 | } |
| 687 | |
| 688 | TEST_P(QuicStreamTest, OnStreamFrameUpperLimit) { |
| 689 | Initialize(); |
| 690 | |
| 691 | // Modify receive window offset and sequencer buffer total_bytes_read_ to |
| 692 | // avoid flow control violation. |
| 693 | QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(), |
| 694 | kMaxStreamLength + 5u); |
| 695 | QuicFlowControllerPeer::SetReceiveWindowOffset(session_->flow_controller(), |
| 696 | kMaxStreamLength + 5u); |
| 697 | QuicStreamSequencerPeer::SetFrameBufferTotalBytesRead( |
| 698 | QuicStreamPeer::sequencer(stream_), kMaxStreamLength - 10u); |
| 699 | |
| 700 | EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _)) |
| 701 | .Times(0); |
| 702 | QuicStreamFrame stream_frame(stream_->id(), false, kMaxStreamLength - 1, |
| 703 | QuicStringPiece(".")); |
| 704 | stream_->OnStreamFrame(stream_frame); |
| 705 | QuicStreamFrame stream_frame2(stream_->id(), true, kMaxStreamLength, |
| 706 | QuicStringPiece("")); |
| 707 | stream_->OnStreamFrame(stream_frame2); |
| 708 | } |
| 709 | |
| 710 | TEST_P(QuicStreamTest, StreamTooLong) { |
| 711 | Initialize(); |
| 712 | EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _)) |
| 713 | .Times(1); |
| 714 | QuicStreamFrame stream_frame(stream_->id(), false, kMaxStreamLength, |
| 715 | QuicStringPiece(".")); |
| 716 | EXPECT_QUIC_PEER_BUG(stream_->OnStreamFrame(stream_frame), |
| 717 | QuicStrCat("Receive stream frame on stream ", |
| 718 | stream_->id(), " reaches max stream length")); |
| 719 | } |
| 720 | |
| 721 | TEST_P(QuicParameterizedStreamTest, SetDrainingIncomingOutgoing) { |
| 722 | // Don't have incoming data consumed. |
| 723 | Initialize(); |
| 724 | |
| 725 | // Incoming data with FIN. |
| 726 | QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, |
| 727 | QuicStringPiece(".")); |
| 728 | stream_->OnStreamFrame(stream_frame_with_fin); |
| 729 | // The FIN has been received but not consumed. |
| 730 | EXPECT_TRUE(stream_->HasFinalReceivedByteOffset()); |
| 731 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 732 | EXPECT_FALSE(stream_->reading_stopped()); |
| 733 | |
| 734 | EXPECT_EQ(1u, session_->GetNumOpenIncomingStreams()); |
| 735 | |
| 736 | // Outgoing data with FIN. |
| 737 | EXPECT_CALL(*session_, WritevData(stream_, kTestStreamId, _, _, _)) |
| 738 | .WillOnce(InvokeWithoutArgs([this]() { |
| 739 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 2u, 0u, |
| 740 | FIN); |
| 741 | })); |
| 742 | stream_->WriteOrBufferData(QuicStringPiece(kData1, 2), true, nullptr); |
| 743 | EXPECT_TRUE(stream_->write_side_closed()); |
| 744 | |
| 745 | EXPECT_EQ(1u, QuicSessionPeer::GetDrainingStreams(session_.get()) |
| 746 | ->count(kTestStreamId)); |
| 747 | EXPECT_EQ(0u, session_->GetNumOpenIncomingStreams()); |
| 748 | } |
| 749 | |
| 750 | TEST_P(QuicParameterizedStreamTest, SetDrainingOutgoingIncoming) { |
| 751 | // Don't have incoming data consumed. |
| 752 | Initialize(); |
| 753 | |
| 754 | // Outgoing data with FIN. |
| 755 | EXPECT_CALL(*session_, WritevData(stream_, kTestStreamId, _, _, _)) |
| 756 | .WillOnce(InvokeWithoutArgs([this]() { |
| 757 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 2u, 0u, |
| 758 | FIN); |
| 759 | })); |
| 760 | stream_->WriteOrBufferData(QuicStringPiece(kData1, 2), true, nullptr); |
| 761 | EXPECT_TRUE(stream_->write_side_closed()); |
| 762 | |
| 763 | EXPECT_EQ(1u, session_->GetNumOpenIncomingStreams()); |
| 764 | |
| 765 | // Incoming data with FIN. |
| 766 | QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234, |
| 767 | QuicStringPiece(".")); |
| 768 | stream_->OnStreamFrame(stream_frame_with_fin); |
| 769 | // The FIN has been received but not consumed. |
| 770 | EXPECT_TRUE(stream_->HasFinalReceivedByteOffset()); |
| 771 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 772 | EXPECT_FALSE(stream_->reading_stopped()); |
| 773 | |
| 774 | EXPECT_EQ(1u, QuicSessionPeer::GetDrainingStreams(session_.get()) |
| 775 | ->count(kTestStreamId)); |
| 776 | EXPECT_EQ(0u, session_->GetNumOpenIncomingStreams()); |
| 777 | } |
| 778 | |
| 779 | TEST_P(QuicStreamTest, EarlyResponseFinHandling) { |
| 780 | // Verify that if the server completes the response before reading the end of |
| 781 | // the request, the received FIN is recorded. |
| 782 | |
| 783 | Initialize(); |
| 784 | EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0); |
| 785 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 786 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 787 | |
| 788 | // Receive data for the request. |
| 789 | QuicStreamFrame frame1(stream_->id(), false, 0, QuicStringPiece("Start")); |
| 790 | stream_->OnStreamFrame(frame1); |
| 791 | // When QuicSimpleServerStream sends the response, it calls |
| 792 | // QuicStream::CloseReadSide() first. |
| 793 | QuicStreamPeer::CloseReadSide(stream_); |
| 794 | // Send data and FIN for the response. |
| 795 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 796 | EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_)); |
| 797 | // Receive remaining data and FIN for the request. |
| 798 | QuicStreamFrame frame2(stream_->id(), true, 0, QuicStringPiece("End")); |
| 799 | stream_->OnStreamFrame(frame2); |
| 800 | EXPECT_TRUE(stream_->fin_received()); |
| 801 | EXPECT_TRUE(stream_->HasFinalReceivedByteOffset()); |
| 802 | } |
| 803 | |
| 804 | TEST_P(QuicStreamTest, StreamWaitsForAcks) { |
| 805 | Initialize(); |
| 806 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 807 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 808 | // Stream is not waiting for acks initially. |
| 809 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 810 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 811 | |
| 812 | // Send kData1. |
| 813 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 814 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 815 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 816 | QuicByteCount newly_acked_length = 0; |
| 817 | EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(), |
| 818 | &newly_acked_length)); |
| 819 | EXPECT_EQ(9u, newly_acked_length); |
| 820 | // Stream is not waiting for acks as all sent data is acked. |
| 821 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 822 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 823 | |
| 824 | // Send kData2. |
| 825 | stream_->WriteOrBufferData(kData2, false, nullptr); |
| 826 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 827 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 828 | // Send FIN. |
| 829 | stream_->WriteOrBufferData("", true, nullptr); |
| 830 | // Fin only frame is not stored in send buffer. |
| 831 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 832 | |
| 833 | // kData2 is retransmitted. |
| 834 | stream_->OnStreamFrameRetransmitted(9, 9, false); |
| 835 | |
| 836 | // kData2 is acked. |
| 837 | EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(), |
| 838 | &newly_acked_length)); |
| 839 | EXPECT_EQ(9u, newly_acked_length); |
| 840 | // Stream is waiting for acks as FIN is not acked. |
| 841 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 842 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 843 | |
| 844 | // FIN is acked. |
| 845 | EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 0, true, QuicTime::Delta::Zero(), |
| 846 | &newly_acked_length)); |
| 847 | EXPECT_EQ(0u, newly_acked_length); |
| 848 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 849 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 850 | } |
| 851 | |
| 852 | TEST_P(QuicStreamTest, StreamDataGetAckedOutOfOrder) { |
| 853 | Initialize(); |
| 854 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 855 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 856 | // Send data. |
| 857 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 858 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 859 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 860 | stream_->WriteOrBufferData("", true, nullptr); |
| 861 | EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 862 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 863 | |
| 864 | QuicByteCount newly_acked_length = 0; |
| 865 | EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(), |
| 866 | &newly_acked_length)); |
| 867 | EXPECT_EQ(9u, newly_acked_length); |
| 868 | EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 869 | EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 9, false, QuicTime::Delta::Zero(), |
| 870 | &newly_acked_length)); |
| 871 | EXPECT_EQ(9u, newly_acked_length); |
| 872 | EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 873 | EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(), |
| 874 | &newly_acked_length)); |
| 875 | EXPECT_EQ(9u, newly_acked_length); |
| 876 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 877 | // FIN is not acked yet. |
| 878 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 879 | EXPECT_TRUE(stream_->OnStreamFrameAcked(27, 0, true, QuicTime::Delta::Zero(), |
| 880 | &newly_acked_length)); |
| 881 | EXPECT_EQ(0u, newly_acked_length); |
| 882 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 883 | } |
| 884 | |
| 885 | TEST_P(QuicStreamTest, CancelStream) { |
| 886 | Initialize(); |
| 887 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 888 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 889 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 890 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 891 | |
| 892 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 893 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 894 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 895 | // Cancel stream. |
| 896 | stream_->Reset(QUIC_STREAM_NO_ERROR); |
| 897 | // stream still waits for acks as the error code is QUIC_STREAM_NO_ERROR, and |
| 898 | // data is going to be retransmitted. |
| 899 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 900 | EXPECT_CALL(*session_, |
| 901 | SendRstStream(stream_->id(), QUIC_STREAM_CANCELLED, 9)); |
| 902 | stream_->Reset(QUIC_STREAM_CANCELLED); |
| 903 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 904 | // Stream stops waiting for acks as data is not going to be retransmitted. |
| 905 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 906 | } |
| 907 | |
| 908 | TEST_P(QuicStreamTest, RstFrameReceivedStreamNotFinishSending) { |
| 909 | Initialize(); |
| 910 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 911 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 912 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 913 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 914 | |
| 915 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 916 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 917 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 918 | |
| 919 | // RST_STREAM received. |
| 920 | QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(), |
| 921 | QUIC_STREAM_CANCELLED, 9); |
| 922 | EXPECT_CALL(*session_, |
| 923 | SendRstStream(stream_->id(), QUIC_RST_ACKNOWLEDGEMENT, 9)); |
| 924 | stream_->OnStreamReset(rst_frame); |
| 925 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 926 | // Stream stops waiting for acks as it does not finish sending and rst is |
| 927 | // sent. |
| 928 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 929 | } |
| 930 | |
| 931 | TEST_P(QuicStreamTest, RstFrameReceivedStreamFinishSending) { |
| 932 | Initialize(); |
| 933 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 934 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 935 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 936 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 937 | |
| 938 | stream_->WriteOrBufferData(kData1, true, nullptr); |
| 939 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 940 | |
| 941 | // RST_STREAM received. |
| 942 | EXPECT_CALL(*session_, SendRstStream(_, _, _)).Times(0); |
| 943 | QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(), |
| 944 | QUIC_STREAM_CANCELLED, 1234); |
| 945 | stream_->OnStreamReset(rst_frame); |
| 946 | // Stream still waits for acks as it finishes sending and has unacked data. |
| 947 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 948 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 949 | } |
| 950 | |
| 951 | TEST_P(QuicStreamTest, ConnectionClosed) { |
| 952 | Initialize(); |
| 953 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 954 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 955 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 956 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 957 | |
| 958 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 959 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 960 | |
| 961 | EXPECT_CALL(*session_, |
| 962 | SendRstStream(stream_->id(), QUIC_RST_ACKNOWLEDGEMENT, 9)); |
| 963 | stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR, |
| 964 | ConnectionCloseSource::FROM_SELF); |
| 965 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 966 | // Stream stops waiting for acks as connection is going to close. |
| 967 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 968 | } |
| 969 | |
| 970 | TEST_P(QuicStreamTest, CanWriteNewDataAfterData) { |
wub | 4985598 | 2019-05-01 14:16:26 -0700 | [diff] [blame] | 971 | SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 972 | Initialize(); |
| 973 | EXPECT_TRUE(stream_->CanWriteNewDataAfterData(99)); |
| 974 | EXPECT_FALSE(stream_->CanWriteNewDataAfterData(100)); |
| 975 | } |
| 976 | |
| 977 | TEST_P(QuicStreamTest, WriteBufferedData) { |
| 978 | // Set buffered data low water mark to be 100. |
wub | 4985598 | 2019-05-01 14:16:26 -0700 | [diff] [blame] | 979 | SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 980 | // Do not stream level flow control block this stream. |
| 981 | set_initial_flow_control_window_bytes(500000); |
| 982 | |
| 983 | Initialize(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 984 | std::string data(1024, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 985 | EXPECT_TRUE(stream_->CanWriteNewData()); |
| 986 | |
| 987 | // Testing WriteOrBufferData. |
| 988 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 989 | .WillOnce(InvokeWithoutArgs([this]() { |
| 990 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 100u, 0u, |
| 991 | NO_FIN); |
| 992 | })); |
| 993 | stream_->WriteOrBufferData(data, false, nullptr); |
| 994 | stream_->WriteOrBufferData(data, false, nullptr); |
| 995 | stream_->WriteOrBufferData(data, false, nullptr); |
| 996 | // Verify all data is saved. |
| 997 | EXPECT_EQ(3 * data.length() - 100, stream_->BufferedDataBytes()); |
| 998 | |
| 999 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1000 | .WillOnce(InvokeWithoutArgs([this]() { |
| 1001 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 100, 100u, |
| 1002 | NO_FIN); |
| 1003 | })); |
| 1004 | // Buffered data size > threshold, do not ask upper layer for more data. |
| 1005 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(0); |
| 1006 | stream_->OnCanWrite(); |
| 1007 | EXPECT_EQ(3 * data.length() - 200, stream_->BufferedDataBytes()); |
| 1008 | EXPECT_FALSE(stream_->CanWriteNewData()); |
| 1009 | |
| 1010 | // Send buffered data to make buffered data size < threshold. |
| 1011 | size_t data_to_write = 3 * data.length() - 200 - |
| 1012 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1; |
| 1013 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1014 | .WillOnce(InvokeWithoutArgs([this, data_to_write]() { |
| 1015 | return MockQuicSession::ConsumeData(stream_, stream_->id(), |
| 1016 | data_to_write, 200u, NO_FIN); |
| 1017 | })); |
| 1018 | // Buffered data size < threshold, ask upper layer for more data. |
| 1019 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1); |
| 1020 | stream_->OnCanWrite(); |
dschinazi | 696ee60 | 2019-04-25 16:05:35 -0700 | [diff] [blame] | 1021 | EXPECT_EQ(static_cast<uint64_t>( |
| 1022 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1023 | stream_->BufferedDataBytes()); |
| 1024 | EXPECT_TRUE(stream_->CanWriteNewData()); |
| 1025 | |
| 1026 | // Flush all buffered data. |
| 1027 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1028 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1029 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1); |
| 1030 | stream_->OnCanWrite(); |
| 1031 | EXPECT_EQ(0u, stream_->BufferedDataBytes()); |
| 1032 | EXPECT_FALSE(stream_->HasBufferedData()); |
| 1033 | EXPECT_TRUE(stream_->CanWriteNewData()); |
| 1034 | |
| 1035 | // Testing Writev. |
| 1036 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1037 | .WillOnce(Return(QuicConsumedData(0, false))); |
| 1038 | struct iovec iov = {const_cast<char*>(data.data()), data.length()}; |
| 1039 | QuicMemSliceStorage storage( |
| 1040 | &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(), |
| 1041 | 1024); |
| 1042 | QuicConsumedData consumed = stream_->WriteMemSlices(storage.ToSpan(), false); |
| 1043 | |
| 1044 | // There is no buffered data before, all data should be consumed without |
| 1045 | // respecting buffered data upper limit. |
| 1046 | EXPECT_EQ(data.length(), consumed.bytes_consumed); |
| 1047 | EXPECT_FALSE(consumed.fin_consumed); |
| 1048 | EXPECT_EQ(data.length(), stream_->BufferedDataBytes()); |
| 1049 | EXPECT_FALSE(stream_->CanWriteNewData()); |
| 1050 | |
| 1051 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)).Times(0); |
| 1052 | QuicMemSliceStorage storage2( |
| 1053 | &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(), |
| 1054 | 1024); |
| 1055 | consumed = stream_->WriteMemSlices(storage2.ToSpan(), false); |
| 1056 | // No Data can be consumed as buffered data is beyond upper limit. |
| 1057 | EXPECT_EQ(0u, consumed.bytes_consumed); |
| 1058 | EXPECT_FALSE(consumed.fin_consumed); |
| 1059 | EXPECT_EQ(data.length(), stream_->BufferedDataBytes()); |
| 1060 | |
| 1061 | data_to_write = |
| 1062 | data.length() - GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1; |
| 1063 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1064 | .WillOnce(InvokeWithoutArgs([this, data_to_write]() { |
| 1065 | return MockQuicSession::ConsumeData(stream_, stream_->id(), |
| 1066 | data_to_write, 0u, NO_FIN); |
| 1067 | })); |
| 1068 | |
| 1069 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1); |
| 1070 | stream_->OnCanWrite(); |
dschinazi | 696ee60 | 2019-04-25 16:05:35 -0700 | [diff] [blame] | 1071 | EXPECT_EQ(static_cast<uint64_t>( |
| 1072 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1073 | stream_->BufferedDataBytes()); |
| 1074 | EXPECT_TRUE(stream_->CanWriteNewData()); |
| 1075 | |
| 1076 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)).Times(0); |
| 1077 | // All data can be consumed as buffered data is below upper limit. |
| 1078 | QuicMemSliceStorage storage3( |
| 1079 | &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(), |
| 1080 | 1024); |
| 1081 | consumed = stream_->WriteMemSlices(storage3.ToSpan(), false); |
| 1082 | EXPECT_EQ(data.length(), consumed.bytes_consumed); |
| 1083 | EXPECT_FALSE(consumed.fin_consumed); |
| 1084 | EXPECT_EQ(data.length() + GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1, |
| 1085 | stream_->BufferedDataBytes()); |
| 1086 | EXPECT_FALSE(stream_->CanWriteNewData()); |
| 1087 | } |
| 1088 | |
| 1089 | TEST_P(QuicStreamTest, WritevDataReachStreamLimit) { |
| 1090 | Initialize(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1091 | std::string data("aaaaa"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1092 | QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - data.length(), |
| 1093 | stream_); |
| 1094 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1095 | .WillOnce(Invoke(&(MockQuicSession::ConsumeData))); |
| 1096 | struct iovec iov = {const_cast<char*>(data.data()), 5u}; |
| 1097 | QuicMemSliceStorage storage( |
| 1098 | &iov, 1, session_->connection()->helper()->GetStreamSendBufferAllocator(), |
| 1099 | 1024); |
| 1100 | QuicConsumedData consumed = stream_->WriteMemSlices(storage.ToSpan(), false); |
| 1101 | EXPECT_EQ(data.length(), consumed.bytes_consumed); |
| 1102 | struct iovec iov2 = {const_cast<char*>(data.data()), 1u}; |
| 1103 | QuicMemSliceStorage storage2( |
| 1104 | &iov2, 1, |
| 1105 | session_->connection()->helper()->GetStreamSendBufferAllocator(), 1024); |
| 1106 | EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _)); |
| 1107 | EXPECT_QUIC_BUG(stream_->WriteMemSlices(storage2.ToSpan(), false), |
| 1108 | "Write too many data via stream"); |
| 1109 | } |
| 1110 | |
| 1111 | TEST_P(QuicStreamTest, WriteMemSlices) { |
| 1112 | // Set buffered data low water mark to be 100. |
wub | 4985598 | 2019-05-01 14:16:26 -0700 | [diff] [blame] | 1113 | SetQuicFlag(FLAGS_quic_buffered_data_threshold, 100); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1114 | // Do not flow control block this stream. |
| 1115 | set_initial_flow_control_window_bytes(500000); |
| 1116 | |
| 1117 | Initialize(); |
| 1118 | char data[1024]; |
| 1119 | std::vector<std::pair<char*, size_t>> buffers; |
| 1120 | buffers.push_back(std::make_pair(data, QUIC_ARRAYSIZE(data))); |
| 1121 | buffers.push_back(std::make_pair(data, QUIC_ARRAYSIZE(data))); |
| 1122 | QuicTestMemSliceVector vector1(buffers); |
| 1123 | QuicTestMemSliceVector vector2(buffers); |
| 1124 | QuicMemSliceSpan span1 = vector1.span(); |
| 1125 | QuicMemSliceSpan span2 = vector2.span(); |
| 1126 | |
| 1127 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1128 | .WillOnce(InvokeWithoutArgs([this]() { |
| 1129 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 100u, 0u, |
| 1130 | NO_FIN); |
| 1131 | })); |
| 1132 | // There is no buffered data before, all data should be consumed. |
| 1133 | QuicConsumedData consumed = stream_->WriteMemSlices(span1, false); |
| 1134 | EXPECT_EQ(2048u, consumed.bytes_consumed); |
| 1135 | EXPECT_FALSE(consumed.fin_consumed); |
| 1136 | EXPECT_EQ(2 * QUIC_ARRAYSIZE(data) - 100, stream_->BufferedDataBytes()); |
| 1137 | EXPECT_FALSE(stream_->fin_buffered()); |
| 1138 | |
| 1139 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)).Times(0); |
| 1140 | // No Data can be consumed as buffered data is beyond upper limit. |
| 1141 | consumed = stream_->WriteMemSlices(span2, true); |
| 1142 | EXPECT_EQ(0u, consumed.bytes_consumed); |
| 1143 | EXPECT_FALSE(consumed.fin_consumed); |
| 1144 | EXPECT_EQ(2 * QUIC_ARRAYSIZE(data) - 100, stream_->BufferedDataBytes()); |
| 1145 | EXPECT_FALSE(stream_->fin_buffered()); |
| 1146 | |
| 1147 | size_t data_to_write = 2 * QUIC_ARRAYSIZE(data) - 100 - |
| 1148 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) + 1; |
| 1149 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1150 | .WillOnce(InvokeWithoutArgs([this, data_to_write]() { |
| 1151 | return MockQuicSession::ConsumeData(stream_, stream_->id(), |
| 1152 | data_to_write, 100u, NO_FIN); |
| 1153 | })); |
| 1154 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(1); |
| 1155 | stream_->OnCanWrite(); |
dschinazi | 696ee60 | 2019-04-25 16:05:35 -0700 | [diff] [blame] | 1156 | EXPECT_EQ(static_cast<uint64_t>( |
| 1157 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1158 | stream_->BufferedDataBytes()); |
| 1159 | // Try to write slices2 again. |
| 1160 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)).Times(0); |
| 1161 | consumed = stream_->WriteMemSlices(span2, true); |
| 1162 | EXPECT_EQ(2048u, consumed.bytes_consumed); |
| 1163 | EXPECT_TRUE(consumed.fin_consumed); |
| 1164 | EXPECT_EQ(2 * QUIC_ARRAYSIZE(data) + |
| 1165 | GetQuicFlag(FLAGS_quic_buffered_data_threshold) - 1, |
| 1166 | stream_->BufferedDataBytes()); |
| 1167 | EXPECT_TRUE(stream_->fin_buffered()); |
| 1168 | |
| 1169 | // Flush all buffered data. |
| 1170 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1171 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1172 | stream_->OnCanWrite(); |
| 1173 | EXPECT_CALL(*stream_, OnCanWriteNewData()).Times(0); |
| 1174 | EXPECT_FALSE(stream_->HasBufferedData()); |
| 1175 | EXPECT_TRUE(stream_->write_side_closed()); |
| 1176 | } |
| 1177 | |
| 1178 | TEST_P(QuicStreamTest, WriteMemSlicesReachStreamLimit) { |
| 1179 | Initialize(); |
| 1180 | QuicStreamPeer::SetStreamBytesWritten(kMaxStreamLength - 5u, stream_); |
| 1181 | char data[5]; |
| 1182 | std::vector<std::pair<char*, size_t>> buffers; |
| 1183 | buffers.push_back(std::make_pair(data, QUIC_ARRAYSIZE(data))); |
| 1184 | QuicTestMemSliceVector vector1(buffers); |
| 1185 | QuicMemSliceSpan span1 = vector1.span(); |
| 1186 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1187 | .WillOnce(InvokeWithoutArgs([this]() { |
| 1188 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 5u, 0u, |
| 1189 | NO_FIN); |
| 1190 | })); |
| 1191 | // There is no buffered data before, all data should be consumed. |
| 1192 | QuicConsumedData consumed = stream_->WriteMemSlices(span1, false); |
| 1193 | EXPECT_EQ(5u, consumed.bytes_consumed); |
| 1194 | |
| 1195 | std::vector<std::pair<char*, size_t>> buffers2; |
| 1196 | buffers2.push_back(std::make_pair(data, 1u)); |
| 1197 | QuicTestMemSliceVector vector2(buffers); |
| 1198 | QuicMemSliceSpan span2 = vector2.span(); |
| 1199 | EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _)); |
| 1200 | EXPECT_QUIC_BUG(stream_->WriteMemSlices(span2, false), |
| 1201 | "Write too many data via stream"); |
| 1202 | } |
| 1203 | |
| 1204 | TEST_P(QuicStreamTest, StreamDataGetAckedMultipleTimes) { |
| 1205 | Initialize(); |
| 1206 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1207 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 1208 | // Send [0, 27) and fin. |
| 1209 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 1210 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 1211 | stream_->WriteOrBufferData(kData1, true, nullptr); |
| 1212 | EXPECT_EQ(3u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1213 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 1214 | |
| 1215 | // Ack [0, 9), [5, 22) and [18, 26) |
| 1216 | // Verify [0, 9) 9 bytes are acked. |
| 1217 | QuicByteCount newly_acked_length = 0; |
| 1218 | EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(), |
| 1219 | &newly_acked_length)); |
| 1220 | EXPECT_EQ(9u, newly_acked_length); |
| 1221 | EXPECT_EQ(2u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1222 | // Verify [9, 22) 13 bytes are acked. |
| 1223 | EXPECT_TRUE(stream_->OnStreamFrameAcked(5, 17, false, QuicTime::Delta::Zero(), |
| 1224 | &newly_acked_length)); |
| 1225 | EXPECT_EQ(13u, newly_acked_length); |
| 1226 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1227 | // Verify [22, 26) 4 bytes are acked. |
| 1228 | EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 8, false, QuicTime::Delta::Zero(), |
| 1229 | &newly_acked_length)); |
| 1230 | EXPECT_EQ(4u, newly_acked_length); |
| 1231 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1232 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 1233 | |
| 1234 | // Ack [0, 27). Verify [26, 27) 1 byte is acked. |
| 1235 | EXPECT_TRUE(stream_->OnStreamFrameAcked(26, 1, false, QuicTime::Delta::Zero(), |
| 1236 | &newly_acked_length)); |
| 1237 | EXPECT_EQ(1u, newly_acked_length); |
| 1238 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1239 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 1240 | |
| 1241 | // Ack Fin. |
| 1242 | EXPECT_TRUE(stream_->OnStreamFrameAcked(27, 0, true, QuicTime::Delta::Zero(), |
| 1243 | &newly_acked_length)); |
| 1244 | EXPECT_EQ(0u, newly_acked_length); |
| 1245 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1246 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 1247 | |
| 1248 | // Ack [10, 27) and fin. No new data is acked. |
| 1249 | EXPECT_FALSE(stream_->OnStreamFrameAcked( |
| 1250 | 10, 17, true, QuicTime::Delta::Zero(), &newly_acked_length)); |
| 1251 | EXPECT_EQ(0u, newly_acked_length); |
| 1252 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1253 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 1254 | } |
| 1255 | |
| 1256 | TEST_P(QuicStreamTest, OnStreamFrameLost) { |
| 1257 | Initialize(); |
| 1258 | |
| 1259 | // Send [0, 9). |
| 1260 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1261 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1262 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 1263 | EXPECT_FALSE(stream_->HasBufferedData()); |
| 1264 | EXPECT_TRUE(stream_->IsStreamFrameOutstanding(0, 9, false)); |
| 1265 | |
| 1266 | // Try to send [9, 27), but connection is blocked. |
| 1267 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1268 | .WillOnce(Return(QuicConsumedData(0, false))); |
| 1269 | stream_->WriteOrBufferData(kData2, false, nullptr); |
| 1270 | stream_->WriteOrBufferData(kData2, false, nullptr); |
| 1271 | EXPECT_TRUE(stream_->HasBufferedData()); |
| 1272 | EXPECT_FALSE(stream_->HasPendingRetransmission()); |
| 1273 | |
| 1274 | // Lost [0, 9). When stream gets a chance to write, only lost data is |
| 1275 | // transmitted. |
| 1276 | stream_->OnStreamFrameLost(0, 9, false); |
| 1277 | EXPECT_TRUE(stream_->HasPendingRetransmission()); |
| 1278 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1279 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1280 | stream_->OnCanWrite(); |
| 1281 | EXPECT_FALSE(stream_->HasPendingRetransmission()); |
| 1282 | EXPECT_TRUE(stream_->HasBufferedData()); |
| 1283 | |
| 1284 | // This OnCanWrite causes [9, 27) to be sent. |
| 1285 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1286 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1287 | stream_->OnCanWrite(); |
| 1288 | EXPECT_FALSE(stream_->HasBufferedData()); |
| 1289 | |
| 1290 | // Send a fin only frame. |
| 1291 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1292 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1293 | stream_->WriteOrBufferData("", true, nullptr); |
| 1294 | |
| 1295 | // Lost [9, 27) and fin. |
| 1296 | stream_->OnStreamFrameLost(9, 18, false); |
| 1297 | stream_->OnStreamFrameLost(27, 0, true); |
| 1298 | EXPECT_TRUE(stream_->HasPendingRetransmission()); |
| 1299 | |
| 1300 | // Ack [9, 18). |
| 1301 | QuicByteCount newly_acked_length = 0; |
| 1302 | EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(), |
| 1303 | &newly_acked_length)); |
| 1304 | EXPECT_EQ(9u, newly_acked_length); |
| 1305 | EXPECT_FALSE(stream_->IsStreamFrameOutstanding(9, 3, false)); |
| 1306 | EXPECT_TRUE(stream_->HasPendingRetransmission()); |
| 1307 | // This OnCanWrite causes [18, 27) and fin to be retransmitted. Verify fin can |
| 1308 | // be bundled with data. |
| 1309 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1310 | .WillOnce(InvokeWithoutArgs([this]() { |
| 1311 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 9u, 18u, |
| 1312 | FIN); |
| 1313 | })); |
| 1314 | stream_->OnCanWrite(); |
| 1315 | EXPECT_FALSE(stream_->HasPendingRetransmission()); |
| 1316 | // Lost [9, 18) again, but it is not considered as lost because kData2 |
| 1317 | // has been acked. |
| 1318 | stream_->OnStreamFrameLost(9, 9, false); |
| 1319 | EXPECT_FALSE(stream_->HasPendingRetransmission()); |
| 1320 | EXPECT_TRUE(stream_->IsStreamFrameOutstanding(27, 0, true)); |
| 1321 | } |
| 1322 | |
| 1323 | TEST_P(QuicStreamTest, CannotBundleLostFin) { |
| 1324 | Initialize(); |
| 1325 | |
| 1326 | // Send [0, 18) and fin. |
| 1327 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1328 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 1329 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 1330 | stream_->WriteOrBufferData(kData2, true, nullptr); |
| 1331 | |
| 1332 | // Lost [0, 9) and fin. |
| 1333 | stream_->OnStreamFrameLost(0, 9, false); |
| 1334 | stream_->OnStreamFrameLost(18, 0, true); |
| 1335 | |
| 1336 | // Retransmit lost data. Verify [0, 9) and fin are retransmitted in two |
| 1337 | // frames. |
| 1338 | InSequence s; |
| 1339 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1340 | .WillOnce(InvokeWithoutArgs([this]() { |
| 1341 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 9u, 0u, |
| 1342 | NO_FIN); |
| 1343 | })); |
| 1344 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1345 | .WillOnce(Return(QuicConsumedData(0, true))); |
| 1346 | stream_->OnCanWrite(); |
| 1347 | } |
| 1348 | |
| 1349 | TEST_P(QuicStreamTest, MarkConnectionLevelWriteBlockedOnWindowUpdateFrame) { |
| 1350 | // Set a small initial control window size. |
| 1351 | set_initial_flow_control_window_bytes(100); |
| 1352 | Initialize(); |
| 1353 | |
| 1354 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1355 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 1356 | EXPECT_CALL(*connection_, SendControlFrame(_)) |
| 1357 | .WillOnce(Invoke(this, &QuicStreamTest::ClearControlFrame)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1358 | std::string data(1024, '.'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1359 | stream_->WriteOrBufferData(data, false, nullptr); |
| 1360 | EXPECT_FALSE(HasWriteBlockedStreams()); |
| 1361 | |
| 1362 | QuicWindowUpdateFrame window_update(kInvalidControlFrameId, stream_->id(), |
| 1363 | 1234); |
| 1364 | |
| 1365 | stream_->OnWindowUpdateFrame(window_update); |
| 1366 | // Verify stream is marked connection level write blocked. |
| 1367 | EXPECT_TRUE(HasWriteBlockedStreams()); |
| 1368 | EXPECT_TRUE(stream_->HasBufferedData()); |
| 1369 | } |
| 1370 | |
| 1371 | // Regression test for b/73282665. |
| 1372 | TEST_P(QuicStreamTest, |
| 1373 | MarkConnectionLevelWriteBlockedOnWindowUpdateFrameWithNoBufferedData) { |
| 1374 | // Set a small initial flow control window size. |
| 1375 | const uint32_t kSmallWindow = 100; |
| 1376 | set_initial_flow_control_window_bytes(kSmallWindow); |
| 1377 | Initialize(); |
| 1378 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1379 | std::string data(kSmallWindow, '.'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1380 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _)) |
| 1381 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 1382 | EXPECT_CALL(*connection_, SendControlFrame(_)) |
| 1383 | .WillOnce(Invoke(this, &QuicStreamTest::ClearControlFrame)); |
| 1384 | stream_->WriteOrBufferData(data, false, nullptr); |
| 1385 | EXPECT_FALSE(HasWriteBlockedStreams()); |
| 1386 | |
| 1387 | QuicWindowUpdateFrame window_update(kInvalidControlFrameId, stream_->id(), |
| 1388 | 120); |
| 1389 | stream_->OnWindowUpdateFrame(window_update); |
| 1390 | EXPECT_FALSE(stream_->HasBufferedData()); |
| 1391 | // Verify stream is marked as blocked although there is no buffered data. |
| 1392 | EXPECT_TRUE(HasWriteBlockedStreams()); |
| 1393 | } |
| 1394 | |
| 1395 | TEST_P(QuicStreamTest, RetransmitStreamData) { |
| 1396 | Initialize(); |
| 1397 | InSequence s; |
| 1398 | |
| 1399 | // Send [0, 18) with fin. |
| 1400 | EXPECT_CALL(*session_, WritevData(_, stream_->id(), _, _, _)) |
| 1401 | .Times(2) |
| 1402 | .WillRepeatedly(Invoke(MockQuicSession::ConsumeData)); |
| 1403 | stream_->WriteOrBufferData(kData1, false, nullptr); |
| 1404 | stream_->WriteOrBufferData(kData1, true, nullptr); |
| 1405 | // Ack [10, 13). |
| 1406 | QuicByteCount newly_acked_length = 0; |
| 1407 | stream_->OnStreamFrameAcked(10, 3, false, QuicTime::Delta::Zero(), |
| 1408 | &newly_acked_length); |
| 1409 | EXPECT_EQ(3u, newly_acked_length); |
| 1410 | // Retransmit [0, 18) with fin, and only [0, 8) is consumed. |
| 1411 | EXPECT_CALL(*session_, WritevData(_, stream_->id(), 10, 0, NO_FIN)) |
| 1412 | .WillOnce(InvokeWithoutArgs([this]() { |
| 1413 | return MockQuicSession::ConsumeData(stream_, stream_->id(), 8, 0u, |
| 1414 | NO_FIN); |
| 1415 | })); |
| 1416 | EXPECT_FALSE(stream_->RetransmitStreamData(0, 18, true)); |
| 1417 | |
| 1418 | // Retransmit [0, 18) with fin, and all is consumed. |
| 1419 | EXPECT_CALL(*session_, WritevData(_, stream_->id(), 10, 0, NO_FIN)) |
| 1420 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1421 | EXPECT_CALL(*session_, WritevData(_, stream_->id(), 5, 13, FIN)) |
| 1422 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1423 | EXPECT_TRUE(stream_->RetransmitStreamData(0, 18, true)); |
| 1424 | |
| 1425 | // Retransmit [0, 8) with fin, and all is consumed. |
| 1426 | EXPECT_CALL(*session_, WritevData(_, stream_->id(), 8, 0, NO_FIN)) |
| 1427 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1428 | EXPECT_CALL(*session_, WritevData(_, stream_->id(), 0, 18, FIN)) |
| 1429 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1430 | EXPECT_TRUE(stream_->RetransmitStreamData(0, 8, true)); |
| 1431 | } |
| 1432 | |
| 1433 | TEST_P(QuicStreamTest, ResetStreamOnTtlExpiresRetransmitLostData) { |
| 1434 | Initialize(); |
| 1435 | |
| 1436 | EXPECT_CALL(*session_, WritevData(_, stream_->id(), 200, 0, FIN)) |
| 1437 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1438 | std::string body(200, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1439 | stream_->WriteOrBufferData(body, true, nullptr); |
| 1440 | |
| 1441 | // Set TTL to be 1 s. |
| 1442 | QuicTime::Delta ttl = QuicTime::Delta::FromSeconds(1); |
| 1443 | ASSERT_TRUE(stream_->MaybeSetTtl(ttl)); |
| 1444 | // Verify data gets retransmitted because TTL does not expire. |
| 1445 | EXPECT_CALL(*session_, WritevData(_, stream_->id(), 100, 0, NO_FIN)) |
| 1446 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 1447 | EXPECT_TRUE(stream_->RetransmitStreamData(0, 100, false)); |
| 1448 | stream_->OnStreamFrameLost(100, 100, true); |
| 1449 | EXPECT_TRUE(stream_->HasPendingRetransmission()); |
| 1450 | |
| 1451 | connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 1452 | // Verify stream gets reset because TTL expires. |
| 1453 | EXPECT_CALL(*session_, SendRstStream(_, QUIC_STREAM_TTL_EXPIRED, _)).Times(1); |
| 1454 | stream_->OnCanWrite(); |
| 1455 | } |
| 1456 | |
| 1457 | TEST_P(QuicStreamTest, ResetStreamOnTtlExpiresEarlyRetransmitData) { |
| 1458 | Initialize(); |
| 1459 | |
| 1460 | EXPECT_CALL(*session_, WritevData(_, stream_->id(), 200, 0, FIN)) |
| 1461 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1462 | std::string body(200, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1463 | stream_->WriteOrBufferData(body, true, nullptr); |
| 1464 | |
| 1465 | // Set TTL to be 1 s. |
| 1466 | QuicTime::Delta ttl = QuicTime::Delta::FromSeconds(1); |
| 1467 | ASSERT_TRUE(stream_->MaybeSetTtl(ttl)); |
| 1468 | |
| 1469 | connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 1470 | // Verify stream gets reset because TTL expires. |
| 1471 | EXPECT_CALL(*session_, SendRstStream(_, QUIC_STREAM_TTL_EXPIRED, _)).Times(1); |
| 1472 | stream_->RetransmitStreamData(0, 100, false); |
| 1473 | } |
| 1474 | |
| 1475 | // Test that QuicStream::StopSending A) is a no-op if the connection is not in |
| 1476 | // version 99, B) that it properly invokes QuicSession::StopSending, and C) that |
| 1477 | // the correct data is passed along, including getting the stream ID. |
| 1478 | TEST_P(QuicParameterizedStreamTest, CheckStopSending) { |
| 1479 | Initialize(); |
| 1480 | const int kStopSendingCode = 123; |
| 1481 | // These must start as false. |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1482 | EXPECT_FALSE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1483 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 1484 | // Expect to actually see a stop sending if and only if we are in version 99. |
| 1485 | if (connection_->transport_version() == QUIC_VERSION_99) { |
| 1486 | EXPECT_CALL(*session_, SendStopSending(kStopSendingCode, stream_->id())) |
| 1487 | .Times(1); |
| 1488 | } else { |
| 1489 | EXPECT_CALL(*session_, SendStopSending(_, _)).Times(0); |
| 1490 | } |
| 1491 | stream_->SendStopSending(kStopSendingCode); |
| 1492 | // Sending a STOP_SENDING does not actually close the local stream. |
| 1493 | // Our implementation waits for the responding RESET_STREAM to effect the |
| 1494 | // closes. Therefore, read- and write-side closes should both be false. |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1495 | EXPECT_FALSE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1496 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 1497 | } |
| 1498 | |
| 1499 | // Test that OnStreamReset does one-way (read) closes if version 99, two way |
| 1500 | // (read and write) if not version 99. |
| 1501 | TEST_P(QuicStreamTest, OnStreamResetReadOrReadWrite) { |
| 1502 | Initialize(); |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1503 | EXPECT_FALSE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1504 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 1505 | |
| 1506 | QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(), |
| 1507 | QUIC_STREAM_CANCELLED, 1234); |
| 1508 | stream_->OnStreamReset(rst_frame); |
| 1509 | if (connection_->transport_version() == QUIC_VERSION_99) { |
| 1510 | // Version 99/IETF QUIC should close just the read side. |
| 1511 | EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_)); |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1512 | EXPECT_FALSE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1513 | } else { |
| 1514 | // Google QUIC should close both sides of the stream. |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1515 | EXPECT_TRUE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1516 | EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_)); |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | // Test that receiving a STOP_SENDING just closes the write side of the stream. |
| 1521 | // If not V99, the test is a noop (no STOP_SENDING in Google QUIC). |
| 1522 | TEST_P(QuicStreamTest, OnStopSendingReadOrReadWrite) { |
| 1523 | Initialize(); |
| 1524 | if (connection_->transport_version() != QUIC_VERSION_99) { |
| 1525 | return; |
| 1526 | } |
| 1527 | |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1528 | EXPECT_FALSE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1529 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 1530 | |
| 1531 | // Simulate receipt of a STOP_SENDING. |
| 1532 | stream_->OnStopSending(123); |
| 1533 | |
| 1534 | // Should close just the read side. |
| 1535 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1536 | EXPECT_TRUE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | // SendOnlyRstStream must only send a RESET_STREAM (no bundled STOP_SENDING). |
| 1540 | TEST_P(QuicStreamTest, SendOnlyRstStream) { |
| 1541 | Initialize(); |
| 1542 | if (connection_->transport_version() != QUIC_VERSION_99) { |
| 1543 | return; |
| 1544 | } |
| 1545 | |
| 1546 | EXPECT_CALL(*connection_, |
| 1547 | OnStreamReset(stream_->id(), QUIC_BAD_APPLICATION_PAYLOAD)); |
| 1548 | EXPECT_CALL(*connection_, SendControlFrame(_)) |
| 1549 | .Times(1) |
| 1550 | .WillOnce(Invoke(this, &QuicStreamTest::ClearResetStreamFrame)); |
| 1551 | |
| 1552 | QuicSessionPeer::SendRstStreamInner(session_.get(), stream_->id(), |
| 1553 | QUIC_BAD_APPLICATION_PAYLOAD, |
| 1554 | stream_->stream_bytes_written(), |
| 1555 | /*close_write_side_only=*/true); |
| 1556 | |
| 1557 | // ResetStreamOnly should just close the write side. |
| 1558 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 1559 | EXPECT_TRUE(stream_->write_side_closed()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | } // namespace |
| 1563 | } // namespace test |
| 1564 | } // namespace quic |