| 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/core/quic_connection.h" | 5 #include "net/quic/core/quic_connection.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <ostream> | 9 #include <ostream> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 820 // Bypassing the packet creator is unrealistic, but allows us to process | 820 // Bypassing the packet creator is unrealistic, but allows us to process |
| 821 // packets the QuicPacketCreator won't allow us to create. | 821 // packets the QuicPacketCreator won't allow us to create. |
| 822 void ForceProcessFramePacket(QuicFrame frame) { | 822 void ForceProcessFramePacket(QuicFrame frame) { |
| 823 QuicFrames frames; | 823 QuicFrames frames; |
| 824 frames.push_back(QuicFrame(frame)); | 824 frames.push_back(QuicFrame(frame)); |
| 825 QuicPacketCreatorPeer::SetSendVersionInPacket( | 825 QuicPacketCreatorPeer::SetSendVersionInPacket( |
| 826 &peer_creator_, connection_.perspective() == Perspective::IS_SERVER); | 826 &peer_creator_, connection_.perspective() == Perspective::IS_SERVER); |
| 827 QuicPacketHeader header; | 827 QuicPacketHeader header; |
| 828 QuicPacketCreatorPeer::FillPacketHeader(&peer_creator_, &header); | 828 QuicPacketCreatorPeer::FillPacketHeader(&peer_creator_, &header); |
| 829 char encrypted_buffer[kMaxPacketSize]; | 829 char encrypted_buffer[kMaxPacketSize]; |
| 830 // TODO(fayang): Use data producer to produce data. |
| 830 size_t length = peer_framer_.BuildDataPacket( | 831 size_t length = peer_framer_.BuildDataPacket( |
| 831 header, frames, encrypted_buffer, kMaxPacketSize); | 832 header, frames, encrypted_buffer, kMaxPacketSize, nullptr); |
| 832 DCHECK_GT(length, 0u); | 833 DCHECK_GT(length, 0u); |
| 833 | 834 |
| 834 const size_t encrypted_length = peer_framer_.EncryptInPlace( | 835 const size_t encrypted_length = peer_framer_.EncryptInPlace( |
| 835 ENCRYPTION_NONE, header.packet_number, | 836 ENCRYPTION_NONE, header.packet_number, |
| 836 GetStartOfEncryptedData(peer_framer_.version(), header), length, | 837 GetStartOfEncryptedData(peer_framer_.version(), header), length, |
| 837 kMaxPacketSize, encrypted_buffer); | 838 kMaxPacketSize, encrypted_buffer); |
| 838 DCHECK_GT(encrypted_length, 0u); | 839 DCHECK_GT(encrypted_length, 0u); |
| 839 | 840 |
| 840 connection_.ProcessUdpPacket( | 841 connection_.ProcessUdpPacket( |
| 841 kSelfAddress, kPeerAddress, | 842 kSelfAddress, kPeerAddress, |
| (...skipping 1050 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1892 ASSERT_EQ(2u, writer_->stream_frames().size()); | 1893 ASSERT_EQ(2u, writer_->stream_frames().size()); |
| 1893 EXPECT_EQ(kClientDataStreamId1, writer_->stream_frames()[0]->stream_id); | 1894 EXPECT_EQ(kClientDataStreamId1, writer_->stream_frames()[0]->stream_id); |
| 1894 EXPECT_EQ(kClientDataStreamId2, writer_->stream_frames()[1]->stream_id); | 1895 EXPECT_EQ(kClientDataStreamId2, writer_->stream_frames()[1]->stream_id); |
| 1895 } | 1896 } |
| 1896 | 1897 |
| 1897 TEST_P(QuicConnectionTest, FramePackingSendv) { | 1898 TEST_P(QuicConnectionTest, FramePackingSendv) { |
| 1898 // Send data in 1 packet by writing multiple blocks in a single iovector | 1899 // Send data in 1 packet by writing multiple blocks in a single iovector |
| 1899 // using writev. | 1900 // using writev. |
| 1900 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | 1901 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 1901 | 1902 |
| 1902 char data[] = "ABCD"; | 1903 char data[] = "ABCDEF"; |
| 1903 struct iovec iov[2]; | 1904 struct iovec iov[2]; |
| 1904 iov[0].iov_base = data; | 1905 iov[0].iov_base = data; |
| 1905 iov[0].iov_len = 2; | 1906 iov[0].iov_len = 4; |
| 1906 iov[1].iov_base = data + 2; | 1907 iov[1].iov_base = data + 4; |
| 1907 iov[1].iov_len = 2; | 1908 iov[1].iov_len = 2; |
| 1908 connection_.SendStreamData(1, QuicIOVector(iov, 2, 4), 0, NO_FIN, nullptr); | 1909 connection_.SendStreamData(1, QuicIOVector(iov, 2, 6), 0, NO_FIN, nullptr); |
| 1909 | 1910 |
| 1910 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | 1911 EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 1911 EXPECT_FALSE(connection_.HasQueuedData()); | 1912 EXPECT_FALSE(connection_.HasQueuedData()); |
| 1912 | 1913 |
| 1913 // Parse the last packet and ensure multiple iovector blocks have | 1914 // Parse the last packet and ensure multiple iovector blocks have |
| 1914 // been packed into a single stream frame from one stream. | 1915 // been packed into a single stream frame from one stream. |
| 1915 EXPECT_EQ(2u, writer_->frame_count()); | 1916 EXPECT_EQ(2u, writer_->frame_count()); |
| 1916 EXPECT_EQ(1u, writer_->stream_frames().size()); | 1917 EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 1917 EXPECT_EQ(1u, writer_->padding_frames().size()); | 1918 EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 1918 QuicStreamFrame* frame = writer_->stream_frames()[0].get(); | 1919 QuicStreamFrame* frame = writer_->stream_frames()[0].get(); |
| 1919 EXPECT_EQ(1u, frame->stream_id); | 1920 EXPECT_EQ(1u, frame->stream_id); |
| 1920 EXPECT_EQ("ABCD", QuicStringPiece(frame->data_buffer, frame->data_length)); | 1921 EXPECT_EQ("ABCDEF", QuicStringPiece(frame->data_buffer, frame->data_length)); |
| 1921 } | 1922 } |
| 1922 | 1923 |
| 1923 TEST_P(QuicConnectionTest, FramePackingSendvQueued) { | 1924 TEST_P(QuicConnectionTest, FramePackingSendvQueued) { |
| 1924 // Try to send two stream frames in 1 packet by using writev. | 1925 // Try to send two stream frames in 1 packet by using writev. |
| 1925 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | 1926 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 1926 | 1927 |
| 1927 BlockOnNextWrite(); | 1928 BlockOnNextWrite(); |
| 1928 char data[] = "ABCD"; | 1929 char data[] = "ABCDEF"; |
| 1929 struct iovec iov[2]; | 1930 struct iovec iov[2]; |
| 1930 iov[0].iov_base = data; | 1931 iov[0].iov_base = data; |
| 1931 iov[0].iov_len = 2; | 1932 iov[0].iov_len = 4; |
| 1932 iov[1].iov_base = data + 2; | 1933 iov[1].iov_base = data + 4; |
| 1933 iov[1].iov_len = 2; | 1934 iov[1].iov_len = 2; |
| 1934 connection_.SendStreamData(1, QuicIOVector(iov, 2, 4), 0, NO_FIN, nullptr); | 1935 connection_.SendStreamData(1, QuicIOVector(iov, 2, 6), 0, NO_FIN, nullptr); |
| 1935 | 1936 |
| 1936 EXPECT_EQ(1u, connection_.NumQueuedPackets()); | 1937 EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 1937 EXPECT_TRUE(connection_.HasQueuedData()); | 1938 EXPECT_TRUE(connection_.HasQueuedData()); |
| 1938 | 1939 |
| 1939 // Unblock the writes and actually send. | 1940 // Unblock the writes and actually send. |
| 1940 writer_->SetWritable(); | 1941 writer_->SetWritable(); |
| 1941 connection_.OnCanWrite(); | 1942 connection_.OnCanWrite(); |
| 1942 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | 1943 EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 1943 | 1944 |
| 1944 // Parse the last packet and ensure it's one stream frame from one stream. | 1945 // Parse the last packet and ensure it's one stream frame from one stream. |
| (...skipping 1148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3093 // Acknowledge all packets sent so far. | 3094 // Acknowledge all packets sent so far. |
| 3094 QuicAckFrame probe_ack = InitAckFrame(probe_packet_number); | 3095 QuicAckFrame probe_ack = InitAckFrame(probe_packet_number); |
| 3095 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | 3096 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3096 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); | 3097 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3097 ProcessAckPacket(&probe_ack); | 3098 ProcessAckPacket(&probe_ack); |
| 3098 EXPECT_EQ(kMtuDiscoveryTargetPacketSizeHigh, connection_.max_packet_length()); | 3099 EXPECT_EQ(kMtuDiscoveryTargetPacketSizeHigh, connection_.max_packet_length()); |
| 3099 EXPECT_EQ(0u, connection_.GetBytesInFlight()); | 3100 EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 3100 | 3101 |
| 3101 // Send more packets, and ensure that none of them sets the alarm. | 3102 // Send more packets, and ensure that none of them sets the alarm. |
| 3102 for (QuicPacketCount i = 0; i < 4 * kPacketsBetweenMtuProbesBase; i++) { | 3103 for (QuicPacketCount i = 0; i < 4 * kPacketsBetweenMtuProbesBase; i++) { |
| 3103 SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); | 3104 SendStreamDataToPeer(3, ".", kPacketsBetweenMtuProbesBase + 1 + i, NO_FIN, |
| 3105 nullptr); |
| 3104 ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | 3106 ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 3105 } | 3107 } |
| 3106 | 3108 |
| 3107 EXPECT_EQ(1u, connection_.mtu_probe_count()); | 3109 EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 3108 } | 3110 } |
| 3109 | 3111 |
| 3110 // Tests whether MTU discovery works correctly when the probes never get | 3112 // Tests whether MTU discovery works correctly when the probes never get |
| 3111 // acknowledged. | 3113 // acknowledged. |
| 3112 TEST_P(QuicConnectionTest, MtuDiscoveryFailed) { | 3114 TEST_P(QuicConnectionTest, MtuDiscoveryFailed) { |
| 3113 EXPECT_TRUE(connection_.connected()); | 3115 EXPECT_TRUE(connection_.connected()); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3209 // Acknowledge all packets sent so far. | 3211 // Acknowledge all packets sent so far. |
| 3210 QuicAckFrame probe_ack = InitAckFrame(probe_sequence_number); | 3212 QuicAckFrame probe_ack = InitAckFrame(probe_sequence_number); |
| 3211 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | 3213 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3212 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); | 3214 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3213 ProcessAckPacket(&probe_ack); | 3215 ProcessAckPacket(&probe_ack); |
| 3214 EXPECT_EQ(mtu_limit, connection_.max_packet_length()); | 3216 EXPECT_EQ(mtu_limit, connection_.max_packet_length()); |
| 3215 EXPECT_EQ(0u, connection_.GetBytesInFlight()); | 3217 EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 3216 | 3218 |
| 3217 // Send more packets, and ensure that none of them sets the alarm. | 3219 // Send more packets, and ensure that none of them sets the alarm. |
| 3218 for (QuicPacketCount i = 0; i < 4 * kPacketsBetweenMtuProbesBase; i++) { | 3220 for (QuicPacketCount i = 0; i < 4 * kPacketsBetweenMtuProbesBase; i++) { |
| 3219 SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); | 3221 SendStreamDataToPeer(3, ".", kPacketsBetweenMtuProbesBase + 1 + i, NO_FIN, |
| 3222 nullptr); |
| 3220 ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | 3223 ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 3221 } | 3224 } |
| 3222 | 3225 |
| 3223 EXPECT_EQ(1u, connection_.mtu_probe_count()); | 3226 EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 3224 } | 3227 } |
| 3225 | 3228 |
| 3226 // Tests whether MTU discovery works when the writer returns an error despite | 3229 // Tests whether MTU discovery works when the writer returns an error despite |
| 3227 // advertising higher packet length. | 3230 // advertising higher packet length. |
| 3228 TEST_P(QuicConnectionTest, MtuDiscoveryWriterFailed) { | 3231 TEST_P(QuicConnectionTest, MtuDiscoveryWriterFailed) { |
| 3229 EXPECT_TRUE(connection_.connected()); | 3232 EXPECT_TRUE(connection_.connected()); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3308 | 3311 |
| 3309 // When we send a packet, the timeout will change to 5ms + | 3312 // When we send a packet, the timeout will change to 5ms + |
| 3310 // kInitialIdleTimeoutSecs. | 3313 // kInitialIdleTimeoutSecs. |
| 3311 clock_.AdvanceTime(five_ms); | 3314 clock_.AdvanceTime(five_ms); |
| 3312 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, FIN, nullptr); | 3315 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, FIN, nullptr); |
| 3313 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | 3316 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 3314 | 3317 |
| 3315 // Now send more data. This will not move the timeout becase | 3318 // Now send more data. This will not move the timeout becase |
| 3316 // no data has been recieved since the previous write. | 3319 // no data has been recieved since the previous write. |
| 3317 clock_.AdvanceTime(five_ms); | 3320 clock_.AdvanceTime(five_ms); |
| 3318 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, FIN, nullptr); | 3321 SendStreamDataToPeer(kClientDataStreamId1, "foo", 3, FIN, nullptr); |
| 3319 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | 3322 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 3320 | 3323 |
| 3321 // The original alarm will fire. We should not time out because we had a | 3324 // The original alarm will fire. We should not time out because we had a |
| 3322 // network event at t=5ms. The alarm will reregister. | 3325 // network event at t=5ms. The alarm will reregister. |
| 3323 clock_.AdvanceTime(initial_idle_timeout - five_ms - five_ms); | 3326 clock_.AdvanceTime(initial_idle_timeout - five_ms - five_ms); |
| 3324 EXPECT_EQ(default_timeout, clock_.ApproximateNow()); | 3327 EXPECT_EQ(default_timeout, clock_.ApproximateNow()); |
| 3325 connection_.GetTimeoutAlarm()->Fire(); | 3328 connection_.GetTimeoutAlarm()->Fire(); |
| 3326 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); | 3329 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 3327 EXPECT_TRUE(connection_.connected()); | 3330 EXPECT_TRUE(connection_.connected()); |
| 3328 EXPECT_EQ(default_timeout + five_ms, | 3331 EXPECT_EQ(default_timeout + five_ms, |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3447 | 3450 |
| 3448 // When we send a packet, the timeout will change to 5ms + | 3451 // When we send a packet, the timeout will change to 5ms + |
| 3449 // kInitialIdleTimeoutSecs. | 3452 // kInitialIdleTimeoutSecs. |
| 3450 clock_.AdvanceTime(five_ms); | 3453 clock_.AdvanceTime(five_ms); |
| 3451 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, FIN, nullptr); | 3454 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, FIN, nullptr); |
| 3452 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | 3455 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 3453 | 3456 |
| 3454 // Now send more data. This will not move the timeout becase | 3457 // Now send more data. This will not move the timeout becase |
| 3455 // no data has been recieved since the previous write. | 3458 // no data has been recieved since the previous write. |
| 3456 clock_.AdvanceTime(five_ms); | 3459 clock_.AdvanceTime(five_ms); |
| 3457 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, FIN, nullptr); | 3460 SendStreamDataToPeer(kClientDataStreamId1, "foo", 3, FIN, nullptr); |
| 3458 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | 3461 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 3459 | 3462 |
| 3460 // The original alarm will fire. We should not time out because we had a | 3463 // The original alarm will fire. We should not time out because we had a |
| 3461 // network event at t=5ms. The alarm will reregister. | 3464 // network event at t=5ms. The alarm will reregister. |
| 3462 clock_.AdvanceTime(default_idle_timeout - five_ms - five_ms); | 3465 clock_.AdvanceTime(default_idle_timeout - five_ms - five_ms); |
| 3463 EXPECT_EQ(default_timeout, clock_.ApproximateNow()); | 3466 EXPECT_EQ(default_timeout, clock_.ApproximateNow()); |
| 3464 connection_.GetTimeoutAlarm()->Fire(); | 3467 connection_.GetTimeoutAlarm()->Fire(); |
| 3465 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); | 3468 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 3466 EXPECT_TRUE(connection_.connected()); | 3469 EXPECT_TRUE(connection_.connected()); |
| 3467 EXPECT_EQ(default_timeout + five_ms, | 3470 EXPECT_EQ(default_timeout + five_ms, |
| (...skipping 1785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5253 error_details, ConnectionCloseSource::FROM_PEER)); | 5256 error_details, ConnectionCloseSource::FROM_PEER)); |
| 5254 connection_.set_perspective(Perspective::IS_CLIENT); | 5257 connection_.set_perspective(Perspective::IS_CLIENT); |
| 5255 connection_.CloseConnection(QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT, | 5258 connection_.CloseConnection(QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT, |
| 5256 error_details, | 5259 error_details, |
| 5257 ConnectionCloseBehavior::SILENT_CLOSE); | 5260 ConnectionCloseBehavior::SILENT_CLOSE); |
| 5258 } | 5261 } |
| 5259 | 5262 |
| 5260 } // namespace | 5263 } // namespace |
| 5261 } // namespace test | 5264 } // namespace test |
| 5262 } // namespace net | 5265 } // namespace net |
| OLD | NEW |