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