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
base / path_service_unittest.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/path_service.h"
#include "base/base_paths.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/scoped_environment_variable_override.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gtest_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#if BUILDFLAG(IS_WIN)
#include <shlobj.h>
#include "base/win/windows_version.h"
#endif
#if BUILDFLAG(IS_APPLE)
#include "base/apple/bundle_locations.h"
#endif
namespace base {
namespace {
#if BUILDFLAG(IS_ANDROID)
// Defined in
// //base/test/android/javatests/src/org/chromium/base/test/util/UrlUtils.java.
constexpr char kExpectedChromiumTestsRoot[] =
"/storage/emulated/0/chromium_tests_root";
#endif
// Returns true if PathService::Get returns true and sets the path parameter
// to non-empty for the given PathService key enumeration value.
bool ReturnsValidPath(int key) {
FilePath path;
bool result = PathService::Get(key, &path);
// Some paths might not exist on some platforms in which case confirming
// |result| is true and !path.empty() is the best we can do.
bool check_path_exists = true;
#if BUILDFLAG(IS_POSIX)
// If chromium has never been started on this account, the cache path may not
// exist.
if (key == DIR_CACHE)
check_path_exists = false;
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop),
// but it doesn't exist.
if (key == DIR_USER_DESKTOP)
check_path_exists = false;
#endif
#if BUILDFLAG(IS_WIN)
if (key == DIR_TASKBAR_PINS)
check_path_exists = false;
#endif
#if BUILDFLAG(IS_MAC)
if (key != DIR_EXE && key != DIR_MODULE && key != FILE_EXE &&
key != FILE_MODULE) {
if (path.ReferencesParent()) {
LOG(INFO) << "Path (" << path << ") references parent.";
return false;
}
}
#else
if (path.ReferencesParent()) {
LOG(INFO) << "Path (" << path << ") references parent.";
return false;
}
#endif // BUILDFLAG(IS_MAC)
if (!result) {
LOG(INFO) << "PathService::Get() returned false.";
return false;
}
if (path.empty()) {
LOG(INFO) << "PathService::Get() returned an empty path.";
return false;
}
if (check_path_exists && !PathExists(path)) {
LOG(INFO) << "Path (" << path << ") does not exist.";
return false;
}
return true;
}
// Returns true if PathService::Get returns false and path parameter is empty
// for the given PathService key enumeration value. Used to test path keys that
// are not supported on the platform or on some versions of Windows.
bool ReturnsInvalidPath(int key) {
FilePath path;
bool result = PathService::Get(key, &path);
return !result && path.empty();
}
} // namespace
// On the Mac this winds up using some autoreleased objects, so we need to
// be a PlatformTest.
typedef PlatformTest PathServiceTest;
// Test that all PathService::Get calls return a value and a true result
// in the development environment. (This test was created because a few
// later changes to Get broke the semantics of the function and yielded the
// correct value while returning false.)
// If this test fails for specific value(s) on a specific platform, consider not
// defining the enum value on that platform rather than skipping or expecting
// failure for the value(s) on that platform in this test.
TEST_F(PathServiceTest, Get) {
// Contains keys that are defined but not supported on the platform.
#if BUILDFLAG(IS_ANDROID)
// The following keys are not intended to be implemented on Android (see
// crbug.com/1257402). Current implementation is described before each key.
// TODO(crbug.com/40796336): Remove the definition of these keys on Android
// or at least fix the behavior of DIR_HOME.
constexpr std::array kUnsupportedKeys = {
// Though DIR_HOME is not intended to be supported, PathProviderPosix
// handles it and returns true. Thus, it is NOT included in the array.
/* DIR_HOME, */
// PathProviderAndroid and PathProviderPosix both return false.
FILE_MODULE,
// PathProviderPosix handles it but fails at some point.
DIR_USER_DESKTOP};
#elif BUILDFLAG(IS_FUCHSIA)
constexpr std::array kUnsupportedKeys = {
// TODO(crbug.com/42050322): Implement DIR_USER_DESKTOP.
DIR_USER_DESKTOP};
#else
constexpr std::array<BasePathKey, 0> kUnsupportedKeys = {};
#endif // BUILDFLAG(IS_ANDROID)
for (int key = PATH_START + 1; key < PATH_END; ++key) {
EXPECT_PRED1(Contains(kUnsupportedKeys, key) ? &ReturnsInvalidPath
: &ReturnsValidPath,
key);
}
#if BUILDFLAG(IS_WIN)
for (int key = PATH_WIN_START + 1; key < PATH_WIN_END; ++key) {
if (key == DIR_SYSTEM_TEMP) {
EXPECT_PRED1(::IsUserAnAdmin() ? &ReturnsValidPath : &ReturnsInvalidPath,
key);
} else {
EXPECT_PRED1(ReturnsValidPath, key);
}
}
#elif BUILDFLAG(IS_MAC)
for (int key = PATH_MAC_START + 1; key < PATH_MAC_END; ++key) {
EXPECT_PRED1(ReturnsValidPath, key);
}
#elif BUILDFLAG(IS_IOS)
for (int key = PATH_IOS_START + 1; key < PATH_IOS_END; ++key) {
EXPECT_PRED1(ReturnsValidPath, key);
}
#elif BUILDFLAG(IS_ANDROID)
for (int key = PATH_ANDROID_START + 1; key < PATH_ANDROID_END;
++key) {
EXPECT_PRED1(ReturnsValidPath, key);
}
#elif BUILDFLAG(IS_POSIX)
for (int key = PATH_POSIX_START + 1; key < PATH_POSIX_END;
++key) {
EXPECT_PRED1(ReturnsValidPath, key);
}
#endif // BUILDFLAG(IS_WIN)
}
// Tests that CheckedGet returns the same path as Get.
TEST_F(PathServiceTest, CheckedGet) {
constexpr int kKey = DIR_CURRENT;
FilePath path;
ASSERT_TRUE(PathService::Get(kKey, &path));
EXPECT_EQ(path, PathService::CheckedGet(kKey));
}
#if defined(GTEST_HAS_DEATH_TEST)
// Tests that CheckedGet CHECKs on failure.
TEST_F(PathServiceTest, CheckedGetFailure) {
constexpr int kBadKey = PATH_END;
FilePath path;
EXPECT_FALSE(PathService::Get(kBadKey, &path));
EXPECT_DEATH(PathService::CheckedGet(kBadKey), "Failed to get the path");
}
#endif // defined(GTEST_HAS_DEATH_TEST)
// Test that all versions of the Override function of PathService do what they
// are supposed to do.
TEST_F(PathServiceTest, Override) {
int my_special_key = 666;
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath fake_cache_dir(temp_dir.GetPath().AppendASCII("cache"));
// PathService::Override should always create the path provided if it doesn't
// exist.
EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir));
EXPECT_TRUE(PathExists(fake_cache_dir));
FilePath fake_cache_dir2(temp_dir.GetPath().AppendASCII("cache2"));
// PathService::OverrideAndCreateIfNeeded should obey the |create| parameter.
PathService::OverrideAndCreateIfNeeded(my_special_key,
fake_cache_dir2,
false,
false);
EXPECT_FALSE(PathExists(fake_cache_dir2));
EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
fake_cache_dir2,
false,
true));
EXPECT_TRUE(PathExists(fake_cache_dir2));
#if BUILDFLAG(IS_POSIX)
FilePath non_existent(
MakeAbsoluteFilePath(temp_dir.GetPath()).AppendASCII("non_existent"));
EXPECT_TRUE(non_existent.IsAbsolute());
EXPECT_FALSE(PathExists(non_existent));
#if !BUILDFLAG(IS_ANDROID)
// This fails because MakeAbsoluteFilePath fails for non-existent files.
// Earlier versions of Bionic libc don't fail for non-existent files, so
// skip this check on Android.
EXPECT_FALSE(PathService::OverrideAndCreateIfNeeded(my_special_key,
non_existent,
false,
false));
#endif // !BUILDFLAG(IS_ANDROID)
// This works because indicating that |non_existent| is absolute skips the
// internal MakeAbsoluteFilePath call.
EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
non_existent,
true,
false));
// Check that the path has been overridden and no directory was created.
EXPECT_FALSE(PathExists(non_existent));
FilePath path;
EXPECT_TRUE(PathService::Get(my_special_key, &path));
EXPECT_EQ(non_existent, path);
#endif // BUILDFLAG(IS_POSIX)
}
// Check if multiple overrides can co-exist.
TEST_F(PathServiceTest, OverrideMultiple) {
int my_special_key = 666;
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath fake_cache_dir1(temp_dir.GetPath().AppendASCII("1"));
EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1));
EXPECT_TRUE(PathExists(fake_cache_dir1));
ASSERT_TRUE(WriteFile(fake_cache_dir1.AppendASCII("t1"), "."));
FilePath fake_cache_dir2(temp_dir.GetPath().AppendASCII("2"));
EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2));
EXPECT_TRUE(PathExists(fake_cache_dir2));
ASSERT_TRUE(WriteFile(fake_cache_dir2.AppendASCII("t2"), "."));
FilePath result;
EXPECT_TRUE(PathService::Get(my_special_key, &result));
// Override might have changed the path representation but our test file
// should be still there.
EXPECT_TRUE(PathExists(result.AppendASCII("t1")));
EXPECT_TRUE(PathService::Get(my_special_key + 1, &result));
EXPECT_TRUE(PathExists(result.AppendASCII("t2")));
}
TEST_F(PathServiceTest, RemoveOverride) {
// Before we start the test we have to call RemoveOverride at least once to
// clear any overrides that might have been left from other tests.
PathService::RemoveOverrideForTests(DIR_TEMP);
FilePath original_user_data_dir;
EXPECT_TRUE(PathService::Get(DIR_TEMP, &original_user_data_dir));
EXPECT_FALSE(PathService::RemoveOverrideForTests(DIR_TEMP));
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
EXPECT_TRUE(PathService::Override(DIR_TEMP, temp_dir.GetPath()));
FilePath new_user_data_dir;
EXPECT_TRUE(PathService::Get(DIR_TEMP, &new_user_data_dir));
EXPECT_NE(original_user_data_dir, new_user_data_dir);
EXPECT_TRUE(PathService::RemoveOverrideForTests(DIR_TEMP));
EXPECT_TRUE(PathService::Get(DIR_TEMP, &new_user_data_dir));
EXPECT_EQ(original_user_data_dir, new_user_data_dir);
}
#if BUILDFLAG(IS_WIN)
TEST_F(PathServiceTest, GetProgramFiles) {
FilePath programfiles_dir;
#if defined(_WIN64)
// 64-bit on 64-bit.
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files (x86)"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
#else
if (base::win::OSInfo::GetInstance()->IsWowX86OnAMD64() ||
base::win::OSInfo::GetInstance()->IsWowX86OnARM64()) {
// 32-bit on 64-bit.
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files (x86)"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files (x86)"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
} else {
// 32-bit on 32-bit.
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
&programfiles_dir));
EXPECT_EQ(programfiles_dir.value(),
FILE_PATH_LITERAL("C:\\Program Files"));
}
#endif // defined(_WIN64)
}
TEST_F(PathServiceTest, GetSystemTemp) {
FilePath secure_system_temp;
EXPECT_EQ(PathService::Get(DIR_SYSTEM_TEMP, &secure_system_temp),
::IsUserAnAdmin());
if (!secure_system_temp.empty()) {
FilePath dir_windows;
ASSERT_TRUE(PathService::Get(DIR_WINDOWS, &dir_windows));
FilePath dir_program_files;
ASSERT_TRUE(PathService::Get(DIR_PROGRAM_FILES, &dir_program_files));
ASSERT_TRUE((dir_windows.AppendASCII("SystemTemp") == secure_system_temp) ||
(dir_program_files == secure_system_temp));
}
}
#endif // BUILDFLAG(IS_WIN)
// Tests that DIR_ASSETS is
// - the package root on Fuchsia,
// - overridden in tests by test_support_android.cc,
// - equals to base::apple::FrameworkBundlePath() on iOS,
// - a sub-directory of base::apple::FrameworkBundlePath() on iOS catalyst,
// - equals to DIR_MODULE otherwise.
TEST_F(PathServiceTest, DIR_ASSETS) {
FilePath path;
ASSERT_TRUE(PathService::Get(DIR_ASSETS, &path));
#if BUILDFLAG(IS_FUCHSIA)
EXPECT_EQ(path.value(), "/pkg");
#elif BUILDFLAG(IS_ANDROID)
// This key is overridden in //base/test/test_support_android.cc.
EXPECT_EQ(path.value(), kExpectedChromiumTestsRoot);
#elif BUILDFLAG(IS_IOS_MACCATALYST)
EXPECT_TRUE(base::apple::FrameworkBundlePath().IsParent(path));
#elif BUILDFLAG(IS_IOS)
EXPECT_EQ(path, base::apple::FrameworkBundlePath());
#else
EXPECT_EQ(path, PathService::CheckedGet(DIR_MODULE));
#endif
}
// DIR_OUT_TEST_DATA_ROOT is DIR_MODULE except on Fuchsia where it is the
// package root, on ios where it is the resources directory and on Android
// where it is overridden in tests by test_support_android.cc.
TEST_F(PathServiceTest, DIR_OUT_TEST_DATA_ROOT) {
FilePath path;
ASSERT_TRUE(PathService::Get(DIR_OUT_TEST_DATA_ROOT, &path));
#if BUILDFLAG(IS_FUCHSIA)
EXPECT_EQ(path.value(), "/pkg");
#elif BUILDFLAG(IS_ANDROID)
// This key is overridden in //base/test/test_support_android.cc.
EXPECT_EQ(path.value(), kExpectedChromiumTestsRoot);
#elif BUILDFLAG(IS_IOS)
// On iOS, build output files are moved to the resources directory.
EXPECT_EQ(path, base::apple::FrameworkBundlePath());
#else
// On other platforms all build output is in the same directory,
// so DIR_OUT_TEST_DATA_ROOT should match DIR_MODULE.
EXPECT_EQ(path, PathService::CheckedGet(DIR_MODULE));
#endif
}
// Test that DIR_GEN_TEST_DATA_ROOT contains dummy_generated.txt which is
// generated for this test.
TEST_F(PathServiceTest, DIR_GEN_TEST_DATA_ROOT) {
FilePath path;
ASSERT_TRUE(PathService::Get(DIR_GEN_TEST_DATA_ROOT, &path));
EXPECT_TRUE(base::PathExists(
path.Append(FILE_PATH_LITERAL("base/generated_file_for_test.txt"))));
}
#if ((BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && \
!BUILDFLAG(IS_ANDROID)) || \
BUILDFLAG(IS_WIN))
// Test that CR_SOURCE_ROOT is being used when set.
// By default on those platforms, this directory is set to two directories up
// the current executable directory ("../../").
TEST_F(PathServiceTest, SetTestDataRootAsAbsolutePath) {
// This is needed because on some platform `DIR_SRC_TEST_DATA_ROOT` can be
// cached before reaching this function.
PathService::DisableCache();
base::ScopedTempDir tempdir;
ASSERT_TRUE(tempdir.CreateUniqueTempDir());
#if BUILDFLAG(IS_WIN)
auto scoped_env = base::ScopedEnvironmentVariableOverride(
"CR_SOURCE_ROOT", base::WideToUTF8(tempdir.GetPath().value()));
#else
auto scoped_env = base::ScopedEnvironmentVariableOverride(
"CR_SOURCE_ROOT", tempdir.GetPath().value());
#endif
base::FilePath test_data_root;
ASSERT_TRUE(PathService::Get(DIR_SRC_TEST_DATA_ROOT, &test_data_root));
ASSERT_EQ(test_data_root, tempdir.GetPath());
}
// Test that CR_SOURCE_ROOT is being used when set.
TEST_F(PathServiceTest, SetTestDataRootAsRelativePath) {
// This is needed because on some platform `DIR_SRC_TEST_DATA_ROOT` can be
// cached before reaching this function.
PathService::DisableCache();
#if BUILDFLAG(IS_WIN)
auto scoped_env = base::ScopedEnvironmentVariableOverride(
"CR_SOURCE_ROOT", base::WideToUTF8(base::FilePath::kParentDirectory));
#else
auto scoped_env = base::ScopedEnvironmentVariableOverride(
"CR_SOURCE_ROOT", base::FilePath::kParentDirectory);
#endif
base::FilePath path;
ASSERT_TRUE(PathService::Get(DIR_EXE, &path));
base::FilePath test_data_root;
ASSERT_TRUE(PathService::Get(DIR_SRC_TEST_DATA_ROOT, &test_data_root));
path = MakeAbsoluteFilePath(path.Append(base::FilePath::kParentDirectory));
ASSERT_EQ(test_data_root, path);
}
#endif
#if BUILDFLAG(IS_FUCHSIA)
// On Fuchsia, some keys have fixed paths that are easy to test.
TEST_F(PathServiceTest, DIR_SRC_TEST_DATA_ROOT) {
FilePath test_binary_path;
EXPECT_EQ(PathService::CheckedGet(DIR_SRC_TEST_DATA_ROOT).value(), "/pkg");
}
#elif BUILDFLAG(IS_ANDROID)
// These keys are overridden in //base/test/test_support_android.cc.
TEST_F(PathServiceTest, AndroidTestOverrides) {
EXPECT_EQ(PathService::CheckedGet(DIR_ANDROID_APP_DATA).value(),
kExpectedChromiumTestsRoot);
EXPECT_EQ(PathService::CheckedGet(DIR_ASSETS).value(),
kExpectedChromiumTestsRoot);
EXPECT_EQ(PathService::CheckedGet(DIR_SRC_TEST_DATA_ROOT).value(),
kExpectedChromiumTestsRoot);
EXPECT_EQ(PathService::CheckedGet(DIR_OUT_TEST_DATA_ROOT).value(),
kExpectedChromiumTestsRoot);
}
#endif // BUILDFLAG(IS_FUCHSIA)
} // namespace base