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" |
| 13 | #include "chrome/updater/mac/xpc_service_names.h" |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 14 | #include "chrome/updater/updater_version.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | namespace updater { |
| 18 | |
| 19 | namespace test { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | base::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 | |
| 34 | base::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 Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame^] | 41 | base::FilePath GetProductPath() { |
| 42 | return base::mac::GetUserLibraryPath() |
| 43 | .AppendASCII(COMPANY_SHORTNAME_STRING) |
| 44 | .AppendASCII(PRODUCT_FULLNAME_STRING); |
| 45 | } |
| 46 | |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 47 | bool 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 | |
| 59 | void Clean() { |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame^] | 60 | 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 Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void ExpectClean() { |
| 70 | // Files must not exist on the file system. |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame^] | 71 | 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 Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void ExpectInstalled() { |
| 81 | // Files must exist on the file system. |
Joshua Pawlicki | 8f755d38 | 2020-03-20 19:51:10 | [diff] [blame^] | 82 | 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 Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void 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 | |
| 99 | void 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 |