OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <windows.h> | 5 #include <windows.h> |
6 | 6 |
7 #include <fstream> | 7 #include <fstream> |
8 | 8 |
9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/command_line.h" |
10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
12 #include "base/process_util.h" | 13 #include "base/process_util.h" |
13 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/utf_string_conversions.h" |
14 #include "base/version.h" | 16 #include "base/version.h" |
| 17 #include "base/win/registry.h" |
| 18 #include "base/win/scoped_handle.h" |
| 19 #include "chrome/installer/util/google_update_constants.h" |
15 #include "chrome/installer/util/helper.h" | 20 #include "chrome/installer/util/helper.h" |
16 #include "chrome/installer/util/package.h" | 21 #include "chrome/installer/util/installation_state.h" |
17 #include "chrome/installer/util/package_properties.h" | 22 #include "chrome/installer/util/installer_state.h" |
| 23 #include "chrome/installer/util/master_preferences.h" |
| 24 #include "chrome/installer/util/product_unittest.h" |
18 #include "chrome/installer/util/work_item.h" | 25 #include "chrome/installer/util/work_item.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
20 | 27 |
21 using installer::ChromePackageProperties; | 28 class InstallerStateTest : public TestWithTempDirAndDeleteTempOverrideKeys { |
22 using installer::Package; | 29 protected: |
| 30 }; |
23 | 31 |
24 namespace { | 32 // An installer state on which we can tweak the target path. |
25 class SetupHelperTest : public testing::Test { | 33 class MockInstallerState : public installer::InstallerState { |
26 protected: | 34 public: |
27 virtual void SetUp() { | 35 MockInstallerState() : InstallerState() { } |
28 // Name a subdirectory of the user temp directory. | 36 void set_target_path(const FilePath& target_path) { |
29 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | 37 target_path_ = target_path; |
30 test_dir_ = test_dir_.AppendASCII("SetupHelperTest"); | 38 } |
| 39 }; |
31 | 40 |
32 // Create a fresh, empty copy of this test directory. | 41 // Simple function to dump some text into a new file. |
33 file_util::Delete(test_dir_, true); | 42 void CreateTextFile(const std::wstring& filename, |
34 file_util::CreateDirectoryW(test_dir_); | 43 const std::wstring& contents) { |
35 ASSERT_TRUE(file_util::PathExists(test_dir_)); | 44 std::ofstream file; |
36 } | 45 file.open(filename.c_str()); |
| 46 ASSERT_TRUE(file.is_open()); |
| 47 file << contents; |
| 48 file.close(); |
| 49 } |
37 | 50 |
38 virtual void TearDown() { | 51 wchar_t text_content_1[] = L"delete me"; |
39 logging::CloseLogFile(); | 52 wchar_t text_content_2[] = L"delete me as well"; |
40 // Clean up test directory | |
41 ASSERT_TRUE(file_util::Delete(test_dir_, true)); | |
42 ASSERT_FALSE(file_util::PathExists(test_dir_)); | |
43 } | |
44 | |
45 // the path to temporary directory used to contain the test operations | |
46 FilePath test_dir_; | |
47 }; | |
48 | |
49 // Simple function to dump some text into a new file. | |
50 void CreateTextFile(const std::wstring& filename, | |
51 const std::wstring& contents) { | |
52 std::ofstream file; | |
53 file.open(filename.c_str()); | |
54 ASSERT_TRUE(file.is_open()); | |
55 file << contents; | |
56 file.close(); | |
57 } | |
58 | |
59 wchar_t text_content_1[] = L"delete me"; | |
60 wchar_t text_content_2[] = L"delete me as well"; | |
61 }; | |
62 | 53 |
63 // Delete version directories. Everything lower than the given version | 54 // Delete version directories. Everything lower than the given version |
64 // should be deleted. | 55 // should be deleted. |
65 TEST_F(SetupHelperTest, Delete) { | 56 TEST_F(InstallerStateTest, Delete) { |
66 // Create a Chrome dir | 57 // Create a Chrome dir |
67 FilePath chrome_dir(test_dir_); | 58 FilePath chrome_dir(test_dir_.path()); |
68 chrome_dir = chrome_dir.AppendASCII("chrome"); | 59 chrome_dir = chrome_dir.AppendASCII("chrome"); |
69 file_util::CreateDirectory(chrome_dir); | 60 file_util::CreateDirectory(chrome_dir); |
70 ASSERT_TRUE(file_util::PathExists(chrome_dir)); | 61 ASSERT_TRUE(file_util::PathExists(chrome_dir)); |
71 | 62 |
72 FilePath chrome_dir_1(chrome_dir); | 63 FilePath chrome_dir_1(chrome_dir); |
73 chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0"); | 64 chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0"); |
74 file_util::CreateDirectory(chrome_dir_1); | 65 file_util::CreateDirectory(chrome_dir_1); |
75 ASSERT_TRUE(file_util::PathExists(chrome_dir_1)); | 66 ASSERT_TRUE(file_util::PathExists(chrome_dir_1)); |
76 | 67 |
77 FilePath chrome_dir_2(chrome_dir); | 68 FilePath chrome_dir_2(chrome_dir); |
(...skipping 24 matching lines...) Expand all Loading... |
102 FilePath chrome_dll_3(chrome_dir_3); | 93 FilePath chrome_dll_3(chrome_dir_3); |
103 chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll"); | 94 chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll"); |
104 CreateTextFile(chrome_dll_3.value(), text_content_1); | 95 CreateTextFile(chrome_dll_3.value(), text_content_1); |
105 ASSERT_TRUE(file_util::PathExists(chrome_dll_3)); | 96 ASSERT_TRUE(file_util::PathExists(chrome_dll_3)); |
106 | 97 |
107 FilePath chrome_dll_4(chrome_dir_4); | 98 FilePath chrome_dll_4(chrome_dir_4); |
108 chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll"); | 99 chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll"); |
109 CreateTextFile(chrome_dll_4.value(), text_content_1); | 100 CreateTextFile(chrome_dll_4.value(), text_content_1); |
110 ASSERT_TRUE(file_util::PathExists(chrome_dll_4)); | 101 ASSERT_TRUE(file_util::PathExists(chrome_dll_4)); |
111 | 102 |
| 103 CommandLine cmd_line = CommandLine::FromString(L"setup.exe"); |
| 104 installer::MasterPreferences prefs(cmd_line); |
| 105 installer::InstallationState machine_state; |
| 106 machine_state.Initialize(); |
| 107 MockInstallerState installer_state; |
| 108 installer_state.Initialize(cmd_line, prefs, machine_state); |
| 109 installer_state.set_target_path(chrome_dir); |
| 110 EXPECT_TRUE(installer_state.FindProduct(BrowserDistribution::CHROME_BROWSER) |
| 111 != NULL); |
112 scoped_ptr<Version> latest_version(Version::GetVersionFromString("1.0.4.0")); | 112 scoped_ptr<Version> latest_version(Version::GetVersionFromString("1.0.4.0")); |
113 ChromePackageProperties properties; | 113 installer_state.RemoveOldVersionDirectories(*latest_version.get()); |
114 scoped_refptr<Package> package(new Package(false, true, chrome_dir, | |
115 &properties)); | |
116 package->RemoveOldVersionDirectories(*latest_version.get()); | |
117 | 114 |
118 // old versions should be gone | 115 // old versions should be gone |
119 EXPECT_FALSE(file_util::PathExists(chrome_dir_1)); | 116 EXPECT_FALSE(file_util::PathExists(chrome_dir_1)); |
120 EXPECT_FALSE(file_util::PathExists(chrome_dir_2)); | 117 EXPECT_FALSE(file_util::PathExists(chrome_dir_2)); |
121 EXPECT_FALSE(file_util::PathExists(chrome_dir_3)); | 118 EXPECT_FALSE(file_util::PathExists(chrome_dir_3)); |
122 // the latest version should stay | 119 // the latest version should stay |
123 EXPECT_TRUE(file_util::PathExists(chrome_dll_4)); | 120 EXPECT_TRUE(file_util::PathExists(chrome_dll_4)); |
124 } | 121 } |
125 | 122 |
126 // Delete older version directories, keeping the one in used intact. | 123 // Delete older version directories, keeping the one in used intact. |
127 TEST_F(SetupHelperTest, DeleteInUsed) { | 124 TEST_F(InstallerStateTest, DeleteInUsed) { |
128 // Create a Chrome dir | 125 // Create a Chrome dir |
129 FilePath chrome_dir(test_dir_); | 126 FilePath chrome_dir(test_dir_.path()); |
130 chrome_dir = chrome_dir.AppendASCII("chrome"); | 127 chrome_dir = chrome_dir.AppendASCII("chrome"); |
131 file_util::CreateDirectory(chrome_dir); | 128 file_util::CreateDirectory(chrome_dir); |
132 ASSERT_TRUE(file_util::PathExists(chrome_dir)); | 129 ASSERT_TRUE(file_util::PathExists(chrome_dir)); |
133 | 130 |
134 FilePath chrome_dir_1(chrome_dir); | 131 FilePath chrome_dir_1(chrome_dir); |
135 chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0"); | 132 chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0"); |
136 file_util::CreateDirectory(chrome_dir_1); | 133 file_util::CreateDirectory(chrome_dir_1); |
137 ASSERT_TRUE(file_util::PathExists(chrome_dir_1)); | 134 ASSERT_TRUE(file_util::PathExists(chrome_dir_1)); |
138 | 135 |
139 FilePath chrome_dir_2(chrome_dir); | 136 FilePath chrome_dir_2(chrome_dir); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 FilePath chrome_dll_3(chrome_dir_3); | 175 FilePath chrome_dll_3(chrome_dir_3); |
179 chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll"); | 176 chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll"); |
180 CreateTextFile(chrome_dll_3.value(), text_content_1); | 177 CreateTextFile(chrome_dll_3.value(), text_content_1); |
181 ASSERT_TRUE(file_util::PathExists(chrome_dll_3)); | 178 ASSERT_TRUE(file_util::PathExists(chrome_dll_3)); |
182 | 179 |
183 FilePath chrome_dll_4(chrome_dir_4); | 180 FilePath chrome_dll_4(chrome_dir_4); |
184 chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll"); | 181 chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll"); |
185 CreateTextFile(chrome_dll_4.value(), text_content_1); | 182 CreateTextFile(chrome_dll_4.value(), text_content_1); |
186 ASSERT_TRUE(file_util::PathExists(chrome_dll_4)); | 183 ASSERT_TRUE(file_util::PathExists(chrome_dll_4)); |
187 | 184 |
| 185 CommandLine cmd_line = CommandLine::FromString(L"setup.exe"); |
| 186 installer::MasterPreferences prefs(cmd_line); |
| 187 installer::InstallationState machine_state; |
| 188 machine_state.Initialize(); |
| 189 MockInstallerState installer_state; |
| 190 installer_state.Initialize(cmd_line, prefs, machine_state); |
| 191 installer_state.set_target_path(chrome_dir); |
| 192 EXPECT_TRUE(installer_state.FindProduct(BrowserDistribution::CHROME_BROWSER) |
| 193 != NULL); |
188 scoped_ptr<Version> latest_version(Version::GetVersionFromString("1.0.4.0")); | 194 scoped_ptr<Version> latest_version(Version::GetVersionFromString("1.0.4.0")); |
189 ChromePackageProperties properties; | 195 installer_state.RemoveOldVersionDirectories(*latest_version.get()); |
190 scoped_refptr<Package> install_path(new Package(false, true, chrome_dir, | |
191 &properties)); | |
192 install_path->RemoveOldVersionDirectories(*latest_version.get()); | |
193 | 196 |
194 // old versions not in used should be gone | 197 // old versions not in used should be gone |
195 EXPECT_FALSE(file_util::PathExists(chrome_dir_1)); | 198 EXPECT_FALSE(file_util::PathExists(chrome_dir_1)); |
196 EXPECT_FALSE(file_util::PathExists(chrome_dir_3)); | 199 EXPECT_FALSE(file_util::PathExists(chrome_dir_3)); |
197 // every thing under in used version should stay | 200 // every thing under in used version should stay |
198 EXPECT_TRUE(file_util::PathExists(chrome_dir_2)); | 201 EXPECT_TRUE(file_util::PathExists(chrome_dir_2)); |
199 EXPECT_TRUE(file_util::PathExists(chrome_dll_2)); | 202 EXPECT_TRUE(file_util::PathExists(chrome_dll_2)); |
200 EXPECT_TRUE(file_util::PathExists(chrome_othera_2)); | 203 EXPECT_TRUE(file_util::PathExists(chrome_othera_2)); |
201 EXPECT_TRUE(file_util::PathExists(chrome_otherb_2)); | 204 EXPECT_TRUE(file_util::PathExists(chrome_otherb_2)); |
202 // the latest version should stay | 205 // the latest version should stay |
203 EXPECT_TRUE(file_util::PathExists(chrome_dll_4)); | 206 EXPECT_TRUE(file_util::PathExists(chrome_dll_4)); |
204 } | 207 } |
| 208 |
| 209 // Tests a few basic things of the Package class. Makes sure that the path |
| 210 // operations are correct |
| 211 TEST_F(InstallerStateTest, Basic) { |
| 212 const bool multi_install = false; |
| 213 const bool system_level = true; |
| 214 CommandLine cmd_line = CommandLine::FromString( |
| 215 std::wstring(L"setup.exe") + |
| 216 (multi_install ? L" --multi-install --chrome" : L"") + |
| 217 (system_level ? L" --system-level" : L"")); |
| 218 installer::MasterPreferences prefs(cmd_line); |
| 219 installer::InstallationState machine_state; |
| 220 machine_state.Initialize(); |
| 221 MockInstallerState installer_state; |
| 222 installer_state.Initialize(cmd_line, prefs, machine_state); |
| 223 installer_state.set_target_path(test_dir_.path()); |
| 224 EXPECT_EQ(test_dir_.path().value(), installer_state.target_path().value()); |
| 225 EXPECT_EQ(1U, installer_state.products().size()); |
| 226 |
| 227 const char kOldVersion[] = "1.2.3.4"; |
| 228 const char kNewVersion[] = "2.3.4.5"; |
| 229 |
| 230 scoped_ptr<Version> new_version(Version::GetVersionFromString(kNewVersion)); |
| 231 scoped_ptr<Version> old_version(Version::GetVersionFromString(kOldVersion)); |
| 232 ASSERT_TRUE(new_version.get() != NULL); |
| 233 ASSERT_TRUE(old_version.get() != NULL); |
| 234 |
| 235 FilePath installer_dir( |
| 236 installer_state.GetInstallerDirectory(*new_version.get())); |
| 237 EXPECT_FALSE(installer_dir.empty()); |
| 238 |
| 239 FilePath new_version_dir(installer_state.target_path().Append( |
| 240 UTF8ToWide(new_version->GetString()))); |
| 241 FilePath old_version_dir(installer_state.target_path().Append( |
| 242 UTF8ToWide(old_version->GetString()))); |
| 243 |
| 244 EXPECT_FALSE(file_util::PathExists(new_version_dir)); |
| 245 EXPECT_FALSE(file_util::PathExists(old_version_dir)); |
| 246 |
| 247 EXPECT_FALSE(file_util::PathExists(installer_dir)); |
| 248 file_util::CreateDirectory(installer_dir); |
| 249 EXPECT_TRUE(file_util::PathExists(new_version_dir)); |
| 250 |
| 251 file_util::CreateDirectory(old_version_dir); |
| 252 EXPECT_TRUE(file_util::PathExists(old_version_dir)); |
| 253 |
| 254 // Create a fake chrome.dll key file in the old version directory. This |
| 255 // should prevent the old version directory from getting deleted. |
| 256 FilePath old_chrome_dll(old_version_dir.Append(installer::kChromeDll)); |
| 257 EXPECT_FALSE(file_util::PathExists(old_chrome_dll)); |
| 258 |
| 259 // Hold on to the file exclusively to prevent the directory from |
| 260 // being deleted. |
| 261 base::win::ScopedHandle file( |
| 262 ::CreateFile(old_chrome_dll.value().c_str(), GENERIC_READ, |
| 263 0, NULL, OPEN_ALWAYS, 0, NULL)); |
| 264 EXPECT_TRUE(file.IsValid()); |
| 265 EXPECT_TRUE(file_util::PathExists(old_chrome_dll)); |
| 266 |
| 267 installer_state.RemoveOldVersionDirectories(*new_version.get()); |
| 268 // The old directory should still exist. |
| 269 EXPECT_TRUE(file_util::PathExists(old_version_dir)); |
| 270 EXPECT_TRUE(file_util::PathExists(new_version_dir)); |
| 271 |
| 272 // Now close the file handle to make it possible to delete our key file. |
| 273 file.Close(); |
| 274 |
| 275 installer_state.RemoveOldVersionDirectories(*new_version.get()); |
| 276 // The new directory should still exist. |
| 277 EXPECT_TRUE(file_util::PathExists(new_version_dir)); |
| 278 |
| 279 // Now, the old directory and key file should be gone. |
| 280 EXPECT_FALSE(file_util::PathExists(old_chrome_dll)); |
| 281 EXPECT_FALSE(file_util::PathExists(old_version_dir)); |
| 282 } |
| 283 |
| 284 TEST_F(InstallerStateTest, WithProduct) { |
| 285 const bool multi_install = false; |
| 286 const bool system_level = true; |
| 287 CommandLine cmd_line = CommandLine::FromString( |
| 288 std::wstring(L"setup.exe") + |
| 289 (multi_install ? L" --multi-install --chrome" : L"") + |
| 290 (system_level ? L" --system-level" : L"")); |
| 291 installer::MasterPreferences prefs(cmd_line); |
| 292 installer::InstallationState machine_state; |
| 293 machine_state.Initialize(); |
| 294 MockInstallerState installer_state; |
| 295 installer_state.Initialize(cmd_line, prefs, machine_state); |
| 296 installer_state.set_target_path(test_dir_.path()); |
| 297 EXPECT_EQ(1U, installer_state.products().size()); |
| 298 EXPECT_EQ(system_level, installer_state.system_install()); |
| 299 |
| 300 const char kCurrentVersion[] = "1.2.3.4"; |
| 301 scoped_ptr<Version> current_version( |
| 302 Version::GetVersionFromString(kCurrentVersion)); |
| 303 |
| 304 HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
| 305 EXPECT_EQ(root, installer_state.root_key()); |
| 306 { |
| 307 TempRegKeyOverride override(root, L"root_pit"); |
| 308 BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution( |
| 309 BrowserDistribution::CHROME_BROWSER); |
| 310 base::win::RegKey chrome_key(root, dist->GetVersionKey().c_str(), |
| 311 KEY_ALL_ACCESS); |
| 312 EXPECT_TRUE(chrome_key.Valid()); |
| 313 if (chrome_key.Valid()) { |
| 314 chrome_key.WriteValue(google_update::kRegVersionField, |
| 315 UTF8ToWide(current_version->GetString()).c_str()); |
| 316 machine_state.Initialize(); |
| 317 // TODO(tommi): Also test for when there exists a new_chrome.exe. |
| 318 scoped_ptr<Version> found_version(installer_state.GetCurrentVersion( |
| 319 machine_state)); |
| 320 EXPECT_TRUE(found_version.get() != NULL); |
| 321 if (found_version.get()) { |
| 322 EXPECT_TRUE(current_version->Equals(*found_version)); |
| 323 } |
| 324 } |
| 325 } |
| 326 } |
OLD | NEW |