| 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 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 const IPEndPoint& peer_address, | 289 const IPEndPoint& peer_address, |
| 290 QuicBlockedWriterInterface* blocked_writer) OVERRIDE { | 290 QuicBlockedWriterInterface* blocked_writer) OVERRIDE { |
| 291 QuicEncryptedPacket packet(buffer, buf_len); | 291 QuicEncryptedPacket packet(buffer, buf_len); |
| 292 ++packets_write_attempts_; | 292 ++packets_write_attempts_; |
| 293 | 293 |
| 294 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { | 294 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { |
| 295 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, | 295 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, |
| 296 sizeof(final_bytes_of_last_packet_)); | 296 sizeof(final_bytes_of_last_packet_)); |
| 297 } | 297 } |
| 298 | 298 |
| 299 QuicFramer framer(QuicVersionMax(), QuicTime::Zero(), !is_server_); | 299 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), !is_server_); |
| 300 if (use_tagging_decrypter_) { | 300 if (use_tagging_decrypter_) { |
| 301 framer.SetDecrypter(new TaggingDecrypter); | 301 framer.SetDecrypter(new TaggingDecrypter); |
| 302 } | 302 } |
| 303 FramerVisitorCapturingFrames visitor; | 303 FramerVisitorCapturingFrames visitor; |
| 304 framer.set_visitor(&visitor); | 304 framer.set_visitor(&visitor); |
| 305 EXPECT_TRUE(framer.ProcessPacket(packet)); | 305 EXPECT_TRUE(framer.ProcessPacket(packet)); |
| 306 header_ = *visitor.header(); | 306 header_ = *visitor.header(); |
| 307 frame_count_ = visitor.frame_count(); | 307 frame_count_ = visitor.frame_count(); |
| 308 if (visitor.ack()) { | 308 if (visitor.ack()) { |
| 309 ack_.reset(new QuicAckFrame(*visitor.ack())); | 309 ack_.reset(new QuicAckFrame(*visitor.ack())); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 }; | 395 }; |
| 396 | 396 |
| 397 class TestConnection : public QuicConnection { | 397 class TestConnection : public QuicConnection { |
| 398 public: | 398 public: |
| 399 TestConnection(QuicGuid guid, | 399 TestConnection(QuicGuid guid, |
| 400 IPEndPoint address, | 400 IPEndPoint address, |
| 401 TestConnectionHelper* helper, | 401 TestConnectionHelper* helper, |
| 402 TestPacketWriter* writer, | 402 TestPacketWriter* writer, |
| 403 bool is_server) | 403 bool is_server) |
| 404 : QuicConnection(guid, address, helper, writer, is_server, | 404 : QuicConnection(guid, address, helper, writer, is_server, |
| 405 QuicVersionMax()), | 405 QuicSupportedVersions()), |
| 406 helper_(helper), | 406 helper_(helper), |
| 407 writer_(writer) { | 407 writer_(writer) { |
| 408 writer_->set_is_server(is_server); | 408 writer_->set_is_server(is_server); |
| 409 } | 409 } |
| 410 | 410 |
| 411 void SendAck() { | 411 void SendAck() { |
| 412 QuicConnectionPeer::SendAck(this); | 412 QuicConnectionPeer::SendAck(this); |
| 413 } | 413 } |
| 414 | 414 |
| 415 void SetReceiveAlgorithm(TestReceiveAlgorithm* receive_algorithm) { | 415 void SetReceiveAlgorithm(TestReceiveAlgorithm* receive_algorithm) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 TestConnectionHelper* helper_; | 526 TestConnectionHelper* helper_; |
| 527 TestPacketWriter* writer_; | 527 TestPacketWriter* writer_; |
| 528 | 528 |
| 529 DISALLOW_COPY_AND_ASSIGN(TestConnection); | 529 DISALLOW_COPY_AND_ASSIGN(TestConnection); |
| 530 }; | 530 }; |
| 531 | 531 |
| 532 class QuicConnectionTest : public ::testing::TestWithParam<bool> { | 532 class QuicConnectionTest : public ::testing::TestWithParam<bool> { |
| 533 protected: | 533 protected: |
| 534 QuicConnectionTest() | 534 QuicConnectionTest() |
| 535 : guid_(42), | 535 : guid_(42), |
| 536 framer_(QuicVersionMax(), QuicTime::Zero(), false), | 536 framer_(QuicSupportedVersions(), QuicTime::Zero(), false), |
| 537 creator_(guid_, &framer_, QuicRandom::GetInstance(), false), | 537 creator_(guid_, &framer_, QuicRandom::GetInstance(), false), |
| 538 send_algorithm_(new StrictMock<MockSendAlgorithm>), | 538 send_algorithm_(new StrictMock<MockSendAlgorithm>), |
| 539 helper_(new TestConnectionHelper(&clock_, &random_generator_)), | 539 helper_(new TestConnectionHelper(&clock_, &random_generator_)), |
| 540 writer_(new TestPacketWriter()), | 540 writer_(new TestPacketWriter()), |
| 541 connection_(guid_, IPEndPoint(), helper_.get(), writer_.get(), false), | 541 connection_(guid_, IPEndPoint(), helper_.get(), writer_.get(), false), |
| 542 frame1_(1, false, 0, data1), | 542 frame1_(1, false, 0, data1), |
| 543 frame2_(1, false, 3, data2), | 543 frame2_(1, false, 3, data2), |
| 544 accept_packet_(true) { | 544 accept_packet_(true) { |
| 545 // TODO(rtenneti): remove g_* flags. | 545 // TODO(rtenneti): remove g_* flags. |
| 546 FLAGS_track_retransmission_history = true; | 546 FLAGS_track_retransmission_history = true; |
| (...skipping 2290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2837 framer_.BuildUnsizedDataPacket(header, frames).packet); | 2837 framer_.BuildUnsizedDataPacket(header, frames).packet); |
| 2838 scoped_ptr<QuicEncryptedPacket> encrypted( | 2838 scoped_ptr<QuicEncryptedPacket> encrypted( |
| 2839 framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet)); | 2839 framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet)); |
| 2840 | 2840 |
| 2841 framer_.set_version(QuicVersionMax()); | 2841 framer_.set_version(QuicVersionMax()); |
| 2842 connection_.set_is_server(true); | 2842 connection_.set_is_server(true); |
| 2843 writer_->set_blocked(true); | 2843 writer_->set_blocked(true); |
| 2844 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); | 2844 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); |
| 2845 EXPECT_EQ(0u, writer_->last_packet_size()); | 2845 EXPECT_EQ(0u, writer_->last_packet_size()); |
| 2846 EXPECT_TRUE(connection_.HasQueuedData()); | 2846 EXPECT_TRUE(connection_.HasQueuedData()); |
| 2847 EXPECT_TRUE(QuicConnectionPeer::IsWriteBlocked(&connection_)); |
| 2847 | 2848 |
| 2848 writer_->set_blocked(false); | 2849 writer_->set_blocked(false); |
| 2849 connection_.OnCanWrite(); | 2850 connection_.OnCanWrite(); |
| 2850 EXPECT_TRUE(writer_->version_negotiation_packet() != NULL); | 2851 EXPECT_TRUE(writer_->version_negotiation_packet() != NULL); |
| 2851 | 2852 |
| 2852 size_t num_versions = arraysize(kSupportedQuicVersions); | 2853 size_t num_versions = arraysize(kSupportedQuicVersions); |
| 2853 EXPECT_EQ(num_versions, | 2854 EXPECT_EQ(num_versions, |
| 2854 writer_->version_negotiation_packet()->versions.size()); | 2855 writer_->version_negotiation_packet()->versions.size()); |
| 2855 | 2856 |
| 2856 // We expect all versions in kSupportedQuicVersions to be | 2857 // We expect all versions in kSupportedQuicVersions to be |
| 2857 // included in the packet. | 2858 // included in the packet. |
| 2858 for (size_t i = 0; i < num_versions; ++i) { | 2859 for (size_t i = 0; i < num_versions; ++i) { |
| 2859 EXPECT_EQ(kSupportedQuicVersions[i], | 2860 EXPECT_EQ(kSupportedQuicVersions[i], |
| 2860 writer_->version_negotiation_packet()->versions[i]); | 2861 writer_->version_negotiation_packet()->versions[i]); |
| 2861 } | 2862 } |
| 2862 } | 2863 } |
| 2863 | 2864 |
| 2865 TEST_P(QuicConnectionTest, |
| 2866 ServerSendsVersionNegotiationPacketSocketBlockedDataBuffered) { |
| 2867 framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED); |
| 2868 |
| 2869 QuicPacketHeader header; |
| 2870 header.public_header.guid = guid_; |
| 2871 header.public_header.reset_flag = false; |
| 2872 header.public_header.version_flag = true; |
| 2873 header.entropy_flag = false; |
| 2874 header.fec_flag = false; |
| 2875 header.packet_sequence_number = 12; |
| 2876 header.fec_group = 0; |
| 2877 |
| 2878 QuicFrames frames; |
| 2879 QuicFrame frame(&frame1_); |
| 2880 frames.push_back(frame); |
| 2881 scoped_ptr<QuicPacket> packet( |
| 2882 framer_.BuildUnsizedDataPacket(header, frames).packet); |
| 2883 scoped_ptr<QuicEncryptedPacket> encrypted( |
| 2884 framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet)); |
| 2885 |
| 2886 framer_.set_version(QuicVersionMax()); |
| 2887 connection_.set_is_server(true); |
| 2888 writer_->set_blocked(true); |
| 2889 writer_->set_is_write_blocked_data_buffered(true); |
| 2890 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); |
| 2891 EXPECT_EQ(0u, writer_->last_packet_size()); |
| 2892 EXPECT_FALSE(connection_.HasQueuedData()); |
| 2893 EXPECT_TRUE(QuicConnectionPeer::IsWriteBlocked(&connection_)); |
| 2894 } |
| 2895 |
| 2864 TEST_P(QuicConnectionTest, ClientHandlesVersionNegotiation) { | 2896 TEST_P(QuicConnectionTest, ClientHandlesVersionNegotiation) { |
| 2865 // Start out with some unsupported version. | 2897 // Start out with some unsupported version. |
| 2866 QuicConnectionPeer::GetFramer(&connection_)->set_version_for_tests( | 2898 QuicConnectionPeer::GetFramer(&connection_)->set_version_for_tests( |
| 2867 QUIC_VERSION_UNSUPPORTED); | 2899 QUIC_VERSION_UNSUPPORTED); |
| 2868 | 2900 |
| 2869 QuicPacketHeader header; | 2901 QuicPacketHeader header; |
| 2870 header.public_header.guid = guid_; | 2902 header.public_header.guid = guid_; |
| 2871 header.public_header.reset_flag = false; | 2903 header.public_header.reset_flag = false; |
| 2872 header.public_header.version_flag = true; | 2904 header.public_header.version_flag = true; |
| 2873 header.entropy_flag = false; | 2905 header.entropy_flag = false; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3058 | 3090 |
| 3059 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, true)); | 3091 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, true)); |
| 3060 EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(0); | 3092 EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(0); |
| 3061 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | 3093 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3062 | 3094 |
| 3063 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); | 3095 connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted); |
| 3064 } | 3096 } |
| 3065 | 3097 |
| 3066 TEST_P(QuicConnectionTest, SelectMutualVersion) { | 3098 TEST_P(QuicConnectionTest, SelectMutualVersion) { |
| 3067 // Set the connection to speak the lowest quic version. | 3099 // Set the connection to speak the lowest quic version. |
| 3068 connection_.set_version(QuicVersionMin()); | 3100 connection_.set_version(test::QuicVersionMin()); |
| 3069 EXPECT_EQ(QuicVersionMin(), connection_.version()); | 3101 EXPECT_EQ(test::QuicVersionMin(), connection_.version()); |
| 3070 | 3102 |
| 3071 // Pass in available versions which includes a higher mutually supported | 3103 // Pass in available versions which includes a higher mutually supported |
| 3072 // version. The higher mutually supported version should be selected. | 3104 // version. The higher mutually supported version should be selected. |
| 3073 QuicVersionVector supported_versions; | 3105 QuicVersionVector supported_versions; |
| 3074 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { | 3106 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { |
| 3075 supported_versions.push_back(kSupportedQuicVersions[i]); | 3107 supported_versions.push_back(kSupportedQuicVersions[i]); |
| 3076 } | 3108 } |
| 3077 EXPECT_TRUE(connection_.SelectMutualVersion(supported_versions)); | 3109 EXPECT_TRUE(connection_.SelectMutualVersion(supported_versions)); |
| 3078 EXPECT_EQ(QuicVersionMax(), connection_.version()); | 3110 EXPECT_EQ(QuicVersionMax(), connection_.version()); |
| 3079 | 3111 |
| 3080 // Expect that the lowest version is selected. | 3112 // Expect that the lowest version is selected. |
| 3081 // Ensure the lowest supported version is less than the max, unless they're | 3113 // Ensure the lowest supported version is less than the max, unless they're |
| 3082 // the same. | 3114 // the same. |
| 3083 EXPECT_LE(QuicVersionMin(), QuicVersionMax()); | 3115 EXPECT_LE(test::QuicVersionMin(), QuicVersionMax()); |
| 3084 QuicVersionVector lowest_version_vector; | 3116 QuicVersionVector lowest_version_vector; |
| 3085 lowest_version_vector.push_back(QuicVersionMin()); | 3117 lowest_version_vector.push_back(test::QuicVersionMin()); |
| 3086 EXPECT_TRUE(connection_.SelectMutualVersion(lowest_version_vector)); | 3118 EXPECT_TRUE(connection_.SelectMutualVersion(lowest_version_vector)); |
| 3087 EXPECT_EQ(QuicVersionMin(), connection_.version()); | 3119 EXPECT_EQ(test::QuicVersionMin(), connection_.version()); |
| 3088 | 3120 |
| 3089 // Shouldn't be able to find a mutually supported version. | 3121 // Shouldn't be able to find a mutually supported version. |
| 3090 QuicVersionVector unsupported_version; | 3122 QuicVersionVector unsupported_version; |
| 3091 unsupported_version.push_back(QUIC_VERSION_UNSUPPORTED); | 3123 unsupported_version.push_back(QUIC_VERSION_UNSUPPORTED); |
| 3092 EXPECT_FALSE(connection_.SelectMutualVersion(unsupported_version)); | 3124 EXPECT_FALSE(connection_.SelectMutualVersion(unsupported_version)); |
| 3093 } | 3125 } |
| 3094 | 3126 |
| 3095 TEST_P(QuicConnectionTest, ConnectionCloseWhenNotWriteBlocked) { | 3127 TEST_P(QuicConnectionTest, ConnectionCloseWhenNotWriteBlocked) { |
| 3096 writer_->set_blocked(false); // Already default. | 3128 writer_->set_blocked(false); // Already default. |
| 3097 | 3129 |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3315 scoped_ptr<MockQuicConnectionDebugVisitor> | 3347 scoped_ptr<MockQuicConnectionDebugVisitor> |
| 3316 debug_visitor(new StrictMock<MockQuicConnectionDebugVisitor>); | 3348 debug_visitor(new StrictMock<MockQuicConnectionDebugVisitor>); |
| 3317 connection_.set_debug_visitor(debug_visitor.get()); | 3349 connection_.set_debug_visitor(debug_visitor.get()); |
| 3318 EXPECT_CALL(*debug_visitor, OnPacketHeader(Ref(header))).Times(1); | 3350 EXPECT_CALL(*debug_visitor, OnPacketHeader(Ref(header))).Times(1); |
| 3319 connection_.OnPacketHeader(header); | 3351 connection_.OnPacketHeader(header); |
| 3320 } | 3352 } |
| 3321 | 3353 |
| 3322 } // namespace | 3354 } // namespace |
| 3323 } // namespace test | 3355 } // namespace test |
| 3324 } // namespace net | 3356 } // namespace net |
| OLD | NEW |