Chromium Code Reviews| 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 <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/scoped_temp_dir.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.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" | |
| 15 #include "chrome/installer/util/work_item.h" | 10 #include "chrome/installer/util/work_item.h" |
| 16 #include "chrome/installer/util/work_item_list.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 13 |
| 19 using base::win::RegKey; | |
| 20 | |
| 21 namespace { | 14 namespace { |
| 22 | 15 |
| 23 const wchar_t kTestRoot[] = L"ListList"; | 16 class MockWorkItem : public WorkItem { |
|
grt (UTC plus 2)
2016/04/14 14:42:22
how about pulling this out into something like wor
fdoray
2016/04/18 13:04:46
Done.
| |
| 24 const wchar_t kDataStr[] = L"data_111"; | 17 public: |
| 25 const wchar_t kName[] = L"name"; | 18 MockWorkItem() = default; |
| 26 | 19 |
| 27 class WorkItemListTest : public testing::Test { | 20 MOCK_METHOD0(Do, bool()); |
| 28 protected: | 21 MOCK_METHOD0(Rollback, void()); |
| 29 void SetUp() override { | |
| 30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 31 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER); | |
| 32 } | |
| 33 | 22 |
| 34 void TearDown() override { logging::CloseLogFile(); } | 23 private: |
| 24 DISALLOW_COPY_AND_ASSIGN(MockWorkItem); | |
| 25 }; | |
| 35 | 26 |
| 36 base::ScopedTempDir temp_dir_; | 27 using StrictMockWorkItem = testing::StrictMock<MockWorkItem>; |
| 37 registry_util::RegistryOverrideManager registry_override_manager_; | 28 using testing::InSequence; |
| 38 }; | 29 using testing::Return; |
| 39 | 30 |
| 40 } // namespace | 31 } // namespace |
| 41 | 32 |
| 42 // Execute a WorkItem list successfully and then rollback. | 33 // Execute a WorkItemList successfully and then rollback. |
| 43 TEST_F(WorkItemListTest, ExecutionSuccess) { | 34 TEST(WorkItemListTest, ExecutionSuccess) { |
| 44 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); | 35 std::unique_ptr<WorkItemList> list(WorkItem::CreateWorkItemList()); |
| 45 scoped_ptr<WorkItem> work_item; | |
| 46 | 36 |
| 47 base::FilePath top_dir_to_create(temp_dir_.path()); | 37 // Create the mock work items. |
| 48 top_dir_to_create = top_dir_to_create.AppendASCII("a"); | 38 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); |
| 49 base::FilePath dir_to_create(top_dir_to_create); | 39 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); |
| 50 dir_to_create = dir_to_create.AppendASCII("b"); | 40 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); |
| 51 ASSERT_FALSE(base::PathExists(dir_to_create)); | |
| 52 | 41 |
| 53 work_item.reset(reinterpret_cast<WorkItem*>( | 42 { |
| 54 WorkItem::CreateCreateDirWorkItem(dir_to_create))); | 43 // Expect all three items to be done in order then undone. |
| 55 work_item_list->AddWorkItem(work_item.release()); | 44 InSequence s; |
| 56 | 45 |
| 57 std::wstring key_to_create(kTestRoot); | 46 EXPECT_CALL(*item1, Do()).WillOnce(Return(true)); |
| 58 key_to_create.push_back(base::FilePath::kSeparators[0]); | 47 EXPECT_CALL(*item2, Do()).WillOnce(Return(true)); |
| 59 key_to_create.append(L"ExecutionSuccess"); | 48 EXPECT_CALL(*item3, Do()).WillOnce(Return(true)); |
| 49 EXPECT_CALL(*item3, Rollback()); | |
| 50 EXPECT_CALL(*item2, Rollback()); | |
| 51 EXPECT_CALL(*item1, Rollback()); | |
| 52 } | |
| 60 | 53 |
| 61 work_item.reset( | 54 // Add the items to the list. |
| 62 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem( | 55 list->AddWorkItem(item1.release()); |
| 63 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default))); | 56 list->AddWorkItem(item2.release()); |
| 64 work_item_list->AddWorkItem(work_item.release()); | 57 list->AddWorkItem(item3.release()); |
| 65 | 58 |
| 66 std::wstring name(kName); | 59 // Do and rollback the list. |
| 67 std::wstring data(kDataStr); | 60 EXPECT_TRUE(list->Do()); |
| 68 work_item.reset(reinterpret_cast<WorkItem*>( | 61 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 } | 62 } |
| 98 | 63 |
| 99 // Execute a WorkItem list. Fail in the middle. Rollback what has been done. | 64 // Execute a WorkItemList. Fail in the middle. Rollback what has been done. |
| 100 TEST_F(WorkItemListTest, ExecutionFailAndRollback) { | 65 TEST(WorkItemListTest, ExecutionFailAndRollback) { |
| 101 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); | 66 std::unique_ptr<WorkItemList> list(WorkItem::CreateWorkItemList()); |
| 102 scoped_ptr<WorkItem> work_item; | |
| 103 | 67 |
| 104 base::FilePath top_dir_to_create(temp_dir_.path()); | 68 // Create the mock work items. |
| 105 top_dir_to_create = top_dir_to_create.AppendASCII("a"); | 69 std::unique_ptr<StrictMockWorkItem> item1(new StrictMockWorkItem); |
| 106 base::FilePath dir_to_create(top_dir_to_create); | 70 std::unique_ptr<StrictMockWorkItem> item2(new StrictMockWorkItem); |
| 107 dir_to_create = dir_to_create.AppendASCII("b"); | 71 std::unique_ptr<StrictMockWorkItem> item3(new StrictMockWorkItem); |
| 108 ASSERT_FALSE(base::PathExists(dir_to_create)); | |
| 109 | 72 |
| 110 work_item.reset(reinterpret_cast<WorkItem*>( | 73 { |
| 111 WorkItem::CreateCreateDirWorkItem(dir_to_create))); | 74 // Expect the two first work items to be done in order then undone. |
| 112 work_item_list->AddWorkItem(work_item.release()); | 75 InSequence s; |
| 113 | 76 |
| 114 std::wstring key_to_create(kTestRoot); | 77 EXPECT_CALL(*item1, Do()).WillOnce(Return(true)); |
| 115 key_to_create.push_back(base::FilePath::kSeparators[0]); | 78 EXPECT_CALL(*item2, Do()).WillOnce(Return(false)); |
| 116 key_to_create.append(L"ExecutionFail"); | 79 EXPECT_CALL(*item2, Rollback()); |
| 80 EXPECT_CALL(*item1, Rollback()); | |
| 81 } | |
| 117 | 82 |
| 118 work_item.reset( | 83 // Add the items to the list. |
| 119 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem( | 84 list->AddWorkItem(item1.release()); |
| 120 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default))); | 85 list->AddWorkItem(item2.release()); |
| 121 work_item_list->AddWorkItem(work_item.release()); | 86 list->AddWorkItem(item3.release()); |
| 122 | 87 |
| 123 std::wstring not_created_key(kTestRoot); | 88 // Do and rollback the list. |
| 124 not_created_key.push_back(base::FilePath::kSeparators[0]); | 89 EXPECT_FALSE(list->Do()); |
| 125 not_created_key.append(L"NotCreated"); | 90 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 } | 91 } |
| 162 | |
| 163 TEST_F(WorkItemListTest, ConditionalExecutionSuccess) { | |
| 164 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); | |
| 165 scoped_ptr<WorkItem> work_item; | |
| 166 | |
| 167 base::FilePath top_dir_to_create(temp_dir_.path()); | |
| 168 top_dir_to_create = top_dir_to_create.AppendASCII("a"); | |
| 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 | |
| 173 work_item.reset(reinterpret_cast<WorkItem*>( | |
| 174 WorkItem::CreateCreateDirWorkItem(dir_to_create))); | |
| 175 work_item_list->AddWorkItem(work_item.release()); | |
| 176 | |
| 177 scoped_ptr<WorkItemList> conditional_work_item_list( | |
| 178 WorkItem::CreateConditionalWorkItemList( | |
| 179 new ConditionRunIfFileExists(dir_to_create))); | |
| 180 | |
| 181 std::wstring key_to_create(kTestRoot); | |
| 182 key_to_create.push_back(base::FilePath::kSeparators[0]); | |
| 183 key_to_create.append(L"ExecutionSuccess"); | |
| 184 work_item.reset( | |
| 185 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem( | |
| 186 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default))); | |
| 187 conditional_work_item_list->AddWorkItem(work_item.release()); | |
| 188 | |
| 189 std::wstring name(kName); | |
| 190 std::wstring data(kDataStr); | |
| 191 work_item.reset(reinterpret_cast<WorkItem*>( | |
| 192 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER, | |
| 193 key_to_create, | |
| 194 WorkItem::kWow64Default, | |
| 195 name, | |
| 196 data, | |
| 197 false))); | |
| 198 conditional_work_item_list->AddWorkItem(work_item.release()); | |
| 199 | |
| 200 work_item_list->AddWorkItem(conditional_work_item_list.release()); | |
| 201 | |
| 202 EXPECT_TRUE(work_item_list->Do()); | |
| 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 } | |
| 223 | |
| 224 TEST_F(WorkItemListTest, ConditionalExecutionConditionFailure) { | |
| 225 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); | |
| 226 scoped_ptr<WorkItem> work_item; | |
| 227 | |
| 228 base::FilePath top_dir_to_create(temp_dir_.path()); | |
| 229 top_dir_to_create = top_dir_to_create.AppendASCII("a"); | |
| 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 | |
| 234 work_item.reset(reinterpret_cast<WorkItem*>( | |
| 235 WorkItem::CreateCreateDirWorkItem(dir_to_create))); | |
| 236 work_item_list->AddWorkItem(work_item.release()); | |
| 237 | |
| 238 scoped_ptr<WorkItemList> conditional_work_item_list( | |
| 239 WorkItem::CreateConditionalWorkItemList( | |
| 240 new ConditionRunIfFileExists(dir_to_create.AppendASCII("c")))); | |
| 241 | |
| 242 std::wstring key_to_create(kTestRoot); | |
| 243 key_to_create.push_back(base::FilePath::kSeparators[0]); | |
| 244 key_to_create.append(L"ExecutionSuccess"); | |
| 245 work_item.reset( | |
| 246 reinterpret_cast<WorkItem*>(WorkItem::CreateCreateRegKeyWorkItem( | |
| 247 HKEY_CURRENT_USER, key_to_create, WorkItem::kWow64Default))); | |
| 248 conditional_work_item_list->AddWorkItem(work_item.release()); | |
| 249 | |
| 250 std::wstring name(kName); | |
| 251 std::wstring data(kDataStr); | |
| 252 work_item.reset(reinterpret_cast<WorkItem*>( | |
| 253 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER, | |
| 254 key_to_create, | |
| 255 WorkItem::kWow64Default, | |
| 256 name, | |
| 257 data, | |
| 258 false))); | |
| 259 conditional_work_item_list->AddWorkItem(work_item.release()); | |
| 260 | |
| 261 work_item_list->AddWorkItem(conditional_work_item_list.release()); | |
| 262 | |
| 263 EXPECT_TRUE(work_item_list->Do()); | |
| 264 | |
| 265 // Verify that the WorkItems added as part of the conditional list have NOT | |
| 266 // been executed. | |
| 267 RegKey key; | |
| 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 | |
| 274 // Verify that the other work item was executed. | |
| 275 EXPECT_TRUE(base::PathExists(dir_to_create)); | |
| 276 | |
| 277 work_item_list->Rollback(); | |
| 278 | |
| 279 // Verify everything is rolled back. | |
| 280 // The value must have been deleted first in roll back otherwise the key | |
| 281 // can not be deleted. | |
| 282 EXPECT_NE(ERROR_SUCCESS, | |
| 283 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); | |
| 284 EXPECT_FALSE(base::PathExists(top_dir_to_create)); | |
| 285 } | |
| OLD | NEW |