| OLD | NEW |
| (Empty) |
| 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_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" | |
| 14 | |
| 15 namespace registry_util { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const wchar_t kTimestampDelimiter[] = L"$"; | |
| 20 const wchar_t kTempTestKeyPath[] = L"Software\\Chromium\\TempTestKeys"; | |
| 21 | |
| 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; | |
| 30 } | |
| 31 | |
| 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 = | |
| 37 base::SplitString(key_name, kTimestampDelimiter, base::KEEP_WHITESPACE, | |
| 38 base::SPLIT_WANT_NONEMPTY); | |
| 39 if (tokens.empty()) | |
| 40 continue; | |
| 41 int64 key_name_as_number = 0; | |
| 42 | |
| 43 if (!base::StringToInt64(tokens[0], &key_name_as_number)) { | |
| 44 test_root_key.DeleteKey(key_name.c_str()); | |
| 45 continue; | |
| 46 } | |
| 47 | |
| 48 base::Time key_time = base::Time::FromInternalValue(key_name_as_number); | |
| 49 base::TimeDelta age = now - key_time; | |
| 50 | |
| 51 if (age > base::TimeDelta::FromHours(24)) | |
| 52 test_root_key.DeleteKey(key_name.c_str()); | |
| 53 } | |
| 54 } | |
| 55 | |
| 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()); | |
| 61 | |
| 62 return key_path; | |
| 63 } | |
| 64 | |
| 65 } // namespace | |
| 66 | |
| 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())); | |
| 76 } | |
| 77 | |
| 78 RegistryOverrideManager:: | |
| 79 ScopedRegistryKeyOverride::~ScopedRegistryKeyOverride() { | |
| 80 ::RegOverridePredefKey(override_, NULL); | |
| 81 temp_key_.DeleteKey(L""); | |
| 82 } | |
| 83 | |
| 84 RegistryOverrideManager::RegistryOverrideManager() | |
| 85 : timestamp_(base::Time::Now()), test_key_root_(kTempTestKeyPath) { | |
| 86 DeleteStaleTestKeys(timestamp_, test_key_root_); | |
| 87 } | |
| 88 | |
| 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_); | |
| 94 } | |
| 95 | |
| 96 RegistryOverrideManager::~RegistryOverrideManager() {} | |
| 97 | |
| 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)); | |
| 101 } | |
| 102 | |
| 103 base::string16 GenerateTempKeyPath() { | |
| 104 return GenerateTempKeyPath(base::string16(kTempTestKeyPath), | |
| 105 base::Time::Now()); | |
| 106 } | |
| 107 | |
| 108 } // namespace registry_util | |
| OLD | NEW |