| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/quic_connection.h" | 5 #include "net/quic/quic_connection.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 | 253 |
| 254 private: | 254 private: |
| 255 MockClock* clock_; | 255 MockClock* clock_; |
| 256 MockRandom* random_generator_; | 256 MockRandom* random_generator_; |
| 257 | 257 |
| 258 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper); | 258 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper); |
| 259 }; | 259 }; |
| 260 | 260 |
| 261 class TestPacketWriter : public QuicPacketWriter { | 261 class TestPacketWriter : public QuicPacketWriter { |
| 262 public: | 262 public: |
| 263 explicit TestPacketWriter(QuicVersion version) | 263 TestPacketWriter(QuicVersion version, MockClock *clock) |
| 264 : version_(version), | 264 : version_(version), |
| 265 framer_(SupportedVersions(version_)), | 265 framer_(SupportedVersions(version_)), |
| 266 last_packet_size_(0), | 266 last_packet_size_(0), |
| 267 write_blocked_(false), | 267 write_blocked_(false), |
| 268 block_on_next_write_(false), | 268 block_on_next_write_(false), |
| 269 is_write_blocked_data_buffered_(false), | 269 is_write_blocked_data_buffered_(false), |
| 270 final_bytes_of_last_packet_(0), | 270 final_bytes_of_last_packet_(0), |
| 271 final_bytes_of_previous_packet_(0), | 271 final_bytes_of_previous_packet_(0), |
| 272 use_tagging_decrypter_(false), | 272 use_tagging_decrypter_(false), |
| 273 packets_write_attempts_(0) { | 273 packets_write_attempts_(0), |
| 274 clock_(clock), |
| 275 write_pause_time_delta_(QuicTime::Delta::Zero()) { |
| 274 } | 276 } |
| 275 | 277 |
| 276 // QuicPacketWriter interface | 278 // QuicPacketWriter interface |
| 277 WriteResult WritePacket(const char* buffer, | 279 WriteResult WritePacket(const char* buffer, |
| 278 size_t buf_len, | 280 size_t buf_len, |
| 279 const IPAddressNumber& self_address, | 281 const IPAddressNumber& self_address, |
| 280 const IPEndPoint& peer_address) override { | 282 const IPEndPoint& peer_address) override { |
| 281 QuicEncryptedPacket packet(buffer, buf_len); | 283 QuicEncryptedPacket packet(buffer, buf_len); |
| 282 ++packets_write_attempts_; | 284 ++packets_write_attempts_; |
| 283 | 285 |
| 284 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { | 286 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { |
| 285 final_bytes_of_previous_packet_ = final_bytes_of_last_packet_; | 287 final_bytes_of_previous_packet_ = final_bytes_of_last_packet_; |
| 286 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, | 288 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, |
| 287 sizeof(final_bytes_of_last_packet_)); | 289 sizeof(final_bytes_of_last_packet_)); |
| 288 } | 290 } |
| 289 | 291 |
| 290 if (use_tagging_decrypter_) { | 292 if (use_tagging_decrypter_) { |
| 291 framer_.framer()->SetDecrypter(new TaggingDecrypter, ENCRYPTION_NONE); | 293 framer_.framer()->SetDecrypter(new TaggingDecrypter, ENCRYPTION_NONE); |
| 292 } | 294 } |
| 293 EXPECT_TRUE(framer_.ProcessPacket(packet)); | 295 EXPECT_TRUE(framer_.ProcessPacket(packet)); |
| 294 if (block_on_next_write_) { | 296 if (block_on_next_write_) { |
| 295 write_blocked_ = true; | 297 write_blocked_ = true; |
| 296 block_on_next_write_ = false; | 298 block_on_next_write_ = false; |
| 297 } | 299 } |
| 298 if (IsWriteBlocked()) { | 300 if (IsWriteBlocked()) { |
| 299 return WriteResult(WRITE_STATUS_BLOCKED, -1); | 301 return WriteResult(WRITE_STATUS_BLOCKED, -1); |
| 300 } | 302 } |
| 301 last_packet_size_ = packet.length(); | 303 last_packet_size_ = packet.length(); |
| 304 |
| 305 if (!write_pause_time_delta_.IsZero()) { |
| 306 clock_->AdvanceTime(write_pause_time_delta_); |
| 307 } |
| 302 return WriteResult(WRITE_STATUS_OK, last_packet_size_); | 308 return WriteResult(WRITE_STATUS_OK, last_packet_size_); |
| 303 } | 309 } |
| 304 | 310 |
| 305 bool IsWriteBlockedDataBuffered() const override { | 311 bool IsWriteBlockedDataBuffered() const override { |
| 306 return is_write_blocked_data_buffered_; | 312 return is_write_blocked_data_buffered_; |
| 307 } | 313 } |
| 308 | 314 |
| 309 bool IsWriteBlocked() const override { return write_blocked_; } | 315 bool IsWriteBlocked() const override { return write_blocked_; } |
| 310 | 316 |
| 311 void SetWritable() override { write_blocked_ = false; } | 317 void SetWritable() override { write_blocked_ = false; } |
| 312 | 318 |
| 313 void BlockOnNextWrite() { block_on_next_write_ = true; } | 319 void BlockOnNextWrite() { block_on_next_write_ = true; } |
| 314 | 320 |
| 321 // Sets the amount of time that the writer should before the actual write. |
| 322 void SetWritePauseTimeDelta(QuicTime::Delta delta) { |
| 323 write_pause_time_delta_ = delta; |
| 324 } |
| 325 |
| 315 const QuicPacketHeader& header() { return framer_.header(); } | 326 const QuicPacketHeader& header() { return framer_.header(); } |
| 316 | 327 |
| 317 size_t frame_count() const { return framer_.num_frames(); } | 328 size_t frame_count() const { return framer_.num_frames(); } |
| 318 | 329 |
| 319 const vector<QuicAckFrame>& ack_frames() const { | 330 const vector<QuicAckFrame>& ack_frames() const { |
| 320 return framer_.ack_frames(); | 331 return framer_.ack_frames(); |
| 321 } | 332 } |
| 322 | 333 |
| 323 const vector<QuicCongestionFeedbackFrame>& feedback_frames() const { | 334 const vector<QuicCongestionFeedbackFrame>& feedback_frames() const { |
| 324 return framer_.feedback_frames(); | 335 return framer_.feedback_frames(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 QuicVersion version_; | 396 QuicVersion version_; |
| 386 SimpleQuicFramer framer_; | 397 SimpleQuicFramer framer_; |
| 387 size_t last_packet_size_; | 398 size_t last_packet_size_; |
| 388 bool write_blocked_; | 399 bool write_blocked_; |
| 389 bool block_on_next_write_; | 400 bool block_on_next_write_; |
| 390 bool is_write_blocked_data_buffered_; | 401 bool is_write_blocked_data_buffered_; |
| 391 uint32 final_bytes_of_last_packet_; | 402 uint32 final_bytes_of_last_packet_; |
| 392 uint32 final_bytes_of_previous_packet_; | 403 uint32 final_bytes_of_previous_packet_; |
| 393 bool use_tagging_decrypter_; | 404 bool use_tagging_decrypter_; |
| 394 uint32 packets_write_attempts_; | 405 uint32 packets_write_attempts_; |
| 406 MockClock *clock_; |
| 407 // If non-zero, the clock will pause during WritePacket for this amount of |
| 408 // time. |
| 409 QuicTime::Delta write_pause_time_delta_; |
| 395 | 410 |
| 396 DISALLOW_COPY_AND_ASSIGN(TestPacketWriter); | 411 DISALLOW_COPY_AND_ASSIGN(TestPacketWriter); |
| 397 }; | 412 }; |
| 398 | 413 |
| 399 class TestConnection : public QuicConnection { | 414 class TestConnection : public QuicConnection { |
| 400 public: | 415 public: |
| 401 TestConnection(QuicConnectionId connection_id, | 416 TestConnection(QuicConnectionId connection_id, |
| 402 IPEndPoint address, | 417 IPEndPoint address, |
| 403 TestConnectionHelper* helper, | 418 TestConnectionHelper* helper, |
| 404 const PacketWriterFactory& factory, | 419 const PacketWriterFactory& factory, |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 | 621 |
| 607 class QuicConnectionTest : public ::testing::TestWithParam<QuicVersion> { | 622 class QuicConnectionTest : public ::testing::TestWithParam<QuicVersion> { |
| 608 protected: | 623 protected: |
| 609 QuicConnectionTest() | 624 QuicConnectionTest() |
| 610 : connection_id_(42), | 625 : connection_id_(42), |
| 611 framer_(SupportedVersions(version()), QuicTime::Zero(), false), | 626 framer_(SupportedVersions(version()), QuicTime::Zero(), false), |
| 612 peer_creator_(connection_id_, &framer_, &random_generator_), | 627 peer_creator_(connection_id_, &framer_, &random_generator_), |
| 613 send_algorithm_(new StrictMock<MockSendAlgorithm>), | 628 send_algorithm_(new StrictMock<MockSendAlgorithm>), |
| 614 loss_algorithm_(new MockLossAlgorithm()), | 629 loss_algorithm_(new MockLossAlgorithm()), |
| 615 helper_(new TestConnectionHelper(&clock_, &random_generator_)), | 630 helper_(new TestConnectionHelper(&clock_, &random_generator_)), |
| 616 writer_(new TestPacketWriter(version())), | 631 writer_(new TestPacketWriter(version(), &clock_)), |
| 617 factory_(writer_.get()), | 632 factory_(writer_.get()), |
| 618 connection_(connection_id_, IPEndPoint(), helper_.get(), | 633 connection_(connection_id_, IPEndPoint(), helper_.get(), |
| 619 factory_, false, version()), | 634 factory_, false, version()), |
| 620 frame1_(1, false, 0, MakeIOVector(data1)), | 635 frame1_(1, false, 0, MakeIOVector(data1)), |
| 621 frame2_(1, false, 3, MakeIOVector(data2)), | 636 frame2_(1, false, 3, MakeIOVector(data2)), |
| 622 sequence_number_length_(PACKET_6BYTE_SEQUENCE_NUMBER), | 637 sequence_number_length_(PACKET_6BYTE_SEQUENCE_NUMBER), |
| 623 connection_id_length_(PACKET_8BYTE_CONNECTION_ID) { | 638 connection_id_length_(PACKET_8BYTE_CONNECTION_ID) { |
| 624 connection_.set_visitor(&visitor_); | 639 connection_.set_visitor(&visitor_); |
| 625 connection_.SetSendAlgorithm(send_algorithm_); | 640 connection_.SetSendAlgorithm(send_algorithm_); |
| 626 connection_.SetLossAlgorithm(loss_algorithm_); | 641 connection_.SetLossAlgorithm(loss_algorithm_); |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 969 ProcessDataPacket(6000, 0, !kEntropyFlag); | 984 ProcessDataPacket(6000, 0, !kEntropyFlag); |
| 970 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) == | 985 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) == |
| 971 nullptr); | 986 nullptr); |
| 972 } | 987 } |
| 973 | 988 |
| 974 void BlockOnNextWrite() { | 989 void BlockOnNextWrite() { |
| 975 writer_->BlockOnNextWrite(); | 990 writer_->BlockOnNextWrite(); |
| 976 EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); | 991 EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); |
| 977 } | 992 } |
| 978 | 993 |
| 994 void SetWritePauseTimeDelta(QuicTime::Delta delta) { |
| 995 writer_->SetWritePauseTimeDelta(delta); |
| 996 } |
| 997 |
| 979 void CongestionBlockWrites() { | 998 void CongestionBlockWrites() { |
| 980 EXPECT_CALL(*send_algorithm_, | 999 EXPECT_CALL(*send_algorithm_, |
| 981 TimeUntilSend(_, _, _)).WillRepeatedly( | 1000 TimeUntilSend(_, _, _)).WillRepeatedly( |
| 982 testing::Return(QuicTime::Delta::FromSeconds(1))); | 1001 testing::Return(QuicTime::Delta::FromSeconds(1))); |
| 983 } | 1002 } |
| 984 | 1003 |
| 985 void CongestionUnblockWrites() { | 1004 void CongestionUnblockWrites() { |
| 986 EXPECT_CALL(*send_algorithm_, | 1005 EXPECT_CALL(*send_algorithm_, |
| 987 TimeUntilSend(_, _, _)).WillRepeatedly( | 1006 TimeUntilSend(_, _, _)).WillRepeatedly( |
| 988 testing::Return(QuicTime::Delta::Zero())); | 1007 testing::Return(QuicTime::Delta::Zero())); |
| (...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1543 SendAckPacketToPeer(); // Packet 7 | 1562 SendAckPacketToPeer(); // Packet 7 |
| 1544 EXPECT_EQ(7u, least_unacked()); | 1563 EXPECT_EQ(7u, least_unacked()); |
| 1545 | 1564 |
| 1546 // But if we send more data it should. | 1565 // But if we send more data it should. |
| 1547 SendStreamDataToPeer(1, "eep", 6, !kFin, &last_packet); // Packet 8 | 1566 SendStreamDataToPeer(1, "eep", 6, !kFin, &last_packet); // Packet 8 |
| 1548 EXPECT_EQ(8u, last_packet); | 1567 EXPECT_EQ(8u, last_packet); |
| 1549 SendAckPacketToPeer(); // Packet 9 | 1568 SendAckPacketToPeer(); // Packet 9 |
| 1550 EXPECT_EQ(7u, least_unacked()); | 1569 EXPECT_EQ(7u, least_unacked()); |
| 1551 } | 1570 } |
| 1552 | 1571 |
| 1572 // If FLAGS_quic_record_send_time_before_write is disabled, QuicConnection |
| 1573 // should record the packet sen-tdime after the packet is sent. |
| 1574 TEST_P(QuicConnectionTest, RecordSentTimeAfterPacketSent) { |
| 1575 ValueRestore<bool> old_flag(&FLAGS_quic_record_send_time_before_write, false); |
| 1576 // We're using a MockClock for the tests, so we have complete control over the |
| 1577 // time. |
| 1578 // Our recorded timestamp for the last packet sent time will be passed in to |
| 1579 // the send_algorithm. Make sure that it is set to the correct value. |
| 1580 QuicTime actual_recorded_send_time = QuicTime::Zero(); |
| 1581 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1582 .WillOnce(DoAll(SaveArg<0>(&actual_recorded_send_time), Return(true))); |
| 1583 |
| 1584 // First send without any pause and check the result. |
| 1585 QuicTime expected_recorded_send_time = clock_.Now(); |
| 1586 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); |
| 1587 EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time) |
| 1588 << "Expected time = " << expected_recorded_send_time.ToDebuggingValue() |
| 1589 << ". Actual time = " << actual_recorded_send_time.ToDebuggingValue(); |
| 1590 |
| 1591 // Now pause during the write, and check the results. |
| 1592 actual_recorded_send_time = QuicTime::Zero(); |
| 1593 const QuicTime::Delta kWritePauseTimeDelta = |
| 1594 QuicTime::Delta::FromMilliseconds(5000); |
| 1595 SetWritePauseTimeDelta(kWritePauseTimeDelta); |
| 1596 expected_recorded_send_time = clock_.Now().Add(kWritePauseTimeDelta); |
| 1597 |
| 1598 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1599 .WillOnce(DoAll(SaveArg<0>(&actual_recorded_send_time), Return(true))); |
| 1600 connection_.SendStreamDataWithString(2, "baz", 0, !kFin, nullptr); |
| 1601 EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time) |
| 1602 << "Expected time = " << expected_recorded_send_time.ToDebuggingValue() |
| 1603 << ". Actual time = " << actual_recorded_send_time.ToDebuggingValue(); |
| 1604 } |
| 1605 |
| 1606 // If FLAGS_quic_record_send_time_before_write is enabled, QuicConnection should |
| 1607 // record the the packet sent-time prior to sending the packet. |
| 1608 TEST_P(QuicConnectionTest, RecordSentTimeBeforePacketSent) { |
| 1609 ValueRestore<bool> old_flag(&FLAGS_quic_record_send_time_before_write, true); |
| 1610 // We're using a MockClock for the tests, so we have complete control over the |
| 1611 // time. |
| 1612 // Our recorded timestamp for the last packet sent time will be passed in to |
| 1613 // the send_algorithm. Make sure that it is set to the correct value. |
| 1614 QuicTime actual_recorded_send_time = QuicTime::Zero(); |
| 1615 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1616 .WillOnce(DoAll(SaveArg<0>(&actual_recorded_send_time), Return(true))); |
| 1617 |
| 1618 // First send without any pause and check the result. |
| 1619 QuicTime expected_recorded_send_time = clock_.Now(); |
| 1620 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); |
| 1621 EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time) |
| 1622 << "Expected time = " << expected_recorded_send_time.ToDebuggingValue() |
| 1623 << ". Actual time = " << actual_recorded_send_time.ToDebuggingValue(); |
| 1624 |
| 1625 // Now pause during the write, and check the results. |
| 1626 actual_recorded_send_time = QuicTime::Zero(); |
| 1627 const QuicTime::Delta kWritePauseTimeDelta = |
| 1628 QuicTime::Delta::FromMilliseconds(5000); |
| 1629 SetWritePauseTimeDelta(kWritePauseTimeDelta); |
| 1630 expected_recorded_send_time = clock_.Now(); |
| 1631 |
| 1632 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1633 .WillOnce(DoAll(SaveArg<0>(&actual_recorded_send_time), Return(true))); |
| 1634 connection_.SendStreamDataWithString(2, "baz", 0, !kFin, nullptr); |
| 1635 EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time) |
| 1636 << "Expected time = " << expected_recorded_send_time.ToDebuggingValue() |
| 1637 << ". Actual time = " << actual_recorded_send_time.ToDebuggingValue(); |
| 1638 } |
| 1639 |
| 1553 TEST_P(QuicConnectionTest, FECSending) { | 1640 TEST_P(QuicConnectionTest, FECSending) { |
| 1554 // All packets carry version info till version is negotiated. | 1641 // All packets carry version info till version is negotiated. |
| 1555 QuicPacketCreator* creator = | 1642 QuicPacketCreator* creator = |
| 1556 QuicConnectionPeer::GetPacketCreator(&connection_); | 1643 QuicConnectionPeer::GetPacketCreator(&connection_); |
| 1557 size_t payload_length; | 1644 size_t payload_length; |
| 1558 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining | 1645 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining |
| 1559 // packet length. The size of the offset field in a stream frame is 0 for | 1646 // packet length. The size of the offset field in a stream frame is 0 for |
| 1560 // offset 0, and 2 for non-zero offsets up through 64K. Increase | 1647 // offset 0, and 2 for non-zero offsets up through 64K. Increase |
| 1561 // max_packet_length by 2 so that subsequent packets containing subsequent | 1648 // max_packet_length by 2 so that subsequent packets containing subsequent |
| 1562 // stream frames with non-zero offets will fit within the packet length. | 1649 // stream frames with non-zero offets will fit within the packet length. |
| (...skipping 2535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4098 QuicBlockedFrame blocked; | 4185 QuicBlockedFrame blocked; |
| 4099 blocked.stream_id = 3; | 4186 blocked.stream_id = 3; |
| 4100 EXPECT_CALL(visitor_, OnBlockedFrames(_)); | 4187 EXPECT_CALL(visitor_, OnBlockedFrames(_)); |
| 4101 ProcessFramePacket(QuicFrame(&blocked)); | 4188 ProcessFramePacket(QuicFrame(&blocked)); |
| 4102 EXPECT_TRUE(ack_alarm->IsSet()); | 4189 EXPECT_TRUE(ack_alarm->IsSet()); |
| 4103 } | 4190 } |
| 4104 | 4191 |
| 4105 } // namespace | 4192 } // namespace |
| 4106 } // namespace test | 4193 } // namespace test |
| 4107 } // namespace net | 4194 } // namespace net |
| OLD | NEW |