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

Side by Side Diff: net/base/net_util_unittest.cc

Issue 670183003: Update from chromium 62675d9fb31fb8cedc40f68e78e8445a74f362e7 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 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
« no previous file with comments | « net/base/net_util_posix.cc ('k') | net/base/sdch_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <ostream> 9 #include <ostream>
10 10
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/scoped_native_library.h" 13 #include "base/scoped_native_library.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/sys_byteorder.h" 18 #include "base/sys_byteorder.h"
19 #include "base/time/time.h" 19 #include "base/time/time.h"
20
21 #if !defined(OS_NACL) && !defined(OS_WIN)
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #endif // !OS_NACL && !OS_WIN
20 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
21 #include "url/gurl.h" 26 #include "url/gurl.h"
22 27
23 #if defined(OS_WIN) 28 #if defined(OS_WIN)
24 #include <iphlpapi.h> 29 #include <iphlpapi.h>
25 #include <objbase.h> 30 #include <objbase.h>
26 #include "base/win/windows_version.h" 31 #include "base/win/windows_version.h"
27 #include "net/base/net_util_win.h" 32 #include "net/base/net_util_win.h"
28 #elif !defined(OS_ANDROID)
29 #include <net/if.h>
30 #endif // OS_WIN 33 #endif // OS_WIN
31 34
35 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_WIN)
36 #include "net/base/address_tracker_linux.h"
37 #endif // !OS_MACOSX && !OS_NACL && !OS_WIN
38
39 #if !defined(OS_WIN)
40 #include "net/base/net_util_posix.h"
41 #endif // !OS_WIN
42
32 using base::ASCIIToUTF16; 43 using base::ASCIIToUTF16;
33 using base::WideToUTF16; 44 using base::WideToUTF16;
34 45
35 namespace net { 46 namespace net {
36 47
37 namespace { 48 namespace {
38 49
39 struct HeaderCase { 50 struct HeaderCase {
40 const char* header_name; 51 const char* header_name;
41 const char* expected; 52 const char* expected;
(...skipping 752 matching lines...) Expand 10 before | Expand all | Expand 10 after
794 EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE, GetWifiPHYLayerProtocol()); 805 EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE, GetWifiPHYLayerProtocol());
795 } 806 }
796 #elif !defined(OS_ANDROID) 807 #elif !defined(OS_ANDROID)
797 char name[IF_NAMESIZE]; 808 char name[IF_NAMESIZE];
798 EXPECT_TRUE(if_indextoname(it->interface_index, name)); 809 EXPECT_TRUE(if_indextoname(it->interface_index, name));
799 EXPECT_STREQ(it->name.c_str(), name); 810 EXPECT_STREQ(it->name.c_str(), name);
800 #endif 811 #endif
801 } 812 }
802 } 813 }
803 814
815 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_NACL)
816
817 char* CopyInterfaceName(const char* ifname, int ifname_size, char* output) {
818 EXPECT_LT(ifname_size, IF_NAMESIZE);
819 memcpy(output, ifname, ifname_size);
820 return output;
821 }
822
823 static const char ifname_em1[] = "em1";
824 char* GetInterfaceName(unsigned int interface_index, char* ifname) {
825 return CopyInterfaceName(ifname_em1, arraysize(ifname_em1), ifname);
826 }
827
828 static const char ifname_vm[] = "vmnet";
829 char* GetInterfaceNameVM(unsigned int interface_index, char* ifname) {
830 return CopyInterfaceName(ifname_vm, arraysize(ifname_vm), ifname);
831 }
832
833 TEST(NetUtilTest, GetNetworkListTrimming) {
834 NetworkInterfaceList results;
835 ::base::hash_set<int> online_links;
836 net::internal::AddressTrackerLinux::AddressMap address_map;
837
838 const unsigned char kIPv6LocalAddr[] = {
839 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
840 const unsigned char kIPv6Addr[] =
841 {0x24, 0x01, 0xfa, 0x00, 0x00, 0x04, 0x10, 0x00, 0xbe, 0x30, 0x5b, 0xff,
842 0xfe, 0xe5, 0x00, 0xc3};
843
844 IPAddressNumber ipv6_local_address(
845 kIPv6LocalAddr, kIPv6LocalAddr + arraysize(kIPv6LocalAddr));
846 IPAddressNumber ipv6_address(kIPv6Addr, kIPv6Addr + arraysize(kIPv6Addr));
847
848 // Interface 1 is offline.
849 struct ifaddrmsg msg = {
850 AF_INET6,
851 1, /* prefix length */
852 IFA_F_TEMPORARY, /* address flags */
853 0, /* link scope */
854 1 /* link index */
855 };
856
857 // Address of offline links should be ignored.
858 ASSERT_TRUE(address_map.insert(std::make_pair(ipv6_address, msg)).second);
859 EXPECT_TRUE(
860 net::internal::GetNetworkListImpl(&results,
861 INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES,
862 online_links,
863 address_map,
864 GetInterfaceName));
865 EXPECT_EQ(results.size(), 0ul);
866
867 // Mark interface 1 online.
868 online_links.insert(1);
869
870 // Local address should be trimmed out.
871 address_map.clear();
872 ASSERT_TRUE(
873 address_map.insert(std::make_pair(ipv6_local_address, msg)).second);
874 EXPECT_TRUE(
875 net::internal::GetNetworkListImpl(&results,
876 INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES,
877 online_links,
878 address_map,
879 GetInterfaceName));
880 EXPECT_EQ(results.size(), 0ul);
881
882 // vmware address should return by default.
883 address_map.clear();
884 ASSERT_TRUE(address_map.insert(std::make_pair(ipv6_address, msg)).second);
885 EXPECT_TRUE(
886 net::internal::GetNetworkListImpl(&results,
887 INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES,
888 online_links,
889 address_map,
890 GetInterfaceNameVM));
891 EXPECT_EQ(results.size(), 1ul);
892 EXPECT_EQ(results[0].name, ifname_vm);
893 EXPECT_EQ(results[0].network_prefix, 1ul);
894 EXPECT_EQ(results[0].address, ipv6_address);
895 results.clear();
896
897 // vmware address should be trimmed out if policy specified so.
898 address_map.clear();
899 ASSERT_TRUE(address_map.insert(std::make_pair(ipv6_address, msg)).second);
900 EXPECT_TRUE(
901 net::internal::GetNetworkListImpl(&results,
902 EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES,
903 online_links,
904 address_map,
905 GetInterfaceNameVM));
906 EXPECT_EQ(results.size(), 0ul);
907 results.clear();
908
909 // Addresses with banned attributes should be ignored.
910 address_map.clear();
911 msg.ifa_flags = IFA_F_TENTATIVE;
912 ASSERT_TRUE(address_map.insert(std::make_pair(ipv6_address, msg)).second);
913 EXPECT_TRUE(
914 net::internal::GetNetworkListImpl(&results,
915 INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES,
916 online_links,
917 address_map,
918 GetInterfaceName));
919 EXPECT_EQ(results.size(), 0ul);
920 results.clear();
921
922 // Addresses with allowed attribute IFA_F_TEMPORARY should be returned and
923 // attributes should be translated correctly.
924 address_map.clear();
925 msg.ifa_flags = IFA_F_TEMPORARY;
926 ASSERT_TRUE(address_map.insert(std::make_pair(ipv6_address, msg)).second);
927 EXPECT_TRUE(
928 net::internal::GetNetworkListImpl(&results,
929 INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES,
930 online_links,
931 address_map,
932 GetInterfaceName));
933 EXPECT_EQ(results.size(), 1ul);
934 EXPECT_EQ(results[0].name, ifname_em1);
935 EXPECT_EQ(results[0].network_prefix, 1ul);
936 EXPECT_EQ(results[0].address, ipv6_address);
937 EXPECT_EQ(results[0].ip_address_attributes, IP_ADDRESS_ATTRIBUTE_TEMPORARY);
938 results.clear();
939
940 // Addresses with allowed attribute IFA_F_DEPRECATED should be returned and
941 // attributes should be translated correctly.
942 address_map.clear();
943 msg.ifa_flags = IFA_F_DEPRECATED;
944 ASSERT_TRUE(address_map.insert(std::make_pair(ipv6_address, msg)).second);
945 EXPECT_TRUE(
946 net::internal::GetNetworkListImpl(&results,
947 INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES,
948 online_links,
949 address_map,
950 GetInterfaceName));
951 EXPECT_EQ(results.size(), 1ul);
952 EXPECT_EQ(results[0].name, ifname_em1);
953 EXPECT_EQ(results[0].network_prefix, 1ul);
954 EXPECT_EQ(results[0].address, ipv6_address);
955 EXPECT_EQ(results[0].ip_address_attributes, IP_ADDRESS_ATTRIBUTE_DEPRECATED);
956 results.clear();
957 }
958
959 #endif
960
804 namespace { 961 namespace {
805 962
806 #if defined(OS_WIN) 963 #if defined(OS_WIN)
807 bool read_int_or_bool(DWORD data_size, 964 bool read_int_or_bool(DWORD data_size,
808 PVOID data) { 965 PVOID data) {
809 switch (data_size) { 966 switch (data_size) {
810 case 1: 967 case 1:
811 return !!*reinterpret_cast<uint8*>(data); 968 return !!*reinterpret_cast<uint8*>(data);
812 case 4: 969 case 4:
813 return !!*reinterpret_cast<uint32*>(data); 970 return !!*reinterpret_cast<uint32*>(data);
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
984 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { 1141 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) {
985 const NonUniqueNameTestData& test_data = GetParam(); 1142 const NonUniqueNameTestData& test_data = GetParam();
986 1143
987 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); 1144 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname));
988 } 1145 }
989 1146
990 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, 1147 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest,
991 testing::ValuesIn(kNonUniqueNameTestData)); 1148 testing::ValuesIn(kNonUniqueNameTestData));
992 1149
993 } // namespace net 1150 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util_posix.cc ('k') | net/base/sdch_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698