[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/quic/quic_stream_sequencer.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/rand_util.h" |
| 11 | #include "net/quic/reliable_quic_stream.h" |
| 12 | #include "testing/gmock/include/gmock/gmock.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | using base::StringPiece; |
| 16 | using std::min; |
| 17 | using std::pair; |
| 18 | using std::vector; |
| 19 | using testing::_; |
| 20 | using testing::AnyNumber; |
| 21 | using testing::InSequence; |
| 22 | using testing::Return; |
| 23 | using testing::StrEq; |
| 24 | |
| 25 | namespace net { |
[email protected] | b1f287d | 2012-12-22 17:25:39 | [diff] [blame] | 26 | namespace test { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 27 | |
| 28 | class QuicStreamSequencerPeer : public QuicStreamSequencer { |
| 29 | public: |
| 30 | explicit QuicStreamSequencerPeer(ReliableQuicStream* stream) |
| 31 | : QuicStreamSequencer(stream) { |
| 32 | } |
| 33 | |
| 34 | QuicStreamSequencerPeer(int32 max_mem, ReliableQuicStream* stream) |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 35 | : QuicStreamSequencer(max_mem, stream) { |
| 36 | } |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 37 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 38 | virtual bool OnFinFrame(QuicStreamOffset byte_offset, |
| 39 | const char* data) { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 40 | QuicStreamFrame frame; |
| 41 | frame.stream_id = 1; |
| 42 | frame.offset = byte_offset; |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 43 | frame.data = StringPiece(data); |
| 44 | frame.fin = true; |
| 45 | return OnStreamFrame(frame); |
| 46 | } |
| 47 | |
| 48 | virtual bool OnFrame(QuicStreamOffset byte_offset, |
| 49 | const char* data) { |
| 50 | QuicStreamFrame frame; |
| 51 | frame.stream_id = 1; |
| 52 | frame.offset = byte_offset; |
| 53 | frame.data = StringPiece(data); |
| 54 | frame.fin = false; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 55 | return OnStreamFrame(frame); |
| 56 | } |
| 57 | |
| 58 | void SetMemoryLimit(size_t limit) { |
| 59 | max_frame_memory_ = limit; |
| 60 | } |
[email protected] | 2ff600a | 2012-11-11 19:22:19 | [diff] [blame] | 61 | uint64 num_bytes_consumed() const { return num_bytes_consumed_; } |
| 62 | const FrameMap* frames() const { return &frames_; } |
[email protected] | 2ff600a | 2012-11-11 19:22:19 | [diff] [blame] | 63 | QuicStreamOffset close_offset() const { return close_offset_; } |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | class MockStream : public ReliableQuicStream { |
| 67 | public: |
| 68 | MockStream(QuicSession* session, QuicStreamId id) |
| 69 | : ReliableQuicStream(id, session) { |
| 70 | } |
| 71 | |
| 72 | MOCK_METHOD1(TerminateFromPeer, void(bool half_close)); |
| 73 | MOCK_METHOD2(ProcessData, uint32(const char* data, uint32 data_len)); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 74 | MOCK_METHOD2(ConnectionClose, void(QuicErrorCode error, bool from_peer)); |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 75 | MOCK_METHOD1(Close, void(QuicRstStreamErrorCode error)); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 76 | MOCK_METHOD0(OnCanWrite, void()); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | namespace { |
| 80 | |
| 81 | static const char kPayload[] = |
| 82 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 83 | |
| 84 | class QuicStreamSequencerTest : public ::testing::Test { |
| 85 | protected: |
| 86 | QuicStreamSequencerTest() |
| 87 | : session_(NULL), |
| 88 | stream_(session_, 1), |
| 89 | sequencer_(new QuicStreamSequencerPeer(&stream_)) { |
| 90 | } |
| 91 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 92 | bool VerifyReadableRegions(const char** expected, size_t num_expected) { |
| 93 | iovec iovecs[5]; |
| 94 | size_t num_iovecs = sequencer_->GetReadableRegions(iovecs, |
| 95 | arraysize(iovecs)); |
| 96 | return VerifyIovecs(iovecs, num_iovecs, expected, num_expected); |
| 97 | } |
| 98 | |
| 99 | bool VerifyIovecs(iovec* iovecs, |
| 100 | size_t num_iovecs, |
| 101 | const char** expected, |
| 102 | size_t num_expected) { |
| 103 | if (num_expected != num_iovecs) { |
| 104 | LOG(ERROR) << "Incorrect number of iovecs. Expected: " |
| 105 | << num_expected << " Actual: " << num_iovecs; |
| 106 | return false; |
| 107 | } |
| 108 | for (size_t i = 0; i < num_expected; ++i) { |
| 109 | if (!VerifyIovec(iovecs[i], expected[i])) { |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool VerifyIovec(const iovec& iovec, StringPiece expected) { |
| 117 | if (iovec.iov_len != expected.length()) { |
| 118 | LOG(ERROR) << "Invalid length: " << iovec.iov_len |
| 119 | << " vs " << expected.length(); |
| 120 | return false; |
| 121 | } |
| 122 | if (memcmp(iovec.iov_base, expected.data(), expected.length()) != 0) { |
| 123 | LOG(ERROR) << "Invalid data: " << static_cast<char*>(iovec.iov_base) |
| 124 | << " vs " << expected.data(); |
| 125 | return false; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 130 | QuicSession* session_; |
| 131 | testing::StrictMock<MockStream> stream_; |
| 132 | scoped_ptr<QuicStreamSequencerPeer> sequencer_; |
| 133 | }; |
| 134 | |
| 135 | TEST_F(QuicStreamSequencerTest, RejectOldFrame) { |
| 136 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)) |
| 137 | .WillOnce(Return(3)); |
| 138 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 139 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 140 | EXPECT_EQ(0u, sequencer_->frames()->size()); |
| 141 | EXPECT_EQ(3u, sequencer_->num_bytes_consumed()); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 142 | // Ignore this - it matches a past sequence number and we should not see it |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 143 | // again. |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 144 | EXPECT_TRUE(sequencer_->OnFrame(0, "def")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 145 | EXPECT_EQ(0u, sequencer_->frames()->size()); |
| 146 | } |
| 147 | |
| 148 | TEST_F(QuicStreamSequencerTest, RejectOverlyLargeFrame) { |
[email protected] | 6dcb550 | 2013-07-27 23:55:09 | [diff] [blame] | 149 | // TODO(rch): enable when chromium supports EXPECT_DFATAL. |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 150 | /* |
| 151 | EXPECT_DFATAL(sequencer_.reset(new QuicStreamSequencerPeer(2, &stream_)), |
| 152 | "Setting max frame memory to 2. " |
| 153 | "Some frames will be impossible to handle."); |
| 154 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 155 | EXPECT_DEBUG_DEATH(sequencer_->OnFrame(0, "abc"), ""); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 156 | */ |
| 157 | } |
| 158 | |
| 159 | TEST_F(QuicStreamSequencerTest, DropFramePastBuffering) { |
| 160 | sequencer_->SetMemoryLimit(3); |
| 161 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 162 | EXPECT_FALSE(sequencer_->OnFrame(3, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | TEST_F(QuicStreamSequencerTest, RejectBufferedFrame) { |
| 166 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)); |
| 167 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 168 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 169 | EXPECT_EQ(1u, sequencer_->frames()->size()); |
| 170 | EXPECT_EQ(0u, sequencer_->num_bytes_consumed()); |
| 171 | // Ignore this - it matches a buffered frame. |
| 172 | // Right now there's no checking that the payload is consistent. |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 173 | EXPECT_TRUE(sequencer_->OnFrame(0, "def")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 174 | EXPECT_EQ(1u, sequencer_->frames()->size()); |
| 175 | } |
| 176 | |
| 177 | TEST_F(QuicStreamSequencerTest, FullFrameConsumed) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 178 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3)); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 179 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 180 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 181 | EXPECT_EQ(0u, sequencer_->frames()->size()); |
| 182 | EXPECT_EQ(3u, sequencer_->num_bytes_consumed()); |
| 183 | } |
| 184 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 185 | TEST_F(QuicStreamSequencerTest, EmptyFrame) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 186 | EXPECT_CALL(stream_, ConnectionClose(QUIC_INVALID_STREAM_FRAME, false)); |
| 187 | EXPECT_FALSE(sequencer_->OnFrame(0, "")); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 188 | EXPECT_EQ(0u, sequencer_->frames()->size()); |
| 189 | EXPECT_EQ(0u, sequencer_->num_bytes_consumed()); |
| 190 | } |
| 191 | |
| 192 | TEST_F(QuicStreamSequencerTest, EmptyFinFrame) { |
| 193 | EXPECT_CALL(stream_, TerminateFromPeer(true)); |
| 194 | EXPECT_TRUE(sequencer_->OnFinFrame(0, "")); |
| 195 | EXPECT_EQ(0u, sequencer_->frames()->size()); |
| 196 | EXPECT_EQ(0u, sequencer_->num_bytes_consumed()); |
| 197 | } |
| 198 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 199 | TEST_F(QuicStreamSequencerTest, PartialFrameConsumed) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 200 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(2)); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 201 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 202 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 203 | EXPECT_EQ(1u, sequencer_->frames()->size()); |
| 204 | EXPECT_EQ(2u, sequencer_->num_bytes_consumed()); |
| 205 | EXPECT_EQ("c", sequencer_->frames()->find(2)->second); |
| 206 | } |
| 207 | |
| 208 | TEST_F(QuicStreamSequencerTest, NextxFrameNotConsumed) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 209 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(0)); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 210 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 211 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 212 | EXPECT_EQ(1u, sequencer_->frames()->size()); |
| 213 | EXPECT_EQ(0u, sequencer_->num_bytes_consumed()); |
| 214 | EXPECT_EQ("abc", sequencer_->frames()->find(0)->second); |
| 215 | } |
| 216 | |
| 217 | TEST_F(QuicStreamSequencerTest, FutureFrameNotProcessed) { |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 218 | EXPECT_TRUE(sequencer_->OnFrame(3, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 219 | EXPECT_EQ(1u, sequencer_->frames()->size()); |
| 220 | EXPECT_EQ(0u, sequencer_->num_bytes_consumed()); |
| 221 | EXPECT_EQ("abc", sequencer_->frames()->find(3)->second); |
| 222 | } |
| 223 | |
| 224 | TEST_F(QuicStreamSequencerTest, OutOfOrderFrameProcessed) { |
| 225 | // Buffer the first |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 226 | EXPECT_TRUE(sequencer_->OnFrame(6, "ghi")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 227 | EXPECT_EQ(1u, sequencer_->frames()->size()); |
| 228 | EXPECT_EQ(0u, sequencer_->num_bytes_consumed()); |
| 229 | // Buffer the second |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 230 | EXPECT_TRUE(sequencer_->OnFrame(3, "def")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 231 | EXPECT_EQ(2u, sequencer_->frames()->size()); |
| 232 | EXPECT_EQ(0u, sequencer_->num_bytes_consumed()); |
| 233 | |
| 234 | InSequence s; |
| 235 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3)); |
| 236 | EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(3)); |
| 237 | EXPECT_CALL(stream_, ProcessData(StrEq("ghi"), 3)).WillOnce(Return(3)); |
| 238 | |
| 239 | // Ack right away |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 240 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 241 | EXPECT_EQ(9u, sequencer_->num_bytes_consumed()); |
| 242 | |
| 243 | EXPECT_EQ(0u, sequencer_->frames()->size()); |
| 244 | } |
| 245 | |
| 246 | TEST_F(QuicStreamSequencerTest, OutOfOrderFramesProcessedWithBuffering) { |
| 247 | sequencer_->SetMemoryLimit(9); |
| 248 | |
| 249 | // Too far to buffer. |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 250 | EXPECT_FALSE(sequencer_->OnFrame(9, "jkl")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 251 | |
| 252 | // We can afford to buffer this. |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 253 | EXPECT_TRUE(sequencer_->OnFrame(6, "ghi")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 254 | EXPECT_EQ(0u, sequencer_->num_bytes_consumed()); |
| 255 | |
| 256 | InSequence s; |
| 257 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3)); |
| 258 | |
| 259 | // Ack right away |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 260 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 261 | EXPECT_EQ(3u, sequencer_->num_bytes_consumed()); |
| 262 | |
| 263 | // We should be willing to buffer this now. |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 264 | EXPECT_TRUE(sequencer_->OnFrame(9, "jkl")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 265 | EXPECT_EQ(3u, sequencer_->num_bytes_consumed()); |
| 266 | |
| 267 | EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(3)); |
| 268 | EXPECT_CALL(stream_, ProcessData(StrEq("ghi"), 3)).WillOnce(Return(3)); |
| 269 | EXPECT_CALL(stream_, ProcessData(StrEq("jkl"), 3)).WillOnce(Return(3)); |
| 270 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 271 | EXPECT_TRUE(sequencer_->OnFrame(3, "def")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 272 | EXPECT_EQ(12u, sequencer_->num_bytes_consumed()); |
| 273 | EXPECT_EQ(0u, sequencer_->frames()->size()); |
| 274 | } |
| 275 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 276 | TEST_F(QuicStreamSequencerTest, OutOfOrderFramesBlockignWithReadv) { |
| 277 | sequencer_->SetMemoryLimit(9); |
| 278 | char buffer[20]; |
| 279 | iovec iov[2]; |
| 280 | iov[0].iov_base = &buffer[0]; |
| 281 | iov[0].iov_len = 1; |
| 282 | iov[1].iov_base = &buffer[1]; |
| 283 | iov[1].iov_len = 2; |
| 284 | |
| 285 | // Push abc - process. |
| 286 | // Push jkl - buffer (not next data) |
| 287 | // Push def - don't process. |
| 288 | // Push mno - drop (too far out) |
| 289 | // Push ghi - buffer (def not processed) |
| 290 | // Read 2. |
| 291 | // Push mno - buffer (not all read) |
| 292 | // Read all |
| 293 | // Push pqr - process |
| 294 | |
| 295 | InSequence s; |
| 296 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3)); |
| 297 | EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(0)); |
| 298 | EXPECT_CALL(stream_, ProcessData(StrEq("pqr"), 3)).WillOnce(Return(3)); |
| 299 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 300 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
| 301 | EXPECT_TRUE(sequencer_->OnFrame(3, "def")); |
| 302 | EXPECT_TRUE(sequencer_->OnFrame(9, "jkl")); |
| 303 | EXPECT_FALSE(sequencer_->OnFrame(12, "mno")); |
| 304 | EXPECT_TRUE(sequencer_->OnFrame(6, "ghi")); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 305 | |
| 306 | // Read 3 bytes. |
| 307 | EXPECT_EQ(3, sequencer_->Readv(iov, 2)); |
| 308 | EXPECT_EQ(0, strncmp(buffer, "def", 3)); |
| 309 | |
| 310 | // Now we have space to bufer this. |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 311 | EXPECT_TRUE(sequencer_->OnFrame(12, "mno")); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 312 | |
| 313 | // Read the remaining 9 bytes. |
| 314 | iov[1].iov_len = 19; |
| 315 | EXPECT_EQ(9, sequencer_->Readv(iov, 2)); |
| 316 | EXPECT_EQ(0, strncmp(buffer, "ghijklmno", 9)); |
| 317 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 318 | EXPECT_TRUE(sequencer_->OnFrame(15, "pqr")); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // Same as above, just using a different method for reading. |
| 322 | TEST_F(QuicStreamSequencerTest, OutOfOrderFramesBlockignWithGetReadableRegion) { |
| 323 | sequencer_->SetMemoryLimit(9); |
| 324 | |
| 325 | InSequence s; |
| 326 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3)); |
| 327 | EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(0)); |
| 328 | EXPECT_CALL(stream_, ProcessData(StrEq("pqr"), 3)).WillOnce(Return(3)); |
| 329 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 330 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
| 331 | EXPECT_TRUE(sequencer_->OnFrame(3, "def")); |
| 332 | EXPECT_TRUE(sequencer_->OnFrame(9, "jkl")); |
| 333 | EXPECT_FALSE(sequencer_->OnFrame(12, "mno")); |
| 334 | EXPECT_TRUE(sequencer_->OnFrame(6, "ghi")); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 335 | |
| 336 | // Read 3 bytes. |
| 337 | const char* expected[] = {"def", "ghi", "jkl"}; |
| 338 | ASSERT_TRUE(VerifyReadableRegions(expected, arraysize(expected))); |
| 339 | char buffer[9]; |
| 340 | iovec read_iov = { &buffer[0], 3 }; |
| 341 | ASSERT_EQ(3, sequencer_->Readv(&read_iov, 1)); |
| 342 | |
| 343 | // Now we have space to bufer this. |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 344 | EXPECT_TRUE(sequencer_->OnFrame(12, "mno")); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 345 | |
| 346 | // Read the remaining 9 bytes. |
| 347 | const char* expected2[] = {"ghi", "jkl", "mno"}; |
| 348 | ASSERT_TRUE(VerifyReadableRegions(expected2, arraysize(expected2))); |
| 349 | read_iov.iov_len = 9; |
| 350 | ASSERT_EQ(9, sequencer_->Readv(&read_iov, 1)); |
| 351 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 352 | EXPECT_TRUE(sequencer_->OnFrame(15, "pqr")); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Same as above, just using a different method for reading. |
| 356 | TEST_F(QuicStreamSequencerTest, MarkConsumed) { |
| 357 | sequencer_->SetMemoryLimit(9); |
| 358 | |
| 359 | InSequence s; |
| 360 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(0)); |
| 361 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 362 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
| 363 | EXPECT_TRUE(sequencer_->OnFrame(3, "def")); |
| 364 | EXPECT_TRUE(sequencer_->OnFrame(6, "ghi")); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 365 | |
| 366 | // Peek into the data. |
| 367 | const char* expected[] = {"abc", "def", "ghi"}; |
| 368 | ASSERT_TRUE(VerifyReadableRegions(expected, arraysize(expected))); |
| 369 | |
| 370 | // Consume 1 byte. |
| 371 | sequencer_->MarkConsumed(1); |
| 372 | // Verify data. |
| 373 | const char* expected2[] = {"bc", "def", "ghi"}; |
| 374 | ASSERT_TRUE(VerifyReadableRegions(expected2, arraysize(expected2))); |
| 375 | |
| 376 | // Consume 2 bytes. |
| 377 | sequencer_->MarkConsumed(2); |
| 378 | // Verify data. |
| 379 | const char* expected3[] = {"def", "ghi"}; |
| 380 | ASSERT_TRUE(VerifyReadableRegions(expected3, arraysize(expected3))); |
| 381 | |
| 382 | // Consume 5 bytes. |
| 383 | sequencer_->MarkConsumed(5); |
| 384 | // Verify data. |
| 385 | const char* expected4[] = {"i"}; |
| 386 | ASSERT_TRUE(VerifyReadableRegions(expected4, arraysize(expected4))); |
| 387 | } |
| 388 | |
[email protected] | 6dcb550 | 2013-07-27 23:55:09 | [diff] [blame] | 389 | TEST_F(QuicStreamSequencerTest, MarkConsumedError) { |
| 390 | // TODO(rch): enable when chromium supports EXPECT_DFATAL. |
| 391 | /* |
| 392 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(0)); |
| 393 | |
| 394 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
| 395 | EXPECT_TRUE(sequencer_->OnFrame(9, "jklmnopqrstuvwxyz")); |
| 396 | |
| 397 | // Peek into the data. Only the first chunk should be readable |
| 398 | // because of the missing data. |
| 399 | const char* expected[] = {"abc"}; |
| 400 | ASSERT_TRUE(VerifyReadableRegions(expected, arraysize(expected))); |
| 401 | |
| 402 | // Now, attempt to mark consumed more data than was readable |
| 403 | // and expect the stream to be closed. |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 404 | EXPECT_CALL(stream_, Close(QUIC_ERROR_PROCESSING_STREAM)); |
[email protected] | 6dcb550 | 2013-07-27 23:55:09 | [diff] [blame] | 405 | EXPECT_DFATAL(sequencer_->MarkConsumed(4), |
| 406 | "Invalid argument to MarkConsumed. num_bytes_consumed_: 3 " |
| 407 | "end_offset: 4 offset: 9 length: 17"); |
| 408 | */ |
| 409 | } |
| 410 | |
| 411 | TEST_F(QuicStreamSequencerTest, MarkConsumedWithMissingPacket) { |
| 412 | InSequence s; |
| 413 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(0)); |
| 414 | |
| 415 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
| 416 | EXPECT_TRUE(sequencer_->OnFrame(3, "def")); |
| 417 | // Missing packet: 6, ghi |
| 418 | EXPECT_TRUE(sequencer_->OnFrame(9, "jkl")); |
| 419 | |
| 420 | const char* expected[] = {"abc", "def"}; |
| 421 | ASSERT_TRUE(VerifyReadableRegions(expected, arraysize(expected))); |
| 422 | |
| 423 | sequencer_->MarkConsumed(6); |
| 424 | } |
| 425 | |
[email protected] | 4e49b6a | 2013-06-18 16:39:28 | [diff] [blame] | 426 | TEST_F(QuicStreamSequencerTest, BasicHalfCloseOrdered) { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 427 | InSequence s; |
| 428 | |
| 429 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3)); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 430 | EXPECT_CALL(stream_, TerminateFromPeer(true)); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 431 | EXPECT_TRUE(sequencer_->OnFinFrame(0, "abc")); |
| 432 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 433 | EXPECT_EQ(3u, sequencer_->close_offset()); |
| 434 | } |
| 435 | |
[email protected] | 4e49b6a | 2013-06-18 16:39:28 | [diff] [blame] | 436 | TEST_F(QuicStreamSequencerTest, BasicHalfCloseUnorderedWithFlush) { |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 437 | sequencer_->OnFinFrame(6, ""); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 438 | EXPECT_EQ(6u, sequencer_->close_offset()); |
| 439 | InSequence s; |
| 440 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3)); |
| 441 | EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(3)); |
| 442 | EXPECT_CALL(stream_, TerminateFromPeer(true)); |
| 443 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 444 | EXPECT_TRUE(sequencer_->OnFrame(3, "def")); |
| 445 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 446 | } |
| 447 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 448 | TEST_F(QuicStreamSequencerTest, BasicHalfUnordered) { |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 449 | sequencer_->OnFinFrame(3, ""); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 450 | EXPECT_EQ(3u, sequencer_->close_offset()); |
| 451 | InSequence s; |
| 452 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3)); |
| 453 | EXPECT_CALL(stream_, TerminateFromPeer(true)); |
| 454 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 455 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 456 | } |
| 457 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 458 | TEST_F(QuicStreamSequencerTest, TerminateWithReadv) { |
| 459 | char buffer[3]; |
| 460 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 461 | sequencer_->OnFinFrame(3, ""); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 462 | EXPECT_EQ(3u, sequencer_->close_offset()); |
| 463 | |
| 464 | EXPECT_FALSE(sequencer_->IsHalfClosed()); |
| 465 | |
| 466 | EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(0)); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 467 | EXPECT_TRUE(sequencer_->OnFrame(0, "abc")); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 468 | |
| 469 | iovec iov = { &buffer[0], 3 }; |
| 470 | int bytes_read = sequencer_->Readv(&iov, 1); |
| 471 | EXPECT_EQ(3, bytes_read); |
| 472 | EXPECT_TRUE(sequencer_->IsHalfClosed()); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 473 | } |
| 474 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 475 | TEST_F(QuicStreamSequencerTest, MutipleOffsets) { |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 476 | sequencer_->OnFinFrame(3, ""); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 477 | EXPECT_EQ(3u, sequencer_->close_offset()); |
| 478 | |
| 479 | EXPECT_CALL(stream_, Close(QUIC_MULTIPLE_TERMINATION_OFFSETS)); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 480 | sequencer_->OnFinFrame(5, ""); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 481 | EXPECT_EQ(3u, sequencer_->close_offset()); |
| 482 | |
| 483 | EXPECT_CALL(stream_, Close(QUIC_MULTIPLE_TERMINATION_OFFSETS)); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 484 | sequencer_->OnFinFrame(1, ""); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 485 | EXPECT_EQ(3u, sequencer_->close_offset()); |
| 486 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 487 | sequencer_->OnFinFrame(3, ""); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 488 | EXPECT_EQ(3u, sequencer_->close_offset()); |
| 489 | } |
| 490 | |
| 491 | class QuicSequencerRandomTest : public QuicStreamSequencerTest { |
| 492 | public: |
| 493 | typedef pair<int, string> Frame; |
| 494 | typedef vector<Frame> FrameList; |
| 495 | |
| 496 | void CreateFrames() { |
| 497 | int payload_size = arraysize(kPayload) - 1; |
| 498 | int remaining_payload = payload_size; |
| 499 | while (remaining_payload != 0) { |
| 500 | int size = min(OneToN(6), remaining_payload); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 501 | int index = payload_size - remaining_payload; |
| 502 | list_.push_back(make_pair(index, string(kPayload + index, size))); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 503 | remaining_payload -= size; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | QuicSequencerRandomTest() { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 508 | CreateFrames(); |
| 509 | } |
| 510 | |
| 511 | int OneToN(int n) { |
| 512 | return base::RandInt(1, n); |
| 513 | } |
| 514 | |
| 515 | int MaybeProcessMaybeBuffer(const char* data, uint32 len) { |
| 516 | int to_process = len; |
| 517 | if (base::RandUint64() % 2 != 0) { |
| 518 | to_process = base::RandInt(0, len); |
| 519 | } |
| 520 | output_.append(data, to_process); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 521 | return to_process; |
| 522 | } |
| 523 | |
| 524 | string output_; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 525 | FrameList list_; |
| 526 | }; |
| 527 | |
| 528 | // All frames are processed as soon as we have sequential data. |
| 529 | // Infinite buffering, so all frames are acked right away. |
| 530 | TEST_F(QuicSequencerRandomTest, RandomFramesNoDroppingNoBackup) { |
| 531 | InSequence s; |
| 532 | for (size_t i = 0; i < list_.size(); ++i) { |
| 533 | string* data = &list_[i].second; |
| 534 | EXPECT_CALL(stream_, ProcessData(StrEq(*data), data->size())) |
| 535 | .WillOnce(Return(data->size())); |
| 536 | } |
| 537 | |
| 538 | while (list_.size() != 0) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 539 | int index = OneToN(list_.size()) - 1; |
| 540 | LOG(ERROR) << "Sending index " << index << " " |
| 541 | << list_[index].second.data(); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 542 | EXPECT_TRUE(sequencer_->OnFrame(list_[index].first, |
| 543 | list_[index].second.data())); |
| 544 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 545 | list_.erase(list_.begin() + index); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | // All frames are processed as soon as we have sequential data. |
| 550 | // Buffering, so some frames are rejected. |
| 551 | TEST_F(QuicSequencerRandomTest, RandomFramesDroppingNoBackup) { |
| 552 | sequencer_->SetMemoryLimit(26); |
| 553 | |
| 554 | InSequence s; |
| 555 | for (size_t i = 0; i < list_.size(); ++i) { |
| 556 | string* data = &list_[i].second; |
| 557 | EXPECT_CALL(stream_, ProcessData(StrEq(*data), data->size())) |
| 558 | .WillOnce(Return(data->size())); |
| 559 | } |
| 560 | |
| 561 | while (list_.size() != 0) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 562 | int index = OneToN(list_.size()) - 1; |
| 563 | LOG(ERROR) << "Sending index " << index << " " |
| 564 | << list_[index].second.data(); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 565 | bool acked = sequencer_->OnFrame(list_[index].first, |
| 566 | list_[index].second.data()); |
| 567 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 568 | if (acked) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 569 | list_.erase(list_.begin() + index); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | } // namespace |
[email protected] | b1f287d | 2012-12-22 17:25:39 | [diff] [blame] | 575 | } // namespace test |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 576 | } // namespace net |