1// Copyright (c) 2011 The Chromium Authors. All rights reserved. 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_H_ 6#define BASE_TEST_TEST_REG_UTIL_H_ 7 8// Registry utility functions used only by tests. 9 10#include "base/basictypes.h" 11#include "base/memory/scoped_vector.h" 12#include "base/strings/string16.h" 13#include "base/time/time.h" 14#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 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. 32class RegistryOverrideManager { 33 public: 34 RegistryOverrideManager(); 35 ~RegistryOverrideManager(); 36 37 // Override the given registry hive using a randomly generated temporary key. 38 // Multiple overrides to the same hive are not supported and lead to undefined 39 // behavior. 40 void OverrideRegistry(HKEY override); 41 42 private: 43 friend class RegistryOverrideManagerTest; 44 45 // Keeps track of one override. 46 class ScopedRegistryKeyOverride { 47 public: 48 ScopedRegistryKeyOverride(HKEY override, const base::string16& key_path); 49 ~ScopedRegistryKeyOverride(); 50 51 private: 52 HKEY override_; 53 base::win::RegKey temp_key_; 54 55 DISALLOW_COPY_AND_ASSIGN(ScopedRegistryKeyOverride); 56 }; 57 58 // Used for testing only. 59 RegistryOverrideManager(const base::Time& timestamp, 60 const base::string16& test_key_root); 61 62 base::Time timestamp_; 63 base::string16 guid_; 64 65 base::string16 test_key_root_; 66 ScopedVector<ScopedRegistryKeyOverride> overrides_; 67 68 DISALLOW_COPY_AND_ASSIGN(RegistryOverrideManager); 69}; 70 71// Generates a temporary key path that will be eventually deleted 72// automatically if the process crashes. 73base::string16 GenerateTempKeyPath(); 74 75} // namespace registry_util 76 77#endif // BASE_TEST_TEST_REG_UTIL_H_ 78