[go: nahoru, domu]

blob: 91cdeabf0ffec02cfea6b18dd390c2df9562c0d2 [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
16namespace registry_util {
17
18// Allows a test to easily override registry hives so that it can start from a
19// known good state, or make sure to not leave any side effects once the test
tommycli@chromium.org19b5cc22013-11-15 04:48:3720// completes. This supports parallel tests. All the overrides are scoped to the
21// lifetime of the override manager. Destroy the manager to undo the overrides.
22//
23// Overridden hives use keys stored at, for instance:
24// HKCU\Software\Chromium\TempTestKeys\
25// 13028145911617809$02AB211C-CF73-478D-8D91-618E11998AED
26// The key path are comprises of:
27// - The test key root, HKCU\Software\Chromium\TempTestKeys\
28// - The base::Time::ToInternalValue of the creation time. This is used to
29// delete stale keys left over from crashed tests.
30// - A GUID used for preventing name collisions (although unlikely) between
31// two RegistryOverrideManagers created with the same timestamp.
rogerta@chromium.orga8f85882011-08-24 20:02:4232class RegistryOverrideManager {
33 public:
rogerta@chromium.orga8f85882011-08-24 20:02:4234 RegistryOverrideManager();
Peter Boström7319bbd2021-09-15 22:59:3835
36 RegistryOverrideManager(const RegistryOverrideManager&) = delete;
37 RegistryOverrideManager& operator=(const RegistryOverrideManager&) = delete;
38
rogerta@chromium.orga8f85882011-08-24 20:02:4239 ~RegistryOverrideManager();
40
gab6f7c83b2014-09-18 02:37:4741 // Override the given registry hive using a randomly generated temporary key.
42 // Multiple overrides to the same hive are not supported and lead to undefined
43 // behavior.
pennymac84fd6692016-07-13 22:35:3444 // Optional return of the registry override path.
grtf6d7da22017-02-14 07:14:4745 // Calls to these functions must be wrapped in ASSERT_NO_FATAL_FAILURE to
46 // ensure that tests do not proceeed in case of failure to override.
gab6f7c83b2014-09-18 02:37:4747 void OverrideRegistry(HKEY override);
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2348 void OverrideRegistry(HKEY override, std::wstring* override_path);
rogerta@chromium.orga8f85882011-08-24 20:02:4249
50 private:
tommycli@chromium.org19b5cc22013-11-15 04:48:3751 friend class RegistryOverrideManagerTest;
52
rogerta@chromium.orga8f85882011-08-24 20:02:4253 // Keeps track of one override.
54 class ScopedRegistryKeyOverride {
55 public:
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2356 ScopedRegistryKeyOverride(HKEY override, const std::wstring& key_path);
Peter Boström7319bbd2021-09-15 22:59:3857
58 ScopedRegistryKeyOverride(const ScopedRegistryKeyOverride&) = delete;
59 ScopedRegistryKeyOverride& operator=(const ScopedRegistryKeyOverride&) =
60 delete;
61
rogerta@chromium.orga8f85882011-08-24 20:02:4262 ~ScopedRegistryKeyOverride();
63
64 private:
65 HKEY override_;
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2366 std::wstring key_path_;
rogerta@chromium.orga8f85882011-08-24 20:02:4267 };
68
tommycli@chromium.org19b5cc22013-11-15 04:48:3769 // Used for testing only.
70 RegistryOverrideManager(const base::Time& timestamp,
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2371 const std::wstring& test_key_root);
tommycli@chromium.org19b5cc22013-11-15 04:48:3772
73 base::Time timestamp_;
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2374 std::wstring guid_;
tommycli@chromium.org19b5cc22013-11-15 04:48:3775
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2376 std::wstring test_key_root_;
dcheng093de9b2016-04-04 21:25:5177 std::vector<std::unique_ptr<ScopedRegistryKeyOverride>> overrides_;
rogerta@chromium.orga8f85882011-08-24 20:02:4278};
79
tommycli@chromium.org19b5cc22013-11-15 04:48:3780// Generates a temporary key path that will be eventually deleted
81// automatically if the process crashes.
Jan Wilken Dörrie0cf1e27f2019-09-11 09:58:2382std::wstring GenerateTempKeyPath();
tommycli@chromium.org19b5cc22013-11-15 04:48:3783
rogerta@chromium.orga8f85882011-08-24 20:02:4284} // namespace registry_util
85
danakj0a448602015-03-10 00:31:1686#endif // BASE_TEST_TEST_REG_UTIL_WIN_H_