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 14 matching lines...) Expand all Loading... |
25 | 25 |
26 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; | 26 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; |
27 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; | 27 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; |
28 | 28 |
29 extern const wchar_t* kEnvVars[]; | 29 extern const wchar_t* kEnvVars[]; |
30 | 30 |
31 extern "C" { | 31 extern "C" { |
32 // When modifying the blacklist in the test process, use the exported test dll | 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 | 33 // functions on the test blacklist dll, not the ones linked into the test |
34 // executable itself. | 34 // executable itself. |
| 35 __declspec(dllimport) bool TestDll_AddDllsFromRegistryToBlacklist(); |
35 __declspec(dllimport) bool TestDll_AddDllToBlacklist(const wchar_t* dll_name); | 36 __declspec(dllimport) bool TestDll_AddDllToBlacklist(const wchar_t* dll_name); |
36 __declspec(dllimport) bool TestDll_IsBlacklistInitialized(); | 37 __declspec(dllimport) bool TestDll_IsBlacklistInitialized(); |
37 __declspec(dllimport) bool TestDll_RemoveDllFromBlacklist( | 38 __declspec(dllimport) bool TestDll_RemoveDllFromBlacklist( |
38 const wchar_t* dll_name); | 39 const wchar_t* dll_name); |
39 __declspec(dllimport) bool TestDll_SuccessfullyBlocked( | 40 __declspec(dllimport) bool TestDll_SuccessfullyBlocked( |
40 const wchar_t** blocked_dlls, | 41 const wchar_t** blocked_dlls, |
41 int* size); | 42 int* size); |
42 } | 43 } |
43 | 44 |
44 class BlacklistTest : public testing::Test { | 45 class BlacklistTest : public testing::Test { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); | 155 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
155 EXPECT_EQ(0, num_blocked_dlls); | 156 EXPECT_EQ(0, num_blocked_dlls); |
156 | 157 |
157 struct TestData { | 158 struct TestData { |
158 const wchar_t* dll_name; | 159 const wchar_t* dll_name; |
159 const wchar_t* dll_beacon; | 160 const wchar_t* dll_beacon; |
160 } test_data[] = { | 161 } test_data[] = { |
161 { kTestDllName2, kDll2Beacon }, | 162 { kTestDllName2, kDll2Beacon }, |
162 { kTestDllName3, kDll3Beacon } | 163 { kTestDllName3, kDll3Beacon } |
163 }; | 164 }; |
164 for (int i = 0 ; i < arraysize(test_data); ++i) { | 165 for (int i = 0; i < arraysize(test_data); ++i) { |
165 // Add the DLL to the blacklist, ensure that it is not loaded both by | 166 // Add the DLL to the blacklist, ensure that it is not loaded both by |
166 // inspecting the handle returned by LoadLibrary and by looking for an | 167 // inspecting the handle returned by LoadLibrary and by looking for an |
167 // environment variable that is set when the DLL's entry point is called. | 168 // environment variable that is set when the DLL's entry point is called. |
168 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); | 169 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); |
169 base::ScopedNativeLibrary dll_blacklisted( | 170 base::ScopedNativeLibrary dll_blacklisted( |
170 current_dir.Append(test_data[i].dll_name)); | 171 current_dir.Append(test_data[i].dll_name)); |
171 EXPECT_FALSE(dll_blacklisted.is_valid()); | 172 EXPECT_FALSE(dll_blacklisted.is_valid()); |
172 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | 173 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
173 dll_blacklisted.Reset(NULL); | 174 dll_blacklisted.Reset(NULL); |
174 | 175 |
(...skipping 25 matching lines...) Expand all Loading... |
200 dll_blacklisted_different_case.Reset(NULL); | 201 dll_blacklisted_different_case.Reset(NULL); |
201 | 202 |
202 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); | 203 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); |
203 | 204 |
204 // The blocked dll was removed, so we shouldn't get anything returned | 205 // The blocked dll was removed, so we shouldn't get anything returned |
205 // here. | 206 // here. |
206 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); | 207 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
207 EXPECT_EQ(0, num_blocked_dlls); | 208 EXPECT_EQ(0, num_blocked_dlls); |
208 } | 209 } |
209 } | 210 } |
| 211 |
| 212 TEST_F(BlacklistTest, AddDllsFromRegistryToBlacklist) { |
| 213 base::FilePath current_dir; |
| 214 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
| 215 |
| 216 // Ensure that the blacklist is loaded. |
| 217 ASSERT_TRUE(TestDll_IsBlacklistInitialized()); |
| 218 |
| 219 HKEY key = NULL; |
| 220 LONG result = ::RegOpenKeyEx(HKEY_CURRENT_USER, |
| 221 blacklist::kRegistryFinchListPath, |
| 222 0, |
| 223 KEY_QUERY_VALUE | KEY_SET_VALUE, |
| 224 &key); |
| 225 |
| 226 // Ensure that we were able to open the registry key. |
| 227 ASSERT_TRUE(result == ERROR_SUCCESS); |
| 228 |
| 229 int num_dlls; |
| 230 int longest_name; |
| 231 ::RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, |
| 232 reinterpret_cast<DWORD*>(&num_dlls), |
| 233 reinterpret_cast<DWORD*>(&longest_name), NULL, NULL, NULL); |
| 234 |
| 235 // Collect any dll names currently stored in the regisry and delete them. |
| 236 std::vector<wchar_t*> dll_names(num_dlls); |
| 237 for (int i = 0; i < num_dlls; ++i) { |
| 238 DWORD name_len(longest_name + 1); |
| 239 wchar_t* name_buffer = new wchar_t[name_len]; |
| 240 result = ::RegEnumValue(key, i, name_buffer, &name_len, NULL, NULL, |
| 241 NULL, NULL); |
| 242 |
| 243 if (result == ERROR_SUCCESS) { |
| 244 dll_names[i] = name_buffer; |
| 245 } |
| 246 } |
| 247 |
| 248 for (int i = 0; i < num_dlls; ++i) { |
| 249 ::RegDeleteValue(key, dll_names[i]); |
| 250 delete[] dll_names[i]; |
| 251 } |
| 252 |
| 253 struct TestData { |
| 254 const wchar_t* dll_name; |
| 255 const wchar_t* dll_beacon; |
| 256 } test_data[] = { |
| 257 { kTestDllName2, kDll2Beacon }, |
| 258 { kTestDllName3, kDll3Beacon } |
| 259 }; |
| 260 |
| 261 // Add the test dlls to the registry. |
| 262 for (int i = 0; i < arraysize(test_data); ++i) { |
| 263 result = ::RegSetKeyValue(key, NULL, test_data[i].dll_name, REG_SZ, |
| 264 &(test_data[i].dll_name), sizeof(test_data[i].dll_name)); |
| 265 } |
| 266 |
| 267 ::RegCloseKey(key); |
| 268 |
| 269 TestDll_AddDllsFromRegistryToBlacklist(); |
| 270 |
| 271 for (int i = 0 ; i < arraysize(test_data); ++i) { |
| 272 // Ensure that the dll has not been loaded both by |
| 273 // inspecting the handle returned by LoadLibrary and by looking for an |
| 274 // environment variable that is set when the DLL's entry point is called. |
| 275 base::ScopedNativeLibrary dll_blacklisted( |
| 276 current_dir.Append(test_data[i].dll_name)); |
| 277 EXPECT_FALSE(dll_blacklisted.is_valid()); |
| 278 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
| 279 dll_blacklisted.Reset(NULL); |
| 280 |
| 281 // Ensure that the dll is recorded as blocked. |
| 282 int array_size = 1; |
| 283 const wchar_t* blocked_dll = NULL; |
| 284 TestDll_SuccessfullyBlocked(&blocked_dll, &array_size); |
| 285 EXPECT_EQ(1, array_size); |
| 286 EXPECT_EQ(test_data[i].dll_name, base::string16(blocked_dll)); |
| 287 |
| 288 // Remove the DLL from the blacklist. Ensure that it loads and that its |
| 289 // entry point was called. |
| 290 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(test_data[i].dll_name)); |
| 291 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); |
| 292 EXPECT_TRUE(dll.is_valid()); |
| 293 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
| 294 dll.Reset(NULL); |
| 295 |
| 296 ::SetEnvironmentVariable(test_data[i].dll_beacon, NULL); |
| 297 |
| 298 // The blocked dll was removed, so we shouldn't get anything returned |
| 299 // here. |
| 300 int num_blocked_dlls = 0; |
| 301 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
| 302 EXPECT_EQ(0, num_blocked_dlls); |
| 303 } |
| 304 } |
OLD | NEW |