1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_session.h"
9 #include "base/basictypes.h"
10 #include "base/containers/hash_tables.h"
11 #include "base/rand_util.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "net/quic/crypto/crypto_protocol.h"
15 #include "net/quic/quic_crypto_stream.h"
16 #include "net/quic/quic_flags.h"
17 #include "net/quic/quic_protocol.h"
18 #include "net/quic/quic_utils.h"
19 #include "net/quic/reliable_quic_stream.h"
20 #include "net/quic/test_tools/quic_config_peer.h"
21 #include "net/quic/test_tools/quic_connection_peer.h"
22 #include "net/quic/test_tools/quic_data_stream_peer.h"
23 #include "net/quic/test_tools/quic_flow_controller_peer.h"
24 #include "net/quic/test_tools/quic_session_peer.h"
25 #include "net/quic/test_tools/quic_spdy_session_peer.h"
26 #include "net/quic/test_tools/quic_test_utils.h"
27 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
28 #include "net/spdy/spdy_framer.h"
29 #include "net/test/gtest_util.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gmock_mutant.h"
32 #include "testing/gtest/include/gtest/gtest.h"
38 using testing::CreateFunctor
;
39 using testing::InSequence
;
40 using testing::Invoke
;
41 using testing::Return
;
42 using testing::StrictMock
;
49 const QuicPriority kHighestPriority
= 0;
50 const QuicPriority kSomeMiddlePriority
= 3;
52 class TestCryptoStream
: public QuicCryptoStream
{
54 explicit TestCryptoStream(QuicSession
* session
)
55 : QuicCryptoStream(session
) {
58 void OnHandshakeMessage(const CryptoHandshakeMessage
& message
) override
{
59 encryption_established_
= true;
60 handshake_confirmed_
= true;
61 CryptoHandshakeMessage msg
;
63 session()->config()->SetInitialStreamFlowControlWindowToSend(
64 kInitialStreamFlowControlWindowForTest
);
65 session()->config()->SetInitialSessionFlowControlWindowToSend(
66 kInitialSessionFlowControlWindowForTest
);
67 session()->config()->ToHandshakeMessage(&msg
);
68 const QuicErrorCode error
= session()->config()->ProcessPeerHello(
69 msg
, CLIENT
, &error_details
);
70 EXPECT_EQ(QUIC_NO_ERROR
, error
);
71 session()->OnConfigNegotiated();
72 session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED
);
75 MOCK_METHOD0(OnCanWrite
, void());
78 class TestHeadersStream
: public QuicHeadersStream
{
80 explicit TestHeadersStream(QuicSpdySession
* session
)
81 : QuicHeadersStream(session
) {}
83 MOCK_METHOD0(OnCanWrite
, void());
86 class TestStream
: public QuicDataStream
{
88 TestStream(QuicStreamId id
, QuicSpdySession
* session
)
89 : QuicDataStream(id
, session
) {}
91 using ReliableQuicStream::CloseWriteSide
;
93 uint32
ProcessData(const char* data
, uint32 data_len
) override
{
97 void SendBody(const string
& data
, bool fin
) {
98 WriteOrBufferData(data
, fin
, nullptr);
101 MOCK_METHOD0(OnCanWrite
, void());
104 // Poor man's functor for use as callback in a mock.
105 class StreamBlocker
{
107 StreamBlocker(QuicSession
* session
, QuicStreamId stream_id
)
109 stream_id_(stream_id
) {
112 void MarkWriteBlocked() {
113 session_
->MarkWriteBlocked(stream_id_
, kSomeMiddlePriority
);
117 QuicSession
* const session_
;
118 const QuicStreamId stream_id_
;
121 class TestSession
: public QuicSpdySession
{
123 explicit TestSession(QuicConnection
* connection
)
124 : QuicSpdySession(connection
, DefaultQuicConfig()),
125 crypto_stream_(this),
126 writev_consumes_all_data_(false) {
130 TestCryptoStream
* GetCryptoStream() override
{ return &crypto_stream_
; }
132 TestStream
* CreateOutgoingDynamicStream() override
{
133 TestStream
* stream
= new TestStream(GetNextStreamId(), this);
134 ActivateStream(stream
);
138 TestStream
* CreateIncomingDynamicStream(QuicStreamId id
) override
{
139 return new TestStream(id
, this);
142 bool IsClosedStream(QuicStreamId id
) {
143 return QuicSession::IsClosedStream(id
);
146 ReliableQuicStream
* GetIncomingDynamicStream(QuicStreamId stream_id
) {
147 return QuicSpdySession::GetIncomingDynamicStream(stream_id
);
150 QuicConsumedData
WritevData(
152 const QuicIOVector
& data
,
153 QuicStreamOffset offset
,
155 FecProtection fec_protection
,
156 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
) override
{
157 // Always consumes everything.
158 if (writev_consumes_all_data_
) {
159 return QuicConsumedData(data
.total_length
, fin
);
161 return QuicSession::WritevData(id
, data
, offset
, fin
, fec_protection
,
162 ack_notifier_delegate
);
166 void set_writev_consumes_all_data(bool val
) {
167 writev_consumes_all_data_
= val
;
170 QuicConsumedData
SendStreamData(QuicStreamId id
) {
172 return WritevData(id
, MakeIOVector("not empty", &iov
), 0, true,
173 MAY_FEC_PROTECT
, nullptr);
176 using QuicSession::PostProcessAfterData
;
179 StrictMock
<TestCryptoStream
> crypto_stream_
;
181 bool writev_consumes_all_data_
;
184 class QuicSessionTestBase
: public ::testing::TestWithParam
<QuicVersion
> {
186 explicit QuicSessionTestBase(Perspective perspective
)
188 new StrictMock
<MockConnection
>(perspective
,
189 SupportedVersions(GetParam()))),
190 session_(connection_
) {
191 session_
.config()->SetInitialStreamFlowControlWindowToSend(
192 kInitialStreamFlowControlWindowForTest
);
193 session_
.config()->SetInitialSessionFlowControlWindowToSend(
194 kInitialSessionFlowControlWindowForTest
);
195 headers_
[":host"] = "www.google.com";
196 headers_
[":path"] = "/index.hml";
197 headers_
[":scheme"] = "http";
199 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
201 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
202 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
203 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
204 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
205 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
206 "1zFMi5vzcns38-8_Sns; "
207 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
208 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
209 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
210 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
211 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
212 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
213 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
214 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
215 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
216 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
217 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
218 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
219 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
220 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
221 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
222 connection_
->AdvanceTime(QuicTime::Delta::FromSeconds(1));
225 void CheckClosedStreams() {
226 for (QuicStreamId i
= kCryptoStreamId
; i
< 100; i
++) {
227 if (!ContainsKey(closed_streams_
, i
)) {
228 EXPECT_FALSE(session_
.IsClosedStream(i
)) << " stream id: " << i
;
230 EXPECT_TRUE(session_
.IsClosedStream(i
)) << " stream id: " << i
;
235 void CloseStream(QuicStreamId id
) {
236 EXPECT_CALL(*connection_
, SendRstStream(id
, _
, _
));
237 session_
.CloseStream(id
);
238 closed_streams_
.insert(id
);
241 QuicVersion
version() const { return connection_
->version(); }
243 StrictMock
<MockConnection
>* connection_
;
244 TestSession session_
;
245 set
<QuicStreamId
> closed_streams_
;
246 SpdyHeaderBlock headers_
;
249 class QuicSessionTestServer
: public QuicSessionTestBase
{
251 QuicSessionTestServer() : QuicSessionTestBase(Perspective::IS_SERVER
) {}
254 INSTANTIATE_TEST_CASE_P(Tests
,
255 QuicSessionTestServer
,
256 ::testing::ValuesIn(QuicSupportedVersions()));
258 TEST_P(QuicSessionTestServer
, PeerAddress
) {
259 EXPECT_EQ(IPEndPoint(Loopback4(), kTestPort
), session_
.peer_address());
262 TEST_P(QuicSessionTestServer
, IsCryptoHandshakeConfirmed
) {
263 EXPECT_FALSE(session_
.IsCryptoHandshakeConfirmed());
264 CryptoHandshakeMessage message
;
265 session_
.GetCryptoStream()->OnHandshakeMessage(message
);
266 EXPECT_TRUE(session_
.IsCryptoHandshakeConfirmed());
269 TEST_P(QuicSessionTestServer
, IsClosedStreamDefault
) {
270 // Ensure that no streams are initially closed.
271 for (QuicStreamId i
= kCryptoStreamId
; i
< 100; i
++) {
272 EXPECT_FALSE(session_
.IsClosedStream(i
)) << "stream id: " << i
;
276 TEST_P(QuicSessionTestServer
, ImplicitlyCreatedStreams
) {
277 ASSERT_TRUE(session_
.GetIncomingDynamicStream(9) != nullptr);
278 // Both 5 and 7 should be implicitly created.
279 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 5));
280 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 7));
281 ASSERT_TRUE(session_
.GetIncomingDynamicStream(7) != nullptr);
282 ASSERT_TRUE(session_
.GetIncomingDynamicStream(5) != nullptr);
285 TEST_P(QuicSessionTestServer
, IsClosedStreamLocallyCreated
) {
286 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
287 EXPECT_EQ(2u, stream2
->id());
288 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
289 EXPECT_EQ(4u, stream4
->id());
291 CheckClosedStreams();
293 CheckClosedStreams();
295 CheckClosedStreams();
298 TEST_P(QuicSessionTestServer
, IsClosedStreamPeerCreated
) {
299 QuicStreamId stream_id1
= kClientDataStreamId1
;
300 QuicStreamId stream_id2
= kClientDataStreamId2
;
301 session_
.GetIncomingDynamicStream(stream_id1
);
302 session_
.GetIncomingDynamicStream(stream_id2
);
304 CheckClosedStreams();
305 CloseStream(stream_id1
);
306 CheckClosedStreams();
307 CloseStream(stream_id2
);
308 // Create a stream explicitly, and another implicitly.
309 ReliableQuicStream
* stream3
=
310 session_
.GetIncomingDynamicStream(stream_id2
+ 4);
311 CheckClosedStreams();
312 // Close one, but make sure the other is still not closed
313 CloseStream(stream3
->id());
314 CheckClosedStreams();
317 TEST_P(QuicSessionTestServer
, StreamIdTooLarge
) {
318 QuicStreamId stream_id
= kClientDataStreamId1
;
319 session_
.GetIncomingDynamicStream(stream_id
);
320 EXPECT_CALL(*connection_
, SendConnectionClose(QUIC_INVALID_STREAM_ID
));
321 session_
.GetIncomingDynamicStream(stream_id
+ kMaxStreamIdDelta
+ 2);
324 TEST_P(QuicSessionTestServer
, DebugDFatalIfMarkingClosedStreamWriteBlocked
) {
325 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
326 QuicStreamId kClosedStreamId
= stream2
->id();
328 EXPECT_CALL(*connection_
, SendRstStream(kClosedStreamId
, _
, _
));
329 stream2
->Reset(QUIC_BAD_APPLICATION_PAYLOAD
);
331 session_
.MarkWriteBlocked(kClosedStreamId
, kSomeMiddlePriority
),
332 "Marking unknown stream 2 blocked.");
335 TEST_P(QuicSessionTestServer
,
336 DebugDFatalIfMarkWriteBlockedCalledWithWrongPriority
) {
337 const QuicPriority kDifferentPriority
= 0;
339 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
340 EXPECT_NE(kDifferentPriority
, stream2
->EffectivePriority());
342 session_
.MarkWriteBlocked(stream2
->id(), kDifferentPriority
),
343 "Priorities do not match. Got: 0 Expected: 3");
346 TEST_P(QuicSessionTestServer
, OnCanWrite
) {
347 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
348 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
349 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
351 session_
.MarkWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
352 session_
.MarkWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
353 session_
.MarkWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
356 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
357 // Reregister, to test the loop limit.
358 EXPECT_CALL(*stream2
, OnCanWrite())
359 .WillOnce(Invoke(&stream2_blocker
, &StreamBlocker::MarkWriteBlocked
));
360 EXPECT_CALL(*stream6
, OnCanWrite());
361 EXPECT_CALL(*stream4
, OnCanWrite());
362 session_
.OnCanWrite();
363 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
366 TEST_P(QuicSessionTestServer
, OnCanWriteBundlesStreams
) {
367 // Drive congestion control manually.
368 MockSendAlgorithm
* send_algorithm
= new StrictMock
<MockSendAlgorithm
>;
369 QuicConnectionPeer::SetSendAlgorithm(session_
.connection(), send_algorithm
);
371 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
372 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
373 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
375 session_
.MarkWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
376 session_
.MarkWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
377 session_
.MarkWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
379 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillRepeatedly(
380 Return(QuicTime::Delta::Zero()));
381 EXPECT_CALL(*send_algorithm
, GetCongestionWindow())
382 .WillRepeatedly(Return(kMaxPacketSize
* 10));
383 EXPECT_CALL(*stream2
, OnCanWrite())
384 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
385 &session_
, &TestSession::SendStreamData
, stream2
->id()))));
386 EXPECT_CALL(*stream4
, OnCanWrite())
387 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
388 &session_
, &TestSession::SendStreamData
, stream4
->id()))));
389 EXPECT_CALL(*stream6
, OnCanWrite())
390 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
391 &session_
, &TestSession::SendStreamData
, stream6
->id()))));
393 // Expect that we only send one packet, the writes from different streams
394 // should be bundled together.
395 MockPacketWriter
* writer
=
396 static_cast<MockPacketWriter
*>(
397 QuicConnectionPeer::GetWriter(session_
.connection()));
398 EXPECT_CALL(*writer
, WritePacket(_
, _
, _
, _
)).WillOnce(
399 Return(WriteResult(WRITE_STATUS_OK
, 0)));
400 EXPECT_CALL(*send_algorithm
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
401 session_
.OnCanWrite();
402 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
405 TEST_P(QuicSessionTestServer
, OnCanWriteCongestionControlBlocks
) {
408 // Drive congestion control manually.
409 MockSendAlgorithm
* send_algorithm
= new StrictMock
<MockSendAlgorithm
>;
410 QuicConnectionPeer::SetSendAlgorithm(session_
.connection(), send_algorithm
);
412 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
413 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
414 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
416 session_
.MarkWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
417 session_
.MarkWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
418 session_
.MarkWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
420 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
421 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
422 QuicTime::Delta::Zero()));
423 EXPECT_CALL(*stream2
, OnCanWrite());
424 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
425 QuicTime::Delta::Zero()));
426 EXPECT_CALL(*stream6
, OnCanWrite());
427 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
428 QuicTime::Delta::Infinite()));
429 // stream4->OnCanWrite is not called.
431 session_
.OnCanWrite();
432 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
434 // Still congestion-control blocked.
435 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
436 QuicTime::Delta::Infinite()));
437 session_
.OnCanWrite();
438 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
440 // stream4->OnCanWrite is called once the connection stops being
441 // congestion-control blocked.
442 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
443 QuicTime::Delta::Zero()));
444 EXPECT_CALL(*stream4
, OnCanWrite());
445 session_
.OnCanWrite();
446 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
449 TEST_P(QuicSessionTestServer
, BufferedHandshake
) {
450 EXPECT_FALSE(session_
.HasPendingHandshake()); // Default value.
452 // Test that blocking other streams does not change our status.
453 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
454 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
455 stream2_blocker
.MarkWriteBlocked();
456 EXPECT_FALSE(session_
.HasPendingHandshake());
458 TestStream
* stream3
= session_
.CreateOutgoingDynamicStream();
459 StreamBlocker
stream3_blocker(&session_
, stream3
->id());
460 stream3_blocker
.MarkWriteBlocked();
461 EXPECT_FALSE(session_
.HasPendingHandshake());
463 // Blocking (due to buffering of) the Crypto stream is detected.
464 session_
.MarkWriteBlocked(kCryptoStreamId
, kHighestPriority
);
465 EXPECT_TRUE(session_
.HasPendingHandshake());
467 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
468 StreamBlocker
stream4_blocker(&session_
, stream4
->id());
469 stream4_blocker
.MarkWriteBlocked();
470 EXPECT_TRUE(session_
.HasPendingHandshake());
473 // Force most streams to re-register, which is common scenario when we block
474 // the Crypto stream, and only the crypto stream can "really" write.
476 // Due to prioritization, we *should* be asked to write the crypto stream
478 // Don't re-register the crypto stream (which signals complete writing).
479 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
480 EXPECT_CALL(*crypto_stream
, OnCanWrite());
482 // Re-register all other streams, to show they weren't able to proceed.
483 EXPECT_CALL(*stream2
, OnCanWrite())
484 .WillOnce(Invoke(&stream2_blocker
, &StreamBlocker::MarkWriteBlocked
));
485 EXPECT_CALL(*stream3
, OnCanWrite())
486 .WillOnce(Invoke(&stream3_blocker
, &StreamBlocker::MarkWriteBlocked
));
487 EXPECT_CALL(*stream4
, OnCanWrite())
488 .WillOnce(Invoke(&stream4_blocker
, &StreamBlocker::MarkWriteBlocked
));
490 session_
.OnCanWrite();
491 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
492 EXPECT_FALSE(session_
.HasPendingHandshake()); // Crypto stream wrote.
495 TEST_P(QuicSessionTestServer
, OnCanWriteWithClosedStream
) {
496 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
497 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
498 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
500 session_
.MarkWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
501 session_
.MarkWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
502 session_
.MarkWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
503 CloseStream(stream6
->id());
506 EXPECT_CALL(*stream2
, OnCanWrite());
507 EXPECT_CALL(*stream4
, OnCanWrite());
508 session_
.OnCanWrite();
509 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
512 TEST_P(QuicSessionTestServer
, OnCanWriteLimitsNumWritesIfFlowControlBlocked
) {
513 // Ensure connection level flow control blockage.
514 QuicFlowControllerPeer::SetSendWindowOffset(session_
.flow_controller(), 0);
515 EXPECT_TRUE(session_
.flow_controller()->IsBlocked());
516 EXPECT_TRUE(session_
.IsConnectionFlowControlBlocked());
517 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
519 // Mark the crypto and headers streams as write blocked, we expect them to be
520 // allowed to write later.
521 session_
.MarkWriteBlocked(kCryptoStreamId
, kHighestPriority
);
522 session_
.MarkWriteBlocked(kHeadersStreamId
, kHighestPriority
);
524 // Create a data stream, and although it is write blocked we never expect it
525 // to be allowed to write as we are connection level flow control blocked.
526 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
527 session_
.MarkWriteBlocked(stream
->id(), kSomeMiddlePriority
);
528 EXPECT_CALL(*stream
, OnCanWrite()).Times(0);
530 // The crypto and headers streams should be called even though we are
531 // connection flow control blocked.
532 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
533 EXPECT_CALL(*crypto_stream
, OnCanWrite()).Times(1);
534 TestHeadersStream
* headers_stream
= new TestHeadersStream(&session_
);
535 QuicSpdySessionPeer::SetHeadersStream(&session_
, headers_stream
);
536 EXPECT_CALL(*headers_stream
, OnCanWrite()).Times(1);
538 session_
.OnCanWrite();
539 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
542 TEST_P(QuicSessionTestServer
, SendGoAway
) {
543 EXPECT_CALL(*connection_
, SendGoAway(QUIC_PEER_GOING_AWAY
, kHeadersStreamId
,
545 session_
.SendGoAway(QUIC_PEER_GOING_AWAY
, "Going Away.");
546 EXPECT_TRUE(session_
.goaway_sent());
548 const QuicStreamId kTestStreamId
= 5u;
549 EXPECT_CALL(*connection_
,
550 SendRstStream(kTestStreamId
, QUIC_STREAM_PEER_GOING_AWAY
, 0))
552 EXPECT_TRUE(session_
.GetIncomingDynamicStream(kTestStreamId
));
555 TEST_P(QuicSessionTestServer
, DoNotSendGoAwayTwice
) {
556 EXPECT_CALL(*connection_
, SendGoAway(QUIC_PEER_GOING_AWAY
, kHeadersStreamId
,
557 "Going Away.")).Times(1);
558 session_
.SendGoAway(QUIC_PEER_GOING_AWAY
, "Going Away.");
559 EXPECT_TRUE(session_
.goaway_sent());
560 session_
.SendGoAway(QUIC_PEER_GOING_AWAY
, "Going Away.");
563 TEST_P(QuicSessionTestServer
, IncreasedTimeoutAfterCryptoHandshake
) {
564 EXPECT_EQ(kInitialIdleTimeoutSecs
+ 3,
565 QuicConnectionPeer::GetNetworkTimeout(connection_
).ToSeconds());
566 CryptoHandshakeMessage msg
;
567 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
568 EXPECT_EQ(kMaximumIdleTimeoutSecs
+ 3,
569 QuicConnectionPeer::GetNetworkTimeout(connection_
).ToSeconds());
572 TEST_P(QuicSessionTestServer
, RstStreamBeforeHeadersDecompressed
) {
573 // Send two bytes of payload.
574 QuicStreamFrame
data1(kClientDataStreamId1
, false, 0, StringPiece("HT"));
575 vector
<QuicStreamFrame
> frames
;
576 frames
.push_back(data1
);
577 session_
.OnStreamFrames(frames
);
578 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
580 EXPECT_CALL(*connection_
, SendRstStream(kClientDataStreamId1
, _
, _
));
581 QuicRstStreamFrame
rst1(kClientDataStreamId1
, QUIC_STREAM_NO_ERROR
, 0);
582 session_
.OnRstStream(rst1
);
583 EXPECT_EQ(0u, session_
.GetNumOpenStreams());
584 // Connection should remain alive.
585 EXPECT_TRUE(connection_
->connected());
588 TEST_P(QuicSessionTestServer
, MultipleRstStreamsCauseSingleConnectionClose
) {
589 // If multiple invalid reset stream frames arrive in a single packet, this
590 // should trigger a connection close. However there is no need to send
591 // multiple connection close frames.
593 // Create valid stream.
594 QuicStreamFrame
data1(kClientDataStreamId1
, false, 0, StringPiece("HT"));
595 vector
<QuicStreamFrame
> frames
;
596 frames
.push_back(data1
);
597 session_
.OnStreamFrames(frames
);
598 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
600 // Process first invalid stream reset, resulting in the connection being
602 EXPECT_CALL(*connection_
, SendConnectionClose(QUIC_INVALID_STREAM_ID
))
604 const QuicStreamId kLargeInvalidStreamId
= 99999999;
605 QuicRstStreamFrame
rst1(kLargeInvalidStreamId
, QUIC_STREAM_NO_ERROR
, 0);
606 session_
.OnRstStream(rst1
);
607 QuicConnectionPeer::CloseConnection(connection_
);
609 // Processing of second invalid stream reset should not result in the
610 // connection being closed for a second time.
611 QuicRstStreamFrame
rst2(kLargeInvalidStreamId
, QUIC_STREAM_NO_ERROR
, 0);
612 session_
.OnRstStream(rst2
);
615 TEST_P(QuicSessionTestServer
, HandshakeUnblocksFlowControlBlockedStream
) {
616 // Test that if a stream is flow control blocked, then on receipt of the SHLO
617 // containing a suitable send window offset, the stream becomes unblocked.
619 // Ensure that Writev consumes all the data it is given (simulate no socket
621 session_
.set_writev_consumes_all_data(true);
623 // Create a stream, and send enough data to make it flow control blocked.
624 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
625 string
body(kMinimumFlowControlSendWindow
, '.');
626 EXPECT_FALSE(stream2
->flow_controller()->IsBlocked());
627 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
628 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
629 EXPECT_CALL(*connection_
, SendBlocked(stream2
->id()));
630 EXPECT_CALL(*connection_
, SendBlocked(0));
631 stream2
->SendBody(body
, false);
632 EXPECT_TRUE(stream2
->flow_controller()->IsBlocked());
633 EXPECT_TRUE(session_
.IsConnectionFlowControlBlocked());
634 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
636 // The handshake message will call OnCanWrite, so the stream can resume
638 EXPECT_CALL(*stream2
, OnCanWrite());
639 // Now complete the crypto handshake, resulting in an increased flow control
641 CryptoHandshakeMessage msg
;
642 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
644 // Stream is now unblocked.
645 EXPECT_FALSE(stream2
->flow_controller()->IsBlocked());
646 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
647 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
650 TEST_P(QuicSessionTestServer
, HandshakeUnblocksFlowControlBlockedCryptoStream
) {
651 // Test that if the crypto stream is flow control blocked, then if the SHLO
652 // contains a larger send window offset, the stream becomes unblocked.
653 session_
.set_writev_consumes_all_data(true);
654 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
655 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
656 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
657 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
658 QuicHeadersStream
* headers_stream
=
659 QuicSpdySessionPeer::GetHeadersStream(&session_
);
660 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
661 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
662 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
663 // Write until the crypto stream is flow control blocked.
664 EXPECT_CALL(*connection_
, SendBlocked(kCryptoStreamId
));
665 for (QuicStreamId i
= 0;
666 !crypto_stream
->flow_controller()->IsBlocked() && i
< 1000u; i
++) {
667 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
668 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
670 CryptoHandshakeMessage crypto_message
;
671 config
.ToHandshakeMessage(&crypto_message
);
672 crypto_stream
->SendHandshakeMessage(crypto_message
);
674 EXPECT_TRUE(crypto_stream
->flow_controller()->IsBlocked());
675 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
676 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
677 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
678 EXPECT_FALSE(session_
.HasDataToWrite());
679 EXPECT_TRUE(crypto_stream
->HasBufferedData());
681 // The handshake message will call OnCanWrite, so the stream can
683 EXPECT_CALL(*crypto_stream
, OnCanWrite());
684 // Now complete the crypto handshake, resulting in an increased flow control
686 CryptoHandshakeMessage msg
;
687 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
689 // Stream is now unblocked and will no longer have buffered data.
690 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
691 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
692 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
695 TEST_P(QuicSessionTestServer
,
696 HandshakeUnblocksFlowControlBlockedHeadersStream
) {
697 // Test that if the header stream is flow control blocked, then if the SHLO
698 // contains a larger send window offset, the stream becomes unblocked.
699 session_
.set_writev_consumes_all_data(true);
700 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
701 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
702 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
703 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
704 QuicHeadersStream
* headers_stream
=
705 QuicSpdySessionPeer::GetHeadersStream(&session_
);
706 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
707 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
708 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
709 QuicStreamId stream_id
= 5;
710 // Write until the header stream is flow control blocked.
711 EXPECT_CALL(*connection_
, SendBlocked(kHeadersStreamId
));
712 SpdyHeaderBlock headers
;
713 while (!headers_stream
->flow_controller()->IsBlocked() && stream_id
< 2000) {
714 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
715 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
716 headers
["header"] = base::Uint64ToString(base::RandUint64()) +
717 base::Uint64ToString(base::RandUint64()) +
718 base::Uint64ToString(base::RandUint64());
719 headers_stream
->WriteHeaders(stream_id
, headers
, true, 0, nullptr);
722 // Write once more to ensure that the headers stream has buffered data. The
723 // random headers may have exactly filled the flow control window.
724 headers_stream
->WriteHeaders(stream_id
, headers
, true, 0, nullptr);
725 EXPECT_TRUE(headers_stream
->HasBufferedData());
727 EXPECT_TRUE(headers_stream
->flow_controller()->IsBlocked());
728 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
729 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
730 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
731 EXPECT_FALSE(session_
.HasDataToWrite());
733 // Now complete the crypto handshake, resulting in an increased flow control
735 CryptoHandshakeMessage msg
;
736 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
738 // Stream is now unblocked and will no longer have buffered data.
739 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
740 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
741 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
742 EXPECT_FALSE(headers_stream
->HasBufferedData());
745 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingRstOutOfOrder
) {
746 // Test that when we receive an out of order stream RST we correctly adjust
747 // our connection level flow control receive window.
748 // On close, the stream should mark as consumed all bytes between the highest
749 // byte consumed so far and the final byte offset from the RST frame.
750 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
752 const QuicStreamOffset kByteOffset
=
753 1 + kInitialSessionFlowControlWindowForTest
/ 2;
755 // Expect no stream WINDOW_UPDATE frames, as stream read side closed.
756 EXPECT_CALL(*connection_
, SendWindowUpdate(stream
->id(), _
)).Times(0);
757 // We do expect a connection level WINDOW_UPDATE when the stream is reset.
758 EXPECT_CALL(*connection_
,
759 SendWindowUpdate(0, kInitialSessionFlowControlWindowForTest
+
760 kByteOffset
)).Times(1);
762 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
763 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
765 session_
.OnRstStream(rst_frame
);
766 session_
.PostProcessAfterData();
767 EXPECT_EQ(kByteOffset
, session_
.flow_controller()->bytes_consumed());
770 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingFinAndLocalReset
) {
771 // Test the situation where we receive a FIN on a stream, and before we fully
772 // consume all the data from the sequencer buffer we locally RST the stream.
773 // The bytes between highest consumed byte, and the final byte offset that we
774 // determined when the FIN arrived, should be marked as consumed at the
775 // connection level flow controller when the stream is reset.
776 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
778 const QuicStreamOffset kByteOffset
=
779 kInitialSessionFlowControlWindowForTest
/ 2;
780 QuicStreamFrame
frame(stream
->id(), true, kByteOffset
, StringPiece());
781 vector
<QuicStreamFrame
> frames
;
782 frames
.push_back(frame
);
783 session_
.OnStreamFrames(frames
);
784 session_
.PostProcessAfterData();
785 EXPECT_TRUE(connection_
->connected());
787 EXPECT_EQ(0u, stream
->flow_controller()->bytes_consumed());
788 EXPECT_EQ(kByteOffset
,
789 stream
->flow_controller()->highest_received_byte_offset());
791 // Reset stream locally.
792 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
793 stream
->Reset(QUIC_STREAM_CANCELLED
);
794 EXPECT_EQ(kByteOffset
, session_
.flow_controller()->bytes_consumed());
797 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingFinAfterRst
) {
798 // Test that when we RST the stream (and tear down stream state), and then
799 // receive a FIN from the peer, we correctly adjust our connection level flow
800 // control receive window.
802 // Connection starts with some non-zero highest received byte offset,
803 // due to other active streams.
804 const uint64 kInitialConnectionBytesConsumed
= 567;
805 const uint64 kInitialConnectionHighestReceivedOffset
= 1234;
806 EXPECT_LT(kInitialConnectionBytesConsumed
,
807 kInitialConnectionHighestReceivedOffset
);
808 session_
.flow_controller()->UpdateHighestReceivedOffset(
809 kInitialConnectionHighestReceivedOffset
);
810 session_
.flow_controller()->AddBytesConsumed(kInitialConnectionBytesConsumed
);
812 // Reset our stream: this results in the stream being closed locally.
813 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
814 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
815 stream
->Reset(QUIC_STREAM_CANCELLED
);
817 // Now receive a response from the peer with a FIN. We should handle this by
818 // adjusting the connection level flow control receive window to take into
819 // account the total number of bytes sent by the peer.
820 const QuicStreamOffset kByteOffset
= 5678;
821 string body
= "hello";
822 QuicStreamFrame
frame(stream
->id(), true, kByteOffset
, StringPiece(body
));
823 vector
<QuicStreamFrame
> frames
;
824 frames
.push_back(frame
);
825 session_
.OnStreamFrames(frames
);
827 QuicStreamOffset total_stream_bytes_sent_by_peer
=
828 kByteOffset
+ body
.length();
829 EXPECT_EQ(kInitialConnectionBytesConsumed
+ total_stream_bytes_sent_by_peer
,
830 session_
.flow_controller()->bytes_consumed());
832 kInitialConnectionHighestReceivedOffset
+ total_stream_bytes_sent_by_peer
,
833 session_
.flow_controller()->highest_received_byte_offset());
836 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingRstAfterRst
) {
837 // Test that when we RST the stream (and tear down stream state), and then
838 // receive a RST from the peer, we correctly adjust our connection level flow
839 // control receive window.
841 // Connection starts with some non-zero highest received byte offset,
842 // due to other active streams.
843 const uint64 kInitialConnectionBytesConsumed
= 567;
844 const uint64 kInitialConnectionHighestReceivedOffset
= 1234;
845 EXPECT_LT(kInitialConnectionBytesConsumed
,
846 kInitialConnectionHighestReceivedOffset
);
847 session_
.flow_controller()->UpdateHighestReceivedOffset(
848 kInitialConnectionHighestReceivedOffset
);
849 session_
.flow_controller()->AddBytesConsumed(kInitialConnectionBytesConsumed
);
851 // Reset our stream: this results in the stream being closed locally.
852 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
853 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
854 stream
->Reset(QUIC_STREAM_CANCELLED
);
856 // Now receive a RST from the peer. We should handle this by adjusting the
857 // connection level flow control receive window to take into account the total
858 // number of bytes sent by the peer.
859 const QuicStreamOffset kByteOffset
= 5678;
860 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
862 session_
.OnRstStream(rst_frame
);
864 EXPECT_EQ(kInitialConnectionBytesConsumed
+ kByteOffset
,
865 session_
.flow_controller()->bytes_consumed());
866 EXPECT_EQ(kInitialConnectionHighestReceivedOffset
+ kByteOffset
,
867 session_
.flow_controller()->highest_received_byte_offset());
870 TEST_P(QuicSessionTestServer
, InvalidStreamFlowControlWindowInHandshake
) {
871 // Test that receipt of an invalid (< default) stream flow control window from
872 // the peer results in the connection being torn down.
873 const uint32 kInvalidWindow
= kMinimumFlowControlSendWindow
- 1;
874 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(session_
.config(),
877 EXPECT_CALL(*connection_
,
878 SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW
));
879 session_
.OnConfigNegotiated();
882 TEST_P(QuicSessionTestServer
, InvalidSessionFlowControlWindowInHandshake
) {
883 // Test that receipt of an invalid (< default) session flow control window
884 // from the peer results in the connection being torn down.
885 const uint32 kInvalidWindow
= kMinimumFlowControlSendWindow
- 1;
886 QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow(session_
.config(),
889 EXPECT_CALL(*connection_
,
890 SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW
));
891 session_
.OnConfigNegotiated();
894 TEST_P(QuicSessionTestServer
, FlowControlWithInvalidFinalOffset
) {
895 // Test that if we receive a stream RST with a highest byte offset that
896 // violates flow control, that we close the connection.
897 const uint64 kLargeOffset
= kInitialSessionFlowControlWindowForTest
+ 1;
898 EXPECT_CALL(*connection_
,
899 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
))
902 // Check that stream frame + FIN results in connection close.
903 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
904 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
905 stream
->Reset(QUIC_STREAM_CANCELLED
);
906 QuicStreamFrame
frame(stream
->id(), true, kLargeOffset
, StringPiece());
907 vector
<QuicStreamFrame
> frames
;
908 frames
.push_back(frame
);
909 session_
.OnStreamFrames(frames
);
911 // Check that RST results in connection close.
912 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
914 session_
.OnRstStream(rst_frame
);
917 TEST_P(QuicSessionTestServer
, WindowUpdateUnblocksHeadersStream
) {
918 // Test that a flow control blocked headers stream gets unblocked on recipt of
919 // a WINDOW_UPDATE frame.
921 // Set the headers stream to be flow control blocked.
922 QuicHeadersStream
* headers_stream
=
923 QuicSpdySessionPeer::GetHeadersStream(&session_
);
924 QuicFlowControllerPeer::SetSendWindowOffset(headers_stream
->flow_controller(),
926 EXPECT_TRUE(headers_stream
->flow_controller()->IsBlocked());
927 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
928 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
930 // Unblock the headers stream by supplying a WINDOW_UPDATE.
931 QuicWindowUpdateFrame
window_update_frame(headers_stream
->id(),
932 2 * kMinimumFlowControlSendWindow
);
933 vector
<QuicWindowUpdateFrame
> frames
;
934 frames
.push_back(window_update_frame
);
935 session_
.OnWindowUpdateFrames(frames
);
936 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
937 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
938 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
941 TEST_P(QuicSessionTestServer
, TooManyUnfinishedStreamsCauseConnectionClose
) {
942 // If a buggy/malicious peer creates too many streams that are not ended with
943 // a FIN or RST then we send a connection close.
944 EXPECT_CALL(*connection_
,
945 SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS
)).Times(1);
947 const QuicStreamId kMaxStreams
= 5;
948 QuicSessionPeer::SetMaxOpenStreams(&session_
, kMaxStreams
);
950 // Create kMaxStreams + 1 data streams, and close them all without receiving a
951 // FIN or a RST_STREAM from the client.
952 const QuicStreamId kFirstStreamId
= kClientDataStreamId1
;
953 const QuicStreamId kFinalStreamId
=
954 kClientDataStreamId1
+ 2 * kMaxStreams
+ 1;
955 for (QuicStreamId i
= kFirstStreamId
; i
< kFinalStreamId
; i
+= 2) {
956 QuicStreamFrame
data1(i
, false, 0, StringPiece("HT"));
957 vector
<QuicStreamFrame
> frames
;
958 frames
.push_back(data1
);
959 session_
.OnStreamFrames(frames
);
960 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
961 EXPECT_CALL(*connection_
, SendRstStream(i
, _
, _
));
962 session_
.CloseStream(i
);
965 // Called after any new data is received by the session, and triggers the call
966 // to close the connection.
967 session_
.PostProcessAfterData();
970 TEST_P(QuicSessionTestServer
, DrainingStreamsDoNotCountAsOpened
) {
971 // Verify that a draining stream (which has received a FIN but not consumed
972 // it) does not count against the open quota (because it is closed from the
973 // protocol point of view).
974 EXPECT_CALL(*connection_
,
975 SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS
)).Times(0);
977 const QuicStreamId kMaxStreams
= 5;
978 QuicSessionPeer::SetMaxOpenStreams(&session_
, kMaxStreams
);
980 // Create kMaxStreams + 1 data streams, and mark them draining.
981 const QuicStreamId kFirstStreamId
= kClientDataStreamId1
;
982 const QuicStreamId kFinalStreamId
=
983 kClientDataStreamId1
+ 2 * kMaxStreams
+ 1;
984 for (QuicStreamId i
= kFirstStreamId
; i
< kFinalStreamId
; i
+= 2) {
985 QuicStreamFrame
data1(i
, true, 0, StringPiece("HT"));
986 vector
<QuicStreamFrame
> frames
;
987 frames
.push_back(data1
);
988 session_
.OnStreamFrames(frames
);
989 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
990 session_
.StreamDraining(i
);
991 EXPECT_EQ(0u, session_
.GetNumOpenStreams());
994 // Called after any new data is received by the session, and triggers the call
995 // to close the connection.
996 session_
.PostProcessAfterData();
999 class QuicSessionTestClient
: public QuicSessionTestBase
{
1001 QuicSessionTestClient() : QuicSessionTestBase(Perspective::IS_CLIENT
) {}
1004 INSTANTIATE_TEST_CASE_P(Tests
,
1005 QuicSessionTestClient
,
1006 ::testing::ValuesIn(QuicSupportedVersions()));
1008 TEST_P(QuicSessionTestClient
, ImplicitlyCreatedStreamsClient
) {
1009 ASSERT_TRUE(session_
.GetIncomingDynamicStream(6) != nullptr);
1010 // Both 2 and 4 should be implicitly created.
1011 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 2));
1012 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 4));
1013 ASSERT_TRUE(session_
.GetIncomingDynamicStream(2) != nullptr);
1014 ASSERT_TRUE(session_
.GetIncomingDynamicStream(4) != nullptr);
1015 // And 5 should be not implicitly created.
1016 EXPECT_FALSE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 5));