Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/environment.h" | 9 #include "base/environment.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/scoped_native_library.h" | 16 #include "base/scoped_native_library.h" |
| 17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/test/test_reg_util_win.h" | 20 #include "base/test/test_reg_util_win.h" |
| 21 #include "base/test/test_timeouts.h" | |
| 21 #include "base/win/registry.h" | 22 #include "base/win/registry.h" |
| 22 #include "chrome/common/chrome_version.h" | 23 #include "chrome/common/chrome_version.h" |
| 23 #include "chrome/install_static/install_util.h" | 24 #include "chrome/install_static/install_util.h" |
| 24 #include "chrome_elf/blacklist/blacklist.h" | 25 #include "chrome_elf/blacklist/blacklist.h" |
| 25 #include "chrome_elf/chrome_elf_constants.h" | 26 #include "chrome_elf/chrome_elf_constants.h" |
| 26 #include "chrome_elf/nt_registry/nt_registry.h" | 27 #include "chrome_elf/nt_registry/nt_registry.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 29 |
| 29 const wchar_t kTestDllName1[] = L"blacklist_test_dll_1.dll"; | 30 const wchar_t kTestDllName1[] = L"blacklist_test_dll_1.dll"; |
| 30 const wchar_t kTestDllName2[] = L"blacklist_test_dll_2.dll"; | 31 const wchar_t kTestDllName2[] = L"blacklist_test_dll_2.dll"; |
| 31 const wchar_t kTestDllName3[] = L"blacklist_test_dll_3.dll"; | 32 const wchar_t kTestDllName3[] = L"blacklist_test_dll_3.dll"; |
| 32 | 33 |
| 33 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; | 34 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; |
| 34 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; | 35 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; |
| 35 | 36 |
| 36 extern const wchar_t* kEnvVars[]; | 37 extern const wchar_t* kEnvVars[]; |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 41 // Timeouts for synchronization. | |
| 42 #define event_timeout \ | |
|
robertshield
2017/05/05 13:04:43
nit, mildly prefer: kEventTimeout or EVENT_TIMEOUT
grt (UTC plus 2)
2017/05/05 13:11:47
if this can't be a true constant owing to TestTime
| |
| 43 static_cast<DWORD>((TestTimeouts::action_timeout()).InMillisecondsRoundedUp()) | |
| 44 | |
| 45 const wchar_t* g_blacklist_test_mutex = L"ChromeElfBlacklistTestMutex"; | |
|
grt (UTC plus 2)
2017/05/05 13:11:47
also from the nit factory:
constexpr wchar_t g_bl
| |
| 46 | |
| 40 // Functions we need from blacklist_test_main_dll.dll | 47 // Functions we need from blacklist_test_main_dll.dll |
| 41 typedef bool (*TestDll_AddDllToBlacklistFunction)(const wchar_t* dll_name); | 48 typedef bool (*TestDll_AddDllToBlacklistFunction)(const wchar_t* dll_name); |
| 42 typedef int (*TestDll_BlacklistSizeFunction)(); | 49 typedef int (*TestDll_BlacklistSizeFunction)(); |
| 43 typedef void (*TestDll_BlockedDllFunction)(size_t blocked_index); | 50 typedef void (*TestDll_BlockedDllFunction)(size_t blocked_index); |
| 44 typedef int (*TestDll_GetBlacklistIndexFunction)(const wchar_t* dll_name); | 51 typedef int (*TestDll_GetBlacklistIndexFunction)(const wchar_t* dll_name); |
| 45 typedef bool (*TestDll_IsBlacklistInitializedFunction)(); | 52 typedef bool (*TestDll_IsBlacklistInitializedFunction)(); |
| 46 typedef bool (*TestDll_RemoveDllFromBlacklistFunction)(const wchar_t* dll_name); | 53 typedef bool (*TestDll_RemoveDllFromBlacklistFunction)(const wchar_t* dll_name); |
| 47 typedef bool (*TestDll_SuccessfullyBlockedFunction)( | 54 typedef bool (*TestDll_SuccessfullyBlockedFunction)( |
| 48 const wchar_t** blocked_dlls, | 55 const wchar_t** blocked_dlls, |
| 49 int* size); | 56 int* size); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 66 {kTestDllName3, kDll3Beacon} | 73 {kTestDllName3, kDll3Beacon} |
| 67 }; | 74 }; |
| 68 | 75 |
| 69 class BlacklistTest : public testing::Test { | 76 class BlacklistTest : public testing::Test { |
| 70 protected: | 77 protected: |
| 71 BlacklistTest() : override_manager_(), num_initially_blocked_(0) { | 78 BlacklistTest() : override_manager_(), num_initially_blocked_(0) { |
| 72 } | 79 } |
| 73 | 80 |
| 74 void CheckBlacklistedDllsNotLoaded() { | 81 void CheckBlacklistedDllsNotLoaded() { |
| 75 base::FilePath current_dir; | 82 base::FilePath current_dir; |
| 76 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); | 83 EXPECT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
| 77 | 84 |
| 78 for (size_t i = 0; i < arraysize(test_data); ++i) { | 85 for (size_t i = 0; i < arraysize(test_data); ++i) { |
| 79 // Ensure that the dll has not been loaded both by inspecting the handle | 86 // Ensure that the dll has not been loaded both by inspecting the handle |
| 80 // returned by LoadLibrary and by looking for an environment variable that | 87 // returned by LoadLibrary and by looking for an environment variable that |
| 81 // is set when the DLL's entry point is called. | 88 // is set when the DLL's entry point is called. |
| 82 base::ScopedNativeLibrary dll_blacklisted( | 89 base::ScopedNativeLibrary dll_blacklisted( |
| 83 current_dir.Append(test_data[i].dll_name)); | 90 current_dir.Append(test_data[i].dll_name)); |
| 84 EXPECT_FALSE(dll_blacklisted.is_valid()); | 91 EXPECT_FALSE(dll_blacklisted.is_valid()); |
| 85 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | 92 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
| 86 dll_blacklisted.Reset(NULL); | 93 dll_blacklisted.Reset(NULL); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 // The number of dlls initially blocked by the blacklist. | 136 // The number of dlls initially blocked by the blacklist. |
| 130 int num_initially_blocked_; | 137 int num_initially_blocked_; |
| 131 | 138 |
| 132 private: | 139 private: |
| 133 // This function puts registry-key redirection paths into | 140 // This function puts registry-key redirection paths into |
| 134 // process-specific environment variables, for our test DLLs to access. | 141 // process-specific environment variables, for our test DLLs to access. |
| 135 // This will only work as long as the IPC is within the same process. | 142 // This will only work as long as the IPC is within the same process. |
| 136 void IpcOverrides() { | 143 void IpcOverrides() { |
| 137 base::string16 temp = nt::GetTestingOverride(nt::HKCU); | 144 base::string16 temp = nt::GetTestingOverride(nt::HKCU); |
| 138 if (!temp.empty()) | 145 if (!temp.empty()) |
| 139 ASSERT_TRUE(::SetEnvironmentVariableW(L"hkcu_override", temp.c_str())); | 146 EXPECT_TRUE(::SetEnvironmentVariableW(L"hkcu_override", temp.c_str())); |
| 140 temp = nt::GetTestingOverride(nt::HKLM); | 147 temp = nt::GetTestingOverride(nt::HKLM); |
| 141 if (!temp.empty()) | 148 if (!temp.empty()) |
| 142 ASSERT_TRUE(::SetEnvironmentVariableW(L"hklm_override", temp.c_str())); | 149 EXPECT_TRUE(::SetEnvironmentVariableW(L"hklm_override", temp.c_str())); |
| 143 } | 150 } |
| 144 | 151 |
| 145 void SetUp() override { | 152 void SetUp() override { |
| 146 base::string16 temp; | 153 base::string16 temp; |
| 147 ASSERT_NO_FATAL_FAILURE( | 154 EXPECT_NO_FATAL_FAILURE( |
| 148 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, &temp)); | 155 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, &temp)); |
| 149 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); | 156 EXPECT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); |
| 150 | 157 |
| 151 // Make the override path available to our test DLL. | 158 // Make the override path available to our test DLL. |
| 152 IpcOverrides(); | 159 IpcOverrides(); |
| 153 | 160 |
| 154 // Load the main test Dll now. | 161 // Load the main test Dll now. |
| 155 // Note: this has to happen after we set up the registry overrides. | 162 // Note: this has to happen after we set up the registry overrides. |
| 156 HMODULE dll = nullptr; | 163 HMODULE dll = nullptr; |
| 157 dll = ::LoadLibraryW(L"blacklist_test_main_dll.dll"); | 164 dll = ::LoadLibraryW(L"blacklist_test_main_dll.dll"); |
| 158 if (!dll) | 165 if (!dll) |
| 159 return; | 166 return; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 | 206 |
| 200 // Find out how many dlls were blocked before the test starts. | 207 // Find out how many dlls were blocked before the test starts. |
| 201 TestDll_SuccessfullyBlocked(NULL, &num_initially_blocked_); | 208 TestDll_SuccessfullyBlocked(NULL, &num_initially_blocked_); |
| 202 } | 209 } |
| 203 | 210 |
| 204 void TearDown() override { | 211 void TearDown() override { |
| 205 TestDll_RemoveDllFromBlacklist(kTestDllName1); | 212 TestDll_RemoveDllFromBlacklist(kTestDllName1); |
| 206 TestDll_RemoveDllFromBlacklist(kTestDllName2); | 213 TestDll_RemoveDllFromBlacklist(kTestDllName2); |
| 207 TestDll_RemoveDllFromBlacklist(kTestDllName3); | 214 TestDll_RemoveDllFromBlacklist(kTestDllName3); |
| 208 | 215 |
| 209 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, base::string16())); | 216 EXPECT_TRUE(nt::SetTestingOverride(nt::HKCU, base::string16())); |
| 210 } | 217 } |
| 211 }; | 218 }; |
| 212 | 219 |
| 213 TEST_F(BlacklistTest, Beacon) { | 220 TEST_F(BlacklistTest, Beacon) { |
| 221 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_blacklist_test_mutex); | |
| 222 EXPECT_TRUE(mutex != NULL); | |
| 223 DWORD timeout = ::IsDebuggerPresent() ? INFINITE : event_timeout; | |
| 224 EXPECT_EQ(WAIT_OBJECT_0, ::WaitForSingleObject(mutex, timeout)); | |
| 225 | |
| 214 // Ensure that the beacon state starts off 'running' for this version. | 226 // Ensure that the beacon state starts off 'running' for this version. |
| 215 LONG result = blacklist_registry_key_->WriteValue( | 227 LONG result = blacklist_registry_key_->WriteValue( |
| 216 blacklist::kBeaconState, blacklist::BLACKLIST_SETUP_RUNNING); | 228 blacklist::kBeaconState, blacklist::BLACKLIST_SETUP_RUNNING); |
| 217 EXPECT_EQ(ERROR_SUCCESS, result); | 229 EXPECT_EQ(ERROR_SUCCESS, result); |
| 218 | 230 |
| 219 result = blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, | 231 result = blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, |
| 220 TEXT(CHROME_VERSION_STRING)); | 232 TEXT(CHROME_VERSION_STRING)); |
| 221 EXPECT_EQ(ERROR_SUCCESS, result); | 233 EXPECT_EQ(ERROR_SUCCESS, result); |
| 222 | 234 |
| 223 // First call should find the beacon and reset it. | 235 // First call should find the beacon and reset it. |
| 224 EXPECT_TRUE(blacklist::ResetBeacon()); | 236 EXPECT_TRUE(blacklist::ResetBeacon()); |
| 225 | 237 |
| 226 // First call should succeed as the beacon is enabled. | 238 // First call should succeed as the beacon is enabled. |
| 227 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); | 239 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); |
| 240 | |
| 241 EXPECT_TRUE(::ReleaseMutex(mutex)); | |
| 242 EXPECT_TRUE(::CloseHandle(mutex)); | |
| 228 } | 243 } |
| 229 | 244 |
| 230 TEST_F(BlacklistTest, AddAndRemoveModules) { | 245 TEST_F(BlacklistTest, AddAndRemoveModules) { |
| 246 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_blacklist_test_mutex); | |
| 247 EXPECT_TRUE(mutex != NULL); | |
| 248 DWORD timeout = ::IsDebuggerPresent() ? INFINITE : event_timeout; | |
| 249 EXPECT_EQ(WAIT_OBJECT_0, ::WaitForSingleObject(mutex, timeout)); | |
| 250 | |
| 231 EXPECT_TRUE(TestDll_AddDllToBlacklist(L"foo.dll")); | 251 EXPECT_TRUE(TestDll_AddDllToBlacklist(L"foo.dll")); |
| 232 // Adding the same item twice should be idempotent. | 252 // Adding the same item twice should be idempotent. |
| 233 EXPECT_TRUE(TestDll_AddDllToBlacklist(L"foo.dll")); | 253 EXPECT_TRUE(TestDll_AddDllToBlacklist(L"foo.dll")); |
| 234 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(L"foo.dll")); | 254 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(L"foo.dll")); |
| 235 EXPECT_FALSE(TestDll_RemoveDllFromBlacklist(L"foo.dll")); | 255 EXPECT_FALSE(TestDll_RemoveDllFromBlacklist(L"foo.dll")); |
| 236 | 256 |
| 237 // Increase the blacklist size by 1 to include the NULL pointer | 257 // Increase the blacklist size by 1 to include the NULL pointer |
| 238 // that marks the end. | 258 // that marks the end. |
| 239 int empty_spaces = | 259 int empty_spaces = |
| 240 blacklist::kTroublesomeDllsMaxCount - (TestDll_BlacklistSize() + 1); | 260 blacklist::kTroublesomeDllsMaxCount - (TestDll_BlacklistSize() + 1); |
| 241 std::vector<base::string16> added_dlls; | 261 std::vector<base::string16> added_dlls; |
| 242 added_dlls.reserve(empty_spaces); | 262 added_dlls.reserve(empty_spaces); |
| 243 for (int i = 0; i < empty_spaces; ++i) { | 263 for (int i = 0; i < empty_spaces; ++i) { |
| 244 added_dlls.push_back(base::IntToString16(i) + L".dll"); | 264 added_dlls.push_back(base::IntToString16(i) + L".dll"); |
| 245 EXPECT_TRUE(TestDll_AddDllToBlacklist(added_dlls[i].c_str())) << i; | 265 EXPECT_TRUE(TestDll_AddDllToBlacklist(added_dlls[i].c_str())) << i; |
| 246 } | 266 } |
| 247 EXPECT_FALSE(TestDll_AddDllToBlacklist(L"overflow.dll")); | 267 EXPECT_FALSE(TestDll_AddDllToBlacklist(L"overflow.dll")); |
| 248 for (int i = 0; i < empty_spaces; ++i) { | 268 for (int i = 0; i < empty_spaces; ++i) { |
| 249 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(added_dlls[i].c_str())) << i; | 269 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(added_dlls[i].c_str())) << i; |
| 250 } | 270 } |
| 251 EXPECT_FALSE(TestDll_RemoveDllFromBlacklist(added_dlls[0].c_str())); | 271 EXPECT_FALSE(TestDll_RemoveDllFromBlacklist(added_dlls[0].c_str())); |
| 252 EXPECT_FALSE( | 272 EXPECT_FALSE( |
| 253 TestDll_RemoveDllFromBlacklist(added_dlls[empty_spaces - 1].c_str())); | 273 TestDll_RemoveDllFromBlacklist(added_dlls[empty_spaces - 1].c_str())); |
| 274 | |
| 275 EXPECT_TRUE(::ReleaseMutex(mutex)); | |
| 276 EXPECT_TRUE(::CloseHandle(mutex)); | |
| 254 } | 277 } |
| 255 | 278 |
| 256 TEST_F(BlacklistTest, SuccessfullyBlocked) { | 279 TEST_F(BlacklistTest, SuccessfullyBlocked) { |
| 280 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_blacklist_test_mutex); | |
| 281 EXPECT_TRUE(mutex != NULL); | |
| 282 DWORD timeout = ::IsDebuggerPresent() ? INFINITE : event_timeout; | |
| 283 EXPECT_EQ(WAIT_OBJECT_0, ::WaitForSingleObject(mutex, timeout)); | |
| 284 | |
| 257 // Add 5 news dlls to blacklist. | 285 // Add 5 news dlls to blacklist. |
| 258 const int kDesiredBlacklistSize = 1; | 286 const int kDesiredBlacklistSize = 1; |
| 259 std::vector<base::string16> dlls_to_block; | 287 std::vector<base::string16> dlls_to_block; |
| 260 for (int i = 0; i < kDesiredBlacklistSize; ++i) { | 288 for (int i = 0; i < kDesiredBlacklistSize; ++i) { |
| 261 dlls_to_block.push_back(base::IntToString16(i) + L".dll"); | 289 dlls_to_block.push_back(base::IntToString16(i) + L".dll"); |
| 262 ASSERT_TRUE(TestDll_AddDllToBlacklist(dlls_to_block[i].c_str())); | 290 EXPECT_TRUE(TestDll_AddDllToBlacklist(dlls_to_block[i].c_str())); |
| 263 } | 291 } |
| 264 | 292 |
| 265 // Block the dlls, one at a time, and ensure SuccesfullyBlocked correctly | 293 // Block the dlls, one at a time, and ensure SuccesfullyBlocked correctly |
| 266 // passes the list of blocked dlls. | 294 // passes the list of blocked dlls. |
| 267 for (int i = 0; i < kDesiredBlacklistSize; ++i) { | 295 for (int i = 0; i < kDesiredBlacklistSize; ++i) { |
| 268 TestDll_BlockedDll(TestDll_GetBlacklistIndex(dlls_to_block[i].c_str())); | 296 TestDll_BlockedDll(TestDll_GetBlacklistIndex(dlls_to_block[i].c_str())); |
| 269 | 297 |
| 270 int size = 0; | 298 int size = 0; |
| 271 TestDll_SuccessfullyBlocked(NULL, &size); | 299 TestDll_SuccessfullyBlocked(NULL, &size); |
| 272 ASSERT_EQ(num_initially_blocked_ + i + 1, size); | 300 EXPECT_EQ(num_initially_blocked_ + i + 1, size); |
| 273 | 301 |
| 274 std::vector<const wchar_t*> blocked_dlls(size); | 302 std::vector<const wchar_t*> blocked_dlls(size); |
| 275 TestDll_SuccessfullyBlocked(&(blocked_dlls[0]), &size); | 303 TestDll_SuccessfullyBlocked(&(blocked_dlls[0]), &size); |
| 276 ASSERT_EQ(num_initially_blocked_ + i + 1, size); | 304 EXPECT_EQ(num_initially_blocked_ + i + 1, size); |
| 277 | 305 |
| 278 for (int j = 0; j <= i; ++j) { | 306 for (int j = 0; j <= i; ++j) { |
| 279 EXPECT_STREQ(blocked_dlls[num_initially_blocked_ + j], | 307 EXPECT_STREQ(blocked_dlls[num_initially_blocked_ + j], |
| 280 dlls_to_block[j].c_str()); | 308 dlls_to_block[j].c_str()); |
| 281 } | 309 } |
| 282 } | 310 } |
| 283 | 311 |
| 284 // Remove the dlls from the blacklist now that we are done. | 312 // Remove the dlls from the blacklist now that we are done. |
| 285 for (const auto& dll : dlls_to_block) { | 313 for (const auto& dll : dlls_to_block) { |
| 286 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(dll.c_str())); | 314 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(dll.c_str())); |
| 287 } | 315 } |
| 316 | |
| 317 EXPECT_TRUE(::ReleaseMutex(mutex)); | |
| 318 EXPECT_TRUE(::CloseHandle(mutex)); | |
| 288 } | 319 } |
| 289 | 320 |
| 290 // Disabled due to flakiness. https://crbug.com/711651. | 321 TEST_F(BlacklistTest, LoadBlacklistedLibrary) { |
| 291 TEST_F(BlacklistTest, DISABLED_LoadBlacklistedLibrary) { | 322 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_blacklist_test_mutex); |
| 323 EXPECT_TRUE(mutex != NULL); | |
| 324 DWORD timeout = ::IsDebuggerPresent() ? INFINITE : event_timeout; | |
| 325 EXPECT_EQ(WAIT_OBJECT_0, ::WaitForSingleObject(mutex, timeout)); | |
| 326 | |
| 292 base::FilePath current_dir; | 327 base::FilePath current_dir; |
| 293 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); | 328 EXPECT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
| 294 | 329 |
| 295 // Ensure that the blacklist is loaded. | 330 // Ensure that the blacklist is loaded. |
| 296 ASSERT_TRUE(TestDll_IsBlacklistInitialized()); | 331 EXPECT_TRUE(TestDll_IsBlacklistInitialized()); |
| 297 | 332 |
| 298 // Test that an un-blacklisted DLL can load correctly. | 333 // Test that an un-blacklisted DLL can load correctly. |
| 299 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); | 334 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); |
| 300 EXPECT_TRUE(dll1.is_valid()); | 335 EXPECT_TRUE(dll1.is_valid()); |
| 301 dll1.Reset(NULL); | 336 dll1.Reset(NULL); |
| 302 | 337 |
| 303 int num_blocked_dlls = 0; | 338 int num_blocked_dlls = 0; |
| 304 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); | 339 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
| 305 EXPECT_EQ(num_initially_blocked_, num_blocked_dlls); | 340 EXPECT_EQ(num_initially_blocked_, num_blocked_dlls); |
| 306 | 341 |
| 307 // Add all DLLs to the blacklist then check they are blocked. | 342 // Add all DLLs to the blacklist then check they are blocked. |
| 308 for (size_t i = 0; i < arraysize(test_data); ++i) { | 343 for (size_t i = 0; i < arraysize(test_data); ++i) { |
| 309 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); | 344 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); |
| 310 } | 345 } |
| 311 CheckBlacklistedDllsNotLoaded(); | 346 CheckBlacklistedDllsNotLoaded(); |
| 347 | |
| 348 EXPECT_TRUE(::ReleaseMutex(mutex)); | |
| 349 EXPECT_TRUE(::CloseHandle(mutex)); | |
| 312 } | 350 } |
| 313 | 351 |
| 314 void TestResetBeacon(std::unique_ptr<base::win::RegKey>& key, | 352 void TestResetBeacon(std::unique_ptr<base::win::RegKey>& key, |
| 315 DWORD input_state, | 353 DWORD input_state, |
| 316 DWORD expected_output_state) { | 354 DWORD expected_output_state) { |
| 317 LONG result = key->WriteValue(blacklist::kBeaconState, input_state); | 355 LONG result = key->WriteValue(blacklist::kBeaconState, input_state); |
| 318 EXPECT_EQ(ERROR_SUCCESS, result); | 356 EXPECT_EQ(ERROR_SUCCESS, result); |
| 319 | 357 |
| 320 EXPECT_TRUE(blacklist::ResetBeacon()); | 358 EXPECT_TRUE(blacklist::ResetBeacon()); |
| 321 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; | 359 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
| 322 result = key->ReadValueDW(blacklist::kBeaconState, &blacklist_state); | 360 result = key->ReadValueDW(blacklist::kBeaconState, &blacklist_state); |
| 323 EXPECT_EQ(ERROR_SUCCESS, result); | 361 EXPECT_EQ(ERROR_SUCCESS, result); |
| 324 EXPECT_EQ(expected_output_state, blacklist_state); | 362 EXPECT_EQ(expected_output_state, blacklist_state); |
| 325 } | 363 } |
| 326 | 364 |
| 327 TEST_F(BlacklistTest, ResetBeacon) { | 365 TEST_F(BlacklistTest, ResetBeacon) { |
| 366 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_blacklist_test_mutex); | |
| 367 EXPECT_TRUE(mutex != NULL); | |
| 368 DWORD timeout = ::IsDebuggerPresent() ? INFINITE : event_timeout; | |
| 369 EXPECT_EQ(WAIT_OBJECT_0, ::WaitForSingleObject(mutex, timeout)); | |
| 370 | |
| 328 // Ensure that ResetBeacon resets properly on successful runs and not on | 371 // Ensure that ResetBeacon resets properly on successful runs and not on |
| 329 // failed or disabled runs. | 372 // failed or disabled runs. |
| 330 TestResetBeacon(blacklist_registry_key_, | 373 TestResetBeacon(blacklist_registry_key_, |
| 331 blacklist::BLACKLIST_SETUP_RUNNING, | 374 blacklist::BLACKLIST_SETUP_RUNNING, |
| 332 blacklist::BLACKLIST_ENABLED); | 375 blacklist::BLACKLIST_ENABLED); |
| 333 | 376 |
| 334 TestResetBeacon(blacklist_registry_key_, | 377 TestResetBeacon(blacklist_registry_key_, |
| 335 blacklist::BLACKLIST_SETUP_FAILED, | 378 blacklist::BLACKLIST_SETUP_FAILED, |
| 336 blacklist::BLACKLIST_SETUP_FAILED); | 379 blacklist::BLACKLIST_SETUP_FAILED); |
| 337 | 380 |
| 338 TestResetBeacon(blacklist_registry_key_, | 381 TestResetBeacon(blacklist_registry_key_, |
| 339 blacklist::BLACKLIST_DISABLED, | 382 blacklist::BLACKLIST_DISABLED, |
| 340 blacklist::BLACKLIST_DISABLED); | 383 blacklist::BLACKLIST_DISABLED); |
| 384 | |
| 385 EXPECT_TRUE(::ReleaseMutex(mutex)); | |
| 386 EXPECT_TRUE(::CloseHandle(mutex)); | |
| 341 } | 387 } |
| 342 | 388 |
| 343 TEST_F(BlacklistTest, SetupFailed) { | 389 TEST_F(BlacklistTest, SetupFailed) { |
| 390 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_blacklist_test_mutex); | |
| 391 EXPECT_TRUE(mutex != NULL); | |
| 392 DWORD timeout = ::IsDebuggerPresent() ? INFINITE : event_timeout; | |
| 393 EXPECT_EQ(WAIT_OBJECT_0, ::WaitForSingleObject(mutex, timeout)); | |
| 394 | |
| 344 // Ensure that when the number of failed tries reaches the maximum allowed, | 395 // Ensure that when the number of failed tries reaches the maximum allowed, |
| 345 // the blacklist state is set to failed. | 396 // the blacklist state is set to failed. |
| 346 LONG result = blacklist_registry_key_->WriteValue( | 397 LONG result = blacklist_registry_key_->WriteValue( |
| 347 blacklist::kBeaconState, blacklist::BLACKLIST_SETUP_RUNNING); | 398 blacklist::kBeaconState, blacklist::BLACKLIST_SETUP_RUNNING); |
| 348 EXPECT_EQ(ERROR_SUCCESS, result); | 399 EXPECT_EQ(ERROR_SUCCESS, result); |
| 349 | 400 |
| 350 // Set the attempt count so that on the next failure the blacklist is | 401 // Set the attempt count so that on the next failure the blacklist is |
| 351 // disabled. | 402 // disabled. |
| 352 result = blacklist_registry_key_->WriteValue( | 403 result = blacklist_registry_key_->WriteValue( |
| 353 blacklist::kBeaconAttemptCount, blacklist::kBeaconMaxAttempts - 1); | 404 blacklist::kBeaconAttemptCount, blacklist::kBeaconMaxAttempts - 1); |
| 354 EXPECT_EQ(ERROR_SUCCESS, result); | 405 EXPECT_EQ(ERROR_SUCCESS, result); |
| 355 | 406 |
| 356 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); | 407 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); |
| 357 | 408 |
| 358 DWORD attempt_count = 0; | 409 DWORD attempt_count = 0; |
| 359 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, | 410 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, |
| 360 &attempt_count); | 411 &attempt_count); |
| 361 EXPECT_EQ(attempt_count, blacklist::kBeaconMaxAttempts); | 412 EXPECT_EQ(attempt_count, blacklist::kBeaconMaxAttempts); |
| 362 | 413 |
| 363 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; | 414 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
| 364 result = blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState, | 415 result = blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState, |
| 365 &blacklist_state); | 416 &blacklist_state); |
| 366 EXPECT_EQ(ERROR_SUCCESS, result); | 417 EXPECT_EQ(ERROR_SUCCESS, result); |
| 367 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_FAILED); | 418 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_FAILED); |
| 419 | |
| 420 EXPECT_TRUE(::ReleaseMutex(mutex)); | |
| 421 EXPECT_TRUE(::CloseHandle(mutex)); | |
| 368 } | 422 } |
| 369 | 423 |
| 370 TEST_F(BlacklistTest, SetupSucceeded) { | 424 TEST_F(BlacklistTest, SetupSucceeded) { |
| 425 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_blacklist_test_mutex); | |
| 426 EXPECT_TRUE(mutex != NULL); | |
| 427 DWORD timeout = ::IsDebuggerPresent() ? INFINITE : event_timeout; | |
| 428 EXPECT_EQ(WAIT_OBJECT_0, ::WaitForSingleObject(mutex, timeout)); | |
| 429 | |
| 371 // Starting with the enabled beacon should result in the setup running state | 430 // Starting with the enabled beacon should result in the setup running state |
| 372 // and the attempt counter reset to zero. | 431 // and the attempt counter reset to zero. |
| 373 LONG result = blacklist_registry_key_->WriteValue( | 432 LONG result = blacklist_registry_key_->WriteValue( |
| 374 blacklist::kBeaconState, blacklist::BLACKLIST_ENABLED); | 433 blacklist::kBeaconState, blacklist::BLACKLIST_ENABLED); |
| 375 EXPECT_EQ(ERROR_SUCCESS, result); | 434 EXPECT_EQ(ERROR_SUCCESS, result); |
| 376 result = blacklist_registry_key_->WriteValue(blacklist::kBeaconAttemptCount, | 435 result = blacklist_registry_key_->WriteValue(blacklist::kBeaconAttemptCount, |
| 377 blacklist::kBeaconMaxAttempts); | 436 blacklist::kBeaconMaxAttempts); |
| 378 EXPECT_EQ(ERROR_SUCCESS, result); | 437 EXPECT_EQ(ERROR_SUCCESS, result); |
| 379 | 438 |
| 380 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); | 439 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); |
| 381 | 440 |
| 382 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; | 441 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
| 383 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState, | 442 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState, |
| 384 &blacklist_state); | 443 &blacklist_state); |
| 385 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_RUNNING); | 444 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_RUNNING); |
| 386 | 445 |
| 387 DWORD attempt_count = blacklist::kBeaconMaxAttempts; | 446 DWORD attempt_count = blacklist::kBeaconMaxAttempts; |
| 388 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, | 447 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, |
| 389 &attempt_count); | 448 &attempt_count); |
| 390 EXPECT_EQ(static_cast<DWORD>(0), attempt_count); | 449 EXPECT_EQ(static_cast<DWORD>(0), attempt_count); |
| 450 | |
| 451 EXPECT_TRUE(::ReleaseMutex(mutex)); | |
| 452 EXPECT_TRUE(::CloseHandle(mutex)); | |
| 391 } | 453 } |
| 392 | 454 |
| 393 } // namespace | 455 } // namespace |
| OLD | NEW |