1 // Copyright 2013 The Chromium Authors
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/test/test_reg_util_win.h"
6
7 #include <memory>
8
9 #include "base/compiler_specific.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/task_environment.h"
14 #include "base/time/time.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace registry_util {
18
19 namespace {
20 const wchar_t kTestKeyPath[] = L"Software\\Chromium\\Foo\\Baz\\TestKey";
21 const wchar_t kTestValueName[] = L"TestValue";
22 } // namespace
23
24 class RegistryOverrideManagerTest : public testing::Test {
25 protected:
RegistryOverrideManagerTest()26 RegistryOverrideManagerTest() {
27 // We assign a fake test key path to our test RegistryOverrideManager
28 // so we don't interfere with any actual RegistryOverrideManagers running
29 // on the system. This fake path will be auto-deleted by other
30 // RegistryOverrideManagers in case we crash.
31 fake_test_key_root_ = registry_util::GenerateTempKeyPath();
32
33 // Ensure a clean test environment.
34 base::win::RegKey key(HKEY_CURRENT_USER);
35 key.DeleteKey(fake_test_key_root_.c_str());
36 key.DeleteKey(kTestKeyPath);
37 }
38
~RegistryOverrideManagerTest()39 ~RegistryOverrideManagerTest() override {
40 base::win::RegKey key(HKEY_CURRENT_USER);
41 key.DeleteKey(fake_test_key_root_.c_str());
42 }
43
AssertKeyExists(const std::wstring & key_path)44 void AssertKeyExists(const std::wstring& key_path) {
45 base::win::RegKey key;
46 ASSERT_EQ(ERROR_SUCCESS,
47 key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ))
48 << key_path << " does not exist.";
49 }
50
AssertKeyAbsent(const std::wstring & key_path)51 void AssertKeyAbsent(const std::wstring& key_path) {
52 base::win::RegKey key;
53 ASSERT_NE(ERROR_SUCCESS,
54 key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ))
55 << key_path << " exists but it should not.";
56 }
57
CreateKey(const std::wstring & key_path)58 void CreateKey(const std::wstring& key_path) {
59 base::win::RegKey key;
60 ASSERT_EQ(ERROR_SUCCESS,
61 key.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS));
62 }
63
FakeOverrideManagerPath(const base::Time & time)64 std::wstring FakeOverrideManagerPath(const base::Time& time) {
65 return fake_test_key_root_ + L"\\" +
66 base::AsWString(base::NumberToString16(time.ToInternalValue()));
67 }
68
CreateManager(const base::Time & timestamp)69 void CreateManager(const base::Time& timestamp) {
70 manager_.reset(new RegistryOverrideManager(timestamp, fake_test_key_root_));
71 manager_->OverrideRegistry(HKEY_CURRENT_USER);
72 }
73
74 std::wstring fake_test_key_root_;
75 std::unique_ptr<RegistryOverrideManager> manager_;
76 };
77
TEST_F(RegistryOverrideManagerTest,Basic)78 TEST_F(RegistryOverrideManagerTest, Basic) {
79 ASSERT_NO_FATAL_FAILURE(CreateManager(base::Time::Now()));
80
81 base::win::RegKey create_key;
82 EXPECT_EQ(ERROR_SUCCESS,
83 create_key.Create(HKEY_CURRENT_USER, kTestKeyPath, KEY_ALL_ACCESS));
84 EXPECT_TRUE(create_key.Valid());
85 EXPECT_EQ(ERROR_SUCCESS, create_key.WriteValue(kTestValueName, 42));
86 create_key.Close();
87
88 ASSERT_NO_FATAL_FAILURE(AssertKeyExists(kTestKeyPath));
89
90 DWORD value;
91 base::win::RegKey read_key;
92 EXPECT_EQ(ERROR_SUCCESS,
93 read_key.Open(HKEY_CURRENT_USER, kTestKeyPath, KEY_READ));
94 EXPECT_TRUE(read_key.Valid());
95 EXPECT_EQ(ERROR_SUCCESS, read_key.ReadValueDW(kTestValueName, &value));
96 EXPECT_EQ(42u, value);
97 read_key.Close();
98
99 manager_.reset();
100
101 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(kTestKeyPath));
102 }
103
TEST_F(RegistryOverrideManagerTest,DeleteStaleKeys)104 TEST_F(RegistryOverrideManagerTest, DeleteStaleKeys) {
105 static constexpr base::Time::Exploded kTestTimeExploded = {
106 .year = 2013, .month = 11, .day_of_week = 1, .day_of_month = 4};
107 base::Time kTestTime;
108 EXPECT_TRUE(base::Time::FromUTCExploded(kTestTimeExploded, &kTestTime));
109
110 std::wstring path_garbage = fake_test_key_root_ + L"\\Blah";
111 std::wstring path_very_stale =
112 FakeOverrideManagerPath(kTestTime - base::Days(100));
113 std::wstring path_stale = FakeOverrideManagerPath(kTestTime - base::Days(5));
114 std::wstring path_current =
115 FakeOverrideManagerPath(kTestTime - base::Minutes(1));
116 std::wstring path_future =
117 FakeOverrideManagerPath(kTestTime + base::Minutes(1));
118
119 ASSERT_NO_FATAL_FAILURE(CreateKey(path_garbage));
120 ASSERT_NO_FATAL_FAILURE(CreateKey(path_very_stale));
121 ASSERT_NO_FATAL_FAILURE(CreateKey(path_stale));
122 ASSERT_NO_FATAL_FAILURE(CreateKey(path_current));
123 ASSERT_NO_FATAL_FAILURE(CreateKey(path_future));
124
125 ASSERT_NO_FATAL_FAILURE(CreateManager(kTestTime));
126 manager_.reset();
127
128 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(path_garbage));
129 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(path_very_stale));
130 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(path_stale));
131 ASSERT_NO_FATAL_FAILURE(AssertKeyExists(path_current));
132 ASSERT_NO_FATAL_FAILURE(AssertKeyExists(path_future));
133 }
134
TEST_F(RegistryOverrideManagerTest,DoesNotUseMockTime)135 TEST_F(RegistryOverrideManagerTest, DoesNotUseMockTime) {
136 // This test is targeted at scenarios when multiple tests run at the same
137 // time using `RegistryOverrideManager`, new instances of
138 // `RegistryOverrideManager` will clean up any redirected registry paths that
139 // have the timestamp generated (1970) when using `base::Time::Now()` with
140 // MOCK_TIME enabled, which then cause the currently running tests to fail
141 // since their expected reg keys were deleted by the other test.
142
143 // To fix this issue, we have updated `RegistryOverrideManager` by using
144 // `base::subtle::TimeNowIgnoringOverride()` instead of `base::Time::Now()`.
145 // So the real current time is used instead of the mock time in 1970. This can
146 // resolve related `RegistryOverrideManager` test failure when using
147 // MOCK_TIME. This test ensures we are fetching the real current time even
148 // when using MOCK_TIME.
149
150 // Use mock time to init RegKey, which is based on 1970-01-03.
151 // The RegKey contains information about the registry for the
152 // `RegistryOverrideManager`, which also contains a time stamp, which is used
153 // to delete stale keys left over from crashed tests.
154 base::test::TaskEnvironment test_task_env(
155 base::test::TaskEnvironment::TimeSource::MOCK_TIME);
156
157 const base::Time kTestTime = base::Time::Now();
158
159 std::wstring mock_time_path_stale =
160 FakeOverrideManagerPath(kTestTime - base::Days(5));
161 std::wstring mock_time_path_current =
162 FakeOverrideManagerPath(kTestTime - base::Minutes(1));
163
164 ASSERT_NO_FATAL_FAILURE(CreateKey(mock_time_path_stale));
165 ASSERT_NO_FATAL_FAILURE(CreateKey(mock_time_path_current));
166
167 // Use real time to init `path_real`.
168 std::wstring path_real = GenerateTempKeyPath();
169 ASSERT_NO_FATAL_FAILURE(CreateKey(path_real));
170
171 ASSERT_NO_FATAL_FAILURE(CreateManager(kTestTime));
172 manager_.reset();
173
174 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(mock_time_path_stale));
175 ASSERT_NO_FATAL_FAILURE(AssertKeyExists(mock_time_path_current));
176 // `path_real` should exist as it is initiated using real time, not mock time
177 // in 1970.
178 ASSERT_NO_FATAL_FAILURE(AssertKeyExists(path_real));
179
180 // Use real time to init following the new set of keys with
181 // `base::subtle::TimeNowIgnoringOverride()`.
182 const base::Time kTestTime_new = base::subtle::TimeNowIgnoringOverride();
183 std::wstring system_time_path_stale =
184 FakeOverrideManagerPath(kTestTime_new - base::Days(5));
185 std::wstring system_time_path_current =
186 FakeOverrideManagerPath(kTestTime_new - base::Minutes(1));
187
188 ASSERT_NO_FATAL_FAILURE(CreateKey(system_time_path_stale));
189 ASSERT_NO_FATAL_FAILURE(CreateKey(system_time_path_current));
190
191 ASSERT_NO_FATAL_FAILURE(CreateManager(kTestTime_new));
192 manager_.reset();
193
194 // Check old keys created with mock time
195 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(mock_time_path_stale));
196 // While old keys are created using mock time in 1970, these keys will be
197 // deleted.
198 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(mock_time_path_current));
199 // `path_real` should exist as it is initiated using real time, not mock time
200 // in 1970.
201 ASSERT_NO_FATAL_FAILURE(AssertKeyExists(path_real));
202
203 // Create a new manager with real system time.
204 const base::Time kTestTime_latest = base::subtle::TimeNowIgnoringOverride();
205 ASSERT_NO_FATAL_FAILURE(CreateManager(kTestTime_latest));
206 manager_.reset();
207
208 // Check new keys created with current time
209 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(system_time_path_stale));
210 ASSERT_NO_FATAL_FAILURE(AssertKeyExists(system_time_path_current));
211 }
212
213 } // namespace registry_util
214