Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Side by Side Diff: base/test/test_reg_util_win.cc

Issue 57423008: Base: Make RegistryOverrideManager support sharded/parallel tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/test/test_reg_util_win.h ('k') | base/test/test_reg_util_win_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/test/test_reg_util_win.h" 5 #include "base/test/test_reg_util_win.h"
6 6
7 #include "base/guid.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
9 12
10 namespace registry_util { 13 namespace registry_util {
11 14
15 namespace {
16
17 void DeleteStaleTestKeys(const base::Time& now, const string16& test_key_root) {
18 base::win::RegKey test_root_key;
19 if (ERROR_SUCCESS !=
20 test_root_key.Open(HKEY_CURRENT_USER, test_key_root.c_str(),
21 KEY_ALL_ACCESS)) {
22 return;
Paweł Hajdan Jr. 2013/11/06 20:33:58 Since this can fail, let's return bool and add WAR
tommycli 2013/11/06 21:44:23 This occurs on first-run, and is harmless. This sh
23 }
24
25 base::win::RegistryKeyIterator iterator_test_root_key(
26 HKEY_CURRENT_USER, test_key_root.c_str());
27 for (; iterator_test_root_key.Valid(); ++iterator_test_root_key) {
28 string16 key_name = iterator_test_root_key.Name();
29 int64 key_name_as_number = 0;
30 if (!base::StringToInt64(key_name, &key_name_as_number)) {
31 test_root_key.DeleteKey(key_name.c_str());
32 continue;
33 }
34
35 base::Time key_time = base::Time::FromInternalValue(key_name_as_number);
36 base::TimeDelta age = now - key_time;
37
38 if (age > base::TimeDelta::FromHours(1))
Paweł Hajdan Jr. 2013/11/06 20:33:58 Let's make this bigger: 24 hours.
tommycli 2013/11/06 21:44:23 Done.
39 test_root_key.DeleteKey(key_name.c_str());
40 }
41 }
42
43 }
Paweł Hajdan Jr. 2013/11/06 20:33:58 nit: // namespace
tommycli 2013/11/06 21:44:23 Done.
44
12 const wchar_t RegistryOverrideManager::kTempTestKeyPath[] = 45 const wchar_t RegistryOverrideManager::kTempTestKeyPath[] =
13 L"Software\\Chromium\\TempTestKeys"; 46 L"Software\\Chromium\\TempTestKeys";
14 47
15 RegistryOverrideManager::ScopedRegistryKeyOverride::ScopedRegistryKeyOverride( 48 RegistryOverrideManager::ScopedRegistryKeyOverride::ScopedRegistryKeyOverride(
16 HKEY override, 49 HKEY override,
17 const std::wstring& temp_name) 50 const string16& key_path)
18 : override_(override), 51 : override_(override) {
19 temp_name_(temp_name) {
20 DCHECK(!temp_name_.empty());
21 std::wstring key_path(RegistryOverrideManager::kTempTestKeyPath);
22 key_path += L"\\" + temp_name_;
23 EXPECT_EQ(ERROR_SUCCESS, 52 EXPECT_EQ(ERROR_SUCCESS,
24 temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS)); 53 temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS));
25 EXPECT_EQ(ERROR_SUCCESS, 54 EXPECT_EQ(ERROR_SUCCESS,
26 ::RegOverridePredefKey(override_, temp_key_.Handle())); 55 ::RegOverridePredefKey(override_, temp_key_.Handle()));
27 } 56 }
28 57
29 RegistryOverrideManager:: 58 RegistryOverrideManager::
30 ScopedRegistryKeyOverride::~ScopedRegistryKeyOverride() { 59 ScopedRegistryKeyOverride::~ScopedRegistryKeyOverride() {
31 ::RegOverridePredefKey(override_, NULL); 60 ::RegOverridePredefKey(override_, NULL);
32 // The temp key will be deleted via a call to DeleteAllTempKeys(). 61 temp_key_.DeleteKey(L"");
33 } 62 }
34 63
35 RegistryOverrideManager::RegistryOverrideManager() { 64 RegistryOverrideManager::RegistryOverrideManager()
36 DeleteAllTempKeys(); 65 : timestamp_(base::Time::Now()),
66 guid_(base::ASCIIToWide(base::GenerateGUID())),
67 test_key_root_(kTempTestKeyPath) {
68 DeleteStaleTestKeys(timestamp_, test_key_root_);
37 } 69 }
38 70
39 RegistryOverrideManager::~RegistryOverrideManager() { 71 RegistryOverrideManager::RegistryOverrideManager(const base::Time& timestamp,
40 RemoveAllOverrides(); 72 const string16& test_key_root)
73 : timestamp_(timestamp),
74 guid_(base::ASCIIToWide(base::GenerateGUID())),
75 test_key_root_(test_key_root) {
76 DeleteStaleTestKeys(timestamp_, test_key_root_);
41 } 77 }
42 78
79
80 RegistryOverrideManager::~RegistryOverrideManager() {}
81
43 void RegistryOverrideManager::OverrideRegistry(HKEY override, 82 void RegistryOverrideManager::OverrideRegistry(HKEY override,
44 const std::wstring& temp_name) { 83 const string16& override_name) {
45 overrides_.push_back(new ScopedRegistryKeyOverride(override, temp_name)); 84 string16 key_path = test_key_root_;
46 } 85 key_path += L"\\" + base::Int64ToString16(timestamp_.ToInternalValue());
86 key_path += L"\\" + guid_;
87 key_path += L"\\" + override_name;
47 88
48 void RegistryOverrideManager::RemoveAllOverrides() { 89 overrides_.push_back(new ScopedRegistryKeyOverride(override, key_path));
49 while (!overrides_.empty()) {
50 delete overrides_.back();
51 overrides_.pop_back();
52 }
53
54 DeleteAllTempKeys();
55 }
56
57 // static
58 void RegistryOverrideManager::DeleteAllTempKeys() {
59 base::win::RegKey key;
60 if (key.Open(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS) == ERROR_SUCCESS) {
61 key.DeleteKey(kTempTestKeyPath);
62 }
63 } 90 }
64 91
65 } // namespace registry_util 92 } // namespace registry_util
OLDNEW
« no previous file with comments | « base/test/test_reg_util_win.h ('k') | base/test/test_reg_util_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698