Fix emulator build error
[platform/framework/web/chromium-efl.git] / base / path_service_unittest.cc
1 // Copyright 2012 The Chromium Authors
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 "base/path_service.h"
6
7 #include "base/base_paths.h"
8 #include "base/containers/contains.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/strings/string_util.h"
14 #include "base/test/gtest_util.h"
15 #include "build/build_config.h"
16 #include "testing/gtest/include/gtest/gtest-spi.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h"
19
20 #if BUILDFLAG(IS_WIN)
21 #include "base/win/windows_version.h"
22 #endif
23
24 #if BUILDFLAG(IS_APPLE)
25 #include "base/apple/bundle_locations.h"
26 #endif
27
28 namespace base {
29
30 namespace {
31
32 #if BUILDFLAG(IS_ANDROID)
33 // Defined in
34 // //base/test/android/javatests/src/org/chromium/base/test/util/UrlUtils.java.
35 constexpr char kExpectedChromiumTestsRoot[] =
36     "/storage/emulated/0/chromium_tests_root";
37 #endif
38
39 // Returns true if PathService::Get returns true and sets the path parameter
40 // to non-empty for the given PathService key enumeration value.
41 bool ReturnsValidPath(int key) {
42   FilePath path;
43   bool result = PathService::Get(key, &path);
44
45   // Some paths might not exist on some platforms in which case confirming
46   // |result| is true and !path.empty() is the best we can do.
47   bool check_path_exists = true;
48
49 #if BUILDFLAG(IS_POSIX)
50   // If chromium has never been started on this account, the cache path may not
51   // exist.
52   if (key == DIR_CACHE)
53     check_path_exists = false;
54 #endif
55 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
56   // On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop),
57   // but it doesn't exist.
58   if (key == DIR_USER_DESKTOP)
59     check_path_exists = false;
60 #endif
61 #if BUILDFLAG(IS_WIN)
62   if (key == DIR_TASKBAR_PINS)
63     check_path_exists = false;
64 #endif
65 #if BUILDFLAG(IS_MAC)
66   if (key != DIR_EXE && key != DIR_MODULE && key != FILE_EXE &&
67       key != FILE_MODULE) {
68     if (path.ReferencesParent()) {
69       LOG(INFO) << "Path (" << path << ") references parent.";
70       return false;
71     }
72   }
73 #else
74   if (path.ReferencesParent()) {
75     LOG(INFO) << "Path (" << path << ") references parent.";
76     return false;
77   }
78 #endif  // BUILDFLAG(IS_MAC)
79   if (!result) {
80     LOG(INFO) << "PathService::Get() returned false.";
81     return false;
82   }
83   if (path.empty()) {
84     LOG(INFO) << "PathService::Get() returned an empty path.";
85     return false;
86   }
87   if (check_path_exists && !PathExists(path)) {
88     LOG(INFO) << "Path (" << path << ") does not exist.";
89     return false;
90   }
91   return true;
92 }
93
94 // Returns true if PathService::Get returns false and path parameter is empty
95 // for the given PathService key enumeration value. Used to test path keys that
96 // are not supported on the platform or on some versions of Windows.
97 bool ReturnsInvalidPath(int key) {
98   FilePath path;
99   bool result = PathService::Get(key, &path);
100   return !result && path.empty();
101 }
102
103 }  // namespace
104
105 // On the Mac this winds up using some autoreleased objects, so we need to
106 // be a PlatformTest.
107 typedef PlatformTest PathServiceTest;
108
109 // Test that all PathService::Get calls return a value and a true result
110 // in the development environment.  (This test was created because a few
111 // later changes to Get broke the semantics of the function and yielded the
112 // correct value while returning false.)
113 // If this test fails for specific value(s) on a specific platform, consider not
114 // defining the enum value on that platform rather than skipping or expecting
115 // failure for the value(s) on that platform in this test.
116 TEST_F(PathServiceTest, Get) {
117   // Contains keys that are defined but not supported on the platform.
118 #if BUILDFLAG(IS_ANDROID)
119   // The following keys are not intended to be implemented on Android (see
120   // crbug.com/1257402). Current implementation is described before each key.
121   // TODO(crbug.com/1257402): Remove the definition of these keys on Android
122   // or at least fix the behavior of DIR_HOME.
123   constexpr std::array kUnsupportedKeys = {
124       // Though DIR_HOME is not intended to be supported, PathProviderPosix
125       // handles it and returns true. Thus, it is NOT included in the array.
126       /* DIR_HOME, */
127       // PathProviderAndroid and PathProviderPosix both return false.
128       FILE_MODULE,
129       // PathProviderPosix handles it but fails at some point.
130       DIR_USER_DESKTOP};
131 #elif BUILDFLAG(IS_FUCHSIA)
132   constexpr std::array kUnsupportedKeys = {
133       // TODO(crbug.com/1231928): Implement DIR_USER_DESKTOP.
134       DIR_USER_DESKTOP};
135 #else
136   constexpr std::array<BasePathKey, 0> kUnsupportedKeys = {};
137 #endif  // BUILDFLAG(IS_ANDROID)
138   for (int key = PATH_START + 1; key < PATH_END; ++key) {
139     EXPECT_PRED1(Contains(kUnsupportedKeys, key) ? &ReturnsInvalidPath
140                                                  : &ReturnsValidPath,
141                  key);
142   }
143 #if BUILDFLAG(IS_WIN)
144   for (int key = PATH_WIN_START + 1; key < PATH_WIN_END; ++key) {
145     EXPECT_PRED1(ReturnsValidPath, key);
146   }
147 #elif BUILDFLAG(IS_MAC)
148   for (int key = PATH_MAC_START + 1; key < PATH_MAC_END; ++key) {
149     EXPECT_PRED1(ReturnsValidPath, key);
150   }
151 #elif BUILDFLAG(IS_IOS)
152   for (int key = PATH_IOS_START + 1; key < PATH_IOS_END; ++key) {
153     EXPECT_PRED1(ReturnsValidPath, key);
154   }
155 #elif BUILDFLAG(IS_ANDROID)
156   for (int key = PATH_ANDROID_START + 1; key < PATH_ANDROID_END;
157        ++key) {
158     EXPECT_PRED1(ReturnsValidPath, key);
159   }
160 #elif BUILDFLAG(IS_POSIX)
161   for (int key = PATH_POSIX_START + 1; key < PATH_POSIX_END;
162        ++key) {
163     EXPECT_PRED1(ReturnsValidPath, key);
164   }
165 #endif  // BUILDFLAG(IS_WIN)
166 }
167
168 // Tests that CheckedGet returns the same path as Get.
169 TEST_F(PathServiceTest, CheckedGet) {
170   constexpr int kKey = DIR_CURRENT;
171   FilePath path;
172   ASSERT_TRUE(PathService::Get(kKey, &path));
173   EXPECT_EQ(path, PathService::CheckedGet(kKey));
174 }
175
176 #if defined(GTEST_HAS_DEATH_TEST)
177
178 // Tests that CheckedGet CHECKs on failure.
179 TEST_F(PathServiceTest, CheckedGetFailure) {
180   constexpr int kBadKey = PATH_END;
181   FilePath path;
182   EXPECT_FALSE(PathService::Get(kBadKey, &path));
183   EXPECT_DEATH(PathService::CheckedGet(kBadKey), "Failed to get the path");
184 }
185
186 #endif  // defined(GTEST_HAS_DEATH_TEST)
187
188 // Test that all versions of the Override function of PathService do what they
189 // are supposed to do.
190 TEST_F(PathServiceTest, Override) {
191   int my_special_key = 666;
192   ScopedTempDir temp_dir;
193   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
194   FilePath fake_cache_dir(temp_dir.GetPath().AppendASCII("cache"));
195   // PathService::Override should always create the path provided if it doesn't
196   // exist.
197   EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir));
198   EXPECT_TRUE(PathExists(fake_cache_dir));
199
200   FilePath fake_cache_dir2(temp_dir.GetPath().AppendASCII("cache2"));
201   // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter.
202   PathService::OverrideAndCreateIfNeeded(my_special_key,
203                                          fake_cache_dir2,
204                                          false,
205                                          false);
206   EXPECT_FALSE(PathExists(fake_cache_dir2));
207   EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
208                                                      fake_cache_dir2,
209                                                      false,
210                                                      true));
211   EXPECT_TRUE(PathExists(fake_cache_dir2));
212
213 #if BUILDFLAG(IS_POSIX)
214   FilePath non_existent(
215       MakeAbsoluteFilePath(temp_dir.GetPath()).AppendASCII("non_existent"));
216   EXPECT_TRUE(non_existent.IsAbsolute());
217   EXPECT_FALSE(PathExists(non_existent));
218 #if !BUILDFLAG(IS_ANDROID)
219   // This fails because MakeAbsoluteFilePath fails for non-existent files.
220   // Earlier versions of Bionic libc don't fail for non-existent files, so
221   // skip this check on Android.
222   EXPECT_FALSE(PathService::OverrideAndCreateIfNeeded(my_special_key,
223                                                       non_existent,
224                                                       false,
225                                                       false));
226 #endif  // !BUILDFLAG(IS_ANDROID)
227   // This works because indicating that |non_existent| is absolute skips the
228   // internal MakeAbsoluteFilePath call.
229   EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
230                                                      non_existent,
231                                                      true,
232                                                      false));
233   // Check that the path has been overridden and no directory was created.
234   EXPECT_FALSE(PathExists(non_existent));
235   FilePath path;
236   EXPECT_TRUE(PathService::Get(my_special_key, &path));
237   EXPECT_EQ(non_existent, path);
238 #endif  // BUILDFLAG(IS_POSIX)
239 }
240
241 // Check if multiple overrides can co-exist.
242 TEST_F(PathServiceTest, OverrideMultiple) {
243   int my_special_key = 666;
244   ScopedTempDir temp_dir;
245   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
246   FilePath fake_cache_dir1(temp_dir.GetPath().AppendASCII("1"));
247   EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1));
248   EXPECT_TRUE(PathExists(fake_cache_dir1));
249   ASSERT_TRUE(WriteFile(fake_cache_dir1.AppendASCII("t1"), "."));
250
251   FilePath fake_cache_dir2(temp_dir.GetPath().AppendASCII("2"));
252   EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2));
253   EXPECT_TRUE(PathExists(fake_cache_dir2));
254   ASSERT_TRUE(WriteFile(fake_cache_dir2.AppendASCII("t2"), "."));
255
256   FilePath result;
257   EXPECT_TRUE(PathService::Get(my_special_key, &result));
258   // Override might have changed the path representation but our test file
259   // should be still there.
260   EXPECT_TRUE(PathExists(result.AppendASCII("t1")));
261   EXPECT_TRUE(PathService::Get(my_special_key + 1, &result));
262   EXPECT_TRUE(PathExists(result.AppendASCII("t2")));
263 }
264
265 TEST_F(PathServiceTest, RemoveOverride) {
266   // Before we start the test we have to call RemoveOverride at least once to
267   // clear any overrides that might have been left from other tests.
268   PathService::RemoveOverrideForTests(DIR_TEMP);
269
270   FilePath original_user_data_dir;
271   EXPECT_TRUE(PathService::Get(DIR_TEMP, &original_user_data_dir));
272   EXPECT_FALSE(PathService::RemoveOverrideForTests(DIR_TEMP));
273
274   ScopedTempDir temp_dir;
275   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
276   EXPECT_TRUE(PathService::Override(DIR_TEMP, temp_dir.GetPath()));
277   FilePath new_user_data_dir;
278   EXPECT_TRUE(PathService::Get(DIR_TEMP, &new_user_data_dir));
279   EXPECT_NE(original_user_data_dir, new_user_data_dir);
280
281   EXPECT_TRUE(PathService::RemoveOverrideForTests(DIR_TEMP));
282   EXPECT_TRUE(PathService::Get(DIR_TEMP, &new_user_data_dir));
283   EXPECT_EQ(original_user_data_dir, new_user_data_dir);
284 }
285
286 #if BUILDFLAG(IS_WIN)
287 TEST_F(PathServiceTest, GetProgramFiles) {
288   FilePath programfiles_dir;
289 #if defined(_WIN64)
290   // 64-bit on 64-bit.
291   EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
292       &programfiles_dir));
293   EXPECT_EQ(programfiles_dir.value(),
294       FILE_PATH_LITERAL("C:\\Program Files"));
295   EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
296       &programfiles_dir));
297   EXPECT_EQ(programfiles_dir.value(),
298       FILE_PATH_LITERAL("C:\\Program Files (x86)"));
299   EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
300       &programfiles_dir));
301   EXPECT_EQ(programfiles_dir.value(),
302       FILE_PATH_LITERAL("C:\\Program Files"));
303 #else
304   if (base::win::OSInfo::GetInstance()->IsWowX86OnAMD64()) {
305     // 32-bit on 64-bit.
306     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
307         &programfiles_dir));
308     EXPECT_EQ(programfiles_dir.value(),
309         FILE_PATH_LITERAL("C:\\Program Files (x86)"));
310     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
311         &programfiles_dir));
312     EXPECT_EQ(programfiles_dir.value(),
313         FILE_PATH_LITERAL("C:\\Program Files (x86)"));
314     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
315         &programfiles_dir));
316     EXPECT_EQ(programfiles_dir.value(),
317         FILE_PATH_LITERAL("C:\\Program Files"));
318   } else {
319     // 32-bit on 32-bit.
320     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
321         &programfiles_dir));
322     EXPECT_EQ(programfiles_dir.value(),
323         FILE_PATH_LITERAL("C:\\Program Files"));
324     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
325         &programfiles_dir));
326     EXPECT_EQ(programfiles_dir.value(),
327         FILE_PATH_LITERAL("C:\\Program Files"));
328     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
329         &programfiles_dir));
330     EXPECT_EQ(programfiles_dir.value(),
331         FILE_PATH_LITERAL("C:\\Program Files"));
332   }
333 #endif  // defined(_WIN64)
334 }
335 #endif  // BUILDFLAG(IS_WIN)
336
337 // Tests that DIR_ASSETS is
338 // - the package root on Fuchsia,
339 // - overridden in tests by test_support_android.cc,
340 // - equals to base::apple::FrameworkBundlePath() on iOS,
341 // - a sub-directory of base::apple::FrameworkBundlePath() on iOS catalyst,
342 // - equals to DIR_MODULE otherwise.
343 TEST_F(PathServiceTest, DIR_ASSETS) {
344   FilePath path;
345   ASSERT_TRUE(PathService::Get(DIR_ASSETS, &path));
346 #if BUILDFLAG(IS_FUCHSIA)
347   EXPECT_EQ(path.value(), "/pkg");
348 #elif BUILDFLAG(IS_ANDROID)
349   // This key is overridden in //base/test/test_support_android.cc.
350   EXPECT_EQ(path.value(), kExpectedChromiumTestsRoot);
351 #elif BUILDFLAG(IS_IOS_MACCATALYST)
352   EXPECT_TRUE(base::apple::FrameworkBundlePath().IsParent(path));
353 #elif BUILDFLAG(IS_IOS)
354   EXPECT_EQ(path, base::apple::FrameworkBundlePath());
355 #else
356   EXPECT_EQ(path, PathService::CheckedGet(DIR_MODULE));
357 #endif
358 }
359
360 // DIR_OUT_TEST_DATA_ROOT is DIR_MODULE except on Fuchsia where it is the
361 // package root, on ios where it is the resources directory and on Android
362 // where it is overridden in tests by test_support_android.cc.
363 TEST_F(PathServiceTest, DIR_OUT_TEST_DATA_ROOT) {
364   FilePath path;
365   ASSERT_TRUE(PathService::Get(DIR_OUT_TEST_DATA_ROOT, &path));
366 #if BUILDFLAG(IS_FUCHSIA)
367   EXPECT_EQ(path.value(), "/pkg");
368 #elif BUILDFLAG(IS_ANDROID)
369   // This key is overridden in //base/test/test_support_android.cc.
370   EXPECT_EQ(path.value(), kExpectedChromiumTestsRoot);
371 #elif BUILDFLAG(IS_IOS)
372   // On iOS, build output files are moved to the resources directory.
373   EXPECT_EQ(path, base::apple::FrameworkBundlePath());
374 #else
375   // On other platforms all build output is in the same directory,
376   // so DIR_OUT_TEST_DATA_ROOT should match DIR_MODULE.
377   EXPECT_EQ(path, PathService::CheckedGet(DIR_MODULE));
378 #endif
379 }
380
381 // Test that DIR_GEN_TEST_DATA_ROOT contains dummy_generated.txt which is
382 // generated for this test.
383 TEST_F(PathServiceTest, DIR_GEN_TEST_DATA_ROOT) {
384   FilePath path;
385   ASSERT_TRUE(PathService::Get(DIR_GEN_TEST_DATA_ROOT, &path));
386   EXPECT_TRUE(base::PathExists(
387       path.Append(FILE_PATH_LITERAL("base/generated_file_for_test.txt"))));
388 }
389
390 #if BUILDFLAG(IS_FUCHSIA)
391 // On Fuchsia, some keys have fixed paths that are easy to test.
392
393 TEST_F(PathServiceTest, DIR_SRC_TEST_DATA_ROOT) {
394   FilePath test_binary_path;
395   EXPECT_EQ(PathService::CheckedGet(DIR_SRC_TEST_DATA_ROOT).value(), "/pkg");
396 }
397
398 #elif BUILDFLAG(IS_ANDROID)
399
400 // These keys are overridden in //base/test/test_support_android.cc.
401 TEST_F(PathServiceTest, AndroidTestOverrides) {
402   EXPECT_EQ(PathService::CheckedGet(DIR_ANDROID_APP_DATA).value(),
403             kExpectedChromiumTestsRoot);
404   EXPECT_EQ(PathService::CheckedGet(DIR_ASSETS).value(),
405             kExpectedChromiumTestsRoot);
406   EXPECT_EQ(PathService::CheckedGet(DIR_SRC_TEST_DATA_ROOT).value(),
407             kExpectedChromiumTestsRoot);
408   EXPECT_EQ(PathService::CheckedGet(DIR_OUT_TEST_DATA_ROOT).value(),
409             kExpectedChromiumTestsRoot);
410 }
411
412 #endif  // BUILDFLAG(IS_FUCHSIA)
413
414 }  // namespace base