OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/test/mini_installer_test/installer_test_util.h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "base/path_service.h" | |
9 #include "base/process/kill.h" | |
10 #include "base/process/launch.h" | |
11 #include "base/process/process.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/threading/platform_thread.h" | |
14 #include "chrome/common/chrome_result_codes.h" | |
15 #include "chrome/installer/util/google_update_constants.h" | |
16 #include "chrome/installer/util/helper.h" | |
17 #include "chrome/installer/util/install_util.h" | |
18 #include "chrome/installer/util/util_constants.h" | |
19 #include "chrome/test/mini_installer_test/mini_installer_test_constants.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 using installer::InstallationValidator; | |
23 | |
24 namespace { | |
25 | |
26 BrowserDistribution::Type ToBrowserDistributionType( | |
27 InstallationValidator::InstallationType type) { | |
28 const int kChromeMask = | |
29 (InstallationValidator::ProductBits::CHROME_SINGLE | | |
30 InstallationValidator::ProductBits::CHROME_MULTI); | |
31 const int kChromeFrameMask = | |
32 (InstallationValidator::ProductBits::CHROME_FRAME_SINGLE | | |
33 InstallationValidator::ProductBits::CHROME_FRAME_MULTI); | |
34 const int kBinariesMask = | |
35 (InstallationValidator::ProductBits::CHROME_MULTI | | |
36 InstallationValidator::ProductBits::CHROME_FRAME_MULTI); | |
37 // Default return is CHROME_BINARIES. | |
38 BrowserDistribution::Type ret_value = BrowserDistribution::CHROME_BINARIES; | |
39 if (type & kChromeMask) | |
40 ret_value = BrowserDistribution::CHROME_BROWSER; | |
41 if (type & kChromeFrameMask) | |
42 ret_value = BrowserDistribution::CHROME_FRAME; | |
43 if (type & kBinariesMask) | |
44 ret_value = BrowserDistribution::CHROME_BINARIES; | |
45 return ret_value; | |
46 } | |
47 | |
48 } // namespace | |
49 | |
50 namespace installer_test { | |
51 | |
52 bool DeleteInstallDirectory(bool system_level, | |
53 InstallationValidator::InstallationType type) { | |
54 std::string version = GetVersion(type); | |
55 if (version.empty()) | |
56 return false; | |
57 base::FilePath path; | |
58 bool has_install_dir = GetInstallDirectory(system_level, | |
59 ToBrowserDistributionType(type), | |
60 &path); | |
61 if (!has_install_dir || !base::PathExists(path)) | |
62 return false; | |
63 path = path.AppendASCII(version); | |
64 return base::DeleteFile(path, true); | |
65 } | |
66 | |
67 bool DeleteRegistryKey(bool system_level, | |
68 InstallationValidator::InstallationType type, | |
69 REGSAM wow64_access) { | |
70 BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution( | |
71 ToBrowserDistributionType(type)); | |
72 base::FilePath::StringType key(google_update::kRegPathClients); | |
73 key.push_back(base::FilePath::kSeparators[0]); | |
74 key.append(dist->GetAppGuid()); | |
75 HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
76 return InstallUtil::DeleteRegistryKey(root, key, wow64_access); | |
77 } | |
78 | |
79 bool GetChromeInstallDirectory(bool system_level, base::FilePath* path) { | |
80 return GetInstallDirectory(system_level, | |
81 BrowserDistribution::CHROME_BROWSER, path); | |
82 } | |
83 | |
84 bool GetInstallDirectory(bool system_level, | |
85 BrowserDistribution::Type type, base::FilePath* path) { | |
86 BrowserDistribution* dist = | |
87 BrowserDistribution::GetSpecificDistribution(type); | |
88 *path = installer::GetChromeInstallPath(system_level, dist); | |
89 base::FilePath parent; | |
90 if (system_level) | |
91 PathService::Get(base::DIR_PROGRAM_FILES, &parent); | |
92 else | |
93 PathService::Get(base::DIR_LOCAL_APP_DATA, &parent); | |
94 return parent.IsParent(*path); | |
95 } | |
96 | |
97 bool GetInstalledProducts( | |
98 std::vector<installer_test::InstalledProduct>* products) { | |
99 // Clear out the products list. | |
100 products->clear(); | |
101 // Check user-level and system-level for products. | |
102 BrowserDistribution* current_dist; | |
103 installer_test::InstalledProduct current_prod; | |
104 for (int i = 0; i < 2; ++i) { | |
105 const bool system_level = (i != 0); | |
106 InstallationValidator::InstallationType type = | |
107 InstallationValidator::NO_PRODUCTS; | |
108 bool is_valid = | |
109 InstallationValidator::ValidateInstallationType(system_level, &type); | |
110 if (type != InstallationValidator::NO_PRODUCTS) { | |
111 current_dist = BrowserDistribution::GetSpecificDistribution( | |
112 ToBrowserDistributionType(type)); | |
113 Version version; | |
114 InstallUtil::GetChromeVersion(current_dist, system_level, &version); | |
115 if (version.IsValid()) { | |
116 current_prod.type = type; | |
117 current_prod.version = version.GetString(); | |
118 current_prod.system = system_level; | |
119 products->push_back(current_prod); | |
120 } | |
121 } | |
122 } | |
123 return !products->empty(); | |
124 } | |
125 | |
126 bool ValidateInstall(bool system_level, | |
127 InstallationValidator::InstallationType expected, | |
128 const std::string& version) { | |
129 if (GetVersion(expected) != version) | |
130 return false; | |
131 InstallationValidator::InstallationType type; | |
132 InstallationValidator::ValidateInstallationType(system_level, &type); | |
133 if (type == InstallationValidator::NO_PRODUCTS) { | |
134 LOG(ERROR) << "No installed Chrome or Chrome Frame versions found."; | |
135 return false; | |
136 } | |
137 if ((type & expected) == 0) { | |
138 LOG(ERROR) << "Expected type: " << expected << "\n Actual type: " << type; | |
139 return false; | |
140 } | |
141 return true; | |
142 } | |
143 | |
144 std::string GetVersion(InstallationValidator::InstallationType product) { | |
145 std::vector<installer_test::InstalledProduct> installed; | |
146 if (GetInstalledProducts(&installed)) { | |
147 for (size_t i = 0; i < installed.size(); ++i) { | |
148 if ((installed[i].type & product) != 0) { | |
149 return installed[i].version; | |
150 } | |
151 } | |
152 } | |
153 return ""; | |
154 } | |
155 | |
156 bool Install(const base::FilePath& installer) { | |
157 if (!base::PathExists(installer)) { | |
158 LOG(ERROR) << "Installer does not exist: " << installer.MaybeAsASCII(); | |
159 return false; | |
160 } | |
161 CommandLine command(installer); | |
162 LOG(INFO) << "Running installer command: " | |
163 << command.GetCommandLineString(); | |
164 return installer_test::RunAndWaitForCommandToFinish(command); | |
165 } | |
166 | |
167 bool Install(const base::FilePath& installer, const SwitchBuilder& switches) { | |
168 if (!base::PathExists(installer)) { | |
169 LOG(ERROR) << "Installer does not exist: " << installer.MaybeAsASCII(); | |
170 return false; | |
171 } | |
172 CommandLine command(installer); | |
173 command.AppendArguments(switches.GetSwitches(), false); | |
174 LOG(INFO) << "Running installer command: " | |
175 << command.GetCommandLineString(); | |
176 return installer_test::RunAndWaitForCommandToFinish(command); | |
177 } | |
178 | |
179 bool LaunchChrome(bool close_after_launch, bool system_level) { | |
180 base::CleanupProcesses(installer::kChromeExe, base::TimeDelta(), | |
181 content::RESULT_CODE_HUNG, NULL); | |
182 base::FilePath install_path; | |
183 if (!GetChromeInstallDirectory( | |
184 system_level, &install_path)) { | |
185 LOG(ERROR) << "Could not find Chrome install directory"; | |
186 return false; | |
187 } | |
188 install_path = install_path.Append(installer::kChromeExe); | |
189 CommandLine browser(install_path); | |
190 | |
191 base::FilePath exe = browser.GetProgram(); | |
192 LOG(INFO) << "Browser launch command: " << browser.GetCommandLineString(); | |
193 base::ProcessHandle chrome; | |
194 if (!base::LaunchProcess(browser, base::LaunchOptions(), &chrome)) { | |
195 LOG(ERROR) << "Could not launch process: " << exe.value(); | |
196 return false; | |
197 } | |
198 if (close_after_launch) { | |
199 if (base::KillProcess(chrome, 0, true)) { | |
200 LOG(ERROR) << "Failed to close chrome.exe after launch."; | |
201 return false; | |
202 } | |
203 } | |
204 return true; | |
205 } | |
206 | |
207 bool LaunchIE(const std::string& url) { | |
208 base::FilePath browser_path; | |
209 PathService::Get(base::DIR_PROGRAM_FILES, &browser_path); | |
210 browser_path = browser_path.Append(mini_installer_constants::kIELocation); | |
211 browser_path = browser_path.Append(mini_installer_constants::kIEProcessName); | |
212 | |
213 CommandLine cmd_line(browser_path); | |
214 cmd_line.AppendArg(url); | |
215 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL); | |
216 } | |
217 | |
218 bool UninstallAll() { | |
219 base::CleanupProcesses(installer::kChromeExe, base::TimeDelta(), | |
220 content::RESULT_CODE_HUNG, NULL); | |
221 base::CleanupProcesses(installer::kChromeFrameHelperExe, base::TimeDelta(), | |
222 content::RESULT_CODE_HUNG, NULL); | |
223 std::vector<installer_test::InstalledProduct> installed; | |
224 if (!GetInstalledProducts(&installed)) { | |
225 LOG(WARNING) << "No installed products to uninstall."; | |
226 return false; | |
227 } | |
228 bool ret_val = false; | |
229 for (size_t i = 0; i < installed.size(); ++i) { | |
230 if (!Uninstall(installed[i].system, installed[i].type)) | |
231 ret_val = false; | |
232 } | |
233 return ret_val; | |
234 } | |
235 | |
236 bool Uninstall(bool system_level, | |
237 InstallationValidator::InstallationType type) { | |
238 std::vector<BrowserDistribution::Type> products; | |
239 if (ToBrowserDistributionType(type) != | |
240 BrowserDistribution::CHROME_BINARIES) { | |
241 products.push_back(ToBrowserDistributionType(type)); | |
242 } else { | |
243 products.push_back(BrowserDistribution::CHROME_BROWSER); | |
244 products.push_back(BrowserDistribution::CHROME_FRAME); | |
245 } | |
246 bool ret_val = false; | |
247 for (size_t i = 0; i < products.size(); ++i) { | |
248 if (!Uninstall(system_level, products[i])) | |
249 ret_val = false; | |
250 } | |
251 return ret_val; | |
252 } | |
253 | |
254 bool Uninstall(bool system_level, | |
255 BrowserDistribution::Type product) { | |
256 static const int kMultiMask = | |
257 (InstallationValidator::ProductBits::CHROME_MULTI | | |
258 InstallationValidator::ProductBits::CHROME_FRAME_MULTI); | |
259 CommandLine uninstall_cmd(InstallUtil::GetChromeUninstallCmd(system_level, | |
260 product)); | |
261 CommandLine::StringType archive = | |
262 uninstall_cmd.GetProgram().DirName().AppendASCII("chrome.7z").value(); | |
263 uninstall_cmd.AppendSwitch(installer::switches::kUninstall); | |
264 uninstall_cmd.AppendSwitch(installer::switches::kForceUninstall); | |
265 uninstall_cmd.AppendSwitchNative( | |
266 installer::switches::kInstallArchive, archive); | |
267 if (system_level) | |
268 uninstall_cmd.AppendSwitch(installer::switches::kSystemLevel); | |
269 if ((product & kMultiMask) !=0) | |
270 uninstall_cmd.AppendSwitch(installer::switches::kMultiInstall); | |
271 LOG(INFO) << "Uninstall command: " << uninstall_cmd.GetCommandLineString(); | |
272 bool ret_val = RunAndWaitForCommandToFinish(uninstall_cmd); | |
273 // Close IE notification when uninstalling Chrome Frame. | |
274 base::CleanupProcesses(mini_installer_constants::kIEProcessName, | |
275 base::TimeDelta(), | |
276 content::RESULT_CODE_HUNG, NULL); | |
277 return ret_val; | |
278 } | |
279 | |
280 | |
281 bool RunAndWaitForCommandToFinish(CommandLine command) { | |
282 if (!base::PathExists(command.GetProgram())) { | |
283 LOG(ERROR) << "Command executable does not exist: " | |
284 << command.GetProgram().MaybeAsASCII(); | |
285 return false; | |
286 } | |
287 base::ProcessHandle process; | |
288 if (!base::LaunchProcess(command, base::LaunchOptions(), &process)) { | |
289 LOG(ERROR) << "Failed to launch command: " | |
290 << command.GetCommandLineString(); | |
291 return false; | |
292 } | |
293 if (!base::WaitForSingleProcess(process, base::TimeDelta::FromMinutes(1))) { | |
294 LOG(ERROR) << "Launched process did not complete."; | |
295 return false; | |
296 } | |
297 return true; | |
298 } | |
299 | |
300 } // namespace installer_test | |
OLD | NEW |