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 24 matching lines...) Expand all Loading... |
35 __declspec(dllimport) bool TestDll_AddDllsFromRegistryToBlacklist(); | 35 __declspec(dllimport) bool TestDll_AddDllsFromRegistryToBlacklist(); |
36 __declspec(dllimport) bool TestDll_AddDllToBlacklist(const wchar_t* dll_name); | 36 __declspec(dllimport) bool TestDll_AddDllToBlacklist(const wchar_t* dll_name); |
37 __declspec(dllimport) bool TestDll_IsBlacklistInitialized(); | 37 __declspec(dllimport) bool TestDll_IsBlacklistInitialized(); |
38 __declspec(dllimport) bool TestDll_RemoveDllFromBlacklist( | 38 __declspec(dllimport) bool TestDll_RemoveDllFromBlacklist( |
39 const wchar_t* dll_name); | 39 const wchar_t* dll_name); |
40 __declspec(dllimport) bool TestDll_SuccessfullyBlocked( | 40 __declspec(dllimport) bool TestDll_SuccessfullyBlocked( |
41 const wchar_t** blocked_dlls, | 41 const wchar_t** blocked_dlls, |
42 int* size); | 42 int* size); |
43 } | 43 } |
44 | 44 |
| 45 namespace { |
| 46 |
45 class BlacklistTest : public testing::Test { | 47 class BlacklistTest : public testing::Test { |
| 48 protected: |
| 49 BlacklistTest() : override_manager_() { |
| 50 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); |
| 51 } |
| 52 |
| 53 scoped_ptr<base::win::RegKey> blacklist_registry_key_; |
| 54 registry_util::RegistryOverrideManager override_manager_; |
| 55 |
| 56 private: |
46 virtual void SetUp() { | 57 virtual void SetUp() { |
47 // Force an import from blacklist_test_main_dll. | 58 // Force an import from blacklist_test_main_dll. |
48 InitBlacklistTestDll(); | 59 InitBlacklistTestDll(); |
| 60 blacklist_registry_key_.reset( |
| 61 new base::win::RegKey(HKEY_CURRENT_USER, |
| 62 blacklist::kRegistryBeaconPath, |
| 63 KEY_QUERY_VALUE | KEY_SET_VALUE)); |
49 } | 64 } |
50 | 65 |
51 virtual void TearDown() { | 66 virtual void TearDown() { |
52 TestDll_RemoveDllFromBlacklist(kTestDllName1); | 67 TestDll_RemoveDllFromBlacklist(kTestDllName1); |
53 TestDll_RemoveDllFromBlacklist(kTestDllName2); | 68 TestDll_RemoveDllFromBlacklist(kTestDllName2); |
54 TestDll_RemoveDllFromBlacklist(kTestDllName3); | 69 TestDll_RemoveDllFromBlacklist(kTestDllName3); |
55 } | 70 } |
56 }; | 71 }; |
57 | 72 |
58 namespace { | |
59 | |
60 struct TestData { | 73 struct TestData { |
61 const wchar_t* dll_name; | 74 const wchar_t* dll_name; |
62 const wchar_t* dll_beacon; | 75 const wchar_t* dll_beacon; |
63 } test_data[] = {{kTestDllName2, kDll2Beacon}, {kTestDllName3, kDll3Beacon}}; | 76 } test_data[] = {{kTestDllName2, kDll2Beacon}, {kTestDllName3, kDll3Beacon}}; |
64 | 77 |
65 TEST_F(BlacklistTest, Beacon) { | 78 TEST_F(BlacklistTest, Beacon) { |
66 registry_util::RegistryOverrideManager override_manager; | 79 // Ensure that the beacon state starts off 'running' for this version. |
67 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); | 80 LONG result = blacklist_registry_key_->WriteValue( |
68 | 81 blacklist::kBeaconState, blacklist::BLACKLIST_SETUP_RUNNING); |
69 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, | |
70 blacklist::kRegistryBeaconPath, | |
71 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
72 | |
73 // Ensure that the beacon state starts off enabled for this version. | |
74 LONG result = blacklist_registry_key.WriteValue(blacklist::kBeaconState, | |
75 blacklist::BLACKLIST_ENABLED); | |
76 EXPECT_EQ(ERROR_SUCCESS, result); | 82 EXPECT_EQ(ERROR_SUCCESS, result); |
77 | 83 |
78 result = blacklist_registry_key.WriteValue(blacklist::kBeaconVersion, | 84 result = blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, |
79 TEXT(CHROME_VERSION_STRING)); | 85 TEXT(CHROME_VERSION_STRING)); |
80 EXPECT_EQ(ERROR_SUCCESS, result); | 86 EXPECT_EQ(ERROR_SUCCESS, result); |
81 | 87 |
82 // First call should find the beacon and reset it. | 88 // First call should find the beacon and reset it. |
83 EXPECT_TRUE(blacklist::ResetBeacon()); | 89 EXPECT_TRUE(blacklist::ResetBeacon()); |
84 | 90 |
85 // First call should succeed as the beacon is enabled. | 91 // First call should succeed as the beacon is enabled. |
86 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); | 92 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); |
87 | |
88 // Second call should fail indicating the beacon wasn't set as enabled. | |
89 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); | |
90 | |
91 // Resetting the beacon should work when setup beacon is present. | |
92 EXPECT_TRUE(blacklist::ResetBeacon()); | |
93 } | 93 } |
94 | 94 |
95 TEST_F(BlacklistTest, AddAndRemoveModules) { | 95 TEST_F(BlacklistTest, AddAndRemoveModules) { |
96 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); | 96 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
97 // Adding the same item twice should be idempotent. | 97 // Adding the same item twice should be idempotent. |
98 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); | 98 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
99 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); | 99 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
100 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); | 100 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
101 | 101 |
102 // Increase the blacklist size by 1 to include the NULL pointer | 102 // Increase the blacklist size by 1 to include the NULL pointer |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 KEY_QUERY_VALUE | KEY_SET_VALUE); | 237 KEY_QUERY_VALUE | KEY_SET_VALUE); |
238 for (int i = 0; i < arraysize(test_data); ++i) { | 238 for (int i = 0; i < arraysize(test_data); ++i) { |
239 finch_blacklist_registry_key.WriteValue(test_data[i].dll_name, | 239 finch_blacklist_registry_key.WriteValue(test_data[i].dll_name, |
240 test_data[i].dll_name); | 240 test_data[i].dll_name); |
241 } | 241 } |
242 | 242 |
243 EXPECT_TRUE(TestDll_AddDllsFromRegistryToBlacklist()); | 243 EXPECT_TRUE(TestDll_AddDllsFromRegistryToBlacklist()); |
244 CheckBlacklistedDllsNotLoaded(); | 244 CheckBlacklistedDllsNotLoaded(); |
245 } | 245 } |
246 | 246 |
| 247 void TestResetBeacon(scoped_ptr<base::win::RegKey>& key, |
| 248 DWORD input_state, |
| 249 DWORD expected_output_state) { |
| 250 LONG result = key->WriteValue(blacklist::kBeaconState, input_state); |
| 251 EXPECT_EQ(ERROR_SUCCESS, result); |
| 252 |
| 253 EXPECT_TRUE(blacklist::ResetBeacon()); |
| 254 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
| 255 result = key->ReadValueDW(blacklist::kBeaconState, &blacklist_state); |
| 256 EXPECT_EQ(ERROR_SUCCESS, result); |
| 257 EXPECT_EQ(expected_output_state, blacklist_state); |
| 258 } |
| 259 |
| 260 TEST_F(BlacklistTest, ResetBeacon) { |
| 261 // Ensure that ResetBeacon resets properly on successful runs and not on |
| 262 // failed or disabled runs. |
| 263 TestResetBeacon(blacklist_registry_key_, |
| 264 blacklist::BLACKLIST_SETUP_RUNNING, |
| 265 blacklist::BLACKLIST_ENABLED); |
| 266 |
| 267 TestResetBeacon(blacklist_registry_key_, |
| 268 blacklist::BLACKLIST_SETUP_FAILED, |
| 269 blacklist::BLACKLIST_SETUP_FAILED); |
| 270 |
| 271 TestResetBeacon(blacklist_registry_key_, |
| 272 blacklist::BLACKLIST_DISABLED, |
| 273 blacklist::BLACKLIST_DISABLED); |
| 274 } |
| 275 |
| 276 TEST_F(BlacklistTest, SetupFailed) { |
| 277 // Ensure that when the number of failed tries reaches the maximum allowed, |
| 278 // the blacklist state is set to failed. |
| 279 LONG result = blacklist_registry_key_->WriteValue( |
| 280 blacklist::kBeaconState, blacklist::BLACKLIST_SETUP_RUNNING); |
| 281 EXPECT_EQ(ERROR_SUCCESS, result); |
| 282 |
| 283 // Set the attempt count so that on the next failure the blacklist is |
| 284 // disabled. |
| 285 result = blacklist_registry_key_->WriteValue( |
| 286 blacklist::kBeaconAttemptCount, blacklist::kBeaconMaxAttempts - 1); |
| 287 EXPECT_EQ(ERROR_SUCCESS, result); |
| 288 |
| 289 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); |
| 290 |
| 291 DWORD attempt_count = 0; |
| 292 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, |
| 293 &attempt_count); |
| 294 EXPECT_EQ(attempt_count, blacklist::kBeaconMaxAttempts); |
| 295 |
| 296 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
| 297 result = blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState, |
| 298 &blacklist_state); |
| 299 EXPECT_EQ(ERROR_SUCCESS, result); |
| 300 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_FAILED); |
| 301 } |
| 302 |
| 303 TEST_F(BlacklistTest, SetupSucceeded) { |
| 304 // Starting with the enabled beacon should result in the setup running state |
| 305 // and the attempt counter reset to zero. |
| 306 LONG result = blacklist_registry_key_->WriteValue( |
| 307 blacklist::kBeaconState, blacklist::BLACKLIST_ENABLED); |
| 308 EXPECT_EQ(ERROR_SUCCESS, result); |
| 309 result = blacklist_registry_key_->WriteValue(blacklist::kBeaconAttemptCount, |
| 310 blacklist::kBeaconMaxAttempts); |
| 311 EXPECT_EQ(ERROR_SUCCESS, result); |
| 312 |
| 313 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); |
| 314 |
| 315 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
| 316 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState, |
| 317 &blacklist_state); |
| 318 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_RUNNING); |
| 319 |
| 320 DWORD attempt_count = blacklist::kBeaconMaxAttempts; |
| 321 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, |
| 322 &attempt_count); |
| 323 EXPECT_EQ(static_cast<DWORD>(0), attempt_count); |
| 324 } |
| 325 |
247 } // namespace | 326 } // namespace |
OLD | NEW |