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