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.
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/test/test_reg_util_win.h"
9 #include "base/version.h"
10 #include "base/win/registry.h"
11 #include "chrome/installer/util/browser_distribution.h"
12 #include "chrome/installer/util/google_update_constants.h"
13 #include "chrome/installer/util/installation_state.h"
14 #include "chrome/installer/util/util_constants.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using base::win::RegKey
;
18 using installer::ProductState
;
19 using registry_util::RegistryOverrideManager
;
21 class ProductStateTest
: public testing::Test
{
23 static void SetUpTestCase();
24 static void TearDownTestCase();
26 void SetUp() override
;
27 void TearDown() override
;
29 void ApplyUninstallCommand(const wchar_t* exe_path
, const wchar_t* args
);
30 void MinimallyInstallProduct(const wchar_t* version
);
32 static BrowserDistribution
* dist_
;
35 registry_util::RegistryOverrideManager registry_override_manager_
;
40 BrowserDistribution
* ProductStateTest::dist_
;
43 void ProductStateTest::SetUpTestCase() {
44 testing::Test::SetUpTestCase();
46 // We'll use Chrome as our test subject.
47 dist_
= BrowserDistribution::GetSpecificDistribution(
48 BrowserDistribution::CHROME_BROWSER
);
52 void ProductStateTest::TearDownTestCase() {
55 testing::Test::TearDownTestCase();
58 void ProductStateTest::SetUp() {
59 testing::Test::SetUp();
61 // Create/open the keys for the product we'll test.
62 system_install_
= true;
63 overridden_
= (system_install_
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
);
65 // Override for test purposes. We don't use ScopedRegistryKeyOverride
66 // directly because it doesn't suit itself to our use here.
69 registry_override_manager_
.OverrideRegistry(overridden_
);
71 EXPECT_EQ(ERROR_SUCCESS
,
72 clients_
.Create(overridden_
, dist_
->GetVersionKey().c_str(),
74 EXPECT_EQ(ERROR_SUCCESS
,
75 client_state_
.Create(overridden_
, dist_
->GetStateKey().c_str(),
79 void ProductStateTest::TearDown() {
80 // Done with the keys.
81 client_state_
.Close();
84 system_install_
= false;
86 testing::Test::TearDown();
89 void ProductStateTest::MinimallyInstallProduct(const wchar_t* version
) {
90 EXPECT_EQ(ERROR_SUCCESS
,
91 clients_
.WriteValue(google_update::kRegVersionField
, version
));
94 void ProductStateTest::ApplyUninstallCommand(const wchar_t* exe_path
,
95 const wchar_t* args
) {
96 if (exe_path
== NULL
) {
97 LONG result
= client_state_
.DeleteValue(installer::kUninstallStringField
);
98 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
100 EXPECT_EQ(ERROR_SUCCESS
,
101 client_state_
.WriteValue(installer::kUninstallStringField
,
107 client_state_
.DeleteValue(installer::kUninstallArgumentsField
);
108 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
110 EXPECT_EQ(ERROR_SUCCESS
,
111 client_state_
.WriteValue(installer::kUninstallArgumentsField
,
116 TEST_F(ProductStateTest
, InitializeInstalled
) {
120 LONG result
= clients_
.DeleteValue(google_update::kRegVersionField
);
121 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
122 EXPECT_FALSE(state
.Initialize(system_install_
, dist_
));
128 LONG result
= clients_
.WriteValue(google_update::kRegVersionField
, L
"");
129 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
130 EXPECT_FALSE(state
.Initialize(system_install_
, dist_
));
136 LONG result
= clients_
.WriteValue(google_update::kRegVersionField
,
138 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
139 EXPECT_FALSE(state
.Initialize(system_install_
, dist_
));
145 LONG result
= clients_
.WriteValue(google_update::kRegVersionField
,
147 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
148 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
149 EXPECT_EQ("10.0.47.0", state
.version().GetString());
153 // Test extraction of the "opv" value from the Clients key.
154 TEST_F(ProductStateTest
, InitializeOldVersion
) {
155 MinimallyInstallProduct(L
"10.0.1.1");
160 LONG result
= clients_
.DeleteValue(google_update::kRegOldVersionField
);
161 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
162 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
163 EXPECT_TRUE(state
.old_version() == NULL
);
166 // Empty "opv" value.
169 LONG result
= clients_
.WriteValue(google_update::kRegOldVersionField
, L
"");
170 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
171 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
172 EXPECT_TRUE(state
.old_version() == NULL
);
175 // Bogus "opv" value.
178 LONG result
= clients_
.WriteValue(google_update::kRegOldVersionField
,
180 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
181 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
182 EXPECT_TRUE(state
.old_version() == NULL
);
185 // Valid "opv" value.
188 LONG result
= clients_
.WriteValue(google_update::kRegOldVersionField
,
190 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
191 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
192 EXPECT_TRUE(state
.old_version() != NULL
);
193 EXPECT_EQ("10.0.47.0", state
.old_version()->GetString());
197 // Test extraction of the "cmd" value from the Clients key.
198 TEST_F(ProductStateTest
, InitializeRenameCmd
) {
199 MinimallyInstallProduct(L
"10.0.1.1");
204 LONG result
= clients_
.DeleteValue(google_update::kRegRenameCmdField
);
205 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
206 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
207 EXPECT_TRUE(state
.rename_cmd().empty());
210 // Empty "cmd" value.
213 LONG result
= clients_
.WriteValue(google_update::kRegRenameCmdField
, L
"");
214 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
215 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
216 EXPECT_TRUE(state
.rename_cmd().empty());
219 // Valid "cmd" value.
222 LONG result
= clients_
.WriteValue(google_update::kRegRenameCmdField
,
223 L
"spam.exe --spamalot");
224 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
225 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
226 EXPECT_EQ(L
"spam.exe --spamalot", state
.rename_cmd());
230 // Test extraction of the "ap" value from the ClientState key.
231 TEST_F(ProductStateTest
, InitializeChannelInfo
) {
232 MinimallyInstallProduct(L
"10.0.1.1");
237 LONG result
= client_state_
.DeleteValue(google_update::kRegApField
);
238 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
239 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
240 EXPECT_TRUE(state
.channel().value().empty());
246 LONG result
= client_state_
.WriteValue(google_update::kRegApField
, L
"");
247 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
248 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
249 EXPECT_TRUE(state
.channel().value().empty());
255 LONG result
= client_state_
.WriteValue(google_update::kRegApField
, L
"spam");
256 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
257 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
258 EXPECT_EQ(L
"spam", state
.channel().value());
262 // Test extraction of the uninstall command and arguments from the ClientState
264 TEST_F(ProductStateTest
, InitializeUninstallCommand
) {
265 MinimallyInstallProduct(L
"10.0.1.1");
267 // No uninstall command.
270 ApplyUninstallCommand(NULL
, NULL
);
271 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
272 EXPECT_TRUE(state
.GetSetupPath().empty());
273 EXPECT_TRUE(state
.uninstall_command().GetCommandLineString().empty());
274 EXPECT_TRUE(state
.uninstall_command().GetSwitches().empty());
280 ApplyUninstallCommand(L
"", L
"");
281 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
282 EXPECT_TRUE(state
.GetSetupPath().empty());
283 EXPECT_TRUE(state
.uninstall_command().GetCommandLineString().empty());
284 EXPECT_TRUE(state
.uninstall_command().GetSwitches().empty());
287 // Uninstall command without exe.
290 ApplyUninstallCommand(NULL
, L
"--uninstall");
291 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
292 EXPECT_TRUE(state
.GetSetupPath().empty());
293 EXPECT_EQ(L
" --uninstall",
294 state
.uninstall_command().GetCommandLineString());
295 EXPECT_EQ(1U, state
.uninstall_command().GetSwitches().size());
298 // Uninstall command without args.
301 ApplyUninstallCommand(L
"setup.exe", NULL
);
302 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
303 EXPECT_EQ(L
"setup.exe", state
.GetSetupPath().value());
304 EXPECT_EQ(L
"setup.exe", state
.uninstall_command().GetCommandLineString());
305 EXPECT_TRUE(state
.uninstall_command().GetSwitches().empty());
308 // Uninstall command with exe that requires quoting.
311 ApplyUninstallCommand(L
"set up.exe", NULL
);
312 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
313 EXPECT_EQ(L
"set up.exe", state
.GetSetupPath().value());
314 EXPECT_EQ(L
"\"set up.exe\"",
315 state
.uninstall_command().GetCommandLineString());
316 EXPECT_TRUE(state
.uninstall_command().GetSwitches().empty());
319 // Uninstall command with both exe and args.
322 ApplyUninstallCommand(L
"setup.exe", L
"--uninstall");
323 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
324 EXPECT_EQ(L
"setup.exe", state
.GetSetupPath().value());
325 EXPECT_EQ(L
"setup.exe --uninstall",
326 state
.uninstall_command().GetCommandLineString());
327 EXPECT_EQ(1U, state
.uninstall_command().GetSwitches().size());
331 // Test extraction of the msi marker from the ClientState key.
332 TEST_F(ProductStateTest
, InitializeMsi
) {
333 MinimallyInstallProduct(L
"10.0.1.1");
338 LONG result
= client_state_
.DeleteValue(google_update::kRegMSIField
);
339 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
340 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
341 EXPECT_FALSE(state
.is_msi());
344 // Msi marker set to zero.
347 EXPECT_EQ(ERROR_SUCCESS
,
348 client_state_
.WriteValue(google_update::kRegMSIField
,
349 static_cast<DWORD
>(0)));
350 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
351 EXPECT_FALSE(state
.is_msi());
354 // Msi marker set to one.
357 EXPECT_EQ(ERROR_SUCCESS
,
358 client_state_
.WriteValue(google_update::kRegMSIField
,
359 static_cast<DWORD
>(1)));
360 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
361 EXPECT_TRUE(state
.is_msi());
364 // Msi marker set to a bogus DWORD.
367 EXPECT_EQ(ERROR_SUCCESS
,
368 client_state_
.WriteValue(google_update::kRegMSIField
,
369 static_cast<DWORD
>(47)));
370 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
371 EXPECT_TRUE(state
.is_msi());
374 // Msi marker set to a bogus string.
377 EXPECT_EQ(ERROR_SUCCESS
,
378 client_state_
.WriteValue(google_update::kRegMSIField
,
380 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
381 EXPECT_FALSE(state
.is_msi());
385 // Test detection of multi-install.
386 TEST_F(ProductStateTest
, InitializeMultiInstall
) {
387 MinimallyInstallProduct(L
"10.0.1.1");
389 // No uninstall command means single install.
392 ApplyUninstallCommand(NULL
, NULL
);
393 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
394 EXPECT_FALSE(state
.is_multi_install());
397 // Uninstall command without --multi-install is single install.
400 ApplyUninstallCommand(L
"setup.exe", L
"--uninstall");
401 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
402 EXPECT_FALSE(state
.is_multi_install());
405 // Uninstall command with --multi-install is multi install.
408 ApplyUninstallCommand(L
"setup.exe",
409 L
"--uninstall --chrome --multi-install");
410 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
411 EXPECT_TRUE(state
.is_multi_install());