henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/network.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
oprypin | 1ea631f | 2017-08-18 00:15:19 -0700 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 15 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | #include <vector> |
oprypin | 1ea631f | 2017-08-18 00:15:19 -0700 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/nethelpers.h" |
| 20 | #include "rtc_base/networkmonitor.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 21 | #if defined(WEBRTC_POSIX) |
kjellander | f1c5ebf | 2017-06-30 05:27:14 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Henrik Kjellander | 0072511 | 2017-06-30 15:14:45 +0200 | [diff] [blame] | 23 | #include <net/if.h> |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/ifaddrs_converter.h" |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 25 | #endif // defined(WEBRTC_POSIX) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/gunit.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "rtc_base/logging.h" // For LOG_GLE |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | namespace rtc { |
| 32 | |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 33 | namespace { |
| 34 | |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 35 | class FakeNetworkMonitor : public NetworkMonitorBase { |
| 36 | public: |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 37 | void Start() override { started_ = true; } |
| 38 | void Stop() override { started_ = false; } |
| 39 | bool started() { return started_; } |
honghaiz | a7ad7c3 | 2016-02-02 12:54:14 -0800 | [diff] [blame] | 40 | AdapterType GetAdapterType(const std::string& if_name) override { |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 41 | // Note that the name matching rules are different from the |
| 42 | // GetAdapterTypeFromName in NetworkManager. |
| 43 | if (if_name.find("wifi") == 0) { |
| 44 | return ADAPTER_TYPE_WIFI; |
| 45 | } |
| 46 | if (if_name.find("cellular") == 0) { |
| 47 | return ADAPTER_TYPE_CELLULAR; |
| 48 | } |
honghaiz | a7ad7c3 | 2016-02-02 12:54:14 -0800 | [diff] [blame] | 49 | return ADAPTER_TYPE_UNKNOWN; |
| 50 | } |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | bool started_ = false; |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | class FakeNetworkMonitorFactory : public NetworkMonitorFactory { |
| 57 | public: |
| 58 | FakeNetworkMonitorFactory() {} |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 59 | NetworkMonitorInterface* CreateNetworkMonitor() override { |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 60 | return new FakeNetworkMonitor(); |
| 61 | } |
| 62 | }; |
| 63 | |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 64 | } // namespace |
| 65 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | class NetworkTest : public testing::Test, public sigslot::has_slots<> { |
| 67 | public: |
| 68 | NetworkTest() : callback_called_(false) {} |
| 69 | |
| 70 | void OnNetworksChanged() { |
| 71 | callback_called_ = true; |
| 72 | } |
| 73 | |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 74 | NetworkManager::Stats MergeNetworkList( |
| 75 | BasicNetworkManager& network_manager, |
| 76 | const NetworkManager::NetworkList& list, |
| 77 | bool* changed) { |
| 78 | NetworkManager::Stats stats; |
| 79 | network_manager.MergeNetworkList(list, changed, &stats); |
| 80 | return stats; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool IsIgnoredNetwork(BasicNetworkManager& network_manager, |
| 84 | const Network& network) { |
| 85 | return network_manager.IsIgnoredNetwork(network); |
| 86 | } |
| 87 | |
| 88 | NetworkManager::NetworkList GetNetworks( |
| 89 | const BasicNetworkManager& network_manager, bool include_ignored) { |
| 90 | NetworkManager::NetworkList list; |
| 91 | network_manager.CreateNetworks(include_ignored, &list); |
| 92 | return list; |
| 93 | } |
| 94 | |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 95 | FakeNetworkMonitor* GetNetworkMonitor(BasicNetworkManager& network_manager) { |
| 96 | return static_cast<FakeNetworkMonitor*>( |
| 97 | network_manager.network_monitor_.get()); |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 98 | } |
| 99 | void ClearNetworks(BasicNetworkManager& network_manager) { |
| 100 | for (const auto& kv : network_manager.networks_map_) { |
| 101 | delete kv.second; |
| 102 | } |
| 103 | network_manager.networks_.clear(); |
| 104 | network_manager.networks_map_.clear(); |
| 105 | } |
| 106 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 107 | AdapterType GetAdapterType(BasicNetworkManager& network_manager) { |
| 108 | BasicNetworkManager::NetworkList list; |
| 109 | network_manager.GetNetworks(&list); |
nisse | c16fa5e | 2017-02-07 07:18:43 -0800 | [diff] [blame] | 110 | RTC_CHECK_EQ(1, list.size()); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 111 | return list[0]->type(); |
| 112 | } |
| 113 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 114 | #if defined(WEBRTC_POSIX) |
| 115 | // Separated from CreateNetworks for tests. |
| 116 | static void CallConvertIfAddrs(const BasicNetworkManager& network_manager, |
| 117 | struct ifaddrs* interfaces, |
| 118 | bool include_ignored, |
| 119 | NetworkManager::NetworkList* networks) { |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 120 | // Use the base IfAddrsConverter for test cases. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 121 | std::unique_ptr<IfAddrsConverter> ifaddrs_converter(new IfAddrsConverter()); |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 122 | network_manager.ConvertIfAddrs(interfaces, ifaddrs_converter.get(), |
| 123 | include_ignored, networks); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | } |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 125 | |
| 126 | struct sockaddr_in6* CreateIpv6Addr(const std::string& ip_string, |
| 127 | uint32_t scope_id) { |
oprypin | 1ea631f | 2017-08-18 00:15:19 -0700 | [diff] [blame] | 128 | struct sockaddr_in6* ipv6_addr = static_cast<struct sockaddr_in6*>( |
| 129 | malloc(sizeof(struct sockaddr_in6))); |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 130 | memset(ipv6_addr, 0, sizeof(struct sockaddr_in6)); |
| 131 | ipv6_addr->sin6_family = AF_INET6; |
| 132 | ipv6_addr->sin6_scope_id = scope_id; |
| 133 | IPAddress ip; |
| 134 | IPFromString(ip_string, &ip); |
| 135 | ipv6_addr->sin6_addr = ip.ipv6_address(); |
| 136 | return ipv6_addr; |
| 137 | } |
| 138 | |
| 139 | // Pointers created here need to be released via ReleaseIfAddrs. |
| 140 | struct ifaddrs* AddIpv6Address(struct ifaddrs* list, |
| 141 | char* if_name, |
| 142 | const std::string& ipv6_address, |
| 143 | const std::string& ipv6_netmask, |
| 144 | uint32_t scope_id) { |
| 145 | struct ifaddrs* if_addr = new struct ifaddrs; |
| 146 | memset(if_addr, 0, sizeof(struct ifaddrs)); |
| 147 | if_addr->ifa_name = if_name; |
| 148 | if_addr->ifa_addr = reinterpret_cast<struct sockaddr*>( |
| 149 | CreateIpv6Addr(ipv6_address, scope_id)); |
| 150 | if_addr->ifa_netmask = |
| 151 | reinterpret_cast<struct sockaddr*>(CreateIpv6Addr(ipv6_netmask, 0)); |
| 152 | if_addr->ifa_next = list; |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 153 | if_addr->ifa_flags = IFF_RUNNING; |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 154 | return if_addr; |
| 155 | } |
| 156 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 157 | struct ifaddrs* InstallIpv6Network(char* if_name, |
| 158 | const std::string& ipv6_address, |
| 159 | const std::string& ipv6_mask, |
| 160 | BasicNetworkManager& network_manager) { |
| 161 | ifaddrs* addr_list = nullptr; |
| 162 | addr_list = AddIpv6Address(addr_list, if_name, ipv6_address, ipv6_mask, 0); |
| 163 | NetworkManager::NetworkList result; |
| 164 | bool changed; |
| 165 | NetworkManager::Stats stats; |
| 166 | CallConvertIfAddrs(network_manager, addr_list, true, &result); |
| 167 | network_manager.MergeNetworkList(result, &changed, &stats); |
| 168 | return addr_list; |
| 169 | } |
| 170 | |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 171 | void ReleaseIfAddrs(struct ifaddrs* list) { |
| 172 | struct ifaddrs* if_addr = list; |
| 173 | while (if_addr != nullptr) { |
| 174 | struct ifaddrs* next_addr = if_addr->ifa_next; |
oprypin | 1ea631f | 2017-08-18 00:15:19 -0700 | [diff] [blame] | 175 | free(if_addr->ifa_addr); |
| 176 | free(if_addr->ifa_netmask); |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 177 | delete if_addr; |
| 178 | if_addr = next_addr; |
| 179 | } |
| 180 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 181 | #endif // defined(WEBRTC_POSIX) |
| 182 | |
| 183 | protected: |
| 184 | bool callback_called_; |
| 185 | }; |
| 186 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 187 | class TestBasicNetworkManager : public BasicNetworkManager { |
| 188 | public: |
| 189 | using BasicNetworkManager::QueryDefaultLocalAddress; |
Guo-wei Shieh | a34c39e | 2015-11-25 13:12:26 -0800 | [diff] [blame] | 190 | using BasicNetworkManager::set_default_local_addresses; |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 193 | // Test that the Network ctor works properly. |
| 194 | TEST_F(NetworkTest, TestNetworkConstruct) { |
| 195 | Network ipv4_network1("test_eth0", "Test Network Adapter 1", |
| 196 | IPAddress(0x12345600U), 24); |
| 197 | EXPECT_EQ("test_eth0", ipv4_network1.name()); |
| 198 | EXPECT_EQ("Test Network Adapter 1", ipv4_network1.description()); |
| 199 | EXPECT_EQ(IPAddress(0x12345600U), ipv4_network1.prefix()); |
| 200 | EXPECT_EQ(24, ipv4_network1.prefix_length()); |
| 201 | EXPECT_FALSE(ipv4_network1.ignored()); |
| 202 | } |
| 203 | |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 204 | TEST_F(NetworkTest, TestIsIgnoredNetworkIgnoresIPsStartingWith0) { |
| 205 | Network ipv4_network1("test_eth0", "Test Network Adapter 1", |
| 206 | IPAddress(0x12345600U), 24, ADAPTER_TYPE_ETHERNET); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 207 | Network ipv4_network2("test_eth1", "Test Network Adapter 2", |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 208 | IPAddress(0x010000U), 24, ADAPTER_TYPE_ETHERNET); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 209 | BasicNetworkManager network_manager; |
| 210 | EXPECT_FALSE(IsIgnoredNetwork(network_manager, ipv4_network1)); |
| 211 | EXPECT_TRUE(IsIgnoredNetwork(network_manager, ipv4_network2)); |
| 212 | } |
| 213 | |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 214 | // TODO(phoglund): Remove when ignore list goes away. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 215 | TEST_F(NetworkTest, TestIgnoreList) { |
| 216 | Network ignore_me("ignore_me", "Ignore me please!", |
| 217 | IPAddress(0x12345600U), 24); |
| 218 | Network include_me("include_me", "Include me please!", |
| 219 | IPAddress(0x12345600U), 24); |
| 220 | BasicNetworkManager network_manager; |
| 221 | EXPECT_FALSE(IsIgnoredNetwork(network_manager, ignore_me)); |
| 222 | EXPECT_FALSE(IsIgnoredNetwork(network_manager, include_me)); |
| 223 | std::vector<std::string> ignore_list; |
| 224 | ignore_list.push_back("ignore_me"); |
| 225 | network_manager.set_network_ignore_list(ignore_list); |
| 226 | EXPECT_TRUE(IsIgnoredNetwork(network_manager, ignore_me)); |
| 227 | EXPECT_FALSE(IsIgnoredNetwork(network_manager, include_me)); |
| 228 | } |
| 229 | |
| 230 | // Test is failing on Windows opt: b/11288214 |
| 231 | TEST_F(NetworkTest, DISABLED_TestCreateNetworks) { |
| 232 | BasicNetworkManager manager; |
| 233 | NetworkManager::NetworkList result = GetNetworks(manager, true); |
| 234 | // We should be able to bind to any addresses we find. |
| 235 | NetworkManager::NetworkList::iterator it; |
| 236 | for (it = result.begin(); |
| 237 | it != result.end(); |
| 238 | ++it) { |
| 239 | sockaddr_storage storage; |
| 240 | memset(&storage, 0, sizeof(storage)); |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 241 | IPAddress ip = (*it)->GetBestIP(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 242 | SocketAddress bindaddress(ip, 0); |
| 243 | bindaddress.SetScopeID((*it)->scope_id()); |
| 244 | // TODO(thaloun): Use rtc::AsyncSocket once it supports IPv6. |
| 245 | int fd = static_cast<int>(socket(ip.family(), SOCK_STREAM, IPPROTO_TCP)); |
| 246 | if (fd > 0) { |
| 247 | size_t ipsize = bindaddress.ToSockAddrStorage(&storage); |
| 248 | EXPECT_GE(ipsize, 0U); |
| 249 | int success = ::bind(fd, |
| 250 | reinterpret_cast<sockaddr*>(&storage), |
| 251 | static_cast<int>(ipsize)); |
| 252 | #if defined(WEBRTC_WIN) |
| 253 | if (success) LOG_GLE(LS_ERROR) << "Socket bind failed."; |
| 254 | #endif |
| 255 | EXPECT_EQ(0, success); |
| 256 | #if defined(WEBRTC_WIN) |
| 257 | closesocket(fd); |
| 258 | #else |
| 259 | close(fd); |
| 260 | #endif |
| 261 | } |
| 262 | delete (*it); |
| 263 | } |
| 264 | } |
| 265 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 266 | // Test StartUpdating() and StopUpdating(). network_permission_state starts with |
| 267 | // ALLOWED. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 268 | TEST_F(NetworkTest, TestUpdateNetworks) { |
| 269 | BasicNetworkManager manager; |
| 270 | manager.SignalNetworksChanged.connect( |
| 271 | static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); |
guoweis | ea1012b | 2015-08-21 09:06:28 -0700 | [diff] [blame] | 272 | EXPECT_EQ(NetworkManager::ENUMERATION_ALLOWED, |
| 273 | manager.enumeration_permission()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 274 | manager.StartUpdating(); |
| 275 | Thread::Current()->ProcessMessages(0); |
| 276 | EXPECT_TRUE(callback_called_); |
| 277 | callback_called_ = false; |
| 278 | // Callback should be triggered immediately when StartUpdating |
| 279 | // is called, after network update signal is already sent. |
| 280 | manager.StartUpdating(); |
| 281 | EXPECT_TRUE(manager.started()); |
| 282 | Thread::Current()->ProcessMessages(0); |
| 283 | EXPECT_TRUE(callback_called_); |
| 284 | manager.StopUpdating(); |
| 285 | EXPECT_TRUE(manager.started()); |
| 286 | manager.StopUpdating(); |
guoweis | ea1012b | 2015-08-21 09:06:28 -0700 | [diff] [blame] | 287 | EXPECT_EQ(NetworkManager::ENUMERATION_ALLOWED, |
| 288 | manager.enumeration_permission()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 289 | EXPECT_FALSE(manager.started()); |
| 290 | manager.StopUpdating(); |
| 291 | EXPECT_FALSE(manager.started()); |
| 292 | callback_called_ = false; |
| 293 | // Callback should be triggered immediately after StartUpdating is called |
| 294 | // when start_count_ is reset to 0. |
| 295 | manager.StartUpdating(); |
| 296 | Thread::Current()->ProcessMessages(0); |
| 297 | EXPECT_TRUE(callback_called_); |
| 298 | } |
| 299 | |
| 300 | // Verify that MergeNetworkList() merges network lists properly. |
| 301 | TEST_F(NetworkTest, TestBasicMergeNetworkList) { |
| 302 | Network ipv4_network1("test_eth0", "Test Network Adapter 1", |
| 303 | IPAddress(0x12345600U), 24); |
| 304 | Network ipv4_network2("test_eth1", "Test Network Adapter 2", |
| 305 | IPAddress(0x00010000U), 16); |
| 306 | ipv4_network1.AddIP(IPAddress(0x12345678)); |
| 307 | ipv4_network2.AddIP(IPAddress(0x00010004)); |
| 308 | BasicNetworkManager manager; |
| 309 | |
| 310 | // Add ipv4_network1 to the list of networks. |
| 311 | NetworkManager::NetworkList list; |
| 312 | list.push_back(new Network(ipv4_network1)); |
| 313 | bool changed; |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 314 | NetworkManager::Stats stats = MergeNetworkList(manager, list, &changed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 315 | EXPECT_TRUE(changed); |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 316 | EXPECT_EQ(stats.ipv6_network_count, 0); |
| 317 | EXPECT_EQ(stats.ipv4_network_count, 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 318 | list.clear(); |
| 319 | |
| 320 | manager.GetNetworks(&list); |
| 321 | EXPECT_EQ(1U, list.size()); |
| 322 | EXPECT_EQ(ipv4_network1.ToString(), list[0]->ToString()); |
| 323 | Network* net1 = list[0]; |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 324 | uint16_t net_id1 = net1->id(); |
| 325 | EXPECT_EQ(1, net_id1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 326 | list.clear(); |
| 327 | |
| 328 | // Replace ipv4_network1 with ipv4_network2. |
| 329 | list.push_back(new Network(ipv4_network2)); |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 330 | stats = MergeNetworkList(manager, list, &changed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 331 | EXPECT_TRUE(changed); |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 332 | EXPECT_EQ(stats.ipv6_network_count, 0); |
| 333 | EXPECT_EQ(stats.ipv4_network_count, 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 334 | list.clear(); |
| 335 | |
| 336 | manager.GetNetworks(&list); |
| 337 | EXPECT_EQ(1U, list.size()); |
| 338 | EXPECT_EQ(ipv4_network2.ToString(), list[0]->ToString()); |
| 339 | Network* net2 = list[0]; |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 340 | uint16_t net_id2 = net2->id(); |
| 341 | // Network id will increase. |
| 342 | EXPECT_LT(net_id1, net_id2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 343 | list.clear(); |
| 344 | |
| 345 | // Add Network2 back. |
| 346 | list.push_back(new Network(ipv4_network1)); |
| 347 | list.push_back(new Network(ipv4_network2)); |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 348 | stats = MergeNetworkList(manager, list, &changed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 349 | EXPECT_TRUE(changed); |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 350 | EXPECT_EQ(stats.ipv6_network_count, 0); |
| 351 | EXPECT_EQ(stats.ipv4_network_count, 2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 352 | list.clear(); |
| 353 | |
| 354 | // Verify that we get previous instances of Network objects. |
| 355 | manager.GetNetworks(&list); |
| 356 | EXPECT_EQ(2U, list.size()); |
| 357 | EXPECT_TRUE((net1 == list[0] && net2 == list[1]) || |
| 358 | (net1 == list[1] && net2 == list[0])); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 359 | EXPECT_TRUE((net_id1 == list[0]->id() && net_id2 == list[1]->id()) || |
| 360 | (net_id1 == list[1]->id() && net_id2 == list[0]->id())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 361 | list.clear(); |
| 362 | |
| 363 | // Call MergeNetworkList() again and verify that we don't get update |
| 364 | // notification. |
| 365 | list.push_back(new Network(ipv4_network2)); |
| 366 | list.push_back(new Network(ipv4_network1)); |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 367 | stats = MergeNetworkList(manager, list, &changed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 368 | EXPECT_FALSE(changed); |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 369 | EXPECT_EQ(stats.ipv6_network_count, 0); |
| 370 | EXPECT_EQ(stats.ipv4_network_count, 2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 371 | list.clear(); |
| 372 | |
| 373 | // Verify that we get previous instances of Network objects. |
| 374 | manager.GetNetworks(&list); |
| 375 | EXPECT_EQ(2U, list.size()); |
| 376 | EXPECT_TRUE((net1 == list[0] && net2 == list[1]) || |
| 377 | (net1 == list[1] && net2 == list[0])); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 378 | EXPECT_TRUE((net_id1 == list[0]->id() && net_id2 == list[1]->id()) || |
| 379 | (net_id1 == list[1]->id() && net_id2 == list[0]->id())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 380 | list.clear(); |
| 381 | } |
| 382 | |
| 383 | // Sets up some test IPv6 networks and appends them to list. |
| 384 | // Four networks are added - public and link local, for two interfaces. |
| 385 | void SetupNetworks(NetworkManager::NetworkList* list) { |
| 386 | IPAddress ip; |
| 387 | IPAddress prefix; |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 388 | EXPECT_TRUE(IPFromString("abcd::1234:5678:abcd:ef12", &ip)); |
| 389 | EXPECT_TRUE(IPFromString("abcd::", &prefix)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 390 | // First, fake link-locals. |
| 391 | Network ipv6_eth0_linklocalnetwork("test_eth0", "Test NetworkAdapter 1", |
| 392 | prefix, 64); |
| 393 | ipv6_eth0_linklocalnetwork.AddIP(ip); |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 394 | EXPECT_TRUE(IPFromString("abcd::5678:abcd:ef12:3456", &ip)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 395 | Network ipv6_eth1_linklocalnetwork("test_eth1", "Test NetworkAdapter 2", |
| 396 | prefix, 64); |
| 397 | ipv6_eth1_linklocalnetwork.AddIP(ip); |
| 398 | // Public networks: |
| 399 | EXPECT_TRUE(IPFromString("2401:fa00:4:1000:be30:5bff:fee5:c3", &ip)); |
| 400 | prefix = TruncateIP(ip, 64); |
| 401 | Network ipv6_eth0_publicnetwork1_ip1("test_eth0", "Test NetworkAdapter 1", |
| 402 | prefix, 64); |
| 403 | ipv6_eth0_publicnetwork1_ip1.AddIP(ip); |
| 404 | EXPECT_TRUE(IPFromString("2400:4030:1:2c00:be30:abcd:efab:cdef", &ip)); |
| 405 | prefix = TruncateIP(ip, 64); |
| 406 | Network ipv6_eth1_publicnetwork1_ip1("test_eth1", "Test NetworkAdapter 1", |
| 407 | prefix, 64); |
| 408 | ipv6_eth1_publicnetwork1_ip1.AddIP(ip); |
| 409 | list->push_back(new Network(ipv6_eth0_linklocalnetwork)); |
| 410 | list->push_back(new Network(ipv6_eth1_linklocalnetwork)); |
| 411 | list->push_back(new Network(ipv6_eth0_publicnetwork1_ip1)); |
| 412 | list->push_back(new Network(ipv6_eth1_publicnetwork1_ip1)); |
| 413 | } |
| 414 | |
| 415 | // Test that the basic network merging case works. |
| 416 | TEST_F(NetworkTest, TestIPv6MergeNetworkList) { |
| 417 | BasicNetworkManager manager; |
| 418 | manager.SignalNetworksChanged.connect( |
| 419 | static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); |
| 420 | NetworkManager::NetworkList original_list; |
| 421 | SetupNetworks(&original_list); |
| 422 | bool changed = false; |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 423 | NetworkManager::Stats stats = |
| 424 | MergeNetworkList(manager, original_list, &changed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 425 | EXPECT_TRUE(changed); |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 426 | EXPECT_EQ(stats.ipv6_network_count, 4); |
| 427 | EXPECT_EQ(stats.ipv4_network_count, 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 428 | NetworkManager::NetworkList list; |
| 429 | manager.GetNetworks(&list); |
| 430 | EXPECT_EQ(original_list.size(), list.size()); |
| 431 | // Verify that the original members are in the merged list. |
| 432 | for (NetworkManager::NetworkList::iterator it = original_list.begin(); |
| 433 | it != original_list.end(); ++it) { |
| 434 | EXPECT_NE(list.end(), std::find(list.begin(), list.end(), *it)); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // Tests that when two network lists that describe the same set of networks are |
| 439 | // merged, that the changed callback is not called, and that the original |
| 440 | // objects remain in the result list. |
| 441 | TEST_F(NetworkTest, TestNoChangeMerge) { |
| 442 | BasicNetworkManager manager; |
| 443 | manager.SignalNetworksChanged.connect( |
| 444 | static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); |
| 445 | NetworkManager::NetworkList original_list; |
| 446 | SetupNetworks(&original_list); |
| 447 | bool changed = false; |
| 448 | MergeNetworkList(manager, original_list, &changed); |
| 449 | EXPECT_TRUE(changed); |
| 450 | // Second list that describes the same networks but with new objects. |
| 451 | NetworkManager::NetworkList second_list; |
| 452 | SetupNetworks(&second_list); |
| 453 | changed = false; |
| 454 | MergeNetworkList(manager, second_list, &changed); |
| 455 | EXPECT_FALSE(changed); |
| 456 | NetworkManager::NetworkList resulting_list; |
| 457 | manager.GetNetworks(&resulting_list); |
| 458 | EXPECT_EQ(original_list.size(), resulting_list.size()); |
| 459 | // Verify that the original members are in the merged list. |
| 460 | for (NetworkManager::NetworkList::iterator it = original_list.begin(); |
| 461 | it != original_list.end(); ++it) { |
| 462 | EXPECT_NE(resulting_list.end(), |
| 463 | std::find(resulting_list.begin(), resulting_list.end(), *it)); |
| 464 | } |
| 465 | // Doublecheck that the new networks aren't in the list. |
| 466 | for (NetworkManager::NetworkList::iterator it = second_list.begin(); |
| 467 | it != second_list.end(); ++it) { |
| 468 | EXPECT_EQ(resulting_list.end(), |
| 469 | std::find(resulting_list.begin(), resulting_list.end(), *it)); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // Test that we can merge a network that is the same as another network but with |
| 474 | // a different IP. The original network should remain in the list, but have its |
| 475 | // IP changed. |
| 476 | TEST_F(NetworkTest, MergeWithChangedIP) { |
| 477 | BasicNetworkManager manager; |
| 478 | manager.SignalNetworksChanged.connect( |
| 479 | static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); |
| 480 | NetworkManager::NetworkList original_list; |
| 481 | SetupNetworks(&original_list); |
| 482 | // Make a network that we're going to change. |
| 483 | IPAddress ip; |
| 484 | EXPECT_TRUE(IPFromString("2401:fa01:4:1000:be30:faa:fee:faa", &ip)); |
| 485 | IPAddress prefix = TruncateIP(ip, 64); |
| 486 | Network* network_to_change = new Network("test_eth0", |
| 487 | "Test Network Adapter 1", |
| 488 | prefix, 64); |
| 489 | Network* changed_network = new Network(*network_to_change); |
| 490 | network_to_change->AddIP(ip); |
| 491 | IPAddress changed_ip; |
| 492 | EXPECT_TRUE(IPFromString("2401:fa01:4:1000:be30:f00:f00:f00", &changed_ip)); |
| 493 | changed_network->AddIP(changed_ip); |
| 494 | original_list.push_back(network_to_change); |
| 495 | bool changed = false; |
| 496 | MergeNetworkList(manager, original_list, &changed); |
| 497 | NetworkManager::NetworkList second_list; |
| 498 | SetupNetworks(&second_list); |
| 499 | second_list.push_back(changed_network); |
| 500 | changed = false; |
| 501 | MergeNetworkList(manager, second_list, &changed); |
| 502 | EXPECT_TRUE(changed); |
| 503 | NetworkManager::NetworkList list; |
| 504 | manager.GetNetworks(&list); |
| 505 | EXPECT_EQ(original_list.size(), list.size()); |
| 506 | // Make sure the original network is still in the merged list. |
| 507 | EXPECT_NE(list.end(), |
| 508 | std::find(list.begin(), list.end(), network_to_change)); |
| 509 | EXPECT_EQ(changed_ip, network_to_change->GetIPs().at(0)); |
| 510 | } |
| 511 | |
| 512 | // Testing a similar case to above, but checking that a network can be updated |
| 513 | // with additional IPs (not just a replacement). |
| 514 | TEST_F(NetworkTest, TestMultipleIPMergeNetworkList) { |
| 515 | BasicNetworkManager manager; |
| 516 | manager.SignalNetworksChanged.connect( |
| 517 | static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); |
| 518 | NetworkManager::NetworkList original_list; |
| 519 | SetupNetworks(&original_list); |
| 520 | bool changed = false; |
| 521 | MergeNetworkList(manager, original_list, &changed); |
| 522 | EXPECT_TRUE(changed); |
| 523 | IPAddress ip; |
| 524 | IPAddress check_ip; |
| 525 | IPAddress prefix; |
| 526 | // Add a second IP to the public network on eth0 (2401:fa00:4:1000/64). |
| 527 | EXPECT_TRUE(IPFromString("2401:fa00:4:1000:be30:5bff:fee5:c6", &ip)); |
| 528 | prefix = TruncateIP(ip, 64); |
| 529 | Network ipv6_eth0_publicnetwork1_ip2("test_eth0", "Test NetworkAdapter 1", |
| 530 | prefix, 64); |
| 531 | // This is the IP that already existed in the public network on eth0. |
| 532 | EXPECT_TRUE(IPFromString("2401:fa00:4:1000:be30:5bff:fee5:c3", &check_ip)); |
| 533 | ipv6_eth0_publicnetwork1_ip2.AddIP(ip); |
| 534 | original_list.push_back(new Network(ipv6_eth0_publicnetwork1_ip2)); |
| 535 | changed = false; |
| 536 | MergeNetworkList(manager, original_list, &changed); |
| 537 | EXPECT_TRUE(changed); |
| 538 | // There should still be four networks. |
| 539 | NetworkManager::NetworkList list; |
| 540 | manager.GetNetworks(&list); |
| 541 | EXPECT_EQ(4U, list.size()); |
| 542 | // Check the gathered IPs. |
| 543 | int matchcount = 0; |
| 544 | for (NetworkManager::NetworkList::iterator it = list.begin(); |
| 545 | it != list.end(); ++it) { |
| 546 | if ((*it)->ToString() == original_list[2]->ToString()) { |
| 547 | ++matchcount; |
| 548 | EXPECT_EQ(1, matchcount); |
| 549 | // This should be the same network object as before. |
| 550 | EXPECT_EQ((*it), original_list[2]); |
| 551 | // But with two addresses now. |
| 552 | EXPECT_EQ(2U, (*it)->GetIPs().size()); |
| 553 | EXPECT_NE((*it)->GetIPs().end(), |
| 554 | std::find((*it)->GetIPs().begin(), |
| 555 | (*it)->GetIPs().end(), |
| 556 | check_ip)); |
| 557 | EXPECT_NE((*it)->GetIPs().end(), |
| 558 | std::find((*it)->GetIPs().begin(), |
| 559 | (*it)->GetIPs().end(), |
| 560 | ip)); |
| 561 | } else { |
| 562 | // Check the IP didn't get added anywhere it wasn't supposed to. |
| 563 | EXPECT_EQ((*it)->GetIPs().end(), |
| 564 | std::find((*it)->GetIPs().begin(), |
| 565 | (*it)->GetIPs().end(), |
| 566 | ip)); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | // Test that merge correctly distinguishes multiple networks on an interface. |
| 572 | TEST_F(NetworkTest, TestMultiplePublicNetworksOnOneInterfaceMerge) { |
| 573 | BasicNetworkManager manager; |
| 574 | manager.SignalNetworksChanged.connect( |
| 575 | static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); |
| 576 | NetworkManager::NetworkList original_list; |
| 577 | SetupNetworks(&original_list); |
| 578 | bool changed = false; |
| 579 | MergeNetworkList(manager, original_list, &changed); |
| 580 | EXPECT_TRUE(changed); |
| 581 | IPAddress ip; |
| 582 | IPAddress prefix; |
| 583 | // A second network for eth0. |
| 584 | EXPECT_TRUE(IPFromString("2400:4030:1:2c00:be30:5bff:fee5:c3", &ip)); |
| 585 | prefix = TruncateIP(ip, 64); |
| 586 | Network ipv6_eth0_publicnetwork2_ip1("test_eth0", "Test NetworkAdapter 1", |
| 587 | prefix, 64); |
| 588 | ipv6_eth0_publicnetwork2_ip1.AddIP(ip); |
| 589 | original_list.push_back(new Network(ipv6_eth0_publicnetwork2_ip1)); |
| 590 | changed = false; |
| 591 | MergeNetworkList(manager, original_list, &changed); |
| 592 | EXPECT_TRUE(changed); |
| 593 | // There should be five networks now. |
| 594 | NetworkManager::NetworkList list; |
| 595 | manager.GetNetworks(&list); |
| 596 | EXPECT_EQ(5U, list.size()); |
| 597 | // Check the resulting addresses. |
| 598 | for (NetworkManager::NetworkList::iterator it = list.begin(); |
| 599 | it != list.end(); ++it) { |
| 600 | if ((*it)->prefix() == ipv6_eth0_publicnetwork2_ip1.prefix() && |
| 601 | (*it)->name() == ipv6_eth0_publicnetwork2_ip1.name()) { |
| 602 | // Check the new network has 1 IP and that it's the correct one. |
| 603 | EXPECT_EQ(1U, (*it)->GetIPs().size()); |
| 604 | EXPECT_EQ(ip, (*it)->GetIPs().at(0)); |
| 605 | } else { |
| 606 | // Check the IP didn't get added anywhere it wasn't supposed to. |
| 607 | EXPECT_EQ((*it)->GetIPs().end(), |
| 608 | std::find((*it)->GetIPs().begin(), |
| 609 | (*it)->GetIPs().end(), |
| 610 | ip)); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 615 | // Test that DumpNetworks does not crash. |
| 616 | TEST_F(NetworkTest, TestCreateAndDumpNetworks) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 617 | BasicNetworkManager manager; |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 618 | NetworkManager::NetworkList list = GetNetworks(manager, true); |
| 619 | bool changed; |
| 620 | MergeNetworkList(manager, list, &changed); |
| 621 | manager.DumpNetworks(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | // Test that we can toggle IPv6 on and off. |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 625 | // Crashes on Linux. See webrtc:4923. |
| 626 | #if defined(WEBRTC_LINUX) |
| 627 | #define MAYBE_TestIPv6Toggle DISABLED_TestIPv6Toggle |
| 628 | #else |
| 629 | #define MAYBE_TestIPv6Toggle TestIPv6Toggle |
| 630 | #endif |
| 631 | TEST_F(NetworkTest, MAYBE_TestIPv6Toggle) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 632 | BasicNetworkManager manager; |
| 633 | bool ipv6_found = false; |
| 634 | NetworkManager::NetworkList list; |
| 635 | #if !defined(WEBRTC_WIN) |
| 636 | // There should be at least one IPv6 network (fe80::/64 should be in there). |
| 637 | // TODO(thaloun): Disabling this test on windows for the moment as the test |
| 638 | // machines don't seem to have IPv6 installed on them at all. |
| 639 | manager.set_ipv6_enabled(true); |
| 640 | list = GetNetworks(manager, true); |
| 641 | for (NetworkManager::NetworkList::iterator it = list.begin(); |
| 642 | it != list.end(); ++it) { |
| 643 | if ((*it)->prefix().family() == AF_INET6) { |
| 644 | ipv6_found = true; |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | EXPECT_TRUE(ipv6_found); |
| 649 | for (NetworkManager::NetworkList::iterator it = list.begin(); |
| 650 | it != list.end(); ++it) { |
| 651 | delete (*it); |
| 652 | } |
| 653 | #endif |
| 654 | ipv6_found = false; |
| 655 | manager.set_ipv6_enabled(false); |
| 656 | list = GetNetworks(manager, true); |
| 657 | for (NetworkManager::NetworkList::iterator it = list.begin(); |
| 658 | it != list.end(); ++it) { |
| 659 | if ((*it)->prefix().family() == AF_INET6) { |
| 660 | ipv6_found = true; |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | EXPECT_FALSE(ipv6_found); |
| 665 | for (NetworkManager::NetworkList::iterator it = list.begin(); |
| 666 | it != list.end(); ++it) { |
| 667 | delete (*it); |
| 668 | } |
| 669 | } |
| 670 | |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 671 | // Test that when network interfaces are sorted and given preference values, |
| 672 | // IPv6 comes first. |
| 673 | TEST_F(NetworkTest, IPv6NetworksPreferredOverIPv4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 674 | BasicNetworkManager manager; |
| 675 | Network ipv4_network1("test_eth0", "Test Network Adapter 1", |
| 676 | IPAddress(0x12345600U), 24); |
| 677 | ipv4_network1.AddIP(IPAddress(0x12345600U)); |
| 678 | |
| 679 | IPAddress ip; |
| 680 | IPAddress prefix; |
| 681 | EXPECT_TRUE(IPFromString("2400:4030:1:2c00:be30:abcd:efab:cdef", &ip)); |
| 682 | prefix = TruncateIP(ip, 64); |
| 683 | Network ipv6_eth1_publicnetwork1_ip1("test_eth1", "Test NetworkAdapter 2", |
| 684 | prefix, 64); |
| 685 | ipv6_eth1_publicnetwork1_ip1.AddIP(ip); |
| 686 | |
| 687 | NetworkManager::NetworkList list; |
| 688 | list.push_back(new Network(ipv4_network1)); |
| 689 | list.push_back(new Network(ipv6_eth1_publicnetwork1_ip1)); |
| 690 | Network* net1 = list[0]; |
| 691 | Network* net2 = list[1]; |
| 692 | |
| 693 | bool changed = false; |
| 694 | MergeNetworkList(manager, list, &changed); |
| 695 | ASSERT_TRUE(changed); |
| 696 | // After sorting IPv6 network should be higher order than IPv4 networks. |
| 697 | EXPECT_TRUE(net1->preference() < net2->preference()); |
| 698 | } |
| 699 | |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 700 | // When two interfaces are equivalent in everything but name, they're expected |
| 701 | // to be preference-ordered by name. For example, "eth0" before "eth1". |
| 702 | TEST_F(NetworkTest, NetworksSortedByInterfaceName) { |
| 703 | BasicNetworkManager manager; |
| 704 | Network* eth0 = new Network("test_eth0", "Test Network Adapter 1", |
| 705 | IPAddress(0x65432100U), 24); |
| 706 | eth0->AddIP(IPAddress(0x65432100U)); |
| 707 | Network* eth1 = new Network("test_eth1", "Test Network Adapter 2", |
| 708 | IPAddress(0x12345600U), 24); |
| 709 | eth1->AddIP(IPAddress(0x12345600U)); |
| 710 | NetworkManager::NetworkList list; |
| 711 | // Add them to the list in the opposite of the expected sorted order, to |
| 712 | // ensure sorting actually occurs. |
| 713 | list.push_back(eth1); |
| 714 | list.push_back(eth0); |
| 715 | |
| 716 | bool changed = false; |
| 717 | MergeNetworkList(manager, list, &changed); |
| 718 | ASSERT_TRUE(changed); |
| 719 | // "test_eth0" should be preferred over "test_eth1". |
| 720 | EXPECT_TRUE(eth0->preference() > eth1->preference()); |
| 721 | } |
| 722 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 723 | TEST_F(NetworkTest, TestNetworkAdapterTypes) { |
| 724 | Network wifi("wlan0", "Wireless Adapter", IPAddress(0x12345600U), 24, |
| 725 | ADAPTER_TYPE_WIFI); |
| 726 | EXPECT_EQ(ADAPTER_TYPE_WIFI, wifi.type()); |
| 727 | Network ethernet("eth0", "Ethernet", IPAddress(0x12345600U), 24, |
| 728 | ADAPTER_TYPE_ETHERNET); |
| 729 | EXPECT_EQ(ADAPTER_TYPE_ETHERNET, ethernet.type()); |
| 730 | Network cellular("test_cell", "Cellular Adapter", IPAddress(0x12345600U), 24, |
| 731 | ADAPTER_TYPE_CELLULAR); |
| 732 | EXPECT_EQ(ADAPTER_TYPE_CELLULAR, cellular.type()); |
| 733 | Network vpn("bridge_test", "VPN Adapter", IPAddress(0x12345600U), 24, |
| 734 | ADAPTER_TYPE_VPN); |
| 735 | EXPECT_EQ(ADAPTER_TYPE_VPN, vpn.type()); |
| 736 | Network unknown("test", "Test Adapter", IPAddress(0x12345600U), 24, |
| 737 | ADAPTER_TYPE_UNKNOWN); |
| 738 | EXPECT_EQ(ADAPTER_TYPE_UNKNOWN, unknown.type()); |
| 739 | } |
| 740 | |
| 741 | #if defined(WEBRTC_POSIX) |
| 742 | // Verify that we correctly handle interfaces with no address. |
| 743 | TEST_F(NetworkTest, TestConvertIfAddrsNoAddress) { |
| 744 | ifaddrs list; |
| 745 | memset(&list, 0, sizeof(list)); |
| 746 | list.ifa_name = const_cast<char*>("test_iface"); |
| 747 | |
| 748 | NetworkManager::NetworkList result; |
| 749 | BasicNetworkManager manager; |
| 750 | CallConvertIfAddrs(manager, &list, true, &result); |
| 751 | EXPECT_TRUE(result.empty()); |
| 752 | } |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 753 | |
| 754 | // Verify that if there are two addresses on one interface, only one network |
| 755 | // is generated. |
| 756 | TEST_F(NetworkTest, TestConvertIfAddrsMultiAddressesOnOneInterface) { |
| 757 | char if_name[20] = "rmnet0"; |
| 758 | ifaddrs* list = nullptr; |
| 759 | list = AddIpv6Address(list, if_name, "1000:2000:3000:4000:0:0:0:1", |
| 760 | "FFFF:FFFF:FFFF:FFFF::", 0); |
| 761 | list = AddIpv6Address(list, if_name, "1000:2000:3000:4000:0:0:0:2", |
| 762 | "FFFF:FFFF:FFFF:FFFF::", 0); |
| 763 | NetworkManager::NetworkList result; |
| 764 | BasicNetworkManager manager; |
| 765 | CallConvertIfAddrs(manager, list, true, &result); |
| 766 | EXPECT_EQ(1U, result.size()); |
| 767 | bool changed; |
| 768 | // This ensures we release the objects created in CallConvertIfAddrs. |
| 769 | MergeNetworkList(manager, result, &changed); |
| 770 | ReleaseIfAddrs(list); |
| 771 | } |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 772 | |
| 773 | TEST_F(NetworkTest, TestConvertIfAddrsNotRunning) { |
| 774 | ifaddrs list; |
| 775 | memset(&list, 0, sizeof(list)); |
| 776 | list.ifa_name = const_cast<char*>("test_iface"); |
| 777 | sockaddr ifa_addr; |
| 778 | sockaddr ifa_netmask; |
| 779 | list.ifa_addr = &ifa_addr; |
| 780 | list.ifa_netmask = &ifa_netmask; |
| 781 | |
| 782 | NetworkManager::NetworkList result; |
| 783 | BasicNetworkManager manager; |
| 784 | CallConvertIfAddrs(manager, &list, true, &result); |
| 785 | EXPECT_TRUE(result.empty()); |
| 786 | } |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 787 | |
| 788 | // Tests that the network type can be updated after the network monitor is |
| 789 | // started. |
| 790 | TEST_F(NetworkTest, TestGetAdapterTypeFromNetworkMonitor) { |
| 791 | char if_name1[20] = "wifi0"; |
| 792 | std::string ipv6_address1 = "1000:2000:3000:4000:0:0:0:1"; |
| 793 | std::string ipv6_address2 = "1000:2000:3000:8000:0:0:0:1"; |
| 794 | std::string ipv6_mask = "FFFF:FFFF:FFFF:FFFF::"; |
| 795 | BasicNetworkManager manager; |
| 796 | // A network created before the network monitor is started will get |
| 797 | // UNKNOWN type. |
| 798 | ifaddrs* addr_list = |
| 799 | InstallIpv6Network(if_name1, ipv6_address1, ipv6_mask, manager); |
| 800 | EXPECT_EQ(ADAPTER_TYPE_UNKNOWN, GetAdapterType(manager)); |
| 801 | ReleaseIfAddrs(addr_list); |
| 802 | // Note: Do not call ClearNetworks here in order to test that the type |
| 803 | // of an existing network can be changed after the network monitor starts |
| 804 | // and detects the network type correctly. |
| 805 | |
| 806 | // After the network monitor starts, the type will be updated. |
| 807 | FakeNetworkMonitorFactory* factory = new FakeNetworkMonitorFactory(); |
| 808 | NetworkMonitorFactory::SetFactory(factory); |
| 809 | // This brings up the hook with the network monitor. |
| 810 | manager.StartUpdating(); |
| 811 | // Add the same ipv6 address as before but it has the right network type |
| 812 | // detected by the network monitor now. |
| 813 | addr_list = InstallIpv6Network(if_name1, ipv6_address1, ipv6_mask, manager); |
| 814 | EXPECT_EQ(ADAPTER_TYPE_WIFI, GetAdapterType(manager)); |
| 815 | ReleaseIfAddrs(addr_list); |
| 816 | ClearNetworks(manager); |
| 817 | |
| 818 | // Add another network with the type inferred from the network monitor. |
| 819 | char if_name2[20] = "cellular0"; |
| 820 | addr_list = InstallIpv6Network(if_name2, ipv6_address2, ipv6_mask, manager); |
| 821 | EXPECT_EQ(ADAPTER_TYPE_CELLULAR, GetAdapterType(manager)); |
| 822 | ReleaseIfAddrs(addr_list); |
| 823 | ClearNetworks(manager); |
| 824 | } |
| 825 | |
| 826 | // Test that the network type can be determined based on name matching in |
| 827 | // a few cases. Note that UNKNOWN type for non-matching strings has been tested |
| 828 | // in the above test. |
| 829 | TEST_F(NetworkTest, TestGetAdapterTypeFromNameMatching) { |
| 830 | std::string ipv6_address1 = "1000:2000:3000:4000:0:0:0:1"; |
| 831 | std::string ipv6_address2 = "1000:2000:3000:8000:0:0:0:1"; |
| 832 | std::string ipv6_mask = "FFFF:FFFF:FFFF:FFFF::"; |
| 833 | BasicNetworkManager manager; |
| 834 | |
deadbeef | 4cd599f | 2017-07-27 15:05:29 -0700 | [diff] [blame] | 835 | // IPSec interface; name is in form "ipsec<index>". |
| 836 | char if_name[20] = "ipsec11"; |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 837 | ifaddrs* addr_list = |
| 838 | InstallIpv6Network(if_name, ipv6_address1, ipv6_mask, manager); |
deadbeef | 4cd599f | 2017-07-27 15:05:29 -0700 | [diff] [blame] | 839 | EXPECT_EQ(ADAPTER_TYPE_VPN, GetAdapterType(manager)); |
| 840 | ClearNetworks(manager); |
| 841 | ReleaseIfAddrs(addr_list); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 842 | |
deadbeef | 4cd599f | 2017-07-27 15:05:29 -0700 | [diff] [blame] | 843 | #if defined(WEBRTC_IOS) |
| 844 | strcpy(if_name, "pdp_ip0"); |
| 845 | addr_list = InstallIpv6Network(if_name, ipv6_address1, ipv6_mask, manager); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 846 | EXPECT_EQ(ADAPTER_TYPE_CELLULAR, GetAdapterType(manager)); |
| 847 | ClearNetworks(manager); |
| 848 | ReleaseIfAddrs(addr_list); |
| 849 | |
Honghai Zhang | 63ab810 | 2016-05-26 20:30:15 -0700 | [diff] [blame] | 850 | strcpy(if_name, "en0"); |
| 851 | addr_list = InstallIpv6Network(if_name, ipv6_address1, ipv6_mask, manager); |
| 852 | EXPECT_EQ(ADAPTER_TYPE_WIFI, GetAdapterType(manager)); |
| 853 | ClearNetworks(manager); |
| 854 | ReleaseIfAddrs(addr_list); |
| 855 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 856 | #elif defined(WEBRTC_ANDROID) |
deadbeef | 4cd599f | 2017-07-27 15:05:29 -0700 | [diff] [blame] | 857 | strcpy(if_name, "rmnet0"); |
| 858 | addr_list = InstallIpv6Network(if_name, ipv6_address1, ipv6_mask, manager); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 859 | EXPECT_EQ(ADAPTER_TYPE_CELLULAR, GetAdapterType(manager)); |
| 860 | ClearNetworks(manager); |
| 861 | ReleaseIfAddrs(addr_list); |
| 862 | |
| 863 | strcpy(if_name, "wlan1"); |
| 864 | addr_list = InstallIpv6Network(if_name, ipv6_address2, ipv6_mask, manager); |
| 865 | EXPECT_EQ(ADAPTER_TYPE_WIFI, GetAdapterType(manager)); |
| 866 | ClearNetworks(manager); |
| 867 | ReleaseIfAddrs(addr_list); |
| 868 | |
| 869 | strcpy(if_name, "v4-rmnet_data0"); |
| 870 | addr_list = InstallIpv6Network(if_name, ipv6_address2, ipv6_mask, manager); |
| 871 | EXPECT_EQ(ADAPTER_TYPE_CELLULAR, GetAdapterType(manager)); |
| 872 | ClearNetworks(manager); |
| 873 | ReleaseIfAddrs(addr_list); |
| 874 | #else |
deadbeef | 4cd599f | 2017-07-27 15:05:29 -0700 | [diff] [blame] | 875 | // TODO(deadbeef): If not iOS or Android, "wlan0" should be treated as |
| 876 | // "unknown"? Why? This should be fixed if there's no good reason. |
| 877 | strcpy(if_name, "wlan0"); |
| 878 | addr_list = InstallIpv6Network(if_name, ipv6_address1, ipv6_mask, manager); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 879 | |
| 880 | EXPECT_EQ(ADAPTER_TYPE_UNKNOWN, GetAdapterType(manager)); |
| 881 | ClearNetworks(manager); |
| 882 | ReleaseIfAddrs(addr_list); |
| 883 | #endif |
| 884 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 885 | #endif // defined(WEBRTC_POSIX) |
| 886 | |
| 887 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
| 888 | // If you want to test non-default routes, you can do the following on a linux |
| 889 | // machine: |
| 890 | // 1) Load the dummy network driver: |
| 891 | // sudo modprobe dummy |
| 892 | // sudo ifconfig dummy0 127.0.0.1 |
| 893 | // 2) Run this test and confirm the output says it found a dummy route (and |
| 894 | // passes). |
| 895 | // 3) When done: |
| 896 | // sudo rmmmod dummy |
| 897 | TEST_F(NetworkTest, TestIgnoreNonDefaultRoutes) { |
| 898 | BasicNetworkManager manager; |
| 899 | NetworkManager::NetworkList list; |
| 900 | list = GetNetworks(manager, false); |
| 901 | bool found_dummy = false; |
| 902 | LOG(LS_INFO) << "Looking for dummy network: "; |
| 903 | for (NetworkManager::NetworkList::iterator it = list.begin(); |
| 904 | it != list.end(); ++it) { |
| 905 | LOG(LS_INFO) << " Network name: " << (*it)->name(); |
| 906 | found_dummy |= (*it)->name().find("dummy0") != std::string::npos; |
| 907 | } |
| 908 | for (NetworkManager::NetworkList::iterator it = list.begin(); |
| 909 | it != list.end(); ++it) { |
| 910 | delete (*it); |
| 911 | } |
| 912 | if (!found_dummy) { |
| 913 | LOG(LS_INFO) << "No dummy found, quitting."; |
| 914 | return; |
| 915 | } |
| 916 | LOG(LS_INFO) << "Found dummy, running again while ignoring non-default " |
| 917 | << "routes."; |
| 918 | manager.set_ignore_non_default_routes(true); |
| 919 | list = GetNetworks(manager, false); |
| 920 | for (NetworkManager::NetworkList::iterator it = list.begin(); |
| 921 | it != list.end(); ++it) { |
| 922 | LOG(LS_INFO) << " Network name: " << (*it)->name(); |
| 923 | EXPECT_TRUE((*it)->name().find("dummy0") == std::string::npos); |
| 924 | } |
| 925 | for (NetworkManager::NetworkList::iterator it = list.begin(); |
| 926 | it != list.end(); ++it) { |
| 927 | delete (*it); |
| 928 | } |
| 929 | } |
| 930 | #endif |
| 931 | |
guoweis@webrtc.org | 4bbd3c8 | 2014-09-09 13:54:45 +0000 | [diff] [blame] | 932 | // Test MergeNetworkList successfully combines all IPs for the same |
| 933 | // prefix/length into a single Network. |
| 934 | TEST_F(NetworkTest, TestMergeNetworkList) { |
| 935 | BasicNetworkManager manager; |
| 936 | NetworkManager::NetworkList list; |
| 937 | |
| 938 | // Create 2 IPAddress classes with only last digit different. |
| 939 | IPAddress ip1, ip2; |
| 940 | EXPECT_TRUE(IPFromString("2400:4030:1:2c00:be30:0:0:1", &ip1)); |
| 941 | EXPECT_TRUE(IPFromString("2400:4030:1:2c00:be30:0:0:2", &ip2)); |
| 942 | |
| 943 | // Create 2 networks with the same prefix and length. |
| 944 | Network* net1 = new Network("em1", "em1", TruncateIP(ip1, 64), 64); |
| 945 | Network* net2 = new Network("em1", "em1", TruncateIP(ip1, 64), 64); |
| 946 | |
| 947 | // Add different IP into each. |
| 948 | net1->AddIP(ip1); |
| 949 | net2->AddIP(ip2); |
| 950 | |
| 951 | list.push_back(net1); |
| 952 | list.push_back(net2); |
| 953 | bool changed; |
| 954 | MergeNetworkList(manager, list, &changed); |
| 955 | EXPECT_TRUE(changed); |
| 956 | |
| 957 | NetworkManager::NetworkList list2; |
| 958 | manager.GetNetworks(&list2); |
| 959 | |
| 960 | // Make sure the resulted networklist has only 1 element and 2 |
| 961 | // IPAddresses. |
| 962 | EXPECT_EQ(list2.size(), 1uL); |
| 963 | EXPECT_EQ(list2[0]->GetIPs().size(), 2uL); |
| 964 | EXPECT_EQ(list2[0]->GetIPs()[0], ip1); |
| 965 | EXPECT_EQ(list2[0]->GetIPs()[1], ip2); |
| 966 | } |
| 967 | |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 968 | // Test that MergeNetworkList successfully detects the change if |
| 969 | // a network becomes inactive and then active again. |
| 970 | TEST_F(NetworkTest, TestMergeNetworkListWithInactiveNetworks) { |
| 971 | BasicNetworkManager manager; |
| 972 | Network network1("test_wifi", "Test Network Adapter 1", |
| 973 | IPAddress(0x12345600U), 24); |
| 974 | Network network2("test_eth0", "Test Network Adapter 2", |
| 975 | IPAddress(0x00010000U), 16); |
| 976 | network1.AddIP(IPAddress(0x12345678)); |
| 977 | network2.AddIP(IPAddress(0x00010004)); |
| 978 | NetworkManager::NetworkList list; |
| 979 | Network* net1 = new Network(network1); |
| 980 | list.push_back(net1); |
| 981 | bool changed; |
| 982 | MergeNetworkList(manager, list, &changed); |
| 983 | EXPECT_TRUE(changed); |
| 984 | list.clear(); |
| 985 | manager.GetNetworks(&list); |
| 986 | ASSERT_EQ(1U, list.size()); |
| 987 | EXPECT_EQ(net1, list[0]); |
| 988 | |
| 989 | list.clear(); |
| 990 | Network* net2 = new Network(network2); |
| 991 | list.push_back(net2); |
| 992 | MergeNetworkList(manager, list, &changed); |
| 993 | EXPECT_TRUE(changed); |
| 994 | list.clear(); |
| 995 | manager.GetNetworks(&list); |
| 996 | ASSERT_EQ(1U, list.size()); |
| 997 | EXPECT_EQ(net2, list[0]); |
| 998 | |
| 999 | // Now network1 is inactive. Try to merge it again. |
| 1000 | list.clear(); |
| 1001 | list.push_back(new Network(network1)); |
| 1002 | MergeNetworkList(manager, list, &changed); |
| 1003 | EXPECT_TRUE(changed); |
| 1004 | list.clear(); |
| 1005 | manager.GetNetworks(&list); |
| 1006 | ASSERT_EQ(1U, list.size()); |
| 1007 | EXPECT_TRUE(list[0]->active()); |
| 1008 | EXPECT_EQ(net1, list[0]); |
| 1009 | } |
| 1010 | |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1011 | // Test that the filtering logic follows the defined ruleset in network.h. |
| 1012 | TEST_F(NetworkTest, TestIPv6Selection) { |
| 1013 | InterfaceAddress ip; |
| 1014 | std::string ipstr; |
| 1015 | |
| 1016 | ipstr = "2401:fa00:4:1000:be30:5bff:fee5:c3"; |
| 1017 | ASSERT_TRUE(IPFromString(ipstr, IPV6_ADDRESS_FLAG_DEPRECATED, &ip)); |
| 1018 | |
| 1019 | // Create a network with this prefix. |
| 1020 | Network ipv6_network( |
| 1021 | "test_eth0", "Test NetworkAdapter", TruncateIP(ip, 64), 64); |
| 1022 | |
| 1023 | // When there is no address added, it should return an unspecified |
| 1024 | // address. |
| 1025 | EXPECT_EQ(ipv6_network.GetBestIP(), IPAddress()); |
| 1026 | EXPECT_TRUE(IPIsUnspec(ipv6_network.GetBestIP())); |
| 1027 | |
| 1028 | // Deprecated one should not be returned. |
| 1029 | ipv6_network.AddIP(ip); |
| 1030 | EXPECT_EQ(ipv6_network.GetBestIP(), IPAddress()); |
| 1031 | |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 1032 | // Add ULA one. ULA is unique local address which is starting either |
| 1033 | // with 0xfc or 0xfd. |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1034 | ipstr = "fd00:fa00:4:1000:be30:5bff:fee5:c4"; |
| 1035 | ASSERT_TRUE(IPFromString(ipstr, IPV6_ADDRESS_FLAG_NONE, &ip)); |
| 1036 | ipv6_network.AddIP(ip); |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 1037 | EXPECT_EQ(ipv6_network.GetBestIP(), static_cast<IPAddress>(ip)); |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1038 | |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 1039 | // Add global one. |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1040 | ipstr = "2401:fa00:4:1000:be30:5bff:fee5:c5"; |
| 1041 | ASSERT_TRUE(IPFromString(ipstr, IPV6_ADDRESS_FLAG_NONE, &ip)); |
| 1042 | ipv6_network.AddIP(ip); |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 1043 | EXPECT_EQ(ipv6_network.GetBestIP(), static_cast<IPAddress>(ip)); |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1044 | |
| 1045 | // Add global dynamic temporary one. |
| 1046 | ipstr = "2401:fa00:4:1000:be30:5bff:fee5:c6"; |
| 1047 | ASSERT_TRUE(IPFromString(ipstr, IPV6_ADDRESS_FLAG_TEMPORARY, &ip)); |
| 1048 | ipv6_network.AddIP(ip); |
| 1049 | EXPECT_EQ(ipv6_network.GetBestIP(), static_cast<IPAddress>(ip)); |
| 1050 | } |
| 1051 | |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 1052 | TEST_F(NetworkTest, TestNetworkMonitoring) { |
| 1053 | BasicNetworkManager manager; |
| 1054 | manager.SignalNetworksChanged.connect(static_cast<NetworkTest*>(this), |
| 1055 | &NetworkTest::OnNetworksChanged); |
| 1056 | FakeNetworkMonitorFactory* factory = new FakeNetworkMonitorFactory(); |
| 1057 | NetworkMonitorFactory::SetFactory(factory); |
| 1058 | manager.StartUpdating(); |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 1059 | FakeNetworkMonitor* network_monitor = GetNetworkMonitor(manager); |
| 1060 | EXPECT_TRUE(network_monitor && network_monitor->started()); |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 1061 | EXPECT_TRUE_WAIT(callback_called_, 1000); |
| 1062 | callback_called_ = false; |
| 1063 | |
| 1064 | // Clear the networks so that there will be network changes below. |
| 1065 | ClearNetworks(manager); |
| 1066 | // Network manager is started, so the callback is called when the network |
| 1067 | // monitor fires the network-change event. |
| 1068 | network_monitor->OnNetworksChanged(); |
| 1069 | EXPECT_TRUE_WAIT(callback_called_, 1000); |
| 1070 | |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 1071 | // Network manager is stopped. |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 1072 | manager.StopUpdating(); |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 1073 | EXPECT_FALSE(GetNetworkMonitor(manager)->started()); |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 1074 | |
| 1075 | NetworkMonitorFactory::ReleaseFactory(factory); |
| 1076 | } |
| 1077 | |
Edward Lemur | 8dc945c | 2016-07-21 10:16:40 +0200 | [diff] [blame] | 1078 | // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
| 1079 | #if defined(WEBRTC_ANDROID) |
| 1080 | #define MAYBE_DefaultLocalAddress DISABLED_DefaultLocalAddress |
| 1081 | #else |
| 1082 | #define MAYBE_DefaultLocalAddress DefaultLocalAddress |
| 1083 | #endif |
| 1084 | TEST_F(NetworkTest, MAYBE_DefaultLocalAddress) { |
Guo-wei Shieh | a34c39e | 2015-11-25 13:12:26 -0800 | [diff] [blame] | 1085 | IPAddress ip; |
guoweis | 56271ed | 2016-01-15 14:45:06 -0800 | [diff] [blame] | 1086 | TestBasicNetworkManager manager; |
| 1087 | manager.SignalNetworksChanged.connect(static_cast<NetworkTest*>(this), |
| 1088 | &NetworkTest::OnNetworksChanged); |
| 1089 | FakeNetworkMonitorFactory* factory = new FakeNetworkMonitorFactory(); |
| 1090 | NetworkMonitorFactory::SetFactory(factory); |
| 1091 | manager.StartUpdating(); |
| 1092 | EXPECT_TRUE_WAIT(callback_called_, 1000); |
Guo-wei Shieh | a34c39e | 2015-11-25 13:12:26 -0800 | [diff] [blame] | 1093 | |
| 1094 | // Make sure we can query default local address when an address for such |
| 1095 | // address family exists. |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1096 | std::vector<Network*> networks; |
| 1097 | manager.GetNetworks(&networks); |
guoweis | 56271ed | 2016-01-15 14:45:06 -0800 | [diff] [blame] | 1098 | EXPECT_TRUE(!networks.empty()); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame^] | 1099 | for (const auto* network : networks) { |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1100 | if (network->GetBestIP().family() == AF_INET) { |
| 1101 | EXPECT_TRUE(manager.QueryDefaultLocalAddress(AF_INET) != IPAddress()); |
guoweis | 56271ed | 2016-01-15 14:45:06 -0800 | [diff] [blame] | 1102 | } else if (network->GetBestIP().family() == AF_INET6 && |
| 1103 | !IPIsLoopback(network->GetBestIP())) { |
| 1104 | // Existence of an IPv6 loopback address doesn't mean it has IPv6 network |
| 1105 | // enabled. |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1106 | EXPECT_TRUE(manager.QueryDefaultLocalAddress(AF_INET6) != IPAddress()); |
| 1107 | } |
| 1108 | } |
Guo-wei Shieh | a34c39e | 2015-11-25 13:12:26 -0800 | [diff] [blame] | 1109 | |
| 1110 | // GetDefaultLocalAddress should return the valid default address after set. |
| 1111 | manager.set_default_local_addresses(GetLoopbackIP(AF_INET), |
| 1112 | GetLoopbackIP(AF_INET6)); |
| 1113 | EXPECT_TRUE(manager.GetDefaultLocalAddress(AF_INET, &ip)); |
| 1114 | EXPECT_EQ(ip, GetLoopbackIP(AF_INET)); |
| 1115 | EXPECT_TRUE(manager.GetDefaultLocalAddress(AF_INET6, &ip)); |
| 1116 | EXPECT_EQ(ip, GetLoopbackIP(AF_INET6)); |
honghaiz | af83fe6 | 2016-04-18 14:50:44 -0700 | [diff] [blame] | 1117 | |
| 1118 | // More tests on GetDefaultLocalAddress with ipv6 addresses where the set |
| 1119 | // default address may be different from the best IP address of any network. |
| 1120 | InterfaceAddress ip1; |
| 1121 | EXPECT_TRUE(IPFromString("abcd::1234:5678:abcd:1111", |
| 1122 | IPV6_ADDRESS_FLAG_TEMPORARY, &ip1)); |
| 1123 | // Create a network with a prefix of ip1. |
| 1124 | Network ipv6_network("test_eth0", "Test NetworkAdapter", TruncateIP(ip1, 64), |
| 1125 | 64); |
| 1126 | IPAddress ip2; |
| 1127 | EXPECT_TRUE(IPFromString("abcd::1234:5678:abcd:2222", &ip2)); |
| 1128 | ipv6_network.AddIP(ip1); |
| 1129 | ipv6_network.AddIP(ip2); |
| 1130 | BasicNetworkManager::NetworkList list(1, new Network(ipv6_network)); |
| 1131 | bool changed; |
| 1132 | MergeNetworkList(manager, list, &changed); |
| 1133 | // If the set default address is not in any network, GetDefaultLocalAddress |
| 1134 | // should return it. |
| 1135 | IPAddress ip3; |
| 1136 | EXPECT_TRUE(IPFromString("abcd::1234:5678:abcd:3333", &ip3)); |
| 1137 | manager.set_default_local_addresses(GetLoopbackIP(AF_INET), ip3); |
| 1138 | EXPECT_TRUE(manager.GetDefaultLocalAddress(AF_INET6, &ip)); |
| 1139 | EXPECT_EQ(ip3, ip); |
| 1140 | // If the set default address is in a network, GetDefaultLocalAddress will |
| 1141 | // return the best IP in that network. |
| 1142 | manager.set_default_local_addresses(GetLoopbackIP(AF_INET), ip2); |
| 1143 | EXPECT_TRUE(manager.GetDefaultLocalAddress(AF_INET6, &ip)); |
| 1144 | EXPECT_EQ(static_cast<IPAddress>(ip1), ip); |
| 1145 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1146 | manager.StopUpdating(); |
| 1147 | } |
| 1148 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1149 | } // namespace rtc |