install_unittest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 <objbase.h> 6 7#include <string> 8 9#include "base/file_util.h" 10#include "base/files/file_path.h" 11#include "base/files/scoped_temp_dir.h" 12#include "base/memory/scoped_ptr.h" 13#include "base/path_service.h" 14#include "base/strings/string16.h" 15#include "base/strings/utf_string_conversions.h" 16#include "base/test/scoped_path_override.h" 17#include "base/test/test_shortcut_win.h" 18#include "base/version.h" 19#include "base/win/shortcut.h" 20#include "chrome/installer/setup/install.h" 21#include "chrome/installer/setup/install_worker.h" 22#include "chrome/installer/setup/setup_constants.h" 23#include "chrome/installer/util/browser_distribution.h" 24#include "chrome/installer/util/installer_state.h" 25#include "chrome/installer/util/master_preferences.h" 26#include "chrome/installer/util/master_preferences_constants.h" 27#include "chrome/installer/util/product.h" 28#include "chrome/installer/util/shell_util.h" 29#include "chrome/installer/util/util_constants.h" 30#include "testing/gtest/include/gtest/gtest.h" 31 32namespace { 33 34class CreateVisualElementsManifestTest : public testing::Test { 35 protected: 36 virtual void SetUp() OVERRIDE { 37 // Create a temp directory for testing. 38 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); 39 40 version_ = Version("0.0.0.0"); 41 42 version_dir_ = test_dir_.path().AppendASCII(version_.GetString()); 43 ASSERT_TRUE(base::CreateDirectory(version_dir_)); 44 45 manifest_path_ = 46 test_dir_.path().Append(installer::kVisualElementsManifest); 47 } 48 49 virtual void TearDown() OVERRIDE { 50 // Clean up test directory manually so we can fail if it leaks. 51 ASSERT_TRUE(test_dir_.Delete()); 52 } 53 54 // The temporary directory used to contain the test operations. 55 base::ScopedTempDir test_dir_; 56 57 // A dummy version number used to create the version directory. 58 Version version_; 59 60 // The path to |test_dir_|\|version_|. 61 base::FilePath version_dir_; 62 63 // The path to VisualElementsManifest.xml. 64 base::FilePath manifest_path_; 65}; 66 67class InstallShortcutTest : public testing::Test { 68 protected: 69 virtual void SetUp() OVERRIDE { 70 EXPECT_EQ(S_OK, CoInitialize(NULL)); 71 72 dist_ = BrowserDistribution::GetDistribution(); 73 ASSERT_TRUE(dist_ != NULL); 74 product_.reset(new installer::Product(dist_)); 75 76 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 77 chrome_exe_ = temp_dir_.path().Append(installer::kChromeExe); 78 EXPECT_EQ(0, base::WriteFile(chrome_exe_, "", 0)); 79 80 ShellUtil::ShortcutProperties chrome_properties(ShellUtil::CURRENT_USER); 81 product_->AddDefaultShortcutProperties(chrome_exe_, &chrome_properties); 82 83 expected_properties_.set_target(chrome_exe_); 84 expected_properties_.set_icon(chrome_properties.icon, 85 chrome_properties.icon_index); 86 expected_properties_.set_app_id(chrome_properties.app_id); 87 expected_properties_.set_description(chrome_properties.description); 88 expected_properties_.set_dual_mode(false); 89 expected_start_menu_properties_ = expected_properties_; 90 expected_start_menu_properties_.set_dual_mode(true); 91 92 prefs_.reset(GetFakeMasterPrefs(false, false, false)); 93 94 ASSERT_TRUE(fake_user_desktop_.CreateUniqueTempDir()); 95 ASSERT_TRUE(fake_common_desktop_.CreateUniqueTempDir()); 96 ASSERT_TRUE(fake_user_quick_launch_.CreateUniqueTempDir()); 97 ASSERT_TRUE(fake_default_user_quick_launch_.CreateUniqueTempDir()); 98 ASSERT_TRUE(fake_start_menu_.CreateUniqueTempDir()); 99 ASSERT_TRUE(fake_common_start_menu_.CreateUniqueTempDir()); 100 user_desktop_override_.reset( 101 new base::ScopedPathOverride(base::DIR_USER_DESKTOP, 102 fake_user_desktop_.path())); 103 common_desktop_override_.reset( 104 new base::ScopedPathOverride(base::DIR_COMMON_DESKTOP, 105 fake_common_desktop_.path())); 106 user_quick_launch_override_.reset( 107 new base::ScopedPathOverride(base::DIR_USER_QUICK_LAUNCH, 108 fake_user_quick_launch_.path())); 109 default_user_quick_launch_override_.reset( 110 new base::ScopedPathOverride(base::DIR_DEFAULT_USER_QUICK_LAUNCH, 111 fake_default_user_quick_launch_.path())); 112 start_menu_override_.reset( 113 new base::ScopedPathOverride(base::DIR_START_MENU, 114 fake_start_menu_.path())); 115 common_start_menu_override_.reset( 116 new base::ScopedPathOverride(base::DIR_COMMON_START_MENU, 117 fake_common_start_menu_.path())); 118 119 base::string16 shortcut_name( 120 dist_->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME) + 121 installer::kLnkExt); 122 base::string16 alternate_shortcut_name( 123 dist_->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME_ALTERNATE) + 124 installer::kLnkExt); 125 126 user_desktop_shortcut_ = 127 fake_user_desktop_.path().Append(shortcut_name); 128 user_quick_launch_shortcut_ = 129 fake_user_quick_launch_.path().Append(shortcut_name); 130 user_start_menu_shortcut_ = 131 fake_start_menu_.path().Append( 132 dist_->GetStartMenuShortcutSubfolder( 133 BrowserDistribution::SUBFOLDER_CHROME)) 134 .Append(shortcut_name); 135 system_desktop_shortcut_ = 136 fake_common_desktop_.path().Append(shortcut_name); 137 system_quick_launch_shortcut_ = 138 fake_default_user_quick_launch_.path().Append(shortcut_name); 139 system_start_menu_shortcut_ = 140 fake_common_start_menu_.path().Append( 141 dist_->GetStartMenuShortcutSubfolder( 142 BrowserDistribution::SUBFOLDER_CHROME)) 143 .Append(shortcut_name); 144 user_alternate_desktop_shortcut_ = 145 fake_user_desktop_.path().Append(alternate_shortcut_name); 146 } 147 148 virtual void TearDown() OVERRIDE { 149 // Try to unpin potentially pinned shortcuts (although pinning isn't tested, 150 // the call itself might still have pinned the Start Menu shortcuts). 151 base::win::TaskbarUnpinShortcutLink( 152 user_start_menu_shortcut_.value().c_str()); 153 base::win::TaskbarUnpinShortcutLink( 154 system_start_menu_shortcut_.value().c_str()); 155 CoUninitialize(); 156 } 157 158 installer::MasterPreferences* GetFakeMasterPrefs( 159 bool do_not_create_desktop_shortcut, 160 bool do_not_create_quick_launch_shortcut, 161 bool alternate_desktop_shortcut) { 162 const struct { 163 const char* pref_name; 164 bool is_desired; 165 } desired_prefs[] = { 166 { installer::master_preferences::kDoNotCreateDesktopShortcut, 167 do_not_create_desktop_shortcut }, 168 { installer::master_preferences::kDoNotCreateQuickLaunchShortcut, 169 do_not_create_quick_launch_shortcut }, 170 { installer::master_preferences::kAltShortcutText, 171 alternate_desktop_shortcut }, 172 }; 173 174 std::string master_prefs("{\"distribution\":{"); 175 for (size_t i = 0; i < arraysize(desired_prefs); ++i) { 176 master_prefs += (i == 0 ? "\"" : ",\""); 177 master_prefs += desired_prefs[i].pref_name; 178 master_prefs += "\":"; 179 master_prefs += desired_prefs[i].is_desired ? "true" : "false"; 180 } 181 master_prefs += "}}"; 182 183 return new installer::MasterPreferences(master_prefs); 184 } 185 186 base::win::ShortcutProperties expected_properties_; 187 base::win::ShortcutProperties expected_start_menu_properties_; 188 189 BrowserDistribution* dist_; 190 base::FilePath chrome_exe_; 191 scoped_ptr<installer::Product> product_; 192 scoped_ptr<installer::MasterPreferences> prefs_; 193 194 base::ScopedTempDir temp_dir_; 195 base::ScopedTempDir fake_user_desktop_; 196 base::ScopedTempDir fake_common_desktop_; 197 base::ScopedTempDir fake_user_quick_launch_; 198 base::ScopedTempDir fake_default_user_quick_launch_; 199 base::ScopedTempDir fake_start_menu_; 200 base::ScopedTempDir fake_common_start_menu_; 201 scoped_ptr<base::ScopedPathOverride> user_desktop_override_; 202 scoped_ptr<base::ScopedPathOverride> common_desktop_override_; 203 scoped_ptr<base::ScopedPathOverride> user_quick_launch_override_; 204 scoped_ptr<base::ScopedPathOverride> default_user_quick_launch_override_; 205 scoped_ptr<base::ScopedPathOverride> start_menu_override_; 206 scoped_ptr<base::ScopedPathOverride> common_start_menu_override_; 207 208 base::FilePath user_desktop_shortcut_; 209 base::FilePath user_quick_launch_shortcut_; 210 base::FilePath user_start_menu_shortcut_; 211 base::FilePath system_desktop_shortcut_; 212 base::FilePath system_quick_launch_shortcut_; 213 base::FilePath system_start_menu_shortcut_; 214 base::FilePath user_alternate_desktop_shortcut_; 215}; 216 217} // namespace 218 219// Test that VisualElementsManifest.xml is not created when VisualElements are 220// not present. 221TEST_F(CreateVisualElementsManifestTest, VisualElementsManifestNotCreated) { 222 ASSERT_TRUE( 223 installer::CreateVisualElementsManifest(test_dir_.path(), version_)); 224 ASSERT_FALSE(base::PathExists(manifest_path_)); 225} 226 227// Test that VisualElementsManifest.xml is created with the correct content when 228// VisualElements are present. 229TEST_F(CreateVisualElementsManifestTest, VisualElementsManifestCreated) { 230 ASSERT_TRUE(base::CreateDirectory( 231 version_dir_.Append(installer::kVisualElements))); 232 ASSERT_TRUE( 233 installer::CreateVisualElementsManifest(test_dir_.path(), version_)); 234 ASSERT_TRUE(base::PathExists(manifest_path_)); 235 236 std::string read_manifest; 237 ASSERT_TRUE(base::ReadFileToString(manifest_path_, &read_manifest)); 238 239 static const char kExpectedManifest[] = 240 "<Application>\r\n" 241 " <VisualElements\r\n" 242 " DisplayName='Google Chrome'\r\n" 243 " Logo='0.0.0.0\\VisualElements\\Logo.png'\r\n" 244 " SmallLogo='0.0.0.0\\VisualElements\\SmallLogo.png'\r\n" 245 " ForegroundText='light'\r\n" 246 " BackgroundColor='#323232'>\r\n" 247 " <DefaultTile ShowName='allLogos'/>\r\n" 248 " <SplashScreen Image='0.0.0.0\\VisualElements\\splash-620x300.png'/>" 249 "\r\n" 250 " </VisualElements>\r\n" 251 "</Application>"; 252 253 ASSERT_STREQ(kExpectedManifest, read_manifest.c_str()); 254} 255 256TEST_F(InstallShortcutTest, CreateAllShortcuts) { 257 installer::CreateOrUpdateShortcuts( 258 chrome_exe_, *product_, *prefs_, installer::CURRENT_USER, 259 installer::INSTALL_SHORTCUT_CREATE_ALL); 260 base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_); 261 base::win::ValidateShortcut(user_quick_launch_shortcut_, 262 expected_properties_); 263 base::win::ValidateShortcut(user_start_menu_shortcut_, 264 expected_start_menu_properties_); 265} 266 267// Disabled failing test; http://crbug.com/329239. 268TEST_F(InstallShortcutTest, DISABLED_CreateAllShortcutsSystemLevel) { 269 installer::CreateOrUpdateShortcuts( 270 chrome_exe_, *product_, *prefs_, installer::ALL_USERS, 271 installer::INSTALL_SHORTCUT_CREATE_ALL); 272 base::win::ValidateShortcut(system_desktop_shortcut_, expected_properties_); 273 base::win::ValidateShortcut(system_quick_launch_shortcut_, 274 expected_properties_); 275 base::win::ValidateShortcut(system_start_menu_shortcut_, 276 expected_start_menu_properties_); 277} 278 279TEST_F(InstallShortcutTest, CreateAllShortcutsAlternateDesktopName) { 280 scoped_ptr<installer::MasterPreferences> prefs_alt_desktop( 281 GetFakeMasterPrefs(false, false, true)); 282 installer::CreateOrUpdateShortcuts( 283 chrome_exe_, *product_, *prefs_alt_desktop, installer::CURRENT_USER, 284 installer::INSTALL_SHORTCUT_CREATE_ALL); 285 base::win::ValidateShortcut(user_alternate_desktop_shortcut_, 286 expected_properties_); 287 base::win::ValidateShortcut(user_quick_launch_shortcut_, 288 expected_properties_); 289 base::win::ValidateShortcut(user_start_menu_shortcut_, 290 expected_start_menu_properties_); 291} 292 293TEST_F(InstallShortcutTest, CreateAllShortcutsButDesktopShortcut) { 294 scoped_ptr<installer::MasterPreferences> prefs_no_desktop( 295 GetFakeMasterPrefs(true, false, false)); 296 installer::CreateOrUpdateShortcuts( 297 chrome_exe_, *product_, *prefs_no_desktop, installer::CURRENT_USER, 298 installer::INSTALL_SHORTCUT_CREATE_ALL); 299 ASSERT_FALSE(base::PathExists(user_desktop_shortcut_)); 300 base::win::ValidateShortcut(user_quick_launch_shortcut_, 301 expected_properties_); 302 base::win::ValidateShortcut(user_start_menu_shortcut_, 303 expected_start_menu_properties_); 304} 305 306TEST_F(InstallShortcutTest, CreateAllShortcutsButQuickLaunchShortcut) { 307 scoped_ptr<installer::MasterPreferences> prefs_no_ql( 308 GetFakeMasterPrefs(false, true, false)); 309 installer::CreateOrUpdateShortcuts( 310 chrome_exe_, *product_, *prefs_no_ql, installer::CURRENT_USER, 311 installer::INSTALL_SHORTCUT_CREATE_ALL); 312 base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_); 313 ASSERT_FALSE(base::PathExists(user_quick_launch_shortcut_)); 314 base::win::ValidateShortcut(user_start_menu_shortcut_, 315 expected_start_menu_properties_); 316} 317 318TEST_F(InstallShortcutTest, ReplaceAll) { 319 base::win::ShortcutProperties dummy_properties; 320 base::FilePath dummy_target; 321 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &dummy_target)); 322 dummy_properties.set_target(dummy_target); 323 dummy_properties.set_working_dir(fake_user_desktop_.path()); 324 dummy_properties.set_arguments(L"--dummy --args"); 325 dummy_properties.set_app_id(L"El.Dummiest"); 326 327 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink( 328 user_desktop_shortcut_, dummy_properties, 329 base::win::SHORTCUT_CREATE_ALWAYS)); 330 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink( 331 user_quick_launch_shortcut_, dummy_properties, 332 base::win::SHORTCUT_CREATE_ALWAYS)); 333 ASSERT_TRUE(base::CreateDirectory(user_start_menu_shortcut_.DirName())); 334 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink( 335 user_start_menu_shortcut_, dummy_properties, 336 base::win::SHORTCUT_CREATE_ALWAYS)); 337 338 installer::CreateOrUpdateShortcuts( 339 chrome_exe_, *product_, *prefs_, installer::CURRENT_USER, 340 installer::INSTALL_SHORTCUT_REPLACE_EXISTING); 341 base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_); 342 base::win::ValidateShortcut(user_quick_launch_shortcut_, 343 expected_properties_); 344 base::win::ValidateShortcut(user_start_menu_shortcut_, 345 expected_start_menu_properties_); 346} 347 348TEST_F(InstallShortcutTest, ReplaceExisting) { 349 base::win::ShortcutProperties dummy_properties; 350 base::FilePath dummy_target; 351 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &dummy_target)); 352 dummy_properties.set_target(dummy_target); 353 dummy_properties.set_working_dir(fake_user_desktop_.path()); 354 dummy_properties.set_arguments(L"--dummy --args"); 355 dummy_properties.set_app_id(L"El.Dummiest"); 356 357 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink( 358 user_desktop_shortcut_, dummy_properties, 359 base::win::SHORTCUT_CREATE_ALWAYS)); 360 ASSERT_TRUE(base::CreateDirectory(user_start_menu_shortcut_.DirName())); 361 362 installer::CreateOrUpdateShortcuts( 363 chrome_exe_, *product_, *prefs_, installer::CURRENT_USER, 364 installer::INSTALL_SHORTCUT_REPLACE_EXISTING); 365 base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_); 366 ASSERT_FALSE(base::PathExists(user_quick_launch_shortcut_)); 367 ASSERT_FALSE(base::PathExists(user_start_menu_shortcut_)); 368} 369 370TEST_F(InstallShortcutTest, CreateIfNoSystemLevelAllSystemShortcutsExist) { 371 base::win::ShortcutProperties dummy_properties; 372 base::FilePath dummy_target; 373 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &dummy_target)); 374 dummy_properties.set_target(dummy_target); 375 376 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink( 377 system_desktop_shortcut_, dummy_properties, 378 base::win::SHORTCUT_CREATE_ALWAYS)); 379 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink( 380 system_quick_launch_shortcut_, dummy_properties, 381 base::win::SHORTCUT_CREATE_ALWAYS)); 382 ASSERT_TRUE(base::CreateDirectory( 383 system_start_menu_shortcut_.DirName())); 384 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink( 385 system_start_menu_shortcut_, dummy_properties, 386 base::win::SHORTCUT_CREATE_ALWAYS)); 387 388 installer::CreateOrUpdateShortcuts( 389 chrome_exe_, *product_, *prefs_, installer::CURRENT_USER, 390 installer::INSTALL_SHORTCUT_CREATE_EACH_IF_NO_SYSTEM_LEVEL); 391 ASSERT_FALSE(base::PathExists(user_desktop_shortcut_)); 392 ASSERT_FALSE(base::PathExists(user_quick_launch_shortcut_)); 393 ASSERT_FALSE(base::PathExists(user_start_menu_shortcut_)); 394} 395 396TEST_F(InstallShortcutTest, CreateIfNoSystemLevelNoSystemShortcutsExist) { 397 installer::CreateOrUpdateShortcuts( 398 chrome_exe_, *product_, *prefs_, installer::CURRENT_USER, 399 installer::INSTALL_SHORTCUT_CREATE_EACH_IF_NO_SYSTEM_LEVEL); 400 base::win::ValidateShortcut(user_desktop_shortcut_, expected_properties_); 401 base::win::ValidateShortcut(user_quick_launch_shortcut_, 402 expected_properties_); 403 base::win::ValidateShortcut(user_start_menu_shortcut_, 404 expected_start_menu_properties_); 405} 406 407TEST_F(InstallShortcutTest, CreateIfNoSystemLevelSomeSystemShortcutsExist) { 408 base::win::ShortcutProperties dummy_properties; 409 base::FilePath dummy_target; 410 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &dummy_target)); 411 dummy_properties.set_target(dummy_target); 412 413 ASSERT_TRUE(base::win::CreateOrUpdateShortcutLink( 414 system_desktop_shortcut_, dummy_properties, 415 base::win::SHORTCUT_CREATE_ALWAYS)); 416 417 installer::CreateOrUpdateShortcuts( 418 chrome_exe_, *product_, *prefs_, installer::CURRENT_USER, 419 installer::INSTALL_SHORTCUT_CREATE_EACH_IF_NO_SYSTEM_LEVEL); 420 ASSERT_FALSE(base::PathExists(user_desktop_shortcut_)); 421 base::win::ValidateShortcut(user_quick_launch_shortcut_, 422 expected_properties_); 423 base::win::ValidateShortcut(user_start_menu_shortcut_, 424 expected_start_menu_properties_); 425} 426 427TEST(EscapeXmlAttributeValueTest, EscapeCrazyValue) { 428 base::string16 val(L"This has 'crazy' \"chars\" && < and > signs."); 429 static const wchar_t kExpectedEscapedVal[] = 430 L"This has 'crazy' \"chars\" && < and > signs."; 431 installer::EscapeXmlAttributeValueInSingleQuotes(&val); 432 ASSERT_STREQ(kExpectedEscapedVal, val.c_str()); 433} 434 435TEST(EscapeXmlAttributeValueTest, DontEscapeNormalValue) { 436 base::string16 val(L"Google Chrome"); 437 static const wchar_t kExpectedEscapedVal[] = L"Google Chrome"; 438 installer::EscapeXmlAttributeValueInSingleQuotes(&val); 439 ASSERT_STREQ(kExpectedEscapedVal, val.c_str()); 440} 441