• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "base/test/test_reg_util_win.h"
6 
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"
13 
14 namespace registry_util {
15 
16 namespace {
17 
18 const wchar_t kTimestampDelimiter[] = L"$";
19 const wchar_t kTempTestKeyPath[] = L"Software\\Chromium\\TempTestKeys";
20 
DeleteStaleTestKeys(const base::Time & now,const base::string16 & test_key_root)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;
29   }
30 
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     Tokenize(key_name, base::string16(kTimestampDelimiter), &tokens);
37     int64 key_name_as_number = 0;
38 
39     if (!base::StringToInt64(tokens[0], &key_name_as_number)) {
40       test_root_key.DeleteKey(key_name.c_str());
41       continue;
42     }
43 
44     base::Time key_time = base::Time::FromInternalValue(key_name_as_number);
45     base::TimeDelta age = now - key_time;
46 
47     if (age > base::TimeDelta::FromHours(24))
48       test_root_key.DeleteKey(key_name.c_str());
49   }
50 }
51 
GenerateTempKeyPath(const base::string16 & test_key_root,const base::Time & timestamp)52 base::string16 GenerateTempKeyPath(const base::string16& test_key_root,
53                                    const base::Time& timestamp) {
54   base::string16 key_path = test_key_root;
55   key_path += L"\\" + base::Int64ToString16(timestamp.ToInternalValue());
56   key_path += kTimestampDelimiter + base::ASCIIToWide(base::GenerateGUID());
57 
58   return key_path;
59 }
60 
61 }  // namespace
62 
ScopedRegistryKeyOverride(HKEY override,const base::string16 & key_path)63 RegistryOverrideManager::ScopedRegistryKeyOverride::ScopedRegistryKeyOverride(
64     HKEY override,
65     const base::string16& key_path)
66     : override_(override) {
67   EXPECT_EQ(
68       ERROR_SUCCESS,
69       temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS));
70   EXPECT_EQ(ERROR_SUCCESS,
71             ::RegOverridePredefKey(override_, temp_key_.Handle()));
72 }
73 
74 RegistryOverrideManager::
~ScopedRegistryKeyOverride()75     ScopedRegistryKeyOverride::~ScopedRegistryKeyOverride() {
76   ::RegOverridePredefKey(override_, NULL);
77   temp_key_.DeleteKey(L"");
78 }
79 
RegistryOverrideManager()80 RegistryOverrideManager::RegistryOverrideManager()
81     : timestamp_(base::Time::Now()), test_key_root_(kTempTestKeyPath) {
82   DeleteStaleTestKeys(timestamp_, test_key_root_);
83 }
84 
RegistryOverrideManager(const base::Time & timestamp,const base::string16 & test_key_root)85 RegistryOverrideManager::RegistryOverrideManager(
86     const base::Time& timestamp,
87     const base::string16& test_key_root)
88     : timestamp_(timestamp), test_key_root_(test_key_root) {
89   DeleteStaleTestKeys(timestamp_, test_key_root_);
90 }
91 
~RegistryOverrideManager()92 RegistryOverrideManager::~RegistryOverrideManager() {}
93 
OverrideRegistry(HKEY override,const base::string16 &)94 void RegistryOverrideManager::OverrideRegistry(
95     HKEY override,
96     const base::string16& /*override_name*/) {
97   base::string16 key_path = GenerateTempKeyPath(test_key_root_, timestamp_);
98   overrides_.push_back(new ScopedRegistryKeyOverride(override, key_path));
99 }
100 
GenerateTempKeyPath()101 base::string16 GenerateTempKeyPath() {
102   return GenerateTempKeyPath(base::string16(kTempTestKeyPath),
103                              base::Time::Now());
104 }
105 
106 }  // namespace registry_util
107