1 // Copyright 2013 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/quic_data_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_flow_controller_peer.h"
13 #include "net/quic/test_tools/quic_session_peer.h"
14 #include "net/quic/test_tools/quic_test_utils.h"
15 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
16 #include "net/test/gtest_util.h"
17 #include "testing/gmock/include/gmock/gmock.h"
19 using base::StringPiece
;
22 using testing::Return
;
23 using testing::StrictMock
;
30 const bool kShouldProcessData
= true;
32 class TestStream
: public QuicDataStream
{
34 TestStream(QuicStreamId id
,
35 QuicSpdySession
* session
,
36 bool should_process_data
)
37 : QuicDataStream(id
, session
),
38 should_process_data_(should_process_data
) {}
40 void OnDataAvailable() override
{
41 if (!should_process_data_
) {
46 vec
.iov_base
= buffer
;
47 vec
.iov_len
= arraysize(buffer
);
48 size_t bytes_read
= Readv(&vec
, 1);
49 data_
+= string(buffer
, bytes_read
);
52 using ReliableQuicStream::WriteOrBufferData
;
53 using ReliableQuicStream::CloseReadSide
;
54 using ReliableQuicStream::CloseWriteSide
;
56 const string
& data() const { return data_
; }
59 bool should_process_data_
;
63 class QuicDataStreamTest
: public ::testing::TestWithParam
<QuicVersion
> {
65 QuicDataStreamTest() {
66 headers_
[":host"] = "www.google.com";
67 headers_
[":path"] = "/index.hml";
68 headers_
[":scheme"] = "https";
70 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
72 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
73 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
74 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
75 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
76 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
77 "1zFMi5vzcns38-8_Sns; "
78 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
79 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
80 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
81 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
82 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
83 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
84 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
85 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
86 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
87 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
88 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
89 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
90 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
91 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
92 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
95 void Initialize(bool stream_should_process_data
) {
96 connection_
= new testing::StrictMock
<MockConnection
>(
97 Perspective::IS_SERVER
, SupportedVersions(GetParam()));
98 session_
.reset(new testing::StrictMock
<MockQuicSpdySession
>(connection_
));
99 stream_
.reset(new TestStream(kClientDataStreamId1
, session_
.get(),
100 stream_should_process_data
));
101 stream2_
.reset(new TestStream(kClientDataStreamId2
, session_
.get(),
102 stream_should_process_data
));
103 write_blocked_list_
=
104 QuicSessionPeer::GetWriteBlockedStreams(session_
.get());
108 MockConnection
* connection_
;
109 scoped_ptr
<MockQuicSpdySession
> session_
;
110 scoped_ptr
<TestStream
> stream_
;
111 scoped_ptr
<TestStream
> stream2_
;
112 SpdyHeaderBlock headers_
;
113 QuicWriteBlockedList
* write_blocked_list_
;
116 INSTANTIATE_TEST_CASE_P(Tests
, QuicDataStreamTest
,
117 ::testing::ValuesIn(QuicSupportedVersions()));
119 TEST_P(QuicDataStreamTest
, ProcessHeaders
) {
120 Initialize(kShouldProcessData
);
123 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
124 stream_
->OnStreamHeadersPriority(QuicUtils::HighestPriority());
125 stream_
->OnStreamHeaders(headers
);
126 EXPECT_EQ("", stream_
->data());
127 EXPECT_EQ(headers
, stream_
->decompressed_headers());
128 stream_
->OnStreamHeadersComplete(false, headers
.size());
129 EXPECT_EQ(QuicUtils::HighestPriority(), stream_
->EffectivePriority());
130 EXPECT_EQ("", stream_
->data());
131 EXPECT_EQ(headers
, stream_
->decompressed_headers());
132 EXPECT_FALSE(stream_
->IsDoneReading());
135 TEST_P(QuicDataStreamTest
, ProcessHeadersWithFin
) {
136 Initialize(kShouldProcessData
);
139 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
140 stream_
->OnStreamHeadersPriority(QuicUtils::HighestPriority());
141 stream_
->OnStreamHeaders(headers
);
142 EXPECT_EQ("", stream_
->data());
143 EXPECT_EQ(headers
, stream_
->decompressed_headers());
144 stream_
->OnStreamHeadersComplete(true, headers
.size());
145 EXPECT_EQ(QuicUtils::HighestPriority(), stream_
->EffectivePriority());
146 EXPECT_EQ("", stream_
->data());
147 EXPECT_EQ(headers
, stream_
->decompressed_headers());
148 EXPECT_FALSE(stream_
->IsDoneReading());
149 EXPECT_TRUE(stream_
->HasFinalReceivedByteOffset());
152 TEST_P(QuicDataStreamTest
, MarkHeadersConsumed
) {
153 Initialize(kShouldProcessData
);
156 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
157 string body
= "this is the body";
159 stream_
->OnStreamHeaders(headers
);
160 stream_
->OnStreamHeadersComplete(false, headers
.size());
161 EXPECT_EQ(headers
, stream_
->decompressed_headers());
163 headers
.erase(0, 10);
164 stream_
->MarkHeadersConsumed(10);
165 EXPECT_EQ(headers
, stream_
->decompressed_headers());
167 stream_
->MarkHeadersConsumed(headers
.length());
168 EXPECT_EQ("", stream_
->decompressed_headers());
171 TEST_P(QuicDataStreamTest
, ProcessHeadersAndBody
) {
172 Initialize(kShouldProcessData
);
175 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
176 string body
= "this is the body";
178 stream_
->OnStreamHeaders(headers
);
179 EXPECT_EQ("", stream_
->data());
180 EXPECT_EQ(headers
, stream_
->decompressed_headers());
181 stream_
->OnStreamHeadersComplete(false, headers
.size());
182 EXPECT_EQ(headers
, stream_
->decompressed_headers());
183 stream_
->MarkHeadersConsumed(headers
.length());
184 QuicStreamFrame
frame(kClientDataStreamId1
, false, 0, StringPiece(body
));
185 stream_
->OnStreamFrame(frame
);
186 EXPECT_EQ("", stream_
->decompressed_headers());
187 EXPECT_EQ(body
, stream_
->data());
190 TEST_P(QuicDataStreamTest
, ProcessHeadersAndBodyFragments
) {
192 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
193 string body
= "this is the body";
195 for (size_t fragment_size
= 1; fragment_size
< body
.size();
197 Initialize(kShouldProcessData
);
198 for (size_t offset
= 0; offset
< headers
.size();
199 offset
+= fragment_size
) {
200 size_t remaining_data
= headers
.size() - offset
;
201 StringPiece
fragment(headers
.data() + offset
,
202 min(fragment_size
, remaining_data
));
203 stream_
->OnStreamHeaders(fragment
);
205 stream_
->OnStreamHeadersComplete(false, headers
.size());
206 ASSERT_EQ(headers
, stream_
->decompressed_headers())
207 << "fragment_size: " << fragment_size
;
208 stream_
->MarkHeadersConsumed(headers
.length());
209 for (size_t offset
= 0; offset
< body
.size(); offset
+= fragment_size
) {
210 size_t remaining_data
= body
.size() - offset
;
211 StringPiece
fragment(body
.data() + offset
,
212 min(fragment_size
, remaining_data
));
213 QuicStreamFrame
frame(kClientDataStreamId1
, false, offset
,
214 StringPiece(fragment
));
215 stream_
->OnStreamFrame(frame
);
217 ASSERT_EQ(body
, stream_
->data()) << "fragment_size: " << fragment_size
;
221 TEST_P(QuicDataStreamTest
, ProcessHeadersAndBodyFragmentsSplit
) {
223 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
224 string body
= "this is the body";
226 for (size_t split_point
= 1; split_point
< body
.size() - 1; ++split_point
) {
227 Initialize(kShouldProcessData
);
228 StringPiece
headers1(headers
.data(), split_point
);
229 stream_
->OnStreamHeaders(headers1
);
231 StringPiece
headers2(headers
.data() + split_point
,
232 headers
.size() - split_point
);
233 stream_
->OnStreamHeaders(headers2
);
234 stream_
->OnStreamHeadersComplete(false, headers
.size());
235 ASSERT_EQ(headers
, stream_
->decompressed_headers())
236 << "split_point: " << split_point
;
237 stream_
->MarkHeadersConsumed(headers
.length());
239 StringPiece
fragment1(body
.data(), split_point
);
240 QuicStreamFrame
frame1(kClientDataStreamId1
, false, 0,
241 StringPiece(fragment1
));
242 stream_
->OnStreamFrame(frame1
);
244 StringPiece
fragment2(body
.data() + split_point
,
245 body
.size() - split_point
);
246 QuicStreamFrame
frame2(kClientDataStreamId1
, false, split_point
,
247 StringPiece(fragment2
));
248 stream_
->OnStreamFrame(frame2
);
250 ASSERT_EQ(body
, stream_
->data()) << "split_point: " << split_point
;
254 TEST_P(QuicDataStreamTest
, ProcessHeadersAndBodyReadv
) {
255 Initialize(!kShouldProcessData
);
258 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
259 string body
= "this is the body";
261 stream_
->OnStreamHeaders(headers
);
262 stream_
->OnStreamHeadersComplete(false, headers
.size());
263 QuicStreamFrame
frame(kClientDataStreamId1
, false, 0, StringPiece(body
));
264 stream_
->OnStreamFrame(frame
);
265 stream_
->MarkHeadersConsumed(headers
.length());
268 ASSERT_LT(body
.length(), arraysize(buffer
));
270 vec
.iov_base
= buffer
;
271 vec
.iov_len
= arraysize(buffer
);
273 size_t bytes_read
= stream_
->Readv(&vec
, 1);
274 EXPECT_EQ(body
.length(), bytes_read
);
275 EXPECT_EQ(body
, string(buffer
, bytes_read
));
278 TEST_P(QuicDataStreamTest
, ProcessHeadersAndBodyMarkConsumed
) {
279 Initialize(!kShouldProcessData
);
282 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
283 string body
= "this is the body";
285 stream_
->OnStreamHeaders(headers
);
286 stream_
->OnStreamHeadersComplete(false, headers
.size());
287 QuicStreamFrame
frame(kClientDataStreamId1
, false, 0, StringPiece(body
));
288 stream_
->OnStreamFrame(frame
);
289 stream_
->MarkHeadersConsumed(headers
.length());
293 EXPECT_EQ(1, stream_
->GetReadableRegions(&vec
, 1));
294 EXPECT_EQ(body
.length(), vec
.iov_len
);
295 EXPECT_EQ(body
, string(static_cast<char*>(vec
.iov_base
), vec
.iov_len
));
297 stream_
->MarkConsumed(body
.length());
298 EXPECT_EQ(body
.length(), stream_
->flow_controller()->bytes_consumed());
301 TEST_P(QuicDataStreamTest
, ProcessHeadersAndBodyIncrementalReadv
) {
302 Initialize(!kShouldProcessData
);
305 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
306 string body
= "this is the body";
307 stream_
->OnStreamHeaders(headers
);
308 stream_
->OnStreamHeadersComplete(false, headers
.size());
309 QuicStreamFrame
frame(kClientDataStreamId1
, false, 0, StringPiece(body
));
310 stream_
->OnStreamFrame(frame
);
311 stream_
->MarkHeadersConsumed(headers
.length());
315 vec
.iov_base
= buffer
;
316 vec
.iov_len
= arraysize(buffer
);
318 for (size_t i
= 0; i
< body
.length(); ++i
) {
319 size_t bytes_read
= stream_
->Readv(&vec
, 1);
320 ASSERT_EQ(1u, bytes_read
);
321 EXPECT_EQ(body
.data()[i
], buffer
[0]);
325 TEST_P(QuicDataStreamTest
, ProcessHeadersUsingReadvWithMultipleIovecs
) {
326 Initialize(!kShouldProcessData
);
329 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
330 string body
= "this is the body";
331 stream_
->OnStreamHeaders(headers
);
332 stream_
->OnStreamHeadersComplete(false, headers
.size());
333 QuicStreamFrame
frame(kClientDataStreamId1
, false, 0, StringPiece(body
));
334 stream_
->OnStreamFrame(frame
);
335 stream_
->MarkHeadersConsumed(headers
.length());
340 vec
[0].iov_base
= buffer1
;
341 vec
[0].iov_len
= arraysize(buffer1
);
342 vec
[1].iov_base
= buffer2
;
343 vec
[1].iov_len
= arraysize(buffer2
);
345 for (size_t i
= 0; i
< body
.length(); i
+= 2) {
346 size_t bytes_read
= stream_
->Readv(vec
, 2);
347 ASSERT_EQ(2u, bytes_read
) << i
;
348 ASSERT_EQ(body
.data()[i
], buffer1
[0]) << i
;
349 ASSERT_EQ(body
.data()[i
+ 1], buffer2
[0]) << i
;
353 TEST_P(QuicDataStreamTest
, StreamFlowControlBlocked
) {
354 // Tests that we send a BLOCKED frame to the peer when we attempt to write,
355 // but are flow control blocked.
356 Initialize(kShouldProcessData
);
358 // Set a small flow control limit.
359 const uint64 kWindow
= 36;
360 QuicFlowControllerPeer::SetSendWindowOffset(stream_
->flow_controller(),
362 EXPECT_EQ(kWindow
, QuicFlowControllerPeer::SendWindowOffset(
363 stream_
->flow_controller()));
365 // Try to send more data than the flow control limit allows.
367 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
369 const uint64 kOverflow
= 15;
370 GenerateBody(&body
, kWindow
+ kOverflow
);
372 EXPECT_CALL(*connection_
, SendBlocked(kClientDataStreamId1
));
373 EXPECT_CALL(*session_
, WritevData(kClientDataStreamId1
, _
, _
, _
, _
, _
))
374 .WillOnce(Return(QuicConsumedData(kWindow
, true)));
375 stream_
->WriteOrBufferData(body
, false, nullptr);
377 // Should have sent as much as possible, resulting in no send window left.
379 QuicFlowControllerPeer::SendWindowSize(stream_
->flow_controller()));
381 // And we should have queued the overflowed data.
383 ReliableQuicStreamPeer::SizeOfQueuedData(stream_
.get()));
386 TEST_P(QuicDataStreamTest
, StreamFlowControlNoWindowUpdateIfNotConsumed
) {
387 // The flow control receive window decreases whenever we add new bytes to the
388 // sequencer, whether they are consumed immediately or buffered. However we
389 // only send WINDOW_UPDATE frames based on increasing number of bytes
392 // Don't process data - it will be buffered instead.
393 Initialize(!kShouldProcessData
);
395 // Expect no WINDOW_UPDATE frames to be sent.
396 EXPECT_CALL(*connection_
, SendWindowUpdate(_
, _
)).Times(0);
398 // Set a small flow control receive window.
399 const uint64 kWindow
= 36;
400 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_
->flow_controller(),
402 QuicFlowControllerPeer::SetMaxReceiveWindow(stream_
->flow_controller(),
404 EXPECT_EQ(kWindow
, QuicFlowControllerPeer::ReceiveWindowOffset(
405 stream_
->flow_controller()));
407 // Stream receives enough data to fill a fraction of the receive window.
409 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
411 GenerateBody(&body
, kWindow
/ 3);
412 stream_
->OnStreamHeaders(headers
);
413 stream_
->OnStreamHeadersComplete(false, headers
.size());
415 QuicStreamFrame
frame1(kClientDataStreamId1
, false, 0, StringPiece(body
));
416 stream_
->OnStreamFrame(frame1
);
417 EXPECT_EQ(kWindow
- (kWindow
/ 3), QuicFlowControllerPeer::ReceiveWindowSize(
418 stream_
->flow_controller()));
420 // Now receive another frame which results in the receive window being over
421 // half full. This should all be buffered, decreasing the receive window but
422 // not sending WINDOW_UPDATE.
423 QuicStreamFrame
frame2(kClientDataStreamId1
, false, kWindow
/ 3,
425 stream_
->OnStreamFrame(frame2
);
427 kWindow
- (2 * kWindow
/ 3),
428 QuicFlowControllerPeer::ReceiveWindowSize(stream_
->flow_controller()));
431 TEST_P(QuicDataStreamTest
, StreamFlowControlWindowUpdate
) {
432 // Tests that on receipt of data, the stream updates its receive window offset
433 // appropriately, and sends WINDOW_UPDATE frames when its receive window drops
435 Initialize(kShouldProcessData
);
437 // Set a small flow control limit.
438 const uint64 kWindow
= 36;
439 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_
->flow_controller(),
441 QuicFlowControllerPeer::SetMaxReceiveWindow(stream_
->flow_controller(),
443 EXPECT_EQ(kWindow
, QuicFlowControllerPeer::ReceiveWindowOffset(
444 stream_
->flow_controller()));
446 // Stream receives enough data to fill a fraction of the receive window.
448 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
450 GenerateBody(&body
, kWindow
/ 3);
451 stream_
->OnStreamHeaders(headers
);
452 stream_
->OnStreamHeadersComplete(false, headers
.size());
453 stream_
->MarkHeadersConsumed(headers
.length());
455 QuicStreamFrame
frame1(kClientDataStreamId1
, false, 0, StringPiece(body
));
456 stream_
->OnStreamFrame(frame1
);
457 EXPECT_EQ(kWindow
- (kWindow
/ 3), QuicFlowControllerPeer::ReceiveWindowSize(
458 stream_
->flow_controller()));
460 // Now receive another frame which results in the receive window being over
461 // half full. This will trigger the stream to increase its receive window
462 // offset and send a WINDOW_UPDATE. The result will be again an available
463 // window of kWindow bytes.
464 QuicStreamFrame
frame2(kClientDataStreamId1
, false, kWindow
/ 3,
466 EXPECT_CALL(*connection_
,
467 SendWindowUpdate(kClientDataStreamId1
,
468 QuicFlowControllerPeer::ReceiveWindowOffset(
469 stream_
->flow_controller()) +
471 stream_
->OnStreamFrame(frame2
);
472 EXPECT_EQ(kWindow
, QuicFlowControllerPeer::ReceiveWindowSize(
473 stream_
->flow_controller()));
476 TEST_P(QuicDataStreamTest
, ConnectionFlowControlWindowUpdate
) {
477 // Tests that on receipt of data, the connection updates its receive window
478 // offset appropriately, and sends WINDOW_UPDATE frames when its receive
479 // window drops too low.
480 Initialize(kShouldProcessData
);
482 // Set a small flow control limit for streams and connection.
483 const uint64 kWindow
= 36;
484 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_
->flow_controller(),
486 QuicFlowControllerPeer::SetMaxReceiveWindow(stream_
->flow_controller(),
488 QuicFlowControllerPeer::SetReceiveWindowOffset(stream2_
->flow_controller(),
490 QuicFlowControllerPeer::SetMaxReceiveWindow(stream2_
->flow_controller(),
492 QuicFlowControllerPeer::SetReceiveWindowOffset(session_
->flow_controller(),
494 QuicFlowControllerPeer::SetMaxReceiveWindow(session_
->flow_controller(),
497 // Supply headers to both streams so that they are happy to receive data.
499 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
500 stream_
->OnStreamHeaders(headers
);
501 stream_
->OnStreamHeadersComplete(false, headers
.size());
502 stream_
->MarkHeadersConsumed(headers
.length());
503 stream2_
->OnStreamHeaders(headers
);
504 stream2_
->OnStreamHeadersComplete(false, headers
.size());
505 stream2_
->MarkHeadersConsumed(headers
.length());
507 // Each stream gets a quarter window of data. This should not trigger a
508 // WINDOW_UPDATE for either stream, nor for the connection.
510 GenerateBody(&body
, kWindow
/ 4);
511 QuicStreamFrame
frame1(kClientDataStreamId1
, false, 0, StringPiece(body
));
512 stream_
->OnStreamFrame(frame1
);
513 QuicStreamFrame
frame2(kClientDataStreamId2
, false, 0, StringPiece(body
));
514 stream2_
->OnStreamFrame(frame2
);
516 // Now receive a further single byte on one stream - again this does not
517 // trigger a stream WINDOW_UPDATE, but now the connection flow control window
518 // is over half full and thus a connection WINDOW_UPDATE is sent.
519 EXPECT_CALL(*connection_
, SendWindowUpdate(kClientDataStreamId1
, _
)).Times(0);
520 EXPECT_CALL(*connection_
, SendWindowUpdate(kClientDataStreamId2
, _
)).Times(0);
521 EXPECT_CALL(*connection_
,
522 SendWindowUpdate(0, QuicFlowControllerPeer::ReceiveWindowOffset(
523 session_
->flow_controller()) +
525 QuicStreamFrame
frame3(kClientDataStreamId1
, false, (kWindow
/ 4),
527 stream_
->OnStreamFrame(frame3
);
530 TEST_P(QuicDataStreamTest
, StreamFlowControlViolation
) {
531 // Tests that on if the peer sends too much data (i.e. violates the flow
532 // control protocol), then we terminate the connection.
534 // Stream should not process data, so that data gets buffered in the
535 // sequencer, triggering flow control limits.
536 Initialize(!kShouldProcessData
);
538 // Set a small flow control limit.
539 const uint64 kWindow
= 50;
540 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_
->flow_controller(),
544 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
545 stream_
->OnStreamHeaders(headers
);
546 stream_
->OnStreamHeadersComplete(false, headers
.size());
548 // Receive data to overflow the window, violating flow control.
550 GenerateBody(&body
, kWindow
+ 1);
551 QuicStreamFrame
frame(kClientDataStreamId1
, false, 0, StringPiece(body
));
552 EXPECT_CALL(*connection_
,
553 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
));
554 stream_
->OnStreamFrame(frame
);
557 TEST_P(QuicDataStreamTest
, ConnectionFlowControlViolation
) {
558 // Tests that on if the peer sends too much data (i.e. violates the flow
559 // control protocol), at the connection level (rather than the stream level)
560 // then we terminate the connection.
562 // Stream should not process data, so that data gets buffered in the
563 // sequencer, triggering flow control limits.
564 Initialize(!kShouldProcessData
);
566 // Set a small flow control window on streams, and connection.
567 const uint64 kStreamWindow
= 50;
568 const uint64 kConnectionWindow
= 10;
569 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_
->flow_controller(),
571 QuicFlowControllerPeer::SetReceiveWindowOffset(session_
->flow_controller(),
575 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
576 stream_
->OnStreamHeaders(headers
);
577 stream_
->OnStreamHeadersComplete(false, headers
.size());
579 // Send enough data to overflow the connection level flow control window.
581 GenerateBody(&body
, kConnectionWindow
+ 1);
582 EXPECT_LT(body
.size(), kStreamWindow
);
583 QuicStreamFrame
frame(kClientDataStreamId1
, false, 0, StringPiece(body
));
585 EXPECT_CALL(*connection_
,
586 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
));
587 stream_
->OnStreamFrame(frame
);
590 TEST_P(QuicDataStreamTest
, StreamFlowControlFinNotBlocked
) {
591 // An attempt to write a FIN with no data should not be flow control blocked,
592 // even if the send window is 0.
594 Initialize(kShouldProcessData
);
596 // Set a flow control limit of zero.
597 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_
->flow_controller(), 0);
598 EXPECT_EQ(0u, QuicFlowControllerPeer::ReceiveWindowOffset(
599 stream_
->flow_controller()));
601 // Send a frame with a FIN but no data. This should not be blocked.
605 EXPECT_CALL(*connection_
, SendBlocked(kClientDataStreamId1
)).Times(0);
606 EXPECT_CALL(*session_
, WritevData(kClientDataStreamId1
, _
, _
, _
, _
, _
))
607 .WillOnce(Return(QuicConsumedData(0, fin
)));
609 stream_
->WriteOrBufferData(body
, fin
, nullptr);