blob: 49b5e04b4425b8581793d5d03e8b13b0b81ef42e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/network.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
oprypin1ea631f2017-08-18 00:15:19 -070013#include <stdlib.h>
14
jbauch555604a2016-04-26 03:13:22 -070015#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include <vector>
oprypin1ea631f2017-08-18 00:15:19 -070017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/nethelpers.h"
20#include "rtc_base/networkmonitor.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021#if defined(WEBRTC_POSIX)
kjellanderf1c5ebf2017-06-30 05:27:14 -070022#include <sys/types.h>
Henrik Kjellander00725112017-06-30 15:14:45 +020023#include <net/if.h>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/ifaddrs_converter.h"
Guo-wei Shieh9faf1542015-12-28 14:06:55 -080025#endif // defined(WEBRTC_POSIX)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/gunit.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/logging.h" // For LOG_GLE
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029#endif
30
31namespace rtc {
32
Guo-wei Shieh9faf1542015-12-28 14:06:55 -080033namespace {
34
honghaiz023f3ef2015-10-19 09:39:32 -070035class FakeNetworkMonitor : public NetworkMonitorBase {
36 public:
honghaizcec0a082016-01-15 14:49:09 -080037 void Start() override { started_ = true; }
38 void Stop() override { started_ = false; }
39 bool started() { return started_; }
honghaiza7ad7c32016-02-02 12:54:14 -080040 AdapterType GetAdapterType(const std::string& if_name) override {
Honghai Zhang351d77b2016-05-20 15:08:29 -070041 // 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 }
honghaiza7ad7c32016-02-02 12:54:14 -080049 return ADAPTER_TYPE_UNKNOWN;
50 }
honghaizcec0a082016-01-15 14:49:09 -080051
52 private:
53 bool started_ = false;
honghaiz023f3ef2015-10-19 09:39:32 -070054};
55
56class FakeNetworkMonitorFactory : public NetworkMonitorFactory {
57 public:
58 FakeNetworkMonitorFactory() {}
honghaizcec0a082016-01-15 14:49:09 -080059 NetworkMonitorInterface* CreateNetworkMonitor() override {
honghaiz023f3ef2015-10-19 09:39:32 -070060 return new FakeNetworkMonitor();
61 }
62};
63
Guo-wei Shieh9faf1542015-12-28 14:06:55 -080064} // namespace
65
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066class 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.orga094cac2015-01-28 19:34:05 +000074 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.orgf0488722014-05-13 18:00:26 +000081 }
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
honghaizcec0a082016-01-15 14:49:09 -080095 FakeNetworkMonitor* GetNetworkMonitor(BasicNetworkManager& network_manager) {
96 return static_cast<FakeNetworkMonitor*>(
97 network_manager.network_monitor_.get());
honghaiz023f3ef2015-10-19 09:39:32 -070098 }
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 Zhang351d77b2016-05-20 15:08:29 -0700107 AdapterType GetAdapterType(BasicNetworkManager& network_manager) {
108 BasicNetworkManager::NetworkList list;
109 network_manager.GetNetworks(&list);
nissec16fa5e2017-02-07 07:18:43 -0800110 RTC_CHECK_EQ(1, list.size());
Honghai Zhang351d77b2016-05-20 15:08:29 -0700111 return list[0]->type();
112 }
113
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114#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 Shieh9faf1542015-12-28 14:06:55 -0800120 // Use the base IfAddrsConverter for test cases.
jbauch555604a2016-04-26 03:13:22 -0700121 std::unique_ptr<IfAddrsConverter> ifaddrs_converter(new IfAddrsConverter());
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800122 network_manager.ConvertIfAddrs(interfaces, ifaddrs_converter.get(),
123 include_ignored, networks);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124 }
honghaizdb8cf502015-12-21 13:08:46 -0800125
126 struct sockaddr_in6* CreateIpv6Addr(const std::string& ip_string,
127 uint32_t scope_id) {
oprypin1ea631f2017-08-18 00:15:19 -0700128 struct sockaddr_in6* ipv6_addr = static_cast<struct sockaddr_in6*>(
129 malloc(sizeof(struct sockaddr_in6)));
honghaizdb8cf502015-12-21 13:08:46 -0800130 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 Shieh9faf1542015-12-28 14:06:55 -0800153 if_addr->ifa_flags = IFF_RUNNING;
honghaizdb8cf502015-12-21 13:08:46 -0800154 return if_addr;
155 }
156
Honghai Zhang351d77b2016-05-20 15:08:29 -0700157 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
honghaizdb8cf502015-12-21 13:08:46 -0800171 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;
oprypin1ea631f2017-08-18 00:15:19 -0700175 free(if_addr->ifa_addr);
176 free(if_addr->ifa_netmask);
honghaizdb8cf502015-12-21 13:08:46 -0800177 delete if_addr;
178 if_addr = next_addr;
179 }
180 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000181#endif // defined(WEBRTC_POSIX)
182
183 protected:
184 bool callback_called_;
185};
186
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800187class TestBasicNetworkManager : public BasicNetworkManager {
188 public:
189 using BasicNetworkManager::QueryDefaultLocalAddress;
Guo-wei Shieha34c39e2015-11-25 13:12:26 -0800190 using BasicNetworkManager::set_default_local_addresses;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800191};
192
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000193// Test that the Network ctor works properly.
194TEST_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.org006521d2015-02-12 09:23:59 +0000204TEST_F(NetworkTest, TestIsIgnoredNetworkIgnoresIPsStartingWith0) {
205 Network ipv4_network1("test_eth0", "Test Network Adapter 1",
206 IPAddress(0x12345600U), 24, ADAPTER_TYPE_ETHERNET);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000207 Network ipv4_network2("test_eth1", "Test Network Adapter 2",
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000208 IPAddress(0x010000U), 24, ADAPTER_TYPE_ETHERNET);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000209 BasicNetworkManager network_manager;
210 EXPECT_FALSE(IsIgnoredNetwork(network_manager, ipv4_network1));
211 EXPECT_TRUE(IsIgnoredNetwork(network_manager, ipv4_network2));
212}
213
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000214// TODO(phoglund): Remove when ignore list goes away.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000215TEST_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
231TEST_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.org369a6372014-09-17 22:37:29 +0000241 IPAddress ip = (*it)->GetBestIP();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000242 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 Shieh47872ec2015-08-19 10:32:46 -0700266// Test StartUpdating() and StopUpdating(). network_permission_state starts with
267// ALLOWED.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000268TEST_F(NetworkTest, TestUpdateNetworks) {
269 BasicNetworkManager manager;
270 manager.SignalNetworksChanged.connect(
271 static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged);
guoweisea1012b2015-08-21 09:06:28 -0700272 EXPECT_EQ(NetworkManager::ENUMERATION_ALLOWED,
273 manager.enumeration_permission());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000274 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();
guoweisea1012b2015-08-21 09:06:28 -0700287 EXPECT_EQ(NetworkManager::ENUMERATION_ALLOWED,
288 manager.enumeration_permission());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289 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.
301TEST_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.orga094cac2015-01-28 19:34:05 +0000314 NetworkManager::Stats stats = MergeNetworkList(manager, list, &changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000315 EXPECT_TRUE(changed);
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000316 EXPECT_EQ(stats.ipv6_network_count, 0);
317 EXPECT_EQ(stats.ipv4_network_count, 1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000318 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];
honghaiza0c44ea2016-03-23 16:07:48 -0700324 uint16_t net_id1 = net1->id();
325 EXPECT_EQ(1, net_id1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000326 list.clear();
327
328 // Replace ipv4_network1 with ipv4_network2.
329 list.push_back(new Network(ipv4_network2));
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000330 stats = MergeNetworkList(manager, list, &changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000331 EXPECT_TRUE(changed);
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000332 EXPECT_EQ(stats.ipv6_network_count, 0);
333 EXPECT_EQ(stats.ipv4_network_count, 1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000334 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];
honghaiza0c44ea2016-03-23 16:07:48 -0700340 uint16_t net_id2 = net2->id();
341 // Network id will increase.
342 EXPECT_LT(net_id1, net_id2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343 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.orga094cac2015-01-28 19:34:05 +0000348 stats = MergeNetworkList(manager, list, &changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000349 EXPECT_TRUE(changed);
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000350 EXPECT_EQ(stats.ipv6_network_count, 0);
351 EXPECT_EQ(stats.ipv4_network_count, 2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000352 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]));
honghaiza0c44ea2016-03-23 16:07:48 -0700359 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.orgf0488722014-05-13 18:00:26 +0000361 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.orga094cac2015-01-28 19:34:05 +0000367 stats = MergeNetworkList(manager, list, &changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000368 EXPECT_FALSE(changed);
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000369 EXPECT_EQ(stats.ipv6_network_count, 0);
370 EXPECT_EQ(stats.ipv4_network_count, 2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000371 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]));
honghaiza0c44ea2016-03-23 16:07:48 -0700378 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.orgf0488722014-05-13 18:00:26 +0000380 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.
385void SetupNetworks(NetworkManager::NetworkList* list) {
386 IPAddress ip;
387 IPAddress prefix;
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000388 EXPECT_TRUE(IPFromString("abcd::1234:5678:abcd:ef12", &ip));
389 EXPECT_TRUE(IPFromString("abcd::", &prefix));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000390 // 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.orgbbce5ef2015-03-05 04:38:29 +0000394 EXPECT_TRUE(IPFromString("abcd::5678:abcd:ef12:3456", &ip));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000395 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.
416TEST_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.orga094cac2015-01-28 19:34:05 +0000423 NetworkManager::Stats stats =
424 MergeNetworkList(manager, original_list, &changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000425 EXPECT_TRUE(changed);
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000426 EXPECT_EQ(stats.ipv6_network_count, 4);
427 EXPECT_EQ(stats.ipv4_network_count, 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000428 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.
441TEST_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.
476TEST_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).
514TEST_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.
572TEST_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
honghaizdb8cf502015-12-21 13:08:46 -0800615// Test that DumpNetworks does not crash.
616TEST_F(NetworkTest, TestCreateAndDumpNetworks) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000617 BasicNetworkManager manager;
honghaizdb8cf502015-12-21 13:08:46 -0800618 NetworkManager::NetworkList list = GetNetworks(manager, true);
619 bool changed;
620 MergeNetworkList(manager, list, &changed);
621 manager.DumpNetworks();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000622}
623
624// Test that we can toggle IPv6 on and off.
minyue5d696482015-08-19 04:42:03 -0700625// 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
631TEST_F(NetworkTest, MAYBE_TestIPv6Toggle) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000632 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
deadbeef3427f532017-07-26 16:09:33 -0700671// Test that when network interfaces are sorted and given preference values,
672// IPv6 comes first.
673TEST_F(NetworkTest, IPv6NetworksPreferredOverIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000674 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
deadbeef3427f532017-07-26 16:09:33 -0700700// When two interfaces are equivalent in everything but name, they're expected
701// to be preference-ordered by name. For example, "eth0" before "eth1".
702TEST_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.orgf0488722014-05-13 18:00:26 +0000723TEST_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.
743TEST_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}
honghaizdb8cf502015-12-21 13:08:46 -0800753
754// Verify that if there are two addresses on one interface, only one network
755// is generated.
756TEST_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 Shieh9faf1542015-12-28 14:06:55 -0800772
773TEST_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 Zhang351d77b2016-05-20 15:08:29 -0700787
788// Tests that the network type can be updated after the network monitor is
789// started.
790TEST_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.
829TEST_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
deadbeef4cd599f2017-07-27 15:05:29 -0700835 // IPSec interface; name is in form "ipsec<index>".
836 char if_name[20] = "ipsec11";
Honghai Zhang351d77b2016-05-20 15:08:29 -0700837 ifaddrs* addr_list =
838 InstallIpv6Network(if_name, ipv6_address1, ipv6_mask, manager);
deadbeef4cd599f2017-07-27 15:05:29 -0700839 EXPECT_EQ(ADAPTER_TYPE_VPN, GetAdapterType(manager));
840 ClearNetworks(manager);
841 ReleaseIfAddrs(addr_list);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700842
deadbeef4cd599f2017-07-27 15:05:29 -0700843#if defined(WEBRTC_IOS)
844 strcpy(if_name, "pdp_ip0");
845 addr_list = InstallIpv6Network(if_name, ipv6_address1, ipv6_mask, manager);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700846 EXPECT_EQ(ADAPTER_TYPE_CELLULAR, GetAdapterType(manager));
847 ClearNetworks(manager);
848 ReleaseIfAddrs(addr_list);
849
Honghai Zhang63ab8102016-05-26 20:30:15 -0700850 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 Zhang351d77b2016-05-20 15:08:29 -0700856#elif defined(WEBRTC_ANDROID)
deadbeef4cd599f2017-07-27 15:05:29 -0700857 strcpy(if_name, "rmnet0");
858 addr_list = InstallIpv6Network(if_name, ipv6_address1, ipv6_mask, manager);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700859 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
deadbeef4cd599f2017-07-27 15:05:29 -0700875 // 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 Zhang351d77b2016-05-20 15:08:29 -0700879
880 EXPECT_EQ(ADAPTER_TYPE_UNKNOWN, GetAdapterType(manager));
881 ClearNetworks(manager);
882 ReleaseIfAddrs(addr_list);
883#endif
884}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000885#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
897TEST_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.org4bbd3c82014-09-09 13:54:45 +0000932// Test MergeNetworkList successfully combines all IPs for the same
933// prefix/length into a single Network.
934TEST_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
honghaizdb8cf502015-12-21 13:08:46 -0800968// Test that MergeNetworkList successfully detects the change if
969// a network becomes inactive and then active again.
970TEST_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.org369a6372014-09-17 22:37:29 +00001011// Test that the filtering logic follows the defined ruleset in network.h.
1012TEST_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.org07dcf602015-02-27 18:42:22 +00001032 // Add ULA one. ULA is unique local address which is starting either
1033 // with 0xfc or 0xfd.
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001034 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.org07dcf602015-02-27 18:42:22 +00001037 EXPECT_EQ(ipv6_network.GetBestIP(), static_cast<IPAddress>(ip));
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001038
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +00001039 // Add global one.
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001040 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.org07dcf602015-02-27 18:42:22 +00001043 EXPECT_EQ(ipv6_network.GetBestIP(), static_cast<IPAddress>(ip));
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001044
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
honghaiz023f3ef2015-10-19 09:39:32 -07001052TEST_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();
honghaizcec0a082016-01-15 14:49:09 -08001059 FakeNetworkMonitor* network_monitor = GetNetworkMonitor(manager);
1060 EXPECT_TRUE(network_monitor && network_monitor->started());
honghaiz023f3ef2015-10-19 09:39:32 -07001061 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
honghaizcec0a082016-01-15 14:49:09 -08001071 // Network manager is stopped.
honghaiz023f3ef2015-10-19 09:39:32 -07001072 manager.StopUpdating();
honghaizcec0a082016-01-15 14:49:09 -08001073 EXPECT_FALSE(GetNetworkMonitor(manager)->started());
honghaiz023f3ef2015-10-19 09:39:32 -07001074
1075 NetworkMonitorFactory::ReleaseFactory(factory);
1076}
1077
Edward Lemur8dc945c2016-07-21 10:16:40 +02001078// 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
1084TEST_F(NetworkTest, MAYBE_DefaultLocalAddress) {
Guo-wei Shieha34c39e2015-11-25 13:12:26 -08001085 IPAddress ip;
guoweis56271ed2016-01-15 14:45:06 -08001086 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 Shieha34c39e2015-11-25 13:12:26 -08001093
1094 // Make sure we can query default local address when an address for such
1095 // address family exists.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001096 std::vector<Network*> networks;
1097 manager.GetNetworks(&networks);
guoweis56271ed2016-01-15 14:45:06 -08001098 EXPECT_TRUE(!networks.empty());
Steve Anton9de3aac2017-10-24 10:08:26 -07001099 for (const auto* network : networks) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001100 if (network->GetBestIP().family() == AF_INET) {
1101 EXPECT_TRUE(manager.QueryDefaultLocalAddress(AF_INET) != IPAddress());
guoweis56271ed2016-01-15 14:45:06 -08001102 } 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 Shieh9af97f82015-11-10 14:47:39 -08001106 EXPECT_TRUE(manager.QueryDefaultLocalAddress(AF_INET6) != IPAddress());
1107 }
1108 }
Guo-wei Shieha34c39e2015-11-25 13:12:26 -08001109
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));
honghaizaf83fe62016-04-18 14:50:44 -07001117
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 Shieh9af97f82015-11-10 14:47:39 -08001146 manager.StopUpdating();
1147}
1148
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001149} // namespace rtc