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