blob: 0b96c123e97abbdca9490c1e3edf593f120651ba [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]640517f2008-10-30 23:54:048#include "base/file_path.h"
[email protected]ea1a3f62012-11-16 20:34:239#include "base/file_util.h"
10#include "base/files/scoped_temp_dir.h"
[email protected]dea1d7d2012-09-20 16:24:5211#include "base/string_util.h"
12#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)
18#include <userenv.h>
19#include "base/win/windows_version.h"
20// userenv.dll is required for GetDefaultUserProfileDirectory().
21#pragma comment(lib, "userenv.lib")
22#endif
23
initial.commitd7cae122008-07-26 21:49:3824namespace {
initial.commitd7cae122008-07-26 21:49:3825
26// Returns true if PathService::Get returns true and sets the path parameter
27// to non-empty for the given PathService::DirType enumeration value.
28bool ReturnsValidPath(int dir_type) {
[email protected]640517f2008-10-30 23:54:0429 FilePath path;
initial.commitd7cae122008-07-26 21:49:3830 bool result = PathService::Get(dir_type, &path);
[email protected]e5f9d822012-11-06 22:27:0131 // Some paths might not exist on some platforms in which case confirming
32 // |result| is true and !path.empty() is the best we can do.
33 bool check_path_exists = true;
[email protected]d8a80d62010-11-23 22:39:3034#if defined(OS_POSIX)
[email protected]b411da32010-11-24 02:23:1535 // If chromium has never been started on this account, the cache path may not
[email protected]d8a80d62010-11-23 22:39:3036 // exist.
[email protected]b411da32010-11-24 02:23:1537 if (dir_type == base::DIR_CACHE)
[email protected]e5f9d822012-11-06 22:27:0138 check_path_exists = false;
[email protected]d8a80d62010-11-23 22:39:3039#endif
[email protected]dea1d7d2012-09-20 16:24:5240#if defined(OS_LINUX)
41 // On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop),
42 // but it doesn't exist.
43 if (dir_type == base::DIR_USER_DESKTOP)
[email protected]e5f9d822012-11-06 22:27:0144 check_path_exists = false;
[email protected]dea1d7d2012-09-20 16:24:5245#endif
46#if defined(OS_WIN)
[email protected]e5f9d822012-11-06 22:27:0147 if (dir_type == base::DIR_DEFAULT_USER_QUICK_LAUNCH) {
48 // On Windows XP, the Quick Launch folder for the "Default User" doesn't
49 // exist by default. At least confirm that the path returned begins with the
50 // Default User's profile path.
51 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
52 wchar_t default_profile_path[MAX_PATH];
53 DWORD size = arraysize(default_profile_path);
54 return (result &&
55 ::GetDefaultUserProfileDirectory(default_profile_path, &size) &&
56 StartsWith(path.value(), default_profile_path, false));
57 }
58 } else if (dir_type == base::DIR_TASKBAR_PINS) {
59 // There is no pinned-to-taskbar shortcuts prior to Win7.
[email protected]9dbad6f2012-11-15 01:01:5160 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]e5f9d822012-11-06 22:27:0161 check_path_exists = false;
[email protected]dea1d7d2012-09-20 16:24:5262 }
63#endif
[email protected]e5f9d822012-11-06 22:27:0164 return result && !path.empty() && (!check_path_exists ||
65 file_util::PathExists(path));
initial.commitd7cae122008-07-26 21:49:3866}
67
[email protected]6723f832008-08-11 15:38:2768#if defined(OS_WIN)
[email protected]b2721b02012-08-30 09:16:5569// Function to test any directory keys that are not supported on some versions
70// of Windows. Checks that the function fails and that the returned path is
71// empty.
[email protected]0cfda1e2008-08-07 23:59:0472bool ReturnsInvalidPath(int dir_type) {
[email protected]b65de8b92009-09-14 19:36:3173 FilePath path;
[email protected]b2721b02012-08-30 09:16:5574 bool result = PathService::Get(dir_type, &path);
[email protected]0cfda1e2008-08-07 23:59:0475 return !result && path.empty();
[email protected]09ad1e622008-08-07 20:23:0976}
[email protected]6723f832008-08-11 15:38:2777#endif
[email protected]09ad1e622008-08-07 20:23:0978
79} // namespace
80
[email protected]ed2f2332008-08-20 15:59:4981// On the Mac this winds up using some autoreleased objects, so we need to
82// be a PlatformTest.
83typedef PlatformTest PathServiceTest;
84
initial.commitd7cae122008-07-26 21:49:3885// Test that all PathService::Get calls return a value and a true result
86// in the development environment. (This test was created because a few
87// later changes to Get broke the semantics of the function and yielded the
88// correct value while returning false.)
[email protected]ed2f2332008-08-20 15:59:4989TEST_F(PathServiceTest, Get) {
[email protected]dea1d7d2012-09-20 16:24:5290 for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) {
[email protected]aa91eb92011-08-26 16:43:5991#if defined(OS_ANDROID)
[email protected]dea1d7d2012-09-20 16:24:5292 if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP)
93 continue; // Android doesn't implement FILE_MODULE and DIR_USER_DESKTOP;
[email protected]ce576fe22012-09-25 18:16:2594#elif defined(OS_IOS)
95 if (key == base::DIR_USER_DESKTOP)
96 continue; // iOS doesn't implement DIR_USER_DESKTOP;
[email protected]aa91eb92011-08-26 16:43:5997#endif
initial.commitd7cae122008-07-26 21:49:3898 EXPECT_PRED1(ReturnsValidPath, key);
99 }
[email protected]405a64b2009-09-16 21:03:44100#if defined(OS_WIN)
[email protected]1010f7d2008-08-06 16:29:44101 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) {
[email protected]b2721b02012-08-30 09:16:55102 bool valid = true;
103 switch(key) {
104 case base::DIR_LOCAL_APP_DATA_LOW:
105 // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected
106 // to fail.
107 valid = base::win::GetVersion() >= base::win::VERSION_VISTA;
108 break;
109 case base::DIR_APP_SHORTCUTS:
110 // DIR_APP_SHORTCUTS is not supported prior Windows 8 and is expected to
111 // fail.
112 valid = base::win::GetVersion() >= base::win::VERSION_WIN8;
113 break;
[email protected]09ad1e622008-08-07 20:23:09114 }
[email protected]b2721b02012-08-30 09:16:55115
116 if (valid)
117 EXPECT_TRUE(ReturnsValidPath(key)) << key;
118 else
119 EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
[email protected]1010f7d2008-08-06 16:29:44120 }
[email protected]405a64b2009-09-16 21:03:44121#elif defined(OS_MACOSX)
122 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) {
[email protected]dea1d7d2012-09-20 16:24:52123 EXPECT_PRED1(ReturnsValidPath, key);
124 }
125#elif defined(OS_ANDROID)
126 for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END;
127 ++key) {
128 EXPECT_PRED1(ReturnsValidPath, key);
129 }
130#elif defined(OS_POSIX)
131 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END;
132 ++key) {
133 EXPECT_PRED1(ReturnsValidPath, key);
[email protected]405a64b2009-09-16 21:03:44134 }
[email protected]1010f7d2008-08-06 16:29:44135#endif
initial.commitd7cae122008-07-26 21:49:38136}
[email protected]cb571e752012-05-09 10:50:10137
138// test that all versions of the Override function of PathService do what they
139// are supposed to do.
140TEST_F(PathServiceTest, Override) {
141 int my_special_key = 666;
[email protected]ea1a3f62012-11-16 20:34:23142 base::ScopedTempDir temp_dir;
[email protected]cb571e752012-05-09 10:50:10143 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
144 FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache"));
145 // PathService::Override should always create the path provided if it doesn't
146 // exist.
147 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir));
148 EXPECT_TRUE(file_util::PathExists(fake_cache_dir));
149
150 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2"));
151 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter.
152 PathService::OverrideAndCreateIfNeeded(my_special_key,
153 fake_cache_dir2,
154 false);
155 EXPECT_FALSE(file_util::PathExists(fake_cache_dir2));
156 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
157 fake_cache_dir2,
158 true));
159 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2));
160}
[email protected]d6b3af92012-09-26 19:05:12161
162// Check if multiple overrides can co-exist.
163TEST_F(PathServiceTest, OverrideMultiple) {
164 int my_special_key = 666;
[email protected]ea1a3f62012-11-16 20:34:23165 base::ScopedTempDir temp_dir;
[email protected]d6b3af92012-09-26 19:05:12166 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
167 FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1"));
168 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1));
169 EXPECT_TRUE(file_util::PathExists(fake_cache_dir1));
170 ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1));
171
172 FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2"));
173 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2));
174 EXPECT_TRUE(file_util::PathExists(fake_cache_dir2));
175 ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1));
176
177 FilePath result;
178 EXPECT_TRUE(PathService::Get(my_special_key, &result));
179 // Override might have changed the path representation but our test file
180 // should be still there.
181 EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t1")));
182 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result));
183 EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t2")));
184}
185
186TEST_F(PathServiceTest, RemoveOverride) {
187 // Before we start the test we have to call RemoveOverride at least once to
188 // clear any overrides that might have been left from other tests.
189 PathService::RemoveOverride(base::DIR_TEMP);
190
191 FilePath original_user_data_dir;
192 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir));
193 EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP));
194
[email protected]ea1a3f62012-11-16 20:34:23195 base::ScopedTempDir temp_dir;
[email protected]d6b3af92012-09-26 19:05:12196 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
197 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path()));
198 FilePath new_user_data_dir;
199 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir));
200 EXPECT_NE(original_user_data_dir, new_user_data_dir);
201
202 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP));
203 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir));
204 EXPECT_EQ(original_user_data_dir, new_user_data_dir);
205}