OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/test/test_reg_util_win.h" | 5 #include "base/test/test_reg_util_win.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 void AssertKeyAbsent(const base::string16& key_path) { | 49 void AssertKeyAbsent(const base::string16& key_path) { |
50 base::win::RegKey key; | 50 base::win::RegKey key; |
51 ASSERT_NE(ERROR_SUCCESS, | 51 ASSERT_NE(ERROR_SUCCESS, |
52 key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ)) | 52 key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ)) |
53 << key_path << " exists but it should not."; | 53 << key_path << " exists but it should not."; |
54 } | 54 } |
55 | 55 |
56 void CreateKey(const base::string16& key_path) { | 56 void CreateKey(const base::string16& key_path) { |
57 base::win::RegKey key; | 57 base::win::RegKey key; |
58 EXPECT_EQ(ERROR_SUCCESS, | 58 ASSERT_EQ(ERROR_SUCCESS, |
59 key.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS)); | 59 key.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS)); |
60 } | 60 } |
61 | 61 |
62 base::string16 FakeOverrideManagerPath(const base::Time& time) { | 62 base::string16 FakeOverrideManagerPath(const base::Time& time) { |
63 return fake_test_key_root_ + L"\\" + | 63 return fake_test_key_root_ + L"\\" + |
64 base::Int64ToString16(time.ToInternalValue()); | 64 base::Int64ToString16(time.ToInternalValue()); |
65 } | 65 } |
66 | 66 |
67 void CreateManager(const base::Time& timestamp) { | 67 void CreateManager(const base::Time& timestamp) { |
68 manager_.reset(new RegistryOverrideManager(timestamp, fake_test_key_root_)); | 68 manager_.reset(new RegistryOverrideManager(timestamp, fake_test_key_root_)); |
69 manager_->OverrideRegistry(HKEY_CURRENT_USER); | 69 manager_->OverrideRegistry(HKEY_CURRENT_USER); |
70 } | 70 } |
71 | 71 |
72 base::string16 fake_test_key_root_; | 72 base::string16 fake_test_key_root_; |
73 std::unique_ptr<RegistryOverrideManager> manager_; | 73 std::unique_ptr<RegistryOverrideManager> manager_; |
74 }; | 74 }; |
75 | 75 |
76 TEST_F(RegistryOverrideManagerTest, Basic) { | 76 TEST_F(RegistryOverrideManagerTest, Basic) { |
77 CreateManager(base::Time::Now()); | 77 ASSERT_NO_FATAL_FAILURE(CreateManager(base::Time::Now())); |
78 | 78 |
79 base::win::RegKey create_key; | 79 base::win::RegKey create_key; |
80 EXPECT_EQ(ERROR_SUCCESS, | 80 EXPECT_EQ(ERROR_SUCCESS, |
81 create_key.Create(HKEY_CURRENT_USER, kTestKeyPath, KEY_ALL_ACCESS)); | 81 create_key.Create(HKEY_CURRENT_USER, kTestKeyPath, KEY_ALL_ACCESS)); |
82 EXPECT_TRUE(create_key.Valid()); | 82 EXPECT_TRUE(create_key.Valid()); |
83 EXPECT_EQ(ERROR_SUCCESS, create_key.WriteValue(kTestValueName, 42)); | 83 EXPECT_EQ(ERROR_SUCCESS, create_key.WriteValue(kTestValueName, 42)); |
84 create_key.Close(); | 84 create_key.Close(); |
85 | 85 |
86 AssertKeyExists(kTestKeyPath); | 86 ASSERT_NO_FATAL_FAILURE(AssertKeyExists(kTestKeyPath)); |
87 | 87 |
88 DWORD value; | 88 DWORD value; |
89 base::win::RegKey read_key; | 89 base::win::RegKey read_key; |
90 EXPECT_EQ(ERROR_SUCCESS, | 90 EXPECT_EQ(ERROR_SUCCESS, |
91 read_key.Open(HKEY_CURRENT_USER, kTestKeyPath, KEY_READ)); | 91 read_key.Open(HKEY_CURRENT_USER, kTestKeyPath, KEY_READ)); |
92 EXPECT_TRUE(read_key.Valid()); | 92 EXPECT_TRUE(read_key.Valid()); |
93 EXPECT_EQ(ERROR_SUCCESS, read_key.ReadValueDW(kTestValueName, &value)); | 93 EXPECT_EQ(ERROR_SUCCESS, read_key.ReadValueDW(kTestValueName, &value)); |
94 EXPECT_EQ(42u, value); | 94 EXPECT_EQ(42u, value); |
95 read_key.Close(); | 95 read_key.Close(); |
96 | 96 |
97 manager_.reset(); | 97 manager_.reset(); |
98 | 98 |
99 AssertKeyAbsent(kTestKeyPath); | 99 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(kTestKeyPath)); |
100 } | 100 } |
101 | 101 |
102 TEST_F(RegistryOverrideManagerTest, DeleteStaleKeys) { | 102 TEST_F(RegistryOverrideManagerTest, DeleteStaleKeys) { |
103 base::Time::Exploded kTestTimeExploded = {2013, 11, 1, 4, 0, 0, 0, 0}; | 103 base::Time::Exploded kTestTimeExploded = {2013, 11, 1, 4, 0, 0, 0, 0}; |
104 base::Time kTestTime; | 104 base::Time kTestTime; |
105 EXPECT_TRUE(base::Time::FromUTCExploded(kTestTimeExploded, &kTestTime)); | 105 EXPECT_TRUE(base::Time::FromUTCExploded(kTestTimeExploded, &kTestTime)); |
106 | 106 |
107 base::string16 path_garbage = fake_test_key_root_ + L"\\Blah"; | 107 base::string16 path_garbage = fake_test_key_root_ + L"\\Blah"; |
108 base::string16 path_very_stale = | 108 base::string16 path_very_stale = |
109 FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromDays(100)); | 109 FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromDays(100)); |
110 base::string16 path_stale = | 110 base::string16 path_stale = |
111 FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromDays(5)); | 111 FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromDays(5)); |
112 base::string16 path_current = | 112 base::string16 path_current = |
113 FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromMinutes(1)); | 113 FakeOverrideManagerPath(kTestTime - base::TimeDelta::FromMinutes(1)); |
114 base::string16 path_future = | 114 base::string16 path_future = |
115 FakeOverrideManagerPath(kTestTime + base::TimeDelta::FromMinutes(1)); | 115 FakeOverrideManagerPath(kTestTime + base::TimeDelta::FromMinutes(1)); |
116 | 116 |
117 CreateKey(path_garbage); | 117 ASSERT_NO_FATAL_FAILURE(CreateKey(path_garbage)); |
118 CreateKey(path_very_stale); | 118 ASSERT_NO_FATAL_FAILURE(CreateKey(path_very_stale)); |
119 CreateKey(path_stale); | 119 ASSERT_NO_FATAL_FAILURE(CreateKey(path_stale)); |
120 CreateKey(path_current); | 120 ASSERT_NO_FATAL_FAILURE(CreateKey(path_current)); |
121 CreateKey(path_future); | 121 ASSERT_NO_FATAL_FAILURE(CreateKey(path_future)); |
122 | 122 |
123 CreateManager(kTestTime); | 123 ASSERT_NO_FATAL_FAILURE(CreateManager(kTestTime)); |
124 manager_.reset(); | 124 manager_.reset(); |
125 | 125 |
126 AssertKeyAbsent(path_garbage); | 126 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(path_garbage)); |
127 AssertKeyAbsent(path_very_stale); | 127 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(path_very_stale)); |
128 AssertKeyAbsent(path_stale); | 128 ASSERT_NO_FATAL_FAILURE(AssertKeyAbsent(path_stale)); |
129 AssertKeyExists(path_current); | 129 ASSERT_NO_FATAL_FAILURE(AssertKeyExists(path_current)); |
130 AssertKeyExists(path_future); | 130 ASSERT_NO_FATAL_FAILURE(AssertKeyExists(path_future)); |
131 } | 131 } |
132 | 132 |
133 } // namespace registry_util | 133 } // namespace registry_util |
OLD | NEW |