Index: chrome/installer/util/installer_state_unittest.cc |
diff --git a/chrome/installer/util/installer_state_unittest.cc b/chrome/installer/util/installer_state_unittest.cc |
index 88db263d9cbcec3b92d0aa0500079e842188e0a7..2aa11025b4d21cd350d2241bd8a11f73e5333f4c 100644 |
--- a/chrome/installer/util/installer_state_unittest.cc |
+++ b/chrome/installer/util/installer_state_unittest.cc |
@@ -11,7 +11,6 @@ |
#include "base/base_paths.h" |
#include "base/command_line.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" |
@@ -25,7 +24,6 @@ |
#include "base/win/registry.h" |
#include "base/win/scoped_handle.h" |
#include "chrome/common/chrome_constants.h" |
-#include "chrome/installer/test/alternate_version_generator.h" |
#include "chrome/installer/util/fake_installation_state.h" |
#include "chrome/installer/util/fake_product_state.h" |
#include "chrome/installer/util/google_update_constants.h" |
@@ -70,276 +68,8 @@ class MockInstallerState : public InstallerState { |
const base::Version& critical_update_version() const { |
return critical_update_version_; |
} |
- void GetExistingExeVersions(std::set<std::string>* existing_version_strings) { |
- return InstallerState::GetExistingExeVersions(existing_version_strings); |
- } |
}; |
-// Simple function to dump some text into a new file. |
-void CreateTextFile(const std::wstring& filename, |
- const std::wstring& contents) { |
- std::ofstream file; |
- file.open(filename.c_str()); |
- ASSERT_TRUE(file.is_open()); |
- file << contents; |
- file.close(); |
-} |
- |
-void BuildSingleChromeState(const base::FilePath& target_dir, |
- MockInstallerState* installer_state) { |
- base::CommandLine cmd_line = base::CommandLine::FromString(L"setup.exe"); |
- MasterPreferences prefs(cmd_line); |
- InstallationState machine_state; |
- machine_state.Initialize(); |
- installer_state->Initialize(cmd_line, prefs, machine_state); |
- installer_state->set_target_path(target_dir); |
- EXPECT_TRUE(installer_state->FindProduct(BrowserDistribution::CHROME_BROWSER) |
- != NULL); |
-} |
- |
-wchar_t text_content_1[] = L"delete me"; |
-wchar_t text_content_2[] = L"delete me as well"; |
- |
-// Delete version directories. Everything lower than the given version |
-// should be deleted. |
-TEST_F(InstallerStateTest, Delete) { |
- // TODO(grt): move common stuff into the test fixture. |
- // Create a Chrome dir |
- base::FilePath chrome_dir(test_dir_.path()); |
- chrome_dir = chrome_dir.AppendASCII("chrome"); |
- base::CreateDirectory(chrome_dir); |
- ASSERT_TRUE(base::PathExists(chrome_dir)); |
- |
- base::FilePath chrome_dir_1(chrome_dir); |
- chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0"); |
- base::CreateDirectory(chrome_dir_1); |
- ASSERT_TRUE(base::PathExists(chrome_dir_1)); |
- |
- base::FilePath chrome_dir_2(chrome_dir); |
- chrome_dir_2 = chrome_dir_2.AppendASCII("1.0.2.0"); |
- base::CreateDirectory(chrome_dir_2); |
- ASSERT_TRUE(base::PathExists(chrome_dir_2)); |
- |
- base::FilePath chrome_dir_3(chrome_dir); |
- chrome_dir_3 = chrome_dir_3.AppendASCII("1.0.3.0"); |
- base::CreateDirectory(chrome_dir_3); |
- ASSERT_TRUE(base::PathExists(chrome_dir_3)); |
- |
- base::FilePath chrome_dir_4(chrome_dir); |
- chrome_dir_4 = chrome_dir_4.AppendASCII("1.0.4.0"); |
- base::CreateDirectory(chrome_dir_4); |
- ASSERT_TRUE(base::PathExists(chrome_dir_4)); |
- |
- base::FilePath chrome_dll_1(chrome_dir_1); |
- chrome_dll_1 = chrome_dll_1.AppendASCII("chrome.dll"); |
- CreateTextFile(chrome_dll_1.value(), text_content_1); |
- ASSERT_TRUE(base::PathExists(chrome_dll_1)); |
- |
- base::FilePath chrome_dll_2(chrome_dir_2); |
- chrome_dll_2 = chrome_dll_2.AppendASCII("chrome.dll"); |
- CreateTextFile(chrome_dll_2.value(), text_content_1); |
- ASSERT_TRUE(base::PathExists(chrome_dll_2)); |
- |
- base::FilePath chrome_dll_3(chrome_dir_3); |
- chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll"); |
- CreateTextFile(chrome_dll_3.value(), text_content_1); |
- ASSERT_TRUE(base::PathExists(chrome_dll_3)); |
- |
- base::FilePath chrome_dll_4(chrome_dir_4); |
- chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll"); |
- CreateTextFile(chrome_dll_4.value(), text_content_1); |
- ASSERT_TRUE(base::PathExists(chrome_dll_4)); |
- |
- MockInstallerState installer_state; |
- BuildSingleChromeState(chrome_dir, &installer_state); |
- base::Version latest_version("1.0.4.0"); |
- { |
- base::ScopedTempDir temp_dir; |
- ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
- installer_state.RemoveOldVersionDirectories(latest_version, NULL, |
- temp_dir.path()); |
- } |
- |
- // old versions should be gone |
- EXPECT_FALSE(base::PathExists(chrome_dir_1)); |
- EXPECT_FALSE(base::PathExists(chrome_dir_2)); |
- EXPECT_FALSE(base::PathExists(chrome_dir_3)); |
- // the latest version should stay |
- EXPECT_TRUE(base::PathExists(chrome_dll_4)); |
-} |
- |
-// Delete older version directories, keeping the one in used intact. |
-TEST_F(InstallerStateTest, DeleteInUsed) { |
- // Create a Chrome dir |
- base::FilePath chrome_dir(test_dir_.path()); |
- chrome_dir = chrome_dir.AppendASCII("chrome"); |
- base::CreateDirectory(chrome_dir); |
- ASSERT_TRUE(base::PathExists(chrome_dir)); |
- |
- base::FilePath chrome_dir_1(chrome_dir); |
- chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0"); |
- base::CreateDirectory(chrome_dir_1); |
- ASSERT_TRUE(base::PathExists(chrome_dir_1)); |
- |
- base::FilePath chrome_dir_2(chrome_dir); |
- chrome_dir_2 = chrome_dir_2.AppendASCII("1.0.2.0"); |
- base::CreateDirectory(chrome_dir_2); |
- ASSERT_TRUE(base::PathExists(chrome_dir_2)); |
- |
- base::FilePath chrome_dir_3(chrome_dir); |
- chrome_dir_3 = chrome_dir_3.AppendASCII("1.0.3.0"); |
- base::CreateDirectory(chrome_dir_3); |
- ASSERT_TRUE(base::PathExists(chrome_dir_3)); |
- |
- base::FilePath chrome_dir_4(chrome_dir); |
- chrome_dir_4 = chrome_dir_4.AppendASCII("1.0.4.0"); |
- base::CreateDirectory(chrome_dir_4); |
- ASSERT_TRUE(base::PathExists(chrome_dir_4)); |
- |
- base::FilePath chrome_dll_1(chrome_dir_1); |
- chrome_dll_1 = chrome_dll_1.AppendASCII("chrome.dll"); |
- CreateTextFile(chrome_dll_1.value(), text_content_1); |
- ASSERT_TRUE(base::PathExists(chrome_dll_1)); |
- |
- base::FilePath chrome_dll_2(chrome_dir_2); |
- chrome_dll_2 = chrome_dll_2.AppendASCII("chrome.dll"); |
- CreateTextFile(chrome_dll_2.value(), text_content_1); |
- ASSERT_TRUE(base::PathExists(chrome_dll_2)); |
- |
- // Open the file to make it in use. |
- std::ofstream file; |
- file.open(chrome_dll_2.value().c_str()); |
- |
- base::FilePath chrome_othera_2(chrome_dir_2); |
- chrome_othera_2 = chrome_othera_2.AppendASCII("othera.dll"); |
- CreateTextFile(chrome_othera_2.value(), text_content_2); |
- ASSERT_TRUE(base::PathExists(chrome_othera_2)); |
- |
- base::FilePath chrome_otherb_2(chrome_dir_2); |
- chrome_otherb_2 = chrome_otherb_2.AppendASCII("otherb.dll"); |
- CreateTextFile(chrome_otherb_2.value(), text_content_2); |
- ASSERT_TRUE(base::PathExists(chrome_otherb_2)); |
- |
- base::FilePath chrome_dll_3(chrome_dir_3); |
- chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll"); |
- CreateTextFile(chrome_dll_3.value(), text_content_1); |
- ASSERT_TRUE(base::PathExists(chrome_dll_3)); |
- |
- base::FilePath chrome_dll_4(chrome_dir_4); |
- chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll"); |
- CreateTextFile(chrome_dll_4.value(), text_content_1); |
- ASSERT_TRUE(base::PathExists(chrome_dll_4)); |
- |
- MockInstallerState installer_state; |
- BuildSingleChromeState(chrome_dir, &installer_state); |
- base::Version latest_version("1.0.4.0"); |
- base::Version existing_version("1.0.1.0"); |
- { |
- base::ScopedTempDir temp_dir; |
- ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
- installer_state.RemoveOldVersionDirectories(latest_version, |
- &existing_version, |
- temp_dir.path()); |
- } |
- |
- // the version defined as the existing version should stay |
- EXPECT_TRUE(base::PathExists(chrome_dir_1)); |
- // old versions not in used should be gone |
- EXPECT_FALSE(base::PathExists(chrome_dir_3)); |
- // every thing under in used version should stay |
- EXPECT_TRUE(base::PathExists(chrome_dir_2)); |
- EXPECT_TRUE(base::PathExists(chrome_dll_2)); |
- EXPECT_TRUE(base::PathExists(chrome_othera_2)); |
- EXPECT_TRUE(base::PathExists(chrome_otherb_2)); |
- // the latest version should stay |
- EXPECT_TRUE(base::PathExists(chrome_dll_4)); |
-} |
- |
-// Tests a few basic things of the Package class. Makes sure that the path |
-// operations are correct |
-TEST_F(InstallerStateTest, Basic) { |
- const bool multi_install = false; |
- const bool system_level = true; |
- base::CommandLine cmd_line = base::CommandLine::FromString( |
- std::wstring(L"setup.exe") + |
- (multi_install ? L" --multi-install --chrome" : L"") + |
- (system_level ? L" --system-level" : L"")); |
- MasterPreferences prefs(cmd_line); |
- InstallationState machine_state; |
- machine_state.Initialize(); |
- MockInstallerState installer_state; |
- installer_state.Initialize(cmd_line, prefs, machine_state); |
- installer_state.set_target_path(test_dir_.path()); |
- EXPECT_EQ(test_dir_.path().value(), installer_state.target_path().value()); |
- EXPECT_EQ(1U, installer_state.products().size()); |
- |
- const char kOldVersion[] = "1.2.3.4"; |
- const char kNewVersion[] = "2.3.4.5"; |
- |
- base::Version new_version(kNewVersion); |
- base::Version old_version(kOldVersion); |
- ASSERT_TRUE(new_version.IsValid()); |
- ASSERT_TRUE(old_version.IsValid()); |
- |
- base::FilePath installer_dir( |
- installer_state.GetInstallerDirectory(new_version)); |
- EXPECT_FALSE(installer_dir.empty()); |
- |
- base::FilePath new_version_dir(installer_state.target_path().Append( |
- base::UTF8ToWide(new_version.GetString()))); |
- base::FilePath old_version_dir(installer_state.target_path().Append( |
- base::UTF8ToWide(old_version.GetString()))); |
- |
- EXPECT_FALSE(base::PathExists(new_version_dir)); |
- EXPECT_FALSE(base::PathExists(old_version_dir)); |
- |
- EXPECT_FALSE(base::PathExists(installer_dir)); |
- base::CreateDirectory(installer_dir); |
- EXPECT_TRUE(base::PathExists(new_version_dir)); |
- |
- base::CreateDirectory(old_version_dir); |
- EXPECT_TRUE(base::PathExists(old_version_dir)); |
- |
- // Create a fake chrome.dll key file in the old version directory. This |
- // should prevent the old version directory from getting deleted. |
- base::FilePath old_chrome_dll(old_version_dir.Append(installer::kChromeDll)); |
- EXPECT_FALSE(base::PathExists(old_chrome_dll)); |
- |
- // Hold on to the file exclusively to prevent the directory from |
- // being deleted. |
- base::win::ScopedHandle file( |
- ::CreateFile(old_chrome_dll.value().c_str(), GENERIC_READ, |
- 0, NULL, OPEN_ALWAYS, 0, NULL)); |
- EXPECT_TRUE(file.IsValid()); |
- EXPECT_TRUE(base::PathExists(old_chrome_dll)); |
- |
- base::ScopedTempDir temp_dir; |
- ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
- |
- // Don't explicitly tell the directory cleanup logic not to delete the |
- // old version, rely on the key files to keep it around. |
- installer_state.RemoveOldVersionDirectories(new_version, |
- NULL, |
- temp_dir.path()); |
- |
- // The old directory should still exist. |
- EXPECT_TRUE(base::PathExists(old_version_dir)); |
- EXPECT_TRUE(base::PathExists(new_version_dir)); |
- |
- // Now close the file handle to make it possible to delete our key file. |
- file.Close(); |
- |
- installer_state.RemoveOldVersionDirectories(new_version, |
- NULL, |
- temp_dir.path()); |
- // The new directory should still exist. |
- EXPECT_TRUE(base::PathExists(new_version_dir)); |
- |
- // Now, the old directory and key file should be gone. |
- EXPECT_FALSE(base::PathExists(old_chrome_dll)); |
- EXPECT_FALSE(base::PathExists(old_version_dir)); |
-} |
- |
TEST_F(InstallerStateTest, WithProduct) { |
const bool multi_install = false; |
const bool system_level = true; |
@@ -494,11 +224,9 @@ TEST_F(InstallerStateTest, IsFileInUse) { |
{ |
// Open a handle to the file with the same access mode and sharing options |
// as the loader. |
- base::win::ScopedHandle temp_handle( |
- CreateFile(temp_file.value().c_str(), |
- SYNCHRONIZE | FILE_EXECUTE, |
- FILE_SHARE_DELETE | FILE_SHARE_READ, |
- NULL, OPEN_EXISTING, 0, 0)); |
+ base::win::ScopedHandle temp_handle(CreateFile( |
+ temp_file.value().c_str(), SYNCHRONIZE | FILE_EXECUTE, |
+ FILE_SHARE_DELETE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0)); |
ASSERT_TRUE(temp_handle.IsValid()); |
// The file should now be in use. |
@@ -509,112 +237,6 @@ TEST_F(InstallerStateTest, IsFileInUse) { |
EXPECT_FALSE(MockInstallerState::IsFileInUse(temp_file)); |
} |
-TEST_F(InstallerStateTest, RemoveOldVersionDirs) { |
- MockInstallerState installer_state; |
- installer_state.set_target_path(test_dir_.path()); |
- EXPECT_EQ(test_dir_.path().value(), installer_state.target_path().value()); |
- |
- const char kOldVersion[] = "2.0.0.0"; |
- const char kNewVersion[] = "3.0.0.0"; |
- const char kOldChromeExeVersion[] = "2.1.0.0"; |
- const char kChromeExeVersion[] = "2.1.1.1"; |
- const char kNewChromeExeVersion[] = "3.0.0.0"; |
- |
- base::Version new_version(kNewVersion); |
- base::Version old_version(kOldVersion); |
- base::Version old_chrome_exe_version(kOldChromeExeVersion); |
- base::Version chrome_exe_version(kChromeExeVersion); |
- base::Version new_chrome_exe_version(kNewChromeExeVersion); |
- |
- ASSERT_TRUE(new_version.IsValid()); |
- ASSERT_TRUE(old_version.IsValid()); |
- ASSERT_TRUE(old_chrome_exe_version.IsValid()); |
- ASSERT_TRUE(chrome_exe_version.IsValid()); |
- ASSERT_TRUE(new_chrome_exe_version.IsValid()); |
- |
- // Set up a bunch of version dir paths. |
- base::FilePath version_dirs[] = { |
- installer_state.target_path().Append(L"1.2.3.4"), |
- installer_state.target_path().Append(L"1.2.3.5"), |
- installer_state.target_path().Append(L"1.2.3.6"), |
- installer_state.target_path().AppendASCII(kOldVersion), |
- installer_state.target_path().AppendASCII(kOldChromeExeVersion), |
- installer_state.target_path().Append(L"2.1.1.0"), |
- installer_state.target_path().AppendASCII(kChromeExeVersion), |
- installer_state.target_path().AppendASCII(kNewVersion), |
- installer_state.target_path().Append(L"3.9.1.1"), |
- }; |
- |
- // Create the version directories. |
- for (size_t i = 0; i < arraysize(version_dirs); i++) { |
- base::CreateDirectory(version_dirs[i]); |
- EXPECT_TRUE(base::PathExists(version_dirs[i])); |
- } |
- |
- // Create exes with the appropriate version resource. |
- // Use the current test exe as a baseline. |
- base::FilePath exe_path; |
- ASSERT_TRUE(PathService::Get(base::FILE_EXE, &exe_path)); |
- |
- struct target_info { |
- base::FilePath target_file; |
- const base::Version& target_version; |
- } targets[] = { |
- { installer_state.target_path().Append(installer::kChromeOldExe), |
- old_chrome_exe_version }, |
- { installer_state.target_path().Append(installer::kChromeExe), |
- chrome_exe_version }, |
- { installer_state.target_path().Append(installer::kChromeNewExe), |
- new_chrome_exe_version }, |
- }; |
- for (size_t i = 0; i < arraysize(targets); ++i) { |
- ASSERT_TRUE(upgrade_test::GenerateSpecificPEFileVersion( |
- exe_path, targets[i].target_file, targets[i].target_version)); |
- } |
- |
- // Call GetExistingExeVersions, validate that picks up the |
- // exe resources. |
- std::set<std::string> expected_exe_versions; |
- expected_exe_versions.insert(kOldChromeExeVersion); |
- expected_exe_versions.insert(kChromeExeVersion); |
- expected_exe_versions.insert(kNewChromeExeVersion); |
- |
- std::set<std::string> actual_exe_versions; |
- installer_state.GetExistingExeVersions(&actual_exe_versions); |
- EXPECT_EQ(expected_exe_versions, actual_exe_versions); |
- |
- // Call RemoveOldVersionDirectories |
- installer_state.RemoveOldVersionDirectories(new_version, |
- &old_version, |
- installer_state.target_path()); |
- |
- // What we expect to have left. |
- std::set<std::string> expected_remaining_dirs; |
- expected_remaining_dirs.insert(kOldVersion); |
- expected_remaining_dirs.insert(kNewVersion); |
- expected_remaining_dirs.insert(kOldChromeExeVersion); |
- expected_remaining_dirs.insert(kChromeExeVersion); |
- expected_remaining_dirs.insert(kNewChromeExeVersion); |
- |
- // Enumerate dirs in target_path(), ensure only desired remain. |
- base::FileEnumerator version_enum(installer_state.target_path(), false, |
- base::FileEnumerator::DIRECTORIES); |
- for (base::FilePath next_version = version_enum.Next(); !next_version.empty(); |
- next_version = version_enum.Next()) { |
- base::FilePath dir_name(next_version.BaseName()); |
- base::Version version(base::UTF16ToASCII(dir_name.value())); |
- if (version.IsValid()) { |
- EXPECT_TRUE(expected_remaining_dirs.erase(version.GetString())) |
- << "Unexpected version dir found: " << version.GetString(); |
- } |
- } |
- |
- std::set<std::string>::const_iterator iter( |
- expected_remaining_dirs.begin()); |
- for (; iter != expected_remaining_dirs.end(); ++iter) |
- ADD_FAILURE() << "Expected to find version dir for " << *iter; |
-} |
- |
TEST_F(InstallerStateTest, InitializeTwice) { |
// Override these paths so that they can be found after the registry override |
// manager is in place. |