Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / quic / reliable_quic_stream_test.cc
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.
4
5 #include "net/quic/reliable_quic_stream.h"
6
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"
21
22 using base::StringPiece;
23 using std::min;
24 using testing::CreateFunctor;
25 using testing::InSequence;
26 using testing::Invoke;
27 using testing::Return;
28 using testing::SaveArg;
29 using testing::StrictMock;
30 using testing::WithArgs;
31 using testing::_;
32
33 namespace net {
34 namespace test {
35 namespace {
36
37 const char kData1[] = "FooAndBar";
38 const char kData2[] = "EepAndBaz";
39 const size_t kDataLen = 9;
40 const bool kIsServer = true;
41 const bool kShouldProcessData = true;
42
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) {}
50
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;
56   }
57
58   QuicPriority EffectivePriority() const override {
59     return QuicUtils::HighestPriority();
60   }
61
62   using ReliableQuicStream::WriteOrBufferData;
63   using ReliableQuicStream::CloseReadSide;
64   using ReliableQuicStream::CloseWriteSide;
65   using ReliableQuicStream::OnClose;
66
67  private:
68   bool should_process_data_;
69   string data_;
70 };
71
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 ";
105   }
106
107   void set_supported_versions(const QuicVersionVector& versions) {
108     supported_versions_ = versions;
109   }
110
111   void Initialize(bool stream_should_process_data) {
112     connection_ =
113         new StrictMock<MockConnection>(kIsServer, supported_versions_);
114     session_.reset(new StrictMock<MockSession>(connection_));
115
116     // New streams rely on having the peer's flow control receive window
117     // negotiated in the config.
118     QuicConfigPeer::SetReceivedInitialFlowControlWindow(
119         session_->config(), initial_flow_control_window_bytes_);
120     QuicConfigPeer::SetReceivedInitialStreamFlowControlWindow(
121         session_->config(), initial_flow_control_window_bytes_);
122
123     stream_.reset(new TestStream(kHeadersStreamId, session_.get(),
124                                  stream_should_process_data));
125     write_blocked_list_ =
126         QuicSessionPeer::GetWriteBlockedStreams(session_.get());
127   }
128
129   bool fin_sent() { return ReliableQuicStreamPeer::FinSent(stream_.get()); }
130   bool rst_sent() { return ReliableQuicStreamPeer::RstSent(stream_.get()); }
131
132   void set_initial_flow_control_window_bytes(uint32 val) {
133     initial_flow_control_window_bytes_ = val;
134   }
135
136   bool HasWriteBlockedStreams() {
137     return write_blocked_list_->HasWriteBlockedCryptoOrHeadersStream() ||
138            write_blocked_list_->HasWriteBlockedDataStreams();
139   }
140
141  protected:
142   MockConnection* connection_;
143   scoped_ptr<MockSession> session_;
144   scoped_ptr<TestStream> stream_;
145   SpdyHeaderBlock headers_;
146   QuicWriteBlockedList* write_blocked_list_;
147   uint32 initial_flow_control_window_bytes_;
148   QuicTime::Delta zero_;
149   QuicVersionVector supported_versions_;
150 };
151
152 TEST_F(ReliableQuicStreamTest, WriteAllData) {
153   Initialize(kShouldProcessData);
154
155   size_t length = 1 + QuicPacketCreator::StreamFramePacketOverhead(
156       PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
157       PACKET_6BYTE_SEQUENCE_NUMBER, 0u, NOT_IN_FEC_GROUP);
158   QuicConnectionPeer::GetPacketCreator(connection_)->set_max_packet_length(
159       length);
160
161   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _)).WillOnce(
162       Return(QuicConsumedData(kDataLen, true)));
163   stream_->WriteOrBufferData(kData1, false, nullptr);
164   EXPECT_FALSE(HasWriteBlockedStreams());
165 }
166
167 TEST_F(ReliableQuicStreamTest, NoBlockingIfNoDataOrFin) {
168   Initialize(kShouldProcessData);
169
170   // Write no data and no fin.  If we consume nothing we should not be write
171   // blocked.
172   EXPECT_DFATAL(stream_->WriteOrBufferData(StringPiece(), false, nullptr), "");
173   EXPECT_FALSE(HasWriteBlockedStreams());
174 }
175
176 TEST_F(ReliableQuicStreamTest, BlockIfOnlySomeDataConsumed) {
177   Initialize(kShouldProcessData);
178
179   // Write some data and no fin.  If we consume some but not all of the data,
180   // we should be write blocked a not all the data was consumed.
181   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
182       .WillOnce(Return(QuicConsumedData(1, false)));
183   stream_->WriteOrBufferData(StringPiece(kData1, 2), false, nullptr);
184   ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
185 }
186
187 TEST_F(ReliableQuicStreamTest, BlockIfFinNotConsumedWithData) {
188   Initialize(kShouldProcessData);
189
190   // Write some data and no fin.  If we consume all the data but not the fin,
191   // we should be write blocked because the fin was not consumed.
192   // (This should never actually happen as the fin should be sent out with the
193   // last data)
194   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
195       .WillOnce(Return(QuicConsumedData(2, false)));
196   stream_->WriteOrBufferData(StringPiece(kData1, 2), true, nullptr);
197   ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
198 }
199
200 TEST_F(ReliableQuicStreamTest, BlockIfSoloFinNotConsumed) {
201   Initialize(kShouldProcessData);
202
203   // Write no data and a fin.  If we consume nothing we should be write blocked,
204   // as the fin was not consumed.
205   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
206       .WillOnce(Return(QuicConsumedData(0, false)));
207   stream_->WriteOrBufferData(StringPiece(), true, nullptr);
208   ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
209 }
210
211 TEST_F(ReliableQuicStreamTest, WriteOrBufferData) {
212   Initialize(kShouldProcessData);
213
214   EXPECT_FALSE(HasWriteBlockedStreams());
215   size_t length = 1 + QuicPacketCreator::StreamFramePacketOverhead(
216       PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
217       PACKET_6BYTE_SEQUENCE_NUMBER, 0u, NOT_IN_FEC_GROUP);
218   QuicConnectionPeer::GetPacketCreator(connection_)->set_max_packet_length(
219       length);
220
221   EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).WillOnce(
222       Return(QuicConsumedData(kDataLen - 1, false)));
223   stream_->WriteOrBufferData(kData1, false, nullptr);
224   EXPECT_TRUE(HasWriteBlockedStreams());
225
226   // Queue a bytes_consumed write.
227   stream_->WriteOrBufferData(kData2, false, nullptr);
228
229   // Make sure we get the tail of the first write followed by the bytes_consumed
230   InSequence s;
231   EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).
232       WillOnce(Return(QuicConsumedData(1, false)));
233   EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).
234       WillOnce(Return(QuicConsumedData(kDataLen - 2, false)));
235   stream_->OnCanWrite();
236
237   // And finally the end of the bytes_consumed.
238   EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).
239       WillOnce(Return(QuicConsumedData(2, true)));
240   stream_->OnCanWrite();
241 }
242
243 TEST_F(ReliableQuicStreamTest, WriteOrBufferDataWithFecProtectAlways) {
244   Initialize(kShouldProcessData);
245
246   // Set FEC policy on stream.
247   ReliableQuicStreamPeer::SetFecPolicy(stream_.get(), FEC_PROTECT_ALWAYS);
248
249   EXPECT_FALSE(HasWriteBlockedStreams());
250   size_t length = 1 + QuicPacketCreator::StreamFramePacketOverhead(
251       PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
252       PACKET_6BYTE_SEQUENCE_NUMBER, 0u, IN_FEC_GROUP);
253   QuicConnectionPeer::GetPacketCreator(connection_)->set_max_packet_length(
254       length);
255
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());
261
262   // Queue a bytes_consumed write.
263   stream_->WriteOrBufferData(kData2, false, nullptr);
264
265   // Make sure we get the tail of the first write followed by the bytes_consumed
266   InSequence s;
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();
272
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();
277 }
278
279 TEST_F(ReliableQuicStreamTest, WriteOrBufferDataWithFecProtectOptional) {
280   Initialize(kShouldProcessData);
281
282   // Set FEC policy on stream.
283   ReliableQuicStreamPeer::SetFecPolicy(stream_.get(), FEC_PROTECT_OPTIONAL);
284
285   EXPECT_FALSE(HasWriteBlockedStreams());
286   size_t length = 1 + QuicPacketCreator::StreamFramePacketOverhead(
287       PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
288       PACKET_6BYTE_SEQUENCE_NUMBER, 0u, NOT_IN_FEC_GROUP);
289   QuicConnectionPeer::GetPacketCreator(connection_)->set_max_packet_length(
290       length);
291
292   // Write first data onto stream, which will cause one session write.
293   EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).WillOnce(
294       Return(QuicConsumedData(kDataLen - 1, false)));
295   stream_->WriteOrBufferData(kData1, false, nullptr);
296   EXPECT_TRUE(HasWriteBlockedStreams());
297
298   // Queue a bytes_consumed write.
299   stream_->WriteOrBufferData(kData2, false, nullptr);
300
301   // Make sure we get the tail of the first write followed by the bytes_consumed
302   InSequence s;
303   EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).
304       WillOnce(Return(QuicConsumedData(1, false)));
305   EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).
306       WillOnce(Return(QuicConsumedData(kDataLen - 2, false)));
307   stream_->OnCanWrite();
308
309   // And finally the end of the bytes_consumed.
310   EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).
311       WillOnce(Return(QuicConsumedData(2, true)));
312   stream_->OnCanWrite();
313 }
314
315 TEST_F(ReliableQuicStreamTest, ConnectionCloseAfterStreamClose) {
316   Initialize(kShouldProcessData);
317
318   stream_->CloseReadSide();
319   stream_->CloseWriteSide();
320   EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error());
321   EXPECT_EQ(QUIC_NO_ERROR, stream_->connection_error());
322   stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR, false);
323   EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error());
324   EXPECT_EQ(QUIC_NO_ERROR, stream_->connection_error());
325 }
326
327 TEST_F(ReliableQuicStreamTest, RstAlwaysSentIfNoFinSent) {
328   // For flow control accounting, a stream must send either a FIN or a RST frame
329   // before termination.
330   // Test that if no FIN has been sent, we send a RST.
331
332   Initialize(kShouldProcessData);
333   EXPECT_FALSE(fin_sent());
334   EXPECT_FALSE(rst_sent());
335
336   // Write some data, with no FIN.
337   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
338       .WillOnce(Return(QuicConsumedData(1, false)));
339   stream_->WriteOrBufferData(StringPiece(kData1, 1), false, nullptr);
340   EXPECT_FALSE(fin_sent());
341   EXPECT_FALSE(rst_sent());
342
343   // Now close the stream, and expect that we send a RST.
344   EXPECT_CALL(*session_, SendRstStream(_, _, _));
345   stream_->OnClose();
346   EXPECT_FALSE(fin_sent());
347   EXPECT_TRUE(rst_sent());
348 }
349
350 TEST_F(ReliableQuicStreamTest, RstNotSentIfFinSent) {
351   // For flow control accounting, a stream must send either a FIN or a RST frame
352   // before termination.
353   // Test that if a FIN has been sent, we don't also send a RST.
354
355   Initialize(kShouldProcessData);
356   EXPECT_FALSE(fin_sent());
357   EXPECT_FALSE(rst_sent());
358
359   // Write some data, with FIN.
360   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
361       .WillOnce(Return(QuicConsumedData(1, true)));
362   stream_->WriteOrBufferData(StringPiece(kData1, 1), true, nullptr);
363   EXPECT_TRUE(fin_sent());
364   EXPECT_FALSE(rst_sent());
365
366   // Now close the stream, and expect that we do not send a RST.
367   stream_->OnClose();
368   EXPECT_TRUE(fin_sent());
369   EXPECT_FALSE(rst_sent());
370 }
371
372 TEST_F(ReliableQuicStreamTest, OnlySendOneRst) {
373   // For flow control accounting, a stream must send either a FIN or a RST frame
374   // before termination.
375   // Test that if a stream sends a RST, it doesn't send an additional RST during
376   // OnClose() (this shouldn't be harmful, but we shouldn't do it anyway...)
377
378   Initialize(kShouldProcessData);
379   EXPECT_FALSE(fin_sent());
380   EXPECT_FALSE(rst_sent());
381
382   // Reset the stream.
383   const int expected_resets = 1;
384   EXPECT_CALL(*session_, SendRstStream(_, _, _)).Times(expected_resets);
385   stream_->Reset(QUIC_STREAM_CANCELLED);
386   EXPECT_FALSE(fin_sent());
387   EXPECT_TRUE(rst_sent());
388
389   // Now close the stream (any further resets being sent would break the
390   // expectation above).
391   stream_->OnClose();
392   EXPECT_FALSE(fin_sent());
393   EXPECT_TRUE(rst_sent());
394 }
395
396 TEST_F(ReliableQuicStreamTest, StreamFlowControlMultipleWindowUpdates) {
397   set_initial_flow_control_window_bytes(1000);
398
399   Initialize(kShouldProcessData);
400
401   // If we receive multiple WINDOW_UPDATES (potentially out of order), then we
402   // want to make sure we latch the largest offset we see.
403
404   // Initially should be default.
405   EXPECT_EQ(
406       initial_flow_control_window_bytes_,
407       QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller()));
408
409   // Check a single WINDOW_UPDATE results in correct offset.
410   QuicWindowUpdateFrame window_update_1(stream_->id(), 1234);
411   stream_->OnWindowUpdateFrame(window_update_1);
412   EXPECT_EQ(
413       window_update_1.byte_offset,
414       QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller()));
415
416   // Now send a few more WINDOW_UPDATES and make sure that only the largest is
417   // remembered.
418   QuicWindowUpdateFrame window_update_2(stream_->id(), 1);
419   QuicWindowUpdateFrame window_update_3(stream_->id(), 9999);
420   QuicWindowUpdateFrame window_update_4(stream_->id(), 5678);
421   stream_->OnWindowUpdateFrame(window_update_2);
422   stream_->OnWindowUpdateFrame(window_update_3);
423   stream_->OnWindowUpdateFrame(window_update_4);
424   EXPECT_EQ(
425       window_update_3.byte_offset,
426       QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller()));
427 }
428
429 void SaveProxyAckNotifierDelegate(
430     scoped_refptr<QuicAckNotifier::DelegateInterface>* delegate_out,
431     QuicAckNotifier::DelegateInterface* delegate) {
432   *delegate_out = delegate;
433 }
434
435 TEST_F(ReliableQuicStreamTest, WriteOrBufferDataWithQuicAckNotifier) {
436   Initialize(kShouldProcessData);
437
438   scoped_refptr<MockAckNotifierDelegate> delegate(
439       new StrictMock<MockAckNotifierDelegate>);
440
441   const int kDataSize = 16 * 1024;
442   const string kData(kDataSize, 'a');
443
444   const int kFirstWriteSize = 100;
445   const int kSecondWriteSize = 50;
446   const int kLastWriteSize = kDataSize - kFirstWriteSize - kSecondWriteSize;
447
448   // Set a large flow control send window so this doesn't interfere with test.
449   stream_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
450   session_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
451
452   scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
453
454   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
455       .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
456                           &SaveProxyAckNotifierDelegate, &proxy_delegate))),
457                       Return(QuicConsumedData(kFirstWriteSize, false))));
458   stream_->WriteOrBufferData(kData, false, delegate.get());
459   EXPECT_TRUE(HasWriteBlockedStreams());
460
461   EXPECT_CALL(*session_,
462               WritevData(kHeadersStreamId, _, _, _, _, proxy_delegate.get()))
463       .WillOnce(Return(QuicConsumedData(kSecondWriteSize, false)));
464   stream_->OnCanWrite();
465
466   // No ack expected for an empty write.
467   EXPECT_CALL(*session_,
468               WritevData(kHeadersStreamId, _, _, _, _, proxy_delegate.get()))
469       .WillOnce(Return(QuicConsumedData(0, false)));
470   stream_->OnCanWrite();
471
472   EXPECT_CALL(*session_,
473               WritevData(kHeadersStreamId, _, _, _, _, proxy_delegate.get()))
474       .WillOnce(Return(QuicConsumedData(kLastWriteSize, false)));
475   stream_->OnCanWrite();
476
477   // There were two writes, so OnAckNotification is not propagated
478   // until the third Ack arrives.
479   proxy_delegate->OnAckNotification(1, 2, 3, 4, zero_);
480   proxy_delegate->OnAckNotification(10, 20, 30, 40, zero_);
481
482   // The arguments to delegate->OnAckNotification are the sum of the
483   // arguments to proxy_delegate OnAckNotification calls.
484   EXPECT_CALL(*delegate.get(), OnAckNotification(111, 222, 333, 444, zero_));
485   proxy_delegate->OnAckNotification(100, 200, 300, 400, zero_);
486 }
487
488 // Verify delegate behavior when packets are acked before the
489 // WritevData call that sends out the last byte.
490 TEST_F(ReliableQuicStreamTest, WriteOrBufferDataAckNotificationBeforeFlush) {
491   Initialize(kShouldProcessData);
492
493   scoped_refptr<MockAckNotifierDelegate> delegate(
494       new StrictMock<MockAckNotifierDelegate>);
495
496   const int kDataSize = 16 * 1024;
497   const string kData(kDataSize, 'a');
498
499   const int kInitialWriteSize = 100;
500
501   // Set a large flow control send window so this doesn't interfere with test.
502   stream_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
503   session_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
504
505   scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
506
507   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
508       .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
509                           &SaveProxyAckNotifierDelegate, &proxy_delegate))),
510                       Return(QuicConsumedData(kInitialWriteSize, false))));
511   stream_->WriteOrBufferData(kData, false, delegate.get());
512   EXPECT_TRUE(HasWriteBlockedStreams());
513
514   // Handle the ack of the first write.
515   proxy_delegate->OnAckNotification(1, 2, 3, 4, zero_);
516   proxy_delegate = nullptr;
517
518   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _)).WillOnce(
519       DoAll(WithArgs<5>(Invoke(CreateFunctor(
520                 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
521             Return(QuicConsumedData(kDataSize - kInitialWriteSize, false))));
522   stream_->OnCanWrite();
523
524   // Handle the ack for the second write.
525   EXPECT_CALL(*delegate.get(), OnAckNotification(101, 202, 303, 404, zero_));
526   proxy_delegate->OnAckNotification(100, 200, 300, 400, zero_);
527 }
528
529 // Verify delegate behavior when WriteOrBufferData does not buffer.
530 TEST_F(ReliableQuicStreamTest, WriteAndBufferDataWithAckNotiferNoBuffer) {
531   Initialize(kShouldProcessData);
532
533   scoped_refptr<MockAckNotifierDelegate> delegate(
534       new StrictMock<MockAckNotifierDelegate>);
535
536   scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
537
538   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
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());
544
545   // Handle the ack.
546   EXPECT_CALL(*delegate.get(), OnAckNotification(1, 2, 3, 4, zero_));
547   proxy_delegate->OnAckNotification(1, 2, 3, 4, zero_);
548 }
549
550 // Verify delegate behavior when WriteOrBufferData buffers all the data.
551 TEST_F(ReliableQuicStreamTest, BufferOnWriteAndBufferDataWithAckNotifer) {
552   Initialize(kShouldProcessData);
553
554   scoped_refptr<MockAckNotifierDelegate> delegate(
555       new StrictMock<MockAckNotifierDelegate>);
556
557   scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
558
559   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
560       .WillOnce(Return(QuicConsumedData(0, false)));
561   stream_->WriteOrBufferData(kData1, true, delegate.get());
562   EXPECT_TRUE(HasWriteBlockedStreams());
563
564   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
565       .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
566                           &SaveProxyAckNotifierDelegate, &proxy_delegate))),
567                       Return(QuicConsumedData(kDataLen, true))));
568   stream_->OnCanWrite();
569
570   // Handle the ack.
571   EXPECT_CALL(*delegate.get(), OnAckNotification(1, 2, 3, 4, zero_));
572   proxy_delegate->OnAckNotification(1, 2, 3, 4, zero_);
573 }
574
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);
579
580   scoped_refptr<MockAckNotifierDelegate> delegate(
581       new StrictMock<MockAckNotifierDelegate>);
582
583   scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
584
585   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
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());
591
592   EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
593       .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
594                           &SaveProxyAckNotifierDelegate, &proxy_delegate))),
595                       Return(QuicConsumedData(0, true))));
596   stream_->OnCanWrite();
597
598   // Handle the acks.
599   proxy_delegate->OnAckNotification(1, 2, 3, 4, zero_);
600   EXPECT_CALL(*delegate.get(), OnAckNotification(11, 22, 33, 44, zero_));
601   proxy_delegate->OnAckNotification(10, 20, 30, 40, zero_);
602 }
603
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);
610
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,
615                         MakeIOVector("."));
616   EXPECT_GT(frame.offset, QuicFlowControllerPeer::ReceiveWindowOffset(
617                               stream_->flow_controller()));
618
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);
623 }
624
625 TEST_F(ReliableQuicStreamTest, FinalByteOffsetFromFin) {
626   Initialize(kShouldProcessData);
627
628   EXPECT_FALSE(stream_->HasFinalReceivedByteOffset());
629
630   QuicStreamFrame stream_frame_no_fin(stream_->id(), false, 1234,
631                                       MakeIOVector("."));
632   stream_->OnStreamFrame(stream_frame_no_fin);
633   EXPECT_FALSE(stream_->HasFinalReceivedByteOffset());
634
635   QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234,
636                                         MakeIOVector("."));
637   stream_->OnStreamFrame(stream_frame_with_fin);
638   EXPECT_TRUE(stream_->HasFinalReceivedByteOffset());
639 }
640
641 TEST_F(ReliableQuicStreamTest, FinalByteOffsetFromRst) {
642   Initialize(kShouldProcessData);
643
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());
648 }
649
650 }  // namespace
651 }  // namespace test
652 }  // namespace net