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/memory/scoped_ptr.h"
8 #include "base/strings/string16.h"
9 #include "base/test/test_reg_util_win.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 kTestKey
[] = L
"DeleteRegValueWorkItemTest";
20 const wchar_t kNameStr
[] = L
"name_str";
21 const wchar_t kNameDword
[] = L
"name_dword";
23 class DeleteRegValueWorkItemTest
: public testing::Test
{
25 DeleteRegValueWorkItemTest() {}
27 void SetUp() override
{
28 registry_override_manager_
.OverrideRegistry(HKEY_CURRENT_USER
);
32 registry_util::RegistryOverrideManager registry_override_manager_
;
34 DISALLOW_COPY_AND_ASSIGN(DeleteRegValueWorkItemTest
);
39 // Delete a value. The value should get deleted after Do() and should be
40 // recreated after Rollback().
41 TEST_F(DeleteRegValueWorkItemTest
, DeleteExistingValue
) {
43 ASSERT_EQ(ERROR_SUCCESS
,
44 key
.Create(HKEY_CURRENT_USER
, kTestKey
, KEY_READ
| KEY_WRITE
));
45 const base::string16
data_str(L
"data_111");
46 ASSERT_EQ(ERROR_SUCCESS
, key
.WriteValue(kNameStr
, data_str
.c_str()));
47 const DWORD data_dword
= 100;
48 ASSERT_EQ(ERROR_SUCCESS
, key
.WriteValue(kNameDword
, data_dword
));
50 static const wchar_t kNameEmpty
[](L
"name_empty");
51 ASSERT_EQ(ERROR_SUCCESS
,
52 RegSetValueEx(key
.Handle(), kNameEmpty
, NULL
, REG_SZ
, NULL
, 0));
54 scoped_ptr
<DeleteRegValueWorkItem
> work_item1(
55 WorkItem::CreateDeleteRegValueWorkItem(
56 HKEY_CURRENT_USER
, kTestKey
, WorkItem::kWow64Default
, kNameStr
));
57 scoped_ptr
<DeleteRegValueWorkItem
> work_item2(
58 WorkItem::CreateDeleteRegValueWorkItem(
59 HKEY_CURRENT_USER
, kTestKey
, WorkItem::kWow64Default
, kNameDword
));
60 scoped_ptr
<DeleteRegValueWorkItem
> work_item3(
61 WorkItem::CreateDeleteRegValueWorkItem(
62 HKEY_CURRENT_USER
, kTestKey
, WorkItem::kWow64Default
, kNameEmpty
));
64 EXPECT_TRUE(key
.HasValue(kNameStr
));
65 EXPECT_TRUE(key
.HasValue(kNameDword
));
66 EXPECT_TRUE(key
.HasValue(kNameEmpty
));
68 EXPECT_TRUE(work_item1
->Do());
69 EXPECT_TRUE(work_item2
->Do());
70 EXPECT_TRUE(work_item3
->Do());
72 EXPECT_FALSE(key
.HasValue(kNameStr
));
73 EXPECT_FALSE(key
.HasValue(kNameDword
));
74 EXPECT_FALSE(key
.HasValue(kNameEmpty
));
76 work_item1
->Rollback();
77 work_item2
->Rollback();
78 work_item3
->Rollback();
80 EXPECT_TRUE(key
.HasValue(kNameStr
));
81 EXPECT_TRUE(key
.HasValue(kNameDword
));
82 EXPECT_TRUE(key
.HasValue(kNameEmpty
));
84 base::string16 read_str
;
86 EXPECT_EQ(ERROR_SUCCESS
, key
.ReadValue(kNameStr
, &read_str
));
87 EXPECT_EQ(ERROR_SUCCESS
, key
.ReadValueDW(kNameDword
, &read_dword
));
88 EXPECT_EQ(read_str
, data_str
);
89 EXPECT_EQ(read_dword
, data_dword
);
91 // Verify empty value.
94 EXPECT_EQ(ERROR_SUCCESS
, key
.ReadValue(kNameEmpty
, NULL
, &size
, &type
));
95 EXPECT_EQ(REG_SZ
, type
);
99 // Try deleting a value that doesn't exist.
100 TEST_F(DeleteRegValueWorkItemTest
, DeleteNonExistentValue
) {
102 ASSERT_EQ(ERROR_SUCCESS
,
103 key
.Create(HKEY_CURRENT_USER
, kTestKey
, KEY_READ
| KEY_WRITE
));
104 EXPECT_FALSE(key
.HasValue(kNameStr
));
105 EXPECT_FALSE(key
.HasValue(kNameDword
));
107 scoped_ptr
<DeleteRegValueWorkItem
> work_item1(
108 WorkItem::CreateDeleteRegValueWorkItem(
109 HKEY_CURRENT_USER
, kTestKey
, WorkItem::kWow64Default
, kNameStr
));
110 scoped_ptr
<DeleteRegValueWorkItem
> work_item2(
111 WorkItem::CreateDeleteRegValueWorkItem(
112 HKEY_CURRENT_USER
, kTestKey
, WorkItem::kWow64Default
, kNameDword
));
114 EXPECT_TRUE(work_item1
->Do());
115 EXPECT_TRUE(work_item2
->Do());
117 EXPECT_FALSE(key
.HasValue(kNameStr
));
118 EXPECT_FALSE(key
.HasValue(kNameDword
));
120 work_item1
->Rollback();
121 work_item2
->Rollback();
123 EXPECT_FALSE(key
.HasValue(kNameStr
));
124 EXPECT_FALSE(key
.HasValue(kNameDword
));
127 // Try deleting a value whose key doesn't even exist.
128 TEST_F(DeleteRegValueWorkItemTest
, DeleteValueInNonExistentKey
) {
130 // Confirm the key doesn't exist.
131 ASSERT_EQ(ERROR_FILE_NOT_FOUND
,
132 key
.Open(HKEY_CURRENT_USER
, kTestKey
, KEY_READ
));
134 scoped_ptr
<DeleteRegValueWorkItem
> work_item1(
135 WorkItem::CreateDeleteRegValueWorkItem(
136 HKEY_CURRENT_USER
, kTestKey
, WorkItem::kWow64Default
, kNameStr
));
137 scoped_ptr
<DeleteRegValueWorkItem
> work_item2(
138 WorkItem::CreateDeleteRegValueWorkItem(
139 HKEY_CURRENT_USER
, kTestKey
, WorkItem::kWow64Default
, kNameDword
));
141 EXPECT_TRUE(work_item1
->Do());
142 EXPECT_TRUE(work_item2
->Do());
144 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
145 key
.Open(HKEY_CURRENT_USER
, kTestKey
, KEY_READ
));
147 work_item1
->Rollback();
148 work_item2
->Rollback();
150 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
151 key
.Open(HKEY_CURRENT_USER
, kTestKey
, KEY_READ
));