OLD | NEW |
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 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
22 | 22 |
23 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
24 #include <iphlpapi.h> | 24 #include <iphlpapi.h> |
25 #include <objbase.h> | 25 #include <objbase.h> |
26 #include "base/win/windows_version.h" | 26 #include "base/win/windows_version.h" |
| 27 #include "net/base/net_util_win.h" |
27 #elif !defined(OS_ANDROID) | 28 #elif !defined(OS_ANDROID) |
28 #include <net/if.h> | 29 #include <net/if.h> |
29 #endif // OS_WIN | 30 #endif // OS_WIN |
30 | 31 |
31 using base::ASCIIToUTF16; | 32 using base::ASCIIToUTF16; |
32 using base::WideToUTF16; | 33 using base::WideToUTF16; |
33 | 34 |
34 namespace net { | 35 namespace net { |
35 | 36 |
36 namespace { | 37 namespace { |
(...skipping 746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE, GetWifiPHYLayerProtocol()); | 784 EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE, GetWifiPHYLayerProtocol()); |
784 } | 785 } |
785 #elif !defined(OS_ANDROID) | 786 #elif !defined(OS_ANDROID) |
786 char name[IF_NAMESIZE]; | 787 char name[IF_NAMESIZE]; |
787 EXPECT_TRUE(if_indextoname(it->interface_index, name)); | 788 EXPECT_TRUE(if_indextoname(it->interface_index, name)); |
788 EXPECT_STREQ(it->name.c_str(), name); | 789 EXPECT_STREQ(it->name.c_str(), name); |
789 #endif | 790 #endif |
790 } | 791 } |
791 } | 792 } |
792 | 793 |
| 794 namespace { |
| 795 |
| 796 #if defined(OS_WIN) |
| 797 bool read_int_or_bool(DWORD data_size, |
| 798 PVOID data) { |
| 799 switch (data_size) { |
| 800 case 1: |
| 801 return !!*reinterpret_cast<uint8*>(data); |
| 802 case 4: |
| 803 return !!*reinterpret_cast<uint32*>(data); |
| 804 default: |
| 805 LOG(FATAL) << "That is not a type I know!"; |
| 806 return false; |
| 807 } |
| 808 } |
| 809 |
| 810 int GetWifiOptions() { |
| 811 const internal::WlanApi& wlanapi = internal::WlanApi::GetInstance(); |
| 812 if (!wlanapi.initialized) |
| 813 return -1; |
| 814 |
| 815 internal::WlanHandle client; |
| 816 DWORD cur_version = 0; |
| 817 const DWORD kMaxClientVersion = 2; |
| 818 DWORD result = wlanapi.OpenHandle( |
| 819 kMaxClientVersion, &cur_version, &client); |
| 820 if (result != ERROR_SUCCESS) |
| 821 return -1; |
| 822 |
| 823 WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL; |
| 824 result = wlanapi.enum_interfaces_func(client, NULL, &interface_list_ptr); |
| 825 if (result != ERROR_SUCCESS) |
| 826 return -1; |
| 827 scoped_ptr<WLAN_INTERFACE_INFO_LIST, internal::WlanApiDeleter> interface_list( |
| 828 interface_list_ptr); |
| 829 |
| 830 for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) { |
| 831 WLAN_INTERFACE_INFO* info = &interface_list->InterfaceInfo[i]; |
| 832 DWORD data_size; |
| 833 PVOID data; |
| 834 int options = 0; |
| 835 result = wlanapi.query_interface_func( |
| 836 client, |
| 837 &info->InterfaceGuid, |
| 838 wlan_intf_opcode_background_scan_enabled, |
| 839 NULL, |
| 840 &data_size, |
| 841 &data, |
| 842 NULL); |
| 843 if (result != ERROR_SUCCESS) |
| 844 continue; |
| 845 if (!read_int_or_bool(data_size, data)) { |
| 846 options |= WIFI_OPTIONS_DISABLE_SCAN; |
| 847 } |
| 848 internal::WlanApi::GetInstance().free_memory_func(data); |
| 849 |
| 850 result = wlanapi.query_interface_func( |
| 851 client, |
| 852 &info->InterfaceGuid, |
| 853 wlan_intf_opcode_media_streaming_mode, |
| 854 NULL, |
| 855 &data_size, |
| 856 &data, |
| 857 NULL); |
| 858 if (result != ERROR_SUCCESS) |
| 859 continue; |
| 860 if (read_int_or_bool(data_size, data)) { |
| 861 options |= WIFI_OPTIONS_MEDIA_STREAMING_MODE; |
| 862 } |
| 863 internal::WlanApi::GetInstance().free_memory_func(data); |
| 864 |
| 865 // Just the the options from the first succesful |
| 866 // interface. |
| 867 return options; |
| 868 } |
| 869 |
| 870 // No wifi interface found. |
| 871 return -1; |
| 872 } |
| 873 |
| 874 #else // OS_WIN |
| 875 |
| 876 int GetWifiOptions() { |
| 877 // Not supported. |
| 878 return -1; |
| 879 } |
| 880 |
| 881 #endif // OS_WIN |
| 882 |
| 883 void TryChangeWifiOptions(int options) { |
| 884 int previous_options = GetWifiOptions(); |
| 885 scoped_ptr<ScopedWifiOptions> scoped_options = SetWifiOptions(options); |
| 886 EXPECT_EQ(previous_options | options, GetWifiOptions()); |
| 887 scoped_options.reset(); |
| 888 EXPECT_EQ(previous_options, GetWifiOptions()); |
| 889 } |
| 890 |
| 891 }; // namespace |
| 892 |
| 893 // Test SetWifiOptions(). |
| 894 TEST(NetUtilTest, SetWifiOptionsTest) { |
| 895 TryChangeWifiOptions(0); |
| 896 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN); |
| 897 TryChangeWifiOptions(WIFI_OPTIONS_MEDIA_STREAMING_MODE); |
| 898 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN | |
| 899 WIFI_OPTIONS_MEDIA_STREAMING_MODE); |
| 900 } |
| 901 |
793 struct NonUniqueNameTestData { | 902 struct NonUniqueNameTestData { |
794 bool is_unique; | 903 bool is_unique; |
795 const char* hostname; | 904 const char* hostname; |
796 }; | 905 }; |
797 | 906 |
798 // Google Test pretty-printer. | 907 // Google Test pretty-printer. |
799 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { | 908 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { |
800 ASSERT_TRUE(data.hostname); | 909 ASSERT_TRUE(data.hostname); |
801 *os << " hostname: " << testing::PrintToString(data.hostname) | 910 *os << " hostname: " << testing::PrintToString(data.hostname) |
802 << "; is_unique: " << testing::PrintToString(data.is_unique); | 911 << "; is_unique: " << testing::PrintToString(data.is_unique); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
864 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { | 973 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { |
865 const NonUniqueNameTestData& test_data = GetParam(); | 974 const NonUniqueNameTestData& test_data = GetParam(); |
866 | 975 |
867 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); | 976 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); |
868 } | 977 } |
869 | 978 |
870 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, | 979 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, |
871 testing::ValuesIn(kNonUniqueNameTestData)); | 980 testing::ValuesIn(kNonUniqueNameTestData)); |
872 | 981 |
873 } // namespace net | 982 } // namespace net |
OLD | NEW |