Revert of Clean up Smart Lock cryptohome keys logic: (patchset #5 id:80001 of https...
[chromium-blink-merge.git] / net / quic / quic_stream_sequencer_test.cc
blob8a71c4c1f8ab659bb5b3c161cda69992262ffab8
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"
7 #include <utility>
8 #include <vector>
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;
22 using std::map;
23 using std::min;
24 using std::pair;
25 using std::string;
26 using std::vector;
27 using testing::_;
28 using testing::AnyNumber;
29 using testing::InSequence;
30 using testing::Return;
31 using testing::StrEq;
33 namespace net {
34 namespace test {
36 class MockStream : public ReliableQuicStream {
37 public:
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 {
52 return true;
56 namespace {
58 static const char kPayload[] =
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
61 class QuicStreamSequencerTest : public ::testing::Test {
62 protected:
63 QuicStreamSequencerTest()
64 : connection_(new MockConnection(false)),
65 session_(connection_),
66 stream_(&session_, 1),
67 sequencer_(new QuicStreamSequencer(&stream_)),
68 buffered_frames_(
69 QuicStreamSequencerPeer::GetBufferedFrames(sequencer_.get())) {
72 bool VerifyReadableRegions(const char** expected, size_t num_expected) {
73 iovec iovecs[5];
74 size_t num_iovecs = sequencer_->GetReadableRegions(iovecs,
75 arraysize(iovecs));
76 return VerifyIovecs(iovecs, num_iovecs, expected, num_expected);
79 bool VerifyIovecs(iovec* iovecs,
80 size_t num_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;
86 return false;
88 for (size_t i = 0; i < num_expected; ++i) {
89 if (!VerifyIovec(iovecs[i], expected[i])) {
90 return false;
93 return true;
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();
100 return false;
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();
105 return false;
107 return true;
110 void OnFinFrame(QuicStreamOffset byte_offset, const char* data) {
111 QuicStreamFrame frame;
112 frame.stream_id = 1;
113 frame.offset = byte_offset;
114 frame.data.Append(const_cast<char*>(data), strlen(data));
115 frame.fin = true;
116 sequencer_->OnStreamFrame(frame);
119 void OnFrame(QuicStreamOffset byte_offset, const char* data) {
120 QuicStreamFrame frame;
121 frame.stream_id = 1;
122 frame.offset = byte_offset;
123 frame.data.Append(const_cast<char*>(data), strlen(data));
124 frame.fin = false;
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));
138 OnFrame(0, "abc");
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
142 // again.
143 OnFrame(0, "def");
144 EXPECT_EQ(0u, buffered_frames_->size());
147 TEST_F(QuicStreamSequencerTest, RejectBufferedFrame) {
148 EXPECT_CALL(stream_, ProcessRawData(StrEq("abc"), 3));
150 OnFrame(0, "abc");
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.
155 OnFrame(0, "def");
156 EXPECT_EQ(1u, buffered_frames_->size());
159 TEST_F(QuicStreamSequencerTest, FullFrameConsumed) {
160 EXPECT_CALL(stream_, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
162 OnFrame(0, "abc");
163 EXPECT_EQ(0u, buffered_frames_->size());
164 EXPECT_EQ(3u, sequencer_->num_bytes_consumed());
167 TEST_F(QuicStreamSequencerTest, BlockedThenFullFrameConsumed) {
168 sequencer_->SetBlockedUntilFlush();
170 OnFrame(0, "abc");
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) {
199 EXPECT_CALL(stream_,
200 CloseConnectionWithDetails(QUIC_INVALID_STREAM_FRAME, _));
201 OnFrame(0, "");
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());
208 OnFinFrame(0, "");
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));
216 OnFrame(0, "abc");
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));
225 OnFrame(0, "abc");
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) {
232 OnFrame(3, "abc");
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) {
239 // Buffer the first
240 OnFrame(6, "ghi");
241 EXPECT_EQ(1u, buffered_frames_->size());
242 EXPECT_EQ(0u, sequencer_->num_bytes_consumed());
243 EXPECT_EQ(3u, sequencer_->num_bytes_buffered());
244 // Buffer the second
245 OnFrame(3, "def");
246 EXPECT_EQ(2u, buffered_frames_->size());
247 EXPECT_EQ(0u, sequencer_->num_bytes_consumed());
248 EXPECT_EQ(6u, sequencer_->num_bytes_buffered());
250 InSequence s;
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));
255 // Ack right away
256 OnFrame(0, "abc");
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) {
264 InSequence s;
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) {
274 OnFinFrame(6, "");
275 EXPECT_EQ(6u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
276 InSequence s;
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());
281 OnFrame(3, "def");
282 OnFrame(0, "abc");
285 TEST_F(QuicStreamSequencerTest, BasicHalfUnordered) {
286 OnFinFrame(3, "");
287 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
288 InSequence s;
289 EXPECT_CALL(stream_, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(3));
290 EXPECT_CALL(stream_, OnFinRead());
292 OnFrame(0, "abc");
295 TEST_F(QuicStreamSequencerTest, TerminateWithReadv) {
296 char buffer[3];
298 OnFinFrame(3, "");
299 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
301 EXPECT_FALSE(sequencer_->IsClosed());
303 EXPECT_CALL(stream_, ProcessRawData(StrEq("abc"), 3)).WillOnce(Return(0));
304 OnFrame(0, "abc");
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) {
313 OnFinFrame(3, "");
314 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
316 EXPECT_CALL(stream_, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS));
317 OnFinFrame(5, "");
318 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
320 EXPECT_CALL(stream_, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS));
321 OnFinFrame(1, "");
322 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
324 OnFinFrame(3, "");
325 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
328 class QuicSequencerRandomTest : public QuicStreamSequencerTest {
329 public:
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() {
345 CreateFrames();
348 int OneToN(int n) {
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);
358 return to_process;
361 string output_;
362 FrameList list_;
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) {
368 InSequence s;
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
420 // buffered frame.
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
425 // buffered frame.
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, _))
440 .Times(1);
441 sequencer_->OnStreamFrame(frame2);
444 } // namespace
445 } // namespace test
446 } // namespace net