Chromium Code Reviews| Index: base/test/test_reg_util_win_unittest.cc |
| diff --git a/base/test/test_reg_util_win_unittest.cc b/base/test/test_reg_util_win_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5876f552a28164bbb3db8f776f3c169e8f41cbda |
| --- /dev/null |
| +++ b/base/test/test_reg_util_win_unittest.cc |
| @@ -0,0 +1,138 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/compiler_specific.h" |
| +#include "base/guid.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/test/test_reg_util_win.h" |
| +#include "base/time/time.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace registry_util { |
| + |
| +namespace { |
| +const wchar_t kTestKeyPath[] = L"Software\\Chromium\\Foo\\Baz\\TestKey"; |
| +const wchar_t kTestValueName[] = L"TestValue"; |
| +} // namespace |
| + |
| +class RegistryOverrideManagerTest : public testing::Test { |
| + 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.
|
| + RegistryOverrideManagerTest() { |
| + // We assign a fake test key path to our test RegistryOverrideManager |
| + // so we don't interfere with any actual RegistryOverrideManagers running |
| + // on the system. This fake path will be auto-deleted by other |
| + // RegistryOverrideManagers in case we crash. |
| + key_to_delete_on_exit_ = |
| + string16(RegistryOverrideManager::kTempTestKeyPath) + |
| + L"\\" + base::Int64ToString16(base::Time::Now().ToInternalValue()); |
| + fake_test_key_root_ = key_to_delete_on_exit_ + L"\\" + |
| + base::ASCIIToWide(base::GenerateGUID()); |
| + |
| + // Ensure a clean test environment. |
| + base::win::RegKey key(HKEY_CURRENT_USER); |
| + key.DeleteKey(fake_test_key_root_.c_str()); |
| + key.DeleteKey(kTestKeyPath); |
| + } |
| + |
| + virtual ~RegistryOverrideManagerTest() { |
| + base::win::RegKey key(HKEY_CURRENT_USER); |
| + key.DeleteKey(key_to_delete_on_exit_.c_str()); |
| + } |
| + |
| + protected: |
| + void AssertKeyExists(const string16& key_path) { |
| + base::win::RegKey key; |
| + 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.
|
| + key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ)) << |
| + key_path << " does not exist."; |
| + } |
| + |
| + void AssertKeyAbsent(const string16& key_path) { |
| + base::win::RegKey key; |
| + ASSERT_NE(ERROR_SUCCESS, |
| + key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ)) << |
| + key_path << " exists but it should not."; |
| + } |
| + |
| + void CreateKey(const string16& key_path) { |
| + base::win::RegKey key; |
| + EXPECT_EQ(ERROR_SUCCESS, |
| + key.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS)); |
| + } |
| + |
| + string16 FakeOverrideManagerPath(const base::Time& time) { |
| + return fake_test_key_root_ + L"\\" + |
| + base::Int64ToString16(time.ToInternalValue()); |
| + } |
| + |
| + void CreateManager(const base::Time& timestamp) { |
| + manager_.reset(new RegistryOverrideManager(timestamp, fake_test_key_root_)); |
| + manager_->OverrideRegistry(HKEY_CURRENT_USER, L"override_manager_unittest"); |
| + } |
| + |
| + string16 key_to_delete_on_exit_; |
| + string16 fake_test_key_root_; |
| + scoped_ptr<RegistryOverrideManager> manager_; |
| +}; |
| + |
| +TEST_F(RegistryOverrideManagerTest, Basic) { |
| + CreateManager(base::Time::Now()); |
| + |
| + base::win::RegKey create_key; |
| + EXPECT_EQ(ERROR_SUCCESS, |
| + 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
|
| + EXPECT_TRUE(create_key.Valid()); |
| + EXPECT_EQ(ERROR_SUCCESS, create_key.WriteValue(kTestValueName, 42)); |
| + create_key.Close(); |
| + |
| + AssertKeyExists(kTestKeyPath); |
| + |
| + DWORD value; |
| + base::win::RegKey read_key; |
| + EXPECT_EQ(ERROR_SUCCESS, |
| + read_key.Open(HKEY_CURRENT_USER, kTestKeyPath, KEY_READ)); |
| + EXPECT_TRUE(read_key.Valid()); |
| + EXPECT_EQ(ERROR_SUCCESS, read_key.ReadValueDW(kTestValueName, &value)); |
| + EXPECT_EQ(42, value); |
| + read_key.Close(); |
| + |
| + manager_.reset(); |
| + |
| + AssertKeyAbsent(kTestKeyPath); |
| +} |
| + |
| +TEST_F(RegistryOverrideManagerTest, DeleteStaleKeys) { |
| + base::Time::Exploded kTestTimeExploded = {2013, 11, 1, 4, 0, 0, 0, 0}; |
| + base::Time kTestTime = base::Time::FromUTCExploded(kTestTimeExploded); |
| + |
| + string16 path_garbage = |
| + fake_test_key_root_ + L"\\Blah"; |
| + string16 path_very_stale = |
| + FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromDays(100)); |
| + string16 path_stale = |
| + FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromDays(5)); |
| + string16 path_current = |
| + FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromMinutes(1)); |
| + string16 path_future = |
| + FakeOverrideManagerPath(kTestTime + base::TimeDelta::FromMinutes(1)); |
| + |
| + CreateKey(path_garbage); |
| + CreateKey(path_very_stale); |
| + CreateKey(path_stale); |
| + CreateKey(path_current); |
| + CreateKey(path_future); |
| + |
| + CreateManager(kTestTime); |
| + manager_.reset(); |
| + |
| + AssertKeyAbsent(path_garbage); |
| + AssertKeyAbsent(path_very_stale); |
| + AssertKeyAbsent(path_stale); |
| + AssertKeyExists(path_current); |
| + AssertKeyExists(path_future); |
| +} |
| + |
| +} // namespace registry_util |