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