blob: fba914f591b536f259c2ae1cba21f8de727596c9 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/third_party/quiche/src/quic/core/quic_session.h"
6
7#include <cstdint>
8#include <set>
9#include <utility>
10
11#include "testing/gmock/include/gmock/gmock.h"
12#include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h"
13#include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h"
14#include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h"
15#include "net/third_party/quiche/src/quic/core/quic_data_writer.h"
16#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
17#include "net/third_party/quiche/src/quic/core/quic_packets.h"
18#include "net/third_party/quiche/src/quic/core/quic_stream.h"
19#include "net/third_party/quiche/src/quic/core/quic_utils.h"
20#include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h"
21#include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h"
22#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
23#include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h"
24#include "net/third_party/quiche/src/quic/platform/api/quic_mem_slice_storage.h"
25#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
26#include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h"
27#include "net/third_party/quiche/src/quic/platform/api/quic_string.h"
28#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
29#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
30#include "net/third_party/quiche/src/quic/platform/api/quic_test_mem_slice_vector.h"
31#include "net/third_party/quiche/src/quic/test_tools/mock_quic_session_visitor.h"
32#include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h"
33#include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h"
34#include "net/third_party/quiche/src/quic/test_tools/quic_flow_controller_peer.h"
35#include "net/third_party/quiche/src/quic/test_tools/quic_session_peer.h"
36#include "net/third_party/quiche/src/quic/test_tools/quic_stream_peer.h"
37#include "net/third_party/quiche/src/quic/test_tools/quic_stream_send_buffer_peer.h"
38#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
39
40using spdy::kV3HighestPriority;
41using spdy::SpdyPriority;
42using testing::_;
43using testing::AtLeast;
44using testing::InSequence;
45using testing::Invoke;
46using testing::NiceMock;
47using testing::Return;
48using testing::StrictMock;
49using testing::WithArg;
50
51namespace quic {
52namespace test {
53namespace {
54
55class TestCryptoStream : public QuicCryptoStream, public QuicCryptoHandshaker {
56 public:
57 explicit TestCryptoStream(QuicSession* session)
58 : QuicCryptoStream(session),
59 QuicCryptoHandshaker(this, session),
60 encryption_established_(false),
61 handshake_confirmed_(false),
62 params_(new QuicCryptoNegotiatedParameters) {}
63
64 void OnHandshakeMessage(const CryptoHandshakeMessage& /*message*/) override {
65 encryption_established_ = true;
66 handshake_confirmed_ = true;
67 CryptoHandshakeMessage msg;
vasilvvc48c8712019-03-11 13:38:16 -070068 std::string error_details;
QUICHE teama6ef0a62019-03-07 20:34:33 -050069 session()->config()->SetInitialStreamFlowControlWindowToSend(
70 kInitialStreamFlowControlWindowForTest);
71 session()->config()->SetInitialSessionFlowControlWindowToSend(
72 kInitialSessionFlowControlWindowForTest);
73 session()->config()->ToHandshakeMessage(&msg);
74 const QuicErrorCode error =
75 session()->config()->ProcessPeerHello(msg, CLIENT, &error_details);
76 EXPECT_EQ(QUIC_NO_ERROR, error);
77 session()->OnConfigNegotiated();
78 session()->connection()->SetDefaultEncryptionLevel(
79 ENCRYPTION_FORWARD_SECURE);
80 session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED);
81 }
82
83 // QuicCryptoStream implementation
84 bool encryption_established() const override {
85 return encryption_established_;
86 }
87 bool handshake_confirmed() const override { return handshake_confirmed_; }
88 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
89 const override {
90 return *params_;
91 }
92 CryptoMessageParser* crypto_message_parser() override {
93 return QuicCryptoHandshaker::crypto_message_parser();
94 }
95
96 MOCK_METHOD0(OnCanWrite, void());
97 bool HasPendingCryptoRetransmission() override { return false; }
98
99 MOCK_CONST_METHOD0(HasPendingRetransmission, bool());
100
101 private:
102 using QuicCryptoStream::session;
103
104 bool encryption_established_;
105 bool handshake_confirmed_;
106 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> params_;
107};
108
109class TestStream : public QuicStream {
110 public:
111 TestStream(QuicStreamId id, QuicSession* session, StreamType type)
112 : QuicStream(id, session, /*is_static=*/false, type) {}
113
114 TestStream(PendingStream pending, StreamType type)
115 : QuicStream(std::move(pending), type) {}
116
117 using QuicStream::CloseReadSide;
118 using QuicStream::CloseWriteSide;
119 using QuicStream::WriteMemSlices;
120 using QuicStream::WritevData;
121
122 void OnDataAvailable() override {}
123
124 MOCK_METHOD0(OnCanWrite, void());
125 MOCK_METHOD3(RetransmitStreamData,
126 bool(QuicStreamOffset, QuicByteCount, bool));
127
128 MOCK_CONST_METHOD0(HasPendingRetransmission, bool());
129 MOCK_METHOD1(OnStopSending, void(uint16_t code));
130};
131
132class TestSession : public QuicSession {
133 public:
134 explicit TestSession(QuicConnection* connection,
135 MockQuicSessionVisitor* session_visitor)
136 : QuicSession(connection,
137 session_visitor,
138 DefaultQuicConfig(),
139 CurrentSupportedVersions()),
140 crypto_stream_(this),
141 writev_consumes_all_data_(false),
142 should_buffer_incoming_streams_(false),
143 num_incoming_streams_created_(0) {
144 Initialize();
145 this->connection()->SetEncrypter(
146 ENCRYPTION_FORWARD_SECURE,
147 QuicMakeUnique<NullEncrypter>(connection->perspective()));
148 }
149
150 ~TestSession() override { delete connection(); }
151
152 TestCryptoStream* GetMutableCryptoStream() override {
153 return &crypto_stream_;
154 }
155
156 const TestCryptoStream* GetCryptoStream() const override {
157 return &crypto_stream_;
158 }
159
160 TestStream* CreateOutgoingBidirectionalStream() {
161 QuicStreamId id = GetNextOutgoingBidirectionalStreamId();
162 if (id ==
163 QuicUtils::GetInvalidStreamId(connection()->transport_version())) {
164 return nullptr;
165 }
166 TestStream* stream = new TestStream(id, this, BIDIRECTIONAL);
167 ActivateStream(QuicWrapUnique(stream));
168 return stream;
169 }
170
171 TestStream* CreateOutgoingUnidirectionalStream() {
172 TestStream* stream = new TestStream(GetNextOutgoingUnidirectionalStreamId(),
173 this, WRITE_UNIDIRECTIONAL);
174 ActivateStream(QuicWrapUnique(stream));
175 return stream;
176 }
177
178 TestStream* CreateIncomingStream(QuicStreamId id) override {
179 // Enforce the limit on the number of open streams.
180 if (GetNumOpenIncomingStreams() + 1 >
181 max_open_incoming_bidirectional_streams() &&
182 connection()->transport_version() != QUIC_VERSION_99) {
183 // No need to do this test for version 99; it's done by
184 // QuicSession::GetOrCreateDynamicStream.
185 connection()->CloseConnection(
186 QUIC_TOO_MANY_OPEN_STREAMS, "Too many streams!",
187 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET);
188 return nullptr;
189 }
190
191 TestStream* stream =
192 new TestStream(id, this,
193 DetermineStreamType(
194 id, connection()->transport_version(), perspective(),
195 /*is_incoming=*/true, BIDIRECTIONAL));
196 ActivateStream(QuicWrapUnique(stream));
197 ++num_incoming_streams_created_;
198 return stream;
199 }
200
201 TestStream* CreateIncomingStream(PendingStream pending) override {
202 QuicStreamId id = pending.id();
203 TestStream* stream =
204 new TestStream(std::move(pending),
205 DetermineStreamType(
206 id, connection()->transport_version(), perspective(),
207 /*is_incoming=*/true, BIDIRECTIONAL));
208 ActivateStream(QuicWrapUnique(stream));
209 ++num_incoming_streams_created_;
210 return stream;
211 }
212
213 bool IsClosedStream(QuicStreamId id) {
214 return QuicSession::IsClosedStream(id);
215 }
216
217 QuicStream* GetOrCreateDynamicStream(QuicStreamId stream_id) {
218 return QuicSession::GetOrCreateDynamicStream(stream_id);
219 }
220
221 bool ShouldKeepConnectionAlive() const override {
222 return GetNumOpenDynamicStreams() > 0;
223 }
224
225 QuicConsumedData WritevData(QuicStream* stream,
226 QuicStreamId id,
227 size_t write_length,
228 QuicStreamOffset offset,
229 StreamSendingState state) override {
230 bool fin = state != NO_FIN;
231 QuicConsumedData consumed(write_length, fin);
232 if (!writev_consumes_all_data_) {
233 consumed =
234 QuicSession::WritevData(stream, id, write_length, offset, state);
235 }
236 if (fin && consumed.fin_consumed) {
237 stream->set_fin_sent(true);
238 }
239 QuicSessionPeer::GetWriteBlockedStreams(this)->UpdateBytesForStream(
240 id, consumed.bytes_consumed);
241 return consumed;
242 }
243
244 MOCK_METHOD0(OnCanCreateNewOutgoingStream, void());
245
246 void set_writev_consumes_all_data(bool val) {
247 writev_consumes_all_data_ = val;
248 }
249
250 QuicConsumedData SendStreamData(QuicStream* stream) {
251 struct iovec iov;
252 if (stream->id() !=
253 QuicUtils::GetCryptoStreamId(connection()->transport_version()) &&
254 this->connection()->encryption_level() != ENCRYPTION_FORWARD_SECURE) {
255 this->connection()->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE);
256 }
257 MakeIOVector("not empty", &iov);
258 QuicStreamPeer::SendBuffer(stream).SaveStreamData(&iov, 1, 0, 9);
259 QuicConsumedData consumed = WritevData(stream, stream->id(), 9, 0, FIN);
260 QuicStreamPeer::SendBuffer(stream).OnStreamDataConsumed(
261 consumed.bytes_consumed);
262 return consumed;
263 }
264
265 bool ClearControlFrame(const QuicFrame& frame) {
266 DeleteFrame(&const_cast<QuicFrame&>(frame));
267 return true;
268 }
269
270 bool SaveFrame(const QuicFrame& frame) {
271 save_frame_ = frame;
272 DeleteFrame(&const_cast<QuicFrame&>(frame));
273 return true;
274 }
275
276 const QuicFrame& save_frame() { return save_frame_; }
277
278 QuicConsumedData SendLargeFakeData(QuicStream* stream, int bytes) {
279 DCHECK(writev_consumes_all_data_);
280 return WritevData(stream, stream->id(), bytes, 0, FIN);
281 }
282
283 bool ShouldBufferIncomingStream(QuicStreamId id) const override {
284 return should_buffer_incoming_streams_;
285 }
286
287 void set_should_buffer_incoming_streams(bool should_buffer_incoming_streams) {
288 should_buffer_incoming_streams_ = should_buffer_incoming_streams;
289 }
290
291 int num_incoming_streams_created() const {
292 return num_incoming_streams_created_;
293 }
294
295 using QuicSession::ActivateStream;
296 using QuicSession::closed_streams;
297 using QuicSession::zombie_streams;
298
299 private:
300 StrictMock<TestCryptoStream> crypto_stream_;
301
302 bool writev_consumes_all_data_;
303 bool should_buffer_incoming_streams_;
304 QuicFrame save_frame_;
305 int num_incoming_streams_created_;
306};
307
308class QuicSessionTestBase : public QuicTestWithParam<ParsedQuicVersion> {
309 protected:
310 explicit QuicSessionTestBase(Perspective perspective)
311 : connection_(
312 new StrictMock<MockQuicConnection>(&helper_,
313 &alarm_factory_,
314 perspective,
315 SupportedVersions(GetParam()))),
316 session_(connection_, &session_visitor_) {
317 session_.config()->SetInitialStreamFlowControlWindowToSend(
318 kInitialStreamFlowControlWindowForTest);
319 session_.config()->SetInitialSessionFlowControlWindowToSend(
320 kInitialSessionFlowControlWindowForTest);
321 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
322 TestCryptoStream* crypto_stream = session_.GetMutableCryptoStream();
323 EXPECT_CALL(*crypto_stream, HasPendingRetransmission())
324 .Times(testing::AnyNumber());
325 }
326
327 void CheckClosedStreams() {
328 for (QuicStreamId i =
329 QuicUtils::GetCryptoStreamId(connection_->transport_version());
330 i < 100; i++) {
331 if (!QuicContainsKey(closed_streams_, i)) {
332 EXPECT_FALSE(session_.IsClosedStream(i)) << " stream id: " << i;
333 } else {
334 EXPECT_TRUE(session_.IsClosedStream(i)) << " stream id: " << i;
335 }
336 }
337 }
338
339 void CloseStream(QuicStreamId id) {
340 if (session_.connection()->transport_version() == QUIC_VERSION_99 &&
341 QuicUtils::GetStreamType(id, session_.perspective(),
342 session_.IsIncomingStream(id)) ==
343 READ_UNIDIRECTIONAL) {
344 // Verify reset is not sent for READ_UNIDIRECTIONAL streams.
345 EXPECT_CALL(*connection_, SendControlFrame(_)).Times(0);
346 EXPECT_CALL(*connection_, OnStreamReset(_, _)).Times(0);
347 } else {
348 // Verify reset IS sent for BIDIRECTIONAL streams.
349 if (session_.connection()->transport_version() == QUIC_VERSION_99) {
350 // Once for the RST_STREAM, Once for the STOP_SENDING
351 EXPECT_CALL(*connection_, SendControlFrame(_))
352 .Times(2)
353 .WillRepeatedly(Invoke(&session_, &TestSession::ClearControlFrame));
354 } else {
355 EXPECT_CALL(*connection_, SendControlFrame(_))
356 .WillOnce(Invoke(&session_, &TestSession::ClearControlFrame));
357 }
358 EXPECT_CALL(*connection_, OnStreamReset(id, _));
359 }
360 session_.CloseStream(id);
361 closed_streams_.insert(id);
362 }
363
364 QuicTransportVersion transport_version() const {
365 return connection_->transport_version();
366 }
367
368 QuicStreamId GetNthClientInitiatedBidirectionalId(int n) {
369 return QuicUtils::GetFirstBidirectionalStreamId(
370 connection_->transport_version(), Perspective::IS_CLIENT) +
371 QuicUtils::StreamIdDelta(connection_->transport_version()) * n;
372 }
373
374 QuicStreamId GetNthClientInitiatedUnidirectionalId(int n) {
375 return QuicUtils::GetFirstUnidirectionalStreamId(
376 connection_->transport_version(), Perspective::IS_CLIENT) +
377 QuicUtils::StreamIdDelta(connection_->transport_version()) * n;
378 }
379
380 QuicStreamId GetNthServerInitiatedBidirectionalId(int n) {
381 return QuicUtils::GetFirstBidirectionalStreamId(
382 connection_->transport_version(), Perspective::IS_SERVER) +
383 QuicUtils::StreamIdDelta(connection_->transport_version()) * n;
384 }
385
386 QuicStreamId GetNthServerInitiatedUnidirectionalId(int n) {
387 return QuicUtils::GetFirstUnidirectionalStreamId(
388 connection_->transport_version(), Perspective::IS_SERVER) +
389 QuicUtils::StreamIdDelta(connection_->transport_version()) * n;
390 }
391
392 MockQuicConnectionHelper helper_;
393 MockAlarmFactory alarm_factory_;
394 NiceMock<MockQuicSessionVisitor> session_visitor_;
395 StrictMock<MockQuicConnection>* connection_;
396 TestSession session_;
397 std::set<QuicStreamId> closed_streams_;
398};
399
400class QuicSessionTestServer : public QuicSessionTestBase {
401 public:
402 // CheckMultiPathResponse validates that a written packet
403 // contains both expected path responses.
404 WriteResult CheckMultiPathResponse(const char* buffer,
405 size_t buf_len,
406 const QuicIpAddress& self_address,
407 const QuicSocketAddress& peer_address,
408 PerPacketOptions* options) {
409 QuicEncryptedPacket packet(buffer, buf_len);
410 {
411 InSequence s;
412 EXPECT_CALL(framer_visitor_, OnPacket());
413 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
414 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
415 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
416 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
417 EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_))
418 .WillOnce(
419 WithArg<0>(Invoke([this](const QuicPathResponseFrame& frame) {
420 EXPECT_EQ(path_frame_buffer1_, frame.data_buffer);
421 return true;
422 })));
423 EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_))
424 .WillOnce(
425 WithArg<0>(Invoke([this](const QuicPathResponseFrame& frame) {
426 EXPECT_EQ(path_frame_buffer2_, frame.data_buffer);
427 return true;
428 })));
429 EXPECT_CALL(framer_visitor_, OnPacketComplete());
430 }
431 client_framer_.ProcessPacket(packet);
432 return WriteResult(WRITE_STATUS_OK, 0);
433 }
434
435 protected:
436 QuicSessionTestServer()
437 : QuicSessionTestBase(Perspective::IS_SERVER),
438 path_frame_buffer1_({0, 1, 2, 3, 4, 5, 6, 7}),
439 path_frame_buffer2_({8, 9, 10, 11, 12, 13, 14, 15}),
440 client_framer_(SupportedVersions(GetParam()),
441 QuicTime::Zero(),
442 Perspective::IS_CLIENT,
443 kQuicDefaultConnectionIdLength) {
444 client_framer_.set_visitor(&framer_visitor_);
445 }
446
447 QuicPathFrameBuffer path_frame_buffer1_;
448 QuicPathFrameBuffer path_frame_buffer2_;
449 StrictMock<MockFramerVisitor> framer_visitor_;
450 // Framer used to process packets sent by server.
451 QuicFramer client_framer_;
452};
453
454INSTANTIATE_TEST_SUITE_P(Tests,
455 QuicSessionTestServer,
456 ::testing::ValuesIn(AllSupportedVersions()));
457
458TEST_P(QuicSessionTestServer, PeerAddress) {
459 EXPECT_EQ(QuicSocketAddress(QuicIpAddress::Loopback4(), kTestPort),
460 session_.peer_address());
461}
462
463TEST_P(QuicSessionTestServer, SelfAddress) {
464 EXPECT_TRUE(session_.self_address().IsInitialized());
465}
466
467TEST_P(QuicSessionTestServer, DontCallOnWriteBlockedForDisconnectedConnection) {
468 EXPECT_CALL(*connection_, CloseConnection(_, _, _))
469 .WillOnce(
470 Invoke(connection_, &MockQuicConnection::ReallyCloseConnection));
471 connection_->CloseConnection(QUIC_NO_ERROR, "Everything is fine.",
472 ConnectionCloseBehavior::SILENT_CLOSE);
473 ASSERT_FALSE(connection_->connected());
474
475 if (GetQuicReloadableFlag(
476 quic_connection_do_not_add_to_write_blocked_list_if_disconnected)) {
477 EXPECT_CALL(session_visitor_, OnWriteBlocked(_)).Times(0);
478 } else {
479 EXPECT_CALL(session_visitor_, OnWriteBlocked(_)).Times(1);
480 }
481 session_.OnWriteBlocked();
482}
483
484TEST_P(QuicSessionTestServer, IsCryptoHandshakeConfirmed) {
485 EXPECT_FALSE(session_.IsCryptoHandshakeConfirmed());
486 CryptoHandshakeMessage message;
487 session_.GetMutableCryptoStream()->OnHandshakeMessage(message);
488 EXPECT_TRUE(session_.IsCryptoHandshakeConfirmed());
489}
490
491TEST_P(QuicSessionTestServer, IsClosedStreamDefault) {
492 // Ensure that no streams are initially closed.
493 for (QuicStreamId i =
494 QuicUtils::GetCryptoStreamId(connection_->transport_version());
495 i < 100; i++) {
496 EXPECT_FALSE(session_.IsClosedStream(i)) << "stream id: " << i;
497 }
498}
499
500TEST_P(QuicSessionTestServer, AvailableBidirectionalStreams) {
501 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
502 GetNthClientInitiatedBidirectionalId(3)) != nullptr);
503 // Smaller bidirectional streams should be available.
504 EXPECT_TRUE(QuicSessionPeer::IsStreamAvailable(
505 &session_, GetNthClientInitiatedBidirectionalId(1)));
506 EXPECT_TRUE(QuicSessionPeer::IsStreamAvailable(
507 &session_, GetNthClientInitiatedBidirectionalId(2)));
508 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
509 GetNthClientInitiatedBidirectionalId(2)) != nullptr);
510 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
511 GetNthClientInitiatedBidirectionalId(1)) != nullptr);
512}
513
514TEST_P(QuicSessionTestServer, AvailableUnidirectionalStreams) {
515 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
516 GetNthClientInitiatedUnidirectionalId(3)) != nullptr);
517 // Smaller unidirectional streams should be available.
518 EXPECT_TRUE(QuicSessionPeer::IsStreamAvailable(
519 &session_, GetNthClientInitiatedUnidirectionalId(1)));
520 EXPECT_TRUE(QuicSessionPeer::IsStreamAvailable(
521 &session_, GetNthClientInitiatedUnidirectionalId(2)));
522 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
523 GetNthClientInitiatedUnidirectionalId(2)) != nullptr);
524 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
525 GetNthClientInitiatedUnidirectionalId(1)) != nullptr);
526}
527
528TEST_P(QuicSessionTestServer, MaxAvailableBidirectionalStreams) {
529 if (transport_version() == QUIC_VERSION_99) {
530 EXPECT_EQ(session_.max_open_incoming_bidirectional_streams(),
531 session_.MaxAvailableBidirectionalStreams());
532 } else {
533 // The protocol specification requires that there can be at least 10 times
534 // as many available streams as the connection's maximum open streams.
535 EXPECT_EQ(session_.max_open_incoming_bidirectional_streams() *
536 kMaxAvailableStreamsMultiplier,
537 session_.MaxAvailableBidirectionalStreams());
538 }
539}
540
541TEST_P(QuicSessionTestServer, MaxAvailableUnidirectionalStreams) {
542 if (transport_version() == QUIC_VERSION_99) {
543 EXPECT_EQ(session_.max_open_incoming_unidirectional_streams(),
544 session_.MaxAvailableUnidirectionalStreams());
545 } else {
546 // The protocol specification requires that there can be at least 10 times
547 // as many available streams as the connection's maximum open streams.
548 EXPECT_EQ(session_.max_open_incoming_unidirectional_streams() *
549 kMaxAvailableStreamsMultiplier,
550 session_.MaxAvailableUnidirectionalStreams());
551 }
552}
553
554TEST_P(QuicSessionTestServer, IsClosedBidirectionalStreamLocallyCreated) {
555 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
556 EXPECT_EQ(GetNthServerInitiatedBidirectionalId(0), stream2->id());
557 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
558 EXPECT_EQ(GetNthServerInitiatedBidirectionalId(1), stream4->id());
559
560 CheckClosedStreams();
561 CloseStream(GetNthServerInitiatedBidirectionalId(0));
562 CheckClosedStreams();
563 CloseStream(GetNthServerInitiatedBidirectionalId(1));
564 CheckClosedStreams();
565}
566
567TEST_P(QuicSessionTestServer, IsClosedUnidirectionalStreamLocallyCreated) {
568 TestStream* stream2 = session_.CreateOutgoingUnidirectionalStream();
569 EXPECT_EQ(GetNthServerInitiatedUnidirectionalId(0), stream2->id());
570 TestStream* stream4 = session_.CreateOutgoingUnidirectionalStream();
571 EXPECT_EQ(GetNthServerInitiatedUnidirectionalId(1), stream4->id());
572
573 CheckClosedStreams();
574 CloseStream(GetNthServerInitiatedUnidirectionalId(0));
575 CheckClosedStreams();
576 CloseStream(GetNthServerInitiatedUnidirectionalId(1));
577 CheckClosedStreams();
578}
579
580TEST_P(QuicSessionTestServer, IsClosedBidirectionalStreamPeerCreated) {
581 QuicStreamId stream_id1 = GetNthClientInitiatedBidirectionalId(0);
582 QuicStreamId stream_id2 = GetNthClientInitiatedBidirectionalId(1);
583 session_.GetOrCreateDynamicStream(stream_id1);
584 session_.GetOrCreateDynamicStream(stream_id2);
585
586 CheckClosedStreams();
587 CloseStream(stream_id1);
588 CheckClosedStreams();
589 CloseStream(stream_id2);
590 // Create a stream, and make another available.
591 QuicStream* stream3 = session_.GetOrCreateDynamicStream(
592 stream_id2 +
593 2 * QuicUtils::StreamIdDelta(connection_->transport_version()));
594 CheckClosedStreams();
595 // Close one, but make sure the other is still not closed
596 CloseStream(stream3->id());
597 CheckClosedStreams();
598}
599
600TEST_P(QuicSessionTestServer, IsClosedUnidirectionalStreamPeerCreated) {
601 QuicStreamId stream_id1 = GetNthClientInitiatedUnidirectionalId(0);
602 QuicStreamId stream_id2 = GetNthClientInitiatedUnidirectionalId(1);
603 session_.GetOrCreateDynamicStream(stream_id1);
604 session_.GetOrCreateDynamicStream(stream_id2);
605
606 CheckClosedStreams();
607 CloseStream(stream_id1);
608 CheckClosedStreams();
609 CloseStream(stream_id2);
610 // Create a stream, and make another available.
611 QuicStream* stream3 = session_.GetOrCreateDynamicStream(
612 stream_id2 +
613 2 * QuicUtils::StreamIdDelta(connection_->transport_version()));
614 CheckClosedStreams();
615 // Close one, but make sure the other is still not closed
616 CloseStream(stream3->id());
617 CheckClosedStreams();
618}
619
620TEST_P(QuicSessionTestServer, MaximumAvailableOpenedBidirectionalStreams) {
621 QuicStreamId stream_id = GetNthClientInitiatedBidirectionalId(0);
622 session_.GetOrCreateDynamicStream(stream_id);
623 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
624 EXPECT_NE(
625 nullptr,
626 session_.GetOrCreateDynamicStream(GetNthClientInitiatedBidirectionalId(
627 session_.max_open_incoming_bidirectional_streams() - 1)));
628}
629
630TEST_P(QuicSessionTestServer, MaximumAvailableOpenedUnidirectionalStreams) {
631 QuicStreamId stream_id = GetNthClientInitiatedUnidirectionalId(0);
632 session_.GetOrCreateDynamicStream(stream_id);
633 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
634 EXPECT_NE(
635 nullptr,
636 session_.GetOrCreateDynamicStream(GetNthClientInitiatedUnidirectionalId(
637 session_.max_open_incoming_unidirectional_streams() - 1)));
638}
639
640TEST_P(QuicSessionTestServer, TooManyAvailableBidirectionalStreams) {
641 QuicStreamId stream_id1 = GetNthClientInitiatedBidirectionalId(0);
642 QuicStreamId stream_id2;
643 EXPECT_NE(nullptr, session_.GetOrCreateDynamicStream(stream_id1));
644 // A stream ID which is too large to create.
645 stream_id2 = GetNthClientInitiatedBidirectionalId(
646 session_.MaxAvailableBidirectionalStreams() + 2);
647 if (transport_version() == QUIC_VERSION_99) {
648 // V99 terminates the connection with invalid stream id
649 EXPECT_CALL(*connection_, CloseConnection(QUIC_INVALID_STREAM_ID, _, _));
650 } else {
651 // other versions terminate the connection with
652 // QUIC_TOO_MANY_AVAILABLE_STREAMS.
653 EXPECT_CALL(*connection_,
654 CloseConnection(QUIC_TOO_MANY_AVAILABLE_STREAMS, _, _));
655 }
656 EXPECT_EQ(nullptr, session_.GetOrCreateDynamicStream(stream_id2));
657}
658
659TEST_P(QuicSessionTestServer, TooManyAvailableUnidirectionalStreams) {
660 QuicStreamId stream_id1 = GetNthClientInitiatedUnidirectionalId(0);
661 QuicStreamId stream_id2;
662 EXPECT_NE(nullptr, session_.GetOrCreateDynamicStream(stream_id1));
663 // A stream ID which is too large to create.
664 stream_id2 = GetNthClientInitiatedUnidirectionalId(
665 session_.MaxAvailableUnidirectionalStreams() + 2);
666 if (transport_version() == QUIC_VERSION_99) {
667 // V99 terminates the connection with invalid stream id
668 EXPECT_CALL(*connection_, CloseConnection(QUIC_INVALID_STREAM_ID, _, _));
669 } else {
670 // other versions terminate the connection with
671 // QUIC_TOO_MANY_AVAILABLE_STREAMS.
672 EXPECT_CALL(*connection_,
673 CloseConnection(QUIC_TOO_MANY_AVAILABLE_STREAMS, _, _));
674 }
675 EXPECT_EQ(nullptr, session_.GetOrCreateDynamicStream(stream_id2));
676}
677
678TEST_P(QuicSessionTestServer, ManyAvailableBidirectionalStreams) {
679 // When max_open_streams_ is 200, should be able to create 200 streams
680 // out-of-order, that is, creating the one with the largest stream ID first.
681 QuicSessionPeer::SetMaxOpenIncomingStreams(&session_, 200);
682 QuicStreamId stream_id = GetNthClientInitiatedBidirectionalId(0);
683 // Create one stream.
684 EXPECT_NE(nullptr, session_.GetOrCreateDynamicStream(stream_id));
685 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
686
687 // Create the largest stream ID of a threatened total of 200 streams.
688 // GetNth... starts at 0, so for 200 streams, get the 199th.
689 EXPECT_NE(nullptr, session_.GetOrCreateDynamicStream(
690 GetNthClientInitiatedBidirectionalId(199)));
691}
692
693TEST_P(QuicSessionTestServer, ManyAvailableUnidirectionalStreams) {
694 // When max_open_streams_ is 200, should be able to create 200 streams
695 // out-of-order, that is, creating the one with the largest stream ID first.
696 QuicSessionPeer::SetMaxOpenIncomingStreams(&session_, 200);
697 QuicStreamId stream_id = GetNthClientInitiatedUnidirectionalId(0);
698 // Create one stream.
699 EXPECT_NE(nullptr, session_.GetOrCreateDynamicStream(stream_id));
700 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
701
702 // Create the largest stream ID of a threatened total of 200 streams.
703 // GetNth... starts at 0, so for 200 streams, get the 199th.
704 EXPECT_NE(nullptr, session_.GetOrCreateDynamicStream(
705 GetNthClientInitiatedUnidirectionalId(199)));
706}
707
708TEST_P(QuicSessionTestServer, DebugDFatalIfMarkingClosedStreamWriteBlocked) {
709 // EXPECT_QUIC_BUG tests are expensive so only run one instance of them.
710 if (GetParam() != AllSupportedVersions()[0]) {
711 return;
712 }
713
714 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
715 QuicStreamId closed_stream_id = stream2->id();
716 // Close the stream.
717 EXPECT_CALL(*connection_, SendControlFrame(_));
718 EXPECT_CALL(*connection_, OnStreamReset(closed_stream_id, _));
719 stream2->Reset(QUIC_BAD_APPLICATION_PAYLOAD);
vasilvvc48c8712019-03-11 13:38:16 -0700720 std::string msg =
QUICHE teama6ef0a62019-03-07 20:34:33 -0500721 QuicStrCat("Marking unknown stream ", closed_stream_id, " blocked.");
722 EXPECT_QUIC_BUG(session_.MarkConnectionLevelWriteBlocked(closed_stream_id),
723 msg);
724}
725
726TEST_P(QuicSessionTestServer, OnCanWrite) {
727 session_.set_writev_consumes_all_data(true);
728 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
729 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
730 TestStream* stream6 = session_.CreateOutgoingBidirectionalStream();
731
732 session_.MarkConnectionLevelWriteBlocked(stream2->id());
733 session_.MarkConnectionLevelWriteBlocked(stream6->id());
734 session_.MarkConnectionLevelWriteBlocked(stream4->id());
735
736 InSequence s;
737
738 // Reregister, to test the loop limit.
739 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(Invoke([this, stream2]() {
740 session_.SendStreamData(stream2);
741 session_.MarkConnectionLevelWriteBlocked(stream2->id());
742 }));
743 // 2 will get called a second time as it didn't finish its block
744 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(Invoke([this, stream2]() {
745 session_.SendStreamData(stream2);
746 }));
747 EXPECT_CALL(*stream6, OnCanWrite()).WillOnce(Invoke([this, stream6]() {
748 session_.SendStreamData(stream6);
749 }));
750 // 4 will not get called, as we exceeded the loop limit.
751 session_.OnCanWrite();
752 EXPECT_TRUE(session_.WillingAndAbleToWrite());
753}
754
755TEST_P(QuicSessionTestServer, TestBatchedWrites) {
756 session_.set_writev_consumes_all_data(true);
757 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
758 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
759 TestStream* stream6 = session_.CreateOutgoingBidirectionalStream();
760
761 session_.set_writev_consumes_all_data(true);
762 session_.MarkConnectionLevelWriteBlocked(stream2->id());
763 session_.MarkConnectionLevelWriteBlocked(stream4->id());
764
765 // With two sessions blocked, we should get two write calls. They should both
766 // go to the first stream as it will only write 6k and mark itself blocked
767 // again.
768 InSequence s;
769 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(Invoke([this, stream2]() {
770 session_.SendLargeFakeData(stream2, 6000);
771 session_.MarkConnectionLevelWriteBlocked(stream2->id());
772 }));
773 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(Invoke([this, stream2]() {
774 session_.SendLargeFakeData(stream2, 6000);
775 session_.MarkConnectionLevelWriteBlocked(stream2->id());
776 }));
777 session_.OnCanWrite();
778
779 // We should get one more call for stream2, at which point it has used its
780 // write quota and we move over to stream 4.
781 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(Invoke([this, stream2]() {
782 session_.SendLargeFakeData(stream2, 6000);
783 session_.MarkConnectionLevelWriteBlocked(stream2->id());
784 }));
785 EXPECT_CALL(*stream4, OnCanWrite()).WillOnce(Invoke([this, stream4]() {
786 session_.SendLargeFakeData(stream4, 6000);
787 session_.MarkConnectionLevelWriteBlocked(stream4->id());
788 }));
789 session_.OnCanWrite();
790
791 // Now let stream 4 do the 2nd of its 3 writes, but add a block for a high
792 // priority stream 6. 4 should be preempted. 6 will write but *not* block so
793 // will cede back to 4.
794 stream6->SetPriority(kV3HighestPriority);
795 EXPECT_CALL(*stream4, OnCanWrite())
796 .WillOnce(Invoke([this, stream4, stream6]() {
797 session_.SendLargeFakeData(stream4, 6000);
798 session_.MarkConnectionLevelWriteBlocked(stream4->id());
799 session_.MarkConnectionLevelWriteBlocked(stream6->id());
800 }));
801 EXPECT_CALL(*stream6, OnCanWrite())
802 .WillOnce(Invoke([this, stream4, stream6]() {
803 session_.SendStreamData(stream6);
804 session_.SendLargeFakeData(stream4, 6000);
805 }));
806 session_.OnCanWrite();
807
808 // Stream4 alread did 6k worth of writes, so after doing another 12k it should
809 // cede and 2 should resume.
810 EXPECT_CALL(*stream4, OnCanWrite()).WillOnce(Invoke([this, stream4]() {
811 session_.SendLargeFakeData(stream4, 12000);
812 session_.MarkConnectionLevelWriteBlocked(stream4->id());
813 }));
814 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(Invoke([this, stream2]() {
815 session_.SendLargeFakeData(stream2, 6000);
816 session_.MarkConnectionLevelWriteBlocked(stream2->id());
817 }));
818 session_.OnCanWrite();
819}
820
821TEST_P(QuicSessionTestServer, OnCanWriteBundlesStreams) {
822 // Encryption needs to be established before data can be sent.
823 CryptoHandshakeMessage msg;
824 MockPacketWriter* writer = static_cast<MockPacketWriter*>(
825 QuicConnectionPeer::GetWriter(session_.connection()));
826 session_.GetMutableCryptoStream()->OnHandshakeMessage(msg);
827
828 // Drive congestion control manually.
829 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>;
830 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm);
831
832 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
833 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
834 TestStream* stream6 = session_.CreateOutgoingBidirectionalStream();
835
836 session_.MarkConnectionLevelWriteBlocked(stream2->id());
837 session_.MarkConnectionLevelWriteBlocked(stream6->id());
838 session_.MarkConnectionLevelWriteBlocked(stream4->id());
839
840 EXPECT_CALL(*send_algorithm, CanSend(_)).WillRepeatedly(Return(true));
841 EXPECT_CALL(*send_algorithm, GetCongestionWindow())
842 .WillRepeatedly(Return(kMaxPacketSize * 10));
843 EXPECT_CALL(*send_algorithm, InRecovery()).WillRepeatedly(Return(false));
844 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(Invoke([this, stream2]() {
845 session_.SendStreamData(stream2);
846 }));
847 EXPECT_CALL(*stream4, OnCanWrite()).WillOnce(Invoke([this, stream4]() {
848 session_.SendStreamData(stream4);
849 }));
850 EXPECT_CALL(*stream6, OnCanWrite()).WillOnce(Invoke([this, stream6]() {
851 session_.SendStreamData(stream6);
852 }));
853
854 // Expect that we only send one packet, the writes from different streams
855 // should be bundled together.
856 EXPECT_CALL(*writer, WritePacket(_, _, _, _, _))
857 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 0)));
858 EXPECT_CALL(*send_algorithm, OnPacketSent(_, _, _, _, _));
859 EXPECT_CALL(*send_algorithm, OnApplicationLimited(_));
860 session_.OnCanWrite();
861 EXPECT_FALSE(session_.WillingAndAbleToWrite());
862}
863
864TEST_P(QuicSessionTestServer, OnCanWriteCongestionControlBlocks) {
865 session_.set_writev_consumes_all_data(true);
866 InSequence s;
867
868 // Drive congestion control manually.
869 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>;
870 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm);
871
872 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
873 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
874 TestStream* stream6 = session_.CreateOutgoingBidirectionalStream();
875
876 session_.MarkConnectionLevelWriteBlocked(stream2->id());
877 session_.MarkConnectionLevelWriteBlocked(stream6->id());
878 session_.MarkConnectionLevelWriteBlocked(stream4->id());
879
880 EXPECT_CALL(*send_algorithm, CanSend(_)).WillOnce(Return(true));
881 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(Invoke([this, stream2]() {
882 session_.SendStreamData(stream2);
883 }));
884 EXPECT_CALL(*send_algorithm, CanSend(_)).WillOnce(Return(true));
885 EXPECT_CALL(*stream6, OnCanWrite()).WillOnce(Invoke([this, stream6]() {
886 session_.SendStreamData(stream6);
887 }));
888 EXPECT_CALL(*send_algorithm, CanSend(_)).WillOnce(Return(false));
889 // stream4->OnCanWrite is not called.
890
891 session_.OnCanWrite();
892 EXPECT_TRUE(session_.WillingAndAbleToWrite());
893
894 // Still congestion-control blocked.
895 EXPECT_CALL(*send_algorithm, CanSend(_)).WillOnce(Return(false));
896 session_.OnCanWrite();
897 EXPECT_TRUE(session_.WillingAndAbleToWrite());
898
899 // stream4->OnCanWrite is called once the connection stops being
900 // congestion-control blocked.
901 EXPECT_CALL(*send_algorithm, CanSend(_)).WillOnce(Return(true));
902 EXPECT_CALL(*stream4, OnCanWrite()).WillOnce(Invoke([this, stream4]() {
903 session_.SendStreamData(stream4);
904 }));
905 EXPECT_CALL(*send_algorithm, OnApplicationLimited(_));
906 session_.OnCanWrite();
907 EXPECT_FALSE(session_.WillingAndAbleToWrite());
908}
909
910TEST_P(QuicSessionTestServer, OnCanWriteWriterBlocks) {
911 // Drive congestion control manually in order to ensure that
912 // application-limited signaling is handled correctly.
913 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>;
914 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm);
915 EXPECT_CALL(*send_algorithm, CanSend(_)).WillRepeatedly(Return(true));
916
917 // Drive packet writer manually.
918 MockPacketWriter* writer = static_cast<MockPacketWriter*>(
919 QuicConnectionPeer::GetWriter(session_.connection()));
920 EXPECT_CALL(*writer, IsWriteBlocked()).WillRepeatedly(Return(true));
921 EXPECT_CALL(*writer, WritePacket(_, _, _, _, _)).Times(0);
922
923 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
924
925 session_.MarkConnectionLevelWriteBlocked(stream2->id());
926
927 EXPECT_CALL(*stream2, OnCanWrite()).Times(0);
928 EXPECT_CALL(*send_algorithm, OnApplicationLimited(_)).Times(0);
929
930 session_.OnCanWrite();
931 EXPECT_TRUE(session_.WillingAndAbleToWrite());
932}
933
934TEST_P(QuicSessionTestServer, BufferedHandshake) {
935 session_.set_writev_consumes_all_data(true);
936 EXPECT_FALSE(session_.HasPendingHandshake()); // Default value.
937
938 // Test that blocking other streams does not change our status.
939 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
940 session_.MarkConnectionLevelWriteBlocked(stream2->id());
941 EXPECT_FALSE(session_.HasPendingHandshake());
942
943 TestStream* stream3 = session_.CreateOutgoingBidirectionalStream();
944 session_.MarkConnectionLevelWriteBlocked(stream3->id());
945 EXPECT_FALSE(session_.HasPendingHandshake());
946
947 // Blocking (due to buffering of) the Crypto stream is detected.
948 session_.MarkConnectionLevelWriteBlocked(
949 QuicUtils::GetCryptoStreamId(connection_->transport_version()));
950 EXPECT_TRUE(session_.HasPendingHandshake());
951
952 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
953 session_.MarkConnectionLevelWriteBlocked(stream4->id());
954 EXPECT_TRUE(session_.HasPendingHandshake());
955
956 InSequence s;
957 // Force most streams to re-register, which is common scenario when we block
958 // the Crypto stream, and only the crypto stream can "really" write.
959
960 // Due to prioritization, we *should* be asked to write the crypto stream
961 // first.
962 // Don't re-register the crypto stream (which signals complete writing).
963 TestCryptoStream* crypto_stream = session_.GetMutableCryptoStream();
964 EXPECT_CALL(*crypto_stream, OnCanWrite());
965
966 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(Invoke([this, stream2]() {
967 session_.SendStreamData(stream2);
968 }));
969 EXPECT_CALL(*stream3, OnCanWrite()).WillOnce(Invoke([this, stream3]() {
970 session_.SendStreamData(stream3);
971 }));
972 EXPECT_CALL(*stream4, OnCanWrite()).WillOnce(Invoke([this, stream4]() {
973 session_.SendStreamData(stream4);
974 session_.MarkConnectionLevelWriteBlocked(stream4->id());
975 }));
976
977 session_.OnCanWrite();
978 EXPECT_TRUE(session_.WillingAndAbleToWrite());
979 EXPECT_FALSE(session_.HasPendingHandshake()); // Crypto stream wrote.
980}
981
982TEST_P(QuicSessionTestServer, OnCanWriteWithClosedStream) {
983 session_.set_writev_consumes_all_data(true);
984 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
985 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
986 TestStream* stream6 = session_.CreateOutgoingBidirectionalStream();
987
988 session_.MarkConnectionLevelWriteBlocked(stream2->id());
989 session_.MarkConnectionLevelWriteBlocked(stream6->id());
990 session_.MarkConnectionLevelWriteBlocked(stream4->id());
991 CloseStream(stream6->id());
992
993 InSequence s;
994 EXPECT_CALL(*connection_, SendControlFrame(_))
995 .WillRepeatedly(Invoke(&session_, &TestSession::ClearControlFrame));
996 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(Invoke([this, stream2]() {
997 session_.SendStreamData(stream2);
998 }));
999 EXPECT_CALL(*stream4, OnCanWrite()).WillOnce(Invoke([this, stream4]() {
1000 session_.SendStreamData(stream4);
1001 }));
1002 session_.OnCanWrite();
1003 EXPECT_FALSE(session_.WillingAndAbleToWrite());
1004}
1005
1006TEST_P(QuicSessionTestServer, OnCanWriteLimitsNumWritesIfFlowControlBlocked) {
1007 // Drive congestion control manually in order to ensure that
1008 // application-limited signaling is handled correctly.
1009 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>;
1010 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm);
1011 EXPECT_CALL(*send_algorithm, CanSend(_)).WillRepeatedly(Return(true));
1012
1013 // Ensure connection level flow control blockage.
1014 QuicFlowControllerPeer::SetSendWindowOffset(session_.flow_controller(), 0);
1015 EXPECT_TRUE(session_.flow_controller()->IsBlocked());
1016 EXPECT_TRUE(session_.IsConnectionFlowControlBlocked());
1017 EXPECT_FALSE(session_.IsStreamFlowControlBlocked());
1018
1019 // Mark the crypto and headers streams as write blocked, we expect them to be
1020 // allowed to write later.
1021 session_.MarkConnectionLevelWriteBlocked(
1022 QuicUtils::GetCryptoStreamId(connection_->transport_version()));
1023
1024 // Create a data stream, and although it is write blocked we never expect it
1025 // to be allowed to write as we are connection level flow control blocked.
1026 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
1027 session_.MarkConnectionLevelWriteBlocked(stream->id());
1028 EXPECT_CALL(*stream, OnCanWrite()).Times(0);
1029
1030 // The crypto and headers streams should be called even though we are
1031 // connection flow control blocked.
1032 TestCryptoStream* crypto_stream = session_.GetMutableCryptoStream();
1033 EXPECT_CALL(*crypto_stream, OnCanWrite());
1034
1035 // After the crypto and header streams perform a write, the connection will be
1036 // blocked by the flow control, hence it should become application-limited.
1037 EXPECT_CALL(*send_algorithm, OnApplicationLimited(_));
1038
1039 session_.OnCanWrite();
1040 EXPECT_FALSE(session_.WillingAndAbleToWrite());
1041}
1042
1043TEST_P(QuicSessionTestServer, SendGoAway) {
1044 if (transport_version() == QUIC_VERSION_99) {
1045 // GoAway frames are not in version 99
1046 return;
1047 }
1048 MockPacketWriter* writer = static_cast<MockPacketWriter*>(
1049 QuicConnectionPeer::GetWriter(session_.connection()));
1050 EXPECT_CALL(*writer, WritePacket(_, _, _, _, _))
1051 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 0)));
1052
1053 EXPECT_CALL(*connection_, SendControlFrame(_))
1054 .WillOnce(
1055 Invoke(connection_, &MockQuicConnection::ReallySendControlFrame));
1056 session_.SendGoAway(QUIC_PEER_GOING_AWAY, "Going Away.");
1057 EXPECT_TRUE(session_.goaway_sent());
1058
1059 const QuicStreamId kTestStreamId = 5u;
1060 EXPECT_CALL(*connection_, SendControlFrame(_)).Times(0);
1061 EXPECT_CALL(*connection_,
1062 OnStreamReset(kTestStreamId, QUIC_STREAM_PEER_GOING_AWAY))
1063 .Times(0);
1064 EXPECT_TRUE(session_.GetOrCreateDynamicStream(kTestStreamId));
1065}
1066
1067TEST_P(QuicSessionTestServer, DoNotSendGoAwayTwice) {
1068 if (transport_version() == QUIC_VERSION_99) {
1069 // TODO(b/118808809): Enable this test for version 99 when GOAWAY is
1070 // supported.
1071 return;
1072 }
1073 EXPECT_CALL(*connection_, SendControlFrame(_))
1074 .WillOnce(Invoke(&session_, &TestSession::ClearControlFrame));
1075 session_.SendGoAway(QUIC_PEER_GOING_AWAY, "Going Away.");
1076 EXPECT_TRUE(session_.goaway_sent());
1077 session_.SendGoAway(QUIC_PEER_GOING_AWAY, "Going Away.");
1078}
1079
1080TEST_P(QuicSessionTestServer, InvalidGoAway) {
1081 if (transport_version() == QUIC_VERSION_99) {
1082 // TODO(b/118808809): Enable this test for version 99 when GOAWAY is
1083 // supported.
1084 return;
1085 }
1086 QuicGoAwayFrame go_away(kInvalidControlFrameId, QUIC_PEER_GOING_AWAY,
1087 session_.next_outgoing_bidirectional_stream_id(), "");
1088 session_.OnGoAway(go_away);
1089}
1090
1091// Test that server session will send a connectivity probe in response to a
1092// connectivity probe on the same path.
1093TEST_P(QuicSessionTestServer, ServerReplyToConnectivityProbe) {
1094 QuicSocketAddress old_peer_address =
1095 QuicSocketAddress(QuicIpAddress::Loopback4(), kTestPort);
1096 EXPECT_EQ(old_peer_address, session_.peer_address());
1097
1098 QuicSocketAddress new_peer_address =
1099 QuicSocketAddress(QuicIpAddress::Loopback4(), kTestPort + 1);
1100
1101 MockPacketWriter* writer = static_cast<MockPacketWriter*>(
1102 QuicConnectionPeer::GetWriter(session_.connection()));
1103 EXPECT_CALL(*writer, WritePacket(_, _, _, new_peer_address, _))
1104 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 0)));
1105 EXPECT_CALL(*connection_, SendConnectivityProbingResponsePacket(_))
1106 .WillOnce(Invoke(
1107 connection_,
1108 &MockQuicConnection::ReallySendConnectivityProbingResponsePacket));
1109 if (transport_version() == QUIC_VERSION_99) {
1110 // Need to explicitly do this to emulate the reception of a PathChallenge,
1111 // which stores its payload for use in generating the response.
1112 connection_->OnPathChallengeFrame(
1113 QuicPathChallengeFrame(0, path_frame_buffer1_));
1114 }
1115 session_.OnConnectivityProbeReceived(session_.self_address(),
1116 new_peer_address);
1117 EXPECT_EQ(old_peer_address, session_.peer_address());
1118}
1119
1120// Same as above, but check that if there are two PATH_CHALLENGE frames in the
1121// packet, the response has both of them AND we do not do migration. This for
1122// V99 only.
1123TEST_P(QuicSessionTestServer, ServerReplyToConnectivityProbes) {
1124 if (transport_version() != QUIC_VERSION_99) {
1125 return;
1126 }
1127 QuicSocketAddress old_peer_address =
1128 QuicSocketAddress(QuicIpAddress::Loopback4(), kTestPort);
1129 EXPECT_EQ(old_peer_address, session_.peer_address());
1130
1131 MockPacketWriter* writer = static_cast<MockPacketWriter*>(
1132 QuicConnectionPeer::GetWriter(session_.connection()));
1133 // CheckMultiPathResponse validates that the written packet
1134 // contains both path responses.
1135 EXPECT_CALL(*writer, WritePacket(_, _, _, old_peer_address, _))
1136 .WillOnce(Invoke(this, &QuicSessionTestServer::CheckMultiPathResponse));
1137
1138 EXPECT_CALL(*connection_, SendConnectivityProbingResponsePacket(_))
1139 .WillOnce(Invoke(
1140 connection_,
1141 &MockQuicConnection::ReallySendConnectivityProbingResponsePacket));
1142 // Need to explicitly do this to emulate the reception of a PathChallenge,
1143 // which stores its payload for use in generating the response.
1144 connection_->OnPathChallengeFrame(
1145 QuicPathChallengeFrame(0, path_frame_buffer1_));
1146 connection_->OnPathChallengeFrame(
1147 QuicPathChallengeFrame(0, path_frame_buffer2_));
1148 session_.OnConnectivityProbeReceived(session_.self_address(),
1149 old_peer_address);
1150}
1151
1152TEST_P(QuicSessionTestServer, IncreasedTimeoutAfterCryptoHandshake) {
1153 EXPECT_EQ(kInitialIdleTimeoutSecs + 3,
1154 QuicConnectionPeer::GetNetworkTimeout(connection_).ToSeconds());
1155 CryptoHandshakeMessage msg;
1156 session_.GetMutableCryptoStream()->OnHandshakeMessage(msg);
1157 EXPECT_EQ(kMaximumIdleTimeoutSecs + 3,
1158 QuicConnectionPeer::GetNetworkTimeout(connection_).ToSeconds());
1159}
1160
1161TEST_P(QuicSessionTestServer, OnStreamFrameFinStaticStreamId) {
1162 // Send two bytes of payload.
1163 QuicStreamFrame data1(
1164 QuicUtils::GetCryptoStreamId(connection_->transport_version()), true, 0,
1165 QuicStringPiece("HT"));
1166 EXPECT_CALL(*connection_,
1167 CloseConnection(
1168 QUIC_INVALID_STREAM_ID, "Attempt to close a static stream",
1169 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET));
1170 session_.OnStreamFrame(data1);
1171}
1172
1173TEST_P(QuicSessionTestServer, OnRstStreamStaticStreamId) {
1174 // Send two bytes of payload.
1175 QuicRstStreamFrame rst1(
1176 kInvalidControlFrameId,
1177 QuicUtils::GetCryptoStreamId(connection_->transport_version()),
1178 QUIC_ERROR_PROCESSING_STREAM, 0);
1179 EXPECT_CALL(*connection_,
1180 CloseConnection(
1181 QUIC_INVALID_STREAM_ID, "Attempt to reset a static stream",
1182 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET));
1183 session_.OnRstStream(rst1);
1184}
1185
1186TEST_P(QuicSessionTestServer, OnStreamFrameInvalidStreamId) {
1187 // Send two bytes of payload.
1188 QuicStreamFrame data1(
1189 QuicUtils::GetInvalidStreamId(connection_->transport_version()), true, 0,
1190 QuicStringPiece("HT"));
1191 EXPECT_CALL(*connection_,
1192 CloseConnection(
1193 QUIC_INVALID_STREAM_ID, "Recevied data for an invalid stream",
1194 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET));
1195 session_.OnStreamFrame(data1);
1196}
1197
1198TEST_P(QuicSessionTestServer, OnRstStreamInvalidStreamId) {
1199 // Send two bytes of payload.
1200 QuicRstStreamFrame rst1(
1201 kInvalidControlFrameId,
1202 QuicUtils::GetInvalidStreamId(connection_->transport_version()),
1203 QUIC_ERROR_PROCESSING_STREAM, 0);
1204 EXPECT_CALL(*connection_,
1205 CloseConnection(
1206 QUIC_INVALID_STREAM_ID, "Recevied data for an invalid stream",
1207 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET));
1208 session_.OnRstStream(rst1);
1209}
1210
1211TEST_P(QuicSessionTestServer, HandshakeUnblocksFlowControlBlockedStream) {
1212 // Test that if a stream is flow control blocked, then on receipt of the SHLO
1213 // containing a suitable send window offset, the stream becomes unblocked.
1214
1215 // Ensure that Writev consumes all the data it is given (simulate no socket
1216 // blocking).
1217 session_.set_writev_consumes_all_data(true);
1218
1219 // Create a stream, and send enough data to make it flow control blocked.
1220 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
vasilvvc48c8712019-03-11 13:38:16 -07001221 std::string body(kMinimumFlowControlSendWindow, '.');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001222 EXPECT_FALSE(stream2->flow_controller()->IsBlocked());
1223 EXPECT_FALSE(session_.IsConnectionFlowControlBlocked());
1224 EXPECT_FALSE(session_.IsStreamFlowControlBlocked());
1225 EXPECT_CALL(*connection_, SendControlFrame(_)).Times(AtLeast(1));
1226 stream2->WriteOrBufferData(body, false, nullptr);
1227 EXPECT_TRUE(stream2->flow_controller()->IsBlocked());
1228 EXPECT_TRUE(session_.IsConnectionFlowControlBlocked());
1229 EXPECT_TRUE(session_.IsStreamFlowControlBlocked());
1230
1231 // Now complete the crypto handshake, resulting in an increased flow control
1232 // send window.
1233 CryptoHandshakeMessage msg;
1234 session_.GetMutableCryptoStream()->OnHandshakeMessage(msg);
1235 EXPECT_TRUE(QuicSessionPeer::IsStreamWriteBlocked(&session_, stream2->id()));
1236 // Stream is now unblocked.
1237 EXPECT_FALSE(stream2->flow_controller()->IsBlocked());
1238 EXPECT_FALSE(session_.IsConnectionFlowControlBlocked());
1239 EXPECT_FALSE(session_.IsStreamFlowControlBlocked());
1240}
1241
1242TEST_P(QuicSessionTestServer, HandshakeUnblocksFlowControlBlockedCryptoStream) {
1243 if (GetParam().transport_version >= QUIC_VERSION_47) {
1244 // QUIC version 47 onwards uses CRYPTO frames for the handshake, so this
1245 // test doesn't make sense for those versions since CRYPTO frames aren't
1246 // flow controlled.
1247 return;
1248 }
1249 // Test that if the crypto stream is flow control blocked, then if the SHLO
1250 // contains a larger send window offset, the stream becomes unblocked.
1251 session_.set_writev_consumes_all_data(true);
1252 TestCryptoStream* crypto_stream = session_.GetMutableCryptoStream();
1253 EXPECT_FALSE(crypto_stream->flow_controller()->IsBlocked());
1254 EXPECT_FALSE(session_.IsConnectionFlowControlBlocked());
1255 EXPECT_FALSE(session_.IsStreamFlowControlBlocked());
1256 EXPECT_FALSE(session_.IsConnectionFlowControlBlocked());
1257 EXPECT_FALSE(session_.IsStreamFlowControlBlocked());
1258 EXPECT_CALL(*connection_, SendControlFrame(_))
1259 .WillOnce(Invoke(&session_, &TestSession::ClearControlFrame));
1260 for (QuicStreamId i = 0;
1261 !crypto_stream->flow_controller()->IsBlocked() && i < 1000u; i++) {
1262 EXPECT_FALSE(session_.IsConnectionFlowControlBlocked());
1263 EXPECT_FALSE(session_.IsStreamFlowControlBlocked());
1264 QuicStreamOffset offset = crypto_stream->stream_bytes_written();
1265 QuicConfig config;
1266 CryptoHandshakeMessage crypto_message;
1267 config.ToHandshakeMessage(&crypto_message);
1268 crypto_stream->SendHandshakeMessage(crypto_message);
1269 char buf[1000];
1270 QuicDataWriter writer(1000, buf, NETWORK_BYTE_ORDER);
1271 crypto_stream->WriteStreamData(offset, crypto_message.size(), &writer);
1272 }
1273 EXPECT_TRUE(crypto_stream->flow_controller()->IsBlocked());
1274 EXPECT_FALSE(session_.IsConnectionFlowControlBlocked());
1275 EXPECT_TRUE(session_.IsStreamFlowControlBlocked());
1276 EXPECT_FALSE(session_.HasDataToWrite());
1277 EXPECT_TRUE(crypto_stream->HasBufferedData());
1278
1279 // Now complete the crypto handshake, resulting in an increased flow control
1280 // send window.
1281 CryptoHandshakeMessage msg;
1282 session_.GetMutableCryptoStream()->OnHandshakeMessage(msg);
1283 EXPECT_TRUE(QuicSessionPeer::IsStreamWriteBlocked(
1284 &session_,
1285 QuicUtils::GetCryptoStreamId(connection_->transport_version())));
1286 // Stream is now unblocked and will no longer have buffered data.
1287 EXPECT_FALSE(crypto_stream->flow_controller()->IsBlocked());
1288 EXPECT_FALSE(session_.IsConnectionFlowControlBlocked());
1289 EXPECT_FALSE(session_.IsStreamFlowControlBlocked());
1290}
1291
1292TEST_P(QuicSessionTestServer, ConnectionFlowControlAccountingRstOutOfOrder) {
1293 // Test that when we receive an out of order stream RST we correctly adjust
1294 // our connection level flow control receive window.
1295 // On close, the stream should mark as consumed all bytes between the highest
1296 // byte consumed so far and the final byte offset from the RST frame.
1297 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
1298
1299 const QuicStreamOffset kByteOffset =
1300 1 + kInitialSessionFlowControlWindowForTest / 2;
1301
1302 EXPECT_CALL(*connection_, SendControlFrame(_))
1303 .Times(2)
1304 .WillRepeatedly(Invoke(&session_, &TestSession::ClearControlFrame));
1305 EXPECT_CALL(*connection_, OnStreamReset(stream->id(), _));
1306
1307 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream->id(),
1308 QUIC_STREAM_CANCELLED, kByteOffset);
1309 session_.OnRstStream(rst_frame);
1310 if (transport_version() == QUIC_VERSION_99) {
1311 // The test is predicated on the stream being fully closed. For V99, the
1312 // RST_STREAM only does one side (the read side from the perspective of the
1313 // node receiving the RST_STREAM). This is needed to fully close the
1314 // stream and therefore fulfill all of the expects.
1315 QuicStopSendingFrame frame(kInvalidControlFrameId, stream->id(),
1316 QUIC_STREAM_CANCELLED);
1317 EXPECT_TRUE(session_.OnStopSendingFrame(frame));
1318 }
1319 EXPECT_EQ(kByteOffset, session_.flow_controller()->bytes_consumed());
1320}
1321
1322TEST_P(QuicSessionTestServer, ConnectionFlowControlAccountingFinAndLocalReset) {
1323 // Test the situation where we receive a FIN on a stream, and before we fully
1324 // consume all the data from the sequencer buffer we locally RST the stream.
1325 // The bytes between highest consumed byte, and the final byte offset that we
1326 // determined when the FIN arrived, should be marked as consumed at the
1327 // connection level flow controller when the stream is reset.
1328 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
1329
1330 const QuicStreamOffset kByteOffset =
1331 kInitialSessionFlowControlWindowForTest / 2 - 1;
1332 QuicStreamFrame frame(stream->id(), true, kByteOffset, ".");
1333 session_.OnStreamFrame(frame);
1334 EXPECT_TRUE(connection_->connected());
1335
1336 EXPECT_EQ(0u, stream->flow_controller()->bytes_consumed());
1337 EXPECT_EQ(kByteOffset + frame.data_length,
1338 stream->flow_controller()->highest_received_byte_offset());
1339
1340 // Reset stream locally.
1341 EXPECT_CALL(*connection_, SendControlFrame(_));
1342 EXPECT_CALL(*connection_, OnStreamReset(stream->id(), _));
1343 stream->Reset(QUIC_STREAM_CANCELLED);
1344 EXPECT_EQ(kByteOffset + frame.data_length,
1345 session_.flow_controller()->bytes_consumed());
1346}
1347
1348TEST_P(QuicSessionTestServer, ConnectionFlowControlAccountingFinAfterRst) {
1349 // Test that when we RST the stream (and tear down stream state), and then
1350 // receive a FIN from the peer, we correctly adjust our connection level flow
1351 // control receive window.
1352
1353 // Connection starts with some non-zero highest received byte offset,
1354 // due to other active streams.
1355 const uint64_t kInitialConnectionBytesConsumed = 567;
1356 const uint64_t kInitialConnectionHighestReceivedOffset = 1234;
1357 EXPECT_LT(kInitialConnectionBytesConsumed,
1358 kInitialConnectionHighestReceivedOffset);
1359 session_.flow_controller()->UpdateHighestReceivedOffset(
1360 kInitialConnectionHighestReceivedOffset);
1361 session_.flow_controller()->AddBytesConsumed(kInitialConnectionBytesConsumed);
1362
1363 // Reset our stream: this results in the stream being closed locally.
1364 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
1365 EXPECT_CALL(*connection_, SendControlFrame(_));
1366 EXPECT_CALL(*connection_, OnStreamReset(stream->id(), _));
1367 stream->Reset(QUIC_STREAM_CANCELLED);
1368
1369 // Now receive a response from the peer with a FIN. We should handle this by
1370 // adjusting the connection level flow control receive window to take into
1371 // account the total number of bytes sent by the peer.
1372 const QuicStreamOffset kByteOffset = 5678;
vasilvvc48c8712019-03-11 13:38:16 -07001373 std::string body = "hello";
QUICHE teama6ef0a62019-03-07 20:34:33 -05001374 QuicStreamFrame frame(stream->id(), true, kByteOffset, QuicStringPiece(body));
1375 session_.OnStreamFrame(frame);
1376
1377 QuicStreamOffset total_stream_bytes_sent_by_peer =
1378 kByteOffset + body.length();
1379 EXPECT_EQ(kInitialConnectionBytesConsumed + total_stream_bytes_sent_by_peer,
1380 session_.flow_controller()->bytes_consumed());
1381 EXPECT_EQ(
1382 kInitialConnectionHighestReceivedOffset + total_stream_bytes_sent_by_peer,
1383 session_.flow_controller()->highest_received_byte_offset());
1384}
1385
1386TEST_P(QuicSessionTestServer, ConnectionFlowControlAccountingRstAfterRst) {
1387 // Test that when we RST the stream (and tear down stream state), and then
1388 // receive a RST from the peer, we correctly adjust our connection level flow
1389 // control receive window.
1390
1391 // Connection starts with some non-zero highest received byte offset,
1392 // due to other active streams.
1393 const uint64_t kInitialConnectionBytesConsumed = 567;
1394 const uint64_t kInitialConnectionHighestReceivedOffset = 1234;
1395 EXPECT_LT(kInitialConnectionBytesConsumed,
1396 kInitialConnectionHighestReceivedOffset);
1397 session_.flow_controller()->UpdateHighestReceivedOffset(
1398 kInitialConnectionHighestReceivedOffset);
1399 session_.flow_controller()->AddBytesConsumed(kInitialConnectionBytesConsumed);
1400
1401 // Reset our stream: this results in the stream being closed locally.
1402 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
1403 EXPECT_CALL(*connection_, SendControlFrame(_));
1404 EXPECT_CALL(*connection_, OnStreamReset(stream->id(), _));
1405 stream->Reset(QUIC_STREAM_CANCELLED);
1406 EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream));
1407
1408 // Now receive a RST from the peer. We should handle this by adjusting the
1409 // connection level flow control receive window to take into account the total
1410 // number of bytes sent by the peer.
1411 const QuicStreamOffset kByteOffset = 5678;
1412 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream->id(),
1413 QUIC_STREAM_CANCELLED, kByteOffset);
1414 session_.OnRstStream(rst_frame);
1415
1416 EXPECT_EQ(kInitialConnectionBytesConsumed + kByteOffset,
1417 session_.flow_controller()->bytes_consumed());
1418 EXPECT_EQ(kInitialConnectionHighestReceivedOffset + kByteOffset,
1419 session_.flow_controller()->highest_received_byte_offset());
1420}
1421
1422TEST_P(QuicSessionTestServer, InvalidStreamFlowControlWindowInHandshake) {
1423 // Test that receipt of an invalid (< default) stream flow control window from
1424 // the peer results in the connection being torn down.
1425 const uint32_t kInvalidWindow = kMinimumFlowControlSendWindow - 1;
1426 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(session_.config(),
1427 kInvalidWindow);
1428
1429 EXPECT_CALL(*connection_,
1430 CloseConnection(QUIC_FLOW_CONTROL_INVALID_WINDOW, _, _));
1431 session_.OnConfigNegotiated();
1432}
1433
1434TEST_P(QuicSessionTestServer, InvalidSessionFlowControlWindowInHandshake) {
1435 // Test that receipt of an invalid (< default) session flow control window
1436 // from the peer results in the connection being torn down.
1437 const uint32_t kInvalidWindow = kMinimumFlowControlSendWindow - 1;
1438 QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow(session_.config(),
1439 kInvalidWindow);
1440
1441 EXPECT_CALL(*connection_,
1442 CloseConnection(QUIC_FLOW_CONTROL_INVALID_WINDOW, _, _));
1443 session_.OnConfigNegotiated();
1444}
1445
1446// Test negotiation of custom server initial flow control window.
1447TEST_P(QuicSessionTestServer, CustomFlowControlWindow) {
1448 QuicTagVector copt;
1449 copt.push_back(kIFW7);
1450 QuicConfigPeer::SetReceivedConnectionOptions(session_.config(), copt);
1451
1452 session_.OnConfigNegotiated();
1453 EXPECT_EQ(192 * 1024u, QuicFlowControllerPeer::ReceiveWindowSize(
1454 session_.flow_controller()));
1455}
1456
1457TEST_P(QuicSessionTestServer, FlowControlWithInvalidFinalOffset) {
1458 // Test that if we receive a stream RST with a highest byte offset that
1459 // violates flow control, that we close the connection.
1460 const uint64_t kLargeOffset = kInitialSessionFlowControlWindowForTest + 1;
1461 EXPECT_CALL(*connection_,
1462 CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _))
1463 .Times(2);
1464
1465 // Check that stream frame + FIN results in connection close.
1466 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
1467 EXPECT_CALL(*connection_, SendControlFrame(_));
1468 EXPECT_CALL(*connection_, OnStreamReset(stream->id(), _));
1469 stream->Reset(QUIC_STREAM_CANCELLED);
1470 QuicStreamFrame frame(stream->id(), true, kLargeOffset, QuicStringPiece());
1471 session_.OnStreamFrame(frame);
1472
1473 // Check that RST results in connection close.
1474 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream->id(),
1475 QUIC_STREAM_CANCELLED, kLargeOffset);
1476 session_.OnRstStream(rst_frame);
1477}
1478
1479TEST_P(QuicSessionTestServer, TooManyUnfinishedStreamsCauseServerRejectStream) {
1480 // If a buggy/malicious peer creates too many streams that are not ended
1481 // with a FIN or RST then we send an RST to refuse streams. For V99 the
1482 // connection is closed.
1483 const QuicStreamId kMaxStreams = 5;
1484 QuicSessionPeer::SetMaxOpenIncomingStreams(&session_, kMaxStreams);
1485 const QuicStreamId kFirstStreamId = GetNthClientInitiatedBidirectionalId(0);
1486 const QuicStreamId kFinalStreamId =
1487 GetNthClientInitiatedBidirectionalId(kMaxStreams);
1488 // Create kMaxStreams data streams, and close them all without receiving a
1489 // FIN or a RST_STREAM from the client.
1490 for (QuicStreamId i = kFirstStreamId; i < kFinalStreamId;
1491 i += QuicUtils::StreamIdDelta(connection_->transport_version())) {
1492 QuicStreamFrame data1(i, false, 0, QuicStringPiece("HT"));
1493 session_.OnStreamFrame(data1);
1494 // EXPECT_EQ(1u, session_.GetNumOpenStreams());
1495 if (transport_version() == QUIC_VERSION_99) {
1496 // Expect two control frames, RST STREAM and STOP SENDING
1497 EXPECT_CALL(*connection_, SendControlFrame(_))
1498 .Times(2)
1499 .WillRepeatedly(Invoke(&session_, &TestSession::ClearControlFrame));
1500 } else {
1501 // Expect one control frame, just RST STREAM
1502 EXPECT_CALL(*connection_, SendControlFrame(_))
1503 .WillOnce(Invoke(&session_, &TestSession::ClearControlFrame));
1504 }
1505 // Close stream. Should not make new streams available since
1506 // the stream is not finished.
1507 EXPECT_CALL(*connection_, OnStreamReset(i, _));
1508 session_.CloseStream(i);
1509 }
1510
1511 if (transport_version() == QUIC_VERSION_99) {
1512 EXPECT_CALL(*connection_, CloseConnection(QUIC_INVALID_STREAM_ID,
1513 "Stream id 24 above 20", _));
1514 } else {
1515 EXPECT_CALL(*connection_, SendControlFrame(_)).Times(1);
1516 EXPECT_CALL(*connection_,
1517 OnStreamReset(kFinalStreamId, QUIC_REFUSED_STREAM))
1518 .Times(1);
1519 }
1520 // Create one more data streams to exceed limit of open stream.
1521 QuicStreamFrame data1(kFinalStreamId, false, 0, QuicStringPiece("HT"));
1522 session_.OnStreamFrame(data1);
1523}
1524
1525TEST_P(QuicSessionTestServer, DrainingStreamsDoNotCountAsOpenedOutgoing) {
1526 // Verify that a draining stream (which has received a FIN but not consumed
1527 // it) does not count against the open quota (because it is closed from the
1528 // protocol point of view).
1529 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
1530 QuicStreamId stream_id = stream->id();
1531 QuicStreamFrame data1(stream_id, true, 0, QuicStringPiece("HT"));
1532 session_.OnStreamFrame(data1);
1533 EXPECT_CALL(session_, OnCanCreateNewOutgoingStream()).Times(1);
1534 session_.StreamDraining(stream_id);
1535}
1536
1537TEST_P(QuicSessionTestServer, NoPendingStreams) {
1538 session_.set_should_buffer_incoming_streams(false);
1539
1540 QuicStreamId stream_id = QuicUtils::GetFirstUnidirectionalStreamId(
1541 transport_version(), Perspective::IS_CLIENT);
1542 QuicStreamFrame data1(stream_id, true, 10, QuicStringPiece("HT"));
1543 session_.OnStreamFrame(data1);
1544 EXPECT_EQ(1, session_.num_incoming_streams_created());
1545
1546 QuicStreamFrame data2(stream_id, false, 0, QuicStringPiece("HT"));
1547 session_.OnStreamFrame(data2);
1548 EXPECT_EQ(1, session_.num_incoming_streams_created());
1549}
1550
1551TEST_P(QuicSessionTestServer, PendingStreams) {
1552 if (connection_->transport_version() != QUIC_VERSION_99) {
1553 return;
1554 }
1555 session_.set_should_buffer_incoming_streams(true);
1556
1557 QuicStreamId stream_id = QuicUtils::GetFirstUnidirectionalStreamId(
1558 transport_version(), Perspective::IS_CLIENT);
1559 QuicStreamFrame data1(stream_id, true, 10, QuicStringPiece("HT"));
1560 session_.OnStreamFrame(data1);
1561 EXPECT_EQ(0, session_.num_incoming_streams_created());
1562
1563 QuicStreamFrame data2(stream_id, false, 0, QuicStringPiece("HT"));
1564 session_.OnStreamFrame(data2);
1565 EXPECT_EQ(1, session_.num_incoming_streams_created());
1566}
1567
1568TEST_P(QuicSessionTestServer, RstPendingStreams) {
1569 if (connection_->transport_version() != QUIC_VERSION_99) {
1570 return;
1571 }
1572 session_.set_should_buffer_incoming_streams(true);
1573
1574 QuicStreamId stream_id = QuicUtils::GetFirstUnidirectionalStreamId(
1575 transport_version(), Perspective::IS_CLIENT);
1576 QuicStreamFrame data1(stream_id, true, 10, QuicStringPiece("HT"));
1577 session_.OnStreamFrame(data1);
1578 EXPECT_EQ(0, session_.num_incoming_streams_created());
1579
1580 EXPECT_CALL(session_, OnCanCreateNewOutgoingStream()).Times(1);
1581 EXPECT_CALL(*connection_, SendControlFrame(_)).Times(1);
1582 EXPECT_CALL(*connection_, OnStreamReset(stream_id, QUIC_RST_ACKNOWLEDGEMENT))
1583 .Times(1);
1584 QuicRstStreamFrame rst1(kInvalidControlFrameId, stream_id,
1585 QUIC_ERROR_PROCESSING_STREAM, 12);
1586 session_.OnRstStream(rst1);
1587 EXPECT_EQ(0, session_.num_incoming_streams_created());
1588
1589 QuicStreamFrame data2(stream_id, false, 0, QuicStringPiece("HT"));
1590 session_.OnStreamFrame(data2);
1591 EXPECT_EQ(0, session_.num_incoming_streams_created());
1592}
1593
1594TEST_P(QuicSessionTestServer, DrainingStreamsDoNotCountAsOpened) {
1595 // Verify that a draining stream (which has received a FIN but not consumed
1596 // it) does not count against the open quota (because it is closed from the
1597 // protocol point of view).
1598 if (transport_version() == QUIC_VERSION_99) {
1599 // On v99, we will expect to see a MAX_STREAM_ID go out when there are not
1600 // enough streams to create the next one.
1601 EXPECT_CALL(*connection_, SendControlFrame(_)).Times(1);
1602 } else {
1603 EXPECT_CALL(*connection_, SendControlFrame(_)).Times(0);
1604 }
1605 EXPECT_CALL(*connection_, OnStreamReset(_, QUIC_REFUSED_STREAM)).Times(0);
1606 const QuicStreamId kMaxStreams = 5;
1607 QuicSessionPeer::SetMaxOpenIncomingStreams(&session_, kMaxStreams);
1608
1609 // Create kMaxStreams + 1 data streams, and mark them draining.
1610 const QuicStreamId kFirstStreamId = GetNthClientInitiatedBidirectionalId(0);
1611 const QuicStreamId kFinalStreamId =
1612 GetNthClientInitiatedBidirectionalId(2 * kMaxStreams + 1);
1613 for (QuicStreamId i = kFirstStreamId; i < kFinalStreamId;
1614 i += QuicUtils::StreamIdDelta(connection_->transport_version())) {
1615 QuicStreamFrame data1(i, true, 0, QuicStringPiece("HT"));
1616 session_.OnStreamFrame(data1);
1617 EXPECT_EQ(1u, session_.GetNumOpenIncomingStreams());
1618 session_.StreamDraining(i);
1619 EXPECT_EQ(0u, session_.GetNumOpenIncomingStreams());
1620 }
1621}
1622
1623class QuicSessionTestClient : public QuicSessionTestBase {
1624 protected:
1625 QuicSessionTestClient() : QuicSessionTestBase(Perspective::IS_CLIENT) {}
1626};
1627
1628INSTANTIATE_TEST_SUITE_P(Tests,
1629 QuicSessionTestClient,
1630 ::testing::ValuesIn(AllSupportedVersions()));
1631
1632TEST_P(QuicSessionTestClient, AvailableBidirectionalStreamsClient) {
1633 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
1634 GetNthServerInitiatedBidirectionalId(2)) != nullptr);
1635 // Smaller bidirectional streams should be available.
1636 EXPECT_TRUE(QuicSessionPeer::IsStreamAvailable(
1637 &session_, GetNthServerInitiatedBidirectionalId(0)));
1638 EXPECT_TRUE(QuicSessionPeer::IsStreamAvailable(
1639 &session_, GetNthServerInitiatedBidirectionalId(1)));
1640 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
1641 GetNthServerInitiatedBidirectionalId(0)) != nullptr);
1642 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
1643 GetNthServerInitiatedBidirectionalId(1)) != nullptr);
1644 // And 5 should be not available.
1645 EXPECT_FALSE(QuicSessionPeer::IsStreamAvailable(
1646 &session_, GetNthClientInitiatedBidirectionalId(1)));
1647}
1648
1649TEST_P(QuicSessionTestClient, AvailableUnidirectionalStreamsClient) {
1650 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
1651 GetNthServerInitiatedUnidirectionalId(2)) != nullptr);
1652 // Smaller unidirectional streams should be available.
1653 EXPECT_TRUE(QuicSessionPeer::IsStreamAvailable(
1654 &session_, GetNthServerInitiatedUnidirectionalId(0)));
1655 EXPECT_TRUE(QuicSessionPeer::IsStreamAvailable(
1656 &session_, GetNthServerInitiatedUnidirectionalId(1)));
1657 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
1658 GetNthServerInitiatedUnidirectionalId(0)) != nullptr);
1659 ASSERT_TRUE(session_.GetOrCreateDynamicStream(
1660 GetNthServerInitiatedUnidirectionalId(1)) != nullptr);
1661 // And 5 should be not available.
1662 EXPECT_FALSE(QuicSessionPeer::IsStreamAvailable(
1663 &session_, GetNthClientInitiatedUnidirectionalId(1)));
1664}
1665
1666TEST_P(QuicSessionTestClient, RecordFinAfterReadSideClosed) {
1667 // Verify that an incoming FIN is recorded in a stream object even if the read
1668 // side has been closed. This prevents an entry from being made in
1669 // locally_closed_streams_highest_offset_ (which will never be deleted).
1670 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
1671 QuicStreamId stream_id = stream->id();
1672
1673 // Close the read side manually.
1674 QuicStreamPeer::CloseReadSide(stream);
1675
1676 // Receive a stream data frame with FIN.
1677 QuicStreamFrame frame(stream_id, true, 0, QuicStringPiece());
1678 session_.OnStreamFrame(frame);
1679 EXPECT_TRUE(stream->fin_received());
1680
1681 // Reset stream locally.
1682 EXPECT_CALL(*connection_, SendControlFrame(_));
1683 EXPECT_CALL(*connection_, OnStreamReset(stream->id(), _));
1684 stream->Reset(QUIC_STREAM_CANCELLED);
1685 EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream));
1686
1687 EXPECT_TRUE(connection_->connected());
1688 EXPECT_TRUE(QuicSessionPeer::IsStreamClosed(&session_, stream_id));
1689 EXPECT_FALSE(QuicSessionPeer::IsStreamCreated(&session_, stream_id));
1690
1691 // The stream is not waiting for the arrival of the peer's final offset as it
1692 // was received with the FIN earlier.
1693 EXPECT_EQ(
1694 0u,
1695 QuicSessionPeer::GetLocallyClosedStreamsHighestOffset(&session_).size());
1696}
1697
1698TEST_P(QuicSessionTestServer, ZombieStreams) {
1699 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
1700 QuicStreamPeer::SetStreamBytesWritten(3, stream2);
1701 EXPECT_TRUE(stream2->IsWaitingForAcks());
1702
1703 EXPECT_CALL(*connection_, SendControlFrame(_));
1704 EXPECT_CALL(*connection_, OnStreamReset(stream2->id(), _));
1705 session_.CloseStream(stream2->id());
1706 EXPECT_FALSE(QuicContainsKey(session_.zombie_streams(), stream2->id()));
1707 ASSERT_EQ(1u, session_.closed_streams()->size());
1708 EXPECT_EQ(stream2->id(), session_.closed_streams()->front()->id());
1709 session_.OnStreamDoneWaitingForAcks(stream2->id());
1710 EXPECT_FALSE(QuicContainsKey(session_.zombie_streams(), stream2->id()));
1711 EXPECT_EQ(1u, session_.closed_streams()->size());
1712 EXPECT_EQ(stream2->id(), session_.closed_streams()->front()->id());
1713}
1714
1715TEST_P(QuicSessionTestServer, RstStreamReceivedAfterRstStreamSent) {
1716 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
1717 QuicStreamPeer::SetStreamBytesWritten(3, stream2);
1718 EXPECT_TRUE(stream2->IsWaitingForAcks());
1719
1720 EXPECT_CALL(*connection_, SendControlFrame(_));
1721 EXPECT_CALL(*connection_, OnStreamReset(stream2->id(), _));
1722 EXPECT_CALL(session_, OnCanCreateNewOutgoingStream()).Times(0);
1723 stream2->Reset(quic::QUIC_STREAM_CANCELLED);
1724
1725 QuicRstStreamFrame rst1(kInvalidControlFrameId, stream2->id(),
1726 QUIC_ERROR_PROCESSING_STREAM, 0);
1727 if (transport_version() != QUIC_VERSION_99) {
1728 EXPECT_CALL(session_, OnCanCreateNewOutgoingStream()).Times(1);
1729 }
1730 session_.OnRstStream(rst1);
1731}
1732
1733// Regression test of b/71548958.
1734TEST_P(QuicSessionTestServer, TestZombieStreams) {
1735 session_.set_writev_consumes_all_data(true);
1736
1737 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
vasilvvc48c8712019-03-11 13:38:16 -07001738 std::string body(100, '.');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001739 stream2->WriteOrBufferData(body, false, nullptr);
1740 EXPECT_TRUE(stream2->IsWaitingForAcks());
1741 EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream2).size());
1742
1743 QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream2->id(),
1744 QUIC_STREAM_CANCELLED, 1234);
1745 // Just for the RST_STREAM
1746 EXPECT_CALL(*connection_, SendControlFrame(_))
1747 .WillOnce(Invoke(&session_, &TestSession::ClearControlFrame));
1748 if (transport_version() == QUIC_VERSION_99) {
1749 EXPECT_CALL(*connection_,
1750 OnStreamReset(stream2->id(), QUIC_STREAM_CANCELLED));
1751 } else {
1752 EXPECT_CALL(*connection_,
1753 OnStreamReset(stream2->id(), QUIC_RST_ACKNOWLEDGEMENT));
1754 }
1755 stream2->OnStreamReset(rst_frame);
1756
1757 if (transport_version() == QUIC_VERSION_99) {
1758 // The test is predicated on the stream being fully closed. For V99, the
1759 // RST_STREAM only does one side (the read side from the perspective of the
1760 // node receiving the RST_STREAM). This is needed to fully close the
1761 // stream and therefore fulfill all of the expects.
1762 QuicStopSendingFrame frame(kInvalidControlFrameId, stream2->id(),
1763 QUIC_STREAM_CANCELLED);
1764 EXPECT_TRUE(session_.OnStopSendingFrame(frame));
1765 }
1766 EXPECT_FALSE(QuicContainsKey(session_.zombie_streams(), stream2->id()));
1767 ASSERT_EQ(1u, session_.closed_streams()->size());
1768 EXPECT_EQ(stream2->id(), session_.closed_streams()->front()->id());
1769
1770 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
1771 if (transport_version() == QUIC_VERSION_99) {
1772 // Once for the RST_STREAM, once for the STOP_SENDING
1773 EXPECT_CALL(*connection_, SendControlFrame(_))
1774 .Times(2)
1775 .WillRepeatedly(Invoke(&session_, &TestSession::ClearControlFrame));
1776 } else {
1777 // Just for the RST_STREAM
1778 EXPECT_CALL(*connection_, SendControlFrame(_)).Times(1);
1779 }
1780 EXPECT_CALL(*connection_,
1781 OnStreamReset(stream4->id(), QUIC_STREAM_CANCELLED));
1782 stream4->WriteOrBufferData(body, false, nullptr);
1783 // Note well: Reset() actually closes the stream in both directions. For
1784 // GOOGLE QUIC it sends a RST_STREAM (which does a 2-way close), for IETF
1785 // QUIC/V99 it sends both a RST_STREAM and a STOP_SENDING (each of which
1786 // closes in only one direction).
1787 stream4->Reset(QUIC_STREAM_CANCELLED);
1788 EXPECT_FALSE(QuicContainsKey(session_.zombie_streams(), stream4->id()));
1789 EXPECT_EQ(2u, session_.closed_streams()->size());
1790}
1791
1792TEST_P(QuicSessionTestServer, OnStreamFrameLost) {
1793 QuicConnectionPeer::SetSessionDecidesWhatToWrite(connection_);
1794 InSequence s;
1795
1796 // Drive congestion control manually.
1797 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>;
1798 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm);
1799
1800 TestCryptoStream* crypto_stream = session_.GetMutableCryptoStream();
1801 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
1802 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
1803
1804 QuicStreamFrame frame1(
1805 QuicUtils::GetCryptoStreamId(connection_->transport_version()), false, 0,
1806 1300);
1807 QuicStreamFrame frame2(stream2->id(), false, 0, 9);
1808 QuicStreamFrame frame3(stream4->id(), false, 0, 9);
1809
1810 // Lost data on cryption stream, streams 2 and 4.
1811 EXPECT_CALL(*stream4, HasPendingRetransmission()).WillOnce(Return(true));
1812 if (connection_->transport_version() < QUIC_VERSION_47) {
1813 EXPECT_CALL(*crypto_stream, HasPendingRetransmission())
1814 .WillOnce(Return(true));
1815 }
1816 EXPECT_CALL(*stream2, HasPendingRetransmission()).WillOnce(Return(true));
1817 session_.OnFrameLost(QuicFrame(frame3));
1818 if (connection_->transport_version() < QUIC_VERSION_47) {
1819 session_.OnFrameLost(QuicFrame(frame1));
1820 } else {
1821 QuicCryptoFrame crypto_frame(ENCRYPTION_NONE, 0, 1300);
1822 session_.OnFrameLost(QuicFrame(&crypto_frame));
1823 }
1824 session_.OnFrameLost(QuicFrame(frame2));
1825 EXPECT_TRUE(session_.WillingAndAbleToWrite());
1826
1827 // Mark streams 2 and 4 write blocked.
1828 session_.MarkConnectionLevelWriteBlocked(stream2->id());
1829 session_.MarkConnectionLevelWriteBlocked(stream4->id());
1830
1831 // Lost data is retransmitted before new data, and retransmissions for crypto
1832 // stream go first.
1833 // Do not check congestion window when crypto stream has lost data.
1834 EXPECT_CALL(*send_algorithm, CanSend(_)).Times(0);
1835 if (connection_->transport_version() < QUIC_VERSION_47) {
1836 EXPECT_CALL(*crypto_stream, OnCanWrite());
1837 EXPECT_CALL(*crypto_stream, HasPendingRetransmission())
1838 .WillOnce(Return(false));
1839 }
1840 // Check congestion window for non crypto streams.
1841 EXPECT_CALL(*send_algorithm, CanSend(_)).WillOnce(Return(true));
1842 EXPECT_CALL(*stream4, OnCanWrite());
1843 EXPECT_CALL(*stream4, HasPendingRetransmission()).WillOnce(Return(false));
1844 // Connection is blocked.
1845 EXPECT_CALL(*send_algorithm, CanSend(_)).WillRepeatedly(Return(false));
1846
1847 session_.OnCanWrite();
1848 EXPECT_TRUE(session_.WillingAndAbleToWrite());
1849
1850 // Unblock connection.
1851 // Stream 2 retransmits lost data.
1852 EXPECT_CALL(*send_algorithm, CanSend(_)).WillOnce(Return(true));
1853 EXPECT_CALL(*stream2, OnCanWrite());
1854 EXPECT_CALL(*stream2, HasPendingRetransmission()).WillOnce(Return(false));
1855 EXPECT_CALL(*send_algorithm, CanSend(_)).WillOnce(Return(true));
1856 // Stream 2 sends new data.
1857 EXPECT_CALL(*stream2, OnCanWrite());
1858 EXPECT_CALL(*send_algorithm, CanSend(_)).WillOnce(Return(true));
1859 EXPECT_CALL(*stream4, OnCanWrite());
1860 EXPECT_CALL(*send_algorithm, OnApplicationLimited(_));
1861
1862 session_.OnCanWrite();
1863 EXPECT_FALSE(session_.WillingAndAbleToWrite());
1864}
1865
1866TEST_P(QuicSessionTestServer, DonotRetransmitDataOfClosedStreams) {
1867 QuicConnectionPeer::SetSessionDecidesWhatToWrite(connection_);
1868 InSequence s;
1869
1870 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
1871 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
1872 TestStream* stream6 = session_.CreateOutgoingBidirectionalStream();
1873
1874 QuicStreamFrame frame1(stream2->id(), false, 0, 9);
1875 QuicStreamFrame frame2(stream4->id(), false, 0, 9);
1876 QuicStreamFrame frame3(stream6->id(), false, 0, 9);
1877
1878 EXPECT_CALL(*stream6, HasPendingRetransmission()).WillOnce(Return(true));
1879 EXPECT_CALL(*stream4, HasPendingRetransmission()).WillOnce(Return(true));
1880 EXPECT_CALL(*stream2, HasPendingRetransmission()).WillOnce(Return(true));
1881 session_.OnFrameLost(QuicFrame(frame3));
1882 session_.OnFrameLost(QuicFrame(frame2));
1883 session_.OnFrameLost(QuicFrame(frame1));
1884
1885 session_.MarkConnectionLevelWriteBlocked(stream2->id());
1886 session_.MarkConnectionLevelWriteBlocked(stream4->id());
1887 session_.MarkConnectionLevelWriteBlocked(stream6->id());
1888
1889 // Reset stream 4 locally.
1890 EXPECT_CALL(*connection_, SendControlFrame(_));
1891 EXPECT_CALL(*connection_, OnStreamReset(stream4->id(), _));
1892 stream4->Reset(QUIC_STREAM_CANCELLED);
1893
1894 // Verify stream 4 is removed from streams with lost data list.
1895 EXPECT_CALL(*stream6, OnCanWrite());
1896 EXPECT_CALL(*stream6, HasPendingRetransmission()).WillOnce(Return(false));
1897 EXPECT_CALL(*stream2, OnCanWrite());
1898 EXPECT_CALL(*stream2, HasPendingRetransmission()).WillOnce(Return(false));
1899 EXPECT_CALL(*connection_, SendControlFrame(_))
1900 .WillRepeatedly(Invoke(&session_, &TestSession::ClearControlFrame));
1901 EXPECT_CALL(*stream2, OnCanWrite());
1902 EXPECT_CALL(*stream6, OnCanWrite());
1903 session_.OnCanWrite();
1904}
1905
1906TEST_P(QuicSessionTestServer, RetransmitFrames) {
1907 QuicConnectionPeer::SetSessionDecidesWhatToWrite(connection_);
1908 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>;
1909 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm);
1910 InSequence s;
1911
1912 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
1913 TestStream* stream4 = session_.CreateOutgoingBidirectionalStream();
1914 TestStream* stream6 = session_.CreateOutgoingBidirectionalStream();
1915 EXPECT_CALL(*connection_, SendControlFrame(_))
1916 .WillOnce(Invoke(&session_, &TestSession::ClearControlFrame));
1917 session_.SendWindowUpdate(stream2->id(), 9);
1918
1919 QuicStreamFrame frame1(stream2->id(), false, 0, 9);
1920 QuicStreamFrame frame2(stream4->id(), false, 0, 9);
1921 QuicStreamFrame frame3(stream6->id(), false, 0, 9);
1922 QuicWindowUpdateFrame window_update(1, stream2->id(), 9);
1923 QuicFrames frames;
1924 frames.push_back(QuicFrame(frame1));
1925 frames.push_back(QuicFrame(&window_update));
1926 frames.push_back(QuicFrame(frame2));
1927 frames.push_back(QuicFrame(frame3));
1928 EXPECT_FALSE(session_.WillingAndAbleToWrite());
1929
1930 EXPECT_CALL(*stream2, RetransmitStreamData(_, _, _)).WillOnce(Return(true));
1931 EXPECT_CALL(*connection_, SendControlFrame(_))
1932 .WillOnce(Invoke(&session_, &TestSession::ClearControlFrame));
1933 EXPECT_CALL(*stream4, RetransmitStreamData(_, _, _)).WillOnce(Return(true));
1934 EXPECT_CALL(*stream6, RetransmitStreamData(_, _, _)).WillOnce(Return(true));
1935 EXPECT_CALL(*send_algorithm, OnApplicationLimited(_));
1936 session_.RetransmitFrames(frames, TLP_RETRANSMISSION);
1937}
1938
1939// Regression test of b/110082001.
1940TEST_P(QuicSessionTestServer, RetransmitLostDataCausesConnectionClose) {
1941 // This test mimics the scenario when a dynamic stream retransmits lost data
1942 // and causes connection close.
1943 QuicConnectionPeer::SetSessionDecidesWhatToWrite(connection_);
1944 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
1945 QuicStreamFrame frame(stream->id(), false, 0, 9);
1946
1947 EXPECT_CALL(*stream, HasPendingRetransmission())
1948 .Times(2)
1949 .WillOnce(Return(true))
1950 .WillOnce(Return(false));
1951 session_.OnFrameLost(QuicFrame(frame));
1952 // Retransmit stream data causes connection close. Stream has not sent fin
1953 // yet, so an RST is sent.
1954 EXPECT_CALL(*stream, OnCanWrite())
1955 .WillOnce(Invoke(stream, &QuicStream::OnClose));
1956 if (transport_version() == QUIC_VERSION_99) {
1957 // Once for the RST_STREAM, once for the STOP_SENDING
1958 EXPECT_CALL(*connection_, SendControlFrame(_))
1959 .Times(2)
1960 .WillRepeatedly(Invoke(&session_, &TestSession::SaveFrame));
1961 } else {
1962 // Just for the RST_STREAM
1963 EXPECT_CALL(*connection_, SendControlFrame(_))
1964 .WillOnce(Invoke(&session_, &TestSession::SaveFrame));
1965 }
1966 EXPECT_CALL(*connection_, OnStreamReset(stream->id(), _));
1967 session_.OnCanWrite();
1968}
1969
1970TEST_P(QuicSessionTestServer, SendMessage) {
1971 // Cannot send message when encryption is not established.
1972 EXPECT_FALSE(session_.IsCryptoHandshakeConfirmed());
1973 quic::QuicMemSliceStorage storage(nullptr, 0, nullptr, 0);
1974 EXPECT_EQ(MessageResult(MESSAGE_STATUS_ENCRYPTION_NOT_ESTABLISHED, 0),
1975 session_.SendMessage(
1976 MakeSpan(connection_->helper()->GetStreamSendBufferAllocator(),
1977 "", &storage)));
1978
1979 // Finish handshake.
1980 CryptoHandshakeMessage handshake_message;
1981 session_.GetMutableCryptoStream()->OnHandshakeMessage(handshake_message);
1982 EXPECT_TRUE(session_.IsCryptoHandshakeConfirmed());
1983
1984 QuicStringPiece message;
1985 EXPECT_CALL(*connection_, SendMessage(1, _))
1986 .WillOnce(Return(MESSAGE_STATUS_SUCCESS));
1987 EXPECT_EQ(MessageResult(MESSAGE_STATUS_SUCCESS, 1),
1988 session_.SendMessage(
1989 MakeSpan(connection_->helper()->GetStreamSendBufferAllocator(),
1990 message, &storage)));
1991 // Verify message_id increases.
1992 EXPECT_CALL(*connection_, SendMessage(2, _))
1993 .WillOnce(Return(MESSAGE_STATUS_TOO_LARGE));
1994 EXPECT_EQ(MessageResult(MESSAGE_STATUS_TOO_LARGE, 0),
1995 session_.SendMessage(
1996 MakeSpan(connection_->helper()->GetStreamSendBufferAllocator(),
1997 message, &storage)));
1998 // Verify unsent message does not consume a message_id.
1999 EXPECT_CALL(*connection_, SendMessage(2, _))
2000 .WillOnce(Return(MESSAGE_STATUS_SUCCESS));
2001 EXPECT_EQ(MessageResult(MESSAGE_STATUS_SUCCESS, 2),
2002 session_.SendMessage(
2003 MakeSpan(connection_->helper()->GetStreamSendBufferAllocator(),
2004 message, &storage)));
2005
2006 QuicMessageFrame frame(1);
2007 QuicMessageFrame frame2(2);
2008 EXPECT_FALSE(session_.IsFrameOutstanding(QuicFrame(&frame)));
2009 EXPECT_FALSE(session_.IsFrameOutstanding(QuicFrame(&frame2)));
2010
2011 // Lost message 2.
2012 session_.OnMessageLost(2);
2013 EXPECT_FALSE(session_.IsFrameOutstanding(QuicFrame(&frame2)));
2014
2015 // message 1 gets acked.
2016 session_.OnMessageAcked(1);
2017 EXPECT_FALSE(session_.IsFrameOutstanding(QuicFrame(&frame)));
2018}
2019
2020// Regression test of b/115323618.
2021TEST_P(QuicSessionTestServer, LocallyResetZombieStreams) {
2022 QuicConnectionPeer::SetSessionDecidesWhatToWrite(connection_);
2023
2024 session_.set_writev_consumes_all_data(true);
2025 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
vasilvvc48c8712019-03-11 13:38:16 -07002026 std::string body(100, '.');
QUICHE teama6ef0a62019-03-07 20:34:33 -05002027 stream2->CloseReadSide();
2028 stream2->WriteOrBufferData(body, true, nullptr);
2029 EXPECT_TRUE(stream2->IsWaitingForAcks());
2030 // Verify stream2 is a zombie streams.
2031 EXPECT_TRUE(QuicContainsKey(session_.zombie_streams(), stream2->id()));
2032
2033 QuicStreamFrame frame(stream2->id(), true, 0, 100);
2034 EXPECT_CALL(*stream2, HasPendingRetransmission())
2035 .WillRepeatedly(Return(true));
2036 session_.OnFrameLost(QuicFrame(frame));
2037
2038 // Reset stream2 locally.
2039 EXPECT_CALL(*connection_, SendControlFrame(_))
2040 .WillRepeatedly(Invoke(&session_, &TestSession::ClearControlFrame));
2041 EXPECT_CALL(*connection_, OnStreamReset(stream2->id(), _));
2042 stream2->Reset(QUIC_STREAM_CANCELLED);
2043
2044 // Verify stream 2 gets closed.
2045 EXPECT_FALSE(QuicContainsKey(session_.zombie_streams(), stream2->id()));
2046 EXPECT_TRUE(session_.IsClosedStream(stream2->id()));
2047 EXPECT_CALL(*stream2, OnCanWrite()).Times(0);
2048 session_.OnCanWrite();
2049}
2050
2051TEST_P(QuicSessionTestServer, CleanUpClosedStreamsAlarm) {
2052 EXPECT_FALSE(
2053 QuicSessionPeer::GetCleanUpClosedStreamsAlarm(&session_)->IsSet());
2054
2055 session_.set_writev_consumes_all_data(true);
2056 TestStream* stream2 = session_.CreateOutgoingBidirectionalStream();
2057 EXPECT_FALSE(stream2->IsWaitingForAcks());
2058
2059 EXPECT_CALL(*connection_, SendControlFrame(_));
2060 EXPECT_CALL(*connection_, OnStreamReset(stream2->id(), _));
2061 session_.CloseStream(stream2->id());
2062 EXPECT_FALSE(QuicContainsKey(session_.zombie_streams(), stream2->id()));
2063 EXPECT_EQ(1u, session_.closed_streams()->size());
2064 EXPECT_TRUE(
2065 QuicSessionPeer::GetCleanUpClosedStreamsAlarm(&session_)->IsSet());
2066
2067 alarm_factory_.FireAlarm(
2068 QuicSessionPeer::GetCleanUpClosedStreamsAlarm(&session_));
2069 EXPECT_TRUE(session_.closed_streams()->empty());
2070}
2071
2072TEST_P(QuicSessionTestServer, WriteUnidirectionalStream) {
2073 session_.set_writev_consumes_all_data(true);
2074 TestStream* stream4 = new TestStream(GetNthServerInitiatedUnidirectionalId(1),
2075 &session_, WRITE_UNIDIRECTIONAL);
2076 session_.ActivateStream(QuicWrapUnique(stream4));
vasilvvc48c8712019-03-11 13:38:16 -07002077 std::string body(100, '.');
QUICHE teama6ef0a62019-03-07 20:34:33 -05002078 stream4->WriteOrBufferData(body, false, nullptr);
2079 EXPECT_FALSE(QuicContainsKey(session_.zombie_streams(), stream4->id()));
2080 stream4->WriteOrBufferData(body, true, nullptr);
2081 EXPECT_TRUE(QuicContainsKey(session_.zombie_streams(), stream4->id()));
2082}
2083
2084TEST_P(QuicSessionTestServer, ReceivedDataOnWriteUnidirectionalStream) {
2085 TestStream* stream4 = new TestStream(GetNthServerInitiatedUnidirectionalId(1),
2086 &session_, WRITE_UNIDIRECTIONAL);
2087 session_.ActivateStream(QuicWrapUnique(stream4));
2088
2089 EXPECT_CALL(
2090 *connection_,
2091 CloseConnection(QUIC_DATA_RECEIVED_ON_WRITE_UNIDIRECTIONAL_STREAM, _, _))
2092 .Times(1);
2093 QuicStreamFrame stream_frame(GetNthServerInitiatedUnidirectionalId(1), false,
2094 0, 2);
2095 session_.OnStreamFrame(stream_frame);
2096}
2097
2098TEST_P(QuicSessionTestServer, ReadUnidirectionalStream) {
2099 TestStream* stream4 = new TestStream(GetNthClientInitiatedUnidirectionalId(1),
2100 &session_, READ_UNIDIRECTIONAL);
2101 session_.ActivateStream(QuicWrapUnique(stream4));
2102 EXPECT_FALSE(stream4->IsWaitingForAcks());
2103 // Discard all incoming data.
2104 stream4->StopReading();
2105
vasilvvc48c8712019-03-11 13:38:16 -07002106 std::string data(100, '.');
QUICHE teama6ef0a62019-03-07 20:34:33 -05002107 QuicStreamFrame stream_frame(GetNthClientInitiatedUnidirectionalId(1), false,
2108 0, data);
2109 stream4->OnStreamFrame(stream_frame);
2110 EXPECT_TRUE(session_.closed_streams()->empty());
2111
2112 QuicStreamFrame stream_frame2(GetNthClientInitiatedUnidirectionalId(1), true,
2113 100, data);
2114 stream4->OnStreamFrame(stream_frame2);
2115 EXPECT_EQ(1u, session_.closed_streams()->size());
2116}
2117
2118TEST_P(QuicSessionTestServer, WriteOrBufferDataOnReadUnidirectionalStream) {
2119 TestStream* stream4 = new TestStream(GetNthClientInitiatedUnidirectionalId(1),
2120 &session_, READ_UNIDIRECTIONAL);
2121 session_.ActivateStream(QuicWrapUnique(stream4));
2122
2123 EXPECT_CALL(*connection_,
2124 CloseConnection(
2125 QUIC_TRY_TO_WRITE_DATA_ON_READ_UNIDIRECTIONAL_STREAM, _, _))
2126 .Times(1);
vasilvvc48c8712019-03-11 13:38:16 -07002127 std::string body(100, '.');
QUICHE teama6ef0a62019-03-07 20:34:33 -05002128 stream4->WriteOrBufferData(body, false, nullptr);
2129}
2130
2131TEST_P(QuicSessionTestServer, WritevDataOnReadUnidirectionalStream) {
2132 TestStream* stream4 = new TestStream(GetNthClientInitiatedUnidirectionalId(1),
2133 &session_, READ_UNIDIRECTIONAL);
2134 session_.ActivateStream(QuicWrapUnique(stream4));
2135
2136 EXPECT_CALL(*connection_,
2137 CloseConnection(
2138 QUIC_TRY_TO_WRITE_DATA_ON_READ_UNIDIRECTIONAL_STREAM, _, _))
2139 .Times(1);
vasilvvc48c8712019-03-11 13:38:16 -07002140 std::string body(100, '.');
QUICHE teama6ef0a62019-03-07 20:34:33 -05002141 struct iovec iov = {const_cast<char*>(body.data()), body.length()};
2142 QuicMemSliceStorage storage(
2143 &iov, 1, session_.connection()->helper()->GetStreamSendBufferAllocator(),
2144 1024);
2145 stream4->WriteMemSlices(storage.ToSpan(), false);
2146}
2147
2148TEST_P(QuicSessionTestServer, WriteMemSlicesOnReadUnidirectionalStream) {
2149 TestStream* stream4 = new TestStream(GetNthClientInitiatedUnidirectionalId(1),
2150 &session_, READ_UNIDIRECTIONAL);
2151 session_.ActivateStream(QuicWrapUnique(stream4));
2152
2153 EXPECT_CALL(*connection_,
2154 CloseConnection(
2155 QUIC_TRY_TO_WRITE_DATA_ON_READ_UNIDIRECTIONAL_STREAM, _, _))
2156 .Times(1);
2157 char data[1024];
2158 std::vector<std::pair<char*, size_t>> buffers;
2159 buffers.push_back(std::make_pair(data, QUIC_ARRAYSIZE(data)));
2160 buffers.push_back(std::make_pair(data, QUIC_ARRAYSIZE(data)));
2161 QuicTestMemSliceVector vector(buffers);
2162 stream4->WriteMemSlices(vector.span(), false);
2163}
2164
2165// Test code that tests that an incoming stream frame with a new (not previously
2166// seen) stream id is acceptable. The ID must not be larger than has been
2167// advertised. It may be equal to what has been advertised. These tests
2168// invoke QuicStreamIdManager::MaybeIncreaseLargestPeerStreamId by calling
2169// QuicSession::OnStreamFrame in order to check that all the steps are connected
2170// properly and that nothing in the call path interferes with the check.
2171// First test make sure that streams with ids below the limit are accepted.
2172TEST_P(QuicSessionTestServer, NewStreamIdBelowLimit) {
2173 if (transport_version() != QUIC_VERSION_99) {
2174 // Applicable only to V99
2175 return;
2176 }
2177 QuicStreamId bidirectional_stream_id =
2178 QuicSessionPeer::v99_streamid_manager(&session_)
2179 ->advertised_max_allowed_incoming_bidirectional_stream_id() -
2180 kV99StreamIdIncrement;
2181 QuicStreamFrame bidirectional_stream_frame(bidirectional_stream_id, false, 0,
2182 "Random String");
2183 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
2184 session_.OnStreamFrame(bidirectional_stream_frame);
2185
2186 QuicStreamId unidirectional_stream_id =
2187 QuicSessionPeer::v99_streamid_manager(&session_)
2188 ->advertised_max_allowed_incoming_unidirectional_stream_id() -
2189 kV99StreamIdIncrement;
2190 QuicStreamFrame unidirectional_stream_frame(unidirectional_stream_id, false,
2191 0, "Random String");
2192 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
2193 session_.OnStreamFrame(unidirectional_stream_frame);
2194}
2195
2196// Accept a stream with an ID that equals the limit.
2197TEST_P(QuicSessionTestServer, NewStreamIdAtLimit) {
2198 if (transport_version() != QUIC_VERSION_99) {
2199 // Applicable only to V99
2200 return;
2201 }
2202 QuicStreamId bidirectional_stream_id =
2203 QuicSessionPeer::v99_streamid_manager(&session_)
2204 ->advertised_max_allowed_incoming_bidirectional_stream_id();
2205 QuicStreamFrame bidirectional_stream_frame(bidirectional_stream_id, false, 0,
2206 "Random String");
2207 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
2208 session_.OnStreamFrame(bidirectional_stream_frame);
2209
2210 QuicStreamId unidirectional_stream_id =
2211 QuicSessionPeer::v99_streamid_manager(&session_)
2212 ->advertised_max_allowed_incoming_unidirectional_stream_id();
2213 QuicStreamFrame unidirectional_stream_frame(unidirectional_stream_id, false,
2214 0, "Random String");
2215 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
2216 session_.OnStreamFrame(unidirectional_stream_frame);
2217}
2218
2219// Close the connection if the id exceeds the limit.
2220TEST_P(QuicSessionTestServer, NewStreamIdAboveLimit) {
2221 if (transport_version() != QUIC_VERSION_99) {
2222 // Applicable only to V99
2223 return;
2224 }
2225 QuicStreamId bidirectional_stream_id =
2226 QuicSessionPeer::v99_streamid_manager(&session_)
2227 ->advertised_max_allowed_incoming_bidirectional_stream_id() +
2228 kV99StreamIdIncrement;
2229 QuicStreamFrame bidirectional_stream_frame(bidirectional_stream_id, false, 0,
2230 "Random String");
2231 EXPECT_CALL(*connection_, CloseConnection(QUIC_INVALID_STREAM_ID,
2232 "Stream id 404 above 400", _));
2233 session_.OnStreamFrame(bidirectional_stream_frame);
2234
2235 QuicStreamId unidirectional_stream_id =
2236 QuicSessionPeer::v99_streamid_manager(&session_)
2237 ->advertised_max_allowed_incoming_unidirectional_stream_id() +
2238 kV99StreamIdIncrement;
2239 QuicStreamFrame unidirectional_stream_frame(unidirectional_stream_id, false,
2240 0, "Random String");
2241 EXPECT_CALL(*connection_, CloseConnection(QUIC_INVALID_STREAM_ID,
2242 "Stream id 402 above 398", _));
2243 session_.OnStreamFrame(unidirectional_stream_frame);
2244}
2245
2246// Check that the OnStopSendingFrame upcall handles bad input properly
2247// First test checks that invalid stream ids are handled.
2248TEST_P(QuicSessionTestServer, OnStopSendingInputInvalidStreamId) {
2249 if (transport_version() != QUIC_VERSION_99) {
2250 // Applicable only to V99
2251 return;
2252 }
2253 // Check that "invalid" stream ids are rejected.
2254 // Note that the notion of an invalid stream id is Google-specific.
2255 QuicStopSendingFrame frame(1, -1, 123);
2256 EXPECT_CALL(
2257 *connection_,
2258 CloseConnection(QUIC_INVALID_STREAM_ID,
2259 "Received STOP_SENDING for an invalid stream", _));
2260 EXPECT_FALSE(session_.OnStopSendingFrame(frame));
2261}
2262
2263// Second test, streams in the static stream map are not subject to
2264// STOP_SENDING; it's ignored.
2265TEST_P(QuicSessionTestServer, OnStopSendingInputStaticStreams) {
2266 if (transport_version() != QUIC_VERSION_99) {
2267 // Applicable only to V99
2268 return;
2269 }
2270 // Check that a stream id in the static stream map is ignored.
2271 // Note that the notion of a static stream is Google-specific.
2272 QuicStopSendingFrame frame(1, 0, 123);
2273 EXPECT_CALL(*connection_,
2274 CloseConnection(QUIC_INVALID_STREAM_ID,
2275 "Received STOP_SENDING for a static stream", _));
2276 EXPECT_FALSE(session_.OnStopSendingFrame(frame));
2277}
2278
2279// Third test, if stream id specifies a closed stream:
2280// return true and do not close the connection.
2281TEST_P(QuicSessionTestServer, OnStopSendingInputClosedStream) {
2282 if (transport_version() != QUIC_VERSION_99) {
2283 // Applicable only to V99
2284 return;
2285 }
2286
2287 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
2288 QuicStreamId stream_id = stream->id();
2289 // Expect these as side effect of the close operations.
2290 EXPECT_CALL(*connection_, SendControlFrame(_));
2291 EXPECT_CALL(*connection_, OnStreamReset(_, _));
2292 stream->CloseWriteSide();
2293 stream->CloseReadSide();
2294 QuicStopSendingFrame frame(1, stream_id, 123);
2295 EXPECT_CALL(*connection_, CloseConnection(_, _, _)).Times(0);
2296 EXPECT_TRUE(session_.OnStopSendingFrame(frame));
2297}
2298
2299// Fourth test, if stream id specifies a nonexistent stream, return false and
2300// close the connection
2301TEST_P(QuicSessionTestServer, OnStopSendingInputNonExistentStream) {
2302 if (transport_version() != QUIC_VERSION_99) {
2303 // Applicable only to V99
2304 return;
2305 }
2306
2307 QuicStopSendingFrame frame(1, GetNthServerInitiatedBidirectionalId(123456),
2308 123);
2309 EXPECT_CALL(
2310 *connection_,
2311 CloseConnection(IETF_QUIC_PROTOCOL_VIOLATION,
2312 "Received STOP_SENDING for a non-existent stream", _))
2313 .Times(1);
2314 EXPECT_FALSE(session_.OnStopSendingFrame(frame));
2315}
2316
2317// For a valid stream, ensure that all works
2318TEST_P(QuicSessionTestServer, OnStopSendingInputValidStream) {
2319 if (transport_version() != QUIC_VERSION_99) {
2320 // Applicable only to V99
2321 return;
2322 }
2323
2324 TestStream* stream = session_.CreateOutgoingBidirectionalStream();
2325
2326 // Ensure that the stream starts out open in both directions.
2327 EXPECT_FALSE(QuicStreamPeer::write_side_closed(stream));
2328 EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream));
2329
2330 QuicStreamId stream_id = stream->id();
2331 QuicStopSendingFrame frame(1, stream_id, 123);
2332 EXPECT_CALL(*stream, OnStopSending(123));
2333 // Expect a reset to come back out.
2334 EXPECT_CALL(*connection_, SendControlFrame(_));
2335 EXPECT_CALL(
2336 *connection_,
2337 OnStreamReset(stream_id, static_cast<QuicRstStreamErrorCode>(123)));
2338 EXPECT_TRUE(session_.OnStopSendingFrame(frame));
2339 // When the STOP_SENDING is received, the node generates a RST_STREAM,
2340 // which closes the stream in the write direction. Ensure this.
2341 EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream));
2342 EXPECT_TRUE(QuicStreamPeer::write_side_closed(stream));
2343}
2344
2345} // namespace
2346} // namespace test
2347} // namespace quic