blob: 85fc865bef6b403ea18defd1828712a7dec6c20c [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"
13#include "chrome/updater/mac/xpc_service_names.h"
Joshua Pawlickibae1c6d2352020-03-19 19:21:4414#include "chrome/updater/updater_version.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace updater {
18
19namespace test {
20
21namespace {
22
23base::FilePath GetExecutablePath() {
24 base::FilePath test_executable;
25 if (!base::PathService::Get(base::FILE_EXE, &test_executable))
26 return base::FilePath();
27 return test_executable.DirName()
28 .Append(FILE_PATH_LITERAL(PRODUCT_FULLNAME_STRING ".App"))
29 .Append(FILE_PATH_LITERAL("Contents"))
30 .Append(FILE_PATH_LITERAL("MacOS"))
31 .Append(FILE_PATH_LITERAL(PRODUCT_FULLNAME_STRING));
32}
33
34base::FilePath GetInstallerPath() {
35 base::FilePath test_executable;
36 if (!base::PathService::Get(base::FILE_EXE, &test_executable))
37 return base::FilePath();
38 return test_executable.DirName().Append("updater_setup");
39}
40
Joshua Pawlicki8f755d382020-03-20 19:51:1041base::FilePath GetProductPath() {
42 return base::mac::GetUserLibraryPath()
43 .AppendASCII(COMPANY_SHORTNAME_STRING)
44 .AppendASCII(PRODUCT_FULLNAME_STRING);
45}
46
Joshua Pawlickibae1c6d2352020-03-19 19:21:4447bool Run(base::CommandLine command_line, int* exit_code) {
48 auto process = base::LaunchProcess(command_line, {});
49 if (!process.IsValid())
50 return false;
51 if (!process.WaitForExitWithTimeout(base::TimeDelta::FromSeconds(60),
52 exit_code))
53 return false;
54 return true;
55}
56
57} // namespace
58
59void Clean() {
Joshua Pawlicki8f755d382020-03-20 19:51:1060 EXPECT_TRUE(base::DeleteFile(GetProductPath(), true));
61 EXPECT_TRUE(Launchd::GetInstance()->DeletePlist(
62 Launchd::User, Launchd::Agent,
63 updater::CopyGoogleUpdateCheckLaunchDName()));
64 EXPECT_TRUE(Launchd::GetInstance()->DeletePlist(
65 Launchd::User, Launchd::Agent,
66 updater::CopyGoogleUpdateCheckLaunchDName()));
Joshua Pawlickibae1c6d2352020-03-19 19:21:4467}
68
69void ExpectClean() {
70 // Files must not exist on the file system.
Joshua Pawlicki8f755d382020-03-20 19:51:1071 EXPECT_FALSE(base::PathExists(GetProductPath()));
72 EXPECT_FALSE(Launchd::GetInstance()->PlistExists(
73 Launchd::User, Launchd::Agent,
74 updater::CopyGoogleUpdateCheckLaunchDName()));
75 EXPECT_FALSE(Launchd::GetInstance()->PlistExists(
76 Launchd::User, Launchd::Agent,
77 updater::CopyGoogleUpdateCheckLaunchDName()));
Joshua Pawlickibae1c6d2352020-03-19 19:21:4478}
79
80void ExpectInstalled() {
81 // Files must exist on the file system.
Joshua Pawlicki8f755d382020-03-20 19:51:1082 EXPECT_TRUE(base::PathExists(GetProductPath()));
83 EXPECT_TRUE(Launchd::GetInstance()->PlistExists(
84 Launchd::User, Launchd::Agent,
85 updater::CopyGoogleUpdateCheckLaunchDName()));
86 EXPECT_TRUE(Launchd::GetInstance()->PlistExists(
87 Launchd::User, Launchd::Agent,
88 updater::CopyGoogleUpdateCheckLaunchDName()));
Joshua Pawlickibae1c6d2352020-03-19 19:21:4489}
90
91void Install() {
92 base::FilePath path = GetInstallerPath();
93 ASSERT_FALSE(path.empty());
94 int exit_code = -1;
95 ASSERT_TRUE(Run(base::CommandLine(path), &exit_code));
96 EXPECT_EQ(0, exit_code);
97}
98
99void Uninstall() {
100 base::FilePath path = GetExecutablePath();
101 ASSERT_FALSE(path.empty());
102 base::CommandLine command_line(path);
103 command_line.AppendSwitch("uninstall");
104 int exit_code = -1;
105 ASSERT_TRUE(Run(command_line, &exit_code));
106 EXPECT_EQ(0, exit_code);
107}
108
109} // namespace test
110
111} // namespace updater