Update broken references to image assets
[chromium-blink-merge.git] / chrome / installer / util / product_state_unittest.cc
blob1b710e0f2d787e4c52542764d782bf522862e3c7
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.
5 #include <windows.h>
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/test/test_reg_util_win.h"
9 #include "base/version.h"
10 #include "base/win/registry.h"
11 #include "chrome/installer/util/browser_distribution.h"
12 #include "chrome/installer/util/google_update_constants.h"
13 #include "chrome/installer/util/installation_state.h"
14 #include "chrome/installer/util/util_constants.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using base::win::RegKey;
18 using installer::ProductState;
19 using registry_util::RegistryOverrideManager;
21 class ProductStateTest : public testing::Test {
22 protected:
23 static void SetUpTestCase();
24 static void TearDownTestCase();
26 void SetUp() override;
27 void TearDown() override;
29 void ApplyUninstallCommand(const wchar_t* exe_path, const wchar_t* args);
30 void MinimallyInstallProduct(const wchar_t* version);
32 static BrowserDistribution* dist_;
33 bool system_install_;
34 HKEY overridden_;
35 registry_util::RegistryOverrideManager registry_override_manager_;
36 RegKey clients_;
37 RegKey client_state_;
40 BrowserDistribution* ProductStateTest::dist_;
42 // static
43 void ProductStateTest::SetUpTestCase() {
44 testing::Test::SetUpTestCase();
46 // We'll use Chrome as our test subject.
47 dist_ = BrowserDistribution::GetSpecificDistribution(
48 BrowserDistribution::CHROME_BROWSER);
51 // static
52 void ProductStateTest::TearDownTestCase() {
53 dist_ = NULL;
55 testing::Test::TearDownTestCase();
58 void ProductStateTest::SetUp() {
59 testing::Test::SetUp();
61 // Create/open the keys for the product we'll test.
62 system_install_ = true;
63 overridden_ = (system_install_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER);
65 // Override for test purposes. We don't use ScopedRegistryKeyOverride
66 // directly because it doesn't suit itself to our use here.
67 RegKey temp_key;
69 registry_override_manager_.OverrideRegistry(overridden_);
71 EXPECT_EQ(ERROR_SUCCESS,
72 clients_.Create(overridden_, dist_->GetVersionKey().c_str(),
73 KEY_ALL_ACCESS));
74 EXPECT_EQ(ERROR_SUCCESS,
75 client_state_.Create(overridden_, dist_->GetStateKey().c_str(),
76 KEY_ALL_ACCESS));
79 void ProductStateTest::TearDown() {
80 // Done with the keys.
81 client_state_.Close();
82 clients_.Close();
83 overridden_ = NULL;
84 system_install_ = false;
86 testing::Test::TearDown();
89 void ProductStateTest::MinimallyInstallProduct(const wchar_t* version) {
90 EXPECT_EQ(ERROR_SUCCESS,
91 clients_.WriteValue(google_update::kRegVersionField, version));
94 void ProductStateTest::ApplyUninstallCommand(const wchar_t* exe_path,
95 const wchar_t* args) {
96 if (exe_path == NULL) {
97 LONG result = client_state_.DeleteValue(installer::kUninstallStringField);
98 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
99 } else {
100 EXPECT_EQ(ERROR_SUCCESS,
101 client_state_.WriteValue(installer::kUninstallStringField,
102 exe_path));
105 if (args == NULL) {
106 LONG result =
107 client_state_.DeleteValue(installer::kUninstallArgumentsField);
108 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
109 } else {
110 EXPECT_EQ(ERROR_SUCCESS,
111 client_state_.WriteValue(installer::kUninstallArgumentsField,
112 args));
116 TEST_F(ProductStateTest, InitializeInstalled) {
117 // Not installed.
119 ProductState state;
120 LONG result = clients_.DeleteValue(google_update::kRegVersionField);
121 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
122 EXPECT_FALSE(state.Initialize(system_install_, dist_));
125 // Empty version.
127 ProductState state;
128 LONG result = clients_.WriteValue(google_update::kRegVersionField, L"");
129 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
130 EXPECT_FALSE(state.Initialize(system_install_, dist_));
133 // Bogus version.
135 ProductState state;
136 LONG result = clients_.WriteValue(google_update::kRegVersionField,
137 L"goofy");
138 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
139 EXPECT_FALSE(state.Initialize(system_install_, dist_));
142 // Valid "pv" value.
144 ProductState state;
145 LONG result = clients_.WriteValue(google_update::kRegVersionField,
146 L"10.0.47.0");
147 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
148 EXPECT_TRUE(state.Initialize(system_install_, dist_));
149 EXPECT_EQ("10.0.47.0", state.version().GetString());
153 // Test extraction of the "opv" value from the Clients key.
154 TEST_F(ProductStateTest, InitializeOldVersion) {
155 MinimallyInstallProduct(L"10.0.1.1");
157 // No "opv" value.
159 ProductState state;
160 LONG result = clients_.DeleteValue(google_update::kRegOldVersionField);
161 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
162 EXPECT_TRUE(state.Initialize(system_install_, dist_));
163 EXPECT_TRUE(state.old_version() == NULL);
166 // Empty "opv" value.
168 ProductState state;
169 LONG result = clients_.WriteValue(google_update::kRegOldVersionField, L"");
170 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
171 EXPECT_TRUE(state.Initialize(system_install_, dist_));
172 EXPECT_TRUE(state.old_version() == NULL);
175 // Bogus "opv" value.
177 ProductState state;
178 LONG result = clients_.WriteValue(google_update::kRegOldVersionField,
179 L"coming home");
180 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
181 EXPECT_TRUE(state.Initialize(system_install_, dist_));
182 EXPECT_TRUE(state.old_version() == NULL);
185 // Valid "opv" value.
187 ProductState state;
188 LONG result = clients_.WriteValue(google_update::kRegOldVersionField,
189 L"10.0.47.0");
190 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
191 EXPECT_TRUE(state.Initialize(system_install_, dist_));
192 EXPECT_TRUE(state.old_version() != NULL);
193 EXPECT_EQ("10.0.47.0", state.old_version()->GetString());
197 // Test extraction of the "cmd" value from the Clients key.
198 TEST_F(ProductStateTest, InitializeRenameCmd) {
199 MinimallyInstallProduct(L"10.0.1.1");
201 // No "cmd" value.
203 ProductState state;
204 LONG result = clients_.DeleteValue(google_update::kRegRenameCmdField);
205 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
206 EXPECT_TRUE(state.Initialize(system_install_, dist_));
207 EXPECT_TRUE(state.rename_cmd().empty());
210 // Empty "cmd" value.
212 ProductState state;
213 LONG result = clients_.WriteValue(google_update::kRegRenameCmdField, L"");
214 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
215 EXPECT_TRUE(state.Initialize(system_install_, dist_));
216 EXPECT_TRUE(state.rename_cmd().empty());
219 // Valid "cmd" value.
221 ProductState state;
222 LONG result = clients_.WriteValue(google_update::kRegRenameCmdField,
223 L"spam.exe --spamalot");
224 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
225 EXPECT_TRUE(state.Initialize(system_install_, dist_));
226 EXPECT_EQ(L"spam.exe --spamalot", state.rename_cmd());
230 // Test extraction of the "ap" value from the ClientState key.
231 TEST_F(ProductStateTest, InitializeChannelInfo) {
232 MinimallyInstallProduct(L"10.0.1.1");
234 // No "ap" value.
236 ProductState state;
237 LONG result = client_state_.DeleteValue(google_update::kRegApField);
238 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
239 EXPECT_TRUE(state.Initialize(system_install_, dist_));
240 EXPECT_TRUE(state.channel().value().empty());
243 // Empty "ap" value.
245 ProductState state;
246 LONG result = client_state_.WriteValue(google_update::kRegApField, L"");
247 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
248 EXPECT_TRUE(state.Initialize(system_install_, dist_));
249 EXPECT_TRUE(state.channel().value().empty());
252 // Valid "ap" value.
254 ProductState state;
255 LONG result = client_state_.WriteValue(google_update::kRegApField, L"spam");
256 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
257 EXPECT_TRUE(state.Initialize(system_install_, dist_));
258 EXPECT_EQ(L"spam", state.channel().value());
262 // Test extraction of the uninstall command and arguments from the ClientState
263 // key.
264 TEST_F(ProductStateTest, InitializeUninstallCommand) {
265 MinimallyInstallProduct(L"10.0.1.1");
267 // No uninstall command.
269 ProductState state;
270 ApplyUninstallCommand(NULL, NULL);
271 EXPECT_TRUE(state.Initialize(system_install_, dist_));
272 EXPECT_TRUE(state.GetSetupPath().empty());
273 EXPECT_TRUE(state.uninstall_command().GetCommandLineString().empty());
274 EXPECT_TRUE(state.uninstall_command().GetSwitches().empty());
277 // Empty values.
279 ProductState state;
280 ApplyUninstallCommand(L"", L"");
281 EXPECT_TRUE(state.Initialize(system_install_, dist_));
282 EXPECT_TRUE(state.GetSetupPath().empty());
283 EXPECT_TRUE(state.uninstall_command().GetCommandLineString().empty());
284 EXPECT_TRUE(state.uninstall_command().GetSwitches().empty());
287 // Uninstall command without exe.
289 ProductState state;
290 ApplyUninstallCommand(NULL, L"--uninstall");
291 EXPECT_TRUE(state.Initialize(system_install_, dist_));
292 EXPECT_TRUE(state.GetSetupPath().empty());
293 EXPECT_EQ(L" --uninstall",
294 state.uninstall_command().GetCommandLineString());
295 EXPECT_EQ(1U, state.uninstall_command().GetSwitches().size());
298 // Uninstall command without args.
300 ProductState state;
301 ApplyUninstallCommand(L"setup.exe", NULL);
302 EXPECT_TRUE(state.Initialize(system_install_, dist_));
303 EXPECT_EQ(L"setup.exe", state.GetSetupPath().value());
304 EXPECT_EQ(L"setup.exe", state.uninstall_command().GetCommandLineString());
305 EXPECT_TRUE(state.uninstall_command().GetSwitches().empty());
308 // Uninstall command with exe that requires quoting.
310 ProductState state;
311 ApplyUninstallCommand(L"set up.exe", NULL);
312 EXPECT_TRUE(state.Initialize(system_install_, dist_));
313 EXPECT_EQ(L"set up.exe", state.GetSetupPath().value());
314 EXPECT_EQ(L"\"set up.exe\"",
315 state.uninstall_command().GetCommandLineString());
316 EXPECT_TRUE(state.uninstall_command().GetSwitches().empty());
319 // Uninstall command with both exe and args.
321 ProductState state;
322 ApplyUninstallCommand(L"setup.exe", L"--uninstall");
323 EXPECT_TRUE(state.Initialize(system_install_, dist_));
324 EXPECT_EQ(L"setup.exe", state.GetSetupPath().value());
325 EXPECT_EQ(L"setup.exe --uninstall",
326 state.uninstall_command().GetCommandLineString());
327 EXPECT_EQ(1U, state.uninstall_command().GetSwitches().size());
331 // Test extraction of the msi marker from the ClientState key.
332 TEST_F(ProductStateTest, InitializeMsi) {
333 MinimallyInstallProduct(L"10.0.1.1");
335 // No msi marker.
337 ProductState state;
338 LONG result = client_state_.DeleteValue(google_update::kRegMSIField);
339 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
340 EXPECT_TRUE(state.Initialize(system_install_, dist_));
341 EXPECT_FALSE(state.is_msi());
344 // Msi marker set to zero.
346 ProductState state;
347 EXPECT_EQ(ERROR_SUCCESS,
348 client_state_.WriteValue(google_update::kRegMSIField,
349 static_cast<DWORD>(0)));
350 EXPECT_TRUE(state.Initialize(system_install_, dist_));
351 EXPECT_FALSE(state.is_msi());
354 // Msi marker set to one.
356 ProductState state;
357 EXPECT_EQ(ERROR_SUCCESS,
358 client_state_.WriteValue(google_update::kRegMSIField,
359 static_cast<DWORD>(1)));
360 EXPECT_TRUE(state.Initialize(system_install_, dist_));
361 EXPECT_TRUE(state.is_msi());
364 // Msi marker set to a bogus DWORD.
366 ProductState state;
367 EXPECT_EQ(ERROR_SUCCESS,
368 client_state_.WriteValue(google_update::kRegMSIField,
369 static_cast<DWORD>(47)));
370 EXPECT_TRUE(state.Initialize(system_install_, dist_));
371 EXPECT_TRUE(state.is_msi());
374 // Msi marker set to a bogus string.
376 ProductState state;
377 EXPECT_EQ(ERROR_SUCCESS,
378 client_state_.WriteValue(google_update::kRegMSIField,
379 L"bogus!"));
380 EXPECT_TRUE(state.Initialize(system_install_, dist_));
381 EXPECT_FALSE(state.is_msi());
385 // Test detection of multi-install.
386 TEST_F(ProductStateTest, InitializeMultiInstall) {
387 MinimallyInstallProduct(L"10.0.1.1");
389 // No uninstall command means single install.
391 ProductState state;
392 ApplyUninstallCommand(NULL, NULL);
393 EXPECT_TRUE(state.Initialize(system_install_, dist_));
394 EXPECT_FALSE(state.is_multi_install());
397 // Uninstall command without --multi-install is single install.
399 ProductState state;
400 ApplyUninstallCommand(L"setup.exe", L"--uninstall");
401 EXPECT_TRUE(state.Initialize(system_install_, dist_));
402 EXPECT_FALSE(state.is_multi_install());
405 // Uninstall command with --multi-install is multi install.
407 ProductState state;
408 ApplyUninstallCommand(L"setup.exe",
409 L"--uninstall --chrome --multi-install");
410 EXPECT_TRUE(state.Initialize(system_install_, dist_));
411 EXPECT_TRUE(state.is_multi_install());