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/path_service.h" | 9 #include "base/path_service.h" |
| 9 #include "base/scoped_native_library.h" | 10 #include "base/scoped_native_library.h" |
| 10 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/test/test_reg_util_win.h" | 14 #include "base/test/test_reg_util_win.h" |
| 14 #include "base/win/registry.h" | 15 #include "base/win/registry.h" |
| 15 #include "chrome_elf/blacklist/blacklist.h" | 16 #include "chrome_elf/blacklist/blacklist.h" |
| 16 #include "chrome_elf/blacklist/test/blacklist_test_main_dll.h" | 17 #include "chrome_elf/blacklist/test/blacklist_test_main_dll.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "version.h" // NOLINT | 19 #include "version.h" // NOLINT |
| 19 | 20 |
| 20 const wchar_t kTestDllName1[] = L"blacklist_test_dll_1.dll"; | 21 const wchar_t kTestDllName1[] = L"blacklist_test_dll_1.dll"; |
| 21 const wchar_t kTestDllName2[] = L"blacklist_test_dll_2.dll"; | 22 const wchar_t kTestDllName2[] = L"blacklist_test_dll_2.dll"; |
| 22 const wchar_t kTestDllName3[] = L"blacklist_test_dll_3.dll"; | 23 const wchar_t kTestDllName3[] = L"blacklist_test_dll_3.dll"; |
| 23 | 24 |
| 24 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; | 25 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; |
| 25 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; | 26 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; |
| 26 | 27 |
| 27 extern const wchar_t* kEnvVars[]; | 28 extern const wchar_t* kEnvVars[]; |
| 28 | 29 |
| 29 extern "C" { | 30 extern "C" { |
| 30 // 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 |
| 31 // 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 |
| 32 // executable itself. | 33 // executable itself. |
| 33 __declspec(dllimport) void TestDll_AddDllToBlacklist(const wchar_t* dll_name); | 34 __declspec(dllimport) bool TestDll_AddDllToBlacklist(const wchar_t* dll_name); |
| 34 __declspec(dllimport) void TestDll_RemoveDllFromBlacklist( | 35 __declspec(dllimport) bool TestDll_RemoveDllFromBlacklist( |
| 35 const wchar_t* dll_name); | 36 const wchar_t* dll_name); |
| 36 } | 37 } |
| 37 | 38 |
| 38 class BlacklistTest : public testing::Test { | 39 class BlacklistTest : public testing::Test { |
| 39 virtual void SetUp() { | 40 virtual void SetUp() { |
| 40 // Force an import from blacklist_test_main_dll. | 41 // Force an import from blacklist_test_main_dll. |
| 41 InitBlacklistTestDll(); | 42 InitBlacklistTestDll(); |
| 42 } | 43 } |
| 43 | 44 |
| 44 virtual void TearDown() { | 45 virtual void TearDown() { |
| 45 TestDll_RemoveDllFromBlacklist(kTestDllName1); | 46 TestDll_RemoveDllFromBlacklist(kTestDllName1); |
| 46 TestDll_RemoveDllFromBlacklist(kTestDllName2); | 47 TestDll_RemoveDllFromBlacklist(kTestDllName2); |
| 48 TestDll_RemoveDllFromBlacklist(kTestDllName3); | |
| 47 } | 49 } |
| 48 }; | 50 }; |
| 49 | 51 |
| 50 TEST_F(BlacklistTest, Beacon) { | 52 TEST_F(BlacklistTest, Beacon) { |
| 51 registry_util::RegistryOverrideManager override_manager; | 53 registry_util::RegistryOverrideManager override_manager; |
| 52 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); | 54 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); |
| 53 | 55 |
| 54 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, | 56 base::win::RegKey blacklist_registry_key(HKEY_CURRENT_USER, |
| 55 blacklist::kRegistryBeaconPath, | 57 blacklist::kRegistryBeaconPath, |
| 56 KEY_QUERY_VALUE | KEY_SET_VALUE); | 58 KEY_QUERY_VALUE | KEY_SET_VALUE); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); | 90 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); |
| 89 } | 91 } |
| 90 | 92 |
| 91 TEST_F(BlacklistTest, AddAndRemoveModules) { | 93 TEST_F(BlacklistTest, AddAndRemoveModules) { |
| 92 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); | 94 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
| 93 // Adding the same item twice should be idempotent. | 95 // Adding the same item twice should be idempotent. |
| 94 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); | 96 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
| 95 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); | 97 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
| 96 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); | 98 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
| 97 | 99 |
| 100 // Increase the blacklist size by 1 to include the NULL pointer | |
| 101 // that marks the end. | |
| 102 int empty_spaces = blacklist::kTroublesomeDllsMaxCount - ( | |
| 103 blacklist::BlacklistSize() + 1); | |
| 98 std::vector<base::string16> added_dlls; | 104 std::vector<base::string16> added_dlls; |
| 99 added_dlls.reserve(blacklist::kTroublesomeDllsMaxCount); | 105 added_dlls.reserve(empty_spaces); |
| 100 for (int i = 0; i < blacklist::kTroublesomeDllsMaxCount; ++i) { | 106 for (int i = 0; i < empty_spaces; ++i) { |
| 101 added_dlls.push_back(base::IntToString16(i) + L".dll"); | 107 added_dlls.push_back(base::IntToString16(i) + L".dll"); |
| 102 EXPECT_TRUE(blacklist::AddDllToBlacklist(added_dlls[i].c_str())) << i; | 108 EXPECT_TRUE(blacklist::AddDllToBlacklist(added_dlls[i].c_str())) << i; |
| 103 } | 109 } |
| 104 EXPECT_FALSE(blacklist::AddDllToBlacklist(L"overflow.dll")); | 110 EXPECT_FALSE(blacklist::AddDllToBlacklist(L"overflow.dll")); |
| 105 for (int i = 0; i < blacklist::kTroublesomeDllsMaxCount; ++i) { | 111 for (int i = 0; i < empty_spaces; ++i) { |
| 106 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(added_dlls[i].c_str())) << i; | 112 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(added_dlls[i].c_str())) << i; |
| 107 } | 113 } |
| 108 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"0.dll")); | 114 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(added_dlls[0].c_str())); |
| 109 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"63.dll")); | 115 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist( |
| 116 added_dlls[empty_spaces - 1].c_str())); | |
| 110 } | 117 } |
| 111 | 118 |
| 112 TEST_F(BlacklistTest, LoadBlacklistedLibrary) { | 119 TEST_F(BlacklistTest, LoadBlacklistedLibrary) { |
| 113 // TODO(robertshield): Add 64-bit support. | 120 // TODO(robertshield): Add 64-bit support. |
| 114 #if !defined(_WIN64) | 121 #if !defined(_WIN64) |
| 115 base::FilePath current_dir; | 122 base::FilePath current_dir; |
| 116 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); | 123 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
| 117 | 124 |
| 118 // Test that an un-blacklisted DLL can load correctly. | 125 // Test that an un-blacklisted DLL can load correctly. |
| 119 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); | 126 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); |
| 120 EXPECT_TRUE(dll1.is_valid()); | 127 EXPECT_TRUE(dll1.is_valid()); |
| 121 dll1.Reset(NULL); | 128 dll1.Reset(NULL); |
| 122 | 129 |
| 123 struct TestData { | 130 struct TestData { |
| 124 const wchar_t* dll_name; | 131 const wchar_t* dll_name; |
| 125 const wchar_t* dll_beacon; | 132 const wchar_t* dll_beacon; |
| 126 } test_data[] = { | 133 } test_data[] = { |
| 127 { kTestDllName2, kDll2Beacon }, | 134 { kTestDllName2, kDll2Beacon }, |
| 128 { kTestDllName3, kDll3Beacon } | 135 { kTestDllName3, kDll3Beacon } |
| 129 }; | 136 }; |
| 130 for (int i = 0 ; i < arraysize(test_data); ++i) { | 137 for (int i = 0 ; i < arraysize(test_data); ++i) { |
| 131 // Add the DLL to the blacklist, ensure that it is not loaded both by | 138 // Add the DLL to the blacklist, ensure that it is not loaded both by |
| 132 // inspecting the handle returned by LoadLibrary and by looking for an | 139 // inspecting the handle returned by LoadLibrary and by looking for an |
| 133 // environment variable that is set when the DLL's entry point is called. | 140 // environment variable that is set when the DLL's entry point is called. |
| 134 TestDll_AddDllToBlacklist(test_data[i].dll_name); | 141 EXPECT_TRUE(TestDll_AddDllToBlacklist(test_data[i].dll_name)); |
| 135 base::ScopedNativeLibrary dll_blacklisted( | 142 base::ScopedNativeLibrary dll_blacklisted( |
| 136 current_dir.Append(test_data[i].dll_name)); | 143 current_dir.Append(test_data[i].dll_name)); |
| 137 EXPECT_FALSE(dll_blacklisted.is_valid()); | 144 EXPECT_FALSE(dll_blacklisted.is_valid()); |
| 138 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | 145 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
| 139 dll_blacklisted.Reset(NULL); | 146 dll_blacklisted.Reset(NULL); |
| 140 | 147 |
| 141 // Remove the DLL from the blacklist. Ensure that it loads and that its | 148 // Remove the DLL from the blacklist. Ensure that it loads and that its |
| 142 // entry point was called. | 149 // entry point was called. |
| 143 TestDll_RemoveDllFromBlacklist(test_data[i].dll_name); | 150 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(test_data[i].dll_name)); |
| 144 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); | 151 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); |
| 145 EXPECT_TRUE(dll.is_valid()); | 152 EXPECT_TRUE(dll.is_valid()); |
| 146 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | 153 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
| 147 dll.Reset(NULL); | 154 dll.Reset(NULL); |
| 155 | |
| 156 ::SetEnvironmentVariable(test_data[i].dll_beacon, NULL); | |
| 157 | |
| 158 // Ensure that the dll won't load even if the name has different | |
| 159 // capitalization. | |
| 160 base::string16 uppercase_name = base::i18n::ToUpper(test_data[i].dll_name); | |
| 161 EXPECT_TRUE(TestDll_AddDllToBlacklist(uppercase_name.c_str())); | |
|
robertshield
2014/01/24 01:50:24
shouldn't only one of the name in the blacklist an
csharp
2014/01/24 16:22:59
Done.
| |
| 162 base::ScopedNativeLibrary dll_blacklisted_different_case( | |
| 163 current_dir.Append(uppercase_name.c_str())); | |
| 164 EXPECT_FALSE(dll_blacklisted_different_case.is_valid()); | |
| 165 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | |
| 166 dll_blacklisted_different_case.Reset(NULL); | |
| 167 | |
| 168 EXPECT_TRUE(TestDll_RemoveDllFromBlacklist(uppercase_name.c_str())); | |
| 148 } | 169 } |
| 149 #endif | 170 #endif |
| 150 } | 171 } |
| OLD | NEW |