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

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

Issue 1421433003: Fix CreateNetworks to stop it from signaling duplicate networks changed events (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Fix the other half of the bug in network manager Created 5 years, 1 month 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
« webrtc/base/network.cc ('K') | « webrtc/base/network.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 } 83 }
84 84
85 #if defined(WEBRTC_POSIX) 85 #if defined(WEBRTC_POSIX)
86 // Separated from CreateNetworks for tests. 86 // Separated from CreateNetworks for tests.
87 static void CallConvertIfAddrs(const BasicNetworkManager& network_manager, 87 static void CallConvertIfAddrs(const BasicNetworkManager& network_manager,
88 struct ifaddrs* interfaces, 88 struct ifaddrs* interfaces,
89 bool include_ignored, 89 bool include_ignored,
90 NetworkManager::NetworkList* networks) { 90 NetworkManager::NetworkList* networks) {
91 network_manager.ConvertIfAddrs(interfaces, include_ignored, networks); 91 network_manager.ConvertIfAddrs(interfaces, include_ignored, networks);
92 } 92 }
93
94 struct sockaddr_in6* CreateIpv6Addr(const std::string& ip_string,
95 uint32_t scope_id) {
96 struct sockaddr_in6* ipv6_addr = new struct sockaddr_in6;
97 memset(ipv6_addr, 0, sizeof(struct sockaddr_in6));
98 ipv6_addr->sin6_family = AF_INET6;
99 ipv6_addr->sin6_scope_id = scope_id;
100 IPAddress ip;
101 IPFromString(ip_string, &ip);
102 ipv6_addr->sin6_addr = ip.ipv6_address();
103 return ipv6_addr;
104 }
105
106 // Pointers created here need to be released via ReleaseIfAddrs.
107 struct ifaddrs* AddIpv6Address(struct ifaddrs* list,
108 char* if_name,
109 const std::string& ipv6_address,
110 const std::string& ipv6_netmask,
111 uint32_t scope_id) {
112 struct ifaddrs* if_addr = new struct ifaddrs;
113 memset(if_addr, 0, sizeof(struct ifaddrs));
114 if_addr->ifa_name = if_name;
115 if_addr->ifa_addr = reinterpret_cast<struct sockaddr*>(
116 CreateIpv6Addr(ipv6_address, scope_id));
117 if_addr->ifa_netmask =
118 reinterpret_cast<struct sockaddr*>(CreateIpv6Addr(ipv6_netmask, 0));
119 if_addr->ifa_next = list;
120 return if_addr;
121 }
122
123 void ReleaseIfAddrs(struct ifaddrs* list) {
124 struct ifaddrs* if_addr = list;
125 while (if_addr != nullptr) {
126 struct ifaddrs* next_addr = if_addr->ifa_next;
127 delete if_addr->ifa_addr;
128 delete if_addr->ifa_netmask;
129 delete if_addr;
130 if_addr = next_addr;
131 }
132 }
93 #endif // defined(WEBRTC_POSIX) 133 #endif // defined(WEBRTC_POSIX)
94 134
95 protected: 135 protected:
96 bool callback_called_; 136 bool callback_called_;
97 }; 137 };
98 138
99 // Test that the Network ctor works properly. 139 // Test that the Network ctor works properly.
100 TEST_F(NetworkTest, TestNetworkConstruct) { 140 TEST_F(NetworkTest, TestNetworkConstruct) {
101 Network ipv4_network1("test_eth0", "Test Network Adapter 1", 141 Network ipv4_network1("test_eth0", "Test Network Adapter 1",
102 IPAddress(0x12345600U), 24); 142 IPAddress(0x12345600U), 24);
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 } else { 616 } else {
577 // Check the IP didn't get added anywhere it wasn't supposed to. 617 // Check the IP didn't get added anywhere it wasn't supposed to.
578 EXPECT_EQ((*it)->GetIPs().end(), 618 EXPECT_EQ((*it)->GetIPs().end(),
579 std::find((*it)->GetIPs().begin(), 619 std::find((*it)->GetIPs().begin(),
580 (*it)->GetIPs().end(), 620 (*it)->GetIPs().end(),
581 ip)); 621 ip));
582 } 622 }
583 } 623 }
584 } 624 }
585 625
586 // Test that DumpNetworks works. 626 // Test that DumpNetworks does not crash.
587 TEST_F(NetworkTest, TestDumpNetworks) { 627 TEST_F(NetworkTest, TestCreateAndDumpNetworks) {
588 BasicNetworkManager manager; 628 BasicNetworkManager manager;
589 manager.DumpNetworks(true); 629 NetworkManager::NetworkList list = GetNetworks(manager, true);
630 bool changed;
631 MergeNetworkList(manager, list, &changed);
632 manager.DumpNetworks();
590 } 633 }
591 634
592 // Test that we can toggle IPv6 on and off. 635 // Test that we can toggle IPv6 on and off.
593 // Crashes on Linux. See webrtc:4923. 636 // Crashes on Linux. See webrtc:4923.
594 #if defined(WEBRTC_LINUX) 637 #if defined(WEBRTC_LINUX)
595 #define MAYBE_TestIPv6Toggle DISABLED_TestIPv6Toggle 638 #define MAYBE_TestIPv6Toggle DISABLED_TestIPv6Toggle
596 #else 639 #else
597 #define MAYBE_TestIPv6Toggle TestIPv6Toggle 640 #define MAYBE_TestIPv6Toggle TestIPv6Toggle
598 #endif 641 #endif
599 TEST_F(NetworkTest, MAYBE_TestIPv6Toggle) { 642 TEST_F(NetworkTest, MAYBE_TestIPv6Toggle) {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 TEST_F(NetworkTest, TestConvertIfAddrsNoAddress) { 729 TEST_F(NetworkTest, TestConvertIfAddrsNoAddress) {
687 ifaddrs list; 730 ifaddrs list;
688 memset(&list, 0, sizeof(list)); 731 memset(&list, 0, sizeof(list));
689 list.ifa_name = const_cast<char*>("test_iface"); 732 list.ifa_name = const_cast<char*>("test_iface");
690 733
691 NetworkManager::NetworkList result; 734 NetworkManager::NetworkList result;
692 BasicNetworkManager manager; 735 BasicNetworkManager manager;
693 CallConvertIfAddrs(manager, &list, true, &result); 736 CallConvertIfAddrs(manager, &list, true, &result);
694 EXPECT_TRUE(result.empty()); 737 EXPECT_TRUE(result.empty());
695 } 738 }
739
740 // Verify that if there are two addresses on one interface, only one network
741 // is generated.
742 TEST_F(NetworkTest, TestConvertIfAddrsMultiAddressesOnOneInterface) {
743 char if_name[20] = "rmnet0";
744 ifaddrs* list = nullptr;
745 list = AddIpv6Address(list, if_name, "1000:2000:3000:4000:0:0:0:1",
746 "FFFF:FFFF:FFFF:FFFF::", 0);
747 list = AddIpv6Address(list, if_name, "1000:2000:3000:4000:0:0:0:2",
748 "FFFF:FFFF:FFFF:FFFF::", 0);
749 NetworkManager::NetworkList result;
750 BasicNetworkManager manager;
751 CallConvertIfAddrs(manager, list, true, &result);
752 EXPECT_EQ(1U, result.size());
753 bool changed;
754 // This ensures releasing the objects created in CallConvertIfAddrs.
pthatcher1 2015/12/08 19:40:24 This ensures releasing => this ensures we realase
honghaiz3 2015/12/10 19:26:18 Done.
755 MergeNetworkList(manager, result, &changed);
756 ReleaseIfAddrs(list);
757 }
696 #endif // defined(WEBRTC_POSIX) 758 #endif // defined(WEBRTC_POSIX)
697 759
698 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) 760 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
699 // If you want to test non-default routes, you can do the following on a linux 761 // If you want to test non-default routes, you can do the following on a linux
700 // machine: 762 // machine:
701 // 1) Load the dummy network driver: 763 // 1) Load the dummy network driver:
702 // sudo modprobe dummy 764 // sudo modprobe dummy
703 // sudo ifconfig dummy0 127.0.0.1 765 // sudo ifconfig dummy0 127.0.0.1
704 // 2) Run this test and confirm the output says it found a dummy route (and 766 // 2) Run this test and confirm the output says it found a dummy route (and
705 // passes). 767 // passes).
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 manager.GetNetworks(&list2); 831 manager.GetNetworks(&list2);
770 832
771 // Make sure the resulted networklist has only 1 element and 2 833 // Make sure the resulted networklist has only 1 element and 2
772 // IPAddresses. 834 // IPAddresses.
773 EXPECT_EQ(list2.size(), 1uL); 835 EXPECT_EQ(list2.size(), 1uL);
774 EXPECT_EQ(list2[0]->GetIPs().size(), 2uL); 836 EXPECT_EQ(list2[0]->GetIPs().size(), 2uL);
775 EXPECT_EQ(list2[0]->GetIPs()[0], ip1); 837 EXPECT_EQ(list2[0]->GetIPs()[0], ip1);
776 EXPECT_EQ(list2[0]->GetIPs()[1], ip2); 838 EXPECT_EQ(list2[0]->GetIPs()[1], ip2);
777 } 839 }
778 840
841 // Test that MergeNetworkList successfully detects the change if
842 // a network becomes inactive and then active again.
843 TEST_F(NetworkTest, TestMergeNetworkListWithInactiveNetworks) {
844 BasicNetworkManager manager;
845 Network network1("test_wifi", "Test Network Adapter 1",
846 IPAddress(0x12345600U), 24);
847 Network network2("test_eth0", "Test Network Adapter 2",
848 IPAddress(0x00010000U), 16);
849 network1.AddIP(IPAddress(0x12345678));
850 network2.AddIP(IPAddress(0x00010004));
851 NetworkManager::NetworkList list;
852 list.push_back(new Network(network1));
853 bool changed;
854 MergeNetworkList(manager, list, &changed);
855 EXPECT_TRUE(changed);
pthatcher1 2015/12/08 19:40:24 Should we test what's in the manager right here?
honghaiz3 2015/12/10 19:26:18 Done.
856
857 list.clear();
858 list.push_back(new Network(network2));
859 MergeNetworkList(manager, list, &changed);
860 EXPECT_TRUE(changed);
861 list.clear();
862 manager.GetNetworks(&list);
863 EXPECT_EQ(1U, list.size());
pthatcher1 2015/12/08 19:40:24 Should we test what's in there, and not just the s
honghaiz3 2015/12/10 19:26:18 Done.
864
865 // Now network1 is inactive. Try to merge it again.
pthatcher1 2015/12/08 19:40:24 What makes network1 inactive?
honghaiz3 2015/12/10 19:26:18 When you merge list2 into an existing list1, If a
866 list.clear();
867 list.push_back(new Network(network1));
868 MergeNetworkList(manager, list, &changed);
869 EXPECT_TRUE(changed);
870 list.clear();
871 manager.GetNetworks(&list);
872 EXPECT_EQ(1U, list.size());
873 EXPECT_TRUE(list.front()->active());
pthatcher1 2015/12/08 19:40:24 Nit: I find list[0].active() more readable.
honghaiz3 2015/12/10 19:26:18 Done.
874 }
875
779 // Test that the filtering logic follows the defined ruleset in network.h. 876 // Test that the filtering logic follows the defined ruleset in network.h.
780 TEST_F(NetworkTest, TestIPv6Selection) { 877 TEST_F(NetworkTest, TestIPv6Selection) {
781 InterfaceAddress ip; 878 InterfaceAddress ip;
782 std::string ipstr; 879 std::string ipstr;
783 880
784 ipstr = "2401:fa00:4:1000:be30:5bff:fee5:c3"; 881 ipstr = "2401:fa00:4:1000:be30:5bff:fee5:c3";
785 ASSERT_TRUE(IPFromString(ipstr, IPV6_ADDRESS_FLAG_DEPRECATED, &ip)); 882 ASSERT_TRUE(IPFromString(ipstr, IPV6_ADDRESS_FLAG_DEPRECATED, &ip));
786 883
787 // Create a network with this prefix. 884 // Create a network with this prefix.
788 Network ipv6_network( 885 Network ipv6_network(
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 EXPECT_TRUE_WAIT(callback_called_, 1000); 933 EXPECT_TRUE_WAIT(callback_called_, 1000);
837 934
838 // Network manager is stopped; the network monitor is removed. 935 // Network manager is stopped; the network monitor is removed.
839 manager.StopUpdating(); 936 manager.StopUpdating();
840 EXPECT_TRUE(GetNetworkMonitor(manager) == nullptr); 937 EXPECT_TRUE(GetNetworkMonitor(manager) == nullptr);
841 938
842 NetworkMonitorFactory::ReleaseFactory(factory); 939 NetworkMonitorFactory::ReleaseFactory(factory);
843 } 940 }
844 941
845 } // namespace rtc 942 } // namespace rtc
OLDNEW
« webrtc/base/network.cc ('K') | « webrtc/base/network.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698