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
3 // found in the LICENSE file.
7 #include "base/files/file_path.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_util.h"
10 #include "base/win/registry.h"
11 #include "chrome/installer/util/delete_reg_value_work_item.h"
12 #include "chrome/installer/util/work_item.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::win::RegKey
;
19 const wchar_t kTestRoot
[] = L
"DeleteRegValueWorkItemTest";
20 const wchar_t kWriteNew
[] = L
"WriteNew";
21 const wchar_t kNameStr
[] = L
"name_str";
22 const wchar_t kNameDword
[] = L
"name_dword";
24 class DeleteRegValueWorkItemTest
: public testing::Test
{
26 virtual void SetUp() {
27 // Create a temporary key for testing
28 RegKey
key(HKEY_CURRENT_USER
, L
"", KEY_ALL_ACCESS
);
29 key
.DeleteKey(kTestRoot
);
30 ASSERT_NE(ERROR_SUCCESS
, key
.Open(HKEY_CURRENT_USER
, kTestRoot
, KEY_READ
));
31 ASSERT_EQ(ERROR_SUCCESS
,
32 key
.Create(HKEY_CURRENT_USER
, kTestRoot
, KEY_READ
));
34 virtual void TearDown() {
35 logging::CloseLogFile();
36 // Clean up the temporary key
37 RegKey
key(HKEY_CURRENT_USER
, L
"", KEY_ALL_ACCESS
);
38 ASSERT_EQ(ERROR_SUCCESS
, key
.DeleteKey(kTestRoot
));
44 // Delete a value. The value should get deleted after Do() and should be
45 // recreated after Rollback().
46 TEST_F(DeleteRegValueWorkItemTest
, DeleteExistingValue
) {
48 std::wstring
parent_key(kTestRoot
);
49 parent_key
.push_back(base::FilePath::kSeparators
[0]);
50 parent_key
.append(kWriteNew
);
51 ASSERT_EQ(ERROR_SUCCESS
,
52 key
.Create(HKEY_CURRENT_USER
, parent_key
.c_str(), KEY_READ
| KEY_WRITE
));
53 std::wstring
name_str(kNameStr
);
54 std::wstring
data_str(L
"data_111");
55 ASSERT_EQ(ERROR_SUCCESS
, key
.WriteValue(name_str
.c_str(), data_str
.c_str()));
56 std::wstring
name_dword(kNameDword
);
57 DWORD data_dword
= 100;
58 ASSERT_EQ(ERROR_SUCCESS
, key
.WriteValue(name_dword
.c_str(), data_dword
));
60 std::wstring
name_empty(L
"name_empty");
61 ASSERT_EQ(ERROR_SUCCESS
, RegSetValueEx(key
.Handle(), name_empty
.c_str(), NULL
,
64 scoped_ptr
<DeleteRegValueWorkItem
> work_item1(
65 WorkItem::CreateDeleteRegValueWorkItem(
66 HKEY_CURRENT_USER
, parent_key
, WorkItem::kWow64Default
, name_str
));
67 scoped_ptr
<DeleteRegValueWorkItem
> work_item2(
68 WorkItem::CreateDeleteRegValueWorkItem(
69 HKEY_CURRENT_USER
, parent_key
, WorkItem::kWow64Default
, name_dword
));
70 scoped_ptr
<DeleteRegValueWorkItem
> work_item3(
71 WorkItem::CreateDeleteRegValueWorkItem(
72 HKEY_CURRENT_USER
, parent_key
, WorkItem::kWow64Default
, name_empty
));
74 EXPECT_TRUE(work_item1
->Do());
75 EXPECT_TRUE(work_item2
->Do());
76 EXPECT_TRUE(work_item3
->Do());
78 EXPECT_FALSE(key
.HasValue(name_str
.c_str()));
79 EXPECT_FALSE(key
.HasValue(name_dword
.c_str()));
80 EXPECT_FALSE(key
.HasValue(name_empty
.c_str()));
82 work_item1
->Rollback();
83 work_item2
->Rollback();
84 work_item3
->Rollback();
86 std::wstring read_str
;
88 EXPECT_EQ(ERROR_SUCCESS
, key
.ReadValue(name_str
.c_str(), &read_str
));
89 EXPECT_EQ(ERROR_SUCCESS
, key
.ReadValueDW(name_dword
.c_str(), &read_dword
));
90 EXPECT_EQ(read_str
, data_str
);
91 EXPECT_EQ(read_dword
, data_dword
);
93 // Verify empty value.
96 EXPECT_EQ(ERROR_SUCCESS
, key
.ReadValue(name_empty
.c_str(), NULL
, &size
,
98 EXPECT_EQ(REG_SZ
, type
);
102 // Try deleting a value that doesn't exist.
103 TEST_F(DeleteRegValueWorkItemTest
, DeleteNonExistentValue
) {
105 std::wstring
parent_key(kTestRoot
);
106 parent_key
.push_back(base::FilePath::kSeparators
[0]);
107 parent_key
.append(kWriteNew
);
108 ASSERT_EQ(ERROR_SUCCESS
,
109 key
.Create(HKEY_CURRENT_USER
, parent_key
.c_str(), KEY_READ
| KEY_WRITE
));
110 std::wstring
name_str(kNameStr
);
111 std::wstring
name_dword(kNameDword
);
112 EXPECT_FALSE(key
.HasValue(name_str
.c_str()));
113 EXPECT_FALSE(key
.HasValue(name_dword
.c_str()));
115 scoped_ptr
<DeleteRegValueWorkItem
> work_item1(
116 WorkItem::CreateDeleteRegValueWorkItem(
117 HKEY_CURRENT_USER
, parent_key
, WorkItem::kWow64Default
, name_str
));
118 scoped_ptr
<DeleteRegValueWorkItem
> work_item2(
119 WorkItem::CreateDeleteRegValueWorkItem(
120 HKEY_CURRENT_USER
, parent_key
, WorkItem::kWow64Default
, name_dword
));
122 EXPECT_TRUE(work_item1
->Do());
123 EXPECT_TRUE(work_item2
->Do());
125 EXPECT_FALSE(key
.HasValue(name_str
.c_str()));
126 EXPECT_FALSE(key
.HasValue(name_dword
.c_str()));
128 work_item1
->Rollback();
129 work_item2
->Rollback();
131 EXPECT_FALSE(key
.HasValue(name_str
.c_str()));
132 EXPECT_FALSE(key
.HasValue(name_dword
.c_str()));