Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 1 | // Copyright 2020 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. |
| 4 | |
| 5 | #include "base/command_line.h" |
| 6 | #include "base/files/file_path.h" |
| 7 | #include "base/files/file_util.h" |
| 8 | #include "base/mac/foundation_util.h" |
| 9 | #include "base/path_service.h" |
| 10 | #include "base/process/launch.h" |
| 11 | #include "base/process/process.h" |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 12 | #include "chrome/common/mac/launchd.h" |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 13 | #include "chrome/updater/constants.h" |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 14 | #include "chrome/updater/mac/xpc_service_names.h" |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 15 | #include "chrome/updater/updater_version.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace updater { |
| 19 | |
| 20 | namespace test { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | base::FilePath GetExecutablePath() { |
| 25 | base::FilePath test_executable; |
| 26 | if (!base::PathService::Get(base::FILE_EXE, &test_executable)) |
| 27 | return base::FilePath(); |
| 28 | return test_executable.DirName() |
| 29 | .Append(FILE_PATH_LITERAL(PRODUCT_FULLNAME_STRING ".App")) |
| 30 | .Append(FILE_PATH_LITERAL("Contents")) |
| 31 | .Append(FILE_PATH_LITERAL("MacOS")) |
| 32 | .Append(FILE_PATH_LITERAL(PRODUCT_FULLNAME_STRING)); |
| 33 | } |
| 34 | |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 35 | base::FilePath GetProductPath() { |
| 36 | return base::mac::GetUserLibraryPath() |
| 37 | .AppendASCII(COMPANY_SHORTNAME_STRING) |
| 38 | .AppendASCII(PRODUCT_FULLNAME_STRING); |
| 39 | } |
| 40 | |
Joshua Pawlicki | 9d1340fc | 2020-08-14 20:59:35 | [diff] [blame] | 41 | base::FilePath GetDataDirPath() { |
| 42 | return base::mac::GetUserLibraryPath() |
| 43 | .AppendASCII("Application Support") |
| 44 | .AppendASCII(COMPANY_SHORTNAME_STRING) |
| 45 | .AppendASCII(PRODUCT_FULLNAME_STRING); |
| 46 | } |
| 47 | |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 48 | bool Run(base::CommandLine command_line, int* exit_code) { |
| 49 | auto process = base::LaunchProcess(command_line, {}); |
| 50 | if (!process.IsValid()) |
| 51 | return false; |
| 52 | if (!process.WaitForExitWithTimeout(base::TimeDelta::FromSeconds(60), |
| 53 | exit_code)) |
| 54 | return false; |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | void Clean() { |
Lei Zhang | 329c5bc | 2020-07-07 21:09:11 | [diff] [blame] | 61 | EXPECT_TRUE(base::DeletePathRecursively(GetProductPath())); |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 62 | EXPECT_TRUE(Launchd::GetInstance()->DeletePlist( |
Mila Green | 7414a0ed | 2020-07-21 21:30:42 | [diff] [blame] | 63 | Launchd::User, Launchd::Agent, updater::CopyWakeLaunchdName())); |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 64 | EXPECT_TRUE(Launchd::GetInstance()->DeletePlist( |
Mila Green | 7414a0ed | 2020-07-21 21:30:42 | [diff] [blame] | 65 | Launchd::User, Launchd::Agent, updater::CopyControlLaunchdName())); |
| 66 | EXPECT_TRUE(Launchd::GetInstance()->DeletePlist( |
| 67 | Launchd::User, Launchd::Agent, updater::CopyServiceLaunchdName())); |
Joshua Pawlicki | 9d1340fc | 2020-08-14 20:59:35 | [diff] [blame] | 68 | EXPECT_TRUE(base::DeletePathRecursively(GetDataDirPath())); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void ExpectClean() { |
| 72 | // Files must not exist on the file system. |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 73 | EXPECT_FALSE(base::PathExists(GetProductPath())); |
| 74 | EXPECT_FALSE(Launchd::GetInstance()->PlistExists( |
Mila Green | 7414a0ed | 2020-07-21 21:30:42 | [diff] [blame] | 75 | Launchd::User, Launchd::Agent, updater::CopyWakeLaunchdName())); |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 76 | EXPECT_FALSE(Launchd::GetInstance()->PlistExists( |
Mila Green | 7414a0ed | 2020-07-21 21:30:42 | [diff] [blame] | 77 | Launchd::User, Launchd::Agent, updater::CopyControlLaunchdName())); |
| 78 | EXPECT_FALSE(Launchd::GetInstance()->PlistExists( |
| 79 | Launchd::User, Launchd::Agent, updater::CopyServiceLaunchdName())); |
Joshua Pawlicki | 9d1340fc | 2020-08-14 20:59:35 | [diff] [blame] | 80 | EXPECT_FALSE(base::PathExists(GetDataDirPath())); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void ExpectInstalled() { |
| 84 | // Files must exist on the file system. |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 85 | EXPECT_TRUE(base::PathExists(GetProductPath())); |
Mila Green | 7414a0ed | 2020-07-21 21:30:42 | [diff] [blame] | 86 | EXPECT_TRUE(Launchd::GetInstance()->PlistExists(Launchd::User, Launchd::Agent, |
| 87 | CopyWakeLaunchdName())); |
| 88 | EXPECT_TRUE(Launchd::GetInstance()->PlistExists(Launchd::User, Launchd::Agent, |
| 89 | CopyControlLaunchdName())); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void Install() { |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 93 | const base::FilePath path = GetExecutablePath(); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 94 | ASSERT_FALSE(path.empty()); |
Michael Chang | e4785a82 | 2020-03-26 20:55:01 | [diff] [blame] | 95 | base::CommandLine command_line(path); |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 96 | command_line.AppendSwitch(kInstallSwitch); |
| 97 | int exit_code = -1; |
| 98 | ASSERT_TRUE(Run(command_line, &exit_code)); |
| 99 | EXPECT_EQ(0, exit_code); |
| 100 | } |
| 101 | |
Mila Green | 9056ede | 2020-05-18 20:48:40 | [diff] [blame] | 102 | void ExpectActive() { |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 103 | // Files must exist on the file system. |
| 104 | EXPECT_TRUE(base::PathExists(GetProductPath())); |
Joshua Pawlicki | 24b48cc | 2020-07-01 21:06:24 | [diff] [blame] | 105 | EXPECT_TRUE(Launchd::GetInstance()->PlistExists(Launchd::User, Launchd::Agent, |
Mila Green | 7414a0ed | 2020-07-21 21:30:42 | [diff] [blame] | 106 | CopyServiceLaunchdName())); |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 107 | } |
| 108 | |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 109 | void Uninstall() { |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 110 | const base::FilePath path = GetExecutablePath(); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 111 | ASSERT_FALSE(path.empty()); |
| 112 | base::CommandLine command_line(path); |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 113 | command_line.AppendSwitch(kUninstallSwitch); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 114 | int exit_code = -1; |
| 115 | ASSERT_TRUE(Run(command_line, &exit_code)); |
| 116 | EXPECT_EQ(0, exit_code); |
| 117 | } |
| 118 | |
| 119 | } // namespace test |
| 120 | |
| 121 | } // namespace updater |