blob: 689ac17994a29c8dbc6ffcc6b54228672f6cb5f0 [file] [log] [blame]
Joshua Pawlickibae1c6d2352020-03-19 19:21:441// 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 Greena3991ae2020-10-16 00:10:555#include <stdint.h>
6
Joshua Pawlickibae1c6d2352020-03-19 19:21:447#include "base/command_line.h"
8#include "base/files/file_path.h"
9#include "base/files/file_util.h"
10#include "base/mac/foundation_util.h"
11#include "base/path_service.h"
Joshua Pawlickicd6925c92020-12-15 15:07:5012#include "base/run_loop.h"
13#include "base/strings/sys_string_conversions.h"
14#include "base/test/bind.h"
Mila Greena3991ae2020-10-16 00:10:5515#include "base/version.h"
Joshua Pawlicki8f755d382020-03-20 19:51:1016#include "chrome/common/mac/launchd.h"
Mila Green8ddc4e02020-05-01 00:15:0217#include "chrome/updater/constants.h"
Joshua Pawlickicd6925c92020-12-15 15:07:5018#include "chrome/updater/launchd_util.h"
Mila Greena3991ae2020-10-16 00:10:5519#import "chrome/updater/mac/util.h"
Joshua Pawlicki8f755d382020-03-20 19:51:1020#include "chrome/updater/mac/xpc_service_names.h"
Mila Greena3991ae2020-10-16 00:10:5521#include "chrome/updater/prefs.h"
Joshua Pawlicki83d1d8232020-10-23 20:13:3122#include "chrome/updater/test/integration_tests.h"
Mila Green3e3058a2020-08-27 16:59:2723#include "chrome/updater/test/test_app/constants.h"
24#include "chrome/updater/test/test_app/test_app_version.h"
Joshua Pawlickibae1c6d2352020-03-19 19:21:4425#include "chrome/updater/updater_version.h"
Joshua Pawlicki4abd1322020-08-19 22:05:5726#include "chrome/updater/util.h"
Joshua Pawlickibae1c6d2352020-03-19 19:21:4427#include "testing/gtest/include/gtest/gtest.h"
28
29namespace updater {
Joshua Pawlickibae1c6d2352020-03-19 19:21:4430namespace test {
31
Joshua Pawlicki83d1d8232020-10-23 20:13:3132// crbug.com/1112527: These tests are not compatible with component build.
33#if !defined(COMPONENT_BUILD)
34
Joshua Pawlickibae1c6d2352020-03-19 19:21:4435namespace {
36
Joshua Pawlickicd6925c92020-12-15 15:07:5037void RemoveJobFromLaunchd(Launchd::Domain domain,
38 Launchd::Type type,
39 base::ScopedCFTypeRef<CFStringRef> name) {
40 EXPECT_TRUE(Launchd::GetInstance()->DeletePlist(domain, type, name))
41 << "Failed to delete plist for " << name;
42
43 // Return value is ignored, since RemoveJob returns false if the job already
44 // doesn't exist.
45 Launchd::GetInstance()->RemoveJob(base::SysCFStringRefToUTF8(name));
46}
47
Joshua Pawlickibae1c6d2352020-03-19 19:21:4448base::FilePath GetExecutablePath() {
49 base::FilePath test_executable;
50 if (!base::PathService::Get(base::FILE_EXE, &test_executable))
51 return base::FilePath();
52 return test_executable.DirName()
Mila Greena3991ae2020-10-16 00:10:5553 .Append(FILE_PATH_LITERAL(PRODUCT_FULLNAME_STRING ".app"))
Joshua Pawlickibae1c6d2352020-03-19 19:21:4454 .Append(FILE_PATH_LITERAL("Contents"))
55 .Append(FILE_PATH_LITERAL("MacOS"))
56 .Append(FILE_PATH_LITERAL(PRODUCT_FULLNAME_STRING));
57}
58
Mila Green3e3058a2020-08-27 16:59:2759base::FilePath GetTestAppExecutablePath() {
60 base::FilePath test_executable;
61 if (!base::PathService::Get(base::FILE_EXE, &test_executable))
62 return base::FilePath();
63 return test_executable.DirName()
Mila Greena3991ae2020-10-16 00:10:5564 .Append(FILE_PATH_LITERAL(TEST_APP_FULLNAME_STRING ".app"))
Mila Green3e3058a2020-08-27 16:59:2765 .Append(FILE_PATH_LITERAL("Contents"))
66 .Append(FILE_PATH_LITERAL("MacOS"))
67 .Append(FILE_PATH_LITERAL(TEST_APP_FULLNAME_STRING));
68}
69
Joshua Pawlicki8f755d382020-03-20 19:51:1070base::FilePath GetProductPath() {
71 return base::mac::GetUserLibraryPath()
72 .AppendASCII(COMPANY_SHORTNAME_STRING)
73 .AppendASCII(PRODUCT_FULLNAME_STRING);
74}
75
Joshua Pawlickicd6925c92020-12-15 15:07:5076void ExpectServiceAbsent(const std::string& service) {
77 bool success = false;
78 base::RunLoop loop;
79 PollLaunchctlList(service, LaunchctlPresence::kAbsent,
80 base::TimeDelta::FromSeconds(7),
81 base::BindLambdaForTesting([&](bool result) {
82 success = result;
83 loop.QuitClosure().Run();
84 }));
85 loop.Run();
86 EXPECT_TRUE(success) << service << " is unexpectedly present.";
87}
88
Joshua Pawlicki83d1d8232020-10-23 20:13:3189} // namespace
90
Joshua Pawlicki9d1340fc2020-08-14 20:59:3591base::FilePath GetDataDirPath() {
92 return base::mac::GetUserLibraryPath()
93 .AppendASCII("Application Support")
94 .AppendASCII(COMPANY_SHORTNAME_STRING)
95 .AppendASCII(PRODUCT_FULLNAME_STRING);
96}
97
Joshua Pawlickibae1c6d2352020-03-19 19:21:4498void Clean() {
Lei Zhang329c5bc2020-07-07 21:09:1199 EXPECT_TRUE(base::DeletePathRecursively(GetProductPath()));
Joshua Pawlicki8f755d382020-03-20 19:51:10100 EXPECT_TRUE(Launchd::GetInstance()->DeletePlist(
Mila Green7414a0ed2020-07-21 21:30:42101 Launchd::User, Launchd::Agent, updater::CopyWakeLaunchdName()));
Joshua Pawlicki8f755d382020-03-20 19:51:10102 EXPECT_TRUE(Launchd::GetInstance()->DeletePlist(
Mila Green80845642020-12-10 03:44:51103 Launchd::User, Launchd::Agent,
104 updater::CopyUpdateServiceInternalLaunchdName()));
Mila Green7414a0ed2020-07-21 21:30:42105 EXPECT_TRUE(Launchd::GetInstance()->DeletePlist(
Mila Green80845642020-12-10 03:44:51106 Launchd::User, Launchd::Agent, updater::CopyUpdateServiceLaunchdName()));
Joshua Pawlicki9d1340fc2020-08-14 20:59:35107 EXPECT_TRUE(base::DeletePathRecursively(GetDataDirPath()));
Joshua Pawlicki3f645882020-08-21 20:36:17108
109 @autoreleasepool {
110 NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
111 [userDefaults
112 removeObjectForKey:[NSString stringWithUTF8String:kDevOverrideKeyUrl]];
113 [userDefaults
114 removeObjectForKey:[NSString
115 stringWithUTF8String:kDevOverrideKeyUseCUP]];
Joshua Pawlickicd6925c92020-12-15 15:07:50116
117 // TODO(crbug.com/1096654): support machine case (Launchd::Domain::Local and
118 // Launchd::Type::Daemon).
119 RemoveJobFromLaunchd(Launchd::Domain::User, Launchd::Type::Agent,
120 CopyUpdateServiceLaunchdName());
121 RemoveJobFromLaunchd(Launchd::Domain::User, Launchd::Type::Agent,
122 CopyUpdateServiceInternalLaunchdName());
Joshua Pawlicki3f645882020-08-21 20:36:17123 }
Joshua Pawlickibae1c6d2352020-03-19 19:21:44124}
125
126void ExpectClean() {
127 // Files must not exist on the file system.
Joshua Pawlicki8f755d382020-03-20 19:51:10128 EXPECT_FALSE(base::PathExists(GetProductPath()));
129 EXPECT_FALSE(Launchd::GetInstance()->PlistExists(
Mila Green7414a0ed2020-07-21 21:30:42130 Launchd::User, Launchd::Agent, updater::CopyWakeLaunchdName()));
Mila Green8ddc4e02020-05-01 00:15:02131 EXPECT_FALSE(Launchd::GetInstance()->PlistExists(
Mila Green80845642020-12-10 03:44:51132 Launchd::User, Launchd::Agent,
133 updater::CopyUpdateServiceInternalLaunchdName()));
Mila Green7414a0ed2020-07-21 21:30:42134 EXPECT_FALSE(Launchd::GetInstance()->PlistExists(
Mila Green80845642020-12-10 03:44:51135 Launchd::User, Launchd::Agent, updater::CopyUpdateServiceLaunchdName()));
Joshua Pawlicki9d1340fc2020-08-14 20:59:35136 EXPECT_FALSE(base::PathExists(GetDataDirPath()));
Joshua Pawlickicd6925c92020-12-15 15:07:50137 ExpectServiceAbsent(kUpdateServiceLaunchdName);
138 ExpectServiceAbsent(kUpdateServiceInternalLaunchdName);
Joshua Pawlickibae1c6d2352020-03-19 19:21:44139}
140
Joshua Pawlicki3f645882020-08-21 20:36:17141void EnterTestMode() {
142 // TODO(crbug.com/1119857): Point this to an actual fake server.
143 @autoreleasepool {
144 NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
145 [userDefaults setURL:[NSURL URLWithString:@"http://localhost:8367"]
146 forKey:[NSString stringWithUTF8String:kDevOverrideKeyUrl]];
147 [userDefaults
148 setBool:NO
149 forKey:[NSString stringWithUTF8String:kDevOverrideKeyUseCUP]];
150 }
151}
152
Joshua Pawlickibae1c6d2352020-03-19 19:21:44153void ExpectInstalled() {
154 // Files must exist on the file system.
Joshua Pawlicki8f755d382020-03-20 19:51:10155 EXPECT_TRUE(base::PathExists(GetProductPath()));
Mila Green7414a0ed2020-07-21 21:30:42156 EXPECT_TRUE(Launchd::GetInstance()->PlistExists(Launchd::User, Launchd::Agent,
157 CopyWakeLaunchdName()));
Mila Green80845642020-12-10 03:44:51158 EXPECT_TRUE(Launchd::GetInstance()->PlistExists(
159 Launchd::User, Launchd::Agent, CopyUpdateServiceInternalLaunchdName()));
Joshua Pawlickibae1c6d2352020-03-19 19:21:44160}
161
162void Install() {
Mila Green8ddc4e02020-05-01 00:15:02163 const base::FilePath path = GetExecutablePath();
Joshua Pawlickibae1c6d2352020-03-19 19:21:44164 ASSERT_FALSE(path.empty());
Michael Change4785a822020-03-26 20:55:01165 base::CommandLine command_line(path);
Joshua Pawlicki91ce318b2020-09-15 14:35:38166 command_line.AppendSwitch(kInstallSwitch);
Mila Green8ddc4e02020-05-01 00:15:02167 int exit_code = -1;
168 ASSERT_TRUE(Run(command_line, &exit_code));
169 EXPECT_EQ(0, exit_code);
170}
171
Mila Green9056ede2020-05-18 20:48:40172void ExpectActive() {
Mila Green8ddc4e02020-05-01 00:15:02173 // Files must exist on the file system.
174 EXPECT_TRUE(base::PathExists(GetProductPath()));
Mila Green80845642020-12-10 03:44:51175 EXPECT_TRUE(Launchd::GetInstance()->PlistExists(
176 Launchd::User, Launchd::Agent, CopyUpdateServiceLaunchdName()));
Mila Green8ddc4e02020-05-01 00:15:02177}
178
Mila Green3e3058a2020-08-27 16:59:27179void RegisterTestApp() {
180 const base::FilePath path = GetTestAppExecutablePath();
181 ASSERT_FALSE(path.empty());
182 base::CommandLine command_line(path);
183 command_line.AppendSwitch(kRegisterUpdaterSwitch);
184 int exit_code = -1;
185 ASSERT_TRUE(Run(command_line, &exit_code));
186 EXPECT_EQ(0, exit_code);
187}
188
Mila Greena3991ae2020-10-16 00:10:55189base::FilePath GetInstalledExecutablePath() {
190 return GetUpdaterExecutablePath();
191}
192
193void ExpectCandidateUninstalled() {
194 base::FilePath versioned_folder_path = GetVersionedUpdaterFolderPath();
195 EXPECT_FALSE(base::PathExists(versioned_folder_path));
196 EXPECT_FALSE(Launchd::GetInstance()->PlistExists(
197 Launchd::User, Launchd::Agent, CopyWakeLaunchdName()));
198 EXPECT_FALSE(Launchd::GetInstance()->PlistExists(
Mila Green80845642020-12-10 03:44:51199 Launchd::User, Launchd::Agent, CopyUpdateServiceInternalLaunchdName()));
Joshua Pawlicki4abd1322020-08-19 22:05:57200}
201
Joshua Pawlickibae1c6d2352020-03-19 19:21:44202void Uninstall() {
Mila Green8ddc4e02020-05-01 00:15:02203 const base::FilePath path = GetExecutablePath();
Joshua Pawlickibae1c6d2352020-03-19 19:21:44204 ASSERT_FALSE(path.empty());
205 base::CommandLine command_line(path);
Mila Green8ddc4e02020-05-01 00:15:02206 command_line.AppendSwitch(kUninstallSwitch);
Joshua Pawlickibae1c6d2352020-03-19 19:21:44207 int exit_code = -1;
208 ASSERT_TRUE(Run(command_line, &exit_code));
209 EXPECT_EQ(0, exit_code);
210}
211
Mila Greena3991ae2020-10-16 00:10:55212base::FilePath GetFakeUpdaterInstallFolderPath(const base::Version& version) {
213 return GetExecutableFolderPathForVersion(version);
214}
215
Joshua Pawlicki83d1d8232020-10-23 20:13:31216#endif // !defined(COMPONENT_BUILD)
217
Joshua Pawlickibae1c6d2352020-03-19 19:21:44218} // namespace test
Joshua Pawlickibae1c6d2352020-03-19 19:21:44219} // namespace updater