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/product_unittest.h"
15 #include "chrome/installer/util/util_constants.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using base::win::RegKey
;
19 using installer::ProductState
;
20 using registry_util::RegistryOverrideManager
;
22 class ProductStateTest
: public testing::Test
{
24 static void SetUpTestCase();
25 static void TearDownTestCase();
28 virtual void TearDown();
30 void ApplyUninstallCommand(const wchar_t* exe_path
, const wchar_t* args
);
31 void MinimallyInstallProduct(const wchar_t* version
);
33 static BrowserDistribution
* dist_
;
36 registry_util::RegistryOverrideManager registry_override_manager_
;
41 BrowserDistribution
* ProductStateTest::dist_
;
44 void ProductStateTest::SetUpTestCase() {
45 testing::Test::SetUpTestCase();
47 // We'll use Chrome as our test subject.
48 dist_
= BrowserDistribution::GetSpecificDistribution(
49 BrowserDistribution::CHROME_BROWSER
);
53 void ProductStateTest::TearDownTestCase() {
56 testing::Test::TearDownTestCase();
59 void ProductStateTest::SetUp() {
60 testing::Test::SetUp();
62 // Create/open the keys for the product we'll test.
63 system_install_
= true;
64 overridden_
= (system_install_
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
);
66 // Override for test purposes. We don't use ScopedRegistryKeyOverride
67 // directly because it doesn't suit itself to our use here.
70 registry_override_manager_
.OverrideRegistry(overridden_
);
72 EXPECT_EQ(ERROR_SUCCESS
,
73 clients_
.Create(overridden_
, dist_
->GetVersionKey().c_str(),
75 EXPECT_EQ(ERROR_SUCCESS
,
76 client_state_
.Create(overridden_
, dist_
->GetStateKey().c_str(),
80 void ProductStateTest::TearDown() {
81 // Done with the keys.
82 client_state_
.Close();
85 system_install_
= false;
87 testing::Test::TearDown();
90 void ProductStateTest::MinimallyInstallProduct(const wchar_t* version
) {
91 EXPECT_EQ(ERROR_SUCCESS
,
92 clients_
.WriteValue(google_update::kRegVersionField
, version
));
95 void ProductStateTest::ApplyUninstallCommand(const wchar_t* exe_path
,
96 const wchar_t* args
) {
97 if (exe_path
== NULL
) {
98 LONG result
= client_state_
.DeleteValue(installer::kUninstallStringField
);
99 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
101 EXPECT_EQ(ERROR_SUCCESS
,
102 client_state_
.WriteValue(installer::kUninstallStringField
,
108 client_state_
.DeleteValue(installer::kUninstallArgumentsField
);
109 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
111 EXPECT_EQ(ERROR_SUCCESS
,
112 client_state_
.WriteValue(installer::kUninstallArgumentsField
,
117 TEST_F(ProductStateTest
, InitializeInstalled
) {
121 LONG result
= clients_
.DeleteValue(google_update::kRegVersionField
);
122 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
123 EXPECT_FALSE(state
.Initialize(system_install_
, dist_
));
129 LONG result
= clients_
.WriteValue(google_update::kRegVersionField
, L
"");
130 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
131 EXPECT_FALSE(state
.Initialize(system_install_
, dist_
));
137 LONG result
= clients_
.WriteValue(google_update::kRegVersionField
,
139 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
140 EXPECT_FALSE(state
.Initialize(system_install_
, dist_
));
146 LONG result
= clients_
.WriteValue(google_update::kRegVersionField
,
148 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
149 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
150 EXPECT_EQ("10.0.47.0", state
.version().GetString());
154 // Test extraction of the "opv" value from the Clients key.
155 TEST_F(ProductStateTest
, InitializeOldVersion
) {
156 MinimallyInstallProduct(L
"10.0.1.1");
161 LONG result
= clients_
.DeleteValue(google_update::kRegOldVersionField
);
162 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
163 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
164 EXPECT_TRUE(state
.old_version() == NULL
);
167 // Empty "opv" value.
170 LONG result
= clients_
.WriteValue(google_update::kRegOldVersionField
, L
"");
171 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
172 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
173 EXPECT_TRUE(state
.old_version() == NULL
);
176 // Bogus "opv" value.
179 LONG result
= clients_
.WriteValue(google_update::kRegOldVersionField
,
181 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
182 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
183 EXPECT_TRUE(state
.old_version() == NULL
);
186 // Valid "opv" value.
189 LONG result
= clients_
.WriteValue(google_update::kRegOldVersionField
,
191 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
192 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
193 EXPECT_TRUE(state
.old_version() != NULL
);
194 EXPECT_EQ("10.0.47.0", state
.old_version()->GetString());
198 // Test extraction of the "cmd" value from the Clients key.
199 TEST_F(ProductStateTest
, InitializeRenameCmd
) {
200 MinimallyInstallProduct(L
"10.0.1.1");
205 LONG result
= clients_
.DeleteValue(google_update::kRegRenameCmdField
);
206 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
207 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
208 EXPECT_TRUE(state
.rename_cmd().empty());
211 // Empty "cmd" value.
214 LONG result
= clients_
.WriteValue(google_update::kRegRenameCmdField
, L
"");
215 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
216 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
217 EXPECT_TRUE(state
.rename_cmd().empty());
220 // Valid "cmd" value.
223 LONG result
= clients_
.WriteValue(google_update::kRegRenameCmdField
,
224 L
"spam.exe --spamalot");
225 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
226 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
227 EXPECT_EQ(L
"spam.exe --spamalot", state
.rename_cmd());
231 // Test extraction of the "ap" value from the ClientState key.
232 TEST_F(ProductStateTest
, InitializeChannelInfo
) {
233 MinimallyInstallProduct(L
"10.0.1.1");
238 LONG result
= client_state_
.DeleteValue(google_update::kRegApField
);
239 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
240 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
241 EXPECT_TRUE(state
.channel().value().empty());
247 LONG result
= client_state_
.WriteValue(google_update::kRegApField
, L
"");
248 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
249 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
250 EXPECT_TRUE(state
.channel().value().empty());
256 LONG result
= client_state_
.WriteValue(google_update::kRegApField
, L
"spam");
257 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
258 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
259 EXPECT_EQ(L
"spam", state
.channel().value());
263 // Test extraction of the uninstall command and arguments from the ClientState
265 TEST_F(ProductStateTest
, InitializeUninstallCommand
) {
266 MinimallyInstallProduct(L
"10.0.1.1");
268 // No uninstall command.
271 ApplyUninstallCommand(NULL
, NULL
);
272 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
273 EXPECT_TRUE(state
.GetSetupPath().empty());
274 EXPECT_TRUE(state
.uninstall_command().GetCommandLineString().empty());
275 EXPECT_TRUE(state
.uninstall_command().GetSwitches().empty());
281 ApplyUninstallCommand(L
"", L
"");
282 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
283 EXPECT_TRUE(state
.GetSetupPath().empty());
284 EXPECT_TRUE(state
.uninstall_command().GetCommandLineString().empty());
285 EXPECT_TRUE(state
.uninstall_command().GetSwitches().empty());
288 // Uninstall command without exe.
291 ApplyUninstallCommand(NULL
, L
"--uninstall");
292 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
293 EXPECT_TRUE(state
.GetSetupPath().empty());
294 EXPECT_EQ(L
" --uninstall",
295 state
.uninstall_command().GetCommandLineString());
296 EXPECT_EQ(1U, state
.uninstall_command().GetSwitches().size());
299 // Uninstall command without args.
302 ApplyUninstallCommand(L
"setup.exe", NULL
);
303 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
304 EXPECT_EQ(L
"setup.exe", state
.GetSetupPath().value());
305 EXPECT_EQ(L
"setup.exe", state
.uninstall_command().GetCommandLineString());
306 EXPECT_TRUE(state
.uninstall_command().GetSwitches().empty());
309 // Uninstall command with exe that requires quoting.
312 ApplyUninstallCommand(L
"set up.exe", NULL
);
313 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
314 EXPECT_EQ(L
"set up.exe", state
.GetSetupPath().value());
315 EXPECT_EQ(L
"\"set up.exe\"",
316 state
.uninstall_command().GetCommandLineString());
317 EXPECT_TRUE(state
.uninstall_command().GetSwitches().empty());
320 // Uninstall command with both exe and args.
323 ApplyUninstallCommand(L
"setup.exe", L
"--uninstall");
324 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
325 EXPECT_EQ(L
"setup.exe", state
.GetSetupPath().value());
326 EXPECT_EQ(L
"setup.exe --uninstall",
327 state
.uninstall_command().GetCommandLineString());
328 EXPECT_EQ(1U, state
.uninstall_command().GetSwitches().size());
332 // Test extraction of the msi marker from the ClientState key.
333 TEST_F(ProductStateTest
, InitializeMsi
) {
334 MinimallyInstallProduct(L
"10.0.1.1");
339 LONG result
= client_state_
.DeleteValue(google_update::kRegMSIField
);
340 EXPECT_TRUE(result
== ERROR_SUCCESS
|| result
== ERROR_FILE_NOT_FOUND
);
341 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
342 EXPECT_FALSE(state
.is_msi());
345 // Msi marker set to zero.
348 EXPECT_EQ(ERROR_SUCCESS
,
349 client_state_
.WriteValue(google_update::kRegMSIField
,
350 static_cast<DWORD
>(0)));
351 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
352 EXPECT_FALSE(state
.is_msi());
355 // Msi marker set to one.
358 EXPECT_EQ(ERROR_SUCCESS
,
359 client_state_
.WriteValue(google_update::kRegMSIField
,
360 static_cast<DWORD
>(1)));
361 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
362 EXPECT_TRUE(state
.is_msi());
365 // Msi marker set to a bogus DWORD.
368 EXPECT_EQ(ERROR_SUCCESS
,
369 client_state_
.WriteValue(google_update::kRegMSIField
,
370 static_cast<DWORD
>(47)));
371 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
372 EXPECT_TRUE(state
.is_msi());
375 // Msi marker set to a bogus string.
378 EXPECT_EQ(ERROR_SUCCESS
,
379 client_state_
.WriteValue(google_update::kRegMSIField
,
381 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
382 EXPECT_FALSE(state
.is_msi());
386 // Test detection of multi-install.
387 TEST_F(ProductStateTest
, InitializeMultiInstall
) {
388 MinimallyInstallProduct(L
"10.0.1.1");
390 // No uninstall command means single install.
393 ApplyUninstallCommand(NULL
, NULL
);
394 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
395 EXPECT_FALSE(state
.is_multi_install());
398 // Uninstall command without --multi-install is single install.
401 ApplyUninstallCommand(L
"setup.exe", L
"--uninstall");
402 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
403 EXPECT_FALSE(state
.is_multi_install());
406 // Uninstall command with --multi-install is multi install.
409 ApplyUninstallCommand(L
"setup.exe",
410 L
"--uninstall --chrome --multi-install");
411 EXPECT_TRUE(state
.Initialize(system_install_
, dist_
));
412 EXPECT_TRUE(state
.is_multi_install());