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::CreateFunctor
;
26 using testing::InSequence
;
27 using testing::Invoke
;
28 using testing::Return
;
29 using testing::SaveArg
;
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;
43 class TestStream
: public ReliableQuicStream
{
45 TestStream(QuicStreamId id
,
47 bool should_process_data
)
48 : ReliableQuicStream(id
, session
),
49 should_process_data_(should_process_data
) {}
51 uint32
ProcessRawData(const char* data
, uint32 data_len
) override
{
52 EXPECT_NE(0u, data_len
);
53 DVLOG(1) << "ProcessData data_len: " << data_len
;
54 data_
+= string(data
, data_len
);
55 return should_process_data_
? data_len
: 0;
58 QuicPriority
EffectivePriority() const override
{
59 return QuicUtils::HighestPriority();
62 using ReliableQuicStream::WriteOrBufferData
;
63 using ReliableQuicStream::CloseReadSide
;
64 using ReliableQuicStream::CloseWriteSide
;
65 using ReliableQuicStream::OnClose
;
68 bool should_process_data_
;
72 class ReliableQuicStreamTest
: public ::testing::TestWithParam
<bool> {
74 ReliableQuicStreamTest()
75 : initial_flow_control_window_bytes_(kMaxPacketSize
),
76 zero_(QuicTime::Delta::Zero()),
77 supported_versions_(QuicSupportedVersions()) {
78 headers_
[":host"] = "www.google.com";
79 headers_
[":path"] = "/index.hml";
80 headers_
[":scheme"] = "https";
82 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
84 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
85 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
86 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
87 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
88 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
89 "1zFMi5vzcns38-8_Sns; "
90 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
91 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
92 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
93 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
94 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
95 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
96 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
97 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
98 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
99 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
100 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
101 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
102 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
103 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
104 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
107 void Initialize(bool stream_should_process_data
) {
108 connection_
= new StrictMock
<MockConnection
>(Perspective::IS_SERVER
,
109 supported_versions_
);
110 session_
.reset(new StrictMock
<MockQuicSpdySession
>(connection_
));
112 // New streams rely on having the peer's flow control receive window
113 // negotiated in the config.
114 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(
115 session_
->config(), initial_flow_control_window_bytes_
);
117 stream_
.reset(new TestStream(kHeadersStreamId
, session_
.get(),
118 stream_should_process_data
));
119 write_blocked_list_
=
120 QuicSessionPeer::GetWriteBlockedStreams(session_
.get());
123 bool fin_sent() { return ReliableQuicStreamPeer::FinSent(stream_
.get()); }
124 bool rst_sent() { return ReliableQuicStreamPeer::RstSent(stream_
.get()); }
126 void set_initial_flow_control_window_bytes(uint32 val
) {
127 initial_flow_control_window_bytes_
= val
;
130 bool HasWriteBlockedStreams() {
131 return write_blocked_list_
->HasWriteBlockedCryptoOrHeadersStream() ||
132 write_blocked_list_
->HasWriteBlockedDataStreams();
136 MockConnection
* connection_
;
137 scoped_ptr
<MockQuicSpdySession
> session_
;
138 scoped_ptr
<TestStream
> stream_
;
139 SpdyHeaderBlock headers_
;
140 QuicWriteBlockedList
* write_blocked_list_
;
141 uint32 initial_flow_control_window_bytes_
;
142 QuicTime::Delta zero_
;
143 QuicVersionVector supported_versions_
;
146 TEST_F(ReliableQuicStreamTest
, WriteAllData
) {
147 Initialize(kShouldProcessData
);
149 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
150 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
151 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
152 connection_
->set_max_packet_length(length
);
154 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
)).WillOnce(
155 Return(QuicConsumedData(kDataLen
, true)));
156 stream_
->WriteOrBufferData(kData1
, false, nullptr);
157 EXPECT_FALSE(HasWriteBlockedStreams());
160 TEST_F(ReliableQuicStreamTest
, NoBlockingIfNoDataOrFin
) {
161 Initialize(kShouldProcessData
);
163 // Write no data and no fin. If we consume nothing we should not be write
165 EXPECT_DFATAL(stream_
->WriteOrBufferData(StringPiece(), false, nullptr), "");
166 EXPECT_FALSE(HasWriteBlockedStreams());
169 TEST_F(ReliableQuicStreamTest
, BlockIfOnlySomeDataConsumed
) {
170 Initialize(kShouldProcessData
);
172 // Write some data and no fin. If we consume some but not all of the data,
173 // we should be write blocked a not all the data was consumed.
174 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
175 .WillOnce(Return(QuicConsumedData(1, false)));
176 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), false, nullptr);
177 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
180 TEST_F(ReliableQuicStreamTest
, BlockIfFinNotConsumedWithData
) {
181 Initialize(kShouldProcessData
);
183 // Write some data and no fin. If we consume all the data but not the fin,
184 // we should be write blocked because the fin was not consumed.
185 // (This should never actually happen as the fin should be sent out with the
187 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
188 .WillOnce(Return(QuicConsumedData(2, false)));
189 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), true, nullptr);
190 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
193 TEST_F(ReliableQuicStreamTest
, BlockIfSoloFinNotConsumed
) {
194 Initialize(kShouldProcessData
);
196 // Write no data and a fin. If we consume nothing we should be write blocked,
197 // as the fin was not consumed.
198 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
199 .WillOnce(Return(QuicConsumedData(0, false)));
200 stream_
->WriteOrBufferData(StringPiece(), true, nullptr);
201 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
204 TEST_F(ReliableQuicStreamTest
, WriteOrBufferData
) {
205 Initialize(kShouldProcessData
);
207 EXPECT_FALSE(HasWriteBlockedStreams());
208 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
209 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
210 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
211 connection_
->set_max_packet_length(length
);
213 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).WillOnce(
214 Return(QuicConsumedData(kDataLen
- 1, false)));
215 stream_
->WriteOrBufferData(kData1
, false, nullptr);
216 EXPECT_TRUE(HasWriteBlockedStreams());
218 // Queue a bytes_consumed write.
219 stream_
->WriteOrBufferData(kData2
, false, nullptr);
221 // Make sure we get the tail of the first write followed by the bytes_consumed
223 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
224 WillOnce(Return(QuicConsumedData(1, false)));
225 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
226 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
227 stream_
->OnCanWrite();
229 // And finally the end of the bytes_consumed.
230 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
231 WillOnce(Return(QuicConsumedData(2, true)));
232 stream_
->OnCanWrite();
235 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithFecProtectAlways
) {
236 Initialize(kShouldProcessData
);
238 // Set FEC policy on stream.
239 ReliableQuicStreamPeer::SetFecPolicy(stream_
.get(), FEC_PROTECT_ALWAYS
);
241 EXPECT_FALSE(HasWriteBlockedStreams());
242 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
243 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
244 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, IN_FEC_GROUP
);
245 connection_
->set_max_packet_length(length
);
247 // Write first data onto stream, which will cause one session write.
248 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).WillOnce(
249 Return(QuicConsumedData(kDataLen
- 1, false)));
250 stream_
->WriteOrBufferData(kData1
, false, nullptr);
251 EXPECT_TRUE(HasWriteBlockedStreams());
253 // Queue a bytes_consumed write.
254 stream_
->WriteOrBufferData(kData2
, false, nullptr);
256 // Make sure we get the tail of the first write followed by the bytes_consumed
258 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
259 WillOnce(Return(QuicConsumedData(1, false)));
260 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
261 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
262 stream_
->OnCanWrite();
264 // And finally the end of the bytes_consumed.
265 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
266 WillOnce(Return(QuicConsumedData(2, true)));
267 stream_
->OnCanWrite();
270 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithFecProtectOptional
) {
271 Initialize(kShouldProcessData
);
273 // Set FEC policy on stream.
274 ReliableQuicStreamPeer::SetFecPolicy(stream_
.get(), FEC_PROTECT_OPTIONAL
);
276 EXPECT_FALSE(HasWriteBlockedStreams());
277 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
278 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
279 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
280 connection_
->set_max_packet_length(length
);
282 // Write first data onto stream, which will cause one session write.
283 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).WillOnce(
284 Return(QuicConsumedData(kDataLen
- 1, false)));
285 stream_
->WriteOrBufferData(kData1
, false, nullptr);
286 EXPECT_TRUE(HasWriteBlockedStreams());
288 // Queue a bytes_consumed write.
289 stream_
->WriteOrBufferData(kData2
, false, nullptr);
291 // Make sure we get the tail of the first write followed by the bytes_consumed
293 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
294 WillOnce(Return(QuicConsumedData(1, false)));
295 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
296 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
297 stream_
->OnCanWrite();
299 // And finally the end of the bytes_consumed.
300 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
301 WillOnce(Return(QuicConsumedData(2, true)));
302 stream_
->OnCanWrite();
305 TEST_F(ReliableQuicStreamTest
, ConnectionCloseAfterStreamClose
) {
306 Initialize(kShouldProcessData
);
308 stream_
->CloseReadSide();
309 stream_
->CloseWriteSide();
310 EXPECT_EQ(QUIC_STREAM_NO_ERROR
, stream_
->stream_error());
311 EXPECT_EQ(QUIC_NO_ERROR
, stream_
->connection_error());
312 stream_
->OnConnectionClosed(QUIC_INTERNAL_ERROR
, false);
313 EXPECT_EQ(QUIC_STREAM_NO_ERROR
, stream_
->stream_error());
314 EXPECT_EQ(QUIC_NO_ERROR
, stream_
->connection_error());
317 TEST_F(ReliableQuicStreamTest
, RstAlwaysSentIfNoFinSent
) {
318 // For flow control accounting, a stream must send either a FIN or a RST frame
319 // before termination.
320 // Test that if no FIN has been sent, we send a RST.
322 Initialize(kShouldProcessData
);
323 EXPECT_FALSE(fin_sent());
324 EXPECT_FALSE(rst_sent());
326 // Write some data, with no FIN.
327 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
328 .WillOnce(Return(QuicConsumedData(1, false)));
329 stream_
->WriteOrBufferData(StringPiece(kData1
, 1), false, nullptr);
330 EXPECT_FALSE(fin_sent());
331 EXPECT_FALSE(rst_sent());
333 // Now close the stream, and expect that we send a RST.
334 EXPECT_CALL(*session_
, SendRstStream(_
, _
, _
));
336 EXPECT_FALSE(fin_sent());
337 EXPECT_TRUE(rst_sent());
340 TEST_F(ReliableQuicStreamTest
, RstNotSentIfFinSent
) {
341 // For flow control accounting, a stream must send either a FIN or a RST frame
342 // before termination.
343 // Test that if a FIN has been sent, we don't also send a RST.
345 Initialize(kShouldProcessData
);
346 EXPECT_FALSE(fin_sent());
347 EXPECT_FALSE(rst_sent());
349 // Write some data, with FIN.
350 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
351 .WillOnce(Return(QuicConsumedData(1, true)));
352 stream_
->WriteOrBufferData(StringPiece(kData1
, 1), true, nullptr);
353 EXPECT_TRUE(fin_sent());
354 EXPECT_FALSE(rst_sent());
356 // Now close the stream, and expect that we do not send a RST.
358 EXPECT_TRUE(fin_sent());
359 EXPECT_FALSE(rst_sent());
362 TEST_F(ReliableQuicStreamTest
, OnlySendOneRst
) {
363 // For flow control accounting, a stream must send either a FIN or a RST frame
364 // before termination.
365 // Test that if a stream sends a RST, it doesn't send an additional RST during
366 // OnClose() (this shouldn't be harmful, but we shouldn't do it anyway...)
368 Initialize(kShouldProcessData
);
369 EXPECT_FALSE(fin_sent());
370 EXPECT_FALSE(rst_sent());
373 const int expected_resets
= 1;
374 EXPECT_CALL(*session_
, SendRstStream(_
, _
, _
)).Times(expected_resets
);
375 stream_
->Reset(QUIC_STREAM_CANCELLED
);
376 EXPECT_FALSE(fin_sent());
377 EXPECT_TRUE(rst_sent());
379 // Now close the stream (any further resets being sent would break the
380 // expectation above).
382 EXPECT_FALSE(fin_sent());
383 EXPECT_TRUE(rst_sent());
386 TEST_F(ReliableQuicStreamTest
, StreamFlowControlMultipleWindowUpdates
) {
387 set_initial_flow_control_window_bytes(1000);
389 Initialize(kShouldProcessData
);
391 // If we receive multiple WINDOW_UPDATES (potentially out of order), then we
392 // want to make sure we latch the largest offset we see.
394 // Initially should be default.
396 initial_flow_control_window_bytes_
,
397 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
399 // Check a single WINDOW_UPDATE results in correct offset.
400 QuicWindowUpdateFrame
window_update_1(stream_
->id(), 1234);
401 stream_
->OnWindowUpdateFrame(window_update_1
);
403 window_update_1
.byte_offset
,
404 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
406 // Now send a few more WINDOW_UPDATES and make sure that only the largest is
408 QuicWindowUpdateFrame
window_update_2(stream_
->id(), 1);
409 QuicWindowUpdateFrame
window_update_3(stream_
->id(), 9999);
410 QuicWindowUpdateFrame
window_update_4(stream_
->id(), 5678);
411 stream_
->OnWindowUpdateFrame(window_update_2
);
412 stream_
->OnWindowUpdateFrame(window_update_3
);
413 stream_
->OnWindowUpdateFrame(window_update_4
);
415 window_update_3
.byte_offset
,
416 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
419 void SaveProxyAckNotifierDelegate(
420 scoped_refptr
<QuicAckNotifier::DelegateInterface
>* delegate_out
,
421 QuicAckNotifier::DelegateInterface
* delegate
) {
422 *delegate_out
= delegate
;
425 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithQuicAckNotifier
) {
426 Initialize(kShouldProcessData
);
428 scoped_refptr
<MockAckNotifierDelegate
> delegate(
429 new StrictMock
<MockAckNotifierDelegate
>);
431 const int kDataSize
= 16 * 1024;
432 const string
kData(kDataSize
, 'a');
434 const int kFirstWriteSize
= 100;
435 const int kSecondWriteSize
= 50;
436 const int kLastWriteSize
= kDataSize
- kFirstWriteSize
- kSecondWriteSize
;
438 // Set a large flow control send window so this doesn't interfere with test.
439 stream_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
440 session_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
442 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
444 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
445 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
446 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
447 Return(QuicConsumedData(kFirstWriteSize
, false))));
448 stream_
->WriteOrBufferData(kData
, false, delegate
.get());
449 EXPECT_TRUE(HasWriteBlockedStreams());
451 EXPECT_CALL(*session_
,
452 WritevData(kHeadersStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
453 .WillOnce(Return(QuicConsumedData(kSecondWriteSize
, false)));
454 stream_
->OnCanWrite();
456 // No ack expected for an empty write.
457 EXPECT_CALL(*session_
,
458 WritevData(kHeadersStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
459 .WillOnce(Return(QuicConsumedData(0, false)));
460 stream_
->OnCanWrite();
462 EXPECT_CALL(*session_
,
463 WritevData(kHeadersStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
464 .WillOnce(Return(QuicConsumedData(kLastWriteSize
, false)));
465 stream_
->OnCanWrite();
467 // There were two writes, so OnAckNotification is not propagated
468 // until the third Ack arrives.
469 proxy_delegate
->OnAckNotification(3, 4, zero_
);
470 proxy_delegate
->OnAckNotification(30, 40, zero_
);
472 // The arguments to delegate->OnAckNotification are the sum of the
473 // arguments to proxy_delegate OnAckNotification calls.
474 EXPECT_CALL(*delegate
.get(), OnAckNotification(333, 444, zero_
));
475 proxy_delegate
->OnAckNotification(300, 400, zero_
);
478 // Verify delegate behavior when packets are acked before the
479 // WritevData call that sends out the last byte.
480 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataAckNotificationBeforeFlush
) {
481 Initialize(kShouldProcessData
);
483 scoped_refptr
<MockAckNotifierDelegate
> delegate(
484 new StrictMock
<MockAckNotifierDelegate
>);
486 const int kDataSize
= 16 * 1024;
487 const string
kData(kDataSize
, 'a');
489 const int kInitialWriteSize
= 100;
491 // Set a large flow control send window so this doesn't interfere with test.
492 stream_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
493 session_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
495 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
497 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
498 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
499 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
500 Return(QuicConsumedData(kInitialWriteSize
, false))));
501 stream_
->WriteOrBufferData(kData
, false, delegate
.get());
502 EXPECT_TRUE(HasWriteBlockedStreams());
504 // Handle the ack of the first write.
505 proxy_delegate
->OnAckNotification(3, 4, zero_
);
506 proxy_delegate
= nullptr;
508 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
)).WillOnce(
509 DoAll(WithArgs
<5>(Invoke(CreateFunctor(
510 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
511 Return(QuicConsumedData(kDataSize
- kInitialWriteSize
, false))));
512 stream_
->OnCanWrite();
514 // Handle the ack for the second write.
515 EXPECT_CALL(*delegate
.get(), OnAckNotification(303, 404, zero_
));
516 proxy_delegate
->OnAckNotification(300, 400, zero_
);
519 // Verify delegate behavior when WriteOrBufferData does not buffer.
520 TEST_F(ReliableQuicStreamTest
, WriteAndBufferDataWithAckNotiferNoBuffer
) {
521 Initialize(kShouldProcessData
);
523 scoped_refptr
<MockAckNotifierDelegate
> delegate(
524 new StrictMock
<MockAckNotifierDelegate
>);
526 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
528 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
529 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
530 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
531 Return(QuicConsumedData(kDataLen
, true))));
532 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
533 EXPECT_FALSE(HasWriteBlockedStreams());
536 EXPECT_CALL(*delegate
.get(), OnAckNotification(3, 4, zero_
));
537 proxy_delegate
->OnAckNotification(3, 4, zero_
);
540 // Verify delegate behavior when WriteOrBufferData buffers all the data.
541 TEST_F(ReliableQuicStreamTest
, BufferOnWriteAndBufferDataWithAckNotifer
) {
542 Initialize(kShouldProcessData
);
544 scoped_refptr
<MockAckNotifierDelegate
> delegate(
545 new StrictMock
<MockAckNotifierDelegate
>);
547 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
549 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
550 .WillOnce(Return(QuicConsumedData(0, false)));
551 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
552 EXPECT_TRUE(HasWriteBlockedStreams());
554 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
555 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
556 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
557 Return(QuicConsumedData(kDataLen
, true))));
558 stream_
->OnCanWrite();
561 EXPECT_CALL(*delegate
.get(), OnAckNotification(3, 4, zero_
));
562 proxy_delegate
->OnAckNotification(3, 4, zero_
);
565 // Verify delegate behavior when WriteOrBufferData when the FIN is
566 // sent out in a different packet.
567 TEST_F(ReliableQuicStreamTest
, WriteAndBufferDataWithAckNotiferOnlyFinRemains
) {
568 Initialize(kShouldProcessData
);
570 scoped_refptr
<MockAckNotifierDelegate
> delegate(
571 new StrictMock
<MockAckNotifierDelegate
>);
573 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
575 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
576 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
577 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
578 Return(QuicConsumedData(kDataLen
, false))));
579 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
580 EXPECT_TRUE(HasWriteBlockedStreams());
582 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
583 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
584 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
585 Return(QuicConsumedData(0, true))));
586 stream_
->OnCanWrite();
589 proxy_delegate
->OnAckNotification(3, 4, zero_
);
590 EXPECT_CALL(*delegate
.get(), OnAckNotification(33, 44, zero_
));
591 proxy_delegate
->OnAckNotification(30, 40, zero_
);
594 // Verify that when we receive a packet which violates flow control (i.e. sends
595 // too much data on the stream) that the stream sequencer never sees this frame,
596 // as we check for violation and close the connection early.
597 TEST_F(ReliableQuicStreamTest
,
598 StreamSequencerNeverSeesPacketsViolatingFlowControl
) {
599 Initialize(kShouldProcessData
);
601 // Receive a stream frame that violates flow control: the byte offset is
602 // higher than the receive window offset.
603 QuicStreamFrame
frame(stream_
->id(), false,
604 kInitialSessionFlowControlWindowForTest
+ 1,
606 EXPECT_GT(frame
.offset
, QuicFlowControllerPeer::ReceiveWindowOffset(
607 stream_
->flow_controller()));
609 // Stream should not accept the frame, and the connection should be closed.
610 EXPECT_CALL(*connection_
,
611 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
));
612 stream_
->OnStreamFrame(frame
);
615 TEST_F(ReliableQuicStreamTest
, FinalByteOffsetFromFin
) {
616 Initialize(kShouldProcessData
);
618 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
620 QuicStreamFrame
stream_frame_no_fin(stream_
->id(), false, 1234,
622 stream_
->OnStreamFrame(stream_frame_no_fin
);
623 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
625 QuicStreamFrame
stream_frame_with_fin(stream_
->id(), true, 1234,
627 stream_
->OnStreamFrame(stream_frame_with_fin
);
628 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
631 TEST_F(ReliableQuicStreamTest
, FinalByteOffsetFromRst
) {
632 Initialize(kShouldProcessData
);
634 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
635 QuicRstStreamFrame
rst_frame(stream_
->id(), QUIC_STREAM_CANCELLED
, 1234);
636 stream_
->OnStreamReset(rst_frame
);
637 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());