blob: 372dc8a02ae5bc3b42da59cfd02e39b50b44e50d [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
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 Pawlicki8f755d382020-03-20 19:51:1012#include "chrome/common/mac/launchd.h"
Mila Green8ddc4e02020-05-01 00:15:0213#include "chrome/updater/constants.h"
Joshua Pawlicki8f755d382020-03-20 19:51:1014#include "chrome/updater/mac/xpc_service_names.h"
Joshua Pawlickibae1c6d2352020-03-19 19:21:4415#include "chrome/updater/updater_version.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace updater {
19
20namespace test {
21
22namespace {
23
24base::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 Pawlicki8f755d382020-03-20 19:51:1035base::FilePath GetProductPath() {
36 return base::mac::GetUserLibraryPath()
37 .AppendASCII(COMPANY_SHORTNAME_STRING)
38 .AppendASCII(PRODUCT_FULLNAME_STRING);
39}
40
Joshua Pawlicki9d1340fc2020-08-14 20:59:3541base::FilePath GetDataDirPath() {
42 return base::mac::GetUserLibraryPath()
43 .AppendASCII("Application Support")
44 .AppendASCII(COMPANY_SHORTNAME_STRING)
45 .AppendASCII(PRODUCT_FULLNAME_STRING);
46}
47
Joshua Pawlickibae1c6d2352020-03-19 19:21:4448bool 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
60void Clean() {
Lei Zhang329c5bc2020-07-07 21:09:1161 EXPECT_TRUE(base::DeletePathRecursively(GetProductPath()));
Joshua Pawlicki8f755d382020-03-20 19:51:1062 EXPECT_TRUE(Launchd::GetInstance()->DeletePlist(
Mila Green7414a0ed2020-07-21 21:30:4263 Launchd::User, Launchd::Agent, updater::CopyWakeLaunchdName()));
Joshua Pawlicki8f755d382020-03-20 19:51:1064 EXPECT_TRUE(Launchd::GetInstance()->DeletePlist(
Mila Green7414a0ed2020-07-21 21:30:4265 Launchd::User, Launchd::Agent, updater::CopyControlLaunchdName()));
66 EXPECT_TRUE(Launchd::GetInstance()->DeletePlist(
67 Launchd::User, Launchd::Agent, updater::CopyServiceLaunchdName()));
Joshua Pawlicki9d1340fc2020-08-14 20:59:3568 EXPECT_TRUE(base::DeletePathRecursively(GetDataDirPath()));
Joshua Pawlickibae1c6d2352020-03-19 19:21:4469}
70
71void ExpectClean() {
72 // Files must not exist on the file system.
Joshua Pawlicki8f755d382020-03-20 19:51:1073 EXPECT_FALSE(base::PathExists(GetProductPath()));
74 EXPECT_FALSE(Launchd::GetInstance()->PlistExists(
Mila Green7414a0ed2020-07-21 21:30:4275 Launchd::User, Launchd::Agent, updater::CopyWakeLaunchdName()));
Mila Green8ddc4e02020-05-01 00:15:0276 EXPECT_FALSE(Launchd::GetInstance()->PlistExists(
Mila Green7414a0ed2020-07-21 21:30:4277 Launchd::User, Launchd::Agent, updater::CopyControlLaunchdName()));
78 EXPECT_FALSE(Launchd::GetInstance()->PlistExists(
79 Launchd::User, Launchd::Agent, updater::CopyServiceLaunchdName()));
Joshua Pawlicki9d1340fc2020-08-14 20:59:3580 EXPECT_FALSE(base::PathExists(GetDataDirPath()));
Joshua Pawlickibae1c6d2352020-03-19 19:21:4481}
82
83void ExpectInstalled() {
84 // Files must exist on the file system.
Joshua Pawlicki8f755d382020-03-20 19:51:1085 EXPECT_TRUE(base::PathExists(GetProductPath()));
Mila Green7414a0ed2020-07-21 21:30:4286 EXPECT_TRUE(Launchd::GetInstance()->PlistExists(Launchd::User, Launchd::Agent,
87 CopyWakeLaunchdName()));
88 EXPECT_TRUE(Launchd::GetInstance()->PlistExists(Launchd::User, Launchd::Agent,
89 CopyControlLaunchdName()));
Joshua Pawlickibae1c6d2352020-03-19 19:21:4490}
91
92void Install() {
Mila Green8ddc4e02020-05-01 00:15:0293 const base::FilePath path = GetExecutablePath();
Joshua Pawlickibae1c6d2352020-03-19 19:21:4494 ASSERT_FALSE(path.empty());
Michael Change4785a822020-03-26 20:55:0195 base::CommandLine command_line(path);
Mila Green8ddc4e02020-05-01 00:15:0296 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 Green9056ede2020-05-18 20:48:40102void ExpectActive() {
Mila Green8ddc4e02020-05-01 00:15:02103 // Files must exist on the file system.
104 EXPECT_TRUE(base::PathExists(GetProductPath()));
Joshua Pawlicki24b48cc2020-07-01 21:06:24105 EXPECT_TRUE(Launchd::GetInstance()->PlistExists(Launchd::User, Launchd::Agent,
Mila Green7414a0ed2020-07-21 21:30:42106 CopyServiceLaunchdName()));
Mila Green8ddc4e02020-05-01 00:15:02107}
108
Joshua Pawlickibae1c6d2352020-03-19 19:21:44109void Uninstall() {
Mila Green8ddc4e02020-05-01 00:15:02110 const base::FilePath path = GetExecutablePath();
Joshua Pawlickibae1c6d2352020-03-19 19:21:44111 ASSERT_FALSE(path.empty());
112 base::CommandLine command_line(path);
Mila Green8ddc4e02020-05-01 00:15:02113 command_line.AppendSwitch(kUninstallSwitch);
Joshua Pawlickibae1c6d2352020-03-19 19:21:44114 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