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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/environment.h" | 9 #include "base/environment.h" |
10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 const wchar_t kTestDllName3[] = L"blacklist_test_dll_3.dll"; | 30 const wchar_t kTestDllName3[] = L"blacklist_test_dll_3.dll"; |
31 | 31 |
32 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; | 32 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; |
33 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; | 33 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; |
34 | 34 |
35 extern const wchar_t* kEnvVars[]; | 35 extern const wchar_t* kEnvVars[]; |
36 | 36 |
37 namespace { | 37 namespace { |
38 | 38 |
39 // Functions we need from blacklist_test_main_dll.dll | 39 // Functions we need from blacklist_test_main_dll.dll |
40 typedef void (*TestDll_AddDllsFromRegistryToBlacklistFunction)(); | |
41 typedef bool (*TestDll_AddDllToBlacklistFunction)(const wchar_t* dll_name); | 40 typedef bool (*TestDll_AddDllToBlacklistFunction)(const wchar_t* dll_name); |
42 typedef int (*TestDll_BlacklistSizeFunction)(); | 41 typedef int (*TestDll_BlacklistSizeFunction)(); |
43 typedef void (*TestDll_BlockedDllFunction)(size_t blocked_index); | 42 typedef void (*TestDll_BlockedDllFunction)(size_t blocked_index); |
44 typedef int (*TestDll_GetBlacklistIndexFunction)(const wchar_t* dll_name); | 43 typedef int (*TestDll_GetBlacklistIndexFunction)(const wchar_t* dll_name); |
45 typedef bool (*TestDll_IsBlacklistInitializedFunction)(); | 44 typedef bool (*TestDll_IsBlacklistInitializedFunction)(); |
46 typedef bool (*TestDll_RemoveDllFromBlacklistFunction)(const wchar_t* dll_name); | 45 typedef bool (*TestDll_RemoveDllFromBlacklistFunction)(const wchar_t* dll_name); |
47 typedef bool (*TestDll_SuccessfullyBlockedFunction)( | 46 typedef bool (*TestDll_SuccessfullyBlockedFunction)( |
48 const wchar_t** blocked_dlls, | 47 const wchar_t** blocked_dlls, |
49 int* size); | 48 int* size); |
50 typedef void (*InitTestDllFunction)(); | 49 typedef void (*InitTestDllFunction)(); |
51 | 50 |
52 TestDll_AddDllsFromRegistryToBlacklistFunction | |
53 TestDll_AddDllsFromRegistryToBlacklist = nullptr; | |
54 TestDll_AddDllToBlacklistFunction TestDll_AddDllToBlacklist = nullptr; | 51 TestDll_AddDllToBlacklistFunction TestDll_AddDllToBlacklist = nullptr; |
55 TestDll_BlacklistSizeFunction TestDll_BlacklistSize = nullptr; | 52 TestDll_BlacklistSizeFunction TestDll_BlacklistSize = nullptr; |
56 TestDll_BlockedDllFunction TestDll_BlockedDll = nullptr; | 53 TestDll_BlockedDllFunction TestDll_BlockedDll = nullptr; |
57 TestDll_GetBlacklistIndexFunction TestDll_GetBlacklistIndex = nullptr; | 54 TestDll_GetBlacklistIndexFunction TestDll_GetBlacklistIndex = nullptr; |
58 TestDll_IsBlacklistInitializedFunction TestDll_IsBlacklistInitialized = nullptr; | 55 TestDll_IsBlacklistInitializedFunction TestDll_IsBlacklistInitialized = nullptr; |
59 TestDll_RemoveDllFromBlacklistFunction TestDll_RemoveDllFromBlacklist = nullptr; | 56 TestDll_RemoveDllFromBlacklistFunction TestDll_RemoveDllFromBlacklist = nullptr; |
60 TestDll_SuccessfullyBlockedFunction TestDll_SuccessfullyBlocked = nullptr; | 57 TestDll_SuccessfullyBlockedFunction TestDll_SuccessfullyBlocked = nullptr; |
61 InitTestDllFunction InitTestDll = nullptr; | 58 InitTestDllFunction InitTestDll = nullptr; |
62 | 59 |
63 struct TestData { | 60 struct TestData { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 150 |
154 // Make the override path available to our test DLL. | 151 // Make the override path available to our test DLL. |
155 IpcOverrides(); | 152 IpcOverrides(); |
156 | 153 |
157 // Load the main test Dll now. | 154 // Load the main test Dll now. |
158 // Note: this has to happen after we set up the registry overrides. | 155 // Note: this has to happen after we set up the registry overrides. |
159 HMODULE dll = nullptr; | 156 HMODULE dll = nullptr; |
160 dll = ::LoadLibraryW(L"blacklist_test_main_dll.dll"); | 157 dll = ::LoadLibraryW(L"blacklist_test_main_dll.dll"); |
161 if (!dll) | 158 if (!dll) |
162 return; | 159 return; |
163 TestDll_AddDllsFromRegistryToBlacklist = | |
164 reinterpret_cast<TestDll_AddDllsFromRegistryToBlacklistFunction>( | |
165 ::GetProcAddress(dll, "TestDll_AddDllsFromRegistryToBlacklist")); | |
166 TestDll_AddDllToBlacklist = | 160 TestDll_AddDllToBlacklist = |
167 reinterpret_cast<TestDll_AddDllToBlacklistFunction>( | 161 reinterpret_cast<TestDll_AddDllToBlacklistFunction>( |
168 ::GetProcAddress(dll, "TestDll_AddDllToBlacklist")); | 162 ::GetProcAddress(dll, "TestDll_AddDllToBlacklist")); |
169 TestDll_BlacklistSize = reinterpret_cast<TestDll_BlacklistSizeFunction>( | 163 TestDll_BlacklistSize = reinterpret_cast<TestDll_BlacklistSizeFunction>( |
170 ::GetProcAddress(dll, "TestDll_BlacklistSize")); | 164 ::GetProcAddress(dll, "TestDll_BlacklistSize")); |
171 TestDll_BlockedDll = reinterpret_cast<TestDll_BlockedDllFunction>( | 165 TestDll_BlockedDll = reinterpret_cast<TestDll_BlockedDllFunction>( |
172 ::GetProcAddress(dll, "TestDll_BlockedDll")); | 166 ::GetProcAddress(dll, "TestDll_BlockedDll")); |
173 TestDll_GetBlacklistIndex = | 167 TestDll_GetBlacklistIndex = |
174 reinterpret_cast<TestDll_GetBlacklistIndexFunction>( | 168 reinterpret_cast<TestDll_GetBlacklistIndexFunction>( |
175 ::GetProcAddress(dll, "TestDll_GetBlacklistIndex")); | 169 ::GetProcAddress(dll, "TestDll_GetBlacklistIndex")); |
176 TestDll_IsBlacklistInitialized = | 170 TestDll_IsBlacklistInitialized = |
177 reinterpret_cast<TestDll_IsBlacklistInitializedFunction>( | 171 reinterpret_cast<TestDll_IsBlacklistInitializedFunction>( |
178 ::GetProcAddress(dll, "TestDll_IsBlacklistInitialized")); | 172 ::GetProcAddress(dll, "TestDll_IsBlacklistInitialized")); |
179 TestDll_RemoveDllFromBlacklist = | 173 TestDll_RemoveDllFromBlacklist = |
180 reinterpret_cast<TestDll_RemoveDllFromBlacklistFunction>( | 174 reinterpret_cast<TestDll_RemoveDllFromBlacklistFunction>( |
181 ::GetProcAddress(dll, "TestDll_RemoveDllFromBlacklist")); | 175 ::GetProcAddress(dll, "TestDll_RemoveDllFromBlacklist")); |
182 TestDll_SuccessfullyBlocked = | 176 TestDll_SuccessfullyBlocked = |
183 reinterpret_cast<TestDll_SuccessfullyBlockedFunction>( | 177 reinterpret_cast<TestDll_SuccessfullyBlockedFunction>( |
184 ::GetProcAddress(dll, "TestDll_SuccessfullyBlocked")); | 178 ::GetProcAddress(dll, "TestDll_SuccessfullyBlocked")); |
185 InitTestDll = reinterpret_cast<InitTestDllFunction>( | 179 InitTestDll = reinterpret_cast<InitTestDllFunction>( |
186 ::GetProcAddress(dll, "InitTestDll")); | 180 ::GetProcAddress(dll, "InitTestDll")); |
187 if (!TestDll_AddDllsFromRegistryToBlacklist || !TestDll_AddDllToBlacklist || | 181 if (!TestDll_AddDllToBlacklist || !TestDll_BlacklistSize || |
188 !TestDll_BlacklistSize || !TestDll_BlockedDll || | 182 !TestDll_BlockedDll || !TestDll_GetBlacklistIndex || |
189 !TestDll_GetBlacklistIndex || !TestDll_IsBlacklistInitialized || | 183 !TestDll_IsBlacklistInitialized || !TestDll_RemoveDllFromBlacklist || |
190 !TestDll_RemoveDllFromBlacklist || !TestDll_SuccessfullyBlocked || | 184 !TestDll_SuccessfullyBlocked || !InitTestDll) |
191 !InitTestDll) | |
192 return; | 185 return; |
193 | 186 |
194 // We have to call this exported function every time this test setup runs. | 187 // We have to call this exported function every time this test setup runs. |
195 // If the tests are running in single process mode, the test DLL does not | 188 // If the tests are running in single process mode, the test DLL does not |
196 // get reloaded everytime - but we need to make sure it updates | 189 // get reloaded everytime - but we need to make sure it updates |
197 // appropriately. | 190 // appropriately. |
198 InitTestDll(); | 191 InitTestDll(); |
199 | 192 |
200 blacklist_registry_key_.reset( | 193 blacklist_registry_key_.reset( |
201 new base::win::RegKey(HKEY_CURRENT_USER, | 194 new base::win::RegKey(HKEY_CURRENT_USER, |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); | 302 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
310 EXPECT_EQ(num_initially_blocked_, num_blocked_dlls); | 303 EXPECT_EQ(num_initially_blocked_, num_blocked_dlls); |
311 | 304 |
312 // Add all DLLs to the blacklist then check they are blocked. | 305 // Add all DLLs to the blacklist then check they are blocked. |
313 for (size_t i = 0; i < arraysize(test_data); ++i) { | 306 for (size_t i = 0; i < arraysize(test_data); ++i) { |
314 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); | 307 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); |
315 } | 308 } |
316 CheckBlacklistedDllsNotLoaded(); | 309 CheckBlacklistedDllsNotLoaded(); |
317 } | 310 } |
318 | 311 |
319 TEST_F(BlacklistTest, AddDllsFromRegistryToBlacklist) { | |
320 // Ensure that the blacklist is loaded. | |
321 ASSERT_TRUE(TestDll_IsBlacklistInitialized()); | |
322 | |
323 // Delete the finch registry key to clear its values. | |
324 base::win::RegKey key(HKEY_CURRENT_USER, | |
325 blacklist::kRegistryFinchListPath, | |
326 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
327 key.DeleteKey(L""); | |
328 | |
329 // Add the test dlls to the registry. | |
330 // (REG_MULTI_SZ: eos separated, double null terminated.) | |
331 base::win::RegKey finch_blacklist_registry_key( | |
332 HKEY_CURRENT_USER, | |
333 blacklist::kRegistryFinchListPath, | |
334 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
335 | |
336 std::vector<wchar_t>(reg_buffer); | |
337 for (size_t i = 0; i < arraysize(test_data); ++i) { | |
338 if (reg_buffer.size() > 0) | |
339 reg_buffer.push_back(L'\0'); | |
340 const wchar_t* dll = test_data[i].dll_name; | |
341 // Append the name, not including terminator. | |
342 reg_buffer.insert(reg_buffer.end(), dll, dll + ::wcslen(dll)); | |
343 } | |
344 reg_buffer.push_back(L'\0'); | |
345 reg_buffer.push_back(L'\0'); | |
346 | |
347 finch_blacklist_registry_key.WriteValue( | |
348 blacklist::kRegistryFinchListValueName, reg_buffer.data(), | |
349 (DWORD)(reg_buffer.size() * sizeof(wchar_t)), REG_MULTI_SZ); | |
350 | |
351 TestDll_AddDllsFromRegistryToBlacklist(); | |
352 CheckBlacklistedDllsNotLoaded(); | |
353 } | |
354 | |
355 void TestResetBeacon(std::unique_ptr<base::win::RegKey>& key, | 312 void TestResetBeacon(std::unique_ptr<base::win::RegKey>& key, |
356 DWORD input_state, | 313 DWORD input_state, |
357 DWORD expected_output_state) { | 314 DWORD expected_output_state) { |
358 LONG result = key->WriteValue(blacklist::kBeaconState, input_state); | 315 LONG result = key->WriteValue(blacklist::kBeaconState, input_state); |
359 EXPECT_EQ(ERROR_SUCCESS, result); | 316 EXPECT_EQ(ERROR_SUCCESS, result); |
360 | 317 |
361 EXPECT_TRUE(blacklist::ResetBeacon()); | 318 EXPECT_TRUE(blacklist::ResetBeacon()); |
362 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; | 319 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
363 result = key->ReadValueDW(blacklist::kBeaconState, &blacklist_state); | 320 result = key->ReadValueDW(blacklist::kBeaconState, &blacklist_state); |
364 EXPECT_EQ(ERROR_SUCCESS, result); | 321 EXPECT_EQ(ERROR_SUCCESS, result); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 &blacklist_state); | 382 &blacklist_state); |
426 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_RUNNING); | 383 EXPECT_EQ(blacklist_state, blacklist::BLACKLIST_SETUP_RUNNING); |
427 | 384 |
428 DWORD attempt_count = blacklist::kBeaconMaxAttempts; | 385 DWORD attempt_count = blacklist::kBeaconMaxAttempts; |
429 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, | 386 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, |
430 &attempt_count); | 387 &attempt_count); |
431 EXPECT_EQ(static_cast<DWORD>(0), attempt_count); | 388 EXPECT_EQ(static_cast<DWORD>(0), attempt_count); |
432 } | 389 } |
433 | 390 |
434 } // namespace | 391 } // namespace |
OLD | NEW |