[go: nahoru, domu]

blob: 8f47eaf1f869afd8ddb28b82562adf308c84911f [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
rogerta@chromium.orga8f85882011-08-24 20:02:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
danakj0a448602015-03-10 00:31:165#ifndef BASE_TEST_TEST_REG_UTIL_WIN_H_
6#define BASE_TEST_TEST_REG_UTIL_WIN_H_
rogerta@chromium.orga8f85882011-08-24 20:02:427
8// Registry utility functions used only by tests.
dcheng093de9b2016-04-04 21:25:519#include <memory>
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2310#include <string>
kimwjdalslab64c482015-12-16 00:11:1011#include <vector>
rogerta@chromium.orga8f85882011-08-24 20:02:4212
tommycli@chromium.org19b5cc22013-11-15 04:48:3713#include "base/time/time.h"
rogerta@chromium.orga8f85882011-08-24 20:02:4214#include "base/win/registry.h"
15
Will Harris576615e2023-09-01 20:58:3216namespace content {
17class BrowserTestBase;
18}
19
rogerta@chromium.orga8f85882011-08-24 20:02:4220namespace registry_util {
21
22// Allows a test to easily override registry hives so that it can start from a
23// known good state, or make sure to not leave any side effects once the test
tommycli@chromium.org19b5cc22013-11-15 04:48:3724// completes. This supports parallel tests. All the overrides are scoped to the
25// lifetime of the override manager. Destroy the manager to undo the overrides.
26//
27// Overridden hives use keys stored at, for instance:
28// HKCU\Software\Chromium\TempTestKeys\
29// 13028145911617809$02AB211C-CF73-478D-8D91-618E11998AED
30// The key path are comprises of:
31// - The test key root, HKCU\Software\Chromium\TempTestKeys\
32// - The base::Time::ToInternalValue of the creation time. This is used to
33// delete stale keys left over from crashed tests.
34// - A GUID used for preventing name collisions (although unlikely) between
35// two RegistryOverrideManagers created with the same timestamp.
rogerta@chromium.orga8f85882011-08-24 20:02:4236class RegistryOverrideManager {
37 public:
rogerta@chromium.orga8f85882011-08-24 20:02:4238 RegistryOverrideManager();
Peter Boström7319bbd2021-09-15 22:59:3839
40 RegistryOverrideManager(const RegistryOverrideManager&) = delete;
41 RegistryOverrideManager& operator=(const RegistryOverrideManager&) = delete;
42
rogerta@chromium.orga8f85882011-08-24 20:02:4243 ~RegistryOverrideManager();
44
gab6f7c83b2014-09-18 02:37:4745 // Override the given registry hive using a randomly generated temporary key.
46 // Multiple overrides to the same hive are not supported and lead to undefined
47 // behavior.
pennymac84fd6692016-07-13 22:35:3448 // Optional return of the registry override path.
grtf6d7da22017-02-14 07:14:4749 // Calls to these functions must be wrapped in ASSERT_NO_FATAL_FAILURE to
Will Harris576615e2023-09-01 20:58:3250 // ensure that tests do not proceed in case of failure to override.
51 // HKEY_LOCAL_MACHINE should not be overridden in initialization for tests
52 // that launch sandboxed processes e.g. browser tests. It is safe to use from
53 // within a text fixture, and in unit tests.
gab6f7c83b2014-09-18 02:37:4754 void OverrideRegistry(HKEY override);
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2355 void OverrideRegistry(HKEY override, std::wstring* override_path);
rogerta@chromium.orga8f85882011-08-24 20:02:4256
57 private:
tommycli@chromium.org19b5cc22013-11-15 04:48:3758 friend class RegistryOverrideManagerTest;
Will Harris576615e2023-09-01 20:58:3259 friend class content::BrowserTestBase;
tommycli@chromium.org19b5cc22013-11-15 04:48:3760
rogerta@chromium.orga8f85882011-08-24 20:02:4261 // Keeps track of one override.
62 class ScopedRegistryKeyOverride {
63 public:
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2364 ScopedRegistryKeyOverride(HKEY override, const std::wstring& key_path);
Peter Boström7319bbd2021-09-15 22:59:3865
66 ScopedRegistryKeyOverride(const ScopedRegistryKeyOverride&) = delete;
67 ScopedRegistryKeyOverride& operator=(const ScopedRegistryKeyOverride&) =
68 delete;
69
rogerta@chromium.orga8f85882011-08-24 20:02:4270 ~ScopedRegistryKeyOverride();
71
72 private:
73 HKEY override_;
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2374 std::wstring key_path_;
rogerta@chromium.orga8f85882011-08-24 20:02:4275 };
76
tommycli@chromium.org19b5cc22013-11-15 04:48:3777 // Used for testing only.
78 RegistryOverrideManager(const base::Time& timestamp,
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2379 const std::wstring& test_key_root);
tommycli@chromium.org19b5cc22013-11-15 04:48:3780
Will Harris576615e2023-09-01 20:58:3281 // Whether or not to allow using the RegistryOverrideManager for HKLM (e.g. in
82 // browser_tests).
83 static void SetAllowHKLMRegistryOverrideForIntegrationTests(bool allow);
84
tommycli@chromium.org19b5cc22013-11-15 04:48:3785 base::Time timestamp_;
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2386 std::wstring guid_;
tommycli@chromium.org19b5cc22013-11-15 04:48:3787
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2388 std::wstring test_key_root_;
dcheng093de9b2016-04-04 21:25:5189 std::vector<std::unique_ptr<ScopedRegistryKeyOverride>> overrides_;
rogerta@chromium.orga8f85882011-08-24 20:02:4290};
91
tommycli@chromium.org19b5cc22013-11-15 04:48:3792// Generates a temporary key path that will be eventually deleted
93// automatically if the process crashes.
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2394std::wstring GenerateTempKeyPath();
tommycli@chromium.org19b5cc22013-11-15 04:48:3795
rogerta@chromium.orga8f85882011-08-24 20:02:4296} // namespace registry_util
97
danakj0a448602015-03-10 00:31:1698#endif // BASE_TEST_TEST_REG_UTIL_WIN_H_