Index: chrome_elf/blacklist/test/blacklist_test.cc |
diff --git a/chrome_elf/blacklist/test/blacklist_test.cc b/chrome_elf/blacklist/test/blacklist_test.cc |
index 05aeb14f1b123611451b66dd3111cc8665f8ef85..4d45bfd38c4af69048611a1abe24f3d1aef1baa3 100644 |
--- a/chrome_elf/blacklist/test/blacklist_test.cc |
+++ b/chrome_elf/blacklist/test/blacklist_test.cc |
@@ -32,6 +32,7 @@ extern "C" { |
// When modifying the blacklist in the test process, use the exported test dll |
// functions on the test blacklist dll, not the ones linked into the test |
// executable itself. |
+__declspec(dllimport) bool TestDll_AddDllsFromRegistryToBlacklist(); |
__declspec(dllimport) bool TestDll_AddDllToBlacklist(const wchar_t* dll_name); |
__declspec(dllimport) bool TestDll_IsBlacklistInitialized(); |
__declspec(dllimport) bool TestDll_RemoveDllFromBlacklist( |
@@ -161,7 +162,7 @@ TEST_F(BlacklistTest, LoadBlacklistedLibrary) { |
{ kTestDllName2, kDll2Beacon }, |
{ kTestDllName3, kDll3Beacon } |
}; |
- for (int i = 0 ; i < arraysize(test_data); ++i) { |
+ for (int i = 0; i < arraysize(test_data); ++i) { |
// Add the DLL to the blacklist, ensure that it is not loaded both by |
// inspecting the handle returned by LoadLibrary and by looking for an |
// environment variable that is set when the DLL's entry point is called. |
@@ -207,3 +208,97 @@ TEST_F(BlacklistTest, LoadBlacklistedLibrary) { |
EXPECT_EQ(0, num_blocked_dlls); |
} |
} |
+ |
+TEST_F(BlacklistTest, AddDllsFromRegistryToBlacklist) { |
+ base::FilePath current_dir; |
+ ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
+ |
+ // Ensure that the blacklist is loaded. |
+ ASSERT_TRUE(TestDll_IsBlacklistInitialized()); |
+ |
+ HKEY key = NULL; |
+ LONG result = ::RegOpenKeyEx(HKEY_CURRENT_USER, |
+ blacklist::kRegistryFinchListPath, |
+ 0, |
+ KEY_QUERY_VALUE | KEY_SET_VALUE, |
+ &key); |
+ |
+ // Ensure that we were able to open the registry key. |
+ ASSERT_TRUE(result == ERROR_SUCCESS); |
+ |
+ int num_dlls; |
+ int longest_name; |
+ ::RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, |
+ reinterpret_cast<DWORD*>(&num_dlls), |
+ reinterpret_cast<DWORD*>(&longest_name), NULL, NULL, NULL); |
+ |
+ // Collect any dll names currently stored in the regisry and delete them. |
+ std::vector<wchar_t*> dll_names(num_dlls); |
+ for (int i = 0; i < num_dlls; ++i) { |
+ DWORD name_len(longest_name + 1); |
+ wchar_t* name_buffer = new wchar_t[name_len]; |
+ result = ::RegEnumValue(key, i, name_buffer, &name_len, NULL, NULL, |
+ NULL, NULL); |
+ |
+ if (result == ERROR_SUCCESS) { |
+ dll_names[i] = name_buffer; |
+ } |
+ } |
+ |
+ for (int i = 0; i < num_dlls; ++i) { |
+ ::RegDeleteValue(key, dll_names[i]); |
+ delete[] dll_names[i]; |
+ } |
+ |
+ struct TestData { |
+ const wchar_t* dll_name; |
+ const wchar_t* dll_beacon; |
+ } test_data[] = { |
+ { kTestDllName2, kDll2Beacon }, |
+ { kTestDllName3, kDll3Beacon } |
+ }; |
+ |
+ // Add the test dlls to the registry. |
+ for (int i = 0; i < arraysize(test_data); ++i) { |
+ result = ::RegSetKeyValue(key, NULL, test_data[i].dll_name, REG_SZ, |
+ &(test_data[i].dll_name), sizeof(test_data[i].dll_name)); |
+ } |
+ |
+ ::RegCloseKey(key); |
+ |
+ TestDll_AddDllsFromRegistryToBlacklist(); |
+ |
+ for (int i = 0 ; i < arraysize(test_data); ++i) { |
+ // Ensure that the dll has not been loaded both by |
+ // inspecting the handle returned by LoadLibrary and by looking for an |
+ // environment variable that is set when the DLL's entry point is called. |
+ base::ScopedNativeLibrary dll_blacklisted( |
+ current_dir.Append(test_data[i].dll_name)); |
+ EXPECT_FALSE(dll_blacklisted.is_valid()); |
+ EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
+ dll_blacklisted.Reset(NULL); |
+ |
+ // Ensure that the dll is recorded as blocked. |
+ int array_size = 1; |
+ const wchar_t* blocked_dll = NULL; |
+ TestDll_SuccessfullyBlocked(&blocked_dll, &array_size); |
+ EXPECT_EQ(1, array_size); |
+ EXPECT_EQ(test_data[i].dll_name, base::string16(blocked_dll)); |
+ |
+ // Remove the DLL from the blacklist. Ensure that it loads and that its |
+ // entry point was called. |
+ EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(test_data[i].dll_name)); |
+ base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); |
+ EXPECT_TRUE(dll.is_valid()); |
+ EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
+ dll.Reset(NULL); |
+ |
+ ::SetEnvironmentVariable(test_data[i].dll_beacon, NULL); |
+ |
+ // The blocked dll was removed, so we shouldn't get anything returned |
+ // here. |
+ int num_blocked_dlls = 0; |
+ TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
+ EXPECT_EQ(0, num_blocked_dlls); |
+ } |
+} |