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 "base/stl_util.h" 10 #include "net/base/net_errors.h" 11 #include "net/quic/congestion_control/receive_algorithm_interface.h" 12 #include "net/quic/congestion_control/send_algorithm_interface.h" 13 #include "net/quic/crypto/null_encrypter.h" 14 #include "net/quic/crypto/quic_decrypter.h" 15 #include "net/quic/crypto/quic_encrypter.h" 16 #include "net/quic/quic_protocol.h" 17 #include "net/quic/quic_sent_packet_manager.h" 18 #include "net/quic/quic_utils.h" 19 #include "net/quic/test_tools/mock_clock.h" 20 #include "net/quic/test_tools/mock_random.h" 21 #include "net/quic/test_tools/quic_connection_peer.h" 22 #include "net/quic/test_tools/quic_framer_peer.h" 23 #include "net/quic/test_tools/quic_packet_creator_peer.h" 24 #include "net/quic/test_tools/quic_test_utils.h" 25 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gtest/include/gtest/gtest.h" 27 28 using base::StringPiece; 29 using std::map; 30 using std::vector; 31 using testing::_; 32 using testing::AnyNumber; 33 using testing::ContainerEq; 34 using testing::DoAll; 35 using testing::InSequence; 36 using testing::InvokeWithoutArgs; 37 using testing::Ref; 38 using testing::Return; 39 using testing::SaveArg; 40 using testing::StrictMock; 41 42 namespace net { 43 namespace test { 44 namespace { 45 46 const char data1[] = "foo"; 47 const char data2[] = "bar"; 48 49 const bool kFin = true; 50 const bool kEntropyFlag = true; 51 52 const QuicPacketEntropyHash kTestEntropyHash = 76; 53 54 const int kDefaultRetransmissionTimeMs = 500; 55 const int kMinRetransmissionTimeMs = 200; 56 57 // Used by TestConnection::SendStreamData3. 58 const QuicStreamId kStreamId3 = 3; 59 // Used by TestConnection::SendStreamData5. 60 const QuicStreamId kStreamId5 = 5; 61 62 class TestReceiveAlgorithm : public ReceiveAlgorithmInterface { 63 public: 64 explicit TestReceiveAlgorithm(QuicCongestionFeedbackFrame* feedback) 65 : feedback_(feedback) { 66 } 67 68 bool GenerateCongestionFeedback( 69 QuicCongestionFeedbackFrame* congestion_feedback) { 70 if (feedback_ == NULL) { 71 return false; 72 } 73 *congestion_feedback = *feedback_; 74 return true; 75 } 76 77 MOCK_METHOD4(RecordIncomingPacket, 78 void(QuicByteCount, QuicPacketSequenceNumber, QuicTime, bool)); 79 80 private: 81 QuicCongestionFeedbackFrame* feedback_; 82 83 DISALLOW_COPY_AND_ASSIGN(TestReceiveAlgorithm); 84 }; 85 86 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message. 87 class TaggingEncrypter : public QuicEncrypter { 88 public: 89 explicit TaggingEncrypter(uint8 tag) 90 : tag_(tag) { 91 } 92 93 virtual ~TaggingEncrypter() {} 94 95 // QuicEncrypter interface. 96 virtual bool SetKey(StringPiece key) OVERRIDE { return true; } 97 virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE { 98 return true; 99 } 100 101 virtual bool Encrypt(StringPiece nonce, 102 StringPiece associated_data, 103 StringPiece plaintext, 104 unsigned char* output) OVERRIDE { 105 memcpy(output, plaintext.data(), plaintext.size()); 106 output += plaintext.size(); 107 memset(output, tag_, kTagSize); 108 return true; 109 } 110 111 virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number, 112 StringPiece associated_data, 113 StringPiece plaintext) OVERRIDE { 114 const size_t len = plaintext.size() + kTagSize; 115 uint8* buffer = new uint8[len]; 116 Encrypt(StringPiece(), associated_data, plaintext, buffer); 117 return new QuicData(reinterpret_cast<char*>(buffer), len, true); 118 } 119 120 virtual size_t GetKeySize() const OVERRIDE { return 0; } 121 virtual size_t GetNoncePrefixSize() const OVERRIDE { return 0; } 122 123 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE { 124 return ciphertext_size - kTagSize; 125 } 126 127 virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE { 128 return plaintext_size + kTagSize; 129 } 130 131 virtual StringPiece GetKey() const OVERRIDE { 132 return StringPiece(); 133 } 134 135 virtual StringPiece GetNoncePrefix() const OVERRIDE { 136 return StringPiece(); 137 } 138 139 private: 140 enum { 141 kTagSize = 12, 142 }; 143 144 const uint8 tag_; 145 }; 146 147 // TaggingDecrypter ensures that the final kTagSize bytes of the message all 148 // have the same value and then removes them. 149 class TaggingDecrypter : public QuicDecrypter { 150 public: 151 virtual ~TaggingDecrypter() {} 152 153 // QuicDecrypter interface 154 virtual bool SetKey(StringPiece key) OVERRIDE { return true; } 155 virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE { 156 return true; 157 } 158 159 virtual bool Decrypt(StringPiece nonce, 160 StringPiece associated_data, 161 StringPiece ciphertext, 162 unsigned char* output, 163 size_t* output_length) OVERRIDE { 164 if (ciphertext.size() < kTagSize) { 165 return false; 166 } 167 if (!CheckTag(ciphertext, GetTag(ciphertext))) { 168 return false; 169 } 170 *output_length = ciphertext.size() - kTagSize; 171 memcpy(output, ciphertext.data(), *output_length); 172 return true; 173 } 174 175 virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number, 176 StringPiece associated_data, 177 StringPiece ciphertext) OVERRIDE { 178 if (ciphertext.size() < kTagSize) { 179 return NULL; 180 } 181 if (!CheckTag(ciphertext, GetTag(ciphertext))) { 182 return NULL; 183 } 184 const size_t len = ciphertext.size() - kTagSize; 185 uint8* buf = new uint8[len]; 186 memcpy(buf, ciphertext.data(), len); 187 return new QuicData(reinterpret_cast<char*>(buf), len, 188 true /* owns buffer */); 189 } 190 191 virtual StringPiece GetKey() const OVERRIDE { return StringPiece(); } 192 virtual StringPiece GetNoncePrefix() const OVERRIDE { return StringPiece(); } 193 194 protected: 195 virtual uint8 GetTag(StringPiece ciphertext) { 196 return ciphertext.data()[ciphertext.size()-1]; 197 } 198 199 private: 200 enum { 201 kTagSize = 12, 202 }; 203 204 bool CheckTag(StringPiece ciphertext, uint8 tag) { 205 for (size_t i = ciphertext.size() - kTagSize; i < ciphertext.size(); i++) { 206 if (ciphertext.data()[i] != tag) { 207 return false; 208 } 209 } 210 211 return true; 212 } 213 }; 214 215 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message 216 // match the expected value. 217 class StrictTaggingDecrypter : public TaggingDecrypter { 218 public: 219 explicit StrictTaggingDecrypter(uint8 tag) : tag_(tag) {} 220 virtual ~StrictTaggingDecrypter() {} 221 222 // TaggingQuicDecrypter 223 virtual uint8 GetTag(StringPiece ciphertext) OVERRIDE { 224 return tag_; 225 } 226 227 private: 228 const uint8 tag_; 229 }; 230 231 class TestConnectionHelper : public QuicConnectionHelperInterface { 232 public: 233 class TestAlarm : public QuicAlarm { 234 public: 235 explicit TestAlarm(QuicAlarm::Delegate* delegate) 236 : QuicAlarm(delegate) { 237 } 238 239 virtual void SetImpl() OVERRIDE {} 240 virtual void CancelImpl() OVERRIDE {} 241 using QuicAlarm::Fire; 242 }; 243 244 TestConnectionHelper(MockClock* clock, MockRandom* random_generator) 245 : clock_(clock), 246 random_generator_(random_generator) { 247 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); 248 } 249 250 // QuicConnectionHelperInterface 251 virtual const QuicClock* GetClock() const OVERRIDE { 252 return clock_; 253 } 254 255 virtual QuicRandom* GetRandomGenerator() OVERRIDE { 256 return random_generator_; 257 } 258 259 virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) OVERRIDE { 260 return new TestAlarm(delegate); 261 } 262 263 private: 264 MockClock* clock_; 265 MockRandom* random_generator_; 266 267 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper); 268 }; 269 270 class TestPacketWriter : public QuicPacketWriter { 271 public: 272 TestPacketWriter() 273 : last_packet_size_(0), 274 blocked_(false), 275 is_write_blocked_data_buffered_(false), 276 is_server_(true), 277 final_bytes_of_last_packet_(0), 278 final_bytes_of_previous_packet_(0), 279 use_tagging_decrypter_(false), 280 packets_write_attempts_(0) { 281 } 282 283 // QuicPacketWriter 284 virtual WriteResult WritePacket( 285 const char* buffer, size_t buf_len, 286 const IPAddressNumber& self_address, 287 const IPEndPoint& peer_address, 288 QuicBlockedWriterInterface* blocked_writer) OVERRIDE { 289 QuicEncryptedPacket packet(buffer, buf_len); 290 ++packets_write_attempts_; 291 292 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { 293 final_bytes_of_previous_packet_ = final_bytes_of_last_packet_; 294 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, 295 sizeof(final_bytes_of_last_packet_)); 296 } 297 298 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), !is_server_); 299 if (use_tagging_decrypter_) { 300 framer.SetDecrypter(new TaggingDecrypter); 301 } 302 visitor_.Reset(); 303 framer.set_visitor(&visitor_); 304 EXPECT_TRUE(framer.ProcessPacket(packet)); 305 if (blocked_) { 306 return WriteResult(WRITE_STATUS_BLOCKED, -1); 307 } 308 last_packet_size_ = packet.length(); 309 return WriteResult(WRITE_STATUS_OK, last_packet_size_); 310 } 311 312 virtual bool IsWriteBlockedDataBuffered() const OVERRIDE { 313 return is_write_blocked_data_buffered_; 314 } 315 316 // Resets the visitor's state by clearing out the headers and frames. 317 void Reset() { 318 visitor_.Reset(); 319 } 320 321 QuicPacketHeader* header() { return visitor_.header(); } 322 323 size_t frame_count() const { return visitor_.frame_count(); } 324 325 QuicAckFrame* ack() { return visitor_.ack(); } 326 327 QuicCongestionFeedbackFrame* feedback() { return visitor_.feedback(); } 328 329 QuicConnectionCloseFrame* close() { return visitor_.close(); } 330 331 const vector<QuicStreamFrame>* stream_frames() const { 332 return visitor_.stream_frames(); 333 } 334 335 size_t last_packet_size() { 336 return last_packet_size_; 337 } 338 339 QuicVersionNegotiationPacket* version_negotiation_packet() { 340 return visitor_.version_negotiation_packet(); 341 } 342 343 void set_blocked(bool blocked) { blocked_ = blocked; } 344 345 void set_is_write_blocked_data_buffered(bool buffered) { 346 is_write_blocked_data_buffered_ = buffered; 347 } 348 349 void set_is_server(bool is_server) { is_server_ = is_server; } 350 351 // final_bytes_of_last_packet_ returns the last four bytes of the previous 352 // packet as a little-endian, uint32. This is intended to be used with a 353 // TaggingEncrypter so that tests can determine which encrypter was used for 354 // a given packet. 355 uint32 final_bytes_of_last_packet() { return final_bytes_of_last_packet_; } 356 357 // Returns the final bytes of the second to last packet. 358 uint32 final_bytes_of_previous_packet() { 359 return final_bytes_of_previous_packet_; 360 } 361 362 void use_tagging_decrypter() { 363 use_tagging_decrypter_ = true; 364 } 365 366 uint32 packets_write_attempts() { return packets_write_attempts_; } 367 368 private: 369 FramerVisitorCapturingFrames visitor_; 370 size_t last_packet_size_; 371 bool blocked_; 372 bool is_write_blocked_data_buffered_; 373 bool is_server_; 374 uint32 final_bytes_of_last_packet_; 375 uint32 final_bytes_of_previous_packet_; 376 bool use_tagging_decrypter_; 377 uint32 packets_write_attempts_; 378 379 DISALLOW_COPY_AND_ASSIGN(TestPacketWriter); 380 }; 381 382 class TestConnection : public QuicConnection { 383 public: 384 TestConnection(QuicGuid guid, 385 IPEndPoint address, 386 TestConnectionHelper* helper, 387 TestPacketWriter* writer, 388 bool is_server) 389 : QuicConnection(guid, address, helper, writer, is_server, 390 QuicSupportedVersions()), 391 helper_(helper), 392 writer_(writer) { 393 writer_->set_is_server(is_server); 394 } 395 396 void SendAck() { 397 QuicConnectionPeer::SendAck(this); 398 } 399 400 void SetReceiveAlgorithm(TestReceiveAlgorithm* receive_algorithm) { 401 QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm); 402 } 403 404 void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) { 405 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm); 406 } 407 408 void SendPacket(EncryptionLevel level, 409 QuicPacketSequenceNumber sequence_number, 410 QuicPacket* packet, 411 QuicPacketEntropyHash entropy_hash, 412 HasRetransmittableData retransmittable) { 413 RetransmittableFrames* retransmittable_frames = 414 retransmittable == HAS_RETRANSMITTABLE_DATA ? 415 new RetransmittableFrames() : NULL; 416 OnSerializedPacket( 417 SerializedPacket(sequence_number, PACKET_6BYTE_SEQUENCE_NUMBER, 418 packet, entropy_hash, retransmittable_frames)); 419 } 420 421 QuicConsumedData SendStreamDataWithString( 422 QuicStreamId id, 423 StringPiece data, 424 QuicStreamOffset offset, 425 bool fin, 426 QuicAckNotifier::DelegateInterface* delegate) { 427 IOVector data_iov; 428 if (!data.empty()) { 429 data_iov.Append(const_cast<char*>(data.data()), data.size()); 430 } 431 return QuicConnection::SendStreamData(id, data_iov, offset, fin, delegate); 432 } 433 434 QuicConsumedData SendStreamData3() { 435 return SendStreamDataWithString(kStreamId3, "food", 0, !kFin, NULL); 436 } 437 438 QuicConsumedData SendStreamData5() { 439 return SendStreamDataWithString(kStreamId5, "food2", 0, !kFin, NULL); 440 } 441 442 // The crypto stream has special semantics so that it is not blocked by a 443 // congestion window limitation, and also so that it gets put into a separate 444 // packet (so that it is easier to reason about a crypto frame not being 445 // split needlessly across packet boundaries). As a result, we have separate 446 // tests for some cases for this stream. 447 QuicConsumedData SendCryptoStreamData() { 448 this->Flush(); 449 QuicConsumedData consumed = 450 SendStreamDataWithString(kCryptoStreamId, "chlo", 0, !kFin, NULL); 451 this->Flush(); 452 return consumed; 453 } 454 455 bool is_server() { 456 return QuicConnectionPeer::IsServer(this); 457 } 458 459 void set_version(QuicVersion version) { 460 framer_.set_version(version); 461 } 462 463 void set_is_server(bool is_server) { 464 writer_->set_is_server(is_server); 465 QuicPacketCreatorPeer::SetIsServer( 466 QuicConnectionPeer::GetPacketCreator(this), is_server); 467 QuicConnectionPeer::SetIsServer(this, is_server); 468 } 469 470 TestConnectionHelper::TestAlarm* GetAckAlarm() { 471 return reinterpret_cast<TestConnectionHelper::TestAlarm*>( 472 QuicConnectionPeer::GetAckAlarm(this)); 473 } 474 475 TestConnectionHelper::TestAlarm* GetRetransmissionAlarm() { 476 return reinterpret_cast<TestConnectionHelper::TestAlarm*>( 477 QuicConnectionPeer::GetRetransmissionAlarm(this)); 478 } 479 480 TestConnectionHelper::TestAlarm* GetSendAlarm() { 481 return reinterpret_cast<TestConnectionHelper::TestAlarm*>( 482 QuicConnectionPeer::GetSendAlarm(this)); 483 } 484 485 TestConnectionHelper::TestAlarm* GetResumeWritesAlarm() { 486 return reinterpret_cast<TestConnectionHelper::TestAlarm*>( 487 QuicConnectionPeer::GetResumeWritesAlarm(this)); 488 } 489 490 TestConnectionHelper::TestAlarm* GetTimeoutAlarm() { 491 return reinterpret_cast<TestConnectionHelper::TestAlarm*>( 492 QuicConnectionPeer::GetTimeoutAlarm(this)); 493 } 494 495 using QuicConnection::SelectMutualVersion; 496 497 private: 498 TestConnectionHelper* helper_; 499 TestPacketWriter* writer_; 500 501 DISALLOW_COPY_AND_ASSIGN(TestConnection); 502 }; 503 504 class QuicConnectionTest : public ::testing::TestWithParam<bool> { 505 protected: 506 QuicConnectionTest() 507 : guid_(42), 508 framer_(QuicSupportedVersions(), QuicTime::Zero(), false), 509 creator_(guid_, &framer_, &random_generator_, false), 510 send_algorithm_(new StrictMock<MockSendAlgorithm>), 511 helper_(new TestConnectionHelper(&clock_, &random_generator_)), 512 writer_(new TestPacketWriter()), 513 connection_(guid_, IPEndPoint(), helper_.get(), writer_.get(), false), 514 frame1_(1, false, 0, MakeIOVector(data1)), 515 frame2_(1, false, 3, MakeIOVector(data2)), 516 accept_packet_(true) { 517 connection_.set_visitor(&visitor_); 518 connection_.SetSendAlgorithm(send_algorithm_); 519 framer_.set_received_entropy_calculator(&entropy_calculator_); 520 // Simplify tests by not sending feedback unless specifically configured. 521 SetFeedback(NULL); 522 EXPECT_CALL( 523 *send_algorithm_, TimeUntilSend(_, _, _, _)).WillRepeatedly(Return( 524 QuicTime::Delta::Zero())); 525 EXPECT_CALL(*receive_algorithm_, 526 RecordIncomingPacket(_, _, _, _)).Times(AnyNumber()); 527 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) 528 .Times(AnyNumber()); 529 EXPECT_CALL(*send_algorithm_, RetransmissionDelay()).WillRepeatedly( 530 Return(QuicTime::Delta::Zero())); 531 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillRepeatedly(Return( 532 QuicBandwidth::FromKBitsPerSecond(100))); 533 EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillRepeatedly(Return( 534 QuicTime::Delta::FromMilliseconds(100))); 535 ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) 536 .WillByDefault(Return(true)); 537 EXPECT_CALL(visitor_, HasPendingHandshake()).Times(AnyNumber()); 538 EXPECT_CALL(visitor_, OnCanWrite()).Times(AnyNumber()).WillRepeatedly( 539 Return(true)); 540 } 541 542 QuicAckFrame* outgoing_ack() { 543 outgoing_ack_.reset(QuicConnectionPeer::CreateAckFrame(&connection_)); 544 return outgoing_ack_.get(); 545 } 546 547 QuicAckFrame* last_ack() { 548 return writer_->ack(); 549 } 550 551 QuicCongestionFeedbackFrame* last_feedback() { 552 return writer_->feedback(); 553 } 554 555 QuicConnectionCloseFrame* last_close() { 556 return writer_->close(); 557 } 558 559 QuicPacketHeader* last_header() { 560 return writer_->header(); 561 } 562 563 size_t last_sent_packet_size() { 564 return writer_->last_packet_size(); 565 } 566 567 uint32 final_bytes_of_last_packet() { 568 return writer_->final_bytes_of_last_packet(); 569 } 570 571 uint32 final_bytes_of_previous_packet() { 572 return writer_->final_bytes_of_previous_packet(); 573 } 574 575 void use_tagging_decrypter() { 576 writer_->use_tagging_decrypter(); 577 } 578 579 void ProcessPacket(QuicPacketSequenceNumber number) { 580 EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_)); 581 ProcessDataPacket(number, 0, !kEntropyFlag); 582 } 583 584 QuicPacketEntropyHash ProcessFramePacket(QuicFrame frame) { 585 QuicFrames frames; 586 frames.push_back(QuicFrame(frame)); 587 QuicPacketCreatorPeer::SetSendVersionInPacket(&creator_, 588 connection_.is_server()); 589 SerializedPacket serialized_packet = creator_.SerializeAllFrames(frames); 590 scoped_ptr<QuicPacket> packet(serialized_packet.packet); 591 scoped_ptr<QuicEncryptedPacket> encrypted( 592 framer_.EncryptPacket(ENCRYPTION_NONE, 593 serialized_packet.sequence_number, *packet)); 594 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 595 return serialized_packet.entropy_hash; 596 } 597 598 size_t ProcessDataPacket(QuicPacketSequenceNumber number, 599 QuicFecGroupNumber fec_group, 600 bool entropy_flag) { 601 return ProcessDataPacketAtLevel(number, fec_group, entropy_flag, 602 ENCRYPTION_NONE); 603 } 604 605 size_t ProcessDataPacketAtLevel(QuicPacketSequenceNumber number, 606 QuicFecGroupNumber fec_group, 607 bool entropy_flag, 608 EncryptionLevel level) { 609 scoped_ptr<QuicPacket> packet(ConstructDataPacket(number, fec_group, 610 entropy_flag)); 611 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket( 612 level, number, *packet)); 613 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 614 return encrypted->length(); 615 } 616 617 void ProcessClosePacket(QuicPacketSequenceNumber number, 618 QuicFecGroupNumber fec_group) { 619 scoped_ptr<QuicPacket> packet(ConstructClosePacket(number, fec_group)); 620 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket( 621 ENCRYPTION_NONE, number, *packet)); 622 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 623 } 624 625 size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number, 626 bool expect_revival, bool entropy_flag) { 627 if (expect_revival) { 628 EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_)); 629 } 630 EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_)) 631 .RetiresOnSaturation(); 632 return ProcessDataPacket(number, 1, entropy_flag); 633 } 634 635 // Processes an FEC packet that covers the packets that would have been 636 // received. 637 size_t ProcessFecPacket(QuicPacketSequenceNumber number, 638 QuicPacketSequenceNumber min_protected_packet, 639 bool expect_revival, 640 bool entropy_flag, 641 QuicPacket* packet) { 642 if (expect_revival) { 643 EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_)); 644 } 645 646 // Construct the decrypted data packet so we can compute the correct 647 // redundancy. If |packet| has been provided then use that, otherwise 648 // construct a default data packet. 649 scoped_ptr<QuicPacket> data_packet; 650 if (packet) { 651 data_packet.reset(packet); 652 } else { 653 data_packet.reset(ConstructDataPacket(number, 1, !kEntropyFlag)); 654 } 655 656 header_.public_header.guid = guid_; 657 header_.public_header.reset_flag = false; 658 header_.public_header.version_flag = false; 659 header_.entropy_flag = entropy_flag; 660 header_.fec_flag = true; 661 header_.packet_sequence_number = number; 662 header_.is_in_fec_group = IN_FEC_GROUP; 663 header_.fec_group = min_protected_packet; 664 QuicFecData fec_data; 665 fec_data.fec_group = header_.fec_group; 666 667 // Since all data packets in this test have the same payload, the 668 // redundancy is either equal to that payload or the xor of that payload 669 // with itself, depending on the number of packets. 670 if (((number - min_protected_packet) % 2) == 0) { 671 for (size_t i = GetStartOfFecProtectedData( 672 header_.public_header.guid_length, 673 header_.public_header.version_flag, 674 header_.public_header.sequence_number_length); 675 i < data_packet->length(); ++i) { 676 data_packet->mutable_data()[i] ^= data_packet->data()[i]; 677 } 678 } 679 fec_data.redundancy = data_packet->FecProtectedData(); 680 681 scoped_ptr<QuicPacket> fec_packet( 682 framer_.BuildFecPacket(header_, fec_data).packet); 683 scoped_ptr<QuicEncryptedPacket> encrypted( 684 framer_.EncryptPacket(ENCRYPTION_NONE, number, *fec_packet)); 685 686 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 687 return encrypted->length(); 688 } 689 690 QuicByteCount SendStreamDataToPeer(QuicStreamId id, 691 StringPiece data, 692 QuicStreamOffset offset, 693 bool fin, 694 QuicPacketSequenceNumber* last_packet) { 695 QuicByteCount packet_size; 696 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) 697 .WillOnce(DoAll(SaveArg<2>(&packet_size), Return(true))); 698 connection_.SendStreamDataWithString(id, data, offset, fin, NULL); 699 if (last_packet != NULL) { 700 *last_packet = 701 QuicConnectionPeer::GetPacketCreator(&connection_)->sequence_number(); 702 } 703 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) 704 .Times(AnyNumber()); 705 return packet_size; 706 } 707 708 void SendAckPacketToPeer() { 709 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); 710 connection_.SendAck(); 711 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) 712 .Times(AnyNumber()); 713 } 714 715 QuicPacketEntropyHash ProcessAckPacket(QuicAckFrame* frame) { 716 return ProcessFramePacket(QuicFrame(frame)); 717 } 718 719 QuicPacketEntropyHash ProcessGoAwayPacket(QuicGoAwayFrame* frame) { 720 return ProcessFramePacket(QuicFrame(frame)); 721 } 722 723 bool IsMissing(QuicPacketSequenceNumber number) { 724 return IsAwaitingPacket(outgoing_ack()->received_info, number); 725 } 726 727 QuicPacket* ConstructDataPacket(QuicPacketSequenceNumber number, 728 QuicFecGroupNumber fec_group, 729 bool entropy_flag) { 730 header_.public_header.guid = guid_; 731 header_.public_header.reset_flag = false; 732 header_.public_header.version_flag = false; 733 header_.entropy_flag = entropy_flag; 734 header_.fec_flag = false; 735 header_.packet_sequence_number = number; 736 header_.is_in_fec_group = fec_group == 0u ? NOT_IN_FEC_GROUP : IN_FEC_GROUP; 737 header_.fec_group = fec_group; 738 739 QuicFrames frames; 740 QuicFrame frame(&frame1_); 741 frames.push_back(frame); 742 QuicPacket* packet = 743 framer_.BuildUnsizedDataPacket(header_, frames).packet; 744 EXPECT_TRUE(packet != NULL); 745 return packet; 746 } 747 748 QuicPacket* ConstructClosePacket(QuicPacketSequenceNumber number, 749 QuicFecGroupNumber fec_group) { 750 header_.public_header.guid = guid_; 751 header_.packet_sequence_number = number; 752 header_.public_header.reset_flag = false; 753 header_.public_header.version_flag = false; 754 header_.entropy_flag = false; 755 header_.fec_flag = false; 756 header_.is_in_fec_group = fec_group == 0u ? NOT_IN_FEC_GROUP : IN_FEC_GROUP; 757 header_.fec_group = fec_group; 758 759 QuicConnectionCloseFrame qccf; 760 qccf.error_code = QUIC_PEER_GOING_AWAY; 761 762 QuicFrames frames; 763 QuicFrame frame(&qccf); 764 frames.push_back(frame); 765 QuicPacket* packet = 766 framer_.BuildUnsizedDataPacket(header_, frames).packet; 767 EXPECT_TRUE(packet != NULL); 768 return packet; 769 } 770 771 void SetFeedback(QuicCongestionFeedbackFrame* feedback) { 772 receive_algorithm_ = new TestReceiveAlgorithm(feedback); 773 connection_.SetReceiveAlgorithm(receive_algorithm_); 774 } 775 776 QuicTime::Delta DefaultRetransmissionTime() { 777 return QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs); 778 } 779 780 QuicTime::Delta DefaultDelayedAckTime() { 781 return QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs/2); 782 } 783 784 QuicGuid guid_; 785 QuicFramer framer_; 786 QuicPacketCreator creator_; 787 MockEntropyCalculator entropy_calculator_; 788 789 MockSendAlgorithm* send_algorithm_; 790 TestReceiveAlgorithm* receive_algorithm_; 791 MockClock clock_; 792 MockRandom random_generator_; 793 scoped_ptr<TestConnectionHelper> helper_; 794 scoped_ptr<TestPacketWriter> writer_; 795 TestConnection connection_; 796 StrictMock<MockConnectionVisitor> visitor_; 797 798 QuicPacketHeader header_; 799 QuicStreamFrame frame1_; 800 QuicStreamFrame frame2_; 801 scoped_ptr<QuicAckFrame> outgoing_ack_; 802 bool accept_packet_; 803 804 private: 805 DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest); 806 }; 807 808 TEST_F(QuicConnectionTest, PacketsInOrder) { 809 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 810 811 ProcessPacket(1); 812 EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed); 813 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size()); 814 815 ProcessPacket(2); 816 EXPECT_EQ(2u, outgoing_ack()->received_info.largest_observed); 817 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size()); 818 819 ProcessPacket(3); 820 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 821 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size()); 822 } 823 824 TEST_F(QuicConnectionTest, PacketsRejected) { 825 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 826 827 ProcessPacket(1); 828 EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed); 829 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size()); 830 831 accept_packet_ = false; 832 ProcessPacket(2); 833 // We should not have an ack for two. 834 EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed); 835 EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size()); 836 } 837 838 TEST_F(QuicConnectionTest, PacketsOutOfOrder) { 839 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 840 841 ProcessPacket(3); 842 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 843 EXPECT_TRUE(IsMissing(2)); 844 EXPECT_TRUE(IsMissing(1)); 845 846 ProcessPacket(2); 847 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 848 EXPECT_FALSE(IsMissing(2)); 849 EXPECT_TRUE(IsMissing(1)); 850 851 ProcessPacket(1); 852 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 853 EXPECT_FALSE(IsMissing(2)); 854 EXPECT_FALSE(IsMissing(1)); 855 } 856 857 TEST_F(QuicConnectionTest, DuplicatePacket) { 858 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 859 860 ProcessPacket(3); 861 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 862 EXPECT_TRUE(IsMissing(2)); 863 EXPECT_TRUE(IsMissing(1)); 864 865 // Send packet 3 again, but do not set the expectation that 866 // the visitor OnStreamFrames() will be called. 867 ProcessDataPacket(3, 0, !kEntropyFlag); 868 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 869 EXPECT_TRUE(IsMissing(2)); 870 EXPECT_TRUE(IsMissing(1)); 871 } 872 873 TEST_F(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndLeastAwaiting) { 874 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 875 876 ProcessPacket(3); 877 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 878 EXPECT_TRUE(IsMissing(2)); 879 EXPECT_TRUE(IsMissing(1)); 880 881 ProcessPacket(2); 882 EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed); 883 EXPECT_TRUE(IsMissing(1)); 884 885 ProcessPacket(5); 886 EXPECT_EQ(5u, outgoing_ack()->received_info.largest_observed); 887 EXPECT_TRUE(IsMissing(1)); 888 EXPECT_TRUE(IsMissing(4)); 889 890 // Pretend at this point the client has gotten acks for 2 and 3 and 1 is a 891 // packet the peer will not retransmit. It indicates this by sending 'least 892 // awaiting' is 4. The connection should then realize 1 will not be 893 // retransmitted, and will remove it from the missing list. 894 creator_.set_sequence_number(5); 895 QuicAckFrame frame(0, QuicTime::Zero(), 4); 896 ProcessAckPacket(&frame); 897 898 // Force an ack to be sent. 899 SendAckPacketToPeer(); 900 EXPECT_TRUE(IsMissing(4)); 901 } 902 903 TEST_F(QuicConnectionTest, RejectPacketTooFarOut) { 904 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a 905 // packet call to the visitor. 906 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false)); 907 ProcessDataPacket(6000, 0, !kEntropyFlag); 908 } 909 910 TEST_F(QuicConnectionTest, TruncatedAck) { 911 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 912 QuicPacketSequenceNumber num_packets = 256 * 2 + 1; 913 for (QuicPacketSequenceNumber i = 0; i < num_packets; ++i) { 914 SendStreamDataToPeer(1, "foo", i * 3, !kFin, NULL); 915 } 916 917 QuicAckFrame frame(num_packets, QuicTime::Zero(), 1); 918 // Create an ack with 256 nacks, none adjacent to one another. 919 for (QuicPacketSequenceNumber i = 1; i <= 256; ++i) { 920 frame.received_info.missing_packets.insert(i * 2); 921 } 922 frame.received_info.entropy_hash = 0; 923 EXPECT_CALL(entropy_calculator_, 924 EntropyHash(511)).WillOnce(testing::Return(0)); 925 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(256); 926 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(2); 927 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(2); 928 ProcessAckPacket(&frame); 929 930 QuicReceivedPacketManager* received_packet_manager = 931 QuicConnectionPeer::GetReceivedPacketManager(&connection_); 932 // A truncated ack will not have the true largest observed. 933 EXPECT_GT(num_packets, 934 received_packet_manager->peer_largest_observed_packet()); 935 936 frame.received_info.missing_packets.erase(192); 937 frame.received_info.entropy_hash = 2; 938 939 // Removing one missing packet allows us to ack 192 and one more range. 940 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(2); 941 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(2); 942 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(2); 943 ProcessAckPacket(&frame); 944 EXPECT_EQ(num_packets, 945 received_packet_manager->peer_largest_observed_packet()); 946 } 947 948 TEST_F(QuicConnectionTest, AckReceiptCausesAckSendBadEntropy) { 949 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 950 951 ProcessPacket(1); 952 // Delay sending, then queue up an ack. 953 EXPECT_CALL(*send_algorithm_, 954 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 955 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 956 QuicConnectionPeer::SendAck(&connection_); 957 958 // Process an ack with a least unacked of the received ack. 959 // This causes an ack to be sent when TimeUntilSend returns 0. 960 EXPECT_CALL(*send_algorithm_, 961 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly( 962 testing::Return(QuicTime::Delta::Zero())); 963 // Skip a packet and then record an ack. 964 creator_.set_sequence_number(2); 965 QuicAckFrame frame(0, QuicTime::Zero(), 3); 966 ProcessAckPacket(&frame); 967 } 968 969 TEST_F(QuicConnectionTest, OutOfOrderReceiptCausesAckSend) { 970 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 971 972 ProcessPacket(3); 973 // Should ack immediately since we have missing packets. 974 EXPECT_EQ(1u, writer_->packets_write_attempts()); 975 976 ProcessPacket(2); 977 // Should ack immediately since we have missing packets. 978 EXPECT_EQ(2u, writer_->packets_write_attempts()); 979 980 ProcessPacket(1); 981 // Should ack immediately, since this fills the last hole. 982 EXPECT_EQ(3u, writer_->packets_write_attempts()); 983 984 ProcessPacket(4); 985 // Should not cause an ack. 986 EXPECT_EQ(3u, writer_->packets_write_attempts()); 987 } 988 989 TEST_F(QuicConnectionTest, AckReceiptCausesAckSend) { 990 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 991 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(1); 992 QuicPacketSequenceNumber original; 993 QuicByteCount packet_size; 994 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)) 995 .WillOnce(DoAll(SaveArg<1>(&original), SaveArg<2>(&packet_size), 996 Return(true))); 997 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)).Times(1); 998 connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL); 999 QuicAckFrame frame(original, QuicTime::Zero(), 1); 1000 frame.received_info.missing_packets.insert(original); 1001 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash( 1002 &connection_, original - 1); 1003 // First nack triggers early retransmit. 1004 QuicPacketSequenceNumber retransmission; 1005 EXPECT_CALL(*send_algorithm_, 1006 OnPacketSent(_, _, packet_size - kQuicVersionSize, 1007 NACK_RETRANSMISSION, _)) 1008 .WillOnce(DoAll(SaveArg<1>(&retransmission), Return(true))); 1009 1010 ProcessAckPacket(&frame); 1011 1012 QuicAckFrame frame2(retransmission, QuicTime::Zero(), 1); 1013 frame2.received_info.missing_packets.insert(original); 1014 frame2.received_info.entropy_hash = 1015 QuicConnectionPeer::GetSentEntropyHash(&connection_, retransmission) ^ 1016 QuicConnectionPeer::GetSentEntropyHash(&connection_, original); 1017 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)); 1018 1019 ProcessAckPacket(&frame2); 1020 // Now if the peer sends an ack which still reports the retransmitted packet 1021 // as missing, then that will count as a packet which instigates an ack. 1022 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)); 1023 ProcessAckPacket(&frame2); 1024 ProcessAckPacket(&frame2); 1025 1026 // But an ack with no missing packets will not send an ack. 1027 frame2.received_info.missing_packets.clear(); 1028 frame2.received_info.entropy_hash = 1029 QuicConnectionPeer::GetSentEntropyHash(&connection_, retransmission); 1030 ProcessAckPacket(&frame2); 1031 ProcessAckPacket(&frame2); 1032 } 1033 1034 TEST_F(QuicConnectionTest, LeastUnackedLower) { 1035 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1036 1037 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 1038 SendStreamDataToPeer(1, "bar", 3, !kFin, NULL); 1039 SendStreamDataToPeer(1, "eep", 6, !kFin, NULL); 1040 1041 // Start out saying the least unacked is 2. 1042 creator_.set_sequence_number(5); 1043 QuicAckFrame frame(0, QuicTime::Zero(), 2); 1044 ProcessAckPacket(&frame); 1045 1046 // Change it to 1, but lower the sequence number to fake out-of-order packets. 1047 // This should be fine. 1048 creator_.set_sequence_number(1); 1049 QuicAckFrame frame2(0, QuicTime::Zero(), 1); 1050 // The scheduler will not process out of order acks. 1051 EXPECT_CALL(visitor_, OnCanWrite()).Times(0); 1052 ProcessAckPacket(&frame2); 1053 1054 // Now claim it's one, but set the ordering so it was sent "after" the first 1055 // one. This should cause a connection error. 1056 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false)); 1057 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); 1058 creator_.set_sequence_number(7); 1059 ProcessAckPacket(&frame2); 1060 } 1061 1062 TEST_F(QuicConnectionTest, LargestObservedLower) { 1063 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1064 1065 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 1066 SendStreamDataToPeer(1, "bar", 3, !kFin, NULL); 1067 SendStreamDataToPeer(1, "eep", 6, !kFin, NULL); 1068 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(2); 1069 1070 // Start out saying the largest observed is 2. 1071 QuicAckFrame frame(2, QuicTime::Zero(), 0); 1072 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash( 1073 &connection_, 2); 1074 ProcessAckPacket(&frame); 1075 1076 // Now change it to 1, and it should cause a connection error. 1077 QuicAckFrame frame2(1, QuicTime::Zero(), 0); 1078 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false)); 1079 EXPECT_CALL(visitor_, OnCanWrite()).Times(0); 1080 ProcessAckPacket(&frame2); 1081 } 1082 1083 TEST_F(QuicConnectionTest, AckUnsentData) { 1084 // Ack a packet which has not been sent. 1085 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false)); 1086 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1087 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); 1088 QuicAckFrame frame(1, QuicTime::Zero(), 0); 1089 EXPECT_CALL(visitor_, OnCanWrite()).Times(0); 1090 ProcessAckPacket(&frame); 1091 } 1092 1093 TEST_F(QuicConnectionTest, AckAll) { 1094 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1095 ProcessPacket(1); 1096 1097 creator_.set_sequence_number(1); 1098 QuicAckFrame frame1(0, QuicTime::Zero(), 1); 1099 ProcessAckPacket(&frame1); 1100 } 1101 1102 TEST_F(QuicConnectionTest, SendingDifferentSequenceNumberLengthsBandwidth) { 1103 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return( 1104 QuicBandwidth::FromKBitsPerSecond(1000))); 1105 1106 QuicPacketSequenceNumber last_packet; 1107 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); 1108 EXPECT_EQ(1u, last_packet); 1109 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, 1110 connection_.options()->send_sequence_number_length); 1111 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, 1112 last_header()->public_header.sequence_number_length); 1113 1114 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return( 1115 QuicBandwidth::FromKBitsPerSecond(1000 * 256))); 1116 1117 SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet); 1118 EXPECT_EQ(2u, last_packet); 1119 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER, 1120 connection_.options()->send_sequence_number_length); 1121 // The 1 packet lag is due to the sequence number length being recalculated in 1122 // QuicConnection after a packet is sent. 1123 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, 1124 last_header()->public_header.sequence_number_length); 1125 1126 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return( 1127 QuicBandwidth::FromKBitsPerSecond(1000 * 256 * 256))); 1128 1129 SendStreamDataToPeer(1, "foo", 6, !kFin, &last_packet); 1130 EXPECT_EQ(3u, last_packet); 1131 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, 1132 connection_.options()->send_sequence_number_length); 1133 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER, 1134 last_header()->public_header.sequence_number_length); 1135 1136 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return( 1137 QuicBandwidth::FromKBitsPerSecond(1000ll * 256 * 256 * 256))); 1138 1139 SendStreamDataToPeer(1, "bar", 9, !kFin, &last_packet); 1140 EXPECT_EQ(4u, last_packet); 1141 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, 1142 connection_.options()->send_sequence_number_length); 1143 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, 1144 last_header()->public_header.sequence_number_length); 1145 1146 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return( 1147 QuicBandwidth::FromKBitsPerSecond(1000ll * 256 * 256 * 256 * 256))); 1148 1149 SendStreamDataToPeer(1, "foo", 12, !kFin, &last_packet); 1150 EXPECT_EQ(5u, last_packet); 1151 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER, 1152 connection_.options()->send_sequence_number_length); 1153 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, 1154 last_header()->public_header.sequence_number_length); 1155 } 1156 1157 TEST_F(QuicConnectionTest, SendingDifferentSequenceNumberLengthsUnackedDelta) { 1158 QuicPacketSequenceNumber last_packet; 1159 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); 1160 EXPECT_EQ(1u, last_packet); 1161 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, 1162 connection_.options()->send_sequence_number_length); 1163 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, 1164 last_header()->public_header.sequence_number_length); 1165 1166 QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number(100); 1167 1168 SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet); 1169 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER, 1170 connection_.options()->send_sequence_number_length); 1171 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, 1172 last_header()->public_header.sequence_number_length); 1173 1174 QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number( 1175 100 * 256); 1176 1177 SendStreamDataToPeer(1, "foo", 6, !kFin, &last_packet); 1178 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, 1179 connection_.options()->send_sequence_number_length); 1180 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER, 1181 last_header()->public_header.sequence_number_length); 1182 1183 QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number( 1184 100 * 256 * 256); 1185 1186 SendStreamDataToPeer(1, "bar", 9, !kFin, &last_packet); 1187 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, 1188 connection_.options()->send_sequence_number_length); 1189 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, 1190 last_header()->public_header.sequence_number_length); 1191 1192 QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number( 1193 100 * 256 * 256 * 256); 1194 1195 SendStreamDataToPeer(1, "foo", 12, !kFin, &last_packet); 1196 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER, 1197 connection_.options()->send_sequence_number_length); 1198 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, 1199 last_header()->public_header.sequence_number_length); 1200 } 1201 1202 TEST_F(QuicConnectionTest, BasicSending) { 1203 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1204 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(6); 1205 QuicPacketSequenceNumber last_packet; 1206 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1 1207 EXPECT_EQ(1u, last_packet); 1208 SendAckPacketToPeer(); // Packet 2 1209 1210 EXPECT_EQ(1u, last_ack()->sent_info.least_unacked); 1211 1212 SendAckPacketToPeer(); // Packet 3 1213 EXPECT_EQ(1u, last_ack()->sent_info.least_unacked); 1214 1215 SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet); // Packet 4 1216 EXPECT_EQ(4u, last_packet); 1217 SendAckPacketToPeer(); // Packet 5 1218 EXPECT_EQ(1u, last_ack()->sent_info.least_unacked); 1219 1220 // Peer acks up to packet 3. 1221 QuicAckFrame frame(3, QuicTime::Zero(), 0); 1222 frame.received_info.entropy_hash = 1223 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3); 1224 ProcessAckPacket(&frame); 1225 SendAckPacketToPeer(); // Packet 6 1226 1227 // As soon as we've acked one, we skip ack packets 2 and 3 and note lack of 1228 // ack for 4. 1229 EXPECT_EQ(4u, last_ack()->sent_info.least_unacked); 1230 1231 // Peer acks up to packet 4, the last packet. 1232 QuicAckFrame frame2(6, QuicTime::Zero(), 0); 1233 frame2.received_info.entropy_hash = 1234 QuicConnectionPeer::GetSentEntropyHash(&connection_, 6); 1235 ProcessAckPacket(&frame2); // Acks don't instigate acks. 1236 1237 // Verify that we did not send an ack. 1238 EXPECT_EQ(6u, last_header()->packet_sequence_number); 1239 1240 // So the last ack has not changed. 1241 EXPECT_EQ(4u, last_ack()->sent_info.least_unacked); 1242 1243 // If we force an ack, we shouldn't change our retransmit state. 1244 SendAckPacketToPeer(); // Packet 7 1245 EXPECT_EQ(7u, last_ack()->sent_info.least_unacked); 1246 1247 // But if we send more data it should. 1248 SendStreamDataToPeer(1, "eep", 6, !kFin, &last_packet); // Packet 8 1249 EXPECT_EQ(8u, last_packet); 1250 SendAckPacketToPeer(); // Packet 9 1251 EXPECT_EQ(8u, last_ack()->sent_info.least_unacked); 1252 } 1253 1254 TEST_F(QuicConnectionTest, FECSending) { 1255 // All packets carry version info till version is negotiated. 1256 size_t payload_length; 1257 connection_.options()->max_packet_length = 1258 GetPacketLengthForOneStream( 1259 connection_.version(), kIncludeVersion, PACKET_1BYTE_SEQUENCE_NUMBER, 1260 IN_FEC_GROUP, &payload_length); 1261 // And send FEC every two packets. 1262 connection_.options()->max_packets_per_fec_group = 2; 1263 1264 // Send 4 data packets and 2 FEC packets. 1265 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(6); 1266 // The first stream frame will consume 2 fewer bytes than the other three. 1267 const string payload(payload_length * 4 - 6, 'a'); 1268 connection_.SendStreamDataWithString(1, payload, 0, !kFin, NULL); 1269 // Expect the FEC group to be closed after SendStreamDataWithString. 1270 EXPECT_FALSE(creator_.ShouldSendFec(true)); 1271 } 1272 1273 TEST_F(QuicConnectionTest, FECQueueing) { 1274 // All packets carry version info till version is negotiated. 1275 size_t payload_length; 1276 connection_.options()->max_packet_length = 1277 GetPacketLengthForOneStream( 1278 connection_.version(), kIncludeVersion, PACKET_1BYTE_SEQUENCE_NUMBER, 1279 IN_FEC_GROUP, &payload_length); 1280 // And send FEC every two packets. 1281 connection_.options()->max_packets_per_fec_group = 2; 1282 1283 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1284 writer_->set_blocked(true); 1285 const string payload(payload_length, 'a'); 1286 connection_.SendStreamDataWithString(1, payload, 0, !kFin, NULL); 1287 EXPECT_FALSE(creator_.ShouldSendFec(true)); 1288 // Expect the first data packet and the fec packet to be queued. 1289 EXPECT_EQ(2u, connection_.NumQueuedPackets()); 1290 } 1291 1292 TEST_F(QuicConnectionTest, AbandonFECFromCongestionWindow) { 1293 connection_.options()->max_packets_per_fec_group = 1; 1294 // 1 Data and 1 FEC packet. 1295 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); 1296 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 1297 1298 const QuicTime::Delta retransmission_time = 1299 QuicTime::Delta::FromMilliseconds(5000); 1300 clock_.AdvanceTime(retransmission_time); 1301 1302 // Abandon FEC packet and data packet. 1303 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(2); 1304 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 1305 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); 1306 EXPECT_CALL(visitor_, OnCanWrite()); 1307 connection_.OnRetransmissionTimeout(); 1308 } 1309 1310 TEST_F(QuicConnectionTest, DontAbandonAckedFEC) { 1311 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1312 connection_.options()->max_packets_per_fec_group = 1; 1313 1314 // 1 Data and 1 FEC packet. 1315 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(6); 1316 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 1317 // Send some more data afterwards to ensure early retransmit doesn't trigger. 1318 connection_.SendStreamDataWithString(1, "foo", 3, !kFin, NULL); 1319 connection_.SendStreamDataWithString(1, "foo", 6, !kFin, NULL); 1320 1321 QuicAckFrame ack_fec(2, QuicTime::Zero(), 1); 1322 // Data packet missing. 1323 // TODO(ianswett): Note that this is not a sensible ack, since if the FEC was 1324 // received, it would cause the covered packet to be acked as well. 1325 ack_fec.received_info.missing_packets.insert(1); 1326 ack_fec.received_info.entropy_hash = 1327 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^ 1328 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 1329 1330 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(1); 1331 1332 ProcessAckPacket(&ack_fec); 1333 1334 clock_.AdvanceTime(DefaultRetransmissionTime()); 1335 1336 // Don't abandon the acked FEC packet, but it will abandon 2 the subsequent 1337 // FEC packets. 1338 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(5); 1339 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 1340 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(3); 1341 connection_.GetRetransmissionAlarm()->Fire(); 1342 } 1343 1344 TEST_F(QuicConnectionTest, DontAbandonAllFEC) { 1345 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1346 connection_.options()->max_packets_per_fec_group = 1; 1347 1348 // 1 Data and 1 FEC packet. 1349 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(6); 1350 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 1351 // Send some more data afterwards to ensure early retransmit doesn't trigger. 1352 connection_.SendStreamDataWithString(1, "foo", 3, !kFin, NULL); 1353 // Advance the time so not all the FEC packets are abandoned. 1354 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1)); 1355 connection_.SendStreamDataWithString(1, "foo", 6, !kFin, NULL); 1356 1357 QuicAckFrame ack_fec(5, QuicTime::Zero(), 1); 1358 // Ack all data packets, but no fec packets. 1359 ack_fec.received_info.missing_packets.insert(2); 1360 ack_fec.received_info.missing_packets.insert(4); 1361 ack_fec.received_info.entropy_hash = 1362 QuicConnectionPeer::GetSentEntropyHash(&connection_, 5) ^ 1363 QuicConnectionPeer::GetSentEntropyHash(&connection_, 4) ^ 1364 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^ 1365 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^ 1366 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 1367 1368 // Lose the first FEC packet and ack the three data packets. 1369 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(3); 1370 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _)); 1371 EXPECT_CALL(*send_algorithm_, OnPacketLost(2, _)); 1372 ProcessAckPacket(&ack_fec); 1373 1374 clock_.AdvanceTime(DefaultRetransmissionTime().Subtract( 1375 QuicTime::Delta::FromMilliseconds(1))); 1376 1377 // Don't abandon the acked FEC packet, but it will abandon 1 of the subsequent 1378 // FEC packets. 1379 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(4, _)); 1380 connection_.GetRetransmissionAlarm()->Fire(); 1381 1382 // Ensure the connection's alarm is still set, in order to abandon the third 1383 // FEC packet. 1384 EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); 1385 } 1386 1387 TEST_F(QuicConnectionTest, FramePacking) { 1388 // Block the connection. 1389 connection_.GetSendAlarm()->Set( 1390 clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1))); 1391 1392 // Send an ack and two stream frames in 1 packet by queueing them. 1393 connection_.SendAck(); 1394 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll( 1395 IgnoreResult(InvokeWithoutArgs(&connection_, 1396 &TestConnection::SendStreamData3)), 1397 IgnoreResult(InvokeWithoutArgs(&connection_, 1398 &TestConnection::SendStreamData5)), 1399 Return(true))); 1400 1401 EXPECT_CALL(*send_algorithm_, 1402 OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)) 1403 .Times(1); 1404 // Unblock the connection. 1405 connection_.GetSendAlarm()->Fire(); 1406 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1407 EXPECT_FALSE(connection_.HasQueuedData()); 1408 1409 // Parse the last packet and ensure it's an ack and two stream frames from 1410 // two different streams. 1411 EXPECT_EQ(3u, writer_->frame_count()); 1412 EXPECT_TRUE(writer_->ack()); 1413 EXPECT_EQ(2u, writer_->stream_frames()->size()); 1414 EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id); 1415 EXPECT_EQ(kStreamId5, (*writer_->stream_frames())[1].stream_id); 1416 } 1417 1418 TEST_F(QuicConnectionTest, FramePackingNonCryptoThenCrypto) { 1419 // Block the connection. 1420 connection_.GetSendAlarm()->Set( 1421 clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1))); 1422 1423 // Send an ack and two stream frames (one non-crypto, then one crypto) in 2 1424 // packets by queueing them. 1425 connection_.SendAck(); 1426 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll( 1427 IgnoreResult(InvokeWithoutArgs(&connection_, 1428 &TestConnection::SendStreamData3)), 1429 IgnoreResult(InvokeWithoutArgs(&connection_, 1430 &TestConnection::SendCryptoStreamData)), 1431 Return(true))); 1432 1433 EXPECT_CALL(*send_algorithm_, 1434 OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)) 1435 .Times(2); 1436 // Unblock the connection. 1437 connection_.GetSendAlarm()->Fire(); 1438 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1439 EXPECT_FALSE(connection_.HasQueuedData()); 1440 1441 // Parse the last packet and ensure it's the crypto stream frame. 1442 EXPECT_EQ(1u, writer_->frame_count()); 1443 EXPECT_EQ(1u, writer_->stream_frames()->size()); 1444 EXPECT_EQ(kCryptoStreamId, (*writer_->stream_frames())[0].stream_id); 1445 } 1446 1447 TEST_F(QuicConnectionTest, FramePackingCryptoThenNonCrypto) { 1448 // Block the connection. 1449 connection_.GetSendAlarm()->Set( 1450 clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1))); 1451 1452 // Send an ack and two stream frames (one crypto, then one non-crypto) in 3 1453 // packets by queueing them. 1454 connection_.SendAck(); 1455 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll( 1456 IgnoreResult(InvokeWithoutArgs(&connection_, 1457 &TestConnection::SendCryptoStreamData)), 1458 IgnoreResult(InvokeWithoutArgs(&connection_, 1459 &TestConnection::SendStreamData3)), 1460 Return(true))); 1461 1462 EXPECT_CALL(*send_algorithm_, 1463 OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)) 1464 .Times(3); 1465 // Unblock the connection. 1466 connection_.GetSendAlarm()->Fire(); 1467 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1468 EXPECT_FALSE(connection_.HasQueuedData()); 1469 1470 // Parse the last packet and ensure it's the stream frame from stream 3. 1471 EXPECT_EQ(1u, writer_->frame_count()); 1472 EXPECT_EQ(1u, writer_->stream_frames()->size()); 1473 EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id); 1474 } 1475 1476 TEST_F(QuicConnectionTest, FramePackingFEC) { 1477 // Enable fec. 1478 connection_.options()->max_packets_per_fec_group = 6; 1479 // Block the connection. 1480 connection_.GetSendAlarm()->Set( 1481 clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1))); 1482 1483 // Send an ack and two stream frames in 1 packet by queueing them. 1484 connection_.SendAck(); 1485 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll( 1486 IgnoreResult(InvokeWithoutArgs(&connection_, 1487 &TestConnection::SendStreamData3)), 1488 IgnoreResult(InvokeWithoutArgs(&connection_, 1489 &TestConnection::SendStreamData5)), 1490 Return(true))); 1491 1492 EXPECT_CALL(*send_algorithm_, 1493 OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)).Times(2); 1494 // Unblock the connection. 1495 connection_.GetSendAlarm()->Fire(); 1496 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1497 EXPECT_FALSE(connection_.HasQueuedData()); 1498 1499 // Parse the last packet and ensure it's in an fec group. 1500 EXPECT_EQ(1u, writer_->header()->fec_group); 1501 EXPECT_EQ(0u, writer_->frame_count()); 1502 } 1503 1504 TEST_F(QuicConnectionTest, FramePackingAckResponse) { 1505 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1506 // Process a data packet to queue up a pending ack. 1507 EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(true)); 1508 ProcessDataPacket(1, 1, kEntropyFlag); 1509 1510 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll( 1511 IgnoreResult(InvokeWithoutArgs(&connection_, 1512 &TestConnection::SendStreamData3)), 1513 IgnoreResult(InvokeWithoutArgs(&connection_, 1514 &TestConnection::SendStreamData5)), 1515 Return(true))); 1516 1517 EXPECT_CALL(*send_algorithm_, 1518 OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)) 1519 .Times(1); 1520 1521 // Process an ack to cause the visitor's OnCanWrite to be invoked. 1522 creator_.set_sequence_number(2); 1523 QuicAckFrame ack_one(0, QuicTime::Zero(), 0); 1524 ProcessAckPacket(&ack_one); 1525 1526 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1527 EXPECT_FALSE(connection_.HasQueuedData()); 1528 1529 // Parse the last packet and ensure it's an ack and two stream frames from 1530 // two different streams. 1531 EXPECT_EQ(3u, writer_->frame_count()); 1532 EXPECT_TRUE(writer_->ack()); 1533 ASSERT_EQ(2u, writer_->stream_frames()->size()); 1534 EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id); 1535 EXPECT_EQ(kStreamId5, (*writer_->stream_frames())[1].stream_id); 1536 } 1537 1538 TEST_F(QuicConnectionTest, FramePackingSendv) { 1539 // Send data in 1 packet by writing multiple blocks in a single iovector 1540 // using writev. 1541 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)); 1542 1543 char data[] = "ABCD"; 1544 IOVector data_iov; 1545 data_iov.AppendNoCoalesce(data, 2); 1546 data_iov.AppendNoCoalesce(data + 2, 2); 1547 connection_.SendStreamData(1, data_iov, 0, !kFin, NULL); 1548 1549 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1550 EXPECT_FALSE(connection_.HasQueuedData()); 1551 1552 // Parse the last packet and ensure multiple iovector blocks have 1553 // been packed into a single stream frame from one stream. 1554 EXPECT_EQ(1u, writer_->frame_count()); 1555 EXPECT_EQ(1u, writer_->stream_frames()->size()); 1556 QuicStreamFrame frame = (*writer_->stream_frames())[0]; 1557 EXPECT_EQ(1u, frame.stream_id); 1558 EXPECT_EQ("ABCD", string(static_cast<char*> 1559 (frame.data.iovec()[0].iov_base), 1560 (frame.data.iovec()[0].iov_len))); 1561 } 1562 1563 TEST_F(QuicConnectionTest, FramePackingSendvQueued) { 1564 // Try to send two stream frames in 1 packet by using writev. 1565 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)); 1566 1567 writer_->set_blocked(true); 1568 char data[] = "ABCD"; 1569 IOVector data_iov; 1570 data_iov.AppendNoCoalesce(data, 2); 1571 data_iov.AppendNoCoalesce(data + 2, 2); 1572 connection_.SendStreamData(1, data_iov, 0, !kFin, NULL); 1573 1574 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1575 EXPECT_TRUE(connection_.HasQueuedData()); 1576 1577 // Attempt to send all packets, but since we're actually still 1578 // blocked, they should all remain queued. 1579 EXPECT_FALSE(connection_.OnCanWrite()); 1580 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1581 1582 // Unblock the writes and actually send. 1583 writer_->set_blocked(false); 1584 EXPECT_TRUE(connection_.OnCanWrite()); 1585 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1586 1587 // Parse the last packet and ensure it's one stream frame from one stream. 1588 EXPECT_EQ(1u, writer_->frame_count()); 1589 EXPECT_EQ(1u, writer_->stream_frames()->size()); 1590 EXPECT_EQ(1u, (*writer_->stream_frames())[0].stream_id); 1591 } 1592 1593 TEST_F(QuicConnectionTest, SendingZeroBytes) { 1594 // Send a zero byte write with a fin using writev. 1595 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)); 1596 IOVector empty_iov; 1597 connection_.SendStreamData(1, empty_iov, 0, kFin, NULL); 1598 1599 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1600 EXPECT_FALSE(connection_.HasQueuedData()); 1601 1602 // Parse the last packet and ensure it's one stream frame from one stream. 1603 EXPECT_EQ(1u, writer_->frame_count()); 1604 EXPECT_EQ(1u, writer_->stream_frames()->size()); 1605 EXPECT_EQ(1u, (*writer_->stream_frames())[0].stream_id); 1606 EXPECT_TRUE((*writer_->stream_frames())[0].fin); 1607 } 1608 1609 TEST_F(QuicConnectionTest, OnCanWrite) { 1610 // Visitor's OnCanWill send data, but will return false. 1611 EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll( 1612 IgnoreResult(InvokeWithoutArgs(&connection_, 1613 &TestConnection::SendStreamData3)), 1614 IgnoreResult(InvokeWithoutArgs(&connection_, 1615 &TestConnection::SendStreamData5)), 1616 Return(false))); 1617 1618 EXPECT_CALL(*send_algorithm_, 1619 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly( 1620 testing::Return(QuicTime::Delta::Zero())); 1621 1622 // Unblock the connection. 1623 connection_.OnCanWrite(); 1624 // Parse the last packet and ensure it's the two stream frames from 1625 // two different streams. 1626 EXPECT_EQ(2u, writer_->frame_count()); 1627 EXPECT_EQ(2u, writer_->stream_frames()->size()); 1628 EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id); 1629 EXPECT_EQ(kStreamId5, (*writer_->stream_frames())[1].stream_id); 1630 } 1631 1632 TEST_F(QuicConnectionTest, RetransmitOnNack) { 1633 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(2); 1634 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(1); 1635 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _)).Times(1); 1636 QuicPacketSequenceNumber last_packet; 1637 QuicByteCount second_packet_size; 1638 SendStreamDataToPeer(3, "foo", 0, !kFin, &last_packet); // Packet 1 1639 second_packet_size = 1640 SendStreamDataToPeer(3, "foos", 3, !kFin, &last_packet); // Packet 2 1641 SendStreamDataToPeer(3, "fooos", 7, !kFin, &last_packet); // Packet 3 1642 1643 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1644 1645 // Peer acks one but not two or three. Right now we only retransmit on 1646 // explicit nack, so it should not trigger a retransmission. 1647 QuicAckFrame ack_one(1, QuicTime::Zero(), 0); 1648 ack_one.received_info.entropy_hash = 1649 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 1650 ProcessAckPacket(&ack_one); 1651 ProcessAckPacket(&ack_one); 1652 ProcessAckPacket(&ack_one); 1653 1654 // Peer acks up to 3 with two explicitly missing. 1655 // Early retransmit causes 2 to be retransmitted on the first ack. 1656 QuicAckFrame nack_two(3, QuicTime::Zero(), 0); 1657 nack_two.received_info.missing_packets.insert(2); 1658 nack_two.received_info.entropy_hash = 1659 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^ 1660 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^ 1661 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 1662 // The third nack should trigger a retransmission. 1663 EXPECT_CALL(*send_algorithm_, 1664 OnPacketSent(_, _, second_packet_size - kQuicVersionSize, 1665 NACK_RETRANSMISSION, _)).Times(1); 1666 ProcessAckPacket(&nack_two); 1667 } 1668 1669 TEST_F(QuicConnectionTest, DiscardRetransmit) { 1670 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(2); 1671 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(1); 1672 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _)).Times(1); 1673 QuicPacketSequenceNumber last_packet; 1674 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1 1675 SendStreamDataToPeer(1, "foos", 3, !kFin, &last_packet); // Packet 2 1676 SendStreamDataToPeer(1, "fooos", 7, !kFin, &last_packet); // Packet 3 1677 1678 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1679 1680 // Peer acks one but not two or three. Right now we only retransmit on 1681 // explicit nack, so it should not trigger a retransmission. 1682 QuicAckFrame ack_one(1, QuicTime::Zero(), 0); 1683 ack_one.received_info.entropy_hash = 1684 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 1685 ProcessAckPacket(&ack_one); 1686 ProcessAckPacket(&ack_one); 1687 ProcessAckPacket(&ack_one); 1688 1689 // Peer acks up to 3 with two explicitly missing. Two nacks should cause no 1690 // change. 1691 QuicAckFrame nack_two(3, QuicTime::Zero(), 0); 1692 nack_two.received_info.missing_packets.insert(2); 1693 nack_two.received_info.entropy_hash = 1694 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^ 1695 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^ 1696 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 1697 // The first nack should trigger a fast retransmission, but we'll be 1698 // write blocked, so the packet will be queued. 1699 writer_->set_blocked(true); 1700 1701 ProcessAckPacket(&nack_two); 1702 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 1703 1704 // Now, ack the previous transmission. 1705 QuicAckFrame ack_all(3, QuicTime::Zero(), 0); 1706 ack_all.received_info.entropy_hash = 1707 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3); 1708 ProcessAckPacket(&ack_all); 1709 1710 // Unblock the socket and attempt to send the queued packets. However, 1711 // since the previous transmission has been acked, we will not 1712 // send the retransmission. 1713 EXPECT_CALL(*send_algorithm_, 1714 OnPacketSent(_, _, _, _, _)).Times(0); 1715 1716 writer_->set_blocked(false); 1717 connection_.OnCanWrite(); 1718 1719 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 1720 } 1721 1722 TEST_F(QuicConnectionTest, RetransmitNackedLargestObserved) { 1723 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1724 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(1); 1725 QuicPacketSequenceNumber largest_observed; 1726 QuicByteCount packet_size; 1727 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)) 1728 .WillOnce(DoAll(SaveArg<1>(&largest_observed), SaveArg<2>(&packet_size), 1729 Return(true))); 1730 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)).Times(1); 1731 connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL); 1732 QuicAckFrame frame(1, QuicTime::Zero(), largest_observed); 1733 frame.received_info.missing_packets.insert(largest_observed); 1734 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash( 1735 &connection_, largest_observed - 1); 1736 // The first nack should retransmit the largest observed packet. 1737 EXPECT_CALL(*send_algorithm_, 1738 OnPacketSent(_, _, packet_size - kQuicVersionSize, 1739 NACK_RETRANSMISSION, _)); 1740 ProcessAckPacket(&frame); 1741 } 1742 1743 TEST_F(QuicConnectionTest, QueueAfterTwoRTOs) { 1744 for (int i = 0; i < 10; ++i) { 1745 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); 1746 connection_.SendStreamDataWithString(1, "foo", i * 3, !kFin, NULL); 1747 } 1748 1749 // Block the congestion window and ensure they're queued. 1750 writer_->set_blocked(true); 1751 clock_.AdvanceTime(DefaultRetransmissionTime()); 1752 // Only one packet should be retransmitted. 1753 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 1754 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(10); 1755 connection_.GetRetransmissionAlarm()->Fire(); 1756 EXPECT_TRUE(connection_.HasQueuedData()); 1757 1758 // Unblock the congestion window. 1759 writer_->set_blocked(false); 1760 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds( 1761 2 * DefaultRetransmissionTime().ToMicroseconds())); 1762 // Retransmit already retransmitted packets event though the sequence number 1763 // greater than the largest observed. 1764 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(10); 1765 connection_.GetRetransmissionAlarm()->Fire(); 1766 connection_.OnCanWrite(); 1767 } 1768 1769 TEST_F(QuicConnectionTest, WriteBlockedThenSent) { 1770 writer_->set_blocked(true); 1771 1772 writer_->set_is_write_blocked_data_buffered(true); 1773 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 1774 EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); 1775 1776 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); 1777 connection_.OnPacketSent(WriteResult(WRITE_STATUS_OK, 0)); 1778 EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); 1779 } 1780 1781 TEST_F(QuicConnectionTest, ResumptionAlarmThenWriteBlocked) { 1782 // Set the send and resumption alarm, then block the connection. 1783 connection_.GetResumeWritesAlarm()->Set(clock_.ApproximateNow()); 1784 connection_.GetSendAlarm()->Set(clock_.ApproximateNow()); 1785 QuicConnectionPeer::SetIsWriteBlocked(&connection_, true); 1786 1787 // Fire the alarms and ensure the connection is still write blocked. 1788 connection_.GetResumeWritesAlarm()->Fire(); 1789 connection_.GetSendAlarm()->Fire(); 1790 EXPECT_TRUE(QuicConnectionPeer::IsWriteBlocked(&connection_)); 1791 } 1792 1793 TEST_F(QuicConnectionTest, LimitPacketsPerNack) { 1794 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1795 EXPECT_CALL(*send_algorithm_, OnPacketAcked(15, _, _)).Times(1); 1796 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(4); 1797 int offset = 0; 1798 // Send packets 1 to 15. 1799 for (int i = 0; i < 15; ++i) { 1800 SendStreamDataToPeer(1, "foo", offset, !kFin, NULL); 1801 offset += 3; 1802 } 1803 1804 // Ack 15, nack 1-14. 1805 QuicAckFrame nack(15, QuicTime::Zero(), 0); 1806 for (int i = 1; i < 15; ++i) { 1807 nack.received_info.missing_packets.insert(i); 1808 } 1809 1810 nack.received_info.entropy_hash = 1811 QuicConnectionPeer::GetSentEntropyHash(&connection_, 15) ^ 1812 QuicConnectionPeer::GetSentEntropyHash(&connection_, 14); 1813 1814 // 13 packets have been NACK'd 3 times, but we limit retransmissions to 2. 1815 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(2); 1816 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); 1817 ProcessAckPacket(&nack); 1818 1819 // The next call should trigger retransmitting 2 more packets. 1820 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(2); 1821 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); 1822 ProcessAckPacket(&nack); 1823 } 1824 1825 // Test sending multiple acks from the connection to the session. 1826 TEST_F(QuicConnectionTest, MultipleAcks) { 1827 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(6); 1828 QuicPacketSequenceNumber last_packet; 1829 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1 1830 EXPECT_EQ(1u, last_packet); 1831 SendStreamDataToPeer(3, "foo", 0, !kFin, &last_packet); // Packet 2 1832 EXPECT_EQ(2u, last_packet); 1833 SendAckPacketToPeer(); // Packet 3 1834 SendStreamDataToPeer(5, "foo", 0, !kFin, &last_packet); // Packet 4 1835 EXPECT_EQ(4u, last_packet); 1836 SendStreamDataToPeer(1, "foo", 3, !kFin, &last_packet); // Packet 5 1837 EXPECT_EQ(5u, last_packet); 1838 SendStreamDataToPeer(3, "foo", 3, !kFin, &last_packet); // Packet 6 1839 EXPECT_EQ(6u, last_packet); 1840 1841 // Client will ack packets 1, 2, [!3], 4, 5. 1842 QuicAckFrame frame1(5, QuicTime::Zero(), 0); 1843 frame1.received_info.missing_packets.insert(3); 1844 frame1.received_info.entropy_hash = 1845 QuicConnectionPeer::GetSentEntropyHash(&connection_, 5) ^ 1846 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^ 1847 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2); 1848 1849 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1850 1851 ProcessAckPacket(&frame1); 1852 1853 // Now the client implicitly acks 3, and explicitly acks 6. 1854 QuicAckFrame frame2(6, QuicTime::Zero(), 0); 1855 frame2.received_info.entropy_hash = 1856 QuicConnectionPeer::GetSentEntropyHash(&connection_, 6); 1857 1858 ProcessAckPacket(&frame2); 1859 } 1860 1861 TEST_F(QuicConnectionTest, DontLatchUnackedPacket) { 1862 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(1); 1863 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); // Packet 1; 1864 SendAckPacketToPeer(); // Packet 2 1865 1866 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1867 QuicAckFrame frame(1, QuicTime::Zero(), 0); 1868 frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash( 1869 &connection_, 1); 1870 ProcessAckPacket(&frame); 1871 1872 // Verify that our internal state has least-unacked as 3. 1873 EXPECT_EQ(3u, outgoing_ack()->sent_info.least_unacked); 1874 1875 // When we send an ack, we make sure our least-unacked makes sense. In this 1876 // case since we're not waiting on an ack for 2 and all packets are acked, we 1877 // set it to 3. 1878 SendAckPacketToPeer(); // Packet 3 1879 // Since this was an ack packet, we set least_unacked to 4. 1880 EXPECT_EQ(4u, outgoing_ack()->sent_info.least_unacked); 1881 // Check that the outgoing ack had its sequence number as least_unacked. 1882 EXPECT_EQ(3u, last_ack()->sent_info.least_unacked); 1883 1884 SendStreamDataToPeer(1, "bar", 3, false, NULL); // Packet 4 1885 EXPECT_EQ(4u, outgoing_ack()->sent_info.least_unacked); 1886 SendAckPacketToPeer(); // Packet 5 1887 EXPECT_EQ(4u, last_ack()->sent_info.least_unacked); 1888 } 1889 1890 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterFecPacket) { 1891 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1892 1893 // Don't send missing packet 1. 1894 ProcessFecPacket(2, 1, true, !kEntropyFlag, NULL); 1895 // Entropy flag should be false, so entropy should be 0. 1896 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2)); 1897 } 1898 1899 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacketThenFecPacket) { 1900 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1901 1902 ProcessFecProtectedPacket(1, false, kEntropyFlag); 1903 // Don't send missing packet 2. 1904 ProcessFecPacket(3, 1, true, !kEntropyFlag, NULL); 1905 // Entropy flag should be true, so entropy should not be 0. 1906 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2)); 1907 } 1908 1909 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacketsThenFecPacket) { 1910 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1911 1912 ProcessFecProtectedPacket(1, false, !kEntropyFlag); 1913 // Don't send missing packet 2. 1914 ProcessFecProtectedPacket(3, false, !kEntropyFlag); 1915 ProcessFecPacket(4, 1, true, kEntropyFlag, NULL); 1916 // Entropy flag should be true, so entropy should not be 0. 1917 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2)); 1918 } 1919 1920 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPacket) { 1921 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1922 1923 // Don't send missing packet 1. 1924 ProcessFecPacket(3, 1, false, !kEntropyFlag, NULL); 1925 // Out of order. 1926 ProcessFecProtectedPacket(2, true, !kEntropyFlag); 1927 // Entropy flag should be false, so entropy should be 0. 1928 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2)); 1929 } 1930 1931 TEST_F(QuicConnectionTest, ReviveMissingPacketAfterDataPackets) { 1932 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 1933 1934 ProcessFecProtectedPacket(1, false, !kEntropyFlag); 1935 // Don't send missing packet 2. 1936 ProcessFecPacket(6, 1, false, kEntropyFlag, NULL); 1937 ProcessFecProtectedPacket(3, false, kEntropyFlag); 1938 ProcessFecProtectedPacket(4, false, kEntropyFlag); 1939 ProcessFecProtectedPacket(5, true, !kEntropyFlag); 1940 // Entropy flag should be true, so entropy should be 0. 1941 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2)); 1942 } 1943 1944 TEST_F(QuicConnectionTest, RTO) { 1945 QuicTime default_retransmission_time = clock_.ApproximateNow().Add( 1946 DefaultRetransmissionTime()); 1947 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 1948 EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked); 1949 1950 EXPECT_EQ(1u, last_header()->packet_sequence_number); 1951 EXPECT_EQ(default_retransmission_time, 1952 connection_.GetRetransmissionAlarm()->deadline()); 1953 // Simulate the retransmission alarm firing. 1954 clock_.AdvanceTime(DefaultRetransmissionTime()); 1955 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 1956 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1u, _)); 1957 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 2u, _, _, _)); 1958 connection_.GetRetransmissionAlarm()->Fire(); 1959 EXPECT_EQ(2u, last_header()->packet_sequence_number); 1960 // We do not raise the high water mark yet. 1961 EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked); 1962 } 1963 1964 TEST_F(QuicConnectionTest, RTOWithSameEncryptionLevel) { 1965 QuicTime default_retransmission_time = clock_.ApproximateNow().Add( 1966 DefaultRetransmissionTime()); 1967 use_tagging_decrypter(); 1968 1969 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at 1970 // the end of the packet. We can test this to check which encrypter was used. 1971 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); 1972 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 1973 EXPECT_EQ(0x01010101u, final_bytes_of_last_packet()); 1974 1975 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02)); 1976 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); 1977 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 1978 EXPECT_EQ(0x02020202u, final_bytes_of_last_packet()); 1979 1980 EXPECT_EQ(default_retransmission_time, 1981 connection_.GetRetransmissionAlarm()->deadline()); 1982 { 1983 InSequence s; 1984 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)); 1985 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _)); 1986 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 1987 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 3, _, RTO_RETRANSMISSION, _)); 1988 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 4, _, RTO_RETRANSMISSION, _)); 1989 } 1990 1991 // Simulate the retransmission alarm firing. 1992 clock_.AdvanceTime(DefaultRetransmissionTime()); 1993 connection_.GetRetransmissionAlarm()->Fire(); 1994 1995 // Packet should have been sent with ENCRYPTION_NONE. 1996 EXPECT_EQ(0x01010101u, final_bytes_of_previous_packet()); 1997 1998 // Packet should have been sent with ENCRYPTION_INITIAL. 1999 EXPECT_EQ(0x02020202u, final_bytes_of_last_packet()); 2000 } 2001 2002 TEST_F(QuicConnectionTest, SendHandshakeMessages) { 2003 use_tagging_decrypter(); 2004 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at 2005 // the end of the packet. We can test this to check which encrypter was used. 2006 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); 2007 2008 // Attempt to send a handshake message while the congestion manager 2009 // does not permit sending. 2010 EXPECT_CALL(*send_algorithm_, 2011 TimeUntilSend(_, _, _, IS_HANDSHAKE)).WillRepeatedly( 2012 testing::Return(QuicTime::Delta::Infinite())); 2013 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 2014 // The packet should be serialized, but not queued. 2015 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2016 2017 // Switch to the new encrypter. 2018 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02)); 2019 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); 2020 2021 // Now become writeable and flush the packets. 2022 EXPECT_CALL(*send_algorithm_, 2023 TimeUntilSend(_, _, _, IS_HANDSHAKE)).WillRepeatedly( 2024 testing::Return(QuicTime::Delta::Zero())); 2025 EXPECT_CALL(visitor_, OnCanWrite()); 2026 connection_.OnCanWrite(); 2027 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2028 2029 // Verify that the handshake packet went out at the null encryption. 2030 EXPECT_EQ(0x01010101u, final_bytes_of_last_packet()); 2031 } 2032 2033 TEST_F(QuicConnectionTest, 2034 DropRetransmitsForNullEncryptedPacketAfterForwardSecure) { 2035 use_tagging_decrypter(); 2036 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); 2037 QuicPacketSequenceNumber sequence_number; 2038 SendStreamDataToPeer(1, "foo", 0, !kFin, &sequence_number); 2039 2040 connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, 2041 new TaggingEncrypter(0x02)); 2042 connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); 2043 2044 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 2045 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); 2046 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(sequence_number, _)).Times(1); 2047 2048 QuicTime default_retransmission_time = clock_.ApproximateNow().Add( 2049 DefaultRetransmissionTime()); 2050 2051 EXPECT_EQ(default_retransmission_time, 2052 connection_.GetRetransmissionAlarm()->deadline()); 2053 // Simulate the retransmission alarm firing. 2054 clock_.AdvanceTime(DefaultRetransmissionTime()); 2055 connection_.GetRetransmissionAlarm()->Fire(); 2056 } 2057 2058 TEST_F(QuicConnectionTest, RetransmitPacketsWithInitialEncryption) { 2059 use_tagging_decrypter(); 2060 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); 2061 connection_.SetDefaultEncryptionLevel(ENCRYPTION_NONE); 2062 2063 SendStreamDataToPeer(1, "foo", 0, !kFin, NULL); 2064 2065 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02)); 2066 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); 2067 2068 SendStreamDataToPeer(2, "bar", 0, !kFin, NULL); 2069 2070 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); 2071 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(1); 2072 2073 connection_.RetransmitUnackedPackets(INITIAL_ENCRYPTION_ONLY); 2074 } 2075 2076 TEST_F(QuicConnectionTest, BufferNonDecryptablePackets) { 2077 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2078 use_tagging_decrypter(); 2079 2080 const uint8 tag = 0x07; 2081 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); 2082 2083 // Process an encrypted packet which can not yet be decrypted 2084 // which should result in the packet being buffered. 2085 ProcessDataPacketAtLevel(1, false, kEntropyFlag, ENCRYPTION_INITIAL); 2086 2087 // Transition to the new encryption state and process another 2088 // encrypted packet which should result in the original packet being 2089 // processed. 2090 connection_.SetDecrypter(new StrictTaggingDecrypter(tag)); 2091 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); 2092 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); 2093 EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(2).WillRepeatedly( 2094 Return(true)); 2095 ProcessDataPacketAtLevel(2, false, kEntropyFlag, ENCRYPTION_INITIAL); 2096 2097 // Finally, process a third packet and note that we do not 2098 // reprocess the buffered packet. 2099 EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(true)); 2100 ProcessDataPacketAtLevel(3, false, kEntropyFlag, ENCRYPTION_INITIAL); 2101 } 2102 2103 TEST_F(QuicConnectionTest, TestRetransmitOrder) { 2104 QuicByteCount first_packet_size; 2105 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).WillOnce( 2106 DoAll(SaveArg<2>(&first_packet_size), Return(true))); 2107 2108 connection_.SendStreamDataWithString(3, "first_packet", 0, !kFin, NULL); 2109 QuicByteCount second_packet_size; 2110 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).WillOnce( 2111 DoAll(SaveArg<2>(&second_packet_size), Return(true))); 2112 connection_.SendStreamDataWithString(3, "second_packet", 12, !kFin, NULL); 2113 EXPECT_NE(first_packet_size, second_packet_size); 2114 // Advance the clock by huge time to make sure packets will be retransmitted. 2115 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); 2116 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 2117 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(2); 2118 { 2119 InSequence s; 2120 EXPECT_CALL(*send_algorithm_, 2121 OnPacketSent(_, _, first_packet_size, _, _)); 2122 EXPECT_CALL(*send_algorithm_, 2123 OnPacketSent(_, _, second_packet_size, _, _)); 2124 } 2125 connection_.GetRetransmissionAlarm()->Fire(); 2126 2127 // Advance again and expect the packets to be sent again in the same order. 2128 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(20)); 2129 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 2130 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(2); 2131 { 2132 InSequence s; 2133 EXPECT_CALL(*send_algorithm_, 2134 OnPacketSent(_, _, first_packet_size, _, _)); 2135 EXPECT_CALL(*send_algorithm_, 2136 OnPacketSent(_, _, second_packet_size, _, _)); 2137 } 2138 connection_.GetRetransmissionAlarm()->Fire(); 2139 } 2140 2141 TEST_F(QuicConnectionTest, RetransmissionCountCalculation) { 2142 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2143 QuicPacketSequenceNumber original_sequence_number; 2144 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)) 2145 .WillOnce(DoAll(SaveArg<1>(&original_sequence_number), Return(true))); 2146 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 2147 2148 EXPECT_TRUE(QuicConnectionPeer::IsSavedForRetransmission( 2149 &connection_, original_sequence_number)); 2150 EXPECT_FALSE(QuicConnectionPeer::IsRetransmission( 2151 &connection_, original_sequence_number)); 2152 // Force retransmission due to RTO. 2153 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); 2154 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 2155 EXPECT_CALL(*send_algorithm_, 2156 OnPacketAbandoned(original_sequence_number, _)).Times(1); 2157 QuicPacketSequenceNumber rto_sequence_number; 2158 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, RTO_RETRANSMISSION, _)) 2159 .WillOnce(DoAll(SaveArg<1>(&rto_sequence_number), Return(true))); 2160 connection_.GetRetransmissionAlarm()->Fire(); 2161 EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission( 2162 &connection_, original_sequence_number)); 2163 ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission( 2164 &connection_, rto_sequence_number)); 2165 EXPECT_TRUE(QuicConnectionPeer::IsRetransmission( 2166 &connection_, rto_sequence_number)); 2167 // Once by explicit nack. 2168 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(1); 2169 EXPECT_CALL(*send_algorithm_, 2170 OnPacketAbandoned(rto_sequence_number, _)).Times(1); 2171 QuicPacketSequenceNumber nack_sequence_number = 0; 2172 // Ack packets might generate some other packets, which are not 2173 // retransmissions. (More ack packets). 2174 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)) 2175 .Times(AnyNumber()); 2176 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NACK_RETRANSMISSION, _)) 2177 .WillOnce(DoAll(SaveArg<1>(&nack_sequence_number), Return(true))); 2178 QuicAckFrame ack(rto_sequence_number, QuicTime::Zero(), 0); 2179 // Ack the retransmitted packet. 2180 ack.received_info.missing_packets.insert(original_sequence_number); 2181 ack.received_info.missing_packets.insert(rto_sequence_number); 2182 ack.received_info.entropy_hash = 2183 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2184 rto_sequence_number - 1) ^ 2185 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2186 original_sequence_number); 2187 for (int i = 0; i < 3; i++) { 2188 ProcessAckPacket(&ack); 2189 } 2190 ASSERT_NE(0u, nack_sequence_number); 2191 EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission( 2192 &connection_, rto_sequence_number)); 2193 ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission( 2194 &connection_, nack_sequence_number)); 2195 EXPECT_TRUE(QuicConnectionPeer::IsRetransmission( 2196 &connection_, nack_sequence_number)); 2197 } 2198 2199 TEST_F(QuicConnectionTest, SetRTOAfterWritingToSocket) { 2200 writer_->set_blocked(true); 2201 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 2202 // Make sure that RTO is not started when the packet is queued. 2203 EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); 2204 2205 // Test that RTO is started once we write to the socket. 2206 writer_->set_blocked(false); 2207 connection_.OnCanWrite(); 2208 EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); 2209 } 2210 2211 TEST_F(QuicConnectionTest, DelayRTOWithAckReceipt) { 2212 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2213 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)) 2214 .Times(2); 2215 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 2216 connection_.SendStreamDataWithString(2, "bar", 0, !kFin, NULL); 2217 QuicAlarm* retransmission_alarm = connection_.GetRetransmissionAlarm(); 2218 EXPECT_TRUE(retransmission_alarm->IsSet()); 2219 2220 // Advance the time right before the RTO, then receive an ack for the first 2221 // packet to delay the RTO. 2222 clock_.AdvanceTime(DefaultRetransmissionTime()); 2223 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(1); 2224 QuicAckFrame ack(1, QuicTime::Zero(), 0); 2225 ack.received_info.entropy_hash = 2226 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 2227 ProcessAckPacket(&ack); 2228 EXPECT_TRUE(retransmission_alarm->IsSet()); 2229 2230 // Move forward past the original RTO and ensure the RTO is still pending. 2231 clock_.AdvanceTime(DefaultRetransmissionTime()); 2232 2233 // Ensure the second packet gets retransmitted when it finally fires. 2234 EXPECT_TRUE(retransmission_alarm->IsSet()); 2235 EXPECT_GE(retransmission_alarm->deadline(), clock_.ApproximateNow()); 2236 clock_.AdvanceTime(DefaultRetransmissionTime()); 2237 EXPECT_LT(retransmission_alarm->deadline(), clock_.ApproximateNow()); 2238 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 2239 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, RTO_RETRANSMISSION, _)); 2240 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)); 2241 // Manually cancel the alarm to simulate a real test. 2242 connection_.GetRetransmissionAlarm()->Fire(); 2243 2244 // The new retransmitted sequence number should set the RTO to a larger value 2245 // than previously. 2246 EXPECT_TRUE(retransmission_alarm->IsSet()); 2247 QuicTime next_rto_time = retransmission_alarm->deadline(); 2248 QuicTime::Delta expected_rto = 2249 connection_.sent_packet_manager().GetRetransmissionDelay(); 2250 EXPECT_EQ(next_rto_time, clock_.ApproximateNow().Add(expected_rto)); 2251 } 2252 2253 TEST_F(QuicConnectionTest, TestQueued) { 2254 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2255 writer_->set_blocked(true); 2256 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 2257 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2258 2259 // Attempt to send all packets, but since we're actually still 2260 // blocked, they should all remain queued. 2261 EXPECT_FALSE(connection_.OnCanWrite()); 2262 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2263 2264 // Unblock the writes and actually send. 2265 writer_->set_blocked(false); 2266 EXPECT_TRUE(connection_.OnCanWrite()); 2267 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2268 } 2269 2270 TEST_F(QuicConnectionTest, CloseFecGroup) { 2271 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2272 // Don't send missing packet 1. 2273 // Don't send missing packet 2. 2274 ProcessFecProtectedPacket(3, false, !kEntropyFlag); 2275 // Don't send missing FEC packet 3. 2276 ASSERT_EQ(1u, connection_.NumFecGroups()); 2277 2278 // Now send non-fec protected ack packet and close the group. 2279 QuicAckFrame frame(0, QuicTime::Zero(), 5); 2280 creator_.set_sequence_number(4); 2281 ProcessAckPacket(&frame); 2282 ASSERT_EQ(0u, connection_.NumFecGroups()); 2283 } 2284 2285 TEST_F(QuicConnectionTest, NoQuicCongestionFeedbackFrame) { 2286 SendAckPacketToPeer(); 2287 EXPECT_TRUE(last_feedback() == NULL); 2288 } 2289 2290 TEST_F(QuicConnectionTest, WithQuicCongestionFeedbackFrame) { 2291 QuicCongestionFeedbackFrame info; 2292 info.type = kFixRate; 2293 info.fix_rate.bitrate = QuicBandwidth::FromBytesPerSecond(123); 2294 SetFeedback(&info); 2295 2296 SendAckPacketToPeer(); 2297 EXPECT_EQ(kFixRate, last_feedback()->type); 2298 EXPECT_EQ(info.fix_rate.bitrate, last_feedback()->fix_rate.bitrate); 2299 } 2300 2301 TEST_F(QuicConnectionTest, UpdateQuicCongestionFeedbackFrame) { 2302 SendAckPacketToPeer(); 2303 EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _, _)); 2304 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2305 ProcessPacket(1); 2306 } 2307 2308 TEST_F(QuicConnectionTest, DontUpdateQuicCongestionFeedbackFrameForRevived) { 2309 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2310 SendAckPacketToPeer(); 2311 // Process an FEC packet, and revive the missing data packet 2312 // but only contact the receive_algorithm once. 2313 EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _, _)); 2314 ProcessFecPacket(2, 1, true, !kEntropyFlag, NULL); 2315 } 2316 2317 TEST_F(QuicConnectionTest, InitialTimeout) { 2318 EXPECT_TRUE(connection_.connected()); 2319 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT, false)); 2320 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); 2321 2322 QuicTime default_timeout = clock_.ApproximateNow().Add( 2323 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)); 2324 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); 2325 2326 // Simulate the timeout alarm firing. 2327 clock_.AdvanceTime( 2328 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)); 2329 connection_.GetTimeoutAlarm()->Fire(); 2330 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); 2331 EXPECT_FALSE(connection_.connected()); 2332 2333 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); 2334 EXPECT_FALSE(connection_.GetResumeWritesAlarm()->IsSet()); 2335 EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); 2336 EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); 2337 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); 2338 } 2339 2340 TEST_F(QuicConnectionTest, TimeoutAfterSend) { 2341 EXPECT_TRUE(connection_.connected()); 2342 2343 QuicTime default_timeout = clock_.ApproximateNow().Add( 2344 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)); 2345 2346 // When we send a packet, the timeout will change to 5000 + 2347 // kDefaultInitialTimeoutSecs. 2348 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); 2349 2350 // Send an ack so we don't set the retransmission alarm. 2351 SendAckPacketToPeer(); 2352 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); 2353 2354 // The original alarm will fire. We should not time out because we had a 2355 // network event at t=5000. The alarm will reregister. 2356 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds( 2357 kDefaultInitialTimeoutSecs * 1000000 - 5000)); 2358 EXPECT_EQ(default_timeout, clock_.ApproximateNow()); 2359 connection_.GetTimeoutAlarm()->Fire(); 2360 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); 2361 EXPECT_TRUE(connection_.connected()); 2362 EXPECT_EQ(default_timeout.Add(QuicTime::Delta::FromMilliseconds(5)), 2363 connection_.GetTimeoutAlarm()->deadline()); 2364 2365 // This time, we should time out. 2366 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT, false)); 2367 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); 2368 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); 2369 EXPECT_EQ(default_timeout.Add(QuicTime::Delta::FromMilliseconds(5)), 2370 clock_.ApproximateNow()); 2371 connection_.GetTimeoutAlarm()->Fire(); 2372 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); 2373 EXPECT_FALSE(connection_.connected()); 2374 } 2375 2376 // TODO(ianswett): Add scheduler tests when should_retransmit is false. 2377 TEST_F(QuicConnectionTest, SendScheduler) { 2378 // Test that if we send a packet without delay, it is not queued. 2379 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2380 EXPECT_CALL(*send_algorithm_, 2381 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 2382 testing::Return(QuicTime::Delta::Zero())); 2383 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); 2384 connection_.SendPacket( 2385 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2386 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2387 } 2388 2389 TEST_F(QuicConnectionTest, SendSchedulerDelay) { 2390 // Test that if we send a packet with a delay, it ends up queued. 2391 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2392 EXPECT_CALL(*send_algorithm_, 2393 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 2394 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 2395 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 1, _, _, _)).Times(0); 2396 connection_.SendPacket( 2397 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2398 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2399 } 2400 2401 TEST_F(QuicConnectionTest, SendSchedulerForce) { 2402 // Test that if we force send a packet, it is not queued. 2403 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2404 EXPECT_CALL(*send_algorithm_, 2405 TimeUntilSend(_, NACK_RETRANSMISSION, _, _)).Times(0); 2406 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); 2407 connection_.SendPacket( 2408 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2409 // XXX: fixme. was: connection_.SendPacket(1, packet, kForce); 2410 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2411 } 2412 2413 TEST_F(QuicConnectionTest, SendSchedulerEAGAIN) { 2414 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2415 writer_->set_blocked(true); 2416 EXPECT_CALL(*send_algorithm_, 2417 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 2418 testing::Return(QuicTime::Delta::Zero())); 2419 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 1, _, _, _)).Times(0); 2420 connection_.SendPacket( 2421 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2422 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2423 } 2424 2425 TEST_F(QuicConnectionTest, SendSchedulerDelayThenSend) { 2426 // Test that if we send a packet with a delay, it ends up queued. 2427 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2428 EXPECT_CALL(*send_algorithm_, 2429 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 2430 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 2431 connection_.SendPacket( 2432 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2433 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2434 2435 // Advance the clock to fire the alarm, and configure the scheduler 2436 // to permit the packet to be sent. 2437 EXPECT_CALL(*send_algorithm_, 2438 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly( 2439 testing::Return(QuicTime::Delta::Zero())); 2440 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1)); 2441 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); 2442 connection_.GetSendAlarm()->Fire(); 2443 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2444 } 2445 2446 TEST_F(QuicConnectionTest, SendSchedulerDelayThenRetransmit) { 2447 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, NOT_RETRANSMISSION, _, _)) 2448 .WillRepeatedly(testing::Return(QuicTime::Delta::Zero())); 2449 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)).Times(1); 2450 EXPECT_CALL(*send_algorithm_, 2451 OnPacketSent(_, 1, _, NOT_RETRANSMISSION, _)); 2452 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 2453 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2454 // Advance the time for retransmission of lost packet. 2455 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(501)); 2456 // Test that if we send a retransmit with a delay, it ends up queued in the 2457 // sent packet manager, but not yet serialized. 2458 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 2459 EXPECT_CALL(*send_algorithm_, 2460 TimeUntilSend(_, RTO_RETRANSMISSION, _, _)).WillOnce( 2461 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 2462 connection_.GetRetransmissionAlarm()->Fire(); 2463 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2464 2465 // Advance the clock to fire the alarm, and configure the scheduler 2466 // to permit the packet to be sent. 2467 EXPECT_CALL(*send_algorithm_, 2468 TimeUntilSend(_, RTO_RETRANSMISSION, _, _)).Times(2). 2469 WillRepeatedly(testing::Return(QuicTime::Delta::Zero())); 2470 2471 // Ensure the scheduler is notified this is a retransmit. 2472 EXPECT_CALL(*send_algorithm_, 2473 OnPacketSent(_, _, _, RTO_RETRANSMISSION, _)); 2474 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1)); 2475 connection_.GetSendAlarm()->Fire(); 2476 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2477 } 2478 2479 TEST_F(QuicConnectionTest, SendSchedulerDelayAndQueue) { 2480 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2481 EXPECT_CALL(*send_algorithm_, 2482 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 2483 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 2484 connection_.SendPacket( 2485 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2486 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2487 2488 // Attempt to send another packet and make sure that it gets queued. 2489 packet = ConstructDataPacket(2, 0, !kEntropyFlag); 2490 connection_.SendPacket( 2491 ENCRYPTION_NONE, 2, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2492 EXPECT_EQ(2u, connection_.NumQueuedPackets()); 2493 } 2494 2495 TEST_F(QuicConnectionTest, SendSchedulerDelayThenAckAndSend) { 2496 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2497 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2498 EXPECT_CALL(*send_algorithm_, 2499 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 2500 testing::Return(QuicTime::Delta::FromMicroseconds(10))); 2501 connection_.SendPacket( 2502 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2503 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2504 2505 // Now send non-retransmitting information, that we're not going to 2506 // retransmit 3. The far end should stop waiting for it. 2507 QuicAckFrame frame(0, QuicTime::Zero(), 1); 2508 EXPECT_CALL(*send_algorithm_, 2509 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly( 2510 testing::Return(QuicTime::Delta::Zero())); 2511 EXPECT_CALL(*send_algorithm_, 2512 OnPacketSent(_, _, _, _, _)); 2513 ProcessAckPacket(&frame); 2514 2515 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2516 // Ensure alarm is not set 2517 EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); 2518 } 2519 2520 TEST_F(QuicConnectionTest, SendSchedulerDelayThenAckAndHold) { 2521 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2522 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2523 EXPECT_CALL(*send_algorithm_, 2524 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 2525 testing::Return(QuicTime::Delta::FromMicroseconds(10))); 2526 connection_.SendPacket( 2527 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2528 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2529 2530 // Now send non-retransmitting information, that we're not going to 2531 // retransmit 3. The far end should stop waiting for it. 2532 QuicAckFrame frame(0, QuicTime::Zero(), 1); 2533 EXPECT_CALL(*send_algorithm_, 2534 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 2535 testing::Return(QuicTime::Delta::FromMicroseconds(1))); 2536 ProcessAckPacket(&frame); 2537 2538 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2539 } 2540 2541 TEST_F(QuicConnectionTest, SendSchedulerDelayThenOnCanWrite) { 2542 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2543 EXPECT_CALL(*send_algorithm_, 2544 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 2545 testing::Return(QuicTime::Delta::FromMicroseconds(10))); 2546 connection_.SendPacket( 2547 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2548 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2549 2550 // OnCanWrite should not send the packet (because of the delay) 2551 // but should still return true. 2552 EXPECT_TRUE(connection_.OnCanWrite()); 2553 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 2554 } 2555 2556 TEST_F(QuicConnectionTest, TestQueueLimitsOnSendStreamData) { 2557 // All packets carry version info till version is negotiated. 2558 size_t payload_length; 2559 connection_.options()->max_packet_length = 2560 GetPacketLengthForOneStream( 2561 connection_.version(), kIncludeVersion, PACKET_1BYTE_SEQUENCE_NUMBER, 2562 NOT_IN_FEC_GROUP, &payload_length); 2563 2564 // Queue the first packet. 2565 EXPECT_CALL(*send_algorithm_, 2566 TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce( 2567 testing::Return(QuicTime::Delta::FromMicroseconds(10))); 2568 const string payload(payload_length, 'a'); 2569 EXPECT_EQ(0u, 2570 connection_.SendStreamDataWithString(3, payload, 0, 2571 !kFin, NULL).bytes_consumed); 2572 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 2573 } 2574 2575 TEST_F(QuicConnectionTest, LoopThroughSendingPackets) { 2576 // All packets carry version info till version is negotiated. 2577 size_t payload_length; 2578 connection_.options()->max_packet_length = 2579 GetPacketLengthForOneStream( 2580 connection_.version(), kIncludeVersion, PACKET_1BYTE_SEQUENCE_NUMBER, 2581 NOT_IN_FEC_GROUP, &payload_length); 2582 2583 // Queue the first packet. 2584 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(7); 2585 // The first stream frame will consume 2 fewer bytes than the other six. 2586 const string payload(payload_length * 7 - 12, 'a'); 2587 EXPECT_EQ(payload.size(), 2588 connection_.SendStreamDataWithString(1, payload, 0, 2589 !kFin, NULL).bytes_consumed); 2590 } 2591 2592 TEST_F(QuicConnectionTest, SendDelayedAckOnTimer) { 2593 QuicTime ack_time = clock_.ApproximateNow().Add(DefaultDelayedAckTime()); 2594 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2595 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); 2596 ProcessPacket(1); 2597 // Check if delayed ack timer is running for the expected interval. 2598 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); 2599 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); 2600 // Simulate delayed ack alarm firing. 2601 connection_.GetAckAlarm()->Fire(); 2602 // Check that ack is sent and that delayed ack alarm is reset. 2603 EXPECT_EQ(1u, writer_->frame_count()); 2604 EXPECT_TRUE(writer_->ack()); 2605 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); 2606 } 2607 2608 TEST_F(QuicConnectionTest, SendDelayedAckOnSecondPacket) { 2609 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2610 ProcessPacket(1); 2611 ProcessPacket(2); 2612 // Check that ack is sent and that delayed ack alarm is reset. 2613 EXPECT_EQ(1u, writer_->frame_count()); 2614 EXPECT_TRUE(writer_->ack()); 2615 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); 2616 } 2617 2618 TEST_F(QuicConnectionTest, NoAckOnOldNacks) { 2619 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2620 // Drop one packet, triggering a sequence of acks. 2621 ProcessPacket(2); 2622 EXPECT_EQ(1u, writer_->frame_count()); 2623 EXPECT_TRUE(writer_->ack()); 2624 writer_->Reset(); 2625 ProcessPacket(3); 2626 EXPECT_EQ(1u, writer_->frame_count()); 2627 EXPECT_TRUE(writer_->ack()); 2628 writer_->Reset(); 2629 ProcessPacket(4); 2630 EXPECT_EQ(1u, writer_->frame_count()); 2631 EXPECT_TRUE(writer_->ack()); 2632 writer_->Reset(); 2633 ProcessPacket(5); 2634 EXPECT_EQ(1u, writer_->frame_count()); 2635 EXPECT_TRUE(writer_->ack()); 2636 // Now only set the timer on the 6th packet, instead of sending another ack. 2637 writer_->Reset(); 2638 ProcessPacket(6); 2639 EXPECT_EQ(0u, writer_->frame_count()); 2640 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); 2641 } 2642 2643 TEST_F(QuicConnectionTest, SendDelayedAckOnOutgoingPacket) { 2644 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2645 ProcessPacket(1); 2646 connection_.SendStreamDataWithString(kStreamId3, "foo", 0, !kFin, NULL); 2647 // Check that ack is bundled with outgoing data and that delayed ack 2648 // alarm is reset. 2649 EXPECT_EQ(2u, writer_->frame_count()); 2650 EXPECT_TRUE(writer_->ack()); 2651 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); 2652 } 2653 2654 TEST_F(QuicConnectionTest, DontSendDelayedAckOnOutgoingCryptoPacket) { 2655 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2656 ProcessPacket(1); 2657 connection_.SendStreamDataWithString(kCryptoStreamId, "foo", 0, !kFin, NULL); 2658 // Check that ack is not bundled with outgoing data. 2659 EXPECT_EQ(1u, writer_->frame_count()); 2660 EXPECT_FALSE(writer_->ack()); 2661 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); 2662 } 2663 2664 TEST_F(QuicConnectionTest, NoAckForClose) { 2665 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2666 ProcessPacket(1); 2667 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(0); 2668 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, true)); 2669 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); 2670 ProcessClosePacket(2, 0); 2671 } 2672 2673 TEST_F(QuicConnectionTest, SendWhenDisconnected) { 2674 EXPECT_TRUE(connection_.connected()); 2675 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, false)); 2676 connection_.CloseConnection(QUIC_PEER_GOING_AWAY, false); 2677 EXPECT_FALSE(connection_.connected()); 2678 QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag); 2679 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 1, _, _, _)).Times(0); 2680 connection_.SendPacket( 2681 ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA); 2682 } 2683 2684 TEST_F(QuicConnectionTest, PublicReset) { 2685 QuicPublicResetPacket header; 2686 header.public_header.guid = guid_; 2687 header.public_header.reset_flag = true; 2688 header.public_header.version_flag = false; 2689 header.rejected_sequence_number = 10101; 2690 scoped_ptr<QuicEncryptedPacket> packet( 2691 framer_.BuildPublicResetPacket(header)); 2692 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PUBLIC_RESET, true)); 2693 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *packet); 2694 } 2695 2696 TEST_F(QuicConnectionTest, GoAway) { 2697 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2698 2699 QuicGoAwayFrame goaway; 2700 goaway.last_good_stream_id = 1; 2701 goaway.error_code = QUIC_PEER_GOING_AWAY; 2702 goaway.reason_phrase = "Going away."; 2703 EXPECT_CALL(visitor_, OnGoAway(_)); 2704 ProcessGoAwayPacket(&goaway); 2705 } 2706 2707 TEST_F(QuicConnectionTest, InvalidPacket) { 2708 EXPECT_CALL(visitor_, 2709 OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false)); 2710 QuicEncryptedPacket encrypted(NULL, 0); 2711 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), encrypted); 2712 // The connection close packet should have error details. 2713 ASSERT_TRUE(last_close() != NULL); 2714 EXPECT_EQ("Unable to read public flags.", last_close()->error_details); 2715 } 2716 2717 TEST_F(QuicConnectionTest, MissingPacketsBeforeLeastUnacked) { 2718 QuicAckFrame ack(0, QuicTime::Zero(), 4); 2719 // Set the sequence number of the ack packet to be least unacked (4). 2720 creator_.set_sequence_number(3); 2721 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2722 ProcessAckPacket(&ack); 2723 EXPECT_TRUE(outgoing_ack()->received_info.missing_packets.empty()); 2724 } 2725 2726 TEST_F(QuicConnectionTest, ReceivedEntropyHashCalculation) { 2727 EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true)); 2728 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2729 ProcessDataPacket(1, 1, kEntropyFlag); 2730 ProcessDataPacket(4, 1, kEntropyFlag); 2731 ProcessDataPacket(3, 1, !kEntropyFlag); 2732 ProcessDataPacket(7, 1, kEntropyFlag); 2733 EXPECT_EQ(146u, outgoing_ack()->received_info.entropy_hash); 2734 } 2735 2736 TEST_F(QuicConnectionTest, UpdateEntropyForReceivedPackets) { 2737 EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true)); 2738 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2739 ProcessDataPacket(1, 1, kEntropyFlag); 2740 ProcessDataPacket(5, 1, kEntropyFlag); 2741 ProcessDataPacket(4, 1, !kEntropyFlag); 2742 EXPECT_EQ(34u, outgoing_ack()->received_info.entropy_hash); 2743 // Make 4th packet my least unacked, and update entropy for 2, 3 packets. 2744 QuicAckFrame ack(0, QuicTime::Zero(), 4); 2745 QuicPacketEntropyHash kRandomEntropyHash = 129u; 2746 ack.sent_info.entropy_hash = kRandomEntropyHash; 2747 creator_.set_sequence_number(5); 2748 QuicPacketEntropyHash six_packet_entropy_hash = 0; 2749 if (ProcessAckPacket(&ack)) { 2750 six_packet_entropy_hash = 1 << 6; 2751 } 2752 2753 EXPECT_EQ((kRandomEntropyHash + (1 << 5) + six_packet_entropy_hash), 2754 outgoing_ack()->received_info.entropy_hash); 2755 } 2756 2757 TEST_F(QuicConnectionTest, UpdateEntropyHashUptoCurrentPacket) { 2758 EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true)); 2759 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2760 ProcessDataPacket(1, 1, kEntropyFlag); 2761 ProcessDataPacket(5, 1, !kEntropyFlag); 2762 ProcessDataPacket(22, 1, kEntropyFlag); 2763 EXPECT_EQ(66u, outgoing_ack()->received_info.entropy_hash); 2764 creator_.set_sequence_number(22); 2765 QuicPacketEntropyHash kRandomEntropyHash = 85u; 2766 // Current packet is the least unacked packet. 2767 QuicAckFrame ack(0, QuicTime::Zero(), 23); 2768 ack.sent_info.entropy_hash = kRandomEntropyHash; 2769 QuicPacketEntropyHash ack_entropy_hash = ProcessAckPacket(&ack); 2770 EXPECT_EQ((kRandomEntropyHash + ack_entropy_hash), 2771 outgoing_ack()->received_info.entropy_hash); 2772 ProcessDataPacket(25, 1, kEntropyFlag); 2773 EXPECT_EQ((kRandomEntropyHash + ack_entropy_hash + (1 << (25 % 8))), 2774 outgoing_ack()->received_info.entropy_hash); 2775 } 2776 2777 TEST_F(QuicConnectionTest, EntropyCalculationForTruncatedAck) { 2778 EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true)); 2779 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2780 QuicPacketEntropyHash entropy[51]; 2781 entropy[0] = 0; 2782 for (int i = 1; i < 51; ++i) { 2783 bool should_send = i % 10 != 0; 2784 bool entropy_flag = (i & (i - 1)) != 0; 2785 if (!should_send) { 2786 entropy[i] = entropy[i - 1]; 2787 continue; 2788 } 2789 if (entropy_flag) { 2790 entropy[i] = entropy[i - 1] ^ (1 << (i % 8)); 2791 } else { 2792 entropy[i] = entropy[i - 1]; 2793 } 2794 ProcessDataPacket(i, 1, entropy_flag); 2795 } 2796 // Till 50 since 50th packet is not sent. 2797 for (int i = 1; i < 50; ++i) { 2798 EXPECT_EQ(entropy[i], QuicConnectionPeer::ReceivedEntropyHash( 2799 &connection_, i)); 2800 } 2801 } 2802 2803 TEST_F(QuicConnectionTest, CheckSentEntropyHash) { 2804 creator_.set_sequence_number(1); 2805 SequenceNumberSet missing_packets; 2806 QuicPacketEntropyHash entropy_hash = 0; 2807 QuicPacketSequenceNumber max_sequence_number = 51; 2808 for (QuicPacketSequenceNumber i = 1; i <= max_sequence_number; ++i) { 2809 bool is_missing = i % 10 != 0; 2810 bool entropy_flag = (i & (i - 1)) != 0; 2811 QuicPacketEntropyHash packet_entropy_hash = 0; 2812 if (entropy_flag) { 2813 packet_entropy_hash = 1 << (i % 8); 2814 } 2815 QuicPacket* packet = ConstructDataPacket(i, 0, entropy_flag); 2816 connection_.SendPacket( 2817 ENCRYPTION_NONE, i, packet, packet_entropy_hash, 2818 HAS_RETRANSMITTABLE_DATA); 2819 2820 if (is_missing) { 2821 missing_packets.insert(i); 2822 continue; 2823 } 2824 2825 entropy_hash ^= packet_entropy_hash; 2826 } 2827 EXPECT_TRUE(QuicConnectionPeer::IsValidEntropy( 2828 &connection_, max_sequence_number, missing_packets, entropy_hash)) 2829 << ""; 2830 } 2831 2832 TEST_F(QuicConnectionTest, ServerSendsVersionNegotiationPacket) { 2833 framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED); 2834 2835 QuicPacketHeader header; 2836 header.public_header.guid = guid_; 2837 header.public_header.reset_flag = false; 2838 header.public_header.version_flag = true; 2839 header.entropy_flag = false; 2840 header.fec_flag = false; 2841 header.packet_sequence_number = 12; 2842 header.fec_group = 0; 2843 2844 QuicFrames frames; 2845 QuicFrame frame(&frame1_); 2846 frames.push_back(frame); 2847 scoped_ptr<QuicPacket> packet( 2848 framer_.BuildUnsizedDataPacket(header, frames).packet); 2849 scoped_ptr<QuicEncryptedPacket> encrypted( 2850 framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet)); 2851 2852 framer_.set_version(QuicVersionMax()); 2853 connection_.set_is_server(true); 2854 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 2855 EXPECT_TRUE(writer_->version_negotiation_packet() != NULL); 2856 2857 size_t num_versions = arraysize(kSupportedQuicVersions); 2858 EXPECT_EQ(num_versions, 2859 writer_->version_negotiation_packet()->versions.size()); 2860 2861 // We expect all versions in kSupportedQuicVersions to be 2862 // included in the packet. 2863 for (size_t i = 0; i < num_versions; ++i) { 2864 EXPECT_EQ(kSupportedQuicVersions[i], 2865 writer_->version_negotiation_packet()->versions[i]); 2866 } 2867 } 2868 2869 TEST_F(QuicConnectionTest, ServerSendsVersionNegotiationPacketSocketBlocked) { 2870 framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED); 2871 2872 QuicPacketHeader header; 2873 header.public_header.guid = guid_; 2874 header.public_header.reset_flag = false; 2875 header.public_header.version_flag = true; 2876 header.entropy_flag = false; 2877 header.fec_flag = false; 2878 header.packet_sequence_number = 12; 2879 header.fec_group = 0; 2880 2881 QuicFrames frames; 2882 QuicFrame frame(&frame1_); 2883 frames.push_back(frame); 2884 scoped_ptr<QuicPacket> packet( 2885 framer_.BuildUnsizedDataPacket(header, frames).packet); 2886 scoped_ptr<QuicEncryptedPacket> encrypted( 2887 framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet)); 2888 2889 framer_.set_version(QuicVersionMax()); 2890 connection_.set_is_server(true); 2891 writer_->set_blocked(true); 2892 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 2893 EXPECT_EQ(0u, writer_->last_packet_size()); 2894 EXPECT_TRUE(connection_.HasQueuedData()); 2895 EXPECT_TRUE(QuicConnectionPeer::IsWriteBlocked(&connection_)); 2896 2897 writer_->set_blocked(false); 2898 connection_.OnCanWrite(); 2899 EXPECT_TRUE(writer_->version_negotiation_packet() != NULL); 2900 2901 size_t num_versions = arraysize(kSupportedQuicVersions); 2902 EXPECT_EQ(num_versions, 2903 writer_->version_negotiation_packet()->versions.size()); 2904 2905 // We expect all versions in kSupportedQuicVersions to be 2906 // included in the packet. 2907 for (size_t i = 0; i < num_versions; ++i) { 2908 EXPECT_EQ(kSupportedQuicVersions[i], 2909 writer_->version_negotiation_packet()->versions[i]); 2910 } 2911 } 2912 2913 TEST_F(QuicConnectionTest, 2914 ServerSendsVersionNegotiationPacketSocketBlockedDataBuffered) { 2915 framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED); 2916 2917 QuicPacketHeader header; 2918 header.public_header.guid = guid_; 2919 header.public_header.reset_flag = false; 2920 header.public_header.version_flag = true; 2921 header.entropy_flag = false; 2922 header.fec_flag = false; 2923 header.packet_sequence_number = 12; 2924 header.fec_group = 0; 2925 2926 QuicFrames frames; 2927 QuicFrame frame(&frame1_); 2928 frames.push_back(frame); 2929 scoped_ptr<QuicPacket> packet( 2930 framer_.BuildUnsizedDataPacket(header, frames).packet); 2931 scoped_ptr<QuicEncryptedPacket> encrypted( 2932 framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet)); 2933 2934 framer_.set_version(QuicVersionMax()); 2935 connection_.set_is_server(true); 2936 writer_->set_blocked(true); 2937 writer_->set_is_write_blocked_data_buffered(true); 2938 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 2939 EXPECT_EQ(0u, writer_->last_packet_size()); 2940 EXPECT_FALSE(connection_.HasQueuedData()); 2941 EXPECT_TRUE(QuicConnectionPeer::IsWriteBlocked(&connection_)); 2942 } 2943 2944 TEST_F(QuicConnectionTest, ClientHandlesVersionNegotiation) { 2945 // Start out with some unsupported version. 2946 QuicConnectionPeer::GetFramer(&connection_)->set_version_for_tests( 2947 QUIC_VERSION_UNSUPPORTED); 2948 2949 QuicPacketHeader header; 2950 header.public_header.guid = guid_; 2951 header.public_header.reset_flag = false; 2952 header.public_header.version_flag = true; 2953 header.entropy_flag = false; 2954 header.fec_flag = false; 2955 header.packet_sequence_number = 12; 2956 header.fec_group = 0; 2957 2958 QuicVersionVector supported_versions; 2959 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { 2960 supported_versions.push_back(kSupportedQuicVersions[i]); 2961 } 2962 2963 // Send a version negotiation packet. 2964 scoped_ptr<QuicEncryptedPacket> encrypted( 2965 framer_.BuildVersionNegotiationPacket( 2966 header.public_header, supported_versions)); 2967 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 2968 2969 // Now force another packet. The connection should transition into 2970 // NEGOTIATED_VERSION state and tell the packet creator to StopSendingVersion. 2971 header.public_header.version_flag = false; 2972 QuicFrames frames; 2973 QuicFrame frame(&frame1_); 2974 frames.push_back(frame); 2975 scoped_ptr<QuicPacket> packet( 2976 framer_.BuildUnsizedDataPacket(header, frames).packet); 2977 encrypted.reset(framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet)); 2978 EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(1); 2979 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 2980 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 2981 2982 ASSERT_FALSE(QuicPacketCreatorPeer::SendVersionInPacket( 2983 QuicConnectionPeer::GetPacketCreator(&connection_))); 2984 } 2985 2986 TEST_F(QuicConnectionTest, BadVersionNegotiation) { 2987 QuicPacketHeader header; 2988 header.public_header.guid = guid_; 2989 header.public_header.reset_flag = false; 2990 header.public_header.version_flag = true; 2991 header.entropy_flag = false; 2992 header.fec_flag = false; 2993 header.packet_sequence_number = 12; 2994 header.fec_group = 0; 2995 2996 QuicVersionVector supported_versions; 2997 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { 2998 supported_versions.push_back(kSupportedQuicVersions[i]); 2999 } 3000 3001 // Send a version negotiation packet with the version the client started with. 3002 // It should be rejected. 3003 EXPECT_CALL(visitor_, 3004 OnConnectionClosed(QUIC_INVALID_VERSION_NEGOTIATION_PACKET, 3005 false)); 3006 scoped_ptr<QuicEncryptedPacket> encrypted( 3007 framer_.BuildVersionNegotiationPacket( 3008 header.public_header, supported_versions)); 3009 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 3010 } 3011 3012 TEST_F(QuicConnectionTest, CheckSendStats) { 3013 EXPECT_CALL(*send_algorithm_, 3014 OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)); 3015 connection_.SendStreamDataWithString(3, "first", 0, !kFin, NULL); 3016 size_t first_packet_size = last_sent_packet_size(); 3017 3018 EXPECT_CALL(*send_algorithm_, 3019 OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)); 3020 connection_.SendStreamDataWithString(5, "second", 0, !kFin, NULL); 3021 size_t second_packet_size = last_sent_packet_size(); 3022 3023 // 2 retransmissions due to rto, 1 due to explicit nack. 3024 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout()); 3025 EXPECT_CALL(*send_algorithm_, 3026 OnPacketSent(_, _, _, RTO_RETRANSMISSION, _)).Times(2); 3027 EXPECT_CALL(*send_algorithm_, 3028 OnPacketSent(_, _, _, NACK_RETRANSMISSION, _)); 3029 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(3); 3030 EXPECT_CALL(visitor_, OnCanWrite()).WillRepeatedly(Return(true)); 3031 3032 // Retransmit due to RTO. 3033 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); 3034 connection_.GetRetransmissionAlarm()->Fire(); 3035 3036 // Retransmit due to explicit nacks. 3037 QuicAckFrame nack_three(4, QuicTime::Zero(), 0); 3038 nack_three.received_info.missing_packets.insert(3); 3039 nack_three.received_info.missing_packets.insert(1); 3040 nack_three.received_info.entropy_hash = 3041 QuicConnectionPeer::GetSentEntropyHash(&connection_, 4) ^ 3042 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^ 3043 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^ 3044 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 3045 QuicFrame frame(&nack_three); 3046 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(1); 3047 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(1); 3048 EXPECT_CALL(visitor_, OnCanWrite()).Times(4).WillRepeatedly(Return(true)); 3049 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 3050 3051 ProcessFramePacket(frame); 3052 ProcessFramePacket(frame); 3053 ProcessFramePacket(frame); 3054 3055 EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillOnce( 3056 Return(QuicTime::Delta::Zero())); 3057 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce( 3058 Return(QuicBandwidth::Zero())); 3059 3060 const QuicConnectionStats& stats = connection_.GetStats(); 3061 EXPECT_EQ(3 * first_packet_size + 2 * second_packet_size - kQuicVersionSize, 3062 stats.bytes_sent); 3063 EXPECT_EQ(5u, stats.packets_sent); 3064 EXPECT_EQ(2 * first_packet_size + second_packet_size - kQuicVersionSize, 3065 stats.bytes_retransmitted); 3066 EXPECT_EQ(3u, stats.packets_retransmitted); 3067 EXPECT_EQ(1u, stats.rto_count); 3068 } 3069 3070 TEST_F(QuicConnectionTest, CheckReceiveStats) { 3071 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 3072 3073 size_t received_bytes = 0; 3074 received_bytes += ProcessFecProtectedPacket(1, false, !kEntropyFlag); 3075 received_bytes += ProcessFecProtectedPacket(3, false, !kEntropyFlag); 3076 // Should be counted against dropped packets. 3077 received_bytes += ProcessDataPacket(3, 1, !kEntropyFlag); 3078 received_bytes += ProcessFecPacket(4, 1, true, !kEntropyFlag, NULL); 3079 3080 EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillOnce( 3081 Return(QuicTime::Delta::Zero())); 3082 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce( 3083 Return(QuicBandwidth::Zero())); 3084 3085 const QuicConnectionStats& stats = connection_.GetStats(); 3086 EXPECT_EQ(received_bytes, stats.bytes_received); 3087 EXPECT_EQ(4u, stats.packets_received); 3088 3089 EXPECT_EQ(1u, stats.packets_revived); 3090 EXPECT_EQ(1u, stats.packets_dropped); 3091 } 3092 3093 TEST_F(QuicConnectionTest, TestFecGroupLimits) { 3094 // Create and return a group for 1. 3095 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 1) != NULL); 3096 3097 // Create and return a group for 2. 3098 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 2) != NULL); 3099 3100 // Create and return a group for 4. This should remove 1 but not 2. 3101 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 4) != NULL); 3102 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 1) == NULL); 3103 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 2) != NULL); 3104 3105 // Create and return a group for 3. This will kill off 2. 3106 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 3) != NULL); 3107 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 2) == NULL); 3108 3109 // Verify that adding 5 kills off 3, despite 4 being created before 3. 3110 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 5) != NULL); 3111 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 4) != NULL); 3112 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 3) == NULL); 3113 } 3114 3115 TEST_F(QuicConnectionTest, ProcessFramesIfPacketClosedConnection) { 3116 // Construct a packet with stream frame and connection close frame. 3117 header_.public_header.guid = guid_; 3118 header_.packet_sequence_number = 1; 3119 header_.public_header.reset_flag = false; 3120 header_.public_header.version_flag = false; 3121 header_.entropy_flag = false; 3122 header_.fec_flag = false; 3123 header_.fec_group = 0; 3124 3125 QuicConnectionCloseFrame qccf; 3126 qccf.error_code = QUIC_PEER_GOING_AWAY; 3127 QuicFrame close_frame(&qccf); 3128 QuicFrame stream_frame(&frame1_); 3129 3130 QuicFrames frames; 3131 frames.push_back(stream_frame); 3132 frames.push_back(close_frame); 3133 scoped_ptr<QuicPacket> packet( 3134 framer_.BuildUnsizedDataPacket(header_, frames).packet); 3135 EXPECT_TRUE(NULL != packet.get()); 3136 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket( 3137 ENCRYPTION_NONE, 1, *packet)); 3138 3139 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, true)); 3140 EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(1); 3141 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 3142 3143 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); 3144 } 3145 3146 TEST_F(QuicConnectionTest, SelectMutualVersion) { 3147 // Set the connection to speak the lowest quic version. 3148 connection_.set_version(QuicVersionMin()); 3149 EXPECT_EQ(QuicVersionMin(), connection_.version()); 3150 3151 // Pass in available versions which includes a higher mutually supported 3152 // version. The higher mutually supported version should be selected. 3153 QuicVersionVector supported_versions; 3154 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { 3155 supported_versions.push_back(kSupportedQuicVersions[i]); 3156 } 3157 EXPECT_TRUE(connection_.SelectMutualVersion(supported_versions)); 3158 EXPECT_EQ(QuicVersionMax(), connection_.version()); 3159 3160 // Expect that the lowest version is selected. 3161 // Ensure the lowest supported version is less than the max, unless they're 3162 // the same. 3163 EXPECT_LE(QuicVersionMin(), QuicVersionMax()); 3164 QuicVersionVector lowest_version_vector; 3165 lowest_version_vector.push_back(QuicVersionMin()); 3166 EXPECT_TRUE(connection_.SelectMutualVersion(lowest_version_vector)); 3167 EXPECT_EQ(QuicVersionMin(), connection_.version()); 3168 3169 // Shouldn't be able to find a mutually supported version. 3170 QuicVersionVector unsupported_version; 3171 unsupported_version.push_back(QUIC_VERSION_UNSUPPORTED); 3172 EXPECT_FALSE(connection_.SelectMutualVersion(unsupported_version)); 3173 } 3174 3175 TEST_F(QuicConnectionTest, ConnectionCloseWhenNotWriteBlocked) { 3176 writer_->set_blocked(false); // Already default. 3177 3178 // Send a packet (but write will not block). 3179 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 3180 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 3181 EXPECT_EQ(1u, writer_->packets_write_attempts()); 3182 3183 // Send an erroneous packet to close the connection. 3184 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false)); 3185 ProcessDataPacket(6000, 0, !kEntropyFlag); 3186 EXPECT_EQ(2u, writer_->packets_write_attempts()); 3187 } 3188 3189 TEST_F(QuicConnectionTest, ConnectionCloseWhenWriteBlocked) { 3190 EXPECT_EQ(0u, connection_.NumQueuedPackets()); 3191 writer_->set_blocked(true); 3192 3193 // Send a packet to so that write will really block. 3194 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 3195 EXPECT_EQ(1u, connection_.NumQueuedPackets()); 3196 EXPECT_EQ(1u, writer_->packets_write_attempts()); 3197 3198 // Send an erroneous packet to close the connection. 3199 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false)); 3200 ProcessDataPacket(6000, 0, !kEntropyFlag); 3201 EXPECT_EQ(1u, writer_->packets_write_attempts()); 3202 } 3203 3204 TEST_F(QuicConnectionTest, ConnectionCloseWhenNothingPending) { 3205 writer_->set_blocked(true); 3206 3207 // Send an erroneous packet to close the connection. 3208 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false)); 3209 ProcessDataPacket(6000, 0, !kEntropyFlag); 3210 EXPECT_EQ(1u, writer_->packets_write_attempts()); 3211 } 3212 3213 TEST_F(QuicConnectionTest, AckNotifierTriggerCallback) { 3214 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 3215 3216 // Create a delegate which we expect to be called. 3217 MockAckNotifierDelegate delegate; 3218 EXPECT_CALL(delegate, OnAckNotification()).Times(1);; 3219 3220 // Send some data, which will register the delegate to be notified. 3221 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, &delegate); 3222 3223 // Process an ACK from the server which should trigger the callback. 3224 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(1); 3225 QuicAckFrame frame(1, QuicTime::Zero(), 0); 3226 frame.received_info.entropy_hash = 3227 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 3228 ProcessAckPacket(&frame); 3229 } 3230 3231 TEST_F(QuicConnectionTest, AckNotifierFailToTriggerCallback) { 3232 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 3233 3234 // Create a delegate which we don't expect to be called. 3235 MockAckNotifierDelegate delegate; 3236 EXPECT_CALL(delegate, OnAckNotification()).Times(0);; 3237 3238 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(2); 3239 3240 // Send some data, which will register the delegate to be notified. This will 3241 // not be ACKed and so the delegate should never be called. 3242 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, &delegate); 3243 3244 // Send some other data which we will ACK. 3245 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 3246 connection_.SendStreamDataWithString(1, "bar", 0, !kFin, NULL); 3247 3248 // Now we receive ACK for packets 2 and 3, but importantly missing packet 1 3249 // which we registered to be notified about. 3250 QuicAckFrame frame(3, QuicTime::Zero(), 0); 3251 frame.received_info.missing_packets.insert(1); 3252 frame.received_info.entropy_hash = 3253 QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^ 3254 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 3255 EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)); 3256 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)); 3257 ProcessAckPacket(&frame); 3258 } 3259 3260 TEST_F(QuicConnectionTest, AckNotifierCallbackAfterRetransmission) { 3261 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 3262 3263 // Create a delegate which we expect to be called. 3264 MockAckNotifierDelegate delegate; 3265 EXPECT_CALL(delegate, OnAckNotification()).Times(1);; 3266 3267 // In total expect ACKs for all 4 packets. 3268 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(4); 3269 3270 // Send four packets, and register to be notified on ACK of packet 2. 3271 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL); 3272 connection_.SendStreamDataWithString(1, "bar", 0, !kFin, &delegate); 3273 connection_.SendStreamDataWithString(1, "baz", 0, !kFin, NULL); 3274 connection_.SendStreamDataWithString(1, "qux", 0, !kFin, NULL); 3275 3276 // Now we receive ACK for packets 1, 3, and 4, which invokes fast retransmit. 3277 QuicAckFrame frame(4, QuicTime::Zero(), 0); 3278 frame.received_info.missing_packets.insert(2); 3279 frame.received_info.entropy_hash = 3280 QuicConnectionPeer::GetSentEntropyHash(&connection_, 4) ^ 3281 QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^ 3282 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 3283 EXPECT_CALL(*send_algorithm_, OnPacketLost(2, _)); 3284 EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _)); 3285 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); 3286 ProcessAckPacket(&frame); 3287 3288 // Now we get an ACK for packet 5 (retransmitted packet 2), which should 3289 // trigger the callback. 3290 QuicAckFrame second_ack_frame(5, QuicTime::Zero(), 0); 3291 second_ack_frame.received_info.entropy_hash = 3292 QuicConnectionPeer::GetSentEntropyHash(&connection_, 5); 3293 ProcessAckPacket(&second_ack_frame); 3294 } 3295 3296 // TODO(rjshade): Add a similar test that FEC recovery on peer (and resulting 3297 // ACK) triggers notification on our end. 3298 TEST_F(QuicConnectionTest, AckNotifierCallbackAfterFECRecovery) { 3299 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); 3300 EXPECT_CALL(visitor_, OnCanWrite()).Times(1).WillOnce(Return(true)); 3301 3302 // Create a delegate which we expect to be called. 3303 MockAckNotifierDelegate delegate; 3304 EXPECT_CALL(delegate, OnAckNotification()).Times(1);; 3305 3306 // Expect ACKs for 1 packet. 3307 EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _, _)).Times(1); 3308 3309 // Send one packet, and register to be notified on ACK. 3310 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, &delegate); 3311 3312 // Ack packet gets dropped, but we receive an FEC packet that covers it. 3313 // Should recover the Ack packet and trigger the notification callback. 3314 QuicFrames frames; 3315 3316 QuicAckFrame ack_frame(1, QuicTime::Zero(), 0); 3317 ack_frame.received_info.entropy_hash = 3318 QuicConnectionPeer::GetSentEntropyHash(&connection_, 1); 3319 frames.push_back(QuicFrame(&ack_frame)); 3320 3321 // Dummy stream frame to satisfy expectations set elsewhere. 3322 frames.push_back(QuicFrame(&frame1_)); 3323 3324 QuicPacketHeader ack_header; 3325 ack_header.public_header.guid = guid_; 3326 ack_header.public_header.reset_flag = false; 3327 ack_header.public_header.version_flag = false; 3328 ack_header.entropy_flag = !kEntropyFlag; 3329 ack_header.fec_flag = true; 3330 ack_header.packet_sequence_number = 1; 3331 ack_header.is_in_fec_group = IN_FEC_GROUP; 3332 ack_header.fec_group = 1; 3333 3334 QuicPacket* packet = 3335 framer_.BuildUnsizedDataPacket(ack_header, frames).packet; 3336 3337 // Take the packet which contains the ACK frame, and construct and deliver an 3338 // FEC packet which allows the ACK packet to be recovered. 3339 ProcessFecPacket(2, 1, true, !kEntropyFlag, packet); 3340 } 3341 3342 class MockQuicConnectionDebugVisitor 3343 : public QuicConnectionDebugVisitorInterface { 3344 public: 3345 MOCK_METHOD1(OnFrameAddedToPacket, 3346 void(const QuicFrame&)); 3347 3348 MOCK_METHOD4(OnPacketSent, 3349 void(QuicPacketSequenceNumber, 3350 EncryptionLevel, 3351 const QuicEncryptedPacket&, 3352 WriteResult)); 3353 3354 MOCK_METHOD2(OnPacketRetransmitted, 3355 void(QuicPacketSequenceNumber, 3356 QuicPacketSequenceNumber)); 3357 3358 MOCK_METHOD3(OnPacketReceived, 3359 void(const IPEndPoint&, 3360 const IPEndPoint&, 3361 const QuicEncryptedPacket&)); 3362 3363 MOCK_METHOD1(OnProtocolVersionMismatch, 3364 void(QuicVersion)); 3365 3366 MOCK_METHOD1(OnPacketHeader, 3367 void(const QuicPacketHeader& header)); 3368 3369 MOCK_METHOD1(OnStreamFrame, 3370 void(const QuicStreamFrame&)); 3371 3372 MOCK_METHOD1(OnAckFrame, 3373 void(const QuicAckFrame& frame)); 3374 3375 MOCK_METHOD1(OnCongestionFeedbackFrame, 3376 void(const QuicCongestionFeedbackFrame&)); 3377 3378 MOCK_METHOD1(OnRstStreamFrame, 3379 void(const QuicRstStreamFrame&)); 3380 3381 MOCK_METHOD1(OnConnectionCloseFrame, 3382 void(const QuicConnectionCloseFrame&)); 3383 3384 MOCK_METHOD1(OnPublicResetPacket, 3385 void(const QuicPublicResetPacket&)); 3386 3387 MOCK_METHOD1(OnVersionNegotiationPacket, 3388 void(const QuicVersionNegotiationPacket&)); 3389 3390 MOCK_METHOD2(OnRevivedPacket, 3391 void(const QuicPacketHeader&, StringPiece payload)); 3392 }; 3393 3394 TEST_F(QuicConnectionTest, OnPacketHeaderDebugVisitor) { 3395 QuicPacketHeader header; 3396 3397 scoped_ptr<MockQuicConnectionDebugVisitor> 3398 debug_visitor(new StrictMock<MockQuicConnectionDebugVisitor>); 3399 connection_.set_debug_visitor(debug_visitor.get()); 3400 EXPECT_CALL(*debug_visitor, OnPacketHeader(Ref(header))).Times(1); 3401 connection_.OnPacketHeader(header); 3402 } 3403 3404 TEST_F(QuicConnectionTest, Pacing) { 3405 ValueRestore<bool> old_flag(&FLAGS_enable_quic_pacing, true); 3406 3407 TestConnection server(guid_, IPEndPoint(), helper_.get(), writer_.get(), 3408 true); 3409 TestConnection client(guid_, IPEndPoint(), helper_.get(), writer_.get(), 3410 false); 3411 EXPECT_TRUE(client.sent_packet_manager().using_pacing()); 3412 EXPECT_FALSE(server.sent_packet_manager().using_pacing()); 3413 } 3414 3415 } // namespace 3416 } // namespace test 3417 } // namespace net 3418