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_protocol.h"
17 #include "net/quic/quic_utils.h"
18 #include "net/quic/reliable_quic_stream.h"
19 #include "net/quic/test_tools/quic_config_peer.h"
20 #include "net/quic/test_tools/quic_connection_peer.h"
21 #include "net/quic/test_tools/quic_data_stream_peer.h"
22 #include "net/quic/test_tools/quic_flow_controller_peer.h"
23 #include "net/quic/test_tools/quic_session_peer.h"
24 #include "net/quic/test_tools/quic_spdy_session_peer.h"
25 #include "net/quic/test_tools/quic_test_utils.h"
26 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
27 #include "net/spdy/spdy_framer.h"
28 #include "net/test/gtest_util.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gmock_mutant.h"
31 #include "testing/gtest/include/gtest/gtest.h"
37 using testing::CreateFunctor
;
38 using testing::InSequence
;
39 using testing::Invoke
;
40 using testing::Return
;
41 using testing::StrictMock
;
48 const QuicPriority kHighestPriority
= 0;
49 const QuicPriority kSomeMiddlePriority
= 3;
51 class TestCryptoStream
: public QuicCryptoStream
{
53 explicit TestCryptoStream(QuicSession
* session
)
54 : QuicCryptoStream(session
) {
57 void OnHandshakeMessage(const CryptoHandshakeMessage
& message
) override
{
58 encryption_established_
= true;
59 handshake_confirmed_
= true;
60 CryptoHandshakeMessage msg
;
62 session()->config()->SetInitialStreamFlowControlWindowToSend(
63 kInitialStreamFlowControlWindowForTest
);
64 session()->config()->SetInitialSessionFlowControlWindowToSend(
65 kInitialSessionFlowControlWindowForTest
);
66 session()->config()->ToHandshakeMessage(&msg
);
67 const QuicErrorCode error
= session()->config()->ProcessPeerHello(
68 msg
, CLIENT
, &error_details
);
69 EXPECT_EQ(QUIC_NO_ERROR
, error
);
70 session()->OnConfigNegotiated();
71 session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED
);
74 MOCK_METHOD0(OnCanWrite
, void());
77 class TestHeadersStream
: public QuicHeadersStream
{
79 explicit TestHeadersStream(QuicSpdySession
* session
)
80 : QuicHeadersStream(session
) {}
82 MOCK_METHOD0(OnCanWrite
, void());
85 class TestStream
: public QuicDataStream
{
87 TestStream(QuicStreamId id
, QuicSpdySession
* session
)
88 : QuicDataStream(id
, session
) {}
90 using ReliableQuicStream::CloseWriteSide
;
92 void OnDataAvailable() override
{}
94 void SendBody(const string
& data
, bool fin
) {
95 WriteOrBufferData(data
, fin
, nullptr);
98 MOCK_METHOD0(OnCanWrite
, void());
101 // Poor man's functor for use as callback in a mock.
102 class StreamBlocker
{
104 StreamBlocker(QuicSession
* session
, QuicStreamId stream_id
)
106 stream_id_(stream_id
) {
109 void MarkConnectionLevelWriteBlocked() {
110 session_
->MarkConnectionLevelWriteBlocked(stream_id_
, kSomeMiddlePriority
);
114 QuicSession
* const session_
;
115 const QuicStreamId stream_id_
;
118 class TestSession
: public QuicSpdySession
{
120 explicit TestSession(QuicConnection
* connection
)
121 : QuicSpdySession(connection
, DefaultQuicConfig()),
122 crypto_stream_(this),
123 writev_consumes_all_data_(false) {
127 TestCryptoStream
* GetCryptoStream() override
{ return &crypto_stream_
; }
129 TestStream
* CreateOutgoingDynamicStream() override
{
130 TestStream
* stream
= new TestStream(GetNextStreamId(), this);
131 ActivateStream(stream
);
135 TestStream
* CreateIncomingDynamicStream(QuicStreamId id
) override
{
136 // Enforce the limit on the number of open streams.
137 if (GetNumOpenStreams() + 1 > get_max_open_streams()) {
138 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS
);
141 return new TestStream(id
, this);
145 bool IsClosedStream(QuicStreamId id
) {
146 return QuicSession::IsClosedStream(id
);
149 ReliableQuicStream
* GetIncomingDynamicStream(QuicStreamId stream_id
) {
150 return QuicSpdySession::GetIncomingDynamicStream(stream_id
);
153 QuicConsumedData
WritevData(
155 const QuicIOVector
& data
,
156 QuicStreamOffset offset
,
158 FecProtection fec_protection
,
159 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
) override
{
160 // Always consumes everything.
161 if (writev_consumes_all_data_
) {
162 return QuicConsumedData(data
.total_length
, fin
);
164 return QuicSession::WritevData(id
, data
, offset
, fin
, fec_protection
,
165 ack_notifier_delegate
);
169 void set_writev_consumes_all_data(bool val
) {
170 writev_consumes_all_data_
= val
;
173 QuicConsumedData
SendStreamData(QuicStreamId id
) {
175 return WritevData(id
, MakeIOVector("not empty", &iov
), 0, true,
176 MAY_FEC_PROTECT
, nullptr);
179 using QuicSession::PostProcessAfterData
;
182 StrictMock
<TestCryptoStream
> crypto_stream_
;
184 bool writev_consumes_all_data_
;
187 class QuicSessionTestBase
: public ::testing::TestWithParam
<QuicVersion
> {
189 explicit QuicSessionTestBase(Perspective perspective
)
191 new StrictMock
<MockConnection
>(perspective
,
192 SupportedVersions(GetParam()))),
193 session_(connection_
) {
194 session_
.config()->SetInitialStreamFlowControlWindowToSend(
195 kInitialStreamFlowControlWindowForTest
);
196 session_
.config()->SetInitialSessionFlowControlWindowToSend(
197 kInitialSessionFlowControlWindowForTest
);
198 headers_
[":host"] = "www.google.com";
199 headers_
[":path"] = "/index.hml";
200 headers_
[":scheme"] = "http";
202 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
204 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
205 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
206 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
207 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
208 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
209 "1zFMi5vzcns38-8_Sns; "
210 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
211 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
212 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
213 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
214 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
215 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
216 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
217 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
218 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
219 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
220 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
221 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
222 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
223 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
224 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
225 connection_
->AdvanceTime(QuicTime::Delta::FromSeconds(1));
228 void CheckClosedStreams() {
229 for (QuicStreamId i
= kCryptoStreamId
; i
< 100; i
++) {
230 if (!ContainsKey(closed_streams_
, i
)) {
231 EXPECT_FALSE(session_
.IsClosedStream(i
)) << " stream id: " << i
;
233 EXPECT_TRUE(session_
.IsClosedStream(i
)) << " stream id: " << i
;
238 void CloseStream(QuicStreamId id
) {
239 EXPECT_CALL(*connection_
, SendRstStream(id
, _
, _
));
240 session_
.CloseStream(id
);
241 closed_streams_
.insert(id
);
244 QuicVersion
version() const { return connection_
->version(); }
246 StrictMock
<MockConnection
>* connection_
;
247 TestSession session_
;
248 set
<QuicStreamId
> closed_streams_
;
249 SpdyHeaderBlock headers_
;
252 class QuicSessionTestServer
: public QuicSessionTestBase
{
254 QuicSessionTestServer() : QuicSessionTestBase(Perspective::IS_SERVER
) {}
257 INSTANTIATE_TEST_CASE_P(Tests
,
258 QuicSessionTestServer
,
259 ::testing::ValuesIn(QuicSupportedVersions()));
261 TEST_P(QuicSessionTestServer
, PeerAddress
) {
262 EXPECT_EQ(IPEndPoint(Loopback4(), kTestPort
), session_
.peer_address());
265 TEST_P(QuicSessionTestServer
, IsCryptoHandshakeConfirmed
) {
266 EXPECT_FALSE(session_
.IsCryptoHandshakeConfirmed());
267 CryptoHandshakeMessage message
;
268 session_
.GetCryptoStream()->OnHandshakeMessage(message
);
269 EXPECT_TRUE(session_
.IsCryptoHandshakeConfirmed());
272 TEST_P(QuicSessionTestServer
, IsClosedStreamDefault
) {
273 // Ensure that no streams are initially closed.
274 for (QuicStreamId i
= kCryptoStreamId
; i
< 100; i
++) {
275 EXPECT_FALSE(session_
.IsClosedStream(i
)) << "stream id: " << i
;
279 TEST_P(QuicSessionTestServer
, ImplicitlyCreatedStreams
) {
280 ASSERT_TRUE(session_
.GetIncomingDynamicStream(9) != nullptr);
281 // Both 5 and 7 should be implicitly created.
282 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 5));
283 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 7));
284 ASSERT_TRUE(session_
.GetIncomingDynamicStream(7) != nullptr);
285 ASSERT_TRUE(session_
.GetIncomingDynamicStream(5) != nullptr);
288 TEST_P(QuicSessionTestServer
, IsClosedStreamLocallyCreated
) {
289 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
290 EXPECT_EQ(2u, stream2
->id());
291 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
292 EXPECT_EQ(4u, stream4
->id());
294 CheckClosedStreams();
296 CheckClosedStreams();
298 CheckClosedStreams();
301 TEST_P(QuicSessionTestServer
, IsClosedStreamPeerCreated
) {
302 QuicStreamId stream_id1
= kClientDataStreamId1
;
303 QuicStreamId stream_id2
= kClientDataStreamId2
;
304 session_
.GetIncomingDynamicStream(stream_id1
);
305 session_
.GetIncomingDynamicStream(stream_id2
);
307 CheckClosedStreams();
308 CloseStream(stream_id1
);
309 CheckClosedStreams();
310 CloseStream(stream_id2
);
311 // Create a stream explicitly, and another implicitly.
312 ReliableQuicStream
* stream3
=
313 session_
.GetIncomingDynamicStream(stream_id2
+ 4);
314 CheckClosedStreams();
315 // Close one, but make sure the other is still not closed
316 CloseStream(stream3
->id());
317 CheckClosedStreams();
320 TEST_P(QuicSessionTestServer
, MaximumImplicitlyOpenedStreams
) {
321 QuicStreamId stream_id
= kClientDataStreamId1
;
322 session_
.GetIncomingDynamicStream(stream_id
);
323 EXPECT_CALL(*connection_
, SendConnectionClose(_
)).Times(0);
325 session_
.GetIncomingDynamicStream(
326 stream_id
+ 2 * (session_
.get_max_open_streams() - 1)));
329 TEST_P(QuicSessionTestServer
, TooManyImplicitlyOpenedStreams
) {
330 QuicStreamId stream_id1
= kClientDataStreamId1
;
331 // A stream ID which is too large to create.
332 QuicStreamId stream_id2
= stream_id1
+ 2 * session_
.get_max_open_streams();
333 EXPECT_NE(nullptr, session_
.GetIncomingDynamicStream(stream_id1
));
334 EXPECT_CALL(*connection_
, SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS
));
335 EXPECT_EQ(nullptr, session_
.GetIncomingDynamicStream(stream_id2
));
338 TEST_P(QuicSessionTestServer
, ManyImplicitlyOpenedStreams
) {
339 // When max_open_streams_ is 200, should be able to create 200 streams
340 // out-of-order, that is, creating the one with the largest stream ID first.
341 QuicSessionPeer::SetMaxOpenStreams(&session_
, 200);
342 QuicStreamId stream_id
= kClientDataStreamId1
;
343 // Create one stream.
344 session_
.GetIncomingDynamicStream(stream_id
);
345 EXPECT_CALL(*connection_
, SendConnectionClose(_
)).Times(0);
346 // Create the largest stream ID of a threatened total of 200 streams.
347 session_
.GetIncomingDynamicStream(stream_id
+ 2 * (200 - 1));
350 TEST_P(QuicSessionTestServer
, DebugDFatalIfMarkingClosedStreamWriteBlocked
) {
351 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
352 QuicStreamId closed_stream_id
= stream2
->id();
354 EXPECT_CALL(*connection_
, SendRstStream(closed_stream_id
, _
, _
));
355 stream2
->Reset(QUIC_BAD_APPLICATION_PAYLOAD
);
356 EXPECT_DEBUG_DFATAL(session_
.MarkConnectionLevelWriteBlocked(
357 closed_stream_id
, kSomeMiddlePriority
),
358 "Marking unknown stream 2 blocked.");
361 TEST_P(QuicSessionTestServer
,
362 DebugDFatalIfMarkWriteBlockedCalledWithWrongPriority
) {
363 const QuicPriority kDifferentPriority
= 0;
365 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
366 EXPECT_NE(kDifferentPriority
, stream2
->EffectivePriority());
367 EXPECT_DEBUG_DFATAL(session_
.MarkConnectionLevelWriteBlocked(
368 stream2
->id(), kDifferentPriority
),
369 "Priorities do not match. Got: 0 Expected: 3");
372 TEST_P(QuicSessionTestServer
, OnCanWrite
) {
373 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
374 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
375 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
377 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
378 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
379 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
382 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
383 // Reregister, to test the loop limit.
384 EXPECT_CALL(*stream2
, OnCanWrite())
385 .WillOnce(Invoke(&stream2_blocker
,
386 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
387 EXPECT_CALL(*stream6
, OnCanWrite());
388 EXPECT_CALL(*stream4
, OnCanWrite());
389 session_
.OnCanWrite();
390 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
393 TEST_P(QuicSessionTestServer
, OnCanWriteBundlesStreams
) {
394 // Drive congestion control manually.
395 MockSendAlgorithm
* send_algorithm
= new StrictMock
<MockSendAlgorithm
>;
396 QuicConnectionPeer::SetSendAlgorithm(session_
.connection(), send_algorithm
);
398 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
399 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
400 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
402 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
403 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
404 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
406 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillRepeatedly(
407 Return(QuicTime::Delta::Zero()));
408 EXPECT_CALL(*send_algorithm
, GetCongestionWindow())
409 .WillRepeatedly(Return(kMaxPacketSize
* 10));
410 EXPECT_CALL(*stream2
, OnCanWrite())
411 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
412 &session_
, &TestSession::SendStreamData
, stream2
->id()))));
413 EXPECT_CALL(*stream4
, OnCanWrite())
414 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
415 &session_
, &TestSession::SendStreamData
, stream4
->id()))));
416 EXPECT_CALL(*stream6
, OnCanWrite())
417 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
418 &session_
, &TestSession::SendStreamData
, stream6
->id()))));
420 // Expect that we only send one packet, the writes from different streams
421 // should be bundled together.
422 MockPacketWriter
* writer
=
423 static_cast<MockPacketWriter
*>(
424 QuicConnectionPeer::GetWriter(session_
.connection()));
425 EXPECT_CALL(*writer
, WritePacket(_
, _
, _
, _
)).WillOnce(
426 Return(WriteResult(WRITE_STATUS_OK
, 0)));
427 EXPECT_CALL(*send_algorithm
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
428 session_
.OnCanWrite();
429 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
432 TEST_P(QuicSessionTestServer
, OnCanWriteCongestionControlBlocks
) {
435 // Drive congestion control manually.
436 MockSendAlgorithm
* send_algorithm
= new StrictMock
<MockSendAlgorithm
>;
437 QuicConnectionPeer::SetSendAlgorithm(session_
.connection(), send_algorithm
);
439 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
440 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
441 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
443 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
444 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
445 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
447 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
448 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
449 QuicTime::Delta::Zero()));
450 EXPECT_CALL(*stream2
, OnCanWrite());
451 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
452 QuicTime::Delta::Zero()));
453 EXPECT_CALL(*stream6
, OnCanWrite());
454 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
455 QuicTime::Delta::Infinite()));
456 // stream4->OnCanWrite is not called.
458 session_
.OnCanWrite();
459 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
461 // Still congestion-control blocked.
462 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
463 QuicTime::Delta::Infinite()));
464 session_
.OnCanWrite();
465 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
467 // stream4->OnCanWrite is called once the connection stops being
468 // congestion-control blocked.
469 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
470 QuicTime::Delta::Zero()));
471 EXPECT_CALL(*stream4
, OnCanWrite());
472 session_
.OnCanWrite();
473 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
476 TEST_P(QuicSessionTestServer
, BufferedHandshake
) {
477 EXPECT_FALSE(session_
.HasPendingHandshake()); // Default value.
479 // Test that blocking other streams does not change our status.
480 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
481 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
482 stream2_blocker
.MarkConnectionLevelWriteBlocked();
483 EXPECT_FALSE(session_
.HasPendingHandshake());
485 TestStream
* stream3
= session_
.CreateOutgoingDynamicStream();
486 StreamBlocker
stream3_blocker(&session_
, stream3
->id());
487 stream3_blocker
.MarkConnectionLevelWriteBlocked();
488 EXPECT_FALSE(session_
.HasPendingHandshake());
490 // Blocking (due to buffering of) the Crypto stream is detected.
491 session_
.MarkConnectionLevelWriteBlocked(kCryptoStreamId
, kHighestPriority
);
492 EXPECT_TRUE(session_
.HasPendingHandshake());
494 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
495 StreamBlocker
stream4_blocker(&session_
, stream4
->id());
496 stream4_blocker
.MarkConnectionLevelWriteBlocked();
497 EXPECT_TRUE(session_
.HasPendingHandshake());
500 // Force most streams to re-register, which is common scenario when we block
501 // the Crypto stream, and only the crypto stream can "really" write.
503 // Due to prioritization, we *should* be asked to write the crypto stream
505 // Don't re-register the crypto stream (which signals complete writing).
506 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
507 EXPECT_CALL(*crypto_stream
, OnCanWrite());
509 // Re-register all other streams, to show they weren't able to proceed.
510 EXPECT_CALL(*stream2
, OnCanWrite())
511 .WillOnce(Invoke(&stream2_blocker
,
512 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
513 EXPECT_CALL(*stream3
, OnCanWrite())
514 .WillOnce(Invoke(&stream3_blocker
,
515 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
516 EXPECT_CALL(*stream4
, OnCanWrite())
517 .WillOnce(Invoke(&stream4_blocker
,
518 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
520 session_
.OnCanWrite();
521 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
522 EXPECT_FALSE(session_
.HasPendingHandshake()); // Crypto stream wrote.
525 TEST_P(QuicSessionTestServer
, OnCanWriteWithClosedStream
) {
526 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
527 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
528 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
530 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
531 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
532 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
533 CloseStream(stream6
->id());
536 EXPECT_CALL(*stream2
, OnCanWrite());
537 EXPECT_CALL(*stream4
, OnCanWrite());
538 session_
.OnCanWrite();
539 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
542 TEST_P(QuicSessionTestServer
, OnCanWriteLimitsNumWritesIfFlowControlBlocked
) {
543 // Ensure connection level flow control blockage.
544 QuicFlowControllerPeer::SetSendWindowOffset(session_
.flow_controller(), 0);
545 EXPECT_TRUE(session_
.flow_controller()->IsBlocked());
546 EXPECT_TRUE(session_
.IsConnectionFlowControlBlocked());
547 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
549 // Mark the crypto and headers streams as write blocked, we expect them to be
550 // allowed to write later.
551 session_
.MarkConnectionLevelWriteBlocked(kCryptoStreamId
, kHighestPriority
);
552 session_
.MarkConnectionLevelWriteBlocked(kHeadersStreamId
, kHighestPriority
);
554 // Create a data stream, and although it is write blocked we never expect it
555 // to be allowed to write as we are connection level flow control blocked.
556 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
557 session_
.MarkConnectionLevelWriteBlocked(stream
->id(), kSomeMiddlePriority
);
558 EXPECT_CALL(*stream
, OnCanWrite()).Times(0);
560 // The crypto and headers streams should be called even though we are
561 // connection flow control blocked.
562 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
563 EXPECT_CALL(*crypto_stream
, OnCanWrite()).Times(1);
564 TestHeadersStream
* headers_stream
= new TestHeadersStream(&session_
);
565 QuicSpdySessionPeer::SetHeadersStream(&session_
, headers_stream
);
566 EXPECT_CALL(*headers_stream
, OnCanWrite()).Times(1);
568 session_
.OnCanWrite();
569 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
572 TEST_P(QuicSessionTestServer
, SendGoAway
) {
573 MockPacketWriter
* writer
= static_cast<MockPacketWriter
*>(
574 QuicConnectionPeer::GetWriter(session_
.connection()));
575 EXPECT_CALL(*writer
, WritePacket(_
, _
, _
, _
))
576 .WillOnce(Return(WriteResult(WRITE_STATUS_OK
, 0)));
577 EXPECT_CALL(*connection_
, SendGoAway(_
, _
, _
))
578 .WillOnce(Invoke(connection_
, &MockConnection::ReallySendGoAway
));
579 session_
.SendGoAway(QUIC_PEER_GOING_AWAY
, "Going Away.");
580 EXPECT_TRUE(session_
.goaway_sent());
582 const QuicStreamId kTestStreamId
= 5u;
583 EXPECT_CALL(*connection_
,
584 SendRstStream(kTestStreamId
, QUIC_STREAM_PEER_GOING_AWAY
, 0))
586 EXPECT_TRUE(session_
.GetIncomingDynamicStream(kTestStreamId
));
589 TEST_P(QuicSessionTestServer
, IncreasedTimeoutAfterCryptoHandshake
) {
590 EXPECT_EQ(kInitialIdleTimeoutSecs
+ 3,
591 QuicConnectionPeer::GetNetworkTimeout(connection_
).ToSeconds());
592 CryptoHandshakeMessage msg
;
593 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
594 EXPECT_EQ(kMaximumIdleTimeoutSecs
+ 3,
595 QuicConnectionPeer::GetNetworkTimeout(connection_
).ToSeconds());
598 TEST_P(QuicSessionTestServer
, RstStreamBeforeHeadersDecompressed
) {
599 // Send two bytes of payload.
600 QuicStreamFrame
data1(kClientDataStreamId1
, false, 0, StringPiece("HT"));
601 session_
.OnStreamFrame(data1
);
602 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
604 EXPECT_CALL(*connection_
, SendRstStream(kClientDataStreamId1
, _
, _
));
605 QuicRstStreamFrame
rst1(kClientDataStreamId1
, QUIC_STREAM_NO_ERROR
, 0);
606 session_
.OnRstStream(rst1
);
607 EXPECT_EQ(0u, session_
.GetNumOpenStreams());
608 // Connection should remain alive.
609 EXPECT_TRUE(connection_
->connected());
612 TEST_P(QuicSessionTestServer
, MultipleRstStreamsCauseSingleConnectionClose
) {
613 // If multiple invalid reset stream frames arrive in a single packet, this
614 // should trigger a connection close. However there is no need to send
615 // multiple connection close frames.
617 // Create valid stream.
618 QuicStreamFrame
data1(kClientDataStreamId1
, false, 0, StringPiece("HT"));
619 session_
.OnStreamFrame(data1
);
620 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
622 // Process first invalid stream reset, resulting in the connection being
624 EXPECT_CALL(*connection_
, SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS
))
626 const QuicStreamId kLargeInvalidStreamId
= 99999999;
627 QuicRstStreamFrame
rst1(kLargeInvalidStreamId
, QUIC_STREAM_NO_ERROR
, 0);
628 session_
.OnRstStream(rst1
);
629 QuicConnectionPeer::CloseConnection(connection_
);
631 // Processing of second invalid stream reset should not result in the
632 // connection being closed for a second time.
633 QuicRstStreamFrame
rst2(kLargeInvalidStreamId
, QUIC_STREAM_NO_ERROR
, 0);
634 session_
.OnRstStream(rst2
);
637 TEST_P(QuicSessionTestServer
, HandshakeUnblocksFlowControlBlockedStream
) {
638 // Test that if a stream is flow control blocked, then on receipt of the SHLO
639 // containing a suitable send window offset, the stream becomes unblocked.
641 // Ensure that Writev consumes all the data it is given (simulate no socket
643 session_
.set_writev_consumes_all_data(true);
645 // Create a stream, and send enough data to make it flow control blocked.
646 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
647 string
body(kMinimumFlowControlSendWindow
, '.');
648 EXPECT_FALSE(stream2
->flow_controller()->IsBlocked());
649 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
650 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
651 EXPECT_CALL(*connection_
, SendBlocked(stream2
->id()));
652 EXPECT_CALL(*connection_
, SendBlocked(0));
653 stream2
->SendBody(body
, false);
654 EXPECT_TRUE(stream2
->flow_controller()->IsBlocked());
655 EXPECT_TRUE(session_
.IsConnectionFlowControlBlocked());
656 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
658 // The handshake message will call OnCanWrite, so the stream can resume
660 EXPECT_CALL(*stream2
, OnCanWrite());
661 // Now complete the crypto handshake, resulting in an increased flow control
663 CryptoHandshakeMessage msg
;
664 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
666 // Stream is now unblocked.
667 EXPECT_FALSE(stream2
->flow_controller()->IsBlocked());
668 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
669 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
672 TEST_P(QuicSessionTestServer
, HandshakeUnblocksFlowControlBlockedCryptoStream
) {
673 // Test that if the crypto stream is flow control blocked, then if the SHLO
674 // contains a larger send window offset, the stream becomes unblocked.
675 session_
.set_writev_consumes_all_data(true);
676 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
677 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
678 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
679 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
680 QuicHeadersStream
* headers_stream
=
681 QuicSpdySessionPeer::GetHeadersStream(&session_
);
682 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
683 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
684 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
685 // Write until the crypto stream is flow control blocked.
686 EXPECT_CALL(*connection_
, SendBlocked(kCryptoStreamId
));
687 for (QuicStreamId i
= 0;
688 !crypto_stream
->flow_controller()->IsBlocked() && i
< 1000u; i
++) {
689 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
690 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
692 CryptoHandshakeMessage crypto_message
;
693 config
.ToHandshakeMessage(&crypto_message
);
694 crypto_stream
->SendHandshakeMessage(crypto_message
);
696 EXPECT_TRUE(crypto_stream
->flow_controller()->IsBlocked());
697 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
698 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
699 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
700 EXPECT_FALSE(session_
.HasDataToWrite());
701 EXPECT_TRUE(crypto_stream
->HasBufferedData());
703 // The handshake message will call OnCanWrite, so the stream can
705 EXPECT_CALL(*crypto_stream
, OnCanWrite());
706 // Now complete the crypto handshake, resulting in an increased flow control
708 CryptoHandshakeMessage msg
;
709 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
711 // Stream is now unblocked and will no longer have buffered data.
712 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
713 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
714 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
717 TEST_P(QuicSessionTestServer
,
718 HandshakeUnblocksFlowControlBlockedHeadersStream
) {
719 // Test that if the header stream is flow control blocked, then if the SHLO
720 // contains a larger send window offset, the stream becomes unblocked.
721 session_
.set_writev_consumes_all_data(true);
722 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
723 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
724 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
725 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
726 QuicHeadersStream
* headers_stream
=
727 QuicSpdySessionPeer::GetHeadersStream(&session_
);
728 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
729 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
730 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
731 QuicStreamId stream_id
= 5;
732 // Write until the header stream is flow control blocked.
733 EXPECT_CALL(*connection_
, SendBlocked(kHeadersStreamId
));
734 SpdyHeaderBlock headers
;
735 while (!headers_stream
->flow_controller()->IsBlocked() && stream_id
< 2000) {
736 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
737 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
738 headers
["header"] = base::Uint64ToString(base::RandUint64()) +
739 base::Uint64ToString(base::RandUint64()) +
740 base::Uint64ToString(base::RandUint64());
741 headers_stream
->WriteHeaders(stream_id
, headers
, true, 0, nullptr);
744 // Write once more to ensure that the headers stream has buffered data. The
745 // random headers may have exactly filled the flow control window.
746 headers_stream
->WriteHeaders(stream_id
, headers
, true, 0, nullptr);
747 EXPECT_TRUE(headers_stream
->HasBufferedData());
749 EXPECT_TRUE(headers_stream
->flow_controller()->IsBlocked());
750 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
751 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
752 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
753 EXPECT_FALSE(session_
.HasDataToWrite());
755 // Now complete the crypto handshake, resulting in an increased flow control
757 CryptoHandshakeMessage msg
;
758 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
760 // Stream is now unblocked and will no longer have buffered data.
761 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
762 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
763 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
764 EXPECT_FALSE(headers_stream
->HasBufferedData());
767 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingRstOutOfOrder
) {
768 // Test that when we receive an out of order stream RST we correctly adjust
769 // our connection level flow control receive window.
770 // On close, the stream should mark as consumed all bytes between the highest
771 // byte consumed so far and the final byte offset from the RST frame.
772 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
774 const QuicStreamOffset kByteOffset
=
775 1 + kInitialSessionFlowControlWindowForTest
/ 2;
777 // Expect no stream WINDOW_UPDATE frames, as stream read side closed.
778 EXPECT_CALL(*connection_
, SendWindowUpdate(stream
->id(), _
)).Times(0);
779 // We do expect a connection level WINDOW_UPDATE when the stream is reset.
780 EXPECT_CALL(*connection_
,
781 SendWindowUpdate(0, kInitialSessionFlowControlWindowForTest
+
782 kByteOffset
)).Times(1);
784 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
785 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
787 session_
.OnRstStream(rst_frame
);
788 session_
.PostProcessAfterData();
789 EXPECT_EQ(kByteOffset
, session_
.flow_controller()->bytes_consumed());
792 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingFinAndLocalReset
) {
793 // Test the situation where we receive a FIN on a stream, and before we fully
794 // consume all the data from the sequencer buffer we locally RST the stream.
795 // The bytes between highest consumed byte, and the final byte offset that we
796 // determined when the FIN arrived, should be marked as consumed at the
797 // connection level flow controller when the stream is reset.
798 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
800 const QuicStreamOffset kByteOffset
=
801 kInitialSessionFlowControlWindowForTest
/ 2;
802 QuicStreamFrame
frame(stream
->id(), true, kByteOffset
, StringPiece());
803 session_
.OnStreamFrame(frame
);
804 session_
.PostProcessAfterData();
805 EXPECT_TRUE(connection_
->connected());
807 EXPECT_EQ(0u, stream
->flow_controller()->bytes_consumed());
808 EXPECT_EQ(kByteOffset
,
809 stream
->flow_controller()->highest_received_byte_offset());
811 // Reset stream locally.
812 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
813 stream
->Reset(QUIC_STREAM_CANCELLED
);
814 EXPECT_EQ(kByteOffset
, session_
.flow_controller()->bytes_consumed());
817 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingFinAfterRst
) {
818 // Test that when we RST the stream (and tear down stream state), and then
819 // receive a FIN from the peer, we correctly adjust our connection level flow
820 // control receive window.
822 // Connection starts with some non-zero highest received byte offset,
823 // due to other active streams.
824 const uint64 kInitialConnectionBytesConsumed
= 567;
825 const uint64 kInitialConnectionHighestReceivedOffset
= 1234;
826 EXPECT_LT(kInitialConnectionBytesConsumed
,
827 kInitialConnectionHighestReceivedOffset
);
828 session_
.flow_controller()->UpdateHighestReceivedOffset(
829 kInitialConnectionHighestReceivedOffset
);
830 session_
.flow_controller()->AddBytesConsumed(kInitialConnectionBytesConsumed
);
832 // Reset our stream: this results in the stream being closed locally.
833 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
834 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
835 stream
->Reset(QUIC_STREAM_CANCELLED
);
837 // Now receive a response from the peer with a FIN. We should handle this by
838 // adjusting the connection level flow control receive window to take into
839 // account the total number of bytes sent by the peer.
840 const QuicStreamOffset kByteOffset
= 5678;
841 string body
= "hello";
842 QuicStreamFrame
frame(stream
->id(), true, kByteOffset
, StringPiece(body
));
843 session_
.OnStreamFrame(frame
);
845 QuicStreamOffset total_stream_bytes_sent_by_peer
=
846 kByteOffset
+ body
.length();
847 EXPECT_EQ(kInitialConnectionBytesConsumed
+ total_stream_bytes_sent_by_peer
,
848 session_
.flow_controller()->bytes_consumed());
850 kInitialConnectionHighestReceivedOffset
+ total_stream_bytes_sent_by_peer
,
851 session_
.flow_controller()->highest_received_byte_offset());
854 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingRstAfterRst
) {
855 // Test that when we RST the stream (and tear down stream state), and then
856 // receive a RST from the peer, we correctly adjust our connection level flow
857 // control receive window.
859 // Connection starts with some non-zero highest received byte offset,
860 // due to other active streams.
861 const uint64 kInitialConnectionBytesConsumed
= 567;
862 const uint64 kInitialConnectionHighestReceivedOffset
= 1234;
863 EXPECT_LT(kInitialConnectionBytesConsumed
,
864 kInitialConnectionHighestReceivedOffset
);
865 session_
.flow_controller()->UpdateHighestReceivedOffset(
866 kInitialConnectionHighestReceivedOffset
);
867 session_
.flow_controller()->AddBytesConsumed(kInitialConnectionBytesConsumed
);
869 // Reset our stream: this results in the stream being closed locally.
870 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
871 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
872 stream
->Reset(QUIC_STREAM_CANCELLED
);
874 // Now receive a RST from the peer. We should handle this by adjusting the
875 // connection level flow control receive window to take into account the total
876 // number of bytes sent by the peer.
877 const QuicStreamOffset kByteOffset
= 5678;
878 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
880 session_
.OnRstStream(rst_frame
);
882 EXPECT_EQ(kInitialConnectionBytesConsumed
+ kByteOffset
,
883 session_
.flow_controller()->bytes_consumed());
884 EXPECT_EQ(kInitialConnectionHighestReceivedOffset
+ kByteOffset
,
885 session_
.flow_controller()->highest_received_byte_offset());
888 TEST_P(QuicSessionTestServer
, InvalidStreamFlowControlWindowInHandshake
) {
889 // Test that receipt of an invalid (< default) stream flow control window from
890 // the peer results in the connection being torn down.
891 const uint32 kInvalidWindow
= kMinimumFlowControlSendWindow
- 1;
892 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(session_
.config(),
895 EXPECT_CALL(*connection_
,
896 SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW
));
897 session_
.OnConfigNegotiated();
900 TEST_P(QuicSessionTestServer
, InvalidSessionFlowControlWindowInHandshake
) {
901 // Test that receipt of an invalid (< default) session flow control window
902 // from the peer results in the connection being torn down.
903 const uint32 kInvalidWindow
= kMinimumFlowControlSendWindow
- 1;
904 QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow(session_
.config(),
907 EXPECT_CALL(*connection_
,
908 SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW
));
909 session_
.OnConfigNegotiated();
912 TEST_P(QuicSessionTestServer
, FlowControlWithInvalidFinalOffset
) {
913 // Test that if we receive a stream RST with a highest byte offset that
914 // violates flow control, that we close the connection.
915 const uint64 kLargeOffset
= kInitialSessionFlowControlWindowForTest
+ 1;
916 EXPECT_CALL(*connection_
,
917 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
))
920 // Check that stream frame + FIN results in connection close.
921 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
922 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
923 stream
->Reset(QUIC_STREAM_CANCELLED
);
924 QuicStreamFrame
frame(stream
->id(), true, kLargeOffset
, StringPiece());
925 session_
.OnStreamFrame(frame
);
927 // Check that RST results in connection close.
928 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
930 session_
.OnRstStream(rst_frame
);
933 TEST_P(QuicSessionTestServer
, WindowUpdateUnblocksHeadersStream
) {
934 // Test that a flow control blocked headers stream gets unblocked on recipt of
935 // a WINDOW_UPDATE frame.
937 // Set the headers stream to be flow control blocked.
938 QuicHeadersStream
* headers_stream
=
939 QuicSpdySessionPeer::GetHeadersStream(&session_
);
940 QuicFlowControllerPeer::SetSendWindowOffset(headers_stream
->flow_controller(),
942 EXPECT_TRUE(headers_stream
->flow_controller()->IsBlocked());
943 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
944 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
946 // Unblock the headers stream by supplying a WINDOW_UPDATE.
947 QuicWindowUpdateFrame
window_update_frame(headers_stream
->id(),
948 2 * kMinimumFlowControlSendWindow
);
949 session_
.OnWindowUpdateFrame(window_update_frame
);
950 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
951 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
952 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
955 TEST_P(QuicSessionTestServer
, TooManyUnfinishedStreamsCauseConnectionClose
) {
956 // If a buggy/malicious peer creates too many streams that are not ended with
957 // a FIN or RST then we send a connection close.
958 EXPECT_CALL(*connection_
,
959 SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS
)).Times(1);
961 const QuicStreamId kMaxStreams
= 5;
962 QuicSessionPeer::SetMaxOpenStreams(&session_
, kMaxStreams
);
964 // Create kMaxStreams + 1 data streams, and close them all without receiving a
965 // FIN or a RST_STREAM from the client.
966 const QuicStreamId kFirstStreamId
= kClientDataStreamId1
;
967 const QuicStreamId kFinalStreamId
=
968 kClientDataStreamId1
+ 2 * kMaxStreams
+ 1;
969 for (QuicStreamId i
= kFirstStreamId
; i
< kFinalStreamId
; i
+= 2) {
970 QuicStreamFrame
data1(i
, false, 0, StringPiece("HT"));
971 session_
.OnStreamFrame(data1
);
972 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
973 EXPECT_CALL(*connection_
, SendRstStream(i
, _
, _
));
974 session_
.CloseStream(i
);
977 // Called after any new data is received by the session, and triggers the call
978 // to close the connection.
979 session_
.PostProcessAfterData();
982 TEST_P(QuicSessionTestServer
, DrainingStreamsDoNotCountAsOpened
) {
983 // Verify that a draining stream (which has received a FIN but not consumed
984 // it) does not count against the open quota (because it is closed from the
985 // protocol point of view).
986 EXPECT_CALL(*connection_
,
987 SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS
)).Times(0);
989 const QuicStreamId kMaxStreams
= 5;
990 QuicSessionPeer::SetMaxOpenStreams(&session_
, kMaxStreams
);
992 // Create kMaxStreams + 1 data streams, and mark them draining.
993 const QuicStreamId kFirstStreamId
= kClientDataStreamId1
;
994 const QuicStreamId kFinalStreamId
=
995 kClientDataStreamId1
+ 2 * kMaxStreams
+ 1;
996 for (QuicStreamId i
= kFirstStreamId
; i
< kFinalStreamId
; i
+= 2) {
997 QuicStreamFrame
data1(i
, true, 0, StringPiece("HT"));
998 session_
.OnStreamFrame(data1
);
999 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
1000 session_
.StreamDraining(i
);
1001 EXPECT_EQ(0u, session_
.GetNumOpenStreams());
1004 // Called after any new data is received by the session, and triggers the call
1005 // to close the connection.
1006 session_
.PostProcessAfterData();
1009 class QuicSessionTestClient
: public QuicSessionTestBase
{
1011 QuicSessionTestClient() : QuicSessionTestBase(Perspective::IS_CLIENT
) {}
1014 INSTANTIATE_TEST_CASE_P(Tests
,
1015 QuicSessionTestClient
,
1016 ::testing::ValuesIn(QuicSupportedVersions()));
1018 TEST_P(QuicSessionTestClient
, ImplicitlyCreatedStreamsClient
) {
1019 ASSERT_TRUE(session_
.GetIncomingDynamicStream(6) != nullptr);
1020 // Both 2 and 4 should be implicitly created.
1021 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 2));
1022 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 4));
1023 ASSERT_TRUE(session_
.GetIncomingDynamicStream(2) != nullptr);
1024 ASSERT_TRUE(session_
.GetIncomingDynamicStream(4) != nullptr);
1025 // And 5 should be not implicitly created.
1026 EXPECT_FALSE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 5));