OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/command_line.h" | |
6 #include "base/logging.h" | |
7 #include "base/utf_string_conversions.h" | |
8 #include "base/win/scoped_handle.h" | |
9 #include "chrome/installer/util/browser_distribution.h" | |
10 #include "chrome/installer/util/google_update_constants.h" | |
11 #include "chrome/installer/util/master_preferences.h" | |
12 #include "chrome/installer/util/package.h" | |
13 #include "chrome/installer/util/package_properties.h" | |
14 #include "chrome/installer/util/product.h" | |
15 #include "chrome/installer/util/product_unittest.h" | |
16 #include "chrome/installer/util/util_constants.h" | |
17 | |
18 using base::win::RegKey; | |
19 using installer::ChromePackageProperties; | |
20 using installer::ChromiumPackageProperties; | |
21 using installer::Package; | |
22 using installer::Product; | |
23 using installer::MasterPreferences; | |
24 | |
25 class PackageTest : public TestWithTempDirAndDeleteTempOverrideKeys { | |
26 protected: | |
27 }; | |
28 | |
29 // Tests a few basic things of the Package class. Makes sure that the path | |
30 // operations are correct | |
31 TEST_F(PackageTest, Basic) { | |
32 const bool multi_install = false; | |
33 const bool system_level = true; | |
34 ChromiumPackageProperties properties; | |
35 scoped_refptr<Package> package(new Package(multi_install, system_level, | |
36 test_dir_.path(), &properties)); | |
37 EXPECT_EQ(test_dir_.path().value(), package->path().value()); | |
38 EXPECT_TRUE(package->IsEqual(test_dir_.path())); | |
39 EXPECT_EQ(0U, package->products().size()); | |
40 | |
41 const char kOldVersion[] = "1.2.3.4"; | |
42 const char kNewVersion[] = "2.3.4.5"; | |
43 | |
44 scoped_ptr<Version> new_version(Version::GetVersionFromString(kNewVersion)); | |
45 scoped_ptr<Version> old_version(Version::GetVersionFromString(kOldVersion)); | |
46 ASSERT_TRUE(new_version.get() != NULL); | |
47 ASSERT_TRUE(old_version.get() != NULL); | |
48 | |
49 FilePath installer_dir(package->GetInstallerDirectory(*new_version.get())); | |
50 EXPECT_FALSE(installer_dir.empty()); | |
51 | |
52 FilePath new_version_dir(package->path().Append( | |
53 UTF8ToWide(new_version->GetString()))); | |
54 FilePath old_version_dir(package->path().Append( | |
55 UTF8ToWide(old_version->GetString()))); | |
56 | |
57 EXPECT_FALSE(file_util::PathExists(new_version_dir)); | |
58 EXPECT_FALSE(file_util::PathExists(old_version_dir)); | |
59 | |
60 EXPECT_FALSE(file_util::PathExists(installer_dir)); | |
61 file_util::CreateDirectory(installer_dir); | |
62 EXPECT_TRUE(file_util::PathExists(new_version_dir)); | |
63 | |
64 file_util::CreateDirectory(old_version_dir); | |
65 EXPECT_TRUE(file_util::PathExists(old_version_dir)); | |
66 | |
67 // Create a fake chrome.dll key file in the old version directory. This | |
68 // should prevent the old version directory from getting deleted. | |
69 FilePath old_chrome_dll(old_version_dir.Append(installer::kChromeDll)); | |
70 EXPECT_FALSE(file_util::PathExists(old_chrome_dll)); | |
71 | |
72 // Hold on to the file exclusively to prevent the directory from | |
73 // being deleted. | |
74 base::win::ScopedHandle file( | |
75 ::CreateFile(old_chrome_dll.value().c_str(), GENERIC_READ, | |
76 0, NULL, OPEN_ALWAYS, 0, NULL)); | |
77 EXPECT_TRUE(file.IsValid()); | |
78 EXPECT_TRUE(file_util::PathExists(old_chrome_dll)); | |
79 | |
80 package->RemoveOldVersionDirectories(*new_version.get()); | |
81 // The old directory should still exist. | |
82 EXPECT_TRUE(file_util::PathExists(old_version_dir)); | |
83 EXPECT_TRUE(file_util::PathExists(new_version_dir)); | |
84 | |
85 // Now close the file handle to make it possible to delete our key file. | |
86 file.Close(); | |
87 | |
88 package->RemoveOldVersionDirectories(*new_version.get()); | |
89 // The new directory should still exist. | |
90 EXPECT_TRUE(file_util::PathExists(new_version_dir)); | |
91 | |
92 // Now, the old directory and key file should be gone. | |
93 EXPECT_FALSE(file_util::PathExists(old_chrome_dll)); | |
94 EXPECT_FALSE(file_util::PathExists(old_version_dir)); | |
95 } | |
96 | |
97 TEST_F(PackageTest, WithProduct) { | |
98 const MasterPreferences& prefs = MasterPreferences::ForCurrentProcess(); | |
99 | |
100 // TODO(tommi): We should mock this and use our mocked distribution. | |
101 const bool multi_install = false; | |
102 const bool system_level = true; | |
103 BrowserDistribution* distribution = | |
104 BrowserDistribution::GetSpecificDistribution( | |
105 BrowserDistribution::CHROME_BROWSER, prefs); | |
106 ChromePackageProperties properties; | |
107 scoped_refptr<Package> package(new Package(multi_install, system_level, | |
108 test_dir_.path(), &properties)); | |
109 scoped_refptr<Product> product(new Product(distribution, package.get())); | |
110 EXPECT_EQ(1U, package->products().size()); | |
111 EXPECT_EQ(system_level, package->system_level()); | |
112 | |
113 const char kCurrentVersion[] = "1.2.3.4"; | |
114 scoped_ptr<Version> current_version( | |
115 Version::GetVersionFromString(kCurrentVersion)); | |
116 | |
117 HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
118 { | |
119 TempRegKeyOverride override(root, L"root_pit"); | |
120 RegKey chrome_key(root, distribution->GetVersionKey().c_str(), | |
121 KEY_ALL_ACCESS); | |
122 EXPECT_TRUE(chrome_key.Valid()); | |
123 if (chrome_key.Valid()) { | |
124 chrome_key.WriteValue(google_update::kRegVersionField, | |
125 UTF8ToWide(current_version->GetString()).c_str()); | |
126 // TODO(tommi): Also test for when there exists a new_chrome.exe. | |
127 scoped_ptr<Version> found_version(package->GetCurrentVersion()); | |
128 EXPECT_TRUE(found_version.get() != NULL); | |
129 if (found_version.get()) { | |
130 EXPECT_TRUE(current_version->Equals(*found_version)); | |
131 } | |
132 } | |
133 } | |
134 } | |
135 | |
136 namespace { | |
137 bool SetUninstallArguments(HKEY root, BrowserDistribution* dist, | |
138 const CommandLine& args) { | |
139 RegKey key(root, dist->GetStateKey().c_str(), KEY_ALL_ACCESS); | |
140 return (key.WriteValue(installer::kUninstallArgumentsField, | |
141 args.command_line_string().c_str()) == ERROR_SUCCESS); | |
142 } | |
143 | |
144 bool SetInstalledVersion(HKEY root, BrowserDistribution* dist, | |
145 const std::wstring& version) { | |
146 RegKey key(root, dist->GetVersionKey().c_str(), KEY_ALL_ACCESS); | |
147 return (key.WriteValue(google_update::kRegVersionField, version.c_str()) == | |
148 ERROR_SUCCESS); | |
149 } | |
150 } // end namespace | |
151 | |
152 TEST_F(PackageTest, Dependency) { | |
153 const bool multi_install = false; | |
154 const bool system_level = true; | |
155 HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
156 TempRegKeyOverride override(root, L"root_dep"); | |
157 | |
158 ChromePackageProperties properties; | |
159 scoped_refptr<Package> package(new Package(multi_install, system_level, | |
160 test_dir_.path(), &properties)); | |
161 EXPECT_EQ(0U, package->GetMultiInstallDependencyCount()); | |
162 | |
163 const MasterPreferences& prefs = MasterPreferences::ForCurrentProcess(); | |
164 | |
165 BrowserDistribution* chrome = BrowserDistribution::GetSpecificDistribution( | |
166 BrowserDistribution::CHROME_BROWSER, prefs); | |
167 BrowserDistribution* cf = BrowserDistribution::GetSpecificDistribution( | |
168 BrowserDistribution::CHROME_FRAME, prefs); | |
169 | |
170 CommandLine multi_uninstall_cmd(CommandLine::NO_PROGRAM); | |
171 multi_uninstall_cmd.AppendSwitch(installer::switches::kUninstall); | |
172 multi_uninstall_cmd.AppendSwitch(installer::switches::kMultiInstall); | |
173 | |
174 CommandLine single_uninstall_cmd(CommandLine::NO_PROGRAM); | |
175 single_uninstall_cmd.AppendSwitch(installer::switches::kUninstall); | |
176 | |
177 // "install" Chrome. | |
178 SetUninstallArguments(root, chrome, multi_uninstall_cmd); | |
179 SetInstalledVersion(root, chrome, L"1.2.3.4"); | |
180 EXPECT_EQ(1U, package->GetMultiInstallDependencyCount()); | |
181 | |
182 // "install" Chrome Frame without multi-install. | |
183 SetUninstallArguments(root, cf, single_uninstall_cmd); | |
184 SetInstalledVersion(root, cf, L"1.2.3.4"); | |
185 EXPECT_EQ(1U, package->GetMultiInstallDependencyCount()); | |
186 | |
187 // "install" Chrome Frame with multi-install. | |
188 SetUninstallArguments(root, cf, multi_uninstall_cmd); | |
189 EXPECT_EQ(2U, package->GetMultiInstallDependencyCount()); | |
190 } | |
OLD | NEW |