summaryrefslogtreecommitdiffstats
path: root/chrome/installer/util/package_unittest.cc
blob: 851f1885ff482e5c87506382317646b580baddda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/logging.h"
#include "base/scoped_handle.h"
#include "base/utf_string_conversions.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/master_preferences.h"
#include "chrome/installer/util/package.h"
#include "chrome/installer/util/package_properties.h"
#include "chrome/installer/util/product.h"
#include "chrome/installer/util/product_unittest.h"
#include "chrome/installer/util/util_constants.h"

using base::win::RegKey;
using base::win::ScopedHandle;
using installer::ChromePackageProperties;
using installer::ChromiumPackageProperties;
using installer::Package;
using installer::Product;
using installer::MasterPreferences;

class PackageTest : public TestWithTempDirAndDeleteTempOverrideKeys {
 protected:
};

// Tests a few basic things of the Package class.  Makes sure that the path
// operations are correct
TEST_F(PackageTest, Basic) {
  const bool multi_install = false;
  const bool system_level = true;
  ChromiumPackageProperties properties;
  scoped_refptr<Package> package(new Package(multi_install, system_level,
                                             test_dir_.path(), &properties));
  EXPECT_EQ(test_dir_.path().value(), package->path().value());
  EXPECT_TRUE(package->IsEqual(test_dir_.path()));
  EXPECT_EQ(0U, package->products().size());

  const char kOldVersion[] = "1.2.3.4";
  const char kNewVersion[] = "2.3.4.5";

  scoped_ptr<Version> new_version(Version::GetVersionFromString(kNewVersion));
  scoped_ptr<Version> old_version(Version::GetVersionFromString(kOldVersion));
  ASSERT_TRUE(new_version.get() != NULL);
  ASSERT_TRUE(old_version.get() != NULL);

  FilePath installer_dir(package->GetInstallerDirectory(*new_version.get()));
  EXPECT_FALSE(installer_dir.empty());

  FilePath new_version_dir(package->path().Append(
      UTF8ToWide(new_version->GetString())));
  FilePath old_version_dir(package->path().Append(
      UTF8ToWide(old_version->GetString())));

  EXPECT_FALSE(file_util::PathExists(new_version_dir));
  EXPECT_FALSE(file_util::PathExists(old_version_dir));

  EXPECT_FALSE(file_util::PathExists(installer_dir));
  file_util::CreateDirectory(installer_dir);
  EXPECT_TRUE(file_util::PathExists(new_version_dir));

  file_util::CreateDirectory(old_version_dir);
  EXPECT_TRUE(file_util::PathExists(old_version_dir));

  // Create a fake chrome.dll key file in the old version directory.  This
  // should prevent the old version directory from getting deleted.
  FilePath old_chrome_dll(old_version_dir.Append(installer::kChromeDll));
  EXPECT_FALSE(file_util::PathExists(old_chrome_dll));

  // Hold on to the file exclusively to prevent the directory from
  // being deleted.
  ScopedHandle file(::CreateFile(old_chrome_dll.value().c_str(), GENERIC_READ,
                                 0, NULL, OPEN_ALWAYS, 0, NULL));
  EXPECT_TRUE(file.IsValid());
  EXPECT_TRUE(file_util::PathExists(old_chrome_dll));

  package->RemoveOldVersionDirectories(*new_version.get());
  // The old directory should still exist.
  EXPECT_TRUE(file_util::PathExists(old_version_dir));
  EXPECT_TRUE(file_util::PathExists(new_version_dir));

  // Now close the file handle to make it possible to delete our key file.
  file.Close();

  package->RemoveOldVersionDirectories(*new_version.get());
  // The new directory should still exist.
  EXPECT_TRUE(file_util::PathExists(new_version_dir));

  // Now, the old directory and key file should be gone.
  EXPECT_FALSE(file_util::PathExists(old_chrome_dll));
  EXPECT_FALSE(file_util::PathExists(old_version_dir));
}

