jkarlin | 59a81093 | 2014-10-06 18:00:43 | [diff] [blame] | 1 | // 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 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; |
| 57 | case NetworkChangeNotifier::CONNECTION_NONE: |
| 58 | EXPECT_EQ(0.0, max_bandwidth); |
| 59 | break; |
| 60 | case NetworkChangeNotifier::CONNECTION_BLUETOOTH: |
| 61 | EXPECT_GE(1.0, max_bandwidth); |
| 62 | EXPECT_LE(24.0, max_bandwidth); |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
derekjchow | 5482d5e | 2015-01-31 01:04:51 | [diff] [blame] | 67 | TEST(NetworkChangeNotifierTest, ConnectionTypeFromInterfaceList) { |
| 68 | NetworkInterfaceList list; |
| 69 | |
| 70 | // Test empty list. |
| 71 | EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list), |
| 72 | NetworkChangeNotifier::CONNECTION_NONE); |
| 73 | |
| 74 | for (int i = NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 75 | i <= NetworkChangeNotifier::CONNECTION_LAST; i++) { |
| 76 | // Check individual types. |
| 77 | NetworkInterface interface; |
| 78 | interface.type = static_cast<NetworkChangeNotifier::ConnectionType>(i); |
| 79 | list.clear(); |
| 80 | list.push_back(interface); |
| 81 | EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list), i); |
| 82 | // Check two types. |
| 83 | for (int j = NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 84 | j <= NetworkChangeNotifier::CONNECTION_LAST; j++) { |
| 85 | list.clear(); |
| 86 | interface.type = static_cast<NetworkChangeNotifier::ConnectionType>(i); |
| 87 | list.push_back(interface); |
| 88 | interface.type = static_cast<NetworkChangeNotifier::ConnectionType>(j); |
| 89 | list.push_back(interface); |
| 90 | EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list), |
| 91 | i == j ? i : NetworkChangeNotifier::CONNECTION_UNKNOWN); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
jkarlin | 0fed4eb | 2015-06-02 15:55:02 | [diff] [blame] | 96 | TEST(NetworkChangeNotifierTest, IgnoreTeredoOnWindows) { |
| 97 | NetworkInterfaceList list; |
| 98 | NetworkInterface interface_teredo; |
| 99 | interface_teredo.type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 100 | interface_teredo.friendly_name = "Teredo Tunneling Pseudo-Interface"; |
| 101 | list.push_back(interface_teredo); |
| 102 | |
| 103 | #if defined(OS_WIN) |
| 104 | EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 105 | NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list)); |
| 106 | #else |
| 107 | EXPECT_EQ(NetworkChangeNotifier::CONNECTION_ETHERNET, |
| 108 | NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list)); |
| 109 | #endif |
| 110 | } |
| 111 | |
jkarlin | 5780bb0 | 2017-05-31 16:39:49 | [diff] [blame] | 112 | TEST(NetworkChangeNotifierTest, IgnoreAirdropOnMac) { |
| 113 | NetworkInterfaceList list; |
| 114 | NetworkInterface interface_airdrop; |
| 115 | interface_airdrop.type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 116 | interface_airdrop.name = "awdl0"; |
| 117 | interface_airdrop.friendly_name = "awdl0"; |
Paul Jensen | 1ea3109 | 2019-04-01 18:34:48 | [diff] [blame] | 118 | interface_airdrop.address = |
| 119 | // Link-local IPv6 address |
| 120 | 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] | 121 | list.push_back(interface_airdrop); |
| 122 | |
Avi Drissman | 25292af6 | 2020-07-29 21:57:11 | [diff] [blame] | 123 | #if defined(OS_APPLE) |
jkarlin | 5780bb0 | 2017-05-31 16:39:49 | [diff] [blame] | 124 | EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 125 | NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list)); |
| 126 | #else |
| 127 | EXPECT_EQ(NetworkChangeNotifier::CONNECTION_ETHERNET, |
| 128 | NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list)); |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | TEST(NetworkChangeNotifierTest, IgnoreTunnelsOnMac) { |
| 133 | NetworkInterfaceList list; |
| 134 | NetworkInterface interface_tunnel; |
| 135 | interface_tunnel.type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 136 | interface_tunnel.name = "utun0"; |
| 137 | interface_tunnel.friendly_name = "utun0"; |
Paul Jensen | 1ea3109 | 2019-04-01 18:34:48 | [diff] [blame] | 138 | interface_tunnel.address = |
| 139 | // Link-local IPv6 address |
| 140 | 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] | 141 | list.push_back(interface_tunnel); |
| 142 | |
Avi Drissman | 25292af6 | 2020-07-29 21:57:11 | [diff] [blame] | 143 | #if defined(OS_APPLE) |
jkarlin | 5780bb0 | 2017-05-31 16:39:49 | [diff] [blame] | 144 | EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 145 | NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list)); |
| 146 | #else |
| 147 | EXPECT_EQ(NetworkChangeNotifier::CONNECTION_ETHERNET, |
| 148 | NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list)); |
| 149 | #endif |
| 150 | } |
| 151 | |
Paul Jensen | 1ea3109 | 2019-04-01 18:34:48 | [diff] [blame] | 152 | TEST(NetworkChangeNotifierTest, IgnoreDisconnectedEthernetOnMac) { |
| 153 | NetworkInterfaceList list; |
| 154 | NetworkInterface interface_ethernet; |
| 155 | interface_ethernet.type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 156 | interface_ethernet.name = "en5"; |
| 157 | interface_ethernet.friendly_name = "en5"; |
| 158 | interface_ethernet.address = |
| 159 | // Link-local IPv6 address |
| 160 | IPAddress({0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 2, 3}); |
| 161 | list.push_back(interface_ethernet); |
| 162 | |
Avi Drissman | 25292af6 | 2020-07-29 21:57:11 | [diff] [blame] | 163 | #if defined(OS_APPLE) |
Paul Jensen | 1ea3109 | 2019-04-01 18:34:48 | [diff] [blame] | 164 | EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 165 | NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list)); |
| 166 | #else |
| 167 | EXPECT_EQ(NetworkChangeNotifier::CONNECTION_ETHERNET, |
| 168 | NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list)); |
| 169 | #endif |
| 170 | } |
| 171 | |
jkarlin | 0fed4eb | 2015-06-02 15:55:02 | [diff] [blame] | 172 | TEST(NetworkChangeNotifierTest, IgnoreVMInterfaces) { |
| 173 | NetworkInterfaceList list; |
| 174 | NetworkInterface interface_vmnet_linux; |
| 175 | interface_vmnet_linux.type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 176 | interface_vmnet_linux.name = "vmnet1"; |
| 177 | interface_vmnet_linux.friendly_name = "vmnet1"; |
| 178 | list.push_back(interface_vmnet_linux); |
| 179 | |
| 180 | NetworkInterface interface_vmnet_win; |
| 181 | interface_vmnet_win.type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 182 | interface_vmnet_win.name = "virtualdevice"; |
| 183 | interface_vmnet_win.friendly_name = "VMware Network Adapter VMnet1"; |
| 184 | list.push_back(interface_vmnet_win); |
| 185 | |
| 186 | EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 187 | NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list)); |
| 188 | } |
| 189 | |
tbansal | 880e49e7 | 2017-03-25 04:44:25 | [diff] [blame] | 190 | TEST(NetworkChangeNotifierTest, GetConnectionSubtype) { |
| 191 | // Call GetConnectionSubtype() and ensure that there is no crash. |
| 192 | NetworkChangeNotifier::GetConnectionSubtype(); |
| 193 | } |
| 194 | |
Eric Orth | a3559ca | 2019-09-05 23:40:54 | [diff] [blame] | 195 | class NetworkChangeNotifierMockedTest : public TestWithTaskEnvironment { |
| 196 | private: |
| 197 | test::ScopedMockNetworkChangeNotifier mock_notifier_; |
| 198 | }; |
| 199 | |
| 200 | class TestDnsObserver : public NetworkChangeNotifier::DNSObserver { |
| 201 | public: |
| 202 | void OnDNSChanged() override { ++dns_changed_calls_; } |
| 203 | |
Eric Orth | a3559ca | 2019-09-05 23:40:54 | [diff] [blame] | 204 | int dns_changed_calls() const { return dns_changed_calls_; } |
| 205 | |
| 206 | private: |
| 207 | int dns_changed_calls_ = 0; |
| 208 | }; |
| 209 | |
| 210 | TEST_F(NetworkChangeNotifierMockedTest, TriggerNonSystemDnsChange) { |
| 211 | TestDnsObserver observer; |
| 212 | NetworkChangeNotifier::AddDNSObserver(&observer); |
| 213 | |
| 214 | ASSERT_EQ(0, observer.dns_changed_calls()); |
| 215 | |
| 216 | NetworkChangeNotifier::TriggerNonSystemDnsChange(); |
| 217 | base::RunLoop().RunUntilIdle(); |
| 218 | |
| 219 | EXPECT_EQ(1, observer.dns_changed_calls()); |
| 220 | |
| 221 | NetworkChangeNotifier::RemoveDNSObserver(&observer); |
| 222 | } |
| 223 | |
jkarlin | 59a81093 | 2014-10-06 18:00:43 | [diff] [blame] | 224 | } // namespace net |