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 91d8ee3944419665219251098ad573c896be47aa..1ced49e2e66760e5289c60de2d1e216d7a7880a1 100644 |
--- a/chrome/installer/util/installer_state_unittest.cc |
+++ b/chrome/installer/util/installer_state_unittest.cc |
@@ -11,6 +11,7 @@ |
#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" |
@@ -69,6 +70,9 @@ |
const 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. |
@@ -91,6 +95,249 @@ |
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); |
+ 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); |
+ Version latest_version("1.0.4.0"); |
+ 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"; |
+ |
+ Version new_version(kNewVersion); |
+ 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) { |
@@ -260,6 +507,112 @@ |
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"; |
+ |
+ Version new_version(kNewVersion); |
+ Version old_version(kOldVersion); |
+ Version old_chrome_exe_version(kOldChromeExeVersion); |
+ Version chrome_exe_version(kChromeExeVersion); |
+ 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 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()); |
+ 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. |