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 { |
45 virtual void SetUp() { | 46 virtual void SetUp() { |
46 // Force an import from blacklist_test_main_dll. | 47 // Force an import from blacklist_test_main_dll. |
47 InitBlacklistTestDll(); | 48 InitBlacklistTestDll(); |
48 } | 49 } |
49 | 50 |
50 virtual void TearDown() { | 51 virtual void TearDown() { |
51 TestDll_RemoveDllFromBlacklist(kTestDllName1); | 52 TestDll_RemoveDllFromBlacklist(kTestDllName1); |
52 TestDll_RemoveDllFromBlacklist(kTestDllName2); | 53 TestDll_RemoveDllFromBlacklist(kTestDllName2); |
53 TestDll_RemoveDllFromBlacklist(kTestDllName3); | 54 TestDll_RemoveDllFromBlacklist(kTestDllName3); |
54 } | 55 } |
55 }; | 56 }; |
56 | 57 |
| 58 namespace { |
| 59 |
| 60 struct TestData { |
| 61 const wchar_t* dll_name; |
| 62 const wchar_t* dll_beacon; |
| 63 } test_data[] = {{kTestDllName2, kDll2Beacon}, {kTestDllName3, kDll3Beacon}}; |
| 64 |
57 TEST_F(BlacklistTest, Beacon) { | 65 TEST_F(BlacklistTest, Beacon) { |
58 registry_util::RegistryOverrideManager override_manager; | 66 registry_util::RegistryOverrideManager override_manager; |
59 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); | 67 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); |
60 | 68 |
61 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, | 69 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, |
62 blacklist::kRegistryBeaconPath, | 70 blacklist::kRegistryBeaconPath, |
63 KEY_QUERY_VALUE | KEY_SET_VALUE); | 71 KEY_QUERY_VALUE | KEY_SET_VALUE); |
64 | 72 |
65 // Ensure that the beacon state starts off enabled for this version. | 73 // Ensure that the beacon state starts off enabled for this version. |
66 LONG result = blacklist_registry_key.WriteValue(blacklist::kBeaconState, | 74 LONG result = blacklist_registry_key.WriteValue(blacklist::kBeaconState, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 std::vector<const wchar_t*> blocked_dlls(size); | 139 std::vector<const wchar_t*> blocked_dlls(size); |
132 blacklist::SuccessfullyBlocked(&(blocked_dlls[0]), &size); | 140 blacklist::SuccessfullyBlocked(&(blocked_dlls[0]), &size); |
133 EXPECT_EQ(i + 1, size); | 141 EXPECT_EQ(i + 1, size); |
134 | 142 |
135 for (size_t j = 0; j < blocked_dlls.size(); ++j) { | 143 for (size_t j = 0; j < blocked_dlls.size(); ++j) { |
136 EXPECT_EQ(blocked_dlls[j], blacklist::g_troublesome_dlls[j]); | 144 EXPECT_EQ(blocked_dlls[j], blacklist::g_troublesome_dlls[j]); |
137 } | 145 } |
138 } | 146 } |
139 } | 147 } |
140 | 148 |
141 TEST_F(BlacklistTest, LoadBlacklistedLibrary) { | 149 void CheckBlacklistedDllsNotLoaded() { |
142 base::FilePath current_dir; | 150 base::FilePath current_dir; |
143 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); | 151 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
144 | 152 |
145 // Ensure that the blacklist is loaded. | 153 for (int i = 0; i < arraysize(test_data); ++i) { |
146 ASSERT_TRUE(TestDll_IsBlacklistInitialized()); | 154 // Ensure that the dll has not been loaded both by inspecting the handle |
147 | 155 // returned by LoadLibrary and by looking for an environment variable that |
148 // Test that an un-blacklisted DLL can load correctly. | 156 // is set when the DLL's entry point is called. |
149 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); | |
150 EXPECT_TRUE(dll1.is_valid()); | |
151 dll1.Reset(NULL); | |
152 | |
153 int num_blocked_dlls = 0; | |
154 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); | |
155 EXPECT_EQ(0, num_blocked_dlls); | |
156 | |
157 struct TestData { | |
158 const wchar_t* dll_name; | |
159 const wchar_t* dll_beacon; | |
160 } test_data[] = { | |
161 { kTestDllName2, kDll2Beacon }, | |
162 { kTestDllName3, kDll3Beacon } | |
163 }; | |
164 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 // 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 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); | |
169 base::ScopedNativeLibrary dll_blacklisted( | 157 base::ScopedNativeLibrary dll_blacklisted( |
170 current_dir.Append(test_data[i].dll_name)); | 158 current_dir.Append(test_data[i].dll_name)); |
171 EXPECT_FALSE(dll_blacklisted.is_valid()); | 159 EXPECT_FALSE(dll_blacklisted.is_valid()); |
172 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | 160 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
173 dll_blacklisted.Reset(NULL); | 161 dll_blacklisted.Reset(NULL); |
174 | 162 |
175 // Ensure that the dll is recorded as blocked. | 163 // Ensure that the dll is recorded as blocked. |
176 int array_size = 1; | 164 int array_size = 1; |
177 const wchar_t* blocked_dll = NULL; | 165 const wchar_t* blocked_dll = NULL; |
178 TestDll_SuccessfullyBlocked(&blocked_dll, &array_size); | 166 TestDll_SuccessfullyBlocked(&blocked_dll, &array_size); |
(...skipping 17 matching lines...) Expand all Loading... |
196 base::ScopedNativeLibrary dll_blacklisted_different_case( | 184 base::ScopedNativeLibrary dll_blacklisted_different_case( |
197 current_dir.Append(test_data[i].dll_name)); | 185 current_dir.Append(test_data[i].dll_name)); |
198 EXPECT_FALSE(dll_blacklisted_different_case.is_valid()); | 186 EXPECT_FALSE(dll_blacklisted_different_case.is_valid()); |
199 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | 187 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
200 dll_blacklisted_different_case.Reset(NULL); | 188 dll_blacklisted_different_case.Reset(NULL); |
201 | 189 |
202 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); | 190 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); |
203 | 191 |
204 // The blocked dll was removed, so we shouldn't get anything returned | 192 // The blocked dll was removed, so we shouldn't get anything returned |
205 // here. | 193 // here. |
| 194 int num_blocked_dlls = 0; |
206 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); | 195 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
207 EXPECT_EQ(0, num_blocked_dlls); | 196 EXPECT_EQ(0, num_blocked_dlls); |
208 } | 197 } |
209 } | 198 } |
| 199 |
| 200 TEST_F(BlacklistTest, LoadBlacklistedLibrary) { |
| 201 base::FilePath current_dir; |
| 202 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
| 203 |
| 204 // Ensure that the blacklist is loaded. |
| 205 ASSERT_TRUE(TestDll_IsBlacklistInitialized()); |
| 206 |
| 207 // Test that an un-blacklisted DLL can load correctly. |
| 208 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); |
| 209 EXPECT_TRUE(dll1.is_valid()); |
| 210 dll1.Reset(NULL); |
| 211 |
| 212 int num_blocked_dlls = 0; |
| 213 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
| 214 EXPECT_EQ(0, num_blocked_dlls); |
| 215 |
| 216 // Add all DLLs to the blacklist then check they are blocked. |
| 217 for (int i = 0; i < arraysize(test_data); ++i) { |
| 218 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); |
| 219 } |
| 220 CheckBlacklistedDllsNotLoaded(); |
| 221 } |
| 222 |
| 223 TEST_F(BlacklistTest, AddDllsFromRegistryToBlacklist) { |
| 224 // Ensure that the blacklist is loaded. |
| 225 ASSERT_TRUE(TestDll_IsBlacklistInitialized()); |
| 226 |
| 227 // Delete the finch registry key to clear its values. |
| 228 base::win::RegKey key(HKEY_CURRENT_USER, |
| 229 blacklist::kRegistryFinchListPath, |
| 230 KEY_QUERY_VALUE | KEY_SET_VALUE); |
| 231 key.DeleteKey(L""); |
| 232 |
| 233 // Add the test dlls to the registry (with their name as both key and value). |
| 234 base::win::RegKey finch_blacklist_registry_key( |
| 235 HKEY_CURRENT_USER, |
| 236 blacklist::kRegistryFinchListPath, |
| 237 KEY_QUERY_VALUE | KEY_SET_VALUE); |
| 238 for (int i = 0; i < arraysize(test_data); ++i) { |
| 239 finch_blacklist_registry_key.WriteValue(test_data[i].dll_name, |
| 240 test_data[i].dll_name); |
| 241 } |
| 242 |
| 243 EXPECT_TRUE(TestDll_AddDllsFromRegistryToBlacklist()); |
| 244 CheckBlacklistedDllsNotLoaded(); |
| 245 } |
| 246 |
| 247 } // namespace |
OLD | NEW |