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