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 { |
45 virtual void SetUp() { | 47 virtual void SetUp() { |
46 // Force an import from blacklist_test_main_dll. | 48 // Force an import from blacklist_test_main_dll. |
47 InitBlacklistTestDll(); | 49 InitBlacklistTestDll(); |
48 } | 50 } |
49 | 51 |
50 virtual void TearDown() { | 52 virtual void TearDown() { |
51 TestDll_RemoveDllFromBlacklist(kTestDllName1); | 53 TestDll_RemoveDllFromBlacklist(kTestDllName1); |
52 TestDll_RemoveDllFromBlacklist(kTestDllName2); | 54 TestDll_RemoveDllFromBlacklist(kTestDllName2); |
53 TestDll_RemoveDllFromBlacklist(kTestDllName3); | 55 TestDll_RemoveDllFromBlacklist(kTestDllName3); |
54 } | 56 } |
55 }; | 57 }; |
56 | 58 |
59 void ResetRegistry(base::win::RegKey* blacklist_registry_key) { | |
60 blacklist_registry_key->WriteValue(blacklist::kBeaconState, | |
61 blacklist::BLACKLIST_ENABLED); | |
62 blacklist_registry_key->WriteValue(blacklist::kBeaconAttemptCount, | |
63 static_cast<DWORD>(0)); | |
64 } | |
65 | |
57 TEST_F(BlacklistTest, Beacon) { | 66 TEST_F(BlacklistTest, Beacon) { |
58 registry_util::RegistryOverrideManager override_manager; | 67 registry_util::RegistryOverrideManager override_manager; |
59 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); | 68 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); |
60 | 69 |
61 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, | 70 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, |
62 blacklist::kRegistryBeaconPath, | 71 blacklist::kRegistryBeaconPath, |
63 KEY_QUERY_VALUE | KEY_SET_VALUE); | 72 KEY_QUERY_VALUE | KEY_SET_VALUE); |
64 | 73 |
65 // Ensure that the beacon state starts off enabled for this version. | 74 // Ensure that the beacon state starts off enabled for this version. |
66 LONG result = blacklist_registry_key.WriteValue(blacklist::kBeaconState, | 75 LONG result = blacklist_registry_key.WriteValue(blacklist::kBeaconState, |
67 blacklist::BLACKLIST_ENABLED); | 76 blacklist::BLACKLIST_ENABLED); |
68 EXPECT_EQ(ERROR_SUCCESS, result); | 77 EXPECT_EQ(ERROR_SUCCESS, result); |
69 | 78 |
70 result = blacklist_registry_key.WriteValue(blacklist::kBeaconVersion, | 79 result = blacklist_registry_key.WriteValue(blacklist::kBeaconVersion, |
71 TEXT(CHROME_VERSION_STRING)); | 80 TEXT(CHROME_VERSION_STRING)); |
72 EXPECT_EQ(ERROR_SUCCESS, result); | 81 EXPECT_EQ(ERROR_SUCCESS, result); |
73 | 82 |
74 // First call should find the beacon and reset it. | 83 // First call should find the beacon and reset it. |
75 EXPECT_TRUE(blacklist::ResetBeacon()); | 84 EXPECT_TRUE(blacklist::ResetBeacon()); |
76 | 85 |
77 // First call should succeed as the beacon is enabled. | 86 // First call should succeed as the beacon is enabled. |
78 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); | 87 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 } | 88 } |
86 | 89 |
87 TEST_F(BlacklistTest, AddAndRemoveModules) { | 90 TEST_F(BlacklistTest, AddAndRemoveModules) { |
88 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); | 91 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
89 // Adding the same item twice should be idempotent. | 92 // Adding the same item twice should be idempotent. |
90 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); | 93 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
91 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); | 94 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
92 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); | 95 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
93 | 96 |
94 // Increase the blacklist size by 1 to include the NULL pointer | 97 // 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); | 203 dll_blacklisted_different_case.Reset(NULL); |
201 | 204 |
202 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); | 205 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); |
203 | 206 |
204 // The blocked dll was removed, so we shouldn't get anything returned | 207 // The blocked dll was removed, so we shouldn't get anything returned |
205 // here. | 208 // here. |
206 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); | 209 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
207 EXPECT_EQ(0, num_blocked_dlls); | 210 EXPECT_EQ(0, num_blocked_dlls); |
208 } | 211 } |
209 } | 212 } |
213 | |
214 void TestResetBeacon(base::win::RegKey* key, | |
215 DWORD input_state, | |
216 DWORD expected_output_state) { | |
217 LONG result = key->WriteValue(blacklist::kBeaconState, input_state); | |
218 EXPECT_EQ(ERROR_SUCCESS, result); | |
219 EXPECT_TRUE(blacklist::ResetBeacon()); | |
csharp
2014/06/10 14:07:02
Nit: Please add a blank line above
krstnmnlsn
2014/06/10 22:03:27
Done.
| |
220 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; | |
221 result = key->ReadValueDW(blacklist::kBeaconState, &blacklist_state); | |
222 EXPECT_EQ(ERROR_SUCCESS, result); | |
223 EXPECT_EQ(expected_output_state, blacklist_state); | |
224 } | |
225 | |
226 TEST_F(BlacklistTest, ResetBeacon) { | |
227 // Ensure that ResetBeacon resets properly on successful runs and not on | |
228 // failed or disabled runs. | |
229 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, | |
csharp
2014/06/10 14:07:02
A lot of tests seem to be declaring this, is it wo
krstnmnlsn
2014/06/10 22:03:27
Done.
| |
230 blacklist::kRegistryBeaconPath, | |
231 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
232 | |
233 TestResetBeacon(&blacklist_registry_key, | |
234 blacklist::BLACKLIST_SETUP_RUNNING, | |
235 blacklist::BLACKLIST_ENABLED); | |
236 | |
237 TestResetBeacon(&blacklist_registry_key, | |
238 blacklist::BLACKLIST_SETUP_FAILED, | |
239 blacklist::BLACKLIST_SETUP_FAILED); | |
240 | |
241 TestResetBeacon(&blacklist_registry_key, | |
242 blacklist::BLACKLIST_DISABLED, | |
243 blacklist::BLACKLIST_DISABLED); | |
244 | |
245 ResetRegistry(&blacklist_registry_key); | |
246 } | |
247 | |
248 TEST_F(BlacklistTest, SetupFailed) { | |
249 // Ensure that when the number of failed tries reaches the maximum allowed the | |
csharp
2014/06/10 14:07:02
nit: allowed -> allowed,
krstnmnlsn
2014/06/10 22:03:27
Done.
| |
250 // blacklist state is set to failed. | |
251 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, | |
252 blacklist::kRegistryBeaconPath, | |
253 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
254 | |
255 LONG result = blacklist_registry_key.WriteValue( | |
256 blacklist::kBeaconState, blacklist::BLACKLIST_SETUP_RUNNING); | |
257 EXPECT_EQ(ERROR_SUCCESS, result); | |
258 | |
259 // Set the attempt count so that on the next failure the blacklist is | |
260 // disabled. | |
261 result = blacklist_registry_key.WriteValue(blacklist::kBeaconAttemptCount, | |
262 blacklist::kBeaconMaxAttempts - 1); | |
263 EXPECT_EQ(ERROR_SUCCESS, result); | |
264 | |
265 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); | |
266 | |
267 DWORD attempt_count = 0; | |
268 blacklist_registry_key.ReadValueDW(blacklist::kBeaconAttemptCount, | |
269 &attempt_count); | |
270 EXPECT_EQ(attempt_count, blacklist::kBeaconMaxAttempts); | |
271 | |
272 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; | |
273 result = blacklist_registry_key.ReadValueDW(blacklist::kBeaconState, | |
274 &blacklist_state); | |
275 EXPECT_EQ(ERROR_SUCCESS, result); | |
276 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_FAILED); | |
277 | |
278 ResetRegistry(&blacklist_registry_key); | |
279 } | |
280 | |
281 TEST_F(BlacklistTest, SetupSucceeded) { | |
282 // Starting with the enabled beacon should result in the setup running state | |
283 // and the attempt counter reset to zero. | |
284 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, | |
285 blacklist::kRegistryBeaconPath, | |
286 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
287 | |
288 LONG result = blacklist_registry_key.WriteValue(blacklist::kBeaconState, | |
289 blacklist::BLACKLIST_ENABLED); | |
290 EXPECT_EQ(ERROR_SUCCESS, result); | |
291 result = blacklist_registry_key.WriteValue(blacklist::kBeaconAttemptCount, | |
292 blacklist::kBeaconMaxAttempts); | |
293 EXPECT_EQ(ERROR_SUCCESS, result); | |
294 | |
295 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); | |
296 | |
297 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; | |
298 blacklist_registry_key.ReadValueDW(blacklist::kBeaconState, &blacklist_state); | |
299 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_RUNNING); | |
300 | |
301 DWORD attempt_count = blacklist::kBeaconMaxAttempts; | |
302 blacklist_registry_key.ReadValueDW(blacklist::kBeaconAttemptCount, | |
303 &attempt_count); | |
304 EXPECT_EQ(static_cast<DWORD>(0), attempt_count); | |
305 | |
306 ResetRegistry(&blacklist_registry_key); | |
307 } | |
308 | |
309 } // namespace | |
csharp
2014/06/10 14:07:02
Please add a newline to the end of the file (lint
krstnmnlsn
2014/06/10 22:03:27
Checked my previous commit (which also complained
| |
OLD | NEW |