setup_util_unittest.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 "chrome/installer/setup/setup_util_unittest.h"
6
7#include <windows.h>
8
9#include <string>
10
11#include "base/command_line.h"
12#include "base/file_util.h"
13#include "base/files/scoped_temp_dir.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/path_service.h"
16#include "base/process_util.h"
17#include "base/threading/platform_thread.h"
18#include "base/time/time.h"
19#include "base/win/scoped_handle.h"
20#include "base/win/windows_version.h"
21#include "chrome/common/chrome_paths.h"
22#include "chrome/installer/setup/setup_util.h"
23#include "testing/gtest/include/gtest/gtest.h"
24
25namespace {
26
27class SetupUtilTestWithDir : public testing::Test {
28 protected:
29  virtual void SetUp() {
30    ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
31    data_dir_ = data_dir_.AppendASCII("installer");
32    ASSERT_TRUE(file_util::PathExists(data_dir_));
33
34    // Create a temp directory for testing.
35    ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
36  }
37
38  virtual void TearDown() {
39    // Clean up test directory manually so we can fail if it leaks.
40    ASSERT_TRUE(test_dir_.Delete());
41  }
42
43  // The temporary directory used to contain the test operations.
44  base::ScopedTempDir test_dir_;
45
46  // The path to input data used in tests.
47  base::FilePath data_dir_;
48};
49
50// The privilege tested in ScopeTokenPrivilege tests below.
51// Use SE_RESTORE_NAME as it is one of the many privileges that is available,
52// but not enabled by default on processes running at high integrity.
53static const wchar_t kTestedPrivilege[] = SE_RESTORE_NAME;
54
55// Returns true if the current process' token has privilege |privilege_name|
56// enabled.
57bool CurrentProcessHasPrivilege(const wchar_t* privilege_name) {
58  base::win::ScopedHandle token;
59  if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY,
60                          token.Receive())) {
61    ADD_FAILURE();
62    return false;
63  }
64
65  // First get the size of the buffer needed for |privileges| below.
66  DWORD size;
67  EXPECT_FALSE(::GetTokenInformation(token, TokenPrivileges, NULL, 0, &size));
68
69  scoped_ptr<BYTE[]> privileges_bytes(new BYTE[size]);
70  TOKEN_PRIVILEGES* privileges =
71      reinterpret_cast<TOKEN_PRIVILEGES*>(privileges_bytes.get());
72
73  if (!::GetTokenInformation(token, TokenPrivileges, privileges, size, &size)) {
74    ADD_FAILURE();
75    return false;
76  }
77
78  // There is no point getting a buffer to store more than |privilege_name|\0 as
79  // anything longer will obviously not be equal to |privilege_name|.
80  const DWORD desired_size = wcslen(privilege_name);
81  const DWORD buffer_size = desired_size + 1;
82  scoped_ptr<wchar_t[]> name_buffer(new wchar_t[buffer_size]);
83  for (int i = privileges->PrivilegeCount - 1; i >= 0 ; --i) {
84    LUID_AND_ATTRIBUTES& luid_and_att = privileges->Privileges[i];
85    DWORD size = buffer_size;
86    ::LookupPrivilegeName(NULL, &luid_and_att.Luid, name_buffer.get(), &size);
87    if (size == desired_size &&
88        wcscmp(name_buffer.get(), privilege_name) == 0) {
89      return luid_and_att.Attributes == SE_PRIVILEGE_ENABLED;
90    }
91  }
92  return false;
93}
94
95}  // namespace
96
97// Test that we are parsing Chrome version correctly.
98TEST_F(SetupUtilTestWithDir, ApplyDiffPatchTest) {
99  base::FilePath work_dir(test_dir_.path());
100  work_dir = work_dir.AppendASCII("ApplyDiffPatchTest");
101  ASSERT_FALSE(file_util::PathExists(work_dir));
102  EXPECT_TRUE(file_util::CreateDirectory(work_dir));
103  ASSERT_TRUE(file_util::PathExists(work_dir));
104
105  base::FilePath src = data_dir_.AppendASCII("archive1.7z");
106  base::FilePath patch = data_dir_.AppendASCII("archive.diff");
107  base::FilePath dest = work_dir.AppendASCII("archive2.7z");
108  EXPECT_EQ(installer::ApplyDiffPatch(src, patch, dest, NULL), 0);
109  base::FilePath base = data_dir_.AppendASCII("archive2.7z");
110  EXPECT_TRUE(file_util::ContentsEqual(dest, base));
111
112  EXPECT_EQ(installer::ApplyDiffPatch(base::FilePath(), base::FilePath(),
113                                      base::FilePath(), NULL),
114            6);
115}
116
117// Test that we are parsing Chrome version correctly.
118TEST_F(SetupUtilTestWithDir, GetMaxVersionFromArchiveDirTest) {
119  // Create a version dir
120  base::FilePath chrome_dir = test_dir_.path().AppendASCII("1.0.0.0");
121  file_util::CreateDirectory(chrome_dir);
122  ASSERT_TRUE(file_util::PathExists(chrome_dir));
123  scoped_ptr<Version> version(
124      installer::GetMaxVersionFromArchiveDir(test_dir_.path()));
125  ASSERT_EQ(version->GetString(), "1.0.0.0");
126
127  base::Delete(chrome_dir, true);
128  ASSERT_FALSE(file_util::PathExists(chrome_dir));
129  ASSERT_TRUE(installer::GetMaxVersionFromArchiveDir(test_dir_.path()) == NULL);
130
131  chrome_dir = test_dir_.path().AppendASCII("ABC");
132  file_util::CreateDirectory(chrome_dir);
133  ASSERT_TRUE(file_util::PathExists(chrome_dir));
134  ASSERT_TRUE(installer::GetMaxVersionFromArchiveDir(test_dir_.path()) == NULL);
135
136  chrome_dir = test_dir_.path().AppendASCII("2.3.4.5");
137  file_util::CreateDirectory(chrome_dir);
138  ASSERT_TRUE(file_util::PathExists(chrome_dir));
139  version.reset(installer::GetMaxVersionFromArchiveDir(test_dir_.path()));
140  ASSERT_EQ(version->GetString(), "2.3.4.5");
141
142  // Create multiple version dirs, ensure that we select the greatest.
143  chrome_dir = test_dir_.path().AppendASCII("9.9.9.9");
144  file_util::CreateDirectory(chrome_dir);
145  ASSERT_TRUE(file_util::PathExists(chrome_dir));
146  chrome_dir = test_dir_.path().AppendASCII("1.1.1.1");
147  file_util::CreateDirectory(chrome_dir);
148  ASSERT_TRUE(file_util::PathExists(chrome_dir));
149
150  version.reset(installer::GetMaxVersionFromArchiveDir(test_dir_.path()));
151  ASSERT_EQ(version->GetString(), "9.9.9.9");
152}
153
154TEST_F(SetupUtilTestWithDir, DeleteFileFromTempProcess) {
155  base::FilePath test_file;
156  file_util::CreateTemporaryFileInDir(test_dir_.path(), &test_file);
157  ASSERT_TRUE(file_util::PathExists(test_file));
158  file_util::WriteFile(test_file, "foo", 3);
159  EXPECT_TRUE(installer::DeleteFileFromTempProcess(test_file, 0));
160  base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200));
161  EXPECT_FALSE(file_util::PathExists(test_file));
162}
163
164// Note: This test is only valid when run at high integrity (i.e. it will fail
165// at medium integrity).
166TEST(SetupUtilTest, ScopedTokenPrivilegeBasic) {
167  ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege));
168
169  {
170    installer::ScopedTokenPrivilege test_scoped_privilege(kTestedPrivilege);
171    ASSERT_TRUE(test_scoped_privilege.is_enabled());
172    ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege));
173  }
174
175  ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege));
176}
177
178// Note: This test is only valid when run at high integrity (i.e. it will fail
179// at medium integrity).
180TEST(SetupUtilTest, ScopedTokenPrivilegeAlreadyEnabled) {
181  ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege));
182
183  {
184    installer::ScopedTokenPrivilege test_scoped_privilege(kTestedPrivilege);
185    ASSERT_TRUE(test_scoped_privilege.is_enabled());
186    ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege));
187    {
188      installer::ScopedTokenPrivilege dup_scoped_privilege(kTestedPrivilege);
189      ASSERT_TRUE(dup_scoped_privilege.is_enabled());
190      ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege));
191    }
192    ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege));
193  }
194
195  ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege));
196}
197
198const char kAdjustProcessPriority[] = "adjust-process-priority";
199
200PriorityClassChangeResult DoProcessPriorityAdjustment() {
201  return installer::AdjustProcessPriority() ? PCCR_CHANGED : PCCR_UNCHANGED;
202}
203
204namespace {
205
206// A scoper that sets/resets the current process's priority class.
207class ScopedPriorityClass {
208 public:
209  // Applies |priority_class|, returning an instance if a change was made.
210  // Otherwise, returns an empty scoped_ptr.
211  static scoped_ptr<ScopedPriorityClass> Create(DWORD priority_class);
212  ~ScopedPriorityClass();
213
214 private:
215  explicit ScopedPriorityClass(DWORD original_priority_class);
216  DWORD original_priority_class_;
217  DISALLOW_COPY_AND_ASSIGN(ScopedPriorityClass);
218};
219
220scoped_ptr<ScopedPriorityClass> ScopedPriorityClass::Create(
221    DWORD priority_class) {
222  HANDLE this_process = ::GetCurrentProcess();
223  DWORD original_priority_class = ::GetPriorityClass(this_process);
224  EXPECT_NE(0U, original_priority_class);
225  if (original_priority_class && original_priority_class != priority_class) {
226    BOOL result = ::SetPriorityClass(this_process, priority_class);
227    EXPECT_NE(FALSE, result);
228    if (result) {
229      return scoped_ptr<ScopedPriorityClass>(
230          new ScopedPriorityClass(original_priority_class));
231    }
232  }
233  return scoped_ptr<ScopedPriorityClass>();
234}
235
236ScopedPriorityClass::ScopedPriorityClass(DWORD original_priority_class)
237    : original_priority_class_(original_priority_class) {}
238
239ScopedPriorityClass::~ScopedPriorityClass() {
240  BOOL result = ::SetPriorityClass(::GetCurrentProcess(),
241                                   original_priority_class_);
242  EXPECT_NE(FALSE, result);
243}
244
245PriorityClassChangeResult RelaunchAndDoProcessPriorityAdjustment() {
246  CommandLine cmd_line(*CommandLine::ForCurrentProcess());
247  cmd_line.AppendSwitch(kAdjustProcessPriority);
248  base::ProcessHandle process_handle = NULL;
249  int exit_code = 0;
250  if (!base::LaunchProcess(cmd_line, base::LaunchOptions(),
251                           &process_handle)) {
252    ADD_FAILURE() << " to launch subprocess.";
253  } else if (!base::WaitForExitCode(process_handle, &exit_code)) {
254    ADD_FAILURE() << " to wait for subprocess to exit.";
255  } else {
256    return static_cast<PriorityClassChangeResult>(exit_code);
257  }
258  return PCCR_UNKNOWN;
259}
260
261}  // namespace
262
263// Launching a subprocess at normal priority class is a noop.
264TEST(SetupUtilTest, AdjustFromNormalPriority) {
265  ASSERT_EQ(NORMAL_PRIORITY_CLASS, ::GetPriorityClass(::GetCurrentProcess()));
266  EXPECT_EQ(PCCR_UNCHANGED, RelaunchAndDoProcessPriorityAdjustment());
267}
268
269// Launching a subprocess below normal priority class drops it to bg mode for
270// sufficiently recent operating systems.
271TEST(SetupUtilTest, AdjustFromBelowNormalPriority) {
272  scoped_ptr<ScopedPriorityClass> below_normal =
273      ScopedPriorityClass::Create(BELOW_NORMAL_PRIORITY_CLASS);
274  ASSERT_TRUE(below_normal);
275  if (base::win::GetVersion() > base::win::VERSION_SERVER_2003)
276    EXPECT_EQ(PCCR_CHANGED, RelaunchAndDoProcessPriorityAdjustment());
277  else
278    EXPECT_EQ(PCCR_UNCHANGED, RelaunchAndDoProcessPriorityAdjustment());
279}
280