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/reliable_quic_stream.h"
7 #include "net/quic/quic_ack_notifier.h"
8 #include "net/quic/quic_connection.h"
9 #include "net/quic/quic_flags.h"
10 #include "net/quic/quic_utils.h"
11 #include "net/quic/quic_write_blocked_list.h"
12 #include "net/quic/spdy_utils.h"
13 #include "net/quic/test_tools/quic_config_peer.h"
14 #include "net/quic/test_tools/quic_connection_peer.h"
15 #include "net/quic/test_tools/quic_flow_controller_peer.h"
16 #include "net/quic/test_tools/quic_session_peer.h"
17 #include "net/quic/test_tools/quic_test_utils.h"
18 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
19 #include "net/test/gtest_util.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gmock_mutant.h"
23 using base::StringPiece
;
26 using testing::AnyNumber
;
27 using testing::CreateFunctor
;
28 using testing::InSequence
;
29 using testing::Invoke
;
30 using testing::Return
;
31 using testing::StrictMock
;
32 using testing::WithArgs
;
39 const char kData1
[] = "FooAndBar";
40 const char kData2
[] = "EepAndBaz";
41 const size_t kDataLen
= 9;
42 const bool kShouldProcessData
= true;
43 const bool kShouldNotProcessData
= false;
45 class TestStream
: public ReliableQuicStream
{
47 TestStream(QuicStreamId id
,
49 bool should_process_data
)
50 : ReliableQuicStream(id
, session
),
51 should_process_data_(should_process_data
) {}
53 uint32
ProcessRawData(const char* data
, uint32 data_len
) override
{
54 EXPECT_NE(0u, data_len
);
55 DVLOG(1) << "ProcessData data_len: " << data_len
;
56 data_
+= string(data
, data_len
);
57 return should_process_data_
? data_len
: 0;
60 QuicPriority
EffectivePriority() const override
{
61 return QuicUtils::HighestPriority();
64 using ReliableQuicStream::WriteOrBufferData
;
65 using ReliableQuicStream::CloseReadSide
;
66 using ReliableQuicStream::CloseWriteSide
;
67 using ReliableQuicStream::OnClose
;
70 bool should_process_data_
;
74 class ReliableQuicStreamTest
: public ::testing::TestWithParam
<bool> {
76 ReliableQuicStreamTest()
77 : initial_flow_control_window_bytes_(kMaxPacketSize
),
78 zero_(QuicTime::Delta::Zero()),
79 supported_versions_(QuicSupportedVersions()) {
80 headers_
[":host"] = "www.google.com";
81 headers_
[":path"] = "/index.hml";
82 headers_
[":scheme"] = "https";
84 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
86 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
87 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
88 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
89 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
90 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
91 "1zFMi5vzcns38-8_Sns; "
92 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
93 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
94 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
95 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
96 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
97 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
98 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
99 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
100 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
101 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
102 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
103 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
104 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
105 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
106 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
109 void Initialize(bool stream_should_process_data
) {
110 connection_
= new StrictMock
<MockConnection
>(Perspective::IS_SERVER
,
111 supported_versions_
);
112 session_
.reset(new StrictMock
<MockQuicSpdySession
>(connection_
));
114 // New streams rely on having the peer's flow control receive window
115 // negotiated in the config.
116 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(
117 session_
->config(), initial_flow_control_window_bytes_
);
119 stream_
= new TestStream(kTestStreamId
, session_
.get(),
120 stream_should_process_data
);
121 // session_ now owns stream_.
122 session_
->ActivateStream(stream_
);
123 // Ignore resetting when session_ is terminated.
124 EXPECT_CALL(*session_
, SendRstStream(kTestStreamId
, _
, _
))
126 write_blocked_list_
=
127 QuicSessionPeer::GetWriteBlockedStreams(session_
.get());
130 bool fin_sent() { return ReliableQuicStreamPeer::FinSent(stream_
); }
131 bool rst_sent() { return ReliableQuicStreamPeer::RstSent(stream_
); }
133 void set_initial_flow_control_window_bytes(uint32 val
) {
134 initial_flow_control_window_bytes_
= val
;
137 bool HasWriteBlockedStreams() {
138 return write_blocked_list_
->HasWriteBlockedCryptoOrHeadersStream() ||
139 write_blocked_list_
->HasWriteBlockedDataStreams();
143 MockConnection
* connection_
;
144 scoped_ptr
<MockQuicSpdySession
> session_
;
146 SpdyHeaderBlock headers_
;
147 QuicWriteBlockedList
* write_blocked_list_
;
148 uint32 initial_flow_control_window_bytes_
;
149 QuicTime::Delta zero_
;
150 QuicVersionVector supported_versions_
;
151 const QuicStreamId kTestStreamId
= 5u;
154 TEST_F(ReliableQuicStreamTest
, WriteAllData
) {
155 Initialize(kShouldProcessData
);
157 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
158 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
159 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
160 connection_
->set_max_packet_length(length
);
162 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
163 .WillOnce(Return(QuicConsumedData(kDataLen
, true)));
164 stream_
->WriteOrBufferData(kData1
, false, nullptr);
165 EXPECT_FALSE(HasWriteBlockedStreams());
168 TEST_F(ReliableQuicStreamTest
, NoBlockingIfNoDataOrFin
) {
169 Initialize(kShouldProcessData
);
171 // Write no data and no fin. If we consume nothing we should not be write
173 EXPECT_DFATAL(stream_
->WriteOrBufferData(StringPiece(), false, nullptr), "");
174 EXPECT_FALSE(HasWriteBlockedStreams());
177 TEST_F(ReliableQuicStreamTest
, BlockIfOnlySomeDataConsumed
) {
178 Initialize(kShouldProcessData
);
180 // Write some data and no fin. If we consume some but not all of the data,
181 // we should be write blocked a not all the data was consumed.
182 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
183 .WillOnce(Return(QuicConsumedData(1, false)));
184 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), false, nullptr);
185 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
188 TEST_F(ReliableQuicStreamTest
, BlockIfFinNotConsumedWithData
) {
189 Initialize(kShouldProcessData
);
191 // Write some data and no fin. If we consume all the data but not the fin,
192 // we should be write blocked because the fin was not consumed.
193 // (This should never actually happen as the fin should be sent out with the
195 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
196 .WillOnce(Return(QuicConsumedData(2, false)));
197 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), true, nullptr);
198 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
201 TEST_F(ReliableQuicStreamTest
, BlockIfSoloFinNotConsumed
) {
202 Initialize(kShouldProcessData
);
204 // Write no data and a fin. If we consume nothing we should be write blocked,
205 // as the fin was not consumed.
206 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
207 .WillOnce(Return(QuicConsumedData(0, false)));
208 stream_
->WriteOrBufferData(StringPiece(), true, nullptr);
209 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
212 TEST_F(ReliableQuicStreamTest
, WriteOrBufferData
) {
213 Initialize(kShouldProcessData
);
215 EXPECT_FALSE(HasWriteBlockedStreams());
216 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
217 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
218 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
219 connection_
->set_max_packet_length(length
);
221 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).WillOnce(
222 Return(QuicConsumedData(kDataLen
- 1, false)));
223 stream_
->WriteOrBufferData(kData1
, false, nullptr);
224 EXPECT_TRUE(HasWriteBlockedStreams());
226 // Queue a bytes_consumed write.
227 stream_
->WriteOrBufferData(kData2
, false, nullptr);
229 // Make sure we get the tail of the first write followed by the bytes_consumed
231 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
232 WillOnce(Return(QuicConsumedData(1, false)));
233 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
234 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
235 stream_
->OnCanWrite();
237 // And finally the end of the bytes_consumed.
238 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
239 WillOnce(Return(QuicConsumedData(2, true)));
240 stream_
->OnCanWrite();
243 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithFecProtectAlways
) {
244 Initialize(kShouldProcessData
);
246 // Set FEC policy on stream.
247 ReliableQuicStreamPeer::SetFecPolicy(stream_
, FEC_PROTECT_ALWAYS
);
249 EXPECT_FALSE(HasWriteBlockedStreams());
250 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
251 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
252 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, IN_FEC_GROUP
);
253 connection_
->set_max_packet_length(length
);
255 // Write first data onto stream, which will cause one session write.
256 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).WillOnce(
257 Return(QuicConsumedData(kDataLen
- 1, false)));
258 stream_
->WriteOrBufferData(kData1
, false, nullptr);
259 EXPECT_TRUE(HasWriteBlockedStreams());
261 // Queue a bytes_consumed write.
262 stream_
->WriteOrBufferData(kData2
, false, nullptr);
264 // Make sure we get the tail of the first write followed by the bytes_consumed
266 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
267 WillOnce(Return(QuicConsumedData(1, false)));
268 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
269 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
270 stream_
->OnCanWrite();
272 // And finally the end of the bytes_consumed.
273 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
274 WillOnce(Return(QuicConsumedData(2, true)));
275 stream_
->OnCanWrite();
278 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithFecProtectOptional
) {
279 Initialize(kShouldProcessData
);
281 // Set FEC policy on stream.
282 ReliableQuicStreamPeer::SetFecPolicy(stream_
, FEC_PROTECT_OPTIONAL
);
284 EXPECT_FALSE(HasWriteBlockedStreams());
285 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
286 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
287 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
288 connection_
->set_max_packet_length(length
);
290 // Write first data onto stream, which will cause one session write.
291 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).WillOnce(
292 Return(QuicConsumedData(kDataLen
- 1, false)));
293 stream_
->WriteOrBufferData(kData1
, false, nullptr);
294 EXPECT_TRUE(HasWriteBlockedStreams());
296 // Queue a bytes_consumed write.
297 stream_
->WriteOrBufferData(kData2
, false, nullptr);
299 // Make sure we get the tail of the first write followed by the bytes_consumed
301 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
302 WillOnce(Return(QuicConsumedData(1, false)));
303 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
304 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
305 stream_
->OnCanWrite();
307 // And finally the end of the bytes_consumed.
308 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
309 WillOnce(Return(QuicConsumedData(2, true)));
310 stream_
->OnCanWrite();
313 TEST_F(ReliableQuicStreamTest
, ConnectionCloseAfterStreamClose
) {
314 Initialize(kShouldProcessData
);
316 stream_
->CloseReadSide();
317 stream_
->CloseWriteSide();
318 EXPECT_EQ(QUIC_STREAM_NO_ERROR
, stream_
->stream_error());
319 EXPECT_EQ(QUIC_NO_ERROR
, stream_
->connection_error());
320 stream_
->OnConnectionClosed(QUIC_INTERNAL_ERROR
, false);
321 EXPECT_EQ(QUIC_STREAM_NO_ERROR
, stream_
->stream_error());
322 EXPECT_EQ(QUIC_NO_ERROR
, stream_
->connection_error());
325 TEST_F(ReliableQuicStreamTest
, RstAlwaysSentIfNoFinSent
) {
326 // For flow control accounting, a stream must send either a FIN or a RST frame
327 // before termination.
328 // Test that if no FIN has been sent, we send a RST.
330 Initialize(kShouldProcessData
);
331 EXPECT_FALSE(fin_sent());
332 EXPECT_FALSE(rst_sent());
334 // Write some data, with no FIN.
335 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
336 .WillOnce(Return(QuicConsumedData(1, false)));
337 stream_
->WriteOrBufferData(StringPiece(kData1
, 1), false, nullptr);
338 EXPECT_FALSE(fin_sent());
339 EXPECT_FALSE(rst_sent());
341 // Now close the stream, and expect that we send a RST.
342 EXPECT_CALL(*session_
, SendRstStream(_
, _
, _
));
344 EXPECT_FALSE(fin_sent());
345 EXPECT_TRUE(rst_sent());
348 TEST_F(ReliableQuicStreamTest
, RstNotSentIfFinSent
) {
349 // For flow control accounting, a stream must send either a FIN or a RST frame
350 // before termination.
351 // Test that if a FIN has been sent, we don't also send a RST.
353 Initialize(kShouldProcessData
);
354 EXPECT_FALSE(fin_sent());
355 EXPECT_FALSE(rst_sent());
357 // Write some data, with FIN.
358 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
359 .WillOnce(Return(QuicConsumedData(1, true)));
360 stream_
->WriteOrBufferData(StringPiece(kData1
, 1), true, nullptr);
361 EXPECT_TRUE(fin_sent());
362 EXPECT_FALSE(rst_sent());
364 // Now close the stream, and expect that we do not send a RST.
366 EXPECT_TRUE(fin_sent());
367 EXPECT_FALSE(rst_sent());
370 TEST_F(ReliableQuicStreamTest
, OnlySendOneRst
) {
371 // For flow control accounting, a stream must send either a FIN or a RST frame
372 // before termination.
373 // Test that if a stream sends a RST, it doesn't send an additional RST during
374 // OnClose() (this shouldn't be harmful, but we shouldn't do it anyway...)
376 Initialize(kShouldProcessData
);
377 EXPECT_FALSE(fin_sent());
378 EXPECT_FALSE(rst_sent());
381 const int expected_resets
= 1;
382 EXPECT_CALL(*session_
, SendRstStream(_
, _
, _
)).Times(expected_resets
);
383 stream_
->Reset(QUIC_STREAM_CANCELLED
);
384 EXPECT_FALSE(fin_sent());
385 EXPECT_TRUE(rst_sent());
387 // Now close the stream (any further resets being sent would break the
388 // expectation above).
390 EXPECT_FALSE(fin_sent());
391 EXPECT_TRUE(rst_sent());
394 TEST_F(ReliableQuicStreamTest
, StreamFlowControlMultipleWindowUpdates
) {
395 set_initial_flow_control_window_bytes(1000);
397 Initialize(kShouldProcessData
);
399 // If we receive multiple WINDOW_UPDATES (potentially out of order), then we
400 // want to make sure we latch the largest offset we see.
402 // Initially should be default.
404 initial_flow_control_window_bytes_
,
405 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
407 // Check a single WINDOW_UPDATE results in correct offset.
408 QuicWindowUpdateFrame
window_update_1(stream_
->id(), 1234);
409 stream_
->OnWindowUpdateFrame(window_update_1
);
411 window_update_1
.byte_offset
,
412 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
414 // Now send a few more WINDOW_UPDATES and make sure that only the largest is
416 QuicWindowUpdateFrame
window_update_2(stream_
->id(), 1);
417 QuicWindowUpdateFrame
window_update_3(stream_
->id(), 9999);
418 QuicWindowUpdateFrame
window_update_4(stream_
->id(), 5678);
419 stream_
->OnWindowUpdateFrame(window_update_2
);
420 stream_
->OnWindowUpdateFrame(window_update_3
);
421 stream_
->OnWindowUpdateFrame(window_update_4
);
423 window_update_3
.byte_offset
,
424 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
427 void SaveProxyAckNotifierDelegate(
428 scoped_refptr
<QuicAckNotifier::DelegateInterface
>* delegate_out
,
429 QuicAckNotifier::DelegateInterface
* delegate
) {
430 *delegate_out
= delegate
;
433 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithQuicAckNotifier
) {
434 Initialize(kShouldProcessData
);
436 scoped_refptr
<MockAckNotifierDelegate
> delegate(
437 new StrictMock
<MockAckNotifierDelegate
>);
439 const int kDataSize
= 16 * 1024;
440 const string
kData(kDataSize
, 'a');
442 const int kFirstWriteSize
= 100;
443 const int kSecondWriteSize
= 50;
444 const int kLastWriteSize
= kDataSize
- kFirstWriteSize
- kSecondWriteSize
;
446 // Set a large flow control send window so this doesn't interfere with test.
447 stream_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
448 session_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
450 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
452 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
453 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
454 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
455 Return(QuicConsumedData(kFirstWriteSize
, false))));
456 stream_
->WriteOrBufferData(kData
, false, delegate
.get());
457 EXPECT_TRUE(HasWriteBlockedStreams());
459 EXPECT_CALL(*session_
,
460 WritevData(kTestStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
461 .WillOnce(Return(QuicConsumedData(kSecondWriteSize
, false)));
462 stream_
->OnCanWrite();
464 // No ack expected for an empty write.
465 EXPECT_CALL(*session_
,
466 WritevData(kTestStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
467 .WillOnce(Return(QuicConsumedData(0, false)));
468 stream_
->OnCanWrite();
470 EXPECT_CALL(*session_
,
471 WritevData(kTestStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
472 .WillOnce(Return(QuicConsumedData(kLastWriteSize
, false)));
473 stream_
->OnCanWrite();
475 // There were two writes, so OnAckNotification is not propagated until the
476 // third Ack arrives.
477 proxy_delegate
->OnAckNotification(3, 4, zero_
);
478 proxy_delegate
->OnAckNotification(30, 40, zero_
);
480 // The arguments to delegate->OnAckNotification are the sum of the
481 // arguments to proxy_delegate OnAckNotification calls.
482 EXPECT_CALL(*delegate
.get(), OnAckNotification(333, 444, zero_
));
483 proxy_delegate
->OnAckNotification(300, 400, zero_
);
486 // Verify delegate behavior when packets are acked before the WritevData call
487 // that sends out the last byte.
488 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataAckNotificationBeforeFlush
) {
489 Initialize(kShouldProcessData
);
491 scoped_refptr
<MockAckNotifierDelegate
> delegate(
492 new StrictMock
<MockAckNotifierDelegate
>);
494 const int kDataSize
= 16 * 1024;
495 const string
kData(kDataSize
, 'a');
497 const int kInitialWriteSize
= 100;
499 // Set a large flow control send window so this doesn't interfere with test.
500 stream_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
501 session_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
503 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
505 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
506 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
507 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
508 Return(QuicConsumedData(kInitialWriteSize
, false))));
509 stream_
->WriteOrBufferData(kData
, false, delegate
.get());
510 EXPECT_TRUE(HasWriteBlockedStreams());
512 // Handle the ack of the first write.
513 proxy_delegate
->OnAckNotification(3, 4, zero_
);
514 proxy_delegate
= nullptr;
516 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
519 CreateFunctor(&SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
520 Return(QuicConsumedData(kDataSize
- kInitialWriteSize
, false))));
521 stream_
->OnCanWrite();
523 // Handle the ack for the second write.
524 EXPECT_CALL(*delegate
.get(), OnAckNotification(303, 404, zero_
));
525 proxy_delegate
->OnAckNotification(300, 400, zero_
);
528 // Verify delegate behavior when WriteOrBufferData does not buffer.
529 TEST_F(ReliableQuicStreamTest
, WriteAndBufferDataWithAckNotiferNoBuffer
) {
530 Initialize(kShouldProcessData
);
532 scoped_refptr
<MockAckNotifierDelegate
> delegate(
533 new StrictMock
<MockAckNotifierDelegate
>);
535 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
537 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
538 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
539 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
540 Return(QuicConsumedData(kDataLen
, true))));
541 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
542 EXPECT_FALSE(HasWriteBlockedStreams());
545 EXPECT_CALL(*delegate
.get(), OnAckNotification(3, 4, zero_
));
546 proxy_delegate
->OnAckNotification(3, 4, zero_
);
549 // Verify delegate behavior when WriteOrBufferData buffers all the data.
550 TEST_F(ReliableQuicStreamTest
, BufferOnWriteAndBufferDataWithAckNotifer
) {
551 Initialize(kShouldProcessData
);
553 scoped_refptr
<MockAckNotifierDelegate
> delegate(
554 new StrictMock
<MockAckNotifierDelegate
>);
556 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
558 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
559 .WillOnce(Return(QuicConsumedData(0, false)));
560 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
561 EXPECT_TRUE(HasWriteBlockedStreams());
563 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
564 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
565 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
566 Return(QuicConsumedData(kDataLen
, true))));
567 stream_
->OnCanWrite();
570 EXPECT_CALL(*delegate
.get(), OnAckNotification(3, 4, zero_
));
571 proxy_delegate
->OnAckNotification(3, 4, zero_
);
574 // Verify delegate behavior when WriteOrBufferData when the FIN is
575 // sent out in a different packet.
576 TEST_F(ReliableQuicStreamTest
, WriteAndBufferDataWithAckNotiferOnlyFinRemains
) {
577 Initialize(kShouldProcessData
);
579 scoped_refptr
<MockAckNotifierDelegate
> delegate(
580 new StrictMock
<MockAckNotifierDelegate
>);
582 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
584 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
585 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
586 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
587 Return(QuicConsumedData(kDataLen
, false))));
588 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
589 EXPECT_TRUE(HasWriteBlockedStreams());
591 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
592 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
593 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
594 Return(QuicConsumedData(0, true))));
595 stream_
->OnCanWrite();
598 proxy_delegate
->OnAckNotification(3, 4, zero_
);
599 EXPECT_CALL(*delegate
.get(), OnAckNotification(33, 44, zero_
));
600 proxy_delegate
->OnAckNotification(30, 40, zero_
);
603 // Verify that when we receive a packet which violates flow control (i.e. sends
604 // too much data on the stream) that the stream sequencer never sees this frame,
605 // as we check for violation and close the connection early.
606 TEST_F(ReliableQuicStreamTest
,
607 StreamSequencerNeverSeesPacketsViolatingFlowControl
) {
608 Initialize(kShouldProcessData
);
610 // Receive a stream frame that violates flow control: the byte offset is
611 // higher than the receive window offset.
612 QuicStreamFrame
frame(stream_
->id(), false,
613 kInitialSessionFlowControlWindowForTest
+ 1,
615 EXPECT_GT(frame
.offset
, QuicFlowControllerPeer::ReceiveWindowOffset(
616 stream_
->flow_controller()));
618 // Stream should not accept the frame, and the connection should be closed.
619 EXPECT_CALL(*connection_
,
620 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
));
621 stream_
->OnStreamFrame(frame
);
624 TEST_F(ReliableQuicStreamTest
, FinalByteOffsetFromFin
) {
625 Initialize(kShouldProcessData
);
627 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
629 QuicStreamFrame
stream_frame_no_fin(stream_
->id(), false, 1234,
631 stream_
->OnStreamFrame(stream_frame_no_fin
);
632 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
634 QuicStreamFrame
stream_frame_with_fin(stream_
->id(), true, 1234,
636 stream_
->OnStreamFrame(stream_frame_with_fin
);
637 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
640 TEST_F(ReliableQuicStreamTest
, FinalByteOffsetFromRst
) {
641 Initialize(kShouldProcessData
);
643 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
644 QuicRstStreamFrame
rst_frame(stream_
->id(), QUIC_STREAM_CANCELLED
, 1234);
645 stream_
->OnStreamReset(rst_frame
);
646 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
649 TEST_F(ReliableQuicStreamTest
, SetDrainingIncomingOutgoing
) {
650 // Don't have incoming data consumed.
651 Initialize(kShouldNotProcessData
);
653 // Incoming data with FIN.
654 QuicStreamFrame
stream_frame_with_fin(stream_
->id(), true, 1234,
656 stream_
->OnStreamFrame(stream_frame_with_fin
);
657 // The FIN has been received but not consumed.
658 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
659 EXPECT_FALSE(stream_
->read_side_closed());
661 EXPECT_EQ(1u, session_
->GetNumOpenStreams());
663 // Outgoing data with FIN.
664 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
665 .WillOnce(Return(QuicConsumedData(2, true)));
666 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), true, nullptr);
667 EXPECT_TRUE(stream_
->write_side_closed());
669 EXPECT_EQ(1u, QuicSessionPeer::GetDrainingStreams(session_
.get())
670 ->count(kTestStreamId
));
671 EXPECT_EQ(0u, session_
->GetNumOpenStreams());
674 TEST_F(ReliableQuicStreamTest
, SetDrainingOutgoingIncoming
) {
675 // Don't have incoming data consumed.
676 Initialize(kShouldNotProcessData
);
678 // Outgoing data with FIN.
679 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
680 .WillOnce(Return(QuicConsumedData(2, true)));
681 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), true, nullptr);
682 EXPECT_TRUE(stream_
->write_side_closed());
684 EXPECT_EQ(1u, session_
->GetNumOpenStreams());
686 // Incoming data with FIN.
687 QuicStreamFrame
stream_frame_with_fin(stream_
->id(), true, 1234,
689 stream_
->OnStreamFrame(stream_frame_with_fin
);
690 // The FIN has been received but not consumed.
691 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
692 EXPECT_FALSE(stream_
->read_side_closed());
694 EXPECT_EQ(1u, QuicSessionPeer::GetDrainingStreams(session_
.get())
695 ->count(kTestStreamId
));
696 EXPECT_EQ(0u, session_
->GetNumOpenStreams());
699 TEST_F(ReliableQuicStreamTest
, FecSendPolicyReceivedConnectionOption
) {
700 ValueRestore
<bool> old_flag(&FLAGS_quic_send_fec_packet_only_on_fec_alarm
,
702 Initialize(kShouldProcessData
);
704 // Test ReceivedConnectionOptions.
705 QuicConfig
* config
= session_
->config();
707 copt
.push_back(kFSTR
);
708 QuicConfigPeer::SetReceivedConnectionOptions(config
, copt
);
709 EXPECT_EQ(FEC_PROTECT_OPTIONAL
, stream_
->fec_policy());
710 stream_
->SetFromConfig();
711 EXPECT_EQ(FEC_PROTECT_ALWAYS
, stream_
->fec_policy());