OLD | NEW |
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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 } | 84 } |
85 | 85 |
86 #if defined(WEBRTC_POSIX) | 86 #if defined(WEBRTC_POSIX) |
87 // Separated from CreateNetworks for tests. | 87 // Separated from CreateNetworks for tests. |
88 static void CallConvertIfAddrs(const BasicNetworkManager& network_manager, | 88 static void CallConvertIfAddrs(const BasicNetworkManager& network_manager, |
89 struct ifaddrs* interfaces, | 89 struct ifaddrs* interfaces, |
90 bool include_ignored, | 90 bool include_ignored, |
91 NetworkManager::NetworkList* networks) { | 91 NetworkManager::NetworkList* networks) { |
92 network_manager.ConvertIfAddrs(interfaces, include_ignored, networks); | 92 network_manager.ConvertIfAddrs(interfaces, include_ignored, networks); |
93 } | 93 } |
| 94 |
| 95 struct sockaddr_in6* CreateIpv6Addr(const std::string& ip_string, |
| 96 uint32_t scope_id) { |
| 97 struct sockaddr_in6* ipv6_addr = new struct sockaddr_in6; |
| 98 memset(ipv6_addr, 0, sizeof(struct sockaddr_in6)); |
| 99 ipv6_addr->sin6_family = AF_INET6; |
| 100 ipv6_addr->sin6_scope_id = scope_id; |
| 101 IPAddress ip; |
| 102 IPFromString(ip_string, &ip); |
| 103 ipv6_addr->sin6_addr = ip.ipv6_address(); |
| 104 return ipv6_addr; |
| 105 } |
| 106 |
| 107 // Pointers created here need to be released via ReleaseIfAddrs. |
| 108 struct ifaddrs* AddIpv6Address(struct ifaddrs* list, |
| 109 char* if_name, |
| 110 const std::string& ipv6_address, |
| 111 const std::string& ipv6_netmask, |
| 112 uint32_t scope_id) { |
| 113 struct ifaddrs* if_addr = new struct ifaddrs; |
| 114 memset(if_addr, 0, sizeof(struct ifaddrs)); |
| 115 if_addr->ifa_name = if_name; |
| 116 if_addr->ifa_addr = reinterpret_cast<struct sockaddr*>( |
| 117 CreateIpv6Addr(ipv6_address, scope_id)); |
| 118 if_addr->ifa_netmask = |
| 119 reinterpret_cast<struct sockaddr*>(CreateIpv6Addr(ipv6_netmask, 0)); |
| 120 if_addr->ifa_next = list; |
| 121 return if_addr; |
| 122 } |
| 123 |
| 124 void ReleaseIfAddrs(struct ifaddrs* list) { |
| 125 struct ifaddrs* if_addr = list; |
| 126 while (if_addr != nullptr) { |
| 127 struct ifaddrs* next_addr = if_addr->ifa_next; |
| 128 delete if_addr->ifa_addr; |
| 129 delete if_addr->ifa_netmask; |
| 130 delete if_addr; |
| 131 if_addr = next_addr; |
| 132 } |
| 133 } |
94 #endif // defined(WEBRTC_POSIX) | 134 #endif // defined(WEBRTC_POSIX) |
95 | 135 |
96 protected: | 136 protected: |
97 bool callback_called_; | 137 bool callback_called_; |
98 }; | 138 }; |
99 | 139 |
100 class TestBasicNetworkManager : public BasicNetworkManager { | 140 class TestBasicNetworkManager : public BasicNetworkManager { |
101 public: | 141 public: |
102 using BasicNetworkManager::QueryDefaultLocalAddress; | 142 using BasicNetworkManager::QueryDefaultLocalAddress; |
103 using BasicNetworkManager::set_default_local_addresses; | 143 using BasicNetworkManager::set_default_local_addresses; |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 } else { | 623 } else { |
584 // Check the IP didn't get added anywhere it wasn't supposed to. | 624 // Check the IP didn't get added anywhere it wasn't supposed to. |
585 EXPECT_EQ((*it)->GetIPs().end(), | 625 EXPECT_EQ((*it)->GetIPs().end(), |
586 std::find((*it)->GetIPs().begin(), | 626 std::find((*it)->GetIPs().begin(), |
587 (*it)->GetIPs().end(), | 627 (*it)->GetIPs().end(), |
588 ip)); | 628 ip)); |
589 } | 629 } |
590 } | 630 } |
591 } | 631 } |
592 | 632 |
593 // Test that DumpNetworks works. | 633 // Test that DumpNetworks does not crash. |
594 TEST_F(NetworkTest, TestDumpNetworks) { | 634 TEST_F(NetworkTest, TestCreateAndDumpNetworks) { |
595 BasicNetworkManager manager; | 635 BasicNetworkManager manager; |
596 manager.DumpNetworks(true); | 636 NetworkManager::NetworkList list = GetNetworks(manager, true); |
| 637 bool changed; |
| 638 MergeNetworkList(manager, list, &changed); |
| 639 manager.DumpNetworks(); |
597 } | 640 } |
598 | 641 |
599 // Test that we can toggle IPv6 on and off. | 642 // Test that we can toggle IPv6 on and off. |
600 // Crashes on Linux. See webrtc:4923. | 643 // Crashes on Linux. See webrtc:4923. |
601 #if defined(WEBRTC_LINUX) | 644 #if defined(WEBRTC_LINUX) |
602 #define MAYBE_TestIPv6Toggle DISABLED_TestIPv6Toggle | 645 #define MAYBE_TestIPv6Toggle DISABLED_TestIPv6Toggle |
603 #else | 646 #else |
604 #define MAYBE_TestIPv6Toggle TestIPv6Toggle | 647 #define MAYBE_TestIPv6Toggle TestIPv6Toggle |
605 #endif | 648 #endif |
606 TEST_F(NetworkTest, MAYBE_TestIPv6Toggle) { | 649 TEST_F(NetworkTest, MAYBE_TestIPv6Toggle) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 TEST_F(NetworkTest, TestConvertIfAddrsNoAddress) { | 736 TEST_F(NetworkTest, TestConvertIfAddrsNoAddress) { |
694 ifaddrs list; | 737 ifaddrs list; |
695 memset(&list, 0, sizeof(list)); | 738 memset(&list, 0, sizeof(list)); |
696 list.ifa_name = const_cast<char*>("test_iface"); | 739 list.ifa_name = const_cast<char*>("test_iface"); |
697 | 740 |
698 NetworkManager::NetworkList result; | 741 NetworkManager::NetworkList result; |
699 BasicNetworkManager manager; | 742 BasicNetworkManager manager; |
700 CallConvertIfAddrs(manager, &list, true, &result); | 743 CallConvertIfAddrs(manager, &list, true, &result); |
701 EXPECT_TRUE(result.empty()); | 744 EXPECT_TRUE(result.empty()); |
702 } | 745 } |
| 746 |
| 747 // Verify that if there are two addresses on one interface, only one network |
| 748 // is generated. |
| 749 TEST_F(NetworkTest, TestConvertIfAddrsMultiAddressesOnOneInterface) { |
| 750 char if_name[20] = "rmnet0"; |
| 751 ifaddrs* list = nullptr; |
| 752 list = AddIpv6Address(list, if_name, "1000:2000:3000:4000:0:0:0:1", |
| 753 "FFFF:FFFF:FFFF:FFFF::", 0); |
| 754 list = AddIpv6Address(list, if_name, "1000:2000:3000:4000:0:0:0:2", |
| 755 "FFFF:FFFF:FFFF:FFFF::", 0); |
| 756 NetworkManager::NetworkList result; |
| 757 BasicNetworkManager manager; |
| 758 CallConvertIfAddrs(manager, list, true, &result); |
| 759 EXPECT_EQ(1U, result.size()); |
| 760 bool changed; |
| 761 // This ensures we release the objects created in CallConvertIfAddrs. |
| 762 MergeNetworkList(manager, result, &changed); |
| 763 ReleaseIfAddrs(list); |
| 764 } |
703 #endif // defined(WEBRTC_POSIX) | 765 #endif // defined(WEBRTC_POSIX) |
704 | 766 |
705 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) | 767 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
706 // If you want to test non-default routes, you can do the following on a linux | 768 // If you want to test non-default routes, you can do the following on a linux |
707 // machine: | 769 // machine: |
708 // 1) Load the dummy network driver: | 770 // 1) Load the dummy network driver: |
709 // sudo modprobe dummy | 771 // sudo modprobe dummy |
710 // sudo ifconfig dummy0 127.0.0.1 | 772 // sudo ifconfig dummy0 127.0.0.1 |
711 // 2) Run this test and confirm the output says it found a dummy route (and | 773 // 2) Run this test and confirm the output says it found a dummy route (and |
712 // passes). | 774 // passes). |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 manager.GetNetworks(&list2); | 838 manager.GetNetworks(&list2); |
777 | 839 |
778 // Make sure the resulted networklist has only 1 element and 2 | 840 // Make sure the resulted networklist has only 1 element and 2 |
779 // IPAddresses. | 841 // IPAddresses. |
780 EXPECT_EQ(list2.size(), 1uL); | 842 EXPECT_EQ(list2.size(), 1uL); |
781 EXPECT_EQ(list2[0]->GetIPs().size(), 2uL); | 843 EXPECT_EQ(list2[0]->GetIPs().size(), 2uL); |
782 EXPECT_EQ(list2[0]->GetIPs()[0], ip1); | 844 EXPECT_EQ(list2[0]->GetIPs()[0], ip1); |
783 EXPECT_EQ(list2[0]->GetIPs()[1], ip2); | 845 EXPECT_EQ(list2[0]->GetIPs()[1], ip2); |
784 } | 846 } |
785 | 847 |
| 848 // Test that MergeNetworkList successfully detects the change if |
| 849 // a network becomes inactive and then active again. |
| 850 TEST_F(NetworkTest, TestMergeNetworkListWithInactiveNetworks) { |
| 851 BasicNetworkManager manager; |
| 852 Network network1("test_wifi", "Test Network Adapter 1", |
| 853 IPAddress(0x12345600U), 24); |
| 854 Network network2("test_eth0", "Test Network Adapter 2", |
| 855 IPAddress(0x00010000U), 16); |
| 856 network1.AddIP(IPAddress(0x12345678)); |
| 857 network2.AddIP(IPAddress(0x00010004)); |
| 858 NetworkManager::NetworkList list; |
| 859 Network* net1 = new Network(network1); |
| 860 list.push_back(net1); |
| 861 bool changed; |
| 862 MergeNetworkList(manager, list, &changed); |
| 863 EXPECT_TRUE(changed); |
| 864 list.clear(); |
| 865 manager.GetNetworks(&list); |
| 866 ASSERT_EQ(1U, list.size()); |
| 867 EXPECT_EQ(net1, list[0]); |
| 868 |
| 869 list.clear(); |
| 870 Network* net2 = new Network(network2); |
| 871 list.push_back(net2); |
| 872 MergeNetworkList(manager, list, &changed); |
| 873 EXPECT_TRUE(changed); |
| 874 list.clear(); |
| 875 manager.GetNetworks(&list); |
| 876 ASSERT_EQ(1U, list.size()); |
| 877 EXPECT_EQ(net2, list[0]); |
| 878 |
| 879 // Now network1 is inactive. Try to merge it again. |
| 880 list.clear(); |
| 881 list.push_back(new Network(network1)); |
| 882 MergeNetworkList(manager, list, &changed); |
| 883 EXPECT_TRUE(changed); |
| 884 list.clear(); |
| 885 manager.GetNetworks(&list); |
| 886 ASSERT_EQ(1U, list.size()); |
| 887 EXPECT_TRUE(list[0]->active()); |
| 888 EXPECT_EQ(net1, list[0]); |
| 889 } |
| 890 |
786 // Test that the filtering logic follows the defined ruleset in network.h. | 891 // Test that the filtering logic follows the defined ruleset in network.h. |
787 TEST_F(NetworkTest, TestIPv6Selection) { | 892 TEST_F(NetworkTest, TestIPv6Selection) { |
788 InterfaceAddress ip; | 893 InterfaceAddress ip; |
789 std::string ipstr; | 894 std::string ipstr; |
790 | 895 |
791 ipstr = "2401:fa00:4:1000:be30:5bff:fee5:c3"; | 896 ipstr = "2401:fa00:4:1000:be30:5bff:fee5:c3"; |
792 ASSERT_TRUE(IPFromString(ipstr, IPV6_ADDRESS_FLAG_DEPRECATED, &ip)); | 897 ASSERT_TRUE(IPFromString(ipstr, IPV6_ADDRESS_FLAG_DEPRECATED, &ip)); |
793 | 898 |
794 // Create a network with this prefix. | 899 // Create a network with this prefix. |
795 Network ipv6_network( | 900 Network ipv6_network( |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 manager.set_default_local_addresses(GetLoopbackIP(AF_INET), | 979 manager.set_default_local_addresses(GetLoopbackIP(AF_INET), |
875 GetLoopbackIP(AF_INET6)); | 980 GetLoopbackIP(AF_INET6)); |
876 EXPECT_TRUE(manager.GetDefaultLocalAddress(AF_INET, &ip)); | 981 EXPECT_TRUE(manager.GetDefaultLocalAddress(AF_INET, &ip)); |
877 EXPECT_EQ(ip, GetLoopbackIP(AF_INET)); | 982 EXPECT_EQ(ip, GetLoopbackIP(AF_INET)); |
878 EXPECT_TRUE(manager.GetDefaultLocalAddress(AF_INET6, &ip)); | 983 EXPECT_TRUE(manager.GetDefaultLocalAddress(AF_INET6, &ip)); |
879 EXPECT_EQ(ip, GetLoopbackIP(AF_INET6)); | 984 EXPECT_EQ(ip, GetLoopbackIP(AF_INET6)); |
880 manager.StopUpdating(); | 985 manager.StopUpdating(); |
881 } | 986 } |
882 | 987 |
883 } // namespace rtc | 988 } // namespace rtc |
OLD | NEW |