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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
|
// 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/quic/quic_connection.h"
#include "net/base/net_errors.h"
#include "net/quic/congestion_control/quic_receipt_metrics_collector.h"
#include "net/quic/congestion_control/quic_send_scheduler.h"
#include "net/quic/crypto/quic_decrypter.h"
#include "net/quic/crypto/quic_encrypter.h"
#include "net/quic/test_tools/mock_clock.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
//DECLARE_int32(fake_packet_loss_percentage);
using std::map;
using testing::_;
using testing::ContainerEq;
using testing::Return;
namespace net {
// Peer to make public a number of otherwise private QuicConnection methods.
class QuicConnectionPeer {
public:
static void SendAck(QuicConnection* connection) {
connection->SendAck();
}
static void SetCollector(QuicConnection* connection,
QuicReceiptMetricsCollector* collector) {
connection->collector_.reset(collector);
}
static void SetScheduler(QuicConnection* connection,
QuicSendScheduler* scheduler) {
connection->scheduler_.reset(scheduler);
}
DISALLOW_COPY_AND_ASSIGN(QuicConnectionPeer);
};
namespace test {
namespace {
const char data1[] = "foo";
const char data2[] = "bar";
class TestCollector : public QuicReceiptMetricsCollector {
public:
explicit TestCollector(CongestionInfo* info)
: QuicReceiptMetricsCollector(&clock_, kFixRate),
info_(info) {
}
bool GenerateCongestionInfo(CongestionInfo* congestion_info) {
if (info_ == NULL) {
return false;
}
*congestion_info = *info_;
return true;
}
MOCK_METHOD4(RecordIncomingPacket,
void(size_t, QuicPacketSequenceNumber, QuicTime, bool));
private:
MockClock clock_;
CongestionInfo* info_;
DISALLOW_COPY_AND_ASSIGN(TestCollector);
};
class TestConnectionHelper : public QuicConnectionHelperInterface {
public:
TestConnectionHelper(MockClock* clock)
: clock_(clock),
blocked_(false) {
}
// QuicConnectionHelperInterface
virtual void SetConnection(QuicConnection* connection) {}
virtual QuicClock* GetClock() {
return clock_;
}
virtual int WritePacketToWire(const QuicEncryptedPacket& packet,
int* error) {
QuicFramer framer(QuicDecrypter::Create(kNULL),
QuicEncrypter::Create(kNULL));
FramerVisitorCapturingAcks visitor;
framer.set_visitor(&visitor);
EXPECT_TRUE(framer.ProcessPacket(IPEndPoint(), IPEndPoint(), packet));
header_ = *visitor.header();
frame_ = *visitor.frame();
if (blocked_) {
*error = ERR_IO_PENDING;
return -1;
}
return packet.length();
}
virtual void SetResendAlarm(QuicPacketSequenceNumber sequence_number,
QuicTime::Delta delay) {
resend_alarms_[sequence_number] = clock_->Now().Add(delay);
}
virtual void SetSendAlarm(QuicTime::Delta delay) {
send_alarm_ = clock_->Now().Add(delay);
}
virtual void SetTimeoutAlarm(QuicTime::Delta delay) {
timeout_alarm_ = clock_->Now().Add(delay);
}
virtual bool IsSendAlarmSet() {
return send_alarm_ > clock_->Now();
}
virtual void UnregisterSendAlarmIfRegistered() {
send_alarm_ = QuicTime();
}
const map<QuicPacketSequenceNumber, QuicTime>& resend_alarms() const {
return resend_alarms_;
}
QuicTime timeout_alarm() const { return timeout_alarm_; }
QuicPacketHeader* header() { return &header_; }
QuicAckFrame* frame() { return &frame_; }
void set_blocked(bool blocked) { blocked_ = blocked; }
private:
MockClock* clock_;
map<QuicPacketSequenceNumber, QuicTime> resend_alarms_;
QuicTime send_alarm_;
QuicTime timeout_alarm_;
QuicPacketHeader header_;
QuicAckFrame frame_;
bool blocked_;
DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper);
};
class TestConnection : public QuicConnection {
public:
TestConnection(QuicGuid guid,
IPEndPoint address,
TestConnectionHelper* helper)
: QuicConnection(guid, address, helper) {
}
void SendAck() {
QuicConnectionPeer::SendAck(this);
}
void SetCollector(QuicReceiptMetricsCollector* collector) {
QuicConnectionPeer::SetCollector(this, collector);
}
void SetScheduler(QuicSendScheduler* scheduler) {
QuicConnectionPeer::SetScheduler(this, scheduler);
}
bool SendPacket(QuicPacketSequenceNumber sequence_number,
QuicPacket* packet,
bool should_resend,
bool force,
bool is_retransmit) {
return QuicConnection::SendPacket(
sequence_number, packet, should_resend, force, is_retransmit);
}
private:
DISALLOW_COPY_AND_ASSIGN(TestConnection);
};
class QuicConnectionTest : public ::testing::Test {
protected:
QuicConnectionTest()
: guid_(42),
framer_(QuicDecrypter::Create(kNULL), QuicEncrypter::Create(kNULL)),
creator_(guid_, &framer_),
scheduler_(new MockScheduler()),
helper_(new TestConnectionHelper(&clock_)),
connection_(guid_, IPEndPoint(), helper_),
frame1_(1, false, 0, data1),
frame2_(1, false, 3, data2),
accept_packet_(true) {
connection_.set_visitor(&visitor_);
connection_.SetScheduler(scheduler_);
EXPECT_CALL(*scheduler_, TimeUntilSend(_)).WillRepeatedly(Return(
QuicTime::Delta()));
}
QuicAckFrame* last_frame() {
return helper_->frame();
}
QuicPacketHeader* last_header() {
return helper_->header();
}
void ProcessPacket(QuicPacketSequenceNumber number) {
EXPECT_CALL(visitor_, OnPacket(_, _, _, _))
.WillOnce(Return(accept_packet_));
ProcessDataPacket(number, 0);
}
void ProcessFecProtectedPacket(QuicPacketSequenceNumber number,
bool expect_revival) {
if (expect_revival) {
EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).Times(2).WillRepeatedly(
Return(accept_packet_));
} else {
EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce(
Return(accept_packet_));
}
ProcessDataPacket(number, 1);
}
void ProcessDataPacket(QuicPacketSequenceNumber number,
QuicFecGroupNumber fec_group) {
scoped_ptr<QuicPacket> packet(ConstructDataPacket(number, fec_group));
scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(*packet));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
}
// Sends an FEC packet that covers the packets that would have been sent.
void ProcessFecPacket(QuicPacketSequenceNumber number,
QuicPacketSequenceNumber min_protected_packet,
bool expect_revival) {
if (expect_revival) {
EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce(
Return(accept_packet_));
}
// Construct the decrypted data packet so we can compute the correct
// redundancy.
scoped_ptr<QuicPacket> data_packet(ConstructDataPacket(number, 1));
header_.guid = guid_;
header_.packet_sequence_number = number;
header_.flags = PACKET_FLAGS_FEC;
header_.fec_group = 1;
QuicFecData fec_data;
fec_data.min_protected_packet_sequence_number = min_protected_packet;
fec_data.fec_group = 1;
// Since all data packets in this test have the same payload, the
// redundancy is either equal to that payload or the xor of that payload
// with itself, depending on the number of packets.
if (((number - min_protected_packet) % 2) == 0) {
for (size_t i = kStartOfFecProtectedData; i < data_packet->length();
++i) {
data_packet->mutable_data()[i] ^= data_packet->data()[i];
}
}
fec_data.redundancy = data_packet->FecProtectedData();
QuicPacket* fec_packet;
framer_.ConstructFecPacket(header_, fec_data, &fec_packet);
scoped_ptr<QuicEncryptedPacket> encrypted(
framer_.EncryptPacket(*fec_packet));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
delete fec_packet;
}
void SendAckPacket(QuicAckFrame* frame) {
scoped_ptr<QuicPacket> packet(creator_.AckPacket(frame).second);
scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(*packet));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
}
void SendAckPacket(QuicPacketSequenceNumber least_unacked) {
QuicAckFrame frame(0, QuicTime(), least_unacked);
SendAckPacket(&frame);
}
bool IsMissing(QuicPacketSequenceNumber number) {
return last_frame()->received_info.missing_packets.find(number) !=
last_frame()->received_info.missing_packets.end();
}
size_t NonRetransmittingSize() {
return last_frame()->sent_info.non_retransmiting.size();
}
bool NonRetransmitting(QuicPacketSequenceNumber number) {
return last_frame()->sent_info.non_retransmiting.find(number) !=
last_frame()->sent_info.non_retransmiting.end();
}
QuicPacket* ConstructDataPacket(QuicPacketSequenceNumber number,
QuicFecGroupNumber fec_group) {
header_.guid = guid_;
header_.packet_sequence_number = number;
header_.flags = PACKET_FLAGS_NONE;
header_.fec_group = fec_group;
QuicFrames frames;
QuicFrame frame(&frame1_);
frames.push_back(frame);
QuicPacket* packet = NULL;
EXPECT_TRUE(framer_.ConstructFrameDataPacket(header_, frames, &packet));
return packet;
}
QuicGuid guid_;
QuicFramer framer_;
QuicPacketCreator creator_;
MockScheduler* scheduler_;
MockClock clock_;
TestConnectionHelper* helper_;
TestConnection connection_;
testing::StrictMock<MockConnectionVisitor> visitor_;
QuicPacketHeader header_;
QuicStreamFrame frame1_;
QuicStreamFrame frame2_;
bool accept_packet_;
private:
DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest);
};
TEST_F(QuicConnectionTest, PacketsInOrder) {
ProcessPacket(1);
EXPECT_EQ(1u, last_frame()->received_info.largest_received);
EXPECT_EQ(0u, last_frame()->received_info.missing_packets.size());
ProcessPacket(2);
EXPECT_EQ(2u, last_frame()->received_info.largest_received);
EXPECT_EQ(0u, last_frame()->received_info.missing_packets.size());
ProcessPacket(3);
EXPECT_EQ(3u, last_frame()->received_info.largest_received);
EXPECT_EQ(0u, last_frame()->received_info.missing_packets.size());
}
TEST_F(QuicConnectionTest, PacketsRejected) {
ProcessPacket(1);
EXPECT_EQ(1u, last_frame()->received_info.largest_received);
EXPECT_EQ(0u, last_frame()->received_info.missing_packets.size());
accept_packet_ = false;
ProcessPacket(2);
// We should not have an ack for two.
EXPECT_EQ(1u, last_frame()->received_info.largest_received);
EXPECT_EQ(0u, last_frame()->received_info.missing_packets.size());
}
TEST_F(QuicConnectionTest, PacketsOutOfOrder) {
ProcessPacket(3);
EXPECT_EQ(3u, last_frame()->received_info.largest_received);
EXPECT_EQ(2u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
ProcessPacket(2);
EXPECT_EQ(3u, last_frame()->received_info.largest_received);
EXPECT_EQ(1u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(1));
ProcessPacket(1);
EXPECT_EQ(3u, last_frame()->received_info.largest_received);
EXPECT_EQ(0u, last_frame()->received_info.missing_packets.size());
}
TEST_F(QuicConnectionTest, DuplicatePacket) {
ProcessPacket(3);
EXPECT_EQ(3u, last_frame()->received_info.largest_received);
EXPECT_EQ(2u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
// Send packet 3 again, but do not set the expectation that
// the visitor OnPacket() will be called.
ProcessDataPacket(3, 0);
EXPECT_EQ(3u, last_frame()->received_info.largest_received);
EXPECT_EQ(2u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
}
TEST_F(QuicConnectionTest, LatePacketMarkedWillNotResend) {
ProcessPacket(5);
// Now send non-resending information, that we're not going to resend 3.
// The far end should stop waiting for it.
QuicPacketSequenceNumber largest_received = 0;
QuicTime time_received;
QuicPacketSequenceNumber least_unacked = 1;
QuicAckFrame frame(largest_received, time_received, least_unacked);
frame.sent_info.non_retransmiting.insert(3);
SendAckPacket(&frame);
// Force an ack to be sent.
connection_.SendAck();
EXPECT_EQ(5u, last_frame()->received_info.largest_received);
EXPECT_EQ(2u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(4));
EXPECT_TRUE(IsMissing(2));
// Send packet 3 again, but do not set the expectation that
// the visitor OnPacket() will be called.
ProcessDataPacket(3, 0);
connection_.SendAck();
EXPECT_EQ(5u, last_frame()->received_info.largest_received);
EXPECT_EQ(2u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(4));
EXPECT_TRUE(IsMissing(2));
}
TEST_F(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndNonResend) {
ProcessPacket(3);
EXPECT_EQ(3u, last_frame()->received_info.largest_received);
EXPECT_EQ(2u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
ProcessPacket(2);
EXPECT_EQ(3u, last_frame()->received_info.largest_received);
EXPECT_EQ(1u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(1));
ProcessPacket(6);
EXPECT_EQ(6u, last_frame()->received_info.largest_received);
EXPECT_EQ(3u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(1));
EXPECT_TRUE(IsMissing(4));
EXPECT_TRUE(IsMissing(5));
// Now send non-resending information, that we're not going to resend 4.
// The far end should stop waiting for it.
// In sending the ack, we also have sent packet 1, so we'll stop waiting for
// that as well.
QuicAckFrame frame(0, QuicTime(), 1);
frame.sent_info.non_retransmiting.insert(4);
SendAckPacket(&frame);
// Force an ack to be sent.
connection_.SendAck();
EXPECT_EQ(1u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(5));
}
TEST_F(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndLeastAwaiting) {
ProcessPacket(3);
EXPECT_EQ(3u, last_frame()->received_info.largest_received);
EXPECT_EQ(2u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
ProcessPacket(2);
EXPECT_EQ(3u, last_frame()->received_info.largest_received);
EXPECT_EQ(1u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(1));
ProcessPacket(5);
EXPECT_EQ(5u, last_frame()->received_info.largest_received);
EXPECT_EQ(2u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(1));
EXPECT_TRUE(IsMissing(4));
// Pretend at this point the client has gotten acks for 2 and 3 and 1 is a
// packet the peer will not retransmit. It indicates this by sending 'least
// awaiting' is 4. The connection should then realize 1 will not be
// retransmitted, and will remove it from the missing list.
QuicAckFrame frame(0, QuicTime(), 4);
SendAckPacket(&frame);
// Force an ack to be sent.
connection_.SendAck();
EXPECT_EQ(1u, last_frame()->received_info.missing_packets.size());
EXPECT_TRUE(IsMissing(4));
}
TEST_F(QuicConnectionTest, RejectPacketTooFarOut) {
// Call ProcessDataPacket rather than ProcessPacket, as we should not get a
// packet call to the visitor.
ProcessDataPacket(6000, 0);;
connection_.SendAck(); // Packet 2
EXPECT_EQ(0u, last_frame()->received_info.largest_received);
}
TEST_F(QuicConnectionTest, LeastUnackedLower) {
connection_.SendStreamData(1, "foo", 0, false, NULL);
connection_.SendStreamData(1, "bar", 3, false, NULL);
connection_.SendStreamData(1, "eep", 6, false, NULL);
// Start out saying the least unacked is 2
creator_.set_sequence_number(5);
QuicAckFrame frame(0, QuicTime(), 2);
SendAckPacket(&frame);
// Change it to 1, but lower the sequence number to fake out-of-order packets.
// This should be fine.
creator_.set_sequence_number(1);
QuicAckFrame frame2(0, QuicTime(), 1);
SendAckPacket(&frame2);
// Now claim it's one, but set the ordering so it was sent "after" the first
// one. This should cause a connection error.
EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false));
creator_.set_sequence_number(7);
SendAckPacket(&frame2);
}
TEST_F(QuicConnectionTest, AckUnsentData) {
// Ack a packet which has not been sent.
EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false));
QuicAckFrame frame(1, QuicTime(), 0);
SendAckPacket(&frame);
}
TEST_F(QuicConnectionTest, AckAll) {
ProcessPacket(1);
creator_.set_sequence_number(1);
QuicAckFrame frame1(1, QuicTime(), 1);
SendAckPacket(&frame1);
// Send an ack with least_unacked == 0, which indicates that all packets
// we have sent have been acked.
QuicAckFrame frame2(1, QuicTime(), 0);
SendAckPacket(&frame2);
}
// This test is meant to validate that we can't overwhelm the far end with a ton
// of missing packets.
// We will likely fix the protocol to allow more than 190 in flight, and the
// test will need to be adjusted accordingly.
TEST_F(QuicConnectionTest, TooManyMissing) {
connection_.SendStreamData(1, "foo", 0, false, NULL);
EXPECT_CALL(visitor_, ConnectionClose(QUIC_PACKET_TOO_LARGE, false));
QuicAckFrame frame(1, QuicTime(), 0);
for (int i = 0; i < 5001; ++i) {
frame.received_info.missing_packets.insert(i);
}
SendAckPacket(&frame);
}
// See comment for TooManyMissing above.
TEST_F(QuicConnectionTest, TooManyNonRetransmitting) {
connection_.SendStreamData(1, "foo", 0, false, NULL);
EXPECT_CALL(visitor_, ConnectionClose(QUIC_PACKET_TOO_LARGE, false));
QuicAckFrame frame(1, QuicTime(), 0);
for (int i = 0; i < 5001; ++i) {
frame.sent_info.non_retransmiting.insert(i);
}
SendAckPacket(&frame);
}
TEST_F(QuicConnectionTest, BasicSending) {
QuicPacketSequenceNumber last_packet;
connection_.SendStreamData(1, "foo", 0, false, &last_packet); // Packet 1
EXPECT_EQ(1u, last_packet);
connection_.SendAck(); // Packet 2
EXPECT_EQ(1u, last_frame()->sent_info.least_unacked);
connection_.SendAck(); // Packet 3
EXPECT_EQ(1u, last_frame()->sent_info.least_unacked);
EXPECT_EQ(1u, NonRetransmittingSize());
EXPECT_TRUE(NonRetransmitting(2));
connection_.SendStreamData(1, "bar", 3, false, &last_packet); // Packet 4
EXPECT_EQ(4u, last_packet);
connection_.SendAck(); // Packet 5
EXPECT_EQ(1u, last_frame()->sent_info.least_unacked);
EXPECT_EQ(2u, NonRetransmittingSize());
EXPECT_TRUE(NonRetransmitting(2));
EXPECT_TRUE(NonRetransmitting(3));
QuicConnectionVisitorInterface::AckedPackets expected_acks;
expected_acks.insert(1);
// Client acks up to packet 3
EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
QuicAckFrame frame(3, QuicTime(), 0);
SendAckPacket(&frame);
connection_.SendAck(); // Packet 6
// As soon as we've acked one, we skip ack packets 2 and 3 and note lack of
// ack for 4.
EXPECT_EQ(4u, last_frame()->sent_info.least_unacked);
EXPECT_EQ(1u, NonRetransmittingSize());
EXPECT_TRUE(NonRetransmitting(5));
expected_acks.clear();
expected_acks.insert(4);
// Client acks up to packet 4, the last packet
EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
QuicAckFrame frame2(6, QuicTime(), 0);
SendAckPacket(&frame2);
connection_.SendAck(); // Packet 7
// The least packet awaiting ack should now be the special value of 0
EXPECT_EQ(0u, last_frame()->sent_info.least_unacked);
EXPECT_EQ(0u, NonRetransmittingSize());
// If we force an ack, we shouldn't change our retransmit state.
connection_.SendAck(); // Packet 8
EXPECT_EQ(0u, last_frame()->sent_info.least_unacked);
EXPECT_EQ(0u, NonRetransmittingSize());
// But if we send more data it should.
connection_.SendStreamData(1, "eep", 6, false, &last_packet); // Packet 9
EXPECT_EQ(9u, last_packet);
connection_.SendAck(); // Packet10
EXPECT_EQ(9u, last_frame()->sent_info.least_unacked);
}
// Test sending multiple acks from the connection to the session.
TEST_F(QuicConnectionTest, MultipleAcks) {
QuicPacketSequenceNumber last_packet;
connection_.SendStreamData(1, "foo", 0, false, &last_packet); // Packet 1
EXPECT_EQ(1u, last_packet);
connection_.SendStreamData(3, "foo", 0, false, &last_packet); // Packet 2
EXPECT_EQ(2u, last_packet);
connection_.SendAck(); // Packet 3
connection_.SendStreamData(5, "foo", 0, false, &last_packet); // Packet 4
EXPECT_EQ(4u, last_packet);
connection_.SendStreamData(1, "foo", 3, false, &last_packet); // Packet 5
EXPECT_EQ(5u, last_packet);
connection_.SendStreamData(3, "foo", 3, false, &last_packet); // Packet 6
EXPECT_EQ(6u, last_packet);
// Client will acks packets 1, [!2], 3, 4, 5
QuicAckFrame frame1(5, QuicTime(), 0);
frame1.received_info.missing_packets.insert(2);
// The connection should pass up acks for 1, 4, 5. 2 is not acked, and 3 was
// an ackframe so should not be passed up.
QuicConnectionVisitorInterface::AckedPackets expected_acks;
expected_acks.insert(1);
expected_acks.insert(4);
expected_acks.insert(5);
EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
SendAckPacket(&frame1);
// Now the client implicitly acks 2, and explicitly acks 6
QuicAckFrame frame2(6, QuicTime(), 0);
expected_acks.clear();
// Both acks should be passed up.
expected_acks.insert(2);
expected_acks.insert(6);
EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
SendAckPacket(&frame2);
}
TEST_F(QuicConnectionTest, ReviveMissingPacketAfterFecPacket) {
// Don't send missing packet 1.
ProcessFecPacket(2, 1, true);
}
TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacketThenFecPacket) {
ProcessFecProtectedPacket(1, false);
// Don't send missing packet 2.
ProcessFecPacket(3, 1, true);
}
TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacketsThenFecPacket) {
ProcessFecProtectedPacket(1, false);
// Don't send missing packet 2.
ProcessFecProtectedPacket(3, false);
ProcessFecPacket(4, 1, true);
}
TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacket) {
// Don't send missing packet 1.
ProcessFecPacket(3, 1, false); // out of order
ProcessFecProtectedPacket(2, true);
}
TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPackets) {
ProcessFecProtectedPacket(1, false);
// Don't send missing packet 2.
ProcessFecPacket(6, 1, false);
ProcessFecProtectedPacket(3, false);
ProcessFecProtectedPacket(4, false);
ProcessFecProtectedPacket(5, true);
}
TEST_F(QuicConnectionTest, TestResend) {
// TODO(rch): make this work
// FLAGS_fake_packet_loss_percentage = 100;
const QuicTime::Delta kDefaultResendTime =
QuicTime::Delta::FromMilliseconds(500);
QuicTime default_resend_time = clock_.Now().Add(kDefaultResendTime);
connection_.SendStreamData(1, "foo", 0, false, NULL);
EXPECT_EQ(1u, last_header()->packet_sequence_number);
EXPECT_EQ(1u, helper_->resend_alarms().size());
EXPECT_EQ(default_resend_time,
helper_->resend_alarms().find(1)->second);
// Simulate the resend alarm firing
clock_.AdvanceTime(kDefaultResendTime);
connection_.MaybeResendPacket(1);
EXPECT_EQ(2u, last_header()->packet_sequence_number);
}
// TODO(rch): Enable after we get non-blocking sockets.
TEST_F(QuicConnectionTest, DISABLED_TestQueued) {
EXPECT_EQ(0u, connection_.NumQueuedPackets());
helper_->set_blocked(true);
connection_.SendStreamData(1, "foo", 0, false, NULL);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
// Attempt to send all packets, but since we're actually still
// blocked, they should all remain queued.
EXPECT_FALSE(connection_.OnCanWrite());
EXPECT_EQ(1u, connection_.NumQueuedPackets());
// Unblock the writes and actually send.
helper_->set_blocked(false);
EXPECT_TRUE(connection_.OnCanWrite());
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_F(QuicConnectionTest, CloseFecGroup) {
// Don't send missing packet 1
// Don't send missing packet 2
ProcessFecProtectedPacket(3, false);
// Don't send missing FEC packet 3
ASSERT_EQ(1u, connection_.NumFecGroups());
// Now send non-fec protected ack packet and close the group
SendAckPacket(5);
ASSERT_EQ(0u, connection_.NumFecGroups());
}
TEST_F(QuicConnectionTest, NoCongestionInfo) {
TestCollector* collector(new TestCollector(NULL));
connection_.SetCollector(collector);
connection_.SendAck();
EXPECT_EQ(kNone, last_frame()->congestion_info.type);
}
TEST_F(QuicConnectionTest, WithCongestionInfo) {
CongestionInfo info;
info.type = kFixRate;
info.fix_rate.bitrate_in_bytes_per_second = 123;
TestCollector* collector(new TestCollector(&info));
connection_.SetCollector(collector);
connection_.SendAck();
EXPECT_EQ(kFixRate, last_frame()->congestion_info.type);
EXPECT_EQ(info.fix_rate.bitrate_in_bytes_per_second,
last_frame()->congestion_info.fix_rate.bitrate_in_bytes_per_second);
}
TEST_F(QuicConnectionTest, UpdateCongestionInfo) {
TestCollector* collector(new TestCollector(NULL));
connection_.SetCollector(collector);
connection_.SendAck();
EXPECT_CALL(*collector, RecordIncomingPacket(_, _, _, _));
ProcessPacket(1);
}
TEST_F(QuicConnectionTest, DontUpdateCongestionInfoForRevived) {
TestCollector* collector(new TestCollector(NULL));
connection_.SetCollector(collector);
connection_.SendAck();
// Process an FEC packet, and revive the missing data packet
// but only contact the collector once.
EXPECT_CALL(*collector, RecordIncomingPacket(_, _, _, _));
ProcessFecPacket(2, 1, true);
}
TEST_F(QuicConnectionTest, InitialTimeout) {
EXPECT_TRUE(connection_.connected());
EXPECT_CALL(visitor_, ConnectionClose(QUIC_CONNECTION_TIMED_OUT, false));
QuicTime default_timeout = clock_.Now().Add(
QuicTime::Delta::FromMicroseconds(kDefaultTimeoutUs));
EXPECT_EQ(default_timeout, helper_->timeout_alarm());
// Simulate the timeout alarm firing
clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(kDefaultTimeoutUs));
EXPECT_TRUE(connection_.CheckForTimeout());
EXPECT_FALSE(connection_.connected());
}
TEST_F(QuicConnectionTest, TimeoutAfterSend) {
EXPECT_TRUE(connection_.connected());
QuicTime default_timeout = clock_.Now().Add(
QuicTime::Delta::FromMicroseconds(kDefaultTimeoutUs));
// When we send a packet, the timeout will change to 5000 + kDefaultTimeout.
clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
// Send an ack so we don't set the resend alarm.
connection_.SendAck();
EXPECT_EQ(default_timeout, helper_->timeout_alarm());
// The original alarm will fire. We should not time out because we had a
// network event at t=5000. The alarm will reregister.
clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(
kDefaultTimeoutUs - 5000));
EXPECT_EQ(default_timeout, clock_.Now());
EXPECT_FALSE(connection_.CheckForTimeout());
EXPECT_TRUE(connection_.connected());
EXPECT_EQ(default_timeout.Add(QuicTime::Delta::FromMilliseconds(5)),
helper_->timeout_alarm());
// This time, we should time out.
EXPECT_CALL(visitor_, ConnectionClose(QUIC_CONNECTION_TIMED_OUT, false));
clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
EXPECT_EQ(default_timeout.Add(QuicTime::Delta::FromMilliseconds(5)),
clock_.Now());
EXPECT_TRUE(connection_.CheckForTimeout());
EXPECT_FALSE(connection_.connected());
}
// TODO(ianswett): Add scheduler tests when resend is false.
TEST_F(QuicConnectionTest, SendScheduler) {
// Test that if we send a packet without delay, it is not queued.
scoped_ptr<QuicPacket> packet(ConstructDataPacket(1, 0));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta()));
connection_.SendPacket(1, packet.get(), true, false, false);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_F(QuicConnectionTest, SendSchedulerDelay) {
// Test that if we send a packet with a delay, it ends up queued.
scoped_ptr<QuicPacket> packet(ConstructDataPacket(1, 0));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta::FromMicroseconds(1)));
EXPECT_CALL(*scheduler_, SentPacket(1, _, _)).Times(0);
connection_.SendPacket(1, packet.get(), true, false, false);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
}
TEST_F(QuicConnectionTest, SendSchedulerForce) {
// Test that if we force send a packet, it is not queued.
scoped_ptr<QuicPacket> packet(ConstructDataPacket(1, 0));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).Times(0);
connection_.SendPacket(1, packet.get(), true, true, false);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
// TODO(rch): Enable after we get non-blocking sockets.
TEST_F(QuicConnectionTest, DISABLED_SendSchedulerEAGAIN) {
scoped_ptr<QuicPacket> packet(ConstructDataPacket(1, 0));
helper_->set_blocked(true);
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta()));
EXPECT_CALL(*scheduler_, SentPacket(1, _, _)).Times(0);
connection_.SendPacket(1, packet.get(), true, false, false);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
}
TEST_F(QuicConnectionTest, SendSchedulerDelayThenSend) {
// Test that if we send a packet with a delay, it ends up queued.
scoped_ptr<QuicPacket> packet(ConstructDataPacket(1, 0));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta::FromMicroseconds(1)));
connection_.SendPacket(1, packet.get(), true, false, false);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
// Advance the clock to fire the alarm, and configure the scheduler
// to permit the packet to be sent.
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta()));
clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1));
connection_.OnCanWrite();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_F(QuicConnectionTest, SendSchedulerDelayThenRetransmit) {
// Test that if we send a retransmit with a delay, it ends up queued.
scoped_ptr<QuicPacket> packet(ConstructDataPacket(1, 0));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta::FromMicroseconds(1)));
connection_.SendPacket(1, packet.get(), true, false, true);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
// Advance the clock to fire the alarm, and configure the scheduler
// to permit the packet to be sent.
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta()));
// Ensure the scheduler is notified this is a retransmit.
EXPECT_CALL(*scheduler_, SentPacket(1, _, true));
clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1));
connection_.OnCanWrite();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_F(QuicConnectionTest, SendSchedulerDelayAndQueue) {
scoped_ptr<QuicPacket> packet(ConstructDataPacket(1, 0));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta::FromMicroseconds(1)));
connection_.SendPacket(1, packet.get(), true, false, false);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
// Attempt to send another packet and make sure that it gets queued.
connection_.SendPacket(2, packet.get(), true, false, false);
EXPECT_EQ(2u, connection_.NumQueuedPackets());
}
TEST_F(QuicConnectionTest, SendSchedulerDelayThenAckAndSend) {
scoped_ptr<QuicPacket> packet(ConstructDataPacket(1, 0));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta::FromMicroseconds(10)));
connection_.SendPacket(1, packet.get(), true, false, false);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
// Now send non-retransmitting information, that we're not going to resend 3.
// The far end should stop waiting for it.
QuicAckFrame frame(0, QuicTime(), 1);
frame.sent_info.non_retransmiting.insert(3);
EXPECT_CALL(*scheduler_, OnIncomingAckFrame(testing::_));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillRepeatedly(testing::Return(
QuicTime::Delta()));
SendAckPacket(&frame);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
// Ensure alarm is not set
EXPECT_FALSE(helper_->IsSendAlarmSet());
}
TEST_F(QuicConnectionTest, SendSchedulerDelayThenAckAndHold) {
scoped_ptr<QuicPacket> packet(ConstructDataPacket(1, 0));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta::FromMicroseconds(10)));
connection_.SendPacket(1, packet.get(), true, false, false);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
// Now send non-resending information, that we're not going to resend 3.
// The far end should stop waiting for it.
QuicAckFrame frame(0, QuicTime(), 1);
frame.sent_info.non_retransmiting.insert(3);
EXPECT_CALL(*scheduler_, OnIncomingAckFrame(testing::_));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta::FromMicroseconds(1)));
SendAckPacket(&frame);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
}
TEST_F(QuicConnectionTest, SendSchedulerDelayThenOnCanWrite) {
scoped_ptr<QuicPacket> packet(ConstructDataPacket(1, 0));
EXPECT_CALL(*scheduler_, TimeUntilSend(true)).WillOnce(testing::Return(
QuicTime::Delta::FromMicroseconds(10)));
connection_.SendPacket(1, packet.get(), true, false, false);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
// OnCanWrite should not send the packet (because of the delay)
// but should still return true.
EXPECT_TRUE(connection_.OnCanWrite());
EXPECT_EQ(1u, connection_.NumQueuedPackets());
}
} // namespace
} // namespace test
} // namespace net
|