1 /* 2 * libjingle 3 * Copyright 2004 Google Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "talk/base/network.h" 29 30 #include <vector> 31 #if defined(POSIX) 32 #include <sys/types.h> 33 #ifndef ANDROID 34 #include <ifaddrs.h> 35 #else 36 #include "talk/base/ifaddrs-android.h" 37 #endif 38 #endif 39 #include "talk/base/gunit.h" 40 #ifdef WIN32 41 #include "talk/base/logging.h" // For LOG_GLE 42 #endif 43 44 namespace talk_base { 45 46 class NetworkTest : public testing::Test, public sigslot::has_slots<> { 47 public: 48 NetworkTest() : callback_called_(false) {} 49 50 void OnNetworksChanged() { 51 callback_called_ = true; 52 } 53 54 void MergeNetworkList(BasicNetworkManager& network_manager, 55 const NetworkManager::NetworkList& list, 56 bool* changed ) { 57 network_manager.MergeNetworkList(list, changed); 58 } 59 60 bool IsIgnoredNetwork(BasicNetworkManager& network_manager, 61 const Network& network) { 62 return network_manager.IsIgnoredNetwork(network); 63 } 64 65 NetworkManager::NetworkList GetNetworks( 66 const BasicNetworkManager& network_manager, bool include_ignored) { 67 NetworkManager::NetworkList list; 68 network_manager.CreateNetworks(include_ignored, &list); 69 return list; 70 } 71 72 #if defined(POSIX) 73 // Separated from CreateNetworks for tests. 74 static void CallConvertIfAddrs(const BasicNetworkManager& network_manager, 75 struct ifaddrs* interfaces, 76 bool include_ignored, 77 NetworkManager::NetworkList* networks) { 78 network_manager.ConvertIfAddrs(interfaces, include_ignored, networks); 79 } 80 #endif // defined(POSIX) 81 82 protected: 83 bool callback_called_; 84 }; 85 86 // Test that the Network ctor works properly. 87 TEST_F(NetworkTest, TestNetworkConstruct) { 88 Network ipv4_network1("test_eth0", "Test Network Adapter 1", 89 IPAddress(0x12345600U), 24); 90 EXPECT_EQ("test_eth0", ipv4_network1.name()); 91 EXPECT_EQ("Test Network Adapter 1", ipv4_network1.description()); 92 EXPECT_EQ(IPAddress(0x12345600U), ipv4_network1.prefix()); 93 EXPECT_EQ(24, ipv4_network1.prefix_length()); 94 EXPECT_FALSE(ipv4_network1.ignored()); 95 } 96 97 // Tests that our ignore function works properly. 98 TEST_F(NetworkTest, TestNetworkIgnore) { 99 Network ipv4_network1("test_eth0", "Test Network Adapter 1", 100 IPAddress(0x12345600U), 24); 101 Network ipv4_network2("test_eth1", "Test Network Adapter 2", 102 IPAddress(0x00010000U), 16); 103 BasicNetworkManager network_manager; 104 EXPECT_FALSE(IsIgnoredNetwork(network_manager, ipv4_network1)); 105 EXPECT_TRUE(IsIgnoredNetwork(network_manager, ipv4_network2)); 106 } 107 108 TEST_F(NetworkTest, TestIgnoreList) { 109 Network ignore_me("ignore_me", "Ignore me please!", 110 IPAddress(0x12345600U), 24); 111 Network include_me("include_me", "Include me please!", 112 IPAddress(0x12345600U), 24); 113 BasicNetworkManager network_manager; 114 EXPECT_FALSE(IsIgnoredNetwork(network_manager, ignore_me)); 115 EXPECT_FALSE(IsIgnoredNetwork(network_manager, include_me)); 116 std::vector<std::string> ignore_list; 117 ignore_list.push_back("ignore_me"); 118 network_manager.set_network_ignore_list(ignore_list); 119 EXPECT_TRUE(IsIgnoredNetwork(network_manager, ignore_me)); 120 EXPECT_FALSE(IsIgnoredNetwork(network_manager, include_me)); 121 } 122 123 // Test is failing on Windows opt: b/11288214 124 TEST_F(NetworkTest, DISABLED_TestCreateNetworks) { 125 BasicNetworkManager manager; 126 NetworkManager::NetworkList result = GetNetworks(manager, true); 127 // We should be able to bind to any addresses we find. 128 NetworkManager::NetworkList::iterator it; 129 for (it = result.begin(); 130 it != result.end(); 131 ++it) { 132 sockaddr_storage storage; 133 memset(&storage, 0, sizeof(storage)); 134 IPAddress ip = (*it)->ip(); 135 SocketAddress bindaddress(ip, 0); 136 bindaddress.SetScopeID((*it)->scope_id()); 137 // TODO(thaloun): Use talk_base::AsyncSocket once it supports IPv6. 138 int fd = static_cast<int>(socket(ip.family(), SOCK_STREAM, IPPROTO_TCP)); 139 if (fd > 0) { 140 size_t ipsize = bindaddress.ToSockAddrStorage(&storage); 141 EXPECT_GE(ipsize, 0U); 142 int success = ::bind(fd, 143 reinterpret_cast<sockaddr*>(&storage), 144 static_cast<int>(ipsize)); 145 #ifdef WIN32 146 if (success) LOG_GLE(LS_ERROR) << "Socket bind failed."; 147 #endif 148 EXPECT_EQ(0, success); 149 #ifdef WIN32 150 closesocket(fd); 151 #else 152 close(fd); 153 #endif 154 } 155 delete (*it); 156 } 157 } 158 159 // Test that UpdateNetworks succeeds. 160 TEST_F(NetworkTest, TestUpdateNetworks) { 161 BasicNetworkManager manager; 162 manager.SignalNetworksChanged.connect( 163 static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); 164 manager.StartUpdating(); 165 Thread::Current()->ProcessMessages(0); 166 EXPECT_TRUE(callback_called_); 167 callback_called_ = false; 168 // Callback should be triggered immediately when StartUpdating 169 // is called, after network update signal is already sent. 170 manager.StartUpdating(); 171 EXPECT_TRUE(manager.started()); 172 Thread::Current()->ProcessMessages(0); 173 EXPECT_TRUE(callback_called_); 174 manager.StopUpdating(); 175 EXPECT_TRUE(manager.started()); 176 manager.StopUpdating(); 177 EXPECT_FALSE(manager.started()); 178 manager.StopUpdating(); 179 EXPECT_FALSE(manager.started()); 180 callback_called_ = false; 181 // Callback should be triggered immediately after StartUpdating is called 182 // when start_count_ is reset to 0. 183 manager.StartUpdating(); 184 Thread::Current()->ProcessMessages(0); 185 EXPECT_TRUE(callback_called_); 186 } 187 188 // Verify that MergeNetworkList() merges network lists properly. 189 TEST_F(NetworkTest, TestBasicMergeNetworkList) { 190 Network ipv4_network1("test_eth0", "Test Network Adapter 1", 191 IPAddress(0x12345600U), 24); 192 Network ipv4_network2("test_eth1", "Test Network Adapter 2", 193 IPAddress(0x00010000U), 16); 194 ipv4_network1.AddIP(IPAddress(0x12345678)); 195 ipv4_network2.AddIP(IPAddress(0x00010004)); 196 BasicNetworkManager manager; 197 198 // Add ipv4_network1 to the list of networks. 199 NetworkManager::NetworkList list; 200 list.push_back(new Network(ipv4_network1)); 201 bool changed; 202 MergeNetworkList(manager, list, &changed); 203 EXPECT_TRUE(changed); 204 list.clear(); 205 206 manager.GetNetworks(&list); 207 EXPECT_EQ(1U, list.size()); 208 EXPECT_EQ(ipv4_network1.ToString(), list[0]->ToString()); 209 Network* net1 = list[0]; 210 list.clear(); 211 212 // Replace ipv4_network1 with ipv4_network2. 213 list.push_back(new Network(ipv4_network2)); 214 MergeNetworkList(manager, list, &changed); 215 EXPECT_TRUE(changed); 216 list.clear(); 217 218 manager.GetNetworks(&list); 219 EXPECT_EQ(1U, list.size()); 220 EXPECT_EQ(ipv4_network2.ToString(), list[0]->ToString()); 221 Network* net2 = list[0]; 222 list.clear(); 223 224 // Add Network2 back. 225 list.push_back(new Network(ipv4_network1)); 226 list.push_back(new Network(ipv4_network2)); 227 MergeNetworkList(manager, list, &changed); 228 EXPECT_TRUE(changed); 229 list.clear(); 230 231 // Verify that we get previous instances of Network objects. 232 manager.GetNetworks(&list); 233 EXPECT_EQ(2U, list.size()); 234 EXPECT_TRUE((net1 == list[0] && net2 == list[1]) || 235 (net1 == list[1] && net2 == list[0])); 236 list.clear(); 237 238 // Call MergeNetworkList() again and verify that we don't get update 239 // notification. 240 list.push_back(new Network(ipv4_network2)); 241 list.push_back(new Network(ipv4_network1)); 242 MergeNetworkList(manager, list, &changed); 243 EXPECT_FALSE(changed); 244 list.clear(); 245 246 // Verify that we get previous instances of Network objects. 247 manager.GetNetworks(&list); 248 EXPECT_EQ(2U, list.size()); 249 EXPECT_TRUE((net1 == list[0] && net2 == list[1]) || 250 (net1 == list[1] && net2 == list[0])); 251 list.clear(); 252 } 253 254 // Sets up some test IPv6 networks and appends them to list. 255 // Four networks are added - public and link local, for two interfaces. 256 void SetupNetworks(NetworkManager::NetworkList* list) { 257 IPAddress ip; 258 IPAddress prefix; 259 EXPECT_TRUE(IPFromString("fe80::1234:5678:abcd:ef12", &ip)); 260 EXPECT_TRUE(IPFromString("fe80::", &prefix)); 261 // First, fake link-locals. 262 Network ipv6_eth0_linklocalnetwork("test_eth0", "Test NetworkAdapter 1", 263 prefix, 64); 264 ipv6_eth0_linklocalnetwork.AddIP(ip); 265 EXPECT_TRUE(IPFromString("fe80::5678:abcd:ef12:3456", &ip)); 266 Network ipv6_eth1_linklocalnetwork("test_eth1", "Test NetworkAdapter 2", 267 prefix, 64); 268 ipv6_eth1_linklocalnetwork.AddIP(ip); 269 // Public networks: 270 EXPECT_TRUE(IPFromString("2401:fa00:4:1000:be30:5bff:fee5:c3", &ip)); 271 prefix = TruncateIP(ip, 64); 272 Network ipv6_eth0_publicnetwork1_ip1("test_eth0", "Test NetworkAdapter 1", 273 prefix, 64); 274 ipv6_eth0_publicnetwork1_ip1.AddIP(ip); 275 EXPECT_TRUE(IPFromString("2400:4030:1:2c00:be30:abcd:efab:cdef", &ip)); 276 prefix = TruncateIP(ip, 64); 277 Network ipv6_eth1_publicnetwork1_ip1("test_eth1", "Test NetworkAdapter 1", 278 prefix, 64); 279 ipv6_eth1_publicnetwork1_ip1.AddIP(ip); 280 list->push_back(new Network(ipv6_eth0_linklocalnetwork)); 281 list->push_back(new Network(ipv6_eth1_linklocalnetwork)); 282 list->push_back(new Network(ipv6_eth0_publicnetwork1_ip1)); 283 list->push_back(new Network(ipv6_eth1_publicnetwork1_ip1)); 284 } 285 286 // Test that the basic network merging case works. 287 TEST_F(NetworkTest, TestIPv6MergeNetworkList) { 288 BasicNetworkManager manager; 289 manager.SignalNetworksChanged.connect( 290 static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); 291 NetworkManager::NetworkList original_list; 292 SetupNetworks(&original_list); 293 bool changed = false; 294 MergeNetworkList(manager, original_list, &changed); 295 EXPECT_TRUE(changed); 296 NetworkManager::NetworkList list; 297 manager.GetNetworks(&list); 298 EXPECT_EQ(original_list.size(), list.size()); 299 // Verify that the original members are in the merged list. 300 for (NetworkManager::NetworkList::iterator it = original_list.begin(); 301 it != original_list.end(); ++it) { 302 EXPECT_NE(list.end(), std::find(list.begin(), list.end(), *it)); 303 } 304 } 305 306 // Tests that when two network lists that describe the same set of networks are 307 // merged, that the changed callback is not called, and that the original 308 // objects remain in the result list. 309 TEST_F(NetworkTest, TestNoChangeMerge) { 310 BasicNetworkManager manager; 311 manager.SignalNetworksChanged.connect( 312 static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); 313 NetworkManager::NetworkList original_list; 314 SetupNetworks(&original_list); 315 bool changed = false; 316 MergeNetworkList(manager, original_list, &changed); 317 EXPECT_TRUE(changed); 318 // Second list that describes the same networks but with new objects. 319 NetworkManager::NetworkList second_list; 320 SetupNetworks(&second_list); 321 changed = false; 322 MergeNetworkList(manager, second_list, &changed); 323 EXPECT_FALSE(changed); 324 NetworkManager::NetworkList resulting_list; 325 manager.GetNetworks(&resulting_list); 326 EXPECT_EQ(original_list.size(), resulting_list.size()); 327 // Verify that the original members are in the merged list. 328 for (NetworkManager::NetworkList::iterator it = original_list.begin(); 329 it != original_list.end(); ++it) { 330 EXPECT_NE(resulting_list.end(), 331 std::find(resulting_list.begin(), resulting_list.end(), *it)); 332 } 333 // Doublecheck that the new networks aren't in the list. 334 for (NetworkManager::NetworkList::iterator it = second_list.begin(); 335 it != second_list.end(); ++it) { 336 EXPECT_EQ(resulting_list.end(), 337 std::find(resulting_list.begin(), resulting_list.end(), *it)); 338 } 339 } 340 341 // Test that we can merge a network that is the same as another network but with 342 // a different IP. The original network should remain in the list, but have its 343 // IP changed. 344 TEST_F(NetworkTest, MergeWithChangedIP) { 345 BasicNetworkManager manager; 346 manager.SignalNetworksChanged.connect( 347 static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); 348 NetworkManager::NetworkList original_list; 349 SetupNetworks(&original_list); 350 // Make a network that we're going to change. 351 IPAddress ip; 352 EXPECT_TRUE(IPFromString("2401:fa01:4:1000:be30:faa:fee:faa", &ip)); 353 IPAddress prefix = TruncateIP(ip, 64); 354 Network* network_to_change = new Network("test_eth0", 355 "Test Network Adapter 1", 356 prefix, 64); 357 Network* changed_network = new Network(*network_to_change); 358 network_to_change->AddIP(ip); 359 IPAddress changed_ip; 360 EXPECT_TRUE(IPFromString("2401:fa01:4:1000:be30:f00:f00:f00", &changed_ip)); 361 changed_network->AddIP(changed_ip); 362 original_list.push_back(network_to_change); 363 bool changed = false; 364 MergeNetworkList(manager, original_list, &changed); 365 NetworkManager::NetworkList second_list; 366 SetupNetworks(&second_list); 367 second_list.push_back(changed_network); 368 changed = false; 369 MergeNetworkList(manager, second_list, &changed); 370 EXPECT_TRUE(changed); 371 NetworkManager::NetworkList list; 372 manager.GetNetworks(&list); 373 EXPECT_EQ(original_list.size(), list.size()); 374 // Make sure the original network is still in the merged list. 375 EXPECT_NE(list.end(), 376 std::find(list.begin(), list.end(), network_to_change)); 377 EXPECT_EQ(changed_ip, network_to_change->GetIPs().at(0)); 378 } 379 380 // Testing a similar case to above, but checking that a network can be updated 381 // with additional IPs (not just a replacement). 382 TEST_F(NetworkTest, TestMultipleIPMergeNetworkList) { 383 BasicNetworkManager manager; 384 manager.SignalNetworksChanged.connect( 385 static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); 386 NetworkManager::NetworkList original_list; 387 SetupNetworks(&original_list); 388 bool changed = false; 389 MergeNetworkList(manager, original_list, &changed); 390 EXPECT_TRUE(changed); 391 IPAddress ip; 392 IPAddress check_ip; 393 IPAddress prefix; 394 // Add a second IP to the public network on eth0 (2401:fa00:4:1000/64). 395 EXPECT_TRUE(IPFromString("2401:fa00:4:1000:be30:5bff:fee5:c6", &ip)); 396 prefix = TruncateIP(ip, 64); 397 Network ipv6_eth0_publicnetwork1_ip2("test_eth0", "Test NetworkAdapter 1", 398 prefix, 64); 399 // This is the IP that already existed in the public network on eth0. 400 EXPECT_TRUE(IPFromString("2401:fa00:4:1000:be30:5bff:fee5:c3", &check_ip)); 401 ipv6_eth0_publicnetwork1_ip2.AddIP(ip); 402 original_list.push_back(new Network(ipv6_eth0_publicnetwork1_ip2)); 403 changed = false; 404 MergeNetworkList(manager, original_list, &changed); 405 EXPECT_TRUE(changed); 406 // There should still be four networks. 407 NetworkManager::NetworkList list; 408 manager.GetNetworks(&list); 409 EXPECT_EQ(4U, list.size()); 410 // Check the gathered IPs. 411 int matchcount = 0; 412 for (NetworkManager::NetworkList::iterator it = list.begin(); 413 it != list.end(); ++it) { 414 if ((*it)->ToString() == original_list[2]->ToString()) { 415 ++matchcount; 416 EXPECT_EQ(1, matchcount); 417 // This should be the same network object as before. 418 EXPECT_EQ((*it), original_list[2]); 419 // But with two addresses now. 420 EXPECT_EQ(2U, (*it)->GetIPs().size()); 421 EXPECT_NE((*it)->GetIPs().end(), 422 std::find((*it)->GetIPs().begin(), 423 (*it)->GetIPs().end(), 424 check_ip)); 425 EXPECT_NE((*it)->GetIPs().end(), 426 std::find((*it)->GetIPs().begin(), 427 (*it)->GetIPs().end(), 428 ip)); 429 } else { 430 // Check the IP didn't get added anywhere it wasn't supposed to. 431 EXPECT_EQ((*it)->GetIPs().end(), 432 std::find((*it)->GetIPs().begin(), 433 (*it)->GetIPs().end(), 434 ip)); 435 } 436 } 437 } 438 439 // Test that merge correctly distinguishes multiple networks on an interface. 440 TEST_F(NetworkTest, TestMultiplePublicNetworksOnOneInterfaceMerge) { 441 BasicNetworkManager manager; 442 manager.SignalNetworksChanged.connect( 443 static_cast<NetworkTest*>(this), &NetworkTest::OnNetworksChanged); 444 NetworkManager::NetworkList original_list; 445 SetupNetworks(&original_list); 446 bool changed = false; 447 MergeNetworkList(manager, original_list, &changed); 448 EXPECT_TRUE(changed); 449 IPAddress ip; 450 IPAddress prefix; 451 // A second network for eth0. 452 EXPECT_TRUE(IPFromString("2400:4030:1:2c00:be30:5bff:fee5:c3", &ip)); 453 prefix = TruncateIP(ip, 64); 454 Network ipv6_eth0_publicnetwork2_ip1("test_eth0", "Test NetworkAdapter 1", 455 prefix, 64); 456 ipv6_eth0_publicnetwork2_ip1.AddIP(ip); 457 original_list.push_back(new Network(ipv6_eth0_publicnetwork2_ip1)); 458 changed = false; 459 MergeNetworkList(manager, original_list, &changed); 460 EXPECT_TRUE(changed); 461 // There should be five networks now. 462 NetworkManager::NetworkList list; 463 manager.GetNetworks(&list); 464 EXPECT_EQ(5U, list.size()); 465 // Check the resulting addresses. 466 for (NetworkManager::NetworkList::iterator it = list.begin(); 467 it != list.end(); ++it) { 468 if ((*it)->prefix() == ipv6_eth0_publicnetwork2_ip1.prefix() && 469 (*it)->name() == ipv6_eth0_publicnetwork2_ip1.name()) { 470 // Check the new network has 1 IP and that it's the correct one. 471 EXPECT_EQ(1U, (*it)->GetIPs().size()); 472 EXPECT_EQ(ip, (*it)->GetIPs().at(0)); 473 } else { 474 // Check the IP didn't get added anywhere it wasn't supposed to. 475 EXPECT_EQ((*it)->GetIPs().end(), 476 std::find((*it)->GetIPs().begin(), 477 (*it)->GetIPs().end(), 478 ip)); 479 } 480 } 481 } 482 483 // Test that DumpNetworks works. 484 TEST_F(NetworkTest, TestDumpNetworks) { 485 BasicNetworkManager manager; 486 manager.DumpNetworks(true); 487 } 488 489 // Test that we can toggle IPv6 on and off. 490 TEST_F(NetworkTest, TestIPv6Toggle) { 491 BasicNetworkManager manager; 492 bool ipv6_found = false; 493 NetworkManager::NetworkList list; 494 #ifndef WIN32 495 // There should be at least one IPv6 network (fe80::/64 should be in there). 496 // TODO(thaloun): Disabling this test on windows for the moment as the test 497 // machines don't seem to have IPv6 installed on them at all. 498 manager.set_ipv6_enabled(true); 499 list = GetNetworks(manager, true); 500 for (NetworkManager::NetworkList::iterator it = list.begin(); 501 it != list.end(); ++it) { 502 if ((*it)->prefix().family() == AF_INET6) { 503 ipv6_found = true; 504 break; 505 } 506 } 507 EXPECT_TRUE(ipv6_found); 508 for (NetworkManager::NetworkList::iterator it = list.begin(); 509 it != list.end(); ++it) { 510 delete (*it); 511 } 512 #endif 513 ipv6_found = false; 514 manager.set_ipv6_enabled(false); 515 list = GetNetworks(manager, true); 516 for (NetworkManager::NetworkList::iterator it = list.begin(); 517 it != list.end(); ++it) { 518 if ((*it)->prefix().family() == AF_INET6) { 519 ipv6_found = true; 520 break; 521 } 522 } 523 EXPECT_FALSE(ipv6_found); 524 for (NetworkManager::NetworkList::iterator it = list.begin(); 525 it != list.end(); ++it) { 526 delete (*it); 527 } 528 } 529 530 #if defined(POSIX) 531 // Verify that we correctly handle interfaces with no address. 532 TEST_F(NetworkTest, TestConvertIfAddrsNoAddress) { 533 ifaddrs list; 534 memset(&list, 0, sizeof(list)); 535 list.ifa_name = const_cast<char*>("test_iface"); 536 537 NetworkManager::NetworkList result; 538 BasicNetworkManager manager; 539 CallConvertIfAddrs(manager, &list, true, &result); 540 EXPECT_TRUE(result.empty()); 541 } 542 #endif // defined(POSIX) 543 544 #if defined(LINUX) 545 // If you want to test non-default routes, you can do the following on a linux 546 // machine: 547 // 1) Load the dummy network driver: 548 // sudo modprobe dummy 549 // sudo ifconfig dummy0 127.0.0.1 550 // 2) Run this test and confirm the output says it found a dummy route (and 551 // passes). 552 // 3) When done: 553 // sudo rmmmod dummy 554 TEST_F(NetworkTest, TestIgnoreNonDefaultRoutes) { 555 BasicNetworkManager manager; 556 NetworkManager::NetworkList list; 557 list = GetNetworks(manager, false); 558 bool found_dummy = false; 559 LOG(LS_INFO) << "Looking for dummy network: "; 560 for (NetworkManager::NetworkList::iterator it = list.begin(); 561 it != list.end(); ++it) { 562 LOG(LS_INFO) << " Network name: " << (*it)->name(); 563 found_dummy |= (*it)->name().find("dummy0") != std::string::npos; 564 } 565 for (NetworkManager::NetworkList::iterator it = list.begin(); 566 it != list.end(); ++it) { 567 delete (*it); 568 } 569 if (!found_dummy) { 570 LOG(LS_INFO) << "No dummy found, quitting."; 571 return; 572 } 573 LOG(LS_INFO) << "Found dummy, running again while ignoring non-default " 574 << "routes."; 575 manager.set_ignore_non_default_routes(true); 576 list = GetNetworks(manager, false); 577 for (NetworkManager::NetworkList::iterator it = list.begin(); 578 it != list.end(); ++it) { 579 LOG(LS_INFO) << " Network name: " << (*it)->name(); 580 EXPECT_TRUE((*it)->name().find("dummy0") == std::string::npos); 581 } 582 for (NetworkManager::NetworkList::iterator it = list.begin(); 583 it != list.end(); ++it) { 584 delete (*it); 585 } 586 } 587 #endif 588 589 } // namespace talk_base 590