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 void OnDataAvailable() override
{}
95 void SendBody(const string
& data
, bool fin
) {
96 WriteOrBufferData(data
, fin
, nullptr);
99 MOCK_METHOD0(OnCanWrite
, void());
102 // Poor man's functor for use as callback in a mock.
103 class StreamBlocker
{
105 StreamBlocker(QuicSession
* session
, QuicStreamId stream_id
)
107 stream_id_(stream_id
) {
110 void MarkConnectionLevelWriteBlocked() {
111 session_
->MarkConnectionLevelWriteBlocked(stream_id_
, kSomeMiddlePriority
);
115 QuicSession
* const session_
;
116 const QuicStreamId stream_id_
;
119 class TestSession
: public QuicSpdySession
{
121 explicit TestSession(QuicConnection
* connection
)
122 : QuicSpdySession(connection
, DefaultQuicConfig()),
123 crypto_stream_(this),
124 writev_consumes_all_data_(false) {
128 TestCryptoStream
* GetCryptoStream() override
{ return &crypto_stream_
; }
130 TestStream
* CreateOutgoingDynamicStream() override
{
131 TestStream
* stream
= new TestStream(GetNextStreamId(), this);
132 ActivateStream(stream
);
136 TestStream
* CreateIncomingDynamicStream(QuicStreamId id
) override
{
137 // Enforce the limit on the number of open streams.
138 if (GetNumOpenStreams() + 1 > get_max_open_streams()) {
139 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS
);
142 return new TestStream(id
, this);
146 bool IsClosedStream(QuicStreamId id
) {
147 return QuicSession::IsClosedStream(id
);
150 ReliableQuicStream
* GetIncomingDynamicStream(QuicStreamId stream_id
) {
151 return QuicSpdySession::GetIncomingDynamicStream(stream_id
);
154 QuicConsumedData
WritevData(
156 const QuicIOVector
& data
,
157 QuicStreamOffset offset
,
159 FecProtection fec_protection
,
160 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
) override
{
161 // Always consumes everything.
162 if (writev_consumes_all_data_
) {
163 return QuicConsumedData(data
.total_length
, fin
);
165 return QuicSession::WritevData(id
, data
, offset
, fin
, fec_protection
,
166 ack_notifier_delegate
);
170 void set_writev_consumes_all_data(bool val
) {
171 writev_consumes_all_data_
= val
;
174 QuicConsumedData
SendStreamData(QuicStreamId id
) {
176 return WritevData(id
, MakeIOVector("not empty", &iov
), 0, true,
177 MAY_FEC_PROTECT
, nullptr);
180 using QuicSession::PostProcessAfterData
;
183 StrictMock
<TestCryptoStream
> crypto_stream_
;
185 bool writev_consumes_all_data_
;
188 class QuicSessionTestBase
: public ::testing::TestWithParam
<QuicVersion
> {
190 explicit QuicSessionTestBase(Perspective perspective
)
192 new StrictMock
<MockConnection
>(perspective
,
193 SupportedVersions(GetParam()))),
194 session_(connection_
) {
195 session_
.config()->SetInitialStreamFlowControlWindowToSend(
196 kInitialStreamFlowControlWindowForTest
);
197 session_
.config()->SetInitialSessionFlowControlWindowToSend(
198 kInitialSessionFlowControlWindowForTest
);
199 headers_
[":host"] = "www.google.com";
200 headers_
[":path"] = "/index.hml";
201 headers_
[":scheme"] = "http";
203 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
205 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
206 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
207 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
208 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
209 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
210 "1zFMi5vzcns38-8_Sns; "
211 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
212 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
213 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
214 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
215 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
216 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
217 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
218 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
219 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
220 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
221 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
222 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
223 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
224 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
225 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
226 connection_
->AdvanceTime(QuicTime::Delta::FromSeconds(1));
229 void CheckClosedStreams() {
230 for (QuicStreamId i
= kCryptoStreamId
; i
< 100; i
++) {
231 if (!ContainsKey(closed_streams_
, i
)) {
232 EXPECT_FALSE(session_
.IsClosedStream(i
)) << " stream id: " << i
;
234 EXPECT_TRUE(session_
.IsClosedStream(i
)) << " stream id: " << i
;
239 void CloseStream(QuicStreamId id
) {
240 EXPECT_CALL(*connection_
, SendRstStream(id
, _
, _
));
241 session_
.CloseStream(id
);
242 closed_streams_
.insert(id
);
245 QuicVersion
version() const { return connection_
->version(); }
247 StrictMock
<MockConnection
>* connection_
;
248 TestSession session_
;
249 set
<QuicStreamId
> closed_streams_
;
250 SpdyHeaderBlock headers_
;
253 class QuicSessionTestServer
: public QuicSessionTestBase
{
255 QuicSessionTestServer() : QuicSessionTestBase(Perspective::IS_SERVER
) {}
258 INSTANTIATE_TEST_CASE_P(Tests
,
259 QuicSessionTestServer
,
260 ::testing::ValuesIn(QuicSupportedVersions()));
262 TEST_P(QuicSessionTestServer
, PeerAddress
) {
263 EXPECT_EQ(IPEndPoint(Loopback4(), kTestPort
), session_
.peer_address());
266 TEST_P(QuicSessionTestServer
, IsCryptoHandshakeConfirmed
) {
267 EXPECT_FALSE(session_
.IsCryptoHandshakeConfirmed());
268 CryptoHandshakeMessage message
;
269 session_
.GetCryptoStream()->OnHandshakeMessage(message
);
270 EXPECT_TRUE(session_
.IsCryptoHandshakeConfirmed());
273 TEST_P(QuicSessionTestServer
, IsClosedStreamDefault
) {
274 // Ensure that no streams are initially closed.
275 for (QuicStreamId i
= kCryptoStreamId
; i
< 100; i
++) {
276 EXPECT_FALSE(session_
.IsClosedStream(i
)) << "stream id: " << i
;
280 TEST_P(QuicSessionTestServer
, ImplicitlyCreatedStreams
) {
281 ASSERT_TRUE(session_
.GetIncomingDynamicStream(9) != nullptr);
282 // Both 5 and 7 should be implicitly created.
283 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 5));
284 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 7));
285 ASSERT_TRUE(session_
.GetIncomingDynamicStream(7) != nullptr);
286 ASSERT_TRUE(session_
.GetIncomingDynamicStream(5) != nullptr);
289 TEST_P(QuicSessionTestServer
, IsClosedStreamLocallyCreated
) {
290 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
291 EXPECT_EQ(2u, stream2
->id());
292 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
293 EXPECT_EQ(4u, stream4
->id());
295 CheckClosedStreams();
297 CheckClosedStreams();
299 CheckClosedStreams();
302 TEST_P(QuicSessionTestServer
, IsClosedStreamPeerCreated
) {
303 QuicStreamId stream_id1
= kClientDataStreamId1
;
304 QuicStreamId stream_id2
= kClientDataStreamId2
;
305 session_
.GetIncomingDynamicStream(stream_id1
);
306 session_
.GetIncomingDynamicStream(stream_id2
);
308 CheckClosedStreams();
309 CloseStream(stream_id1
);
310 CheckClosedStreams();
311 CloseStream(stream_id2
);
312 // Create a stream explicitly, and another implicitly.
313 ReliableQuicStream
* stream3
=
314 session_
.GetIncomingDynamicStream(stream_id2
+ 4);
315 CheckClosedStreams();
316 // Close one, but make sure the other is still not closed
317 CloseStream(stream3
->id());
318 CheckClosedStreams();
321 TEST_P(QuicSessionTestServer
, MaximumImplicitlyOpenedStreams
) {
322 QuicStreamId stream_id
= kClientDataStreamId1
;
323 session_
.GetIncomingDynamicStream(stream_id
);
324 EXPECT_CALL(*connection_
, SendConnectionClose(_
)).Times(0);
326 session_
.GetIncomingDynamicStream(
327 stream_id
+ 2 * (session_
.get_max_open_streams() - 1)));
330 TEST_P(QuicSessionTestServer
, TooManyImplicitlyOpenedStreams
) {
331 QuicStreamId stream_id1
= kClientDataStreamId1
;
332 // A stream ID which is too large to create.
333 const QuicStreamId kMaxStreamIdDelta
= 200;
334 QuicStreamId stream_id2
=
335 FLAGS_exact_stream_id_delta
336 ? stream_id1
+ 2 * session_
.get_max_open_streams()
337 : stream_id1
+ kMaxStreamIdDelta
+ 2;
338 EXPECT_NE(nullptr, session_
.GetIncomingDynamicStream(stream_id1
));
339 EXPECT_CALL(*connection_
, SendConnectionClose(FLAGS_exact_stream_id_delta
340 ? QUIC_TOO_MANY_OPEN_STREAMS
341 : QUIC_INVALID_STREAM_ID
));
342 EXPECT_EQ(nullptr, session_
.GetIncomingDynamicStream(stream_id2
));
345 TEST_P(QuicSessionTestServer
, ManyImplicitlyOpenedStreams
) {
346 // When max_open_streams_ is 200, should be able to create 200 streams
347 // out-of-order, that is, creating the one with the largest stream ID first.
348 QuicSessionPeer::SetMaxOpenStreams(&session_
, 200);
349 QuicStreamId stream_id
= kClientDataStreamId1
;
350 // Create one stream.
351 session_
.GetIncomingDynamicStream(stream_id
);
352 EXPECT_CALL(*connection_
, SendConnectionClose(_
))
353 .Times(FLAGS_exact_stream_id_delta
? 0 : 1);
354 // Create the largest stream ID of a threatened total of 200 streams.
355 session_
.GetIncomingDynamicStream(stream_id
+ 2 * (200 - 1));
358 TEST_P(QuicSessionTestServer
, DebugDFatalIfMarkingClosedStreamWriteBlocked
) {
359 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
360 QuicStreamId closed_stream_id
= stream2
->id();
362 EXPECT_CALL(*connection_
, SendRstStream(closed_stream_id
, _
, _
));
363 stream2
->Reset(QUIC_BAD_APPLICATION_PAYLOAD
);
364 EXPECT_DEBUG_DFATAL(session_
.MarkConnectionLevelWriteBlocked(
365 closed_stream_id
, kSomeMiddlePriority
),
366 "Marking unknown stream 2 blocked.");
369 TEST_P(QuicSessionTestServer
,
370 DebugDFatalIfMarkWriteBlockedCalledWithWrongPriority
) {
371 const QuicPriority kDifferentPriority
= 0;
373 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
374 EXPECT_NE(kDifferentPriority
, stream2
->EffectivePriority());
375 EXPECT_DEBUG_DFATAL(session_
.MarkConnectionLevelWriteBlocked(
376 stream2
->id(), kDifferentPriority
),
377 "Priorities do not match. Got: 0 Expected: 3");
380 TEST_P(QuicSessionTestServer
, OnCanWrite
) {
381 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
382 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
383 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
385 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
386 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
387 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
390 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
391 // Reregister, to test the loop limit.
392 EXPECT_CALL(*stream2
, OnCanWrite())
393 .WillOnce(Invoke(&stream2_blocker
,
394 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
395 EXPECT_CALL(*stream6
, OnCanWrite());
396 EXPECT_CALL(*stream4
, OnCanWrite());
397 session_
.OnCanWrite();
398 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
401 TEST_P(QuicSessionTestServer
, OnCanWriteBundlesStreams
) {
402 // Drive congestion control manually.
403 MockSendAlgorithm
* send_algorithm
= new StrictMock
<MockSendAlgorithm
>;
404 QuicConnectionPeer::SetSendAlgorithm(session_
.connection(), send_algorithm
);
406 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
407 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
408 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
410 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
411 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
412 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
414 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillRepeatedly(
415 Return(QuicTime::Delta::Zero()));
416 EXPECT_CALL(*send_algorithm
, GetCongestionWindow())
417 .WillRepeatedly(Return(kMaxPacketSize
* 10));
418 EXPECT_CALL(*stream2
, OnCanWrite())
419 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
420 &session_
, &TestSession::SendStreamData
, stream2
->id()))));
421 EXPECT_CALL(*stream4
, OnCanWrite())
422 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
423 &session_
, &TestSession::SendStreamData
, stream4
->id()))));
424 EXPECT_CALL(*stream6
, OnCanWrite())
425 .WillOnce(testing::IgnoreResult(Invoke(CreateFunctor(
426 &session_
, &TestSession::SendStreamData
, stream6
->id()))));
428 // Expect that we only send one packet, the writes from different streams
429 // should be bundled together.
430 MockPacketWriter
* writer
=
431 static_cast<MockPacketWriter
*>(
432 QuicConnectionPeer::GetWriter(session_
.connection()));
433 EXPECT_CALL(*writer
, WritePacket(_
, _
, _
, _
)).WillOnce(
434 Return(WriteResult(WRITE_STATUS_OK
, 0)));
435 EXPECT_CALL(*send_algorithm
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
436 session_
.OnCanWrite();
437 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
440 TEST_P(QuicSessionTestServer
, OnCanWriteCongestionControlBlocks
) {
443 // Drive congestion control manually.
444 MockSendAlgorithm
* send_algorithm
= new StrictMock
<MockSendAlgorithm
>;
445 QuicConnectionPeer::SetSendAlgorithm(session_
.connection(), send_algorithm
);
447 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
448 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
449 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
451 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
452 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
453 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
455 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
456 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
457 QuicTime::Delta::Zero()));
458 EXPECT_CALL(*stream2
, OnCanWrite());
459 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
460 QuicTime::Delta::Zero()));
461 EXPECT_CALL(*stream6
, OnCanWrite());
462 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
463 QuicTime::Delta::Infinite()));
464 // stream4->OnCanWrite is not called.
466 session_
.OnCanWrite();
467 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
469 // Still congestion-control blocked.
470 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
471 QuicTime::Delta::Infinite()));
472 session_
.OnCanWrite();
473 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
475 // stream4->OnCanWrite is called once the connection stops being
476 // congestion-control blocked.
477 EXPECT_CALL(*send_algorithm
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
478 QuicTime::Delta::Zero()));
479 EXPECT_CALL(*stream4
, OnCanWrite());
480 session_
.OnCanWrite();
481 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
484 TEST_P(QuicSessionTestServer
, BufferedHandshake
) {
485 EXPECT_FALSE(session_
.HasPendingHandshake()); // Default value.
487 // Test that blocking other streams does not change our status.
488 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
489 StreamBlocker
stream2_blocker(&session_
, stream2
->id());
490 stream2_blocker
.MarkConnectionLevelWriteBlocked();
491 EXPECT_FALSE(session_
.HasPendingHandshake());
493 TestStream
* stream3
= session_
.CreateOutgoingDynamicStream();
494 StreamBlocker
stream3_blocker(&session_
, stream3
->id());
495 stream3_blocker
.MarkConnectionLevelWriteBlocked();
496 EXPECT_FALSE(session_
.HasPendingHandshake());
498 // Blocking (due to buffering of) the Crypto stream is detected.
499 session_
.MarkConnectionLevelWriteBlocked(kCryptoStreamId
, kHighestPriority
);
500 EXPECT_TRUE(session_
.HasPendingHandshake());
502 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
503 StreamBlocker
stream4_blocker(&session_
, stream4
->id());
504 stream4_blocker
.MarkConnectionLevelWriteBlocked();
505 EXPECT_TRUE(session_
.HasPendingHandshake());
508 // Force most streams to re-register, which is common scenario when we block
509 // the Crypto stream, and only the crypto stream can "really" write.
511 // Due to prioritization, we *should* be asked to write the crypto stream
513 // Don't re-register the crypto stream (which signals complete writing).
514 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
515 EXPECT_CALL(*crypto_stream
, OnCanWrite());
517 // Re-register all other streams, to show they weren't able to proceed.
518 EXPECT_CALL(*stream2
, OnCanWrite())
519 .WillOnce(Invoke(&stream2_blocker
,
520 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
521 EXPECT_CALL(*stream3
, OnCanWrite())
522 .WillOnce(Invoke(&stream3_blocker
,
523 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
524 EXPECT_CALL(*stream4
, OnCanWrite())
525 .WillOnce(Invoke(&stream4_blocker
,
526 &StreamBlocker::MarkConnectionLevelWriteBlocked
));
528 session_
.OnCanWrite();
529 EXPECT_TRUE(session_
.WillingAndAbleToWrite());
530 EXPECT_FALSE(session_
.HasPendingHandshake()); // Crypto stream wrote.
533 TEST_P(QuicSessionTestServer
, OnCanWriteWithClosedStream
) {
534 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
535 TestStream
* stream4
= session_
.CreateOutgoingDynamicStream();
536 TestStream
* stream6
= session_
.CreateOutgoingDynamicStream();
538 session_
.MarkConnectionLevelWriteBlocked(stream2
->id(), kSomeMiddlePriority
);
539 session_
.MarkConnectionLevelWriteBlocked(stream6
->id(), kSomeMiddlePriority
);
540 session_
.MarkConnectionLevelWriteBlocked(stream4
->id(), kSomeMiddlePriority
);
541 CloseStream(stream6
->id());
544 EXPECT_CALL(*stream2
, OnCanWrite());
545 EXPECT_CALL(*stream4
, OnCanWrite());
546 session_
.OnCanWrite();
547 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
550 TEST_P(QuicSessionTestServer
, OnCanWriteLimitsNumWritesIfFlowControlBlocked
) {
551 // Ensure connection level flow control blockage.
552 QuicFlowControllerPeer::SetSendWindowOffset(session_
.flow_controller(), 0);
553 EXPECT_TRUE(session_
.flow_controller()->IsBlocked());
554 EXPECT_TRUE(session_
.IsConnectionFlowControlBlocked());
555 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
557 // Mark the crypto and headers streams as write blocked, we expect them to be
558 // allowed to write later.
559 session_
.MarkConnectionLevelWriteBlocked(kCryptoStreamId
, kHighestPriority
);
560 session_
.MarkConnectionLevelWriteBlocked(kHeadersStreamId
, kHighestPriority
);
562 // Create a data stream, and although it is write blocked we never expect it
563 // to be allowed to write as we are connection level flow control blocked.
564 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
565 session_
.MarkConnectionLevelWriteBlocked(stream
->id(), kSomeMiddlePriority
);
566 EXPECT_CALL(*stream
, OnCanWrite()).Times(0);
568 // The crypto and headers streams should be called even though we are
569 // connection flow control blocked.
570 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
571 EXPECT_CALL(*crypto_stream
, OnCanWrite()).Times(1);
572 TestHeadersStream
* headers_stream
= new TestHeadersStream(&session_
);
573 QuicSpdySessionPeer::SetHeadersStream(&session_
, headers_stream
);
574 EXPECT_CALL(*headers_stream
, OnCanWrite()).Times(1);
576 session_
.OnCanWrite();
577 EXPECT_FALSE(session_
.WillingAndAbleToWrite());
580 TEST_P(QuicSessionTestServer
, SendGoAway
) {
581 MockPacketWriter
* writer
= static_cast<MockPacketWriter
*>(
582 QuicConnectionPeer::GetWriter(session_
.connection()));
583 EXPECT_CALL(*writer
, WritePacket(_
, _
, _
, _
))
584 .WillOnce(Return(WriteResult(WRITE_STATUS_OK
, 0)));
585 EXPECT_CALL(*connection_
, SendGoAway(_
, _
, _
))
586 .WillOnce(Invoke(connection_
, &MockConnection::ReallySendGoAway
));
587 session_
.SendGoAway(QUIC_PEER_GOING_AWAY
, "Going Away.");
588 EXPECT_TRUE(session_
.goaway_sent());
590 const QuicStreamId kTestStreamId
= 5u;
591 EXPECT_CALL(*connection_
,
592 SendRstStream(kTestStreamId
, QUIC_STREAM_PEER_GOING_AWAY
, 0))
594 EXPECT_TRUE(session_
.GetIncomingDynamicStream(kTestStreamId
));
597 TEST_P(QuicSessionTestServer
, IncreasedTimeoutAfterCryptoHandshake
) {
598 EXPECT_EQ(kInitialIdleTimeoutSecs
+ 3,
599 QuicConnectionPeer::GetNetworkTimeout(connection_
).ToSeconds());
600 CryptoHandshakeMessage msg
;
601 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
602 EXPECT_EQ(kMaximumIdleTimeoutSecs
+ 3,
603 QuicConnectionPeer::GetNetworkTimeout(connection_
).ToSeconds());
606 TEST_P(QuicSessionTestServer
, RstStreamBeforeHeadersDecompressed
) {
607 // Send two bytes of payload.
608 QuicStreamFrame
data1(kClientDataStreamId1
, false, 0, StringPiece("HT"));
609 session_
.OnStreamFrame(data1
);
610 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
612 EXPECT_CALL(*connection_
, SendRstStream(kClientDataStreamId1
, _
, _
));
613 QuicRstStreamFrame
rst1(kClientDataStreamId1
, QUIC_STREAM_NO_ERROR
, 0);
614 session_
.OnRstStream(rst1
);
615 EXPECT_EQ(0u, session_
.GetNumOpenStreams());
616 // Connection should remain alive.
617 EXPECT_TRUE(connection_
->connected());
620 TEST_P(QuicSessionTestServer
, MultipleRstStreamsCauseSingleConnectionClose
) {
621 // If multiple invalid reset stream frames arrive in a single packet, this
622 // should trigger a connection close. However there is no need to send
623 // multiple connection close frames.
625 // Create valid stream.
626 QuicStreamFrame
data1(kClientDataStreamId1
, false, 0, StringPiece("HT"));
627 session_
.OnStreamFrame(data1
);
628 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
630 // Process first invalid stream reset, resulting in the connection being
632 EXPECT_CALL(*connection_
, SendConnectionClose(FLAGS_exact_stream_id_delta
633 ? QUIC_TOO_MANY_OPEN_STREAMS
634 : QUIC_INVALID_STREAM_ID
))
636 const QuicStreamId kLargeInvalidStreamId
= 99999999;
637 QuicRstStreamFrame
rst1(kLargeInvalidStreamId
, QUIC_STREAM_NO_ERROR
, 0);
638 session_
.OnRstStream(rst1
);
639 QuicConnectionPeer::CloseConnection(connection_
);
641 // Processing of second invalid stream reset should not result in the
642 // connection being closed for a second time.
643 QuicRstStreamFrame
rst2(kLargeInvalidStreamId
, QUIC_STREAM_NO_ERROR
, 0);
644 session_
.OnRstStream(rst2
);
647 TEST_P(QuicSessionTestServer
, HandshakeUnblocksFlowControlBlockedStream
) {
648 // Test that if a stream is flow control blocked, then on receipt of the SHLO
649 // containing a suitable send window offset, the stream becomes unblocked.
651 // Ensure that Writev consumes all the data it is given (simulate no socket
653 session_
.set_writev_consumes_all_data(true);
655 // Create a stream, and send enough data to make it flow control blocked.
656 TestStream
* stream2
= session_
.CreateOutgoingDynamicStream();
657 string
body(kMinimumFlowControlSendWindow
, '.');
658 EXPECT_FALSE(stream2
->flow_controller()->IsBlocked());
659 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
660 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
661 EXPECT_CALL(*connection_
, SendBlocked(stream2
->id()));
662 EXPECT_CALL(*connection_
, SendBlocked(0));
663 stream2
->SendBody(body
, false);
664 EXPECT_TRUE(stream2
->flow_controller()->IsBlocked());
665 EXPECT_TRUE(session_
.IsConnectionFlowControlBlocked());
666 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
668 // The handshake message will call OnCanWrite, so the stream can resume
670 EXPECT_CALL(*stream2
, OnCanWrite());
671 // Now complete the crypto handshake, resulting in an increased flow control
673 CryptoHandshakeMessage msg
;
674 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
676 // Stream is now unblocked.
677 EXPECT_FALSE(stream2
->flow_controller()->IsBlocked());
678 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
679 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
682 TEST_P(QuicSessionTestServer
, HandshakeUnblocksFlowControlBlockedCryptoStream
) {
683 // Test that if the crypto stream is flow control blocked, then if the SHLO
684 // contains a larger send window offset, the stream becomes unblocked.
685 session_
.set_writev_consumes_all_data(true);
686 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
687 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
688 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
689 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
690 QuicHeadersStream
* headers_stream
=
691 QuicSpdySessionPeer::GetHeadersStream(&session_
);
692 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
693 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
694 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
695 // Write until the crypto stream is flow control blocked.
696 EXPECT_CALL(*connection_
, SendBlocked(kCryptoStreamId
));
697 for (QuicStreamId i
= 0;
698 !crypto_stream
->flow_controller()->IsBlocked() && i
< 1000u; i
++) {
699 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
700 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
702 CryptoHandshakeMessage crypto_message
;
703 config
.ToHandshakeMessage(&crypto_message
);
704 crypto_stream
->SendHandshakeMessage(crypto_message
);
706 EXPECT_TRUE(crypto_stream
->flow_controller()->IsBlocked());
707 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
708 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
709 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
710 EXPECT_FALSE(session_
.HasDataToWrite());
711 EXPECT_TRUE(crypto_stream
->HasBufferedData());
713 // The handshake message will call OnCanWrite, so the stream can
715 EXPECT_CALL(*crypto_stream
, OnCanWrite());
716 // Now complete the crypto handshake, resulting in an increased flow control
718 CryptoHandshakeMessage msg
;
719 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
721 // Stream is now unblocked and will no longer have buffered data.
722 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
723 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
724 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
727 TEST_P(QuicSessionTestServer
,
728 HandshakeUnblocksFlowControlBlockedHeadersStream
) {
729 // Test that if the header stream is flow control blocked, then if the SHLO
730 // contains a larger send window offset, the stream becomes unblocked.
731 session_
.set_writev_consumes_all_data(true);
732 TestCryptoStream
* crypto_stream
= session_
.GetCryptoStream();
733 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
734 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
735 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
736 QuicHeadersStream
* headers_stream
=
737 QuicSpdySessionPeer::GetHeadersStream(&session_
);
738 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
739 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
740 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
741 QuicStreamId stream_id
= 5;
742 // Write until the header stream is flow control blocked.
743 EXPECT_CALL(*connection_
, SendBlocked(kHeadersStreamId
));
744 SpdyHeaderBlock headers
;
745 while (!headers_stream
->flow_controller()->IsBlocked() && stream_id
< 2000) {
746 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
747 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
748 headers
["header"] = base::Uint64ToString(base::RandUint64()) +
749 base::Uint64ToString(base::RandUint64()) +
750 base::Uint64ToString(base::RandUint64());
751 headers_stream
->WriteHeaders(stream_id
, headers
, true, 0, nullptr);
754 // Write once more to ensure that the headers stream has buffered data. The
755 // random headers may have exactly filled the flow control window.
756 headers_stream
->WriteHeaders(stream_id
, headers
, true, 0, nullptr);
757 EXPECT_TRUE(headers_stream
->HasBufferedData());
759 EXPECT_TRUE(headers_stream
->flow_controller()->IsBlocked());
760 EXPECT_FALSE(crypto_stream
->flow_controller()->IsBlocked());
761 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
762 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
763 EXPECT_FALSE(session_
.HasDataToWrite());
765 // Now complete the crypto handshake, resulting in an increased flow control
767 CryptoHandshakeMessage msg
;
768 session_
.GetCryptoStream()->OnHandshakeMessage(msg
);
770 // Stream is now unblocked and will no longer have buffered data.
771 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
772 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
773 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
774 EXPECT_FALSE(headers_stream
->HasBufferedData());
777 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingRstOutOfOrder
) {
778 // Test that when we receive an out of order stream RST we correctly adjust
779 // our connection level flow control receive window.
780 // On close, the stream should mark as consumed all bytes between the highest
781 // byte consumed so far and the final byte offset from the RST frame.
782 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
784 const QuicStreamOffset kByteOffset
=
785 1 + kInitialSessionFlowControlWindowForTest
/ 2;
787 // Expect no stream WINDOW_UPDATE frames, as stream read side closed.
788 EXPECT_CALL(*connection_
, SendWindowUpdate(stream
->id(), _
)).Times(0);
789 // We do expect a connection level WINDOW_UPDATE when the stream is reset.
790 EXPECT_CALL(*connection_
,
791 SendWindowUpdate(0, kInitialSessionFlowControlWindowForTest
+
792 kByteOffset
)).Times(1);
794 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
795 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
797 session_
.OnRstStream(rst_frame
);
798 session_
.PostProcessAfterData();
799 EXPECT_EQ(kByteOffset
, session_
.flow_controller()->bytes_consumed());
802 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingFinAndLocalReset
) {
803 // Test the situation where we receive a FIN on a stream, and before we fully
804 // consume all the data from the sequencer buffer we locally RST the stream.
805 // The bytes between highest consumed byte, and the final byte offset that we
806 // determined when the FIN arrived, should be marked as consumed at the
807 // connection level flow controller when the stream is reset.
808 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
810 const QuicStreamOffset kByteOffset
=
811 kInitialSessionFlowControlWindowForTest
/ 2;
812 QuicStreamFrame
frame(stream
->id(), true, kByteOffset
, StringPiece());
813 session_
.OnStreamFrame(frame
);
814 session_
.PostProcessAfterData();
815 EXPECT_TRUE(connection_
->connected());
817 EXPECT_EQ(0u, stream
->flow_controller()->bytes_consumed());
818 EXPECT_EQ(kByteOffset
,
819 stream
->flow_controller()->highest_received_byte_offset());
821 // Reset stream locally.
822 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
823 stream
->Reset(QUIC_STREAM_CANCELLED
);
824 EXPECT_EQ(kByteOffset
, session_
.flow_controller()->bytes_consumed());
827 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingFinAfterRst
) {
828 // Test that when we RST the stream (and tear down stream state), and then
829 // receive a FIN from the peer, we correctly adjust our connection level flow
830 // control receive window.
832 // Connection starts with some non-zero highest received byte offset,
833 // due to other active streams.
834 const uint64 kInitialConnectionBytesConsumed
= 567;
835 const uint64 kInitialConnectionHighestReceivedOffset
= 1234;
836 EXPECT_LT(kInitialConnectionBytesConsumed
,
837 kInitialConnectionHighestReceivedOffset
);
838 session_
.flow_controller()->UpdateHighestReceivedOffset(
839 kInitialConnectionHighestReceivedOffset
);
840 session_
.flow_controller()->AddBytesConsumed(kInitialConnectionBytesConsumed
);
842 // Reset our stream: this results in the stream being closed locally.
843 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
844 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
845 stream
->Reset(QUIC_STREAM_CANCELLED
);
847 // Now receive a response from the peer with a FIN. We should handle this by
848 // adjusting the connection level flow control receive window to take into
849 // account the total number of bytes sent by the peer.
850 const QuicStreamOffset kByteOffset
= 5678;
851 string body
= "hello";
852 QuicStreamFrame
frame(stream
->id(), true, kByteOffset
, StringPiece(body
));
853 session_
.OnStreamFrame(frame
);
855 QuicStreamOffset total_stream_bytes_sent_by_peer
=
856 kByteOffset
+ body
.length();
857 EXPECT_EQ(kInitialConnectionBytesConsumed
+ total_stream_bytes_sent_by_peer
,
858 session_
.flow_controller()->bytes_consumed());
860 kInitialConnectionHighestReceivedOffset
+ total_stream_bytes_sent_by_peer
,
861 session_
.flow_controller()->highest_received_byte_offset());
864 TEST_P(QuicSessionTestServer
, ConnectionFlowControlAccountingRstAfterRst
) {
865 // Test that when we RST the stream (and tear down stream state), and then
866 // receive a RST from the peer, we correctly adjust our connection level flow
867 // control receive window.
869 // Connection starts with some non-zero highest received byte offset,
870 // due to other active streams.
871 const uint64 kInitialConnectionBytesConsumed
= 567;
872 const uint64 kInitialConnectionHighestReceivedOffset
= 1234;
873 EXPECT_LT(kInitialConnectionBytesConsumed
,
874 kInitialConnectionHighestReceivedOffset
);
875 session_
.flow_controller()->UpdateHighestReceivedOffset(
876 kInitialConnectionHighestReceivedOffset
);
877 session_
.flow_controller()->AddBytesConsumed(kInitialConnectionBytesConsumed
);
879 // Reset our stream: this results in the stream being closed locally.
880 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
881 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
882 stream
->Reset(QUIC_STREAM_CANCELLED
);
884 // Now receive a RST from the peer. We should handle this by adjusting the
885 // connection level flow control receive window to take into account the total
886 // number of bytes sent by the peer.
887 const QuicStreamOffset kByteOffset
= 5678;
888 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
890 session_
.OnRstStream(rst_frame
);
892 EXPECT_EQ(kInitialConnectionBytesConsumed
+ kByteOffset
,
893 session_
.flow_controller()->bytes_consumed());
894 EXPECT_EQ(kInitialConnectionHighestReceivedOffset
+ kByteOffset
,
895 session_
.flow_controller()->highest_received_byte_offset());
898 TEST_P(QuicSessionTestServer
, InvalidStreamFlowControlWindowInHandshake
) {
899 // Test that receipt of an invalid (< default) stream flow control window from
900 // the peer results in the connection being torn down.
901 const uint32 kInvalidWindow
= kMinimumFlowControlSendWindow
- 1;
902 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(session_
.config(),
905 EXPECT_CALL(*connection_
,
906 SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW
));
907 session_
.OnConfigNegotiated();
910 TEST_P(QuicSessionTestServer
, InvalidSessionFlowControlWindowInHandshake
) {
911 // Test that receipt of an invalid (< default) session flow control window
912 // from the peer results in the connection being torn down.
913 const uint32 kInvalidWindow
= kMinimumFlowControlSendWindow
- 1;
914 QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow(session_
.config(),
917 EXPECT_CALL(*connection_
,
918 SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW
));
919 session_
.OnConfigNegotiated();
922 TEST_P(QuicSessionTestServer
, FlowControlWithInvalidFinalOffset
) {
923 // Test that if we receive a stream RST with a highest byte offset that
924 // violates flow control, that we close the connection.
925 const uint64 kLargeOffset
= kInitialSessionFlowControlWindowForTest
+ 1;
926 EXPECT_CALL(*connection_
,
927 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
))
930 // Check that stream frame + FIN results in connection close.
931 TestStream
* stream
= session_
.CreateOutgoingDynamicStream();
932 EXPECT_CALL(*connection_
, SendRstStream(stream
->id(), _
, _
));
933 stream
->Reset(QUIC_STREAM_CANCELLED
);
934 QuicStreamFrame
frame(stream
->id(), true, kLargeOffset
, StringPiece());
935 session_
.OnStreamFrame(frame
);
937 // Check that RST results in connection close.
938 QuicRstStreamFrame
rst_frame(stream
->id(), QUIC_STREAM_CANCELLED
,
940 session_
.OnRstStream(rst_frame
);
943 TEST_P(QuicSessionTestServer
, WindowUpdateUnblocksHeadersStream
) {
944 // Test that a flow control blocked headers stream gets unblocked on recipt of
945 // a WINDOW_UPDATE frame.
947 // Set the headers stream to be flow control blocked.
948 QuicHeadersStream
* headers_stream
=
949 QuicSpdySessionPeer::GetHeadersStream(&session_
);
950 QuicFlowControllerPeer::SetSendWindowOffset(headers_stream
->flow_controller(),
952 EXPECT_TRUE(headers_stream
->flow_controller()->IsBlocked());
953 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
954 EXPECT_TRUE(session_
.IsStreamFlowControlBlocked());
956 // Unblock the headers stream by supplying a WINDOW_UPDATE.
957 QuicWindowUpdateFrame
window_update_frame(headers_stream
->id(),
958 2 * kMinimumFlowControlSendWindow
);
959 session_
.OnWindowUpdateFrame(window_update_frame
);
960 EXPECT_FALSE(headers_stream
->flow_controller()->IsBlocked());
961 EXPECT_FALSE(session_
.IsConnectionFlowControlBlocked());
962 EXPECT_FALSE(session_
.IsStreamFlowControlBlocked());
965 TEST_P(QuicSessionTestServer
, TooManyUnfinishedStreamsCauseConnectionClose
) {
966 // If a buggy/malicious peer creates too many streams that are not ended with
967 // a FIN or RST then we send a connection close.
968 EXPECT_CALL(*connection_
,
969 SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS
)).Times(1);
971 const QuicStreamId kMaxStreams
= 5;
972 QuicSessionPeer::SetMaxOpenStreams(&session_
, kMaxStreams
);
974 // Create kMaxStreams + 1 data streams, and close them all without receiving a
975 // FIN or a RST_STREAM from the client.
976 const QuicStreamId kFirstStreamId
= kClientDataStreamId1
;
977 const QuicStreamId kFinalStreamId
=
978 kClientDataStreamId1
+ 2 * kMaxStreams
+ 1;
979 for (QuicStreamId i
= kFirstStreamId
; i
< kFinalStreamId
; i
+= 2) {
980 QuicStreamFrame
data1(i
, false, 0, StringPiece("HT"));
981 session_
.OnStreamFrame(data1
);
982 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
983 EXPECT_CALL(*connection_
, SendRstStream(i
, _
, _
));
984 session_
.CloseStream(i
);
987 // Called after any new data is received by the session, and triggers the call
988 // to close the connection.
989 session_
.PostProcessAfterData();
992 TEST_P(QuicSessionTestServer
, DrainingStreamsDoNotCountAsOpened
) {
993 // Verify that a draining stream (which has received a FIN but not consumed
994 // it) does not count against the open quota (because it is closed from the
995 // protocol point of view).
996 EXPECT_CALL(*connection_
,
997 SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS
)).Times(0);
999 const QuicStreamId kMaxStreams
= 5;
1000 QuicSessionPeer::SetMaxOpenStreams(&session_
, kMaxStreams
);
1002 // Create kMaxStreams + 1 data streams, and mark them draining.
1003 const QuicStreamId kFirstStreamId
= kClientDataStreamId1
;
1004 const QuicStreamId kFinalStreamId
=
1005 kClientDataStreamId1
+ 2 * kMaxStreams
+ 1;
1006 for (QuicStreamId i
= kFirstStreamId
; i
< kFinalStreamId
; i
+= 2) {
1007 QuicStreamFrame
data1(i
, true, 0, StringPiece("HT"));
1008 session_
.OnStreamFrame(data1
);
1009 EXPECT_EQ(1u, session_
.GetNumOpenStreams());
1010 session_
.StreamDraining(i
);
1011 EXPECT_EQ(0u, session_
.GetNumOpenStreams());
1014 // Called after any new data is received by the session, and triggers the call
1015 // to close the connection.
1016 session_
.PostProcessAfterData();
1019 class QuicSessionTestClient
: public QuicSessionTestBase
{
1021 QuicSessionTestClient() : QuicSessionTestBase(Perspective::IS_CLIENT
) {}
1024 INSTANTIATE_TEST_CASE_P(Tests
,
1025 QuicSessionTestClient
,
1026 ::testing::ValuesIn(QuicSupportedVersions()));
1028 TEST_P(QuicSessionTestClient
, ImplicitlyCreatedStreamsClient
) {
1029 ASSERT_TRUE(session_
.GetIncomingDynamicStream(6) != nullptr);
1030 // Both 2 and 4 should be implicitly created.
1031 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 2));
1032 EXPECT_TRUE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 4));
1033 ASSERT_TRUE(session_
.GetIncomingDynamicStream(2) != nullptr);
1034 ASSERT_TRUE(session_
.GetIncomingDynamicStream(4) != nullptr);
1035 // And 5 should be not implicitly created.
1036 EXPECT_FALSE(QuicSessionPeer::IsStreamImplicitlyCreated(&session_
, 5));