blob: c6cc0e6739c6dcaa89a524bf6655010e71ce6c1c [file] [log] [blame]
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
evanm@google.com874d1672008-10-31 08:54:04 +09005#include "base/path_service.h"
6
initial.commit3f4a7322008-07-27 06:49:38 +09007#include "base/basictypes.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +09008#include "base/files/file_path.h"
brettw@chromium.org01f3da42014-08-14 05:22:14 +09009#include "base/files/file_util.h"
brettw@chromium.org091db522012-11-17 05:34:23 +090010#include "base/files/scoped_temp_dir.h"
avi@chromium.org67d593d2013-06-11 04:06:57 +090011#include "base/strings/string_util.h"
gab@chromium.org97fc1e62012-09-21 01:24:52 +090012#include "build/build_config.h"
wjia@chromium.org9d594d12012-09-20 10:59:36 +090013#include "testing/gtest/include/gtest/gtest-spi.h"
brettw@chromium.org091db522012-11-17 05:34:23 +090014#include "testing/gtest/include/gtest/gtest.h"
wjia@chromium.org9d594d12012-09-20 10:59:36 +090015#include "testing/platform_test.h"
gab@chromium.org4011fea2012-09-20 09:13:40 +090016
gab@chromium.org97fc1e62012-09-21 01:24:52 +090017#if defined(OS_WIN)
gab@chromium.org97fc1e62012-09-21 01:24:52 +090018#include "base/win/windows_version.h"
gab@chromium.org97fc1e62012-09-21 01:24:52 +090019#endif
20
initial.commit3f4a7322008-07-27 06:49:38 +090021namespace {
initial.commit3f4a7322008-07-27 06:49:38 +090022
23// Returns true if PathService::Get returns true and sets the path parameter
24// to non-empty for the given PathService::DirType enumeration value.
25bool ReturnsValidPath(int dir_type) {
brettw@chromium.org82bcf512013-02-17 14:07:23 +090026 base::FilePath path;
initial.commit3f4a7322008-07-27 06:49:38 +090027 bool result = PathService::Get(dir_type, &path);
maruel@chromium.org805e39a2013-01-26 08:00:55 +090028
gab@chromium.org798226b2012-11-07 07:27:01 +090029 // Some paths might not exist on some platforms in which case confirming
30 // |result| is true and !path.empty() is the best we can do.
31 bool check_path_exists = true;
evan@chromium.org9e3973b2010-11-24 07:39:30 +090032#if defined(OS_POSIX)
viettrungluu@chromium.org366266c2010-11-24 11:23:15 +090033 // If chromium has never been started on this account, the cache path may not
evan@chromium.org9e3973b2010-11-24 07:39:30 +090034 // exist.
viettrungluu@chromium.org366266c2010-11-24 11:23:15 +090035 if (dir_type == base::DIR_CACHE)
gab@chromium.org798226b2012-11-07 07:27:01 +090036 check_path_exists = false;
evan@chromium.org9e3973b2010-11-24 07:39:30 +090037#endif
gab@chromium.org97fc1e62012-09-21 01:24:52 +090038#if defined(OS_LINUX)
39 // On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop),
40 // but it doesn't exist.
41 if (dir_type == base::DIR_USER_DESKTOP)
gab@chromium.org798226b2012-11-07 07:27:01 +090042 check_path_exists = false;
gab@chromium.org97fc1e62012-09-21 01:24:52 +090043#endif
44#if defined(OS_WIN)
gab@chromium.orgdeaba9c2014-08-09 03:17:53 +090045 if (dir_type == base::DIR_TASKBAR_PINS) {
gab@chromium.org798226b2012-11-07 07:27:01 +090046 // There is no pinned-to-taskbar shortcuts prior to Win7.
gab@chromium.org6aaf8f02012-11-15 10:01:51 +090047 if (base::win::GetVersion() < base::win::VERSION_WIN7)
gab@chromium.org798226b2012-11-07 07:27:01 +090048 check_path_exists = false;
gab@chromium.org97fc1e62012-09-21 01:24:52 +090049 }
50#endif
dalecurtis@google.comd30c6822013-03-20 07:06:20 +090051#if defined(OS_MACOSX)
maruel@chromium.org805e39a2013-01-26 08:00:55 +090052 if (dir_type != base::DIR_EXE && dir_type != base::DIR_MODULE &&
53 dir_type != base::FILE_EXE && dir_type != base::FILE_MODULE) {
54 if (path.ReferencesParent())
55 return false;
56 }
57#else
58 if (path.ReferencesParent())
59 return false;
60#endif
gab@chromium.org798226b2012-11-07 07:27:01 +090061 return result && !path.empty() && (!check_path_exists ||
brettw@chromium.org10b64122013-07-12 02:36:07 +090062 base::PathExists(path));
initial.commit3f4a7322008-07-27 06:49:38 +090063}
64
erikkay@google.com3620e4c2008-08-12 00:38:27 +090065#if defined(OS_WIN)
benwells@chromium.orgbd09d672012-08-30 18:16:55 +090066// Function to test any directory keys that are not supported on some versions
67// of Windows. Checks that the function fails and that the returned path is
68// empty.
maruel@google.com31177672008-08-08 08:59:04 +090069bool ReturnsInvalidPath(int dir_type) {
brettw@chromium.org82bcf512013-02-17 14:07:23 +090070 base::FilePath path;
benwells@chromium.orgbd09d672012-08-30 18:16:55 +090071 bool result = PathService::Get(dir_type, &path);
maruel@google.com31177672008-08-08 08:59:04 +090072 return !result && path.empty();
maruel@google.com15cfc7f2008-08-08 05:23:09 +090073}
erikkay@google.com3620e4c2008-08-12 00:38:27 +090074#endif
maruel@google.com15cfc7f2008-08-08 05:23:09 +090075
76} // namespace
77
erikkay@google.comf2406842008-08-21 00:59:49 +090078// On the Mac this winds up using some autoreleased objects, so we need to
79// be a PlatformTest.
80typedef PlatformTest PathServiceTest;
81
initial.commit3f4a7322008-07-27 06:49:38 +090082// Test that all PathService::Get calls return a value and a true result
83// in the development environment. (This test was created because a few
84// later changes to Get broke the semantics of the function and yielded the
85// correct value while returning false.)
erikkay@google.comf2406842008-08-21 00:59:49 +090086TEST_F(PathServiceTest, Get) {
gab@chromium.org97fc1e62012-09-21 01:24:52 +090087 for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) {
michaelbai@google.com4464bef2011-08-27 01:43:59 +090088#if defined(OS_ANDROID)
brettw@chromium.org49de1af2014-02-20 05:34:23 +090089 if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP ||
90 key == base::DIR_HOME)
91 continue; // Android doesn't implement these.
rohitrao@chromium.org5599a7f2012-09-26 03:16:25 +090092#elif defined(OS_IOS)
93 if (key == base::DIR_USER_DESKTOP)
94 continue; // iOS doesn't implement DIR_USER_DESKTOP;
michaelbai@google.com4464bef2011-08-27 01:43:59 +090095#endif
initial.commit3f4a7322008-07-27 06:49:38 +090096 EXPECT_PRED1(ReturnsValidPath, key);
97 }
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +090098#if defined(OS_WIN)
erikkay@google.com1d4507f2008-08-07 01:29:44 +090099 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) {
benwells@chromium.orgbd09d672012-08-30 18:16:55 +0900100 bool valid = true;
101 switch(key) {
102 case base::DIR_LOCAL_APP_DATA_LOW:
103 // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected
104 // to fail.
105 valid = base::win::GetVersion() >= base::win::VERSION_VISTA;
106 break;
107 case base::DIR_APP_SHORTCUTS:
108 // DIR_APP_SHORTCUTS is not supported prior Windows 8 and is expected to
109 // fail.
110 valid = base::win::GetVersion() >= base::win::VERSION_WIN8;
111 break;
maruel@google.com15cfc7f2008-08-08 05:23:09 +0900112 }
benwells@chromium.orgbd09d672012-08-30 18:16:55 +0900113
114 if (valid)
115 EXPECT_TRUE(ReturnsValidPath(key)) << key;
116 else
117 EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
erikkay@google.com1d4507f2008-08-07 01:29:44 +0900118 }
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900119#elif defined(OS_MACOSX)
120 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) {
gab@chromium.org97fc1e62012-09-21 01:24:52 +0900121 EXPECT_PRED1(ReturnsValidPath, key);
122 }
123#elif defined(OS_ANDROID)
124 for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END;
125 ++key) {
126 EXPECT_PRED1(ReturnsValidPath, key);
127 }
128#elif defined(OS_POSIX)
129 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END;
130 ++key) {
131 EXPECT_PRED1(ReturnsValidPath, key);
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900132 }
erikkay@google.com1d4507f2008-08-07 01:29:44 +0900133#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900134}
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +0900135
joaodasilva@chromium.org41819c52014-05-03 02:59:42 +0900136// Test that all versions of the Override function of PathService do what they
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +0900137// are supposed to do.
138TEST_F(PathServiceTest, Override) {
139 int my_special_key = 666;
brettw@chromium.org091db522012-11-17 05:34:23 +0900140 base::ScopedTempDir temp_dir;
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +0900141 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
brettw@chromium.org82bcf512013-02-17 14:07:23 +0900142 base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache"));
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +0900143 // PathService::Override should always create the path provided if it doesn't
144 // exist.
145 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir));
brettw@chromium.org10b64122013-07-12 02:36:07 +0900146 EXPECT_TRUE(base::PathExists(fake_cache_dir));
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +0900147
brettw@chromium.org82bcf512013-02-17 14:07:23 +0900148 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2"));
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +0900149 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter.
150 PathService::OverrideAndCreateIfNeeded(my_special_key,
151 fake_cache_dir2,
joaodasilva@chromium.org41819c52014-05-03 02:59:42 +0900152 false,
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +0900153 false);
brettw@chromium.org10b64122013-07-12 02:36:07 +0900154 EXPECT_FALSE(base::PathExists(fake_cache_dir2));
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +0900155 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
156 fake_cache_dir2,
joaodasilva@chromium.org41819c52014-05-03 02:59:42 +0900157 false,
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +0900158 true));
brettw@chromium.org10b64122013-07-12 02:36:07 +0900159 EXPECT_TRUE(base::PathExists(fake_cache_dir2));
joaodasilva@chromium.org41819c52014-05-03 02:59:42 +0900160
161#if defined(OS_POSIX)
162 base::FilePath non_existent(
163 base::MakeAbsoluteFilePath(temp_dir.path()).AppendASCII("non_existent"));
164 EXPECT_TRUE(non_existent.IsAbsolute());
165 EXPECT_FALSE(base::PathExists(non_existent));
166#if !defined(OS_ANDROID)
167 // This fails because MakeAbsoluteFilePath fails for non-existent files.
168 // Earlier versions of Bionic libc don't fail for non-existent files, so
169 // skip this check on Android.
170 EXPECT_FALSE(PathService::OverrideAndCreateIfNeeded(my_special_key,
171 non_existent,
172 false,
173 false));
174#endif
175 // This works because indicating that |non_existent| is absolute skips the
176 // internal MakeAbsoluteFilePath call.
177 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
178 non_existent,
179 true,
180 false));
181 // Check that the path has been overridden and no directory was created.
182 EXPECT_FALSE(base::PathExists(non_existent));
183 base::FilePath path;
184 EXPECT_TRUE(PathService::Get(my_special_key, &path));
185 EXPECT_EQ(non_existent, path);
186#endif
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +0900187}
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900188
189// Check if multiple overrides can co-exist.
190TEST_F(PathServiceTest, OverrideMultiple) {
191 int my_special_key = 666;
brettw@chromium.org091db522012-11-17 05:34:23 +0900192 base::ScopedTempDir temp_dir;
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900193 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
brettw@chromium.org82bcf512013-02-17 14:07:23 +0900194 base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1"));
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900195 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1));
brettw@chromium.org10b64122013-07-12 02:36:07 +0900196 EXPECT_TRUE(base::PathExists(fake_cache_dir1));
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900197 ASSERT_EQ(1, base::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1));
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900198
brettw@chromium.org82bcf512013-02-17 14:07:23 +0900199 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2"));
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900200 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2));
brettw@chromium.org10b64122013-07-12 02:36:07 +0900201 EXPECT_TRUE(base::PathExists(fake_cache_dir2));
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900202 ASSERT_EQ(1, base::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1));
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900203
brettw@chromium.org82bcf512013-02-17 14:07:23 +0900204 base::FilePath result;
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900205 EXPECT_TRUE(PathService::Get(my_special_key, &result));
206 // Override might have changed the path representation but our test file
207 // should be still there.
brettw@chromium.org10b64122013-07-12 02:36:07 +0900208 EXPECT_TRUE(base::PathExists(result.AppendASCII("t1")));
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900209 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result));
brettw@chromium.org10b64122013-07-12 02:36:07 +0900210 EXPECT_TRUE(base::PathExists(result.AppendASCII("t2")));
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900211}
212
213TEST_F(PathServiceTest, RemoveOverride) {
214 // Before we start the test we have to call RemoveOverride at least once to
215 // clear any overrides that might have been left from other tests.
216 PathService::RemoveOverride(base::DIR_TEMP);
217
brettw@chromium.org82bcf512013-02-17 14:07:23 +0900218 base::FilePath original_user_data_dir;
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900219 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir));
220 EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP));
221
brettw@chromium.org091db522012-11-17 05:34:23 +0900222 base::ScopedTempDir temp_dir;
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900223 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
224 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path()));
brettw@chromium.org82bcf512013-02-17 14:07:23 +0900225 base::FilePath new_user_data_dir;
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +0900226 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir));
227 EXPECT_NE(original_user_data_dir, new_user_data_dir);
228
229 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP));
230 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir));
231 EXPECT_EQ(original_user_data_dir, new_user_data_dir);
232}