Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 1 | // Copyright 2017 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 | |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 5 | #include "net/quic/quic_connectivity_probing_manager.h" |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 6 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7 | #include "base/stl_util.h" |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 8 | #include "base/test/test_mock_time_task_runner.h" |
| 9 | #include "net/log/test_net_log.h" |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 10 | #include "net/socket/socket_test_util.h" |
| 11 | #include "net/test/gtest_util.h" |
Victor Vasiliev | 6bb59d2 | 2019-03-08 21:34:51 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/test_tools/mock_clock.h" |
| 13 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 14 | #include "testing/gmock/include/gmock/gmock.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | using testing::_; |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 18 | using testing::Return; |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 19 | |
| 20 | namespace net { |
| 21 | namespace test { |
| 22 | namespace { |
| 23 | |
Zhongyi Shi | 7b280d0 | 2018-12-13 02:54:41 | [diff] [blame] | 24 | const NetworkChangeNotifier::NetworkHandle testNetworkHandle = 1; |
| 25 | |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 26 | const IPEndPoint kIpEndPoint = |
| 27 | IPEndPoint(IPAddress::IPv4AllZeros(), quic::test::kTestPort); |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 28 | const quic::QuicSocketAddress testPeerAddress = |
| 29 | quic::QuicSocketAddress(quic::QuicSocketAddressImpl(kIpEndPoint)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 30 | |
Zhongyi Shi | 7b280d0 | 2018-12-13 02:54:41 | [diff] [blame] | 31 | const IPEndPoint newIpEndPoint = |
| 32 | IPEndPoint(IPAddress::IPv4AllZeros(), quic::test::kTestPort + 1); |
| 33 | const quic::QuicSocketAddress newPeerAddress = |
| 34 | quic::QuicSocketAddress(quic::QuicSocketAddressImpl(newIpEndPoint)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 35 | } // anonymous namespace |
| 36 | |
| 37 | class MockQuicChromiumClientSession |
| 38 | : public QuicConnectivityProbingManager::Delegate, |
| 39 | public QuicChromiumPacketReader::Visitor { |
| 40 | public: |
| 41 | MockQuicChromiumClientSession() |
Zhongyi Shi | a0b6c68 | 2018-12-17 19:01:44 | [diff] [blame] | 42 | : probed_network_(NetworkChangeNotifier::kInvalidNetworkHandle) {} |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 43 | ~MockQuicChromiumClientSession() override {} |
| 44 | |
| 45 | // QuicChromiumPacketReader::Visitor interface. |
| 46 | MOCK_METHOD2(OnReadError, |
| 47 | void(int result, const DatagramClientSocket* socket)); |
| 48 | |
| 49 | MOCK_METHOD3(OnPacket, |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 50 | bool(const quic::QuicReceivedPacket& packet, |
| 51 | const quic::QuicSocketAddress& local_address, |
| 52 | const quic::QuicSocketAddress& peer_address)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 53 | |
Zhongyi Shi | fdacb82 | 2018-12-11 22:49:15 | [diff] [blame] | 54 | MOCK_METHOD2(OnProbeFailed, |
| 55 | void(NetworkChangeNotifier::NetworkHandle network, |
| 56 | const quic::QuicSocketAddress& peer_address)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 57 | |
| 58 | MOCK_METHOD2(OnSendConnectivityProbingPacket, |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 59 | bool(QuicChromiumPacketWriter* writer, |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 60 | const quic::QuicSocketAddress& peer_address)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 61 | |
Zhongyi Shi | fdacb82 | 2018-12-11 22:49:15 | [diff] [blame] | 62 | void OnProbeSucceeded( |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 63 | NetworkChangeNotifier::NetworkHandle network, |
Zhongyi Shi | fdacb82 | 2018-12-11 22:49:15 | [diff] [blame] | 64 | const quic::QuicSocketAddress& peer_address, |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 65 | const quic::QuicSocketAddress& self_address, |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 66 | std::unique_ptr<DatagramClientSocket> socket, |
| 67 | std::unique_ptr<QuicChromiumPacketWriter> writer, |
| 68 | std::unique_ptr<QuicChromiumPacketReader> reader) override { |
Zhongyi Shi | a0b6c68 | 2018-12-17 19:01:44 | [diff] [blame] | 69 | probed_network_ = network; |
| 70 | probed_peer_address_ = peer_address; |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 71 | } |
| 72 | |
Zhongyi Shi | a0b6c68 | 2018-12-17 19:01:44 | [diff] [blame] | 73 | NetworkChangeNotifier::NetworkHandle probed_network() const { |
| 74 | return probed_network_; |
| 75 | } |
| 76 | |
| 77 | quic::QuicSocketAddress probed_peer_address() const { |
| 78 | return probed_peer_address_; |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | private: |
Zhongyi Shi | a0b6c68 | 2018-12-17 19:01:44 | [diff] [blame] | 82 | NetworkChangeNotifier::NetworkHandle probed_network_; |
| 83 | quic::QuicSocketAddress probed_peer_address_; |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(MockQuicChromiumClientSession); |
| 86 | }; |
| 87 | |
| 88 | class QuicConnectivityProbingManagerTest : public ::testing::Test { |
| 89 | public: |
| 90 | QuicConnectivityProbingManagerTest() |
| 91 | : test_task_runner_(new base::TestMockTimeTaskRunner()), |
| 92 | test_task_runner_context_(test_task_runner_), |
| 93 | probing_manager_(&session_, test_task_runner_.get()), |
| 94 | default_read_(new MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0)), |
| 95 | socket_data_( |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 96 | new SequencedSocketData(base::make_span(default_read_.get(), 1), |
| 97 | base::span<MockWrite>())) { |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 98 | socket_factory_.AddSocketDataProvider(socket_data_.get()); |
| 99 | // Create a connected socket for probing. |
| 100 | socket_ = socket_factory_.CreateDatagramClientSocket( |
Sergey Ulanov | cbdfc885 | 2018-03-16 20:13:28 | [diff] [blame] | 101 | DatagramSocket::DEFAULT_BIND, &net_log_, NetLogSource()); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 102 | EXPECT_THAT(socket_->Connect(kIpEndPoint), IsOk()); |
| 103 | IPEndPoint self_address; |
| 104 | socket_->GetLocalAddress(&self_address); |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 105 | self_address_ = |
| 106 | quic::QuicSocketAddress(quic::QuicSocketAddressImpl(self_address)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 107 | // Create packet writer and reader for probing. |
Ryan Hamilton | 9edcf1a | 2017-11-22 05:55:17 | [diff] [blame] | 108 | writer_.reset( |
| 109 | new QuicChromiumPacketWriter(socket_.get(), test_task_runner_.get())); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 110 | reader_.reset(new QuicChromiumPacketReader( |
| 111 | socket_.get(), &clock_, &session_, kQuicYieldAfterPacketsRead, |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 112 | quic::QuicTime::Delta::FromMilliseconds( |
| 113 | kQuicYieldAfterDurationMilliseconds), |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 114 | bound_test_net_log_.bound())); |
| 115 | } |
| 116 | |
| 117 | protected: |
| 118 | // All tests will run inside the scope of |test_task_runner_|. |
| 119 | scoped_refptr<base::TestMockTimeTaskRunner> test_task_runner_; |
| 120 | base::TestMockTimeTaskRunner::ScopedContext test_task_runner_context_; |
| 121 | MockQuicChromiumClientSession session_; |
| 122 | QuicConnectivityProbingManager probing_manager_; |
| 123 | |
| 124 | std::unique_ptr<MockRead> default_read_; |
| 125 | std::unique_ptr<SequencedSocketData> socket_data_; |
| 126 | |
| 127 | std::unique_ptr<DatagramClientSocket> socket_; |
| 128 | std::unique_ptr<QuicChromiumPacketWriter> writer_; |
| 129 | std::unique_ptr<QuicChromiumPacketReader> reader_; |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 130 | quic::QuicSocketAddress self_address_; |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 131 | |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 132 | quic::MockClock clock_; |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 133 | MockClientSocketFactory socket_factory_; |
| 134 | TestNetLog net_log_; |
| 135 | BoundTestNetLog bound_test_net_log_; |
| 136 | |
| 137 | DISALLOW_COPY_AND_ASSIGN(QuicConnectivityProbingManagerTest); |
| 138 | }; |
| 139 | |
| 140 | TEST_F(QuicConnectivityProbingManagerTest, ReceiveProbingResponseOnSamePath) { |
| 141 | int initial_timeout_ms = 100; |
| 142 | |
| 143 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 144 | .WillOnce(Return(true)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 145 | probing_manager_.StartProbing( |
| 146 | testNetworkHandle, testPeerAddress, std::move(socket_), |
| 147 | std::move(writer_), std::move(reader_), |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 148 | base::TimeDelta::FromMilliseconds(initial_timeout_ms), |
| 149 | bound_test_net_log_.bound()); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 150 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 151 | |
| 152 | // Fast forward initial_timeout_ms, timeout the first connectivity probing |
| 153 | // packet, introduce another probing packet to sent out with timeout set to |
| 154 | // 2 * initial_timeout_ms. |
| 155 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 156 | .WillOnce(Return(true)); |
| 157 | |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 158 | test_task_runner_->FastForwardBy( |
| 159 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 160 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 161 | |
| 162 | // Fast forward initial_timeout_ms, should be no-op. |
| 163 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 164 | .Times(0); |
| 165 | test_task_runner_->FastForwardBy( |
| 166 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 167 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 168 | |
| 169 | // Notify the manager a connectivity probing packet is received from |
| 170 | // testPeerAddress to |self_address_|, manager should decalre probing as |
| 171 | // successful, notify delegate and will no longer send connectivity probing |
| 172 | // packet for this probing. |
| 173 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 174 | .Times(0); |
| 175 | probing_manager_.OnConnectivityProbingReceived(self_address_, |
| 176 | testPeerAddress); |
Zhongyi Shi | a0b6c68 | 2018-12-17 19:01:44 | [diff] [blame] | 177 | EXPECT_EQ(session_.probed_network(), testNetworkHandle); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 178 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 179 | |
| 180 | // Verify there's nothing to send. |
| 181 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 182 | .Times(0); |
| 183 | test_task_runner_->FastForwardBy( |
| 184 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 185 | EXPECT_EQ(0u, test_task_runner_->GetPendingTaskCount()); |
| 186 | } |
| 187 | |
| 188 | TEST_F(QuicConnectivityProbingManagerTest, |
| 189 | ReceiveProbingResponseOnDifferentPath) { |
| 190 | int initial_timeout_ms = 100; |
| 191 | |
| 192 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 193 | .WillOnce(Return(true)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 194 | probing_manager_.StartProbing( |
| 195 | testNetworkHandle, testPeerAddress, std::move(socket_), |
| 196 | std::move(writer_), std::move(reader_), |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 197 | base::TimeDelta::FromMilliseconds(initial_timeout_ms), |
| 198 | bound_test_net_log_.bound()); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 199 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 200 | |
| 201 | // Fast forward initial_timeout_ms, timeout the first connectivity probing |
| 202 | // packet, introduce another probing packet to sent out with timeout set to |
| 203 | // 2 * initial_timeout_ms. |
| 204 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 205 | .WillOnce(Return(true)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 206 | test_task_runner_->FastForwardBy( |
| 207 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 208 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 209 | |
| 210 | // Fast forward initial_timeout_ms, should be no-op. |
| 211 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 212 | .Times(0); |
| 213 | test_task_runner_->FastForwardBy( |
| 214 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 215 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 216 | |
| 217 | // Notify the manager a connectivity probing packet is received from |
| 218 | // testPeerAddress to a different self address, manager should ignore the |
| 219 | // probing response and continue waiting. |
| 220 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 221 | .Times(0); |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 222 | probing_manager_.OnConnectivityProbingReceived(quic::QuicSocketAddress(), |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 223 | testPeerAddress); |
Zhongyi Shi | a0b6c68 | 2018-12-17 19:01:44 | [diff] [blame] | 224 | EXPECT_NE(session_.probed_network(), testNetworkHandle); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 225 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 226 | |
| 227 | // Fast forward another initial_timeout_ms, another probing packet will be |
| 228 | // sent. |
| 229 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 230 | .WillOnce(Return(true)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 231 | test_task_runner_->FastForwardBy( |
| 232 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 233 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 234 | |
| 235 | // Finally receive the probing response on the same path. |
| 236 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 237 | .Times(0); |
| 238 | probing_manager_.OnConnectivityProbingReceived(self_address_, |
| 239 | testPeerAddress); |
Zhongyi Shi | a0b6c68 | 2018-12-17 19:01:44 | [diff] [blame] | 240 | EXPECT_EQ(session_.probed_network(), testNetworkHandle); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 241 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 242 | |
| 243 | // Verify there's nothing to send. |
| 244 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 245 | .Times(0); |
| 246 | test_task_runner_->RunUntilIdle(); |
| 247 | } |
| 248 | |
| 249 | TEST_F(QuicConnectivityProbingManagerTest, RetryProbingWithExponentailBackoff) { |
| 250 | int initial_timeout_ms = 100; |
| 251 | |
| 252 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 253 | .WillOnce(Return(true)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 254 | probing_manager_.StartProbing( |
| 255 | testNetworkHandle, testPeerAddress, std::move(socket_), |
| 256 | std::move(writer_), std::move(reader_), |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 257 | base::TimeDelta::FromMilliseconds(initial_timeout_ms), |
| 258 | bound_test_net_log_.bound()); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 259 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 260 | |
| 261 | // For expential backoff, this will try to resend: 100ms, 200ms, 400ms, 800ms, |
| 262 | // 1600ms. |
| 263 | for (int retry_count = 0; retry_count < 4; retry_count++) { |
| 264 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 265 | .WillOnce(Return(true)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 266 | int timeout_ms = (1 << retry_count) * initial_timeout_ms; |
| 267 | test_task_runner_->FastForwardBy( |
| 268 | base::TimeDelta::FromMilliseconds(timeout_ms)); |
| 269 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 270 | } |
| 271 | |
| 272 | // Move forward another 1600ms, expect probing manager will no longer send any |
| 273 | // connectivity probing packet but declare probing as failed . |
Zhongyi Shi | fdacb82 | 2018-12-11 22:49:15 | [diff] [blame] | 274 | EXPECT_CALL(session_, OnProbeFailed(testNetworkHandle, testPeerAddress)) |
| 275 | .Times(1); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 276 | int timeout_ms = (1 << 4) * initial_timeout_ms; |
| 277 | test_task_runner_->FastForwardBy( |
| 278 | base::TimeDelta::FromMilliseconds(timeout_ms)); |
| 279 | EXPECT_EQ(0u, test_task_runner_->GetPendingTaskCount()); |
| 280 | } |
| 281 | |
| 282 | TEST_F(QuicConnectivityProbingManagerTest, CancelProbing) { |
| 283 | int initial_timeout_ms = 100; |
| 284 | |
| 285 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 286 | .WillOnce(Return(true)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 287 | probing_manager_.StartProbing( |
| 288 | testNetworkHandle, testPeerAddress, std::move(socket_), |
| 289 | std::move(writer_), std::move(reader_), |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 290 | base::TimeDelta::FromMilliseconds(initial_timeout_ms), |
| 291 | bound_test_net_log_.bound()); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 292 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 293 | |
| 294 | // Fast forward initial_timeout_ms, timeout the first connectivity probing |
| 295 | // packet, introduce another probing packet to sent out with timeout set to |
| 296 | // 2 * initial_timeout_ms. |
| 297 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 298 | .WillOnce(Return(true)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 299 | test_task_runner_->FastForwardBy( |
| 300 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 301 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 302 | |
| 303 | // Fast forward initial_timeout_ms, should be no-op. |
| 304 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 305 | .Times(0); |
| 306 | test_task_runner_->FastForwardBy( |
| 307 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 308 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 309 | |
Zhongyi Shi | 7b280d0 | 2018-12-13 02:54:41 | [diff] [blame] | 310 | // Request cancel probing, manager will no longer send connectivity probes. |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 311 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, _)).Times(0); |
Zhongyi Shi | fdacb82 | 2018-12-11 22:49:15 | [diff] [blame] | 312 | EXPECT_CALL(session_, OnProbeFailed(_, _)).Times(0); |
Zhongyi Shi | 7b280d0 | 2018-12-13 02:54:41 | [diff] [blame] | 313 | probing_manager_.CancelProbing(testNetworkHandle, testPeerAddress); |
Zhongyi Shi | f4747b3 | 2018-12-14 01:19:06 | [diff] [blame] | 314 | EXPECT_FALSE( |
| 315 | probing_manager_.IsUnderProbing(testNetworkHandle, testPeerAddress)); |
| 316 | |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 317 | test_task_runner_->RunUntilIdle(); |
| 318 | } |
| 319 | |
Zhongyi Shi | 7b280d0 | 2018-12-13 02:54:41 | [diff] [blame] | 320 | TEST_F(QuicConnectivityProbingManagerTest, DoNotCancelProbing) { |
| 321 | int initial_timeout_ms = 100; |
| 322 | |
| 323 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 324 | .WillOnce(Return(true)); |
| 325 | // Start probing |testPeerAddress| on |testNetworkHandle|. |
| 326 | probing_manager_.StartProbing( |
| 327 | testNetworkHandle, testPeerAddress, std::move(socket_), |
| 328 | std::move(writer_), std::move(reader_), |
| 329 | base::TimeDelta::FromMilliseconds(initial_timeout_ms), |
| 330 | bound_test_net_log_.bound()); |
| 331 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 332 | |
| 333 | // Request cancel probing for |newPeerAddress| on |testNetworkHandle| doesn't |
| 334 | // affect the exisiting probing. |
| 335 | probing_manager_.CancelProbing(testNetworkHandle, newPeerAddress); |
Zhongyi Shi | f4747b3 | 2018-12-14 01:19:06 | [diff] [blame] | 336 | EXPECT_TRUE( |
| 337 | probing_manager_.IsUnderProbing(testNetworkHandle, testPeerAddress)); |
Zhongyi Shi | 7b280d0 | 2018-12-13 02:54:41 | [diff] [blame] | 338 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 339 | |
| 340 | for (int retry_count = 0; retry_count < 4; retry_count++) { |
| 341 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 342 | .WillOnce(Return(true)); |
| 343 | int timeout_ms = (1 << retry_count) * initial_timeout_ms; |
| 344 | test_task_runner_->FastForwardBy( |
| 345 | base::TimeDelta::FromMilliseconds(timeout_ms)); |
| 346 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 347 | } |
| 348 | |
| 349 | EXPECT_CALL(session_, OnProbeFailed(testNetworkHandle, testPeerAddress)) |
| 350 | .Times(1); |
| 351 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, _)).Times(0); |
| 352 | int timeout_ms = (1 << 4) * initial_timeout_ms; |
| 353 | test_task_runner_->FastForwardBy( |
| 354 | base::TimeDelta::FromMilliseconds(timeout_ms)); |
| 355 | EXPECT_EQ(0u, test_task_runner_->GetPendingTaskCount()); |
| 356 | } |
| 357 | |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 358 | TEST_F(QuicConnectivityProbingManagerTest, ProbingWriterError) { |
| 359 | int initial_timeout_ms = 100; |
| 360 | |
| 361 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 362 | .WillOnce(Return(true)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 363 | QuicChromiumPacketWriter* writer_ptr = writer_.get(); |
| 364 | probing_manager_.StartProbing( |
| 365 | testNetworkHandle, testPeerAddress, std::move(socket_), |
| 366 | std::move(writer_), std::move(reader_), |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 367 | base::TimeDelta::FromMilliseconds(initial_timeout_ms), |
| 368 | bound_test_net_log_.bound()); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 369 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 370 | |
| 371 | // Fast forward initial_timeout_ms, timeout the first connectivity probing |
| 372 | // packet, introduce another probing packet to sent out with timeout set to |
| 373 | // 2 * initial_timeout_ms. |
| 374 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
Zhongyi Shi | 5f587cc | 2017-11-21 23:24:17 | [diff] [blame] | 375 | .WillOnce(Return(true)); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 376 | test_task_runner_->FastForwardBy( |
| 377 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 378 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 379 | |
| 380 | // Fast forward initial_timeout_ms, should be no-op. |
| 381 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 382 | .Times(0); |
| 383 | test_task_runner_->FastForwardBy( |
| 384 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 385 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 386 | |
| 387 | // Probing packet writer received an write error, notifies manager to handle |
| 388 | // write error. Manager will notify session of the probe failure, cancel |
| 389 | // probing to prevent future connectivity probing packet to be sent. |
| 390 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, _)).Times(0); |
Zhongyi Shi | fdacb82 | 2018-12-11 22:49:15 | [diff] [blame] | 391 | EXPECT_CALL(session_, OnProbeFailed(testNetworkHandle, testPeerAddress)) |
| 392 | .Times(1); |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 393 | writer_ptr->OnWriteComplete(ERR_CONNECTION_CLOSED); |
| 394 | test_task_runner_->FastForwardBy( |
| 395 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 396 | EXPECT_EQ(0u, test_task_runner_->GetPendingTaskCount()); |
| 397 | } |
| 398 | |
Zhongyi Shi | a0b6c68 | 2018-12-17 19:01:44 | [diff] [blame] | 399 | TEST_F(QuicConnectivityProbingManagerTest, |
| 400 | ProbeServerPreferredAddressSucceeded) { |
| 401 | int initial_timeout_ms = 100; |
| 402 | |
| 403 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 404 | .WillOnce(Return(true)); |
| 405 | // A probe for server preferred address is usually initiated with an |
| 406 | // invalid network handle passed in. |
| 407 | probing_manager_.StartProbing( |
| 408 | NetworkChangeNotifier::kInvalidNetworkHandle, testPeerAddress, |
| 409 | std::move(socket_), std::move(writer_), std::move(reader_), |
| 410 | base::TimeDelta::FromMilliseconds(initial_timeout_ms), |
| 411 | bound_test_net_log_.bound()); |
| 412 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 413 | |
| 414 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 415 | .WillOnce(Return(true)); |
| 416 | |
| 417 | test_task_runner_->FastForwardBy( |
| 418 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 419 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 420 | |
| 421 | // Fast forward initial_timeout_ms, should be no-op. |
| 422 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 423 | .Times(0); |
| 424 | test_task_runner_->FastForwardBy( |
| 425 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 426 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 427 | |
| 428 | // Notify the manager a connectivity probing packet is received from |
| 429 | // testPeerAddress to |self_address_|, manager should decalre probing as |
| 430 | // successful, notify delegate and will no longer send connectivity probes. |
| 431 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 432 | .Times(0); |
| 433 | probing_manager_.OnConnectivityProbingReceived(self_address_, |
| 434 | testPeerAddress); |
| 435 | |
| 436 | // Verify that session marked <kInvalidNetworkHandle, testPeerAddress> as |
| 437 | // successfully probed. |
| 438 | EXPECT_EQ(session_.probed_network(), |
| 439 | NetworkChangeNotifier::kInvalidNetworkHandle); |
| 440 | EXPECT_EQ(session_.probed_peer_address(), testPeerAddress); |
| 441 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 442 | |
| 443 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 444 | .Times(0); |
| 445 | test_task_runner_->FastForwardBy( |
| 446 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 447 | EXPECT_EQ(0u, test_task_runner_->GetPendingTaskCount()); |
| 448 | } |
| 449 | |
| 450 | TEST_F(QuicConnectivityProbingManagerTest, ProbeServerPreferredAddressFailed) { |
| 451 | int initial_timeout_ms = 100; |
| 452 | |
| 453 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 454 | .WillOnce(Return(true)); |
| 455 | QuicChromiumPacketWriter* writer_ptr = writer_.get(); |
| 456 | // A probe for server preferred address is usually initiated with an |
| 457 | // invalid network handle passed in. |
| 458 | probing_manager_.StartProbing( |
| 459 | NetworkChangeNotifier::kInvalidNetworkHandle, testPeerAddress, |
| 460 | std::move(socket_), std::move(writer_), std::move(reader_), |
| 461 | base::TimeDelta::FromMilliseconds(initial_timeout_ms), |
| 462 | bound_test_net_log_.bound()); |
| 463 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 464 | |
| 465 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 466 | .WillOnce(Return(true)); |
| 467 | test_task_runner_->FastForwardBy( |
| 468 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 469 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 470 | |
| 471 | // Fast forward initial_timeout_ms, should be no-op. |
| 472 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress)) |
| 473 | .Times(0); |
| 474 | test_task_runner_->FastForwardBy( |
| 475 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 476 | EXPECT_EQ(1u, test_task_runner_->GetPendingTaskCount()); |
| 477 | |
| 478 | // Probing packet writer received an write error, notifies manager to handle |
| 479 | // write error. Manager will notify session of the probe failure, cancel |
| 480 | // probing to prevent future connectivity probing packet to be sent. |
| 481 | EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, _)).Times(0); |
| 482 | EXPECT_CALL(session_, |
| 483 | OnProbeFailed(NetworkChangeNotifier::kInvalidNetworkHandle, |
| 484 | testPeerAddress)) |
| 485 | .Times(1); |
| 486 | writer_ptr->OnWriteComplete(ERR_CONNECTION_CLOSED); |
| 487 | test_task_runner_->FastForwardBy( |
| 488 | base::TimeDelta::FromMilliseconds(initial_timeout_ms)); |
| 489 | EXPECT_EQ(0u, test_task_runner_->GetPendingTaskCount()); |
| 490 | } |
| 491 | |
Zhongyi Shi | 8fff75b | 2017-11-19 21:36:36 | [diff] [blame] | 492 | } // namespace test |
| 493 | } // namespace net |