[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 5 | #include "base/path_service.h" |
| 6 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | #include "base/basictypes.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 9 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 10 | #include "base/files/scoped_temp_dir.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | 1f4ae16 | 2012-09-20 01:59:36 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest-spi.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 1f4ae16 | 2012-09-20 01:59:36 | [diff] [blame] | 15 | #include "testing/platform_test.h" |
[email protected] | e5738a0 | 2012-09-20 00:13:40 | [diff] [blame] | 16 | |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 17 | #if defined(OS_WIN) |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 18 | #include "base/win/windows_version.h" |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 19 | #endif |
| 20 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | namespace { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 22 | |
| 23 | // Returns true if PathService::Get returns true and sets the path parameter |
| 24 | // to non-empty for the given PathService::DirType enumeration value. |
| 25 | bool ReturnsValidPath(int dir_type) { |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 26 | base::FilePath path; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 27 | bool result = PathService::Get(dir_type, &path); |
[email protected] | f4f5c92 | 2013-01-25 23:00:55 | [diff] [blame] | 28 | |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 29 | // 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] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 32 | #if defined(OS_POSIX) |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 33 | // If chromium has never been started on this account, the cache path may not |
[email protected] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 34 | // exist. |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 35 | if (dir_type == base::DIR_CACHE) |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 36 | check_path_exists = false; |
[email protected] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 37 | #endif |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 38 | #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] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 42 | check_path_exists = false; |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 43 | #endif |
| 44 | #if defined(OS_WIN) |
[email protected] | da4d4fb | 2014-08-08 18:17:53 | [diff] [blame] | 45 | if (dir_type == base::DIR_TASKBAR_PINS) { |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 46 | // There is no pinned-to-taskbar shortcuts prior to Win7. |
[email protected] | 9dbad6f | 2012-11-15 01:01:51 | [diff] [blame] | 47 | if (base::win::GetVersion() < base::win::VERSION_WIN7) |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 48 | check_path_exists = false; |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 49 | } |
| 50 | #endif |
[email protected] | 0d7717fa | 2013-03-19 22:06:20 | [diff] [blame] | 51 | #if defined(OS_MACOSX) |
[email protected] | f4f5c92 | 2013-01-25 23:00:55 | [diff] [blame] | 52 | 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] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 61 | return result && !path.empty() && (!check_path_exists || |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 62 | base::PathExists(path)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 65 | #if defined(OS_WIN) |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 66 | // 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] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 69 | bool ReturnsInvalidPath(int dir_type) { |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 70 | base::FilePath path; |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 71 | bool result = PathService::Get(dir_type, &path); |
[email protected] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 72 | return !result && path.empty(); |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 73 | } |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 74 | #endif |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 75 | |
| 76 | } // namespace |
| 77 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 78 | // On the Mac this winds up using some autoreleased objects, so we need to |
| 79 | // be a PlatformTest. |
| 80 | typedef PlatformTest PathServiceTest; |
| 81 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 82 | // 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] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 86 | TEST_F(PathServiceTest, Get) { |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 87 | for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) { |
[email protected] | aa91eb9 | 2011-08-26 16:43:59 | [diff] [blame] | 88 | #if defined(OS_ANDROID) |
[email protected] | ffaee18e | 2014-02-19 20:34:23 | [diff] [blame] | 89 | if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP || |
| 90 | key == base::DIR_HOME) |
| 91 | continue; // Android doesn't implement these. |
[email protected] | ce576fe2 | 2012-09-25 18:16:25 | [diff] [blame] | 92 | #elif defined(OS_IOS) |
| 93 | if (key == base::DIR_USER_DESKTOP) |
| 94 | continue; // iOS doesn't implement DIR_USER_DESKTOP; |
[email protected] | aa91eb9 | 2011-08-26 16:43:59 | [diff] [blame] | 95 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 96 | EXPECT_PRED1(ReturnsValidPath, key); |
| 97 | } |
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 98 | #if defined(OS_WIN) |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 99 | for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) { |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 100 | 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] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 112 | } |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 113 | |
| 114 | if (valid) |
| 115 | EXPECT_TRUE(ReturnsValidPath(key)) << key; |
| 116 | else |
| 117 | EXPECT_TRUE(ReturnsInvalidPath(key)) << key; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 118 | } |
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 119 | #elif defined(OS_MACOSX) |
| 120 | for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) { |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 121 | 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] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 132 | } |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 133 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 134 | } |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 135 | |
[email protected] | ff9ed9f | 2014-05-02 17:59:42 | [diff] [blame] | 136 | // Test that all versions of the Override function of PathService do what they |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 137 | // are supposed to do. |
| 138 | TEST_F(PathServiceTest, Override) { |
| 139 | int my_special_key = 666; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 140 | base::ScopedTempDir temp_dir; |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 141 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 142 | base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 143 | // 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] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 146 | EXPECT_TRUE(base::PathExists(fake_cache_dir)); |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 147 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 148 | base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 149 | // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. |
| 150 | PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 151 | fake_cache_dir2, |
[email protected] | ff9ed9f | 2014-05-02 17:59:42 | [diff] [blame] | 152 | false, |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 153 | false); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 154 | EXPECT_FALSE(base::PathExists(fake_cache_dir2)); |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 155 | EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 156 | fake_cache_dir2, |
[email protected] | ff9ed9f | 2014-05-02 17:59:42 | [diff] [blame] | 157 | false, |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 158 | true)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 159 | EXPECT_TRUE(base::PathExists(fake_cache_dir2)); |
[email protected] | ff9ed9f | 2014-05-02 17:59:42 | [diff] [blame] | 160 | |
| 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] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 187 | } |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 188 | |
| 189 | // Check if multiple overrides can co-exist. |
| 190 | TEST_F(PathServiceTest, OverrideMultiple) { |
| 191 | int my_special_key = 666; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 192 | base::ScopedTempDir temp_dir; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 193 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 194 | base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1")); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 195 | EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 196 | EXPECT_TRUE(base::PathExists(fake_cache_dir1)); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 197 | ASSERT_EQ(1, base::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1)); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 198 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 199 | base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2")); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 200 | EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 201 | EXPECT_TRUE(base::PathExists(fake_cache_dir2)); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 202 | ASSERT_EQ(1, base::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1)); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 203 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 204 | base::FilePath result; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 205 | 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] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 208 | EXPECT_TRUE(base::PathExists(result.AppendASCII("t1"))); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 209 | EXPECT_TRUE(PathService::Get(my_special_key + 1, &result)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 210 | EXPECT_TRUE(base::PathExists(result.AppendASCII("t2"))); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | TEST_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] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 218 | base::FilePath original_user_data_dir; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 219 | EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir)); |
| 220 | EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP)); |
| 221 | |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 222 | base::ScopedTempDir temp_dir; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 223 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 224 | EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 225 | base::FilePath new_user_data_dir; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 226 | 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 | } |