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