blob: 2d4f7fe0bab066df3ebfce35ef93a34cb9814369 [file] [log] [blame]
Zhongyi Shi8fff75b2017-11-19 21:36:361// 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 Hamiltona3ee93a72018-08-01 22:03:085#include "net/quic/quic_connectivity_probing_manager.h"
Zhongyi Shi8fff75b2017-11-19 21:36:366
Ryan Sleevib8d7ea02018-05-07 20:01:017#include "base/stl_util.h"
Zhongyi Shi8fff75b2017-11-19 21:36:368#include "base/test/test_mock_time_task_runner.h"
9#include "net/log/test_net_log.h"
Zhongyi Shi8fff75b2017-11-19 21:36:3610#include "net/socket/socket_test_util.h"
11#include "net/test/gtest_util.h"
Victor Vasiliev6bb59d22019-03-08 21:34:5112#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 Shi8fff75b2017-11-19 21:36:3614#include "testing/gmock/include/gmock/gmock.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17using testing::_;
Zhongyi Shi5f587cc2017-11-21 23:24:1718using testing::Return;
Zhongyi Shi8fff75b2017-11-19 21:36:3619
20namespace net {
21namespace test {
22namespace {
23
Zhongyi Shi7b280d02018-12-13 02:54:4124const NetworkChangeNotifier::NetworkHandle testNetworkHandle = 1;
25
Ryan Hamilton8d9ee76e2018-05-29 23:52:5226const IPEndPoint kIpEndPoint =
27 IPEndPoint(IPAddress::IPv4AllZeros(), quic::test::kTestPort);
Ryan Hamilton8d9ee76e2018-05-29 23:52:5228const quic::QuicSocketAddress testPeerAddress =
29 quic::QuicSocketAddress(quic::QuicSocketAddressImpl(kIpEndPoint));
Zhongyi Shi8fff75b2017-11-19 21:36:3630
Zhongyi Shi7b280d02018-12-13 02:54:4131const IPEndPoint newIpEndPoint =
32 IPEndPoint(IPAddress::IPv4AllZeros(), quic::test::kTestPort + 1);
33const quic::QuicSocketAddress newPeerAddress =
34 quic::QuicSocketAddress(quic::QuicSocketAddressImpl(newIpEndPoint));
Zhongyi Shi8fff75b2017-11-19 21:36:3635} // anonymous namespace
36
37class MockQuicChromiumClientSession
38 : public QuicConnectivityProbingManager::Delegate,
39 public QuicChromiumPacketReader::Visitor {
40 public:
41 MockQuicChromiumClientSession()
Zhongyi Shia0b6c682018-12-17 19:01:4442 : probed_network_(NetworkChangeNotifier::kInvalidNetworkHandle) {}
Zhongyi Shi8fff75b2017-11-19 21:36:3643 ~MockQuicChromiumClientSession() override {}
44
45 // QuicChromiumPacketReader::Visitor interface.
46 MOCK_METHOD2(OnReadError,
47 void(int result, const DatagramClientSocket* socket));
48
49 MOCK_METHOD3(OnPacket,
Ryan Hamilton8d9ee76e2018-05-29 23:52:5250 bool(const quic::QuicReceivedPacket& packet,
51 const quic::QuicSocketAddress& local_address,
52 const quic::QuicSocketAddress& peer_address));
Zhongyi Shi8fff75b2017-11-19 21:36:3653
Zhongyi Shifdacb822018-12-11 22:49:1554 MOCK_METHOD2(OnProbeFailed,
55 void(NetworkChangeNotifier::NetworkHandle network,
56 const quic::QuicSocketAddress& peer_address));
Zhongyi Shi8fff75b2017-11-19 21:36:3657
58 MOCK_METHOD2(OnSendConnectivityProbingPacket,
Zhongyi Shi5f587cc2017-11-21 23:24:1759 bool(QuicChromiumPacketWriter* writer,
Ryan Hamilton8d9ee76e2018-05-29 23:52:5260 const quic::QuicSocketAddress& peer_address));
Zhongyi Shi8fff75b2017-11-19 21:36:3661
Zhongyi Shifdacb822018-12-11 22:49:1562 void OnProbeSucceeded(
Zhongyi Shi8fff75b2017-11-19 21:36:3663 NetworkChangeNotifier::NetworkHandle network,
Zhongyi Shifdacb822018-12-11 22:49:1564 const quic::QuicSocketAddress& peer_address,
Ryan Hamilton8d9ee76e2018-05-29 23:52:5265 const quic::QuicSocketAddress& self_address,
Zhongyi Shi8fff75b2017-11-19 21:36:3666 std::unique_ptr<DatagramClientSocket> socket,
67 std::unique_ptr<QuicChromiumPacketWriter> writer,
68 std::unique_ptr<QuicChromiumPacketReader> reader) override {
Zhongyi Shia0b6c682018-12-17 19:01:4469 probed_network_ = network;
70 probed_peer_address_ = peer_address;
Zhongyi Shi8fff75b2017-11-19 21:36:3671 }
72
Zhongyi Shia0b6c682018-12-17 19:01:4473 NetworkChangeNotifier::NetworkHandle probed_network() const {
74 return probed_network_;
75 }
76
77 quic::QuicSocketAddress probed_peer_address() const {
78 return probed_peer_address_;
Zhongyi Shi8fff75b2017-11-19 21:36:3679 }
80
81 private:
Zhongyi Shia0b6c682018-12-17 19:01:4482 NetworkChangeNotifier::NetworkHandle probed_network_;
83 quic::QuicSocketAddress probed_peer_address_;
Zhongyi Shi8fff75b2017-11-19 21:36:3684
85 DISALLOW_COPY_AND_ASSIGN(MockQuicChromiumClientSession);
86};
87
88class 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 Sleevib8d7ea02018-05-07 20:01:0196 new SequencedSocketData(base::make_span(default_read_.get(), 1),
97 base::span<MockWrite>())) {
Zhongyi Shi8fff75b2017-11-19 21:36:3698 socket_factory_.AddSocketDataProvider(socket_data_.get());
99 // Create a connected socket for probing.
100 socket_ = socket_factory_.CreateDatagramClientSocket(
Sergey Ulanovcbdfc8852018-03-16 20:13:28101 DatagramSocket::DEFAULT_BIND, &net_log_, NetLogSource());
Zhongyi Shi8fff75b2017-11-19 21:36:36102 EXPECT_THAT(socket_->Connect(kIpEndPoint), IsOk());
103 IPEndPoint self_address;
104 socket_->GetLocalAddress(&self_address);
Ryan Hamilton8d9ee76e2018-05-29 23:52:52105 self_address_ =
106 quic::QuicSocketAddress(quic::QuicSocketAddressImpl(self_address));
Zhongyi Shi8fff75b2017-11-19 21:36:36107 // Create packet writer and reader for probing.
Ryan Hamilton9edcf1a2017-11-22 05:55:17108 writer_.reset(
109 new QuicChromiumPacketWriter(socket_.get(), test_task_runner_.get()));
Zhongyi Shi8fff75b2017-11-19 21:36:36110 reader_.reset(new QuicChromiumPacketReader(
111 socket_.get(), &clock_, &session_, kQuicYieldAfterPacketsRead,
Ryan Hamilton8d9ee76e2018-05-29 23:52:52112 quic::QuicTime::Delta::FromMilliseconds(
113 kQuicYieldAfterDurationMilliseconds),
Zhongyi Shi8fff75b2017-11-19 21:36:36114 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 Hamilton8d9ee76e2018-05-29 23:52:52130 quic::QuicSocketAddress self_address_;
Zhongyi Shi8fff75b2017-11-19 21:36:36131
Ryan Hamilton8d9ee76e2018-05-29 23:52:52132 quic::MockClock clock_;
Zhongyi Shi8fff75b2017-11-19 21:36:36133 MockClientSocketFactory socket_factory_;
134 TestNetLog net_log_;
135 BoundTestNetLog bound_test_net_log_;
136
137 DISALLOW_COPY_AND_ASSIGN(QuicConnectivityProbingManagerTest);
138};
139
140TEST_F(QuicConnectivityProbingManagerTest, ReceiveProbingResponseOnSamePath) {
141 int initial_timeout_ms = 100;
142
143 EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress))
Zhongyi Shi5f587cc2017-11-21 23:24:17144 .WillOnce(Return(true));
Zhongyi Shi8fff75b2017-11-19 21:36:36145 probing_manager_.StartProbing(
146 testNetworkHandle, testPeerAddress, std::move(socket_),
147 std::move(writer_), std::move(reader_),
Zhongyi Shi5f587cc2017-11-21 23:24:17148 base::TimeDelta::FromMilliseconds(initial_timeout_ms),
149 bound_test_net_log_.bound());
Zhongyi Shi8fff75b2017-11-19 21:36:36150 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 Shi5f587cc2017-11-21 23:24:17156 .WillOnce(Return(true));
157
Zhongyi Shi8fff75b2017-11-19 21:36:36158 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 Shia0b6c682018-12-17 19:01:44177 EXPECT_EQ(session_.probed_network(), testNetworkHandle);
Zhongyi Shi8fff75b2017-11-19 21:36:36178 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
188TEST_F(QuicConnectivityProbingManagerTest,
189 ReceiveProbingResponseOnDifferentPath) {
190 int initial_timeout_ms = 100;
191
192 EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress))
Zhongyi Shi5f587cc2017-11-21 23:24:17193 .WillOnce(Return(true));
Zhongyi Shi8fff75b2017-11-19 21:36:36194 probing_manager_.StartProbing(
195 testNetworkHandle, testPeerAddress, std::move(socket_),
196 std::move(writer_), std::move(reader_),
Zhongyi Shi5f587cc2017-11-21 23:24:17197 base::TimeDelta::FromMilliseconds(initial_timeout_ms),
198 bound_test_net_log_.bound());
Zhongyi Shi8fff75b2017-11-19 21:36:36199 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 Shi5f587cc2017-11-21 23:24:17205 .WillOnce(Return(true));
Zhongyi Shi8fff75b2017-11-19 21:36:36206 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 Hamilton8d9ee76e2018-05-29 23:52:52222 probing_manager_.OnConnectivityProbingReceived(quic::QuicSocketAddress(),
Zhongyi Shi8fff75b2017-11-19 21:36:36223 testPeerAddress);
Zhongyi Shia0b6c682018-12-17 19:01:44224 EXPECT_NE(session_.probed_network(), testNetworkHandle);
Zhongyi Shi8fff75b2017-11-19 21:36:36225 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 Shi5f587cc2017-11-21 23:24:17230 .WillOnce(Return(true));
Zhongyi Shi8fff75b2017-11-19 21:36:36231 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 Shia0b6c682018-12-17 19:01:44240 EXPECT_EQ(session_.probed_network(), testNetworkHandle);
Zhongyi Shi8fff75b2017-11-19 21:36:36241 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
249TEST_F(QuicConnectivityProbingManagerTest, RetryProbingWithExponentailBackoff) {
250 int initial_timeout_ms = 100;
251
252 EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress))
Zhongyi Shi5f587cc2017-11-21 23:24:17253 .WillOnce(Return(true));
Zhongyi Shi8fff75b2017-11-19 21:36:36254 probing_manager_.StartProbing(
255 testNetworkHandle, testPeerAddress, std::move(socket_),
256 std::move(writer_), std::move(reader_),
Zhongyi Shi5f587cc2017-11-21 23:24:17257 base::TimeDelta::FromMilliseconds(initial_timeout_ms),
258 bound_test_net_log_.bound());
Zhongyi Shi8fff75b2017-11-19 21:36:36259 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 Shi5f587cc2017-11-21 23:24:17265 .WillOnce(Return(true));
Zhongyi Shi8fff75b2017-11-19 21:36:36266 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 Shifdacb822018-12-11 22:49:15274 EXPECT_CALL(session_, OnProbeFailed(testNetworkHandle, testPeerAddress))
275 .Times(1);
Zhongyi Shi8fff75b2017-11-19 21:36:36276 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
282TEST_F(QuicConnectivityProbingManagerTest, CancelProbing) {
283 int initial_timeout_ms = 100;
284
285 EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress))
Zhongyi Shi5f587cc2017-11-21 23:24:17286 .WillOnce(Return(true));
Zhongyi Shi8fff75b2017-11-19 21:36:36287 probing_manager_.StartProbing(
288 testNetworkHandle, testPeerAddress, std::move(socket_),
289 std::move(writer_), std::move(reader_),
Zhongyi Shi5f587cc2017-11-21 23:24:17290 base::TimeDelta::FromMilliseconds(initial_timeout_ms),
291 bound_test_net_log_.bound());
Zhongyi Shi8fff75b2017-11-19 21:36:36292 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 Shi5f587cc2017-11-21 23:24:17298 .WillOnce(Return(true));
Zhongyi Shi8fff75b2017-11-19 21:36:36299 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 Shi7b280d02018-12-13 02:54:41310 // Request cancel probing, manager will no longer send connectivity probes.
Zhongyi Shi8fff75b2017-11-19 21:36:36311 EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, _)).Times(0);
Zhongyi Shifdacb822018-12-11 22:49:15312 EXPECT_CALL(session_, OnProbeFailed(_, _)).Times(0);
Zhongyi Shi7b280d02018-12-13 02:54:41313 probing_manager_.CancelProbing(testNetworkHandle, testPeerAddress);
Zhongyi Shif4747b32018-12-14 01:19:06314 EXPECT_FALSE(
315 probing_manager_.IsUnderProbing(testNetworkHandle, testPeerAddress));
316
Zhongyi Shi8fff75b2017-11-19 21:36:36317 test_task_runner_->RunUntilIdle();
318}
319
Zhongyi Shi7b280d02018-12-13 02:54:41320TEST_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 Shif4747b32018-12-14 01:19:06336 EXPECT_TRUE(
337 probing_manager_.IsUnderProbing(testNetworkHandle, testPeerAddress));
Zhongyi Shi7b280d02018-12-13 02:54:41338 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 Shi8fff75b2017-11-19 21:36:36358TEST_F(QuicConnectivityProbingManagerTest, ProbingWriterError) {
359 int initial_timeout_ms = 100;
360
361 EXPECT_CALL(session_, OnSendConnectivityProbingPacket(_, testPeerAddress))
Zhongyi Shi5f587cc2017-11-21 23:24:17362 .WillOnce(Return(true));
Zhongyi Shi8fff75b2017-11-19 21:36:36363 QuicChromiumPacketWriter* writer_ptr = writer_.get();
364 probing_manager_.StartProbing(
365 testNetworkHandle, testPeerAddress, std::move(socket_),
366 std::move(writer_), std::move(reader_),
Zhongyi Shi5f587cc2017-11-21 23:24:17367 base::TimeDelta::FromMilliseconds(initial_timeout_ms),
368 bound_test_net_log_.bound());
Zhongyi Shi8fff75b2017-11-19 21:36:36369 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 Shi5f587cc2017-11-21 23:24:17375 .WillOnce(Return(true));
Zhongyi Shi8fff75b2017-11-19 21:36:36376 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 Shifdacb822018-12-11 22:49:15391 EXPECT_CALL(session_, OnProbeFailed(testNetworkHandle, testPeerAddress))
392 .Times(1);
Zhongyi Shi8fff75b2017-11-19 21:36:36393 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 Shia0b6c682018-12-17 19:01:44399TEST_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
450TEST_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 Shi8fff75b2017-11-19 21:36:36492} // namespace test
493} // namespace net