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_utils.h"
10 #include "net/quic/quic_write_blocked_list.h"
11 #include "net/quic/spdy_utils.h"
12 #include "net/quic/test_tools/quic_config_peer.h"
13 #include "net/quic/test_tools/quic_connection_peer.h"
14 #include "net/quic/test_tools/quic_flow_controller_peer.h"
15 #include "net/quic/test_tools/quic_session_peer.h"
16 #include "net/quic/test_tools/quic_test_utils.h"
17 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
18 #include "net/test/gtest_util.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gmock_mutant.h"
22 using base::StringPiece
;
25 using testing::AnyNumber
;
26 using testing::CreateFunctor
;
27 using testing::InSequence
;
28 using testing::Invoke
;
29 using testing::Return
;
30 using testing::StrictMock
;
31 using testing::WithArgs
;
38 const char kData1
[] = "FooAndBar";
39 const char kData2
[] = "EepAndBaz";
40 const size_t kDataLen
= 9;
41 const bool kShouldProcessData
= true;
42 const bool kShouldNotProcessData
= false;
44 class TestStream
: public ReliableQuicStream
{
46 TestStream(QuicStreamId id
,
48 bool should_process_data
)
49 : ReliableQuicStream(id
, session
),
50 should_process_data_(should_process_data
) {}
52 void OnDataAvailable() override
{}
54 uint32
ProcessRawData(const char* data
, uint32 data_len
) {
55 EXPECT_NE(0u, data_len
);
56 DVLOG(1) << "ProcessData data_len: " << data_len
;
57 data_
+= string(data
, data_len
);
58 return should_process_data_
? data_len
: 0;
61 QuicPriority
EffectivePriority() const override
{
62 return QuicUtils::HighestPriority();
65 using ReliableQuicStream::WriteOrBufferData
;
66 using ReliableQuicStream::CloseReadSide
;
67 using ReliableQuicStream::CloseWriteSide
;
68 using ReliableQuicStream::OnClose
;
71 bool should_process_data_
;
75 class ReliableQuicStreamTest
: public ::testing::TestWithParam
<bool> {
77 ReliableQuicStreamTest()
78 : initial_flow_control_window_bytes_(kMaxPacketSize
),
79 zero_(QuicTime::Delta::Zero()),
80 supported_versions_(QuicSupportedVersions()) {
81 headers_
[":host"] = "www.google.com";
82 headers_
[":path"] = "/index.hml";
83 headers_
[":scheme"] = "https";
85 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
87 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
88 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
89 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
90 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
91 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
92 "1zFMi5vzcns38-8_Sns; "
93 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
94 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
95 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
96 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
97 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
98 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
99 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
100 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
101 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
102 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
103 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
104 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
105 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
106 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
107 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
110 void Initialize(bool stream_should_process_data
) {
111 connection_
= new StrictMock
<MockConnection
>(Perspective::IS_SERVER
,
112 supported_versions_
);
113 session_
.reset(new StrictMock
<MockQuicSpdySession
>(connection_
));
115 // New streams rely on having the peer's flow control receive window
116 // negotiated in the config.
117 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(
118 session_
->config(), initial_flow_control_window_bytes_
);
120 stream_
= new TestStream(kTestStreamId
, session_
.get(),
121 stream_should_process_data
);
122 // session_ now owns stream_.
123 session_
->ActivateStream(stream_
);
124 // Ignore resetting when session_ is terminated.
125 EXPECT_CALL(*session_
, SendRstStream(kTestStreamId
, _
, _
))
127 write_blocked_list_
=
128 QuicSessionPeer::GetWriteBlockedStreams(session_
.get());
131 bool fin_sent() { return ReliableQuicStreamPeer::FinSent(stream_
); }
132 bool rst_sent() { return ReliableQuicStreamPeer::RstSent(stream_
); }
134 void set_initial_flow_control_window_bytes(uint32 val
) {
135 initial_flow_control_window_bytes_
= val
;
138 bool HasWriteBlockedStreams() {
139 return write_blocked_list_
->HasWriteBlockedCryptoOrHeadersStream() ||
140 write_blocked_list_
->HasWriteBlockedDataStreams();
144 MockConnection
* connection_
;
145 scoped_ptr
<MockQuicSpdySession
> session_
;
147 SpdyHeaderBlock headers_
;
148 QuicWriteBlockedList
* write_blocked_list_
;
149 uint32 initial_flow_control_window_bytes_
;
150 QuicTime::Delta zero_
;
151 QuicVersionVector supported_versions_
;
152 const QuicStreamId kTestStreamId
= 5u;
155 TEST_F(ReliableQuicStreamTest
, WriteAllData
) {
156 Initialize(kShouldProcessData
);
158 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
159 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
160 PACKET_6BYTE_PACKET_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
161 connection_
->set_max_packet_length(length
);
163 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
164 .WillOnce(Return(QuicConsumedData(kDataLen
, true)));
165 stream_
->WriteOrBufferData(kData1
, false, nullptr);
166 EXPECT_FALSE(HasWriteBlockedStreams());
169 TEST_F(ReliableQuicStreamTest
, NoBlockingIfNoDataOrFin
) {
170 Initialize(kShouldProcessData
);
172 // Write no data and no fin. If we consume nothing we should not be write
174 EXPECT_DFATAL(stream_
->WriteOrBufferData(StringPiece(), false, nullptr), "");
175 EXPECT_FALSE(HasWriteBlockedStreams());
178 TEST_F(ReliableQuicStreamTest
, BlockIfOnlySomeDataConsumed
) {
179 Initialize(kShouldProcessData
);
181 // Write some data and no fin. If we consume some but not all of the data,
182 // we should be write blocked a not all the data was consumed.
183 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
184 .WillOnce(Return(QuicConsumedData(1, false)));
185 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), false, nullptr);
186 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
189 TEST_F(ReliableQuicStreamTest
, BlockIfFinNotConsumedWithData
) {
190 Initialize(kShouldProcessData
);
192 // Write some data and no fin. If we consume all the data but not the fin,
193 // we should be write blocked because the fin was not consumed.
194 // (This should never actually happen as the fin should be sent out with the
196 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
197 .WillOnce(Return(QuicConsumedData(2, false)));
198 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), true, nullptr);
199 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
202 TEST_F(ReliableQuicStreamTest
, BlockIfSoloFinNotConsumed
) {
203 Initialize(kShouldProcessData
);
205 // Write no data and a fin. If we consume nothing we should be write blocked,
206 // as the fin was not consumed.
207 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
208 .WillOnce(Return(QuicConsumedData(0, false)));
209 stream_
->WriteOrBufferData(StringPiece(), true, nullptr);
210 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
213 TEST_F(ReliableQuicStreamTest
, WriteOrBufferData
) {
214 Initialize(kShouldProcessData
);
216 EXPECT_FALSE(HasWriteBlockedStreams());
217 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
218 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
219 PACKET_6BYTE_PACKET_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
220 connection_
->set_max_packet_length(length
);
222 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).WillOnce(
223 Return(QuicConsumedData(kDataLen
- 1, false)));
224 stream_
->WriteOrBufferData(kData1
, false, nullptr);
225 EXPECT_TRUE(HasWriteBlockedStreams());
227 // Queue a bytes_consumed write.
228 stream_
->WriteOrBufferData(kData2
, false, nullptr);
230 // Make sure we get the tail of the first write followed by the bytes_consumed
232 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
233 WillOnce(Return(QuicConsumedData(1, false)));
234 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
235 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
236 stream_
->OnCanWrite();
238 // And finally the end of the bytes_consumed.
239 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
240 WillOnce(Return(QuicConsumedData(2, true)));
241 stream_
->OnCanWrite();
244 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithFecProtectAlways
) {
245 Initialize(kShouldProcessData
);
247 // Set FEC policy on stream.
248 ReliableQuicStreamPeer::SetFecPolicy(stream_
, FEC_PROTECT_ALWAYS
);
250 EXPECT_FALSE(HasWriteBlockedStreams());
251 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
252 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
253 PACKET_6BYTE_PACKET_NUMBER
, 0u, IN_FEC_GROUP
);
254 connection_
->set_max_packet_length(length
);
256 // Write first data onto stream, which will cause one session write.
257 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).WillOnce(
258 Return(QuicConsumedData(kDataLen
- 1, false)));
259 stream_
->WriteOrBufferData(kData1
, false, nullptr);
260 EXPECT_TRUE(HasWriteBlockedStreams());
262 // Queue a bytes_consumed write.
263 stream_
->WriteOrBufferData(kData2
, false, nullptr);
265 // Make sure we get the tail of the first write followed by the bytes_consumed
267 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
268 WillOnce(Return(QuicConsumedData(1, false)));
269 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
270 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
271 stream_
->OnCanWrite();
273 // And finally the end of the bytes_consumed.
274 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
275 WillOnce(Return(QuicConsumedData(2, true)));
276 stream_
->OnCanWrite();
279 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithFecProtectOptional
) {
280 Initialize(kShouldProcessData
);
282 // Set FEC policy on stream.
283 ReliableQuicStreamPeer::SetFecPolicy(stream_
, FEC_PROTECT_OPTIONAL
);
285 EXPECT_FALSE(HasWriteBlockedStreams());
286 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
287 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
288 PACKET_6BYTE_PACKET_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
289 connection_
->set_max_packet_length(length
);
291 // Write first data onto stream, which will cause one session write.
292 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).WillOnce(
293 Return(QuicConsumedData(kDataLen
- 1, false)));
294 stream_
->WriteOrBufferData(kData1
, false, nullptr);
295 EXPECT_TRUE(HasWriteBlockedStreams());
297 // Queue a bytes_consumed write.
298 stream_
->WriteOrBufferData(kData2
, false, nullptr);
300 // Make sure we get the tail of the first write followed by the bytes_consumed
302 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
303 WillOnce(Return(QuicConsumedData(1, false)));
304 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
305 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
306 stream_
->OnCanWrite();
308 // And finally the end of the bytes_consumed.
309 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
310 WillOnce(Return(QuicConsumedData(2, true)));
311 stream_
->OnCanWrite();
314 TEST_F(ReliableQuicStreamTest
, ConnectionCloseAfterStreamClose
) {
315 Initialize(kShouldProcessData
);
317 stream_
->CloseReadSide();
318 stream_
->CloseWriteSide();
319 EXPECT_EQ(QUIC_STREAM_NO_ERROR
, stream_
->stream_error());
320 EXPECT_EQ(QUIC_NO_ERROR
, stream_
->connection_error());
321 stream_
->OnConnectionClosed(QUIC_INTERNAL_ERROR
, false);
322 EXPECT_EQ(QUIC_STREAM_NO_ERROR
, stream_
->stream_error());
323 EXPECT_EQ(QUIC_NO_ERROR
, stream_
->connection_error());
326 TEST_F(ReliableQuicStreamTest
, RstAlwaysSentIfNoFinSent
) {
327 // For flow control accounting, a stream must send either a FIN or a RST frame
328 // before termination.
329 // Test that if no FIN has been sent, we send a RST.
331 Initialize(kShouldProcessData
);
332 EXPECT_FALSE(fin_sent());
333 EXPECT_FALSE(rst_sent());
335 // Write some data, with no FIN.
336 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
337 .WillOnce(Return(QuicConsumedData(1, false)));
338 stream_
->WriteOrBufferData(StringPiece(kData1
, 1), false, nullptr);
339 EXPECT_FALSE(fin_sent());
340 EXPECT_FALSE(rst_sent());
342 // Now close the stream, and expect that we send a RST.
343 EXPECT_CALL(*session_
, SendRstStream(_
, _
, _
));
345 EXPECT_FALSE(fin_sent());
346 EXPECT_TRUE(rst_sent());
349 TEST_F(ReliableQuicStreamTest
, RstNotSentIfFinSent
) {
350 // For flow control accounting, a stream must send either a FIN or a RST frame
351 // before termination.
352 // Test that if a FIN has been sent, we don't also send a RST.
354 Initialize(kShouldProcessData
);
355 EXPECT_FALSE(fin_sent());
356 EXPECT_FALSE(rst_sent());
358 // Write some data, with FIN.
359 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
360 .WillOnce(Return(QuicConsumedData(1, true)));
361 stream_
->WriteOrBufferData(StringPiece(kData1
, 1), true, nullptr);
362 EXPECT_TRUE(fin_sent());
363 EXPECT_FALSE(rst_sent());
365 // Now close the stream, and expect that we do not send a RST.
367 EXPECT_TRUE(fin_sent());
368 EXPECT_FALSE(rst_sent());
371 TEST_F(ReliableQuicStreamTest
, OnlySendOneRst
) {
372 // For flow control accounting, a stream must send either a FIN or a RST frame
373 // before termination.
374 // Test that if a stream sends a RST, it doesn't send an additional RST during
375 // OnClose() (this shouldn't be harmful, but we shouldn't do it anyway...)
377 Initialize(kShouldProcessData
);
378 EXPECT_FALSE(fin_sent());
379 EXPECT_FALSE(rst_sent());
382 const int expected_resets
= 1;
383 EXPECT_CALL(*session_
, SendRstStream(_
, _
, _
)).Times(expected_resets
);
384 stream_
->Reset(QUIC_STREAM_CANCELLED
);
385 EXPECT_FALSE(fin_sent());
386 EXPECT_TRUE(rst_sent());
388 // Now close the stream (any further resets being sent would break the
389 // expectation above).
391 EXPECT_FALSE(fin_sent());
392 EXPECT_TRUE(rst_sent());
395 TEST_F(ReliableQuicStreamTest
, StreamFlowControlMultipleWindowUpdates
) {
396 set_initial_flow_control_window_bytes(1000);
398 Initialize(kShouldProcessData
);
400 // If we receive multiple WINDOW_UPDATES (potentially out of order), then we
401 // want to make sure we latch the largest offset we see.
403 // Initially should be default.
405 initial_flow_control_window_bytes_
,
406 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
408 // Check a single WINDOW_UPDATE results in correct offset.
409 QuicWindowUpdateFrame
window_update_1(stream_
->id(), 1234);
410 stream_
->OnWindowUpdateFrame(window_update_1
);
412 window_update_1
.byte_offset
,
413 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
415 // Now send a few more WINDOW_UPDATES and make sure that only the largest is
417 QuicWindowUpdateFrame
window_update_2(stream_
->id(), 1);
418 QuicWindowUpdateFrame
window_update_3(stream_
->id(), 9999);
419 QuicWindowUpdateFrame
window_update_4(stream_
->id(), 5678);
420 stream_
->OnWindowUpdateFrame(window_update_2
);
421 stream_
->OnWindowUpdateFrame(window_update_3
);
422 stream_
->OnWindowUpdateFrame(window_update_4
);
424 window_update_3
.byte_offset
,
425 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
428 void SaveProxyAckNotifierDelegate(
429 scoped_refptr
<QuicAckNotifier::DelegateInterface
>* delegate_out
,
430 QuicAckNotifier::DelegateInterface
* delegate
) {
431 *delegate_out
= delegate
;
434 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithQuicAckNotifier
) {
435 Initialize(kShouldProcessData
);
437 scoped_refptr
<MockAckNotifierDelegate
> delegate(
438 new StrictMock
<MockAckNotifierDelegate
>);
440 const int kDataSize
= 16 * 1024;
441 const string
kData(kDataSize
, 'a');
443 const int kFirstWriteSize
= 100;
444 const int kSecondWriteSize
= 50;
445 const int kLastWriteSize
= kDataSize
- kFirstWriteSize
- kSecondWriteSize
;
447 // Set a large flow control send window so this doesn't interfere with test.
448 stream_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
449 session_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
451 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
453 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
454 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
455 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
456 Return(QuicConsumedData(kFirstWriteSize
, false))));
457 stream_
->WriteOrBufferData(kData
, false, delegate
.get());
458 EXPECT_TRUE(HasWriteBlockedStreams());
460 EXPECT_CALL(*session_
,
461 WritevData(kTestStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
462 .WillOnce(Return(QuicConsumedData(kSecondWriteSize
, false)));
463 stream_
->OnCanWrite();
465 // No ack expected for an empty write.
466 EXPECT_CALL(*session_
,
467 WritevData(kTestStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
468 .WillOnce(Return(QuicConsumedData(0, false)));
469 stream_
->OnCanWrite();
471 EXPECT_CALL(*session_
,
472 WritevData(kTestStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
473 .WillOnce(Return(QuicConsumedData(kLastWriteSize
, false)));
474 stream_
->OnCanWrite();
476 // There were two writes, so OnAckNotification is not propagated until the
477 // third Ack arrives.
478 proxy_delegate
->OnAckNotification(3, 4, zero_
);
479 proxy_delegate
->OnAckNotification(30, 40, zero_
);
481 // The arguments to delegate->OnAckNotification are the sum of the
482 // arguments to proxy_delegate OnAckNotification calls.
483 EXPECT_CALL(*delegate
.get(), OnAckNotification(333, 444, zero_
));
484 proxy_delegate
->OnAckNotification(300, 400, zero_
);
487 // Verify delegate behavior when packets are acked before the WritevData call
488 // that sends out the last byte.
489 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataAckNotificationBeforeFlush
) {
490 Initialize(kShouldProcessData
);
492 scoped_refptr
<MockAckNotifierDelegate
> delegate(
493 new StrictMock
<MockAckNotifierDelegate
>);
495 const int kDataSize
= 16 * 1024;
496 const string
kData(kDataSize
, 'a');
498 const int kInitialWriteSize
= 100;
500 // Set a large flow control send window so this doesn't interfere with test.
501 stream_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
502 session_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
504 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
506 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
507 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
508 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
509 Return(QuicConsumedData(kInitialWriteSize
, false))));
510 stream_
->WriteOrBufferData(kData
, false, delegate
.get());
511 EXPECT_TRUE(HasWriteBlockedStreams());
513 // Handle the ack of the first write.
514 proxy_delegate
->OnAckNotification(3, 4, zero_
);
515 proxy_delegate
= nullptr;
517 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
520 CreateFunctor(&SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
521 Return(QuicConsumedData(kDataSize
- kInitialWriteSize
, false))));
522 stream_
->OnCanWrite();
524 // Handle the ack for the second write.
525 EXPECT_CALL(*delegate
.get(), OnAckNotification(303, 404, zero_
));
526 proxy_delegate
->OnAckNotification(300, 400, zero_
);
529 // Verify delegate behavior when WriteOrBufferData does not buffer.
530 TEST_F(ReliableQuicStreamTest
, WriteAndBufferDataWithAckNotiferNoBuffer
) {
531 Initialize(kShouldProcessData
);
533 scoped_refptr
<MockAckNotifierDelegate
> delegate(
534 new StrictMock
<MockAckNotifierDelegate
>);
536 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
538 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
539 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
540 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
541 Return(QuicConsumedData(kDataLen
, true))));
542 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
543 EXPECT_FALSE(HasWriteBlockedStreams());
546 EXPECT_CALL(*delegate
.get(), OnAckNotification(3, 4, zero_
));
547 proxy_delegate
->OnAckNotification(3, 4, zero_
);
550 // Verify delegate behavior when WriteOrBufferData buffers all the data.
551 TEST_F(ReliableQuicStreamTest
, BufferOnWriteAndBufferDataWithAckNotifer
) {
552 Initialize(kShouldProcessData
);
554 scoped_refptr
<MockAckNotifierDelegate
> delegate(
555 new StrictMock
<MockAckNotifierDelegate
>);
557 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
559 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
560 .WillOnce(Return(QuicConsumedData(0, false)));
561 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
562 EXPECT_TRUE(HasWriteBlockedStreams());
564 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
565 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
566 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
567 Return(QuicConsumedData(kDataLen
, true))));
568 stream_
->OnCanWrite();
571 EXPECT_CALL(*delegate
.get(), OnAckNotification(3, 4, zero_
));
572 proxy_delegate
->OnAckNotification(3, 4, zero_
);
575 // Verify delegate behavior when WriteOrBufferData when the FIN is
576 // sent out in a different packet.
577 TEST_F(ReliableQuicStreamTest
, WriteAndBufferDataWithAckNotiferOnlyFinRemains
) {
578 Initialize(kShouldProcessData
);
580 scoped_refptr
<MockAckNotifierDelegate
> delegate(
581 new StrictMock
<MockAckNotifierDelegate
>);
583 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
585 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
586 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
587 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
588 Return(QuicConsumedData(kDataLen
, false))));
589 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
590 EXPECT_TRUE(HasWriteBlockedStreams());
592 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
593 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
594 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
595 Return(QuicConsumedData(0, true))));
596 stream_
->OnCanWrite();
599 proxy_delegate
->OnAckNotification(3, 4, zero_
);
600 EXPECT_CALL(*delegate
.get(), OnAckNotification(33, 44, zero_
));
601 proxy_delegate
->OnAckNotification(30, 40, zero_
);
604 // Verify that when we receive a packet which violates flow control (i.e. sends
605 // too much data on the stream) that the stream sequencer never sees this frame,
606 // as we check for violation and close the connection early.
607 TEST_F(ReliableQuicStreamTest
,
608 StreamSequencerNeverSeesPacketsViolatingFlowControl
) {
609 Initialize(kShouldProcessData
);
611 // Receive a stream frame that violates flow control: the byte offset is
612 // higher than the receive window offset.
613 QuicStreamFrame
frame(stream_
->id(), false,
614 kInitialSessionFlowControlWindowForTest
+ 1,
616 EXPECT_GT(frame
.offset
, QuicFlowControllerPeer::ReceiveWindowOffset(
617 stream_
->flow_controller()));
619 // Stream should not accept the frame, and the connection should be closed.
620 EXPECT_CALL(*connection_
,
621 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
));
622 stream_
->OnStreamFrame(frame
);
625 TEST_F(ReliableQuicStreamTest
, FinalByteOffsetFromFin
) {
626 Initialize(kShouldProcessData
);
628 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
630 QuicStreamFrame
stream_frame_no_fin(stream_
->id(), false, 1234,
632 stream_
->OnStreamFrame(stream_frame_no_fin
);
633 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
635 QuicStreamFrame
stream_frame_with_fin(stream_
->id(), true, 1234,
637 stream_
->OnStreamFrame(stream_frame_with_fin
);
638 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
641 TEST_F(ReliableQuicStreamTest
, FinalByteOffsetFromRst
) {
642 Initialize(kShouldProcessData
);
644 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
645 QuicRstStreamFrame
rst_frame(stream_
->id(), QUIC_STREAM_CANCELLED
, 1234);
646 stream_
->OnStreamReset(rst_frame
);
647 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
650 TEST_F(ReliableQuicStreamTest
, SetDrainingIncomingOutgoing
) {
651 // Don't have incoming data consumed.
652 Initialize(kShouldNotProcessData
);
654 // Incoming data with FIN.
655 QuicStreamFrame
stream_frame_with_fin(stream_
->id(), true, 1234,
657 stream_
->OnStreamFrame(stream_frame_with_fin
);
658 // The FIN has been received but not consumed.
659 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
660 EXPECT_FALSE(stream_
->read_side_closed());
662 EXPECT_EQ(1u, session_
->GetNumOpenStreams());
664 // Outgoing data with FIN.
665 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
666 .WillOnce(Return(QuicConsumedData(2, true)));
667 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), true, nullptr);
668 EXPECT_TRUE(stream_
->write_side_closed());
670 EXPECT_EQ(1u, QuicSessionPeer::GetDrainingStreams(session_
.get())
671 ->count(kTestStreamId
));
672 EXPECT_EQ(0u, session_
->GetNumOpenStreams());
675 TEST_F(ReliableQuicStreamTest
, SetDrainingOutgoingIncoming
) {
676 // Don't have incoming data consumed.
677 Initialize(kShouldNotProcessData
);
679 // Outgoing data with FIN.
680 EXPECT_CALL(*session_
, WritevData(kTestStreamId
, _
, _
, _
, _
, _
))
681 .WillOnce(Return(QuicConsumedData(2, true)));
682 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), true, nullptr);
683 EXPECT_TRUE(stream_
->write_side_closed());
685 EXPECT_EQ(1u, session_
->GetNumOpenStreams());
687 // Incoming data with FIN.
688 QuicStreamFrame
stream_frame_with_fin(stream_
->id(), true, 1234,
690 stream_
->OnStreamFrame(stream_frame_with_fin
);
691 // The FIN has been received but not consumed.
692 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
693 EXPECT_FALSE(stream_
->read_side_closed());
695 EXPECT_EQ(1u, QuicSessionPeer::GetDrainingStreams(session_
.get())
696 ->count(kTestStreamId
));
697 EXPECT_EQ(0u, session_
->GetNumOpenStreams());
700 TEST_F(ReliableQuicStreamTest
, FecSendPolicyReceivedConnectionOption
) {
701 Initialize(kShouldProcessData
);
703 // Test ReceivedConnectionOptions.
704 QuicConfig
* config
= session_
->config();
706 copt
.push_back(kFSTR
);
707 QuicConfigPeer::SetReceivedConnectionOptions(config
, copt
);
708 EXPECT_EQ(FEC_PROTECT_OPTIONAL
, stream_
->fec_policy());
709 stream_
->SetFromConfig();
710 EXPECT_EQ(FEC_PROTECT_ALWAYS
, stream_
->fec_policy());