blob: b5c2d8624992e98c1bfe0224a14f79e6ddc77544 [file] [log] [blame]
jkarlin59a810932014-10-06 18:00:431// Copyright (c) 2014 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
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
108#if defined(OS_WIN)
109 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
Avi Drissman25292af62020-07-29 21:57:11128#if defined(OS_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
Avi Drissman25292af62020-07-29 21:57:11148#if defined(OS_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
Avi Drissman25292af62020-07-29 21:57:11168#if defined(OS_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 {
201 private:
202 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
jkarlin59a810932014-10-06 18:00:43229} // namespace net