Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
jkarlin | 59a81093 | 2014-10-06 18:00:43 | [diff] [blame] | 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 Orth | a3559ca | 2019-09-05 23:40:54 | [diff] [blame] | 7 | #include "base/run_loop.h" |
Paul Jensen | 1ea3109 | 2019-04-01 18:34:48 | [diff] [blame] | 8 | #include "build/build_config.h" |
Eric Orth | a3559ca | 2019-09-05 23:40:54 | [diff] [blame] | 9 | #include "net/base/mock_network_change_notifier.h" |
olli.raula | 80a44be | 2015-09-01 07:41:53 | [diff] [blame] | 10 | #include "net/base/network_interfaces.h" |
Eric Orth | a3559ca | 2019-09-05 23:40:54 | [diff] [blame] | 11 | #include "net/test/test_with_task_environment.h" |
jkarlin | 59a81093 | 2014-10-06 18:00:43 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace 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. |
| 19 | TEST(NetworkChangeNotifierTest, NetMaxBandwidthRange) { |
| 20 | NetworkChangeNotifier::ConnectionType connection_type = |
jkarlin | bce49186 | 2015-09-18 15:14:07 | [diff] [blame] | 21 | NetworkChangeNotifier::CONNECTION_NONE; |
| 22 | double max_bandwidth = 0.0; |
| 23 | NetworkChangeNotifier::GetMaxBandwidthAndConnectionType(&max_bandwidth, |
| 24 | &connection_type); |
jkarlin | 59a81093 | 2014-10-06 18:00:43 | [diff] [blame] | 25 | |
| 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 Rockot | 08252de | 2020-09-23 17:56:59 | [diff] [blame] | 57 | 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; |
jkarlin | 59a81093 | 2014-10-06 18:00:43 | [diff] [blame] | 62 | 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 | |
derekjchow | 5482d5e | 2015-01-31 01:04:51 | [diff] [blame] | 72 | TEST(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 | |
jkarlin | 0fed4eb | 2015-06-02 15:55:02 | [diff] [blame] | 101 | TEST(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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 108 | #if BUILDFLAG(IS_WIN) |
jkarlin | 0fed4eb | 2015-06-02 15:55:02 | [diff] [blame] | 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 | |
jkarlin | 5780bb0 | 2017-05-31 16:39:49 | [diff] [blame] | 117 | TEST(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 Jensen | 1ea3109 | 2019-04-01 18:34:48 | [diff] [blame] | 123 | 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}); |
jkarlin | 5780bb0 | 2017-05-31 16:39:49 | [diff] [blame] | 126 | list.push_back(interface_airdrop); |
| 127 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 128 | #if BUILDFLAG(IS_APPLE) |
jkarlin | 5780bb0 | 2017-05-31 16:39:49 | [diff] [blame] | 129 | 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 | |
| 137 | TEST(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 Jensen | 1ea3109 | 2019-04-01 18:34:48 | [diff] [blame] | 143 | 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}); |
jkarlin | 5780bb0 | 2017-05-31 16:39:49 | [diff] [blame] | 146 | list.push_back(interface_tunnel); |
| 147 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 148 | #if BUILDFLAG(IS_APPLE) |
jkarlin | 5780bb0 | 2017-05-31 16:39:49 | [diff] [blame] | 149 | 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 Jensen | 1ea3109 | 2019-04-01 18:34:48 | [diff] [blame] | 157 | TEST(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 Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 168 | #if BUILDFLAG(IS_APPLE) |
Paul Jensen | 1ea3109 | 2019-04-01 18:34:48 | [diff] [blame] | 169 | 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 | |
jkarlin | 0fed4eb | 2015-06-02 15:55:02 | [diff] [blame] | 177 | TEST(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 | |
tbansal | 880e49e7 | 2017-03-25 04:44:25 | [diff] [blame] | 195 | TEST(NetworkChangeNotifierTest, GetConnectionSubtype) { |
| 196 | // Call GetConnectionSubtype() and ensure that there is no crash. |
| 197 | NetworkChangeNotifier::GetConnectionSubtype(); |
| 198 | } |
| 199 | |
Eric Orth | a3559ca | 2019-09-05 23:40:54 | [diff] [blame] | 200 | class NetworkChangeNotifierMockedTest : public TestWithTaskEnvironment { |
Cliff Smolinsky | 81edb9c | 2021-01-17 15:59:17 | [diff] [blame] | 201 | protected: |
Eric Orth | a3559ca | 2019-09-05 23:40:54 | [diff] [blame] | 202 | test::ScopedMockNetworkChangeNotifier mock_notifier_; |
| 203 | }; |
| 204 | |
| 205 | class TestDnsObserver : public NetworkChangeNotifier::DNSObserver { |
| 206 | public: |
| 207 | void OnDNSChanged() override { ++dns_changed_calls_; } |
| 208 | |
Eric Orth | a3559ca | 2019-09-05 23:40:54 | [diff] [blame] | 209 | int dns_changed_calls() const { return dns_changed_calls_; } |
| 210 | |
| 211 | private: |
| 212 | int dns_changed_calls_ = 0; |
| 213 | }; |
| 214 | |
| 215 | TEST_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 Huang | aee0a42 | 2022-11-01 18:21:35 | [diff] [blame] | 229 | class 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 Smolinsky | 81edb9c | 2021-01-17 15:59:17 | [diff] [blame] | 249 | TEST_F(NetworkChangeNotifierMockedTest, TriggerConnectionCostChange) { |
| 250 | TestConnectionCostObserver observer; |
| 251 | NetworkChangeNotifier::AddConnectionCostObserver(&observer); |
| 252 | |
Kuan Huang | aee0a42 | 2022-11-01 18:21:35 | [diff] [blame] | 253 | ASSERT_EQ(0, observer.cost_changed_calls()); |
Cliff Smolinsky | 81edb9c | 2021-01-17 15:59:17 | [diff] [blame] | 254 | |
| 255 | NetworkChangeNotifier::NotifyObserversOfConnectionCostChangeForTests( |
| 256 | NetworkChangeNotifier::CONNECTION_COST_METERED); |
| 257 | base::RunLoop().RunUntilIdle(); |
| 258 | |
Kuan Huang | aee0a42 | 2022-11-01 18:21:35 | [diff] [blame] | 259 | EXPECT_EQ(1, observer.cost_changed_calls()); |
Cliff Smolinsky | 81edb9c | 2021-01-17 15:59:17 | [diff] [blame] | 260 | 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 Huang | aee0a42 | 2022-11-01 18:21:35 | [diff] [blame] | 268 | EXPECT_EQ(1, observer.cost_changed_calls()); |
Cliff Smolinsky | 81edb9c | 2021-01-17 15:59:17 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | TEST_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 | |
| 290 | class 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 | |
| 303 | TEST_F(NetworkChangeNotifierConnectionCostTest, GetConnectionCost) { |
| 304 | EXPECT_NE(NetworkChangeNotifier::ConnectionCost::CONNECTION_COST_UNKNOWN, |
| 305 | NetworkChangeNotifier::GetConnectionCost()); |
| 306 | } |
| 307 | |
| 308 | TEST_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 | |
jkarlin | 59a81093 | 2014-10-06 18:00:43 | [diff] [blame] | 317 | } // namespace net |