Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame^] | 1 | // Copyright 2011 The Chromium Authors |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 5 | #ifndef BASE_TEST_TEST_REG_UTIL_WIN_H_ |
| 6 | #define BASE_TEST_TEST_REG_UTIL_WIN_H_ |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 7 | |
| 8 | // Registry utility functions used only by tests. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 9 | #include <memory> |
Jan Wilken Dörrie | 0cf1e27f | 2019-09-11 09:58:23 | [diff] [blame] | 10 | #include <string> |
kimwjdalsl | ab64c48 | 2015-12-16 00:11:10 | [diff] [blame] | 11 | #include <vector> |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 12 | |
tommycli@chromium.org | 19b5cc2 | 2013-11-15 04:48:37 | [diff] [blame] | 13 | #include "base/time/time.h" |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 14 | #include "base/win/registry.h" |
| 15 | |
| 16 | namespace 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.org | 19b5cc2 | 2013-11-15 04:48:37 | [diff] [blame] | 20 | // 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.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 32 | class RegistryOverrideManager { |
| 33 | public: |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 34 | RegistryOverrideManager(); |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 35 | |
| 36 | RegistryOverrideManager(const RegistryOverrideManager&) = delete; |
| 37 | RegistryOverrideManager& operator=(const RegistryOverrideManager&) = delete; |
| 38 | |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 39 | ~RegistryOverrideManager(); |
| 40 | |
gab | 6f7c83b | 2014-09-18 02:37:47 | [diff] [blame] | 41 | // 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. |
pennymac | 84fd669 | 2016-07-13 22:35:34 | [diff] [blame] | 44 | // Optional return of the registry override path. |
grt | f6d7da2 | 2017-02-14 07:14:47 | [diff] [blame] | 45 | // 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. |
gab | 6f7c83b | 2014-09-18 02:37:47 | [diff] [blame] | 47 | void OverrideRegistry(HKEY override); |
Jan Wilken Dörrie | 0cf1e27f | 2019-09-11 09:58:23 | [diff] [blame] | 48 | void OverrideRegistry(HKEY override, std::wstring* override_path); |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 49 | |
| 50 | private: |
tommycli@chromium.org | 19b5cc2 | 2013-11-15 04:48:37 | [diff] [blame] | 51 | friend class RegistryOverrideManagerTest; |
| 52 | |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 53 | // Keeps track of one override. |
| 54 | class ScopedRegistryKeyOverride { |
| 55 | public: |
Jan Wilken Dörrie | 0cf1e27f | 2019-09-11 09:58:23 | [diff] [blame] | 56 | ScopedRegistryKeyOverride(HKEY override, const std::wstring& key_path); |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 57 | |
| 58 | ScopedRegistryKeyOverride(const ScopedRegistryKeyOverride&) = delete; |
| 59 | ScopedRegistryKeyOverride& operator=(const ScopedRegistryKeyOverride&) = |
| 60 | delete; |
| 61 | |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 62 | ~ScopedRegistryKeyOverride(); |
| 63 | |
| 64 | private: |
| 65 | HKEY override_; |
Jan Wilken Dörrie | 0cf1e27f | 2019-09-11 09:58:23 | [diff] [blame] | 66 | std::wstring key_path_; |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 67 | }; |
| 68 | |
tommycli@chromium.org | 19b5cc2 | 2013-11-15 04:48:37 | [diff] [blame] | 69 | // Used for testing only. |
| 70 | RegistryOverrideManager(const base::Time& timestamp, |
Jan Wilken Dörrie | 0cf1e27f | 2019-09-11 09:58:23 | [diff] [blame] | 71 | const std::wstring& test_key_root); |
tommycli@chromium.org | 19b5cc2 | 2013-11-15 04:48:37 | [diff] [blame] | 72 | |
| 73 | base::Time timestamp_; |
Jan Wilken Dörrie | 0cf1e27f | 2019-09-11 09:58:23 | [diff] [blame] | 74 | std::wstring guid_; |
tommycli@chromium.org | 19b5cc2 | 2013-11-15 04:48:37 | [diff] [blame] | 75 | |
Jan Wilken Dörrie | 0cf1e27f | 2019-09-11 09:58:23 | [diff] [blame] | 76 | std::wstring test_key_root_; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 77 | std::vector<std::unique_ptr<ScopedRegistryKeyOverride>> overrides_; |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 78 | }; |
| 79 | |
tommycli@chromium.org | 19b5cc2 | 2013-11-15 04:48:37 | [diff] [blame] | 80 | // Generates a temporary key path that will be eventually deleted |
| 81 | // automatically if the process crashes. |
Jan Wilken Dörrie | 0cf1e27f | 2019-09-11 09:58:23 | [diff] [blame] | 82 | std::wstring GenerateTempKeyPath(); |
tommycli@chromium.org | 19b5cc2 | 2013-11-15 04:48:37 | [diff] [blame] | 83 | |
rogerta@chromium.org | a8f8588 | 2011-08-24 20:02:42 | [diff] [blame] | 84 | } // namespace registry_util |
| 85 | |
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 86 | #endif // BASE_TEST_TEST_REG_UTIL_WIN_H_ |