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

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

Issue 6090006: Regkey functions return error code instead of bool (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/path_service.h" 9 #include "base/path_service.h"
10 #include "base/scoped_ptr.h" 10 #include "base/scoped_ptr.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/win/registry.h" 12 #include "base/win/registry.h"
13 #include "chrome/installer/util/work_item.h" 13 #include "chrome/installer/util/work_item.h"
14 #include "chrome/installer/util/work_item_list.h" 14 #include "chrome/installer/util/work_item_list.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 using base::win::RegKey; 17 using base::win::RegKey;
18 18
19 namespace { 19 namespace {
20 20
21 wchar_t test_root[] = L"ListList"; 21 wchar_t test_root[] = L"ListList";
22 wchar_t data_str[] = L"data_111"; 22 wchar_t data_str[] = L"data_111";
23 23
24 class WorkItemListTest : public testing::Test { 24 class WorkItemListTest : public testing::Test {
25 protected: 25 protected:
26 virtual void SetUp() { 26 virtual void SetUp() {
27 // Create a temporary key for testing 27 // Create a temporary key for testing
28 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS); 28 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS);
29 key.DeleteKey(test_root); 29 key.DeleteKey(test_root);
30 ASSERT_FALSE(key.Open(HKEY_CURRENT_USER, test_root, KEY_READ)); 30 ASSERT_NE(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, test_root, KEY_READ));
31 ASSERT_TRUE(key.Create(HKEY_CURRENT_USER, test_root, KEY_READ)); 31 ASSERT_EQ(ERROR_SUCCESS,
32 key.Create(HKEY_CURRENT_USER, test_root, KEY_READ));
32 33
33 // Create a temp directory for test. 34 // Create a temp directory for test.
34 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); 35 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
35 test_dir_ = test_dir_.AppendASCII("WorkItemListTest"); 36 test_dir_ = test_dir_.AppendASCII("WorkItemListTest");
36 file_util::Delete(test_dir_, true); 37 file_util::Delete(test_dir_, true);
37 ASSERT_FALSE(file_util::PathExists(test_dir_)); 38 ASSERT_FALSE(file_util::PathExists(test_dir_));
38 file_util::CreateDirectoryW(test_dir_); 39 file_util::CreateDirectoryW(test_dir_);
39 ASSERT_TRUE(file_util::PathExists(test_dir_)); 40 ASSERT_TRUE(file_util::PathExists(test_dir_));
40 } 41 }
41 42
42 virtual void TearDown() { 43 virtual void TearDown() {
43 logging::CloseLogFile(); 44 logging::CloseLogFile();
44 // Clean up test directory 45 // Clean up test directory
45 ASSERT_TRUE(file_util::Delete(test_dir_, true)); 46 ASSERT_TRUE(file_util::Delete(test_dir_, true));
46 ASSERT_FALSE(file_util::PathExists(test_dir_)); 47 ASSERT_FALSE(file_util::PathExists(test_dir_));
47 // Clean up the temporary key 48 // Clean up the temporary key
48 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS); 49 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS);
49 ASSERT_TRUE(key.DeleteKey(test_root)); 50 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(test_root));
50 } 51 }
51 52
52 FilePath test_dir_; 53 FilePath test_dir_;
53 }; 54 };
54 55
55 } // namespace 56 } // namespace
56 57
57 // Execute a WorkItem list successfully and then rollback. 58 // Execute a WorkItem list successfully and then rollback.
58 TEST_F(WorkItemListTest, ExecutionSuccess) { 59 TEST_F(WorkItemListTest, ExecutionSuccess) {
59 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); 60 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList());
(...skipping 20 matching lines...) Expand all
80 std::wstring data(data_str); 81 std::wstring data(data_str);
81 work_item.reset(reinterpret_cast<WorkItem*>( 82 work_item.reset(reinterpret_cast<WorkItem*>(
82 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER, key_to_create, 83 WorkItem::CreateSetRegValueWorkItem(HKEY_CURRENT_USER, key_to_create,
83 name, data, false))); 84 name, data, false)));
84 EXPECT_TRUE(work_item_list->AddWorkItem(work_item.release())); 85 EXPECT_TRUE(work_item_list->AddWorkItem(work_item.release()));
85 86
86 EXPECT_TRUE(work_item_list->Do()); 87 EXPECT_TRUE(work_item_list->Do());
87 88
88 // Verify all WorkItems have been executed. 89 // Verify all WorkItems have been executed.
89 RegKey key; 90 RegKey key;
90 EXPECT_TRUE(key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); 91 EXPECT_EQ(ERROR_SUCCESS,
92 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
91 std::wstring read_out; 93 std::wstring read_out;
92 EXPECT_TRUE(key.ReadValue(name.c_str(), &read_out)); 94 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(name.c_str(), &read_out));
93 EXPECT_EQ(0, read_out.compare(data_str)); 95 EXPECT_EQ(0, read_out.compare(data_str));
94 key.Close(); 96 key.Close();
95 EXPECT_TRUE(file_util::PathExists(dir_to_create)); 97 EXPECT_TRUE(file_util::PathExists(dir_to_create));
96 98
97 work_item_list->Rollback(); 99 work_item_list->Rollback();
98 100
99 // Verify everything is rolled back. 101 // Verify everything is rolled back.
100 // The value must have been deleted first in roll back otherwise the key 102 // The value must have been deleted first in roll back otherwise the key
101 // can not be deleted. 103 // can not be deleted.
102 EXPECT_FALSE(key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); 104 EXPECT_NE(ERROR_SUCCESS,
105 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
103 EXPECT_FALSE(file_util::PathExists(top_dir_to_create)); 106 EXPECT_FALSE(file_util::PathExists(top_dir_to_create));
104 } 107 }
105 108
106 // Execute a WorkItem list. Fail in the middle. Rollback what has been done. 109 // Execute a WorkItem list. Fail in the middle. Rollback what has been done.
107 TEST_F(WorkItemListTest, ExecutionFailAndRollback) { 110 TEST_F(WorkItemListTest, ExecutionFailAndRollback) {
108 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); 111 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList());
109 scoped_ptr<WorkItem> work_item; 112 scoped_ptr<WorkItem> work_item;
110 113
111 FilePath top_dir_to_create(test_dir_); 114 FilePath top_dir_to_create(test_dir_);
112 top_dir_to_create = top_dir_to_create.AppendASCII("a"); 115 top_dir_to_create = top_dir_to_create.AppendASCII("a");
(...skipping 24 matching lines...) Expand all
137 // This one will not be executed because we will fail early. 140 // This one will not be executed because we will fail early.
138 work_item.reset(reinterpret_cast<WorkItem*>( 141 work_item.reset(reinterpret_cast<WorkItem*>(
139 WorkItem::CreateCreateRegKeyWorkItem(HKEY_CURRENT_USER, 142 WorkItem::CreateCreateRegKeyWorkItem(HKEY_CURRENT_USER,
140 not_created_key))); 143 not_created_key)));
141 EXPECT_TRUE(work_item_list->AddWorkItem(work_item.release())); 144 EXPECT_TRUE(work_item_list->AddWorkItem(work_item.release()));
142 145
143 EXPECT_FALSE(work_item_list->Do()); 146 EXPECT_FALSE(work_item_list->Do());
144 147
145 // Verify the first 2 WorkItems have been executed. 148 // Verify the first 2 WorkItems have been executed.
146 RegKey key; 149 RegKey key;
147 EXPECT_TRUE(key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); 150 EXPECT_EQ(ERROR_SUCCESS,
151 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
148 key.Close(); 152 key.Close();
149 EXPECT_TRUE(file_util::PathExists(dir_to_create)); 153 EXPECT_TRUE(file_util::PathExists(dir_to_create));
150 // The last one should not be there. 154 // The last one should not be there.
151 EXPECT_FALSE(key.Open(HKEY_CURRENT_USER, not_created_key.c_str(), 155 EXPECT_NE(ERROR_SUCCESS,
152 KEY_READ)); 156 key.Open(HKEY_CURRENT_USER, not_created_key.c_str(), KEY_READ));
153 157
154 work_item_list->Rollback(); 158 work_item_list->Rollback();
155 159
156 // Verify everything is rolled back. 160 // Verify everything is rolled back.
157 EXPECT_FALSE(key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ)); 161 EXPECT_NE(ERROR_SUCCESS,
162 key.Open(HKEY_CURRENT_USER, key_to_create.c_str(), KEY_READ));
158 EXPECT_FALSE(file_util::PathExists(top_dir_to_create)); 163 EXPECT_FALSE(file_util::PathExists(top_dir_to_create));
159 } 164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698