[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / net / quic / reliable_quic_stream_test.cc
blobf88beb129a31103fa5328fd35777cdd675c6027e
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_)->set_max_packet_length(
153 length);
155 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _)).WillOnce(
156 Return(QuicConsumedData(kDataLen, true)));
157 stream_->WriteOrBufferData(kData1, false, nullptr);
158 EXPECT_FALSE(HasWriteBlockedStreams());
161 TEST_F(ReliableQuicStreamTest, NoBlockingIfNoDataOrFin) {
162 Initialize(kShouldProcessData);
164 // Write no data and no fin. If we consume nothing we should not be write
165 // blocked.
166 EXPECT_DFATAL(stream_->WriteOrBufferData(StringPiece(), false, nullptr), "");
167 EXPECT_FALSE(HasWriteBlockedStreams());
170 TEST_F(ReliableQuicStreamTest, BlockIfOnlySomeDataConsumed) {
171 Initialize(kShouldProcessData);
173 // Write some data and no fin. If we consume some but not all of the data,
174 // we should be write blocked a not all the data was consumed.
175 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
176 .WillOnce(Return(QuicConsumedData(1, false)));
177 stream_->WriteOrBufferData(StringPiece(kData1, 2), false, nullptr);
178 ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
181 TEST_F(ReliableQuicStreamTest, BlockIfFinNotConsumedWithData) {
182 Initialize(kShouldProcessData);
184 // Write some data and no fin. If we consume all the data but not the fin,
185 // we should be write blocked because the fin was not consumed.
186 // (This should never actually happen as the fin should be sent out with the
187 // last data)
188 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
189 .WillOnce(Return(QuicConsumedData(2, false)));
190 stream_->WriteOrBufferData(StringPiece(kData1, 2), true, nullptr);
191 ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
194 TEST_F(ReliableQuicStreamTest, BlockIfSoloFinNotConsumed) {
195 Initialize(kShouldProcessData);
197 // Write no data and a fin. If we consume nothing we should be write blocked,
198 // as the fin was not consumed.
199 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
200 .WillOnce(Return(QuicConsumedData(0, false)));
201 stream_->WriteOrBufferData(StringPiece(), true, nullptr);
202 ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
205 TEST_F(ReliableQuicStreamTest, WriteOrBufferData) {
206 Initialize(kShouldProcessData);
208 EXPECT_FALSE(HasWriteBlockedStreams());
209 size_t length = 1 + QuicPacketCreator::StreamFramePacketOverhead(
210 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
211 PACKET_6BYTE_SEQUENCE_NUMBER, 0u, NOT_IN_FEC_GROUP);
212 QuicConnectionPeer::GetPacketCreator(connection_)->set_max_packet_length(
213 length);
215 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).WillOnce(
216 Return(QuicConsumedData(kDataLen - 1, false)));
217 stream_->WriteOrBufferData(kData1, false, nullptr);
218 EXPECT_TRUE(HasWriteBlockedStreams());
220 // Queue a bytes_consumed write.
221 stream_->WriteOrBufferData(kData2, false, nullptr);
223 // Make sure we get the tail of the first write followed by the bytes_consumed
224 InSequence s;
225 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).
226 WillOnce(Return(QuicConsumedData(1, false)));
227 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).
228 WillOnce(Return(QuicConsumedData(kDataLen - 2, false)));
229 stream_->OnCanWrite();
231 // And finally the end of the bytes_consumed.
232 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).
233 WillOnce(Return(QuicConsumedData(2, true)));
234 stream_->OnCanWrite();
237 TEST_F(ReliableQuicStreamTest, WriteOrBufferDataWithFecProtectAlways) {
238 Initialize(kShouldProcessData);
240 // Set FEC policy on stream.
241 ReliableQuicStreamPeer::SetFecPolicy(stream_.get(), FEC_PROTECT_ALWAYS);
243 EXPECT_FALSE(HasWriteBlockedStreams());
244 size_t length = 1 + QuicPacketCreator::StreamFramePacketOverhead(
245 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
246 PACKET_6BYTE_SEQUENCE_NUMBER, 0u, IN_FEC_GROUP);
247 QuicConnectionPeer::GetPacketCreator(connection_)->set_max_packet_length(
248 length);
250 // Write first data onto stream, which will cause one session write.
251 EXPECT_CALL(*session_, WritevData(_, _, _, _, MUST_FEC_PROTECT, _)).WillOnce(
252 Return(QuicConsumedData(kDataLen - 1, false)));
253 stream_->WriteOrBufferData(kData1, false, nullptr);
254 EXPECT_TRUE(HasWriteBlockedStreams());
256 // Queue a bytes_consumed write.
257 stream_->WriteOrBufferData(kData2, false, nullptr);
259 // Make sure we get the tail of the first write followed by the bytes_consumed
260 InSequence s;
261 EXPECT_CALL(*session_, WritevData(_, _, _, _, MUST_FEC_PROTECT, _)).
262 WillOnce(Return(QuicConsumedData(1, false)));
263 EXPECT_CALL(*session_, WritevData(_, _, _, _, MUST_FEC_PROTECT, _)).
264 WillOnce(Return(QuicConsumedData(kDataLen - 2, false)));
265 stream_->OnCanWrite();
267 // And finally the end of the bytes_consumed.
268 EXPECT_CALL(*session_, WritevData(_, _, _, _, MUST_FEC_PROTECT, _)).
269 WillOnce(Return(QuicConsumedData(2, true)));
270 stream_->OnCanWrite();
273 TEST_F(ReliableQuicStreamTest, WriteOrBufferDataWithFecProtectOptional) {
274 Initialize(kShouldProcessData);
276 // Set FEC policy on stream.
277 ReliableQuicStreamPeer::SetFecPolicy(stream_.get(), FEC_PROTECT_OPTIONAL);
279 EXPECT_FALSE(HasWriteBlockedStreams());
280 size_t length = 1 + QuicPacketCreator::StreamFramePacketOverhead(
281 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
282 PACKET_6BYTE_SEQUENCE_NUMBER, 0u, NOT_IN_FEC_GROUP);
283 QuicConnectionPeer::GetPacketCreator(connection_)->set_max_packet_length(
284 length);
286 // Write first data onto stream, which will cause one session write.
287 EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).WillOnce(
288 Return(QuicConsumedData(kDataLen - 1, false)));
289 stream_->WriteOrBufferData(kData1, false, nullptr);
290 EXPECT_TRUE(HasWriteBlockedStreams());
292 // Queue a bytes_consumed write.
293 stream_->WriteOrBufferData(kData2, false, nullptr);
295 // Make sure we get the tail of the first write followed by the bytes_consumed
296 InSequence s;
297 EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).
298 WillOnce(Return(QuicConsumedData(1, false)));
299 EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).
300 WillOnce(Return(QuicConsumedData(kDataLen - 2, false)));
301 stream_->OnCanWrite();
303 // And finally the end of the bytes_consumed.
304 EXPECT_CALL(*session_, WritevData(_, _, _, _, MAY_FEC_PROTECT, _)).
305 WillOnce(Return(QuicConsumedData(2, true)));
306 stream_->OnCanWrite();
309 TEST_F(ReliableQuicStreamTest, ConnectionCloseAfterStreamClose) {
310 Initialize(kShouldProcessData);
312 stream_->CloseReadSide();
313 stream_->CloseWriteSide();
314 EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error());
315 EXPECT_EQ(QUIC_NO_ERROR, stream_->connection_error());
316 stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR, false);
317 EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error());
318 EXPECT_EQ(QUIC_NO_ERROR, stream_->connection_error());
321 TEST_F(ReliableQuicStreamTest, RstAlwaysSentIfNoFinSent) {
322 // For flow control accounting, a stream must send either a FIN or a RST frame
323 // before termination.
324 // Test that if no FIN has been sent, we send a RST.
326 Initialize(kShouldProcessData);
327 EXPECT_FALSE(fin_sent());
328 EXPECT_FALSE(rst_sent());
330 // Write some data, with no FIN.
331 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
332 .WillOnce(Return(QuicConsumedData(1, false)));
333 stream_->WriteOrBufferData(StringPiece(kData1, 1), false, nullptr);
334 EXPECT_FALSE(fin_sent());
335 EXPECT_FALSE(rst_sent());
337 // Now close the stream, and expect that we send a RST.
338 EXPECT_CALL(*session_, SendRstStream(_, _, _));
339 stream_->OnClose();
340 EXPECT_FALSE(fin_sent());
341 EXPECT_TRUE(rst_sent());
344 TEST_F(ReliableQuicStreamTest, RstNotSentIfFinSent) {
345 // For flow control accounting, a stream must send either a FIN or a RST frame
346 // before termination.
347 // Test that if a FIN has been sent, we don't also send a RST.
349 Initialize(kShouldProcessData);
350 EXPECT_FALSE(fin_sent());
351 EXPECT_FALSE(rst_sent());
353 // Write some data, with FIN.
354 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
355 .WillOnce(Return(QuicConsumedData(1, true)));
356 stream_->WriteOrBufferData(StringPiece(kData1, 1), true, nullptr);
357 EXPECT_TRUE(fin_sent());
358 EXPECT_FALSE(rst_sent());
360 // Now close the stream, and expect that we do not send a RST.
361 stream_->OnClose();
362 EXPECT_TRUE(fin_sent());
363 EXPECT_FALSE(rst_sent());
366 TEST_F(ReliableQuicStreamTest, OnlySendOneRst) {
367 // For flow control accounting, a stream must send either a FIN or a RST frame
368 // before termination.
369 // Test that if a stream sends a RST, it doesn't send an additional RST during
370 // OnClose() (this shouldn't be harmful, but we shouldn't do it anyway...)
372 Initialize(kShouldProcessData);
373 EXPECT_FALSE(fin_sent());
374 EXPECT_FALSE(rst_sent());
376 // Reset the stream.
377 const int expected_resets = 1;
378 EXPECT_CALL(*session_, SendRstStream(_, _, _)).Times(expected_resets);
379 stream_->Reset(QUIC_STREAM_CANCELLED);
380 EXPECT_FALSE(fin_sent());
381 EXPECT_TRUE(rst_sent());
383 // Now close the stream (any further resets being sent would break the
384 // expectation above).
385 stream_->OnClose();
386 EXPECT_FALSE(fin_sent());
387 EXPECT_TRUE(rst_sent());
390 TEST_F(ReliableQuicStreamTest, StreamFlowControlMultipleWindowUpdates) {
391 set_initial_flow_control_window_bytes(1000);
393 Initialize(kShouldProcessData);
395 // If we receive multiple WINDOW_UPDATES (potentially out of order), then we
396 // want to make sure we latch the largest offset we see.
398 // Initially should be default.
399 EXPECT_EQ(
400 initial_flow_control_window_bytes_,
401 QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller()));
403 // Check a single WINDOW_UPDATE results in correct offset.
404 QuicWindowUpdateFrame window_update_1(stream_->id(), 1234);
405 stream_->OnWindowUpdateFrame(window_update_1);
406 EXPECT_EQ(
407 window_update_1.byte_offset,
408 QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller()));
410 // Now send a few more WINDOW_UPDATES and make sure that only the largest is
411 // remembered.
412 QuicWindowUpdateFrame window_update_2(stream_->id(), 1);
413 QuicWindowUpdateFrame window_update_3(stream_->id(), 9999);
414 QuicWindowUpdateFrame window_update_4(stream_->id(), 5678);
415 stream_->OnWindowUpdateFrame(window_update_2);
416 stream_->OnWindowUpdateFrame(window_update_3);
417 stream_->OnWindowUpdateFrame(window_update_4);
418 EXPECT_EQ(
419 window_update_3.byte_offset,
420 QuicFlowControllerPeer::SendWindowOffset(stream_->flow_controller()));
423 void SaveProxyAckNotifierDelegate(
424 scoped_refptr<QuicAckNotifier::DelegateInterface>* delegate_out,
425 QuicAckNotifier::DelegateInterface* delegate) {
426 *delegate_out = delegate;
429 TEST_F(ReliableQuicStreamTest, WriteOrBufferDataWithQuicAckNotifier) {
430 Initialize(kShouldProcessData);
432 scoped_refptr<MockAckNotifierDelegate> delegate(
433 new StrictMock<MockAckNotifierDelegate>);
435 const int kDataSize = 16 * 1024;
436 const string kData(kDataSize, 'a');
438 const int kFirstWriteSize = 100;
439 const int kSecondWriteSize = 50;
440 const int kLastWriteSize = kDataSize - kFirstWriteSize - kSecondWriteSize;
442 // Set a large flow control send window so this doesn't interfere with test.
443 stream_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
444 session_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
446 scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
448 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
449 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
450 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
451 Return(QuicConsumedData(kFirstWriteSize, false))));
452 stream_->WriteOrBufferData(kData, false, delegate.get());
453 EXPECT_TRUE(HasWriteBlockedStreams());
455 EXPECT_CALL(*session_,
456 WritevData(kHeadersStreamId, _, _, _, _, proxy_delegate.get()))
457 .WillOnce(Return(QuicConsumedData(kSecondWriteSize, false)));
458 stream_->OnCanWrite();
460 // No ack expected for an empty write.
461 EXPECT_CALL(*session_,
462 WritevData(kHeadersStreamId, _, _, _, _, proxy_delegate.get()))
463 .WillOnce(Return(QuicConsumedData(0, false)));
464 stream_->OnCanWrite();
466 EXPECT_CALL(*session_,
467 WritevData(kHeadersStreamId, _, _, _, _, proxy_delegate.get()))
468 .WillOnce(Return(QuicConsumedData(kLastWriteSize, false)));
469 stream_->OnCanWrite();
471 // There were two writes, so OnAckNotification is not propagated
472 // until the third Ack arrives.
473 proxy_delegate->OnAckNotification(3, 4, zero_);
474 proxy_delegate->OnAckNotification(30, 40, zero_);
476 // The arguments to delegate->OnAckNotification are the sum of the
477 // arguments to proxy_delegate OnAckNotification calls.
478 EXPECT_CALL(*delegate.get(), OnAckNotification(333, 444, zero_));
479 proxy_delegate->OnAckNotification(300, 400, zero_);
482 // Verify delegate behavior when packets are acked before the
483 // WritevData call that sends out the last byte.
484 TEST_F(ReliableQuicStreamTest, WriteOrBufferDataAckNotificationBeforeFlush) {
485 Initialize(kShouldProcessData);
487 scoped_refptr<MockAckNotifierDelegate> delegate(
488 new StrictMock<MockAckNotifierDelegate>);
490 const int kDataSize = 16 * 1024;
491 const string kData(kDataSize, 'a');
493 const int kInitialWriteSize = 100;
495 // Set a large flow control send window so this doesn't interfere with test.
496 stream_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
497 session_->flow_controller()->UpdateSendWindowOffset(kDataSize + 1);
499 scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
501 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
502 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
503 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
504 Return(QuicConsumedData(kInitialWriteSize, false))));
505 stream_->WriteOrBufferData(kData, false, delegate.get());
506 EXPECT_TRUE(HasWriteBlockedStreams());
508 // Handle the ack of the first write.
509 proxy_delegate->OnAckNotification(3, 4, zero_);
510 proxy_delegate = nullptr;
512 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _)).WillOnce(
513 DoAll(WithArgs<5>(Invoke(CreateFunctor(
514 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
515 Return(QuicConsumedData(kDataSize - kInitialWriteSize, false))));
516 stream_->OnCanWrite();
518 // Handle the ack for the second write.
519 EXPECT_CALL(*delegate.get(), OnAckNotification(303, 404, zero_));
520 proxy_delegate->OnAckNotification(300, 400, zero_);
523 // Verify delegate behavior when WriteOrBufferData does not buffer.
524 TEST_F(ReliableQuicStreamTest, WriteAndBufferDataWithAckNotiferNoBuffer) {
525 Initialize(kShouldProcessData);
527 scoped_refptr<MockAckNotifierDelegate> delegate(
528 new StrictMock<MockAckNotifierDelegate>);
530 scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
532 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
533 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
534 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
535 Return(QuicConsumedData(kDataLen, true))));
536 stream_->WriteOrBufferData(kData1, true, delegate.get());
537 EXPECT_FALSE(HasWriteBlockedStreams());
539 // Handle the ack.
540 EXPECT_CALL(*delegate.get(), OnAckNotification(3, 4, zero_));
541 proxy_delegate->OnAckNotification(3, 4, zero_);
544 // Verify delegate behavior when WriteOrBufferData buffers all the data.
545 TEST_F(ReliableQuicStreamTest, BufferOnWriteAndBufferDataWithAckNotifer) {
546 Initialize(kShouldProcessData);
548 scoped_refptr<MockAckNotifierDelegate> delegate(
549 new StrictMock<MockAckNotifierDelegate>);
551 scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
553 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
554 .WillOnce(Return(QuicConsumedData(0, false)));
555 stream_->WriteOrBufferData(kData1, true, delegate.get());
556 EXPECT_TRUE(HasWriteBlockedStreams());
558 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
559 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
560 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
561 Return(QuicConsumedData(kDataLen, true))));
562 stream_->OnCanWrite();
564 // Handle the ack.
565 EXPECT_CALL(*delegate.get(), OnAckNotification(3, 4, zero_));
566 proxy_delegate->OnAckNotification(3, 4, zero_);
569 // Verify delegate behavior when WriteOrBufferData when the FIN is
570 // sent out in a different packet.
571 TEST_F(ReliableQuicStreamTest, WriteAndBufferDataWithAckNotiferOnlyFinRemains) {
572 Initialize(kShouldProcessData);
574 scoped_refptr<MockAckNotifierDelegate> delegate(
575 new StrictMock<MockAckNotifierDelegate>);
577 scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
579 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
580 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
581 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
582 Return(QuicConsumedData(kDataLen, false))));
583 stream_->WriteOrBufferData(kData1, true, delegate.get());
584 EXPECT_TRUE(HasWriteBlockedStreams());
586 EXPECT_CALL(*session_, WritevData(kHeadersStreamId, _, _, _, _, _))
587 .WillOnce(DoAll(WithArgs<5>(Invoke(CreateFunctor(
588 &SaveProxyAckNotifierDelegate, &proxy_delegate))),
589 Return(QuicConsumedData(0, true))));
590 stream_->OnCanWrite();
592 // Handle the acks.
593 proxy_delegate->OnAckNotification(3, 4, zero_);
594 EXPECT_CALL(*delegate.get(), OnAckNotification(33, 44, zero_));
595 proxy_delegate->OnAckNotification(30, 40, zero_);
598 // Verify that when we receive a packet which violates flow control (i.e. sends
599 // too much data on the stream) that the stream sequencer never sees this frame,
600 // as we check for violation and close the connection early.
601 TEST_F(ReliableQuicStreamTest,
602 StreamSequencerNeverSeesPacketsViolatingFlowControl) {
603 Initialize(kShouldProcessData);
605 // Receive a stream frame that violates flow control: the byte offset is
606 // higher than the receive window offset.
607 QuicStreamFrame frame(stream_->id(), false,
608 kInitialSessionFlowControlWindowForTest + 1,
609 MakeIOVector("."));
610 EXPECT_GT(frame.offset, QuicFlowControllerPeer::ReceiveWindowOffset(
611 stream_->flow_controller()));
613 // Stream should not accept the frame, and the connection should be closed.
614 EXPECT_CALL(*connection_,
615 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA));
616 stream_->OnStreamFrame(frame);
619 TEST_F(ReliableQuicStreamTest, FinalByteOffsetFromFin) {
620 Initialize(kShouldProcessData);
622 EXPECT_FALSE(stream_->HasFinalReceivedByteOffset());
624 QuicStreamFrame stream_frame_no_fin(stream_->id(), false, 1234,
625 MakeIOVector("."));
626 stream_->OnStreamFrame(stream_frame_no_fin);
627 EXPECT_FALSE(stream_->HasFinalReceivedByteOffset());
629 QuicStreamFrame stream_frame_with_fin(stream_->id(), true, 1234,
630 MakeIOVector("."));
631 stream_->OnStreamFrame(stream_frame_with_fin);
632 EXPECT_TRUE(stream_->HasFinalReceivedByteOffset());
635 TEST_F(ReliableQuicStreamTest, FinalByteOffsetFromRst) {
636 Initialize(kShouldProcessData);
638 EXPECT_FALSE(stream_->HasFinalReceivedByteOffset());
639 QuicRstStreamFrame rst_frame(stream_->id(), QUIC_STREAM_CANCELLED, 1234);
640 stream_->OnStreamReset(rst_frame);
641 EXPECT_TRUE(stream_->HasFinalReceivedByteOffset());
644 } // namespace
645 } // namespace test
646 } // namespace net