blob: 19d045324c098b340bd30fbbb1087d96c1690622 [file] [log] [blame]
S. Ganesh0ed418762023-06-27 21:39:351// Copyright 2023 The Chromium Authors
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 "chrome/updater/cleanup_task.h"
6
Sorin Jianua4b3d382023-11-10 20:08:467#include <optional>
8
S. Ganesh0ed418762023-06-27 21:39:359#include "base/files/file_path.h"
Joshua Pawlicki1f7b3a82023-07-07 17:00:4810#include "base/files/file_util.h"
S. Ganesh0ed418762023-06-27 21:39:3511#include "base/memory/scoped_refptr.h"
Joshua Pawlicki1f7b3a82023-07-07 17:00:4812#include "base/run_loop.h"
13#include "base/test/task_environment.h"
14#include "base/version.h"
Joshua Pawlicki7eb44b02025-05-06 15:51:1815#include "chrome/updater/configurator.h"
S. Ganeshd03515a2025-12-12 12:28:1916#include "chrome/updater/constants.h"
Joshua Pawlicki7eb44b02025-05-06 15:51:1817#include "chrome/updater/external_constants.h"
18#include "chrome/updater/prefs.h"
Sorin Jianua961b612024-05-02 22:07:5719#include "chrome/updater/test/test_scope.h"
20#include "chrome/updater/test/unit_test_util.h"
Joshua Pawlicki5e84a1c12024-04-16 22:40:5221#include "chrome/updater/updater_version.h"
Joshua Pawlicki1f7b3a82023-07-07 17:00:4822#include "chrome/updater/util/util.h"
S. Ganeshd03515a2025-12-12 12:28:1923#include "components/update_client/utils.h"
S. Ganesh0ed418762023-06-27 21:39:3524#include "testing/gtest/include/gtest/gtest.h"
S. Ganesh0ed418762023-06-27 21:39:3525
26#if BUILDFLAG(IS_WIN)
S. Ganesh0ed418762023-06-27 21:39:3527#include "chrome/updater/util/win_util.h"
28#endif
29
30namespace updater {
31
Joshua Pawlicki5e84a1c12024-04-16 22:40:5232class CleanupTaskTest : public testing::Test {
33 protected:
34 void TearDown() override {
Sorin Jianu6ca80242024-05-03 21:13:3335 base::DeletePathRecursively(
36 *GetInstallDirectory(GetUpdaterScopeForTesting()));
Joshua Pawlicki5e84a1c12024-04-16 22:40:5237 }
38};
S. Ganesh0ed418762023-06-27 21:39:3539
S. Ganesh0ed418762023-06-27 21:39:3540TEST_F(CleanupTaskTest, RunCleanupObsoleteFiles) {
Joshua Pawlicki1f7b3a82023-07-07 17:00:4841 base::test::TaskEnvironment task_environment;
Joshua Pawlicki1f7b3a82023-07-07 17:00:4842#if BUILDFLAG(IS_POSIX)
S. Ganeshb97bcc92025-09-25 15:03:5743 if (IsSystemInstall(GetUpdaterScopeForTesting())) {
Joshua Pawlicki1f7b3a82023-07-07 17:00:4844 GTEST_SKIP();
45 }
Sorin Jianucea91e32024-02-23 08:22:1346#endif // BUILDFLAG(IS_POSIX)
Joshua Pawlicki1f7b3a82023-07-07 17:00:4847
48#if BUILDFLAG(IS_WIN)
S. Ganesh0ed418762023-06-27 21:39:3549 // Set up a mock `GoogleUpdate.exe`, and the following mock directories:
50 // `Download`, `Install`, and a versioned `1.2.3.4` directory.
Sorin Jianua4b3d382023-11-10 20:08:4651 const std::optional<base::FilePath> google_update_exe =
Sorin Jianu6ca80242024-05-03 21:13:3352 GetGoogleUpdateExePath(GetUpdaterScopeForTesting());
S. Ganesh0ed418762023-06-27 21:39:3553 ASSERT_TRUE(google_update_exe.has_value());
S. Ganesh5ba95c1f2023-06-27 23:59:5354 test::SetupMockUpdater(google_update_exe.value());
Joshua Pawlicki1f7b3a82023-07-07 17:00:4855#endif // BUILDFLAG(IS_WIN)
56
S. Ganeshb97bcc92025-09-25 15:03:5757 base::FilePath chrome_url_fetcher_dir;
58 base::FilePath chrome_unpacker_dir;
59 base::FilePath chrome_bits_dir;
60 base::FilePath random_temp_dir;
S. Ganesh959e2ea2025-10-14 13:39:4061
62 // Set up mock update_client temp directories.
63 ASSERT_TRUE(base::CreateNewTempDirectory(
S. Ganeshd03515a2025-12-12 12:28:1964 update_client::UTF8ToStringType(
65 base::StrCat({kProdId, "_chrome_url_fetcher_BazBar"})),
66 &chrome_url_fetcher_dir));
S. Ganesh959e2ea2025-10-14 13:39:4067 ASSERT_TRUE(base::CreateNewTempDirectory(
S. Ganeshd03515a2025-12-12 12:28:1968 update_client::UTF8ToStringType(
69 base::StrCat({kProdId, "_chrome_Unpacker_BeginUnzippingBazBar"})),
S. Ganesh959e2ea2025-10-14 13:39:4070 &chrome_unpacker_dir));
71 ASSERT_TRUE(base::CreateNewTempDirectory(
S. Ganeshd03515a2025-12-12 12:28:1972 update_client::UTF8ToStringType(
73 base::StrCat({kProdId, "_chrome_BITS_BazBar"})),
74 &chrome_bits_dir));
75 ASSERT_TRUE(base::CreateNewTempDirectory(
76 update_client::UTF8ToStringType(base::StrCat({kProdId, "_random_temp"})),
77 &random_temp_dir));
S. Ganeshb97bcc92025-09-25 15:03:5778
Sorin Jianu6ca80242024-05-03 21:13:3379 std::optional<base::FilePath> folder_path = GetVersionedInstallDirectory(
80 GetUpdaterScopeForTesting(), base::Version("100"));
Joshua Pawlicki1f7b3a82023-07-07 17:00:4881 ASSERT_TRUE(folder_path);
82 ASSERT_TRUE(base::CreateDirectory(*folder_path));
Joshua Pawlicki5e84a1c12024-04-16 22:40:5283 std::optional<base::FilePath> folder_path_current =
Sorin Jianu6ca80242024-05-03 21:13:3384 GetVersionedInstallDirectory(GetUpdaterScopeForTesting(),
Joshua Pawlicki5e84a1c12024-04-16 22:40:5285 base::Version(kUpdaterVersion));
86 ASSERT_TRUE(folder_path_current);
87 ASSERT_TRUE(base::CreateDirectory(*folder_path_current));
S. Ganesh0ed418762023-06-27 21:39:3588
Joshua Pawlicki7eb44b02025-05-06 15:51:1889 const UpdaterScope scope = GetUpdaterScopeForTesting();
90 auto cleanup_task = base::MakeRefCounted<CleanupTask>(
91 scope, base::MakeRefCounted<Configurator>(
92 CreateGlobalPrefs(scope), CreateExternalConstants(), scope));
Joshua Pawlicki1f7b3a82023-07-07 17:00:4893 base::RunLoop run_loop;
94 cleanup_task->Run(run_loop.QuitClosure());
95 run_loop.Run();
S. Ganesh0ed418762023-06-27 21:39:3596
Joshua Pawlicki1f7b3a82023-07-07 17:00:4897 ASSERT_FALSE(base::PathExists(*folder_path));
Joshua Pawlicki5e84a1c12024-04-16 22:40:5298 ASSERT_TRUE(base::PathExists(*folder_path_current));
Joshua Pawlicki1f7b3a82023-07-07 17:00:4899
S. Ganesh959e2ea2025-10-14 13:39:40100 // Expect all the mock update_client temp directories to be cleaned up.
101 EXPECT_FALSE(base::PathExists(chrome_url_fetcher_dir));
102 EXPECT_FALSE(base::PathExists(chrome_unpacker_dir));
103 EXPECT_FALSE(base::PathExists(chrome_bits_dir));
104 EXPECT_TRUE(base::PathExists(random_temp_dir));
105 EXPECT_TRUE(base::DeletePathRecursively(random_temp_dir));
S. Ganeshb97bcc92025-09-25 15:03:57106
S. Ganesh959e2ea2025-10-14 13:39:40107#if BUILDFLAG(IS_WIN)
S. Ganesh0ed418762023-06-27 21:39:35108 // Expect only a single file `GoogleUpdate.exe` and nothing else under
109 // `\Google\Update`.
S. Ganesh5ba95c1f2023-06-27 23:59:53110 test::ExpectOnlyMockUpdater(google_update_exe.value());
Joshua Pawlicki1f7b3a82023-07-07 17:00:48111#endif // BUILDFLAG(IS_WIN)
S. Ganesh0ed418762023-06-27 21:39:35112}
S. Ganesh0ed418762023-06-27 21:39:35113
114} // namespace updater