Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2914)

Side by Side Diff: chrome/installer/util/installer_state_unittest.cc

Issue 6288009: More installer refactoring in the interest of fixing some bugs and cleaning t... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/installer/util/installer_state.cc ('k') | chrome/installer/util/package.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 void BuildSingleChromeState(const FilePath& target_dir,
39 logging::CloseLogFile(); 52 MockInstallerState* installer_state) {
40 // Clean up test directory 53 CommandLine cmd_line = CommandLine::FromString(L"setup.exe");
41 ASSERT_TRUE(file_util::Delete(test_dir_, true)); 54 installer::MasterPreferences prefs(cmd_line);
42 ASSERT_FALSE(file_util::PathExists(test_dir_)); 55 installer::InstallationState machine_state;
43 } 56 machine_state.Initialize();
57 installer_state->Initialize(cmd_line, prefs, machine_state);
58 installer_state->set_target_path(target_dir);
59 EXPECT_TRUE(installer_state->FindProduct(BrowserDistribution::CHROME_BROWSER)
60 != NULL);
61 EXPECT_TRUE(installer_state->FindProduct(BrowserDistribution::CHROME_FRAME)
62 == NULL);
63 }
44 64
45 // the path to temporary directory used to contain the test operations 65 wchar_t text_content_1[] = L"delete me";
46 FilePath test_dir_; 66 wchar_t text_content_2[] = L"delete me as well";
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 67
63 // Delete version directories. Everything lower than the given version 68 // Delete version directories. Everything lower than the given version
64 // should be deleted. 69 // should be deleted.
65 TEST_F(SetupHelperTest, Delete) { 70 TEST_F(InstallerStateTest, Delete) {
71 // TODO(grt): move common stuff into the test fixture.
66 // Create a Chrome dir 72 // Create a Chrome dir
67 FilePath chrome_dir(test_dir_); 73 FilePath chrome_dir(test_dir_.path());
68 chrome_dir = chrome_dir.AppendASCII("chrome"); 74 chrome_dir = chrome_dir.AppendASCII("chrome");
69 file_util::CreateDirectory(chrome_dir); 75 file_util::CreateDirectory(chrome_dir);
70 ASSERT_TRUE(file_util::PathExists(chrome_dir)); 76 ASSERT_TRUE(file_util::PathExists(chrome_dir));
71 77
72 FilePath chrome_dir_1(chrome_dir); 78 FilePath chrome_dir_1(chrome_dir);
73 chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0"); 79 chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0");
74 file_util::CreateDirectory(chrome_dir_1); 80 file_util::CreateDirectory(chrome_dir_1);
75 ASSERT_TRUE(file_util::PathExists(chrome_dir_1)); 81 ASSERT_TRUE(file_util::PathExists(chrome_dir_1));
76 82
77 FilePath chrome_dir_2(chrome_dir); 83 FilePath chrome_dir_2(chrome_dir);
(...skipping 24 matching lines...) Expand all
102 FilePath chrome_dll_3(chrome_dir_3); 108 FilePath chrome_dll_3(chrome_dir_3);
103 chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll"); 109 chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll");
104 CreateTextFile(chrome_dll_3.value(), text_content_1); 110 CreateTextFile(chrome_dll_3.value(), text_content_1);
105 ASSERT_TRUE(file_util::PathExists(chrome_dll_3)); 111 ASSERT_TRUE(file_util::PathExists(chrome_dll_3));
106 112
107 FilePath chrome_dll_4(chrome_dir_4); 113 FilePath chrome_dll_4(chrome_dir_4);
108 chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll"); 114 chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll");
109 CreateTextFile(chrome_dll_4.value(), text_content_1); 115 CreateTextFile(chrome_dll_4.value(), text_content_1);
110 ASSERT_TRUE(file_util::PathExists(chrome_dll_4)); 116 ASSERT_TRUE(file_util::PathExists(chrome_dll_4));
111 117
118 MockInstallerState installer_state;
119 BuildSingleChromeState(chrome_dir, &installer_state);
112 scoped_ptr<Version> latest_version(Version::GetVersionFromString("1.0.4.0")); 120 scoped_ptr<Version> latest_version(Version::GetVersionFromString("1.0.4.0"));
113 ChromePackageProperties properties; 121 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 122
118 // old versions should be gone 123 // old versions should be gone
119 EXPECT_FALSE(file_util::PathExists(chrome_dir_1)); 124 EXPECT_FALSE(file_util::PathExists(chrome_dir_1));
120 EXPECT_FALSE(file_util::PathExists(chrome_dir_2)); 125 EXPECT_FALSE(file_util::PathExists(chrome_dir_2));
121 EXPECT_FALSE(file_util::PathExists(chrome_dir_3)); 126 EXPECT_FALSE(file_util::PathExists(chrome_dir_3));
122 // the latest version should stay 127 // the latest version should stay
123 EXPECT_TRUE(file_util::PathExists(chrome_dll_4)); 128 EXPECT_TRUE(file_util::PathExists(chrome_dll_4));
124 } 129 }
125 130
126 // Delete older version directories, keeping the one in used intact. 131 // Delete older version directories, keeping the one in used intact.
127 TEST_F(SetupHelperTest, DeleteInUsed) { 132 TEST_F(InstallerStateTest, DeleteInUsed) {
128 // Create a Chrome dir 133 // Create a Chrome dir
129 FilePath chrome_dir(test_dir_); 134 FilePath chrome_dir(test_dir_.path());
130 chrome_dir = chrome_dir.AppendASCII("chrome"); 135 chrome_dir = chrome_dir.AppendASCII("chrome");
131 file_util::CreateDirectory(chrome_dir); 136 file_util::CreateDirectory(chrome_dir);
132 ASSERT_TRUE(file_util::PathExists(chrome_dir)); 137 ASSERT_TRUE(file_util::PathExists(chrome_dir));
133 138
134 FilePath chrome_dir_1(chrome_dir); 139 FilePath chrome_dir_1(chrome_dir);
135 chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0"); 140 chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0");
136 file_util::CreateDirectory(chrome_dir_1); 141 file_util::CreateDirectory(chrome_dir_1);
137 ASSERT_TRUE(file_util::PathExists(chrome_dir_1)); 142 ASSERT_TRUE(file_util::PathExists(chrome_dir_1));
138 143
139 FilePath chrome_dir_2(chrome_dir); 144 FilePath chrome_dir_2(chrome_dir);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 FilePath chrome_dll_3(chrome_dir_3); 183 FilePath chrome_dll_3(chrome_dir_3);
179 chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll"); 184 chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll");
180 CreateTextFile(chrome_dll_3.value(), text_content_1); 185 CreateTextFile(chrome_dll_3.value(), text_content_1);
181 ASSERT_TRUE(file_util::PathExists(chrome_dll_3)); 186 ASSERT_TRUE(file_util::PathExists(chrome_dll_3));
182 187
183 FilePath chrome_dll_4(chrome_dir_4); 188 FilePath chrome_dll_4(chrome_dir_4);
184 chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll"); 189 chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll");
185 CreateTextFile(chrome_dll_4.value(), text_content_1); 190 CreateTextFile(chrome_dll_4.value(), text_content_1);
186 ASSERT_TRUE(file_util::PathExists(chrome_dll_4)); 191 ASSERT_TRUE(file_util::PathExists(chrome_dll_4));
187 192
193 MockInstallerState installer_state;
194 BuildSingleChromeState(chrome_dir, &installer_state);
188 scoped_ptr<Version> latest_version(Version::GetVersionFromString("1.0.4.0")); 195 scoped_ptr<Version> latest_version(Version::GetVersionFromString("1.0.4.0"));
189 ChromePackageProperties properties; 196 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 197
194 // old versions not in used should be gone 198 // old versions not in used should be gone
195 EXPECT_FALSE(file_util::PathExists(chrome_dir_1)); 199 EXPECT_FALSE(file_util::PathExists(chrome_dir_1));
196 EXPECT_FALSE(file_util::PathExists(chrome_dir_3)); 200 EXPECT_FALSE(file_util::PathExists(chrome_dir_3));
197 // every thing under in used version should stay 201 // every thing under in used version should stay
198 EXPECT_TRUE(file_util::PathExists(chrome_dir_2)); 202 EXPECT_TRUE(file_util::PathExists(chrome_dir_2));
199 EXPECT_TRUE(file_util::PathExists(chrome_dll_2)); 203 EXPECT_TRUE(file_util::PathExists(chrome_dll_2));
200 EXPECT_TRUE(file_util::PathExists(chrome_othera_2)); 204 EXPECT_TRUE(file_util::PathExists(chrome_othera_2));
201 EXPECT_TRUE(file_util::PathExists(chrome_otherb_2)); 205 EXPECT_TRUE(file_util::PathExists(chrome_otherb_2));
202 // the latest version should stay 206 // the latest version should stay
203 EXPECT_TRUE(file_util::PathExists(chrome_dll_4)); 207 EXPECT_TRUE(file_util::PathExists(chrome_dll_4));
204 } 208 }
209
210 // Tests a few basic things of the Package class. Makes sure that the path
211 // operations are correct
212 TEST_F(InstallerStateTest, Basic) {
213 const bool multi_install = false;
214 const bool system_level = true;
215 CommandLine cmd_line = CommandLine::FromString(
216 std::wstring(L"setup.exe") +
217 (multi_install ? L" --multi-install --chrome" : L"") +
218 (system_level ? L" --system-level" : L""));
219 installer::MasterPreferences prefs(cmd_line);
220 installer::InstallationState machine_state;
221 machine_state.Initialize();
222 MockInstallerState installer_state;
223 installer_state.Initialize(cmd_line, prefs, machine_state);
224 installer_state.set_target_path(test_dir_.path());
225 EXPECT_EQ(test_dir_.path().value(), installer_state.target_path().value());
226 EXPECT_EQ(1U, installer_state.products().size());
227
228 const char kOldVersion[] = "1.2.3.4";
229 const char kNewVersion[] = "2.3.4.5";
230
231 scoped_ptr<Version> new_version(Version::GetVersionFromString(kNewVersion));
232 scoped_ptr<Version> old_version(Version::GetVersionFromString(kOldVersion));
233 ASSERT_TRUE(new_version.get() != NULL);
234 ASSERT_TRUE(old_version.get() != NULL);
235
236 FilePath installer_dir(
237 installer_state.GetInstallerDirectory(*new_version.get()));
238 EXPECT_FALSE(installer_dir.empty());
239
240 FilePath new_version_dir(installer_state.target_path().Append(
241 UTF8ToWide(new_version->GetString())));
242 FilePath old_version_dir(installer_state.target_path().Append(
243 UTF8ToWide(old_version->GetString())));
244
245 EXPECT_FALSE(file_util::PathExists(new_version_dir));
246 EXPECT_FALSE(file_util::PathExists(old_version_dir));
247
248 EXPECT_FALSE(file_util::PathExists(installer_dir));
249 file_util::CreateDirectory(installer_dir);
250 EXPECT_TRUE(file_util::PathExists(new_version_dir));
251
252 file_util::CreateDirectory(old_version_dir);
253 EXPECT_TRUE(file_util::PathExists(old_version_dir));
254
255 // Create a fake chrome.dll key file in the old version directory. This
256 // should prevent the old version directory from getting deleted.
257 FilePath old_chrome_dll(old_version_dir.Append(installer::kChromeDll));
258 EXPECT_FALSE(file_util::PathExists(old_chrome_dll));
259
260 // Hold on to the file exclusively to prevent the directory from
261 // being deleted.
262 base::win::ScopedHandle file(
263 ::CreateFile(old_chrome_dll.value().c_str(), GENERIC_READ,
264 0, NULL, OPEN_ALWAYS, 0, NULL));
265 EXPECT_TRUE(file.IsValid());
266 EXPECT_TRUE(file_util::PathExists(old_chrome_dll));
267
268 installer_state.RemoveOldVersionDirectories(*new_version.get());
269 // The old directory should still exist.
270 EXPECT_TRUE(file_util::PathExists(old_version_dir));
271 EXPECT_TRUE(file_util::PathExists(new_version_dir));
272
273 // Now close the file handle to make it possible to delete our key file.
274 file.Close();
275
276 installer_state.RemoveOldVersionDirectories(*new_version.get());
277 // The new directory should still exist.
278 EXPECT_TRUE(file_util::PathExists(new_version_dir));
279
280 // Now, the old directory and key file should be gone.
281 EXPECT_FALSE(file_util::PathExists(old_chrome_dll));
282 EXPECT_FALSE(file_util::PathExists(old_version_dir));
283 }
284
285 TEST_F(InstallerStateTest, WithProduct) {
286 const bool multi_install = false;
287 const bool system_level = true;
288 CommandLine cmd_line = CommandLine::FromString(
289 std::wstring(L"setup.exe") +
290 (multi_install ? L" --multi-install --chrome" : L"") +
291 (system_level ? L" --system-level" : L""));
292 installer::MasterPreferences prefs(cmd_line);
293 installer::InstallationState machine_state;
294 machine_state.Initialize();
295 MockInstallerState installer_state;
296 installer_state.Initialize(cmd_line, prefs, machine_state);
297 installer_state.set_target_path(test_dir_.path());
298 EXPECT_EQ(1U, installer_state.products().size());
299 EXPECT_EQ(system_level, installer_state.system_install());
300
301 const char kCurrentVersion[] = "1.2.3.4";
302 scoped_ptr<Version> current_version(
303 Version::GetVersionFromString(kCurrentVersion));
304
305 HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
306 EXPECT_EQ(root, installer_state.root_key());
307 {
308 TempRegKeyOverride override(root, L"root_pit");
309 BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution(
310 BrowserDistribution::CHROME_BROWSER);
311 base::win::RegKey chrome_key(root, dist->GetVersionKey().c_str(),
312 KEY_ALL_ACCESS);
313 EXPECT_TRUE(chrome_key.Valid());
314 if (chrome_key.Valid()) {
315 chrome_key.WriteValue(google_update::kRegVersionField,
316 UTF8ToWide(current_version->GetString()).c_str());
317 machine_state.Initialize();
318 // TODO(tommi): Also test for when there exists a new_chrome.exe.
319 scoped_ptr<Version> found_version(installer_state.GetCurrentVersion(
320 machine_state));
321 EXPECT_TRUE(found_version.get() != NULL);
322 if (found_version.get()) {
323 EXPECT_TRUE(current_version->Equals(*found_version));
324 }
325 }
326 }
327 }
OLDNEW
« no previous file with comments | « chrome/installer/util/installer_state.cc ('k') | chrome/installer/util/package.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698