TEST_F(PackageTest, WithProduct) {
  const MasterPreferences& prefs = MasterPreferences::ForCurrentProcess();

  // TODO(tommi): We should mock this and use our mocked distribution.
  const bool multi_install = false;
  const bool system_level = true;
  BrowserDistribution* distribution =
      BrowserDistribution::GetSpecificDistribution(
          BrowserDistribution::CHROME_BROWSER, prefs);
  ChromePackageProperties properties;
  scoped_refptr<Package> package(new Package(multi_install, system_level,
                                             test_dir_.path(), &properties));
  scoped_refptr<Product> product(new Product(distribution, package.get()));
  EXPECT_EQ(1U, package->products().size());
  EXPECT_EQ(system_level, package->system_level());

  const char kCurrentVersion[] = "1.2.3.4";
  scoped_ptr<Version> current_version(
      Version::GetVersionFromString(kCurrentVersion));

  HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
  {
    TempRegKeyOverride override(root, L"root_pit");
    RegKey chrome_key(root, distribution->GetVersionKey().c_str(),
                      KEY_ALL_ACCESS);
    EXPECT_TRUE(chrome_key.Valid());
    if (chrome_key.Valid()) {
      chrome_key.WriteValue(google_update::kRegVersionField,
                            UTF8ToWide(current_version->GetString()).c_str());
      // TODO(tommi): Also test for when there exists a new_chrome.exe.
      scoped_ptr<Version> found_version(package->GetCurrentVersion());
      EXPECT_TRUE(found_version.get() != NULL);
      if (found_version.get()) {
        EXPECT_TRUE(current_version->Equals(*found_version));
      }
    }
  }
}

namespace {
bool SetUninstallArguments(HKEY root, BrowserDistribution* dist,
                           const CommandLine& args) {
  RegKey key(root, dist->GetStateKey().c_str(), KEY_ALL_ACCESS);
  return key.WriteValue(installer::kUninstallArgumentsField,
                        args.command_line_string().c_str());
}

bool SetInstalledVersion(HKEY root, BrowserDistribution* dist,
                         const std::wstring& version) {
  RegKey key(root, dist->GetVersionKey().c_str(), KEY_ALL_ACCESS);
  return key.WriteValue(google_update::kRegVersionField, version.c_str());
}
}  // end namespace

TEST_F(PackageTest, Dependency) {
  const bool multi_install = false;
  const bool system_level = true;
  HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
  TempRegKeyOverride override(root, L"root_dep");

  ChromePackageProperties properties;
  scoped_refptr<Package> package(new Package(multi_install, system_level,
                                             test_dir_.path(), &properties));
  EXPECT_EQ(0U, package->GetMultiInstallDependencyCount());

  const MasterPreferences& prefs = MasterPreferences::ForCurrentProcess();

  BrowserDistribution* chrome = BrowserDistribution::GetSpecificDistribution(
      BrowserDistribution::CHROME_BROWSER, prefs);
  BrowserDistribution* cf = BrowserDistribution::GetSpecificDistribution(
      BrowserDistribution::CHROME_FRAME, prefs);

  CommandLine multi_uninstall_cmd(CommandLine::NO_PROGRAM);
  multi_uninstall_cmd.AppendSwitch(installer::switches::kUninstall);
  multi_uninstall_cmd.AppendSwitch(installer::switches::kMultiInstall);

  CommandLine single_uninstall_cmd(CommandLine::NO_PROGRAM);
  single_uninstall_cmd.AppendSwitch(installer::switches::kUninstall);

  // "install" Chrome.
  SetUninstallArguments(root, chrome, multi_uninstall_cmd);
  SetInstalledVersion(root, chrome, L"1.2.3.4");
  EXPECT_EQ(1U, package->GetMultiInstallDependencyCount());

  // "install" Chrome Frame without multi-install.
  SetUninstallArguments(root, cf, single_uninstall_cmd);
  SetInstalledVersion(root, cf, L"1.2.3.4");
  EXPECT_EQ(1U, package->GetMultiInstallDependencyCount());

  // "install" Chrome Frame with multi-install.
  SetUninstallArguments(root, cf, multi_uninstall_cmd);
  EXPECT_EQ(2U, package->GetMultiInstallDependencyCount());
}