1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/third_party/quiche/src/quic/core/quic_stream_sequencer.h"
#include <algorithm>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "net/third_party/quiche/src/quic/core/quic_stream.h"
#include "net/third_party/quiche/src/quic/core/quic_utils.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
#include "net/third_party/quiche/src/quic/test_tools/quic_stream_sequencer_peer.h"
#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
using testing::_;
using testing::AnyNumber;
using testing::InSequence;
namespace quic {
namespace test {
class MockStream : public QuicStreamSequencer::StreamInterface {
public:
MOCK_METHOD0(OnFinRead, void());
MOCK_METHOD0(OnDataAvailable, void());
MOCK_METHOD2(CloseConnectionWithDetails,
void(QuicErrorCode error, const std::string& details));
MOCK_METHOD1(Reset, void(QuicRstStreamErrorCode error));
MOCK_METHOD0(OnCanWrite, void());
MOCK_METHOD1(AddBytesConsumed, void(QuicByteCount bytes));
QuicStreamId id() const override { return 1; }
const QuicSocketAddress& PeerAddressOfLatestPacket() const override {
return peer_address_;
}
protected:
QuicSocketAddress peer_address_ =
QuicSocketAddress(QuicIpAddress::Any4(), 65535);
};
namespace {
static const char kPayload[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
class QuicStreamSequencerTest : public QuicTest {
public:
void ConsumeData(size_t num_bytes) {
char buffer[1024];
ASSERT_GT(QUIC_ARRAYSIZE(buffer), num_bytes);
struct iovec iov;
iov.iov_base = buffer;
iov.iov_len = num_bytes;
ASSERT_EQ(static_cast<int>(num_bytes), sequencer_->Readv(&iov, 1));
}
protected:
QuicStreamSequencerTest()
: stream_(), sequencer_(new QuicStreamSequencer(&stream_)) {}
// Verify that the data in first region match with the expected[0].
bool VerifyReadableRegion(const std::vector<std::string>& expected) {
return VerifyReadableRegion(*sequencer_, expected);
}
// Verify that the data in each of currently readable regions match with each
// item given in |expected|.
bool VerifyReadableRegions(const std::vector<std::string>& expected) {
return VerifyReadableRegions(*sequencer_, expected);
}
bool VerifyIovecs(iovec* iovecs,
size_t num_iovecs,
const std::vector<std::string>& expected) {
return VerifyIovecs(*sequencer_, iovecs, num_iovecs, expected);
}
bool VerifyReadableRegion(const QuicStreamSequencer& sequencer,
const std::vector<std::string>& expected) {
iovec iovecs[1];
if (sequencer.GetReadableRegions(iovecs, 1)) {
return (VerifyIovecs(sequencer, iovecs, 1,
std::vector<std::string>{expected[0]}));
}
return false;
}
// Verify that the data in each of currently readable regions match with each
// item given in |expected|.
bool VerifyReadableRegions(const QuicStreamSequencer& sequencer,
const std::vector<std::string>& expected) {
iovec iovecs[5];
size_t num_iovecs =
sequencer.GetReadableRegions(iovecs, QUIC_ARRAYSIZE(iovecs));
return VerifyReadableRegion(sequencer, expected) &&
VerifyIovecs(sequencer, iovecs, num_iovecs, expected);
}
bool VerifyIovecs(const QuicStreamSequencer& /*sequencer*/,
iovec* iovecs,
size_t num_iovecs,
const std::vector<std::string>& expected) {
int start_position = 0;
for (size_t i = 0; i < num_iovecs; ++i) {
if (!VerifyIovec(iovecs[i],
expected[0].substr(start_position, iovecs[i].iov_len))) {
return false;
}
start_position += iovecs[i].iov_len;
}
return true;
}
bool VerifyIovec(const iovec& iovec, QuicStringPiece expected) {
if (iovec.iov_len != expected.length()) {
QUIC_LOG(ERROR) << "Invalid length: " << iovec.iov_len << " vs "
<< expected.length();
return false;
}
if (memcmp(iovec.iov_base, expected.data(), expected.length()) != 0) {
QUIC_LOG(ERROR) << "Invalid data: " << static_cast<char*>(iovec.iov_base)
<< " vs " << expected;
return false;
}
return true;
}
void OnFinFrame(QuicStreamOffset byte_offset, const char* data) {
QuicStreamFrame frame;
frame.stream_id = 1;
frame.offset = byte_offset;
frame.data_buffer = data;
frame.data_length = strlen(data);
frame.fin = true;
sequencer_->OnStreamFrame(frame);
}
void OnFrame(QuicStreamOffset byte_offset, const char* data) {
QuicStreamFrame frame;
frame.stream_id = 1;
frame.offset = byte_offset;
frame.data_buffer = data;
frame.data_length = strlen(data);
frame.fin = false;
sequencer_->OnStreamFrame(frame);
}
size_t NumBufferedBytes() {
return QuicStreamSequencerPeer::GetNumBufferedBytes(sequencer_.get());
}
testing::StrictMock<MockStream> stream_;
std::unique_ptr<QuicStreamSequencer> sequencer_;
};
// TODO(rch): reorder these tests so they build on each other.
TEST_F(QuicStreamSequencerTest, RejectOldFrame) {
EXPECT_CALL(stream_, AddBytesConsumed(3));
EXPECT_CALL(stream_, OnDataAvailable()).WillOnce(testing::Invoke([this]() {
ConsumeData(3);
}));
OnFrame(0, "abc");
EXPECT_EQ(0u, NumBufferedBytes());
EXPECT_EQ(3u, sequencer_->NumBytesConsumed());
// Ignore this - it matches a past packet number and we should not see it
// again.
OnFrame(0, "def");
EXPECT_EQ(0u, NumBufferedBytes());
}
TEST_F(QuicStreamSequencerTest, RejectBufferedFrame) {
EXPECT_CALL(stream_, OnDataAvailable());
OnFrame(0, "abc");
EXPECT_EQ(3u, NumBufferedBytes());
EXPECT_EQ(0u, sequencer_->NumBytesConsumed());
// Ignore this - it matches a buffered frame.
// Right now there's no checking that the payload is consistent.
OnFrame(0, "def");
EXPECT_EQ(3u, NumBufferedBytes());
}
TEST_F(QuicStreamSequencerTest, FullFrameConsumed) {
EXPECT_CALL(stream_, AddBytesConsumed(3));
EXPECT_CALL(stream_, OnDataAvailable()).WillOnce(testing::Invoke([this]() {
ConsumeData(3);
}));
OnFrame(0, "abc");
EXPECT_EQ(0u, NumBufferedBytes());
EXPECT_EQ(3u, sequencer_->NumBytesConsumed());
}
TEST_F(QuicStreamSequencerTest, BlockedThenFullFrameConsumed) {
sequencer_->SetBlockedUntilFlush();
OnFrame(0, "abc");
EXPECT_EQ(3u, NumBufferedBytes());
EXPECT_EQ(0u, sequencer_->NumBytesConsumed());
EXPECT_CALL(stream_, AddBytesConsumed(3));
EXPECT_CALL(stream_, OnDataAvailable()).WillOnce(testing::Invoke([this]() {
ConsumeData(3);
}));
sequencer_->SetUnblocked();
EXPECT_EQ(0u, NumBufferedBytes());
EXPECT_EQ(3u, sequencer_->NumBytesConsumed());
EXPECT_CALL(stream_, AddBytesConsumed(3));
EXPECT_CALL(stream_, OnDataAvailable()).WillOnce(testing::Invoke([this]() {
ConsumeData(3);
}));
EXPECT_FALSE(sequencer_->IsClosed());
OnFinFrame(3, "def");
EXPECT_TRUE(sequencer_->IsClosed());
}
TEST_F(QuicStreamSequencerTest, BlockedThenFullFrameAndFinConsumed) {
sequencer_->SetBlockedUntilFlush();
OnFinFrame(0, "abc");
EXPECT_EQ(3u, NumBufferedBytes());
EXPECT_EQ(0u, sequencer_->NumBytesConsumed());
EXPECT_CALL(stream_, AddBytesConsumed(3));
EXPECT_CALL(stream_, OnDataAvailable()).WillOnce(testing::Invoke([this]() {
ConsumeData(3);
}));
EXPECT_FALSE(sequencer_->IsClosed());
sequencer_->SetUnblocked();
EXPECT_TRUE(sequencer_->IsClosed());
EXPECT_EQ(0u, NumBufferedBytes());
EXPECT_EQ(3u, sequencer_->NumBytesConsumed());
}
TEST_F(QuicStreamSequencerTest, EmptyFrame) {
EXPECT_CALL(stream_,
CloseConnectionWithDetails(QUIC_EMPTY_STREAM_FRAME_NO_FIN, _));
OnFrame(0, "");
EXPECT_EQ(0u, NumBufferedBytes());
EXPECT_EQ(0u, sequencer_->NumBytesConsumed());
}
TEST_F(QuicStreamSequencerTest, EmptyFinFrame) {
EXPECT_CALL(stream_, OnDataAvailable());
OnFinFrame(0, "");
EXPECT_EQ(0u, NumBufferedBytes());
EXPECT_EQ(0u, sequencer_->NumBytesConsumed());
}
TEST_F(QuicStreamSequencerTest, PartialFrameConsumed) {
EXPECT_CALL(stream_, AddBytesConsumed(2));
EXPECT_CALL(stream_, OnDataAvailable()).WillOnce(testing::Invoke([this]() {
ConsumeData(2);
}));
OnFrame(0, "abc");
EXPECT_EQ(1u, NumBufferedBytes());
EXPECT_EQ(2u, sequencer_->NumBytesConsumed());
}
TEST_F(QuicStreamSequencerTest, NextxFrameNotConsumed) {
EXPECT_CALL(stream_, OnDataAvailable());
OnFrame(0, "abc");
EXPECT_EQ(3u, NumBufferedBytes());
EXPECT_EQ(0u, sequencer_->NumBytesConsumed());
}
TEST_F(QuicStreamSequencerTest, FutureFrameNotProcessed) {
OnFrame(3, "abc");
EXPECT_EQ(3u, NumBufferedBytes());
EXPECT_EQ(0u, sequencer_->NumBytesConsumed());
}
TEST_F(QuicStreamSequencerTest, OutOfOrderFrameProcessed) {
// Buffer the first
OnFrame(6, "ghi");
EXPECT_EQ(3u, NumBufferedBytes());
EXPECT_EQ(0u, sequencer_->NumBytesConsumed());
EXPECT_EQ(3u, sequencer_->NumBytesBuffered());
// Buffer the second
OnFrame(3, "def");
EXPECT_EQ(6u, NumBufferedBytes());
EXPECT_EQ(0u, sequencer_->NumBytesConsumed());
EXPECT_EQ(6u, sequencer_->NumBytesBuffered());
EXPECT_CALL(stream_, AddBytesConsumed(9));
EXPECT_CALL(stream_, OnDataAvailable()).WillOnce(testing::Invoke([this]() {
ConsumeData(9);
}));
// Now process all of them at once.
OnFrame(0, "abc");
EXPECT_EQ(9u, sequencer_->NumBytesConsumed());
EXPECT_EQ(0u, sequencer_->NumBytesBuffered());
EXPECT_EQ(0u, NumBufferedBytes());
}
TEST_F(QuicStreamSequencerTest, BasicHalfCloseOrdered) {
InSequence s;
EXPECT_CALL(stream_, OnDataAvailable()).WillOnce(testing::Invoke([this]() {
ConsumeData(3);
}));
EXPECT_CALL(stream_, AddBytesConsumed(3));
OnFinFrame(0, "abc");
EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
}
TEST_F(QuicStreamSequencerTest, BasicHalfCloseUnorderedWithFlush) {
OnFinFrame(6, "");
EXPECT_EQ(6u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
OnFrame(3, "def");
EXPECT_CALL(stream_, AddBytesConsumed(6));
EXPECT_CALL(stream_, OnDataAvailable()).WillOnce(testing::Invoke([this]() {
ConsumeData(6);
}));
EXPECT_FALSE(sequencer_->IsClosed());
OnFrame(0, "abc");
EXPECT_TRUE(sequencer_->IsClosed());
}
TEST_F(QuicStreamSequencerTest, BasicHalfUnordered) {
OnFinFrame(3, "");
EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
EXPECT_CALL(stream_, AddBytesConsumed(3));
EXPECT_CALL(stream_, OnDataAvailable()).WillOnce(testing::Invoke([this]() {
ConsumeData(3);
}));
EXPECT_FALSE(sequencer_->IsClosed());
OnFrame(0, "abc");
EXPECT_TRUE(sequencer_->IsClosed());
}
TEST_F(QuicStreamSequencerTest, TerminateWithReadv) {
char buffer[3];
OnFinFrame(3, "");
EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
EXPECT_FALSE(sequencer_->IsClosed());
EXPECT_CALL(stream_, OnDataAvailable());
OnFrame(0, "abc");
EXPECT_CALL(stream_, AddBytesConsumed(3));
iovec iov = {&buffer[0], 3};
int bytes_read = sequencer_->Readv(&iov, 1);
EXPECT_EQ(3, bytes_read);
EXPECT_TRUE(sequencer_->IsClosed());
}
TEST_F(QuicStreamSequencerTest, MultipleOffsets) {
OnFinFrame(3, "");
EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get()));
if (!GetQuicReloadableFlag(
quic_close_connection_and_discard_data_on_wrong_offset)) {
EXPECT_CALL(stream_, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS));
} else {
EXPECT_CALL(stream_, CloseConnectionWithDetails(
QUIC_STREAM_SEQUENCER_INVALID_STATE,
"Stream 1 received new final offset: 1, which is "
"different from close offset: 3"));
}
OnFinFrame(1, "");
}
class QuicSequencerRandomTest : public QuicStreamSequencerTest {
public:
typedef std::pair<int, std::string> Frame;
typedef std::vector<Frame> FrameList;
void CreateFrames() {
int payload_size = QUIC_ARRAYSIZE(kPayload) - 1;
int remaining_payload = payload_size;
while (remaining_payload != 0) {
int size = std::min(OneToN(6), remaining_payload);
int index = payload_size - remaining_payload;
list_.push_back(
std::make_pair(index, std::string(kPayload + index, size)));
remaining_payload -= size;
}
}
QuicSequencerRandomTest() {
uint64_t seed = QuicRandom::GetInstance()->RandUint64();
QUIC_LOG(INFO) << "**** The current seed is " << seed << " ****";
random_.set_seed(seed);
CreateFrames();
}
int OneToN(int n) { return random_.RandUint64() % n + 1; }
void ReadAvailableData() {
// Read all available data
char output[QUIC_ARRAYSIZE(kPayload) + 1];
iovec iov;
iov.iov_base = output;
iov.iov_len = QUIC_ARRAYSIZE(output);
int bytes_read = sequencer_->Readv(&iov, 1);
EXPECT_NE(0, bytes_read);
output_.append(output, bytes_read);
}
std::string output_;
// Data which peek at using GetReadableRegion if we back up.
std::string peeked_;
SimpleRandom random_;
FrameList list_;
};
// All frames are processed as soon as we have sequential data.
// Infinite buffering, so all frames are acked right away.
TEST_F(QuicSequencerRandomTest, RandomFramesNoDroppingNoBackup) {
EXPECT_CALL(stream_, OnDataAvailable())
.Times(AnyNumber())
.WillRepeatedly(
Invoke(this, &QuicSequencerRandomTest::ReadAvailableData));
QuicByteCount total_bytes_consumed = 0;
EXPECT_CALL(stream_, AddBytesConsumed(_))
.Times(AnyNumber())
.WillRepeatedly(
testing::Invoke([&total_bytes_consumed](QuicByteCount bytes) {
total_bytes_consumed += bytes;
}));
while (!list_.empty()) {
int index = OneToN(list_.size()) - 1;
QUIC_LOG(ERROR) << "Sending index " << index << " " << list_[index].second;
OnFrame(list_[index].first, list_[index].second.data());
list_.erase(list_.begin() + index);
}
ASSERT_EQ(QUIC_ARRAYSIZE(kPayload) - 1, output_.size());
EXPECT_EQ(kPayload, output_);
EXPECT_EQ(QUIC_ARRAYSIZE(kPayload) - 1, total_bytes_consumed);
}
TEST_F(QuicSequencerRandomTest, RandomFramesNoDroppingBackup) {
char buffer[10];
iovec iov[2];
iov[0].iov_base = &buffer[0];
iov[0].iov_len = 5;
iov[1].iov_base = &buffer[5];
iov[1].iov_len = 5;
EXPECT_CALL(stream_, OnDataAvailable()).Times(AnyNumber());
QuicByteCount total_bytes_consumed = 0;
EXPECT_CALL(stream_, AddBytesConsumed(_))
.Times(AnyNumber())
.WillRepeatedly(
testing::Invoke([&total_bytes_consumed](QuicByteCount bytes) {
total_bytes_consumed += bytes;
}));
while (output_.size() != QUIC_ARRAYSIZE(kPayload) - 1) {
if (!list_.empty() && OneToN(2) == 1) { // Send data
int index = OneToN(list_.size()) - 1;
OnFrame(list_[index].first, list_[index].second.data());
list_.erase(list_.begin() + index);
} else { // Read data
bool has_bytes = sequencer_->HasBytesToRead();
iovec peek_iov[20];
int iovs_peeked = sequencer_->GetReadableRegions(peek_iov, 20);
if (has_bytes) {
ASSERT_LT(0, iovs_peeked);
ASSERT_TRUE(sequencer_->GetReadableRegion(peek_iov));
} else {
ASSERT_EQ(0, iovs_peeked);
ASSERT_FALSE(sequencer_->GetReadableRegion(peek_iov));
}
int total_bytes_to_peek = QUIC_ARRAYSIZE(buffer);
for (int i = 0; i < iovs_peeked; ++i) {
int bytes_to_peek =
std::min<int>(peek_iov[i].iov_len, total_bytes_to_peek);
peeked_.append(static_cast<char*>(peek_iov[i].iov_base), bytes_to_peek);
total_bytes_to_peek -= bytes_to_peek;
if (total_bytes_to_peek == 0) {
break;
}
}
int bytes_read = sequencer_->Readv(iov, 2);
output_.append(buffer, bytes_read);
ASSERT_EQ(output_.size(), peeked_.size());
}
}
EXPECT_EQ(std::string(kPayload), output_);
EXPECT_EQ(std::string(kPayload), peeked_);
EXPECT_EQ(QUIC_ARRAYSIZE(kPayload) - 1, total_bytes_consumed);
}
// Same as above, just using a different method for reading.
TEST_F(QuicStreamSequencerTest, MarkConsumed) {
InSequence s;
EXPECT_CALL(stream_, OnDataAvailable());
OnFrame(0, "abc");
OnFrame(3, "def");
OnFrame(6, "ghi");
// abcdefghi buffered.
EXPECT_EQ(9u, sequencer_->NumBytesBuffered());
// Peek into the data.
std::vector<std::string> expected = {"abcdefghi"};
ASSERT_TRUE(VerifyReadableRegions(expected));
// Consume 1 byte.
EXPECT_CALL(stream_, AddBytesConsumed(1));
sequencer_->MarkConsumed(1);
// Verify data.
std::vector<std::string> expected2 = {"bcdefghi"};
ASSERT_TRUE(VerifyReadableRegions(expected2));
EXPECT_EQ(8u, sequencer_->NumBytesBuffered());
// Consume 2 bytes.
EXPECT_CALL(stream_, AddBytesConsumed(2));
sequencer_->MarkConsumed(2);
// Verify data.
std::vector<std::string> expected3 = {"defghi"};
ASSERT_TRUE(VerifyReadableRegions(expected3));
EXPECT_EQ(6u, sequencer_->NumBytesBuffered());
// Consume 5 bytes.
EXPECT_CALL(stream_, AddBytesConsumed(5));
sequencer_->MarkConsumed(5);
// Verify data.
std::vector<std::string> expected4{"i"};
ASSERT_TRUE(VerifyReadableRegions(expected4));
EXPECT_EQ(1u, sequencer_->NumBytesBuffered());
}
TEST_F(QuicStreamSequencerTest, MarkConsumedError) {
EXPECT_CALL(stream_, OnDataAvailable());
OnFrame(0, "abc");
OnFrame(9, "jklmnopqrstuvwxyz");
// Peek into the data. Only the first chunk should be readable because of the
// missing data.
std::vector<std::string> expected{"abc"};
ASSERT_TRUE(VerifyReadableRegions(expected));
// Now, attempt to mark consumed more data than was readable and expect the
// stream to be closed.
EXPECT_CALL(stream_, Reset(QUIC_ERROR_PROCESSING_STREAM));
EXPECT_QUIC_BUG(sequencer_->MarkConsumed(4),
"Invalid argument to MarkConsumed."
" expect to consume: 4, but not enough bytes available.");
}
TEST_F(QuicStreamSequencerTest, MarkConsumedWithMissingPacket) {
InSequence s;
EXPECT_CALL(stream_, OnDataAvailable());
OnFrame(0, "abc");
OnFrame(3, "def");
// Missing packet: 6, ghi.
OnFrame(9, "jkl");
std::vector<std::string> expected = {"abcdef"};
ASSERT_TRUE(VerifyReadableRegions(expected));
EXPECT_CALL(stream_, AddBytesConsumed(6));
sequencer_->MarkConsumed(6);
}
TEST_F(QuicStreamSequencerTest, Move) {
InSequence s;
EXPECT_CALL(stream_, OnDataAvailable());
OnFrame(0, "abc");
OnFrame(3, "def");
OnFrame(6, "ghi");
// abcdefghi buffered.
EXPECT_EQ(9u, sequencer_->NumBytesBuffered());
// Peek into the data.
std::vector<std::string> expected = {"abcdefghi"};
ASSERT_TRUE(VerifyReadableRegions(expected));
QuicStreamSequencer sequencer2(std::move(*sequencer_));
ASSERT_TRUE(VerifyReadableRegions(sequencer2, expected));
}
TEST_F(QuicStreamSequencerTest, OverlappingFramesReceived) {
// The peer should never send us non-identical stream frames which contain
// overlapping byte ranges - if they do, we close the connection.
QuicStreamId id = 1;
QuicStreamFrame frame1(id, false, 1, QuicStringPiece("hello"));
sequencer_->OnStreamFrame(frame1);
QuicStreamFrame frame2(id, false, 2, QuicStringPiece("hello"));
EXPECT_CALL(stream_,
CloseConnectionWithDetails(QUIC_OVERLAPPING_STREAM_DATA, _))
.Times(0);
sequencer_->OnStreamFrame(frame2);
}
TEST_F(QuicStreamSequencerTest, DataAvailableOnOverlappingFrames) {
QuicStreamId id = 1;
const std::string data(1000, '.');
// Received [0, 1000).
QuicStreamFrame frame1(id, false, 0, data);
EXPECT_CALL(stream_, OnDataAvailable());
sequencer_->OnStreamFrame(frame1);
// Consume [0, 500).
EXPECT_CALL(stream_, AddBytesConsumed(500));
QuicStreamSequencerTest::ConsumeData(500);
EXPECT_EQ(500u, sequencer_->NumBytesConsumed());
EXPECT_EQ(500u, sequencer_->NumBytesBuffered());
// Received [500, 1500).
QuicStreamFrame frame2(id, false, 500, data);
// Do not call OnDataAvailable as there are readable bytes left in the buffer.
EXPECT_CALL(stream_, OnDataAvailable()).Times(0);
sequencer_->OnStreamFrame(frame2);
// Consume [1000, 1500).
EXPECT_CALL(stream_, AddBytesConsumed(1000));
QuicStreamSequencerTest::ConsumeData(1000);
EXPECT_EQ(1500u, sequencer_->NumBytesConsumed());
EXPECT_EQ(0u, sequencer_->NumBytesBuffered());
// Received [1498, 1503).
QuicStreamFrame frame3(id, false, 1498, QuicStringPiece("hello"));
EXPECT_CALL(stream_, OnDataAvailable());
sequencer_->OnStreamFrame(frame3);
EXPECT_CALL(stream_, AddBytesConsumed(3));
QuicStreamSequencerTest::ConsumeData(3);
EXPECT_EQ(1503u, sequencer_->NumBytesConsumed());
EXPECT_EQ(0u, sequencer_->NumBytesBuffered());
// Received [1000, 1005).
QuicStreamFrame frame4(id, false, 1000, QuicStringPiece("hello"));
EXPECT_CALL(stream_, OnDataAvailable()).Times(0);
sequencer_->OnStreamFrame(frame4);
EXPECT_EQ(1503u, sequencer_->NumBytesConsumed());
EXPECT_EQ(0u, sequencer_->NumBytesBuffered());
}
TEST_F(QuicStreamSequencerTest, OnDataAvailableWhenReadableBytesIncrease) {
sequencer_->set_level_triggered(true);
QuicStreamId id = 1;
// Received [0, 5).
QuicStreamFrame frame1(id, false, 0, "hello");
EXPECT_CALL(stream_, OnDataAvailable());
sequencer_->OnStreamFrame(frame1);
EXPECT_EQ(5u, sequencer_->NumBytesBuffered());
// Without consuming the buffer bytes, continue receiving [5, 11).
QuicStreamFrame frame2(id, false, 5, " world");
// OnDataAvailable should still be called because there are more data to read.
EXPECT_CALL(stream_, OnDataAvailable());
sequencer_->OnStreamFrame(frame2);
EXPECT_EQ(11u, sequencer_->NumBytesBuffered());
// Without consuming the buffer bytes, continue receiving [12, 13).
QuicStreamFrame frame3(id, false, 5, "a");
// OnDataAvailable shouldn't be called becasue there are still only 11 bytes
// available.
EXPECT_CALL(stream_, OnDataAvailable()).Times(0);
sequencer_->OnStreamFrame(frame3);
EXPECT_EQ(11u, sequencer_->NumBytesBuffered());
}
TEST_F(QuicStreamSequencerTest, ReadSingleFrame) {
EXPECT_CALL(stream_, OnDataAvailable());
OnFrame(0u, "abc");
std::string actual;
EXPECT_CALL(stream_, AddBytesConsumed(3));
sequencer_->Read(&actual);
EXPECT_EQ("abc", actual);
EXPECT_EQ(0u, sequencer_->NumBytesBuffered());
}
TEST_F(QuicStreamSequencerTest, ReadMultipleFramesWithMissingFrame) {
EXPECT_CALL(stream_, OnDataAvailable());
OnFrame(0u, "abc");
OnFrame(3u, "def");
OnFrame(6u, "ghi");
OnFrame(10u, "xyz"); // Byte 9 is missing.
std::string actual;
EXPECT_CALL(stream_, AddBytesConsumed(9));
sequencer_->Read(&actual);
EXPECT_EQ("abcdefghi", actual);
EXPECT_EQ(3u, sequencer_->NumBytesBuffered());
}
TEST_F(QuicStreamSequencerTest, ReadAndAppendToString) {
EXPECT_CALL(stream_, OnDataAvailable());
OnFrame(0u, "def");
OnFrame(3u, "ghi");
std::string actual = "abc";
EXPECT_CALL(stream_, AddBytesConsumed(6));
sequencer_->Read(&actual);
EXPECT_EQ("abcdefghi", actual);
EXPECT_EQ(0u, sequencer_->NumBytesBuffered());
}
TEST_F(QuicStreamSequencerTest, StopReading) {
EXPECT_CALL(stream_, OnDataAvailable()).Times(0);
EXPECT_CALL(stream_, OnFinRead());
EXPECT_CALL(stream_, AddBytesConsumed(0));
sequencer_->StopReading();
EXPECT_CALL(stream_, AddBytesConsumed(3));
OnFrame(0u, "abc");
EXPECT_CALL(stream_, AddBytesConsumed(3));
OnFrame(3u, "def");
EXPECT_CALL(stream_, AddBytesConsumed(3));
OnFinFrame(6u, "ghi");
}
TEST_F(QuicStreamSequencerTest, StopReadingWithLevelTriggered) {
if (GetQuicReloadableFlag(quic_stop_reading_when_level_triggered)) {
EXPECT_CALL(stream_, AddBytesConsumed(0));
EXPECT_CALL(stream_, AddBytesConsumed(3)).Times(3);
EXPECT_CALL(stream_, OnDataAvailable()).Times(0);
EXPECT_CALL(stream_, OnFinRead());
} else {
EXPECT_CALL(stream_, AddBytesConsumed(0));
EXPECT_CALL(stream_, OnDataAvailable()).Times(3);
}
sequencer_->set_level_triggered(true);
sequencer_->StopReading();
OnFrame(0u, "abc");
OnFrame(3u, "def");
OnFinFrame(6u, "ghi");
}
// Regression test for https://crbug.com/992486.
TEST_F(QuicStreamSequencerTest, CorruptFinFrames) {
if (!GetQuicReloadableFlag(
quic_close_connection_and_discard_data_on_wrong_offset)) {
return;
}
EXPECT_CALL(stream_, CloseConnectionWithDetails(
QUIC_STREAM_SEQUENCER_INVALID_STATE,
"Stream 1 received new final offset: 1, which is "
"different from close offset: 2"));
OnFinFrame(2u, "");
OnFinFrame(0u, "a");
EXPECT_FALSE(sequencer_->HasBytesToRead());
}
// Regression test for crbug.com/1015693
TEST_F(QuicStreamSequencerTest, ReceiveFinLessThanHighestOffset) {
if (!GetQuicReloadableFlag(
quic_close_connection_and_discard_data_on_wrong_offset)) {
return;
}
EXPECT_CALL(stream_, OnDataAvailable()).Times(1);
EXPECT_CALL(stream_, CloseConnectionWithDetails(
QUIC_STREAM_SEQUENCER_INVALID_STATE,
"Stream 1 received fin with offset: 0, which "
"reduces current highest offset: 3"));
OnFrame(0u, "abc");
OnFinFrame(0u, "");
}
} // namespace
} // namespace test
} // namespace quic
|