Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(568)

Side by Side Diff: webrtc/base/network_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698