Cleanup test files to use only one HttpUrlRequestFactory
[chromium-blink-merge.git] / base / test / test_reg_util_win.cc
blob2cbafef105a6b5cba88c02b224bbe887a0d58b2b
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_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace registry_util {
16 namespace {
18 const wchar_t kTimestampDelimiter[] = L"$";
19 const wchar_t kTempTestKeyPath[] = L"Software\\Chromium\\TempTestKeys";
21 void DeleteStaleTestKeys(const base::Time& now,
22 const base::string16& test_key_root) {
23 base::win::RegKey test_root_key;
24 if (test_root_key.Open(HKEY_CURRENT_USER,
25 test_key_root.c_str(),
26 KEY_ALL_ACCESS) != ERROR_SUCCESS) {
27 // This will occur on first-run, but is harmless.
28 return;
31 base::win::RegistryKeyIterator iterator_test_root_key(HKEY_CURRENT_USER,
32 test_key_root.c_str());
33 for (; iterator_test_root_key.Valid(); ++iterator_test_root_key) {
34 base::string16 key_name = iterator_test_root_key.Name();
35 std::vector<base::string16> tokens;
36 if (!Tokenize(key_name, base::string16(kTimestampDelimiter), &tokens))
37 continue;
38 int64 key_name_as_number = 0;
40 if (!base::StringToInt64(tokens[0], &key_name_as_number)) {
41 test_root_key.DeleteKey(key_name.c_str());
42 continue;
45 base::Time key_time = base::Time::FromInternalValue(key_name_as_number);
46 base::TimeDelta age = now - key_time;
48 if (age > base::TimeDelta::FromHours(24))
49 test_root_key.DeleteKey(key_name.c_str());
53 base::string16 GenerateTempKeyPath(const base::string16& test_key_root,
54 const base::Time& timestamp) {
55 base::string16 key_path = test_key_root;
56 key_path += L"\\" + base::Int64ToString16(timestamp.ToInternalValue());
57 key_path += kTimestampDelimiter + base::ASCIIToWide(base::GenerateGUID());
59 return key_path;
62 } // namespace
64 RegistryOverrideManager::ScopedRegistryKeyOverride::ScopedRegistryKeyOverride(
65 HKEY override,
66 const base::string16& key_path)
67 : override_(override) {
68 EXPECT_EQ(
69 ERROR_SUCCESS,
70 temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS));
71 EXPECT_EQ(ERROR_SUCCESS,
72 ::RegOverridePredefKey(override_, temp_key_.Handle()));
75 RegistryOverrideManager::
76 ScopedRegistryKeyOverride::~ScopedRegistryKeyOverride() {
77 ::RegOverridePredefKey(override_, NULL);
78 temp_key_.DeleteKey(L"");
81 RegistryOverrideManager::RegistryOverrideManager()
82 : timestamp_(base::Time::Now()), test_key_root_(kTempTestKeyPath) {
83 DeleteStaleTestKeys(timestamp_, test_key_root_);
86 RegistryOverrideManager::RegistryOverrideManager(
87 const base::Time& timestamp,
88 const base::string16& test_key_root)
89 : timestamp_(timestamp), test_key_root_(test_key_root) {
90 DeleteStaleTestKeys(timestamp_, test_key_root_);
93 RegistryOverrideManager::~RegistryOverrideManager() {}
95 void RegistryOverrideManager::OverrideRegistry(HKEY override) {
96 base::string16 key_path = GenerateTempKeyPath(test_key_root_, timestamp_);
97 overrides_.push_back(new ScopedRegistryKeyOverride(override, key_path));
100 base::string16 GenerateTempKeyPath() {
101 return GenerateTempKeyPath(base::string16(kTempTestKeyPath),
102 base::Time::Now());
105 } // namespace registry_util