OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/environment.h" |
| 6 #include "base/files/file_path.h" |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/scoped_native_library.h" |
| 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/test/test_reg_util_win.h" |
| 14 #include "chrome_elf/blacklist/blacklist.h" |
| 15 #include "chrome_elf/blacklist/test/blacklist_test_main_dll.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 const wchar_t kTestDllName1[] = L"blacklist_test_dll_1.dll"; |
| 19 const wchar_t kTestDllName2[] = L"blacklist_test_dll_2.dll"; |
| 20 const wchar_t kTestDllName3[] = L"blacklist_test_dll_3.dll"; |
| 21 |
| 22 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; |
| 23 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; |
| 24 |
| 25 extern const wchar_t* kEnvVars[]; |
| 26 |
| 27 extern "C" { |
| 28 // When modifying the blacklist in the test process, use the exported test dll |
| 29 // functions on the test blacklist dll, not the ones linked into the test |
| 30 // executable itself. |
| 31 __declspec(dllimport) void TestDll_AddDllToBlacklist(const wchar_t* dll_name); |
| 32 __declspec(dllimport) void TestDll_RemoveDllFromBlacklist( |
| 33 const wchar_t* dll_name); |
| 34 } |
| 35 |
| 36 class BlacklistTest : public testing::Test { |
| 37 virtual void SetUp() { |
| 38 // Force an import from blacklist_test_main_dll. |
| 39 InitBlacklistTestDll(); |
| 40 |
| 41 // Ensure that the beacon state starts off cleared. |
| 42 blacklist::ClearBeacon(); |
| 43 } |
| 44 |
| 45 virtual void TearDown() { |
| 46 TestDll_RemoveDllFromBlacklist(kTestDllName1); |
| 47 TestDll_RemoveDllFromBlacklist(kTestDllName2); |
| 48 } |
| 49 }; |
| 50 |
| 51 TEST_F(BlacklistTest, Beacon) { |
| 52 registry_util::RegistryOverrideManager override_manager; |
| 53 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); |
| 54 |
| 55 // First call should succeed as the beacon is newly created. |
| 56 EXPECT_TRUE(blacklist::CreateBeacon()); |
| 57 |
| 58 // Second call should fail indicating the beacon already existed. |
| 59 EXPECT_FALSE(blacklist::CreateBeacon()); |
| 60 |
| 61 // First call should find the beacon and delete it. |
| 62 EXPECT_TRUE(blacklist::ClearBeacon()); |
| 63 |
| 64 // Second call should fail to find the beacon and delete it. |
| 65 EXPECT_FALSE(blacklist::ClearBeacon()); |
| 66 } |
| 67 |
| 68 TEST_F(BlacklistTest, AddAndRemoveModules) { |
| 69 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
| 70 // Adding the same item twice should be idempotent. |
| 71 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
| 72 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
| 73 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
| 74 |
| 75 std::vector<string16> added_dlls; |
| 76 added_dlls.reserve(blacklist::kTroublesomeDllsMaxCount); |
| 77 for (int i = 0; i < blacklist::kTroublesomeDllsMaxCount; ++i) { |
| 78 added_dlls.push_back(base::IntToString16(i) + L".dll"); |
| 79 EXPECT_TRUE(blacklist::AddDllToBlacklist(added_dlls[i].c_str())) << i; |
| 80 } |
| 81 EXPECT_FALSE(blacklist::AddDllToBlacklist(L"overflow.dll")); |
| 82 for (int i = 0; i < blacklist::kTroublesomeDllsMaxCount; ++i) { |
| 83 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(added_dlls[i].c_str())) << i; |
| 84 } |
| 85 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"0.dll")); |
| 86 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"63.dll")); |
| 87 } |
| 88 |
| 89 TEST_F(BlacklistTest, LoadBlacklistedLibrary) { |
| 90 // TODO(robertshield): Add 64-bit support. |
| 91 #if !defined(_WIN64) |
| 92 base::FilePath current_dir; |
| 93 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
| 94 |
| 95 // Test that an un-blacklisted DLL can load correctly. |
| 96 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); |
| 97 EXPECT_TRUE(dll1.is_valid()); |
| 98 dll1.Reset(NULL); |
| 99 |
| 100 struct TestData { |
| 101 const wchar_t* dll_name; |
| 102 const wchar_t* dll_beacon; |
| 103 } test_data[] = { |
| 104 { kTestDllName2, kDll2Beacon }, |
| 105 { kTestDllName3, kDll3Beacon } |
| 106 }; |
| 107 for (int i = 0 ; i < arraysize(test_data); ++i) { |
| 108 // Add the DLL to the blacklist, ensure that it is not loaded both by |
| 109 // inspecting the handle returned by LoadLibrary and by looking for an |
| 110 // environment variable that is set when the DLL's entry point is called. |
| 111 TestDll_AddDllToBlacklist(test_data[i].dll_name); |
| 112 base::ScopedNativeLibrary dll_blacklisted( |
| 113 current_dir.Append(test_data[i].dll_name)); |
| 114 EXPECT_FALSE(dll_blacklisted.is_valid()); |
| 115 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
| 116 dll_blacklisted.Reset(NULL); |
| 117 |
| 118 // Remove the DLL from the blacklist. Ensure that it loads and that its |
| 119 // entry point was called. |
| 120 TestDll_RemoveDllFromBlacklist(test_data[i].dll_name); |
| 121 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); |
| 122 EXPECT_TRUE(dll.is_valid()); |
| 123 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
| 124 dll.Reset(NULL); |
| 125 } |
| 126 #endif |
| 127 } |
OLD | NEW |