Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: chrome/installer/util/work_item_list_unittest.cc

Issue 1869383003: Simplify WorkItemListTest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use gmock Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <windows.h> 5 #include "chrome/installer/util/work_item_list.h"
6 6
7 #include "base/base_paths.h" 7 #include <memory>
8 #include "base/files/file_util.h" 8
9 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/macros.h"
11 #include "base/strings/string_util.h"
12 #include "base/test/test_reg_util_win.h"
13 #include "base/win/registry.h"
14 #include "chrome/installer/util/conditional_work_item_list.h" 12 #include "chrome/installer/util/conditional_work_item_list.h"
15 #include "chrome/installer/util/work_item.h" 13 #include "chrome/installer/util/work_item.h"
16 #include "chrome/installer/util/work_item_list.h" 14 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
18 16
19 using base::win::RegKey;
20
21 namespace { 17 namespace {
22 18
23 const wchar_t kTestRoot[] = L"ListList"; 19 class MockWorkItem : public WorkItem {
24 const wchar_t kDataStr[] = L"data_111"; 20 public:
25 const wchar_t kName[] = L"name"; 21 MockWorkItem() = default;
26 22
27 class WorkItemListTest : public testing::Test { 23 MOCK_METHOD0(Do, bool());
28 protected: 24 MOCK_METHOD0(Rollback, void());
29 void SetUp() override {
30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
31 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER);
32 }
33 25
34 void TearDown() override { logging::CloseLogFile(); } 26 private:
27 DISALLOW_COPY_AND_ASSIGN(MockWorkItem);
28 };
35 29
36 base::ScopedTempDir temp_dir_; 30 using StrictMockWorkItem = testing::StrictMock<MockWorkItem>;
37 registry_util::RegistryOverrideManager registry_override_manager_; 31 using testing::InSequence;
38 }; 32 using testing::Return;
39 33
40 } // namespace 34 } // namespace
41 35
42 // Execute a WorkItem list successfully and then rollback. 36 // Execute a WorkItemList successfully and then rollback.
43 TEST_F(WorkItemListTest, ExecutionSuccess) { 37 TEST(WorkItemListTest, ExecutionSuccess) {
44 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); 38 std::unique_ptr<WorkItemList> list(WorkItem::CreateWorkItemList());
45 scoped_ptr<WorkItem> work_item;
46 39
47 base::FilePath top_dir_to_create(temp_dir_.path()); 40 // Create the mock work items.
48 top_dir_to_create = top_dir_to_create.AppendASCII("a"); 41 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem);
49 base::FilePath dir_to_create(top_dir_to_create); 42 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem);
50 dir_to_create = dir_to_create.AppendASCII("b"); 43 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem);
51 ASSERT_FALSE(base::PathExists(dir_to_create));
52 44
53 work_item.reset(reinterpret_cast<WorkItem*>( 45 {
54 WorkItem::CreateCreateDirWorkItem(dir_to_create))); 46 // Expect all three items to be done in order then undone.
55 work_item_list->AddWorkItem(work_item.release()); 47 InSequence s;
56 48
57 std::wstring key_to_create(kTestRoot); 49 EXPECT_CALL(*item1, Do()).WillOnce(Return(true));
58 key_to_create.push_back(base::FilePath::kSeparators[0]); 50 EXPECT_CALL(*item2, Do()).WillOnce(Return(true));
59 key_to_create.append(L"ExecutionSuccess"); 51 EXPECT_CALL(*item3, Do()).WillOnce(Return(true));
52 EXPECT_CALL(*item3, Rollback());
53 EXPECT_CALL(*item2, Rollback());
54 EXPECT_CALL(*item1, Rollback());
55 }
60 56
61 work_item.reset( 57 // Add the items to the list.
62 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem( 58 list->AddWorkItem(item1.release());
63 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default))); 59 list->AddWorkItem(item2.release());
64 work_item_list->AddWorkItem(work_item.release()); 60 list->AddWorkItem(item3.release());
65 61
66 std::wstring name(kName); 62 // Do and rollback the list.
67 std::wstring data(kDataStr); 63 EXPECT_TRUE(list->Do());
68 work_item.reset(reinterpret_cast<WorkItem*>( 64 list->Rollback();
69 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER,
70 key_to_create,
71 WorkItem::kWow64Default,
72 name,
73 data,
74 false)));
75 work_item_list->AddWorkItem(work_item.release());
76
77 EXPECT_TRUE(work_item_list->Do());
78
79 // Verify all WorkItems have been executed.
80 RegKey key;
81 EXPECT_EQ(ERROR_SUCCESS,
82 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
83 std::wstring read_out;
84 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(name.c_str(), &read_out));
85 EXPECT_EQ(0, read_out.compare(kDataStr));
86 key.Close();
87 EXPECT_TRUE(base::PathExists(dir_to_create));
88
89 work_item_list->Rollback();
90
91 // Verify everything is rolled back.
92 // The value must have been deleted first in roll back otherwise the key
93 // can not be deleted.
94 EXPECT_NE(ERROR_SUCCESS,
95 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
96 EXPECT_FALSE(base::PathExists(top_dir_to_create));
97 } 65 }
98 66
99 // Execute a WorkItem list. Fail in the middle. Rollback what has been done. 67 // Execute a WorkItemList. Fail in the middle. Rollback what has been done.
100 TEST_F(WorkItemListTest, ExecutionFailAndRollback) { 68 TEST(WorkItemListTest, ExecutionFailAndRollback) {
101 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); 69 std::unique_ptr<WorkItemList> list(WorkItem::CreateWorkItemList());
102 scoped_ptr<WorkItem> work_item;
103 70
104 base::FilePath top_dir_to_create(temp_dir_.path()); 71 // Create the mock work items.
105 top_dir_to_create = top_dir_to_create.AppendASCII("a"); 72 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem);
106 base::FilePath dir_to_create(top_dir_to_create); 73 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem);
107 dir_to_create = dir_to_create.AppendASCII("b"); 74 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem);
108 ASSERT_FALSE(base::PathExists(dir_to_create));
109 75
110 work_item.reset(reinterpret_cast<WorkItem*>( 76 {
111 WorkItem::CreateCreateDirWorkItem(dir_to_create))); 77 // Expect the two first work items to be done in order then undone.
112 work_item_list->AddWorkItem(work_item.release()); 78 InSequence s;
113 79
114 std::wstring key_to_create(kTestRoot); 80 EXPECT_CALL(*item1, Do()).WillOnce(Return(true));
115 key_to_create.push_back(base::FilePath::kSeparators[0]); 81 EXPECT_CALL(*item2, Do()).WillOnce(Return(false));
116 key_to_create.append(L"ExecutionFail"); 82 EXPECT_CALL(*item2, Rollback());
83 EXPECT_CALL(*item1, Rollback());
84 }
117 85
118 work_item.reset( 86 // Add the items to the list.
119 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem( 87 list->AddWorkItem(item1.release());
120 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default))); 88 list->AddWorkItem(item2.release());
121 work_item_list->AddWorkItem(work_item.release()); 89 list->AddWorkItem(item3.release());
122 90
123 std::wstring not_created_key(kTestRoot); 91 // Do and rollback the list.
124 not_created_key.push_back(base::FilePath::kSeparators[0]); 92 EXPECT_FALSE(list->Do());
125 not_created_key.append(L"NotCreated"); 93 list->Rollback();
126 std::wstring name(kName);
127 std::wstring data(kDataStr);
128 work_item.reset(reinterpret_cast<WorkItem*>(
129 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER,
130 not_created_key,
131 WorkItem::kWow64Default,
132 name,
133 data,
134 false)));
135 work_item_list->AddWorkItem(work_item.release());
136
137 // This one will not be executed because we will fail early.
138 work_item.reset(
139 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem(
140 HKEY_CURRENT_USER, not_created_key, WorkItem::kWow64Default)));
141 work_item_list->AddWorkItem(work_item.release());
142
143 EXPECT_FALSE(work_item_list->Do());
144
145 // Verify the first 2 WorkItems have been executed.
146 RegKey key;
147 EXPECT_EQ(ERROR_SUCCESS,
148 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
149 key.Close();
150 EXPECT_TRUE(base::PathExists(dir_to_create));
151 // The last one should not be there.
152 EXPECT_NE(ERROR_SUCCESS,
153 key.Open(HKEY_CURRENT_USER, not_created_key.c_str(), KEY_READ));
154
155 work_item_list->Rollback();
156
157 // Verify everything is rolled back.
158 EXPECT_NE(ERROR_SUCCESS,
159 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
160 EXPECT_FALSE(base::PathExists(top_dir_to_create));
161 } 94 }
162 95
163 TEST_F(WorkItemListTest, ConditionalExecutionSuccess) { 96 // Execute a ConditionalWorkItemList whose condition is met and then rollback.
164 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); 97 TEST(WorkItemListTest, ConditionalExecutionSuccess) {
165 scoped_ptr<WorkItem> work_item; 98 base::ScopedTempDir temp_dir;
99 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
166 100
167 base::FilePath top_dir_to_create(temp_dir_.path()); 101 std::unique_ptr<WorkItemList> list(WorkItem::CreateConditionalWorkItemList(
168 top_dir_to_create = top_dir_to_create.AppendASCII("a"); 102 new ConditionRunIfFileExists(temp_dir.path())));
grt (UTC plus 2) 2016/04/13 15:37:07 this is testing both CWIL and CRIFFE. could the la
fdoray 2016/04/13 16:46:45 Done.
grt (UTC plus 2) 2016/04/13 18:08:51 Sorry, forgot to mention before that these belong
fdoray 2016/04/13 18:57:16 Done.
169 base::FilePath dir_to_create(top_dir_to_create);
170 dir_to_create = dir_to_create.AppendASCII("b");
171 ASSERT_FALSE(base::PathExists(dir_to_create));
172 103
173 work_item.reset(reinterpret_cast<WorkItem*>( 104 // Create the mock work items.
174 WorkItem::CreateCreateDirWorkItem(dir_to_create))); 105 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem);
175 work_item_list->AddWorkItem(work_item.release()); 106 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem);
107 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem);
176 108
177 scoped_ptr<WorkItemList> conditional_work_item_list( 109 {
178 WorkItem::CreateConditionalWorkItemList( 110 // Expect all three items to be done in order then undone.
179 new ConditionRunIfFileExists(dir_to_create))); 111 InSequence s;
180 112
181 std::wstring key_to_create(kTestRoot); 113 EXPECT_CALL(*item1, Do()).WillOnce(Return(true));
182 key_to_create.push_back(base::FilePath::kSeparators[0]); 114 EXPECT_CALL(*item2, Do()).WillOnce(Return(true));
183 key_to_create.append(L"ExecutionSuccess"); 115 EXPECT_CALL(*item3, Do()).WillOnce(Return(true));
184 work_item.reset( 116 EXPECT_CALL(*item3, Rollback());
185 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem( 117 EXPECT_CALL(*item2, Rollback());
186 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default))); 118 EXPECT_CALL(*item1, Rollback());
187 conditional_work_item_list->AddWorkItem(work_item.release()); 119 }
188 120
189 std::wstring name(kName); 121 // Add the items to the list.
190 std::wstring data(kDataStr); 122 list->AddWorkItem(item1.release());
191 work_item.reset(reinterpret_cast<WorkItem*>( 123 list->AddWorkItem(item2.release());
192 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER, 124 list->AddWorkItem(item3.release());
193 key_to_create,
194 WorkItem::kWow64Default,
195 name,
196 data,
197 false)));
198 conditional_work_item_list->AddWorkItem(work_item.release());
199 125
200 work_item_list->AddWorkItem(conditional_work_item_list.release()); 126 // Do and rollback the list.
201 127 EXPECT_TRUE(list->Do());
202 EXPECT_TRUE(work_item_list->Do()); 128 list->Rollback();
203
204 // Verify all WorkItems have been executed.
205 RegKey key;
206 EXPECT_EQ(ERROR_SUCCESS,
207 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
208 std::wstring read_out;
209 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(name.c_str(), &read_out));
210 EXPECT_EQ(0, read_out.compare(kDataStr));
211 key.Close();
212 EXPECT_TRUE(base::PathExists(dir_to_create));
213
214 work_item_list->Rollback();
215
216 // Verify everything is rolled back.
217 // The value must have been deleted first in roll back otherwise the key
218 // can not be deleted.
219 EXPECT_NE(ERROR_SUCCESS,
220 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
221 EXPECT_FALSE(base::PathExists(top_dir_to_create));
222 } 129 }
223 130
224 TEST_F(WorkItemListTest, ConditionalExecutionConditionFailure) { 131 // Execute a ConditionalWorkItemList whose condition is met. Fail in the middle.
225 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); 132 // Rollback what has been done.
226 scoped_ptr<WorkItem> work_item; 133 TEST(WorkItemListTest, ConditionalExecutionFailAndRollback) {
134 base::ScopedTempDir temp_dir;
135 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
227 136
228 base::FilePath top_dir_to_create(temp_dir_.path()); 137 std::unique_ptr<WorkItemList> list(WorkItem::CreateConditionalWorkItemList(
229 top_dir_to_create = top_dir_to_create.AppendASCII("a"); 138 new ConditionRunIfFileExists(temp_dir.path())));
230 base::FilePath dir_to_create(top_dir_to_create);
231 dir_to_create = dir_to_create.AppendASCII("b");
232 ASSERT_FALSE(base::PathExists(dir_to_create));
233 139
234 work_item.reset(reinterpret_cast<WorkItem*>( 140 // Create the mock work items.
235 WorkItem::CreateCreateDirWorkItem(dir_to_create))); 141 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem);
236 work_item_list->AddWorkItem(work_item.release()); 142 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem);
143 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem);
237 144
238 scoped_ptr<WorkItemList> conditional_work_item_list( 145 {
239 WorkItem::CreateConditionalWorkItemList( 146 // Expect the two first work items to be done in order then undone.
240 new ConditionRunIfFileExists(dir_to_create.AppendASCII("c")))); 147 InSequence s;
241 148
242 std::wstring key_to_create(kTestRoot); 149 EXPECT_CALL(*item1, Do()).WillOnce(Return(true));
243 key_to_create.push_back(base::FilePath::kSeparators[0]); 150 EXPECT_CALL(*item2, Do()).WillOnce(Return(false));
244 key_to_create.append(L"ExecutionSuccess"); 151 EXPECT_CALL(*item2, Rollback());
245 work_item.reset( 152 EXPECT_CALL(*item1, Rollback());
246 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem( 153 }
247 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default)));
248 conditional_work_item_list->AddWorkItem(work_item.release());
249 154
250 std::wstring name(kName); 155 // Add the items to the list.
251 std::wstring data(kDataStr); 156 list->AddWorkItem(item1.release());
252 work_item.reset(reinterpret_cast<WorkItem*>( 157 list->AddWorkItem(item2.release());
253 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER, 158 list->AddWorkItem(item3.release());
254 key_to_create,
255 WorkItem::kWow64Default,
256 name,
257 data,
258 false)));
259 conditional_work_item_list->AddWorkItem(work_item.release());
260 159
261 work_item_list->AddWorkItem(conditional_work_item_list.release()); 160 // Do and rollback the list.
161 EXPECT_FALSE(list->Do());
162 list->Rollback();
163 }
262 164
263 EXPECT_TRUE(work_item_list->Do()); 165 // Execute a ConditionalWorkItemList whose condition isn't met.
166 TEST(WorkItemListTest, ConditionalExecutionConditionFailure) {
167 base::ScopedTempDir temp_dir;
168 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
264 169
265 // Verify that the WorkItems added as part of the conditional list have NOT 170 std::unique_ptr<WorkItemList> list(
266 // been executed. 171 WorkItem::CreateConditionalWorkItemList(new ConditionRunIfFileExists(
267 RegKey key; 172 temp_dir.path().Append(FILE_PATH_LITERAL("DoesNotExist")))));
268 EXPECT_NE(ERROR_SUCCESS,
269 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
270 std::wstring read_out;
271 EXPECT_NE(ERROR_SUCCESS, key.ReadValue(name.c_str(), &read_out));
272 key.Close();
273 173
274 // Verify that the other work item was executed. 174 // Create the mock work items.
275 EXPECT_TRUE(base::PathExists(dir_to_create)); 175 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem);
176 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem);
177 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem);
276 178
277 work_item_list->Rollback(); 179 // Don't expect any call to the methods of the work items.
278 180
279 // Verify everything is rolled back. 181 // Add the items to the list.
280 // The value must have been deleted first in roll back otherwise the key 182 list->AddWorkItem(item1.release());
281 // can not be deleted. 183 list->AddWorkItem(item2.release());
282 EXPECT_NE(ERROR_SUCCESS, 184 list->AddWorkItem(item3.release());
283 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); 185
284 EXPECT_FALSE(base::PathExists(top_dir_to_create)); 186 // Do and rollback the list.
187 EXPECT_TRUE(list->Do());
188 list->Rollback();
285 } 189 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698