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
14namespace registry_util {
15
16namespace {
17
18const wchar_t kTimestampDelimiter[] = L"$";
19const wchar_t kTempTestKeyPath[] = L"Software\\Chromium\\TempTestKeys";
20
21void DeleteStaleTestKeys(const base::Time& now, const string16& test_key_root) {
22  base::win::RegKey test_root_key;
23  if (test_root_key.Open(HKEY_CURRENT_USER,
24                         test_key_root.c_str(),
25                         KEY_ALL_ACCESS) != ERROR_SUCCESS) {
26    // This will occur on first-run, but is harmless.
27    return;
28  }
29
30  base::win::RegistryKeyIterator iterator_test_root_key(HKEY_CURRENT_USER,
31                                                        test_key_root.c_str());
32  for (; iterator_test_root_key.Valid(); ++iterator_test_root_key) {
33    string16 key_name = iterator_test_root_key.Name();
34    std::vector<string16> tokens;
35    Tokenize(key_name, string16(kTimestampDelimiter), &tokens);
36    int64 key_name_as_number = 0;
37
38    if (!base::StringToInt64(tokens[0], &key_name_as_number)) {
39      test_root_key.DeleteKey(key_name.c_str());
40      continue;
41    }
42
43    base::Time key_time = base::Time::FromInternalValue(key_name_as_number);
44    base::TimeDelta age = now - key_time;
45
46    if (age > base::TimeDelta::FromHours(24))
47      test_root_key.DeleteKey(key_name.c_str());
48  }
49}
50
51string16 GenerateTempKeyPath(const string16& test_key_root,
52                             const base::Time& timestamp) {
53  string16 key_path = test_key_root;
54  key_path += L"\\" + base::Int64ToString16(timestamp.ToInternalValue());
55  key_path += kTimestampDelimiter + base::ASCIIToWide(base::GenerateGUID());
56
57  return key_path;
58}
59
60}  // namespace
61
62RegistryOverrideManager::ScopedRegistryKeyOverride::ScopedRegistryKeyOverride(
63    HKEY override,
64    const string16& key_path)
65    : override_(override) {
66  EXPECT_EQ(
67      ERROR_SUCCESS,
68      temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS));
69  EXPECT_EQ(ERROR_SUCCESS,
70            ::RegOverridePredefKey(override_, temp_key_.Handle()));
71}
72
73RegistryOverrideManager::
74    ScopedRegistryKeyOverride::~ScopedRegistryKeyOverride() {
75  ::RegOverridePredefKey(override_, NULL);
76  temp_key_.DeleteKey(L"");
77}
78
79RegistryOverrideManager::RegistryOverrideManager()
80    : timestamp_(base::Time::Now()), test_key_root_(kTempTestKeyPath) {
81  DeleteStaleTestKeys(timestamp_, test_key_root_);
82}
83
84RegistryOverrideManager::RegistryOverrideManager(const base::Time& timestamp,
85                                                 const string16& test_key_root)
86    : timestamp_(timestamp), test_key_root_(test_key_root) {
87  DeleteStaleTestKeys(timestamp_, test_key_root_);
88}
89
90RegistryOverrideManager::~RegistryOverrideManager() {}
91
92void RegistryOverrideManager::OverrideRegistry(
93    HKEY override,
94    const string16& /*override_name*/) {
95  string16 key_path = GenerateTempKeyPath(test_key_root_, timestamp_);
96  overrides_.push_back(new ScopedRegistryKeyOverride(override, key_path));
97}
98
99string16 GenerateTempKeyPath() {
100  return GenerateTempKeyPath(string16(kTempTestKeyPath), base::Time::Now());
101}
102
103}  // namespace registry_util
104