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.
5 #include "base/path_service.h"
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"
17 #include "base/win/windows_version.h"
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) {
28 bool result = PathService::Get(dir_type, &path);
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;
34 // If chromium has never been started on this account, the cache path may not
36 if (dir_type == DIR_CACHE)
37 check_path_exists = false;
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;
46 // Bundled unittests on iOS may not have Resources directory in the bundle.
47 if (dir_type == DIR_ASSETS)
48 check_path_exists = false;
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())
57 if (path.ReferencesParent())
60 return result && !path.empty() && (!check_path_exists || PathExists(path));
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
67 bool ReturnsInvalidPath(int dir_type) {
69 bool result = PathService::Get(dir_type, &path);
70 return !result && path.empty();
76 // On the Mac this winds up using some autoreleased objects, so we need to
78 typedef PlatformTest PathServiceTest;
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 ||
89 continue; // Android doesn't implement these.
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
98 EXPECT_PRED1(ReturnsValidPath, key);
101 for (int key = PATH_WIN_START + 1; key < PATH_WIN_END; ++key) {
103 if (key == DIR_APP_SHORTCUTS)
104 valid = base::win::GetVersion() >= base::win::VERSION_WIN8;
107 EXPECT_TRUE(ReturnsValidPath(key)) << key;
109 EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
111 #elif defined(OS_MACOSX)
112 for (int key = PATH_MAC_START + 1; key < PATH_MAC_END; ++key) {
113 EXPECT_PRED1(ReturnsValidPath, key);
115 #elif defined(OS_ANDROID)
116 for (int key = PATH_ANDROID_START + 1; key < PATH_ANDROID_END;
118 EXPECT_PRED1(ReturnsValidPath, key);
120 #elif defined(OS_POSIX)
121 for (int key = PATH_POSIX_START + 1; key < PATH_POSIX_END;
123 EXPECT_PRED1(ReturnsValidPath, key);
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
137 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir));
138 EXPECT_TRUE(PathExists(fake_cache_dir));
140 FilePath fake_cache_dir2(temp_dir.GetPath().AppendASCII("cache2"));
141 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter.
142 PathService::OverrideAndCreateIfNeeded(my_special_key,
146 EXPECT_FALSE(PathExists(fake_cache_dir2));
147 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
151 EXPECT_TRUE(PathExists(fake_cache_dir2));
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,
167 // This works because indicating that |non_existent| is absolute skips the
168 // internal MakeAbsoluteFilePath call.
169 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
173 // Check that the path has been overridden and no directory was created.
174 EXPECT_FALSE(PathExists(non_existent));
176 EXPECT_TRUE(PathService::Get(my_special_key, &path));
177 EXPECT_EQ(non_existent, path);
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));
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));
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")));
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);
210 FilePath original_user_data_dir;
211 EXPECT_TRUE(PathService::Get(DIR_TEMP, &original_user_data_dir));
212 EXPECT_FALSE(PathService::RemoveOverride(DIR_TEMP));
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);
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);
227 TEST_F(PathServiceTest, GetProgramFiles) {
228 FilePath programfiles_dir;
231 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
233 EXPECT_EQ(programfiles_dir.value(),
234 FILE_PATH_LITERAL("C:\\Program Files"));
235 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
237 EXPECT_EQ(programfiles_dir.value(),
238 FILE_PATH_LITERAL("C:\\Program Files (x86)"));
239 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
241 EXPECT_EQ(programfiles_dir.value(),
242 FILE_PATH_LITERAL("C:\\Program Files"));
244 if (base::win::OSInfo::GetInstance()->wow64_status() ==
245 base::win::OSInfo::WOW64_ENABLED) {
247 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
249 EXPECT_EQ(programfiles_dir.value(),
250 FILE_PATH_LITERAL("C:\\Program Files (x86)"));
251 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
253 EXPECT_EQ(programfiles_dir.value(),
254 FILE_PATH_LITERAL("C:\\Program Files (x86)"));
255 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
257 EXPECT_EQ(programfiles_dir.value(),
258 FILE_PATH_LITERAL("C:\\Program Files"));
261 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
263 EXPECT_EQ(programfiles_dir.value(),
264 FILE_PATH_LITERAL("C:\\Program Files"));
265 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
267 EXPECT_EQ(programfiles_dir.value(),
268 FILE_PATH_LITERAL("C:\\Program Files"));
269 EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
271 EXPECT_EQ(programfiles_dir.value(),
272 FILE_PATH_LITERAL("C:\\Program Files"));