Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / base / path_service_unittest.cc
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 "base/path_service.h"
6
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/strings/string_util.h"
11 #include "build/build_config.h"
12 #include "testing/gtest/include/gtest/gtest-spi.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
15
16 #if defined(OS_WIN)
17 #include "base/win/windows_version.h"
18 #endif
19
20 namespace base {
21
22 namespace {
23
24 // Returns true if PathService::Get returns true and sets the path parameter
25 // to non-empty for the given PathService::DirType enumeration value.
26 bool ReturnsValidPath(int dir_type) {
27   FilePath path;
28   bool result = PathService::Get(dir_type, &path);
29
30   // Some paths might not exist on some platforms in which case confirming
31   // |result| is true and !path.empty() is the best we can do.
32   bool check_path_exists = true;
33 #if defined(OS_POSIX)
34   // If chromium has never been started on this account, the cache path may not
35   // exist.
36   if (dir_type == DIR_CACHE)
37     check_path_exists = false;
38 #endif
39 #if defined(OS_LINUX)
40   // On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop),
41   // but it doesn't exist.
42   if (dir_type == DIR_USER_DESKTOP)
43     check_path_exists = false;
44 #endif
45 #if defined(OS_IOS)
46   // Bundled unittests on iOS may not have Resources directory in the bundle.
47   if (dir_type == DIR_ASSETS)
48     check_path_exists = false;
49 #endif
50 #if defined(OS_MACOSX)
51   if (dir_type != DIR_EXE && dir_type != DIR_MODULE && dir_type != FILE_EXE &&
52       dir_type != FILE_MODULE) {
53     if (path.ReferencesParent())
54       return false;
55   }
56 #else
57   if (path.ReferencesParent())
58     return false;
59 #endif
60   return result && !path.empty() && (!check_path_exists || PathExists(path));
61 }
62
63 #if defined(OS_WIN)
64 // Function to test any directory keys that are not supported on some versions
65 // of Windows. Checks that the function fails and that the returned path is
66 // empty.
67 bool ReturnsInvalidPath(int dir_type) {
68   FilePath path;
69   bool result = PathService::Get(dir_type, &path);
70   return !result && path.empty();
71 }
72 #endif
73
74 }  // namespace
75
76 // On the Mac this winds up using some autoreleased objects, so we need to
77 // be a PlatformTest.
78 typedef PlatformTest PathServiceTest;
79
80 // Test that all PathService::Get calls return a value and a true result
81 // in the development environment.  (This test was created because a few
82 // later changes to Get broke the semantics of the function and yielded the
83 // correct value while returning false.)
84 TEST_F(PathServiceTest, Get) {
85   for (int key = PATH_START + 1; key < PATH_END; ++key) {
86 #if defined(OS_ANDROID)
87     if (key == FILE_MODULE || key == DIR_USER_DESKTOP ||
88         key == DIR_HOME)
89       continue;  // Android doesn't implement these.
90 #elif defined(OS_IOS)
91     if (key == DIR_USER_DESKTOP)
92       continue;  // iOS doesn't implement DIR_USER_DESKTOP.
93 #elif defined(OS_FUCHSIA)
94     if (key == DIR_USER_DESKTOP || key == FILE_MODULE || key == DIR_MODULE)
95       continue;  // Fuchsia doesn't implement DIR_USER_DESKTOP, FILE_MODULE and
96                  // DIR_MODULE.
97 #endif
98     EXPECT_PRED1(ReturnsValidPath, key);
99   }
100 #if defined(OS_WIN)
101   for (int key = PATH_WIN_START + 1; key < PATH_WIN_END; ++key) {
102     bool valid = true;
103     if (key == DIR_APP_SHORTCUTS)
104       valid = base::win::GetVersion() >= base::win::VERSION_WIN8;
105
106     if (valid)
107       EXPECT_TRUE(ReturnsValidPath(key)) << key;
108     else
109       EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
110   }
111 #elif defined(OS_MACOSX)
112   for (int key = PATH_MAC_START + 1; key < PATH_MAC_END; ++key) {
113     EXPECT_PRED1(ReturnsValidPath, key);
114   }
115 #elif defined(OS_ANDROID)
116   for (int key = PATH_ANDROID_START + 1; key < PATH_ANDROID_END;
117        ++key) {
118     EXPECT_PRED1(ReturnsValidPath, key);
119   }
120 #elif defined(OS_POSIX)
121   for (int key = PATH_POSIX_START + 1; key < PATH_POSIX_END;
122        ++key) {
123     EXPECT_PRED1(ReturnsValidPath, key);
124   }
125 #endif
126 }
127
128 // Test that all versions of the Override function of PathService do what they
129 // are supposed to do.
130 TEST_F(PathServiceTest, Override) {
131   int my_special_key = 666;
132   ScopedTempDir temp_dir;
133   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
134   FilePath fake_cache_dir(temp_dir.GetPath().AppendASCII("cache"));
135   // PathService::Override should always create the path provided if it doesn't
136   // exist.
137   EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir));
138   EXPECT_TRUE(PathExists(fake_cache_dir));
139
140   FilePath fake_cache_dir2(temp_dir.GetPath().AppendASCII("cache2"));
141   // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter.
142   PathService::OverrideAndCreateIfNeeded(my_special_key,
143                                          fake_cache_dir2,
144                                          false,
145                                          false);
146   EXPECT_FALSE(PathExists(fake_cache_dir2));
147   EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
148                                                      fake_cache_dir2,
149                                                      false,
150                                                      true));
151   EXPECT_TRUE(PathExists(fake_cache_dir2));
152
153 #if defined(OS_POSIX)
154   FilePath non_existent(
155       MakeAbsoluteFilePath(temp_dir.GetPath()).AppendASCII("non_existent"));
156   EXPECT_TRUE(non_existent.IsAbsolute());
157   EXPECT_FALSE(PathExists(non_existent));
158 #if !defined(OS_ANDROID)
159   // This fails because MakeAbsoluteFilePath fails for non-existent files.
160   // Earlier versions of Bionic libc don't fail for non-existent files, so
161   // skip this check on Android.
162   EXPECT_FALSE(PathService::OverrideAndCreateIfNeeded(my_special_key,
163                                                       non_existent,
164                                                       false,
165                                                       false));
166 #endif
167   // This works because indicating that |non_existent| is absolute skips the
168   // internal MakeAbsoluteFilePath call.
169   EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
170                                                      non_existent,
171                                                      true,
172                                                      false));
173   // Check that the path has been overridden and no directory was created.
174   EXPECT_FALSE(PathExists(non_existent));
175   FilePath path;
176   EXPECT_TRUE(PathService::Get(my_special_key, &path));
177   EXPECT_EQ(non_existent, path);
178 #endif
179 }
180
181 // Check if multiple overrides can co-exist.
182 TEST_F(PathServiceTest, OverrideMultiple) {
183   int my_special_key = 666;
184   ScopedTempDir temp_dir;
185   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
186   FilePath fake_cache_dir1(temp_dir.GetPath().AppendASCII("1"));
187   EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1));
188   EXPECT_TRUE(PathExists(fake_cache_dir1));
189   ASSERT_EQ(1, WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1));
190
191   FilePath fake_cache_dir2(temp_dir.GetPath().AppendASCII("2"));
192   EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2));
193   EXPECT_TRUE(PathExists(fake_cache_dir2));
194   ASSERT_EQ(1, WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1));
195
196   FilePath result;
197   EXPECT_TRUE(PathService::Get(my_special_key, &result));
198   // Override might have changed the path representation but our test file
199   // should be still there.
200   EXPECT_TRUE(PathExists(result.AppendASCII("t1")));
201   EXPECT_TRUE(PathService::Get(my_special_key + 1, &result));
202   EXPECT_TRUE(PathExists(result.AppendASCII("t2")));
203 }
204
205 TEST_F(PathServiceTest, RemoveOverride) {
206   // Before we start the test we have to call RemoveOverride at least once to
207   // clear any overrides that might have been left from other tests.
208   PathService::RemoveOverride(DIR_TEMP);
209
210   FilePath original_user_data_dir;
211   EXPECT_TRUE(PathService::Get(DIR_TEMP, &original_user_data_dir));
212   EXPECT_FALSE(PathService::RemoveOverride(DIR_TEMP));
213
214   ScopedTempDir temp_dir;
215   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
216   EXPECT_TRUE(PathService::Override(DIR_TEMP, temp_dir.GetPath()));
217   FilePath new_user_data_dir;
218   EXPECT_TRUE(PathService::Get(DIR_TEMP, &new_user_data_dir));
219   EXPECT_NE(original_user_data_dir, new_user_data_dir);
220
221   EXPECT_TRUE(PathService::RemoveOverride(DIR_TEMP));
222   EXPECT_TRUE(PathService::Get(DIR_TEMP, &new_user_data_dir));
223   EXPECT_EQ(original_user_data_dir, new_user_data_dir);
224 }
225
226 #if defined(OS_WIN)
227 TEST_F(PathServiceTest, GetProgramFiles) {
228   FilePath programfiles_dir;
229 #if defined(_WIN64)
230   // 64-bit on 64-bit.
231   EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
232       &programfiles_dir));
233   EXPECT_EQ(programfiles_dir.value(),
234       FILE_PATH_LITERAL("C:\\Program Files"));
235   EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
236       &programfiles_dir));
237   EXPECT_EQ(programfiles_dir.value(),
238       FILE_PATH_LITERAL("C:\\Program Files (x86)"));
239   EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
240       &programfiles_dir));
241   EXPECT_EQ(programfiles_dir.value(),
242       FILE_PATH_LITERAL("C:\\Program Files"));
243 #else
244   if (base::win::OSInfo::GetInstance()->wow64_status() ==
245       base::win::OSInfo::WOW64_ENABLED) {
246     // 32-bit on 64-bit.
247     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
248         &programfiles_dir));
249     EXPECT_EQ(programfiles_dir.value(),
250         FILE_PATH_LITERAL("C:\\Program Files (x86)"));
251     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
252         &programfiles_dir));
253     EXPECT_EQ(programfiles_dir.value(),
254         FILE_PATH_LITERAL("C:\\Program Files (x86)"));
255     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
256         &programfiles_dir));
257     EXPECT_EQ(programfiles_dir.value(),
258         FILE_PATH_LITERAL("C:\\Program Files"));
259   } else {
260     // 32-bit on 32-bit.
261     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
262         &programfiles_dir));
263     EXPECT_EQ(programfiles_dir.value(),
264         FILE_PATH_LITERAL("C:\\Program Files"));
265     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
266         &programfiles_dir));
267     EXPECT_EQ(programfiles_dir.value(),
268         FILE_PATH_LITERAL("C:\\Program Files"));
269     EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
270         &programfiles_dir));
271     EXPECT_EQ(programfiles_dir.value(),
272         FILE_PATH_LITERAL("C:\\Program Files"));
273   }
274 #endif
275 }
276 #endif
277
278 }  // namespace base