blob: c6cc0e6739c6dcaa89a524bf6655010e71ce6c1c [file] [log] [blame]
[email protected]cb571e752012-05-09 10:50:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]640517f2008-10-30 23:54:045#include "base/path_service.h"
6
initial.commitd7cae122008-07-26 21:49:387#include "base/basictypes.h"
[email protected]57999812013-02-24 05:40:528#include "base/files/file_path.h"
[email protected]e3177dd52014-08-13 20:22:149#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2310#include "base/files/scoped_temp_dir.h"
[email protected]d529cb02013-06-10 19:06:5711#include "base/strings/string_util.h"
[email protected]dea1d7d2012-09-20 16:24:5212#include "build/build_config.h"
[email protected]1f4ae162012-09-20 01:59:3613#include "testing/gtest/include/gtest/gtest-spi.h"
[email protected]ea1a3f62012-11-16 20:34:2314#include "testing/gtest/include/gtest/gtest.h"
[email protected]1f4ae162012-09-20 01:59:3615#include "testing/platform_test.h"
[email protected]e5738a02012-09-20 00:13:4016
[email protected]dea1d7d2012-09-20 16:24:5217#if defined(OS_WIN)
[email protected]dea1d7d2012-09-20 16:24:5218#include "base/win/windows_version.h"
[email protected]dea1d7d2012-09-20 16:24:5219#endif
20
initial.commitd7cae122008-07-26 21:49:3821namespace {
initial.commitd7cae122008-07-26 21:49:3822
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) {
[email protected]023ad6ab2013-02-17 05:07:2326 base::FilePath path;
initial.commitd7cae122008-07-26 21:49:3827 bool result = PathService::Get(dir_type, &path);
[email protected]f4f5c922013-01-25 23:00:5528
[email protected]e5f9d822012-11-06 22:27:0129 // 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;
[email protected]d8a80d62010-11-23 22:39:3032#if defined(OS_POSIX)
[email protected]b411da32010-11-24 02:23:1533 // If chromium has never been started on this account, the cache path may not
[email protected]d8a80d62010-11-23 22:39:3034 // exist.
[email protected]b411da32010-11-24 02:23:1535 if (dir_type == base::DIR_CACHE)
[email protected]e5f9d822012-11-06 22:27:0136 check_path_exists = false;
[email protected]d8a80d62010-11-23 22:39:3037#endif
[email protected]dea1d7d2012-09-20 16:24:5238#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)
[email protected]e5f9d822012-11-06 22:27:0142 check_path_exists = false;
[email protected]dea1d7d2012-09-20 16:24:5243#endif
44#if defined(OS_WIN)
[email protected]da4d4fb2014-08-08 18:17:5345 if (dir_type == base::DIR_TASKBAR_PINS) {
[email protected]e5f9d822012-11-06 22:27:0146 // There is no pinned-to-taskbar shortcuts prior to Win7.
[email protected]9dbad6f2012-11-15 01:01:5147 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]e5f9d822012-11-06 22:27:0148 check_path_exists = false;
[email protected]dea1d7d2012-09-20 16:24:5249 }
50#endif
[email protected]0d7717fa2013-03-19 22:06:2051#if defined(OS_MACOSX)
[email protected]f4f5c922013-01-25 23:00:5552 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
[email protected]e5f9d822012-11-06 22:27:0161 return result && !path.empty() && (!check_path_exists ||
[email protected]7567484142013-07-11 17:36:0762 base::PathExists(path));
initial.commitd7cae122008-07-26 21:49:3863}
64
[email protected]6723f832008-08-11 15:38:2765#if defined(OS_WIN)
[email protected]b2721b02012-08-30 09:16:5566// 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.
[email protected]0cfda1e2008-08-07 23:59:0469bool ReturnsInvalidPath(int dir_type) {
[email protected]023ad6ab2013-02-17 05:07:2370 base::FilePath path;
[email protected]b2721b02012-08-30 09:16:5571 bool result = PathService::Get(dir_type, &path);
[email protected]0cfda1e2008-08-07 23:59:0472 return !result && path.empty();
[email protected]09ad1e622008-08-07 20:23:0973}
[email protected]6723f832008-08-11 15:38:2774#endif
[email protected]09ad1e622008-08-07 20:23:0975
76} // namespace
77
[email protected]ed2f2332008-08-20 15:59:4978// On the Mac this winds up using some autoreleased objects, so we need to
79// be a PlatformTest.
80typedef PlatformTest PathServiceTest;
81
initial.commitd7cae122008-07-26 21:49:3882// 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.)
[email protected]ed2f2332008-08-20 15:59:4986TEST_F(PathServiceTest, Get) {
[email protected]dea1d7d2012-09-20 16:24:5287 for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) {
[email protected]aa91eb92011-08-26 16:43:5988#if defined(OS_ANDROID)
[email protected]ffaee18e2014-02-19 20:34:2389 if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP ||
90 key == base::DIR_HOME)
91 continue; // Android doesn't implement these.
[email protected]ce576fe22012-09-25 18:16:2592#elif defined(OS_IOS)
93 if (key == base::DIR_USER_DESKTOP)
94 continue; // iOS doesn't implement DIR_USER_DESKTOP;
[email protected]aa91eb92011-08-26 16:43:5995#endif
initial.commitd7cae122008-07-26 21:49:3896 EXPECT_PRED1(ReturnsValidPath, key);
97 }
[email protected]405a64b2009-09-16 21:03:4498#if defined(OS_WIN)
[email protected]1010f7d2008-08-06 16:29:4499 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) {
[email protected]b2721b02012-08-30 09:16:55100 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;
[email protected]09ad1e622008-08-07 20:23:09112 }
[email protected]b2721b02012-08-30 09:16:55113
114 if (valid)
115 EXPECT_TRUE(ReturnsValidPath(key)) << key;
116 else
117 EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
[email protected]1010f7d2008-08-06 16:29:44118 }
[email protected]405a64b2009-09-16 21:03:44119#elif defined(OS_MACOSX)
120 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) {
[email protected]dea1d7d2012-09-20 16:24:52121 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);
[email protected]405a64b2009-09-16 21:03:44132 }
[email protected]1010f7d2008-08-06 16:29:44133#endif
initial.commitd7cae122008-07-26 21:49:38134}
[email protected]cb571e752012-05-09 10:50:10135
[email protected]ff9ed9f2014-05-02 17:59:42136// Test that all versions of the Override function of PathService do what they
[email protected]cb571e752012-05-09 10:50:10137// are supposed to do.
138TEST_F(PathServiceTest, Override) {
139 int my_special_key = 666;
[email protected]ea1a3f62012-11-16 20:34:23140 base::ScopedTempDir temp_dir;
[email protected]cb571e752012-05-09 10:50:10141 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
[email protected]023ad6ab2013-02-17 05:07:23142 base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache"));
[email protected]cb571e752012-05-09 10:50:10143 // 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));
[email protected]7567484142013-07-11 17:36:07146 EXPECT_TRUE(base::PathExists(fake_cache_dir));
[email protected]cb571e752012-05-09 10:50:10147
[email protected]023ad6ab2013-02-17 05:07:23148 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2"));
[email protected]cb571e752012-05-09 10:50:10149 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter.
150 PathService::OverrideAndCreateIfNeeded(my_special_key,
151 fake_cache_dir2,
[email protected]ff9ed9f2014-05-02 17:59:42152 false,
[email protected]cb571e752012-05-09 10:50:10153 false);
[email protected]7567484142013-07-11 17:36:07154 EXPECT_FALSE(base::PathExists(fake_cache_dir2));
[email protected]cb571e752012-05-09 10:50:10155 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
156 fake_cache_dir2,
[email protected]ff9ed9f2014-05-02 17:59:42157 false,
[email protected]cb571e752012-05-09 10:50:10158 true));
[email protected]7567484142013-07-11 17:36:07159 EXPECT_TRUE(base::PathExists(fake_cache_dir2));
[email protected]ff9ed9f2014-05-02 17:59:42160
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
[email protected]cb571e752012-05-09 10:50:10187}
[email protected]d6b3af92012-09-26 19:05:12188
189// Check if multiple overrides can co-exist.
190TEST_F(PathServiceTest, OverrideMultiple) {
191 int my_special_key = 666;
[email protected]ea1a3f62012-11-16 20:34:23192 base::ScopedTempDir temp_dir;
[email protected]d6b3af92012-09-26 19:05:12193 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
[email protected]023ad6ab2013-02-17 05:07:23194 base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1"));
[email protected]d6b3af92012-09-26 19:05:12195 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1));
[email protected]7567484142013-07-11 17:36:07196 EXPECT_TRUE(base::PathExists(fake_cache_dir1));
[email protected]e5c2a22e2014-03-06 20:42:30197 ASSERT_EQ(1, base::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1));
[email protected]d6b3af92012-09-26 19:05:12198
[email protected]023ad6ab2013-02-17 05:07:23199 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2"));
[email protected]d6b3af92012-09-26 19:05:12200 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2));
[email protected]7567484142013-07-11 17:36:07201 EXPECT_TRUE(base::PathExists(fake_cache_dir2));
[email protected]e5c2a22e2014-03-06 20:42:30202 ASSERT_EQ(1, base::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1));
[email protected]d6b3af92012-09-26 19:05:12203
[email protected]023ad6ab2013-02-17 05:07:23204 base::FilePath result;
[email protected]d6b3af92012-09-26 19:05:12205 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.
[email protected]7567484142013-07-11 17:36:07208 EXPECT_TRUE(base::PathExists(result.AppendASCII("t1")));
[email protected]d6b3af92012-09-26 19:05:12209 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result));
[email protected]7567484142013-07-11 17:36:07210 EXPECT_TRUE(base::PathExists(result.AppendASCII("t2")));
[email protected]d6b3af92012-09-26 19:05:12211}
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
[email protected]023ad6ab2013-02-17 05:07:23218 base::FilePath original_user_data_dir;
[email protected]d6b3af92012-09-26 19:05:12219 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir));
220 EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP));
221
[email protected]ea1a3f62012-11-16 20:34:23222 base::ScopedTempDir temp_dir;
[email protected]d6b3af92012-09-26 19:05:12223 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
224 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path()));
[email protected]023ad6ab2013-02-17 05:07:23225 base::FilePath new_user_data_dir;
[email protected]d6b3af92012-09-26 19:05:12226 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}