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 VerifyReadableRegions(const char** expected
, size_t num_expected
) {
74 size_t num_iovecs
= sequencer_
->GetReadableRegions(iovecs
,
76 return VerifyIovecs(iovecs
, num_iovecs
, expected
, num_expected
);
79 bool VerifyIovecs(iovec
* iovecs
,
81 const char** expected
,
82 size_t num_expected
) {
83 if (num_expected
!= num_iovecs
) {
84 LOG(ERROR
) << "Incorrect number of iovecs. Expected: "
85 << num_expected
<< " Actual: " << num_iovecs
;
88 for (size_t i
= 0; i
< num_expected
; ++i
) {
89 if (!VerifyIovec(iovecs
[i
], expected
[i
])) {
96 bool VerifyIovec(const iovec
& iovec
, StringPiece expected
) {
97 if (iovec
.iov_len
!= expected
.length()) {
98 LOG(ERROR
) << "Invalid length: " << iovec
.iov_len
99 << " vs " << expected
.length();
102 if (memcmp(iovec
.iov_base
, expected
.data(), expected
.length()) != 0) {
103 LOG(ERROR
) << "Invalid data: " << static_cast<char*>(iovec
.iov_base
)
104 << " vs " << expected
.data();
110 void OnFinFrame(QuicStreamOffset byte_offset
, const char* data
) {
111 QuicStreamFrame frame
;
113 frame
.offset
= byte_offset
;
114 frame
.data
.Append(const_cast<char*>(data
), strlen(data
));
116 sequencer_
->OnStreamFrame(frame
);
119 void OnFrame(QuicStreamOffset byte_offset
, const char* data
) {
120 QuicStreamFrame frame
;
122 frame
.offset
= byte_offset
;
123 frame
.data
.Append(const_cast<char*>(data
), strlen(data
));
125 sequencer_
->OnStreamFrame(frame
);
128 MockConnection
* connection_
;
129 MockSession session_
;
130 testing::StrictMock
<MockStream
> stream_
;
131 scoped_ptr
<QuicStreamSequencer
> sequencer_
;
132 map
<QuicStreamOffset
, string
>* buffered_frames_
;
135 TEST_F(QuicStreamSequencerTest
, RejectOldFrame
) {
136 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
139 EXPECT_EQ(0u, buffered_frames_
->size());
140 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
141 // Ignore this - it matches a past sequence number and we should not see it
144 EXPECT_EQ(0u, buffered_frames_
->size());
147 TEST_F(QuicStreamSequencerTest
, RejectBufferedFrame
) {
148 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3));
151 EXPECT_EQ(1u, buffered_frames_
->size());
152 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
153 // Ignore this - it matches a buffered frame.
154 // Right now there's no checking that the payload is consistent.
156 EXPECT_EQ(1u, buffered_frames_
->size());
159 TEST_F(QuicStreamSequencerTest
, FullFrameConsumed
) {
160 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
163 EXPECT_EQ(0u, buffered_frames_
->size());
164 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
167 TEST_F(QuicStreamSequencerTest
, BlockedThenFullFrameConsumed
) {
168 sequencer_
->SetBlockedUntilFlush();
171 EXPECT_EQ(1u, buffered_frames_
->size());
172 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
174 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
175 sequencer_
->FlushBufferedFrames();
176 EXPECT_EQ(0u, buffered_frames_
->size());
177 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
179 EXPECT_CALL(stream_
, ProcessRawData(StrEq("def"), 3)).WillOnce(Return(3));
180 EXPECT_CALL(stream_
, OnFinRead());
181 OnFinFrame(3, "def");
184 TEST_F(QuicStreamSequencerTest
, BlockedThenFullFrameAndFinConsumed
) {
185 sequencer_
->SetBlockedUntilFlush();
187 OnFinFrame(0, "abc");
188 EXPECT_EQ(1u, buffered_frames_
->size());
189 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
191 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
192 EXPECT_CALL(stream_
, OnFinRead());
193 sequencer_
->FlushBufferedFrames();
194 EXPECT_EQ(0u, buffered_frames_
->size());
195 EXPECT_EQ(3u, sequencer_
->num_bytes_consumed());
198 TEST_F(QuicStreamSequencerTest
, EmptyFrame
) {
200 CloseConnectionWithDetails(QUIC_INVALID_STREAM_FRAME
, _
));
202 EXPECT_EQ(0u, buffered_frames_
->size());
203 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
206 TEST_F(QuicStreamSequencerTest
, EmptyFinFrame
) {
207 EXPECT_CALL(stream_
, OnFinRead());
209 EXPECT_EQ(0u, buffered_frames_
->size());
210 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
213 TEST_F(QuicStreamSequencerTest
, PartialFrameConsumed
) {
214 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(2));
217 EXPECT_EQ(1u, buffered_frames_
->size());
218 EXPECT_EQ(2u, sequencer_
->num_bytes_consumed());
219 EXPECT_EQ("c", buffered_frames_
->find(2)->second
);
222 TEST_F(QuicStreamSequencerTest
, NextxFrameNotConsumed
) {
223 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(0));
226 EXPECT_EQ(1u, buffered_frames_
->size());
227 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
228 EXPECT_EQ("abc", buffered_frames_
->find(0)->second
);
231 TEST_F(QuicStreamSequencerTest
, FutureFrameNotProcessed
) {
233 EXPECT_EQ(1u, buffered_frames_
->size());
234 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
235 EXPECT_EQ("abc", buffered_frames_
->find(3)->second
);
238 TEST_F(QuicStreamSequencerTest
, OutOfOrderFrameProcessed
) {
241 EXPECT_EQ(1u, buffered_frames_
->size());
242 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
243 EXPECT_EQ(3u, sequencer_
->num_bytes_buffered());
246 EXPECT_EQ(2u, buffered_frames_
->size());
247 EXPECT_EQ(0u, sequencer_
->num_bytes_consumed());
248 EXPECT_EQ(6u, sequencer_
->num_bytes_buffered());
251 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
252 EXPECT_CALL(stream_
, ProcessRawData(StrEq("def"), 3)).WillOnce(Return(3));
253 EXPECT_CALL(stream_
, ProcessRawData(StrEq("ghi"), 3)).WillOnce(Return(3));
257 EXPECT_EQ(9u, sequencer_
->num_bytes_consumed());
258 EXPECT_EQ(0u, sequencer_
->num_bytes_buffered());
260 EXPECT_EQ(0u, buffered_frames_
->size());
263 TEST_F(QuicStreamSequencerTest
, BasicHalfCloseOrdered
) {
266 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
267 EXPECT_CALL(stream_
, OnFinRead());
268 OnFinFrame(0, "abc");
270 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
273 TEST_F(QuicStreamSequencerTest
, BasicHalfCloseUnorderedWithFlush
) {
275 EXPECT_EQ(6u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
277 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
278 EXPECT_CALL(stream_
, ProcessRawData(StrEq("def"), 3)).WillOnce(Return(3));
279 EXPECT_CALL(stream_
, OnFinRead());
285 TEST_F(QuicStreamSequencerTest
, BasicHalfUnordered
) {
287 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
289 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
290 EXPECT_CALL(stream_
, OnFinRead());
295 TEST_F(QuicStreamSequencerTest
, TerminateWithReadv
) {
299 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
301 EXPECT_FALSE(sequencer_
->IsClosed());
303 EXPECT_CALL(stream_
, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(0));
306 iovec iov
= {&buffer
[0], 3};
307 int bytes_read
= sequencer_
->Readv(&iov
, 1);
308 EXPECT_EQ(3, bytes_read
);
309 EXPECT_TRUE(sequencer_
->IsClosed());
312 TEST_F(QuicStreamSequencerTest
, MutipleOffsets
) {
314 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
316 EXPECT_CALL(stream_
, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS
));
318 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
320 EXPECT_CALL(stream_
, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS
));
322 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
325 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_
.get()));
328 class QuicSequencerRandomTest
: public QuicStreamSequencerTest
{
330 typedef pair
<int, string
> Frame
;
331 typedef vector
<Frame
> FrameList
;
333 void CreateFrames() {
334 int payload_size
= arraysize(kPayload
) - 1;
335 int remaining_payload
= payload_size
;
336 while (remaining_payload
!= 0) {
337 int size
= min(OneToN(6), remaining_payload
);
338 int index
= payload_size
- remaining_payload
;
339 list_
.push_back(make_pair(index
, string(kPayload
+ index
, size
)));
340 remaining_payload
-= size
;
344 QuicSequencerRandomTest() {
349 return base::RandInt(1, n
);
352 int MaybeProcessMaybeBuffer(const char* data
, uint32 len
) {
353 int to_process
= len
;
354 if (base::RandUint64() % 2 != 0) {
355 to_process
= base::RandInt(0, len
);
357 output_
.append(data
, to_process
);
365 // All frames are processed as soon as we have sequential data.
366 // Infinite buffering, so all frames are acked right away.
367 TEST_F(QuicSequencerRandomTest
, RandomFramesNoDroppingNoBackup
) {
369 for (size_t i
= 0; i
< list_
.size(); ++i
) {
370 string
* data
= &list_
[i
].second
;
371 EXPECT_CALL(stream_
, ProcessRawData(StrEq(*data
), data
->size()))
372 .WillOnce(Return(data
->size()));
375 while (!list_
.empty()) {
376 int index
= OneToN(list_
.size()) - 1;
377 LOG(ERROR
) << "Sending index " << index
<< " " << list_
[index
].second
;
378 OnFrame(list_
[index
].first
, list_
[index
].second
.data());
380 list_
.erase(list_
.begin() + index
);
384 TEST_F(QuicStreamSequencerTest
, FrameOverlapsBufferedData
) {
385 // Ensure that FrameOverlapsBufferedData returns appropriate responses when
386 // there is existing data buffered.
388 map
<QuicStreamOffset
, string
>* buffered_frames
=
389 QuicStreamSequencerPeer::GetBufferedFrames(sequencer_
.get());
391 const int kBufferedOffset
= 10;
392 const int kBufferedDataLength
= 3;
393 const int kNewDataLength
= 3;
394 IOVector data
= MakeIOVector(string(kNewDataLength
, '.'));
396 // No overlap if no buffered frames.
397 EXPECT_TRUE(buffered_frames_
->empty());
398 EXPECT_FALSE(sequencer_
->FrameOverlapsBufferedData(
399 QuicStreamFrame(1, false, kBufferedOffset
- 1, data
)));
401 // Add a buffered frame.
402 buffered_frames
->insert(
403 make_pair(kBufferedOffset
, string(kBufferedDataLength
, '.')));
405 // New byte range partially overlaps with buffered frame, start offset
406 // preceeding buffered frame.
407 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(
408 QuicStreamFrame(1, false, kBufferedOffset
- 1, data
)));
409 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(
410 QuicStreamFrame(1, false, kBufferedOffset
- kNewDataLength
+ 1, data
)));
412 // New byte range partially overlaps with buffered frame, start offset
413 // inside existing buffered frame.
414 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(
415 QuicStreamFrame(1, false, kBufferedOffset
+ 1, data
)));
416 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(QuicStreamFrame(
417 1, false, kBufferedOffset
+ kBufferedDataLength
- 1, data
)));
419 // New byte range entirely outside of buffered frames, start offset preceeding
421 EXPECT_FALSE(sequencer_
->FrameOverlapsBufferedData(
422 QuicStreamFrame(1, false, kBufferedOffset
- kNewDataLength
, data
)));
424 // New byte range entirely outside of buffered frames, start offset later than
426 EXPECT_FALSE(sequencer_
->FrameOverlapsBufferedData(QuicStreamFrame(
427 1, false, kBufferedOffset
+ kBufferedDataLength
, data
)));
430 TEST_F(QuicStreamSequencerTest
, DontAcceptOverlappingFrames
) {
431 // The peer should never send us non-identical stream frames which contain
432 // overlapping byte ranges - if they do, we close the connection.
434 QuicStreamFrame
frame1(kClientDataStreamId1
, false, 1, MakeIOVector("hello"));
435 sequencer_
->OnStreamFrame(frame1
);
437 QuicStreamFrame
frame2(kClientDataStreamId1
, false, 2, MakeIOVector("hello"));
438 EXPECT_TRUE(sequencer_
->FrameOverlapsBufferedData(frame2
));
439 EXPECT_CALL(stream_
, CloseConnectionWithDetails(QUIC_INVALID_STREAM_FRAME
, _
))
441 sequencer_
->OnStreamFrame(frame2
);