| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 1 | // 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 Jianu | a4b3d38 | 2023-11-10 20:08:46 | [diff] [blame] | 7 | #include <optional> |
| 8 | |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 11 | #include "base/memory/scoped_refptr.h" |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 12 | #include "base/run_loop.h" |
| 13 | #include "base/test/task_environment.h" |
| 14 | #include "base/version.h" |
| Joshua Pawlicki | 7eb44b0 | 2025-05-06 15:51:18 | [diff] [blame] | 15 | #include "chrome/updater/configurator.h" |
| S. Ganesh | d03515a | 2025-12-12 12:28:19 | [diff] [blame] | 16 | #include "chrome/updater/constants.h" |
| Joshua Pawlicki | 7eb44b0 | 2025-05-06 15:51:18 | [diff] [blame] | 17 | #include "chrome/updater/external_constants.h" |
| 18 | #include "chrome/updater/prefs.h" |
| Sorin Jianu | a961b61 | 2024-05-02 22:07:57 | [diff] [blame] | 19 | #include "chrome/updater/test/test_scope.h" |
| 20 | #include "chrome/updater/test/unit_test_util.h" |
| Joshua Pawlicki | 5e84a1c1 | 2024-04-16 22:40:52 | [diff] [blame] | 21 | #include "chrome/updater/updater_version.h" |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 22 | #include "chrome/updater/util/util.h" |
| S. Ganesh | d03515a | 2025-12-12 12:28:19 | [diff] [blame] | 23 | #include "components/update_client/utils.h" |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 25 | |
| 26 | #if BUILDFLAG(IS_WIN) |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 27 | #include "chrome/updater/util/win_util.h" |
| 28 | #endif |
| 29 | |
| 30 | namespace updater { |
| 31 | |
| Joshua Pawlicki | 5e84a1c1 | 2024-04-16 22:40:52 | [diff] [blame] | 32 | class CleanupTaskTest : public testing::Test { |
| 33 | protected: |
| 34 | void TearDown() override { |
| Sorin Jianu | 6ca8024 | 2024-05-03 21:13:33 | [diff] [blame] | 35 | base::DeletePathRecursively( |
| 36 | *GetInstallDirectory(GetUpdaterScopeForTesting())); |
| Joshua Pawlicki | 5e84a1c1 | 2024-04-16 22:40:52 | [diff] [blame] | 37 | } |
| 38 | }; |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 39 | |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 40 | TEST_F(CleanupTaskTest, RunCleanupObsoleteFiles) { |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 41 | base::test::TaskEnvironment task_environment; |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 42 | #if BUILDFLAG(IS_POSIX) |
| S. Ganesh | b97bcc9 | 2025-09-25 15:03:57 | [diff] [blame] | 43 | if (IsSystemInstall(GetUpdaterScopeForTesting())) { |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 44 | GTEST_SKIP(); |
| 45 | } |
| Sorin Jianu | cea91e3 | 2024-02-23 08:22:13 | [diff] [blame] | 46 | #endif // BUILDFLAG(IS_POSIX) |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 47 | |
| 48 | #if BUILDFLAG(IS_WIN) |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 49 | // Set up a mock `GoogleUpdate.exe`, and the following mock directories: |
| 50 | // `Download`, `Install`, and a versioned `1.2.3.4` directory. |
| Sorin Jianu | a4b3d38 | 2023-11-10 20:08:46 | [diff] [blame] | 51 | const std::optional<base::FilePath> google_update_exe = |
| Sorin Jianu | 6ca8024 | 2024-05-03 21:13:33 | [diff] [blame] | 52 | GetGoogleUpdateExePath(GetUpdaterScopeForTesting()); |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 53 | ASSERT_TRUE(google_update_exe.has_value()); |
| S. Ganesh | 5ba95c1f | 2023-06-27 23:59:53 | [diff] [blame] | 54 | test::SetupMockUpdater(google_update_exe.value()); |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 55 | #endif // BUILDFLAG(IS_WIN) |
| 56 | |
| S. Ganesh | b97bcc9 | 2025-09-25 15:03:57 | [diff] [blame] | 57 | 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. Ganesh | 959e2ea | 2025-10-14 13:39:40 | [diff] [blame] | 61 | |
| 62 | // Set up mock update_client temp directories. |
| 63 | ASSERT_TRUE(base::CreateNewTempDirectory( |
| S. Ganesh | d03515a | 2025-12-12 12:28:19 | [diff] [blame] | 64 | update_client::UTF8ToStringType( |
| 65 | base::StrCat({kProdId, "_chrome_url_fetcher_BazBar"})), |
| 66 | &chrome_url_fetcher_dir)); |
| S. Ganesh | 959e2ea | 2025-10-14 13:39:40 | [diff] [blame] | 67 | ASSERT_TRUE(base::CreateNewTempDirectory( |
| S. Ganesh | d03515a | 2025-12-12 12:28:19 | [diff] [blame] | 68 | update_client::UTF8ToStringType( |
| 69 | base::StrCat({kProdId, "_chrome_Unpacker_BeginUnzippingBazBar"})), |
| S. Ganesh | 959e2ea | 2025-10-14 13:39:40 | [diff] [blame] | 70 | &chrome_unpacker_dir)); |
| 71 | ASSERT_TRUE(base::CreateNewTempDirectory( |
| S. Ganesh | d03515a | 2025-12-12 12:28:19 | [diff] [blame] | 72 | 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. Ganesh | b97bcc9 | 2025-09-25 15:03:57 | [diff] [blame] | 78 | |
| Sorin Jianu | 6ca8024 | 2024-05-03 21:13:33 | [diff] [blame] | 79 | std::optional<base::FilePath> folder_path = GetVersionedInstallDirectory( |
| 80 | GetUpdaterScopeForTesting(), base::Version("100")); |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 81 | ASSERT_TRUE(folder_path); |
| 82 | ASSERT_TRUE(base::CreateDirectory(*folder_path)); |
| Joshua Pawlicki | 5e84a1c1 | 2024-04-16 22:40:52 | [diff] [blame] | 83 | std::optional<base::FilePath> folder_path_current = |
| Sorin Jianu | 6ca8024 | 2024-05-03 21:13:33 | [diff] [blame] | 84 | GetVersionedInstallDirectory(GetUpdaterScopeForTesting(), |
| Joshua Pawlicki | 5e84a1c1 | 2024-04-16 22:40:52 | [diff] [blame] | 85 | base::Version(kUpdaterVersion)); |
| 86 | ASSERT_TRUE(folder_path_current); |
| 87 | ASSERT_TRUE(base::CreateDirectory(*folder_path_current)); |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 88 | |
| Joshua Pawlicki | 7eb44b0 | 2025-05-06 15:51:18 | [diff] [blame] | 89 | const UpdaterScope scope = GetUpdaterScopeForTesting(); |
| 90 | auto cleanup_task = base::MakeRefCounted<CleanupTask>( |
| 91 | scope, base::MakeRefCounted<Configurator>( |
| 92 | CreateGlobalPrefs(scope), CreateExternalConstants(), scope)); |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 93 | base::RunLoop run_loop; |
| 94 | cleanup_task->Run(run_loop.QuitClosure()); |
| 95 | run_loop.Run(); |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 96 | |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 97 | ASSERT_FALSE(base::PathExists(*folder_path)); |
| Joshua Pawlicki | 5e84a1c1 | 2024-04-16 22:40:52 | [diff] [blame] | 98 | ASSERT_TRUE(base::PathExists(*folder_path_current)); |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 99 | |
| S. Ganesh | 959e2ea | 2025-10-14 13:39:40 | [diff] [blame] | 100 | // 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. Ganesh | b97bcc9 | 2025-09-25 15:03:57 | [diff] [blame] | 106 | |
| S. Ganesh | 959e2ea | 2025-10-14 13:39:40 | [diff] [blame] | 107 | #if BUILDFLAG(IS_WIN) |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 108 | // Expect only a single file `GoogleUpdate.exe` and nothing else under |
| 109 | // `\Google\Update`. |
| S. Ganesh | 5ba95c1f | 2023-06-27 23:59:53 | [diff] [blame] | 110 | test::ExpectOnlyMockUpdater(google_update_exe.value()); |
| Joshua Pawlicki | 1f7b3a8 | 2023-07-07 17:00:48 | [diff] [blame] | 111 | #endif // BUILDFLAG(IS_WIN) |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 112 | } |
| S. Ganesh | 0ed41876 | 2023-06-27 21:39:35 | [diff] [blame] | 113 | |
| 114 | } // namespace updater |