blob: 3f3fbd6ac9a9ea7f7a49460a2683741e3e47d87f [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2014 The Chromium Authors
jkarlin59a810932014-10-06 18:00:432// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/base/network_change_notifier.h"
6
Eric Ortha3559ca2019-09-05 23:40:547#include "base/run_loop.h"
Paul Jensen1ea31092019-04-01 18:34:488#include "build/build_config.h"
Eric Ortha3559ca2019-09-05 23:40:549#include "net/base/mock_network_change_notifier.h"
olli.raula80a44be2015-09-01 07:41:5310#include "net/base/network_interfaces.h"
Eric Ortha3559ca2019-09-05 23:40:5411#include "net/test/test_with_task_environment.h"
jkarlin59a810932014-10-06 18:00:4312#include "testing/gtest/include/gtest/gtest.h"
13
14namespace net {
15
16// Note: This test is subject to the host's OS and network connection. This test
17// is not future-proof. New standards will come about necessitating the need to
18// alter the ranges of these tests.
19TEST(NetworkChangeNotifierTest, NetMaxBandwidthRange) {
20 NetworkChangeNotifier::ConnectionType connection_type =
jkarlinbce491862015-09-18 15:14:0721 NetworkChangeNotifier::CONNECTION_NONE;
22 double max_bandwidth = 0.0;
23 NetworkChangeNotifier::GetMaxBandwidthAndConnectionType(&max_bandwidth,
24 &connection_type);
jkarlin59a810932014-10-06 18:00:4325
26 // Always accept infinity as it's the default value if the bandwidth is
27 // unknown.
28 if (max_bandwidth == std::numeric_limits<double>::infinity()) {
29 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, connection_type);
30 return;
31 }
32
33 switch (connection_type) {
34 case NetworkChangeNotifier::CONNECTION_UNKNOWN:
35 EXPECT_EQ(std::numeric_limits<double>::infinity(), max_bandwidth);
36 break;
37 case NetworkChangeNotifier::CONNECTION_ETHERNET:
38 EXPECT_GE(10.0, max_bandwidth);
39 EXPECT_LE(10000.0, max_bandwidth);
40 break;
41 case NetworkChangeNotifier::CONNECTION_WIFI:
42 EXPECT_GE(1.0, max_bandwidth);
43 EXPECT_LE(7000.0, max_bandwidth);
44 break;
45 case NetworkChangeNotifier::CONNECTION_2G:
46 EXPECT_GE(0.01, max_bandwidth);
47 EXPECT_LE(0.384, max_bandwidth);
48 break;
49 case NetworkChangeNotifier::CONNECTION_3G:
50 EXPECT_GE(2.0, max_bandwidth);
51 EXPECT_LE(42.0, max_bandwidth);
52 break;
53 case NetworkChangeNotifier::CONNECTION_4G:
54 EXPECT_GE(100.0, max_bandwidth);
55 EXPECT_LE(100.0, max_bandwidth);
56 break;
Ken Rockot08252de2020-09-23 17:56:5957 case NetworkChangeNotifier::CONNECTION_5G:
58 // TODO(crbug.com/1127134): Expect proper bounds once we have introduced
59 // subtypes for 5G connections.
60 EXPECT_EQ(std::numeric_limits<double>::infinity(), max_bandwidth);
61 break;
jkarlin59a810932014-10-06 18:00:4362 case NetworkChangeNotifier::CONNECTION_NONE:
63 EXPECT_EQ(0.0, max_bandwidth);
64 break;
65 case NetworkChangeNotifier::CONNECTION_BLUETOOTH:
66 EXPECT_GE(1.0, max_bandwidth);
67 EXPECT_LE(24.0, max_bandwidth);
68 break;
69 }
70}
71
derekjchow5482d5e2015-01-31 01:04:5172TEST(NetworkChangeNotifierTest, ConnectionTypeFromInterfaceList) {
73 NetworkInterfaceList list;
74
75 // Test empty list.
76 EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list),
77 NetworkChangeNotifier::CONNECTION_NONE);
78
79 for (int i = NetworkChangeNotifier::CONNECTION_UNKNOWN;
80 i <= NetworkChangeNotifier::CONNECTION_LAST; i++) {
81 // Check individual types.
82 NetworkInterface interface;
83 interface.type = static_cast<NetworkChangeNotifier::ConnectionType>(i);
84 list.clear();
85 list.push_back(interface);
86 EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list), i);
87 // Check two types.
88 for (int j = NetworkChangeNotifier::CONNECTION_UNKNOWN;
89 j <= NetworkChangeNotifier::CONNECTION_LAST; j++) {
90 list.clear();
91 interface.type = static_cast<NetworkChangeNotifier::ConnectionType>(i);
92 list.push_back(interface);
93 interface.type = static_cast<NetworkChangeNotifier::ConnectionType>(j);
94 list.push_back(interface);
95 EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list),
96 i == j ? i : NetworkChangeNotifier::CONNECTION_UNKNOWN);
97 }
98 }
99}
100
jkarlin0fed4eb2015-06-02 15:55:02101TEST(NetworkChangeNotifierTest, IgnoreTeredoOnWindows) {
102 NetworkInterfaceList list;
103 NetworkInterface interface_teredo;
104 interface_teredo.type = NetworkChangeNotifier::CONNECTION_ETHERNET;
105 interface_teredo.friendly_name = "Teredo Tunneling Pseudo-Interface";
106 list.push_back(interface_teredo);
107
Xiaohan Wang2a6845b2022-01-08 04:40:57108#if BUILDFLAG(IS_WIN)
jkarlin0fed4eb2015-06-02 15:55:02109 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
110 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list));
111#else
112 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_ETHERNET,
113 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list));
114#endif
115}
116
jkarlin5780bb02017-05-31 16:39:49117TEST(NetworkChangeNotifierTest, IgnoreAirdropOnMac) {
118 NetworkInterfaceList list;
119 NetworkInterface interface_airdrop;
120 interface_airdrop.type = NetworkChangeNotifier::CONNECTION_ETHERNET;
121 interface_airdrop.name = "awdl0";
122 interface_airdrop.friendly_name = "awdl0";
Paul Jensen1ea31092019-04-01 18:34:48123 interface_airdrop.address =
124 // Link-local IPv6 address
125 IPAddress({0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4});
jkarlin5780bb02017-05-31 16:39:49126 list.push_back(interface_airdrop);
127
Xiaohan Wang2a6845b2022-01-08 04:40:57128#if BUILDFLAG(IS_APPLE)
jkarlin5780bb02017-05-31 16:39:49129 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
130 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list));
131#else
132 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_ETHERNET,
133 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list));
134#endif
135}
136
137TEST(NetworkChangeNotifierTest, IgnoreTunnelsOnMac) {
138 NetworkInterfaceList list;
139 NetworkInterface interface_tunnel;
140 interface_tunnel.type = NetworkChangeNotifier::CONNECTION_ETHERNET;
141 interface_tunnel.name = "utun0";
142 interface_tunnel.friendly_name = "utun0";
Paul Jensen1ea31092019-04-01 18:34:48143 interface_tunnel.address =
144 // Link-local IPv6 address
145 IPAddress({0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 3, 2, 1});
jkarlin5780bb02017-05-31 16:39:49146 list.push_back(interface_tunnel);
147
Xiaohan Wang2a6845b2022-01-08 04:40:57148#if BUILDFLAG(IS_APPLE)
jkarlin5780bb02017-05-31 16:39:49149 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
150 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list));
151#else
152 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_ETHERNET,
153 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list));
154#endif
155}
156
Paul Jensen1ea31092019-04-01 18:34:48157TEST(NetworkChangeNotifierTest, IgnoreDisconnectedEthernetOnMac) {
158 NetworkInterfaceList list;
159 NetworkInterface interface_ethernet;
160 interface_ethernet.type = NetworkChangeNotifier::CONNECTION_ETHERNET;
161 interface_ethernet.name = "en5";
162 interface_ethernet.friendly_name = "en5";
163 interface_ethernet.address =
164 // Link-local IPv6 address
165 IPAddress({0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 2, 3});
166 list.push_back(interface_ethernet);
167
Xiaohan Wang2a6845b2022-01-08 04:40:57168#if BUILDFLAG(IS_APPLE)
Paul Jensen1ea31092019-04-01 18:34:48169 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
170 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list));
171#else
172 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_ETHERNET,
173 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list));
174#endif
175}
176
jkarlin0fed4eb2015-06-02 15:55:02177TEST(NetworkChangeNotifierTest, IgnoreVMInterfaces) {
178 NetworkInterfaceList list;
179 NetworkInterface interface_vmnet_linux;
180 interface_vmnet_linux.type = NetworkChangeNotifier::CONNECTION_ETHERNET;
181 interface_vmnet_linux.name = "vmnet1";
182 interface_vmnet_linux.friendly_name = "vmnet1";
183 list.push_back(interface_vmnet_linux);
184
185 NetworkInterface interface_vmnet_win;
186 interface_vmnet_win.type = NetworkChangeNotifier::CONNECTION_ETHERNET;
187 interface_vmnet_win.name = "virtualdevice";
188 interface_vmnet_win.friendly_name = "VMware Network Adapter VMnet1";
189 list.push_back(interface_vmnet_win);
190
191 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
192 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list));
193}
194
tbansal880e49e72017-03-25 04:44:25195TEST(NetworkChangeNotifierTest, GetConnectionSubtype) {
196 // Call GetConnectionSubtype() and ensure that there is no crash.
197 NetworkChangeNotifier::GetConnectionSubtype();
198}
199
Eric Ortha3559ca2019-09-05 23:40:54200class NetworkChangeNotifierMockedTest : public TestWithTaskEnvironment {
Cliff Smolinsky81edb9c2021-01-17 15:59:17201 protected:
Eric Ortha3559ca2019-09-05 23:40:54202 test::ScopedMockNetworkChangeNotifier mock_notifier_;
203};
204
205class TestDnsObserver : public NetworkChangeNotifier::DNSObserver {
206 public:
207 void OnDNSChanged() override { ++dns_changed_calls_; }
208
Eric Ortha3559ca2019-09-05 23:40:54209 int dns_changed_calls() const { return dns_changed_calls_; }
210
211 private:
212 int dns_changed_calls_ = 0;
213};
214
215TEST_F(NetworkChangeNotifierMockedTest, TriggerNonSystemDnsChange) {
216 TestDnsObserver observer;
217 NetworkChangeNotifier::AddDNSObserver(&observer);
218
219 ASSERT_EQ(0, observer.dns_changed_calls());
220
221 NetworkChangeNotifier::TriggerNonSystemDnsChange();
222 base::RunLoop().RunUntilIdle();
223
224 EXPECT_EQ(1, observer.dns_changed_calls());
225
226 NetworkChangeNotifier::RemoveDNSObserver(&observer);
227}
228
Kuan Huangaee0a422022-11-01 18:21:35229class TestConnectionCostObserver
230 : public NetworkChangeNotifier::ConnectionCostObserver {
231 public:
232 void OnConnectionCostChanged(
233 NetworkChangeNotifier::ConnectionCost cost) override {
234 cost_changed_inputs_.push_back(cost);
235 ++cost_changed_calls_;
236 }
237
238 int cost_changed_calls() const { return cost_changed_calls_; }
239 std::vector<NetworkChangeNotifier::ConnectionCost> cost_changed_inputs()
240 const {
241 return cost_changed_inputs_;
242 }
243
244 private:
245 int cost_changed_calls_ = 0;
246 std::vector<NetworkChangeNotifier::ConnectionCost> cost_changed_inputs_;
247};
248
Cliff Smolinsky81edb9c2021-01-17 15:59:17249TEST_F(NetworkChangeNotifierMockedTest, TriggerConnectionCostChange) {
250 TestConnectionCostObserver observer;
251 NetworkChangeNotifier::AddConnectionCostObserver(&observer);
252
Kuan Huangaee0a422022-11-01 18:21:35253 ASSERT_EQ(0, observer.cost_changed_calls());
Cliff Smolinsky81edb9c2021-01-17 15:59:17254
255 NetworkChangeNotifier::NotifyObserversOfConnectionCostChangeForTests(
256 NetworkChangeNotifier::CONNECTION_COST_METERED);
257 base::RunLoop().RunUntilIdle();
258
Kuan Huangaee0a422022-11-01 18:21:35259 EXPECT_EQ(1, observer.cost_changed_calls());
Cliff Smolinsky81edb9c2021-01-17 15:59:17260 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_COST_METERED,
261 observer.cost_changed_inputs()[0]);
262
263 NetworkChangeNotifier::RemoveConnectionCostObserver(&observer);
264 NetworkChangeNotifier::NotifyObserversOfConnectionCostChangeForTests(
265 NetworkChangeNotifier::CONNECTION_COST_UNMETERED);
266 base::RunLoop().RunUntilIdle();
267
Kuan Huangaee0a422022-11-01 18:21:35268 EXPECT_EQ(1, observer.cost_changed_calls());
Cliff Smolinsky81edb9c2021-01-17 15:59:17269}
270
271TEST_F(NetworkChangeNotifierMockedTest, ConnectionCostDefaultsToCellular) {
272 mock_notifier_.mock_network_change_notifier()
273 ->SetUseDefaultConnectionCostImplementation(true);
274
275 mock_notifier_.mock_network_change_notifier()->SetConnectionType(
276 NetworkChangeNotifier::CONNECTION_4G);
277 EXPECT_TRUE(NetworkChangeNotifier::IsConnectionCellular(
278 NetworkChangeNotifier::GetConnectionType()));
279 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_COST_METERED,
280 NetworkChangeNotifier::GetConnectionCost());
281
282 mock_notifier_.mock_network_change_notifier()->SetConnectionType(
283 NetworkChangeNotifier::CONNECTION_WIFI);
284 EXPECT_FALSE(NetworkChangeNotifier::IsConnectionCellular(
285 NetworkChangeNotifier::GetConnectionType()));
286 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_COST_UNMETERED,
287 NetworkChangeNotifier::GetConnectionCost());
288}
289
290class NetworkChangeNotifierConnectionCostTest : public TestWithTaskEnvironment {
291 public:
292 void SetUp() override {
293 network_change_notifier_ = NetworkChangeNotifier::CreateIfNeeded();
294 }
295
296 private:
297 // Allows creating a new NetworkChangeNotifier. Must be created before
298 // |network_change_notifier_| and destroyed after it to avoid DCHECK failures.
299 NetworkChangeNotifier::DisableForTest disable_for_test_;
300 std::unique_ptr<NetworkChangeNotifier> network_change_notifier_;
301};
302
303TEST_F(NetworkChangeNotifierConnectionCostTest, GetConnectionCost) {
304 EXPECT_NE(NetworkChangeNotifier::ConnectionCost::CONNECTION_COST_UNKNOWN,
305 NetworkChangeNotifier::GetConnectionCost());
306}
307
308TEST_F(NetworkChangeNotifierConnectionCostTest, AddObserver) {
309 TestConnectionCostObserver observer;
310 EXPECT_NO_FATAL_FAILURE(
311 NetworkChangeNotifier::AddConnectionCostObserver(&observer));
312 // RunUntilIdle because the secondary work resulting from adding an observer
313 // may be posted to a task queue.
314 base::RunLoop().RunUntilIdle();
315}
316
jkarlin59a810932014-10-06 18:00:43317} // namespace net