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 <windows.h> |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "base/version.h" |
| 9 #include "base/win/registry.h" |
| 10 #include "chrome/installer/util/browser_distribution.h" |
| 11 #include "chrome/installer/util/google_update_constants.h" |
| 12 #include "chrome/installer/util/installation_state.h" |
| 13 #include "chrome/installer/util/product_unittest.h" |
| 14 #include "chrome/installer/util/util_constants.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using base::win::RegKey; |
| 18 using installer::ProductState; |
| 19 |
| 20 class ProductStateTest : public testing::Test { |
| 21 protected: |
| 22 static void SetUpTestCase(); |
| 23 static void TearDownTestCase(); |
| 24 |
| 25 virtual void SetUp(); |
| 26 virtual void TearDown(); |
| 27 |
| 28 void ApplyUninstallCommand(const wchar_t* exe_path, const wchar_t* args); |
| 29 void MinimallyInstallProduct(const wchar_t* version); |
| 30 |
| 31 static BrowserDistribution* dist_; |
| 32 static std::wstring temp_key_path_; |
| 33 bool system_install_; |
| 34 HKEY overridden_; |
| 35 RegKey clients_; |
| 36 RegKey client_state_; |
| 37 }; |
| 38 |
| 39 BrowserDistribution* ProductStateTest::dist_; |
| 40 std::wstring ProductStateTest::temp_key_path_; |
| 41 |
| 42 // static |
| 43 void ProductStateTest::SetUpTestCase() { |
| 44 testing::Test::SetUpTestCase(); |
| 45 |
| 46 // We'll use Chrome as our test subject. |
| 47 dist_ = BrowserDistribution::GetSpecificDistribution( |
| 48 BrowserDistribution::CHROME_BROWSER); |
| 49 |
| 50 // And we'll play in HKCU here: |
| 51 temp_key_path_.assign(TempRegKeyOverride::kTempTestKeyPath) |
| 52 .append(1, L'\\') |
| 53 .append(L"ProductStateTest"); |
| 54 } |
| 55 |
| 56 // static |
| 57 void ProductStateTest::TearDownTestCase() { |
| 58 temp_key_path_.clear(); |
| 59 dist_ = NULL; |
| 60 |
| 61 testing::Test::TearDownTestCase(); |
| 62 } |
| 63 |
| 64 void ProductStateTest::SetUp() { |
| 65 testing::Test::SetUp(); |
| 66 |
| 67 // Create/open the keys for the product we'll test. |
| 68 system_install_ = true; |
| 69 overridden_ = (system_install_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER); |
| 70 |
| 71 // Override for test purposes. We don't use TempRegKeyOverride |
| 72 // directly because it doesn't suit itself to our use here. |
| 73 RegKey temp_key; |
| 74 EXPECT_EQ(ERROR_SUCCESS, |
| 75 temp_key.Create(HKEY_CURRENT_USER, temp_key_path_.c_str(), |
| 76 KEY_ALL_ACCESS)); |
| 77 EXPECT_EQ(ERROR_SUCCESS, |
| 78 ::RegOverridePredefKey(overridden_, temp_key.Handle())); |
| 79 |
| 80 EXPECT_EQ(ERROR_SUCCESS, |
| 81 clients_.Create(overridden_, dist_->GetVersionKey().c_str(), |
| 82 KEY_ALL_ACCESS)); |
| 83 EXPECT_EQ(ERROR_SUCCESS, |
| 84 client_state_.Create(overridden_, dist_->GetStateKey().c_str(), |
| 85 KEY_ALL_ACCESS)); |
| 86 } |
| 87 |
| 88 void ProductStateTest::TearDown() { |
| 89 // Done with the keys. |
| 90 client_state_.Close(); |
| 91 clients_.Close(); |
| 92 EXPECT_EQ(ERROR_SUCCESS, ::RegOverridePredefKey(overridden_, NULL)); |
| 93 overridden_ = NULL; |
| 94 system_install_ = false; |
| 95 |
| 96 // Shotgun approach to clearing out data we may have written. |
| 97 TempRegKeyOverride::DeleteAllTempKeys(); |
| 98 |
| 99 testing::Test::TearDown(); |
| 100 } |
| 101 |
| 102 void ProductStateTest::MinimallyInstallProduct(const wchar_t* version) { |
| 103 EXPECT_EQ(ERROR_SUCCESS, |
| 104 clients_.WriteValue(google_update::kRegVersionField, version)); |
| 105 } |
| 106 |
| 107 void ProductStateTest::ApplyUninstallCommand(const wchar_t* exe_path, |
| 108 const wchar_t* args) { |
| 109 if (exe_path == NULL) { |
| 110 LONG result = client_state_.DeleteValue(installer::kUninstallStringField); |
| 111 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 112 } else { |
| 113 EXPECT_EQ(ERROR_SUCCESS, |
| 114 client_state_.WriteValue(installer::kUninstallStringField, |
| 115 exe_path)); |
| 116 } |
| 117 |
| 118 if (args == NULL) { |
| 119 LONG result = |
| 120 client_state_.DeleteValue(installer::kUninstallArgumentsField); |
| 121 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 122 } else { |
| 123 EXPECT_EQ(ERROR_SUCCESS, |
| 124 client_state_.WriteValue(installer::kUninstallArgumentsField, |
| 125 args)); |
| 126 } |
| 127 } |
| 128 |
| 129 TEST_F(ProductStateTest, InitializeInstalled) { |
| 130 // Not installed. |
| 131 { |
| 132 ProductState state; |
| 133 LONG result = clients_.DeleteValue(google_update::kRegVersionField); |
| 134 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 135 EXPECT_FALSE(state.Initialize(system_install_, dist_)); |
| 136 } |
| 137 |
| 138 // Empty version. |
| 139 { |
| 140 ProductState state; |
| 141 LONG result = clients_.WriteValue(google_update::kRegVersionField, L""); |
| 142 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 143 EXPECT_FALSE(state.Initialize(system_install_, dist_)); |
| 144 } |
| 145 |
| 146 // Bogus version. |
| 147 { |
| 148 ProductState state; |
| 149 LONG result = clients_.WriteValue(google_update::kRegVersionField, |
| 150 L"goofy"); |
| 151 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 152 EXPECT_FALSE(state.Initialize(system_install_, dist_)); |
| 153 } |
| 154 |
| 155 // Valid "pv" value. |
| 156 { |
| 157 ProductState state; |
| 158 LONG result = clients_.WriteValue(google_update::kRegVersionField, |
| 159 L"10.0.47.0"); |
| 160 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 161 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 162 EXPECT_EQ("10.0.47.0", state.version().GetString()); |
| 163 } |
| 164 } |
| 165 |
| 166 // Test extraction of the "opv" value from the Clients key. |
| 167 TEST_F(ProductStateTest, InitializeOldVersion) { |
| 168 MinimallyInstallProduct(L"10.0.1.1"); |
| 169 |
| 170 // No "opv" value. |
| 171 { |
| 172 ProductState state; |
| 173 LONG result = clients_.DeleteValue(google_update::kRegOldVersionField); |
| 174 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 175 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 176 EXPECT_TRUE(state.old_version() == NULL); |
| 177 } |
| 178 |
| 179 // Empty "opv" value. |
| 180 { |
| 181 ProductState state; |
| 182 LONG result = clients_.WriteValue(google_update::kRegOldVersionField, L""); |
| 183 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 184 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 185 EXPECT_TRUE(state.old_version() == NULL); |
| 186 } |
| 187 |
| 188 // Bogus "opv" value. |
| 189 { |
| 190 ProductState state; |
| 191 LONG result = clients_.WriteValue(google_update::kRegOldVersionField, |
| 192 L"coming home"); |
| 193 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 194 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 195 EXPECT_TRUE(state.old_version() == NULL); |
| 196 } |
| 197 |
| 198 // Valid "opv" value. |
| 199 { |
| 200 ProductState state; |
| 201 LONG result = clients_.WriteValue(google_update::kRegOldVersionField, |
| 202 L"10.0.47.0"); |
| 203 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 204 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 205 EXPECT_TRUE(state.old_version() != NULL); |
| 206 EXPECT_EQ("10.0.47.0", state.old_version()->GetString()); |
| 207 } |
| 208 } |
| 209 |
| 210 // Test extraction of the "cmd" value from the Clients key. |
| 211 TEST_F(ProductStateTest, InitializeRenameCmd) { |
| 212 MinimallyInstallProduct(L"10.0.1.1"); |
| 213 |
| 214 // No "cmd" value. |
| 215 { |
| 216 ProductState state; |
| 217 LONG result = clients_.DeleteValue(google_update::kRegRenameCmdField); |
| 218 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 219 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 220 EXPECT_TRUE(state.rename_cmd().empty()); |
| 221 } |
| 222 |
| 223 // Empty "cmd" value. |
| 224 { |
| 225 ProductState state; |
| 226 LONG result = clients_.WriteValue(google_update::kRegRenameCmdField, L""); |
| 227 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 228 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 229 EXPECT_TRUE(state.rename_cmd().empty()); |
| 230 } |
| 231 |
| 232 // Valid "cmd" value. |
| 233 { |
| 234 ProductState state; |
| 235 LONG result = clients_.WriteValue(google_update::kRegRenameCmdField, |
| 236 L"spam.exe --spamalot"); |
| 237 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 238 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 239 EXPECT_EQ(L"spam.exe --spamalot", state.rename_cmd()); |
| 240 } |
| 241 } |
| 242 |
| 243 // Test extraction of the "ap" value from the ClientState key. |
| 244 TEST_F(ProductStateTest, InitializeChannelInfo) { |
| 245 MinimallyInstallProduct(L"10.0.1.1"); |
| 246 |
| 247 // No "ap" value. |
| 248 { |
| 249 ProductState state; |
| 250 LONG result = client_state_.DeleteValue(google_update::kRegApField); |
| 251 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 252 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 253 EXPECT_TRUE(state.channel().value().empty()); |
| 254 } |
| 255 |
| 256 // Empty "ap" value. |
| 257 { |
| 258 ProductState state; |
| 259 LONG result = client_state_.WriteValue(google_update::kRegApField, L""); |
| 260 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 261 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 262 EXPECT_TRUE(state.channel().value().empty()); |
| 263 } |
| 264 |
| 265 // Valid "ap" value. |
| 266 { |
| 267 ProductState state; |
| 268 LONG result = client_state_.WriteValue(google_update::kRegApField, L"spam"); |
| 269 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 270 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 271 EXPECT_EQ(L"spam", state.channel().value()); |
| 272 } |
| 273 } |
| 274 |
| 275 // Test extraction of the uninstall command and arguments from the ClientState |
| 276 // key. |
| 277 TEST_F(ProductStateTest, InitializeUninstallCommand) { |
| 278 MinimallyInstallProduct(L"10.0.1.1"); |
| 279 |
| 280 // No uninstall command. |
| 281 { |
| 282 ProductState state; |
| 283 ApplyUninstallCommand(NULL, NULL); |
| 284 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 285 EXPECT_TRUE(state.GetSetupPath().empty()); |
| 286 EXPECT_TRUE(state.uninstall_command().command_line_string().empty()); |
| 287 EXPECT_EQ(0U, state.uninstall_command().GetSwitchCount()); |
| 288 } |
| 289 |
| 290 // Empty values. |
| 291 { |
| 292 ProductState state; |
| 293 ApplyUninstallCommand(L"", L""); |
| 294 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 295 EXPECT_TRUE(state.GetSetupPath().empty()); |
| 296 EXPECT_TRUE(state.uninstall_command().command_line_string().empty()); |
| 297 EXPECT_EQ(0U, state.uninstall_command().GetSwitchCount()); |
| 298 } |
| 299 |
| 300 // Uninstall command without exe. |
| 301 { |
| 302 ProductState state; |
| 303 ApplyUninstallCommand(NULL, L"--uninstall"); |
| 304 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 305 EXPECT_TRUE(state.GetSetupPath().empty()); |
| 306 EXPECT_EQ(L"\"\" --uninstall", |
| 307 state.uninstall_command().command_line_string()); |
| 308 EXPECT_EQ(1U, state.uninstall_command().GetSwitchCount()); |
| 309 } |
| 310 |
| 311 // Uninstall command without args. |
| 312 { |
| 313 ProductState state; |
| 314 ApplyUninstallCommand(L"setup.exe", NULL); |
| 315 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 316 EXPECT_EQ(L"setup.exe", state.GetSetupPath().value()); |
| 317 EXPECT_EQ(L"\"setup.exe\"", |
| 318 state.uninstall_command().command_line_string()); |
| 319 EXPECT_EQ(0U, state.uninstall_command().GetSwitchCount()); |
| 320 } |
| 321 |
| 322 // Uninstall command with both exe and args. |
| 323 { |
| 324 ProductState state; |
| 325 ApplyUninstallCommand(L"setup.exe", L"--uninstall"); |
| 326 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 327 EXPECT_EQ(L"setup.exe", state.GetSetupPath().value()); |
| 328 EXPECT_EQ(L"\"setup.exe\" --uninstall", |
| 329 state.uninstall_command().command_line_string()); |
| 330 EXPECT_EQ(1U, state.uninstall_command().GetSwitchCount()); |
| 331 } |
| 332 } |
| 333 |
| 334 // Test extraction of the msi marker from the ClientState key. |
| 335 TEST_F(ProductStateTest, InitializeMsi) { |
| 336 MinimallyInstallProduct(L"10.0.1.1"); |
| 337 |
| 338 // No msi marker. |
| 339 { |
| 340 ProductState state; |
| 341 LONG result = client_state_.DeleteValue(google_update::kRegMSIField); |
| 342 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 343 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 344 EXPECT_FALSE(state.is_msi()); |
| 345 } |
| 346 |
| 347 // Msi marker set to zero. |
| 348 { |
| 349 ProductState state; |
| 350 EXPECT_EQ(ERROR_SUCCESS, |
| 351 client_state_.WriteValue(google_update::kRegMSIField, |
| 352 static_cast<DWORD>(0))); |
| 353 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 354 EXPECT_FALSE(state.is_msi()); |
| 355 } |
| 356 |
| 357 // Msi marker set to one. |
| 358 { |
| 359 ProductState state; |
| 360 EXPECT_EQ(ERROR_SUCCESS, |
| 361 client_state_.WriteValue(google_update::kRegMSIField, |
| 362 static_cast<DWORD>(1))); |
| 363 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 364 EXPECT_TRUE(state.is_msi()); |
| 365 } |
| 366 |
| 367 // Msi marker set to a bogus DWORD. |
| 368 { |
| 369 ProductState state; |
| 370 EXPECT_EQ(ERROR_SUCCESS, |
| 371 client_state_.WriteValue(google_update::kRegMSIField, |
| 372 static_cast<DWORD>(47))); |
| 373 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 374 EXPECT_TRUE(state.is_msi()); |
| 375 } |
| 376 |
| 377 // Msi marker set to a bogus string. |
| 378 { |
| 379 ProductState state; |
| 380 EXPECT_EQ(ERROR_SUCCESS, |
| 381 client_state_.WriteValue(google_update::kRegMSIField, |
| 382 L"bogus!")); |
| 383 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 384 EXPECT_FALSE(state.is_msi()); |
| 385 } |
| 386 } |
| 387 |
| 388 // Test detection of multi-install. |
| 389 TEST_F(ProductStateTest, InitializeMultiInstall) { |
| 390 MinimallyInstallProduct(L"10.0.1.1"); |
| 391 |
| 392 // No uninstall command means single install. |
| 393 { |
| 394 ProductState state; |
| 395 ApplyUninstallCommand(NULL, NULL); |
| 396 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 397 EXPECT_FALSE(state.is_multi_install()); |
| 398 } |
| 399 |
| 400 // Uninstall command without --multi-install is single install. |
| 401 { |
| 402 ProductState state; |
| 403 ApplyUninstallCommand(L"setup.exe", L"--uninstall"); |
| 404 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 405 EXPECT_FALSE(state.is_multi_install()); |
| 406 } |
| 407 |
| 408 // Uninstall command with --multi-install is multi install. |
| 409 { |
| 410 ProductState state; |
| 411 ApplyUninstallCommand(L"setup.exe", |
| 412 L"--uninstall --chrome --multi-install"); |
| 413 EXPECT_TRUE(state.Initialize(system_install_, dist_)); |
| 414 EXPECT_TRUE(state.is_multi_install()); |
| 415 } |
| 416 } |
OLD | NEW |