1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/win/registry.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/install_util.h"
#include "chrome/installer/util/product_unittest.h"
#include "testing/gmock/include/gmock/gmock.h"
using base::win::RegKey;
using ::testing::_;
using ::testing::Return;
using ::testing::StrEq;
class MockRegistryValuePredicate : public InstallUtil::RegistryValuePredicate {
public:
MOCK_CONST_METHOD1(Evaluate, bool(const std::wstring&));
};
class InstallUtilTest : public TestWithTempDirAndDeleteTempOverrideKeys {
protected:
};
TEST_F(InstallUtilTest, MakeUninstallCommand) {
CommandLine command_line(CommandLine::NO_PROGRAM);
std::pair<std::wstring, std::wstring> params[] = {
std::make_pair(std::wstring(L""), std::wstring(L"")),
std::make_pair(std::wstring(L""), std::wstring(L"--do-something --silly")),
std::make_pair(std::wstring(L"spam.exe"), std::wstring(L"")),
std::make_pair(std::wstring(L"spam.exe"),
std::wstring(L"--do-something --silly")),
};
for (int i = 0; i < arraysize(params); ++i) {
std::pair<std::wstring, std::wstring>& param = params[i];
InstallUtil::MakeUninstallCommand(param.first, param.second, &command_line);
EXPECT_EQ(param.first, command_line.GetProgram().value());
if (param.second.empty()) {
EXPECT_EQ(0U, command_line.GetSwitchCount());
} else {
EXPECT_EQ(2U, command_line.GetSwitchCount());
EXPECT_TRUE(command_line.HasSwitch("do-something"));
EXPECT_TRUE(command_line.HasSwitch("silly"));
}
}
}
TEST_F(InstallUtilTest, GetCurrentDate) {
std::wstring date(InstallUtil::GetCurrentDate());
EXPECT_EQ(8, date.length());
if (date.length() == 8) {
// For an invalid date value, SystemTimeToFileTime will fail.
// We use this to validate that we have a correct date string.
SYSTEMTIME systime = {0};
FILETIME ft = {0};
// Just to make sure our assumption holds.
EXPECT_FALSE(SystemTimeToFileTime(&systime, &ft));
// Now fill in the values from our string.
systime.wYear = _wtoi(date.substr(0, 4).c_str());
systime.wMonth = _wtoi(date.substr(4, 2).c_str());
systime.wDay = _wtoi(date.substr(6, 2).c_str());
// Check if they make sense.
EXPECT_TRUE(SystemTimeToFileTime(&systime, &ft));
}
}
TEST_F(InstallUtilTest, UpdateInstallerStage) {
const bool system_level = false;
const HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
std::wstring state_key_path(L"PhonyClientState");
// Update the stage when there's no "ap" value.
{
TempRegKeyOverride override(root, L"root_inst_res");
RegKey(root, state_key_path.c_str(), KEY_SET_VALUE);
InstallUtil::UpdateInstallerStage(system_level, state_key_path,
installer::BUILDING);
std::wstring value;
EXPECT_EQ(ERROR_SUCCESS,
RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE)
.ReadValue(google_update::kRegApField, &value));
EXPECT_EQ(L"-stage:building", value);
}
TempRegKeyOverride::DeleteAllTempKeys();
// Update the stage when there is an "ap" value.
{
TempRegKeyOverride override(root, L"root_inst_res");
RegKey(root, state_key_path.c_str(), KEY_SET_VALUE)
.WriteValue(google_update::kRegApField, L"2.0-dev");
InstallUtil::UpdateInstallerStage(system_level, state_key_path,
installer::BUILDING);
std::wstring value;
EXPECT_EQ(ERROR_SUCCESS,
RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE)
.ReadValue(google_update::kRegApField, &value));
EXPECT_EQ(L"2.0-dev-stage:building", value);
}
TempRegKeyOverride::DeleteAllTempKeys();
// Clear the stage.
{
TempRegKeyOverride override(root, L"root_inst_res");
RegKey(root, state_key_path.c_str(), KEY_SET_VALUE)
.WriteValue(google_update::kRegApField, L"2.0-dev-stage:building");
InstallUtil::UpdateInstallerStage(system_level, state_key_path,
installer::NO_STAGE);
std::wstring value;
EXPECT_EQ(ERROR_SUCCESS,
RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE)
.ReadValue(google_update::kRegApField, &value));
EXPECT_EQ(L"2.0-dev", value);
}
TempRegKeyOverride::DeleteAllTempKeys();
}
TEST_F(InstallUtilTest, DeleteRegistryKeyIf) {
const HKEY root = HKEY_CURRENT_USER;
std::wstring parent_key_path(L"SomeKey\\ToDelete");
std::wstring child_key_path(parent_key_path);
child_key_path.append(L"\\ChildKey\\WithAValue");
const wchar_t value_name[] = L"some_value_name";
const wchar_t value[] = L"hi mom";
{
TempRegKeyOverride override(root, L"root_key");
// Nothing to delete if the keys aren't even there.
{
MockRegistryValuePredicate pred;
EXPECT_CALL(pred, Evaluate(_)).Times(0);
ASSERT_FALSE(RegKey(root, parent_key_path.c_str(),
KEY_QUERY_VALUE).Valid());
EXPECT_TRUE(InstallUtil::DeleteRegistryKeyIf(
root, parent_key_path, child_key_path, value_name, pred));
EXPECT_FALSE(RegKey(root, parent_key_path.c_str(),
KEY_QUERY_VALUE).Valid());
}
// Parent exists, but not child: no delete.
{
MockRegistryValuePredicate pred;
EXPECT_CALL(pred, Evaluate(_)).Times(0);
ASSERT_TRUE(RegKey(root, parent_key_path.c_str(), KEY_SET_VALUE).Valid());
EXPECT_TRUE(InstallUtil::DeleteRegistryKeyIf(
root, parent_key_path, child_key_path, value_name, pred));
EXPECT_TRUE(RegKey(root, parent_key_path.c_str(),
KEY_QUERY_VALUE).Valid());
}
// Child exists, but no value: no delete.
{
MockRegistryValuePredicate pred;
EXPECT_CALL(pred, Evaluate(_)).Times(0);
ASSERT_TRUE(RegKey(root, child_key_path.c_str(), KEY_SET_VALUE).Valid());
EXPECT_TRUE(InstallUtil::DeleteRegistryKeyIf(
root, parent_key_path, child_key_path, value_name, pred));
EXPECT_TRUE(RegKey(root, parent_key_path.c_str(),
KEY_QUERY_VALUE).Valid());
}
// Value exists, but doesn't match: no delete.
{
MockRegistryValuePredicate pred;
EXPECT_CALL(pred, Evaluate(StrEq(L"foosball!"))).WillOnce(Return(false));
ASSERT_EQ(ERROR_SUCCESS,
RegKey(root, child_key_path.c_str(),
KEY_SET_VALUE).WriteValue(value_name, L"foosball!"));
EXPECT_TRUE(InstallUtil::DeleteRegistryKeyIf(
root, parent_key_path, child_key_path, value_name, pred));
EXPECT_TRUE(RegKey(root, parent_key_path.c_str(),
KEY_QUERY_VALUE).Valid());
}
// Value exists, and matches: delete.
{
MockRegistryValuePredicate pred;
EXPECT_CALL(pred, Evaluate(StrEq(value))).WillOnce(Return(true));
ASSERT_EQ(ERROR_SUCCESS,
RegKey(root, child_key_path.c_str(),
KEY_SET_VALUE).WriteValue(value_name, value));
EXPECT_TRUE(InstallUtil::DeleteRegistryKeyIf(
root, parent_key_path, child_key_path, value_name, pred));
EXPECT_FALSE(RegKey(root, parent_key_path.c_str(),
KEY_QUERY_VALUE).Valid());
}
}
TempRegKeyOverride::DeleteAllTempKeys();
}
TEST_F(InstallUtilTest, DeleteRegistryValueIf) {
const HKEY root = HKEY_CURRENT_USER;
std::wstring key_path(L"SomeKey\\ToDelete");
const wchar_t value_name[] = L"some_value_name";
const wchar_t value[] = L"hi mom";
{
TempRegKeyOverride override(root, L"root_key");
// Nothing to delete if the key isn't even there.
{
MockRegistryValuePredicate pred;
EXPECT_CALL(pred, Evaluate(_)).Times(0);
ASSERT_FALSE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid());
EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf(
root, key_path.c_str(), value_name, pred));
EXPECT_FALSE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid());
}
// Key exists, but no value: no delete.
{
MockRegistryValuePredicate pred;
EXPECT_CALL(pred, Evaluate(_)).Times(0);
ASSERT_TRUE(RegKey(root, key_path.c_str(), KEY_SET_VALUE).Valid());
EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf(
root, key_path.c_str(), value_name, pred));
EXPECT_TRUE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid());
}
// Value exists, but doesn't match: no delete.
{
MockRegistryValuePredicate pred;
EXPECT_CALL(pred, Evaluate(StrEq(L"foosball!"))).WillOnce(Return(false));
ASSERT_EQ(ERROR_SUCCESS,
RegKey(root, key_path.c_str(),
KEY_SET_VALUE).WriteValue(value_name, L"foosball!"));
EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf(
root, key_path.c_str(), value_name, pred));
EXPECT_TRUE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid());
EXPECT_TRUE(RegKey(root, key_path.c_str(),
KEY_QUERY_VALUE).ValueExists(value_name));
}
// Value exists, and matches: delete.
{
MockRegistryValuePredicate pred;
EXPECT_CALL(pred, Evaluate(StrEq(value))).WillOnce(Return(true));
ASSERT_EQ(ERROR_SUCCESS,
RegKey(root, key_path.c_str(),
KEY_SET_VALUE).WriteValue(value_name, value));
EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf(
root, key_path.c_str(), value_name, pred));
EXPECT_TRUE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid());
EXPECT_FALSE(RegKey(root, key_path.c_str(),
KEY_QUERY_VALUE).ValueExists(value_name));
}
}
TempRegKeyOverride::DeleteAllTempKeys();
{
TempRegKeyOverride override(root, L"root_key");
// Default value matches: delete.
{
MockRegistryValuePredicate pred;
EXPECT_CALL(pred, Evaluate(StrEq(value))).WillOnce(Return(true));
ASSERT_EQ(ERROR_SUCCESS,
RegKey(root, key_path.c_str(),
KEY_SET_VALUE).WriteValue(L"", value));
EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf(
root, key_path.c_str(), L"", pred));
EXPECT_TRUE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid());
EXPECT_FALSE(RegKey(root, key_path.c_str(),
KEY_QUERY_VALUE).ValueExists(L""));
}
}
TempRegKeyOverride::DeleteAllTempKeys();
}
TEST_F(InstallUtilTest, ValueEquals) {
InstallUtil::ValueEquals pred(L"howdy");
EXPECT_FALSE(pred.Evaluate(L""));
EXPECT_FALSE(pred.Evaluate(L"Howdy"));
EXPECT_FALSE(pred.Evaluate(L"howdy!"));
EXPECT_FALSE(pred.Evaluate(L"!howdy"));
EXPECT_TRUE(pred.Evaluate(L"howdy"));
}
|