Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: chrome_elf/blacklist/test/blacklist_test.cc

Issue 174013007: Add UMA stats to record when DLLs are successfully blocked in the Browser. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Responding to comments Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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,
40 int* size);
38 } 41 }
39 42
40 class BlacklistTest : public testing::Test { 43 class BlacklistTest : public testing::Test {
41 virtual void SetUp() { 44 virtual void SetUp() {
42 // Force an import from blacklist_test_main_dll. 45 // Force an import from blacklist_test_main_dll.
43 InitBlacklistTestDll(); 46 InitBlacklistTestDll();
44 } 47 }
45 48
46 virtual void TearDown() { 49 virtual void TearDown() {
47 TestDll_RemoveDllFromBlacklist(kTestDllName1); 50 TestDll_RemoveDllFromBlacklist(kTestDllName1);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 } 113 }
111 EXPECT_FALSE(blacklist::AddDllToBlacklist(L"overflow.dll")); 114 EXPECT_FALSE(blacklist::AddDllToBlacklist(L"overflow.dll"));
112 for (int i = 0; i < empty_spaces; ++i) { 115 for (int i = 0; i < empty_spaces; ++i) {
113 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(added_dlls[i].c_str())) << i; 116 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(added_dlls[i].c_str())) << i;
114 } 117 }
115 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(added_dlls[0].c_str())); 118 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(added_dlls[0].c_str()));
116 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist( 119 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(
117 added_dlls[empty_spaces - 1].c_str())); 120 added_dlls[empty_spaces - 1].c_str()));
118 } 121 }
119 122
123 TEST_F(BlacklistTest, SuccessfullyBlocked) {
124 // Ensure that we have at least 5 dlls to blacklist.
125 int blacklist_size = blacklist::BlacklistSize();
126 int k_desired_blacklist_size = 5;
Alexei Svitkine (slow) 2014/02/25 15:00:20 Nit: This should probably be: const int kDesiredB
csharp 2014/02/25 15:20:50 Done.
127 for (int i = blacklist_size; i < k_desired_blacklist_size; ++i) {
128 base::string16 new_dll_name(base::IntToString16(i) + L".dll");
129 EXPECT_TRUE(blacklist::AddDllToBlacklist(new_dll_name.c_str()));
130 }
131
132 // Block 5 dlls, one at a time, starting from the end of the list, and
133 // ensuring SuccesfullyBlocked correctly passes the list of blocked dlls.
134 for (int i = 0; i < k_desired_blacklist_size; ++i) {
135 blacklist::BlockedDll(i);
136
137 int size = 0;
138 blacklist::SuccessfullyBlocked(NULL, &size);
139 EXPECT_EQ(i + 1, size);
140
141 std::vector<const wchar_t*> blocked_dlls(size);
142 blacklist::SuccessfullyBlocked(&(blocked_dlls[0]), &size);
143 EXPECT_EQ(i + 1, size);
144
145 for (size_t j = 0; j < blocked_dlls.size(); ++j) {
146 EXPECT_EQ(blocked_dlls[j], blacklist::g_troublesome_dlls[j]);
147 }
148 }
149 }
150
120 TEST_F(BlacklistTest, LoadBlacklistedLibrary) { 151 TEST_F(BlacklistTest, LoadBlacklistedLibrary) {
121 base::FilePath current_dir; 152 base::FilePath current_dir;
122 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &current_dir)); 153 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &current_dir));
123 154
124 // Ensure that the blacklist is loaded. 155 // Ensure that the blacklist is loaded.
125 ASSERT_TRUE(TestDLL_IsBlacklistInitialized()); 156 ASSERT_TRUE(TestDll_IsBlacklistInitialized());
126 157
127 // Test that an un-blacklisted DLL can load correctly. 158 // Test that an un-blacklisted DLL can load correctly.
128 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); 159 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1));
129 EXPECT_TRUE(dll1.is_valid()); 160 EXPECT_TRUE(dll1.is_valid());
130 dll1.Reset(NULL); 161 dll1.Reset(NULL);
131 162
163 int num_blocked_dlls = 0;
164 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls);
165 EXPECT_EQ(0, num_blocked_dlls);
166
132 struct TestData { 167 struct TestData {
133 const wchar_t* dll_name; 168 const wchar_t* dll_name;
134 const wchar_t* dll_beacon; 169 const wchar_t* dll_beacon;
135 } test_data[] = { 170 } test_data[] = {
136 { kTestDllName2, kDll2Beacon }, 171 { kTestDllName2, kDll2Beacon },
137 { kTestDllName3, kDll3Beacon } 172 { kTestDllName3, kDll3Beacon }
138 }; 173 };
139 for (int i = 0 ; i < arraysize(test_data); ++i) { 174 for (int i = 0 ; i < arraysize(test_data); ++i) {
140 // Add the DLL to the blacklist, ensure that it is not loaded both by 175 // 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 176 // 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. 177 // environment variable that is set when the DLL's entry point is called.
143 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); 178 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name));
144 base::ScopedNativeLibrary dll_blacklisted( 179 base::ScopedNativeLibrary dll_blacklisted(
145 current_dir.Append(test_data[i].dll_name)); 180 current_dir.Append(test_data[i].dll_name));
146 EXPECT_FALSE(dll_blacklisted.is_valid()); 181 EXPECT_FALSE(dll_blacklisted.is_valid());
147 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); 182 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0));
148 dll_blacklisted.Reset(NULL); 183 dll_blacklisted.Reset(NULL);
149 184
185 // Ensure that the dll is recorded as blocked.
186 int array_size = 1;
187 const wchar_t* blocked_dll = NULL;
188 TestDll_SuccessfullyBlocked(&blocked_dll, &array_size);
189 EXPECT_EQ(1, array_size);
190 EXPECT_EQ(test_data[i].dll_name, base::string16(blocked_dll));
191
150 // Remove the DLL from the blacklist. Ensure that it loads and that its 192 // Remove the DLL from the blacklist. Ensure that it loads and that its
151 // entry point was called. 193 // entry point was called.
152 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(test_data[i].dll_name)); 194 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(test_data[i].dll_name));
153 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); 195 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name));
154 EXPECT_TRUE(dll.is_valid()); 196 EXPECT_TRUE(dll.is_valid());
155 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); 197 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0));
156 dll.Reset(NULL); 198 dll.Reset(NULL);
157 199
158 ::SetEnvironmentVariable(test_data[i].dll_beacon, NULL); 200 ::SetEnvironmentVariable(test_data[i].dll_beacon, NULL);
159 201
160 // Ensure that the dll won't load even if the name has different 202 // Ensure that the dll won't load even if the name has different
161 // capitalization. 203 // capitalization.
162 base::string16 uppercase_name = base::i18n::ToUpper(test_data[i].dll_name); 204 base::string16 uppercase_name = base::i18n::ToUpper(test_data[i].dll_name);
163 EXPECT_TRUE(TestDll_AddDllToBlacklist(uppercase_name.c_str())); 205 EXPECT_TRUE(TestDll_AddDllToBlacklist(uppercase_name.c_str()));
164 base::ScopedNativeLibrary dll_blacklisted_different_case( 206 base::ScopedNativeLibrary dll_blacklisted_different_case(
165 current_dir.Append(test_data[i].dll_name)); 207 current_dir.Append(test_data[i].dll_name));
166 EXPECT_FALSE(dll_blacklisted_different_case.is_valid()); 208 EXPECT_FALSE(dll_blacklisted_different_case.is_valid());
167 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); 209 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0));
168 dll_blacklisted_different_case.Reset(NULL); 210 dll_blacklisted_different_case.Reset(NULL);
169 211
170 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); 212 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str()));
213
214 // The blocked dll was removed, so we shouldn't get anything returned
215 // here.
216 TestDll_SuccessfullyBlocked(NULL, &num_blocked_dlls);
217 EXPECT_EQ(0, num_blocked_dlls);
171 } 218 }
172 } 219 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698