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 | |
Mila Green | a3991ae | 2020-10-16 00:10:55 | [diff] [blame] | 5 | #include <stdint.h> |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 6 | #include <string> |
Adam Norberg | 14b66a1 | 2021-01-20 21:54:03 | [diff] [blame] | 7 | #include <vector> |
Mila Green | a3991ae | 2020-10-16 00:10:55 | [diff] [blame] | 8 | |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 9 | #include "base/command_line.h" |
| 10 | #include "base/files/file_path.h" |
| 11 | #include "base/files/file_util.h" |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 12 | #include "base/logging.h" |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 13 | #include "base/mac/foundation_util.h" |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 14 | #include "base/path_service.h" |
Joshua Pawlicki | a6f88e2 | 2021-05-06 14:50:12 | [diff] [blame] | 15 | #include "base/process/launch.h" |
Joshua Pawlicki | cd6925c9 | 2020-12-15 15:07:50 | [diff] [blame] | 16 | #include "base/run_loop.h" |
| 17 | #include "base/strings/sys_string_conversions.h" |
| 18 | #include "base/test/bind.h" |
Joshua Pawlicki | a6f88e2 | 2021-05-06 14:50:12 | [diff] [blame] | 19 | #include "base/test/test_timeouts.h" |
| 20 | #include "base/time/time.h" |
Mila Green | a3991ae | 2020-10-16 00:10:55 | [diff] [blame] | 21 | #include "base/version.h" |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 22 | #include "chrome/common/mac/launchd.h" |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 23 | #include "chrome/updater/constants.h" |
Adam Norberg | 14b66a1 | 2021-01-20 21:54:03 | [diff] [blame] | 24 | #include "chrome/updater/external_constants_builder.h" |
Joshua Pawlicki | cd6925c9 | 2020-12-15 15:07:50 | [diff] [blame] | 25 | #include "chrome/updater/launchd_util.h" |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 26 | #import "chrome/updater/mac/mac_util.h" |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 27 | #include "chrome/updater/mac/xpc_service_names.h" |
Mila Green | a3991ae | 2020-10-16 00:10:55 | [diff] [blame] | 28 | #include "chrome/updater/prefs.h" |
Mila Green | 3c9375d | 2021-03-17 16:33:59 | [diff] [blame] | 29 | #include "chrome/updater/test/integration_tests_impl.h" |
Mila Green | 3e3058a | 2020-08-27 16:59:27 | [diff] [blame] | 30 | #include "chrome/updater/test/test_app/constants.h" |
| 31 | #include "chrome/updater/test/test_app/test_app_version.h" |
Mila Green | aff08690 | 2021-01-07 22:00:52 | [diff] [blame] | 32 | #include "chrome/updater/updater_branding.h" |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 33 | #include "chrome/updater/updater_scope.h" |
Joshua Pawlicki | 4abd132 | 2020-08-19 22:05:57 | [diff] [blame] | 34 | #include "chrome/updater/util.h" |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 35 | #include "testing/gtest/include/gtest/gtest.h" |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 36 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 37 | #include "url/gurl.h" |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 38 | |
| 39 | namespace updater { |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 40 | namespace test { |
| 41 | |
| 42 | namespace { |
| 43 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 44 | Launchd::Domain LaunchdDomain(UpdaterScope scope) { |
| 45 | switch (scope) { |
| 46 | case UpdaterScope::kSystem: |
| 47 | return Launchd::Domain::Local; |
| 48 | case UpdaterScope::kUser: |
| 49 | return Launchd::Domain::User; |
| 50 | } |
| 51 | } |
Joshua Pawlicki | cd6925c9 | 2020-12-15 15:07:50 | [diff] [blame] | 52 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 53 | Launchd::Type LaunchdType(UpdaterScope scope) { |
| 54 | switch (scope) { |
| 55 | case UpdaterScope::kSystem: |
| 56 | return Launchd::Type::Daemon; |
| 57 | case UpdaterScope::kUser: |
| 58 | return Launchd::Type::Agent; |
| 59 | } |
Joshua Pawlicki | cd6925c9 | 2020-12-15 15:07:50 | [diff] [blame] | 60 | } |
| 61 | |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 62 | base::FilePath GetExecutablePath() { |
| 63 | base::FilePath test_executable; |
| 64 | if (!base::PathService::Get(base::FILE_EXE, &test_executable)) |
| 65 | return base::FilePath(); |
| 66 | return test_executable.DirName() |
Mila Green | a3991ae | 2020-10-16 00:10:55 | [diff] [blame] | 67 | .Append(FILE_PATH_LITERAL(PRODUCT_FULLNAME_STRING ".app")) |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 68 | .Append(FILE_PATH_LITERAL("Contents")) |
| 69 | .Append(FILE_PATH_LITERAL("MacOS")) |
| 70 | .Append(FILE_PATH_LITERAL(PRODUCT_FULLNAME_STRING)); |
| 71 | } |
| 72 | |
Mila Green | 3e3058a | 2020-08-27 16:59:27 | [diff] [blame] | 73 | base::FilePath GetTestAppExecutablePath() { |
| 74 | base::FilePath test_executable; |
| 75 | if (!base::PathService::Get(base::FILE_EXE, &test_executable)) |
| 76 | return base::FilePath(); |
| 77 | return test_executable.DirName() |
Mila Green | a3991ae | 2020-10-16 00:10:55 | [diff] [blame] | 78 | .Append(FILE_PATH_LITERAL(TEST_APP_FULLNAME_STRING ".app")) |
Mila Green | 3e3058a | 2020-08-27 16:59:27 | [diff] [blame] | 79 | .Append(FILE_PATH_LITERAL("Contents")) |
| 80 | .Append(FILE_PATH_LITERAL("MacOS")) |
| 81 | .Append(FILE_PATH_LITERAL(TEST_APP_FULLNAME_STRING)); |
| 82 | } |
| 83 | |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 84 | absl::optional<base::FilePath> GetProductPath(UpdaterScope scope) { |
| 85 | absl::optional<base::FilePath> path = GetLibraryFolderPath(scope); |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 86 | if (!path) |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 87 | return absl::nullopt; |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 88 | |
| 89 | return path->AppendASCII(COMPANY_SHORTNAME_STRING) |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 90 | .AppendASCII(PRODUCT_FULLNAME_STRING); |
| 91 | } |
| 92 | |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 93 | absl::optional<base::FilePath> GetActiveFile(UpdaterScope scope, |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 94 | const std::string& id) { |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 95 | const absl::optional<base::FilePath> path = |
Mila Green | 708bf0d9 | 2021-03-23 17:33:15 | [diff] [blame] | 96 | GetLibraryFolderPath(UpdaterScope::kUser); |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 97 | if (!path) |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 98 | return absl::nullopt; |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 99 | |
| 100 | return path->AppendASCII(COMPANY_SHORTNAME_STRING) |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 101 | .AppendASCII(COMPANY_SHORTNAME_STRING "SoftwareUpdate") |
| 102 | .AppendASCII("Actives") |
| 103 | .AppendASCII(id); |
| 104 | } |
| 105 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 106 | void ExpectServiceAbsent(UpdaterScope scope, const std::string& service) { |
| 107 | VLOG(0) << __func__ << " - scope: " << scope << ". service: " << service; |
Joshua Pawlicki | cd6925c9 | 2020-12-15 15:07:50 | [diff] [blame] | 108 | bool success = false; |
| 109 | base::RunLoop loop; |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 110 | PollLaunchctlList(scope, service, LaunchctlPresence::kAbsent, |
Joshua Pawlicki | cd6925c9 | 2020-12-15 15:07:50 | [diff] [blame] | 111 | base::TimeDelta::FromSeconds(7), |
| 112 | base::BindLambdaForTesting([&](bool result) { |
| 113 | success = result; |
| 114 | loop.QuitClosure().Run(); |
| 115 | })); |
| 116 | loop.Run(); |
| 117 | EXPECT_TRUE(success) << service << " is unexpectedly present."; |
| 118 | } |
| 119 | |
Joshua Pawlicki | 83d1d823 | 2020-10-23 20:13:31 | [diff] [blame] | 120 | } // namespace |
| 121 | |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 122 | void EnterTestMode(const GURL& url) { |
Adam Norberg | 14b66a1 | 2021-01-20 21:54:03 | [diff] [blame] | 123 | ASSERT_TRUE(ExternalConstantsBuilder() |
| 124 | .SetUpdateURL(std::vector<std::string>{url.spec()}) |
| 125 | .SetUseCUP(false) |
Mila Green | 1cb2696 | 2021-01-21 01:00:00 | [diff] [blame] | 126 | .SetInitialDelay(0.1) |
Mila Green | 59389f1 | 2021-02-03 20:52:32 | [diff] [blame] | 127 | .SetServerKeepAliveSeconds(1) |
Adam Norberg | 14b66a1 | 2021-01-20 21:54:03 | [diff] [blame] | 128 | .Overwrite()); |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 129 | } |
| 130 | |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 131 | absl::optional<base::FilePath> GetDataDirPath(UpdaterScope scope) { |
| 132 | absl::optional<base::FilePath> app_path = |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 133 | GetApplicationSupportDirectory(scope); |
| 134 | if (!app_path) { |
| 135 | VLOG(1) << "Failed to get Application support path."; |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 136 | return absl::nullopt; |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 137 | } |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 138 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 139 | return app_path->AppendASCII(COMPANY_SHORTNAME_STRING) |
Joshua Pawlicki | 9d1340fc | 2020-08-14 20:59:35 | [diff] [blame] | 140 | .AppendASCII(PRODUCT_FULLNAME_STRING); |
| 141 | } |
| 142 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 143 | void Clean(UpdaterScope scope) { |
| 144 | Launchd::Domain launchd_domain = LaunchdDomain(scope); |
| 145 | Launchd::Type launchd_type = LaunchdType(scope); |
| 146 | |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 147 | absl::optional<base::FilePath> path = GetProductPath(scope); |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 148 | EXPECT_TRUE(path); |
| 149 | if (path) |
| 150 | EXPECT_TRUE(base::DeletePathRecursively(*path)); |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 151 | EXPECT_TRUE(Launchd::GetInstance()->DeletePlist( |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 152 | launchd_domain, launchd_type, updater::CopyWakeLaunchdName())); |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 153 | EXPECT_TRUE(Launchd::GetInstance()->DeletePlist( |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 154 | launchd_domain, launchd_type, |
Mila Green | 8084564 | 2020-12-10 03:44:51 | [diff] [blame] | 155 | updater::CopyUpdateServiceInternalLaunchdName())); |
Mila Green | 7414a0ed | 2020-07-21 21:30:42 | [diff] [blame] | 156 | EXPECT_TRUE(Launchd::GetInstance()->DeletePlist( |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 157 | launchd_domain, launchd_type, updater::CopyUpdateServiceLaunchdName())); |
| 158 | |
| 159 | path = GetDataDirPath(scope); |
| 160 | EXPECT_TRUE(path); |
| 161 | if (path) |
| 162 | EXPECT_TRUE(base::DeletePathRecursively(*path)); |
Joshua Pawlicki | 3f64588 | 2020-08-21 20:36:17 | [diff] [blame] | 163 | |
| 164 | @autoreleasepool { |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 165 | RemoveJobFromLaunchd(scope, launchd_domain, launchd_type, |
Mila Green | 3c9375d | 2021-03-17 16:33:59 | [diff] [blame] | 166 | CopyWakeLaunchdName()); |
| 167 | RemoveJobFromLaunchd(scope, launchd_domain, launchd_type, |
Joshua Pawlicki | cd6925c9 | 2020-12-15 15:07:50 | [diff] [blame] | 168 | CopyUpdateServiceLaunchdName()); |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 169 | RemoveJobFromLaunchd(scope, launchd_domain, launchd_type, |
Joshua Pawlicki | cd6925c9 | 2020-12-15 15:07:50 | [diff] [blame] | 170 | CopyUpdateServiceInternalLaunchdName()); |
Joshua Pawlicki | 3f64588 | 2020-08-21 20:36:17 | [diff] [blame] | 171 | } |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 172 | } |
| 173 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 174 | void ExpectClean(UpdaterScope scope) { |
| 175 | Launchd::Domain launchd_domain = LaunchdDomain(scope); |
| 176 | Launchd::Type launchd_type = LaunchdType(scope); |
| 177 | |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 178 | // Files must not exist on the file system. |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 179 | absl::optional<base::FilePath> path = GetProductPath(scope); |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 180 | EXPECT_TRUE(path); |
| 181 | if (path) |
| 182 | EXPECT_FALSE(base::PathExists(*path)); |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame] | 183 | EXPECT_FALSE(Launchd::GetInstance()->PlistExists( |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 184 | launchd_domain, launchd_type, updater::CopyWakeLaunchdName())); |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 185 | EXPECT_FALSE(Launchd::GetInstance()->PlistExists( |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 186 | launchd_domain, launchd_type, |
Mila Green | 8084564 | 2020-12-10 03:44:51 | [diff] [blame] | 187 | updater::CopyUpdateServiceInternalLaunchdName())); |
Mila Green | 7414a0ed | 2020-07-21 21:30:42 | [diff] [blame] | 188 | EXPECT_FALSE(Launchd::GetInstance()->PlistExists( |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 189 | launchd_domain, launchd_type, updater::CopyUpdateServiceLaunchdName())); |
| 190 | |
| 191 | path = GetDataDirPath(scope); |
| 192 | EXPECT_TRUE(path); |
| 193 | if (path) |
| 194 | EXPECT_FALSE(base::PathExists(*path)); |
| 195 | |
Joshua Pawlicki | 6c67e23c | 2021-05-07 19:32:31 | [diff] [blame] | 196 | ExpectServiceAbsent(scope, GetUpdateServiceLaunchdName()); |
| 197 | ExpectServiceAbsent(scope, GetUpdateServiceInternalLaunchdName()); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 198 | } |
| 199 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 200 | void ExpectInstalled(UpdaterScope scope) { |
| 201 | Launchd::Domain launchd_domain = LaunchdDomain(scope); |
| 202 | Launchd::Type launchd_type = LaunchdType(scope); |
| 203 | |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 204 | // Files must exist on the file system. |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 205 | absl::optional<base::FilePath> path = GetProductPath(scope); |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 206 | EXPECT_TRUE(path); |
| 207 | if (path) |
| 208 | EXPECT_TRUE(base::PathExists(*path)); |
| 209 | |
| 210 | EXPECT_TRUE(Launchd::GetInstance()->PlistExists(launchd_domain, launchd_type, |
Mila Green | 7414a0ed | 2020-07-21 21:30:42 | [diff] [blame] | 211 | CopyWakeLaunchdName())); |
Mila Green | 8084564 | 2020-12-10 03:44:51 | [diff] [blame] | 212 | EXPECT_TRUE(Launchd::GetInstance()->PlistExists( |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 213 | launchd_domain, launchd_type, CopyUpdateServiceInternalLaunchdName())); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 214 | } |
| 215 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 216 | void Install(UpdaterScope scope) { |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 217 | const base::FilePath path = GetExecutablePath(); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 218 | ASSERT_FALSE(path.empty()); |
Michael Chang | e4785a82 | 2020-03-26 20:55:01 | [diff] [blame] | 219 | base::CommandLine command_line(path); |
Joshua Pawlicki | 91ce318b | 2020-09-15 14:35:38 | [diff] [blame] | 220 | command_line.AppendSwitch(kInstallSwitch); |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 221 | int exit_code = -1; |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 222 | ASSERT_TRUE(Run(scope, command_line, &exit_code)); |
| 223 | EXPECT_EQ(exit_code, 0); |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 224 | } |
| 225 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 226 | void ExpectActiveUpdater(UpdaterScope scope) { |
| 227 | Launchd::Domain launchd_domain = LaunchdDomain(scope); |
| 228 | Launchd::Type launchd_type = LaunchdType(scope); |
| 229 | |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 230 | // Files must exist on the file system. |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 231 | absl::optional<base::FilePath> path = GetProductPath(scope); |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 232 | EXPECT_TRUE(path); |
| 233 | if (path) |
| 234 | EXPECT_TRUE(base::PathExists(*path)); |
| 235 | |
Mila Green | 8084564 | 2020-12-10 03:44:51 | [diff] [blame] | 236 | EXPECT_TRUE(Launchd::GetInstance()->PlistExists( |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 237 | launchd_domain, launchd_type, CopyUpdateServiceLaunchdName())); |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 238 | } |
| 239 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 240 | void RegisterTestApp(UpdaterScope scope) { |
Mila Green | 3e3058a | 2020-08-27 16:59:27 | [diff] [blame] | 241 | const base::FilePath path = GetTestAppExecutablePath(); |
| 242 | ASSERT_FALSE(path.empty()); |
| 243 | base::CommandLine command_line(path); |
| 244 | command_line.AppendSwitch(kRegisterUpdaterSwitch); |
| 245 | int exit_code = -1; |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 246 | ASSERT_TRUE(Run(scope, command_line, &exit_code)); |
| 247 | EXPECT_EQ(exit_code, 0); |
Mila Green | 3e3058a | 2020-08-27 16:59:27 | [diff] [blame] | 248 | } |
| 249 | |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 250 | absl::optional<base::FilePath> GetInstalledExecutablePath(UpdaterScope scope) { |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 251 | return GetUpdaterExecutablePath(scope); |
Mila Green | a3991ae | 2020-10-16 00:10:55 | [diff] [blame] | 252 | } |
| 253 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 254 | void ExpectCandidateUninstalled(UpdaterScope scope) { |
| 255 | Launchd::Domain launchd_domain = LaunchdDomain(scope); |
| 256 | Launchd::Type launchd_type = LaunchdType(scope); |
| 257 | |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 258 | absl::optional<base::FilePath> versioned_folder_path = |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 259 | GetVersionedUpdaterFolderPath(scope); |
| 260 | EXPECT_TRUE(versioned_folder_path); |
| 261 | if (versioned_folder_path) |
| 262 | EXPECT_FALSE(base::PathExists(*versioned_folder_path)); |
| 263 | |
| 264 | EXPECT_FALSE(Launchd::GetInstance()->PlistExists(launchd_domain, launchd_type, |
| 265 | CopyWakeLaunchdName())); |
Mila Green | a3991ae | 2020-10-16 00:10:55 | [diff] [blame] | 266 | EXPECT_FALSE(Launchd::GetInstance()->PlistExists( |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 267 | launchd_domain, launchd_type, CopyUpdateServiceInternalLaunchdName())); |
Joshua Pawlicki | 4abd132 | 2020-08-19 22:05:57 | [diff] [blame] | 268 | } |
| 269 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 270 | void Uninstall(UpdaterScope scope) { |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 271 | absl::optional<base::FilePath> path = GetExecutablePath(); |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 272 | ASSERT_TRUE(path); |
| 273 | base::CommandLine command_line(*path); |
Mila Green | 8ddc4e0 | 2020-05-01 00:15:02 | [diff] [blame] | 274 | command_line.AppendSwitch(kUninstallSwitch); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 275 | int exit_code = -1; |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 276 | ASSERT_TRUE(Run(scope, command_line, &exit_code)); |
| 277 | EXPECT_EQ(exit_code, 0); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 278 | } |
| 279 | |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 280 | absl::optional<base::FilePath> GetFakeUpdaterInstallFolderPath( |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 281 | UpdaterScope scope, |
| 282 | const base::Version& version) { |
| 283 | return GetExecutableFolderPathForVersion(scope, version); |
Mila Green | a3991ae | 2020-10-16 00:10:55 | [diff] [blame] | 284 | } |
| 285 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 286 | void SetActive(UpdaterScope scope, const std::string& app_id) { |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 287 | const absl::optional<base::FilePath> path = GetActiveFile(scope, app_id); |
Mila Green | 708bf0d9 | 2021-03-23 17:33:15 | [diff] [blame] | 288 | ASSERT_TRUE(path); |
| 289 | VLOG(0) << "Actives file: " << *path; |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 290 | base::File::Error err = base::File::FILE_OK; |
Mila Green | 708bf0d9 | 2021-03-23 17:33:15 | [diff] [blame] | 291 | EXPECT_TRUE(base::CreateDirectoryAndGetError(path->DirName(), &err)) |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 292 | << "Error: " << err; |
Mila Green | 708bf0d9 | 2021-03-23 17:33:15 | [diff] [blame] | 293 | EXPECT_TRUE(base::WriteFile(*path, "")); |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 294 | } |
| 295 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 296 | void ExpectActive(UpdaterScope scope, const std::string& app_id) { |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 297 | const absl::optional<base::FilePath> path = GetActiveFile(scope, app_id); |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 298 | ASSERT_TRUE(path); |
| 299 | EXPECT_TRUE(base::PathExists(*path)); |
| 300 | EXPECT_TRUE(base::PathIsWritable(*path)); |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 301 | } |
| 302 | |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 303 | void ExpectNotActive(UpdaterScope scope, const std::string& app_id) { |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame^] | 304 | const absl::optional<base::FilePath> path = GetActiveFile(scope, app_id); |
Mila Green | f6d82f8 | 2021-03-05 22:14:29 | [diff] [blame] | 305 | ASSERT_TRUE(path); |
| 306 | EXPECT_FALSE(base::PathExists(*path)); |
| 307 | EXPECT_FALSE(base::PathIsWritable(*path)); |
Joshua Pawlicki | 6867d7c | 2021-01-06 15:51:09 | [diff] [blame] | 308 | } |
| 309 | |
Joshua Pawlicki | a6f88e2 | 2021-05-06 14:50:12 | [diff] [blame] | 310 | void WaitForServerExit(UpdaterScope scope) { |
| 311 | base::TimeTicks deadline = |
| 312 | base::TimeTicks::Now() + TestTimeouts::action_max_timeout(); |
| 313 | while (base::TimeTicks::Now() < deadline) { |
| 314 | std::string ps_stdout; |
| 315 | ASSERT_TRUE(base::GetAppOutput({"ps", "ax", "-o", "command"}, &ps_stdout)); |
| 316 | if (ps_stdout.find(GetExecutablePath().BaseName().AsUTF8Unsafe()) == |
| 317 | std::string::npos) { |
| 318 | return; |
| 319 | } |
| 320 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200)); |
| 321 | } |
| 322 | FAIL() << __func__ << " timed out."; |
| 323 | } |
| 324 | |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 325 | } // namespace test |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 326 | } // namespace updater |