Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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/compiler_specific.h" | |
| 6 #include "base/guid.h" | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/strings/string_number_conversions.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "base/test/test_reg_util_win.h" | |
| 11 #include "base/time/time.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace registry_util { | |
| 15 | |
| 16 namespace { | |
| 17 const wchar_t kTestKeyPath[] = L"Software\\Chromium\\Foo\\Baz\\TestKey"; | |
| 18 const wchar_t kTestValueName[] = L"TestValue"; | |
| 19 } // namespace | |
| 20 | |
| 21 class RegistryOverrideManagerTest : public testing::Test { | |
| 22 public: | |
|
grt (UTC plus 2)
2013/11/07 02:39:33
instead of public ctor and dtor, do this work in o
tommycli
2013/11/07 18:02:07
Hmm... I was following
https://code.google.com/p/
grt (UTC plus 2)
2013/11/08 04:33:05
Oh, cool. I hadn't seen that. Thanks for the point
tommycli
2013/11/08 16:24:00
Done.
| |
| 23 RegistryOverrideManagerTest() { | |
| 24 // We assign a fake test key path to our test RegistryOverrideManager | |
| 25 // so we don't interfere with any actual RegistryOverrideManagers running | |
| 26 // on the system. This fake path will be auto-deleted by other | |
| 27 // RegistryOverrideManagers in case we crash. | |
| 28 key_to_delete_on_exit_ = | |
| 29 string16(RegistryOverrideManager::kTempTestKeyPath) + | |
| 30 L"\\" + base::Int64ToString16(base::Time::Now().ToInternalValue()); | |
| 31 fake_test_key_root_ = key_to_delete_on_exit_ + L"\\" + | |
| 32 base::ASCIIToWide(base::GenerateGUID()); | |
| 33 | |
| 34 // Ensure a clean test environment. | |
| 35 base::win::RegKey key(HKEY_CURRENT_USER); | |
| 36 key.DeleteKey(fake_test_key_root_.c_str()); | |
| 37 key.DeleteKey(kTestKeyPath); | |
| 38 } | |
| 39 | |
| 40 virtual ~RegistryOverrideManagerTest() { | |
| 41 base::win::RegKey key(HKEY_CURRENT_USER); | |
| 42 key.DeleteKey(key_to_delete_on_exit_.c_str()); | |
| 43 } | |
| 44 | |
| 45 protected: | |
| 46 void AssertKeyExists(const string16& key_path) { | |
| 47 base::win::RegKey key; | |
| 48 ASSERT_EQ(ERROR_SUCCESS, | |
|
grt (UTC plus 2)
2013/11/07 02:39:33
as per http://www.chromium.org/developers/coding-s
tommycli
2013/11/07 18:02:07
Done.
| |
| 49 key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ)) << | |
| 50 key_path << " does not exist."; | |
| 51 } | |
| 52 | |
| 53 void AssertKeyAbsent(const string16& key_path) { | |
| 54 base::win::RegKey key; | |
| 55 ASSERT_NE(ERROR_SUCCESS, | |
| 56 key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ)) << | |
| 57 key_path << " exists but it should not."; | |
| 58 } | |
| 59 | |
| 60 void CreateKey(const string16& key_path) { | |
| 61 base::win::RegKey key; | |
| 62 EXPECT_EQ(ERROR_SUCCESS, | |
| 63 key.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS)); | |
| 64 } | |
| 65 | |
| 66 string16 FakeOverrideManagerPath(const base::Time& time) { | |
| 67 return fake_test_key_root_ + L"\\" + | |
| 68 base::Int64ToString16(time.ToInternalValue()); | |
| 69 } | |
| 70 | |
| 71 void CreateManager(const base::Time& timestamp) { | |
| 72 manager_.reset(new RegistryOverrideManager(timestamp, fake_test_key_root_)); | |
| 73 manager_->OverrideRegistry(HKEY_CURRENT_USER, L"override_manager_unittest"); | |
| 74 } | |
| 75 | |
| 76 string16 key_to_delete_on_exit_; | |
| 77 string16 fake_test_key_root_; | |
| 78 scoped_ptr<RegistryOverrideManager> manager_; | |
| 79 }; | |
| 80 | |
| 81 TEST_F(RegistryOverrideManagerTest, Basic) { | |
| 82 CreateManager(base::Time::Now()); | |
| 83 | |
| 84 base::win::RegKey create_key; | |
| 85 EXPECT_EQ(ERROR_SUCCESS, | |
| 86 create_key.Create(HKEY_CURRENT_USER, kTestKeyPath, KEY_ALL_ACCESS)); | |
|
grt (UTC plus 2)
2013/11/07 02:39:33
align with arg on previous line as per http://goog
tommycli
2013/11/07 18:02:07
Done.
grt (UTC plus 2)
2013/11/08 04:33:05
really? i don't see a change here. for example:
tommycli
2013/11/08 16:24:00
Sorry I must have missed it. Patchsets 6 and 7 sho
| |
| 87 EXPECT_TRUE(create_key.Valid()); | |
| 88 EXPECT_EQ(ERROR_SUCCESS, create_key.WriteValue(kTestValueName, 42)); | |
| 89 create_key.Close(); | |
| 90 | |
| 91 AssertKeyExists(kTestKeyPath); | |
| 92 | |
| 93 DWORD value; | |
| 94 base::win::RegKey read_key; | |
| 95 EXPECT_EQ(ERROR_SUCCESS, | |
| 96 read_key.Open(HKEY_CURRENT_USER, kTestKeyPath, KEY_READ)); | |
| 97 EXPECT_TRUE(read_key.Valid()); | |
| 98 EXPECT_EQ(ERROR_SUCCESS, read_key.ReadValueDW(kTestValueName, &value)); | |
| 99 EXPECT_EQ(42, value); | |
| 100 read_key.Close(); | |
| 101 | |
| 102 manager_.reset(); | |
| 103 | |
| 104 AssertKeyAbsent(kTestKeyPath); | |
| 105 } | |
| 106 | |
| 107 TEST_F(RegistryOverrideManagerTest, DeleteStaleKeys) { | |
| 108 base::Time::Exploded kTestTimeExploded = {2013, 11, 1, 4, 0, 0, 0, 0}; | |
| 109 base::Time kTestTime = base::Time::FromUTCExploded(kTestTimeExploded); | |
| 110 | |
| 111 string16 path_garbage = | |
| 112 fake_test_key_root_ + L"\\Blah"; | |
| 113 string16 path_very_stale = | |
| 114 FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromDays(100)); | |
| 115 string16 path_stale = | |
| 116 FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromDays(5)); | |
| 117 string16 path_current = | |
| 118 FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromMinutes(1)); | |
| 119 string16 path_future = | |
| 120 FakeOverrideManagerPath(kTestTime + base::TimeDelta::FromMinutes(1)); | |
| 121 | |
| 122 CreateKey(path_garbage); | |
| 123 CreateKey(path_very_stale); | |
| 124 CreateKey(path_stale); | |
| 125 CreateKey(path_current); | |
| 126 CreateKey(path_future); | |
| 127 | |
| 128 CreateManager(kTestTime); | |
| 129 manager_.reset(); | |
| 130 | |
| 131 AssertKeyAbsent(path_garbage); | |
| 132 AssertKeyAbsent(path_very_stale); | |
| 133 AssertKeyAbsent(path_stale); | |
| 134 AssertKeyExists(path_current); | |
| 135 AssertKeyExists(path_future); | |
| 136 } | |
| 137 | |
| 138 } // namespace registry_util | |
| OLD | NEW |