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(false)),
65 session_(connection_
),
66 stream_(&session_
, 1),
67 sequencer_(new QuicStreamSequencer(&stream_
)),
69 QuicStreamSequencerPeer::GetBufferedFrames(sequencer_
.get())) {
72 bool VerifyIovecs(iovec
* iovecs
,
74 const char** expected
,
75 size_t num_expected
) {
76 if (num_expected
!= num_iovecs
) {
77 LOG(ERROR
) << "Incorrect number of iovecs. Expected: "
78 << num_expected
<< " Actual: " << num_iovecs
;
81 for (size_t i
= 0; i
< num_expected
; ++i
) {
82 if (!VerifyIovec(iovecs
[i
], expected
[i
])) {
89 bool VerifyIovec(const iovec
& iovec
, StringPiece expected
) {
90 if (iovec
.iov_len
!= expected
.length()) {
91 LOG(ERROR
) << "Invalid length: " << iovec
.iov_len
92 << " vs " << expected
.length();
95 if (memcmp(iovec
.iov_base
, expected
.data(), expected
.length()) != 0) {
96 LOG(ERROR
) << "Invalid data: " << static_cast<char*>(iovec
.iov_base
)
97 << " vs " << expected
.data();
103 void OnFinFrame(QuicStreamOffset byte_offset
, const char* data
) {
104 QuicStreamFrame frame
;
106 frame
.offset
= byte_offset
;
107 frame
.data
.Append(const_cast<char*>(data
), strlen(data
));
109 sequencer_
->OnStreamFrame(frame
);
112 void OnFrame(QuicStreamOffset byte_offset
, const char* data
) {
113 QuicStreamFrame frame
;
115 frame
.offset
= byte_offset
;
116 frame
.data
.Append(const_cast<char*>(data
), strlen(data
));
118 sequencer_
->OnStreamFrame(frame
);
121 MockConnection
* connection_
;
122 MockSession session_
;
123 testing::StrictMock
<MockStream
> stream_
;
124 scoped_ptr
<QuicStreamSequencer
> sequencer_
;
125 map
<QuicStreamOffset
, string
>* buffered_frames_
;
128 TEST_F(QuicStreamSequencerTest
, RejectOldFrame
) {
129 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
132 EXPECT_EQ(0u, buffered_frames_
->size());
133 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
134 // Ignore this - it matches a past sequence number and we should not see it
137 EXPECT_EQ(0u, buffered_frames_
->size());
140 TEST_F(QuicStreamSequencerTest
, RejectBufferedFrame
) {
141 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3));
144 EXPECT_EQ(1u, buffered_frames_
->size());
145 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
146 // Ignore this - it matches a buffered frame.
147 // Right now there's no checking that the payload is consistent.
149 EXPECT_EQ(1u, buffered_frames_
->size());
152 TEST_F(QuicStreamSequencerTest
, FullFrameConsumed
) {
153 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
156 EXPECT_EQ(0u, buffered_frames_
->size());
157 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
160 TEST_F(QuicStreamSequencerTest
, BlockedThenFullFrameConsumed
) {
161 sequencer_
->SetBlockedUntilFlush();
164 EXPECT_EQ(1u, buffered_frames_
->size());
165 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
167 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
168 sequencer_
->FlushBufferedFrames();
169 EXPECT_EQ(0u, buffered_frames_
->size());
170 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
172 EXPECT_CALL(stream_
, ProcessRawData(StrEq("def"), 3)).WillOnce(Return(3));
173 EXPECT_CALL(stream_
, OnFinRead());
174 OnFinFrame(3, "def");
177 TEST_F(QuicStreamSequencerTest
, BlockedThenFullFrameAndFinConsumed
) {
178 sequencer_
->SetBlockedUntilFlush();
180 OnFinFrame(0, "abc");
181 EXPECT_EQ(1u, buffered_frames_
->size());
182 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
184 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
185 EXPECT_CALL(stream_
, OnFinRead());
186 sequencer_
->FlushBufferedFrames();
187 EXPECT_EQ(0u, buffered_frames_
->size());
188 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
191 TEST_F(QuicStreamSequencerTest
, EmptyFrame
) {
193 CloseConnectionWithDetails(QUIC_INVALID_STREAM_FRAME
, _
));
195 EXPECT_EQ(0u, buffered_frames_
->size());
196 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
199 TEST_F(QuicStreamSequencerTest
, EmptyFinFrame
) {
200 EXPECT_CALL(stream_
, OnFinRead());
202 EXPECT_EQ(0u, buffered_frames_
->size());
203 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
206 TEST_F(QuicStreamSequencerTest
, PartialFrameConsumed
) {
207 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(2));
210 EXPECT_EQ(1u, buffered_frames_
->size());
211 EXPECT_EQ(2u, sequencer_
->num_bytes_consumed());
212 EXPECT_EQ("c", buffered_frames_
->find(2)->second
);
215 TEST_F(QuicStreamSequencerTest
, NextxFrameNotConsumed
) {
216 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(0));
219 EXPECT_EQ(1u, buffered_frames_
->size());
220 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
221 EXPECT_EQ("abc", buffered_frames_
->find(0)->second
);
222 EXPECT_EQ(0, sequencer_
->num_early_frames_received());
225 TEST_F(QuicStreamSequencerTest
, FutureFrameNotProcessed
) {
227 EXPECT_EQ(1u, buffered_frames_
->size());
228 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
229 EXPECT_EQ("abc", buffered_frames_
->find(3)->second
);
230 EXPECT_EQ(1, sequencer_
->num_early_frames_received());
233 TEST_F(QuicStreamSequencerTest
, OutOfOrderFrameProcessed
) {
236 EXPECT_EQ(1u, buffered_frames_
->size());
237 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
238 EXPECT_EQ(3u, sequencer_
->num_bytes_buffered());
241 EXPECT_EQ(2u, buffered_frames_
->size());
242 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
243 EXPECT_EQ(6u, sequencer_
->num_bytes_buffered());
246 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
247 EXPECT_CALL(stream_
, ProcessRawData(StrEq("def"), 3)).WillOnce(Return(3));
248 EXPECT_CALL(stream_
, ProcessRawData(StrEq("ghi"), 3)).WillOnce(Return(3));
252 EXPECT_EQ(9u, sequencer_
->num_bytes_consumed());
253 EXPECT_EQ(0u, sequencer_
->num_bytes_buffered());
255 EXPECT_EQ(0u, buffered_frames_
->size());
258 TEST_F(QuicStreamSequencerTest
, BasicHalfCloseOrdered
) {
261 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
262 EXPECT_CALL(stream_
, OnFinRead());
263 OnFinFrame(0, "abc");
265 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
268 TEST_F(QuicStreamSequencerTest
, BasicHalfCloseUnorderedWithFlush
) {
270 EXPECT_EQ(6u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
272 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
273 EXPECT_CALL(stream_
, ProcessRawData(StrEq("def"), 3)).WillOnce(Return(3));
274 EXPECT_CALL(stream_
, OnFinRead());
280 TEST_F(QuicStreamSequencerTest
, BasicHalfUnordered
) {
282 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
284 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
285 EXPECT_CALL(stream_
, OnFinRead());
290 TEST_F(QuicStreamSequencerTest
, TerminateWithReadv
) {
294 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
296 EXPECT_FALSE(sequencer_
->IsClosed());
298 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(0));
301 iovec iov
= {&buffer
[0], 3};
302 int bytes_read
= sequencer_
->Readv(&iov
, 1);
303 EXPECT_EQ(3, bytes_read
);
304 EXPECT_TRUE(sequencer_
->IsClosed());
307 TEST_F(QuicStreamSequencerTest
, MutipleOffsets
) {
309 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
311 EXPECT_CALL(stream_
, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS
));
313 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
315 EXPECT_CALL(stream_
, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS
));
317 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
320 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
323 class QuicSequencerRandomTest
: public QuicStreamSequencerTest
{
325 typedef pair
<int, string
> Frame
;
326 typedef vector
<Frame
> FrameList
;
328 void CreateFrames() {
329 int payload_size
= arraysize(kPayload
) - 1;
330 int remaining_payload
= payload_size
;
331 while (remaining_payload
!= 0) {
332 int size
= min(OneToN(6), remaining_payload
);
333 int index
= payload_size
- remaining_payload
;
334 list_
.push_back(std::make_pair(index
, string(kPayload
+ index
, size
)));
335 remaining_payload
-= size
;
339 QuicSequencerRandomTest() {
344 return base::RandInt(1, n
);
347 int MaybeProcessMaybeBuffer(const char* data
, uint32 len
) {
348 int to_process
= len
;
349 if (base::RandUint64() % 2 != 0) {
350 to_process
= base::RandInt(0, len
);
352 output_
.append(data
, to_process
);
360 // All frames are processed as soon as we have sequential data.
361 // Infinite buffering, so all frames are acked right away.
362 TEST_F(QuicSequencerRandomTest
, RandomFramesNoDroppingNoBackup
) {
364 for (size_t i
= 0; i
< list_
.size(); ++i
) {
365 string
* data
= &list_
[i
].second
;
366 EXPECT_CALL(stream_
, ProcessRawData(StrEq(*data
), data
->size()))
367 .WillOnce(Return(data
->size()));
370 while (!list_
.empty()) {
371 int index
= OneToN(list_
.size()) - 1;
372 LOG(ERROR
) << "Sending index " << index
<< " " << list_
[index
].second
;
373 OnFrame(list_
[index
].first
, list_
[index
].second
.data());
375 list_
.erase(list_
.begin() + index
);
379 TEST_F(QuicStreamSequencerTest
, FrameOverlapsBufferedData
) {
380 // Ensure that FrameOverlapsBufferedData returns appropriate responses when
381 // there is existing data buffered.
383 map
<QuicStreamOffset
, string
>* buffered_frames
=
384 QuicStreamSequencerPeer::GetBufferedFrames(sequencer_
.get());
386 const int kBufferedOffset
= 10;
387 const int kBufferedDataLength
= 3;
388 const int kNewDataLength
= 3;
389 IOVector data
= MakeIOVector(string(kNewDataLength
, '.'));
391 // No overlap if no buffered frames.
392 EXPECT_TRUE(buffered_frames_
->empty());
393 EXPECT_FALSE(sequencer_
->FrameOverlapsBufferedData(
394 QuicStreamFrame(1, false, kBufferedOffset
- 1, data
)));
396 // Add a buffered frame.
397 buffered_frames
->insert(
398 std::make_pair(kBufferedOffset
, string(kBufferedDataLength
, '.')));
400 // New byte range partially overlaps with buffered frame, start offset
401 // preceeding buffered frame.
402 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(
403 QuicStreamFrame(1, false, kBufferedOffset
- 1, data
)));
404 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(
405 QuicStreamFrame(1, false, kBufferedOffset
- kNewDataLength
+ 1, data
)));
407 // New byte range partially overlaps with buffered frame, start offset
408 // inside existing buffered frame.
409 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(
410 QuicStreamFrame(1, false, kBufferedOffset
+ 1, data
)));
411 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(QuicStreamFrame(
412 1, false, kBufferedOffset
+ kBufferedDataLength
- 1, data
)));
414 // New byte range entirely outside of buffered frames, start offset preceeding
416 EXPECT_FALSE(sequencer_
->FrameOverlapsBufferedData(
417 QuicStreamFrame(1, false, kBufferedOffset
- kNewDataLength
, data
)));
419 // New byte range entirely outside of buffered frames, start offset later than
421 EXPECT_FALSE(sequencer_
->FrameOverlapsBufferedData(QuicStreamFrame(
422 1, false, kBufferedOffset
+ kBufferedDataLength
, data
)));
425 TEST_F(QuicStreamSequencerTest
, DontAcceptOverlappingFrames
) {
426 // The peer should never send us non-identical stream frames which contain
427 // overlapping byte ranges - if they do, we close the connection.
429 QuicStreamFrame
frame1(kClientDataStreamId1
, false, 1, MakeIOVector("hello"));
430 sequencer_
->OnStreamFrame(frame1
);
432 QuicStreamFrame
frame2(kClientDataStreamId1
, false, 2, MakeIOVector("hello"));
433 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(frame2
));
434 EXPECT_CALL(stream_
, CloseConnectionWithDetails(QUIC_INVALID_STREAM_FRAME
, _
))
436 sequencer_
->OnStreamFrame(frame2
);