Land Recent QUIC Changes.
[chromium-blink-merge.git] / net / quic / quic_session_test.cc
blob17f53f529c9122384954ebf93746cc01aa65860e
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"
7 #include <set>
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/containers/hash_tables.h"
12 #include "net/quic/crypto/crypto_protocol.h"
13 #include "net/quic/quic_crypto_stream.h"
14 #include "net/quic/quic_flags.h"
15 #include "net/quic/quic_protocol.h"
16 #include "net/quic/quic_utils.h"
17 #include "net/quic/reliable_quic_stream.h"
18 #include "net/quic/test_tools/quic_connection_peer.h"
19 #include "net/quic/test_tools/quic_data_stream_peer.h"
20 #include "net/quic/test_tools/quic_flow_controller_peer.h"
21 #include "net/quic/test_tools/quic_session_peer.h"
22 #include "net/quic/test_tools/quic_test_utils.h"
23 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
24 #include "net/spdy/spdy_framer.h"
25 #include "net/test/gtest_util.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 using base::hash_map;
30 using std::set;
31 using std::vector;
32 using testing::_;
33 using testing::InSequence;
34 using testing::InvokeWithoutArgs;
35 using testing::Return;
36 using testing::StrictMock;
38 namespace net {
39 namespace test {
40 namespace {
42 const QuicPriority kHighestPriority = 0;
43 const QuicPriority kSomeMiddlePriority = 3;
45 class TestCryptoStream : public QuicCryptoStream {
46 public:
47 explicit TestCryptoStream(QuicSession* session)
48 : QuicCryptoStream(session) {
51 virtual void OnHandshakeMessage(
52 const CryptoHandshakeMessage& message) OVERRIDE {
53 encryption_established_ = true;
54 handshake_confirmed_ = true;
55 CryptoHandshakeMessage msg;
56 string error_details;
57 session()->config()->SetInitialFlowControlWindowToSend(
58 kInitialFlowControlWindowForTest);
59 session()->config()->ToHandshakeMessage(&msg);
60 const QuicErrorCode error = session()->config()->ProcessPeerHello(
61 msg, CLIENT, &error_details);
62 EXPECT_EQ(QUIC_NO_ERROR, error);
63 session()->OnConfigNegotiated();
64 session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED);
67 MOCK_METHOD0(OnCanWrite, void());
70 class TestHeadersStream : public QuicHeadersStream {
71 public:
72 explicit TestHeadersStream(QuicSession* session)
73 : QuicHeadersStream(session) {
76 MOCK_METHOD0(OnCanWrite, void());
79 class TestStream : public QuicDataStream {
80 public:
81 TestStream(QuicStreamId id, QuicSession* session)
82 : QuicDataStream(id, session) {
85 using ReliableQuicStream::CloseWriteSide;
87 virtual uint32 ProcessData(const char* data, uint32 data_len) {
88 return data_len;
91 void SendBody(const string& data, bool fin) {
92 WriteOrBufferData(data, fin, NULL);
95 MOCK_METHOD0(OnCanWrite, void());
98 // Poor man's functor for use as callback in a mock.
99 class StreamBlocker {
100 public:
101 StreamBlocker(QuicSession* session, QuicStreamId stream_id)
102 : session_(session),
103 stream_id_(stream_id) {
106 void MarkWriteBlocked() {
107 session_->MarkWriteBlocked(stream_id_, kSomeMiddlePriority);
110 private:
111 QuicSession* const session_;
112 const QuicStreamId stream_id_;
115 class TestSession : public QuicSession {
116 public:
117 explicit TestSession(QuicConnection* connection,
118 uint32 max_initial_flow_control_window)
119 : QuicSession(connection, max_initial_flow_control_window,
120 DefaultQuicConfig()),
121 crypto_stream_(this),
122 writev_consumes_all_data_(false) {}
124 virtual TestCryptoStream* GetCryptoStream() OVERRIDE {
125 return &crypto_stream_;
128 virtual TestStream* CreateOutgoingDataStream() OVERRIDE {
129 TestStream* stream = new TestStream(GetNextStreamId(), this);
130 ActivateStream(stream);
131 return stream;
134 virtual TestStream* CreateIncomingDataStream(QuicStreamId id) OVERRIDE {
135 return new TestStream(id, this);
138 bool IsClosedStream(QuicStreamId id) {
139 return QuicSession::IsClosedStream(id);
142 QuicDataStream* GetIncomingDataStream(QuicStreamId stream_id) {
143 return QuicSession::GetIncomingDataStream(stream_id);
146 virtual QuicConsumedData WritevData(
147 QuicStreamId id,
148 const IOVector& data,
149 QuicStreamOffset offset,
150 bool fin,
151 QuicAckNotifier::DelegateInterface* ack_notifier_delegate) OVERRIDE {
152 // Always consumes everything.
153 if (writev_consumes_all_data_) {
154 return QuicConsumedData(data.TotalBufferSize(), fin);
155 } else {
156 return QuicSession::WritevData(id, data, offset, fin,
157 ack_notifier_delegate);
161 void set_writev_consumes_all_data(bool val) {
162 writev_consumes_all_data_ = val;
165 QuicConsumedData SendStreamData() {
166 return WritevData(5, IOVector(), 0, true, NULL);
169 private:
170 StrictMock<TestCryptoStream> crypto_stream_;
172 bool writev_consumes_all_data_;
175 class QuicSessionTest : public ::testing::TestWithParam<QuicVersion> {
176 protected:
177 QuicSessionTest()
178 : connection_(new MockConnection(true, SupportedVersions(GetParam()))),
179 session_(connection_, kInitialFlowControlWindowForTest) {
180 headers_[":host"] = "www.google.com";
181 headers_[":path"] = "/index.hml";
182 headers_[":scheme"] = "http";
183 headers_["cookie"] =
184 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
185 "__utmc=160408618; "
186 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
187 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
188 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
189 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
190 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
191 "1zFMi5vzcns38-8_Sns; "
192 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
193 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
194 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
195 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
196 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
197 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
198 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
199 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
200 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
201 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
202 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
203 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
204 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
205 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
206 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
209 void CheckClosedStreams() {
210 for (int i = kCryptoStreamId; i < 100; i++) {
211 if (closed_streams_.count(i) == 0) {
212 EXPECT_FALSE(session_.IsClosedStream(i)) << " stream id: " << i;
213 } else {
214 EXPECT_TRUE(session_.IsClosedStream(i)) << " stream id: " << i;
219 void CloseStream(QuicStreamId id) {
220 session_.CloseStream(id);
221 closed_streams_.insert(id);
224 QuicVersion version() const { return connection_->version(); }
226 MockConnection* connection_;
227 TestSession session_;
228 set<QuicStreamId> closed_streams_;
229 SpdyHeaderBlock headers_;
232 INSTANTIATE_TEST_CASE_P(Tests, QuicSessionTest,
233 ::testing::ValuesIn(QuicSupportedVersions()));
235 TEST_P(QuicSessionTest, PeerAddress) {
236 EXPECT_EQ(IPEndPoint(Loopback4(), kTestPort), session_.peer_address());
239 TEST_P(QuicSessionTest, IsCryptoHandshakeConfirmed) {
240 EXPECT_FALSE(session_.IsCryptoHandshakeConfirmed());
241 CryptoHandshakeMessage message;
242 session_.GetCryptoStream()->OnHandshakeMessage(message);
243 EXPECT_TRUE(session_.IsCryptoHandshakeConfirmed());
246 TEST_P(QuicSessionTest, IsClosedStreamDefault) {
247 // Ensure that no streams are initially closed.
248 for (int i = kCryptoStreamId; i < 100; i++) {
249 EXPECT_FALSE(session_.IsClosedStream(i)) << "stream id: " << i;
253 TEST_P(QuicSessionTest, ImplicitlyCreatedStreams) {
254 ASSERT_TRUE(session_.GetIncomingDataStream(7) != NULL);
255 // Both 3 and 5 should be implicitly created.
256 EXPECT_FALSE(session_.IsClosedStream(3));
257 EXPECT_FALSE(session_.IsClosedStream(5));
258 ASSERT_TRUE(session_.GetIncomingDataStream(5) != NULL);
259 ASSERT_TRUE(session_.GetIncomingDataStream(3) != NULL);
262 TEST_P(QuicSessionTest, IsClosedStreamLocallyCreated) {
263 TestStream* stream2 = session_.CreateOutgoingDataStream();
264 EXPECT_EQ(2u, stream2->id());
265 TestStream* stream4 = session_.CreateOutgoingDataStream();
266 EXPECT_EQ(4u, stream4->id());
268 CheckClosedStreams();
269 CloseStream(4);
270 CheckClosedStreams();
271 CloseStream(2);
272 CheckClosedStreams();
275 TEST_P(QuicSessionTest, IsClosedStreamPeerCreated) {
276 QuicStreamId stream_id1 = 5;
277 QuicStreamId stream_id2 = stream_id1 + 2;
278 QuicDataStream* stream1 = session_.GetIncomingDataStream(stream_id1);
279 QuicDataStreamPeer::SetHeadersDecompressed(stream1, true);
280 QuicDataStream* stream2 = session_.GetIncomingDataStream(stream_id2);
281 QuicDataStreamPeer::SetHeadersDecompressed(stream2, true);
283 CheckClosedStreams();
284 CloseStream(stream_id1);
285 CheckClosedStreams();
286 CloseStream(stream_id2);
287 // Create a stream explicitly, and another implicitly.
288 QuicDataStream* stream3 = session_.GetIncomingDataStream(stream_id2 + 4);
289 QuicDataStreamPeer::SetHeadersDecompressed(stream3, true);
290 CheckClosedStreams();
291 // Close one, but make sure the other is still not closed
292 CloseStream(stream3->id());
293 CheckClosedStreams();
296 TEST_P(QuicSessionTest, StreamIdTooLarge) {
297 QuicStreamId stream_id = 5;
298 session_.GetIncomingDataStream(stream_id);
299 EXPECT_CALL(*connection_, SendConnectionClose(QUIC_INVALID_STREAM_ID));
300 session_.GetIncomingDataStream(stream_id + kMaxStreamIdDelta + 2);
303 TEST_P(QuicSessionTest, DecompressionError) {
304 QuicHeadersStream* stream = QuicSessionPeer::GetHeadersStream(&session_);
305 const unsigned char data[] = {
306 0x80, 0x03, 0x00, 0x01, // SPDY/3 SYN_STREAM frame
307 0x00, 0x00, 0x00, 0x25, // flags/length
308 0x00, 0x00, 0x00, 0x05, // stream id
309 0x00, 0x00, 0x00, 0x00, // associated stream id
310 0x00, 0x00,
311 'a', 'b', 'c', 'd' // invalid compressed data
313 EXPECT_CALL(*connection_,
314 SendConnectionCloseWithDetails(QUIC_INVALID_HEADERS_STREAM_DATA,
315 "SPDY framing error."));
316 stream->ProcessRawData(reinterpret_cast<const char*>(data),
317 arraysize(data));
320 TEST_P(QuicSessionTest, DebugDFatalIfMarkingClosedStreamWriteBlocked) {
321 TestStream* stream2 = session_.CreateOutgoingDataStream();
322 // Close the stream.
323 stream2->Reset(QUIC_BAD_APPLICATION_PAYLOAD);
324 // TODO(rtenneti): enable when chromium supports EXPECT_DEBUG_DFATAL.
326 QuicStreamId kClosedStreamId = stream2->id();
327 EXPECT_DEBUG_DFATAL(
328 session_.MarkWriteBlocked(kClosedStreamId, kSomeMiddlePriority),
329 "Marking unknown stream 2 blocked.");
333 TEST_P(QuicSessionTest, DebugDFatalIfMarkWriteBlockedCalledWithWrongPriority) {
334 const QuicPriority kDifferentPriority = 0;
336 TestStream* stream2 = session_.CreateOutgoingDataStream();
337 EXPECT_NE(kDifferentPriority, stream2->EffectivePriority());
338 // TODO(rtenneti): enable when chromium supports EXPECT_DEBUG_DFATAL.
340 EXPECT_DEBUG_DFATAL(
341 session_.MarkWriteBlocked(stream2->id(), kDifferentPriority),
342 "Priorities do not match. Got: 0 Expected: 3");
346 TEST_P(QuicSessionTest, OnCanWrite) {
347 TestStream* stream2 = session_.CreateOutgoingDataStream();
348 TestStream* stream4 = session_.CreateOutgoingDataStream();
349 TestStream* stream6 = session_.CreateOutgoingDataStream();
351 session_.MarkWriteBlocked(stream2->id(), kSomeMiddlePriority);
352 session_.MarkWriteBlocked(stream6->id(), kSomeMiddlePriority);
353 session_.MarkWriteBlocked(stream4->id(), kSomeMiddlePriority);
355 InSequence s;
356 StreamBlocker stream2_blocker(&session_, stream2->id());
357 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(
358 // Reregister, to test the loop limit.
359 InvokeWithoutArgs(&stream2_blocker, &StreamBlocker::MarkWriteBlocked));
360 EXPECT_CALL(*stream6, OnCanWrite());
361 EXPECT_CALL(*stream4, OnCanWrite());
362 session_.OnCanWrite();
363 EXPECT_TRUE(session_.WillingAndAbleToWrite());
366 TEST_P(QuicSessionTest, OnCanWriteBundlesStreams) {
367 // Drive congestion control manually.
368 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>;
369 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm);
371 TestStream* stream2 = session_.CreateOutgoingDataStream();
372 TestStream* stream4 = session_.CreateOutgoingDataStream();
373 TestStream* stream6 = session_.CreateOutgoingDataStream();
375 session_.MarkWriteBlocked(stream2->id(), kSomeMiddlePriority);
376 session_.MarkWriteBlocked(stream6->id(), kSomeMiddlePriority);
377 session_.MarkWriteBlocked(stream4->id(), kSomeMiddlePriority);
380 EXPECT_CALL(*send_algorithm, TimeUntilSend(_, _, _)).WillRepeatedly(
381 Return(QuicTime::Delta::Zero()));
382 EXPECT_CALL(*send_algorithm, GetCongestionWindow()).WillOnce(
383 Return(kMaxPacketSize * 10));
384 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(IgnoreResult(
385 InvokeWithoutArgs(&session_, &TestSession::SendStreamData)));
386 EXPECT_CALL(*stream6, OnCanWrite()).WillOnce(IgnoreResult(
387 InvokeWithoutArgs(&session_, &TestSession::SendStreamData)));
388 EXPECT_CALL(*stream4, OnCanWrite()).WillOnce(IgnoreResult(
389 InvokeWithoutArgs(&session_, &TestSession::SendStreamData)));
390 MockPacketWriter* writer =
391 static_cast<MockPacketWriter*>(
392 QuicConnectionPeer::GetWriter(session_.connection()));
393 EXPECT_CALL(*writer, WritePacket(_, _, _, _)).WillOnce(
394 Return(WriteResult(WRITE_STATUS_OK, 0)));
395 EXPECT_CALL(*send_algorithm, OnPacketSent(_, _, _, _, _));
396 session_.OnCanWrite();
397 EXPECT_FALSE(session_.WillingAndAbleToWrite());
400 TEST_P(QuicSessionTest, OnCanWriteCongestionControlBlocks) {
401 InSequence s;
403 // Drive congestion control manually.
404 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>;
405 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm);
407 TestStream* stream2 = session_.CreateOutgoingDataStream();
408 TestStream* stream4 = session_.CreateOutgoingDataStream();
409 TestStream* stream6 = session_.CreateOutgoingDataStream();
411 session_.MarkWriteBlocked(stream2->id(), kSomeMiddlePriority);
412 session_.MarkWriteBlocked(stream6->id(), kSomeMiddlePriority);
413 session_.MarkWriteBlocked(stream4->id(), kSomeMiddlePriority);
415 StreamBlocker stream2_blocker(&session_, stream2->id());
416 EXPECT_CALL(*send_algorithm, TimeUntilSend(_, _, _)).WillOnce(Return(
417 QuicTime::Delta::Zero()));
418 EXPECT_CALL(*stream2, OnCanWrite());
419 EXPECT_CALL(*send_algorithm, TimeUntilSend(_, _, _)).WillOnce(Return(
420 QuicTime::Delta::Zero()));
421 EXPECT_CALL(*stream6, OnCanWrite());
422 EXPECT_CALL(*send_algorithm, TimeUntilSend(_, _, _)).WillOnce(Return(
423 QuicTime::Delta::Infinite()));
424 // stream4->OnCanWrite is not called.
426 session_.OnCanWrite();
427 EXPECT_TRUE(session_.WillingAndAbleToWrite());
429 // Still congestion-control blocked.
430 EXPECT_CALL(*send_algorithm, TimeUntilSend(_, _, _)).WillOnce(Return(
431 QuicTime::Delta::Infinite()));
432 session_.OnCanWrite();
433 EXPECT_TRUE(session_.WillingAndAbleToWrite());
435 // stream4->OnCanWrite is called once the connection stops being
436 // congestion-control blocked.
437 EXPECT_CALL(*send_algorithm, TimeUntilSend(_, _, _)).WillOnce(Return(
438 QuicTime::Delta::Zero()));
439 EXPECT_CALL(*stream4, OnCanWrite());
440 session_.OnCanWrite();
441 EXPECT_FALSE(session_.WillingAndAbleToWrite());
444 TEST_P(QuicSessionTest, BufferedHandshake) {
445 EXPECT_FALSE(session_.HasPendingHandshake()); // Default value.
447 // Test that blocking other streams does not change our status.
448 TestStream* stream2 = session_.CreateOutgoingDataStream();
449 StreamBlocker stream2_blocker(&session_, stream2->id());
450 stream2_blocker.MarkWriteBlocked();
451 EXPECT_FALSE(session_.HasPendingHandshake());
453 TestStream* stream3 = session_.CreateOutgoingDataStream();
454 StreamBlocker stream3_blocker(&session_, stream3->id());
455 stream3_blocker.MarkWriteBlocked();
456 EXPECT_FALSE(session_.HasPendingHandshake());
458 // Blocking (due to buffering of) the Crypto stream is detected.
459 session_.MarkWriteBlocked(kCryptoStreamId, kHighestPriority);
460 EXPECT_TRUE(session_.HasPendingHandshake());
462 TestStream* stream4 = session_.CreateOutgoingDataStream();
463 StreamBlocker stream4_blocker(&session_, stream4->id());
464 stream4_blocker.MarkWriteBlocked();
465 EXPECT_TRUE(session_.HasPendingHandshake());
467 InSequence s;
468 // Force most streams to re-register, which is common scenario when we block
469 // the Crypto stream, and only the crypto stream can "really" write.
471 // Due to prioritization, we *should* be asked to write the crypto stream
472 // first.
473 // Don't re-register the crypto stream (which signals complete writing).
474 TestCryptoStream* crypto_stream = session_.GetCryptoStream();
475 EXPECT_CALL(*crypto_stream, OnCanWrite());
477 // Re-register all other streams, to show they weren't able to proceed.
478 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(
479 InvokeWithoutArgs(&stream2_blocker, &StreamBlocker::MarkWriteBlocked));
481 EXPECT_CALL(*stream3, OnCanWrite()).WillOnce(
482 InvokeWithoutArgs(&stream3_blocker, &StreamBlocker::MarkWriteBlocked));
484 EXPECT_CALL(*stream4, OnCanWrite()).WillOnce(
485 InvokeWithoutArgs(&stream4_blocker, &StreamBlocker::MarkWriteBlocked));
487 session_.OnCanWrite();
488 EXPECT_TRUE(session_.WillingAndAbleToWrite());
489 EXPECT_FALSE(session_.HasPendingHandshake()); // Crypto stream wrote.
492 TEST_P(QuicSessionTest, OnCanWriteWithClosedStream) {
493 TestStream* stream2 = session_.CreateOutgoingDataStream();
494 TestStream* stream4 = session_.CreateOutgoingDataStream();
495 TestStream* stream6 = session_.CreateOutgoingDataStream();
497 session_.MarkWriteBlocked(stream2->id(), kSomeMiddlePriority);
498 session_.MarkWriteBlocked(stream6->id(), kSomeMiddlePriority);
499 session_.MarkWriteBlocked(stream4->id(), kSomeMiddlePriority);
500 CloseStream(stream6->id());
502 InSequence s;
503 EXPECT_CALL(*stream2, OnCanWrite());
504 EXPECT_CALL(*stream4, OnCanWrite());
505 session_.OnCanWrite();
506 EXPECT_FALSE(session_.WillingAndAbleToWrite());
509 TEST_P(QuicSessionTest, OnCanWriteLimitsNumWritesIfFlowControlBlocked) {
510 ValueRestore<bool> old_flag(&FLAGS_enable_quic_connection_flow_control, true);
511 if (version() < QUIC_VERSION_19) {
512 return;
515 // Ensure connection level flow control blockage.
516 QuicFlowControllerPeer::SetSendWindowOffset(session_.flow_controller(), 0);
517 EXPECT_TRUE(session_.flow_controller()->IsBlocked());
519 // Mark the crypto and headers streams as write blocked, we expect them to be
520 // allowed to write later.
521 session_.MarkWriteBlocked(kCryptoStreamId, kHighestPriority);
522 session_.MarkWriteBlocked(kHeadersStreamId, kHighestPriority);
524 // Create a data stream, and although it is write blocked we never expect it
525 // to be allowed to write as we are connection level flow control blocked.
526 TestStream* stream = session_.CreateOutgoingDataStream();
527 session_.MarkWriteBlocked(stream->id(), kSomeMiddlePriority);
528 EXPECT_CALL(*stream, OnCanWrite()).Times(0);
530 // The crypto and headers streams should be called even though we are
531 // connection flow control blocked.
532 TestCryptoStream* crypto_stream = session_.GetCryptoStream();
533 EXPECT_CALL(*crypto_stream, OnCanWrite()).Times(1);
534 TestHeadersStream* headers_stream = new TestHeadersStream(&session_);
535 QuicSessionPeer::SetHeadersStream(&session_, headers_stream);
536 EXPECT_CALL(*headers_stream, OnCanWrite()).Times(1);
538 session_.OnCanWrite();
539 EXPECT_FALSE(session_.WillingAndAbleToWrite());
542 TEST_P(QuicSessionTest, SendGoAway) {
543 EXPECT_CALL(*connection_,
544 SendGoAway(QUIC_PEER_GOING_AWAY, 0u, "Going Away."));
545 session_.SendGoAway(QUIC_PEER_GOING_AWAY, "Going Away.");
546 EXPECT_TRUE(session_.goaway_sent());
548 EXPECT_CALL(*connection_,
549 SendRstStream(3u, QUIC_STREAM_PEER_GOING_AWAY, 0)).Times(0);
550 EXPECT_TRUE(session_.GetIncomingDataStream(3u));
553 TEST_P(QuicSessionTest, DoNotSendGoAwayTwice) {
554 EXPECT_CALL(*connection_,
555 SendGoAway(QUIC_PEER_GOING_AWAY, 0u, "Going Away.")).Times(1);
556 session_.SendGoAway(QUIC_PEER_GOING_AWAY, "Going Away.");
557 EXPECT_TRUE(session_.goaway_sent());
558 session_.SendGoAway(QUIC_PEER_GOING_AWAY, "Going Away.");
561 TEST_P(QuicSessionTest, IncreasedTimeoutAfterCryptoHandshake) {
562 EXPECT_EQ(kDefaultInitialTimeoutSecs,
563 QuicConnectionPeer::GetNetworkTimeout(connection_).ToSeconds());
564 CryptoHandshakeMessage msg;
565 session_.GetCryptoStream()->OnHandshakeMessage(msg);
566 EXPECT_EQ(kDefaultTimeoutSecs,
567 QuicConnectionPeer::GetNetworkTimeout(connection_).ToSeconds());
570 TEST_P(QuicSessionTest, RstStreamBeforeHeadersDecompressed) {
571 QuicStreamId stream_id1 = 5;
572 // Send two bytes of payload.
573 QuicStreamFrame data1(stream_id1, false, 0, MakeIOVector("HT"));
574 vector<QuicStreamFrame> frames;
575 frames.push_back(data1);
576 session_.OnStreamFrames(frames);
577 EXPECT_EQ(1u, session_.GetNumOpenStreams());
579 QuicRstStreamFrame rst1(stream_id1, QUIC_STREAM_NO_ERROR, 0);
580 session_.OnRstStream(rst1);
581 EXPECT_EQ(0u, session_.GetNumOpenStreams());
582 // Connection should remain alive.
583 EXPECT_TRUE(connection_->connected());
586 TEST_P(QuicSessionTest, MultipleRstStreamsCauseSingleConnectionClose) {
587 // If multiple invalid reset stream frames arrive in a single packet, this
588 // should trigger a connection close. However there is no need to send
589 // multiple connection close frames.
591 // Create valid stream.
592 const QuicStreamId kStreamId = 5;
593 QuicStreamFrame data1(kStreamId, false, 0, MakeIOVector("HT"));
594 vector<QuicStreamFrame> frames;
595 frames.push_back(data1);
596 session_.OnStreamFrames(frames);
597 EXPECT_EQ(1u, session_.GetNumOpenStreams());
599 // Process first invalid stream reset, resulting in the connection being
600 // closed.
601 EXPECT_CALL(*connection_, SendConnectionClose(QUIC_INVALID_STREAM_ID))
602 .Times(1);
603 QuicStreamId kLargeInvalidStreamId = 99999999;
604 QuicRstStreamFrame rst1(kLargeInvalidStreamId, QUIC_STREAM_NO_ERROR, 0);
605 session_.OnRstStream(rst1);
606 QuicConnectionPeer::CloseConnection(connection_);
608 // Processing of second invalid stream reset should not result in the
609 // connection being closed for a second time.
610 QuicRstStreamFrame rst2(kLargeInvalidStreamId, QUIC_STREAM_NO_ERROR, 0);
611 session_.OnRstStream(rst2);
614 TEST_P(QuicSessionTest, HandshakeUnblocksFlowControlBlockedStream) {
615 // Test that if a stream is flow control blocked, then on receipt of the SHLO
616 // containing a suitable send window offset, the stream becomes unblocked.
617 if (version() < QUIC_VERSION_17) {
618 return;
620 ValueRestore<bool> old_flag(&FLAGS_enable_quic_stream_flow_control_2, true);
622 // Ensure that Writev consumes all the data it is given (simulate no socket
623 // blocking).
624 session_.set_writev_consumes_all_data(true);
626 // Create a stream, and send enough data to make it flow control blocked.
627 TestStream* stream2 = session_.CreateOutgoingDataStream();
628 string body(kDefaultFlowControlSendWindow, '.');
629 EXPECT_FALSE(stream2->flow_controller()->IsBlocked());
630 stream2->SendBody(body, false);
631 EXPECT_TRUE(stream2->flow_controller()->IsBlocked());
633 // Now complete the crypto handshake, resulting in an increased flow control
634 // send window.
635 CryptoHandshakeMessage msg;
636 session_.GetCryptoStream()->OnHandshakeMessage(msg);
638 // Stream is now unblocked.
639 EXPECT_FALSE(stream2->flow_controller()->IsBlocked());
642 TEST_P(QuicSessionTest, InvalidFlowControlWindowInHandshake) {
643 // Test that receipt of an invalid (< default) flow control window from peer
644 // results in the connection being torn down.
645 if (version() < QUIC_VERSION_17) {
646 return;
648 ValueRestore<bool> old_flag(&FLAGS_enable_quic_stream_flow_control_2, true);
650 uint32 kInvalidWindow = kDefaultFlowControlSendWindow - 1;
652 CryptoHandshakeMessage msg;
653 string error_details;
654 session_.config()->SetInitialFlowControlWindowToSend(kInvalidWindow);
655 session_.config()->ToHandshakeMessage(&msg);
656 const QuicErrorCode error =
657 session_.config()->ProcessPeerHello(msg, CLIENT, &error_details);
658 EXPECT_EQ(QUIC_NO_ERROR, error);
660 EXPECT_CALL(*connection_, SendConnectionClose(QUIC_FLOW_CONTROL_ERROR));
661 session_.OnConfigNegotiated();
664 TEST_P(QuicSessionTest, InvalidFlowControlWindow) {
665 QuicConnection* connection =
666 new MockConnection(true, SupportedVersions(GetParam()));
668 const uint32 kSmallerFlowControlWindow = kDefaultFlowControlSendWindow - 1;
669 TestSession session(connection, kSmallerFlowControlWindow);
671 EXPECT_EQ(kDefaultFlowControlSendWindow,
672 session.max_flow_control_receive_window_bytes());
675 } // namespace
676 } // namespace test
677 } // namespace net