blob: 13074ad86fd8a911734c81706a355c987c5681e5 [file] [log] [blame]
[email protected]457d6952013-12-13 09:24:581// 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.
4
5#include "net/quic/quic_data_stream.h"
6
7#include "net/quic/quic_ack_notifier.h"
8#include "net/quic/quic_connection.h"
[email protected]9bb57c72014-03-31 20:36:049#include "net/quic/quic_flags.h"
[email protected]457d6952013-12-13 09:24:5810#include "net/quic/quic_utils.h"
[email protected]9f0dcd4e2014-01-16 15:58:1411#include "net/quic/quic_write_blocked_list.h"
[email protected]457d6952013-12-13 09:24:5812#include "net/quic/spdy_utils.h"
[email protected]51cc1342014-04-18 23:44:3713#include "net/quic/test_tools/quic_flow_controller_peer.h"
[email protected]457d6952013-12-13 09:24:5814#include "net/quic/test_tools/quic_session_peer.h"
15#include "net/quic/test_tools/quic_test_utils.h"
[email protected]9bb57c72014-03-31 20:36:0416#include "net/quic/test_tools/reliable_quic_stream_peer.h"
17#include "net/test/gtest_util.h"
[email protected]457d6952013-12-13 09:24:5818#include "testing/gmock/include/gmock/gmock.h"
19
20using base::StringPiece;
21using std::min;
[email protected]9bb57c72014-03-31 20:36:0422using testing::AnyNumber;
[email protected]457d6952013-12-13 09:24:5823using testing::InSequence;
[email protected]9bb57c72014-03-31 20:36:0424using testing::Return;
[email protected]457d6952013-12-13 09:24:5825using testing::SaveArg;
[email protected]457d6952013-12-13 09:24:5826using testing::StrictMock;
[email protected]fddd8c82014-05-22 19:10:1827using testing::_;
[email protected]457d6952013-12-13 09:24:5828
29namespace net {
30namespace test {
31namespace {
32
[email protected]457d6952013-12-13 09:24:5833const bool kIsServer = true;
34const bool kShouldProcessData = true;
35
36class TestStream : public QuicDataStream {
37 public:
38 TestStream(QuicStreamId id,
39 QuicSession* session,
40 bool should_process_data)
41 : QuicDataStream(id, session),
42 should_process_data_(should_process_data) {}
43
44 virtual uint32 ProcessData(const char* data, uint32 data_len) OVERRIDE {
45 EXPECT_NE(0u, data_len);
46 DVLOG(1) << "ProcessData data_len: " << data_len;
47 data_ += string(data, data_len);
48 return should_process_data_ ? data_len : 0;
49 }
50
51 using ReliableQuicStream::WriteOrBufferData;
52 using ReliableQuicStream::CloseReadSide;
53 using ReliableQuicStream::CloseWriteSide;
54
55 const string& data() const { return data_; }
56
57 private:
58 bool should_process_data_;
59 string data_;
60};
61
[email protected]4d640792013-12-18 22:21:0862class QuicDataStreamTest : public ::testing::TestWithParam<QuicVersion> {
[email protected]457d6952013-12-13 09:24:5863 public:
64 QuicDataStreamTest() {
65 headers_[":host"] = "www.google.com";
66 headers_[":path"] = "/index.hml";
67 headers_[":scheme"] = "https";
68 headers_["cookie"] =
69 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
70 "__utmc=160408618; "
71 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
72 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
73 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
74 "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
75 "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
76 "1zFMi5vzcns38-8_Sns; "
77 "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
78 "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
79 "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
80 "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
81 "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
82 "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
83 "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
84 "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
85 "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
86 "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
87 "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
88 "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
89 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
90 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
91 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
92 }
93
94 void Initialize(bool stream_should_process_data) {
[email protected]4d640792013-12-18 22:21:0895 connection_ = new testing::StrictMock<MockConnection>(
96 kIsServer, SupportedVersions(GetParam()));
97 session_.reset(new testing::StrictMock<MockSession>(connection_));
[email protected]66ae5962014-05-22 11:13:0598 stream_.reset(new TestStream(kClientDataStreamId1, session_.get(),
[email protected]457d6952013-12-13 09:24:5899 stream_should_process_data));
[email protected]66ae5962014-05-22 11:13:05100 stream2_.reset(new TestStream(kClientDataStreamId2, session_.get(),
101 stream_should_process_data));
[email protected]457d6952013-12-13 09:24:58102 write_blocked_list_ =
[email protected]aa7e4ef2014-05-28 03:53:15103 QuicSessionPeer::GetWriteBlockedStreams(session_.get());
[email protected]457d6952013-12-13 09:24:58104 }
105
106 protected:
107 MockConnection* connection_;
108 scoped_ptr<MockSession> session_;
109 scoped_ptr<TestStream> stream_;
110 scoped_ptr<TestStream> stream2_;
[email protected]457d6952013-12-13 09:24:58111 SpdyHeaderBlock headers_;
[email protected]9f0dcd4e2014-01-16 15:58:14112 QuicWriteBlockedList* write_blocked_list_;
[email protected]457d6952013-12-13 09:24:58113};
114
[email protected]4d640792013-12-18 22:21:08115INSTANTIATE_TEST_CASE_P(Tests, QuicDataStreamTest,
116 ::testing::ValuesIn(QuicSupportedVersions()));
117
118TEST_P(QuicDataStreamTest, ProcessHeaders) {
[email protected]457d6952013-12-13 09:24:58119 Initialize(kShouldProcessData);
120
[email protected]4d640792013-12-18 22:21:08121 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
[email protected]92bf17c2014-03-03 21:14:03122 stream_->OnStreamHeadersPriority(QuicUtils::HighestPriority());
123 stream_->OnStreamHeaders(headers);
124 EXPECT_EQ(headers, stream_->data());
125 stream_->OnStreamHeadersComplete(false, headers.size());
[email protected]c05a6d222013-12-16 19:42:03126 EXPECT_EQ(QuicUtils::HighestPriority(), stream_->EffectivePriority());
[email protected]4d640792013-12-18 22:21:08127 EXPECT_EQ(headers, stream_->data());
128 EXPECT_FALSE(stream_->IsDoneReading());
[email protected]457d6952013-12-13 09:24:58129}
130
[email protected]4d640792013-12-18 22:21:08131TEST_P(QuicDataStreamTest, ProcessHeadersAndBody) {
[email protected]457d6952013-12-13 09:24:58132 Initialize(kShouldProcessData);
133
[email protected]4d640792013-12-18 22:21:08134 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
[email protected]457d6952013-12-13 09:24:58135 string body = "this is the body";
[email protected]457d6952013-12-13 09:24:58136
[email protected]92bf17c2014-03-03 21:14:03137 stream_->OnStreamHeaders(headers);
138 EXPECT_EQ(headers, stream_->data());
139 stream_->OnStreamHeadersComplete(false, headers.size());
[email protected]66ae5962014-05-22 11:13:05140 QuicStreamFrame frame(kClientDataStreamId1, false, 0, MakeIOVector(body));
[email protected]92bf17c2014-03-03 21:14:03141 stream_->OnStreamFrame(frame);
[email protected]4d640792013-12-18 22:21:08142
143 EXPECT_EQ(headers + body, stream_->data());
[email protected]457d6952013-12-13 09:24:58144}
145
[email protected]4d640792013-12-18 22:21:08146TEST_P(QuicDataStreamTest, ProcessHeadersAndBodyFragments) {
147 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
[email protected]457d6952013-12-13 09:24:58148 string body = "this is the body";
[email protected]457d6952013-12-13 09:24:58149
[email protected]92bf17c2014-03-03 21:14:03150 for (size_t fragment_size = 1; fragment_size < body.size();
151 ++fragment_size) {
[email protected]457d6952013-12-13 09:24:58152 Initialize(kShouldProcessData);
[email protected]92bf17c2014-03-03 21:14:03153 for (size_t offset = 0; offset < headers.size();
154 offset += fragment_size) {
155 size_t remaining_data = headers.size() - offset;
156 StringPiece fragment(headers.data() + offset,
157 min(fragment_size, remaining_data));
158 stream_->OnStreamHeaders(fragment);
[email protected]4d640792013-12-18 22:21:08159 }
[email protected]92bf17c2014-03-03 21:14:03160 stream_->OnStreamHeadersComplete(false, headers.size());
161 for (size_t offset = 0; offset < body.size(); offset += fragment_size) {
162 size_t remaining_data = body.size() - offset;
163 StringPiece fragment(body.data() + offset,
164 min(fragment_size, remaining_data));
[email protected]66ae5962014-05-22 11:13:05165 QuicStreamFrame frame(kClientDataStreamId1, false, offset,
166 MakeIOVector(fragment));
[email protected]92bf17c2014-03-03 21:14:03167 stream_->OnStreamFrame(frame);
168 }
169 ASSERT_EQ(headers + body,
170 stream_->data()) << "fragment_size: " << fragment_size;
[email protected]457d6952013-12-13 09:24:58171 }
[email protected]457d6952013-12-13 09:24:58172}
173
[email protected]4d640792013-12-18 22:21:08174TEST_P(QuicDataStreamTest, ProcessHeadersAndBodyFragmentsSplit) {
175 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
176 string body = "this is the body";
177
[email protected]92bf17c2014-03-03 21:14:03178 for (size_t split_point = 1; split_point < body.size() - 1; ++split_point) {
[email protected]4d640792013-12-18 22:21:08179 Initialize(kShouldProcessData);
[email protected]92bf17c2014-03-03 21:14:03180 StringPiece headers1(headers.data(), split_point);
181 stream_->OnStreamHeaders(headers1);
[email protected]4d640792013-12-18 22:21:08182
[email protected]92bf17c2014-03-03 21:14:03183 StringPiece headers2(headers.data() + split_point,
184 headers.size() - split_point);
185 stream_->OnStreamHeaders(headers2);
186 stream_->OnStreamHeadersComplete(false, headers.size());
[email protected]4d640792013-12-18 22:21:08187
[email protected]92bf17c2014-03-03 21:14:03188 StringPiece fragment1(body.data(), split_point);
[email protected]66ae5962014-05-22 11:13:05189 QuicStreamFrame frame1(kClientDataStreamId1, false, 0,
190 MakeIOVector(fragment1));
[email protected]92bf17c2014-03-03 21:14:03191 stream_->OnStreamFrame(frame1);
[email protected]4d640792013-12-18 22:21:08192
[email protected]92bf17c2014-03-03 21:14:03193 StringPiece fragment2(body.data() + split_point,
194 body.size() - split_point);
[email protected]66ae5962014-05-22 11:13:05195 QuicStreamFrame frame2(kClientDataStreamId1, false, split_point,
196 MakeIOVector(fragment2));
[email protected]92bf17c2014-03-03 21:14:03197 stream_->OnStreamFrame(frame2);
[email protected]4d640792013-12-18 22:21:08198
[email protected]92bf17c2014-03-03 21:14:03199 ASSERT_EQ(headers + body,
200 stream_->data()) << "split_point: " << split_point;
[email protected]4d640792013-12-18 22:21:08201 }
202}
203
204TEST_P(QuicDataStreamTest, ProcessHeadersAndBodyReadv) {
[email protected]457d6952013-12-13 09:24:58205 Initialize(!kShouldProcessData);
206
[email protected]4d640792013-12-18 22:21:08207 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
[email protected]457d6952013-12-13 09:24:58208 string body = "this is the body";
[email protected]457d6952013-12-13 09:24:58209
[email protected]92bf17c2014-03-03 21:14:03210 stream_->OnStreamHeaders(headers);
211 EXPECT_EQ(headers, stream_->data());
212 stream_->OnStreamHeadersComplete(false, headers.size());
[email protected]66ae5962014-05-22 11:13:05213 QuicStreamFrame frame(kClientDataStreamId1, false, 0, MakeIOVector(body));
[email protected]92bf17c2014-03-03 21:14:03214 stream_->OnStreamFrame(frame);
[email protected]457d6952013-12-13 09:24:58215
216 char buffer[2048];
[email protected]4d640792013-12-18 22:21:08217 ASSERT_LT(headers.length() + body.length(), arraysize(buffer));
[email protected]457d6952013-12-13 09:24:58218 struct iovec vec;
219 vec.iov_base = buffer;
220 vec.iov_len = arraysize(buffer);
221
222 size_t bytes_read = stream_->Readv(&vec, 1);
[email protected]4d640792013-12-18 22:21:08223 EXPECT_EQ(headers.length(), bytes_read);
224 EXPECT_EQ(headers, string(buffer, bytes_read));
[email protected]457d6952013-12-13 09:24:58225
226 bytes_read = stream_->Readv(&vec, 1);
227 EXPECT_EQ(body.length(), bytes_read);
228 EXPECT_EQ(body, string(buffer, bytes_read));
229}
230
[email protected]4d640792013-12-18 22:21:08231TEST_P(QuicDataStreamTest, ProcessHeadersAndBodyIncrementalReadv) {
[email protected]457d6952013-12-13 09:24:58232 Initialize(!kShouldProcessData);
233
[email protected]4d640792013-12-18 22:21:08234 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
[email protected]457d6952013-12-13 09:24:58235 string body = "this is the body";
[email protected]92bf17c2014-03-03 21:14:03236 stream_->OnStreamHeaders(headers);
237 EXPECT_EQ(headers, stream_->data());
238 stream_->OnStreamHeadersComplete(false, headers.size());
[email protected]66ae5962014-05-22 11:13:05239 QuicStreamFrame frame(kClientDataStreamId1, false, 0, MakeIOVector(body));
[email protected]92bf17c2014-03-03 21:14:03240 stream_->OnStreamFrame(frame);
241
[email protected]457d6952013-12-13 09:24:58242 char buffer[1];
243 struct iovec vec;
244 vec.iov_base = buffer;
245 vec.iov_len = arraysize(buffer);
[email protected]4d640792013-12-18 22:21:08246
247 string data = headers + body;
248 for (size_t i = 0; i < data.length(); ++i) {
[email protected]457d6952013-12-13 09:24:58249 size_t bytes_read = stream_->Readv(&vec, 1);
250 ASSERT_EQ(1u, bytes_read);
[email protected]4d640792013-12-18 22:21:08251 EXPECT_EQ(data.data()[i], buffer[0]);
[email protected]457d6952013-12-13 09:24:58252 }
253}
254
[email protected]4d640792013-12-18 22:21:08255TEST_P(QuicDataStreamTest, ProcessHeadersUsingReadvWithMultipleIovecs) {
[email protected]457d6952013-12-13 09:24:58256 Initialize(!kShouldProcessData);
257
[email protected]4d640792013-12-18 22:21:08258 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
[email protected]457d6952013-12-13 09:24:58259 string body = "this is the body";
[email protected]92bf17c2014-03-03 21:14:03260 stream_->OnStreamHeaders(headers);
261 EXPECT_EQ(headers, stream_->data());
262 stream_->OnStreamHeadersComplete(false, headers.size());
[email protected]66ae5962014-05-22 11:13:05263 QuicStreamFrame frame(kClientDataStreamId1, false, 0, MakeIOVector(body));
[email protected]92bf17c2014-03-03 21:14:03264 stream_->OnStreamFrame(frame);
265
[email protected]457d6952013-12-13 09:24:58266 char buffer1[1];
267 char buffer2[1];
268 struct iovec vec[2];
269 vec[0].iov_base = buffer1;
270 vec[0].iov_len = arraysize(buffer1);
271 vec[1].iov_base = buffer2;
272 vec[1].iov_len = arraysize(buffer2);
[email protected]4d640792013-12-18 22:21:08273 string data = headers + body;
274 for (size_t i = 0; i < data.length(); i += 2) {
[email protected]457d6952013-12-13 09:24:58275 size_t bytes_read = stream_->Readv(vec, 2);
276 ASSERT_EQ(2u, bytes_read) << i;
[email protected]4d640792013-12-18 22:21:08277 ASSERT_EQ(data.data()[i], buffer1[0]) << i;
278 ASSERT_EQ(data.data()[i + 1], buffer2[0]) << i;
[email protected]457d6952013-12-13 09:24:58279 }
280}
281
[email protected]9bb57c72014-03-31 20:36:04282TEST_P(QuicDataStreamTest, StreamFlowControlBlocked) {
283 // Tests that we send a BLOCKED frame to the peer when we attempt to write,
284 // but are flow control blocked.
285 if (GetParam() < QUIC_VERSION_17) {
286 return;
287 }
[email protected]08da9adb2014-04-24 08:33:31288 ValueRestore<bool> old_flag(&FLAGS_enable_quic_stream_flow_control_2, true);
[email protected]9bb57c72014-03-31 20:36:04289
290 Initialize(kShouldProcessData);
291
292 // Set a small flow control limit.
293 const uint64 kWindow = 36;
[email protected]82168ee22014-04-30 22:25:48294 QuicFlowControllerPeer::SetSendWindowOffset(stream_->flow_controller(),
[email protected]51cc1342014-04-18 23:44:37295 kWindow);
296 EXPECT_EQ(kWindow, QuicFlowControllerPeer::SendWindowOffset(
[email protected]82168ee22014-04-30 22:25:48297 stream_->flow_controller()));
[email protected]9bb57c72014-03-31 20:36:04298
299 // Try to send more data than the flow control limit allows.
300 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
301 string body;
302 const uint64 kOverflow = 15;
303 GenerateBody(&body, kWindow + kOverflow);
304
[email protected]66ae5962014-05-22 11:13:05305 EXPECT_CALL(*connection_, SendBlocked(kClientDataStreamId1));
[email protected]bbb10072014-06-13 07:41:59306 EXPECT_CALL(*session_, WritevData(kClientDataStreamId1, _, _, _, _, _))
[email protected]66ae5962014-05-22 11:13:05307 .WillOnce(Return(QuicConsumedData(kWindow, true)));
[email protected]9bb57c72014-03-31 20:36:04308 stream_->WriteOrBufferData(body, false, NULL);
309
310 // Should have sent as much as possible, resulting in no send window left.
[email protected]82168ee22014-04-30 22:25:48311 EXPECT_EQ(0u,
312 QuicFlowControllerPeer::SendWindowSize(stream_->flow_controller()));
[email protected]9bb57c72014-03-31 20:36:04313
314 // And we should have queued the overflowed data.
315 EXPECT_EQ(kOverflow,
316 ReliableQuicStreamPeer::SizeOfQueuedData(stream_.get()));
317}
318
319TEST_P(QuicDataStreamTest, StreamFlowControlNoWindowUpdateIfNotConsumed) {
320 // The flow control receive window decreases whenever we add new bytes to the
321 // sequencer, whether they are consumed immediately or buffered. However we
322 // only send WINDOW_UPDATE frames based on increasing number of bytes
323 // consumed.
324 if (GetParam() < QUIC_VERSION_17) {
325 return;
326 }
[email protected]08da9adb2014-04-24 08:33:31327 ValueRestore<bool> old_flag(&FLAGS_enable_quic_stream_flow_control_2, true);
[email protected]9bb57c72014-03-31 20:36:04328
329 // Don't process data - it will be buffered instead.
330 Initialize(!kShouldProcessData);
331
332 // Expect no WINDOW_UPDATE frames to be sent.
333 EXPECT_CALL(*connection_, SendWindowUpdate(_, _)).Times(0);
334
[email protected]51cc1342014-04-18 23:44:37335 // Set a small flow control receive window.
[email protected]9bb57c72014-03-31 20:36:04336 const uint64 kWindow = 36;
[email protected]82168ee22014-04-30 22:25:48337 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
338 kWindow);
339 QuicFlowControllerPeer::SetMaxReceiveWindow(stream_->flow_controller(),
[email protected]51cc1342014-04-18 23:44:37340 kWindow);
341 EXPECT_EQ(kWindow, QuicFlowControllerPeer::ReceiveWindowOffset(
[email protected]82168ee22014-04-30 22:25:48342 stream_->flow_controller()));
[email protected]9bb57c72014-03-31 20:36:04343
344 // Stream receives enough data to fill a fraction of the receive window.
345 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
346 string body;
347 GenerateBody(&body, kWindow / 3);
348 stream_->OnStreamHeaders(headers);
349 EXPECT_EQ(headers, stream_->data());
350 stream_->OnStreamHeadersComplete(false, headers.size());
351
[email protected]66ae5962014-05-22 11:13:05352 QuicStreamFrame frame1(kClientDataStreamId1, false, 0, MakeIOVector(body));
[email protected]9bb57c72014-03-31 20:36:04353 stream_->OnStreamFrame(frame1);
[email protected]51cc1342014-04-18 23:44:37354 EXPECT_EQ(kWindow - (kWindow / 3), QuicFlowControllerPeer::ReceiveWindowSize(
[email protected]82168ee22014-04-30 22:25:48355 stream_->flow_controller()));
[email protected]9bb57c72014-03-31 20:36:04356
357 // Now receive another frame which results in the receive window being over
358 // half full. This should all be buffered, decreasing the receive window but
359 // not sending WINDOW_UPDATE.
[email protected]66ae5962014-05-22 11:13:05360 QuicStreamFrame frame2(kClientDataStreamId1, false, kWindow / 3,
361 MakeIOVector(body));
[email protected]9bb57c72014-03-31 20:36:04362 stream_->OnStreamFrame(frame2);
[email protected]82168ee22014-04-30 22:25:48363 EXPECT_EQ(
364 kWindow - (2 * kWindow / 3),
365 QuicFlowControllerPeer::ReceiveWindowSize(stream_->flow_controller()));
[email protected]9bb57c72014-03-31 20:36:04366}
367
368TEST_P(QuicDataStreamTest, StreamFlowControlWindowUpdate) {
369 // Tests that on receipt of data, the stream updates its receive window offset
370 // appropriately, and sends WINDOW_UPDATE frames when its receive window drops
371 // too low.
372 if (GetParam() < QUIC_VERSION_17) {
373 return;
374 }
[email protected]08da9adb2014-04-24 08:33:31375 ValueRestore<bool> old_flag(&FLAGS_enable_quic_stream_flow_control_2, true);
[email protected]9bb57c72014-03-31 20:36:04376
377 Initialize(kShouldProcessData);
378
379 // Set a small flow control limit.
380 const uint64 kWindow = 36;
[email protected]82168ee22014-04-30 22:25:48381 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
382 kWindow);
383 QuicFlowControllerPeer::SetMaxReceiveWindow(stream_->flow_controller(),
[email protected]51cc1342014-04-18 23:44:37384 kWindow);
385 EXPECT_EQ(kWindow, QuicFlowControllerPeer::ReceiveWindowOffset(
[email protected]82168ee22014-04-30 22:25:48386 stream_->flow_controller()));
[email protected]9bb57c72014-03-31 20:36:04387
388 // Stream receives enough data to fill a fraction of the receive window.
389 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
390 string body;
391 GenerateBody(&body, kWindow / 3);
392 stream_->OnStreamHeaders(headers);
393 EXPECT_EQ(headers, stream_->data());
394 stream_->OnStreamHeadersComplete(false, headers.size());
395
[email protected]66ae5962014-05-22 11:13:05396 QuicStreamFrame frame1(kClientDataStreamId1, false, 0, MakeIOVector(body));
[email protected]9bb57c72014-03-31 20:36:04397 stream_->OnStreamFrame(frame1);
[email protected]51cc1342014-04-18 23:44:37398 EXPECT_EQ(kWindow - (kWindow / 3), QuicFlowControllerPeer::ReceiveWindowSize(
[email protected]82168ee22014-04-30 22:25:48399 stream_->flow_controller()));
[email protected]9bb57c72014-03-31 20:36:04400
401 // Now receive another frame which results in the receive window being over
402 // half full. This will trigger the stream to increase its receive window
403 // offset and send a WINDOW_UPDATE. The result will be again an available
404 // window of kWindow bytes.
[email protected]66ae5962014-05-22 11:13:05405 QuicStreamFrame frame2(kClientDataStreamId1, false, kWindow / 3,
406 MakeIOVector(body));
407 EXPECT_CALL(*connection_,
408 SendWindowUpdate(kClientDataStreamId1,
409 QuicFlowControllerPeer::ReceiveWindowOffset(
410 stream_->flow_controller()) +
411 2 * kWindow / 3));
[email protected]9bb57c72014-03-31 20:36:04412 stream_->OnStreamFrame(frame2);
[email protected]51cc1342014-04-18 23:44:37413 EXPECT_EQ(kWindow, QuicFlowControllerPeer::ReceiveWindowSize(
[email protected]82168ee22014-04-30 22:25:48414 stream_->flow_controller()));
[email protected]9bb57c72014-03-31 20:36:04415}
416
[email protected]77b5d50b2014-05-07 22:48:48417TEST_P(QuicDataStreamTest, ConnectionFlowControlWindowUpdate) {
418 // Tests that on receipt of data, the connection updates its receive window
419 // offset appropriately, and sends WINDOW_UPDATE frames when its receive
420 // window drops too low.
421 if (GetParam() < QUIC_VERSION_19) {
422 return;
423 }
424 ValueRestore<bool> old_flag2(&FLAGS_enable_quic_stream_flow_control_2, true);
425 ValueRestore<bool> old_flag(&FLAGS_enable_quic_connection_flow_control, true);
426
427 Initialize(kShouldProcessData);
428
429 // Set a small flow control limit for streams and connection.
430 const uint64 kWindow = 36;
431 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
432 kWindow);
433 QuicFlowControllerPeer::SetMaxReceiveWindow(stream_->flow_controller(),
434 kWindow);
435 QuicFlowControllerPeer::SetReceiveWindowOffset(stream2_->flow_controller(),
436 kWindow);
437 QuicFlowControllerPeer::SetMaxReceiveWindow(stream2_->flow_controller(),
438 kWindow);
[email protected]ce7bb1412014-05-17 15:51:33439 QuicFlowControllerPeer::SetReceiveWindowOffset(session_->flow_controller(),
[email protected]77b5d50b2014-05-07 22:48:48440 kWindow);
[email protected]ce7bb1412014-05-17 15:51:33441 QuicFlowControllerPeer::SetMaxReceiveWindow(session_->flow_controller(),
[email protected]77b5d50b2014-05-07 22:48:48442 kWindow);
443
444 // Supply headers to both streams so that they are happy to receive data.
445 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
446 stream_->OnStreamHeaders(headers);
447 stream_->OnStreamHeadersComplete(false, headers.size());
448 stream2_->OnStreamHeaders(headers);
449 stream2_->OnStreamHeadersComplete(false, headers.size());
450
451 // Each stream gets a quarter window of data. This should not trigger a
452 // WINDOW_UPDATE for either stream, nor for the connection.
453 string body;
454 GenerateBody(&body, kWindow / 4);
[email protected]66ae5962014-05-22 11:13:05455 QuicStreamFrame frame1(kClientDataStreamId1, false, 0, MakeIOVector(body));
[email protected]77b5d50b2014-05-07 22:48:48456 stream_->OnStreamFrame(frame1);
[email protected]66ae5962014-05-22 11:13:05457 QuicStreamFrame frame2(kClientDataStreamId2, false, 0, MakeIOVector(body));
[email protected]77b5d50b2014-05-07 22:48:48458 stream2_->OnStreamFrame(frame2);
459
460 // Now receive a further single byte on one stream - again this does not
461 // trigger a stream WINDOW_UPDATE, but now the connection flow control window
462 // is over half full and thus a connection WINDOW_UPDATE is sent.
[email protected]66ae5962014-05-22 11:13:05463 EXPECT_CALL(*connection_, SendWindowUpdate(kClientDataStreamId1, _)).Times(0);
464 EXPECT_CALL(*connection_, SendWindowUpdate(kClientDataStreamId2, _)).Times(0);
[email protected]77b5d50b2014-05-07 22:48:48465 EXPECT_CALL(*connection_,
466 SendWindowUpdate(0, QuicFlowControllerPeer::ReceiveWindowOffset(
[email protected]ce7bb1412014-05-17 15:51:33467 session_->flow_controller()) +
[email protected]77b5d50b2014-05-07 22:48:48468 1 + kWindow / 2));
[email protected]66ae5962014-05-22 11:13:05469 QuicStreamFrame frame3(kClientDataStreamId1, false, (kWindow / 4),
470 MakeIOVector("a"));
[email protected]77b5d50b2014-05-07 22:48:48471 stream_->OnStreamFrame(frame3);
472}
473
[email protected]9bb57c72014-03-31 20:36:04474TEST_P(QuicDataStreamTest, StreamFlowControlViolation) {
475 // Tests that on if the peer sends too much data (i.e. violates the flow
476 // control protocol), then we terminate the connection.
477 if (GetParam() < QUIC_VERSION_17) {
478 return;
479 }
[email protected]08da9adb2014-04-24 08:33:31480 ValueRestore<bool> old_flag(&FLAGS_enable_quic_stream_flow_control_2, true);
[email protected]9bb57c72014-03-31 20:36:04481
482 // Stream should not process data, so that data gets buffered in the
483 // sequencer, triggering flow control limits.
484 Initialize(!kShouldProcessData);
485
486 // Set a small flow control limit.
487 const uint64 kWindow = 50;
[email protected]82168ee22014-04-30 22:25:48488 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
489 kWindow);
[email protected]9bb57c72014-03-31 20:36:04490
491 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
[email protected]9bb57c72014-03-31 20:36:04492 stream_->OnStreamHeaders(headers);
493 EXPECT_EQ(headers, stream_->data());
494 stream_->OnStreamHeadersComplete(false, headers.size());
495
496 // Receive data to overflow the window, violating flow control.
[email protected]51cc1342014-04-18 23:44:37497 string body;
498 GenerateBody(&body, kWindow + 1);
[email protected]66ae5962014-05-22 11:13:05499 QuicStreamFrame frame(kClientDataStreamId1, false, 0, MakeIOVector(body));
[email protected]730b35d72014-06-05 03:23:22500 EXPECT_CALL(*connection_,
501 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA));
[email protected]d8c522112014-04-23 09:23:25502 stream_->OnStreamFrame(frame);
[email protected]9bb57c72014-03-31 20:36:04503}
504
[email protected]77b5d50b2014-05-07 22:48:48505TEST_P(QuicDataStreamTest, ConnectionFlowControlViolation) {
506 // Tests that on if the peer sends too much data (i.e. violates the flow
507 // control protocol), at the connection level (rather than the stream level)
508 // then we terminate the connection.
509 if (GetParam() < QUIC_VERSION_19) {
510 return;
511 }
512 ValueRestore<bool> old_flag2(&FLAGS_enable_quic_stream_flow_control_2, true);
513 ValueRestore<bool> old_flag(&FLAGS_enable_quic_connection_flow_control, true);
514
515 // Stream should not process data, so that data gets buffered in the
516 // sequencer, triggering flow control limits.
517 Initialize(!kShouldProcessData);
518
519 // Set a small flow control window on streams, and connection.
520 const uint64 kStreamWindow = 50;
521 const uint64 kConnectionWindow = 10;
522 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(),
523 kStreamWindow);
[email protected]ce7bb1412014-05-17 15:51:33524 QuicFlowControllerPeer::SetReceiveWindowOffset(session_->flow_controller(),
[email protected]77b5d50b2014-05-07 22:48:48525 kConnectionWindow);
526
527 string headers = SpdyUtils::SerializeUncompressedHeaders(headers_);
528 stream_->OnStreamHeaders(headers);
529 EXPECT_EQ(headers, stream_->data());
530 stream_->OnStreamHeadersComplete(false, headers.size());
531
532 // Send enough data to overflow the connection level flow control window.
533 string body;
534 GenerateBody(&body, kConnectionWindow + 1);
535 EXPECT_LT(body.size(), kStreamWindow);
[email protected]66ae5962014-05-22 11:13:05536 QuicStreamFrame frame(kClientDataStreamId1, false, 0, MakeIOVector(body));
[email protected]77b5d50b2014-05-07 22:48:48537
[email protected]730b35d72014-06-05 03:23:22538 EXPECT_CALL(*connection_,
539 SendConnectionClose(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA));
[email protected]77b5d50b2014-05-07 22:48:48540 stream_->OnStreamFrame(frame);
541}
542
[email protected]9bb57c72014-03-31 20:36:04543TEST_P(QuicDataStreamTest, StreamFlowControlFinNotBlocked) {
544 // An attempt to write a FIN with no data should not be flow control blocked,
545 // even if the send window is 0.
546 if (GetParam() < QUIC_VERSION_17) {
547 return;
548 }
[email protected]08da9adb2014-04-24 08:33:31549 ValueRestore<bool> old_flag(&FLAGS_enable_quic_stream_flow_control_2, true);
[email protected]9bb57c72014-03-31 20:36:04550
551 Initialize(kShouldProcessData);
552
553 // Set a flow control limit of zero.
[email protected]82168ee22014-04-30 22:25:48554 QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(), 0);
[email protected]51cc1342014-04-18 23:44:37555 EXPECT_EQ(0u, QuicFlowControllerPeer::ReceiveWindowOffset(
[email protected]82168ee22014-04-30 22:25:48556 stream_->flow_controller()));
[email protected]9bb57c72014-03-31 20:36:04557
558 // Send a frame with a FIN but no data. This should not be blocked.
559 string body = "";
560 bool fin = true;
561
[email protected]66ae5962014-05-22 11:13:05562 EXPECT_CALL(*connection_, SendBlocked(kClientDataStreamId1)).Times(0);
[email protected]bbb10072014-06-13 07:41:59563 EXPECT_CALL(*session_, WritevData(kClientDataStreamId1, _, _, _, _, _))
[email protected]66ae5962014-05-22 11:13:05564 .WillOnce(Return(QuicConsumedData(0, fin)));
[email protected]9bb57c72014-03-31 20:36:04565
566 stream_->WriteOrBufferData(body, fin, NULL);
567}
568
[email protected]457d6952013-12-13 09:24:58569} // namespace
570} // namespace test
571} // namespace net