blob: 96c3b3362f5f35a83402a5c1046e76aea42e86c6 [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/path_service.h"
9#include "base/process/launch.h"
10#include "base/process/process.h"
Joshua Pawlicki2a8aa252020-04-17 22:22:0511#include "base/synchronization/waitable_event.h"
12#include "base/task/task_traits.h"
13#include "base/task/thread_pool.h"
Joshua Pawlicki4abd1322020-08-19 22:05:5714#include "chrome/updater/constants.h"
Joshua Pawlickibae1c6d2352020-03-19 19:21:4415#include "chrome/updater/updater_version.h"
16#include "chrome/updater/util.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace updater {
20
21namespace test {
22
23namespace {
24
25base::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
32bool 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 Pawlicki2a8aa252020-04-17 22:22:0539 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 Pawlickibae1c6d2352020-03-19 19:21:4448 return true;
49}
50
51base::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. Ganeshd3edb1e2020-06-17 21:12:0256 .AppendASCII(PRODUCT_FULLNAME_STRING)
57 .AppendASCII(UPDATER_VERSION_STRING);
Joshua Pawlickibae1c6d2352020-03-19 19:21:4458}
59
60base::FilePath GetExecutablePath() {
61 return GetProductPath().AppendASCII("updater.exe");
62}
63
Joshua Pawlicki9d1340fc2020-08-14 20:59:3564base::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 Pawlickibae1c6d2352020-03-19 19:21:4472} // namespace
73
74void 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 Jianudc61b6a2020-05-20 00:40:4879 // TODO(crbug.com/1062288): Delete the Wake task.
Lei Zhang329c5bc2020-07-07 21:09:1180 EXPECT_TRUE(base::DeletePathRecursively(GetProductPath()));
Joshua Pawlicki9d1340fc2020-08-14 20:59:3581 EXPECT_TRUE(base::DeletePathRecursively(GetDataDirPath()));
Joshua Pawlickibae1c6d2352020-03-19 19:21:4482}
83
84void 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 Jianudc61b6a2020-05-20 00:40:4890 // TODO(crbug.com/1062288): Assert there are no Wake tasks.
Joshua Pawlickibae1c6d2352020-03-19 19:21:4491
92 // Files must not exist on the file system.
Joshua Pawlicki2a8aa252020-04-17 22:22:0593 EXPECT_FALSE(base::PathExists(GetProductPath()));
Joshua Pawlicki9d1340fc2020-08-14 20:59:3594 EXPECT_FALSE(base::PathExists(GetDataDirPath()));
Joshua Pawlickibae1c6d2352020-03-19 19:21:4495}
96
97void 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 Jianudc61b6a2020-05-20 00:40:48103 // TODO(crbug.com/1062288): Assert there are Wake tasks.
Joshua Pawlickibae1c6d2352020-03-19 19:21:44104
105 // Files must exist on the file system.
106 EXPECT_TRUE(base::PathExists(GetProductPath()));
107}
108
Joshua Pawlicki4abd1322020-08-19 22:05:57109void 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
116void 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 Pawlickibae1c6d2352020-03-19 19:21:44126void Install() {
127 int exit_code = -1;
128 ASSERT_TRUE(Run(base::CommandLine(GetInstallerPath()), &exit_code));
129 EXPECT_EQ(0, exit_code);
130}
131
132void 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