| 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 15 matching lines...) Expand all Loading... |
| 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_AddDllToBlacklist(const wchar_t* dll_name); | 35 __declspec(dllimport) bool TestDll_AddDllToBlacklist(const wchar_t* dll_name); |
| 36 __declspec(dllimport) bool TestDLL_IsBlacklistInitialized(); | 36 __declspec(dllimport) bool TestDll_IsBlacklistInitialized(); |
| 37 __declspec(dllimport) bool TestDll_RemoveDllFromBlacklist( | 37 __declspec(dllimport) bool TestDll_RemoveDllFromBlacklist( |
| 38 const wchar_t* dll_name); | 38 const wchar_t* dll_name); |
| 39 __declspec(dllimport) bool TestDll_SuccessfullyBlocked( |
| 40 const wchar_t** blocked_dlls, |
| 41 int* size); |
| 39 } | 42 } |
| 40 | 43 |
| 41 class BlacklistTest : public testing::Test { | 44 class BlacklistTest : public testing::Test { |
| 42 virtual void SetUp() { | 45 virtual void SetUp() { |
| 43 // Force an import from blacklist_test_main_dll. | 46 // Force an import from blacklist_test_main_dll. |
| 44 InitBlacklistTestDll(); | 47 InitBlacklistTestDll(); |
| 45 } | 48 } |
| 46 | 49 |
| 47 virtual void TearDown() { | 50 virtual void TearDown() { |
| 48 TestDll_RemoveDllFromBlacklist(kTestDllName1); | 51 TestDll_RemoveDllFromBlacklist(kTestDllName1); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 } | 114 } |
| 112 EXPECT_FALSE(blacklist::AddDllToBlacklist(L"overflow.dll")); | 115 EXPECT_FALSE(blacklist::AddDllToBlacklist(L"overflow.dll")); |
| 113 for (int i = 0; i < empty_spaces; ++i) { | 116 for (int i = 0; i < empty_spaces; ++i) { |
| 114 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(added_dlls[i].c_str())) << i; | 117 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(added_dlls[i].c_str())) << i; |
| 115 } | 118 } |
| 116 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(added_dlls[0].c_str())); | 119 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(added_dlls[0].c_str())); |
| 117 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist( | 120 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist( |
| 118 added_dlls[empty_spaces - 1].c_str())); | 121 added_dlls[empty_spaces - 1].c_str())); |
| 119 } | 122 } |
| 120 | 123 |
| 124 TEST_F(BlacklistTest, SuccessfullyBlocked) { |
| 125 // Ensure that we have at least 5 dlls to blacklist. |
| 126 int blacklist_size = blacklist::BlacklistSize(); |
| 127 const int kDesiredBlacklistSize = 5; |
| 128 for (int i = blacklist_size; i < kDesiredBlacklistSize; ++i) { |
| 129 base::string16 new_dll_name(base::IntToString16(i) + L".dll"); |
| 130 EXPECT_TRUE(blacklist::AddDllToBlacklist(new_dll_name.c_str())); |
| 131 } |
| 132 |
| 133 // Block 5 dlls, one at a time, starting from the end of the list, and |
| 134 // ensuring SuccesfullyBlocked correctly passes the list of blocked dlls. |
| 135 for (int i = 0; i < kDesiredBlacklistSize; ++i) { |
| 136 blacklist::BlockedDll(i); |
| 137 |
| 138 int size = 0; |
| 139 blacklist::SuccessfullyBlocked(NULL, &size); |
| 140 EXPECT_EQ(i + 1, size); |
| 141 |
| 142 std::vector<const wchar_t*> blocked_dlls(size); |
| 143 blacklist::SuccessfullyBlocked(&(blocked_dlls[0]), &size); |
| 144 EXPECT_EQ(i + 1, size); |
| 145 |
| 146 for (size_t j = 0; j < blocked_dlls.size(); ++j) { |
| 147 EXPECT_EQ(blocked_dlls[j], blacklist::g_troublesome_dlls[j]); |
| 148 } |
| 149 } |
| 150 } |
| 151 |
| 121 TEST_F(BlacklistTest, LoadBlacklistedLibrary) { | 152 TEST_F(BlacklistTest, LoadBlacklistedLibrary) { |
| 122 base::FilePath current_dir; | 153 base::FilePath current_dir; |
| 123 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); | 154 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
| 124 | 155 |
| 125 // Ensure that the blacklist is loaded. | 156 // Ensure that the blacklist is loaded. |
| 126 ASSERT_TRUE(TestDLL_IsBlacklistInitialized()); | 157 ASSERT_TRUE(TestDll_IsBlacklistInitialized()); |
| 127 | 158 |
| 128 // Test that an un-blacklisted DLL can load correctly. | 159 // Test that an un-blacklisted DLL can load correctly. |
| 129 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); | 160 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); |
| 130 EXPECT_TRUE(dll1.is_valid()); | 161 EXPECT_TRUE(dll1.is_valid()); |
| 131 dll1.Reset(NULL); | 162 dll1.Reset(NULL); |
| 132 | 163 |
| 164 int num_blocked_dlls = 0; |
| 165 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
| 166 EXPECT_EQ(0, num_blocked_dlls); |
| 167 |
| 133 struct TestData { | 168 struct TestData { |
| 134 const wchar_t* dll_name; | 169 const wchar_t* dll_name; |
| 135 const wchar_t* dll_beacon; | 170 const wchar_t* dll_beacon; |
| 136 } test_data[] = { | 171 } test_data[] = { |
| 137 { kTestDllName2, kDll2Beacon }, | 172 { kTestDllName2, kDll2Beacon }, |
| 138 { kTestDllName3, kDll3Beacon } | 173 { kTestDllName3, kDll3Beacon } |
| 139 }; | 174 }; |
| 140 for (int i = 0 ; i < arraysize(test_data); ++i) { | 175 for (int i = 0 ; i < arraysize(test_data); ++i) { |
| 141 // Add the DLL to the blacklist, ensure that it is not loaded both by | 176 // Add the DLL to the blacklist, ensure that it is not loaded both by |
| 142 // inspecting the handle returned by LoadLibrary and by looking for an | 177 // inspecting the handle returned by LoadLibrary and by looking for an |
| 143 // environment variable that is set when the DLL's entry point is called. | 178 // environment variable that is set when the DLL's entry point is called. |
| 144 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); | 179 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); |
| 145 base::ScopedNativeLibrary dll_blacklisted( | 180 base::ScopedNativeLibrary dll_blacklisted( |
| 146 current_dir.Append(test_data[i].dll_name)); | 181 current_dir.Append(test_data[i].dll_name)); |
| 147 EXPECT_FALSE(dll_blacklisted.is_valid()); | 182 EXPECT_FALSE(dll_blacklisted.is_valid()); |
| 148 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | 183 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
| 149 dll_blacklisted.Reset(NULL); | 184 dll_blacklisted.Reset(NULL); |
| 150 | 185 |
| 186 // Ensure that the dll is recorded as blocked. |
| 187 int array_size = 1; |
| 188 const wchar_t* blocked_dll = NULL; |
| 189 TestDll_SuccessfullyBlocked(&blocked_dll, &array_size); |
| 190 EXPECT_EQ(1, array_size); |
| 191 EXPECT_EQ(test_data[i].dll_name, base::string16(blocked_dll)); |
| 192 |
| 151 // Remove the DLL from the blacklist. Ensure that it loads and that its | 193 // Remove the DLL from the blacklist. Ensure that it loads and that its |
| 152 // entry point was called. | 194 // entry point was called. |
| 153 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(test_data[i].dll_name)); | 195 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(test_data[i].dll_name)); |
| 154 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); | 196 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); |
| 155 EXPECT_TRUE(dll.is_valid()); | 197 EXPECT_TRUE(dll.is_valid()); |
| 156 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | 198 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
| 157 dll.Reset(NULL); | 199 dll.Reset(NULL); |
| 158 | 200 |
| 159 ::SetEnvironmentVariable(test_data[i].dll_beacon, NULL); | 201 ::SetEnvironmentVariable(test_data[i].dll_beacon, NULL); |
| 160 | 202 |
| 161 // Ensure that the dll won't load even if the name has different | 203 // Ensure that the dll won't load even if the name has different |
| 162 // capitalization. | 204 // capitalization. |
| 163 base::string16 uppercase_name = base::i18n::ToUpper(test_data[i].dll_name); | 205 base::string16 uppercase_name = base::i18n::ToUpper(test_data[i].dll_name); |
| 164 EXPECT_TRUE(TestDll_AddDllToBlacklist(uppercase_name.c_str())); | 206 EXPECT_TRUE(TestDll_AddDllToBlacklist(uppercase_name.c_str())); |
| 165 base::ScopedNativeLibrary dll_blacklisted_different_case( | 207 base::ScopedNativeLibrary dll_blacklisted_different_case( |
| 166 current_dir.Append(test_data[i].dll_name)); | 208 current_dir.Append(test_data[i].dll_name)); |
| 167 EXPECT_FALSE(dll_blacklisted_different_case.is_valid()); | 209 EXPECT_FALSE(dll_blacklisted_different_case.is_valid()); |
| 168 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | 210 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
| 169 dll_blacklisted_different_case.Reset(NULL); | 211 dll_blacklisted_different_case.Reset(NULL); |
| 170 | 212 |
| 171 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); | 213 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); |
| 214 |
| 215 // The blocked dll was removed, so we shouldn't get anything returned |
| 216 // here. |
| 217 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls); |
| 218 EXPECT_EQ(0, num_blocked_dlls); |
| 172 } | 219 } |
| 173 } | 220 } |
| OLD | NEW |