test_reg_util_win.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef BASE_TEST_TEST_REG_UTIL_WIN_H_
  5. #define BASE_TEST_TEST_REG_UTIL_WIN_H_
  6. // Registry utility functions used only by tests.
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/macros.h"
  11. #include "base/time/time.h"
  12. #include "base/win/registry.h"
  13. namespace registry_util {
  14. // Allows a test to easily override registry hives so that it can start from a
  15. // known good state, or make sure to not leave any side effects once the test
  16. // completes. This supports parallel tests. All the overrides are scoped to the
  17. // lifetime of the override manager. Destroy the manager to undo the overrides.
  18. //
  19. // Overridden hives use keys stored at, for instance:
  20. // HKCU\Software\Chromium\TempTestKeys\
  21. // 13028145911617809$02AB211C-CF73-478D-8D91-618E11998AED
  22. // The key path are comprises of:
  23. // - The test key root, HKCU\Software\Chromium\TempTestKeys\
  24. // - The base::Time::ToInternalValue of the creation time. This is used to
  25. // delete stale keys left over from crashed tests.
  26. // - A GUID used for preventing name collisions (although unlikely) between
  27. // two RegistryOverrideManagers created with the same timestamp.
  28. class RegistryOverrideManager {
  29. public:
  30. RegistryOverrideManager();
  31. ~RegistryOverrideManager();
  32. // Override the given registry hive using a randomly generated temporary key.
  33. // Multiple overrides to the same hive are not supported and lead to undefined
  34. // behavior.
  35. // Optional return of the registry override path.
  36. // Calls to these functions must be wrapped in ASSERT_NO_FATAL_FAILURE to
  37. // ensure that tests do not proceeed in case of failure to override.
  38. void OverrideRegistry(HKEY override);
  39. void OverrideRegistry(HKEY override, std::wstring* override_path);
  40. private:
  41. friend class RegistryOverrideManagerTest;
  42. // Keeps track of one override.
  43. class ScopedRegistryKeyOverride {
  44. public:
  45. ScopedRegistryKeyOverride(HKEY override, const std::wstring& key_path);
  46. ~ScopedRegistryKeyOverride();
  47. private:
  48. HKEY override_;
  49. std::wstring key_path_;
  50. DISALLOW_COPY_AND_ASSIGN(ScopedRegistryKeyOverride);
  51. };
  52. // Used for testing only.
  53. RegistryOverrideManager(const base::Time& timestamp,
  54. const std::wstring& test_key_root);
  55. base::Time timestamp_;
  56. std::wstring guid_;
  57. std::wstring test_key_root_;
  58. std::vector<std::unique_ptr<ScopedRegistryKeyOverride>> overrides_;
  59. DISALLOW_COPY_AND_ASSIGN(RegistryOverrideManager);
  60. };
  61. // Generates a temporary key path that will be eventually deleted
  62. // automatically if the process crashes.
  63. std::wstring GenerateTempKeyPath();
  64. } // namespace registry_util
  65. #endif // BASE_TEST_TEST_REG_UTIL_WIN_H_