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 MarkConnectionLevelWriteBlocked() {
113 session_
->MarkConnectionLevelWriteBlocked(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
);
330 EXPECT_DEBUG_DFATAL(session_
.MarkConnectionLevelWriteBlocked(
331 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());
341 EXPECT_DEBUG_DFATAL(session_
.MarkConnectionLevelWriteBlocked(
342 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_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
352 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
353 session_
.MarkConnectionLevelWriteBlocked(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
,
360 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
361 EXPECT_CALL(*stream6
, OnCanWrite());
362 EXPECT_CALL(*stream4
, OnCanWrite());
363 session_
.OnCanWrite();
364 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
367 TEST_P(QuicSessionTestServer
, OnCanWriteBundlesStreams
) {
368 // Drive congestion control manually.
369 MockSendAlgorithm
* send_algorithm
= new StrictMock
<MockSendAlgorithm
>;
370 QuicConnectionPeer::SetSendAlgorithm(session_
.connection(), send_algorithm
);
372 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
373 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
374 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
376 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
377 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
378 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
380 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillRepeatedly(
381 Return(QuicTime::Delta::Zero()));
382 EXPECT_CALL(*send_algorithm
, GetCongestionWindow())
383 .WillRepeatedly(Return(kMaxPacketSize
* 10));
384 EXPECT_CALL(*stream2
, OnCanWrite())
385 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
386 &session_
, &TestSession::SendStreamData
, stream2
->id()))));
387 EXPECT_CALL(*stream4
, OnCanWrite())
388 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
389 &session_
, &TestSession::SendStreamData
, stream4
->id()))));
390 EXPECT_CALL(*stream6
, OnCanWrite())
391 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
392 &session_
, &TestSession::SendStreamData
, stream6
->id()))));
394 // Expect that we only send one packet, the writes from different streams
395 // should be bundled together.
396 MockPacketWriter
* writer
=
397 static_cast<MockPacketWriter
*>(
398 QuicConnectionPeer::GetWriter(session_
.connection()));
399 EXPECT_CALL(*writer
, WritePacket(_
, _
, _
, _
)).WillOnce(
400 Return(WriteResult(WRITE_STATUS_OK
, 0)));
401 EXPECT_CALL(*send_algorithm
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
402 session_
.OnCanWrite();
403 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
406 TEST_P(QuicSessionTestServer
, OnCanWriteCongestionControlBlocks
) {
409 // Drive congestion control manually.
410 MockSendAlgorithm
* send_algorithm
= new StrictMock
<MockSendAlgorithm
>;
411 QuicConnectionPeer::SetSendAlgorithm(session_
.connection(), send_algorithm
);
413 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
414 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
415 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
417 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
418 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
419 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
421 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
422 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
423 QuicTime::Delta::Zero()));
424 EXPECT_CALL(*stream2
, OnCanWrite());
425 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
426 QuicTime::Delta::Zero()));
427 EXPECT_CALL(*stream6
, OnCanWrite());
428 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
429 QuicTime::Delta::Infinite()));
430 // stream4->OnCanWrite is not called.
432 session_
.OnCanWrite();
433 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
435 // Still congestion-control blocked.
436 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
437 QuicTime::Delta::Infinite()));
438 session_
.OnCanWrite();
439 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
441 // stream4->OnCanWrite is called once the connection stops being
442 // congestion-control blocked.
443 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
444 QuicTime::Delta::Zero()));
445 EXPECT_CALL(*stream4
, OnCanWrite());
446 session_
.OnCanWrite();
447 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
450 TEST_P(QuicSessionTestServer
, BufferedHandshake
) {
451 EXPECT_FALSE(session_
.HasPendingHandshake()); // Default value.
453 // Test that blocking other streams does not change our status.
454 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
455 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
456 stream2_blocker
.MarkConnectionLevelWriteBlocked();
457 EXPECT_FALSE(session_
.HasPendingHandshake());
459 TestStream
* stream3
= session_
.CreateOutgoingDynamicStream();
460 StreamBlocker
stream3_blocker(&session_
, stream3
->id());
461 stream3_blocker
.MarkConnectionLevelWriteBlocked();
462 EXPECT_FALSE(session_
.HasPendingHandshake());
464 // Blocking (due to buffering of) the Crypto stream is detected.
465 session_
.MarkConnectionLevelWriteBlocked(kCryptoStreamId
, kHighestPriority
);
466 EXPECT_TRUE(session_
.HasPendingHandshake());
468 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
469 StreamBlocker
stream4_blocker(&session_
, stream4
->id());
470 stream4_blocker
.MarkConnectionLevelWriteBlocked();
471 EXPECT_TRUE(session_
.HasPendingHandshake());
474 // Force most streams to re-register, which is common scenario when we block
475 // the Crypto stream, and only the crypto stream can "really" write.
477 // Due to prioritization, we *should* be asked to write the crypto stream
479 // Don't re-register the crypto stream (which signals complete writing).
480 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
481 EXPECT_CALL(*crypto_stream
, OnCanWrite());
483 // Re-register all other streams, to show they weren't able to proceed.
484 EXPECT_CALL(*stream2
, OnCanWrite())
485 .WillOnce(Invoke(&stream2_blocker
,
486 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
487 EXPECT_CALL(*stream3
, OnCanWrite())
488 .WillOnce(Invoke(&stream3_blocker
,
489 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
490 EXPECT_CALL(*stream4
, OnCanWrite())
491 .WillOnce(Invoke(&stream4_blocker
,
492 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
494 session_
.OnCanWrite();
495 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
496 EXPECT_FALSE(session_
.HasPendingHandshake()); // Crypto stream wrote.
499 TEST_P(QuicSessionTestServer
, OnCanWriteWithClosedStream
) {
500 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
501 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
502 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
504 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
505 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
506 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
507 CloseStream(stream6
->id());
510 EXPECT_CALL(*stream2
, OnCanWrite());
511 EXPECT_CALL(*stream4
, OnCanWrite());
512 session_
.OnCanWrite();
513 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
516 TEST_P(QuicSessionTestServer
, OnCanWriteLimitsNumWritesIfFlowControlBlocked
) {
517 // Ensure connection level flow control blockage.
518 QuicFlowControllerPeer::SetSendWindowOffset(session_
.flow_controller(), 0);
519 EXPECT_TRUE(session_
.flow_controller()->IsBlocked());
520 EXPECT_TRUE(session_
.IsConnectionFlowControlBlocked());
521 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
523 // Mark the crypto and headers streams as write blocked, we expect them to be
524 // allowed to write later.
525 session_
.MarkConnectionLevelWriteBlocked(kCryptoStreamId
, kHighestPriority
);
526 session_
.MarkConnectionLevelWriteBlocked(kHeadersStreamId
, kHighestPriority
);
528 // Create a data stream, and although it is write blocked we never expect it
529 // to be allowed to write as we are connection level flow control blocked.
530 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
531 session_
.MarkConnectionLevelWriteBlocked(stream
->id(), kSomeMiddlePriority
);
532 EXPECT_CALL(*stream
, OnCanWrite()).Times(0);
534 // The crypto and headers streams should be called even though we are
535 // connection flow control blocked.
536 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
537 EXPECT_CALL(*crypto_stream
, OnCanWrite()).Times(1);
538 TestHeadersStream
* headers_stream
= new TestHeadersStream(&session_
);
539 QuicSpdySessionPeer::SetHeadersStream(&session_
, headers_stream
);
540 EXPECT_CALL(*headers_stream
, OnCanWrite()).Times(1);
542 session_
.OnCanWrite();
543 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
546 TEST_P(QuicSessionTestServer
, SendGoAway
) {
547 EXPECT_CALL(*connection_
, SendGoAway(QUIC_PEER_GOING_AWAY
, kHeadersStreamId
,
549 session_
.SendGoAway(QUIC_PEER_GOING_AWAY
, "Going Away.");
550 EXPECT_TRUE(session_
.goaway_sent());
552 const QuicStreamId kTestStreamId
= 5u;
553 EXPECT_CALL(*connection_
,
554 SendRstStream(kTestStreamId
, QUIC_STREAM_PEER_GOING_AWAY
, 0))
556 EXPECT_TRUE(session_
.GetIncomingDynamicStream(kTestStreamId
));
559 TEST_P(QuicSessionTestServer
, DoNotSendGoAwayTwice
) {
560 EXPECT_CALL(*connection_
, SendGoAway(QUIC_PEER_GOING_AWAY
, kHeadersStreamId
,
561 "Going Away.")).Times(1);
562 session_
.SendGoAway(QUIC_PEER_GOING_AWAY
, "Going Away.");
563 EXPECT_TRUE(session_
.goaway_sent());
564 session_
.SendGoAway(QUIC_PEER_GOING_AWAY
, "Going Away.");
567 TEST_P(QuicSessionTestServer
, IncreasedTimeoutAfterCryptoHandshake
) {
568 EXPECT_EQ(kInitialIdleTimeoutSecs
+ 3,
569 QuicConnectionPeer::GetNetworkTimeout(connection_
).ToSeconds());
570 CryptoHandshakeMessage msg
;
571 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
572 EXPECT_EQ(kMaximumIdleTimeoutSecs
+ 3,
573 QuicConnectionPeer::GetNetworkTimeout(connection_
).ToSeconds());
576 TEST_P(QuicSessionTestServer
, RstStreamBeforeHeadersDecompressed
) {
577 // Send two bytes of payload.
578 QuicStreamFrame
data1(kClientDataStreamId1
, false, 0, StringPiece("HT"));
579 session_
.OnStreamFrame(data1
);
580 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
582 EXPECT_CALL(*connection_
, SendRstStream(kClientDataStreamId1
, _
, _
));
583 QuicRstStreamFrame
rst1(kClientDataStreamId1
, QUIC_STREAM_NO_ERROR
, 0);
584 session_
.OnRstStream(rst1
);
585 EXPECT_EQ(0u, session_
.GetNumOpenStreams());
586 // Connection should remain alive.
587 EXPECT_TRUE(connection_
->connected());
590 TEST_P(QuicSessionTestServer
, MultipleRstStreamsCauseSingleConnectionClose
) {
591 // If multiple invalid reset stream frames arrive in a single packet, this
592 // should trigger a connection close. However there is no need to send
593 // multiple connection close frames.
595 // Create valid stream.
596 QuicStreamFrame
data1(kClientDataStreamId1
, false, 0, StringPiece("HT"));
597 session_
.OnStreamFrame(data1
);
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 session_
.OnStreamFrame(frame
);
782 session_
.PostProcessAfterData();
783 EXPECT_TRUE(connection_
->connected());
785 EXPECT_EQ(0u, stream
->flow_controller()->bytes_consumed());
786 EXPECT_EQ(kByteOffset
,
787 stream
->flow_controller()->highest_received_byte_offset());
789 // Reset stream locally.
790 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
791 stream
->Reset(QUIC_STREAM_CANCELLED
);
792 EXPECT_EQ(kByteOffset
, session_
.flow_controller()->bytes_consumed());
795 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingFinAfterRst
) {
796 // Test that when we RST the stream (and tear down stream state), and then
797 // receive a FIN from the peer, we correctly adjust our connection level flow
798 // control receive window.
800 // Connection starts with some non-zero highest received byte offset,
801 // due to other active streams.
802 const uint64 kInitialConnectionBytesConsumed
= 567;
803 const uint64 kInitialConnectionHighestReceivedOffset
= 1234;
804 EXPECT_LT(kInitialConnectionBytesConsumed
,
805 kInitialConnectionHighestReceivedOffset
);
806 session_
.flow_controller()->UpdateHighestReceivedOffset(
807 kInitialConnectionHighestReceivedOffset
);
808 session_
.flow_controller()->AddBytesConsumed(kInitialConnectionBytesConsumed
);
810 // Reset our stream: this results in the stream being closed locally.
811 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
812 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
813 stream
->Reset(QUIC_STREAM_CANCELLED
);
815 // Now receive a response from the peer with a FIN. We should handle this by
816 // adjusting the connection level flow control receive window to take into
817 // account the total number of bytes sent by the peer.
818 const QuicStreamOffset kByteOffset
= 5678;
819 string body
= "hello";
820 QuicStreamFrame
frame(stream
->id(), true, kByteOffset
, StringPiece(body
));
821 session_
.OnStreamFrame(frame
);
823 QuicStreamOffset total_stream_bytes_sent_by_peer
=
824 kByteOffset
+ body
.length();
825 EXPECT_EQ(kInitialConnectionBytesConsumed
+ total_stream_bytes_sent_by_peer
,
826 session_
.flow_controller()->bytes_consumed());
828 kInitialConnectionHighestReceivedOffset
+ total_stream_bytes_sent_by_peer
,
829 session_
.flow_controller()->highest_received_byte_offset());
832 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingRstAfterRst
) {
833 // Test that when we RST the stream (and tear down stream state), and then
834 // receive a RST from the peer, we correctly adjust our connection level flow
835 // control receive window.
837 // Connection starts with some non-zero highest received byte offset,
838 // due to other active streams.
839 const uint64 kInitialConnectionBytesConsumed
= 567;
840 const uint64 kInitialConnectionHighestReceivedOffset
= 1234;
841 EXPECT_LT(kInitialConnectionBytesConsumed
,
842 kInitialConnectionHighestReceivedOffset
);
843 session_
.flow_controller()->UpdateHighestReceivedOffset(
844 kInitialConnectionHighestReceivedOffset
);
845 session_
.flow_controller()->AddBytesConsumed(kInitialConnectionBytesConsumed
);
847 // Reset our stream: this results in the stream being closed locally.
848 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
849 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
850 stream
->Reset(QUIC_STREAM_CANCELLED
);
852 // Now receive a RST from the peer. We should handle this by adjusting the
853 // connection level flow control receive window to take into account the total
854 // number of bytes sent by the peer.
855 const QuicStreamOffset kByteOffset
= 5678;
856 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
858 session_
.OnRstStream(rst_frame
);
860 EXPECT_EQ(kInitialConnectionBytesConsumed
+ kByteOffset
,
861 session_
.flow_controller()->bytes_consumed());
862 EXPECT_EQ(kInitialConnectionHighestReceivedOffset
+ kByteOffset
,
863 session_
.flow_controller()->highest_received_byte_offset());
866 TEST_P(QuicSessionTestServer
, InvalidStreamFlowControlWindowInHandshake
) {
867 // Test that receipt of an invalid (< default) stream flow control window from
868 // the peer results in the connection being torn down.
869 const uint32 kInvalidWindow
= kMinimumFlowControlSendWindow
- 1;
870 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(session_
.config(),
873 EXPECT_CALL(*connection_
,
874 SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW
));
875 session_
.OnConfigNegotiated();
878 TEST_P(QuicSessionTestServer
, InvalidSessionFlowControlWindowInHandshake
) {
879 // Test that receipt of an invalid (< default) session flow control window
880 // from the peer results in the connection being torn down.
881 const uint32 kInvalidWindow
= kMinimumFlowControlSendWindow
- 1;
882 QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow(session_
.config(),
885 EXPECT_CALL(*connection_
,
886 SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW
));
887 session_
.OnConfigNegotiated();
890 TEST_P(QuicSessionTestServer
, FlowControlWithInvalidFinalOffset
) {
891 // Test that if we receive a stream RST with a highest byte offset that
892 // violates flow control, that we close the connection.
893 const uint64 kLargeOffset
= kInitialSessionFlowControlWindowForTest
+ 1;
894 EXPECT_CALL(*connection_
,
895 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
))
898 // Check that stream frame + FIN results in connection close.
899 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
900 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
901 stream
->Reset(QUIC_STREAM_CANCELLED
);
902 QuicStreamFrame
frame(stream
->id(), true, kLargeOffset
, StringPiece());
903 session_
.OnStreamFrame(frame
);
905 // Check that RST results in connection close.
906 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
908 session_
.OnRstStream(rst_frame
);
911 TEST_P(QuicSessionTestServer
, WindowUpdateUnblocksHeadersStream
) {
912 // Test that a flow control blocked headers stream gets unblocked on recipt of
913 // a WINDOW_UPDATE frame.
915 // Set the headers stream to be flow control blocked.
916 QuicHeadersStream
* headers_stream
=
917 QuicSpdySessionPeer::GetHeadersStream(&session_
);
918 QuicFlowControllerPeer::SetSendWindowOffset(headers_stream
->flow_controller(),
920 EXPECT_TRUE(headers_stream
->flow_controller()->IsBlocked());
921 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
922 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
924 // Unblock the headers stream by supplying a WINDOW_UPDATE.
925 QuicWindowUpdateFrame
window_update_frame(headers_stream
->id(),
926 2 * kMinimumFlowControlSendWindow
);
927 session_
.OnWindowUpdateFrame(window_update_frame
);
928 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
929 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
930 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
933 TEST_P(QuicSessionTestServer
, TooManyUnfinishedStreamsCauseConnectionClose
) {
934 // If a buggy/malicious peer creates too many streams that are not ended with
935 // a FIN or RST then we send a connection close.
936 EXPECT_CALL(*connection_
,
937 SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS
)).Times(1);
939 const QuicStreamId kMaxStreams
= 5;
940 QuicSessionPeer::SetMaxOpenStreams(&session_
, kMaxStreams
);
942 // Create kMaxStreams + 1 data streams, and close them all without receiving a
943 // FIN or a RST_STREAM from the client.
944 const QuicStreamId kFirstStreamId
= kClientDataStreamId1
;
945 const QuicStreamId kFinalStreamId
=
946 kClientDataStreamId1
+ 2 * kMaxStreams
+ 1;
947 for (QuicStreamId i
= kFirstStreamId
; i
< kFinalStreamId
; i
+= 2) {
948 QuicStreamFrame
data1(i
, false, 0, StringPiece("HT"));
949 session_
.OnStreamFrame(data1
);
950 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
951 EXPECT_CALL(*connection_
, SendRstStream(i
, _
, _
));
952 session_
.CloseStream(i
);
955 // Called after any new data is received by the session, and triggers the call
956 // to close the connection.
957 session_
.PostProcessAfterData();
960 TEST_P(QuicSessionTestServer
, DrainingStreamsDoNotCountAsOpened
) {
961 // Verify that a draining stream (which has received a FIN but not consumed
962 // it) does not count against the open quota (because it is closed from the
963 // protocol point of view).
964 EXPECT_CALL(*connection_
,
965 SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS
)).Times(0);
967 const QuicStreamId kMaxStreams
= 5;
968 QuicSessionPeer::SetMaxOpenStreams(&session_
, kMaxStreams
);
970 // Create kMaxStreams + 1 data streams, and mark them draining.
971 const QuicStreamId kFirstStreamId
= kClientDataStreamId1
;
972 const QuicStreamId kFinalStreamId
=
973 kClientDataStreamId1
+ 2 * kMaxStreams
+ 1;
974 for (QuicStreamId i
= kFirstStreamId
; i
< kFinalStreamId
; i
+= 2) {
975 QuicStreamFrame
data1(i
, true, 0, StringPiece("HT"));
976 session_
.OnStreamFrame(data1
);
977 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
978 session_
.StreamDraining(i
);
979 EXPECT_EQ(0u, session_
.GetNumOpenStreams());
982 // Called after any new data is received by the session, and triggers the call
983 // to close the connection.
984 session_
.PostProcessAfterData();
987 class QuicSessionTestClient
: public QuicSessionTestBase
{
989 QuicSessionTestClient() : QuicSessionTestBase(Perspective::IS_CLIENT
) {}
992 INSTANTIATE_TEST_CASE_P(Tests
,
993 QuicSessionTestClient
,
994 ::testing::ValuesIn(QuicSupportedVersions()));
996 TEST_P(QuicSessionTestClient
, ImplicitlyCreatedStreamsClient
) {
997 ASSERT_TRUE(session_
.GetIncomingDynamicStream(6) != nullptr);
998 // Both 2 and 4 should be implicitly created.
999 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 2));
1000 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 4));
1001 ASSERT_TRUE(session_
.GetIncomingDynamicStream(2) != nullptr);
1002 ASSERT_TRUE(session_
.GetIncomingDynamicStream(4) != nullptr);
1003 // And 5 should be not implicitly created.
1004 EXPECT_FALSE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 5));