File: installer_win_unittest.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (286 lines) | stat: -rw-r--r-- 10,545 bytes parent folder | download | duplicates (3)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/platform_experience/installer/installer_win.h"

#include <shobjidl.h>

#include <wrl/client.h>
#include <wrl/implements.h>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/process/process.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_path_override.h"
#include "base/test/task_environment.h"
#include "base/win/scoped_variant.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/install_static/test/scoped_install_details.h"
#include "chrome/installer/util/util_constants.h"
#include "chrome/updater/app/server/win/updater_legacy_idl.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace platform_experience {

namespace {

// Mock implementation of IAppCommandWeb for testing system-level installs.
class MockAppCommand
    : public Microsoft::WRL::RuntimeClass<
          Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
          IAppCommandWeb> {
 public:
  // IDispatch methods.
  MOCK_METHOD(HRESULT,
              GetTypeInfoCount,
              (UINT * pctinfo),
              (override, Calltype(STDMETHODCALLTYPE)));
  MOCK_METHOD(HRESULT,
              GetTypeInfo,
              (UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo),
              (override, Calltype(STDMETHODCALLTYPE)));
  MOCK_METHOD(HRESULT,
              GetIDsOfNames,
              (REFIID riid,
               LPOLESTR* rgszNames,
               UINT cNames,
               LCID lcid,
               DISPID* rgDispId),
              (override, Calltype(STDMETHODCALLTYPE)));
  MOCK_METHOD(HRESULT,
              Invoke,
              (DISPID dispIdMember,
               REFIID riid,
               LCID lcid,
               WORD wFlags,
               DISPPARAMS* pDispParams,
               VARIANT* pVarResult,
               EXCEPINFO* pExcepInfo,
               UINT* puArgErr),
              (override, Calltype(STDMETHODCALLTYPE)));

  // IAppCommandWeb methods.
  MOCK_METHOD(HRESULT,
              get_status,
              (UINT*),
              (override, Calltype(STDMETHODCALLTYPE)));
  MOCK_METHOD(HRESULT,
              get_exitCode,
              (DWORD*),
              (override, Calltype(STDMETHODCALLTYPE)));
  MOCK_METHOD(HRESULT,
              get_output,
              (BSTR*),
              (override, Calltype(STDMETHODCALLTYPE)));

  MOCK_METHOD(HRESULT,
              execute,
              (VARIANT,
               VARIANT,
               VARIANT,
               VARIANT,
               VARIANT,
               VARIANT,
               VARIANT,
               VARIANT,
               VARIANT),
              (override, Calltype(STDMETHODCALLTYPE)));
};

// Mock implementation of the InstallerLauncherDelegate to intercept external
// calls.
class MockInstallerLauncherDelegate : public InstallerLauncherDelegate {
 public:
  MOCK_METHOD(Microsoft::WRL::ComPtr<IAppCommandWeb>,
              GetUpdaterAppCommand,
              (const std::wstring& command_name),
              (override));
  MOCK_METHOD(base::Process,
              LaunchProcess,
              (const base::CommandLine& cmd_line,
               const base::LaunchOptions& options),
              (override));
};

}  // namespace

class PlatformExperienceInstallerWinTest : public testing::Test {
 protected:
  // Simulates that the Platform Experience Helper is already installed by
  // creating its executable in the expected location.
  void CreateFakeHelperExecutable(bool is_system_install) {
    const int path_key = is_system_install
                             ? static_cast<int>(base::DIR_EXE)
                             : static_cast<int>(chrome::DIR_USER_DATA);
    base::FilePath peh_dir = base::PathService::CheckedGet(path_key).Append(
        L"PlatformExperienceHelper");
    ASSERT_TRUE(base::CreateDirectory(peh_dir));
    base::FilePath peh_exe_path =
        peh_dir.Append(L"platform_experience_helper.exe");
    ASSERT_TRUE(base::WriteFile(peh_exe_path, ""));
  }

  void SetUp() override {
    platform_experience::SetInstallerLauncherDelegateForTesting(
        &mock_delegate_);
  }

  void TearDown() override {
    platform_experience::SetInstallerLauncherDelegateForTesting(nullptr);
  }

  // A helper to run the main function under test with mocks and system-level
  // install settings.
  void RunMaybeInstallForSystem(bool is_system_install) {
    install_static::ScopedInstallDetails scoped_install_details(
        is_system_install);
    MaybeInstallPlatformExperienceHelper();
  }

  base::test::TaskEnvironment task_environment_;
  base::HistogramTester histogram_tester_;
  testing::StrictMock<MockInstallerLauncherDelegate> mock_delegate_;

 private:
  // These members override the respective paths with temporary directories
  // for the duration of the test.
  base::ScopedPathOverride user_data_dir_override_{chrome::DIR_USER_DATA};
  base::ScopedPathOverride exe_dir_override_{base::DIR_EXE};
  base::ScopedPathOverride module_dir_override_{base::DIR_MODULE};
};

