| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 <windows.h> |
| 6 | 6 |
| 7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/scoped_temp_dir.h" |
| 10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 11 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 12 #include "base/win/registry.h" | 13 #include "base/win/registry.h" |
| 13 #include "chrome/installer/util/conditional_work_item_list.h" | 14 #include "chrome/installer/util/conditional_work_item_list.h" |
| 14 #include "chrome/installer/util/work_item.h" | 15 #include "chrome/installer/util/work_item.h" |
| 15 #include "chrome/installer/util/work_item_list.h" | 16 #include "chrome/installer/util/work_item_list.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 using base::win::RegKey; | 19 using base::win::RegKey; |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 wchar_t test_root[] = L"ListList"; | 23 wchar_t test_root[] = L"ListList"; |
| 23 wchar_t data_str[] = L"data_111"; | 24 wchar_t data_str[] = L"data_111"; |
| 24 | 25 |
| 25 class WorkItemListTest : public testing::Test { | 26 class WorkItemListTest : public testing::Test { |
| 26 protected: | 27 protected: |
| 27 virtual void SetUp() { | 28 virtual void SetUp() { |
| 28 // Create a temporary key for testing | 29 // Create a temporary key for testing |
| 29 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS); | 30 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS); |
| 30 key.DeleteKey(test_root); | 31 key.DeleteKey(test_root); |
| 31 ASSERT_NE(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, test_root, KEY_READ)); | 32 ASSERT_NE(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, test_root, KEY_READ)); |
| 32 ASSERT_EQ(ERROR_SUCCESS, | 33 ASSERT_EQ(ERROR_SUCCESS, |
| 33 key.Create(HKEY_CURRENT_USER, test_root, KEY_READ)); | 34 key.Create(HKEY_CURRENT_USER, test_root, KEY_READ)); |
| 34 | 35 |
| 35 // Create a temp directory for test. | 36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 36 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | |
| 37 test_dir_ = test_dir_.AppendASCII("WorkItemListTest"); | |
| 38 file_util::Delete(test_dir_, true); | |
| 39 ASSERT_FALSE(file_util::PathExists(test_dir_)); | |
| 40 file_util::CreateDirectoryW(test_dir_); | |
| 41 ASSERT_TRUE(file_util::PathExists(test_dir_)); | |
| 42 } | 37 } |
| 43 | 38 |
| 44 virtual void TearDown() { | 39 virtual void TearDown() { |
| 45 logging::CloseLogFile(); | 40 logging::CloseLogFile(); |
| 46 // Clean up test directory | 41 |
| 47 ASSERT_TRUE(file_util::Delete(test_dir_, true)); | |
| 48 ASSERT_FALSE(file_util::PathExists(test_dir_)); | |
| 49 // Clean up the temporary key | 42 // Clean up the temporary key |
| 50 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS); | 43 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS); |
| 51 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(test_root)); | 44 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(test_root)); |
| 52 } | 45 } |
| 53 | 46 |
| 54 FilePath test_dir_; | 47 ScopedTempDir temp_dir_; |
| 55 }; | 48 }; |
| 56 | 49 |
| 57 } // namespace | 50 } // namespace |
| 58 | 51 |
| 59 // Execute a WorkItem list successfully and then rollback. | 52 // Execute a WorkItem list successfully and then rollback. |
| 60 TEST_F(WorkItemListTest, ExecutionSuccess) { | 53 TEST_F(WorkItemListTest, ExecutionSuccess) { |
| 61 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); | 54 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); |
| 62 scoped_ptr<WorkItem> work_item; | 55 scoped_ptr<WorkItem> work_item; |
| 63 | 56 |
| 64 FilePath top_dir_to_create(test_dir_); | 57 FilePath top_dir_to_create(temp_dir_.path()); |
| 65 top_dir_to_create = top_dir_to_create.AppendASCII("a"); | 58 top_dir_to_create = top_dir_to_create.AppendASCII("a"); |
| 66 FilePath dir_to_create(top_dir_to_create); | 59 FilePath dir_to_create(top_dir_to_create); |
| 67 dir_to_create = dir_to_create.AppendASCII("b"); | 60 dir_to_create = dir_to_create.AppendASCII("b"); |
| 68 ASSERT_FALSE(file_util::PathExists(dir_to_create)); | 61 ASSERT_FALSE(file_util::PathExists(dir_to_create)); |
| 69 | 62 |
| 70 work_item.reset(reinterpret_cast<WorkItem*>( | 63 work_item.reset(reinterpret_cast<WorkItem*>( |
| 71 WorkItem::CreateCreateDirWorkItem(dir_to_create))); | 64 WorkItem::CreateCreateDirWorkItem(dir_to_create))); |
| 72 work_item_list->AddWorkItem(work_item.release()); | 65 work_item_list->AddWorkItem(work_item.release()); |
| 73 | 66 |
| 74 std::wstring key_to_create(test_root); | 67 std::wstring key_to_create(test_root); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 105 EXPECT_NE(ERROR_SUCCESS, | 98 EXPECT_NE(ERROR_SUCCESS, |
| 106 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); | 99 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); |
| 107 EXPECT_FALSE(file_util::PathExists(top_dir_to_create)); | 100 EXPECT_FALSE(file_util::PathExists(top_dir_to_create)); |
| 108 } | 101 } |
| 109 | 102 |
| 110 // Execute a WorkItem list. Fail in the middle. Rollback what has been done. | 103 // Execute a WorkItem list. Fail in the middle. Rollback what has been done. |
| 111 TEST_F(WorkItemListTest, ExecutionFailAndRollback) { | 104 TEST_F(WorkItemListTest, ExecutionFailAndRollback) { |
| 112 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); | 105 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); |
| 113 scoped_ptr<WorkItem> work_item; | 106 scoped_ptr<WorkItem> work_item; |
| 114 | 107 |
| 115 FilePath top_dir_to_create(test_dir_); | 108 FilePath top_dir_to_create(temp_dir_.path()); |
| 116 top_dir_to_create = top_dir_to_create.AppendASCII("a"); | 109 top_dir_to_create = top_dir_to_create.AppendASCII("a"); |
| 117 FilePath dir_to_create(top_dir_to_create); | 110 FilePath dir_to_create(top_dir_to_create); |
| 118 dir_to_create = dir_to_create.AppendASCII("b"); | 111 dir_to_create = dir_to_create.AppendASCII("b"); |
| 119 ASSERT_FALSE(file_util::PathExists(dir_to_create)); | 112 ASSERT_FALSE(file_util::PathExists(dir_to_create)); |
| 120 | 113 |
| 121 work_item.reset(reinterpret_cast<WorkItem*>( | 114 work_item.reset(reinterpret_cast<WorkItem*>( |
| 122 WorkItem::CreateCreateDirWorkItem(dir_to_create))); | 115 WorkItem::CreateCreateDirWorkItem(dir_to_create))); |
| 123 work_item_list->AddWorkItem(work_item.release()); | 116 work_item_list->AddWorkItem(work_item.release()); |
| 124 | 117 |
| 125 std::wstring key_to_create(test_root); | 118 std::wstring key_to_create(test_root); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 // Verify everything is rolled back. | 154 // Verify everything is rolled back. |
| 162 EXPECT_NE(ERROR_SUCCESS, | 155 EXPECT_NE(ERROR_SUCCESS, |
| 163 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); | 156 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); |
| 164 EXPECT_FALSE(file_util::PathExists(top_dir_to_create)); | 157 EXPECT_FALSE(file_util::PathExists(top_dir_to_create)); |
| 165 } | 158 } |
| 166 | 159 |
| 167 TEST_F(WorkItemListTest, ConditionalExecutionSuccess) { | 160 TEST_F(WorkItemListTest, ConditionalExecutionSuccess) { |
| 168 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); | 161 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); |
| 169 scoped_ptr<WorkItem> work_item; | 162 scoped_ptr<WorkItem> work_item; |
| 170 | 163 |
| 171 FilePath top_dir_to_create(test_dir_); | 164 FilePath top_dir_to_create(temp_dir_.path()); |
| 172 top_dir_to_create = top_dir_to_create.AppendASCII("a"); | 165 top_dir_to_create = top_dir_to_create.AppendASCII("a"); |
| 173 FilePath dir_to_create(top_dir_to_create); | 166 FilePath dir_to_create(top_dir_to_create); |
| 174 dir_to_create = dir_to_create.AppendASCII("b"); | 167 dir_to_create = dir_to_create.AppendASCII("b"); |
| 175 ASSERT_FALSE(file_util::PathExists(dir_to_create)); | 168 ASSERT_FALSE(file_util::PathExists(dir_to_create)); |
| 176 | 169 |
| 177 work_item.reset(reinterpret_cast<WorkItem*>( | 170 work_item.reset(reinterpret_cast<WorkItem*>( |
| 178 WorkItem::CreateCreateDirWorkItem(dir_to_create))); | 171 WorkItem::CreateCreateDirWorkItem(dir_to_create))); |
| 179 work_item_list->AddWorkItem(work_item.release()); | 172 work_item_list->AddWorkItem(work_item.release()); |
| 180 | 173 |
| 181 scoped_ptr<WorkItemList> conditional_work_item_list( | 174 scoped_ptr<WorkItemList> conditional_work_item_list( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 // can not be deleted. | 209 // can not be deleted. |
| 217 EXPECT_NE(ERROR_SUCCESS, | 210 EXPECT_NE(ERROR_SUCCESS, |
| 218 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); | 211 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); |
| 219 EXPECT_FALSE(file_util::PathExists(top_dir_to_create)); | 212 EXPECT_FALSE(file_util::PathExists(top_dir_to_create)); |
| 220 } | 213 } |
| 221 | 214 |
| 222 TEST_F(WorkItemListTest, ConditionalExecutionConditionFailure) { | 215 TEST_F(WorkItemListTest, ConditionalExecutionConditionFailure) { |
| 223 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); | 216 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); |
| 224 scoped_ptr<WorkItem> work_item; | 217 scoped_ptr<WorkItem> work_item; |
| 225 | 218 |
| 226 FilePath top_dir_to_create(test_dir_); | 219 FilePath top_dir_to_create(temp_dir_.path()); |
| 227 top_dir_to_create = top_dir_to_create.AppendASCII("a"); | 220 top_dir_to_create = top_dir_to_create.AppendASCII("a"); |
| 228 FilePath dir_to_create(top_dir_to_create); | 221 FilePath dir_to_create(top_dir_to_create); |
| 229 dir_to_create = dir_to_create.AppendASCII("b"); | 222 dir_to_create = dir_to_create.AppendASCII("b"); |
| 230 ASSERT_FALSE(file_util::PathExists(dir_to_create)); | 223 ASSERT_FALSE(file_util::PathExists(dir_to_create)); |
| 231 | 224 |
| 232 work_item.reset(reinterpret_cast<WorkItem*>( | 225 work_item.reset(reinterpret_cast<WorkItem*>( |
| 233 WorkItem::CreateCreateDirWorkItem(dir_to_create))); | 226 WorkItem::CreateCreateDirWorkItem(dir_to_create))); |
| 234 work_item_list->AddWorkItem(work_item.release()); | 227 work_item_list->AddWorkItem(work_item.release()); |
| 235 | 228 |
| 236 scoped_ptr<WorkItemList> conditional_work_item_list( | 229 scoped_ptr<WorkItemList> conditional_work_item_list( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 | 263 |
| 271 // Verify everything is rolled back. | 264 // Verify everything is rolled back. |
| 272 // The value must have been deleted first in roll back otherwise the key | 265 // The value must have been deleted first in roll back otherwise the key |
| 273 // can not be deleted. | 266 // can not be deleted. |
| 274 EXPECT_NE(ERROR_SUCCESS, | 267 EXPECT_NE(ERROR_SUCCESS, |
| 275 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); | 268 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); |
| 276 EXPECT_FALSE(file_util::PathExists(top_dir_to_create)); | 269 EXPECT_FALSE(file_util::PathExists(top_dir_to_create)); |
| 277 } | 270 } |
| 278 | 271 |
| 279 | 272 |
| OLD | NEW |