Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / net / quic / quic_connection_test.cc
blobe4b44009bd66413f7fa99f19034ab4c5452a1ec6
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_connection.h"
7 #include "base/bind.h"
8 #include "net/base/net_errors.h"
9 #include "net/quic/congestion_control/receive_algorithm_interface.h"
10 #include "net/quic/congestion_control/send_algorithm_interface.h"
11 #include "net/quic/crypto/null_encrypter.h"
12 #include "net/quic/crypto/quic_decrypter.h"
13 #include "net/quic/crypto/quic_encrypter.h"
14 #include "net/quic/crypto/quic_random.h"
15 #include "net/quic/test_tools/mock_clock.h"
16 #include "net/quic/test_tools/mock_random.h"
17 #include "net/quic/test_tools/quic_connection_peer.h"
18 #include "net/quic/test_tools/quic_framer_peer.h"
19 #include "net/quic/test_tools/quic_packet_creator_peer.h"
20 #include "net/quic/test_tools/quic_test_utils.h"
21 #include "net/quic/quic_utils.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using base::StringPiece;
26 using std::map;
27 using std::vector;
28 using testing::_;
29 using testing::AnyNumber;
30 using testing::Between;
31 using testing::ContainerEq;
32 using testing::DoAll;
33 using testing::InSequence;
34 using testing::InvokeWithoutArgs;
35 using testing::Return;
36 using testing::StrictMock;
37 using testing::SaveArg;
39 namespace net {
40 namespace test {
41 namespace {
43 const char data1[] = "foo";
44 const char data2[] = "bar";
46 const bool kFin = true;
47 const bool kEntropyFlag = true;
48 const bool kFecEntropyFlag = true;
49 const QuicPacketEntropyHash kTestEntropyHash = 76;
51 class TestReceiveAlgorithm : public ReceiveAlgorithmInterface {
52 public:
53 explicit TestReceiveAlgorithm(QuicCongestionFeedbackFrame* feedback)
54 : feedback_(feedback) {
57 bool GenerateCongestionFeedback(
58 QuicCongestionFeedbackFrame* congestion_feedback) {
59 if (feedback_ == NULL) {
60 return false;
62 *congestion_feedback = *feedback_;
63 return true;
66 MOCK_METHOD4(RecordIncomingPacket,
67 void(QuicByteCount, QuicPacketSequenceNumber, QuicTime, bool));
69 private:
70 QuicCongestionFeedbackFrame* feedback_;
72 DISALLOW_COPY_AND_ASSIGN(TestReceiveAlgorithm);
75 class TestConnectionHelper : public QuicConnectionHelperInterface {
76 public:
77 TestConnectionHelper(MockClock* clock, MockRandom* random_generator)
78 : clock_(clock),
79 random_generator_(random_generator),
80 retransmission_alarm_(QuicTime::Zero()),
81 send_alarm_(QuicTime::Zero().Subtract(
82 QuicTime::Delta::FromMilliseconds(1))),
83 timeout_alarm_(QuicTime::Zero()),
84 blocked_(false),
85 is_server_(true) {
88 // QuicConnectionHelperInterface
89 virtual void SetConnection(QuicConnection* connection) OVERRIDE {}
91 virtual const QuicClock* GetClock() const OVERRIDE {
92 return clock_;
95 virtual QuicRandom* GetRandomGenerator() OVERRIDE {
96 return random_generator_;
99 virtual int WritePacketToWire(const QuicEncryptedPacket& packet,
100 int* error) OVERRIDE {
101 QuicFramer framer(kQuicVersion1,
102 QuicDecrypter::Create(kNULL),
103 QuicEncrypter::Create(kNULL),
104 QuicTime::Zero(),
105 is_server_);
106 FramerVisitorCapturingFrames visitor;
107 framer.set_visitor(&visitor);
108 EXPECT_TRUE(framer.ProcessPacket(packet));
109 header_ = *visitor.header();
110 frame_count_ = visitor.frame_count();
111 if (visitor.ack()) {
112 ack_.reset(new QuicAckFrame(*visitor.ack()));
114 if (visitor.feedback()) {
115 feedback_.reset(new QuicCongestionFeedbackFrame(*visitor.feedback()));
117 if (visitor.stream_frames() != NULL && !visitor.stream_frames()->empty()) {
118 stream_frames_ = *visitor.stream_frames();
120 if (visitor.version_negotiation_packet() != NULL) {
121 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(
122 *visitor.version_negotiation_packet()));
124 if (blocked_) {
125 *error = ERR_IO_PENDING;
126 return -1;
128 *error = 0;
129 last_packet_size_ = packet.length();
130 return last_packet_size_;
133 virtual void SetRetransmissionAlarm(QuicTime::Delta delay) OVERRIDE {
134 retransmission_alarm_ = clock_->ApproximateNow().Add(delay);
137 virtual bool IsWriteBlockedDataBuffered() OVERRIDE {
138 return false;
141 virtual bool IsWriteBlocked(int error) OVERRIDE {
142 return error == ERR_IO_PENDING;
145 virtual void SetSendAlarm(QuicTime alarm_time) OVERRIDE {
146 send_alarm_ = alarm_time;
149 virtual void SetTimeoutAlarm(QuicTime::Delta delay) OVERRIDE {
150 timeout_alarm_ = clock_->ApproximateNow().Add(delay);
153 virtual bool IsSendAlarmSet() OVERRIDE {
154 return send_alarm_ >= clock_->ApproximateNow();
157 virtual void UnregisterSendAlarmIfRegistered() OVERRIDE {
158 send_alarm_ =
159 QuicTime::Zero().Subtract(QuicTime::Delta::FromMilliseconds(1));
162 virtual void SetAckAlarm(QuicTime::Delta delay) OVERRIDE {}
163 virtual void ClearAckAlarm() OVERRIDE {}
165 QuicTime retransmission_alarm() const {
166 return retransmission_alarm_;
169 QuicTime timeout_alarm() const { return timeout_alarm_; }
171 QuicPacketHeader* header() { return &header_; }
173 size_t frame_count() const { return frame_count_; }
175 QuicAckFrame* ack() { return ack_.get(); }
177 QuicCongestionFeedbackFrame* feedback() { return feedback_.get(); }
179 const vector<QuicStreamFrame>* stream_frames() const {
180 return &stream_frames_;
183 size_t last_packet_size() {
184 return last_packet_size_;
187 QuicVersionNegotiationPacket* version_negotiation_packet() {
188 return version_negotiation_packet_.get();
191 void set_blocked(bool blocked) { blocked_ = blocked; }
193 void set_is_server(bool is_server) { is_server_ = is_server; }
195 private:
196 MockClock* clock_;
197 MockRandom* random_generator_;
198 QuicTime retransmission_alarm_;
199 QuicTime send_alarm_;
200 QuicTime timeout_alarm_;
201 QuicPacketHeader header_;
202 size_t frame_count_;
203 scoped_ptr<QuicAckFrame> ack_;
204 scoped_ptr<QuicCongestionFeedbackFrame> feedback_;
205 vector<QuicStreamFrame> stream_frames_;
206 scoped_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_;
207 size_t last_packet_size_;
208 bool blocked_;
209 bool is_server_;
211 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper);
214 class TestConnection : public QuicConnection {
215 public:
216 TestConnection(QuicGuid guid,
217 IPEndPoint address,
218 TestConnectionHelper* helper,
219 bool is_server)
220 : QuicConnection(guid, address, helper, is_server),
221 helper_(helper) {
222 helper_->set_is_server(!is_server);
225 void SendAck() {
226 QuicConnectionPeer::SendAck(this);
229 void SetReceiveAlgorithm(TestReceiveAlgorithm* receive_algorithm) {
230 QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm);
233 void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) {
234 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm);
237 QuicConsumedData SendStreamData1() {
238 return SendStreamData(1u, "food", 0, !kFin);
241 QuicConsumedData SendStreamData2() {
242 return SendStreamData(2u, "food2", 0, !kFin);
245 bool is_server() {
246 return QuicConnectionPeer::IsServer(this);
249 void set_is_server(bool is_server) {
250 helper_->set_is_server(!is_server);
251 QuicPacketCreatorPeer::SetIsServer(
252 QuicConnectionPeer::GetPacketCreator(this), is_server);
253 QuicConnectionPeer::SetIsServer(this, is_server);
256 using QuicConnection::SendOrQueuePacket;
257 using QuicConnection::DontWaitForPacketsBefore;
259 private:
260 TestConnectionHelper* helper_;
262 DISALLOW_COPY_AND_ASSIGN(TestConnection);
265 class QuicConnectionTest : public ::testing::Test {
266 protected:
267 QuicConnectionTest()
268 : guid_(42),
269 framer_(kQuicVersion1,
270 QuicDecrypter::Create(kNULL),
271 QuicEncrypter::Create(kNULL),
272 QuicTime::Zero(),
273 false),
274 creator_(guid_, &framer_, QuicRandom::GetInstance(), false),
275 send_algorithm_(new StrictMock<MockSendAlgorithm>),
276 helper_(new TestConnectionHelper(&clock_, &random_generator_)),
277 connection_(guid_, IPEndPoint(), helper_, false),
278 frame1_(1, false, 0, data1),
279 frame2_(1, false, 3, data2),
280 accept_packet_(true) {
281 connection_.set_visitor(&visitor_);
282 connection_.SetSendAlgorithm(send_algorithm_);
283 // Simplify tests by not sending feedback unless specifically configured.
284 SetFeedback(NULL);
285 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillRepeatedly(Return(
286 QuicTime::Delta::Zero()));
287 EXPECT_CALL(*receive_algorithm_,
288 RecordIncomingPacket(_, _, _, _)).Times(AnyNumber());
289 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(AnyNumber());
292 QuicAckFrame* outgoing_ack() {
293 return QuicConnectionPeer::GetOutgoingAck(&connection_);
296 QuicAckFrame* last_ack() {
297 return helper_->ack();
300 QuicCongestionFeedbackFrame* last_feedback() {
301 return helper_->feedback();
304 QuicPacketHeader* last_header() {
305 return helper_->header();
308 size_t last_sent_packet_size() {
309 return helper_->last_packet_size();
312 void ProcessPacket(QuicPacketSequenceNumber number) {
313 EXPECT_CALL(visitor_, OnPacket(_, _, _, _))
314 .WillOnce(Return(accept_packet_));
315 ProcessDataPacket(number, 0, !kEntropyFlag);
318 QuicPacketEntropyHash ProcessFramePacket(QuicFrame frame) {
319 QuicFrames frames;
320 frames.push_back(QuicFrame(frame));
321 QuicPacketCreatorPeer::SetSendVersionInPacket(&creator_,
322 connection_.is_server());
323 SerializedPacket serialized_packet = creator_.SerializeAllFrames(frames);
324 scoped_ptr<QuicPacket> packet(serialized_packet.packet);
325 scoped_ptr<QuicEncryptedPacket> encrypted(
326 framer_.EncryptPacket(serialized_packet.sequence_number, *packet));
327 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
328 return serialized_packet.entropy_hash;
331 size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number,
332 bool expect_revival) {
333 if (expect_revival) {
334 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).Times(2).WillRepeatedly(
335 Return(accept_packet_));
336 } else {
337 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce(
338 Return(accept_packet_));
340 return ProcessDataPacket(number, 1, !kEntropyFlag);
343 size_t ProcessDataPacket(QuicPacketSequenceNumber number,
344 QuicFecGroupNumber fec_group,
345 bool entropy_flag) {
346 scoped_ptr<QuicPacket> packet(ConstructDataPacket(number, fec_group,
347 entropy_flag));
348 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(number,
349 *packet));
350 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
351 return encrypted->length();
354 void ProcessClosePacket(QuicPacketSequenceNumber number,
355 QuicFecGroupNumber fec_group) {
356 scoped_ptr<QuicPacket> packet(ConstructClosePacket(number, fec_group));
357 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(number,
358 *packet));
359 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
362 size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number,
363 bool expect_revival, bool entropy_flag) {
364 if (expect_revival) {
365 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce(DoAll(
366 SaveArg<2>(&revived_header_), Return(accept_packet_)));
368 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce(Return(accept_packet_))
369 .RetiresOnSaturation();
370 return ProcessDataPacket(number, 1, entropy_flag);
373 // Sends an FEC packet that covers the packets that would have been sent.
374 size_t ProcessFecPacket(QuicPacketSequenceNumber number,
375 QuicPacketSequenceNumber min_protected_packet,
376 bool expect_revival,
377 bool fec_entropy_flag) {
378 if (expect_revival) {
379 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce(DoAll(
380 SaveArg<2>(&revived_header_), Return(accept_packet_)));
383 // Construct the decrypted data packet so we can compute the correct
384 // redundancy.
385 scoped_ptr<QuicPacket> data_packet(ConstructDataPacket(number, 1,
386 !kEntropyFlag));
388 header_.public_header.guid = guid_;
389 header_.public_header.reset_flag = false;
390 header_.public_header.version_flag = false;
391 header_.entropy_flag = kEntropyFlag;
392 header_.fec_flag = true;
393 header_.fec_entropy_flag = fec_entropy_flag;
394 header_.packet_sequence_number = number;
395 header_.fec_group = min_protected_packet;
396 QuicFecData fec_data;
397 fec_data.fec_group = header_.fec_group;
398 // Since all data packets in this test have the same payload, the
399 // redundancy is either equal to that payload or the xor of that payload
400 // with itself, depending on the number of packets.
401 if (((number - min_protected_packet) % 2) == 0) {
402 for (size_t i = GetStartOfFecProtectedData(
403 header_.public_header.version_flag);
404 i < data_packet->length(); ++i) {
405 data_packet->mutable_data()[i] ^= data_packet->data()[i];
408 fec_data.redundancy = data_packet->FecProtectedData();
409 scoped_ptr<QuicPacket> fec_packet(
410 framer_.ConstructFecPacket(header_, fec_data).packet);
411 scoped_ptr<QuicEncryptedPacket> encrypted(
412 framer_.EncryptPacket(number, *fec_packet));
414 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
415 return encrypted->length();
418 QuicByteCount SendStreamDataToPeer(QuicStreamId id, StringPiece data,
419 QuicStreamOffset offset, bool fin,
420 QuicPacketSequenceNumber* last_packet) {
421 QuicByteCount packet_size;
422 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).WillOnce(
423 SaveArg<2>(&packet_size));
424 connection_.SendStreamData(id, data, offset, fin);
425 if (last_packet != NULL) {
426 *last_packet =
427 QuicConnectionPeer::GetPacketCreator(&connection_)->sequence_number();
429 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(AnyNumber());
430 return packet_size;
433 void SendAckPacketToPeer() {
434 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(1);
435 connection_.SendAck();
436 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(AnyNumber());
439 QuicPacketEntropyHash ProcessAckPacket(QuicAckFrame* frame) {
440 return ProcessFramePacket(QuicFrame(frame));
443 QuicPacketEntropyHash ProcessGoAwayPacket(QuicGoAwayFrame* frame) {
444 return ProcessFramePacket(QuicFrame(frame));
447 bool IsMissing(QuicPacketSequenceNumber number) {
448 return IsAwaitingPacket(outgoing_ack()->received_info, number);
451 QuicPacket* ConstructDataPacket(QuicPacketSequenceNumber number,
452 QuicFecGroupNumber fec_group,
453 bool entropy_flag) {
454 header_.public_header.guid = guid_;
455 header_.public_header.reset_flag = false;
456 header_.public_header.version_flag = false;
457 header_.entropy_flag = entropy_flag;
458 header_.fec_flag = false;
459 header_.fec_entropy_flag = false;
460 header_.packet_sequence_number = number;
461 header_.fec_group = fec_group;
463 QuicFrames frames;
464 QuicFrame frame(&frame1_);
465 frames.push_back(frame);
466 QuicPacket* packet =
467 framer_.ConstructFrameDataPacket(header_, frames).packet;
468 EXPECT_TRUE(packet != NULL);
469 return packet;
472 QuicPacket* ConstructClosePacket(QuicPacketSequenceNumber number,
473 QuicFecGroupNumber fec_group) {
474 header_.public_header.guid = guid_;
475 header_.packet_sequence_number = number;
476 header_.public_header.reset_flag = false;
477 header_.public_header.version_flag = false;
478 header_.entropy_flag = false;
479 header_.fec_flag = false;
480 header_.fec_entropy_flag = false;
481 header_.fec_group = fec_group;
483 QuicConnectionCloseFrame qccf;
484 qccf.error_code = QUIC_PEER_GOING_AWAY;
485 qccf.ack_frame = QuicAckFrame(0, QuicTime::Zero(), 1);
487 QuicFrames frames;
488 QuicFrame frame(&qccf);
489 frames.push_back(frame);
490 QuicPacket* packet =
491 framer_.ConstructFrameDataPacket(header_, frames).packet;
492 EXPECT_TRUE(packet != NULL);
493 return packet;
496 void SetFeedback(QuicCongestionFeedbackFrame* feedback) {
497 receive_algorithm_ = new TestReceiveAlgorithm(feedback);
498 connection_.SetReceiveAlgorithm(receive_algorithm_);
501 QuicGuid guid_;
502 QuicFramer framer_;
503 QuicPacketCreator creator_;
505 MockSendAlgorithm* send_algorithm_;
506 TestReceiveAlgorithm* receive_algorithm_;
507 MockClock clock_;
508 MockRandom random_generator_;
509 TestConnectionHelper* helper_;
510 TestConnection connection_;
511 testing::StrictMock<MockConnectionVisitor> visitor_;
513 QuicPacketHeader header_;
514 QuicPacketHeader revived_header_;
515 QuicStreamFrame frame1_;
516 QuicStreamFrame frame2_;
517 bool accept_packet_;
519 private:
520 DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest);
523 TEST_F(QuicConnectionTest, PacketsInOrder) {
524 ProcessPacket(1);
525 EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed);
526 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
528 ProcessPacket(2);
529 EXPECT_EQ(2u, outgoing_ack()->received_info.largest_observed);
530 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
532 ProcessPacket(3);
533 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
534 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
537 TEST_F(QuicConnectionTest, PacketsRejected) {
538 ProcessPacket(1);
539 EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed);
540 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
542 accept_packet_ = false;
543 ProcessPacket(2);
544 // We should not have an ack for two.
545 EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed);
546 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
549 TEST_F(QuicConnectionTest, PacketsOutOfOrder) {
550 ProcessPacket(3);
551 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
552 EXPECT_TRUE(IsMissing(2));
553 EXPECT_TRUE(IsMissing(1));
555 ProcessPacket(2);
556 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
557 EXPECT_FALSE(IsMissing(2));
558 EXPECT_TRUE(IsMissing(1));
560 ProcessPacket(1);
561 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
562 EXPECT_FALSE(IsMissing(2));
563 EXPECT_FALSE(IsMissing(1));
566 TEST_F(QuicConnectionTest, DuplicatePacket) {
567 ProcessPacket(3);
568 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
569 EXPECT_TRUE(IsMissing(2));
570 EXPECT_TRUE(IsMissing(1));
572 // Send packet 3 again, but do not set the expectation that
573 // the visitor OnPacket() will be called.
574 ProcessDataPacket(3, 0, !kEntropyFlag);
575 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
576 EXPECT_TRUE(IsMissing(2));
577 EXPECT_TRUE(IsMissing(1));
580 TEST_F(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndLeastAwaiting) {
581 ProcessPacket(3);
582 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
583 EXPECT_TRUE(IsMissing(2));
584 EXPECT_TRUE(IsMissing(1));
586 ProcessPacket(2);
587 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
588 EXPECT_TRUE(IsMissing(1));
590 ProcessPacket(5);
591 EXPECT_EQ(5u, outgoing_ack()->received_info.largest_observed);
592 EXPECT_TRUE(IsMissing(1));
593 EXPECT_TRUE(IsMissing(4));
595 // Pretend at this point the client has gotten acks for 2 and 3 and 1 is a
596 // packet the peer will not retransmit. It indicates this by sending 'least
597 // awaiting' is 4. The connection should then realize 1 will not be
598 // retransmitted, and will remove it from the missing list.
599 creator_.set_sequence_number(5);
600 QuicAckFrame frame(0, QuicTime::Zero(), 4);
601 ProcessAckPacket(&frame);
603 // Force an ack to be sent.
604 SendAckPacketToPeer();
605 EXPECT_TRUE(IsMissing(4));
608 TEST_F(QuicConnectionTest, RejectPacketTooFarOut) {
609 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
610 // packet call to the visitor.
611 ProcessDataPacket(6000, 0, !kEntropyFlag);
613 SendAckPacketToPeer(); // Packet 2
614 EXPECT_EQ(0u, outgoing_ack()->received_info.largest_observed);
617 TEST_F(QuicConnectionTest, TruncatedAck) {
618 EXPECT_CALL(visitor_, OnAck(_)).Times(testing::AnyNumber());
619 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2);
620 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
621 for (int i = 0; i < 200; ++i) {
622 SendStreamDataToPeer(1, "foo", i * 3, !kFin, NULL);
625 QuicAckFrame frame(0, QuicTime::Zero(), 1);
626 frame.received_info.largest_observed = 192;
627 InsertMissingPacketsBetween(&frame.received_info, 1, 192);
628 frame.received_info.entropy_hash =
629 QuicConnectionPeer::GetSentEntropyHash(&connection_, 192) ^
630 QuicConnectionPeer::GetSentEntropyHash(&connection_, 191);
632 ProcessAckPacket(&frame);
634 EXPECT_TRUE(QuicConnectionPeer::GetReceivedTruncatedAck(&connection_));
636 frame.received_info.missing_packets.erase(191);
637 frame.received_info.entropy_hash =
638 QuicConnectionPeer::GetSentEntropyHash(&connection_, 192) ^
639 QuicConnectionPeer::GetSentEntropyHash(&connection_, 190);
641 ProcessAckPacket(&frame);
642 EXPECT_FALSE(QuicConnectionPeer::GetReceivedTruncatedAck(&connection_));
645 TEST_F(QuicConnectionTest, LeastUnackedLower) {
646 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
647 SendStreamDataToPeer(1, "bar", 3, !kFin, NULL);
648 SendStreamDataToPeer(1, "eep", 6, !kFin, NULL);
650 // Start out saying the least unacked is 2
651 creator_.set_sequence_number(5);
652 QuicAckFrame frame(0, QuicTime::Zero(), 2);
653 ProcessAckPacket(&frame);
655 // Change it to 1, but lower the sequence number to fake out-of-order packets.
656 // This should be fine.
657 creator_.set_sequence_number(1);
658 QuicAckFrame frame2(0, QuicTime::Zero(), 1);
659 // The scheduler will not process out of order acks.
660 ProcessAckPacket(&frame2);
662 // Now claim it's one, but set the ordering so it was sent "after" the first
663 // one. This should cause a connection error.
664 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false));
665 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _));
666 creator_.set_sequence_number(7);
667 ProcessAckPacket(&frame2);
670 TEST_F(QuicConnectionTest, LargestObservedLower) {
671 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
672 SendStreamDataToPeer(1, "bar", 3, !kFin, NULL);
673 SendStreamDataToPeer(1, "eep", 6, !kFin, NULL);
674 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2);
676 // Start out saying the largest observed is 2.
677 QuicAckFrame frame(2, QuicTime::Zero(), 0);
678 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash(
679 &connection_, 2);
680 EXPECT_CALL(visitor_, OnAck(_));
681 ProcessAckPacket(&frame);
683 // Now change it to 1, and it should cause a connection error.
684 QuicAckFrame frame2(1, QuicTime::Zero(), 0);
685 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false));
686 ProcessAckPacket(&frame2);
689 TEST_F(QuicConnectionTest, LeastUnackedGreaterThanPacketSequenceNumber) {
690 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false));
691 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _));
692 // Create an ack with least_unacked is 2 in packet number 1.
693 creator_.set_sequence_number(0);
694 QuicAckFrame frame(0, QuicTime::Zero(), 2);
695 ProcessAckPacket(&frame);
698 TEST_F(QuicConnectionTest,
699 NackSequenceNumberGreaterThanLargestReceived) {
700 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
701 SendStreamDataToPeer(1, "bar", 3, !kFin, NULL);
702 SendStreamDataToPeer(1, "eep", 6, !kFin, NULL);
704 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false));
705 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _));
706 QuicAckFrame frame(0, QuicTime::Zero(), 1);
707 frame.received_info.missing_packets.insert(3);
708 ProcessAckPacket(&frame);
711 TEST_F(QuicConnectionTest, AckUnsentData) {
712 // Ack a packet which has not been sent.
713 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false));
714 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _));
715 QuicAckFrame frame(1, QuicTime::Zero(), 0);
716 ProcessAckPacket(&frame);
719 TEST_F(QuicConnectionTest, AckAll) {
720 ProcessPacket(1);
722 creator_.set_sequence_number(1);
723 QuicAckFrame frame1(0, QuicTime::Zero(), 1);
724 ProcessAckPacket(&frame1);
727 TEST_F(QuicConnectionTest, DontWaitForPacketsBefore) {
728 ProcessPacket(2);
729 ProcessPacket(7);
730 EXPECT_TRUE(connection_.DontWaitForPacketsBefore(4));
731 EXPECT_EQ(3u, outgoing_ack()->received_info.missing_packets.size());
734 TEST_F(QuicConnectionTest, BasicSending) {
735 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(6);
736 QuicPacketSequenceNumber last_packet;
737 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1
738 EXPECT_EQ(1u, last_packet);
739 SendAckPacketToPeer(); // Packet 2
741 EXPECT_EQ(1u, last_ack()->sent_info.least_unacked);
743 SendAckPacketToPeer(); // Packet 3
744 EXPECT_EQ(1u, last_ack()->sent_info.least_unacked);
746 SendStreamDataToPeer(1u, "bar", 3, !kFin, &last_packet); // Packet 4
747 EXPECT_EQ(4u, last_packet);
748 SendAckPacketToPeer(); // Packet 5
749 EXPECT_EQ(1u, last_ack()->sent_info.least_unacked);
751 SequenceNumberSet expected_acks;
752 expected_acks.insert(1);
754 // Client acks up to packet 3
755 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
756 QuicAckFrame frame(3, QuicTime::Zero(), 0);
757 frame.received_info.entropy_hash =
758 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3);
759 ProcessAckPacket(&frame);
760 SendAckPacketToPeer(); // Packet 6
762 // As soon as we've acked one, we skip ack packets 2 and 3 and note lack of
763 // ack for 4.
764 EXPECT_EQ(4u, last_ack()->sent_info.least_unacked);
766 expected_acks.clear();
767 expected_acks.insert(4);
769 // Client acks up to packet 4, the last packet
770 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
771 QuicAckFrame frame2(6, QuicTime::Zero(), 0);
772 frame2.received_info.entropy_hash =
773 QuicConnectionPeer::GetSentEntropyHash(&connection_, 6);
774 ProcessAckPacket(&frame2); // Even parity triggers ack packet 7
776 // The least packet awaiting ack should now be 7
777 EXPECT_EQ(7u, last_ack()->sent_info.least_unacked);
779 // If we force an ack, we shouldn't change our retransmit state.
780 SendAckPacketToPeer(); // Packet 8
781 EXPECT_EQ(8u, last_ack()->sent_info.least_unacked);
783 // But if we send more data it should.
784 SendStreamDataToPeer(1, "eep", 6, !kFin, &last_packet); // Packet 9
785 EXPECT_EQ(9u, last_packet);
786 SendAckPacketToPeer(); // Packet10
787 EXPECT_EQ(9u, last_ack()->sent_info.least_unacked);
790 TEST_F(QuicConnectionTest, FECSending) {
791 // Limit to one byte per packet.
792 // All packets carry version info till version is negotiated.
793 connection_.options()->max_packet_length =
794 GetPacketLengthForOneStream(kIncludeVersion, 4);
795 // And send FEC every two packets.
796 connection_.options()->max_packets_per_fec_group = 2;
798 // Send 4 data packets and 2 FEC packets.
799 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(6);
800 connection_.SendStreamData(1, "foodfoodfoodfood", 0, !kFin);
801 // Expect the FEC group to be closed after SendStreamData.
802 EXPECT_FALSE(creator_.ShouldSendFec(true));
805 TEST_F(QuicConnectionTest, FECQueueing) {
806 // Limit to one byte per packet.
807 // All packets carry version info till version is negotiated.
808 connection_.options()->max_packet_length =
809 GetPacketLengthForOneStream(kIncludeVersion, 4);
810 // And send FEC every two packets.
811 connection_.options()->max_packets_per_fec_group = 2;
813 EXPECT_EQ(0u, connection_.NumQueuedPackets());
814 helper_->set_blocked(true);
815 connection_.SendStreamData(1, "food", 0, !kFin);
816 EXPECT_FALSE(creator_.ShouldSendFec(true));
817 // Expect the first data packet and the fec packet to be queued.
818 EXPECT_EQ(2u, connection_.NumQueuedPackets());
821 TEST_F(QuicConnectionTest, FramePacking) {
822 // Block the connection.
823 helper_->SetSendAlarm(
824 clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1)));
826 // Send an ack and two stream frames in 1 packet by queueing them.
827 connection_.SendAck();
828 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
829 IgnoreResult(InvokeWithoutArgs(&connection_,
830 &TestConnection::SendStreamData1)),
831 IgnoreResult(InvokeWithoutArgs(&connection_,
832 &TestConnection::SendStreamData2)),
833 Return(true)));
835 // Unblock the connection.
836 helper_->UnregisterSendAlarmIfRegistered();
837 EXPECT_CALL(*send_algorithm_,
838 SentPacket(_, _, _, NOT_RETRANSMISSION))
839 .Times(1);
840 connection_.OnCanWrite();
841 EXPECT_EQ(0u, connection_.NumQueuedPackets());
842 EXPECT_FALSE(connection_.HasQueuedData());
844 // Parse the last packet and ensure it's an ack and two stream frames from
845 // two different streams.
846 EXPECT_EQ(3u, helper_->frame_count());
847 EXPECT_TRUE(helper_->ack());
848 EXPECT_EQ(2u, helper_->stream_frames()->size());
849 EXPECT_EQ(1u, (*helper_->stream_frames())[0].stream_id);
850 EXPECT_EQ(2u, (*helper_->stream_frames())[1].stream_id);
853 TEST_F(QuicConnectionTest, FramePackingFEC) {
854 // Enable fec.
855 connection_.options()->max_packets_per_fec_group = 6;
856 // Block the connection.
857 helper_->SetSendAlarm(
858 clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1)));
860 // Send an ack and two stream frames in 1 packet by queueing them.
861 connection_.SendAck();
862 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
863 IgnoreResult(InvokeWithoutArgs(&connection_,
864 &TestConnection::SendStreamData1)),
865 IgnoreResult(InvokeWithoutArgs(&connection_,
866 &TestConnection::SendStreamData2)),
867 Return(true)));
869 // Unblock the connection.
870 helper_->UnregisterSendAlarmIfRegistered();
871 EXPECT_CALL(*send_algorithm_,
872 SentPacket(_, _, _, NOT_RETRANSMISSION)).Times(2);
873 connection_.OnCanWrite();
874 EXPECT_EQ(0u, connection_.NumQueuedPackets());
875 EXPECT_FALSE(connection_.HasQueuedData());
877 // Parse the last packet and ensure it's in an fec group.
878 EXPECT_EQ(1u, helper_->header()->fec_group);
879 EXPECT_EQ(0u, helper_->frame_count());
882 TEST_F(QuicConnectionTest, OnCanWrite) {
883 // Visitor's OnCanWill send data, but will return false.
884 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
885 IgnoreResult(InvokeWithoutArgs(&connection_,
886 &TestConnection::SendStreamData1)),
887 IgnoreResult(InvokeWithoutArgs(&connection_,
888 &TestConnection::SendStreamData2)),
889 Return(false)));
891 EXPECT_CALL(*send_algorithm_,
892 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillRepeatedly(
893 testing::Return(QuicTime::Delta::Zero()));
895 // Unblock the connection.
896 connection_.OnCanWrite();
897 // Parse the last packet and ensure it's the two stream frames from
898 // two different streams.
899 EXPECT_EQ(2u, helper_->frame_count());
900 EXPECT_EQ(2u, helper_->stream_frames()->size());
901 EXPECT_EQ(1u, (*helper_->stream_frames())[0].stream_id);
902 EXPECT_EQ(2u, (*helper_->stream_frames())[1].stream_id);
905 TEST_F(QuicConnectionTest, RetransmitOnNack) {
906 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2);
907 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
908 EXPECT_CALL(*send_algorithm_, AbandoningPacket(2, _)).Times(1);
909 QuicPacketSequenceNumber last_packet;
910 QuicByteCount second_packet_size;
911 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1
912 second_packet_size =
913 SendStreamDataToPeer(1, "foos", 3, !kFin, &last_packet); // Packet 2
914 SendStreamDataToPeer(1, "fooos", 7, !kFin, &last_packet); // Packet 3
916 SequenceNumberSet expected_acks;
917 expected_acks.insert(1);
918 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
920 // Client acks one but not two or three. Right now we only retransmit on
921 // explicit nack, so it should not trigger a retransimission.
922 QuicAckFrame ack_one(1, QuicTime::Zero(), 0);
923 ack_one.received_info.entropy_hash =
924 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1);
925 ProcessAckPacket(&ack_one);
926 ProcessAckPacket(&ack_one);
927 ProcessAckPacket(&ack_one);
929 expected_acks.clear();
930 expected_acks.insert(3);
931 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
933 // Client acks up to 3 with two explicitly missing. Two nacks should cause no
934 // change.
935 QuicAckFrame nack_two(3, QuicTime::Zero(), 0);
936 nack_two.received_info.missing_packets.insert(2);
937 nack_two.received_info.entropy_hash =
938 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^
939 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^
940 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1);
941 ProcessAckPacket(&nack_two);
942 ProcessAckPacket(&nack_two);
944 // The third nack should trigger a retransimission.
945 EXPECT_CALL(*send_algorithm_,
946 SentPacket(_, _, second_packet_size - kQuicVersionSize,
947 IS_RETRANSMISSION)).Times(1);
948 ProcessAckPacket(&nack_two);
951 TEST_F(QuicConnectionTest, RetransmitNackedLargestObserved) {
952 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
953 QuicPacketSequenceNumber largest_observed;
954 QuicByteCount packet_size;
955 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, NOT_RETRANSMISSION))
956 .WillOnce(DoAll(SaveArg<1>(&largest_observed), SaveArg<2>(&packet_size)));
957 EXPECT_CALL(*send_algorithm_, AbandoningPacket(1, _)).Times(1);
958 connection_.SendStreamData(1, "foo", 0, !kFin);
959 QuicAckFrame frame(1, QuicTime::Zero(), largest_observed);
960 frame.received_info.missing_packets.insert(largest_observed);
961 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash(
962 &connection_, largest_observed - 1);
963 ProcessAckPacket(&frame);
964 // Second udp packet will force an ack frame.
965 EXPECT_CALL(*send_algorithm_,
966 SentPacket(_, _, _, NOT_RETRANSMISSION));
967 ProcessAckPacket(&frame);
968 // Third nack should retransmit the largest observed packet.
969 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, packet_size - kQuicVersionSize,
970 IS_RETRANSMISSION));
971 ProcessAckPacket(&frame);
974 TEST_F(QuicConnectionTest, LimitPacketsPerNack) {
975 EXPECT_CALL(*send_algorithm_, OnIncomingAck(12, _, _)).Times(1);
976 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
977 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(11);
978 int offset = 0;
979 // Send packets 1 to 12
980 for (int i = 0; i < 12; ++i) {
981 SendStreamDataToPeer(1, "foo", offset, !kFin, NULL);
982 offset += 3;
985 // Ack 12, nack 1-11
986 QuicAckFrame nack(12, QuicTime::Zero(), 0);
987 for (int i = 1; i < 12; ++i) {
988 nack.received_info.missing_packets.insert(i);
991 nack.received_info.entropy_hash =
992 QuicConnectionPeer::GetSentEntropyHash(&connection_, 12) ^
993 QuicConnectionPeer::GetSentEntropyHash(&connection_, 11);
994 SequenceNumberSet expected_acks;
995 expected_acks.insert(12);
996 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
998 // Nack three times.
999 ProcessAckPacket(&nack);
1000 // The second call will trigger an ack.
1001 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(1);
1002 ProcessAckPacket(&nack);
1003 // The third call should trigger retransmitting 10 packets.
1004 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(10);
1005 ProcessAckPacket(&nack);
1007 // The fourth call should trigger retransmitting the 11th packet and an ack.
1008 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(2);
1009 ProcessAckPacket(&nack);
1012 // Test sending multiple acks from the connection to the session.
1013 TEST_F(QuicConnectionTest, MultipleAcks) {
1014 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(6);
1015 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
1016 QuicPacketSequenceNumber last_packet;
1017 SendStreamDataToPeer(1u, "foo", 0, !kFin, &last_packet); // Packet 1
1018 EXPECT_EQ(1u, last_packet);
1019 SendStreamDataToPeer(3u, "foo", 0, !kFin, &last_packet); // Packet 2
1020 EXPECT_EQ(2u, last_packet);
1021 SendAckPacketToPeer(); // Packet 3
1022 SendStreamDataToPeer(5u, "foo", 0, !kFin, &last_packet); // Packet 4
1023 EXPECT_EQ(4u, last_packet);
1024 SendStreamDataToPeer(1u, "foo", 3, !kFin, &last_packet); // Packet 5
1025 EXPECT_EQ(5u, last_packet);
1026 SendStreamDataToPeer(3u, "foo", 3, !kFin, &last_packet); // Packet 6
1027 EXPECT_EQ(6u, last_packet);
1029 // Client will ack packets 1, [!2], 3, 4, 5
1030 QuicAckFrame frame1(5, QuicTime::Zero(), 0);
1031 frame1.received_info.missing_packets.insert(2);
1032 frame1.received_info.entropy_hash =
1033 QuicConnectionPeer::GetSentEntropyHash(&connection_, 5) ^
1034 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^
1035 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1);
1037 // The connection should pass up acks for 1, 4, 5. 2 is not acked, and 3 was
1038 // an ackframe so should not be passed up.
1039 SequenceNumberSet expected_acks;
1040 expected_acks.insert(1);
1041 expected_acks.insert(4);
1042 expected_acks.insert(5);
1044 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
1045 ProcessAckPacket(&frame1);
1047 // Now the client implicitly acks 2, and explicitly acks 6
1048 QuicAckFrame frame2(6, QuicTime::Zero(), 0);
1049 frame2.received_info.entropy_hash =
1050 QuicConnectionPeer::GetSentEntropyHash(&connection_, 6);
1051 expected_acks.clear();
1052 // Both acks should be passed up.
1053 expected_acks.insert(2);
1054 expected_acks.insert(6);
1056 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
1057 ProcessAckPacket(&frame2);
1060 TEST_F(QuicConnectionTest, DontLatchUnackedPacket) {
1061 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1);
1062 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); // Packet 1;
1063 SendAckPacketToPeer(); // Packet 2
1065 // This sets least unacked to 3 (unsent packet), since we don't need
1066 // an ack for Packet 2 (ack packet).
1067 SequenceNumberSet expected_acks;
1068 expected_acks.insert(1);
1069 // Client acks packet 1
1070 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks)));
1071 QuicAckFrame frame(1, QuicTime::Zero(), 0);
1072 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash(
1073 &connection_, 1);
1074 ProcessAckPacket(&frame);
1076 // Verify that our internal state has least-unacked as 3.
1077 EXPECT_EQ(3u, outgoing_ack()->sent_info.least_unacked);
1079 // When we send an ack, we make sure our least-unacked makes sense. In this
1080 // case since we're not waiting on an ack for 2 and all packets are acked, we
1081 // set it to 3.
1082 SendAckPacketToPeer(); // Packet 3
1083 // Since this was an ack packet, we set least_unacked to 4.
1084 EXPECT_EQ(4u, outgoing_ack()->sent_info.least_unacked);
1085 // Check that the outgoing ack had its sequence number as least_unacked.
1086 EXPECT_EQ(3u, last_ack()->sent_info.least_unacked);
1088 SendStreamDataToPeer(1, "bar", 3, false, NULL); // Packet 4
1089 EXPECT_EQ(4u, outgoing_ack()->sent_info.least_unacked);
1090 SendAckPacketToPeer(); // Packet 5
1091 EXPECT_EQ(4u, last_ack()->sent_info.least_unacked);
1094 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterFecPacket) {
1095 // Don't send missing packet 1.
1096 ProcessFecPacket(2, 1, true, !kFecEntropyFlag);
1097 EXPECT_FALSE(revived_header_.entropy_flag);
1100 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacketThenFecPacket) {
1101 ProcessFecProtectedPacket(1, false, kEntropyFlag);
1102 // Don't send missing packet 2.
1103 ProcessFecPacket(3, 1, true, !kFecEntropyFlag);
1104 EXPECT_TRUE(revived_header_.entropy_flag);
1107 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacketsThenFecPacket) {
1108 ProcessFecProtectedPacket(1, false, !kEntropyFlag);
1109 // Don't send missing packet 2.
1110 ProcessFecProtectedPacket(3, false, !kEntropyFlag);
1111 ProcessFecPacket(4, 1, true, kFecEntropyFlag);
1112 EXPECT_TRUE(revived_header_.entropy_flag);
1115 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacket) {
1116 // Don't send missing packet 1.
1117 ProcessFecPacket(3, 1, false, !kFecEntropyFlag);
1118 // out of order
1119 ProcessFecProtectedPacket(2, true, !kEntropyFlag);
1120 EXPECT_FALSE(revived_header_.entropy_flag);
1123 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPackets) {
1124 ProcessFecProtectedPacket(1, false, !kEntropyFlag);
1125 // Don't send missing packet 2.
1126 ProcessFecPacket(6, 1, false, kFecEntropyFlag);
1127 ProcessFecProtectedPacket(3, false, kEntropyFlag);
1128 ProcessFecProtectedPacket(4, false, kEntropyFlag);
1129 ProcessFecProtectedPacket(5, true, !kEntropyFlag);
1130 EXPECT_TRUE(revived_header_.entropy_flag);
1133 TEST_F(QuicConnectionTest, TestRetransmit) {
1134 const QuicTime::Delta kDefaultRetransmissionTime =
1135 QuicTime::Delta::FromMilliseconds(500);
1137 QuicTime default_retransmission_time = clock_.ApproximateNow().Add(
1138 kDefaultRetransmissionTime);
1139 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
1140 EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked);
1142 EXPECT_EQ(1u, last_header()->packet_sequence_number);
1143 EXPECT_EQ(default_retransmission_time, helper_->retransmission_alarm());
1144 // Simulate the retransimission alarm firing
1145 clock_.AdvanceTime(kDefaultRetransmissionTime);
1146 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _));
1147 EXPECT_CALL(*send_algorithm_, AbandoningPacket(1, _)).Times(1);
1148 connection_.RetransmitPacket(1);
1149 EXPECT_EQ(2u, last_header()->packet_sequence_number);
1150 EXPECT_EQ(2u, outgoing_ack()->sent_info.least_unacked);
1153 TEST_F(QuicConnectionTest, TestRetransmitOrder) {
1154 QuicByteCount first_packet_size;
1155 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).WillOnce(
1156 SaveArg<2>(&first_packet_size));
1157 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(2);
1159 connection_.SendStreamData(1, "first_packet", 0, !kFin);
1160 QuicByteCount second_packet_size;
1161 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).WillOnce(
1162 SaveArg<2>(&second_packet_size));
1163 connection_.SendStreamData(1, "second_packet", 12, !kFin);
1164 EXPECT_NE(first_packet_size, second_packet_size);
1165 // Advance the clock by huge time to make sure packets will be retransmitted.
1166 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10));
1168 InSequence s;
1169 EXPECT_CALL(*send_algorithm_,
1170 SentPacket(_, _, first_packet_size, _));
1171 EXPECT_CALL(*send_algorithm_,
1172 SentPacket(_, _, second_packet_size, _));
1174 connection_.OnRetransmissionTimeout();
1177 TEST_F(QuicConnectionTest, TestRetransmissionCountCalculation) {
1178 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
1179 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(2);
1180 QuicPacketSequenceNumber original_sequence_number;
1181 EXPECT_CALL(*send_algorithm_,
1182 SentPacket(_, _, _, NOT_RETRANSMISSION))
1183 .WillOnce(SaveArg<1>(&original_sequence_number));
1184 connection_.SendStreamData(1, "foo", 0, !kFin);
1185 EXPECT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
1186 &connection_, original_sequence_number));
1187 EXPECT_EQ(0u, QuicConnectionPeer::GetRetransmissionCount(
1188 &connection_, original_sequence_number));
1189 // Force retransmission due to RTO.
1190 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10));
1191 QuicPacketSequenceNumber rto_sequence_number;
1192 EXPECT_CALL(*send_algorithm_,
1193 SentPacket(_, _, _, IS_RETRANSMISSION))
1194 .WillOnce(SaveArg<1>(&rto_sequence_number));
1195 connection_.OnRetransmissionTimeout();
1196 EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission(
1197 &connection_, original_sequence_number));
1198 ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
1199 &connection_, rto_sequence_number));
1200 EXPECT_EQ(1u, QuicConnectionPeer::GetRetransmissionCount(
1201 &connection_, rto_sequence_number));
1202 // Once by explicit nack.
1203 QuicPacketSequenceNumber nack_sequence_number;
1204 // Ack packets might generate some other packets, which are not
1205 // retransmissions. (More ack packets).
1206 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, NOT_RETRANSMISSION))
1207 .Times(AnyNumber());
1208 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, IS_RETRANSMISSION))
1209 .WillOnce(SaveArg<1>(&nack_sequence_number));
1210 QuicAckFrame ack(rto_sequence_number, QuicTime::Zero(), 0);
1211 // Ack the retransmitted packet.
1212 ack.received_info.missing_packets.insert(rto_sequence_number);
1213 ack.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash(
1214 &connection_, rto_sequence_number - 1);
1215 for (int i = 0; i < 3; i++) {
1216 ProcessAckPacket(&ack);
1218 EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission(
1219 &connection_, rto_sequence_number));
1220 EXPECT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
1221 &connection_, nack_sequence_number));
1222 EXPECT_EQ(2u, QuicConnectionPeer::GetRetransmissionCount(
1223 &connection_, nack_sequence_number));
1226 TEST_F(QuicConnectionTest, SetRTOAfterWritingToSocket) {
1227 helper_->set_blocked(true);
1228 connection_.SendStreamData(1, "foo", 0, !kFin);
1229 // Make sure that RTO is not started when the packet is queued.
1230 EXPECT_EQ(0u, QuicConnectionPeer::GetNumRetransmissionTimeouts(&connection_));
1232 // Test that RTO is started once we write to the socket.
1233 helper_->set_blocked(false);
1234 EXPECT_CALL(visitor_, OnCanWrite());
1235 connection_.OnCanWrite();
1236 EXPECT_EQ(1u, QuicConnectionPeer::GetNumRetransmissionTimeouts(&connection_));
1239 TEST_F(QuicConnectionTest, TestQueued) {
1240 EXPECT_EQ(0u, connection_.NumQueuedPackets());
1241 helper_->set_blocked(true);
1242 connection_.SendStreamData(1, "foo", 0, !kFin);
1243 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1245 // Attempt to send all packets, but since we're actually still
1246 // blocked, they should all remain queued.
1247 EXPECT_FALSE(connection_.OnCanWrite());
1248 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1250 // Unblock the writes and actually send.
1251 helper_->set_blocked(false);
1252 EXPECT_CALL(visitor_, OnCanWrite());
1253 EXPECT_TRUE(connection_.OnCanWrite());
1254 EXPECT_EQ(0u, connection_.NumQueuedPackets());
1257 TEST_F(QuicConnectionTest, CloseFecGroup) {
1258 // Don't send missing packet 1
1259 // Don't send missing packet 2
1260 ProcessFecProtectedPacket(3, false, !kEntropyFlag);
1261 // Don't send missing FEC packet 3
1262 ASSERT_EQ(1u, connection_.NumFecGroups());
1264 // Now send non-fec protected ack packet and close the group
1265 QuicAckFrame frame(0, QuicTime::Zero(), 5);
1266 creator_.set_sequence_number(4);
1267 ProcessAckPacket(&frame);
1268 ASSERT_EQ(0u, connection_.NumFecGroups());
1271 TEST_F(QuicConnectionTest, NoQuicCongestionFeedbackFrame) {
1272 SendAckPacketToPeer();
1273 EXPECT_TRUE(last_feedback() == NULL);
1276 TEST_F(QuicConnectionTest, WithQuicCongestionFeedbackFrame) {
1277 QuicCongestionFeedbackFrame info;
1278 info.type = kFixRate;
1279 info.fix_rate.bitrate = QuicBandwidth::FromBytesPerSecond(123);
1280 SetFeedback(&info);
1282 SendAckPacketToPeer();
1283 EXPECT_EQ(kFixRate, last_feedback()->type);
1284 EXPECT_EQ(info.fix_rate.bitrate, last_feedback()->fix_rate.bitrate);
1287 TEST_F(QuicConnectionTest, UpdateQuicCongestionFeedbackFrame) {
1288 SendAckPacketToPeer();
1289 EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _, _));
1290 ProcessPacket(1);
1293 TEST_F(QuicConnectionTest, DontUpdateQuicCongestionFeedbackFrameForRevived) {
1294 SendAckPacketToPeer();
1295 // Process an FEC packet, and revive the missing data packet
1296 // but only contact the receive_algorithm once.
1297 EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _, _));
1298 ProcessFecPacket(2, 1, true, !kEntropyFlag);
1301 TEST_F(QuicConnectionTest, InitialTimeout) {
1302 EXPECT_TRUE(connection_.connected());
1303 EXPECT_CALL(visitor_, ConnectionClose(QUIC_CONNECTION_TIMED_OUT, false));
1304 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _));
1306 QuicTime default_timeout = clock_.ApproximateNow().Add(
1307 QuicTime::Delta::FromMicroseconds(kDefaultTimeoutUs));
1308 EXPECT_EQ(default_timeout, helper_->timeout_alarm());
1310 // Simulate the timeout alarm firing
1311 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(kDefaultTimeoutUs));
1312 EXPECT_TRUE(connection_.CheckForTimeout());
1313 EXPECT_FALSE(connection_.connected());
1316 TEST_F(QuicConnectionTest, TimeoutAfterSend) {
1317 EXPECT_TRUE(connection_.connected());
1319 QuicTime default_timeout = clock_.ApproximateNow().Add(
1320 QuicTime::Delta::FromMicroseconds(kDefaultTimeoutUs));
1322 // When we send a packet, the timeout will change to 5000 + kDefaultTimeout.
1323 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
1325 // Send an ack so we don't set the retransimission alarm.
1326 SendAckPacketToPeer();
1327 EXPECT_EQ(default_timeout, helper_->timeout_alarm());
1329 // The original alarm will fire. We should not time out because we had a
1330 // network event at t=5000. The alarm will reregister.
1331 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(
1332 kDefaultTimeoutUs - 5000));
1333 EXPECT_EQ(default_timeout, clock_.ApproximateNow());
1334 EXPECT_FALSE(connection_.CheckForTimeout());
1335 EXPECT_TRUE(connection_.connected());
1336 EXPECT_EQ(default_timeout.Add(QuicTime::Delta::FromMilliseconds(5)),
1337 helper_->timeout_alarm());
1339 // This time, we should time out.
1340 EXPECT_CALL(visitor_, ConnectionClose(QUIC_CONNECTION_TIMED_OUT, false));
1341 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _));
1342 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
1343 EXPECT_EQ(default_timeout.Add(QuicTime::Delta::FromMilliseconds(5)),
1344 clock_.ApproximateNow());
1345 EXPECT_TRUE(connection_.CheckForTimeout());
1346 EXPECT_FALSE(connection_.connected());
1349 // TODO(ianswett): Add scheduler tests when should_retransmit is false.
1350 TEST_F(QuicConnectionTest, SendScheduler) {
1351 // Test that if we send a packet without delay, it is not queued.
1352 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
1353 EXPECT_CALL(*send_algorithm_,
1354 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillOnce(
1355 testing::Return(QuicTime::Delta::Zero()));
1356 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _));
1357 connection_.SendOrQueuePacket(
1358 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1359 EXPECT_EQ(0u, connection_.NumQueuedPackets());
1362 TEST_F(QuicConnectionTest, SendSchedulerDelay) {
1363 // Test that if we send a packet with a delay, it ends up queued.
1364 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
1365 EXPECT_CALL(*send_algorithm_,
1366 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillOnce(
1367 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
1368 EXPECT_CALL(*send_algorithm_, SentPacket(_, 1, _, _)).Times(0);
1369 connection_.SendOrQueuePacket(
1370 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1371 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1374 TEST_F(QuicConnectionTest, SendSchedulerForce) {
1375 // Test that if we force send a packet, it is not queued.
1376 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
1377 EXPECT_CALL(*send_algorithm_,
1378 TimeUntilSend(_, IS_RETRANSMISSION, _)).Times(0);
1379 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _));
1380 connection_.SendOrQueuePacket(
1381 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1382 // XXX: fixme. was: connection_.SendOrQueuePacket(1, packet, kForce);
1383 EXPECT_EQ(0u, connection_.NumQueuedPackets());
1386 TEST_F(QuicConnectionTest, SendSchedulerEAGAIN) {
1387 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
1388 helper_->set_blocked(true);
1389 EXPECT_CALL(*send_algorithm_,
1390 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillOnce(
1391 testing::Return(QuicTime::Delta::Zero()));
1392 EXPECT_CALL(*send_algorithm_, SentPacket(_, 1, _, _)).Times(0);
1393 connection_.SendOrQueuePacket(
1394 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1395 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1398 TEST_F(QuicConnectionTest, SendSchedulerDelayThenSend) {
1399 // Test that if we send a packet with a delay, it ends up queued.
1400 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
1401 EXPECT_CALL(*send_algorithm_,
1402 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillOnce(
1403 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
1404 connection_.SendOrQueuePacket(
1405 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1406 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1408 // Advance the clock to fire the alarm, and configure the scheduler
1409 // to permit the packet to be sent.
1410 EXPECT_CALL(*send_algorithm_,
1411 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillRepeatedly(
1412 testing::Return(QuicTime::Delta::Zero()));
1413 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1));
1414 helper_->UnregisterSendAlarmIfRegistered();
1415 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _));
1416 EXPECT_CALL(visitor_, OnCanWrite());
1417 connection_.OnCanWrite();
1418 EXPECT_EQ(0u, connection_.NumQueuedPackets());
1421 TEST_F(QuicConnectionTest, SendSchedulerDelayThenRetransmit) {
1422 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, NOT_RETRANSMISSION, _))
1423 .WillRepeatedly(testing::Return(QuicTime::Delta::Zero()));
1424 EXPECT_CALL(*send_algorithm_, AbandoningPacket(1, _)).Times(1);
1425 EXPECT_CALL(*send_algorithm_,
1426 SentPacket(_, 1, _, NOT_RETRANSMISSION));
1427 connection_.SendStreamData(1, "foo", 0, !kFin);
1428 EXPECT_EQ(0u, connection_.NumQueuedPackets());
1429 // Advance the time for retransmission of lost packet.
1430 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(501));
1431 // Test that if we send a retransmit with a delay, it ends up queued.
1432 EXPECT_CALL(*send_algorithm_,
1433 TimeUntilSend(_, IS_RETRANSMISSION, _)).WillOnce(
1434 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
1435 connection_.OnRetransmissionTimeout();
1436 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1438 // Advance the clock to fire the alarm, and configure the scheduler
1439 // to permit the packet to be sent.
1440 EXPECT_CALL(*send_algorithm_,
1441 TimeUntilSend(_, IS_RETRANSMISSION, _)).WillOnce(
1442 testing::Return(QuicTime::Delta::Zero()));
1444 // Ensure the scheduler is notified this is a retransmit.
1445 EXPECT_CALL(*send_algorithm_,
1446 SentPacket(_, _, _, IS_RETRANSMISSION));
1447 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1));
1448 helper_->UnregisterSendAlarmIfRegistered();
1449 EXPECT_CALL(visitor_, OnCanWrite());
1450 connection_.OnCanWrite();
1451 EXPECT_EQ(0u, connection_.NumQueuedPackets());
1454 TEST_F(QuicConnectionTest, SendSchedulerDelayAndQueue) {
1455 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
1456 EXPECT_CALL(*send_algorithm_,
1457 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillOnce(
1458 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
1459 connection_.SendOrQueuePacket(
1460 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1461 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1463 // Attempt to send another packet and make sure that it gets queued.
1464 packet = ConstructDataPacket(2, 0, !kEntropyFlag);
1465 connection_.SendOrQueuePacket(
1466 2, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1467 EXPECT_EQ(2u, connection_.NumQueuedPackets());
1470 TEST_F(QuicConnectionTest, SendSchedulerDelayThenAckAndSend) {
1471 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
1472 EXPECT_CALL(*send_algorithm_,
1473 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillOnce(
1474 testing::Return(QuicTime::Delta::FromMicroseconds(10)));
1475 connection_.SendOrQueuePacket(
1476 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1477 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1479 // Now send non-retransmitting information, that we're not going to
1480 // retransmit 3. The far end should stop waiting for it.
1481 QuicAckFrame frame(0, QuicTime::Zero(), 1);
1482 EXPECT_CALL(*send_algorithm_,
1483 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillRepeatedly(
1484 testing::Return(QuicTime::Delta::Zero()));
1485 EXPECT_CALL(*send_algorithm_,
1486 SentPacket(_, _, _, _));
1487 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(Return(true));
1488 ProcessAckPacket(&frame);
1490 EXPECT_EQ(0u, connection_.NumQueuedPackets());
1491 // Ensure alarm is not set
1492 EXPECT_FALSE(helper_->IsSendAlarmSet());
1495 TEST_F(QuicConnectionTest, SendSchedulerDelayThenAckAndHold) {
1496 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
1497 EXPECT_CALL(*send_algorithm_,
1498 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillOnce(
1499 testing::Return(QuicTime::Delta::FromMicroseconds(10)));
1500 connection_.SendOrQueuePacket(
1501 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1502 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1504 // Now send non-retransmitting information, that we're not going to
1505 // retransmit 3. The far end should stop waiting for it.
1506 QuicAckFrame frame(0, QuicTime::Zero(), 1);
1507 EXPECT_CALL(*send_algorithm_,
1508 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillOnce(
1509 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
1510 ProcessAckPacket(&frame);
1512 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1515 TEST_F(QuicConnectionTest, SendSchedulerDelayThenOnCanWrite) {
1516 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
1517 EXPECT_CALL(*send_algorithm_,
1518 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillOnce(
1519 testing::Return(QuicTime::Delta::FromMicroseconds(10)));
1520 connection_.SendOrQueuePacket(
1521 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1522 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1524 // OnCanWrite should not send the packet (because of the delay)
1525 // but should still return true.
1526 EXPECT_TRUE(connection_.OnCanWrite());
1527 EXPECT_EQ(1u, connection_.NumQueuedPackets());
1530 TEST_F(QuicConnectionTest, TestQueueLimitsOnSendStreamData) {
1531 // Limit to one byte per packet.
1532 // All packets carry version info till version is negotiated.
1533 connection_.options()->max_packet_length =
1534 GetPacketLengthForOneStream(kIncludeVersion, 4);
1536 // Queue the first packet.
1537 EXPECT_CALL(*send_algorithm_,
1538 TimeUntilSend(_, NOT_RETRANSMISSION, _)).WillOnce(
1539 testing::Return(QuicTime::Delta::FromMicroseconds(10)));
1540 EXPECT_EQ(0u, connection_.SendStreamData(
1541 1, "EnoughDataToQueue", 0, !kFin).bytes_consumed);
1542 EXPECT_EQ(0u, connection_.NumQueuedPackets());
1545 TEST_F(QuicConnectionTest, LoopThroughSendingPackets) {
1546 // Limit to 4 bytes per packet.
1547 // All packets carry version info till version is negotiated.
1548 connection_.options()->max_packet_length =
1549 GetPacketLengthForOneStream(kIncludeVersion, 4);
1551 // Queue the first packet.
1552 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(7);
1553 EXPECT_EQ(27u, connection_.SendStreamData(
1554 1, "EnoughDataToQueueStreamData", 0, !kFin).bytes_consumed);
1557 TEST_F(QuicConnectionTest, NoAckForClose) {
1558 ProcessPacket(1);
1559 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(0);
1560 EXPECT_CALL(visitor_, ConnectionClose(QUIC_PEER_GOING_AWAY, true));
1561 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(0);
1562 ProcessClosePacket(2, 0);
1565 TEST_F(QuicConnectionTest, SendWhenDisconnected) {
1566 EXPECT_TRUE(connection_.connected());
1567 EXPECT_CALL(visitor_, ConnectionClose(QUIC_PEER_GOING_AWAY, false));
1568 connection_.CloseConnection(QUIC_PEER_GOING_AWAY, false);
1569 EXPECT_FALSE(connection_.connected());
1570 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
1571 EXPECT_CALL(*send_algorithm_, SentPacket(_, 1, _, _)).Times(0);
1572 connection_.SendOrQueuePacket(
1573 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
1576 TEST_F(QuicConnectionTest, PublicReset) {
1577 QuicPublicResetPacket header;
1578 header.public_header.guid = guid_;
1579 header.public_header.reset_flag = true;
1580 header.public_header.version_flag = false;
1581 header.rejected_sequence_number = 10101;
1582 scoped_ptr<QuicEncryptedPacket> packet(
1583 framer_.ConstructPublicResetPacket(header));
1584 EXPECT_CALL(visitor_, ConnectionClose(QUIC_PUBLIC_RESET, true));
1585 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *packet);
1588 TEST_F(QuicConnectionTest, GoAway) {
1589 QuicGoAwayFrame goaway;
1590 goaway.last_good_stream_id = 1;
1591 goaway.error_code = QUIC_PEER_GOING_AWAY;
1592 goaway.reason_phrase = "Going away.";
1593 EXPECT_CALL(visitor_, OnGoAway(_));
1594 ProcessGoAwayPacket(&goaway);
1597 TEST_F(QuicConnectionTest, MissingPacketsBeforeLeastUnacked) {
1598 QuicAckFrame ack(0, QuicTime::Zero(), 4);
1599 // Set the sequence number of the ack packet to be least unacked (4)
1600 creator_.set_sequence_number(3);
1601 ProcessAckPacket(&ack);
1602 EXPECT_TRUE(outgoing_ack()->received_info.missing_packets.empty());
1605 TEST_F(QuicConnectionTest, ReceivedEntropyHashCalculation) {
1606 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillRepeatedly(Return(true));
1607 ProcessDataPacket(1, 1, kEntropyFlag);
1608 ProcessDataPacket(4, 1, kEntropyFlag);
1609 ProcessDataPacket(3, 1, !kEntropyFlag);
1610 ProcessDataPacket(7, 1, kEntropyFlag);
1611 EXPECT_EQ(146u, outgoing_ack()->received_info.entropy_hash);
1614 TEST_F(QuicConnectionTest, UpdateEntropyForReceivedPackets) {
1615 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillRepeatedly(Return(true));
1616 ProcessDataPacket(1, 1, kEntropyFlag);
1617 ProcessDataPacket(5, 1, kEntropyFlag);
1618 ProcessDataPacket(4, 1, !kEntropyFlag);
1619 EXPECT_EQ(34u, outgoing_ack()->received_info.entropy_hash);
1620 // Make 4th packet my least unacked, and update entropy for 2, 3 packets.
1621 QuicAckFrame ack(0, QuicTime::Zero(), 4);
1622 QuicPacketEntropyHash kRandomEntropyHash = 129u;
1623 ack.sent_info.entropy_hash = kRandomEntropyHash;
1624 creator_.set_sequence_number(5);
1625 QuicPacketEntropyHash six_packet_entropy_hash = 0;
1626 if (ProcessAckPacket(&ack)) {
1627 six_packet_entropy_hash = 1 << 6;
1630 EXPECT_EQ((kRandomEntropyHash + (1 << 5) + six_packet_entropy_hash),
1631 outgoing_ack()->received_info.entropy_hash);
1634 TEST_F(QuicConnectionTest, UpdateEntropyHashUptoCurrentPacket) {
1635 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillRepeatedly(Return(true));
1636 ProcessDataPacket(1, 1, kEntropyFlag);
1637 ProcessDataPacket(5, 1, !kEntropyFlag);
1638 ProcessDataPacket(22, 1, kEntropyFlag);
1639 EXPECT_EQ(66u, outgoing_ack()->received_info.entropy_hash);
1640 creator_.set_sequence_number(22);
1641 QuicPacketEntropyHash kRandomEntropyHash = 85u;
1642 // Current packet is the least unacked packet.
1643 QuicAckFrame ack(0, QuicTime::Zero(), 23);
1644 ack.sent_info.entropy_hash = kRandomEntropyHash;
1645 QuicPacketEntropyHash ack_entropy_hash = ProcessAckPacket(&ack);
1646 EXPECT_EQ((kRandomEntropyHash + ack_entropy_hash),
1647 outgoing_ack()->received_info.entropy_hash);
1648 ProcessDataPacket(25, 1, kEntropyFlag);
1649 EXPECT_EQ((kRandomEntropyHash + ack_entropy_hash + (1 << (25 % 8))),
1650 outgoing_ack()->received_info.entropy_hash);
1653 TEST_F(QuicConnectionTest, EntropyCalculationForTruncatedAck) {
1654 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillRepeatedly(Return(true));
1655 QuicPacketEntropyHash entropy[51];
1656 entropy[0] = 0;
1657 for (int i = 1; i < 51; ++i) {
1658 bool should_send = i % 10 != 0;
1659 bool entropy_flag = (i & (i - 1)) != 0;
1660 if (!should_send) {
1661 entropy[i] = entropy[i - 1];
1662 continue;
1664 if (entropy_flag) {
1665 entropy[i] = entropy[i - 1] ^ (1 << (i % 8));
1666 } else {
1667 entropy[i] = entropy[i - 1];
1669 ProcessDataPacket(i, 1, entropy_flag);
1671 // Till 50 since 50th packet is not sent.
1672 for (int i = 1; i < 50; ++i) {
1673 EXPECT_EQ(entropy[i], QuicConnectionPeer::ReceivedEntropyHash(
1674 &connection_, i));
1678 TEST_F(QuicConnectionTest, CheckSentEntropyHash) {
1679 creator_.set_sequence_number(1);
1680 SequenceNumberSet missing_packets;
1681 QuicPacketEntropyHash entropy_hash = 0;
1682 QuicPacketSequenceNumber max_sequence_number = 51;
1683 for (QuicPacketSequenceNumber i = 1; i <= max_sequence_number; ++i) {
1684 bool is_missing = i % 10 != 0;
1685 bool entropy_flag = (i & (i - 1)) != 0;
1686 QuicPacketEntropyHash packet_entropy_hash = 0;
1687 if (entropy_flag) {
1688 packet_entropy_hash = 1 << (i % 8);
1690 QuicPacket* packet = ConstructDataPacket(i, 0, entropy_flag);
1691 connection_.SendOrQueuePacket(
1692 i, packet, packet_entropy_hash, HAS_RETRANSMITTABLE_DATA);
1694 if (is_missing) {
1695 missing_packets.insert(i);
1696 continue;
1699 entropy_hash ^= packet_entropy_hash;
1701 EXPECT_TRUE(QuicConnectionPeer::IsValidEntropy(
1702 &connection_, max_sequence_number, missing_packets, entropy_hash))
1703 << "";
1706 // TODO(satyamsehkhar): Add more test when we start supporting more versions.
1707 TEST_F(QuicConnectionTest, SendVersionNegotiationPacket) {
1708 QuicVersionTag kRandomVersion = 143;
1709 QuicFramerPeer::SetVersion(&framer_, kRandomVersion);
1711 QuicPacketHeader header;
1712 header.public_header.guid = guid_;
1713 header.public_header.reset_flag = false;
1714 header.public_header.version_flag = true;
1715 header.entropy_flag = false;
1716 header.fec_flag = false;
1717 header.fec_entropy_flag = false;
1718 header.packet_sequence_number = 12;
1719 header.fec_group = 0;
1721 QuicFrames frames;
1722 QuicFrame frame(&frame1_);
1723 frames.push_back(frame);
1724 scoped_ptr<QuicPacket> packet(
1725 framer_.ConstructFrameDataPacket(header, frames).packet);
1726 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(12, *packet));
1728 QuicFramerPeer::SetVersion(&framer_, kQuicVersion1);
1729 connection_.set_is_server(true);
1730 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
1731 EXPECT_TRUE(helper_->version_negotiation_packet() != NULL);
1732 EXPECT_EQ(1u,
1733 helper_->version_negotiation_packet()->versions.size());
1734 EXPECT_EQ(kQuicVersion1,
1735 helper_->version_negotiation_packet()->versions[0]);
1738 TEST_F(QuicConnectionTest, CheckSendStats) {
1739 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(3);
1740 EXPECT_CALL(*send_algorithm_,
1741 SentPacket(_, _, _, NOT_RETRANSMISSION));
1742 connection_.SendStreamData(1u, "first", 0, !kFin);
1743 size_t first_packet_size = last_sent_packet_size();
1745 EXPECT_CALL(*send_algorithm_,
1746 SentPacket(_, _, _, NOT_RETRANSMISSION)).Times(2);
1747 connection_.SendStreamData(1u, "second", 0, !kFin);
1748 size_t second_packet_size = last_sent_packet_size();
1750 // 2 retransmissions due to rto, 1 due to explicit nack.
1751 EXPECT_CALL(*send_algorithm_,
1752 SentPacket(_, _, _, IS_RETRANSMISSION)).Times(3);
1754 // Retransmit due to RTO.
1755 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10));
1756 connection_.OnRetransmissionTimeout();
1758 // Retransmit due to explicit nacks
1759 QuicAckFrame nack_three(4, QuicTime::Zero(), 0);
1760 nack_three.received_info.missing_packets.insert(3);
1761 nack_three.received_info.entropy_hash =
1762 QuicConnectionPeer::GetSentEntropyHash(&connection_, 4) ^
1763 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^
1764 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2);
1765 QuicFrame frame(&nack_three);
1766 EXPECT_CALL(visitor_, OnAck(_));
1767 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1);
1768 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
1770 ProcessFramePacket(frame);
1771 ProcessFramePacket(frame);
1772 size_t ack_packet_size = last_sent_packet_size();
1773 ProcessFramePacket(frame);
1775 EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillOnce(
1776 Return(QuicTime::Delta::Zero()));
1777 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(
1778 Return(QuicBandwidth::Zero()));
1780 const QuicConnectionStats& stats = connection_.GetStats();
1781 EXPECT_EQ(3 * first_packet_size + 2 * second_packet_size + ack_packet_size -
1782 kQuicVersionSize, stats.bytes_sent);
1783 EXPECT_EQ(6u, stats.packets_sent);
1784 EXPECT_EQ(2 * first_packet_size + second_packet_size - kQuicVersionSize,
1785 stats.bytes_retransmitted);
1786 EXPECT_EQ(3u, stats.packets_retransmitted);
1787 EXPECT_EQ(2u, stats.rto_count);
1790 TEST_F(QuicConnectionTest, CheckReceiveStats) {
1791 size_t received_bytes = 0;
1792 received_bytes += ProcessFecProtectedPacket(1, false, !kEntropyFlag);
1793 received_bytes += ProcessFecProtectedPacket(3, false, !kEntropyFlag);
1794 // Should be counted against dropped packets.
1795 received_bytes += ProcessDataPacket(3, 1, !kEntropyFlag);
1796 received_bytes += ProcessFecPacket(4, 1, true, !kEntropyFlag); // Fec packet
1798 EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillOnce(
1799 Return(QuicTime::Delta::Zero()));
1800 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(
1801 Return(QuicBandwidth::Zero()));
1803 const QuicConnectionStats& stats = connection_.GetStats();
1804 EXPECT_EQ(received_bytes, stats.bytes_received);
1805 EXPECT_EQ(4u, stats.packets_received);
1807 EXPECT_EQ(1u, stats.packets_revived);
1808 EXPECT_EQ(1u, stats.packets_dropped);
1811 } // namespace
1812 } // namespace test
1813 } // namespace net