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
3 // found in the LICENSE file.
6 #include <shlwapi.h> // NOLINT
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/win/registry.h"
11 #include "chrome/installer/util/copy_reg_key_work_item.h"
12 #include "chrome/installer/util/registry_test_data.h"
13 #include "chrome/installer/util/work_item.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::win::RegKey
;
18 class CopyRegKeyWorkItemTest
: public testing::Test
{
20 static void TearDownTestCase() {
21 logging::CloseLogFile();
24 virtual void SetUp() {
25 ASSERT_TRUE(test_data_
.Initialize(HKEY_CURRENT_USER
, L
"SOFTWARE\\TmpTmp"));
26 destination_path_
.assign(test_data_
.base_path()).append(L
"\\Destination");
29 RegistryTestData test_data_
;
30 std::wstring destination_path_
;
33 // Test that copying a key that doesn't exist succeeds, and that rollback does
35 TEST_F(CopyRegKeyWorkItemTest
, TestNoKey
) {
36 const std::wstring key_paths
[] = {
37 std::wstring(test_data_
.base_path() + L
"\\NoKeyHere"),
38 std::wstring(test_data_
.base_path() + L
"\\NoKeyHere\\OrHere")
41 for (size_t i
= 0; i
< arraysize(key_paths
); ++i
) {
42 const std::wstring
& key_path
= key_paths
[i
];
43 scoped_ptr
<CopyRegKeyWorkItem
> item(
44 WorkItem::CreateCopyRegKeyWorkItem(test_data_
.root_key(), key_path
,
47 EXPECT_TRUE(item
->Do());
48 EXPECT_NE(ERROR_SUCCESS
,
49 key
.Open(test_data_
.root_key(), destination_path_
.c_str(),
53 EXPECT_NE(ERROR_SUCCESS
,
54 key
.Open(test_data_
.root_key(), destination_path_
.c_str(),
59 // Test that copying an empty key succeeds, and that rollback removes the copy.
60 TEST_F(CopyRegKeyWorkItemTest
, TestEmptyKey
) {
62 scoped_ptr
<CopyRegKeyWorkItem
> item(
63 WorkItem::CreateCopyRegKeyWorkItem(test_data_
.root_key(),
64 test_data_
.empty_key_path(),
67 EXPECT_TRUE(item
->Do());
68 RegistryTestData::ExpectEmptyKey(test_data_
.root_key(),
69 destination_path_
.c_str());
72 EXPECT_NE(ERROR_SUCCESS
,
73 key
.Open(test_data_
.root_key(), destination_path_
.c_str(),
77 // Test that copying a key with subkeys and values succeeds, and that rollback
79 TEST_F(CopyRegKeyWorkItemTest
, TestNonEmptyKey
) {
81 scoped_ptr
<CopyRegKeyWorkItem
> item(
82 WorkItem::CreateCopyRegKeyWorkItem(test_data_
.root_key(),
83 test_data_
.non_empty_key_path(),
86 EXPECT_TRUE(item
->Do());
87 test_data_
.ExpectMatchesNonEmptyKey(test_data_
.root_key(),
88 destination_path_
.c_str());
91 EXPECT_NE(ERROR_SUCCESS
,
92 key
.Open(test_data_
.root_key(), destination_path_
.c_str(),
96 // Test that existing data isn't overwritten.
97 TEST_F(CopyRegKeyWorkItemTest
, TestNoOverwrite
) {
99 // First copy the data into the dest.
100 EXPECT_EQ(ERROR_SUCCESS
,
101 key
.Create(test_data_
.root_key(), destination_path_
.c_str(),
103 EXPECT_EQ(ERROR_SUCCESS
,
104 SHCopyKey(test_data_
.root_key(),
105 test_data_
.non_empty_key_path().c_str(), key
.Handle(),
109 // Now copy the empty key into the dest, which should do nothing.
110 scoped_ptr
<CopyRegKeyWorkItem
> item(
111 WorkItem::CreateCopyRegKeyWorkItem(test_data_
.root_key(),
112 test_data_
.empty_key_path(),
114 WorkItem::IF_NOT_PRESENT
));
115 EXPECT_TRUE(item
->Do());
117 // Make sure it's all there.
118 test_data_
.ExpectMatchesNonEmptyKey(test_data_
.root_key(),
119 destination_path_
.c_str());
121 // Rollback should do nothing.
125 // Make sure it's still all there.
126 test_data_
.ExpectMatchesNonEmptyKey(test_data_
.root_key(),
127 destination_path_
.c_str());
130 // Test that copying an empty key over a key with data succeeds, and that
131 // rollback restores the original data.
132 TEST_F(CopyRegKeyWorkItemTest
, TestOverwriteAndRestore
) {
134 // First copy the data into the dest.
135 EXPECT_EQ(ERROR_SUCCESS
,
136 key
.Create(test_data_
.root_key(), destination_path_
.c_str(),
138 EXPECT_EQ(ERROR_SUCCESS
,
139 SHCopyKey(test_data_
.root_key(),
140 test_data_
.non_empty_key_path().c_str(), key
.Handle(),
144 // Now copy the empty key into the dest.
145 scoped_ptr
<CopyRegKeyWorkItem
> item(
146 WorkItem::CreateCopyRegKeyWorkItem(test_data_
.root_key(),
147 test_data_
.empty_key_path(),
150 EXPECT_TRUE(item
->Do());
152 // Make sure the dest is now empty.
153 RegistryTestData::ExpectEmptyKey(test_data_
.root_key(),
154 destination_path_
.c_str());
160 // Make sure it's all there.
161 test_data_
.ExpectMatchesNonEmptyKey(test_data_
.root_key(),
162 destination_path_
.c_str());
165 // Test that Rollback does nothing when the item is configured to ignore
167 TEST_F(CopyRegKeyWorkItemTest
, TestIgnoreFailRollback
) {
168 // Copy the empty key onto the non-empty key, ignoring failures.
169 scoped_ptr
<CopyRegKeyWorkItem
> item(
170 WorkItem::CreateCopyRegKeyWorkItem(test_data_
.root_key(),
171 test_data_
.empty_key_path(),
172 test_data_
.non_empty_key_path(),
174 item
->set_ignore_failure(true);
175 EXPECT_TRUE(item
->Do());
178 RegistryTestData::ExpectEmptyKey(test_data_
.root_key(),
179 test_data_
.non_empty_key_path().c_str());