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/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/scoped_native_library.h" | 9 #include "base/scoped_native_library.h" |
10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/test/test_reg_util_win.h" | 13 #include "base/test/test_reg_util_win.h" |
14 #include "chrome_elf/blacklist/blacklist.h" | 14 #include "chrome_elf/blacklist/blacklist.h" |
15 #include "chrome_elf/blacklist/test/blacklist_test_main_dll.h" | 15 #include "chrome_elf/blacklist/test/blacklist_test_main_dll.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "version.h" // NOLINT |
17 | 18 |
18 const wchar_t kTestDllName1[] = L"blacklist_test_dll_1.dll"; | 19 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 kTestDllName2[] = L"blacklist_test_dll_2.dll"; |
20 const wchar_t kTestDllName3[] = L"blacklist_test_dll_3.dll"; | 21 const wchar_t kTestDllName3[] = L"blacklist_test_dll_3.dll"; |
21 | 22 |
22 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; | 23 const wchar_t kDll2Beacon[] = L"{F70A0100-2889-4629-9B44-610FE5C73231}"; |
23 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; | 24 const wchar_t kDll3Beacon[] = L"{9E056AEC-169E-400c-B2D0-5A07E3ACE2EB}"; |
24 | 25 |
25 extern const wchar_t* kEnvVars[]; | 26 extern const wchar_t* kEnvVars[]; |
26 | 27 |
27 extern "C" { | 28 extern "C" { |
28 // When modifying the blacklist in the test process, use the exported test dll | 29 // 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 // functions on the test blacklist dll, not the ones linked into the test |
30 // executable itself. | 31 // executable itself. |
31 __declspec(dllimport) void TestDll_AddDllToBlacklist(const wchar_t* dll_name); | 32 __declspec(dllimport) void TestDll_AddDllToBlacklist(const wchar_t* dll_name); |
32 __declspec(dllimport) void TestDll_RemoveDllFromBlacklist( | 33 __declspec(dllimport) void TestDll_RemoveDllFromBlacklist( |
33 const wchar_t* dll_name); | 34 const wchar_t* dll_name); |
34 } | 35 } |
35 | 36 |
36 class BlacklistTest : public testing::Test { | 37 class BlacklistTest : public testing::Test { |
37 virtual void SetUp() { | 38 virtual void SetUp() { |
38 // Force an import from blacklist_test_main_dll. | 39 // Force an import from blacklist_test_main_dll. |
39 InitBlacklistTestDll(); | 40 InitBlacklistTestDll(); |
40 | |
41 // Ensure that the beacon state starts off cleared. | |
42 blacklist::ClearBeacon(); | |
43 } | 41 } |
44 | 42 |
45 virtual void TearDown() { | 43 virtual void TearDown() { |
46 TestDll_RemoveDllFromBlacklist(kTestDllName1); | 44 TestDll_RemoveDllFromBlacklist(kTestDllName1); |
47 TestDll_RemoveDllFromBlacklist(kTestDllName2); | 45 TestDll_RemoveDllFromBlacklist(kTestDllName2); |
48 } | 46 } |
49 }; | 47 }; |
50 | 48 |
51 TEST_F(BlacklistTest, Beacon) { | 49 TEST_F(BlacklistTest, Beacon) { |
52 registry_util::RegistryOverrideManager override_manager; | 50 registry_util::RegistryOverrideManager override_manager; |
53 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); | 51 override_manager.OverrideRegistry(HKEY_CURRENT_USER, L"beacon_test"); |
54 | 52 |
55 // First call should succeed as the beacon is newly created. | 53 // Ensure that the beacon state starts off enabled for this version. |
56 EXPECT_TRUE(blacklist::CreateBeacon()); | 54 DWORD blacklist_state = blacklist::BLACKLIST_ENABLED; |
| 55 LONG result = ::RegSetKeyValue(HKEY_CURRENT_USER, |
| 56 blacklist::kRegistryBeaconPath, |
| 57 blacklist::kBeaconState, |
| 58 REG_DWORD, |
| 59 &blacklist_state, |
| 60 sizeof(blacklist_state)); |
| 61 ASSERT_EQ(result, ERROR_SUCCESS); |
57 | 62 |
58 // Second call should fail indicating the beacon already existed. | 63 result = ::RegSetKeyValue(HKEY_CURRENT_USER, |
59 EXPECT_FALSE(blacklist::CreateBeacon()); | 64 blacklist::kRegistryBeaconPath, |
| 65 blacklist::kBeaconVersion, |
| 66 REG_SZ, |
| 67 TEXT(CHROME_VERSION_STRING), |
| 68 sizeof(TEXT(CHROME_VERSION_STRING))); |
| 69 ASSERT_EQ(result, ERROR_SUCCESS); |
60 | 70 |
61 // First call should find the beacon and delete it. | 71 // First call should find the beacon and reset it. |
62 EXPECT_TRUE(blacklist::ClearBeacon()); | 72 EXPECT_TRUE(blacklist::ResetBeacon()); |
63 | 73 |
64 // Second call should fail to find the beacon and delete it. | 74 // First call should succeed as the beacon is enabled. |
65 EXPECT_FALSE(blacklist::ClearBeacon()); | 75 EXPECT_TRUE(blacklist::LeaveSetupBeacon()); |
| 76 |
| 77 // Second call should fail indicating the beacon wasn't set as enabled. |
| 78 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); |
| 79 |
| 80 // Resetting the beacon should work when setup beacon is present. |
| 81 EXPECT_TRUE(blacklist::ResetBeacon()); |
| 82 |
| 83 // Change the version and ensure that the setup fails due to the version |
| 84 // mismatch. |
| 85 base::string16 different_version(L"other_version"); |
| 86 ASSERT_NE(different_version, TEXT(CHROME_VERSION_STRING)); |
| 87 |
| 88 result = ::RegSetKeyValue(HKEY_CURRENT_USER, |
| 89 blacklist::kRegistryBeaconPath, |
| 90 blacklist::kBeaconVersion, |
| 91 REG_SZ, |
| 92 different_version.c_str(), |
| 93 sizeof(different_version)); |
| 94 ASSERT_EQ(result, ERROR_SUCCESS); |
| 95 |
| 96 EXPECT_FALSE(blacklist::LeaveSetupBeacon()); |
66 } | 97 } |
67 | 98 |
68 TEST_F(BlacklistTest, AddAndRemoveModules) { | 99 TEST_F(BlacklistTest, AddAndRemoveModules) { |
69 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); | 100 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
70 // Adding the same item twice should be idempotent. | 101 // Adding the same item twice should be idempotent. |
71 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); | 102 EXPECT_TRUE(blacklist::AddDllToBlacklist(L"foo.dll")); |
72 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); | 103 EXPECT_TRUE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
73 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); | 104 EXPECT_FALSE(blacklist::RemoveDllFromBlacklist(L"foo.dll")); |
74 | 105 |
75 std::vector<base::string16> added_dlls; | 106 std::vector<base::string16> added_dlls; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 // Remove the DLL from the blacklist. Ensure that it loads and that its | 149 // Remove the DLL from the blacklist. Ensure that it loads and that its |
119 // entry point was called. | 150 // entry point was called. |
120 TestDll_RemoveDllFromBlacklist(test_data[i].dll_name); | 151 TestDll_RemoveDllFromBlacklist(test_data[i].dll_name); |
121 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); | 152 base::ScopedNativeLibrary dll(current_dir.Append(test_data[i].dll_name)); |
122 EXPECT_TRUE(dll.is_valid()); | 153 EXPECT_TRUE(dll.is_valid()); |
123 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); | 154 EXPECT_NE(0u, ::GetEnvironmentVariable(test_data[i].dll_beacon, NULL, 0)); |
124 dll.Reset(NULL); | 155 dll.Reset(NULL); |
125 } | 156 } |
126 #endif | 157 #endif |
127 } | 158 } |
OLD | NEW |