Chromium Code Reviews| Index: chrome/installer/util/delete_old_versions_unittest.cc |
| diff --git a/chrome/installer/util/delete_old_versions_unittest.cc b/chrome/installer/util/delete_old_versions_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a4fab4fc48c6badf3743efc99d4ecc29008eaa4b |
| --- /dev/null |
| +++ b/chrome/installer/util/delete_old_versions_unittest.cc |
| @@ -0,0 +1,285 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/installer/util/delete_old_versions.h" |
| + |
| +#include <set> |
| + |
| +#include "base/files/file.h" |
| +#include "base/files/file_enumerator.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/path_service.h" |
| +#include "base/strings/string16.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/version.h" |
| +#include "chrome/installer/test/alternate_version_generator.h" |
| +#include "chrome/installer/util/util_constants.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using upgrade_test::Direction; |
| + |
| +namespace installer { |
| + |
| +namespace { |
| + |
| +class DeleteOldVersionsTest : public testing::Test { |
| + protected: |
| + DeleteOldVersionsTest() = default; |
| + |
| + void SetUp() override { ASSERT_TRUE(install_dir_.CreateUniqueTempDir()); } |
| + |
| + // Creates an executable with |name| in |install_dir_|. Depending on |
|
grt (UTC plus 2)
2016/08/25 11:24:47
the second sentence implies that the created execu
fdoray
2016/08/26 15:16:15
Done.
|
| + // |direction|, the version of the created executable is higher or lower than |
| + // the version of the current executable. |
| + base::string16 CreateExecutable(const base::string16& name, |
| + Direction direction) { |
| + base::FilePath current_exe_path; |
| + if (!base::PathService::Get(base::FILE_EXE, ¤t_exe_path)) |
| + return std::wstring(); |
|
grt (UTC plus 2)
2016/08/25 11:24:47
base::string16()
fdoray
2016/08/26 15:16:15
Done.
|
| + |
| + return upgrade_test::GenerateAlternatePEFileVersion( |
| + current_exe_path, install_dir().Append(name), direction); |
| + } |
| + |
| + // Creates a version directory named |name| in |install_dir_|. |
|
grt (UTC plus 2)
2016/08/25 11:24:47
"Creates a version directory containing a subset o
fdoray
2016/08/26 15:16:15
Done.
|
| + bool CreateVersionDirectory(const base::string16& name) { |
| + static const char kDummyContent[] = "dummy"; |
|
grt (UTC plus 2)
2016/08/25 11:24:47
static constexpr
fdoray
2016/08/26 15:16:15
Done.
|
| + const base::FilePath version_dir_path(install_dir().Append(name)); |
| + |
| + return base::CreateDirectory(install_dir().Append(name)) && |
| + base::CreateDirectory(version_dir_path.Append(L"Installer")) && |
| + base::WriteFile(version_dir_path.Append(L"chrome.dll"), |
| + kDummyContent, sizeof(kDummyContent)) && |
| + base::WriteFile(version_dir_path.Append(L"nacl64.exe"), |
| + kDummyContent, sizeof(kDummyContent)) && |
| + base::WriteFile(version_dir_path.Append(L"icudtl.dat"), |
| + kDummyContent, sizeof(kDummyContent)) && |
| + base::WriteFile(version_dir_path.Append(L"Installer\\setup.exe"), |
| + kDummyContent, sizeof(kDummyContent)); |
| + } |
| + |
| + // Returns the relative paths of all files and directories in |install_dir_|. |
| + using FilePathSet = std::set<base::FilePath>; |
| + FilePathSet GetInstallDirContent() const { |
| + std::set<base::FilePath> content; |
| + base::FileEnumerator file_enumerator( |
| + install_dir(), true, |
| + base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES); |
| + for (base::FilePath path = file_enumerator.Next(); !path.empty(); |
| + path = file_enumerator.Next()) { |
| + DCHECK(base::StartsWith(path.value(), install_dir().value(), |
| + base::CompareCase::SENSITIVE)); |
| + content.insert(base::FilePath( |
| + path.value().substr(install_dir().value().size() + 1))); |
| + } |
| + return content; |
| + } |
| + |
| + // Adds to |file_path_set| all files and directories that are expected to be |
| + // found in the version directory |version| before any attempt to delete it. |
| + void AddVersionFiles(const base::string16& version, |
| + FilePathSet* file_path_set) { |
| + file_path_set->insert(base::FilePath(version)); |
| + file_path_set->insert(base::FilePath(version).Append(L"chrome.dll")); |
| + file_path_set->insert(base::FilePath(version).Append(L"nacl64.exe")); |
| + file_path_set->insert(base::FilePath(version).Append(L"icudtl.dat")); |
| + file_path_set->insert(base::FilePath(version).Append(L"Installer")); |
| + file_path_set->insert( |
| + base::FilePath(version).Append(L"Installer\\setup.exe")); |
| + } |
| + |
| + base::FilePath install_dir() const { return install_dir_.path(); } |
| + |
| + private: |
| + base::ScopedTempDir install_dir_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DeleteOldVersionsTest); |
| +}; |
| + |
| +} // namespace |
| + |
| +// An old executable without a matching directory should be deleted. |
| +TEST_F(DeleteOldVersionsTest, DeleteOldExecutableWithoutMatchingDirectory) { |
| + ASSERT_FALSE( |
| + CreateExecutable(installer::kChromeOldExe, Direction::PREVIOUS_VERSION) |
| + .empty()); |
| + |
| + DeleteOldVersions(install_dir()); |
| + EXPECT_TRUE(GetInstallDirContent().empty()); |
| +} |
| + |
| +// chrome.exe and new_chrome.exe should never be deleted. |
| +TEST_F(DeleteOldVersionsTest, DeleteNewExecutablesWithoutMatchingDirectory) { |
| + ASSERT_FALSE( |
| + CreateExecutable(installer::kChromeExe, Direction::PREVIOUS_VERSION) |
| + .empty()); |
| + ASSERT_FALSE( |
| + CreateExecutable(installer::kChromeNewExe, Direction::NEXT_VERSION) |
| + .empty()); |
| + |
| + DeleteOldVersions(install_dir()); |
| + FilePathSet expected_install_dir_content; |
| + expected_install_dir_content.insert(base::FilePath(installer::kChromeExe)); |
| + expected_install_dir_content.insert(base::FilePath(installer::kChromeNewExe)); |
| + EXPECT_EQ(expected_install_dir_content, GetInstallDirContent()); |
| +} |
| + |
| +// A directory without a matching executable should be deleted. |
| +TEST_F(DeleteOldVersionsTest, DeleteDirectoryWithoutMatchingExecutable) { |
| + static constexpr base::char16 kVersion[] = L"48.0.0.0"; |
|
grt (UTC plus 2)
2016/08/25 11:24:47
48 was a very good milestone. ;-)
fdoray
2016/08/26 15:16:15
:)
|
| + ASSERT_TRUE(CreateVersionDirectory(kVersion)); |
| + |
| + DeleteOldVersions(install_dir()); |
| + EXPECT_TRUE(GetInstallDirContent().empty()); |
| +} |
| + |
| +// A pair of matching old executable/version directory that is not in use should |
| +// be deleted. |
| +TEST_F(DeleteOldVersionsTest, DeleteOldExecutableWithMatchingDirectory) { |
| + const base::string16 version_a = |
| + CreateExecutable(installer::kChromeOldExe, Direction::PREVIOUS_VERSION); |
| + ASSERT_FALSE(version_a.empty()); |
| + ASSERT_TRUE(CreateVersionDirectory(version_a)); |
| + |
| + DeleteOldVersions(install_dir()); |
| + EXPECT_TRUE(GetInstallDirContent().empty()); |
| +} |
| + |
| +// chrome.exe, new_chrome.exe and their matching version directories should |
| +// never be deleted. |
| +TEST_F(DeleteOldVersionsTest, DeleteNewExecutablesWithMatchingDirectory) { |
| + const base::string16 version_a = |
| + CreateExecutable(installer::kChromeExe, Direction::PREVIOUS_VERSION); |
| + ASSERT_FALSE(version_a.empty()); |
| + ASSERT_TRUE(CreateVersionDirectory(version_a)); |
| + const base::string16 version_b = |
| + CreateExecutable(installer::kChromeNewExe, Direction::NEXT_VERSION); |
| + ASSERT_FALSE(version_b.empty()); |
| + ASSERT_TRUE(CreateVersionDirectory(version_b)); |
| + |
| + DeleteOldVersions(install_dir()); |
| + |
| + FilePathSet expected_install_dir_content; |
| + expected_install_dir_content.insert(base::FilePath(installer::kChromeExe)); |
| + AddVersionFiles(version_a, &expected_install_dir_content); |
| + expected_install_dir_content.insert(base::FilePath(installer::kChromeNewExe)); |
| + AddVersionFiles(version_b, &expected_install_dir_content); |
| + EXPECT_EQ(expected_install_dir_content, GetInstallDirContent()); |
| +} |
| + |
| +// chrome.exe, new_chrome.exe and their matching version directories should |
| +// never be deleted, even when files named old_chrome*.exe have the same |
| +// versions as chrome.exe/new_chrome.exe. The old_chrome*.exe files, however, |
| +// should be deleted. |
| +TEST_F(DeleteOldVersionsTest, |
| + DeleteNewExecutablesWithMatchingDirectoryAndOldExecutables) { |
| + const base::string16 version_a = |
| + CreateExecutable(installer::kChromeExe, Direction::PREVIOUS_VERSION); |
| + ASSERT_FALSE(version_a.empty()); |
| + ASSERT_TRUE(CreateVersionDirectory(version_a)); |
| + const base::string16 version_b = |
| + CreateExecutable(installer::kChromeNewExe, Direction::NEXT_VERSION); |
| + ASSERT_FALSE(version_b.empty()); |
| + ASSERT_TRUE(CreateVersionDirectory(version_b)); |
| + EXPECT_EQ(version_a, |
| + CreateExecutable(L"old_chrome.exe", Direction::PREVIOUS_VERSION)); |
| + EXPECT_EQ(version_b, |
| + CreateExecutable(L"old_chrome2.exe", Direction::NEXT_VERSION)); |
| + |
| + DeleteOldVersions(install_dir()); |
| + |
| + FilePathSet expected_install_dir_content; |
| + expected_install_dir_content.insert(base::FilePath(installer::kChromeExe)); |
| + AddVersionFiles(version_a, &expected_install_dir_content); |
| + expected_install_dir_content.insert(base::FilePath(installer::kChromeNewExe)); |
| + AddVersionFiles(version_b, &expected_install_dir_content); |
| + EXPECT_EQ(expected_install_dir_content, GetInstallDirContent()); |
| +} |
| + |
| +// No file should be deleted for a given version if the executable is in use. |
| +TEST_F(DeleteOldVersionsTest, DeleteVersionWithExecutableInUse) { |
| + const base::string16 version_a = |
| + CreateExecutable(installer::kChromeOldExe, Direction::PREVIOUS_VERSION); |
| + ASSERT_FALSE(version_a.empty()); |
| + ASSERT_TRUE(CreateVersionDirectory(version_a)); |
| + |
| + base::File file_in_use(install_dir().Append(installer::kChromeOldExe), |
| + base::File::FLAG_OPEN | base::File::FLAG_READ); |
| + ASSERT_TRUE(file_in_use.IsValid()); |
| + |
| + DeleteOldVersions(install_dir()); |
| + |
| + FilePathSet expected_install_dir_content; |
| + expected_install_dir_content.insert(base::FilePath(installer::kChromeOldExe)); |
| + AddVersionFiles(version_a, &expected_install_dir_content); |
| + EXPECT_EQ(expected_install_dir_content, GetInstallDirContent()); |
| +} |
| + |
| +// No file should be deleted for a given version if a .dll file in the version |
| +// directory is in use. |
| +TEST_F(DeleteOldVersionsTest, DeleteVersionWithVersionDirectoryDllInUse) { |
| + const base::string16 version_a = |
| + CreateExecutable(installer::kChromeOldExe, Direction::PREVIOUS_VERSION); |
| + ASSERT_FALSE(version_a.empty()); |
| + ASSERT_TRUE(CreateVersionDirectory(version_a)); |
| + |
| + base::File file_in_use(install_dir().Append(version_a).Append(L"chrome.dll"), |
| + base::File::FLAG_OPEN | base::File::FLAG_READ); |
| + ASSERT_TRUE(file_in_use.IsValid()); |
| + |
| + DeleteOldVersions(install_dir()); |
| + |
| + FilePathSet expected_install_dir_content; |
| + expected_install_dir_content.insert(base::FilePath(installer::kChromeOldExe)); |
| + AddVersionFiles(version_a, &expected_install_dir_content); |
| + EXPECT_EQ(expected_install_dir_content, GetInstallDirContent()); |
| +} |
| + |
| +// No file should be deleted for a given version if a .exe file in the version |
| +// directory is in use. |
| +TEST_F(DeleteOldVersionsTest, DeleteVersionWithVersionDirectoryExeInUse) { |
| + const base::string16 version_a = |
| + CreateExecutable(installer::kChromeOldExe, Direction::PREVIOUS_VERSION); |
| + ASSERT_FALSE(version_a.empty()); |
| + ASSERT_TRUE(CreateVersionDirectory(version_a)); |
| + |
| + base::File file_in_use( |
| + install_dir().Append(version_a).Append(L"Installer\\setup.exe"), |
| + base::File::FLAG_OPEN | base::File::FLAG_READ); |
| + ASSERT_TRUE(file_in_use.IsValid()); |
| + |
| + DeleteOldVersions(install_dir()); |
| + |
| + FilePathSet expected_install_dir_content; |
| + expected_install_dir_content.insert(base::FilePath(installer::kChromeOldExe)); |
| + AddVersionFiles(version_a, &expected_install_dir_content); |
| + EXPECT_EQ(expected_install_dir_content, GetInstallDirContent()); |
| +} |
| + |
| +// If an installation directory contains a file named chrome.exe with a matching |
| +// directory v1 and a file named old_chrome.exe with a matching directory v2, |
| +// old_chrome.exe and v2 should be deleted but chrome.exe and v1 shouldn't. |
| +TEST_F(DeleteOldVersionsTest, TypicalAfterRenameState) { |
| + const base::string16 version_a = |
| + CreateExecutable(installer::kChromeOldExe, Direction::PREVIOUS_VERSION); |
| + ASSERT_FALSE(version_a.empty()); |
| + ASSERT_TRUE(CreateVersionDirectory(version_a)); |
| + const base::string16 version_b = |
| + CreateExecutable(installer::kChromeExe, Direction::NEXT_VERSION); |
| + ASSERT_FALSE(version_b.empty()); |
| + ASSERT_TRUE(CreateVersionDirectory(version_b)); |
| + |
| + DeleteOldVersions(install_dir()); |
| + |
| + FilePathSet expected_install_dir_content; |
| + expected_install_dir_content.insert(base::FilePath(installer::kChromeExe)); |
| + AddVersionFiles(version_b, &expected_install_dir_content); |
| + EXPECT_EQ(expected_install_dir_content, GetInstallDirContent()); |
| +} |
| + |
| +} // namespace installer |