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

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

Issue 566243005: Cast: Allow extension to control wifi options on windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add missing NET_EXPORT Created 6 years, 3 months 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
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 #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
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 OS_WIN
pauljensen 2014/09/23 14:03:54 defined(OS_WIN)
hubbe 2014/09/23 17:37:49 Done.
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 WlanApi& wlanapi = WlanApi::GetInstance();
812 if (!wlanapi.initialized)
813 return -1;
814
815 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, 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 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 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
pauljensen 2014/09/23 14:03:55 unnecessary lines
hubbe 2014/09/23 17:37:49 Done.
883
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 }
pauljensen 2014/09/23 14:03:54 need a new line
hubbe 2014/09/23 17:37:49 Done.
892 };
pauljensen 2014/09/23 14:03:55 // namespace
hubbe 2014/09/23 17:37:49 Done.
893
894 // Test SetWifiOptions().
895 TEST(NetUtilTest, SetWifiOptionsTest) {
896 TryChangeWifiOptions(0);
897 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN);
898 TryChangeWifiOptions(WIFI_OPTIONS_MEDIA_STREAMING_MODE);
899 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN |
900 WIFI_OPTIONS_MEDIA_STREAMING_MODE);
901 }
902
903
pauljensen 2014/09/23 14:03:54 unnecessary line
hubbe 2014/09/23 17:37:49 Done.
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
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
OLDNEW
« no previous file with comments | « net/base/net_util_posix.cc ('k') | net/base/net_util_win.h » ('j') | net/base/net_util_win.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698