Don't add an aura tooltip to bubble close buttons on Windows.
[chromium-blink-merge.git] / net / quic / reliable_quic_stream_test.cc
blobb17def0d1b4fd749cfd1ebda4369df1aafb849b5
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;
23 using std::min;
24 using std::string;
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;
32 using testing::_;
34 namespace net {
35 namespace test {
36 namespace {
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 {
44 public:
45 TestStream(QuicStreamId id,
46 QuicSession* session,
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;
67 private:
68 bool should_process_data_;
69 string data_;
72 class ReliableQuicStreamTest : public ::testing::TestWithParam<bool> {
73 public:
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";
81 headers_["cookie"] =
82 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
83 "__utmc=160408618; "
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<MockSession>(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();
135 protected:
136 MockConnection* connection_;
137 scoped_ptr<MockSession> 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 QuicConnectionPeer::GetPacketCreator(connection_)->SetMaxPacketLength(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
164 // blocked.
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
186 // last data)
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 QuicConnectionPeer::GetPacketCreator(connection_)->SetMaxPacketLength(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
222 InSequence s;
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 QuicConnectionPeer::GetPacketCreator(connection_)->SetMaxPacketLength(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
257 InSequence s;
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 QuicConnectionPeer::GetPacketCreator(connection_)->SetMaxPacketLength(
281 length);
283 // Write first data onto stream, which will cause one session write.
284 EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).WillOnce(
285 Return(QuicConsumedData(kDataLen - 1, false)));
286 stream_->WriteOrBufferData(kData1, false, nullptr);
287 EXPECT_TRUE(HasWriteBlockedStreams());
289 // Queue a bytes_consumed write.
290 stream_->WriteOrBufferData(kData2, false, nullptr);
292 // Make sure we get the tail of the first write followed by the bytes_consumed
293 InSequence s;
294 EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).
295 WillOnce(Return(QuicConsumedData(1, false)));
296 EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).
297 WillOnce(Return(QuicConsumedData(kDataLen - 2, false)));
298 stream_->OnCanWrite();
300 // And finally the end of the bytes_consumed.
301 EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).
302 WillOnce(Return(QuicConsumedData(2, true)));
303 stream_->OnCanWrite();
306 TEST_F(ReliableQuicStreamTest, ConnectionCloseAfterStreamClose) {
307 Initialize(kShouldProcessData);
309 stream_->CloseReadSide();
310 stream_->CloseWriteSide();
311 EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error());
312 EXPECT_EQ(QUIC_NO_ERROR, stream_->connection_error());
313 stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR, false);
314 EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error());
315 EXPECT_EQ(QUIC_NO_ERROR, stream_->connection_error());
318 TEST_F(ReliableQuicStreamTest, RstAlwaysSentIfNoFinSent) {
319 // For flow control accounting, a stream must send either a FIN or a RST frame
320 // before termination.
321 // Test that if no FIN has been sent, we send a RST.
323 Initialize(kShouldProcessData);
324 EXPECT_FALSE(fin_sent());
325 EXPECT_FALSE(rst_sent());
327 // Write some data, with no FIN.
328 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
329 .WillOnce(Return(QuicConsumedData(1, false)));
330 stream_->WriteOrBufferData(StringPiece(kData1, 1), false, nullptr);
331 EXPECT_FALSE(fin_sent());
332 EXPECT_FALSE(rst_sent());
334 // Now close the stream, and expect that we send a RST.
335 EXPECT_CALL(*session_, SendRstStream(_, _, _));
336 stream_->OnClose();
337 EXPECT_FALSE(fin_sent());
338 EXPECT_TRUE(rst_sent());
341 TEST_F(ReliableQuicStreamTest, RstNotSentIfFinSent) {
342 // For flow control accounting, a stream must send either a FIN or a RST frame
343 // before termination.
344 // Test that if a FIN has been sent, we don't also send a RST.
346 Initialize(kShouldProcessData);
347 EXPECT_FALSE(fin_sent());
348 EXPECT_FALSE(rst_sent());
350 // Write some data, with FIN.
351 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
352 .WillOnce(Return(QuicConsumedData(1, true)));
353 stream_->WriteOrBufferData(StringPiece(kData1, 1), true, nullptr);
354 EXPECT_TRUE(fin_sent());
355 EXPECT_FALSE(rst_sent());
357 // Now close the stream, and expect that we do not send a RST.
358 stream_->OnClose();
359 EXPECT_TRUE(fin_sent());
360 EXPECT_FALSE(rst_sent());
363 TEST_F(ReliableQuicStreamTest, OnlySendOneRst) {
364 // For flow control accounting, a stream must send either a FIN or a RST frame
365 // before termination.
366 // Test that if a stream sends a RST, it doesn't send an additional RST during
367 // OnClose() (this shouldn't be harmful, but we shouldn't do it anyway...)
369 Initialize(kShouldProcessData);
370 EXPECT_FALSE(fin_sent());
371 EXPECT_FALSE(rst_sent());
373 // Reset the stream.
374 const int expected_resets = 1;
375 EXPECT_CALL(*session_, SendRstStream(_, _, _)).Times(expected_resets);
376 stream_->Reset(QUIC_STREAM_CANCELLED);
377 EXPECT_FALSE(fin_sent());
378 EXPECT_TRUE(rst_sent());
380 // Now close the stream (any further resets being sent would break the
381 // expectation above).
382 stream_->OnClose();
383 EXPECT_FALSE(fin_sent());
384 EXPECT_TRUE(rst_sent());
387 TEST_F(ReliableQuicStreamTest, StreamFlowControlMultipleWindowUpdates) {
388 set_initial_flow_control_window_bytes(1000);
390 Initialize(kShouldProcessData);
392 // If we receive multiple WINDOW_UPDATES (potentially out of order), then we
393 // want to make sure we latch the largest offset we see.
395 // Initially should be default.
396 EXPECT_EQ(
397 initial_flow_control_window_bytes_,
398 QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller()));
400 // Check a single WINDOW_UPDATE results in correct offset.
401 QuicWindowUpdateFrame window_update_1(stream_->id(), 1234);
402 stream_->OnWindowUpdateFrame(window_update_1);
403 EXPECT_EQ(
404 window_update_1.byte_offset,
405 QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller()));
407 // Now send a few more WINDOW_UPDATES and make sure that only the largest is
408 // remembered.
409 QuicWindowUpdateFrame window_update_2(stream_->id(), 1);
410 QuicWindowUpdateFrame window_update_3(stream_->id(), 9999);
411 QuicWindowUpdateFrame window_update_4(stream_->id(), 5678);
412 stream_->OnWindowUpdateFrame(window_update_2);
413 stream_->OnWindowUpdateFrame(window_update_3);
414 stream_->OnWindowUpdateFrame(window_update_4);
415 EXPECT_EQ(
416 window_update_3.byte_offset,
417 QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller()));
420 void SaveProxyAckNotifierDelegate(
421 scoped_refptr<QuicAckNotifier::DelegateInterface>* delegate_out,
422 QuicAckNotifier::DelegateInterface* delegate) {
423 *delegate_out = delegate;
426 TEST_F(ReliableQuicStreamTest, WriteOrBufferDataWithQuicAckNotifier) {
427 Initialize(kShouldProcessData);
429 scoped_refptr<MockAckNotifierDelegate> delegate(
430 new StrictMock<MockAckNotifierDelegate>);
432 const int kDataSize = 16 * 1024;
433 const string kData(kDataSize, 'a');
435 const int kFirstWriteSize = 100;
436 const int kSecondWriteSize = 50;
437 const int kLastWriteSize = kDataSize - kFirstWriteSize - kSecondWriteSize;
439 // Set a large flow control send window so this doesn't interfere with test.
440 stream_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
441 session_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
443 scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
445 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
446 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
447 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
448 Return(QuicConsumedData(kFirstWriteSize, false))));
449 stream_->WriteOrBufferData(kData, false, delegate.get());
450 EXPECT_TRUE(HasWriteBlockedStreams());
452 EXPECT_CALL(*session_,
453 WritevData(kHeadersStreamId, _, _, _, _, proxy_delegate.get()))
454 .WillOnce(Return(QuicConsumedData(kSecondWriteSize, false)));
455 stream_->OnCanWrite();
457 // No ack expected for an empty write.
458 EXPECT_CALL(*session_,
459 WritevData(kHeadersStreamId, _, _, _, _, proxy_delegate.get()))
460 .WillOnce(Return(QuicConsumedData(0, false)));
461 stream_->OnCanWrite();
463 EXPECT_CALL(*session_,
464 WritevData(kHeadersStreamId, _, _, _, _, proxy_delegate.get()))
465 .WillOnce(Return(QuicConsumedData(kLastWriteSize, false)));
466 stream_->OnCanWrite();
468 // There were two writes, so OnAckNotification is not propagated
469 // until the third Ack arrives.
470 proxy_delegate->OnAckNotification(3, 4, zero_);
471 proxy_delegate->OnAckNotification(30, 40, zero_);
473 // The arguments to delegate->OnAckNotification are the sum of the
474 // arguments to proxy_delegate OnAckNotification calls.
475 EXPECT_CALL(*delegate.get(), OnAckNotification(333, 444, zero_));
476 proxy_delegate->OnAckNotification(300, 400, zero_);
479 // Verify delegate behavior when packets are acked before the
480 // WritevData call that sends out the last byte.
481 TEST_F(ReliableQuicStreamTest, WriteOrBufferDataAckNotificationBeforeFlush) {
482 Initialize(kShouldProcessData);
484 scoped_refptr<MockAckNotifierDelegate> delegate(
485 new StrictMock<MockAckNotifierDelegate>);
487 const int kDataSize = 16 * 1024;
488 const string kData(kDataSize, 'a');
490 const int kInitialWriteSize = 100;
492 // Set a large flow control send window so this doesn't interfere with test.
493 stream_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
494 session_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
496 scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
498 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
499 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
500 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
501 Return(QuicConsumedData(kInitialWriteSize, false))));
502 stream_->WriteOrBufferData(kData, false, delegate.get());
503 EXPECT_TRUE(HasWriteBlockedStreams());
505 // Handle the ack of the first write.
506 proxy_delegate->OnAckNotification(3, 4, zero_);
507 proxy_delegate = nullptr;
509 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _)).WillOnce(
510 DoAll(WithArgs<5>(Invoke(CreateFunctor(
511 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
512 Return(QuicConsumedData(kDataSize - kInitialWriteSize, false))));
513 stream_->OnCanWrite();
515 // Handle the ack for the second write.
516 EXPECT_CALL(*delegate.get(), OnAckNotification(303, 404, zero_));
517 proxy_delegate->OnAckNotification(300, 400, zero_);
520 // Verify delegate behavior when WriteOrBufferData does not buffer.
521 TEST_F(ReliableQuicStreamTest, WriteAndBufferDataWithAckNotiferNoBuffer) {
522 Initialize(kShouldProcessData);
524 scoped_refptr<MockAckNotifierDelegate> delegate(
525 new StrictMock<MockAckNotifierDelegate>);
527 scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
529 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
530 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
531 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
532 Return(QuicConsumedData(kDataLen, true))));
533 stream_->WriteOrBufferData(kData1, true, delegate.get());
534 EXPECT_FALSE(HasWriteBlockedStreams());
536 // Handle the ack.
537 EXPECT_CALL(*delegate.get(), OnAckNotification(3, 4, zero_));
538 proxy_delegate->OnAckNotification(3, 4, zero_);
541 // Verify delegate behavior when WriteOrBufferData buffers all the data.
542 TEST_F(ReliableQuicStreamTest, BufferOnWriteAndBufferDataWithAckNotifer) {
543 Initialize(kShouldProcessData);
545 scoped_refptr<MockAckNotifierDelegate> delegate(
546 new StrictMock<MockAckNotifierDelegate>);
548 scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
550 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
551 .WillOnce(Return(QuicConsumedData(0, false)));
552 stream_->WriteOrBufferData(kData1, true, delegate.get());
553 EXPECT_TRUE(HasWriteBlockedStreams());
555 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
556 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
557 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
558 Return(QuicConsumedData(kDataLen, true))));
559 stream_->OnCanWrite();
561 // Handle the ack.
562 EXPECT_CALL(*delegate.get(), OnAckNotification(3, 4, zero_));
563 proxy_delegate->OnAckNotification(3, 4, zero_);
566 // Verify delegate behavior when WriteOrBufferData when the FIN is
567 // sent out in a different packet.
568 TEST_F(ReliableQuicStreamTest, WriteAndBufferDataWithAckNotiferOnlyFinRemains) {
569 Initialize(kShouldProcessData);
571 scoped_refptr<MockAckNotifierDelegate> delegate(
572 new StrictMock<MockAckNotifierDelegate>);
574 scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
576 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
577 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
578 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
579 Return(QuicConsumedData(kDataLen, false))));
580 stream_->WriteOrBufferData(kData1, true, delegate.get());
581 EXPECT_TRUE(HasWriteBlockedStreams());
583 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
584 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
585 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
586 Return(QuicConsumedData(0, true))));
587 stream_->OnCanWrite();
589 // Handle the acks.
590 proxy_delegate->OnAckNotification(3, 4, zero_);
591 EXPECT_CALL(*delegate.get(), OnAckNotification(33, 44, zero_));
592 proxy_delegate->OnAckNotification(30, 40, zero_);
595 // Verify that when we receive a packet which violates flow control (i.e. sends
596 // too much data on the stream) that the stream sequencer never sees this frame,
597 // as we check for violation and close the connection early.
598 TEST_F(ReliableQuicStreamTest,
599 StreamSequencerNeverSeesPacketsViolatingFlowControl) {
600 Initialize(kShouldProcessData);
602 // Receive a stream frame that violates flow control: the byte offset is
603 // higher than the receive window offset.
604 QuicStreamFrame frame(stream_->id(), false,
605 kInitialSessionFlowControlWindowForTest + 1,
606 MakeIOVector("."));
607 EXPECT_GT(frame.offset, QuicFlowControllerPeer::ReceiveWindowOffset(
608 stream_->flow_controller()));
610 // Stream should not accept the frame, and the connection should be closed.
611 EXPECT_CALL(*connection_,
612 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA));
613 stream_->OnStreamFrame(frame);
616 TEST_F(ReliableQuicStreamTest, FinalByteOffsetFromFin) {
617 Initialize(kShouldProcessData);
619 EXPECT_FALSE(stream_->HasFinalReceivedByteOffset());
621 QuicStreamFrame stream_frame_no_fin(stream_->id(), false, 1234,
622 MakeIOVector("."));
623 stream_->OnStreamFrame(stream_frame_no_fin);
624 EXPECT_FALSE(stream_->HasFinalReceivedByteOffset());
626 QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234,
627 MakeIOVector("."));
628 stream_->OnStreamFrame(stream_frame_with_fin);
629 EXPECT_TRUE(stream_->HasFinalReceivedByteOffset());
632 TEST_F(ReliableQuicStreamTest, FinalByteOffsetFromRst) {
633 Initialize(kShouldProcessData);
635 EXPECT_FALSE(stream_->HasFinalReceivedByteOffset());
636 QuicRstStreamFrame rst_frame(stream_->id(), QUIC_STREAM_CANCELLED, 1234);
637 stream_->OnStreamReset(rst_frame);
638 EXPECT_TRUE(stream_->HasFinalReceivedByteOffset());
641 } // namespace
642 } // namespace test
643 } // namespace net