1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
// Copyright 2010 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/win/win_util.h"
#include <objbase.h>
#include <ntstatus.h>
#include <string_view>
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/process/process.h"
#include "base/process/process_handle.h"
#include "base/scoped_environment_variable_override.h"
#include "base/scoped_native_library.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_expected_support.h"
#include "base/win/registry.h"
#include "base/win/scoped_co_mem.h"
#include "base/win/scoped_com_initializer.h"
#include "base/win/windows_handle_util.h"
#include "base/win/windows_version.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace win {
namespace {
// Saves the current thread's locale ID when initialized, and restores it when
// the instance is going out of scope.
class ThreadLocaleSaver {
public:
ThreadLocaleSaver() : original_locale_id_(GetThreadLocale()) {}
ThreadLocaleSaver(const ThreadLocaleSaver&) = delete;
ThreadLocaleSaver& operator=(const ThreadLocaleSaver&) = delete;
~ThreadLocaleSaver() { SetThreadLocale(original_locale_id_); }
private:
LCID original_locale_id_;
};
auto* csm_false = static_cast<bool (*)()>([]() -> bool { return false; });
auto* csm_true = static_cast<bool (*)()>([]() -> bool { return true; });
} // namespace
// The test is somewhat silly, because some bots some have UAC enabled and some
// have it disabled. At least we check that it does not crash.
TEST(BaseWinUtilTest, TestIsUACEnabled) {
UserAccountControlIsEnabled();
}
TEST(BaseWinUtilTest, TestGetUserSidString) {
std::wstring user_sid;
EXPECT_TRUE(GetUserSidString(&user_sid));
EXPECT_TRUE(!user_sid.empty());
}
TEST(BaseWinUtilTest, TestGetLoadedModulesSnapshot) {
std::vector<HMODULE> snapshot;
ASSERT_TRUE(GetLoadedModulesSnapshot(::GetCurrentProcess(), &snapshot));
size_t original_snapshot_size = snapshot.size();
ASSERT_GT(original_snapshot_size, 0u);
snapshot.clear();
// Load in a new module. Pick zipfldr.dll as it is present from WinXP to
// Win10, including ARM64 Win10, and yet rarely used.
const FilePath::CharType dll_name[] = FILE_PATH_LITERAL("zipfldr.dll");
ASSERT_EQ(nullptr, ::GetModuleHandle(dll_name));
ScopedNativeLibrary new_dll((FilePath(dll_name)));
ASSERT_NE(static_cast<HMODULE>(nullptr), new_dll.get());
ASSERT_TRUE(GetLoadedModulesSnapshot(::GetCurrentProcess(), &snapshot));
ASSERT_GT(snapshot.size(), original_snapshot_size);
ASSERT_TRUE(Contains(snapshot, new_dll.get()));
}
TEST(BaseWinUtilTest, TestUint32ToInvalidHandle) {
// Ensure that INVALID_HANDLE_VALUE is preserved when going to a 32-bit value
// and back on 64-bit platforms.
uint32_t invalid_handle = HandleToUint32(INVALID_HANDLE_VALUE);
EXPECT_EQ(INVALID_HANDLE_VALUE, Uint32ToHandle(invalid_handle));
}
TEST(BaseWinUtilTest, PseudoHandles) {
EXPECT_TRUE(IsPseudoHandle(::GetCurrentProcess()));
EXPECT_TRUE(IsPseudoHandle(::GetCurrentThread()));
EXPECT_FALSE(IsPseudoHandle(nullptr));
}
TEST(BaseWinUtilTest, WStringFromGUID) {
const GUID kGuid = {0x7698f759,
0xf5b0,
0x4328,
{0x92, 0x38, 0xbd, 0x70, 0x8a, 0x6d, 0xc9, 0x63}};
const std::wstring_view kGuidStr = L"{7698F759-F5B0-4328-9238-BD708A6DC963}";
auto guid_wstring = WStringFromGUID(kGuid);
EXPECT_EQ(guid_wstring, kGuidStr);
wchar_t guid_wchar[39];
::StringFromGUID2(kGuid, guid_wchar, std::size(guid_wchar));
EXPECT_STREQ(guid_wstring.c_str(), guid_wchar);
ScopedCoMem<OLECHAR> clsid_string;
::StringFromCLSID(kGuid, &clsid_string);
EXPECT_STREQ(guid_wstring.c_str(), clsid_string.get());
}
TEST(BaseWinUtilTest, GetWindowObjectName) {
std::wstring created_desktop_name(L"test_desktop");
HDESK desktop_handle =
::CreateDesktop(created_desktop_name.c_str(), nullptr, nullptr, 0,
DESKTOP_CREATEWINDOW | DESKTOP_READOBJECTS |
READ_CONTROL | WRITE_DAC | WRITE_OWNER,
nullptr);
ASSERT_NE(desktop_handle, nullptr);
EXPECT_EQ(created_desktop_name, GetWindowObjectName(desktop_handle));
ASSERT_TRUE(::CloseDesktop(desktop_handle));
}
TEST(BaseWinUtilTest, IsRunningUnderDesktopName) {
HDESK thread_desktop = ::GetThreadDesktop(::GetCurrentThreadId());
ASSERT_NE(thread_desktop, nullptr);
std::wstring desktop_name = GetWindowObjectName(thread_desktop);
EXPECT_TRUE(IsRunningUnderDesktopName(desktop_name));
EXPECT_TRUE(IsRunningUnderDesktopName(
AsWString(ToLowerASCII(AsStringPiece16(desktop_name)))));
EXPECT_TRUE(IsRunningUnderDesktopName(
AsWString(ToUpperASCII(AsStringPiece16(desktop_name)))));
EXPECT_FALSE(
IsRunningUnderDesktopName(desktop_name + L"_non_existent_desktop_name"));
}
TEST(BaseWinUtilTest, ExpandEnvironmentVariables) {
constexpr char kTestEnvVar[] = "TEST_ENV_VAR";
constexpr char kTestEnvVarValue[] = "TEST_VALUE";
ScopedEnvironmentVariableOverride scoped_env(kTestEnvVar, kTestEnvVarValue);
auto path_with_env_var = UTF8ToWide(std::string("C:\\%") + kTestEnvVar + "%");
auto path_expanded = UTF8ToWide(std::string("C:\\") + kTestEnvVarValue);
EXPECT_EQ(ExpandEnvironmentVariables(path_with_env_var).value(),
path_expanded);
}
TEST(BaseWinUtilTest, ExpandEnvironmentVariablesEmptyValue) {
constexpr char kTestEnvVar[] = "TEST_ENV_VAR";
constexpr char kTestEnvVarValue[] = "";
ScopedEnvironmentVariableOverride scoped_env(kTestEnvVar, kTestEnvVarValue);
auto path_with_env_var = UTF8ToWide(std::string("C:\\%") + kTestEnvVar + "%");
auto path_expanded = UTF8ToWide(std::string("C:\\") + kTestEnvVarValue);
EXPECT_EQ(ExpandEnvironmentVariables(path_with_env_var).value(),
path_expanded);
}
TEST(BaseWinUtilTest, ExpandEnvironmentVariablesUndefinedValue) {
constexpr char kTestEnvVar[] = "TEST_ENV_VAR";
auto path_with_env_var = UTF8ToWide(std::string("C:\\%") + kTestEnvVar + "%");
// Undefined env vars are left unexpanded.
auto path_expanded = path_with_env_var;
EXPECT_EQ(ExpandEnvironmentVariables(path_with_env_var).value(),
path_expanded);
}
TEST(BaseWinUtilTest, ProcessPowerThrottling) {
if (GetVersion() < Version::WIN11_22H2) {
GTEST_SKIP() << "Test only applies to Windows 11 22H2 and later.";
}
// Clear any previous state.
ASSERT_TRUE(
SetProcessEcoQoSState(::GetCurrentProcess(), ProcessPowerState::kUnset));
ASSERT_TRUE(SetProcessTimerThrottleState(::GetCurrentProcess(),
ProcessPowerState::kUnset));
// Verify the initial state.
ASSERT_TRUE(GetProcessEcoQoSState(::GetCurrentProcess()) ==
ProcessPowerState::kUnset);
ASSERT_TRUE(GetProcessTimerThrottleState(::GetCurrentProcess()) ==
ProcessPowerState::kUnset);
// Verify setting the EcoQoS state.
ASSERT_TRUE(SetProcessEcoQoSState(::GetCurrentProcess(),
ProcessPowerState::kEnabled));
ASSERT_TRUE(GetProcessEcoQoSState(::GetCurrentProcess()) ==
ProcessPowerState::kEnabled);
ASSERT_TRUE(
SetProcessEcoQoSState(::GetCurrentProcess(), ProcessPowerState::kUnset));
// Verify setting the timer resolution state.
ASSERT_TRUE(SetProcessTimerThrottleState(::GetCurrentProcess(),
ProcessPowerState::kEnabled));
ASSERT_TRUE(GetProcessTimerThrottleState(::GetCurrentProcess()) ==
ProcessPowerState::kEnabled);
// Set the EcoQoS state again and verify the timer throttling state is not
// clobbered.
ASSERT_TRUE(SetProcessEcoQoSState(::GetCurrentProcess(),
ProcessPowerState::kEnabled));
ASSERT_TRUE(GetProcessEcoQoSState(::GetCurrentProcess()) ==
ProcessPowerState::kEnabled);
ASSERT_TRUE(GetProcessTimerThrottleState(::GetCurrentProcess()) ==
ProcessPowerState::kEnabled);
// Disable the EcoQoS state and verify the timer throttling state is not
// clobbered.
ASSERT_TRUE(SetProcessEcoQoSState(::GetCurrentProcess(),
ProcessPowerState::kDisabled));
ASSERT_TRUE(GetProcessEcoQoSState(::GetCurrentProcess()) ==
ProcessPowerState::kDisabled);
ASSERT_TRUE(GetProcessTimerThrottleState(::GetCurrentProcess()) ==
ProcessPowerState::kEnabled);
// Disable the timer throttling state and verify state.
ASSERT_TRUE(SetProcessTimerThrottleState(::GetCurrentProcess(),
ProcessPowerState::kDisabled));
ASSERT_TRUE(GetProcessTimerThrottleState(::GetCurrentProcess()) ==
ProcessPowerState::kDisabled);
ASSERT_TRUE(GetProcessEcoQoSState(::GetCurrentProcess()) ==
ProcessPowerState::kDisabled);
// Enable both states and verify.
ASSERT_TRUE(SetProcessEcoQoSState(::GetCurrentProcess(),
ProcessPowerState::kEnabled));
ASSERT_TRUE(SetProcessTimerThrottleState(::GetCurrentProcess(),
ProcessPowerState::kEnabled));
ASSERT_TRUE(GetProcessEcoQoSState(::GetCurrentProcess()) ==
ProcessPowerState::kEnabled);
ASSERT_TRUE(GetProcessTimerThrottleState(::GetCurrentProcess()) ==
ProcessPowerState::kEnabled);
// Clear both states and verify.
ASSERT_TRUE(
SetProcessEcoQoSState(::GetCurrentProcess(), ProcessPowerState::kUnset));
ASSERT_TRUE(SetProcessTimerThrottleState(::GetCurrentProcess(),
ProcessPowerState::kUnset));
ASSERT_TRUE(GetProcessEcoQoSState(::GetCurrentProcess()) ==
ProcessPowerState::kUnset);
ASSERT_TRUE(GetProcessTimerThrottleState(::GetCurrentProcess()) ==
ProcessPowerState::kUnset);
}
TEST(GetObjectTypeNameTest, NullHandle) {
auto name_or_error = GetObjectTypeName(kNullProcessHandle);
ASSERT_FALSE(name_or_error.has_value());
ASSERT_EQ(name_or_error.error(), STATUS_INVALID_HANDLE);
}
TEST(GetObjectTypeNameTest, InvalidHandle) {
auto name_or_error = GetObjectTypeName(INVALID_HANDLE_VALUE);
ASSERT_FALSE(name_or_error.has_value());
ASSERT_EQ(name_or_error.error(), STATUS_INVALID_HANDLE);
}
TEST(GetObjectTypeNameTest, CurrentProcess) {
auto name_or_error = GetObjectTypeName(::GetCurrentProcess());
ASSERT_FALSE(name_or_error.has_value());
ASSERT_EQ(name_or_error.error(), STATUS_INVALID_HANDLE);
}
TEST(GetObjectTypeNameTest, CrazyHandle) {
auto name_or_error = GetObjectTypeName(Uint32ToHandle(0x12345678U));
ASSERT_FALSE(name_or_error.has_value());
ASSERT_EQ(name_or_error.error(), STATUS_INVALID_HANDLE);
}
TEST(GetObjectTypeNameTest, ProcessHandle) {
Process this_process = Process::Open(GetCurrentProcId());
ASSERT_OK_AND_ASSIGN(std::wstring type_name,
GetObjectTypeName(this_process.Handle()));
ASSERT_EQ(type_name, L"Process");
}
TEST(DeviceConvertibilityTest, None) {
ScopedCOMInitializer com_initializer;
ASSERT_TRUE(com_initializer.Succeeded());
ScopedDeviceConvertibilityStateForTesting scoper(false, false, csm_false,
std::nullopt, std::nullopt);
EXPECT_FALSE(QueryDeviceConvertibility());
}
TEST(DeviceConvertibilityTest, ConvertibilityDisabled) {
ScopedCOMInitializer com_initializer;
ASSERT_TRUE(com_initializer.Succeeded());
// If convertibility is not enabled but the key exists, other values shouldn't
// be checked. Device is not convertible.
ScopedDeviceConvertibilityStateForTesting scoper(
/*form_convertible=*/true, /*chassis_convertible=*/true,
/*csm_changed=*/csm_false, /*convertible_chassis_key=*/std::nullopt,
/*convertibility_enabled=*/std::optional<bool>{false});
EXPECT_FALSE(QueryDeviceConvertibility());
}
TEST(DeviceConvertibilityTest, ConvertibilityEnabled) {
ScopedCOMInitializer com_initializer;
ASSERT_TRUE(com_initializer.Succeeded());
ScopedDeviceConvertibilityStateForTesting scoper(
/*form_convertible=*/false, /*chassis_convertible=*/false,
/*csm_changed=*/csm_false, /*convertible_chassis_key=*/std::nullopt,
/*convertibility_enabled=*/std::optional<bool>{true});
EXPECT_TRUE(QueryDeviceConvertibility());
}
TEST(DeviceConvertibilityTest, ChassisConvertibleKeyTrue) {
ScopedCOMInitializer com_initializer;
ASSERT_TRUE(com_initializer.Succeeded());
ScopedDeviceConvertibilityStateForTesting scoper(
/*form_convertible=*/false, /*chassis_convertible=*/false,
/*csm_changed=*/csm_false,
/*convertible_chassis_key=*/std::optional<bool>{true},
/*convertibility_enabled=*/std::nullopt);
EXPECT_TRUE(QueryDeviceConvertibility());
}
TEST(DeviceConvertibilityTest, ChassisConvertibleKeyFalse) {
ScopedCOMInitializer com_initializer;
ASSERT_TRUE(com_initializer.Succeeded());
ScopedDeviceConvertibilityStateForTesting scoper(
/*form_convertible=*/false, /*chassis_convertible=*/true,
/*csm_changed=*/csm_true,
/*convertible_chassis_key=*/std::optional<bool>{false},
/*convertibility_enabled=*/std::nullopt);
EXPECT_FALSE(QueryDeviceConvertibility());
}
TEST(DeviceConvertibilityTest, FormConvertibleTrue) {
ScopedCOMInitializer com_initializer;
ASSERT_TRUE(com_initializer.Succeeded());
ScopedDeviceConvertibilityStateForTesting scoper(
/*form_convertible=*/true, /*chassis_convertible=*/false,
/*csm_changed=*/csm_false,
/*convertible_chassis_key=*/std::nullopt,
/*convertibility_enabled=*/std::nullopt);
EXPECT_TRUE(QueryDeviceConvertibility());
}
TEST(DeviceConvertibilityTest, ChassisConvertibleTrue) {
ScopedCOMInitializer com_initializer;
ASSERT_TRUE(com_initializer.Succeeded());
ScopedDeviceConvertibilityStateForTesting scoper(
/*form_convertible=*/false, /*chassis_convertible=*/true,
/*csm_changed=*/csm_false,
/*convertible_chassis_key=*/std::nullopt,
/*convertibility_enabled=*/std::nullopt);
EXPECT_TRUE(QueryDeviceConvertibility());
}
TEST(DeviceConvertibilityTest, ConvertibleSlateModeChangeTrue) {
ScopedCOMInitializer com_initializer;
ASSERT_TRUE(com_initializer.Succeeded());
ScopedDeviceConvertibilityStateForTesting scoper(
/*form_convertible=*/false, /*chassis_convertible=*/false,
/*csm_changed=*/csm_true,
/*convertible_chassis_key=*/std::nullopt,
/*convertibility_enabled=*/std::nullopt);
EXPECT_TRUE(QueryDeviceConvertibility());
}
TEST(DeviceConvertibilityTest, ConvertibilityEnabledSanityCheck) {
RegKey key(HKEY_LOCAL_MACHINE,
L"System\\CurrentControlSet\\Control\\PriorityControl", KEY_READ);
if (key.HasValue(L"ConvertibilityEnabled")) {
ASSERT_TRUE(GetConvertibilityEnabledOverride().has_value());
} else {
ASSERT_FALSE(GetConvertibilityEnabledOverride().has_value());
}
}
TEST(DeviceConvertibilityTest, ConvertibilityKeySanityCheck) {
RegKey key(HKEY_CURRENT_USER,
L"SOFTWARE\\Microsoft\\TabletTip\\ConvertibleChassis", KEY_READ);
if (key.HasValue(L"ConvertibleChassis")) {
ASSERT_TRUE(GetConvertibleChassisKeyValue().has_value());
} else {
ASSERT_FALSE(GetConvertibleChassisKeyValue().has_value());
}
}
TEST(DeviceConvertibilityTest, DeviceFormAndChassisConvertible) {
ScopedCOMInitializer com_initializer;
ASSERT_TRUE(com_initializer.Succeeded());
EXPECT_FALSE(IsDeviceFormConvertible() || IsChassisConvertible());
}
TEST(BaseWinUtilTest, GetSerialNumber) {
ScopedCOMInitializer com_initializer;
ASSERT_OK_AND_ASSIGN(std::wstring serial_number, GetSerialNumber());
EXPECT_FALSE(serial_number.empty());
}
} // namespace win
} // namespace base
|