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