Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: chrome_elf/blacklist/test/blacklist_test.cc

Issue 311893005: Can now adjust the number of retries before the blacklist is disabled. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: UMA_HISTOGRAM_COUNTS->UMA_HISTOGRAM_COUNTS_100 Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 base::win::RegKey* blacklist_registry_key_;
robertshield 2014/06/16 13:56:54 While this case is quite simple, still prefer usin
krstnmnlsn 2014/06/16 17:37:43 Done. I will try to remember this!
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_ =
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);
70 delete blacklist_registry_key_;
55 } 71 }
56 }; 72 };
57 73
58 namespace {
59
60 struct TestData { 74 struct TestData {
61 const wchar_t* dll_name; 75 const wchar_t* dll_name;
62 const wchar_t* dll_beacon; 76 const wchar_t* dll_beacon;
63 } test_data[] = {{kTestDllName2, kDll2Beacon}, {kTestDllName3, kDll3Beacon}}; 77 } test_data[] = {{kTestDllName2, kDll2Beacon}, {kTestDllName3, kDll3Beacon}};
64 78
65 TEST_F(BlacklistTest, Beacon) { 79 TEST_F(BlacklistTest, Beacon) {
66 registry_util::RegistryOverrideManager override_manager; 80 // Ensure that the beacon state starts off 'running' for this version.
67 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); 81 LONG result = blacklist_registry_key_->WriteValue(
68 82 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); 83 EXPECT_EQ(ERROR_SUCCESS, result);
77 84
78 result = blacklist_registry_key.WriteValue(blacklist::kBeaconVersion, 85 result = blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion,
79 TEXT(CHROME_VERSION_STRING)); 86 TEXT(CHROME_VERSION_STRING));
80 EXPECT_EQ(ERROR_SUCCESS, result); 87 EXPECT_EQ(ERROR_SUCCESS, result);
81 88
82 // First call should find the beacon and reset it. 89 // First call should find the beacon and reset it.
83 EXPECT_TRUE(blacklist::ResetBeacon()); 90 EXPECT_TRUE(blacklist::ResetBeacon());
84 91
85 // First call should succeed as the beacon is enabled. 92 // First call should succeed as the beacon is enabled.
86 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); 93 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 } 94 }
94 95
95 TEST_F(BlacklistTest, AddAndRemoveModules) { 96 TEST_F(BlacklistTest, AddAndRemoveModules) {
96 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); 97 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll"));
97 // Adding the same item twice should be idempotent. 98 // Adding the same item twice should be idempotent.
98 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); 99 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll"));
99 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); 100 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll"));
100 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); 101 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll"));
101 102
102 // Increase the blacklist size by 1 to include the NULL pointer 103 // Increase the blacklist size by 1 to include the NULL pointer
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 KEY_QUERY_VALUE | KEY_SET_VALUE); 238 KEY_QUERY_VALUE | KEY_SET_VALUE);
238 for (int i = 0; i < arraysize(test_data); ++i) { 239 for (int i = 0; i < arraysize(test_data); ++i) {
239 finch_blacklist_registry_key.WriteValue(test_data[i].dll_name, 240 finch_blacklist_registry_key.WriteValue(test_data[i].dll_name,
240 test_data[i].dll_name); 241 test_data[i].dll_name);
241 } 242 }
242 243
243 EXPECT_TRUE(TestDll_AddDllsFromRegistryToBlacklist()); 244 EXPECT_TRUE(TestDll_AddDllsFromRegistryToBlacklist());
244 CheckBlacklistedDllsNotLoaded(); 245 CheckBlacklistedDllsNotLoaded();
245 } 246 }
246 247
248 void TestResetBeacon(base::win::RegKey* key,
249 DWORD input_state,
250 DWORD expected_output_state) {
251 LONG result = key->WriteValue(blacklist::kBeaconState, input_state);
252 EXPECT_EQ(ERROR_SUCCESS, result);
253
254 EXPECT_TRUE(blacklist::ResetBeacon());
255 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX;
256 result = key->ReadValueDW(blacklist::kBeaconState, &blacklist_state);
257 EXPECT_EQ(ERROR_SUCCESS, result);
258 EXPECT_EQ(expected_output_state, blacklist_state);
259 }
260
261 TEST_F(BlacklistTest, ResetBeacon) {
262 // Ensure that ResetBeacon resets properly on successful runs and not on
263 // failed or disabled runs.
264 TestResetBeacon(blacklist_registry_key_,
265 blacklist::BLACKLIST_SETUP_RUNNING,
266 blacklist::BLACKLIST_ENABLED);
267
268 TestResetBeacon(blacklist_registry_key_,
269 blacklist::BLACKLIST_SETUP_FAILED,
270 blacklist::BLACKLIST_SETUP_FAILED);
271
272 TestResetBeacon(blacklist_registry_key_,
273 blacklist::BLACKLIST_DISABLED,
274 blacklist::BLACKLIST_DISABLED);
275 }
276
277 TEST_F(BlacklistTest, SetupFailed) {
278 // Ensure that when the number of failed tries reaches the maximum allowed,
279 // the blacklist state is set to failed.
280 LONG result = blacklist_registry_key_->WriteValue(
281 blacklist::kBeaconState, blacklist::BLACKLIST_SETUP_RUNNING);
282 EXPECT_EQ(ERROR_SUCCESS, result);
283
284 // Set the attempt count so that on the next failure the blacklist is
285 // disabled.
286 result = blacklist_registry_key_->WriteValue(
287 blacklist::kBeaconAttemptCount, blacklist::kBeaconMaxAttempts - 1);
288 EXPECT_EQ(ERROR_SUCCESS, result);
289
290 EXPECT_FALSE(blacklist::LeaveSetupBeacon());
291
292 DWORD attempt_count = 0;
293 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount,
294 &attempt_count);
295 EXPECT_EQ(attempt_count, blacklist::kBeaconMaxAttempts);
296
297 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX;
298 result = blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState,
299 &blacklist_state);
300 EXPECT_EQ(ERROR_SUCCESS, result);
301 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_FAILED);
302 }
303
304 TEST_F(BlacklistTest, SetupSucceeded) {
305 // Starting with the enabled beacon should result in the setup running state
306 // and the attempt counter reset to zero.
307 LONG result = blacklist_registry_key_->WriteValue(
308 blacklist::kBeaconState, blacklist::BLACKLIST_ENABLED);
309 EXPECT_EQ(ERROR_SUCCESS, result);
310 result = blacklist_registry_key_->WriteValue(blacklist::kBeaconAttemptCount,
311 blacklist::kBeaconMaxAttempts);
312 EXPECT_EQ(ERROR_SUCCESS, result);
313
314 EXPECT_TRUE(blacklist::LeaveSetupBeacon());
315
316 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX;
317 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState,
318 &blacklist_state);
319 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_RUNNING);
320
321 DWORD attempt_count = blacklist::kBeaconMaxAttempts;
322 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount,
323 &attempt_count);
324 EXPECT_EQ(static_cast<DWORD>(0), attempt_count);
325 }
326
247 } // namespace 327 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698