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
3 // found in the LICENSE file.
5 #include "base/environment.h"
6 #include "base/files/file_path.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/i18n/case_conversion.h"
9 #include "base/path_service.h"
10 #include "base/scoped_native_library.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/test_reg_util_win.h"
15 #include "base/win/registry.h"
16 #include "chrome_elf/blacklist/blacklist.h"
17 #include "chrome_elf/blacklist/test/blacklist_test_main_dll.h"
18 #include "chrome_elf/chrome_elf_constants.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "version.h" // NOLINT
22 const wchar_t kTestDllName1
[] = L
"blacklist_test_dll_1.dll";
23 const wchar_t kTestDllName2
[] = L
"blacklist_test_dll_2.dll";
24 const wchar_t kTestDllName3
[] = L
"blacklist_test_dll_3.dll";
26 const wchar_t kDll2Beacon
[] = L
"{F70A0100-2889-4629-9B44-610FE5C73231}";
27 const wchar_t kDll3Beacon
[] = L
"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}";
29 extern const wchar_t* kEnvVars
[];
32 // When modifying the blacklist in the test process, use the exported test dll
33 // functions on the test blacklist dll, not the ones linked into the test
35 __declspec(dllimport
) void TestDll_AddDllsFromRegistryToBlacklist();
36 __declspec(dllimport
) bool TestDll_AddDllToBlacklist(const wchar_t* dll_name
);
37 __declspec(dllimport
) int TestDll_BlacklistSize();
38 __declspec(dllimport
) void TestDll_BlockedDll(size_t blocked_index
);
39 __declspec(dllimport
) int TestDll_GetBlacklistIndex(const wchar_t* dll_name
);
40 __declspec(dllimport
) bool TestDll_IsBlacklistInitialized();
41 __declspec(dllimport
) bool TestDll_RemoveDllFromBlacklist(
42 const wchar_t* dll_name
);
43 __declspec(dllimport
) bool TestDll_SuccessfullyBlocked(
44 const wchar_t** blocked_dlls
,
51 const wchar_t* dll_name
;
52 const wchar_t* dll_beacon
;
54 {kTestDllName2
, kDll2Beacon
},
55 {kTestDllName3
, kDll3Beacon
}
58 class BlacklistTest
: public testing::Test
{
60 BlacklistTest() : override_manager_(), num_initially_blocked_(0) {
61 override_manager_
.OverrideRegistry(HKEY_CURRENT_USER
);
64 void CheckBlacklistedDllsNotLoaded() {
65 base::FilePath current_dir
;
66 ASSERT_TRUE(PathService::Get(base::DIR_EXE
, ¤t_dir
));
68 for (int i
= 0; i
< arraysize(test_data
); ++i
) {
69 // Ensure that the dll has not been loaded both by inspecting the handle
70 // returned by LoadLibrary and by looking for an environment variable that
71 // is set when the DLL's entry point is called.
72 base::ScopedNativeLibrary
dll_blacklisted(
73 current_dir
.Append(test_data
[i
].dll_name
));
74 EXPECT_FALSE(dll_blacklisted
.is_valid());
75 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data
[i
].dll_beacon
, NULL
, 0));
76 dll_blacklisted
.Reset(NULL
);
78 // Ensure that the dll is recorded as blocked.
79 int array_size
= 1 + num_initially_blocked_
;
80 std::vector
<const wchar_t*> blocked_dlls(array_size
);
81 TestDll_SuccessfullyBlocked(&blocked_dlls
[0], &array_size
);
82 EXPECT_EQ(1 + num_initially_blocked_
, array_size
);
83 EXPECT_STREQ(test_data
[i
].dll_name
, blocked_dlls
[num_initially_blocked_
]);
85 // Remove the DLL from the blacklist. Ensure that it loads and that its
86 // entry point was called.
87 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(test_data
[i
].dll_name
));
88 base::ScopedNativeLibrary
dll(current_dir
.Append(test_data
[i
].dll_name
));
89 EXPECT_TRUE(dll
.is_valid());
90 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data
[i
].dll_beacon
, NULL
, 0));
93 ::SetEnvironmentVariable(test_data
[i
].dll_beacon
, NULL
);
95 // Ensure that the dll won't load even if the name has different
97 base::string16 uppercase_name
=
98 base::i18n::ToUpper(test_data
[i
].dll_name
);
99 EXPECT_TRUE(TestDll_AddDllToBlacklist(uppercase_name
.c_str()));
100 base::ScopedNativeLibrary
dll_blacklisted_different_case(
101 current_dir
.Append(test_data
[i
].dll_name
));
102 EXPECT_FALSE(dll_blacklisted_different_case
.is_valid());
103 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data
[i
].dll_beacon
, NULL
, 0));
104 dll_blacklisted_different_case
.Reset(NULL
);
106 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name
.c_str()));
108 // The blocked dll was removed, so the number of blocked dlls should
109 // return to what it originally was.
110 int num_blocked_dlls
= 0;
111 TestDll_SuccessfullyBlocked(NULL
, &num_blocked_dlls
);
112 EXPECT_EQ(num_initially_blocked_
, num_blocked_dlls
);
116 scoped_ptr
<base::win::RegKey
> blacklist_registry_key_
;
117 registry_util::RegistryOverrideManager override_manager_
;
119 // The number of dlls initially blocked by the blacklist.
120 int num_initially_blocked_
;
123 virtual void SetUp() {
124 // Force an import from blacklist_test_main_dll.
125 InitBlacklistTestDll();
126 blacklist_registry_key_
.reset(
127 new base::win::RegKey(HKEY_CURRENT_USER
,
128 blacklist::kRegistryBeaconPath
,
129 KEY_QUERY_VALUE
| KEY_SET_VALUE
));
131 // Find out how many dlls were blocked before the test starts.
132 TestDll_SuccessfullyBlocked(NULL
, &num_initially_blocked_
);
135 virtual void TearDown() {
136 TestDll_RemoveDllFromBlacklist(kTestDllName1
);
137 TestDll_RemoveDllFromBlacklist(kTestDllName2
);
138 TestDll_RemoveDllFromBlacklist(kTestDllName3
);
142 TEST_F(BlacklistTest
, Beacon
) {
143 // Ensure that the beacon state starts off 'running' for this version.
144 LONG result
= blacklist_registry_key_
->WriteValue(
145 blacklist::kBeaconState
, blacklist::BLACKLIST_SETUP_RUNNING
);
146 EXPECT_EQ(ERROR_SUCCESS
, result
);
148 result
= blacklist_registry_key_
->WriteValue(blacklist::kBeaconVersion
,
149 TEXT(CHROME_VERSION_STRING
));
150 EXPECT_EQ(ERROR_SUCCESS
, result
);
152 // First call should find the beacon and reset it.
153 EXPECT_TRUE(blacklist::ResetBeacon());
155 // First call should succeed as the beacon is enabled.
156 EXPECT_TRUE(blacklist::LeaveSetupBeacon());
159 TEST_F(BlacklistTest
, AddAndRemoveModules
) {
160 EXPECT_TRUE(TestDll_AddDllToBlacklist(L
"foo.dll"));
161 // Adding the same item twice should be idempotent.
162 EXPECT_TRUE(TestDll_AddDllToBlacklist(L
"foo.dll"));
163 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(L
"foo.dll"));
164 EXPECT_FALSE(TestDll_RemoveDllFromBlacklist(L
"foo.dll"));
166 // Increase the blacklist size by 1 to include the NULL pointer
167 // that marks the end.
169 blacklist::kTroublesomeDllsMaxCount
- (TestDll_BlacklistSize() + 1);
170 std::vector
<base::string16
> added_dlls
;
171 added_dlls
.reserve(empty_spaces
);
172 for (int i
= 0; i
< empty_spaces
; ++i
) {
173 added_dlls
.push_back(base::IntToString16(i
) + L
".dll");
174 EXPECT_TRUE(TestDll_AddDllToBlacklist(added_dlls
[i
].c_str())) << i
;
176 EXPECT_FALSE(TestDll_AddDllToBlacklist(L
"overflow.dll"));
177 for (int i
= 0; i
< empty_spaces
; ++i
) {
178 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(added_dlls
[i
].c_str())) << i
;
180 EXPECT_FALSE(TestDll_RemoveDllFromBlacklist(added_dlls
[0].c_str()));
182 TestDll_RemoveDllFromBlacklist(added_dlls
[empty_spaces
- 1].c_str()));
185 TEST_F(BlacklistTest
, SuccessfullyBlocked
) {
186 // Add 5 news dlls to blacklist.
187 const int kDesiredBlacklistSize
= 1;
188 std::vector
<base::string16
> dlls_to_block
;
189 for (int i
= 0; i
< kDesiredBlacklistSize
; ++i
) {
190 dlls_to_block
.push_back(base::IntToString16(i
) + L
".dll");
191 ASSERT_TRUE(TestDll_AddDllToBlacklist(dlls_to_block
[i
].c_str()));
194 // Block the dlls, one at a time, and ensure SuccesfullyBlocked correctly
195 // passes the list of blocked dlls.
196 for (int i
= 0; i
< kDesiredBlacklistSize
; ++i
) {
197 TestDll_BlockedDll(TestDll_GetBlacklistIndex(dlls_to_block
[i
].c_str()));
200 TestDll_SuccessfullyBlocked(NULL
, &size
);
201 ASSERT_EQ(num_initially_blocked_
+ i
+ 1, size
);
203 std::vector
<const wchar_t*> blocked_dlls(size
);
204 TestDll_SuccessfullyBlocked(&(blocked_dlls
[0]), &size
);
205 ASSERT_EQ(num_initially_blocked_
+ i
+ 1, size
);
207 for (int j
= 0; j
<= i
; ++j
) {
208 EXPECT_STREQ(blocked_dlls
[num_initially_blocked_
+ j
],
209 dlls_to_block
[j
].c_str());
213 // Remove the dlls from the blacklist now that we are done.
214 for (const auto& dll
: dlls_to_block
) {
215 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(dll
.c_str()));
219 TEST_F(BlacklistTest
, LoadBlacklistedLibrary
) {
220 base::FilePath current_dir
;
221 ASSERT_TRUE(PathService::Get(base::DIR_EXE
, ¤t_dir
));
223 // Ensure that the blacklist is loaded.
224 ASSERT_TRUE(TestDll_IsBlacklistInitialized());
226 // Test that an un-blacklisted DLL can load correctly.
227 base::ScopedNativeLibrary
dll1(current_dir
.Append(kTestDllName1
));
228 EXPECT_TRUE(dll1
.is_valid());
231 int num_blocked_dlls
= 0;
232 TestDll_SuccessfullyBlocked(NULL
, &num_blocked_dlls
);
233 EXPECT_EQ(num_initially_blocked_
, num_blocked_dlls
);
235 // Add all DLLs to the blacklist then check they are blocked.
236 for (int i
= 0; i
< arraysize(test_data
); ++i
) {
237 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data
[i
].dll_name
));
239 CheckBlacklistedDllsNotLoaded();
242 TEST_F(BlacklistTest
, AddDllsFromRegistryToBlacklist
) {
243 // Ensure that the blacklist is loaded.
244 ASSERT_TRUE(TestDll_IsBlacklistInitialized());
246 // Delete the finch registry key to clear its values.
247 base::win::RegKey
key(HKEY_CURRENT_USER
,
248 blacklist::kRegistryFinchListPath
,
249 KEY_QUERY_VALUE
| KEY_SET_VALUE
);
252 // Add the test dlls to the registry (with their name as both key and value).
253 base::win::RegKey
finch_blacklist_registry_key(
255 blacklist::kRegistryFinchListPath
,
256 KEY_QUERY_VALUE
| KEY_SET_VALUE
);
257 for (int i
= 0; i
< arraysize(test_data
); ++i
) {
258 finch_blacklist_registry_key
.WriteValue(test_data
[i
].dll_name
,
259 test_data
[i
].dll_name
);
262 TestDll_AddDllsFromRegistryToBlacklist();
263 CheckBlacklistedDllsNotLoaded();
266 void TestResetBeacon(scoped_ptr
<base::win::RegKey
>& key
,
268 DWORD expected_output_state
) {
269 LONG result
= key
->WriteValue(blacklist::kBeaconState
, input_state
);
270 EXPECT_EQ(ERROR_SUCCESS
, result
);
272 EXPECT_TRUE(blacklist::ResetBeacon());
273 DWORD blacklist_state
= blacklist::BLACKLIST_STATE_MAX
;
274 result
= key
->ReadValueDW(blacklist::kBeaconState
, &blacklist_state
);
275 EXPECT_EQ(ERROR_SUCCESS
, result
);
276 EXPECT_EQ(expected_output_state
, blacklist_state
);
279 TEST_F(BlacklistTest
, ResetBeacon
) {
280 // Ensure that ResetBeacon resets properly on successful runs and not on
281 // failed or disabled runs.
282 TestResetBeacon(blacklist_registry_key_
,
283 blacklist::BLACKLIST_SETUP_RUNNING
,
284 blacklist::BLACKLIST_ENABLED
);
286 TestResetBeacon(blacklist_registry_key_
,
287 blacklist::BLACKLIST_SETUP_FAILED
,
288 blacklist::BLACKLIST_SETUP_FAILED
);
290 TestResetBeacon(blacklist_registry_key_
,
291 blacklist::BLACKLIST_DISABLED
,
292 blacklist::BLACKLIST_DISABLED
);
295 TEST_F(BlacklistTest
, SetupFailed
) {
296 // Ensure that when the number of failed tries reaches the maximum allowed,
297 // the blacklist state is set to failed.
298 LONG result
= blacklist_registry_key_
->WriteValue(
299 blacklist::kBeaconState
, blacklist::BLACKLIST_SETUP_RUNNING
);
300 EXPECT_EQ(ERROR_SUCCESS
, result
);
302 // Set the attempt count so that on the next failure the blacklist is
304 result
= blacklist_registry_key_
->WriteValue(
305 blacklist::kBeaconAttemptCount
, blacklist::kBeaconMaxAttempts
- 1);
306 EXPECT_EQ(ERROR_SUCCESS
, result
);
308 EXPECT_FALSE(blacklist::LeaveSetupBeacon());
310 DWORD attempt_count
= 0;
311 blacklist_registry_key_
->ReadValueDW(blacklist::kBeaconAttemptCount
,
313 EXPECT_EQ(attempt_count
, blacklist::kBeaconMaxAttempts
);
315 DWORD blacklist_state
= blacklist::BLACKLIST_STATE_MAX
;
316 result
= blacklist_registry_key_
->ReadValueDW(blacklist::kBeaconState
,
318 EXPECT_EQ(ERROR_SUCCESS
, result
);
319 EXPECT_EQ(blacklist_state
, blacklist::BLACKLIST_SETUP_FAILED
);
322 TEST_F(BlacklistTest
, SetupSucceeded
) {
323 // Starting with the enabled beacon should result in the setup running state
324 // and the attempt counter reset to zero.
325 LONG result
= blacklist_registry_key_
->WriteValue(
326 blacklist::kBeaconState
, blacklist::BLACKLIST_ENABLED
);
327 EXPECT_EQ(ERROR_SUCCESS
, result
);
328 result
= blacklist_registry_key_
->WriteValue(blacklist::kBeaconAttemptCount
,
329 blacklist::kBeaconMaxAttempts
);
330 EXPECT_EQ(ERROR_SUCCESS
, result
);
332 EXPECT_TRUE(blacklist::LeaveSetupBeacon());
334 DWORD blacklist_state
= blacklist::BLACKLIST_STATE_MAX
;
335 blacklist_registry_key_
->ReadValueDW(blacklist::kBeaconState
,
337 EXPECT_EQ(blacklist_state
, blacklist::BLACKLIST_SETUP_RUNNING
);
339 DWORD attempt_count
= blacklist::kBeaconMaxAttempts
;
340 blacklist_registry_key_
->ReadValueDW(blacklist::kBeaconAttemptCount
,
342 EXPECT_EQ(static_cast<DWORD
>(0), attempt_count
);