forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_unittest.cc
666 lines (578 loc) · 25.6 KB
/
helper_unittest.cc
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// Copyright 2020 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/installer/util/helper.h"
#include <windows.h>
#include <shlobj.h>
#include <optional>
#include <string_view>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/test/scoped_path_override.h"
#include "base/test/test_reg_util_win.h"
#include "base/values.h"
#include "base/version.h"
#include "base/win/registry.h"
#include "build/build_config.h"
#include "chrome/install_static/install_util.h"
#include "chrome/install_static/test/scoped_install_details.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/initial_preferences.h"
#include "chrome/installer/util/util_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace installer {
namespace {
// A helper that overrides an environment variable for the lifetime of an
// instance.
class ScopedEnvironmentOverride {
public:
ScopedEnvironmentOverride(std::wstring_view name, const wchar_t* new_value)
: name_(name) {
std::array<wchar_t, MAX_PATH> value;
value[0] = L'\0';
DWORD len =
::GetEnvironmentVariableW(name_.c_str(), value.data(), value.size());
if (len > 0 && len < value.size()) {
old_value_.emplace(value.data(), len);
}
::SetEnvironmentVariableW(name_.c_str(), new_value);
}
~ScopedEnvironmentOverride() {
::SetEnvironmentVariableW(name_.c_str(),
old_value_ ? old_value_->c_str() : nullptr);
}
private:
const std::wstring name_;
std::optional<std::wstring> old_value_;
};
} // namespace
class GetInstalledDirectoryTest : public testing::TestWithParam<bool> {
protected:
void SetUp() override {
ASSERT_TRUE(random_.CreateUniqueTempDir());
ASSERT_NO_FATAL_FAILURE(
registry_override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE));
ASSERT_NO_FATAL_FAILURE(
registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER));
}
static bool is_system_level() { return GetParam(); }
base::FilePath random_path() const { return random_.GetPath(); }
static base::win::RegKey GetClientsRegKey() {
return base::win::RegKey(
is_system_level() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
install_static::GetClientsKeyPath().c_str(),
KEY_SET_VALUE | KEY_WOW64_32KEY);
}
static base::win::RegKey GetClientStateRegKey() {
return base::win::RegKey(
is_system_level() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
install_static::GetClientStateKeyPath().c_str(),
KEY_SET_VALUE | KEY_WOW64_32KEY);
}
private:
base::ScopedTempDir random_;
registry_util::RegistryOverrideManager registry_override_manager_;
};
TEST_P(GetInstalledDirectoryTest, NoRegistryValue) {
EXPECT_EQ(GetInstalledDirectory(is_system_level()), base::FilePath());
}
TEST_P(GetInstalledDirectoryTest, RegistryValueSet) {
const base::FilePath install_path =
random_path()
.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir);
ASSERT_TRUE(base::CreateDirectory(install_path));
base::win::RegKey client_state_key(GetClientStateRegKey());
ASSERT_EQ(client_state_key.WriteValue(
kUninstallStringField,
install_path.AppendASCII("1.0.0.0\\Installer\\setup.exe")
.value()
.c_str()),
ERROR_SUCCESS);
base::win::RegKey client_key(GetClientsRegKey());
ASSERT_EQ(client_key.WriteValue(google_update::kRegVersionField, L"1.0.0.0"),
ERROR_SUCCESS);
EXPECT_EQ(GetInstalledDirectory(is_system_level()), install_path);
}
TEST_P(GetInstalledDirectoryTest, NoDirectory) {
base::win::RegKey client_state_key(GetClientStateRegKey());
ASSERT_EQ(client_state_key.WriteValue(
kUninstallStringField,
random_path()
.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir)
.AppendASCII("1.0.0.0\\Installer\\setup.exe")
.value()
.c_str()),
ERROR_SUCCESS);
base::win::RegKey client_key(GetClientsRegKey());
ASSERT_EQ(client_key.WriteValue(google_update::kRegVersionField, L"1.0.0.0"),
ERROR_SUCCESS);
EXPECT_EQ(GetInstalledDirectory(is_system_level()), base::FilePath());
}
TEST_P(GetInstalledDirectoryTest, ReferencesParent) {
const base::FilePath install_path =
random_path()
.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir);
ASSERT_TRUE(base::CreateDirectory(install_path));
base::win::RegKey client_state_key(GetClientStateRegKey());
ASSERT_EQ(client_state_key.WriteValue(
kUninstallStringField,
install_path
.AppendASCII("1.0.0.0\\Installer\\..\\Installer\\setup.exe")
.value()
.c_str()),
ERROR_SUCCESS);
base::win::RegKey client_key(GetClientsRegKey());
ASSERT_EQ(client_key.WriteValue(google_update::kRegVersionField, L"1.0.0.0"),
ERROR_SUCCESS);
EXPECT_EQ(GetInstalledDirectory(is_system_level()), base::FilePath());
}
TEST_P(GetInstalledDirectoryTest, NotAbsolute) {
base::win::RegKey client_state_key(GetClientStateRegKey());
ASSERT_EQ(client_state_key.WriteValue(
kUninstallStringField,
base::FilePath(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir)
.AppendASCII("1.0.0.0\\Installer\\setup.exe")
.value()
.c_str()),
ERROR_SUCCESS);
base::win::RegKey client_key(GetClientsRegKey());
ASSERT_EQ(client_key.WriteValue(google_update::kRegVersionField, L"1.0.0.0"),
ERROR_SUCCESS);
EXPECT_EQ(GetInstalledDirectory(is_system_level()), base::FilePath());
}
TEST_P(GetInstalledDirectoryTest, AtRoot) {
base::win::RegKey client_state_key(GetClientStateRegKey());
ASSERT_EQ(
client_state_key.WriteValue(
kUninstallStringField,
base::FilePath(FILE_PATH_LITERAL("C:\\1.0.0.0\\Installer\\setup.exe"))
.value()
.c_str()),
ERROR_SUCCESS);
base::win::RegKey client_key(GetClientsRegKey());
ASSERT_EQ(client_key.WriteValue(google_update::kRegVersionField, L"1.0.0.0"),
ERROR_SUCCESS);
EXPECT_EQ(GetInstalledDirectory(is_system_level()), base::FilePath());
}
TEST_P(GetInstalledDirectoryTest, RegistryValueSetWrongScope) {
base::win::RegKey client_state_key(GetClientStateRegKey());
ASSERT_EQ(client_state_key.WriteValue(
kUninstallStringField,
random_path()
.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir)
.AppendASCII("1.0.0.0\\Installer\\setup.exe")
.value()
.c_str()),
ERROR_SUCCESS);
base::win::RegKey client_key(GetClientsRegKey());
ASSERT_EQ(client_key.WriteValue(google_update::kRegVersionField, L"1.0.0.0"),
ERROR_SUCCESS);
EXPECT_EQ(GetInstalledDirectory(!is_system_level()), base::FilePath());
}
TEST_P(GetInstalledDirectoryTest, RegistryValueSetNoProductVersion) {
base::win::RegKey client_state_key(GetClientStateRegKey());
ASSERT_EQ(client_state_key.WriteValue(
kUninstallStringField,
random_path()
.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir)
.AppendASCII("1.0.0.0\\Installer\\setup.exe")
.value()
.c_str()),
ERROR_SUCCESS);
EXPECT_EQ(GetInstalledDirectory(is_system_level()), base::FilePath());
}
INSTANTIATE_TEST_SUITE_P(UserLevelTest,
GetInstalledDirectoryTest,
testing::Values(false));
INSTANTIATE_TEST_SUITE_P(SystemLevelTest,
GetInstalledDirectoryTest,
testing::Values(true));
class GetDefaultChromeInstallPathTest : public testing::TestWithParam<bool> {
protected:
void SetUp() override {
ASSERT_TRUE(program_files_.CreateUniqueTempDir());
ASSERT_TRUE(program_files_x86_.CreateUniqueTempDir());
ASSERT_TRUE(local_app_data_.CreateUniqueTempDir());
program_files_override_.emplace(base::DIR_PROGRAM_FILES,
program_files_path());
program_files_x86_override_.emplace(base::DIR_PROGRAM_FILESX86,
program_files_x86_path());
local_data_app_override_.emplace(base::DIR_LOCAL_APP_DATA,
local_app_data_path());
}
static bool is_system_level() { return GetParam(); }
base::FilePath program_files_path() const { return program_files_.GetPath(); }
base::FilePath program_files_x86_path() const {
return program_files_x86_.GetPath();
}
base::FilePath local_app_data_path() const {
return local_app_data_.GetPath();
}
base::FilePath GetExpectedPath(bool system_level) const {
auto path = system_level ? program_files_path() : local_app_data_path();
return path.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir);
}
private:
base::ScopedTempDir program_files_;
base::ScopedTempDir program_files_x86_;
base::ScopedTempDir local_app_data_;
std::optional<base::ScopedPathOverride> program_files_override_;
std::optional<base::ScopedPathOverride> program_files_x86_override_;
std::optional<base::ScopedPathOverride> local_data_app_override_;
};
// Tests that the PathService is used to get the default install path.
TEST_P(GetDefaultChromeInstallPathTest, PathService) {
EXPECT_EQ(GetDefaultChromeInstallPath(is_system_level()),
GetExpectedPath(is_system_level()));
}
// Tests that the environment variable fallback is used if the PathService
// returns a path that doesn't exist.
TEST_P(GetDefaultChromeInstallPathTest, EnvironmentFallback) {
// Override the relevant paths with directories that do not exist so that the
// env var fallback is reached.
base::ScopedPathOverride bad_program_files_override(
base::DIR_PROGRAM_FILES, program_files_path().AppendASCII("doesnotexist"),
/*is_absolute=*/true, /*create=*/false);
base::ScopedPathOverride bad_program_files_x86_override(
base::DIR_PROGRAM_FILESX86,
program_files_x86_path().AppendASCII("doesnotexist"),
/*is_absolute=*/true, /*create=*/false);
base::ScopedPathOverride bad_local_ap_data_override(
base::DIR_LOCAL_APP_DATA,
local_app_data_path().AppendASCII("doesnotexist"),
/*is_absolute=*/true, /*create=*/false);
ScopedEnvironmentOverride program_files_env(
L"PROGRAMFILES", program_files_path().value().c_str());
ScopedEnvironmentOverride program_files_x86_env(
L"PROGRAMFILES(X86)", program_files_x86_path().value().c_str());
ScopedEnvironmentOverride local_app_data_env(
L"LOCALAPPDATA", local_app_data_path().value().c_str());
EXPECT_EQ(GetDefaultChromeInstallPath(is_system_level()),
GetExpectedPath(is_system_level()));
}
INSTANTIATE_TEST_SUITE_P(UserLevelTest,
GetDefaultChromeInstallPathTest,
testing::Values(false));
INSTANTIATE_TEST_SUITE_P(SystemLevelTest,
GetDefaultChromeInstallPathTest,
testing::Values(true));
struct Params {
Params(bool system_level, std::optional<int> target_dir_key)
: system_level(system_level), target_dir_key(target_dir_key) {}
bool system_level;
std::optional<int> target_dir_key;
};
// Tests GetChromeInstallPath with a params object that contains a boolean
// |system_level| which is |true| if the test must use system-level values or
// |false| it the test must use user-level values, and a |target_dir| path in
// which the installation should be made.
class GetChromeInstallPathWithPrefsTest
: public testing::TestWithParam<Params> {
protected:
void SetUp() override {
ASSERT_TRUE(program_files_.CreateUniqueTempDir());
ASSERT_TRUE(program_files_x86_.CreateUniqueTempDir());
ASSERT_TRUE(random_.CreateUniqueTempDir());
ASSERT_TRUE(local_app_data_.CreateUniqueTempDir());
ASSERT_NO_FATAL_FAILURE(
registry_override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE));
ASSERT_NO_FATAL_FAILURE(
registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER));
program_files_override_.emplace(base::DIR_PROGRAM_FILES,
program_files_path());
program_files_x86_override_.emplace(base::DIR_PROGRAM_FILESX86,
program_files_x86_path());
local_data_app_override_.emplace(base::DIR_LOCAL_APP_DATA,
local_app_data_path());
}
base::FilePath random_path() const { return random_.GetPath(); }
base::FilePath program_files_path() const { return program_files_.GetPath(); }
base::FilePath program_files_x86_path() const {
return program_files_x86_.GetPath();
}
base::FilePath local_app_data_path() const {
return local_app_data_.GetPath();
}
static bool is_system_level() { return GetParam().system_level; }
base::FilePath GetExpectedPath(bool system_level) {
auto path = system_level ? program_files_path() : local_app_data_path();
return path.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir);
}
static base::win::RegKey GetClientsRegKey() {
return base::win::RegKey(
is_system_level() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
install_static::GetClientsKeyPath().c_str(),
KEY_SET_VALUE | KEY_WOW64_32KEY);
}
static base::win::RegKey GetClientStateRegKey() {
return base::win::RegKey(
is_system_level() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
install_static::GetClientStateKeyPath().c_str(),
KEY_SET_VALUE | KEY_WOW64_32KEY);
}
base::FilePath GetExpectedPathForSetup(bool system_level,
base::FilePath target_dir) {
if (system_level && !target_dir.empty() &&
(target_dir == program_files_path() ||
target_dir == program_files_x86_path())) {
return target_dir.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir);
}
return GetExpectedPath(system_level);
}
static base::FilePath target_dir() {
base::FilePath result;
if (GetParam().target_dir_key.has_value())
base::PathService::Get(GetParam().target_dir_key.value(), &result);
return result;
}
static base::Value::Dict prefs_json() {
base::FilePath result;
if (GetParam().target_dir_key.has_value())
base::PathService::Get(GetParam().target_dir_key.value(), &result);
base::Value::Dict distribution;
distribution.SetByDottedPath("distribution.program_files_dir",
result.AsUTF8Unsafe());
return distribution;
}
private:
base::ScopedTempDir program_files_;
base::ScopedTempDir program_files_x86_;
base::ScopedTempDir random_;
base::ScopedTempDir local_app_data_;
registry_util::RegistryOverrideManager registry_override_manager_;
std::optional<base::ScopedPathOverride> program_files_override_;
std::optional<base::ScopedPathOverride> program_files_x86_override_;
std::optional<base::ScopedPathOverride> local_data_app_override_;
};
TEST_P(GetChromeInstallPathWithPrefsTest, NoRegistryValue) {
EXPECT_EQ(GetChromeInstallPathWithPrefs(is_system_level(),
InitialPreferences(prefs_json())),
GetExpectedPathForSetup(is_system_level(), target_dir()));
}
TEST_P(GetChromeInstallPathWithPrefsTest, RegistryValueSet) {
const base::FilePath install_path =
random_path()
.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir);
ASSERT_TRUE(base::CreateDirectory(install_path));
base::win::RegKey client_state_key(GetClientStateRegKey());
ASSERT_EQ(client_state_key.WriteValue(
kUninstallStringField,
install_path.AppendASCII("1.0.0.0\\Installer\\setup.exe")
.value()
.c_str()),
ERROR_SUCCESS);
base::win::RegKey client_key(GetClientsRegKey());
ASSERT_EQ(client_key.WriteValue(google_update::kRegVersionField, L"1.0.0.0"),
ERROR_SUCCESS);
EXPECT_EQ(GetChromeInstallPathWithPrefs(is_system_level(),
InitialPreferences(prefs_json())),
install_path);
}
TEST_P(GetChromeInstallPathWithPrefsTest, RegistryValueSetWrongScope) {
base::win::RegKey client_state_key(GetClientStateRegKey());
ASSERT_EQ(client_state_key.WriteValue(
kUninstallStringField,
random_path()
.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir)
.AppendASCII("1.0.0.0\\Installer\\setup.exe")
.value()
.c_str()),
ERROR_SUCCESS);
base::win::RegKey client_key(GetClientsRegKey());
ASSERT_EQ(client_key.WriteValue(google_update::kRegVersionField, L"1.0.0.0"),
ERROR_SUCCESS);
EXPECT_EQ(GetChromeInstallPathWithPrefs(!is_system_level(),
InitialPreferences(prefs_json())),
GetExpectedPathForSetup(!is_system_level(), target_dir()));
}
TEST_P(GetChromeInstallPathWithPrefsTest, RegistryValueSetNoProductVersion) {
base::win::RegKey client_state_key(GetClientStateRegKey());
ASSERT_EQ(client_state_key.WriteValue(
kUninstallStringField,
random_path()
.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir)
.AppendASCII("1.0.0.0\\Installer\\setup.exe")
.value()
.c_str()),
ERROR_SUCCESS);
EXPECT_EQ(GetChromeInstallPathWithPrefs(is_system_level(),
InitialPreferences(prefs_json())),
GetExpectedPathForSetup(is_system_level(), target_dir()));
}
INSTANTIATE_TEST_SUITE_P(
UserLevelX86SetupTest,
GetChromeInstallPathWithPrefsTest,
testing::Values<Params>(Params(false, base::DIR_PROGRAM_FILESX86)));
INSTANTIATE_TEST_SUITE_P(
UserLevelX64SetupTest,
GetChromeInstallPathWithPrefsTest,
testing::Values<Params>(Params(false, base::DIR_PROGRAM_FILES)));
INSTANTIATE_TEST_SUITE_P(UserLevelUnsupportedPathSetupTest,
GetChromeInstallPathWithPrefsTest,
testing::Values<Params>(Params(false,
base::DIR_HOME)));
INSTANTIATE_TEST_SUITE_P(UserLevelEmptyPathSetupTest,
GetChromeInstallPathWithPrefsTest,
testing::Values<Params>(Params(false, std::nullopt)));
INSTANTIATE_TEST_SUITE_P(
MachineLevelX86SetupTest,
GetChromeInstallPathWithPrefsTest,
testing::Values<Params>(Params(true, base::DIR_PROGRAM_FILESX86)));
INSTANTIATE_TEST_SUITE_P(
MachineLevelX64SetupTest,
GetChromeInstallPathWithPrefsTest,
testing::Values<Params>(Params(true, base::DIR_PROGRAM_FILES)));
INSTANTIATE_TEST_SUITE_P(MachineLevelUnsupportedPathSetupTest,
GetChromeInstallPathWithPrefsTest,
testing::Values<Params>(Params(true, base::DIR_HOME)));
INSTANTIATE_TEST_SUITE_P(MachineLevelEmptyPathSetupTest,
GetChromeInstallPathWithPrefsTest,
testing::Values<Params>(Params(true, std::nullopt)));
class FindInstallPathTest
: public ::testing::TestWithParam<std::tuple<bool, int>> {
protected:
void SetUp() override {
ASSERT_TRUE(program_files_.CreateUniqueTempDir());
ASSERT_TRUE(program_files_x86_.CreateUniqueTempDir());
ASSERT_TRUE(program_files_6432_.CreateUniqueTempDir());
ASSERT_TRUE(local_app_data_.CreateUniqueTempDir());
program_files_override_.emplace(base::DIR_PROGRAM_FILES,
program_files_path());
program_files_x86_override_.emplace(base::DIR_PROGRAM_FILESX86,
program_files_x86_path());
program_files_6432_override_.emplace(base::DIR_PROGRAM_FILES6432,
program_files_6432_path());
local_data_app_override_.emplace(base::DIR_LOCAL_APP_DATA,
local_app_data_path());
}
static bool is_system_level() { return std::get<0>(GetParam()); }
static int target_path_key() { return std::get<1>(GetParam()); }
base::FilePath program_files_path() const { return program_files_.GetPath(); }
base::FilePath program_files_x86_path() const {
return program_files_x86_.GetPath();
}
base::FilePath program_files_6432_path() const {
return program_files_6432_.GetPath();
}
base::FilePath local_app_data_path() const {
return local_app_data_.GetPath();
}
private:
base::ScopedTempDir program_files_;
base::ScopedTempDir program_files_x86_;
base::ScopedTempDir program_files_6432_;
base::ScopedTempDir local_app_data_;
std::optional<base::ScopedPathOverride> program_files_override_;
std::optional<base::ScopedPathOverride> program_files_x86_override_;
std::optional<base::ScopedPathOverride> program_files_6432_override_;
std::optional<base::ScopedPathOverride> local_data_app_override_;
};
// Tests that FindInstallPath returns an empty string when no install directory
// is present.
TEST_P(FindInstallPathTest, NoDirectory) {
EXPECT_EQ(FindInstallPath(is_system_level(), base::Version("1.0.0.0")),
base::FilePath());
}
// Tests that FindInstallPath returns the path to the installed version
// directory.
TEST_P(FindInstallPathTest, Installed) {
const auto path = base::PathService::CheckedGet(target_path_key())
.Append(install_static::GetChromeInstallSubDirectory())
.Append(kInstallBinaryDir)
.Append(L"1.0.0.0");
ASSERT_TRUE(base::CreateDirectoryAndGetError(path, nullptr));
EXPECT_EQ(FindInstallPath(is_system_level(), base::Version("1.0.0.0")), path);
}
INSTANTIATE_TEST_SUITE_P(
UserLevelTest,
FindInstallPathTest,
testing::Values(std::make_tuple(false, base::DIR_LOCAL_APP_DATA)));
INSTANTIATE_TEST_SUITE_P(
SystemLevelTest,
FindInstallPathTest,
testing::Values(std::make_tuple(true, base::DIR_PROGRAM_FILES),
#if defined(ARCH_CPU_64_BITS)
std::make_tuple(true, base::DIR_PROGRAM_FILESX86)
#else
std::make_tuple(true, base::DIR_PROGRAM_FILES6432)
#endif
));
class IsCurrentProcessInstalledTest : public testing::TestWithParam<bool> {
protected:
void SetUp() override {
if (GetParam() && !::IsUserAnAdmin()) {
GTEST_SKIP() << "Test requires admin rights";
}
ASSERT_NO_FATAL_FAILURE(registry_override_manager_.OverrideRegistry(
GetParam() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER));
}
void SetInstalledDirectory(const base::FilePath& installed_dir) {
const HKEY kRoot = GetParam() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
base::win::RegKey clients_key(kRoot,
install_static::GetClientsKeyPath().c_str(),
KEY_SET_VALUE | KEY_WOW64_32KEY);
ASSERT_EQ(
clients_key.WriteValue(google_update::kRegVersionField, L"1.0.0.0"),
ERROR_SUCCESS);
base::win::RegKey client_state_key(
kRoot, install_static::GetClientStateKeyPath().c_str(),
KEY_SET_VALUE | KEY_WOW64_32KEY);
ASSERT_EQ(client_state_key.WriteValue(
kUninstallStringField,
installed_dir.AppendASCII("1.0.0.0\\Installer\\setup.exe")
.value()
.c_str()),
ERROR_SUCCESS);
}
private:
install_static::ScopedInstallDetails install_details_{
/*system_level=*/GetParam()};
registry_util::RegistryOverrideManager registry_override_manager_;
};
// Tests that IsCurrentProcessInstalled returns false when the product is not
// registered with the updater.
TEST_P(IsCurrentProcessInstalledTest, NotInstalled) {
ASSERT_FALSE(IsCurrentProcessInstalled());
}
// Tests that IsCurrentProcessInstalled returns false when the product is
// registered with the updater but the current exe resides elsewhere.
TEST_P(IsCurrentProcessInstalledTest, InstalledNotInDir) {
ASSERT_NO_FATAL_FAILURE(
SetInstalledDirectory(base::PathService::CheckedGet(base::DIR_TEMP)));
ASSERT_FALSE(IsCurrentProcessInstalled());
}
// Tests that IsCurrentProcessInstalled returns true when the product is
// registered with the updater and the current exe resides in the same dir.
TEST_P(IsCurrentProcessInstalledTest, InstalledInDir) {
ASSERT_NO_FATAL_FAILURE(
SetInstalledDirectory(base::PathService::CheckedGet(base::DIR_EXE)));
ASSERT_TRUE(IsCurrentProcessInstalled());
}
// Tests that IsCurrentProcessInstalled returns true when the product is
// registered with the updater and the current exe resides in a subdir.
TEST_P(IsCurrentProcessInstalledTest, InstalledInSubDir) {
ASSERT_NO_FATAL_FAILURE(SetInstalledDirectory(
base::PathService::CheckedGet(base::DIR_EXE).DirName()));
ASSERT_TRUE(IsCurrentProcessInstalled());
}
INSTANTIATE_TEST_SUITE_P(UserLevelTest,
IsCurrentProcessInstalledTest,
testing::Values(false));
INSTANTIATE_TEST_SUITE_P(SystemLevelTest,
IsCurrentProcessInstalledTest,
testing::Values(true));
} // namespace installer