Make a directory in which to put bisect-related modules.
[chromium-blink-merge.git] / base / path_service_unittest.cc
blob7a70a89c41a97b03abc75e6a56994ec0cbf00da8
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/basictypes.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/strings/string_util.h"
12 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest-spi.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
17 #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
24 namespace {
26 // Returns true if PathService::Get returns true and sets the path parameter
27 // to non-empty for the given PathService::DirType enumeration value.
28 bool ReturnsValidPath(int dir_type) {
29 base::FilePath path;
30 bool result = PathService::Get(dir_type, &path);
32 // Some paths might not exist on some platforms in which case confirming
33 // |result| is true and !path.empty() is the best we can do.
34 bool check_path_exists = true;
35 #if defined(OS_POSIX)
36 // If chromium has never been started on this account, the cache path may not
37 // exist.
38 if (dir_type == base::DIR_CACHE)
39 check_path_exists = false;
40 #endif
41 #if defined(OS_LINUX)
42 // On the linux try-bots: a path is returned (e.g. /home/chrome-bot/Desktop),
43 // but it doesn't exist.
44 if (dir_type == base::DIR_USER_DESKTOP)
45 check_path_exists = false;
46 #endif
47 #if defined(OS_WIN)
48 if (dir_type == base::DIR_DEFAULT_USER_QUICK_LAUNCH) {
49 // On Windows XP, the Quick Launch folder for the "Default User" doesn't
50 // exist by default. At least confirm that the path returned begins with the
51 // Default User's profile path.
52 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
53 wchar_t default_profile_path[MAX_PATH];
54 DWORD size = arraysize(default_profile_path);
55 return (result &&
56 ::GetDefaultUserProfileDirectory(default_profile_path, &size) &&
57 StartsWith(path.value(), default_profile_path, false));
59 } else if (dir_type == base::DIR_TASKBAR_PINS) {
60 // There is no pinned-to-taskbar shortcuts prior to Win7.
61 if (base::win::GetVersion() < base::win::VERSION_WIN7)
62 check_path_exists = false;
64 #endif
65 #if defined(OS_MACOSX)
66 if (dir_type != base::DIR_EXE && dir_type != base::DIR_MODULE &&
67 dir_type != base::FILE_EXE && dir_type != base::FILE_MODULE) {
68 if (path.ReferencesParent())
69 return false;
71 #else
72 if (path.ReferencesParent())
73 return false;
74 #endif
75 return result && !path.empty() && (!check_path_exists ||
76 base::PathExists(path));
79 #if defined(OS_WIN)
80 // Function to test any directory keys that are not supported on some versions
81 // of Windows. Checks that the function fails and that the returned path is
82 // empty.
83 bool ReturnsInvalidPath(int dir_type) {
84 base::FilePath path;
85 bool result = PathService::Get(dir_type, &path);
86 return !result && path.empty();
88 #endif
90 } // namespace
92 // On the Mac this winds up using some autoreleased objects, so we need to
93 // be a PlatformTest.
94 typedef PlatformTest PathServiceTest;
96 // Test that all PathService::Get calls return a value and a true result
97 // in the development environment. (This test was created because a few
98 // later changes to Get broke the semantics of the function and yielded the
99 // correct value while returning false.)
100 TEST_F(PathServiceTest, Get) {
101 for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) {
102 #if defined(OS_ANDROID)
103 if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP ||
104 key == base::DIR_HOME)
105 continue; // Android doesn't implement these.
106 #elif defined(OS_IOS)
107 if (key == base::DIR_USER_DESKTOP)
108 continue; // iOS doesn't implement DIR_USER_DESKTOP;
109 #endif
110 EXPECT_PRED1(ReturnsValidPath, key);
112 #if defined(OS_WIN)
113 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) {
114 bool valid = true;
115 switch(key) {
116 case base::DIR_LOCAL_APP_DATA_LOW:
117 // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected
118 // to fail.
119 valid = base::win::GetVersion() >= base::win::VERSION_VISTA;
120 break;
121 case base::DIR_APP_SHORTCUTS:
122 // DIR_APP_SHORTCUTS is not supported prior Windows 8 and is expected to
123 // fail.
124 valid = base::win::GetVersion() >= base::win::VERSION_WIN8;
125 break;
128 if (valid)
129 EXPECT_TRUE(ReturnsValidPath(key)) << key;
130 else
131 EXPECT_TRUE(ReturnsInvalidPath(key)) << key;
133 #elif defined(OS_MACOSX)
134 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) {
135 EXPECT_PRED1(ReturnsValidPath, key);
137 #elif defined(OS_ANDROID)
138 for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END;
139 ++key) {
140 EXPECT_PRED1(ReturnsValidPath, key);
142 #elif defined(OS_POSIX)
143 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END;
144 ++key) {
145 EXPECT_PRED1(ReturnsValidPath, key);
147 #endif
150 // Test that all versions of the Override function of PathService do what they
151 // are supposed to do.
152 TEST_F(PathServiceTest, Override) {
153 int my_special_key = 666;
154 base::ScopedTempDir temp_dir;
155 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
156 base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache"));
157 // PathService::Override should always create the path provided if it doesn't
158 // exist.
159 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir));
160 EXPECT_TRUE(base::PathExists(fake_cache_dir));
162 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2"));
163 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter.
164 PathService::OverrideAndCreateIfNeeded(my_special_key,
165 fake_cache_dir2,
166 false,
167 false);
168 EXPECT_FALSE(base::PathExists(fake_cache_dir2));
169 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
170 fake_cache_dir2,
171 false,
172 true));
173 EXPECT_TRUE(base::PathExists(fake_cache_dir2));
175 #if defined(OS_POSIX)
176 base::FilePath non_existent(
177 base::MakeAbsoluteFilePath(temp_dir.path()).AppendASCII("non_existent"));
178 EXPECT_TRUE(non_existent.IsAbsolute());
179 EXPECT_FALSE(base::PathExists(non_existent));
180 #if !defined(OS_ANDROID)
181 // This fails because MakeAbsoluteFilePath fails for non-existent files.
182 // Earlier versions of Bionic libc don't fail for non-existent files, so
183 // skip this check on Android.
184 EXPECT_FALSE(PathService::OverrideAndCreateIfNeeded(my_special_key,
185 non_existent,
186 false,
187 false));
188 #endif
189 // This works because indicating that |non_existent| is absolute skips the
190 // internal MakeAbsoluteFilePath call.
191 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key,
192 non_existent,
193 true,
194 false));
195 // Check that the path has been overridden and no directory was created.
196 EXPECT_FALSE(base::PathExists(non_existent));
197 base::FilePath path;
198 EXPECT_TRUE(PathService::Get(my_special_key, &path));
199 EXPECT_EQ(non_existent, path);
200 #endif
203 // Check if multiple overrides can co-exist.
204 TEST_F(PathServiceTest, OverrideMultiple) {
205 int my_special_key = 666;
206 base::ScopedTempDir temp_dir;
207 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
208 base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1"));
209 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1));
210 EXPECT_TRUE(base::PathExists(fake_cache_dir1));
211 ASSERT_EQ(1, base::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1));
213 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2"));
214 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2));
215 EXPECT_TRUE(base::PathExists(fake_cache_dir2));
216 ASSERT_EQ(1, base::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1));
218 base::FilePath result;
219 EXPECT_TRUE(PathService::Get(my_special_key, &result));
220 // Override might have changed the path representation but our test file
221 // should be still there.
222 EXPECT_TRUE(base::PathExists(result.AppendASCII("t1")));
223 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result));
224 EXPECT_TRUE(base::PathExists(result.AppendASCII("t2")));
227 TEST_F(PathServiceTest, RemoveOverride) {
228 // Before we start the test we have to call RemoveOverride at least once to
229 // clear any overrides that might have been left from other tests.
230 PathService::RemoveOverride(base::DIR_TEMP);
232 base::FilePath original_user_data_dir;
233 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir));
234 EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP));
236 base::ScopedTempDir temp_dir;
237 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
238 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path()));
239 base::FilePath new_user_data_dir;
240 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir));
241 EXPECT_NE(original_user_data_dir, new_user_data_dir);
243 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP));
244 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir));
245 EXPECT_EQ(original_user_data_dir, new_user_data_dir);