1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/quic/quic_connection.h" 6 7 #include "base/basictypes.h" 8 #include "base/bind.h" 9 #include "net/base/net_errors.h" 10 #include "net/quic/congestion_control/receive_algorithm_interface.h" 11 #include "net/quic/congestion_control/send_algorithm_interface.h" 12 #include "net/quic/crypto/null_encrypter.h" 13 #include "net/quic/crypto/quic_decrypter.h" 14 #include "net/quic/crypto/quic_encrypter.h" 15 #include "net/quic/crypto/quic_random.h" 16 #include "net/quic/quic_protocol.h" 17 #include "net/quic/quic_utils.h" 18 #include "net/quic/test_tools/mock_clock.h" 19 #include "net/quic/test_tools/mock_random.h" 20 #include "net/quic/test_tools/quic_connection_peer.h" 21 #include "net/quic/test_tools/quic_framer_peer.h" 22 #include "net/quic/test_tools/quic_packet_creator_peer.h" 23 #include "net/quic/test_tools/quic_test_utils.h" 24 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gtest/include/gtest/gtest.h" 26 27 using base::StringPiece; 28 using std::map; 29 using std::vector; 30 using testing::_; 31 using testing::AnyNumber; 32 using testing::Between; 33 using testing::ContainerEq; 34 using testing::DoAll; 35 using testing::InSequence; 36 using testing::InvokeWithoutArgs; 37 using testing::Return; 38 using testing::StrictMock; 39 using testing::SaveArg; 40 41 namespace net { 42 namespace test { 43 namespace { 44 45 const char data1[] = "foo"; 46 const char data2[] = "bar"; 47 48 const bool kFin = true; 49 const bool kEntropyFlag = true; 50 51 const QuicPacketEntropyHash kTestEntropyHash = 76; 52 53 class TestReceiveAlgorithm : public ReceiveAlgorithmInterface { 54 public: 55 explicit TestReceiveAlgorithm(QuicCongestionFeedbackFrame* feedback) 56 : feedback_(feedback) { 57 } 58 59 bool GenerateCongestionFeedback( 60 QuicCongestionFeedbackFrame* congestion_feedback) { 61 if (feedback_ == NULL) { 62 return false; 63 } 64 *congestion_feedback = *feedback_; 65 return true; 66 } 67 68 MOCK_METHOD4(RecordIncomingPacket, 69 void(QuicByteCount, QuicPacketSequenceNumber, QuicTime, bool)); 70 71 private: 72 QuicCongestionFeedbackFrame* feedback_; 73 74 DISALLOW_COPY_AND_ASSIGN(TestReceiveAlgorithm); 75 }; 76 77 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message. 78 class TaggingEncrypter : public QuicEncrypter { 79 public: 80 explicit TaggingEncrypter(uint8 tag) 81 : tag_(tag) { 82 } 83 84 virtual ~TaggingEncrypter() {} 85 86 // QuicEncrypter interface. 87 virtual bool SetKey(StringPiece key) OVERRIDE { return true; } 88 virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE { 89 return true; 90 } 91 92 virtual bool Encrypt(StringPiece nonce, 93 StringPiece associated_data, 94 StringPiece plaintext, 95 unsigned char* output) OVERRIDE { 96 memcpy(output, plaintext.data(), plaintext.size()); 97 output += plaintext.size(); 98 memset(output, tag_, kTagSize); 99 return true; 100 } 101 102 virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number, 103 StringPiece associated_data, 104 StringPiece plaintext) OVERRIDE { 105 const size_t len = plaintext.size() + kTagSize; 106 uint8* buffer = new uint8[len]; 107 Encrypt(StringPiece(), associated_data, plaintext, buffer); 108 return new QuicData(reinterpret_cast<char*>(buffer), len, true); 109 } 110 111 virtual size_t GetKeySize() const OVERRIDE { return 0; } 112 virtual size_t GetNoncePrefixSize() const OVERRIDE { return 0; } 113 114 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE { 115 return ciphertext_size - kTagSize; 116 } 117 118 virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE { 119 return plaintext_size + kTagSize; 120 } 121 122 virtual StringPiece GetKey() const OVERRIDE { 123 return StringPiece(); 124 } 125 126 virtual StringPiece GetNoncePrefix() const OVERRIDE { 127 return StringPiece(); 128 } 129 130 private: 131 enum { 132 kTagSize = 12, 133 }; 134 135 const uint8 tag_; 136 }; 137 138 // TaggingDecrypter ensures that the final kTagSize bytes of the message all 139 // have the same value and then removes them. 140 class TaggingDecrypter : public QuicDecrypter { 141 public: 142 virtual ~TaggingDecrypter() {} 143 144 // QuicDecrypter interface 145 virtual bool SetKey(StringPiece key) OVERRIDE { return true; } 146 virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE { 147 return true; 148 } 149 150 virtual bool Decrypt(StringPiece nonce, 151 StringPiece associated_data, 152 StringPiece ciphertext, 153 unsigned char* output, 154 size_t* output_length) OVERRIDE { 155 if (ciphertext.size() < kTagSize) { 156 return false; 157 } 158 if (!CheckTag(ciphertext, GetTag(ciphertext))) { 159 return false; 160 } 161 *output_length = ciphertext.size() - kTagSize; 162 memcpy(output, ciphertext.data(), *output_length); 163 return true; 164 } 165 166 virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number, 167 StringPiece associated_data, 168 StringPiece ciphertext) OVERRIDE { 169 if (ciphertext.size() < kTagSize) { 170 return NULL; 171 } 172 if (!CheckTag(ciphertext, GetTag(ciphertext))) { 173 return NULL; 174 } 175 const size_t len = ciphertext.size() - kTagSize; 176 uint8* buf = new uint8[len]; 177 memcpy(buf, ciphertext.data(), len); 178 return new QuicData(reinterpret_cast<char*>(buf), len, 179 true /* owns buffer */); 180 } 181 182 virtual StringPiece GetKey() const OVERRIDE { return StringPiece(); } 183 virtual StringPiece GetNoncePrefix() const OVERRIDE { return StringPiece(); } 184 185 protected: 186 virtual uint8 GetTag(StringPiece ciphertext) { 187 return ciphertext.data()[ciphertext.size()-1]; 188 } 189 190 private: 191 enum { 192 kTagSize = 12, 193 }; 194 195 bool CheckTag(StringPiece ciphertext, uint8 tag) { 196 for (size_t i = ciphertext.size() - kTagSize; i < ciphertext.size(); i++) { 197 if (ciphertext.data()[i] != tag) { 198 return false; 199 } 200 } 201 202 return true; 203 } 204 }; 205 206 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message 207 // match the expected value. 208 class StrictTaggingDecrypter : public TaggingDecrypter { 209 public: 210 explicit StrictTaggingDecrypter(uint8 tag) : tag_(tag) {} 211 virtual ~StrictTaggingDecrypter() {} 212 213 // TaggingQuicDecrypter 214 virtual uint8 GetTag(StringPiece ciphertext) OVERRIDE { 215 return tag_; 216 } 217 218 private: 219 const uint8 tag_; 220 }; 221 222 class TestConnectionHelper : public QuicConnectionHelperInterface { 223 public: 224 class TestAlarm : public QuicAlarm { 225 public: 226 explicit TestAlarm(QuicAlarm::Delegate* delegate) 227 : QuicAlarm(delegate) { 228 } 229 230 virtual void SetImpl() OVERRIDE {} 231 virtual void CancelImpl() OVERRIDE {} 232 }; 233 234 TestConnectionHelper(MockClock* clock, MockRandom* random_generator) 235 : clock_(clock), 236 random_generator_(random_generator), 237 blocked_(false), 238 is_server_(true), 239 use_tagging_decrypter_(false), 240 packets_write_attempts_(0) { 241 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); 242 } 243 244 // QuicConnectionHelperInterface 245 virtual void SetConnection(QuicConnection* connection) OVERRIDE {} 246 247 virtual const QuicClock* GetClock() const OVERRIDE { 248 return clock_; 249 } 250 251 virtual QuicRandom* GetRandomGenerator() OVERRIDE { 252 return random_generator_; 253 } 254 255 virtual int WritePacketToWire(const QuicEncryptedPacket& packet, 256 int* error) OVERRIDE { 257 ++packets_write_attempts_; 258 259 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { 260 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, 261 sizeof(final_bytes_of_last_packet_)); 262 } 263 264 QuicFramer framer(QuicVersionMax(), QuicTime::Zero(), is_server_); 265 if (use_tagging_decrypter_) { 266 framer.SetDecrypter(new TaggingDecrypter); 267 } 268 FramerVisitorCapturingFrames visitor; 269 framer.set_visitor(&visitor); 270 EXPECT_TRUE(framer.ProcessPacket(packet)); 271 header_ = *visitor.header(); 272 frame_count_ = visitor.frame_count(); 273 if (visitor.ack()) { 274 ack_.reset(new QuicAckFrame(*visitor.ack())); 275 } 276 if (visitor.feedback()) { 277 feedback_.reset(new QuicCongestionFeedbackFrame(*visitor.feedback())); 278 } 279 if (visitor.stream_frames() != NULL && !visitor.stream_frames()->empty()) { 280 stream_frames_ = *visitor.stream_frames(); 281 } 282 if (visitor.version_negotiation_packet() != NULL) { 283 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket( 284 *visitor.version_negotiation_packet())); 285 } 286 if (blocked_) { 287 *error = ERR_IO_PENDING; 288 return -1; 289 } 290 *error = 0; 291 last_packet_size_ = packet.length(); 292 return last_packet_size_; 293 } 294 295 virtual bool IsWriteBlockedDataBuffered() OVERRIDE { 296 return false; 297 } 298 299 virtual bool IsWriteBlocked(int error) OVERRIDE { 300 return error == ERR_IO_PENDING; 301 } 302 303 virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) OVERRIDE { 304 return new TestAlarm(delegate); 305 } 306 307 QuicPacketHeader* header() { return &header_; } 308 309 size_t frame_count() const { return frame_count_; } 310 311 QuicAckFrame* ack() { return ack_.get(); } 312 313 QuicCongestionFeedbackFrame* feedback() { return feedback_.get(); } 314 315 const vector<QuicStreamFrame>* stream_frames() const { 316 return &stream_frames_; 317 } 318 319 size_t last_packet_size() { 320 return last_packet_size_; 321 } 322 323 QuicVersionNegotiationPacket* version_negotiation_packet() { 324 return version_negotiation_packet_.get(); 325 } 326 327 void set_blocked(bool blocked) { blocked_ = blocked; } 328 329 void set_is_server(bool is_server) { is_server_ = is_server; } 330 331 // final_bytes_of_last_packet_ returns the last four bytes of the previous 332 // packet as a little-endian, uint32. This is intended to be used with a 333 // TaggingEncrypter so that tests can determine which encrypter was used for 334 // a given packet. 335 uint32 final_bytes_of_last_packet() { return final_bytes_of_last_packet_; } 336 337 void use_tagging_decrypter() { 338 use_tagging_decrypter_ = true; 339 } 340 341 uint32 packets_write_attempts() { return packets_write_attempts_; } 342 343 private: 344 MockClock* clock_; 345 MockRandom* random_generator_; 346 QuicPacketHeader header_; 347 size_t frame_count_; 348 scoped_ptr<QuicAckFrame> ack_; 349 scoped_ptr<QuicCongestionFeedbackFrame> feedback_; 350 vector<QuicStreamFrame> stream_frames_; 351 scoped_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_; 352 size_t last_packet_size_; 353 bool blocked_; 354 bool is_server_; 355 uint32 final_bytes_of_last_packet_; 356 bool use_tagging_decrypter_; 357 uint32 packets_write_attempts_; 358 359 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper); 360 }; 361 362 class TestConnection : public QuicConnection { 363 public: 364 TestConnection(QuicGuid guid, 365 IPEndPoint address, 366 TestConnectionHelper* helper, 367 bool is_server) 368 : QuicConnection(guid, address, helper, is_server, QuicVersionMax()), 369 helper_(helper) { 370 helper_->set_is_server(!is_server); 371 } 372 373 void SendAck() { 374 QuicConnectionPeer::SendAck(this); 375 } 376 377 void SetReceiveAlgorithm(TestReceiveAlgorithm* receive_algorithm) { 378 QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm); 379 } 380 381 void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) { 382 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm); 383 } 384 385 QuicConsumedData SendStreamData1() { 386 return SendStreamData(1u, "food", 0, !kFin); 387 } 388 389 QuicConsumedData SendStreamData2() { 390 return SendStreamData(2u, "food2", 0, !kFin); 391 } 392 393 bool is_server() { 394 return QuicConnectionPeer::IsServer(this); 395 } 396 397 void set_version(QuicVersion version) { 398 framer_.set_version(version); 399 } 400 401 void set_is_server(bool is_server) { 402 helper_->set_is_server(!is_server); 403 QuicPacketCreatorPeer::SetIsServer( 404 QuicConnectionPeer::GetPacketCreator(this), is_server); 405 QuicConnectionPeer::SetIsServer(this, is_server); 406 } 407 408 QuicAlarm* GetAckAlarm() { 409 return QuicConnectionPeer::GetAckAlarm(this); 410 } 411 412 QuicAlarm* GetRetransmissionAlarm() { 413 return QuicConnectionPeer::GetRetransmissionAlarm(this); 414 } 415 416 QuicAlarm* GetSendAlarm() { 417 return QuicConnectionPeer::GetSendAlarm(this); 418 } 419 420 QuicAlarm* GetTimeoutAlarm() { 421 return QuicConnectionPeer::GetTimeoutAlarm(this); 422 } 423 424 using QuicConnection::SendOrQueuePacket; 425 using QuicConnection::SelectMutualVersion; 426 427 private: 428 TestConnectionHelper* helper_; 429 430 DISALLOW_COPY_AND_ASSIGN(TestConnection); 431 }; 432 433 class QuicConnectionTest : public ::testing::Test { 434 protected: 435 QuicConnectionTest() 436 : guid_(42), 437 framer_(QuicVersionMax(), QuicTime::Zero(), false), 438 creator_(guid_, &framer_, QuicRandom::GetInstance(), false), 439 send_algorithm_(new StrictMock<MockSendAlgorithm>), 440 helper_(new TestConnectionHelper(&clock_, &random_generator_)), 441 connection_(guid_, IPEndPoint(), helper_, false), 442 frame1_(1, false, 0, data1), 443 frame2_(1, false, 3, data2), 444 accept_packet_(true) { 445 connection_.set_visitor(&visitor_); 446 connection_.SetSendAlgorithm(send_algorithm_); 447 // Simplify tests by not sending feedback unless specifically configured. 448 SetFeedback(NULL); 449 EXPECT_CALL( 450 *send_algorithm_, TimeUntilSend(_, _, _, _)).WillRepeatedly(Return( 451 QuicTime::Delta::Zero())); 452 EXPECT_CALL(*receive_algorithm_, 453 RecordIncomingPacket(_, _, _, _)).Times(AnyNumber()); 454 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(AnyNumber()); 455 EXPECT_CALL(*send_algorithm_, RetransmissionDelay()).WillRepeatedly( 456 Return(QuicTime::Delta::Zero())); 457 } 458 459 QuicAckFrame* outgoing_ack() { 460 outgoing_ack_.reset(QuicConnectionPeer::CreateAckFrame(&connection_)); 461 return outgoing_ack_.get(); 462 } 463 464 QuicAckFrame* last_ack() { 465 return helper_->ack(); 466 } 467 468 QuicCongestionFeedbackFrame* last_feedback() { 469 return helper_->feedback(); 470 } 471 472 QuicPacketHeader* last_header() { 473 return helper_->header(); 474 } 475 476 size_t last_sent_packet_size() { 477 return helper_->last_packet_size(); 478 } 479 480 uint32 final_bytes_of_last_packet() { 481 return helper_->final_bytes_of_last_packet(); 482 } 483 484 void use_tagging_decrypter() { 485 helper_->use_tagging_decrypter(); 486 } 487 488 void ProcessPacket(QuicPacketSequenceNumber number) { 489 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)) 490 .WillOnce(Return(accept_packet_)); 491 ProcessDataPacket(number, 0, !kEntropyFlag); 492 } 493 494 QuicPacketEntropyHash ProcessFramePacket(QuicFrame frame) { 495 QuicFrames frames; 496 frames.push_back(QuicFrame(frame)); 497 QuicPacketCreatorPeer::SetSendVersionInPacket(&creator_, 498 connection_.is_server()); 499 SerializedPacket serialized_packet = creator_.SerializeAllFrames(frames); 500 scoped_ptr<QuicPacket> packet(serialized_packet.packet); 501 scoped_ptr<QuicEncryptedPacket> encrypted( 502 framer_.EncryptPacket(ENCRYPTION_NONE, 503 serialized_packet.sequence_number, *packet)); 504 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 505 return serialized_packet.entropy_hash; 506 } 507 508 size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number, 509 bool expect_revival) { 510 if (expect_revival) { 511 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).Times(2).WillRepeatedly( 512 Return(accept_packet_)); 513 } else { 514 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce( 515 Return(accept_packet_)); 516 } 517 return ProcessDataPacket(number, 1, !kEntropyFlag); 518 } 519 520 size_t ProcessDataPacket(QuicPacketSequenceNumber number, 521 QuicFecGroupNumber fec_group, 522 bool entropy_flag) { 523 return ProcessDataPacketAtLevel(number, fec_group, entropy_flag, 524 ENCRYPTION_NONE); 525 } 526 527 size_t ProcessDataPacketAtLevel(QuicPacketSequenceNumber number, 528 QuicFecGroupNumber fec_group, 529 bool entropy_flag, 530 EncryptionLevel level) { 531 scoped_ptr<QuicPacket> packet(ConstructDataPacket(number, fec_group, 532 entropy_flag)); 533 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket( 534 level, number, *packet)); 535 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 536 return encrypted->length(); 537 } 538 539 void ProcessClosePacket(QuicPacketSequenceNumber number, 540 QuicFecGroupNumber fec_group) { 541 scoped_ptr<QuicPacket> packet(ConstructClosePacket(number, fec_group)); 542 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket( 543 ENCRYPTION_NONE, number, *packet)); 544 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 545 } 546 547 size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number, 548 bool expect_revival, bool entropy_flag) { 549 if (expect_revival) { 550 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce(DoAll( 551 SaveArg<2>(&revived_header_), Return(accept_packet_))); 552 } 553 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce(Return(accept_packet_)) 554 .RetiresOnSaturation(); 555 return ProcessDataPacket(number, 1, entropy_flag); 556 } 557 558 // Sends an FEC packet that covers the packets that would have been sent. 559 size_t ProcessFecPacket(QuicPacketSequenceNumber number, 560 QuicPacketSequenceNumber min_protected_packet, 561 bool expect_revival, 562 bool entropy_flag) { 563 if (expect_revival) { 564 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce(DoAll( 565 SaveArg<2>(&revived_header_), Return(accept_packet_))); 566 } 567 568 // Construct the decrypted data packet so we can compute the correct 569 // redundancy. 570 scoped_ptr<QuicPacket> data_packet(ConstructDataPacket(number, 1, 571 !kEntropyFlag)); 572 573 header_.public_header.guid = guid_; 574 header_.public_header.reset_flag = false; 575 header_.public_header.version_flag = false; 576 header_.entropy_flag = entropy_flag; 577 header_.fec_flag = true; 578 header_.packet_sequence_number = number; 579 header_.is_in_fec_group = IN_FEC_GROUP; 580 header_.fec_group = min_protected_packet; 581 QuicFecData fec_data; 582 fec_data.fec_group = header_.fec_group; 583 // Since all data packets in this test have the same payload, the 584 // redundancy is either equal to that payload or the xor of that payload 585 // with itself, depending on the number of packets. 586 if (((number - min_protected_packet) % 2) == 0) { 587 for (size_t i = GetStartOfFecProtectedData( 588 header_.public_header.guid_length, 589 header_.public_header.version_flag, 590 header_.public_header.sequence_number_length); 591 i < data_packet->length(); ++i) { 592 data_packet->mutable_data()[i] ^= data_packet->data()[i]; 593 } 594 } 595 fec_data.redundancy = data_packet->FecProtectedData(); 596 scoped_ptr<QuicPacket> fec_packet( 597 framer_.BuildFecPacket(header_, fec_data).packet); 598 scoped_ptr<QuicEncryptedPacket> encrypted( 599 framer_.EncryptPacket(ENCRYPTION_NONE, number, *fec_packet)); 600 601 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 602 return encrypted->length(); 603 } 604 605 QuicByteCount SendStreamDataToPeer(QuicStreamId id, StringPiece data, 606 QuicStreamOffset offset, bool fin, 607 QuicPacketSequenceNumber* last_packet) { 608 QuicByteCount packet_size; 609 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).WillOnce( 610 SaveArg<2>(&packet_size)); 611 connection_.SendStreamData(id, data, offset, fin); 612 if (last_packet != NULL) { 613 *last_packet = 614 QuicConnectionPeer::GetPacketCreator(&connection_)->sequence_number(); 615 } 616 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(AnyNumber()); 617 return packet_size; 618 } 619 620 void SendAckPacketToPeer() { 621 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(1); 622 connection_.SendAck(); 623 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(AnyNumber()); 624 } 625 626 QuicPacketEntropyHash ProcessAckPacket(QuicAckFrame* frame, 627 bool expect_writes) { 628 if (expect_writes) { 629 EXPECT_CALL(visitor_, OnCanWrite()).Times(1).WillOnce(Return(true)); 630 } 631 return ProcessFramePacket(QuicFrame(frame)); 632 } 633 634 QuicPacketEntropyHash ProcessGoAwayPacket(QuicGoAwayFrame* frame) { 635 return ProcessFramePacket(QuicFrame(frame)); 636 } 637 638 bool IsMissing(QuicPacketSequenceNumber number) { 639 return IsAwaitingPacket(outgoing_ack()->received_info, number); 640 } 641 642 QuicPacket* ConstructDataPacket(QuicPacketSequenceNumber number, 643 QuicFecGroupNumber fec_group, 644 bool entropy_flag) { 645 header_.public_header.guid = guid_; 646 header_.public_header.reset_flag = false; 647 header_.public_header.version_flag = false; 648 header_.entropy_flag = entropy_flag; 649 header_.fec_flag = false; 650 header_.packet_sequence_number = number; 651 header_.is_in_fec_group = fec_group == 0u ? NOT_IN_FEC_GROUP : IN_FEC_GROUP; 652 header_.fec_group = fec_group; 653 654 QuicFrames frames; 655 QuicFrame frame(&frame1_); 656 frames.push_back(frame); 657 QuicPacket* packet = 658 framer_.BuildUnsizedDataPacket(header_, frames).packet; 659 EXPECT_TRUE(packet != NULL); 660 return packet; 661 } 662 663 QuicPacket* ConstructClosePacket(QuicPacketSequenceNumber number, 664 QuicFecGroupNumber fec_group) { 665 header_.public_header.guid = guid_; 666 header_.packet_sequence_number = number; 667 header_.public_header.reset_flag = false; 668 header_.public_header.version_flag = false; 669 header_.entropy_flag = false; 670 header_.fec_flag = false; 671 header_.is_in_fec_group = fec_group == 0u ? NOT_IN_FEC_GROUP : IN_FEC_GROUP; 672 header_.fec_group = fec_group; 673 674 QuicConnectionCloseFrame qccf; 675 qccf.error_code = QUIC_PEER_GOING_AWAY; 676 qccf.ack_frame = QuicAckFrame(0, QuicTime::Zero(), 1); 677 678 QuicFrames frames; 679 QuicFrame frame(&qccf); 680 frames.push_back(frame); 681 QuicPacket* packet = 682 framer_.BuildUnsizedDataPacket(header_, frames).packet; 683 EXPECT_TRUE(packet != NULL); 684 return packet; 685 } 686 687 void SetFeedback(QuicCongestionFeedbackFrame* feedback) { 688 receive_algorithm_ = new TestReceiveAlgorithm(feedback); 689 connection_.SetReceiveAlgorithm(receive_algorithm_); 690 } 691 692 QuicGuid guid_; 693 QuicFramer framer_; 694 QuicPacketCreator creator_; 695 696 MockSendAlgorithm* send_algorithm_; 697 TestReceiveAlgorithm* receive_algorithm_; 698 MockClock clock_; 699 MockRandom random_generator_; 700 TestConnectionHelper* helper_; 701 TestConnection connection_; 702 testing::StrictMock<MockConnectionVisitor> visitor_; 703 704 QuicPacketHeader header_; 705 QuicPacketHeader revived_header_; 706 QuicStreamFrame frame1_; 707 QuicStreamFrame frame2_; 708 scoped_ptr<QuicAckFrame> outgoing_ack_; 709 bool accept_packet_; 710 711 private: 712 DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest); 713 }; 714 715 TEST_F(QuicConnectionTest, PacketsInOrder) { 716 ProcessPacket(1); 717 EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed); 718 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size()); 719 720 ProcessPacket(2); 721 EXPECT_EQ(2u, outgoing_ack()->received_info.largest_observed); 722 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size()); 723 724 ProcessPacket(3); 725 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 726 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size()); 727 } 728 729 TEST_F(QuicConnectionTest, PacketsRejected) { 730 ProcessPacket(1); 731 EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed); 732 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size()); 733 734 accept_packet_ = false; 735 ProcessPacket(2); 736 // We should not have an ack for two. 737 EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed); 738 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size()); 739 } 740 741 TEST_F(QuicConnectionTest, PacketsOutOfOrder) { 742 ProcessPacket(3); 743 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 744 EXPECT_TRUE(IsMissing(2)); 745 EXPECT_TRUE(IsMissing(1)); 746 747 ProcessPacket(2); 748 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 749 EXPECT_FALSE(IsMissing(2)); 750 EXPECT_TRUE(IsMissing(1)); 751 752 ProcessPacket(1); 753 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 754 EXPECT_FALSE(IsMissing(2)); 755 EXPECT_FALSE(IsMissing(1)); 756 } 757 758 TEST_F(QuicConnectionTest, DuplicatePacket) { 759 ProcessPacket(3); 760 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 761 EXPECT_TRUE(IsMissing(2)); 762 EXPECT_TRUE(IsMissing(1)); 763 764 // Send packet 3 again, but do not set the expectation that 765 // the visitor OnPacket() will be called. 766 ProcessDataPacket(3, 0, !kEntropyFlag); 767 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 768 EXPECT_TRUE(IsMissing(2)); 769 EXPECT_TRUE(IsMissing(1)); 770 } 771 772 TEST_F(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndLeastAwaiting) { 773 ProcessPacket(3); 774 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 775 EXPECT_TRUE(IsMissing(2)); 776 EXPECT_TRUE(IsMissing(1)); 777 778 ProcessPacket(2); 779 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 780 EXPECT_TRUE(IsMissing(1)); 781 782 ProcessPacket(5); 783 EXPECT_EQ(5u, outgoing_ack()->received_info.largest_observed); 784 EXPECT_TRUE(IsMissing(1)); 785 EXPECT_TRUE(IsMissing(4)); 786 787 // Pretend at this point the client has gotten acks for 2 and 3 and 1 is a 788 // packet the peer will not retransmit. It indicates this by sending 'least 789 // awaiting' is 4. The connection should then realize 1 will not be 790 // retransmitted, and will remove it from the missing list. 791 creator_.set_sequence_number(5); 792 QuicAckFrame frame(0, QuicTime::Zero(), 4); 793 ProcessAckPacket(&frame, true); 794 795 // Force an ack to be sent. 796 SendAckPacketToPeer(); 797 EXPECT_TRUE(IsMissing(4)); 798 } 799 800 TEST_F(QuicConnectionTest, RejectPacketTooFarOut) { 801 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a 802 // packet call to the visitor. 803 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_PACKET_HEADER, false)); 804 ProcessDataPacket(6000, 0, !kEntropyFlag); 805 } 806 807 TEST_F(QuicConnectionTest, TruncatedAck) { 808 EXPECT_CALL(visitor_, OnAck(_)).Times(testing::AnyNumber()); 809 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2); 810 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1); 811 for (int i = 0; i < 200; ++i) { 812 SendStreamDataToPeer(1, "foo", i * 3, !kFin, NULL); 813 } 814 815 QuicAckFrame frame(0, QuicTime::Zero(), 1); 816 frame.received_info.largest_observed = 192; 817 InsertMissingPacketsBetween(&frame.received_info, 1, 192); 818 frame.received_info.entropy_hash = 819 QuicConnectionPeer::GetSentEntropyHash(&connection_, 192) ^ 820 QuicConnectionPeer::GetSentEntropyHash(&connection_, 191); 821 822 ProcessAckPacket(&frame, true); 823 824 EXPECT_TRUE(QuicConnectionPeer::GetReceivedTruncatedAck(&connection_)); 825 826 frame.received_info.missing_packets.erase(191); 827 frame.received_info.entropy_hash = 828 QuicConnectionPeer::GetSentEntropyHash(&connection_, 192) ^ 829 QuicConnectionPeer::GetSentEntropyHash(&connection_, 190); 830 831 ProcessAckPacket(&frame, true); 832 EXPECT_FALSE(QuicConnectionPeer::GetReceivedTruncatedAck(&connection_)); 833 } 834 835 TEST_F(QuicConnectionTest, AckReceiptCausesAckSendBadEntropy) { 836 ProcessPacket(1); 837 // Delay sending, then queue up an ack. 838 EXPECT_CALL(*send_algorithm_, 839 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 840 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 841 QuicConnectionPeer::SendAck(&connection_); 842 843 // Process an ack with a least unacked of the received ack. 844 // This causes an ack to be sent when TimeUntilSend returns 0. 845 EXPECT_CALL(*send_algorithm_, 846 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly( 847 testing::Return(QuicTime::Delta::Zero())); 848 // Skip a packet and then record an ack. 849 creator_.set_sequence_number(2); 850 QuicAckFrame frame(0, QuicTime::Zero(), 3); 851 ProcessAckPacket(&frame, true); 852 } 853 854 TEST_F(QuicConnectionTest, AckReceiptCausesAckSend) { 855 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1); 856 QuicPacketSequenceNumber largest_observed; 857 QuicByteCount packet_size; 858 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, NOT_RETRANSMISSION)) 859 .WillOnce(DoAll(SaveArg<1>(&largest_observed), SaveArg<2>(&packet_size))); 860 EXPECT_CALL(*send_algorithm_, AbandoningPacket(1, _)).Times(1); 861 connection_.SendStreamData(1, "foo", 0, !kFin); 862 QuicAckFrame frame(1, QuicTime::Zero(), largest_observed); 863 frame.received_info.missing_packets.insert(largest_observed); 864 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash( 865 &connection_, largest_observed - 1); 866 ProcessAckPacket(&frame, true); 867 ProcessAckPacket(&frame, true); 868 // Third nack should retransmit the largest observed packet. 869 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, packet_size - kQuicVersionSize, 870 IS_RETRANSMISSION)); 871 ProcessAckPacket(&frame, true); 872 873 // Now if the peer sends an ack which still reports the retransmitted packet 874 // as missing, then that will count as a packet which instigates an ack. 875 ProcessAckPacket(&frame, true); 876 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, NOT_RETRANSMISSION)); 877 ProcessAckPacket(&frame, true); 878 879 // But an ack with no new missing packest will not send an ack. 880 frame.received_info.missing_packets.clear(); 881 ProcessAckPacket(&frame, true); 882 ProcessAckPacket(&frame, true); 883 } 884 885 TEST_F(QuicConnectionTest, LeastUnackedLower) { 886 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 887 SendStreamDataToPeer(1, "bar", 3, !kFin, NULL); 888 SendStreamDataToPeer(1, "eep", 6, !kFin, NULL); 889 890 // Start out saying the least unacked is 2 891 creator_.set_sequence_number(5); 892 QuicAckFrame frame(0, QuicTime::Zero(), 2); 893 ProcessAckPacket(&frame, true); 894 895 // Change it to 1, but lower the sequence number to fake out-of-order packets. 896 // This should be fine. 897 creator_.set_sequence_number(1); 898 QuicAckFrame frame2(0, QuicTime::Zero(), 1); 899 // The scheduler will not process out of order acks. 900 ProcessAckPacket(&frame2, false); 901 902 // Now claim it's one, but set the ordering so it was sent "after" the first 903 // one. This should cause a connection error. 904 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false)); 905 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 906 creator_.set_sequence_number(7); 907 ProcessAckPacket(&frame2, false); 908 } 909 910 TEST_F(QuicConnectionTest, LargestObservedLower) { 911 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 912 SendStreamDataToPeer(1, "bar", 3, !kFin, NULL); 913 SendStreamDataToPeer(1, "eep", 6, !kFin, NULL); 914 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2); 915 916 // Start out saying the largest observed is 2. 917 QuicAckFrame frame(2, QuicTime::Zero(), 0); 918 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash( 919 &connection_, 2); 920 EXPECT_CALL(visitor_, OnAck(_)); 921 ProcessAckPacket(&frame, true); 922 923 // Now change it to 1, and it should cause a connection error. 924 QuicAckFrame frame2(1, QuicTime::Zero(), 0); 925 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false)); 926 ProcessAckPacket(&frame2, false); 927 } 928 929 TEST_F(QuicConnectionTest, LeastUnackedGreaterThanPacketSequenceNumber) { 930 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false)); 931 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 932 // Create an ack with least_unacked is 2 in packet number 1. 933 creator_.set_sequence_number(0); 934 QuicAckFrame frame(0, QuicTime::Zero(), 2); 935 ProcessAckPacket(&frame, false); 936 } 937 938 TEST_F(QuicConnectionTest, 939 NackSequenceNumberGreaterThanLargestReceived) { 940 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 941 SendStreamDataToPeer(1, "bar", 3, !kFin, NULL); 942 SendStreamDataToPeer(1, "eep", 6, !kFin, NULL); 943 944 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false)); 945 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 946 QuicAckFrame frame(0, QuicTime::Zero(), 1); 947 frame.received_info.missing_packets.insert(3); 948 ProcessAckPacket(&frame, false); 949 } 950 951 TEST_F(QuicConnectionTest, AckUnsentData) { 952 // Ack a packet which has not been sent. 953 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_ACK_DATA, false)); 954 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 955 QuicAckFrame frame(1, QuicTime::Zero(), 0); 956 ProcessAckPacket(&frame, false); 957 } 958 959 TEST_F(QuicConnectionTest, AckAll) { 960 ProcessPacket(1); 961 962 creator_.set_sequence_number(1); 963 QuicAckFrame frame1(0, QuicTime::Zero(), 1); 964 ProcessAckPacket(&frame1, true); 965 } 966 967 TEST_F(QuicConnectionTest, BasicSending) { 968 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(6); 969 QuicPacketSequenceNumber last_packet; 970 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1 971 EXPECT_EQ(1u, last_packet); 972 SendAckPacketToPeer(); // Packet 2 973 974 EXPECT_EQ(1u, last_ack()->sent_info.least_unacked); 975 976 SendAckPacketToPeer(); // Packet 3 977 EXPECT_EQ(1u, last_ack()->sent_info.least_unacked); 978 979 SendStreamDataToPeer(1u, "bar", 3, !kFin, &last_packet); // Packet 4 980 EXPECT_EQ(4u, last_packet); 981 SendAckPacketToPeer(); // Packet 5 982 EXPECT_EQ(1u, last_ack()->sent_info.least_unacked); 983 984 SequenceNumberSet expected_acks; 985 expected_acks.insert(1); 986 987 // Peer acks up to packet 3. 988 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks))); 989 QuicAckFrame frame(3, QuicTime::Zero(), 0); 990 frame.received_info.entropy_hash = 991 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3); 992 ProcessAckPacket(&frame, true); 993 SendAckPacketToPeer(); // Packet 6 994 995 // As soon as we've acked one, we skip ack packets 2 and 3 and note lack of 996 // ack for 4. 997 EXPECT_EQ(4u, last_ack()->sent_info.least_unacked); 998 999 expected_acks.clear(); 1000 expected_acks.insert(4); 1001 1002 // Peer acks up to packet 4, the last packet. 1003 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks))); 1004 QuicAckFrame frame2(6, QuicTime::Zero(), 0); 1005 frame2.received_info.entropy_hash = 1006 QuicConnectionPeer::GetSentEntropyHash(&connection_, 6); 1007 ProcessAckPacket(&frame2, true); // Acks don't instigate acks. 1008 1009 // Verify that we did not send an ack. 1010 EXPECT_EQ(6u, last_header()->packet_sequence_number); 1011 1012 // So the last ack has not changed. 1013 EXPECT_EQ(4u, last_ack()->sent_info.least_unacked); 1014 1015 // If we force an ack, we shouldn't change our retransmit state. 1016 SendAckPacketToPeer(); // Packet 7 1017 EXPECT_EQ(7u, last_ack()->sent_info.least_unacked); 1018 1019 // But if we send more data it should. 1020 SendStreamDataToPeer(1, "eep", 6, !kFin, &last_packet); // Packet 8 1021 EXPECT_EQ(8u, last_packet); 1022 SendAckPacketToPeer(); // Packet 9 1023 EXPECT_EQ(8u, last_ack()->sent_info.least_unacked); 1024 } 1025 1026 TEST_F(QuicConnectionTest, FECSending) { 1027 // All packets carry version info till version is negotiated. 1028 size_t payload_length; 1029 connection_.options()->max_packet_length = 1030 GetPacketLengthForOneStream(connection_.version(), kIncludeVersion, 1031 IN_FEC_GROUP, &payload_length); 1032 // And send FEC every two packets. 1033 connection_.options()->max_packets_per_fec_group = 2; 1034 1035 // Send 4 data packets and 2 FEC packets. 1036 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(6); 1037 // The first stream frame will consume 2 fewer bytes than the other three. 1038 const string payload(payload_length * 4 - 6, 'a'); 1039 connection_.SendStreamData(1, payload, 0, !kFin); 1040 // Expect the FEC group to be closed after SendStreamData. 1041 EXPECT_FALSE(creator_.ShouldSendFec(true)); 1042 } 1043 1044 TEST_F(QuicConnectionTest, FECQueueing) { 1045 // All packets carry version info till version is negotiated. 1046 size_t payload_length; 1047 connection_.options()->max_packet_length = 1048 GetPacketLengthForOneStream(connection_.version(), kIncludeVersion, 1049 IN_FEC_GROUP, &payload_length); 1050 // And send FEC every two packets. 1051 connection_.options()->max_packets_per_fec_group = 2; 1052 1053 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1054 helper_->set_blocked(true); 1055 const string payload(payload_length, 'a'); 1056 connection_.SendStreamData(1, payload, 0, !kFin); 1057 EXPECT_FALSE(creator_.ShouldSendFec(true)); 1058 // Expect the first data packet and the fec packet to be queued. 1059 EXPECT_EQ(2u, connection_.NumQueuedPackets()); 1060 } 1061 1062 TEST_F(QuicConnectionTest, AbandonFECFromCongestionWindow) { 1063 connection_.options()->max_packets_per_fec_group = 1; 1064 // 1 Data and 1 FEC packet. 1065 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(2); 1066 connection_.SendStreamData(1, "foo", 0, !kFin); 1067 1068 // Larger timeout for FEC bytes to expire. 1069 const QuicTime::Delta retransmission_time = 1070 QuicTime::Delta::FromMilliseconds(5000); 1071 clock_.AdvanceTime(retransmission_time); 1072 1073 // Send only data packet. 1074 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(1); 1075 // Abandon both FEC and data packet. 1076 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(2); 1077 1078 connection_.OnRetransmissionTimeout(); 1079 } 1080 1081 TEST_F(QuicConnectionTest, DontAbandonAckedFEC) { 1082 connection_.options()->max_packets_per_fec_group = 1; 1083 const QuicPacketSequenceNumber sequence_number = 1084 QuicConnectionPeer::GetPacketCreator(&connection_)->sequence_number() + 1; 1085 1086 // 1 Data and 1 FEC packet. 1087 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(2); 1088 connection_.SendStreamData(1, "foo", 0, !kFin); 1089 1090 QuicAckFrame ack_fec(2, QuicTime::Zero(), 1); 1091 // Data packet missing. 1092 ack_fec.received_info.missing_packets.insert(1); 1093 ack_fec.received_info.entropy_hash = 1094 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^ 1095 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 1096 1097 EXPECT_CALL(visitor_, OnAck(_)).Times(1); 1098 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1); 1099 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1); 1100 1101 ProcessAckPacket(&ack_fec, true); 1102 1103 const QuicTime::Delta kDefaultRetransmissionTime = 1104 QuicTime::Delta::FromMilliseconds(5000); 1105 clock_.AdvanceTime(kDefaultRetransmissionTime); 1106 1107 // Abandon only data packet, FEC has been acked. 1108 EXPECT_CALL(*send_algorithm_, AbandoningPacket(sequence_number, _)).Times(1); 1109 // Send only data packet. 1110 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(1); 1111 connection_.OnRetransmissionTimeout(); 1112 } 1113 1114 TEST_F(QuicConnectionTest, FramePacking) { 1115 // Block the connection. 1116 connection_.GetSendAlarm()->Set( 1117 clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1))); 1118 1119 // Send an ack and two stream frames in 1 packet by queueing them. 1120 connection_.SendAck(); 1121 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll( 1122 IgnoreResult(InvokeWithoutArgs(&connection_, 1123 &TestConnection::SendStreamData1)), 1124 IgnoreResult(InvokeWithoutArgs(&connection_, 1125 &TestConnection::SendStreamData2)), 1126 Return(true))); 1127 1128 // Unblock the connection. 1129 connection_.GetSendAlarm()->Cancel(); 1130 EXPECT_CALL(*send_algorithm_, 1131 SentPacket(_, _, _, NOT_RETRANSMISSION)) 1132 .Times(1); 1133 connection_.OnCanWrite(); 1134 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1135 EXPECT_FALSE(connection_.HasQueuedData()); 1136 1137 // Parse the last packet and ensure it's an ack and two stream frames from 1138 // two different streams. 1139 EXPECT_EQ(3u, helper_->frame_count()); 1140 EXPECT_TRUE(helper_->ack()); 1141 EXPECT_EQ(2u, helper_->stream_frames()->size()); 1142 EXPECT_EQ(1u, (*helper_->stream_frames())[0].stream_id); 1143 EXPECT_EQ(2u, (*helper_->stream_frames())[1].stream_id); 1144 } 1145 1146 TEST_F(QuicConnectionTest, FramePackingFEC) { 1147 // Enable fec. 1148 connection_.options()->max_packets_per_fec_group = 6; 1149 // Block the connection. 1150 connection_.GetSendAlarm()->Set( 1151 clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1))); 1152 1153 // Send an ack and two stream frames in 1 packet by queueing them. 1154 connection_.SendAck(); 1155 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll( 1156 IgnoreResult(InvokeWithoutArgs(&connection_, 1157 &TestConnection::SendStreamData1)), 1158 IgnoreResult(InvokeWithoutArgs(&connection_, 1159 &TestConnection::SendStreamData2)), 1160 Return(true))); 1161 1162 // Unblock the connection. 1163 connection_.GetSendAlarm()->Cancel(); 1164 EXPECT_CALL(*send_algorithm_, 1165 SentPacket(_, _, _, NOT_RETRANSMISSION)).Times(2); 1166 connection_.OnCanWrite(); 1167 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1168 EXPECT_FALSE(connection_.HasQueuedData()); 1169 1170 // Parse the last packet and ensure it's in an fec group. 1171 EXPECT_EQ(1u, helper_->header()->fec_group); 1172 EXPECT_EQ(0u, helper_->frame_count()); 1173 } 1174 1175 TEST_F(QuicConnectionTest, OnCanWrite) { 1176 // Visitor's OnCanWill send data, but will return false. 1177 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll( 1178 IgnoreResult(InvokeWithoutArgs(&connection_, 1179 &TestConnection::SendStreamData1)), 1180 IgnoreResult(InvokeWithoutArgs(&connection_, 1181 &TestConnection::SendStreamData2)), 1182 Return(false))); 1183 1184 EXPECT_CALL(*send_algorithm_, 1185 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly( 1186 testing::Return(QuicTime::Delta::Zero())); 1187 1188 // Unblock the connection. 1189 connection_.OnCanWrite(); 1190 // Parse the last packet and ensure it's the two stream frames from 1191 // two different streams. 1192 EXPECT_EQ(2u, helper_->frame_count()); 1193 EXPECT_EQ(2u, helper_->stream_frames()->size()); 1194 EXPECT_EQ(1u, (*helper_->stream_frames())[0].stream_id); 1195 EXPECT_EQ(2u, (*helper_->stream_frames())[1].stream_id); 1196 } 1197 1198 TEST_F(QuicConnectionTest, RetransmitOnNack) { 1199 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2); 1200 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1); 1201 EXPECT_CALL(*send_algorithm_, AbandoningPacket(2, _)).Times(1); 1202 QuicPacketSequenceNumber last_packet; 1203 QuicByteCount second_packet_size; 1204 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1 1205 second_packet_size = 1206 SendStreamDataToPeer(1, "foos", 3, !kFin, &last_packet); // Packet 2 1207 SendStreamDataToPeer(1, "fooos", 7, !kFin, &last_packet); // Packet 3 1208 1209 SequenceNumberSet expected_acks; 1210 expected_acks.insert(1); 1211 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks))); 1212 1213 // Peer acks one but not two or three. Right now we only retransmit on 1214 // explicit nack, so it should not trigger a retransimission. 1215 QuicAckFrame ack_one(1, QuicTime::Zero(), 0); 1216 ack_one.received_info.entropy_hash = 1217 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 1218 ProcessAckPacket(&ack_one, true); 1219 ProcessAckPacket(&ack_one, true); 1220 ProcessAckPacket(&ack_one, true); 1221 1222 expected_acks.clear(); 1223 expected_acks.insert(3); 1224 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks))); 1225 1226 // Peer acks up to 3 with two explicitly missing. Two nacks should cause no 1227 // change. 1228 QuicAckFrame nack_two(3, QuicTime::Zero(), 0); 1229 nack_two.received_info.missing_packets.insert(2); 1230 nack_two.received_info.entropy_hash = 1231 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^ 1232 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^ 1233 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 1234 ProcessAckPacket(&nack_two, true); 1235 ProcessAckPacket(&nack_two, true); 1236 1237 // The third nack should trigger a retransimission. 1238 EXPECT_CALL(*send_algorithm_, 1239 SentPacket(_, _, second_packet_size - kQuicVersionSize, 1240 IS_RETRANSMISSION)).Times(1); 1241 ProcessAckPacket(&nack_two, true); 1242 } 1243 1244 TEST_F(QuicConnectionTest, RetransmitNackedLargestObserved) { 1245 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1); 1246 QuicPacketSequenceNumber largest_observed; 1247 QuicByteCount packet_size; 1248 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, NOT_RETRANSMISSION)) 1249 .WillOnce(DoAll(SaveArg<1>(&largest_observed), SaveArg<2>(&packet_size))); 1250 EXPECT_CALL(*send_algorithm_, AbandoningPacket(1, _)).Times(1); 1251 connection_.SendStreamData(1, "foo", 0, !kFin); 1252 QuicAckFrame frame(1, QuicTime::Zero(), largest_observed); 1253 frame.received_info.missing_packets.insert(largest_observed); 1254 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash( 1255 &connection_, largest_observed - 1); 1256 ProcessAckPacket(&frame, true); 1257 ProcessAckPacket(&frame, true); 1258 // Third nack should retransmit the largest observed packet. 1259 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, packet_size - kQuicVersionSize, 1260 IS_RETRANSMISSION)); 1261 ProcessAckPacket(&frame, true); 1262 } 1263 1264 TEST_F(QuicConnectionTest, RetransmitNackedPacketsOnTruncatedAck) { 1265 for (int i = 0; i < 200; ++i) { 1266 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(1); 1267 connection_.SendStreamData(1, "foo", i * 3, !kFin); 1268 } 1269 1270 // Make a truncated ack frame. 1271 QuicAckFrame frame(0, QuicTime::Zero(), 1); 1272 frame.received_info.largest_observed = 192; 1273 InsertMissingPacketsBetween(&frame.received_info, 1, 192); 1274 frame.received_info.entropy_hash = 1275 QuicConnectionPeer::GetSentEntropyHash(&connection_, 192) ^ 1276 QuicConnectionPeer::GetSentEntropyHash(&connection_, 191); 1277 1278 1279 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1); 1280 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1); 1281 EXPECT_CALL(visitor_, OnAck(_)).Times(1); 1282 ProcessAckPacket(&frame, true); 1283 EXPECT_TRUE(QuicConnectionPeer::GetReceivedTruncatedAck(&connection_)); 1284 1285 QuicConnectionPeer::SetMaxPacketsPerRetransmissionAlarm(&connection_, 200); 1286 const QuicTime::Delta kDefaultRetransmissionTime = 1287 QuicTime::Delta::FromMilliseconds(500); 1288 clock_.AdvanceTime(kDefaultRetransmissionTime); 1289 // Only packets that are less than largest observed should be retransmitted. 1290 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(191); 1291 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(191); 1292 connection_.OnRetransmissionTimeout(); 1293 1294 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds( 1295 2 * kDefaultRetransmissionTime.ToMicroseconds())); 1296 // Retransmit already retransmitted packets event though the sequence number 1297 // greater than the largest observed. 1298 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(191); 1299 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(191); 1300 connection_.OnRetransmissionTimeout(); 1301 } 1302 1303 TEST_F(QuicConnectionTest, LimitPacketsPerNack) { 1304 EXPECT_CALL(*send_algorithm_, OnIncomingAck(12, _, _)).Times(1); 1305 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1); 1306 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(11); 1307 int offset = 0; 1308 // Send packets 1 to 12 1309 for (int i = 0; i < 12; ++i) { 1310 SendStreamDataToPeer(1, "foo", offset, !kFin, NULL); 1311 offset += 3; 1312 } 1313 1314 // Ack 12, nack 1-11 1315 QuicAckFrame nack(12, QuicTime::Zero(), 0); 1316 for (int i = 1; i < 12; ++i) { 1317 nack.received_info.missing_packets.insert(i); 1318 } 1319 1320 nack.received_info.entropy_hash = 1321 QuicConnectionPeer::GetSentEntropyHash(&connection_, 12) ^ 1322 QuicConnectionPeer::GetSentEntropyHash(&connection_, 11); 1323 SequenceNumberSet expected_acks; 1324 expected_acks.insert(12); 1325 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks))); 1326 1327 // Nack three times. 1328 ProcessAckPacket(&nack, true); 1329 ProcessAckPacket(&nack, true); 1330 // The third call should trigger retransmitting 10 packets. 1331 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(10); 1332 ProcessAckPacket(&nack, true); 1333 1334 // The fourth call should trigger retransmitting the 11th packet. 1335 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(1); 1336 ProcessAckPacket(&nack, true); 1337 } 1338 1339 // Test sending multiple acks from the connection to the session. 1340 TEST_F(QuicConnectionTest, MultipleAcks) { 1341 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(6); 1342 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1); 1343 QuicPacketSequenceNumber last_packet; 1344 SendStreamDataToPeer(1u, "foo", 0, !kFin, &last_packet); // Packet 1 1345 EXPECT_EQ(1u, last_packet); 1346 SendStreamDataToPeer(3u, "foo", 0, !kFin, &last_packet); // Packet 2 1347 EXPECT_EQ(2u, last_packet); 1348 SendAckPacketToPeer(); // Packet 3 1349 SendStreamDataToPeer(5u, "foo", 0, !kFin, &last_packet); // Packet 4 1350 EXPECT_EQ(4u, last_packet); 1351 SendStreamDataToPeer(1u, "foo", 3, !kFin, &last_packet); // Packet 5 1352 EXPECT_EQ(5u, last_packet); 1353 SendStreamDataToPeer(3u, "foo", 3, !kFin, &last_packet); // Packet 6 1354 EXPECT_EQ(6u, last_packet); 1355 1356 // Client will ack packets 1, [!2], 3, 4, 5 1357 QuicAckFrame frame1(5, QuicTime::Zero(), 0); 1358 frame1.received_info.missing_packets.insert(2); 1359 frame1.received_info.entropy_hash = 1360 QuicConnectionPeer::GetSentEntropyHash(&connection_, 5) ^ 1361 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^ 1362 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 1363 1364 // The connection should pass up acks for 1, 4, 5. 2 is not acked, and 3 was 1365 // an ackframe so should not be passed up. 1366 SequenceNumberSet expected_acks; 1367 expected_acks.insert(1); 1368 expected_acks.insert(4); 1369 expected_acks.insert(5); 1370 1371 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks))); 1372 ProcessAckPacket(&frame1, true); 1373 1374 // Now the client implicitly acks 2, and explicitly acks 6 1375 QuicAckFrame frame2(6, QuicTime::Zero(), 0); 1376 frame2.received_info.entropy_hash = 1377 QuicConnectionPeer::GetSentEntropyHash(&connection_, 6); 1378 expected_acks.clear(); 1379 // Both acks should be passed up. 1380 expected_acks.insert(2); 1381 expected_acks.insert(6); 1382 1383 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks))); 1384 ProcessAckPacket(&frame2, true); 1385 } 1386 1387 TEST_F(QuicConnectionTest, DontLatchUnackedPacket) { 1388 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1); 1389 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); // Packet 1; 1390 SendAckPacketToPeer(); // Packet 2 1391 1392 // This sets least unacked to 3 (unsent packet), since we don't need 1393 // an ack for Packet 2 (ack packet). 1394 SequenceNumberSet expected_acks; 1395 expected_acks.insert(1); 1396 // Peer acks packet 1. 1397 EXPECT_CALL(visitor_, OnAck(ContainerEq(expected_acks))); 1398 QuicAckFrame frame(1, QuicTime::Zero(), 0); 1399 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash( 1400 &connection_, 1); 1401 ProcessAckPacket(&frame, true); 1402 1403 // Verify that our internal state has least-unacked as 3. 1404 EXPECT_EQ(3u, outgoing_ack()->sent_info.least_unacked); 1405 1406 // When we send an ack, we make sure our least-unacked makes sense. In this 1407 // case since we're not waiting on an ack for 2 and all packets are acked, we 1408 // set it to 3. 1409 SendAckPacketToPeer(); // Packet 3 1410 // Since this was an ack packet, we set least_unacked to 4. 1411 EXPECT_EQ(4u, outgoing_ack()->sent_info.least_unacked); 1412 // Check that the outgoing ack had its sequence number as least_unacked. 1413 EXPECT_EQ(3u, last_ack()->sent_info.least_unacked); 1414 1415 SendStreamDataToPeer(1, "bar", 3, false, NULL); // Packet 4 1416 EXPECT_EQ(4u, outgoing_ack()->sent_info.least_unacked); 1417 SendAckPacketToPeer(); // Packet 5 1418 EXPECT_EQ(4u, last_ack()->sent_info.least_unacked); 1419 } 1420 1421 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterFecPacket) { 1422 // Don't send missing packet 1. 1423 ProcessFecPacket(2, 1, true, !kEntropyFlag); 1424 EXPECT_FALSE(revived_header_.entropy_flag); 1425 } 1426 1427 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacketThenFecPacket) { 1428 ProcessFecProtectedPacket(1, false, kEntropyFlag); 1429 // Don't send missing packet 2. 1430 ProcessFecPacket(3, 1, true, !kEntropyFlag); 1431 EXPECT_TRUE(revived_header_.entropy_flag); 1432 } 1433 1434 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacketsThenFecPacket) { 1435 ProcessFecProtectedPacket(1, false, !kEntropyFlag); 1436 // Don't send missing packet 2. 1437 ProcessFecProtectedPacket(3, false, !kEntropyFlag); 1438 ProcessFecPacket(4, 1, true, kEntropyFlag); 1439 EXPECT_TRUE(revived_header_.entropy_flag); 1440 } 1441 1442 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacket) { 1443 // Don't send missing packet 1. 1444 ProcessFecPacket(3, 1, false, !kEntropyFlag); 1445 // out of order 1446 ProcessFecProtectedPacket(2, true, !kEntropyFlag); 1447 EXPECT_FALSE(revived_header_.entropy_flag); 1448 } 1449 1450 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPackets) { 1451 ProcessFecProtectedPacket(1, false, !kEntropyFlag); 1452 // Don't send missing packet 2. 1453 ProcessFecPacket(6, 1, false, kEntropyFlag); 1454 ProcessFecProtectedPacket(3, false, kEntropyFlag); 1455 ProcessFecProtectedPacket(4, false, kEntropyFlag); 1456 ProcessFecProtectedPacket(5, true, !kEntropyFlag); 1457 EXPECT_TRUE(revived_header_.entropy_flag); 1458 } 1459 1460 TEST_F(QuicConnectionTest, TestRetransmit) { 1461 const QuicTime::Delta kDefaultRetransmissionTime = 1462 QuicTime::Delta::FromMilliseconds(500); 1463 1464 QuicTime default_retransmission_time = clock_.ApproximateNow().Add( 1465 kDefaultRetransmissionTime); 1466 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 1467 EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked); 1468 1469 EXPECT_EQ(1u, last_header()->packet_sequence_number); 1470 EXPECT_EQ(default_retransmission_time, 1471 connection_.GetRetransmissionAlarm()->deadline()); 1472 // Simulate the retransimission alarm firing 1473 clock_.AdvanceTime(kDefaultRetransmissionTime); 1474 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 1475 EXPECT_CALL(*send_algorithm_, AbandoningPacket(1, _)).Times(1); 1476 connection_.RetransmitPacket(1); 1477 EXPECT_EQ(2u, last_header()->packet_sequence_number); 1478 EXPECT_EQ(2u, outgoing_ack()->sent_info.least_unacked); 1479 } 1480 1481 TEST_F(QuicConnectionTest, RetransmitWithSameEncryptionLevel) { 1482 const QuicTime::Delta kDefaultRetransmissionTime = 1483 QuicTime::Delta::FromMilliseconds(500); 1484 1485 QuicTime default_retransmission_time = clock_.ApproximateNow().Add( 1486 kDefaultRetransmissionTime); 1487 use_tagging_decrypter(); 1488 1489 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at 1490 // the end of the packet. We can test this to check which encrypter was used. 1491 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); 1492 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 1493 EXPECT_EQ(0x01010101u, final_bytes_of_last_packet()); 1494 1495 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02)); 1496 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); 1497 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 1498 EXPECT_EQ(0x02020202u, final_bytes_of_last_packet()); 1499 1500 EXPECT_EQ(default_retransmission_time, 1501 connection_.GetRetransmissionAlarm()->deadline()); 1502 // Simulate the retransimission alarm firing 1503 clock_.AdvanceTime(kDefaultRetransmissionTime); 1504 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(2); 1505 1506 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 1507 connection_.RetransmitPacket(1); 1508 // Packet should have been sent with ENCRYPTION_NONE. 1509 EXPECT_EQ(0x01010101u, final_bytes_of_last_packet()); 1510 1511 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 1512 connection_.RetransmitPacket(2); 1513 // Packet should have been sent with ENCRYPTION_INITIAL. 1514 EXPECT_EQ(0x02020202u, final_bytes_of_last_packet()); 1515 } 1516 1517 TEST_F(QuicConnectionTest, 1518 DropRetransmitsForNullEncryptedPacketAfterForwardSecure) { 1519 use_tagging_decrypter(); 1520 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); 1521 QuicPacketSequenceNumber sequence_number; 1522 SendStreamDataToPeer(1, "foo", 0, !kFin, &sequence_number); 1523 1524 connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, 1525 new TaggingEncrypter(0x02)); 1526 connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); 1527 1528 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(0); 1529 EXPECT_CALL(*send_algorithm_, AbandoningPacket(sequence_number, _)).Times(1); 1530 1531 const QuicTime::Delta kDefaultRetransmissionTime = 1532 QuicTime::Delta::FromMilliseconds(500); 1533 QuicTime default_retransmission_time = clock_.ApproximateNow().Add( 1534 kDefaultRetransmissionTime); 1535 1536 EXPECT_EQ(default_retransmission_time, 1537 connection_.GetRetransmissionAlarm()->deadline()); 1538 // Simulate the retransimission alarm firing 1539 clock_.AdvanceTime(kDefaultRetransmissionTime); 1540 connection_.OnRetransmissionTimeout(); 1541 } 1542 1543 TEST_F(QuicConnectionTest, RetransmitPacketsWithInitialEncryption) { 1544 use_tagging_decrypter(); 1545 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); 1546 connection_.SetDefaultEncryptionLevel(ENCRYPTION_NONE); 1547 1548 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 1549 1550 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02)); 1551 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); 1552 1553 SendStreamDataToPeer(2, "bar", 0, !kFin, NULL); 1554 1555 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(1); 1556 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(1); 1557 1558 connection_.RetransmitUnackedPackets(QuicConnection::INITIAL_ENCRYPTION_ONLY); 1559 } 1560 1561 TEST_F(QuicConnectionTest, BufferNonDecryptablePackets) { 1562 use_tagging_decrypter(); 1563 1564 const uint8 tag = 0x07; 1565 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); 1566 1567 // Process an encrypted packet which can not yet be decrypted 1568 // which should result in the packet being buffered. 1569 ProcessDataPacketAtLevel(1, false, kEntropyFlag, ENCRYPTION_INITIAL); 1570 1571 // Transition to the new encryption state and process another 1572 // encrypted packet which should result in the original packet being 1573 // processed. 1574 connection_.SetDecrypter(new StrictTaggingDecrypter(tag)); 1575 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); 1576 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); 1577 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).Times(2).WillRepeatedly( 1578 Return(true)); 1579 ProcessDataPacketAtLevel(2, false, kEntropyFlag, ENCRYPTION_INITIAL); 1580 1581 // Finally, process a third packet and note that we do not 1582 // reprocess the buffered packet. 1583 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillOnce(Return(true)); 1584 ProcessDataPacketAtLevel(3, false, kEntropyFlag, ENCRYPTION_INITIAL); 1585 } 1586 1587 TEST_F(QuicConnectionTest, TestRetransmitOrder) { 1588 QuicByteCount first_packet_size; 1589 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).WillOnce( 1590 SaveArg<2>(&first_packet_size)); 1591 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(2); 1592 1593 connection_.SendStreamData(1, "first_packet", 0, !kFin); 1594 QuicByteCount second_packet_size; 1595 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).WillOnce( 1596 SaveArg<2>(&second_packet_size)); 1597 connection_.SendStreamData(1, "second_packet", 12, !kFin); 1598 EXPECT_NE(first_packet_size, second_packet_size); 1599 // Advance the clock by huge time to make sure packets will be retransmitted. 1600 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); 1601 { 1602 InSequence s; 1603 EXPECT_CALL(*send_algorithm_, 1604 SentPacket(_, _, first_packet_size, _)); 1605 EXPECT_CALL(*send_algorithm_, 1606 SentPacket(_, _, second_packet_size, _)); 1607 } 1608 connection_.OnRetransmissionTimeout(); 1609 } 1610 1611 TEST_F(QuicConnectionTest, TestRetransmissionCountCalculation) { 1612 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1); 1613 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(2); 1614 QuicPacketSequenceNumber original_sequence_number; 1615 EXPECT_CALL(*send_algorithm_, 1616 SentPacket(_, _, _, NOT_RETRANSMISSION)) 1617 .WillOnce(SaveArg<1>(&original_sequence_number)); 1618 connection_.SendStreamData(1, "foo", 0, !kFin); 1619 EXPECT_TRUE(QuicConnectionPeer::IsSavedForRetransmission( 1620 &connection_, original_sequence_number)); 1621 EXPECT_EQ(0u, QuicConnectionPeer::GetRetransmissionCount( 1622 &connection_, original_sequence_number)); 1623 // Force retransmission due to RTO. 1624 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); 1625 QuicPacketSequenceNumber rto_sequence_number; 1626 EXPECT_CALL(*send_algorithm_, 1627 SentPacket(_, _, _, IS_RETRANSMISSION)) 1628 .WillOnce(SaveArg<1>(&rto_sequence_number)); 1629 connection_.OnRetransmissionTimeout(); 1630 EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission( 1631 &connection_, original_sequence_number)); 1632 ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission( 1633 &connection_, rto_sequence_number)); 1634 EXPECT_EQ(1u, QuicConnectionPeer::GetRetransmissionCount( 1635 &connection_, rto_sequence_number)); 1636 // Once by explicit nack. 1637 QuicPacketSequenceNumber nack_sequence_number; 1638 // Ack packets might generate some other packets, which are not 1639 // retransmissions. (More ack packets). 1640 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, NOT_RETRANSMISSION)) 1641 .Times(AnyNumber()); 1642 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, IS_RETRANSMISSION)) 1643 .WillOnce(SaveArg<1>(&nack_sequence_number)); 1644 QuicAckFrame ack(rto_sequence_number, QuicTime::Zero(), 0); 1645 // Ack the retransmitted packet. 1646 ack.received_info.missing_packets.insert(rto_sequence_number); 1647 ack.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash( 1648 &connection_, rto_sequence_number - 1); 1649 for (int i = 0; i < 3; i++) { 1650 ProcessAckPacket(&ack, true); 1651 } 1652 EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission( 1653 &connection_, rto_sequence_number)); 1654 EXPECT_TRUE(QuicConnectionPeer::IsSavedForRetransmission( 1655 &connection_, nack_sequence_number)); 1656 EXPECT_EQ(2u, QuicConnectionPeer::GetRetransmissionCount( 1657 &connection_, nack_sequence_number)); 1658 } 1659 1660 TEST_F(QuicConnectionTest, SetRTOAfterWritingToSocket) { 1661 helper_->set_blocked(true); 1662 connection_.SendStreamData(1, "foo", 0, !kFin); 1663 // Make sure that RTO is not started when the packet is queued. 1664 EXPECT_EQ(0u, QuicConnectionPeer::GetNumRetransmissionTimeouts(&connection_)); 1665 1666 // Test that RTO is started once we write to the socket. 1667 helper_->set_blocked(false); 1668 EXPECT_CALL(visitor_, OnCanWrite()); 1669 connection_.OnCanWrite(); 1670 EXPECT_EQ(1u, QuicConnectionPeer::GetNumRetransmissionTimeouts(&connection_)); 1671 } 1672 1673 TEST_F(QuicConnectionTest, TestQueued) { 1674 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1675 helper_->set_blocked(true); 1676 connection_.SendStreamData(1, "foo", 0, !kFin); 1677 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1678 1679 // Attempt to send all packets, but since we're actually still 1680 // blocked, they should all remain queued. 1681 EXPECT_FALSE(connection_.OnCanWrite()); 1682 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1683 1684 // Unblock the writes and actually send. 1685 helper_->set_blocked(false); 1686 EXPECT_CALL(visitor_, OnCanWrite()); 1687 EXPECT_TRUE(connection_.OnCanWrite()); 1688 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1689 } 1690 1691 TEST_F(QuicConnectionTest, CloseFecGroup) { 1692 // Don't send missing packet 1 1693 // Don't send missing packet 2 1694 ProcessFecProtectedPacket(3, false, !kEntropyFlag); 1695 // Don't send missing FEC packet 3 1696 ASSERT_EQ(1u, connection_.NumFecGroups()); 1697 1698 // Now send non-fec protected ack packet and close the group 1699 QuicAckFrame frame(0, QuicTime::Zero(), 5); 1700 creator_.set_sequence_number(4); 1701 ProcessAckPacket(&frame, true); 1702 ASSERT_EQ(0u, connection_.NumFecGroups()); 1703 } 1704 1705 TEST_F(QuicConnectionTest, NoQuicCongestionFeedbackFrame) { 1706 SendAckPacketToPeer(); 1707 EXPECT_TRUE(last_feedback() == NULL); 1708 } 1709 1710 TEST_F(QuicConnectionTest, WithQuicCongestionFeedbackFrame) { 1711 QuicCongestionFeedbackFrame info; 1712 info.type = kFixRate; 1713 info.fix_rate.bitrate = QuicBandwidth::FromBytesPerSecond(123); 1714 SetFeedback(&info); 1715 1716 SendAckPacketToPeer(); 1717 EXPECT_EQ(kFixRate, last_feedback()->type); 1718 EXPECT_EQ(info.fix_rate.bitrate, last_feedback()->fix_rate.bitrate); 1719 } 1720 1721 TEST_F(QuicConnectionTest, UpdateQuicCongestionFeedbackFrame) { 1722 SendAckPacketToPeer(); 1723 EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _, _)); 1724 ProcessPacket(1); 1725 } 1726 1727 TEST_F(QuicConnectionTest, DontUpdateQuicCongestionFeedbackFrameForRevived) { 1728 SendAckPacketToPeer(); 1729 // Process an FEC packet, and revive the missing data packet 1730 // but only contact the receive_algorithm once. 1731 EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _, _)); 1732 ProcessFecPacket(2, 1, true, !kEntropyFlag); 1733 } 1734 1735 TEST_F(QuicConnectionTest, InitialTimeout) { 1736 EXPECT_TRUE(connection_.connected()); 1737 EXPECT_CALL(visitor_, ConnectionClose(QUIC_CONNECTION_TIMED_OUT, false)); 1738 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 1739 1740 QuicTime default_timeout = clock_.ApproximateNow().Add( 1741 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)); 1742 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); 1743 1744 // Simulate the timeout alarm firing 1745 clock_.AdvanceTime( 1746 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)); 1747 EXPECT_TRUE(connection_.CheckForTimeout()); 1748 EXPECT_FALSE(connection_.connected()); 1749 } 1750 1751 TEST_F(QuicConnectionTest, TimeoutAfterSend) { 1752 EXPECT_TRUE(connection_.connected()); 1753 1754 QuicTime default_timeout = clock_.ApproximateNow().Add( 1755 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)); 1756 1757 // When we send a packet, the timeout will change to 5000 + 1758 // kDefaultInitialTimeoutSecs. 1759 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); 1760 1761 // Send an ack so we don't set the retransimission alarm. 1762 SendAckPacketToPeer(); 1763 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); 1764 1765 // The original alarm will fire. We should not time out because we had a 1766 // network event at t=5000. The alarm will reregister. 1767 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds( 1768 kDefaultInitialTimeoutSecs * 1000000 - 5000)); 1769 EXPECT_EQ(default_timeout, clock_.ApproximateNow()); 1770 EXPECT_FALSE(connection_.CheckForTimeout()); 1771 EXPECT_TRUE(connection_.connected()); 1772 EXPECT_EQ(default_timeout.Add(QuicTime::Delta::FromMilliseconds(5)), 1773 connection_.GetTimeoutAlarm()->deadline()); 1774 1775 // This time, we should time out. 1776 EXPECT_CALL(visitor_, ConnectionClose(QUIC_CONNECTION_TIMED_OUT, false)); 1777 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 1778 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); 1779 EXPECT_EQ(default_timeout.Add(QuicTime::Delta::FromMilliseconds(5)), 1780 clock_.ApproximateNow()); 1781 EXPECT_TRUE(connection_.CheckForTimeout()); 1782 EXPECT_FALSE(connection_.connected()); 1783 } 1784 1785 // TODO(ianswett): Add scheduler tests when should_retransmit is false. 1786 TEST_F(QuicConnectionTest, SendScheduler) { 1787 // Test that if we send a packet without delay, it is not queued. 1788 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 1789 EXPECT_CALL(*send_algorithm_, 1790 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 1791 testing::Return(QuicTime::Delta::Zero())); 1792 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 1793 connection_.SendOrQueuePacket( 1794 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 1795 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1796 } 1797 1798 TEST_F(QuicConnectionTest, SendSchedulerDelay) { 1799 // Test that if we send a packet with a delay, it ends up queued. 1800 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 1801 EXPECT_CALL(*send_algorithm_, 1802 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 1803 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 1804 EXPECT_CALL(*send_algorithm_, SentPacket(_, 1, _, _)).Times(0); 1805 connection_.SendOrQueuePacket( 1806 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 1807 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1808 } 1809 1810 TEST_F(QuicConnectionTest, SendSchedulerForce) { 1811 // Test that if we force send a packet, it is not queued. 1812 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 1813 EXPECT_CALL(*send_algorithm_, 1814 TimeUntilSend(_, IS_RETRANSMISSION, _, _)).Times(0); 1815 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 1816 connection_.SendOrQueuePacket( 1817 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 1818 // XXX: fixme. was: connection_.SendOrQueuePacket(1, packet, kForce); 1819 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1820 } 1821 1822 TEST_F(QuicConnectionTest, SendSchedulerEAGAIN) { 1823 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 1824 helper_->set_blocked(true); 1825 EXPECT_CALL(*send_algorithm_, 1826 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 1827 testing::Return(QuicTime::Delta::Zero())); 1828 EXPECT_CALL(*send_algorithm_, SentPacket(_, 1, _, _)).Times(0); 1829 connection_.SendOrQueuePacket( 1830 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 1831 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1832 } 1833 1834 TEST_F(QuicConnectionTest, SendSchedulerDelayThenSend) { 1835 // Test that if we send a packet with a delay, it ends up queued. 1836 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 1837 EXPECT_CALL(*send_algorithm_, 1838 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 1839 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 1840 connection_.SendOrQueuePacket( 1841 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 1842 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1843 1844 // Advance the clock to fire the alarm, and configure the scheduler 1845 // to permit the packet to be sent. 1846 EXPECT_CALL(*send_algorithm_, 1847 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly( 1848 testing::Return(QuicTime::Delta::Zero())); 1849 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1)); 1850 connection_.GetSendAlarm()->Cancel(); 1851 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)); 1852 EXPECT_CALL(visitor_, OnCanWrite()); 1853 connection_.OnCanWrite(); 1854 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1855 } 1856 1857 TEST_F(QuicConnectionTest, SendSchedulerDelayThenRetransmit) { 1858 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, NOT_RETRANSMISSION, _, _)) 1859 .WillRepeatedly(testing::Return(QuicTime::Delta::Zero())); 1860 EXPECT_CALL(*send_algorithm_, AbandoningPacket(1, _)).Times(1); 1861 EXPECT_CALL(*send_algorithm_, 1862 SentPacket(_, 1, _, NOT_RETRANSMISSION)); 1863 connection_.SendStreamData(1, "foo", 0, !kFin); 1864 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1865 // Advance the time for retransmission of lost packet. 1866 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(501)); 1867 // Test that if we send a retransmit with a delay, it ends up queued. 1868 EXPECT_CALL(*send_algorithm_, 1869 TimeUntilSend(_, IS_RETRANSMISSION, _, _)).WillOnce( 1870 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 1871 connection_.OnRetransmissionTimeout(); 1872 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1873 1874 // Advance the clock to fire the alarm, and configure the scheduler 1875 // to permit the packet to be sent. 1876 EXPECT_CALL(*send_algorithm_, 1877 TimeUntilSend(_, IS_RETRANSMISSION, _, _)).WillOnce( 1878 testing::Return(QuicTime::Delta::Zero())); 1879 1880 // Ensure the scheduler is notified this is a retransmit. 1881 EXPECT_CALL(*send_algorithm_, 1882 SentPacket(_, _, _, IS_RETRANSMISSION)); 1883 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1)); 1884 connection_.GetSendAlarm()->Cancel(); 1885 EXPECT_CALL(visitor_, OnCanWrite()); 1886 connection_.OnCanWrite(); 1887 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1888 } 1889 1890 TEST_F(QuicConnectionTest, SendSchedulerDelayAndQueue) { 1891 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 1892 EXPECT_CALL(*send_algorithm_, 1893 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 1894 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 1895 connection_.SendOrQueuePacket( 1896 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 1897 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1898 1899 // Attempt to send another packet and make sure that it gets queued. 1900 packet = ConstructDataPacket(2, 0, !kEntropyFlag); 1901 connection_.SendOrQueuePacket( 1902 ENCRYPTION_NONE, 2, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 1903 EXPECT_EQ(2u, connection_.NumQueuedPackets()); 1904 } 1905 1906 TEST_F(QuicConnectionTest, SendSchedulerDelayThenAckAndSend) { 1907 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 1908 EXPECT_CALL(*send_algorithm_, 1909 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 1910 testing::Return(QuicTime::Delta::FromMicroseconds(10))); 1911 connection_.SendOrQueuePacket( 1912 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 1913 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1914 1915 // Now send non-retransmitting information, that we're not going to 1916 // retransmit 3. The far end should stop waiting for it. 1917 QuicAckFrame frame(0, QuicTime::Zero(), 1); 1918 EXPECT_CALL(*send_algorithm_, 1919 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly( 1920 testing::Return(QuicTime::Delta::Zero())); 1921 EXPECT_CALL(*send_algorithm_, 1922 SentPacket(_, _, _, _)); 1923 ProcessAckPacket(&frame, true); 1924 1925 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1926 // Ensure alarm is not set 1927 EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); 1928 } 1929 1930 TEST_F(QuicConnectionTest, SendSchedulerDelayThenAckAndHold) { 1931 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 1932 EXPECT_CALL(*send_algorithm_, 1933 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 1934 testing::Return(QuicTime::Delta::FromMicroseconds(10))); 1935 connection_.SendOrQueuePacket( 1936 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 1937 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1938 1939 // Now send non-retransmitting information, that we're not going to 1940 // retransmit 3. The far end should stop waiting for it. 1941 QuicAckFrame frame(0, QuicTime::Zero(), 1); 1942 EXPECT_CALL(*send_algorithm_, 1943 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 1944 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 1945 ProcessAckPacket(&frame, false); 1946 1947 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1948 } 1949 1950 TEST_F(QuicConnectionTest, SendSchedulerDelayThenOnCanWrite) { 1951 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 1952 EXPECT_CALL(*send_algorithm_, 1953 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 1954 testing::Return(QuicTime::Delta::FromMicroseconds(10))); 1955 connection_.SendOrQueuePacket( 1956 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 1957 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1958 1959 // OnCanWrite should not send the packet (because of the delay) 1960 // but should still return true. 1961 EXPECT_TRUE(connection_.OnCanWrite()); 1962 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1963 } 1964 1965 TEST_F(QuicConnectionTest, TestQueueLimitsOnSendStreamData) { 1966 // All packets carry version info till version is negotiated. 1967 size_t payload_length; 1968 connection_.options()->max_packet_length = 1969 GetPacketLengthForOneStream(connection_.version(), kIncludeVersion, 1970 NOT_IN_FEC_GROUP, &payload_length); 1971 1972 // Queue the first packet. 1973 EXPECT_CALL(*send_algorithm_, 1974 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 1975 testing::Return(QuicTime::Delta::FromMicroseconds(10))); 1976 const string payload(payload_length, 'a'); 1977 EXPECT_EQ(0u, 1978 connection_.SendStreamData(1, payload, 0, !kFin).bytes_consumed); 1979 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1980 } 1981 1982 TEST_F(QuicConnectionTest, LoopThroughSendingPackets) { 1983 // All packets carry version info till version is negotiated. 1984 size_t payload_length; 1985 connection_.options()->max_packet_length = 1986 GetPacketLengthForOneStream(connection_.version(), kIncludeVersion, 1987 NOT_IN_FEC_GROUP, &payload_length); 1988 1989 // Queue the first packet. 1990 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(7); 1991 // The first stream frame will consume 2 fewer bytes than the other six. 1992 const string payload(payload_length * 7 - 12, 'a'); 1993 EXPECT_EQ(payload.size(), 1994 connection_.SendStreamData(1, payload, 0, !kFin).bytes_consumed); 1995 } 1996 1997 TEST_F(QuicConnectionTest, NoAckForClose) { 1998 ProcessPacket(1); 1999 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(0); 2000 EXPECT_CALL(visitor_, ConnectionClose(QUIC_PEER_GOING_AWAY, true)); 2001 EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _)).Times(0); 2002 ProcessClosePacket(2, 0); 2003 } 2004 2005 TEST_F(QuicConnectionTest, SendWhenDisconnected) { 2006 EXPECT_TRUE(connection_.connected()); 2007 EXPECT_CALL(visitor_, ConnectionClose(QUIC_PEER_GOING_AWAY, false)); 2008 connection_.CloseConnection(QUIC_PEER_GOING_AWAY, false); 2009 EXPECT_FALSE(connection_.connected()); 2010 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2011 EXPECT_CALL(*send_algorithm_, SentPacket(_, 1, _, _)).Times(0); 2012 connection_.SendOrQueuePacket( 2013 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2014 } 2015 2016 TEST_F(QuicConnectionTest, PublicReset) { 2017 QuicPublicResetPacket header; 2018 header.public_header.guid = guid_; 2019 header.public_header.reset_flag = true; 2020 header.public_header.version_flag = false; 2021 header.rejected_sequence_number = 10101; 2022 scoped_ptr<QuicEncryptedPacket> packet( 2023 framer_.BuildPublicResetPacket(header)); 2024 EXPECT_CALL(visitor_, ConnectionClose(QUIC_PUBLIC_RESET, true)); 2025 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *packet); 2026 } 2027 2028 TEST_F(QuicConnectionTest, GoAway) { 2029 QuicGoAwayFrame goaway; 2030 goaway.last_good_stream_id = 1; 2031 goaway.error_code = QUIC_PEER_GOING_AWAY; 2032 goaway.reason_phrase = "Going away."; 2033 EXPECT_CALL(visitor_, OnGoAway(_)); 2034 ProcessGoAwayPacket(&goaway); 2035 } 2036 2037 TEST_F(QuicConnectionTest, MissingPacketsBeforeLeastUnacked) { 2038 QuicAckFrame ack(0, QuicTime::Zero(), 4); 2039 // Set the sequence number of the ack packet to be least unacked (4) 2040 creator_.set_sequence_number(3); 2041 ProcessAckPacket(&ack, true); 2042 EXPECT_TRUE(outgoing_ack()->received_info.missing_packets.empty()); 2043 } 2044 2045 TEST_F(QuicConnectionTest, ReceivedEntropyHashCalculation) { 2046 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillRepeatedly(Return(true)); 2047 ProcessDataPacket(1, 1, kEntropyFlag); 2048 ProcessDataPacket(4, 1, kEntropyFlag); 2049 ProcessDataPacket(3, 1, !kEntropyFlag); 2050 ProcessDataPacket(7, 1, kEntropyFlag); 2051 EXPECT_EQ(146u, outgoing_ack()->received_info.entropy_hash); 2052 } 2053 2054 TEST_F(QuicConnectionTest, UpdateEntropyForReceivedPackets) { 2055 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillRepeatedly(Return(true)); 2056 ProcessDataPacket(1, 1, kEntropyFlag); 2057 ProcessDataPacket(5, 1, kEntropyFlag); 2058 ProcessDataPacket(4, 1, !kEntropyFlag); 2059 EXPECT_EQ(34u, outgoing_ack()->received_info.entropy_hash); 2060 // Make 4th packet my least unacked, and update entropy for 2, 3 packets. 2061 QuicAckFrame ack(0, QuicTime::Zero(), 4); 2062 QuicPacketEntropyHash kRandomEntropyHash = 129u; 2063 ack.sent_info.entropy_hash = kRandomEntropyHash; 2064 creator_.set_sequence_number(5); 2065 QuicPacketEntropyHash six_packet_entropy_hash = 0; 2066 if (ProcessAckPacket(&ack, true)) { 2067 six_packet_entropy_hash = 1 << 6; 2068 }; 2069 2070 EXPECT_EQ((kRandomEntropyHash + (1 << 5) + six_packet_entropy_hash), 2071 outgoing_ack()->received_info.entropy_hash); 2072 } 2073 2074 TEST_F(QuicConnectionTest, UpdateEntropyHashUptoCurrentPacket) { 2075 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillRepeatedly(Return(true)); 2076 ProcessDataPacket(1, 1, kEntropyFlag); 2077 ProcessDataPacket(5, 1, !kEntropyFlag); 2078 ProcessDataPacket(22, 1, kEntropyFlag); 2079 EXPECT_EQ(66u, outgoing_ack()->received_info.entropy_hash); 2080 creator_.set_sequence_number(22); 2081 QuicPacketEntropyHash kRandomEntropyHash = 85u; 2082 // Current packet is the least unacked packet. 2083 QuicAckFrame ack(0, QuicTime::Zero(), 23); 2084 ack.sent_info.entropy_hash = kRandomEntropyHash; 2085 QuicPacketEntropyHash ack_entropy_hash = ProcessAckPacket(&ack, true); 2086 EXPECT_EQ((kRandomEntropyHash + ack_entropy_hash), 2087 outgoing_ack()->received_info.entropy_hash); 2088 ProcessDataPacket(25, 1, kEntropyFlag); 2089 EXPECT_EQ((kRandomEntropyHash + ack_entropy_hash + (1 << (25 % 8))), 2090 outgoing_ack()->received_info.entropy_hash); 2091 } 2092 2093 TEST_F(QuicConnectionTest, EntropyCalculationForTruncatedAck) { 2094 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).WillRepeatedly(Return(true)); 2095 QuicPacketEntropyHash entropy[51]; 2096 entropy[0] = 0; 2097 for (int i = 1; i < 51; ++i) { 2098 bool should_send = i % 10 != 0; 2099 bool entropy_flag = (i & (i - 1)) != 0; 2100 if (!should_send) { 2101 entropy[i] = entropy[i - 1]; 2102 continue; 2103 } 2104 if (entropy_flag) { 2105 entropy[i] = entropy[i - 1] ^ (1 << (i % 8)); 2106 } else { 2107 entropy[i] = entropy[i - 1]; 2108 } 2109 ProcessDataPacket(i, 1, entropy_flag); 2110 } 2111 // Till 50 since 50th packet is not sent. 2112 for (int i = 1; i < 50; ++i) { 2113 EXPECT_EQ(entropy[i], QuicConnectionPeer::ReceivedEntropyHash( 2114 &connection_, i)); 2115 } 2116 } 2117 2118 TEST_F(QuicConnectionTest, CheckSentEntropyHash) { 2119 creator_.set_sequence_number(1); 2120 SequenceNumberSet missing_packets; 2121 QuicPacketEntropyHash entropy_hash = 0; 2122 QuicPacketSequenceNumber max_sequence_number = 51; 2123 for (QuicPacketSequenceNumber i = 1; i <= max_sequence_number; ++i) { 2124 bool is_missing = i % 10 != 0; 2125 bool entropy_flag = (i & (i - 1)) != 0; 2126 QuicPacketEntropyHash packet_entropy_hash = 0; 2127 if (entropy_flag) { 2128 packet_entropy_hash = 1 << (i % 8); 2129 } 2130 QuicPacket* packet = ConstructDataPacket(i, 0, entropy_flag); 2131 connection_.SendOrQueuePacket( 2132 ENCRYPTION_NONE, i, packet, packet_entropy_hash, 2133 HAS_RETRANSMITTABLE_DATA); 2134 2135 if (is_missing) { 2136 missing_packets.insert(i); 2137 continue; 2138 } 2139 2140 entropy_hash ^= packet_entropy_hash; 2141 } 2142 EXPECT_TRUE(QuicConnectionPeer::IsValidEntropy( 2143 &connection_, max_sequence_number, missing_packets, entropy_hash)) 2144 << ""; 2145 } 2146 2147 TEST_F(QuicConnectionTest, ServerSendsVersionNegotiationPacket) { 2148 framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED); 2149 2150 QuicPacketHeader header; 2151 header.public_header.guid = guid_; 2152 header.public_header.reset_flag = false; 2153 header.public_header.version_flag = true; 2154 header.entropy_flag = false; 2155 header.fec_flag = false; 2156 header.packet_sequence_number = 12; 2157 header.fec_group = 0; 2158 2159 QuicFrames frames; 2160 QuicFrame frame(&frame1_); 2161 frames.push_back(frame); 2162 scoped_ptr<QuicPacket> packet( 2163 framer_.BuildUnsizedDataPacket(header, frames).packet); 2164 scoped_ptr<QuicEncryptedPacket> encrypted( 2165 framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet)); 2166 2167 framer_.set_version(QuicVersionMax()); 2168 connection_.set_is_server(true); 2169 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 2170 EXPECT_TRUE(helper_->version_negotiation_packet() != NULL); 2171 2172 size_t num_versions = arraysize(kSupportedQuicVersions); 2173 EXPECT_EQ(num_versions, 2174 helper_->version_negotiation_packet()->versions.size()); 2175 2176 // We expect all versions in kSupportedQuicVersions to be 2177 // included in the packet. 2178 for (size_t i = 0; i < num_versions; ++i) { 2179 EXPECT_EQ(kSupportedQuicVersions[i], 2180 helper_->version_negotiation_packet()->versions[i]); 2181 } 2182 } 2183 2184 TEST_F(QuicConnectionTest, ClientHandlesVersionNegotiation) { 2185 // Start out with some unsupported version. 2186 QuicConnectionPeer::GetFramer(&connection_)->set_version_for_tests( 2187 QUIC_VERSION_UNSUPPORTED); 2188 2189 QuicPacketHeader header; 2190 header.public_header.guid = guid_; 2191 header.public_header.reset_flag = false; 2192 header.public_header.version_flag = true; 2193 header.entropy_flag = false; 2194 header.fec_flag = false; 2195 header.packet_sequence_number = 12; 2196 header.fec_group = 0; 2197 2198 QuicVersionVector supported_versions; 2199 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { 2200 supported_versions.push_back(kSupportedQuicVersions[i]); 2201 } 2202 2203 // Send a version negotiation packet. 2204 scoped_ptr<QuicEncryptedPacket> encrypted( 2205 framer_.BuildVersionNegotiationPacket( 2206 header.public_header, supported_versions)); 2207 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 2208 2209 // Now force another packet. The connection should transition into 2210 // NEGOTIATED_VERSION state and tell the packet creator to StopSendingVersion. 2211 header.public_header.version_flag = false; 2212 QuicFrames frames; 2213 QuicFrame frame(&frame1_); 2214 frames.push_back(frame); 2215 scoped_ptr<QuicPacket> packet( 2216 framer_.BuildUnsizedDataPacket(header, frames).packet); 2217 encrypted.reset(framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet)); 2218 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).Times(1); 2219 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 2220 2221 ASSERT_FALSE(QuicPacketCreatorPeer::SendVersionInPacket( 2222 QuicConnectionPeer::GetPacketCreator(&connection_))); 2223 } 2224 2225 TEST_F(QuicConnectionTest, BadVersionNegotiation) { 2226 QuicPacketHeader header; 2227 header.public_header.guid = guid_; 2228 header.public_header.reset_flag = false; 2229 header.public_header.version_flag = true; 2230 header.entropy_flag = false; 2231 header.fec_flag = false; 2232 header.packet_sequence_number = 12; 2233 header.fec_group = 0; 2234 2235 QuicVersionVector supported_versions; 2236 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { 2237 supported_versions.push_back(kSupportedQuicVersions[i]); 2238 } 2239 2240 // Send a version negotiation packet with the version the client started with. 2241 // It should be rejected. 2242 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_VERSION_NEGOTIATION_PACKET, 2243 false)); 2244 scoped_ptr<QuicEncryptedPacket> encrypted( 2245 framer_.BuildVersionNegotiationPacket( 2246 header.public_header, supported_versions)); 2247 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 2248 } 2249 2250 TEST_F(QuicConnectionTest, CheckSendStats) { 2251 EXPECT_CALL(*send_algorithm_, AbandoningPacket(_, _)).Times(3); 2252 EXPECT_CALL(*send_algorithm_, 2253 SentPacket(_, _, _, NOT_RETRANSMISSION)); 2254 connection_.SendStreamData(1u, "first", 0, !kFin); 2255 size_t first_packet_size = last_sent_packet_size(); 2256 2257 EXPECT_CALL(*send_algorithm_, 2258 SentPacket(_, _, _, NOT_RETRANSMISSION)); 2259 connection_.SendStreamData(1u, "second", 0, !kFin); 2260 size_t second_packet_size = last_sent_packet_size(); 2261 2262 // 2 retransmissions due to rto, 1 due to explicit nack. 2263 EXPECT_CALL(*send_algorithm_, 2264 SentPacket(_, _, _, IS_RETRANSMISSION)).Times(3); 2265 2266 // Retransmit due to RTO. 2267 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); 2268 connection_.OnRetransmissionTimeout(); 2269 2270 // Retransmit due to explicit nacks 2271 QuicAckFrame nack_three(4, QuicTime::Zero(), 0); 2272 nack_three.received_info.missing_packets.insert(3); 2273 nack_three.received_info.entropy_hash = 2274 QuicConnectionPeer::GetSentEntropyHash(&connection_, 4) ^ 2275 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^ 2276 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2); 2277 QuicFrame frame(&nack_three); 2278 EXPECT_CALL(visitor_, OnAck(_)); 2279 EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1); 2280 EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1); 2281 EXPECT_CALL(visitor_, OnCanWrite()).Times(3).WillRepeatedly(Return(true)); 2282 2283 ProcessFramePacket(frame); 2284 ProcessFramePacket(frame); 2285 ProcessFramePacket(frame); 2286 2287 EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillOnce( 2288 Return(QuicTime::Delta::Zero())); 2289 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce( 2290 Return(QuicBandwidth::Zero())); 2291 2292 const QuicConnectionStats& stats = connection_.GetStats(); 2293 EXPECT_EQ(3 * first_packet_size + 2 * second_packet_size - kQuicVersionSize, 2294 stats.bytes_sent); 2295 EXPECT_EQ(5u, stats.packets_sent); 2296 EXPECT_EQ(2 * first_packet_size + second_packet_size - kQuicVersionSize, 2297 stats.bytes_retransmitted); 2298 EXPECT_EQ(3u, stats.packets_retransmitted); 2299 EXPECT_EQ(2u, stats.rto_count); 2300 } 2301 2302 TEST_F(QuicConnectionTest, CheckReceiveStats) { 2303 size_t received_bytes = 0; 2304 received_bytes += ProcessFecProtectedPacket(1, false, !kEntropyFlag); 2305 received_bytes += ProcessFecProtectedPacket(3, false, !kEntropyFlag); 2306 // Should be counted against dropped packets. 2307 received_bytes += ProcessDataPacket(3, 1, !kEntropyFlag); 2308 received_bytes += ProcessFecPacket(4, 1, true, !kEntropyFlag); // Fec packet 2309 2310 EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillOnce( 2311 Return(QuicTime::Delta::Zero())); 2312 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce( 2313 Return(QuicBandwidth::Zero())); 2314 2315 const QuicConnectionStats& stats = connection_.GetStats(); 2316 EXPECT_EQ(received_bytes, stats.bytes_received); 2317 EXPECT_EQ(4u, stats.packets_received); 2318 2319 EXPECT_EQ(1u, stats.packets_revived); 2320 EXPECT_EQ(1u, stats.packets_dropped); 2321 } 2322 2323 TEST_F(QuicConnectionTest, TestFecGroupLimits) { 2324 // Create and return a group for 1 2325 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 1) != NULL); 2326 2327 // Create and return a group for 2 2328 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 2) != NULL); 2329 2330 // Create and return a group for 4. This should remove 1 but not 2. 2331 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 4) != NULL); 2332 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 1) == NULL); 2333 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 2) != NULL); 2334 2335 // Create and return a group for 3. This will kill off 2. 2336 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 3) != NULL); 2337 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 2) == NULL); 2338 2339 // Verify that adding 5 kills off 3, despite 4 being created before 3. 2340 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 5) != NULL); 2341 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 4) != NULL); 2342 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 3) == NULL); 2343 } 2344 2345 TEST_F(QuicConnectionTest, DontProcessFramesIfPacketClosedConnection) { 2346 // Construct a packet with stream frame and connection close frame. 2347 header_.public_header.guid = guid_; 2348 header_.packet_sequence_number = 1; 2349 header_.public_header.reset_flag = false; 2350 header_.public_header.version_flag = false; 2351 header_.entropy_flag = false; 2352 header_.fec_flag = false; 2353 header_.fec_group = 0; 2354 2355 QuicConnectionCloseFrame qccf; 2356 qccf.error_code = QUIC_PEER_GOING_AWAY; 2357 qccf.ack_frame = QuicAckFrame(0, QuicTime::Zero(), 1); 2358 QuicFrame close_frame(&qccf); 2359 QuicFrame stream_frame(&frame1_); 2360 2361 QuicFrames frames; 2362 frames.push_back(stream_frame); 2363 frames.push_back(close_frame); 2364 scoped_ptr<QuicPacket> packet( 2365 framer_.BuildUnsizedDataPacket(header_, frames).packet); 2366 EXPECT_TRUE(NULL != packet.get()); 2367 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket( 2368 ENCRYPTION_NONE, 1, *packet)); 2369 2370 EXPECT_CALL(visitor_, ConnectionClose(QUIC_PEER_GOING_AWAY, true)); 2371 EXPECT_CALL(visitor_, OnPacket(_, _, _, _)).Times(0); 2372 2373 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 2374 } 2375 2376 TEST_F(QuicConnectionTest, SelectMutualVersion) { 2377 // Set the connection to speak the lowest quic version. 2378 connection_.set_version(QuicVersionMin()); 2379 EXPECT_EQ(QuicVersionMin(), connection_.version()); 2380 2381 // Pass in available versions which includes a higher mutually supported 2382 // version. The higher mutually supported version should be selected. 2383 QuicVersionVector supported_versions; 2384 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { 2385 supported_versions.push_back(kSupportedQuicVersions[i]); 2386 } 2387 EXPECT_TRUE(connection_.SelectMutualVersion(supported_versions)); 2388 EXPECT_EQ(QuicVersionMax(), connection_.version()); 2389 2390 // Expect that the lowest version is selected. 2391 // Ensure the lowest supported version is less than the max, unless they're 2392 // the same. 2393 EXPECT_LE(QuicVersionMin(), QuicVersionMax()); 2394 QuicVersionVector lowest_version_vector; 2395 lowest_version_vector.push_back(QuicVersionMin()); 2396 EXPECT_TRUE(connection_.SelectMutualVersion(lowest_version_vector)); 2397 EXPECT_EQ(QuicVersionMin(), connection_.version()); 2398 2399 // Shouldn't be able to find a mutually supported version. 2400 QuicVersionVector unsupported_version; 2401 unsupported_version.push_back(QUIC_VERSION_UNSUPPORTED); 2402 EXPECT_FALSE(connection_.SelectMutualVersion(unsupported_version)); 2403 } 2404 2405 TEST_F(QuicConnectionTest, ConnectionCloseWhenNotWriteBlocked) { 2406 helper_->set_blocked(false); // Already default. 2407 2408 // Send a packet (but write will not block). 2409 connection_.SendStreamData(1, "foo", 0, !kFin); 2410 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2411 EXPECT_EQ(1u, helper_->packets_write_attempts()); 2412 2413 // Send an erroneous packet to close the connection. 2414 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_PACKET_HEADER, false)); 2415 ProcessDataPacket(6000, 0, !kEntropyFlag); 2416 EXPECT_EQ(2u, helper_->packets_write_attempts()); 2417 } 2418 2419 TEST_F(QuicConnectionTest, ConnectionCloseWhenWriteBlocked) { 2420 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2421 helper_->set_blocked(true); 2422 2423 // Send a packet to so that write will really block. 2424 connection_.SendStreamData(1, "foo", 0, !kFin); 2425 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2426 EXPECT_EQ(1u, helper_->packets_write_attempts()); 2427 2428 // Send an erroneous packet to close the connection. 2429 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_PACKET_HEADER, false)); 2430 ProcessDataPacket(6000, 0, !kEntropyFlag); 2431 EXPECT_EQ(1u, helper_->packets_write_attempts()); 2432 } 2433 2434 TEST_F(QuicConnectionTest, ConnectionCloseWhenNothingPending) { 2435 helper_->set_blocked(true); 2436 2437 // Send an erroneous packet to close the connection. 2438 EXPECT_CALL(visitor_, ConnectionClose(QUIC_INVALID_PACKET_HEADER, false)); 2439 ProcessDataPacket(6000, 0, !kEntropyFlag); 2440 EXPECT_EQ(1u, helper_->packets_write_attempts()); 2441 } 2442 2443 } // namespace 2444 } // namespace test 2445 } // namespace net 2446