1 // Copyright 2011 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 #ifndef BASE_TEST_TEST_REG_UTIL_WIN_H_ 6 #define BASE_TEST_TEST_REG_UTIL_WIN_H_ 7 8 // Registry utility functions used only by tests. 9 #include <memory> 10 #include <string> 11 #include <vector> 12 13 #include "base/time/time.h" 14 #include "base/win/registry.h" 15 16 namespace content { 17 class BrowserTestBase; 18 } 19 20 namespace 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 24 // 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. 36 class RegistryOverrideManager { 37 public: 38 RegistryOverrideManager(); 39 40 RegistryOverrideManager(const RegistryOverrideManager&) = delete; 41 RegistryOverrideManager& operator=(const RegistryOverrideManager&) = delete; 42 43 ~RegistryOverrideManager(); 44 45 // 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. 48 // Optional return of the registry override path. 49 // Calls to these functions must be wrapped in ASSERT_NO_FATAL_FAILURE to 50 // 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. 54 void OverrideRegistry(HKEY override); 55 void OverrideRegistry(HKEY override, std::wstring* override_path); 56 57 private: 58 friend class RegistryOverrideManagerTest; 59 friend class content::BrowserTestBase; 60 61 // Keeps track of one override. 62 class ScopedRegistryKeyOverride { 63 public: 64 ScopedRegistryKeyOverride(HKEY override, const std::wstring& key_path); 65 66 ScopedRegistryKeyOverride(const ScopedRegistryKeyOverride&) = delete; 67 ScopedRegistryKeyOverride& operator=(const ScopedRegistryKeyOverride&) = 68 delete; 69 70 ~ScopedRegistryKeyOverride(); 71 72 private: 73 HKEY override_; 74 std::wstring key_path_; 75 }; 76 77 // Used for testing only. 78 RegistryOverrideManager(const base::Time& timestamp, 79 const std::wstring& test_key_root); 80 81 // Whether or not to allow using the RegistryOverrideManager for HKLM (e.g. in 82 // browser_tests). 83 static void SetAllowHKLMRegistryOverrideForIntegrationTests(bool allow); 84 85 base::Time timestamp_; 86 std::wstring guid_; 87 88 std::wstring test_key_root_; 89 std::vector<std::unique_ptr<ScopedRegistryKeyOverride>> overrides_; 90 }; 91 92 // Generates a temporary key path that will be eventually deleted 93 // automatically if the process crashes. 94 std::wstring GenerateTempKeyPath(); 95 96 } // namespace registry_util 97 98 #endif // BASE_TEST_TEST_REG_UTIL_WIN_H_ 99