// Test that no installation is attempted if the helper already exists.
TEST_F(PlatformExperienceInstallerWinTest,
       HelperAlreadyInstalled_NoActionTaken) {
  CreateFakeHelperExecutable(/*is_system_install=*/false);
  // No expectations on mock_delegate_ are set. If any of its methods are
  // called, the StrictMock will fail the test.
  RunMaybeInstallForSystem(/*is_system_install=*/false);

  // No histograms should be recorded.
  histogram_tester_.ExpectTotalCount(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.User", 0);
  histogram_tester_.ExpectTotalCount(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.System", 0);
}

// Test system install case where the AppCommand cannot be found.
TEST_F(PlatformExperienceInstallerWinTest, SystemInstall_AppCommandNotFound) {
  EXPECT_CALL(mock_delegate_,
              GetUpdaterAppCommand(std::wstring(installer::kCmdInstallPEH)))
      .WillOnce(testing::Return(nullptr));

  RunMaybeInstallForSystem(/*is_system_install=*/true);

  histogram_tester_.ExpectUniqueSample(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.System",
      /*SystemInstallerLaunchStatus::kAppCommandNotFound*/ 1, 1);
}

// Test system install case where executing the AppCommand fails.
TEST_F(PlatformExperienceInstallerWinTest, SystemInstall_AppCommandExecFails) {
  auto mock_app_command = Microsoft::WRL::Make<MockAppCommand>();
  EXPECT_CALL(
      *mock_app_command.Get(),
      execute(testing::_, testing::_, testing::_, testing::_, testing::_,
              testing::_, testing::_, testing::_, testing::_))
      .WillOnce(testing::Return(E_FAIL));

  EXPECT_CALL(mock_delegate_,
              GetUpdaterAppCommand(std::wstring(installer::kCmdInstallPEH)))
      .WillOnce(testing::Return(mock_app_command));

  RunMaybeInstallForSystem(/*is_system_install=*/true);

  histogram_tester_.ExpectUniqueSample(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.System",
      /*SystemInstallerLaunchStatus::kAppCommandExecutionFailed*/ 2, 1);
}

// Test the successful system install case.
TEST_F(PlatformExperienceInstallerWinTest, SystemInstall_Success) {
  auto mock_app_command = Microsoft::WRL::Make<MockAppCommand>();
  EXPECT_CALL(
      *mock_app_command.Get(),
      execute(testing::_, testing::_, testing::_, testing::_, testing::_,
              testing::_, testing::_, testing::_, testing::_))
      .WillOnce(testing::Return(S_OK));

  EXPECT_CALL(mock_delegate_,
              GetUpdaterAppCommand(std::wstring(installer::kCmdInstallPEH)))
      .WillOnce(testing::Return(mock_app_command));

  RunMaybeInstallForSystem(/*is_system_install=*/true);

  histogram_tester_.ExpectUniqueSample(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.System",
      /*SystemInstallerLaunchStatus::kSuccess*/ 0, 1);
}

// Test the successful user-level install case.
TEST_F(PlatformExperienceInstallerWinTest, UserInstall_LaunchSuccess) {
  EXPECT_CALL(mock_delegate_, LaunchProcess(testing::_, testing::_))
      .WillOnce(testing::Return(base::Process::Current()));

  RunMaybeInstallForSystem(/*is_system_install=*/false);

  histogram_tester_.ExpectUniqueSample(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.User",
      /*UserInstallerLaunchStatus::kSuccess*/ 0, 1);
}

// Test user install failure due to ERROR_FILE_NOT_FOUND.
TEST_F(PlatformExperienceInstallerWinTest,
       UserInstall_LaunchFails_FileNotFound) {
  EXPECT_CALL(mock_delegate_, LaunchProcess(testing::_, testing::_))
      .WillOnce(testing::Invoke([](const auto&, const auto&) {
        ::SetLastError(ERROR_FILE_NOT_FOUND);
        return base::Process();  // Return invalid process
      }));

  RunMaybeInstallForSystem(/*is_system_install=*/false);

  histogram_tester_.ExpectUniqueSample(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.User",
      /*UserInstallerLaunchStatus::kFileNotFound*/ 1, 1);
}

// Test user install failure due to ERROR_ACCESS_DENIED.
TEST_F(PlatformExperienceInstallerWinTest,
       UserInstall_LaunchFails_AccessDenied) {
  EXPECT_CALL(mock_delegate_, LaunchProcess(testing::_, testing::_))
      .WillOnce(testing::Invoke([](const auto&, const auto&) {
        ::SetLastError(ERROR_ACCESS_DENIED);
        return base::Process();  // Return invalid process
      }));

  RunMaybeInstallForSystem(/*is_system_install=*/false);

  histogram_tester_.ExpectUniqueSample(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.User",
      /*UserInstallerLaunchStatus::kAccessDenied*/ 2, 1);
}

// Test user install failure due to an error other than file not found or
// access denied.
TEST_F(PlatformExperienceInstallerWinTest,
       UserInstall_LaunchFails_OtherFailure) {
  EXPECT_CALL(mock_delegate_, LaunchProcess(testing::_, testing::_))
      .WillOnce(testing::Invoke([](const auto&, const auto&) {
        ::SetLastError(ERROR_INVALID_FUNCTION);  // An arbitrary error
        return base::Process();                  // Return invalid process
      }));

  RunMaybeInstallForSystem(/*is_system_install=*/false);

  histogram_tester_.ExpectUniqueSample(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.User",
      /*UserInstallerLaunchStatus::kOtherFailure*/ 3, 1);
}

}  // namespace platform_experience