Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / base / test / test_reg_util_win.cc
bloba7d07f7ab2c617a86c7e33297f70c63d83ab4841
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.
5 #include "base/test/test_reg_util_win.h"
7 #include "base/guid.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace registry_util {
17 namespace {
19 const wchar_t kTimestampDelimiter[] = L"$";
20 const wchar_t kTempTestKeyPath[] = L"Software\\Chromium\\TempTestKeys";
22 void DeleteStaleTestKeys(const base::Time& now,
23 const base::string16& test_key_root) {
24 base::win::RegKey test_root_key;
25 if (test_root_key.Open(HKEY_CURRENT_USER,
26 test_key_root.c_str(),
27 KEY_ALL_ACCESS) != ERROR_SUCCESS) {
28 // This will occur on first-run, but is harmless.
29 return;
32 base::win::RegistryKeyIterator iterator_test_root_key(HKEY_CURRENT_USER,
33 test_key_root.c_str());
34 for (; iterator_test_root_key.Valid(); ++iterator_test_root_key) {
35 base::string16 key_name = iterator_test_root_key.Name();
36 std::vector<base::string16> tokens = base::SplitString(
37 key_name, kTimestampDelimiter, base::KEEP_WHITESPACE,
38 base::SPLIT_WANT_NONEMPTY);
39 if (tokens.empty())
40 continue;
41 int64 key_name_as_number = 0;
43 if (!base::StringToInt64(tokens[0], &key_name_as_number)) {
44 test_root_key.DeleteKey(key_name.c_str());
45 continue;
48 base::Time key_time = base::Time::FromInternalValue(key_name_as_number);
49 base::TimeDelta age = now - key_time;
51 if (age > base::TimeDelta::FromHours(24))
52 test_root_key.DeleteKey(key_name.c_str());
56 base::string16 GenerateTempKeyPath(const base::string16& test_key_root,
57 const base::Time& timestamp) {
58 base::string16 key_path = test_key_root;
59 key_path += L"\\" + base::Int64ToString16(timestamp.ToInternalValue());
60 key_path += kTimestampDelimiter + base::ASCIIToUTF16(base::GenerateGUID());
62 return key_path;
65 } // namespace
67 RegistryOverrideManager::ScopedRegistryKeyOverride::ScopedRegistryKeyOverride(
68 HKEY override,
69 const base::string16& key_path)
70 : override_(override) {
71 EXPECT_EQ(
72 ERROR_SUCCESS,
73 temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS));
74 EXPECT_EQ(ERROR_SUCCESS,
75 ::RegOverridePredefKey(override_, temp_key_.Handle()));
78 RegistryOverrideManager::
79 ScopedRegistryKeyOverride::~ScopedRegistryKeyOverride() {
80 ::RegOverridePredefKey(override_, NULL);
81 temp_key_.DeleteKey(L"");
84 RegistryOverrideManager::RegistryOverrideManager()
85 : timestamp_(base::Time::Now()), test_key_root_(kTempTestKeyPath) {
86 DeleteStaleTestKeys(timestamp_, test_key_root_);
89 RegistryOverrideManager::RegistryOverrideManager(
90 const base::Time& timestamp,
91 const base::string16& test_key_root)
92 : timestamp_(timestamp), test_key_root_(test_key_root) {
93 DeleteStaleTestKeys(timestamp_, test_key_root_);
96 RegistryOverrideManager::~RegistryOverrideManager() {}
98 void RegistryOverrideManager::OverrideRegistry(HKEY override) {
99 base::string16 key_path = GenerateTempKeyPath(test_key_root_, timestamp_);
100 overrides_.push_back(new ScopedRegistryKeyOverride(override, key_path));
103 base::string16 GenerateTempKeyPath() {
104 return GenerateTempKeyPath(base::string16(kTempTestKeyPath),
105 base::Time::Now());
108 } // namespace registry_util