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/quic_stream_sequencer.h"
10 #include "base/logging.h"
11 #include "base/rand_util.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/quic/quic_utils.h"
14 #include "net/quic/reliable_quic_stream.h"
15 #include "net/quic/test_tools/quic_stream_sequencer_peer.h"
16 #include "net/quic/test_tools/quic_test_utils.h"
17 #include "net/test/gtest_util.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using base::StringPiece
;
28 using testing::AnyNumber
;
29 using testing::InSequence
;
30 using testing::Return
;
36 class MockStream
: public ReliableQuicStream
{
38 MockStream(QuicSession
* session
, QuicStreamId id
)
39 : ReliableQuicStream(id
, session
) {
42 MOCK_METHOD0(OnFinRead
, void());
43 MOCK_METHOD2(ProcessRawData
, uint32(const char* data
, uint32 data_len
));
44 MOCK_METHOD2(CloseConnectionWithDetails
, void(QuicErrorCode error
,
45 const string
& details
));
46 MOCK_METHOD1(Reset
, void(QuicRstStreamErrorCode error
));
47 MOCK_METHOD0(OnCanWrite
, void());
48 QuicPriority
EffectivePriority() const override
{
49 return QuicUtils::HighestPriority();
51 virtual bool IsFlowControlEnabled() const {
58 static const char kPayload
[] =
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
61 class QuicStreamSequencerTest
: public ::testing::Test
{
63 QuicStreamSequencerTest()
64 : connection_(new MockConnection(Perspective::IS_CLIENT
)),
65 session_(connection_
),
66 stream_(&session_
, 1),
67 sequencer_(new QuicStreamSequencer(&stream_
)),
69 QuicStreamSequencerPeer::GetBufferedFrames(sequencer_
.get())) {}
71 bool VerifyIovec(const iovec
& iovec
, StringPiece expected
) {
72 if (iovec
.iov_len
!= expected
.length()) {
73 LOG(ERROR
) << "Invalid length: " << iovec
.iov_len
74 << " vs " << expected
.length();
77 if (memcmp(iovec
.iov_base
, expected
.data(), expected
.length()) != 0) {
78 LOG(ERROR
) << "Invalid data: " << static_cast<char*>(iovec
.iov_base
)
79 << " vs " << expected
.data();
85 void OnFinFrame(QuicStreamOffset byte_offset
, const char* data
) {
86 QuicStreamFrame frame
;
88 frame
.offset
= byte_offset
;
89 frame
.data
.Append(const_cast<char*>(data
), strlen(data
));
91 sequencer_
->OnStreamFrame(frame
);
94 void OnFrame(QuicStreamOffset byte_offset
, const char* data
) {
95 QuicStreamFrame frame
;
97 frame
.offset
= byte_offset
;
98 frame
.data
.Append(const_cast<char*>(data
), strlen(data
));
100 sequencer_
->OnStreamFrame(frame
);
103 MockConnection
* connection_
;
104 MockSession session_
;
105 testing::StrictMock
<MockStream
> stream_
;
106 scoped_ptr
<QuicStreamSequencer
> sequencer_
;
107 map
<QuicStreamOffset
, string
>* buffered_frames_
;
110 TEST_F(QuicStreamSequencerTest
, RejectOldFrame
) {
111 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
114 EXPECT_EQ(0u, buffered_frames_
->size());
115 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
116 // Ignore this - it matches a past sequence number and we should not see it
119 EXPECT_EQ(0u, buffered_frames_
->size());
122 TEST_F(QuicStreamSequencerTest
, RejectBufferedFrame
) {
123 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3));
126 EXPECT_EQ(1u, buffered_frames_
->size());
127 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
128 // Ignore this - it matches a buffered frame.
129 // Right now there's no checking that the payload is consistent.
131 EXPECT_EQ(1u, buffered_frames_
->size());
134 TEST_F(QuicStreamSequencerTest
, FullFrameConsumed
) {
135 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
138 EXPECT_EQ(0u, buffered_frames_
->size());
139 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
142 TEST_F(QuicStreamSequencerTest
, BlockedThenFullFrameConsumed
) {
143 sequencer_
->SetBlockedUntilFlush();
146 EXPECT_EQ(1u, buffered_frames_
->size());
147 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
149 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
150 sequencer_
->FlushBufferedFrames();
151 EXPECT_EQ(0u, buffered_frames_
->size());
152 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
154 EXPECT_CALL(stream_
, ProcessRawData(StrEq("def"), 3)).WillOnce(Return(3));
155 EXPECT_CALL(stream_
, OnFinRead());
156 OnFinFrame(3, "def");
159 TEST_F(QuicStreamSequencerTest
, BlockedThenFullFrameAndFinConsumed
) {
160 sequencer_
->SetBlockedUntilFlush();
162 OnFinFrame(0, "abc");
163 EXPECT_EQ(1u, buffered_frames_
->size());
164 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
166 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
167 EXPECT_CALL(stream_
, OnFinRead());
168 sequencer_
->FlushBufferedFrames();
169 EXPECT_EQ(0u, buffered_frames_
->size());
170 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
173 TEST_F(QuicStreamSequencerTest
, EmptyFrame
) {
175 CloseConnectionWithDetails(QUIC_INVALID_STREAM_FRAME
, _
));
177 EXPECT_EQ(0u, buffered_frames_
->size());
178 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
181 TEST_F(QuicStreamSequencerTest
, EmptyFinFrame
) {
182 EXPECT_CALL(stream_
, OnFinRead());
184 EXPECT_EQ(0u, buffered_frames_
->size());
185 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
188 TEST_F(QuicStreamSequencerTest
, PartialFrameConsumed
) {
189 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(2));
192 EXPECT_EQ(1u, buffered_frames_
->size());
193 EXPECT_EQ(2u, sequencer_
->num_bytes_consumed());
194 EXPECT_EQ("c", buffered_frames_
->find(2)->second
);
197 TEST_F(QuicStreamSequencerTest
, NextxFrameNotConsumed
) {
198 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(0));
201 EXPECT_EQ(1u, buffered_frames_
->size());
202 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
203 EXPECT_EQ("abc", buffered_frames_
->find(0)->second
);
204 EXPECT_EQ(0, sequencer_
->num_early_frames_received());
207 TEST_F(QuicStreamSequencerTest
, FutureFrameNotProcessed
) {
209 EXPECT_EQ(1u, buffered_frames_
->size());
210 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
211 EXPECT_EQ("abc", buffered_frames_
->find(3)->second
);
212 EXPECT_EQ(1, sequencer_
->num_early_frames_received());
215 TEST_F(QuicStreamSequencerTest
, OutOfOrderFrameProcessed
) {
218 EXPECT_EQ(1u, buffered_frames_
->size());
219 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
220 EXPECT_EQ(3u, sequencer_
->num_bytes_buffered());
223 EXPECT_EQ(2u, buffered_frames_
->size());
224 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
225 EXPECT_EQ(6u, sequencer_
->num_bytes_buffered());
228 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
229 EXPECT_CALL(stream_
, ProcessRawData(StrEq("def"), 3)).WillOnce(Return(3));
230 EXPECT_CALL(stream_
, ProcessRawData(StrEq("ghi"), 3)).WillOnce(Return(3));
234 EXPECT_EQ(9u, sequencer_
->num_bytes_consumed());
235 EXPECT_EQ(0u, sequencer_
->num_bytes_buffered());
237 EXPECT_EQ(0u, buffered_frames_
->size());
240 TEST_F(QuicStreamSequencerTest
, BasicHalfCloseOrdered
) {
243 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
244 EXPECT_CALL(stream_
, OnFinRead());
245 OnFinFrame(0, "abc");
247 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
250 TEST_F(QuicStreamSequencerTest
, BasicHalfCloseUnorderedWithFlush
) {
252 EXPECT_EQ(6u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
254 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
255 EXPECT_CALL(stream_
, ProcessRawData(StrEq("def"), 3)).WillOnce(Return(3));
256 EXPECT_CALL(stream_
, OnFinRead());
262 TEST_F(QuicStreamSequencerTest
, BasicHalfUnordered
) {
264 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
266 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
267 EXPECT_CALL(stream_
, OnFinRead());
272 TEST_F(QuicStreamSequencerTest
, TerminateWithReadv
) {
276 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
278 EXPECT_FALSE(sequencer_
->IsClosed());
280 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(0));
283 iovec iov
= {&buffer
[0], 3};
284 int bytes_read
= sequencer_
->Readv(&iov
, 1);
285 EXPECT_EQ(3, bytes_read
);
286 EXPECT_TRUE(sequencer_
->IsClosed());
289 TEST_F(QuicStreamSequencerTest
, MutipleOffsets
) {
291 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
293 EXPECT_CALL(stream_
, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS
));
295 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
297 EXPECT_CALL(stream_
, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS
));
299 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
302 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
305 class QuicSequencerRandomTest
: public QuicStreamSequencerTest
{
307 typedef pair
<int, string
> Frame
;
308 typedef vector
<Frame
> FrameList
;
310 void CreateFrames() {
311 int payload_size
= arraysize(kPayload
) - 1;
312 int remaining_payload
= payload_size
;
313 while (remaining_payload
!= 0) {
314 int size
= min(OneToN(6), remaining_payload
);
315 int index
= payload_size
- remaining_payload
;
316 list_
.push_back(std::make_pair(index
, string(kPayload
+ index
, size
)));
317 remaining_payload
-= size
;
321 QuicSequencerRandomTest() {
326 return base::RandInt(1, n
);
329 int MaybeProcessMaybeBuffer(const char* data
, uint32 len
) {
330 int to_process
= len
;
331 if (base::RandUint64() % 2 != 0) {
332 to_process
= base::RandInt(0, len
);
334 output_
.append(data
, to_process
);
342 // All frames are processed as soon as we have sequential data.
343 // Infinite buffering, so all frames are acked right away.
344 TEST_F(QuicSequencerRandomTest
, RandomFramesNoDroppingNoBackup
) {
346 for (size_t i
= 0; i
< list_
.size(); ++i
) {
347 string
* data
= &list_
[i
].second
;
348 EXPECT_CALL(stream_
, ProcessRawData(StrEq(*data
), data
->size()))
349 .WillOnce(Return(data
->size()));
352 while (!list_
.empty()) {
353 int index
= OneToN(list_
.size()) - 1;
354 LOG(ERROR
) << "Sending index " << index
<< " " << list_
[index
].second
;
355 OnFrame(list_
[index
].first
, list_
[index
].second
.data());
357 list_
.erase(list_
.begin() + index
);
361 TEST_F(QuicStreamSequencerTest
, FrameOverlapsBufferedData
) {
362 // Ensure that FrameOverlapsBufferedData returns appropriate responses when
363 // there is existing data buffered.
365 map
<QuicStreamOffset
, string
>* buffered_frames
=
366 QuicStreamSequencerPeer::GetBufferedFrames(sequencer_
.get());
368 const int kBufferedOffset
= 10;
369 const int kBufferedDataLength
= 3;
370 const int kNewDataLength
= 3;
371 IOVector data
= MakeIOVector(string(kNewDataLength
, '.'));
373 // No overlap if no buffered frames.
374 EXPECT_TRUE(buffered_frames_
->empty());
375 EXPECT_FALSE(sequencer_
->FrameOverlapsBufferedData(
376 QuicStreamFrame(1, false, kBufferedOffset
- 1, data
)));
378 // Add a buffered frame.
379 buffered_frames
->insert(
380 std::make_pair(kBufferedOffset
, string(kBufferedDataLength
, '.')));
382 // New byte range partially overlaps with buffered frame, start offset
383 // preceeding buffered frame.
384 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(
385 QuicStreamFrame(1, false, kBufferedOffset
- 1, data
)));
386 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(
387 QuicStreamFrame(1, false, kBufferedOffset
- kNewDataLength
+ 1, data
)));
389 // New byte range partially overlaps with buffered frame, start offset
390 // inside existing buffered frame.
391 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(
392 QuicStreamFrame(1, false, kBufferedOffset
+ 1, data
)));
393 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(QuicStreamFrame(
394 1, false, kBufferedOffset
+ kBufferedDataLength
- 1, data
)));
396 // New byte range entirely outside of buffered frames, start offset preceeding
398 EXPECT_FALSE(sequencer_
->FrameOverlapsBufferedData(
399 QuicStreamFrame(1, false, kBufferedOffset
- kNewDataLength
, data
)));
401 // New byte range entirely outside of buffered frames, start offset later than
403 EXPECT_FALSE(sequencer_
->FrameOverlapsBufferedData(QuicStreamFrame(
404 1, false, kBufferedOffset
+ kBufferedDataLength
, data
)));
407 TEST_F(QuicStreamSequencerTest
, DontAcceptOverlappingFrames
) {
408 // The peer should never send us non-identical stream frames which contain
409 // overlapping byte ranges - if they do, we close the connection.
411 QuicStreamFrame
frame1(kClientDataStreamId1
, false, 1, MakeIOVector("hello"));
412 sequencer_
->OnStreamFrame(frame1
);
414 QuicStreamFrame
frame2(kClientDataStreamId1
, false, 2, MakeIOVector("hello"));
415 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(frame2
));
416 EXPECT_CALL(stream_
, CloseConnectionWithDetails(QUIC_INVALID_STREAM_FRAME
, _
))
418 sequencer_
->OnStreamFrame(frame2
);