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 base::FilePath current_dir; | |
91 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); | |
92 | |
93 // Test that an un-blacklisted DLL can load correctly. | |
94 base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName1)); | |
95 EXPECT_TRUE(dll1.is_valid()); | |
96 dll1.Reset(NULL); | |
97 | |
98 struct TestData { | |
99 const wchar_t* dll_name; | |
100 const wchar_t* dll_beacon; | |
101 } test_data[] = { | |
102 { kTestDllName2, kDll2Beacon }, | |
103 { kTestDllName3, kDll3Beacon } | |
104 }; | |
105 for (int i = 0 ; i < arraysize(test_data); ++i) { | |
106 // Add the DLL to the blacklist, ensure that it is not loaded both by | |
107 // inspecting the handle returned by LoadLibrary and by looking for an | |
108 // environment variable that is set when the DLL's entry point is called. | |
109 TestDll_AddDllToBlacklist(test_data[i].dll_name); | |
110 base::ScopedNativeLibrary dll_blacklisted( | |
111 current_dir.Append(test_data[i].dll_name)); | |
112 EXPECT_FALSE(dll_blacklisted.is_valid()); | |
113 EXPECT_EQ(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | |
114 dll_blacklisted.Reset(NULL); | |
115 | |
116 // Remove the DLL from the blacklist. Ensure that it loads and that its | |
117 // entry point was called. | |
118 TestDll_RemoveDllFromBlacklist(test_data[i].dll_name); | |
119 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); | |
120 EXPECT_TRUE(dll.is_valid()); | |
121 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | |
122 dll.Reset(NULL); | |
123 } | |
124 } | |
OLD | NEW |