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 kIsServer
= true;
42 const bool kShouldProcessData
= true;
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 uint32
ProcessRawData(const char* data
, uint32 data_len
) override
{
53 EXPECT_NE(0u, data_len
);
54 DVLOG(1) << "ProcessData data_len: " << data_len
;
55 data_
+= string(data
, data_len
);
56 return should_process_data_
? data_len
: 0;
59 QuicPriority
EffectivePriority() const override
{
60 return QuicUtils::HighestPriority();
63 using ReliableQuicStream::WriteOrBufferData
;
64 using ReliableQuicStream::CloseReadSide
;
65 using ReliableQuicStream::CloseWriteSide
;
66 using ReliableQuicStream::OnClose
;
69 bool should_process_data_
;
73 class ReliableQuicStreamTest
: public ::testing::TestWithParam
<bool> {
75 ReliableQuicStreamTest()
76 : initial_flow_control_window_bytes_(kMaxPacketSize
),
77 zero_(QuicTime::Delta::Zero()),
78 supported_versions_(QuicSupportedVersions()) {
79 headers_
[":host"] = "www.google.com";
80 headers_
[":path"] = "/index.hml";
81 headers_
[":scheme"] = "https";
83 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
85 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
86 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
87 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
88 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
89 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
90 "1zFMi5vzcns38-8_Sns; "
91 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
92 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
93 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
94 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
95 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
96 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
97 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
98 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
99 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
100 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
101 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
102 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
103 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
104 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
105 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
108 void Initialize(bool stream_should_process_data
) {
110 new StrictMock
<MockConnection
>(kIsServer
, supported_versions_
);
111 session_
.reset(new StrictMock
<MockSession
>(connection_
));
113 // New streams rely on having the peer's flow control receive window
114 // negotiated in the config.
115 QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(
116 session_
->config(), initial_flow_control_window_bytes_
);
118 stream_
.reset(new TestStream(kHeadersStreamId
, session_
.get(),
119 stream_should_process_data
));
120 write_blocked_list_
=
121 QuicSessionPeer::GetWriteBlockedStreams(session_
.get());
124 bool fin_sent() { return ReliableQuicStreamPeer::FinSent(stream_
.get()); }
125 bool rst_sent() { return ReliableQuicStreamPeer::RstSent(stream_
.get()); }
127 void set_initial_flow_control_window_bytes(uint32 val
) {
128 initial_flow_control_window_bytes_
= val
;
131 bool HasWriteBlockedStreams() {
132 return write_blocked_list_
->HasWriteBlockedCryptoOrHeadersStream() ||
133 write_blocked_list_
->HasWriteBlockedDataStreams();
137 MockConnection
* connection_
;
138 scoped_ptr
<MockSession
> session_
;
139 scoped_ptr
<TestStream
> stream_
;
140 SpdyHeaderBlock headers_
;
141 QuicWriteBlockedList
* write_blocked_list_
;
142 uint32 initial_flow_control_window_bytes_
;
143 QuicTime::Delta zero_
;
144 QuicVersionVector supported_versions_
;
147 TEST_F(ReliableQuicStreamTest
, WriteAllData
) {
148 Initialize(kShouldProcessData
);
150 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
151 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
152 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
153 QuicConnectionPeer::GetPacketCreator(connection_
)->set_max_packet_length(
156 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
)).WillOnce(
157 Return(QuicConsumedData(kDataLen
, true)));
158 stream_
->WriteOrBufferData(kData1
, false, nullptr);
159 EXPECT_FALSE(HasWriteBlockedStreams());
162 TEST_F(ReliableQuicStreamTest
, NoBlockingIfNoDataOrFin
) {
163 Initialize(kShouldProcessData
);
165 // Write no data and no fin. If we consume nothing we should not be write
167 EXPECT_DFATAL(stream_
->WriteOrBufferData(StringPiece(), false, nullptr), "");
168 EXPECT_FALSE(HasWriteBlockedStreams());
171 TEST_F(ReliableQuicStreamTest
, BlockIfOnlySomeDataConsumed
) {
172 Initialize(kShouldProcessData
);
174 // Write some data and no fin. If we consume some but not all of the data,
175 // we should be write blocked a not all the data was consumed.
176 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
177 .WillOnce(Return(QuicConsumedData(1, false)));
178 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), false, nullptr);
179 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
182 TEST_F(ReliableQuicStreamTest
, BlockIfFinNotConsumedWithData
) {
183 Initialize(kShouldProcessData
);
185 // Write some data and no fin. If we consume all the data but not the fin,
186 // we should be write blocked because the fin was not consumed.
187 // (This should never actually happen as the fin should be sent out with the
189 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
190 .WillOnce(Return(QuicConsumedData(2, false)));
191 stream_
->WriteOrBufferData(StringPiece(kData1
, 2), true, nullptr);
192 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
195 TEST_F(ReliableQuicStreamTest
, BlockIfSoloFinNotConsumed
) {
196 Initialize(kShouldProcessData
);
198 // Write no data and a fin. If we consume nothing we should be write blocked,
199 // as the fin was not consumed.
200 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
201 .WillOnce(Return(QuicConsumedData(0, false)));
202 stream_
->WriteOrBufferData(StringPiece(), true, nullptr);
203 ASSERT_EQ(1u, write_blocked_list_
->NumBlockedStreams());
206 TEST_F(ReliableQuicStreamTest
, WriteOrBufferData
) {
207 Initialize(kShouldProcessData
);
209 EXPECT_FALSE(HasWriteBlockedStreams());
210 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
211 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
212 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
213 QuicConnectionPeer::GetPacketCreator(connection_
)->set_max_packet_length(
216 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).WillOnce(
217 Return(QuicConsumedData(kDataLen
- 1, false)));
218 stream_
->WriteOrBufferData(kData1
, false, nullptr);
219 EXPECT_TRUE(HasWriteBlockedStreams());
221 // Queue a bytes_consumed write.
222 stream_
->WriteOrBufferData(kData2
, false, nullptr);
224 // Make sure we get the tail of the first write followed by the bytes_consumed
226 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
227 WillOnce(Return(QuicConsumedData(1, false)));
228 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
229 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
230 stream_
->OnCanWrite();
232 // And finally the end of the bytes_consumed.
233 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, _
, _
)).
234 WillOnce(Return(QuicConsumedData(2, true)));
235 stream_
->OnCanWrite();
238 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithFecProtectAlways
) {
239 Initialize(kShouldProcessData
);
241 // Set FEC policy on stream.
242 ReliableQuicStreamPeer::SetFecPolicy(stream_
.get(), FEC_PROTECT_ALWAYS
);
244 EXPECT_FALSE(HasWriteBlockedStreams());
245 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
246 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
247 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, IN_FEC_GROUP
);
248 QuicConnectionPeer::GetPacketCreator(connection_
)->set_max_packet_length(
251 // Write first data onto stream, which will cause one session write.
252 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).WillOnce(
253 Return(QuicConsumedData(kDataLen
- 1, false)));
254 stream_
->WriteOrBufferData(kData1
, false, nullptr);
255 EXPECT_TRUE(HasWriteBlockedStreams());
257 // Queue a bytes_consumed write.
258 stream_
->WriteOrBufferData(kData2
, false, nullptr);
260 // Make sure we get the tail of the first write followed by the bytes_consumed
262 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
263 WillOnce(Return(QuicConsumedData(1, false)));
264 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
265 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
266 stream_
->OnCanWrite();
268 // And finally the end of the bytes_consumed.
269 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MUST_FEC_PROTECT
, _
)).
270 WillOnce(Return(QuicConsumedData(2, true)));
271 stream_
->OnCanWrite();
274 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithFecProtectOptional
) {
275 Initialize(kShouldProcessData
);
277 // Set FEC policy on stream.
278 ReliableQuicStreamPeer::SetFecPolicy(stream_
.get(), FEC_PROTECT_OPTIONAL
);
280 EXPECT_FALSE(HasWriteBlockedStreams());
281 size_t length
= 1 + QuicPacketCreator::StreamFramePacketOverhead(
282 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
283 PACKET_6BYTE_SEQUENCE_NUMBER
, 0u, NOT_IN_FEC_GROUP
);
284 QuicConnectionPeer::GetPacketCreator(connection_
)->set_max_packet_length(
287 // Write first data onto stream, which will cause one session write.
288 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).WillOnce(
289 Return(QuicConsumedData(kDataLen
- 1, false)));
290 stream_
->WriteOrBufferData(kData1
, false, nullptr);
291 EXPECT_TRUE(HasWriteBlockedStreams());
293 // Queue a bytes_consumed write.
294 stream_
->WriteOrBufferData(kData2
, false, nullptr);
296 // Make sure we get the tail of the first write followed by the bytes_consumed
298 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
299 WillOnce(Return(QuicConsumedData(1, false)));
300 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
301 WillOnce(Return(QuicConsumedData(kDataLen
- 2, false)));
302 stream_
->OnCanWrite();
304 // And finally the end of the bytes_consumed.
305 EXPECT_CALL(*session_
, WritevData(_
, _
, _
, _
, MAY_FEC_PROTECT
, _
)).
306 WillOnce(Return(QuicConsumedData(2, true)));
307 stream_
->OnCanWrite();
310 TEST_F(ReliableQuicStreamTest
, ConnectionCloseAfterStreamClose
) {
311 Initialize(kShouldProcessData
);
313 stream_
->CloseReadSide();
314 stream_
->CloseWriteSide();
315 EXPECT_EQ(QUIC_STREAM_NO_ERROR
, stream_
->stream_error());
316 EXPECT_EQ(QUIC_NO_ERROR
, stream_
->connection_error());
317 stream_
->OnConnectionClosed(QUIC_INTERNAL_ERROR
, false);
318 EXPECT_EQ(QUIC_STREAM_NO_ERROR
, stream_
->stream_error());
319 EXPECT_EQ(QUIC_NO_ERROR
, stream_
->connection_error());
322 TEST_F(ReliableQuicStreamTest
, RstAlwaysSentIfNoFinSent
) {
323 // For flow control accounting, a stream must send either a FIN or a RST frame
324 // before termination.
325 // Test that if no FIN has been sent, we send a RST.
327 Initialize(kShouldProcessData
);
328 EXPECT_FALSE(fin_sent());
329 EXPECT_FALSE(rst_sent());
331 // Write some data, with no FIN.
332 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
333 .WillOnce(Return(QuicConsumedData(1, false)));
334 stream_
->WriteOrBufferData(StringPiece(kData1
, 1), false, nullptr);
335 EXPECT_FALSE(fin_sent());
336 EXPECT_FALSE(rst_sent());
338 // Now close the stream, and expect that we send a RST.
339 EXPECT_CALL(*session_
, SendRstStream(_
, _
, _
));
341 EXPECT_FALSE(fin_sent());
342 EXPECT_TRUE(rst_sent());
345 TEST_F(ReliableQuicStreamTest
, RstNotSentIfFinSent
) {
346 // For flow control accounting, a stream must send either a FIN or a RST frame
347 // before termination.
348 // Test that if a FIN has been sent, we don't also send a RST.
350 Initialize(kShouldProcessData
);
351 EXPECT_FALSE(fin_sent());
352 EXPECT_FALSE(rst_sent());
354 // Write some data, with FIN.
355 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
356 .WillOnce(Return(QuicConsumedData(1, true)));
357 stream_
->WriteOrBufferData(StringPiece(kData1
, 1), true, nullptr);
358 EXPECT_TRUE(fin_sent());
359 EXPECT_FALSE(rst_sent());
361 // Now close the stream, and expect that we do not send a RST.
363 EXPECT_TRUE(fin_sent());
364 EXPECT_FALSE(rst_sent());
367 TEST_F(ReliableQuicStreamTest
, OnlySendOneRst
) {
368 // For flow control accounting, a stream must send either a FIN or a RST frame
369 // before termination.
370 // Test that if a stream sends a RST, it doesn't send an additional RST during
371 // OnClose() (this shouldn't be harmful, but we shouldn't do it anyway...)
373 Initialize(kShouldProcessData
);
374 EXPECT_FALSE(fin_sent());
375 EXPECT_FALSE(rst_sent());
378 const int expected_resets
= 1;
379 EXPECT_CALL(*session_
, SendRstStream(_
, _
, _
)).Times(expected_resets
);
380 stream_
->Reset(QUIC_STREAM_CANCELLED
);
381 EXPECT_FALSE(fin_sent());
382 EXPECT_TRUE(rst_sent());
384 // Now close the stream (any further resets being sent would break the
385 // expectation above).
387 EXPECT_FALSE(fin_sent());
388 EXPECT_TRUE(rst_sent());
391 TEST_F(ReliableQuicStreamTest
, StreamFlowControlMultipleWindowUpdates
) {
392 set_initial_flow_control_window_bytes(1000);
394 Initialize(kShouldProcessData
);
396 // If we receive multiple WINDOW_UPDATES (potentially out of order), then we
397 // want to make sure we latch the largest offset we see.
399 // Initially should be default.
401 initial_flow_control_window_bytes_
,
402 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
404 // Check a single WINDOW_UPDATE results in correct offset.
405 QuicWindowUpdateFrame
window_update_1(stream_
->id(), 1234);
406 stream_
->OnWindowUpdateFrame(window_update_1
);
408 window_update_1
.byte_offset
,
409 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
411 // Now send a few more WINDOW_UPDATES and make sure that only the largest is
413 QuicWindowUpdateFrame
window_update_2(stream_
->id(), 1);
414 QuicWindowUpdateFrame
window_update_3(stream_
->id(), 9999);
415 QuicWindowUpdateFrame
window_update_4(stream_
->id(), 5678);
416 stream_
->OnWindowUpdateFrame(window_update_2
);
417 stream_
->OnWindowUpdateFrame(window_update_3
);
418 stream_
->OnWindowUpdateFrame(window_update_4
);
420 window_update_3
.byte_offset
,
421 QuicFlowControllerPeer::SendWindowOffset(stream_
->flow_controller()));
424 void SaveProxyAckNotifierDelegate(
425 scoped_refptr
<QuicAckNotifier::DelegateInterface
>* delegate_out
,
426 QuicAckNotifier::DelegateInterface
* delegate
) {
427 *delegate_out
= delegate
;
430 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataWithQuicAckNotifier
) {
431 Initialize(kShouldProcessData
);
433 scoped_refptr
<MockAckNotifierDelegate
> delegate(
434 new StrictMock
<MockAckNotifierDelegate
>);
436 const int kDataSize
= 16 * 1024;
437 const string
kData(kDataSize
, 'a');
439 const int kFirstWriteSize
= 100;
440 const int kSecondWriteSize
= 50;
441 const int kLastWriteSize
= kDataSize
- kFirstWriteSize
- kSecondWriteSize
;
443 // Set a large flow control send window so this doesn't interfere with test.
444 stream_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
445 session_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
447 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
449 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
450 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
451 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
452 Return(QuicConsumedData(kFirstWriteSize
, false))));
453 stream_
->WriteOrBufferData(kData
, false, delegate
.get());
454 EXPECT_TRUE(HasWriteBlockedStreams());
456 EXPECT_CALL(*session_
,
457 WritevData(kHeadersStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
458 .WillOnce(Return(QuicConsumedData(kSecondWriteSize
, false)));
459 stream_
->OnCanWrite();
461 // No ack expected for an empty write.
462 EXPECT_CALL(*session_
,
463 WritevData(kHeadersStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
464 .WillOnce(Return(QuicConsumedData(0, false)));
465 stream_
->OnCanWrite();
467 EXPECT_CALL(*session_
,
468 WritevData(kHeadersStreamId
, _
, _
, _
, _
, proxy_delegate
.get()))
469 .WillOnce(Return(QuicConsumedData(kLastWriteSize
, false)));
470 stream_
->OnCanWrite();
472 // There were two writes, so OnAckNotification is not propagated
473 // until the third Ack arrives.
474 proxy_delegate
->OnAckNotification(3, 4, zero_
);
475 proxy_delegate
->OnAckNotification(30, 40, zero_
);
477 // The arguments to delegate->OnAckNotification are the sum of the
478 // arguments to proxy_delegate OnAckNotification calls.
479 EXPECT_CALL(*delegate
.get(), OnAckNotification(333, 444, zero_
));
480 proxy_delegate
->OnAckNotification(300, 400, zero_
);
483 // Verify delegate behavior when packets are acked before the
484 // WritevData call that sends out the last byte.
485 TEST_F(ReliableQuicStreamTest
, WriteOrBufferDataAckNotificationBeforeFlush
) {
486 Initialize(kShouldProcessData
);
488 scoped_refptr
<MockAckNotifierDelegate
> delegate(
489 new StrictMock
<MockAckNotifierDelegate
>);
491 const int kDataSize
= 16 * 1024;
492 const string
kData(kDataSize
, 'a');
494 const int kInitialWriteSize
= 100;
496 // Set a large flow control send window so this doesn't interfere with test.
497 stream_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
498 session_
->flow_controller()->UpdateSendWindowOffset(kDataSize
+ 1);
500 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
502 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
503 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
504 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
505 Return(QuicConsumedData(kInitialWriteSize
, false))));
506 stream_
->WriteOrBufferData(kData
, false, delegate
.get());
507 EXPECT_TRUE(HasWriteBlockedStreams());
509 // Handle the ack of the first write.
510 proxy_delegate
->OnAckNotification(3, 4, zero_
);
511 proxy_delegate
= nullptr;
513 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
)).WillOnce(
514 DoAll(WithArgs
<5>(Invoke(CreateFunctor(
515 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
516 Return(QuicConsumedData(kDataSize
- kInitialWriteSize
, false))));
517 stream_
->OnCanWrite();
519 // Handle the ack for the second write.
520 EXPECT_CALL(*delegate
.get(), OnAckNotification(303, 404, zero_
));
521 proxy_delegate
->OnAckNotification(300, 400, zero_
);
524 // Verify delegate behavior when WriteOrBufferData does not buffer.
525 TEST_F(ReliableQuicStreamTest
, WriteAndBufferDataWithAckNotiferNoBuffer
) {
526 Initialize(kShouldProcessData
);
528 scoped_refptr
<MockAckNotifierDelegate
> delegate(
529 new StrictMock
<MockAckNotifierDelegate
>);
531 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
533 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
534 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
535 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
536 Return(QuicConsumedData(kDataLen
, true))));
537 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
538 EXPECT_FALSE(HasWriteBlockedStreams());
541 EXPECT_CALL(*delegate
.get(), OnAckNotification(3, 4, zero_
));
542 proxy_delegate
->OnAckNotification(3, 4, zero_
);
545 // Verify delegate behavior when WriteOrBufferData buffers all the data.
546 TEST_F(ReliableQuicStreamTest
, BufferOnWriteAndBufferDataWithAckNotifer
) {
547 Initialize(kShouldProcessData
);
549 scoped_refptr
<MockAckNotifierDelegate
> delegate(
550 new StrictMock
<MockAckNotifierDelegate
>);
552 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
554 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
555 .WillOnce(Return(QuicConsumedData(0, false)));
556 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
557 EXPECT_TRUE(HasWriteBlockedStreams());
559 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
560 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
561 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
562 Return(QuicConsumedData(kDataLen
, true))));
563 stream_
->OnCanWrite();
566 EXPECT_CALL(*delegate
.get(), OnAckNotification(3, 4, zero_
));
567 proxy_delegate
->OnAckNotification(3, 4, zero_
);
570 // Verify delegate behavior when WriteOrBufferData when the FIN is
571 // sent out in a different packet.
572 TEST_F(ReliableQuicStreamTest
, WriteAndBufferDataWithAckNotiferOnlyFinRemains
) {
573 Initialize(kShouldProcessData
);
575 scoped_refptr
<MockAckNotifierDelegate
> delegate(
576 new StrictMock
<MockAckNotifierDelegate
>);
578 scoped_refptr
<QuicAckNotifier::DelegateInterface
> proxy_delegate
;
580 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
581 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
582 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
583 Return(QuicConsumedData(kDataLen
, false))));
584 stream_
->WriteOrBufferData(kData1
, true, delegate
.get());
585 EXPECT_TRUE(HasWriteBlockedStreams());
587 EXPECT_CALL(*session_
, WritevData(kHeadersStreamId
, _
, _
, _
, _
, _
))
588 .WillOnce(DoAll(WithArgs
<5>(Invoke(CreateFunctor(
589 &SaveProxyAckNotifierDelegate
, &proxy_delegate
))),
590 Return(QuicConsumedData(0, true))));
591 stream_
->OnCanWrite();
594 proxy_delegate
->OnAckNotification(3, 4, zero_
);
595 EXPECT_CALL(*delegate
.get(), OnAckNotification(33, 44, zero_
));
596 proxy_delegate
->OnAckNotification(30, 40, zero_
);
599 // Verify that when we receive a packet which violates flow control (i.e. sends
600 // too much data on the stream) that the stream sequencer never sees this frame,
601 // as we check for violation and close the connection early.
602 TEST_F(ReliableQuicStreamTest
,
603 StreamSequencerNeverSeesPacketsViolatingFlowControl
) {
604 Initialize(kShouldProcessData
);
606 // Receive a stream frame that violates flow control: the byte offset is
607 // higher than the receive window offset.
608 QuicStreamFrame
frame(stream_
->id(), false,
609 kInitialSessionFlowControlWindowForTest
+ 1,
611 EXPECT_GT(frame
.offset
, QuicFlowControllerPeer::ReceiveWindowOffset(
612 stream_
->flow_controller()));
614 // Stream should not accept the frame, and the connection should be closed.
615 EXPECT_CALL(*connection_
,
616 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
));
617 stream_
->OnStreamFrame(frame
);
620 TEST_F(ReliableQuicStreamTest
, FinalByteOffsetFromFin
) {
621 Initialize(kShouldProcessData
);
623 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
625 QuicStreamFrame
stream_frame_no_fin(stream_
->id(), false, 1234,
627 stream_
->OnStreamFrame(stream_frame_no_fin
);
628 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
630 QuicStreamFrame
stream_frame_with_fin(stream_
->id(), true, 1234,
632 stream_
->OnStreamFrame(stream_frame_with_fin
);
633 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
636 TEST_F(ReliableQuicStreamTest
, FinalByteOffsetFromRst
) {
637 Initialize(kShouldProcessData
);
639 EXPECT_FALSE(stream_
->HasFinalReceivedByteOffset());
640 QuicRstStreamFrame
rst_frame(stream_
->id(), QUIC_STREAM_CANCELLED
, 1234);
641 stream_
->OnStreamReset(rst_frame
);
642 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());