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 "base/environment.h" | 5 #include "base/environment.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/scoped_native_library.h" | 10 #include "base/scoped_native_library.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 // executable itself. | 34 // executable itself. |
35 __declspec(dllimport) bool TestDll_AddDllToBlacklist(const wchar_t* dll_name); | 35 __declspec(dllimport) bool TestDll_AddDllToBlacklist(const wchar_t* dll_name); |
36 __declspec(dllimport) bool TestDll_IsBlacklistInitialized(); | 36 __declspec(dllimport) bool TestDll_IsBlacklistInitialized(); |
37 __declspec(dllimport) bool TestDll_RemoveDllFromBlacklist( | 37 __declspec(dllimport) bool TestDll_RemoveDllFromBlacklist( |
38 const wchar_t* dll_name); | 38 const wchar_t* dll_name); |
39 __declspec(dllimport) bool TestDll_SuccessfullyBlocked( | 39 __declspec(dllimport) bool TestDll_SuccessfullyBlocked( |
40 const wchar_t** blocked_dlls, | 40 const wchar_t** blocked_dlls, |
41 int* size); | 41 int* size); |
42 } | 42 } |
43 | 43 |
| 44 namespace { |
| 45 |
44 class BlacklistTest : public testing::Test { | 46 class BlacklistTest : public testing::Test { |
| 47 protected: |
| 48 BlacklistTest() : override_manager_() { |
| 49 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); |
| 50 } |
| 51 |
| 52 base::win::RegKey* blacklist_registry_key_; |
| 53 registry_util::RegistryOverrideManager override_manager_; |
| 54 |
| 55 private: |
45 virtual void SetUp() { | 56 virtual void SetUp() { |
46 // Force an import from blacklist_test_main_dll. | 57 // Force an import from blacklist_test_main_dll. |
47 InitBlacklistTestDll(); | 58 InitBlacklistTestDll(); |
| 59 |
| 60 blacklist_registry_key_ = |
| 61 new base::win::RegKey(HKEY_CURRENT_USER, |
| 62 blacklist::kRegistryBeaconPath, |
| 63 KEY_QUERY_VALUE | KEY_SET_VALUE); |
48 } | 64 } |
49 | 65 |
50 virtual void TearDown() { | 66 virtual void TearDown() { |
51 TestDll_RemoveDllFromBlacklist(kTestDllName1); | 67 TestDll_RemoveDllFromBlacklist(kTestDllName1); |
52 TestDll_RemoveDllFromBlacklist(kTestDllName2); | 68 TestDll_RemoveDllFromBlacklist(kTestDllName2); |
53 TestDll_RemoveDllFromBlacklist(kTestDllName3); | 69 TestDll_RemoveDllFromBlacklist(kTestDllName3); |
| 70 delete blacklist_registry_key_; |
54 } | 71 } |
55 }; | 72 }; |
56 | 73 |
57 TEST_F(BlacklistTest, Beacon) { | 74 TEST_F(BlacklistTest, Beacon) { |
58 registry_util::RegistryOverrideManager override_manager; | 75 // Ensure that the beacon state starts off 'running' for this version. |
59 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); | 76 LONG result = blacklist_registry_key_->WriteValue( |
60 | 77 blacklist::kBeaconState, blacklist::BLACKLIST_SETUP_RUNNING); |
61 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, | |
62 blacklist::kRegistryBeaconPath, | |
63 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
64 | |
65 // Ensure that the beacon state starts off enabled for this version. | |
66 LONG result = blacklist_registry_key.WriteValue(blacklist::kBeaconState, | |
67 blacklist::BLACKLIST_ENABLED); | |
68 EXPECT_EQ(ERROR_SUCCESS, result); | 78 EXPECT_EQ(ERROR_SUCCESS, result); |
69 | 79 |
70 result = blacklist_registry_key.WriteValue(blacklist::kBeaconVersion, | 80 result = blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, |
71 TEXT(CHROME_VERSION_STRING)); | 81 TEXT(CHROME_VERSION_STRING)); |
72 EXPECT_EQ(ERROR_SUCCESS, result); | 82 EXPECT_EQ(ERROR_SUCCESS, result); |
73 | 83 |
74 // First call should find the beacon and reset it. | 84 // First call should find the beacon and reset it. |
75 EXPECT_TRUE(blacklist::ResetBeacon()); | 85 EXPECT_TRUE(blacklist::ResetBeacon()); |
76 | 86 |
77 // First call should succeed as the beacon is enabled. | 87 // First call should succeed as the beacon is enabled. |
78 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); | 88 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); |
79 | |
80 // Second call should fail indicating the beacon wasn't set as enabled. | |
81 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); | |
82 | |
83 // Resetting the beacon should work when setup beacon is present. | |
84 EXPECT_TRUE(blacklist::ResetBeacon()); | |
85 } | 89 } |
86 | 90 |
87 TEST_F(BlacklistTest, AddAndRemoveModules) { | 91 TEST_F(BlacklistTest, AddAndRemoveModules) { |
88 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); | 92 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
89 // Adding the same item twice should be idempotent. | 93 // Adding the same item twice should be idempotent. |
90 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); | 94 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
91 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); | 95 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
92 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); | 96 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
93 | 97 |
94 // Increase the blacklist size by 1 to include the NULL pointer | 98 // Increase the blacklist size by 1 to include the NULL pointer |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 dll_blacklisted_different_case.Reset(NULL); | 204 dll_blacklisted_different_case.Reset(NULL); |
201 | 205 |
202 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); | 206 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); |
203 | 207 |
204 // The blocked dll was removed, so we shouldn't get anything returned | 208 // The blocked dll was removed, so we shouldn't get anything returned |
205 // here. | 209 // here. |
206 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); | 210 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
207 EXPECT_EQ(0, num_blocked_dlls); | 211 EXPECT_EQ(0, num_blocked_dlls); |
208 } | 212 } |
209 } | 213 } |
| 214 |
| 215 void TestResetBeacon(base::win::RegKey* key, |
| 216 DWORD input_state, |
| 217 DWORD expected_output_state) { |
| 218 LONG result = key->WriteValue(blacklist::kBeaconState, input_state); |
| 219 EXPECT_EQ(ERROR_SUCCESS, result); |
| 220 |
| 221 EXPECT_TRUE(blacklist::ResetBeacon()); |
| 222 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
| 223 result = key->ReadValueDW(blacklist::kBeaconState, &blacklist_state); |
| 224 EXPECT_EQ(ERROR_SUCCESS, result); |
| 225 EXPECT_EQ(expected_output_state, blacklist_state); |
| 226 } |
| 227 |
| 228 TEST_F(BlacklistTest, ResetBeacon) { |
| 229 // Ensure that ResetBeacon resets properly on successful runs and not on |
| 230 // failed or disabled runs. |
| 231 TestResetBeacon(blacklist_registry_key_, |
| 232 blacklist::BLACKLIST_SETUP_RUNNING, |
| 233 blacklist::BLACKLIST_ENABLED); |
| 234 |
| 235 TestResetBeacon(blacklist_registry_key_, |
| 236 blacklist::BLACKLIST_SETUP_FAILED, |
| 237 blacklist::BLACKLIST_SETUP_FAILED); |
| 238 |
| 239 TestResetBeacon(blacklist_registry_key_, |
| 240 blacklist::BLACKLIST_DISABLED, |
| 241 blacklist::BLACKLIST_DISABLED); |
| 242 } |
| 243 |
| 244 TEST_F(BlacklistTest, SetupFailed) { |
| 245 // Ensure that when the number of failed tries reaches the maximum allowed, |
| 246 // the blacklist state is set to failed. |
| 247 LONG result = blacklist_registry_key_->WriteValue( |
| 248 blacklist::kBeaconState, blacklist::BLACKLIST_SETUP_RUNNING); |
| 249 EXPECT_EQ(ERROR_SUCCESS, result); |
| 250 |
| 251 // Set the attempt count so that on the next failure the blacklist is |
| 252 // disabled. |
| 253 result = blacklist_registry_key_->WriteValue( |
| 254 blacklist::kBeaconAttemptCount, blacklist::kBeaconMaxAttempts - 1); |
| 255 EXPECT_EQ(ERROR_SUCCESS, result); |
| 256 |
| 257 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); |
| 258 |
| 259 DWORD attempt_count = 0; |
| 260 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, |
| 261 &attempt_count); |
| 262 EXPECT_EQ(attempt_count, blacklist::kBeaconMaxAttempts); |
| 263 |
| 264 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
| 265 result = blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState, |
| 266 &blacklist_state); |
| 267 EXPECT_EQ(ERROR_SUCCESS, result); |
| 268 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_FAILED); |
| 269 } |
| 270 |
| 271 TEST_F(BlacklistTest, SetupSucceeded) { |
| 272 // Starting with the enabled beacon should result in the setup running state |
| 273 // and the attempt counter reset to zero. |
| 274 LONG result = blacklist_registry_key_->WriteValue( |
| 275 blacklist::kBeaconState, blacklist::BLACKLIST_ENABLED); |
| 276 EXPECT_EQ(ERROR_SUCCESS, result); |
| 277 result = blacklist_registry_key_->WriteValue(blacklist::kBeaconAttemptCount, |
| 278 blacklist::kBeaconMaxAttempts); |
| 279 EXPECT_EQ(ERROR_SUCCESS, result); |
| 280 |
| 281 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); |
| 282 |
| 283 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
| 284 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState, |
| 285 &blacklist_state); |
| 286 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_RUNNING); |
| 287 |
| 288 DWORD attempt_count = blacklist::kBeaconMaxAttempts; |
| 289 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, |
| 290 &attempt_count); |
| 291 EXPECT_EQ(static_cast<DWORD>(0), attempt_count); |
| 292 } |
| 293 |
| 294 } // namespace |
OLD | NEW |