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.
5 #include "chrome/installer/setup/setup_util_unittest.h"
11 #include "base/command_line.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/process/kill.h"
17 #include "base/process/launch.h"
18 #include "base/process/process_handle.h"
19 #include "base/strings/string_util.h"
20 #include "base/test/test_reg_util_win.h"
21 #include "base/test/test_timeouts.h"
22 #include "base/threading/platform_thread.h"
23 #include "base/version.h"
24 #include "base/win/registry.h"
25 #include "base/win/scoped_handle.h"
26 #include "base/win/windows_version.h"
27 #include "chrome/installer/setup/setup_constants.h"
28 #include "chrome/installer/setup/setup_util.h"
29 #include "chrome/installer/util/browser_distribution.h"
30 #include "chrome/installer/util/google_update_constants.h"
31 #include "chrome/installer/util/installation_state.h"
32 #include "chrome/installer/util/installer_state.h"
33 #include "chrome/installer/util/updating_app_registration_data.h"
34 #include "chrome/installer/util/util_constants.h"
35 #include "testing/gtest/include/gtest/gtest.h"
39 // The privilege tested in ScopeTokenPrivilege tests below.
40 // Use SE_RESTORE_NAME as it is one of the many privileges that is available,
41 // but not enabled by default on processes running at high integrity.
42 static const wchar_t kTestedPrivilege
[] = SE_RESTORE_NAME
;
44 // Returns true if the current process' token has privilege |privilege_name|
46 bool CurrentProcessHasPrivilege(const wchar_t* privilege_name
) {
48 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY
,
54 base::win::ScopedHandle
token(temp_handle
);
56 // First get the size of the buffer needed for |privileges| below.
58 EXPECT_FALSE(::GetTokenInformation(token
.Get(), TokenPrivileges
, NULL
, 0,
61 scoped_ptr
<BYTE
[]> privileges_bytes(new BYTE
[size
]);
62 TOKEN_PRIVILEGES
* privileges
=
63 reinterpret_cast<TOKEN_PRIVILEGES
*>(privileges_bytes
.get());
65 if (!::GetTokenInformation(token
.Get(), TokenPrivileges
, privileges
, size
,
71 // There is no point getting a buffer to store more than |privilege_name|\0 as
72 // anything longer will obviously not be equal to |privilege_name|.
73 const DWORD desired_size
= static_cast<DWORD
>(wcslen(privilege_name
));
74 const DWORD buffer_size
= desired_size
+ 1;
75 scoped_ptr
<wchar_t[]> name_buffer(new wchar_t[buffer_size
]);
76 for (int i
= privileges
->PrivilegeCount
- 1; i
>= 0 ; --i
) {
77 LUID_AND_ATTRIBUTES
& luid_and_att
= privileges
->Privileges
[i
];
78 DWORD size
= buffer_size
;
79 ::LookupPrivilegeName(NULL
, &luid_and_att
.Luid
, name_buffer
.get(), &size
);
80 if (size
== desired_size
&&
81 wcscmp(name_buffer
.get(), privilege_name
) == 0) {
82 return luid_and_att
.Attributes
== SE_PRIVILEGE_ENABLED
;
90 // Test that we are parsing Chrome version correctly.
91 TEST(SetupUtilTest
, GetMaxVersionFromArchiveDirTest
) {
92 // Create a version dir
93 base::ScopedTempDir test_dir
;
94 ASSERT_TRUE(test_dir
.CreateUniqueTempDir());
95 base::FilePath chrome_dir
= test_dir
.path().AppendASCII("1.0.0.0");
96 base::CreateDirectory(chrome_dir
);
97 ASSERT_TRUE(base::PathExists(chrome_dir
));
98 scoped_ptr
<Version
> version(
99 installer::GetMaxVersionFromArchiveDir(test_dir
.path()));
100 ASSERT_EQ(version
->GetString(), "1.0.0.0");
102 base::DeleteFile(chrome_dir
, true);
103 ASSERT_FALSE(base::PathExists(chrome_dir
)) << chrome_dir
.value();
104 ASSERT_TRUE(installer::GetMaxVersionFromArchiveDir(test_dir
.path()) == NULL
);
106 chrome_dir
= test_dir
.path().AppendASCII("ABC");
107 base::CreateDirectory(chrome_dir
);
108 ASSERT_TRUE(base::PathExists(chrome_dir
));
109 ASSERT_TRUE(installer::GetMaxVersionFromArchiveDir(test_dir
.path()) == NULL
);
111 chrome_dir
= test_dir
.path().AppendASCII("2.3.4.5");
112 base::CreateDirectory(chrome_dir
);
113 ASSERT_TRUE(base::PathExists(chrome_dir
));
114 version
.reset(installer::GetMaxVersionFromArchiveDir(test_dir
.path()));
115 ASSERT_EQ(version
->GetString(), "2.3.4.5");
117 // Create multiple version dirs, ensure that we select the greatest.
118 chrome_dir
= test_dir
.path().AppendASCII("9.9.9.9");
119 base::CreateDirectory(chrome_dir
);
120 ASSERT_TRUE(base::PathExists(chrome_dir
));
121 chrome_dir
= test_dir
.path().AppendASCII("1.1.1.1");
122 base::CreateDirectory(chrome_dir
);
123 ASSERT_TRUE(base::PathExists(chrome_dir
));
125 version
.reset(installer::GetMaxVersionFromArchiveDir(test_dir
.path()));
126 ASSERT_EQ(version
->GetString(), "9.9.9.9");
129 TEST(SetupUtilTest
, DeleteFileFromTempProcess
) {
130 base::ScopedTempDir test_dir
;
131 ASSERT_TRUE(test_dir
.CreateUniqueTempDir());
132 base::FilePath test_file
;
133 base::CreateTemporaryFileInDir(test_dir
.path(), &test_file
);
134 ASSERT_TRUE(base::PathExists(test_file
));
135 base::WriteFile(test_file
, "foo", 3);
136 EXPECT_TRUE(installer::DeleteFileFromTempProcess(test_file
, 0));
137 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 3);
138 EXPECT_FALSE(base::PathExists(test_file
)) << test_file
.value();
141 // Note: This test is only valid when run at high integrity (i.e. it will fail
142 // at medium integrity).
143 TEST(SetupUtilTest
, ScopedTokenPrivilegeBasic
) {
144 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege
));
147 installer::ScopedTokenPrivilege
test_scoped_privilege(kTestedPrivilege
);
148 ASSERT_TRUE(test_scoped_privilege
.is_enabled());
149 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege
));
152 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege
));
155 // Note: This test is only valid when run at high integrity (i.e. it will fail
156 // at medium integrity).
157 TEST(SetupUtilTest
, ScopedTokenPrivilegeAlreadyEnabled
) {
158 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege
));
161 installer::ScopedTokenPrivilege
test_scoped_privilege(kTestedPrivilege
);
162 ASSERT_TRUE(test_scoped_privilege
.is_enabled());
163 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege
));
165 installer::ScopedTokenPrivilege
dup_scoped_privilege(kTestedPrivilege
);
166 ASSERT_TRUE(dup_scoped_privilege
.is_enabled());
167 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege
));
169 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege
));
172 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege
));
175 const char kAdjustProcessPriority
[] = "adjust-process-priority";
177 PriorityClassChangeResult
DoProcessPriorityAdjustment() {
178 return installer::AdjustProcessPriority() ? PCCR_CHANGED
: PCCR_UNCHANGED
;
183 // A scoper that sets/resets the current process's priority class.
184 class ScopedPriorityClass
{
186 // Applies |priority_class|, returning an instance if a change was made.
187 // Otherwise, returns an empty scoped_ptr.
188 static scoped_ptr
<ScopedPriorityClass
> Create(DWORD priority_class
);
189 ~ScopedPriorityClass();
192 explicit ScopedPriorityClass(DWORD original_priority_class
);
193 DWORD original_priority_class_
;
194 DISALLOW_COPY_AND_ASSIGN(ScopedPriorityClass
);
197 scoped_ptr
<ScopedPriorityClass
> ScopedPriorityClass::Create(
198 DWORD priority_class
) {
199 HANDLE this_process
= ::GetCurrentProcess();
200 DWORD original_priority_class
= ::GetPriorityClass(this_process
);
201 EXPECT_NE(0U, original_priority_class
);
202 if (original_priority_class
&& original_priority_class
!= priority_class
) {
203 BOOL result
= ::SetPriorityClass(this_process
, priority_class
);
204 EXPECT_NE(FALSE
, result
);
206 return scoped_ptr
<ScopedPriorityClass
>(
207 new ScopedPriorityClass(original_priority_class
));
210 return scoped_ptr
<ScopedPriorityClass
>();
213 ScopedPriorityClass::ScopedPriorityClass(DWORD original_priority_class
)
214 : original_priority_class_(original_priority_class
) {}
216 ScopedPriorityClass::~ScopedPriorityClass() {
217 BOOL result
= ::SetPriorityClass(::GetCurrentProcess(),
218 original_priority_class_
);
219 EXPECT_NE(FALSE
, result
);
222 PriorityClassChangeResult
RelaunchAndDoProcessPriorityAdjustment() {
223 base::CommandLine
cmd_line(*base::CommandLine::ForCurrentProcess());
224 cmd_line
.AppendSwitch(kAdjustProcessPriority
);
225 base::Process process
= base::LaunchProcess(cmd_line
, base::LaunchOptions());
227 if (!process
.IsValid()) {
228 ADD_FAILURE() << " to launch subprocess.";
229 } else if (!process
.WaitForExit(&exit_code
)) {
230 ADD_FAILURE() << " to wait for subprocess to exit.";
232 return static_cast<PriorityClassChangeResult
>(exit_code
);
239 // Launching a subprocess at normal priority class is a noop.
240 TEST(SetupUtilTest
, AdjustFromNormalPriority
) {
241 ASSERT_EQ(NORMAL_PRIORITY_CLASS
, ::GetPriorityClass(::GetCurrentProcess()));
242 EXPECT_EQ(PCCR_UNCHANGED
, RelaunchAndDoProcessPriorityAdjustment());
245 // Launching a subprocess below normal priority class drops it to bg mode for
246 // sufficiently recent operating systems.
247 TEST(SetupUtilTest
, AdjustFromBelowNormalPriority
) {
248 scoped_ptr
<ScopedPriorityClass
> below_normal
=
249 ScopedPriorityClass::Create(BELOW_NORMAL_PRIORITY_CLASS
);
250 ASSERT_TRUE(below_normal
);
251 if (base::win::GetVersion() > base::win::VERSION_SERVER_2003
)
252 EXPECT_EQ(PCCR_CHANGED
, RelaunchAndDoProcessPriorityAdjustment());
254 EXPECT_EQ(PCCR_UNCHANGED
, RelaunchAndDoProcessPriorityAdjustment());
259 // A test fixture that configures an InstallationState and an InstallerState
260 // with a product being updated.
261 class FindArchiveToPatchTest
: public testing::Test
{
263 class FakeInstallationState
: public installer::InstallationState
{
266 class FakeProductState
: public installer::ProductState
{
268 static FakeProductState
* FromProductState(const ProductState
* product
) {
269 return static_cast<FakeProductState
*>(const_cast<ProductState
*>(product
));
272 void set_version(const Version
& version
) {
273 if (version
.IsValid())
274 version_
.reset(new Version(version
));
279 void set_uninstall_command(const base::CommandLine
& uninstall_command
) {
280 uninstall_command_
= uninstall_command
;
284 FindArchiveToPatchTest() {}
286 void SetUp() override
{
287 ASSERT_TRUE(test_dir_
.CreateUniqueTempDir());
288 registry_override_manager_
.OverrideRegistry(HKEY_CURRENT_USER
);
289 registry_override_manager_
.OverrideRegistry(HKEY_LOCAL_MACHINE
);
290 product_version_
= Version("30.0.1559.0");
291 max_version_
= Version("47.0.1559.0");
293 // Install the product according to the version.
294 original_state_
.reset(new FakeInstallationState());
297 // Prepare to update the product in the temp dir.
298 installer_state_
.reset(new installer::InstallerState(
299 kSystemInstall_
? installer::InstallerState::SYSTEM_LEVEL
:
300 installer::InstallerState::USER_LEVEL
));
301 installer_state_
->AddProductFromState(
303 *original_state_
->GetProductState(kSystemInstall_
, kProductType_
));
305 // Create archives in the two version dirs.
307 base::CreateDirectory(GetProductVersionArchivePath().DirName()));
308 ASSERT_EQ(1, base::WriteFile(GetProductVersionArchivePath(), "a", 1));
310 base::CreateDirectory(GetMaxVersionArchivePath().DirName()));
311 ASSERT_EQ(1, base::WriteFile(GetMaxVersionArchivePath(), "b", 1));
314 void TearDown() override
{
315 original_state_
.reset();
318 base::FilePath
GetArchivePath(const Version
& version
) const {
319 return test_dir_
.path()
320 .AppendASCII(version
.GetString())
321 .Append(installer::kInstallerDir
)
322 .Append(installer::kChromeArchive
);
325 base::FilePath
GetMaxVersionArchivePath() const {
326 return GetArchivePath(max_version_
);
329 base::FilePath
GetProductVersionArchivePath() const {
330 return GetArchivePath(product_version_
);
333 void InstallProduct() {
334 FakeProductState
* product
= FakeProductState::FromProductState(
335 original_state_
->GetNonVersionedProductState(kSystemInstall_
,
338 product
->set_version(product_version_
);
339 base::CommandLine
uninstall_command(
341 .AppendASCII(product_version_
.GetString())
342 .Append(installer::kInstallerDir
)
343 .Append(installer::kSetupExe
));
344 uninstall_command
.AppendSwitch(installer::switches::kUninstall
);
345 product
->set_uninstall_command(uninstall_command
);
348 void UninstallProduct() {
349 FakeProductState::FromProductState(
350 original_state_
->GetNonVersionedProductState(kSystemInstall_
,
352 ->set_version(Version());
355 static const bool kSystemInstall_
;
356 static const BrowserDistribution::Type kProductType_
;
357 base::ScopedTempDir test_dir_
;
358 Version product_version_
;
359 Version max_version_
;
360 scoped_ptr
<FakeInstallationState
> original_state_
;
361 scoped_ptr
<installer::InstallerState
> installer_state_
;
364 registry_util::RegistryOverrideManager registry_override_manager_
;
366 DISALLOW_COPY_AND_ASSIGN(FindArchiveToPatchTest
);
369 const bool FindArchiveToPatchTest::kSystemInstall_
= false;
370 const BrowserDistribution::Type
FindArchiveToPatchTest::kProductType_
=
371 BrowserDistribution::CHROME_BROWSER
;
375 // Test that the path to the advertised product version is found.
376 TEST_F(FindArchiveToPatchTest
, ProductVersionFound
) {
377 base::FilePath
patch_source(installer::FindArchiveToPatch(
378 *original_state_
, *installer_state_
, base::Version()));
379 EXPECT_EQ(GetProductVersionArchivePath().value(), patch_source
.value());
382 // Test that the path to the max version is found if the advertised version is
384 TEST_F(FindArchiveToPatchTest
, MaxVersionFound
) {
385 // The patch file is absent.
386 ASSERT_TRUE(base::DeleteFile(GetProductVersionArchivePath(), false));
387 base::FilePath
patch_source(installer::FindArchiveToPatch(
388 *original_state_
, *installer_state_
, base::Version()));
389 EXPECT_EQ(GetMaxVersionArchivePath().value(), patch_source
.value());
391 // The product doesn't appear to be installed, so the max version is found.
393 patch_source
= installer::FindArchiveToPatch(
394 *original_state_
, *installer_state_
, base::Version());
395 EXPECT_EQ(GetMaxVersionArchivePath().value(), patch_source
.value());
398 // Test that an empty path is returned if no version is found.
399 TEST_F(FindArchiveToPatchTest
, NoVersionFound
) {
400 // The product doesn't appear to be installed and no archives are present.
402 ASSERT_TRUE(base::DeleteFile(GetProductVersionArchivePath(), false));
403 ASSERT_TRUE(base::DeleteFile(GetMaxVersionArchivePath(), false));
405 base::FilePath
patch_source(installer::FindArchiveToPatch(
406 *original_state_
, *installer_state_
, base::Version()));
407 EXPECT_EQ(base::FilePath::StringType(), patch_source
.value());
410 TEST_F(FindArchiveToPatchTest
, DesiredVersionFound
) {
411 base::FilePath
patch_source1(installer::FindArchiveToPatch(
412 *original_state_
, *installer_state_
, product_version_
));
413 EXPECT_EQ(GetProductVersionArchivePath().value(), patch_source1
.value());
414 base::FilePath
patch_source2(installer::FindArchiveToPatch(
415 *original_state_
, *installer_state_
, max_version_
));
416 EXPECT_EQ(GetMaxVersionArchivePath().value(), patch_source2
.value());
419 TEST_F(FindArchiveToPatchTest
, DesiredVersionNotFound
) {
420 base::FilePath
patch_source(installer::FindArchiveToPatch(
421 *original_state_
, *installer_state_
, base::Version("1.2.3.4")));
422 EXPECT_EQ(base::FilePath().value(), patch_source
.value());
425 #if defined(GOOGLE_CHROME_BUILD)
427 const bool kSystemLevel
= false;
428 const HKEY kRootKey
= kSystemLevel
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
429 const wchar_t kVersionString
[] = L
"30.0.1574.0";
430 const wchar_t kMultiChannel
[] = L
"2.0-dev-multi-chromeframe";
432 class MigrateMultiToSingleTest
: public testing::Test
{
434 void SetUp() override
{
435 registry_override_manager_
.OverrideRegistry(kRootKey
);
439 registry_util::RegistryOverrideManager registry_override_manager_
;
443 // Test migrating Chrome Frame from multi to single.
444 TEST_F(MigrateMultiToSingleTest
, ChromeFrame
) {
445 installer::ProductState chrome_frame
;
446 installer::ProductState binaries
;
447 DWORD usagestats
= 0;
449 // Set up a config with dev-channel multi-install GCF.
450 base::win::RegKey key
;
452 BrowserDistribution
* dist
= BrowserDistribution::GetSpecificDistribution(
453 BrowserDistribution::CHROME_BINARIES
);
454 ASSERT_EQ(ERROR_SUCCESS
,
455 base::win::RegKey(kRootKey
, dist
->GetVersionKey().c_str(),
457 .WriteValue(google_update::kRegVersionField
, kVersionString
));
458 ASSERT_EQ(ERROR_SUCCESS
,
459 base::win::RegKey(kRootKey
, dist
->GetStateKey().c_str(),
461 .WriteValue(google_update::kRegApField
, kMultiChannel
));
462 ASSERT_EQ(ERROR_SUCCESS
,
463 base::win::RegKey(kRootKey
, dist
->GetStateKey().c_str(),
465 .WriteValue(google_update::kRegUsageStatsField
, 1U));
467 dist
= BrowserDistribution::GetSpecificDistribution(
468 BrowserDistribution::CHROME_FRAME
);
469 ASSERT_EQ(ERROR_SUCCESS
,
470 base::win::RegKey(kRootKey
, dist
->GetVersionKey().c_str(),
472 .WriteValue(google_update::kRegVersionField
, kVersionString
));
473 ASSERT_EQ(ERROR_SUCCESS
,
474 base::win::RegKey(kRootKey
, dist
->GetStateKey().c_str(),
476 .WriteValue(google_update::kRegApField
, kMultiChannel
));
478 // Do the registry migration.
479 installer::InstallationState machine_state
;
480 machine_state
.Initialize();
482 installer::MigrateGoogleUpdateStateMultiToSingle(
484 BrowserDistribution::CHROME_FRAME
,
487 // Confirm that usagestats were copied to CF and that its channel was
489 ASSERT_TRUE(chrome_frame
.Initialize(kSystemLevel
,
490 BrowserDistribution::CHROME_FRAME
));
491 EXPECT_TRUE(chrome_frame
.GetUsageStats(&usagestats
));
492 EXPECT_EQ(1U, usagestats
);
493 EXPECT_EQ(L
"2.0-dev", chrome_frame
.channel().value());
495 // Confirm that the binaries' channel no longer contains GCF.
496 ASSERT_TRUE(binaries
.Initialize(kSystemLevel
,
497 BrowserDistribution::CHROME_BINARIES
));
498 EXPECT_EQ(L
"2.0-dev-multi", binaries
.channel().value());
502 TEST(SetupUtilTest
, ContainsUnsupportedSwitch
) {
503 EXPECT_FALSE(installer::ContainsUnsupportedSwitch(
504 base::CommandLine::FromString(L
"foo.exe")));
505 EXPECT_FALSE(installer::ContainsUnsupportedSwitch(
506 base::CommandLine::FromString(L
"foo.exe --multi-install --chrome")));
507 EXPECT_TRUE(installer::ContainsUnsupportedSwitch(
508 base::CommandLine::FromString(L
"foo.exe --chrome-frame")));
511 TEST(SetupUtilTest
, GetRegistrationDataCommandKey
) {
512 base::string16 app_guid
= L
"{AAAAAAAA-BBBB-1111-0123-456789ABCDEF}";
513 UpdatingAppRegistrationData
reg_data(app_guid
);
515 installer::GetRegistrationDataCommandKey(reg_data
, L
"test_name");
516 EXPECT_TRUE(base::EndsWith(key
, app_guid
+ L
"\\Commands\\test_name",
517 base::CompareCase::SENSITIVE
));