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/path_service.h" |
| 9 | #include "base/process/launch.h" |
| 10 | #include "base/process/process.h" |
Joshua Pawlicki | 2a8aa25 | 2020-04-17 22:22:05 | [diff] [blame] | 11 | #include "base/synchronization/waitable_event.h" |
| 12 | #include "base/task/task_traits.h" |
| 13 | #include "base/task/thread_pool.h" |
Joshua Pawlicki | 4abd132 | 2020-08-19 22:05:57 | [diff] [blame^] | 14 | #include "chrome/updater/constants.h" |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 15 | #include "chrome/updater/updater_version.h" |
| 16 | #include "chrome/updater/util.h" |
| 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | namespace updater { |
| 20 | |
| 21 | namespace test { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | base::FilePath GetInstallerPath() { |
| 26 | base::FilePath test_executable; |
| 27 | if (!base::PathService::Get(base::FILE_EXE, &test_executable)) |
| 28 | return base::FilePath(); |
| 29 | return test_executable.DirName().AppendASCII("UpdaterSetup.exe"); |
| 30 | } |
| 31 | |
| 32 | bool Run(base::CommandLine command_line, int* exit_code) { |
| 33 | auto process = base::LaunchProcess(command_line, {}); |
| 34 | if (!process.IsValid()) |
| 35 | return false; |
| 36 | if (!process.WaitForExitWithTimeout(base::TimeDelta::FromSeconds(60), |
| 37 | exit_code)) |
| 38 | return false; |
Joshua Pawlicki | 2a8aa25 | 2020-04-17 22:22:05 | [diff] [blame] | 39 | base::WaitableEvent sleep(base::WaitableEvent::ResetPolicy::MANUAL, |
| 40 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 41 | // The process will exit before it is done uninstalling: sleep for five |
| 42 | // seconds to allow uninstall to complete. |
| 43 | base::ThreadPool::PostDelayedTask( |
| 44 | FROM_HERE, {base::MayBlock()}, |
| 45 | base::BindOnce(&base::WaitableEvent::Signal, base::Unretained(&sleep)), |
| 46 | base::TimeDelta::FromSeconds(5)); |
| 47 | sleep.Wait(); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 48 | return true; |
| 49 | } |
| 50 | |
| 51 | base::FilePath GetProductPath() { |
| 52 | base::FilePath app_data_dir; |
| 53 | if (!base::PathService::Get(base::DIR_LOCAL_APP_DATA, &app_data_dir)) |
| 54 | return base::FilePath(); |
| 55 | return app_data_dir.AppendASCII(COMPANY_SHORTNAME_STRING) |
S. Ganesh | d3edb1e | 2020-06-17 21:12:02 | [diff] [blame] | 56 | .AppendASCII(PRODUCT_FULLNAME_STRING) |
| 57 | .AppendASCII(UPDATER_VERSION_STRING); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | base::FilePath GetExecutablePath() { |
| 61 | return GetProductPath().AppendASCII("updater.exe"); |
| 62 | } |
| 63 | |
Joshua Pawlicki | 9d1340fc | 2020-08-14 20:59:35 | [diff] [blame] | 64 | base::FilePath GetDataDirPath() { |
| 65 | base::FilePath app_data_dir; |
| 66 | if (!base::PathService::Get(base::DIR_LOCAL_APP_DATA, &app_data_dir)) |
| 67 | return base::FilePath(); |
| 68 | return app_data_dir.AppendASCII(COMPANY_SHORTNAME_STRING) |
| 69 | .AppendASCII(PRODUCT_FULLNAME_STRING); |
| 70 | } |
| 71 | |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 72 | } // namespace |
| 73 | |
| 74 | void Clean() { |
| 75 | // TODO(crbug.com/1062288): Delete the Client / ClientState registry keys. |
| 76 | // TODO(crbug.com/1062288): Delete the COM server items. |
| 77 | // TODO(crbug.com/1062288): Delete the COM service items. |
| 78 | // TODO(crbug.com/1062288): Delete the COM interfaces. |
Sorin Jianu | dc61b6a | 2020-05-20 00:40:48 | [diff] [blame] | 79 | // TODO(crbug.com/1062288): Delete the Wake task. |
Lei Zhang | 329c5bc | 2020-07-07 21:09:11 | [diff] [blame] | 80 | EXPECT_TRUE(base::DeletePathRecursively(GetProductPath())); |
Joshua Pawlicki | 9d1340fc | 2020-08-14 20:59:35 | [diff] [blame] | 81 | EXPECT_TRUE(base::DeletePathRecursively(GetDataDirPath())); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void ExpectClean() { |
| 85 | // TODO(crbug.com/1062288): Assert there are no Client / ClientState registry |
| 86 | // keys. |
| 87 | // TODO(crbug.com/1062288): Assert there are no COM server items. |
| 88 | // TODO(crbug.com/1062288): Assert there are no COM service items. |
| 89 | // TODO(crbug.com/1062288): Assert there are no COM interfaces. |
Sorin Jianu | dc61b6a | 2020-05-20 00:40:48 | [diff] [blame] | 90 | // TODO(crbug.com/1062288): Assert there are no Wake tasks. |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 91 | |
| 92 | // Files must not exist on the file system. |
Joshua Pawlicki | 2a8aa25 | 2020-04-17 22:22:05 | [diff] [blame] | 93 | EXPECT_FALSE(base::PathExists(GetProductPath())); |
Joshua Pawlicki | 9d1340fc | 2020-08-14 20:59:35 | [diff] [blame] | 94 | EXPECT_FALSE(base::PathExists(GetDataDirPath())); |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ExpectInstalled() { |
| 98 | // TODO(crbug.com/1062288): Assert there are Client / ClientState registry |
| 99 | // keys. |
| 100 | // TODO(crbug.com/1062288): Assert there are COM server items. |
| 101 | // TODO(crbug.com/1062288): Assert there are COM service items. (Maybe.) |
| 102 | // TODO(crbug.com/1062288): Assert there are COM interfaces. |
Sorin Jianu | dc61b6a | 2020-05-20 00:40:48 | [diff] [blame] | 103 | // TODO(crbug.com/1062288): Assert there are Wake tasks. |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 104 | |
| 105 | // Files must exist on the file system. |
| 106 | EXPECT_TRUE(base::PathExists(GetProductPath())); |
| 107 | } |
| 108 | |
Joshua Pawlicki | 4abd132 | 2020-08-19 22:05:57 | [diff] [blame^] | 109 | void ExpectActive() { |
| 110 | // TODO(crbug.com/1062288): Assert that COM interfaces point to this version. |
| 111 | |
| 112 | // Files must exist on the file system. |
| 113 | EXPECT_TRUE(base::PathExists(GetProductPath())); |
| 114 | } |
| 115 | |
| 116 | void RunWake(int expected_exit_code) { |
| 117 | const base::FilePath path = GetExecutablePath(); |
| 118 | ASSERT_FALSE(path.empty()); |
| 119 | base::CommandLine command_line(path); |
| 120 | command_line.AppendSwitch(kWakeSwitch); |
| 121 | int exit_code = -1; |
| 122 | ASSERT_TRUE(Run(command_line, &exit_code)); |
| 123 | EXPECT_EQ(exit_code, expected_exit_code); |
| 124 | } |
| 125 | |
Joshua Pawlicki | bae1c6d235 | 2020-03-19 19:21:44 | [diff] [blame] | 126 | void Install() { |
| 127 | int exit_code = -1; |
| 128 | ASSERT_TRUE(Run(base::CommandLine(GetInstallerPath()), &exit_code)); |
| 129 | EXPECT_EQ(0, exit_code); |
| 130 | } |
| 131 | |
| 132 | void Uninstall() { |
| 133 | base::FilePath path = GetExecutablePath(); |
| 134 | ASSERT_FALSE(path.empty()); |
| 135 | base::CommandLine command_line(path); |
| 136 | command_line.AppendSwitch("uninstall"); |
| 137 | int exit_code = -1; |
| 138 | ASSERT_TRUE(Run(command_line, &exit_code)); |
| 139 | EXPECT_EQ(0, exit_code); |
| 140 | } |
| 141 | |
| 142 | } // namespace test |
| 143 | |
| 144 | } // namespace updater |