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 <windows.h> | |
6 #include <shlwapi.h> // NOLINT | |
7 | |
8 #include "base/logging.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/win/registry.h" | |
11 #include "chrome/installer/util/copy_reg_key_work_item.h" | |
12 #include "chrome/installer/util/registry_test_data.h" | |
13 #include "chrome/installer/util/work_item.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 using base::win::RegKey; | |
17 | |
18 class CopyRegKeyWorkItemTest : public testing::Test { | |
19 protected: | |
20 static void TearDownTestCase() { | |
21 logging::CloseLogFile(); | |
22 } | |
23 | |
24 virtual void SetUp() { | |
25 ASSERT_TRUE(test_data_.Initialize(HKEY_CURRENT_USER, L"SOFTWARE\\TmpTmp")); | |
26 destination_path_.assign(test_data_.base_path()).append(L"\\Destination"); | |
27 } | |
28 | |
29 RegistryTestData test_data_; | |
30 std::wstring destination_path_; | |
31 }; | |
32 | |
33 // Test that copying a key that doesn't exist succeeds, and that rollback does | |
34 // nothing. | |
35 TEST_F(CopyRegKeyWorkItemTest, TestNoKey) { | |
36 const std::wstring key_paths[] = { | |
37 std::wstring(test_data_.base_path() + L"\\NoKeyHere"), | |
38 std::wstring(test_data_.base_path() + L"\\NoKeyHere\\OrHere") | |
39 }; | |
40 RegKey key; | |
41 for (size_t i = 0; i < arraysize(key_paths); ++i) { | |
42 const std::wstring& key_path = key_paths[i]; | |
43 scoped_ptr<CopyRegKeyWorkItem> item( | |
44 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), key_path, | |
45 destination_path_, | |
46 WorkItem::ALWAYS)); | |
47 EXPECT_TRUE(item->Do()); | |
48 EXPECT_NE(ERROR_SUCCESS, | |
49 key.Open(test_data_.root_key(), destination_path_.c_str(), | |
50 KEY_READ)); | |
51 item->Rollback(); | |
52 item.reset(); | |
53 EXPECT_NE(ERROR_SUCCESS, | |
54 key.Open(test_data_.root_key(), destination_path_.c_str(), | |
55 KEY_READ)); | |
56 } | |
57 } | |
58 | |
59 // Test that copying an empty key succeeds, and that rollback removes the copy. | |
60 TEST_F(CopyRegKeyWorkItemTest, TestEmptyKey) { | |
61 RegKey key; | |
62 scoped_ptr<CopyRegKeyWorkItem> item( | |
63 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), | |
64 test_data_.empty_key_path(), | |
65 destination_path_, | |
66 WorkItem::ALWAYS)); | |
67 EXPECT_TRUE(item->Do()); | |
68 RegistryTestData::ExpectEmptyKey(test_data_.root_key(), | |
69 destination_path_.c_str()); | |
70 item->Rollback(); | |
71 item.reset(); | |
72 EXPECT_NE(ERROR_SUCCESS, | |
73 key.Open(test_data_.root_key(), destination_path_.c_str(), | |
74 KEY_READ)); | |
75 } | |
76 | |
77 // Test that copying a key with subkeys and values succeeds, and that rollback | |
78 // removes the copy. | |
79 TEST_F(CopyRegKeyWorkItemTest, TestNonEmptyKey) { | |
80 RegKey key; | |
81 scoped_ptr<CopyRegKeyWorkItem> item( | |
82 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), | |
83 test_data_.non_empty_key_path(), | |
84 destination_path_, | |
85 WorkItem::ALWAYS)); | |
86 EXPECT_TRUE(item->Do()); | |
87 test_data_.ExpectMatchesNonEmptyKey(test_data_.root_key(), | |
88 destination_path_.c_str()); | |
89 item->Rollback(); | |
90 item.reset(); | |
91 EXPECT_NE(ERROR_SUCCESS, | |
92 key.Open(test_data_.root_key(), destination_path_.c_str(), | |
93 KEY_READ)); | |
94 } | |
95 | |
96 // Test that existing data isn't overwritten. | |
97 TEST_F(CopyRegKeyWorkItemTest, TestNoOverwrite) { | |
98 RegKey key; | |
99 // First copy the data into the dest. | |
100 EXPECT_EQ(ERROR_SUCCESS, | |
101 key.Create(test_data_.root_key(), destination_path_.c_str(), | |
102 KEY_WRITE)); | |
103 EXPECT_EQ(ERROR_SUCCESS, | |
104 SHCopyKey(test_data_.root_key(), | |
105 test_data_.non_empty_key_path().c_str(), key.Handle(), | |
106 0)); | |
107 key.Close(); | |
108 | |
109 // Now copy the empty key into the dest, which should do nothing. | |
110 scoped_ptr<CopyRegKeyWorkItem> item( | |
111 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), | |
112 test_data_.empty_key_path(), | |
113 destination_path_, | |
114 WorkItem::IF_NOT_PRESENT)); | |
115 EXPECT_TRUE(item->Do()); | |
116 | |
117 // Make sure it's all there. | |
118 test_data_.ExpectMatchesNonEmptyKey(test_data_.root_key(), | |
119 destination_path_.c_str()); | |
120 | |
121 // Rollback should do nothing. | |
122 item->Rollback(); | |
123 item.reset(); | |
124 | |
125 // Make sure it's still all there. | |
126 test_data_.ExpectMatchesNonEmptyKey(test_data_.root_key(), | |
127 destination_path_.c_str()); | |
128 } | |
129 | |
130 // Test that copying an empty key over a key with data succeeds, and that | |
131 // rollback restores the original data. | |
132 TEST_F(CopyRegKeyWorkItemTest, TestOverwriteAndRestore) { | |
133 RegKey key; | |
134 // First copy the data into the dest. | |
135 EXPECT_EQ(ERROR_SUCCESS, | |
136 key.Create(test_data_.root_key(), destination_path_.c_str(), | |
137 KEY_WRITE)); | |
138 EXPECT_EQ(ERROR_SUCCESS, | |
139 SHCopyKey(test_data_.root_key(), | |
140 test_data_.non_empty_key_path().c_str(), key.Handle(), | |
141 0)); | |
142 key.Close(); | |
143 | |
144 // Now copy the empty key into the dest. | |
145 scoped_ptr<CopyRegKeyWorkItem> item( | |
146 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), | |
147 test_data_.empty_key_path(), | |
148 destination_path_, | |
149 WorkItem::ALWAYS)); | |
150 EXPECT_TRUE(item->Do()); | |
151 | |
152 // Make sure the dest is now empty. | |
153 RegistryTestData::ExpectEmptyKey(test_data_.root_key(), | |
154 destination_path_.c_str()); | |
155 | |
156 // Restore the data. | |
157 item->Rollback(); | |
158 item.reset(); | |
159 | |
160 // Make sure it's all there. | |
161 test_data_.ExpectMatchesNonEmptyKey(test_data_.root_key(), | |
162 destination_path_.c_str()); | |
163 } | |
164 | |
165 // Test that Rollback does nothing when the item is configured to ignore | |
166 // failures. | |
167 TEST_F(CopyRegKeyWorkItemTest, TestIgnoreFailRollback) { | |
168 // Copy the empty key onto the non-empty key, ignoring failures. | |
169 scoped_ptr<CopyRegKeyWorkItem> item( | |
170 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), | |
171 test_data_.empty_key_path(), | |
172 test_data_.non_empty_key_path(), | |
173 WorkItem::ALWAYS)); | |
174 item->set_ignore_failure(true); | |
175 EXPECT_TRUE(item->Do()); | |
176 item->Rollback(); | |
177 item.reset(); | |
178 RegistryTestData::ExpectEmptyKey(test_data_.root_key(), | |
179 test_data_.non_empty_key_path().c_str()); | |
180 } | |
OLD | NEW |