[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] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 10 | #include "base/files/scoped_temp_dir.h" |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 11 | #include "base/string_util.h" |
| 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) |
| 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | namespace { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 25 | |
| 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) { |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 29 | base::FilePath path; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | bool result = PathService::Get(dir_type, &path); |
[email protected] | f4f5c92 | 2013-01-25 23:00:55 | [diff] [blame] | 31 | |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 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; |
[email protected] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 35 | #if defined(OS_POSIX) |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 36 | // 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] | 37 | // exist. |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 38 | if (dir_type == base::DIR_CACHE) |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 39 | check_path_exists = false; |
[email protected] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 40 | #endif |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 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) |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 45 | check_path_exists = false; |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 46 | #endif |
| 47 | #if defined(OS_WIN) |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 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)); |
| 58 | } |
| 59 | } else if (dir_type == base::DIR_TASKBAR_PINS) { |
| 60 | // There is no pinned-to-taskbar shortcuts prior to Win7. |
[email protected] | 9dbad6f | 2012-11-15 01:01:51 | [diff] [blame] | 61 | if (base::win::GetVersion() < base::win::VERSION_WIN7) |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 62 | check_path_exists = false; |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 63 | } |
| 64 | #endif |
[email protected] | 0d7717fa | 2013-03-19 22:06:20 | [diff] [blame] | 65 | #if defined(OS_MACOSX) |
[email protected] | f4f5c92 | 2013-01-25 23:00:55 | [diff] [blame] | 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; |
| 70 | } |
| 71 | #else |
| 72 | if (path.ReferencesParent()) |
| 73 | return false; |
| 74 | #endif |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 75 | return result && !path.empty() && (!check_path_exists || |
| 76 | file_util::PathExists(path)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 79 | #if defined(OS_WIN) |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 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. |
[email protected] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 83 | bool ReturnsInvalidPath(int dir_type) { |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 84 | base::FilePath path; |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 85 | bool result = PathService::Get(dir_type, &path); |
[email protected] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 86 | return !result && path.empty(); |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 87 | } |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 88 | #endif |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 89 | |
| 90 | } // namespace |
| 91 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 92 | // On the Mac this winds up using some autoreleased objects, so we need to |
| 93 | // be a PlatformTest. |
| 94 | typedef PlatformTest PathServiceTest; |
| 95 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 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.) |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 100 | TEST_F(PathServiceTest, Get) { |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 101 | for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) { |
[email protected] | aa91eb9 | 2011-08-26 16:43:59 | [diff] [blame] | 102 | #if defined(OS_ANDROID) |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 103 | if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP) |
| 104 | continue; // Android doesn't implement FILE_MODULE and DIR_USER_DESKTOP; |
[email protected] | ce576fe2 | 2012-09-25 18:16:25 | [diff] [blame] | 105 | #elif defined(OS_IOS) |
| 106 | if (key == base::DIR_USER_DESKTOP) |
| 107 | continue; // iOS doesn't implement DIR_USER_DESKTOP; |
[email protected] | aa91eb9 | 2011-08-26 16:43:59 | [diff] [blame] | 108 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 109 | EXPECT_PRED1(ReturnsValidPath, key); |
| 110 | } |
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 111 | #if defined(OS_WIN) |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 112 | 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] | 113 | bool valid = true; |
| 114 | switch(key) { |
| 115 | case base::DIR_LOCAL_APP_DATA_LOW: |
| 116 | // DIR_LOCAL_APP_DATA_LOW is not supported prior Vista and is expected |
| 117 | // to fail. |
| 118 | valid = base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 119 | break; |
| 120 | case base::DIR_APP_SHORTCUTS: |
| 121 | // DIR_APP_SHORTCUTS is not supported prior Windows 8 and is expected to |
| 122 | // fail. |
| 123 | valid = base::win::GetVersion() >= base::win::VERSION_WIN8; |
| 124 | break; |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 125 | } |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 126 | |
| 127 | if (valid) |
| 128 | EXPECT_TRUE(ReturnsValidPath(key)) << key; |
| 129 | else |
| 130 | EXPECT_TRUE(ReturnsInvalidPath(key)) << key; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 131 | } |
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 132 | #elif defined(OS_MACOSX) |
| 133 | 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] | 134 | EXPECT_PRED1(ReturnsValidPath, key); |
| 135 | } |
| 136 | #elif defined(OS_ANDROID) |
| 137 | for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END; |
| 138 | ++key) { |
| 139 | EXPECT_PRED1(ReturnsValidPath, key); |
| 140 | } |
| 141 | #elif defined(OS_POSIX) |
| 142 | for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END; |
| 143 | ++key) { |
| 144 | EXPECT_PRED1(ReturnsValidPath, key); |
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 145 | } |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 146 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 147 | } |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 148 | |
| 149 | // test that all versions of the Override function of PathService do what they |
| 150 | // are supposed to do. |
| 151 | TEST_F(PathServiceTest, Override) { |
| 152 | int my_special_key = 666; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 153 | base::ScopedTempDir temp_dir; |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 154 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 155 | base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 156 | // PathService::Override should always create the path provided if it doesn't |
| 157 | // exist. |
| 158 | EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); |
| 159 | EXPECT_TRUE(file_util::PathExists(fake_cache_dir)); |
| 160 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 161 | base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 162 | // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. |
| 163 | PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 164 | fake_cache_dir2, |
| 165 | false); |
| 166 | EXPECT_FALSE(file_util::PathExists(fake_cache_dir2)); |
| 167 | EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, |
| 168 | fake_cache_dir2, |
| 169 | true)); |
| 170 | EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); |
| 171 | } |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 172 | |
| 173 | // Check if multiple overrides can co-exist. |
| 174 | TEST_F(PathServiceTest, OverrideMultiple) { |
| 175 | int my_special_key = 666; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 176 | base::ScopedTempDir temp_dir; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 177 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 178 | base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1")); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 179 | EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); |
| 180 | EXPECT_TRUE(file_util::PathExists(fake_cache_dir1)); |
| 181 | ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1)); |
| 182 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 183 | base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2")); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 184 | EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2)); |
| 185 | EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); |
| 186 | ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1)); |
| 187 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 188 | base::FilePath result; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 189 | EXPECT_TRUE(PathService::Get(my_special_key, &result)); |
| 190 | // Override might have changed the path representation but our test file |
| 191 | // should be still there. |
| 192 | EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t1"))); |
| 193 | EXPECT_TRUE(PathService::Get(my_special_key + 1, &result)); |
| 194 | EXPECT_TRUE(file_util::PathExists(result.AppendASCII("t2"))); |
| 195 | } |
| 196 | |
| 197 | TEST_F(PathServiceTest, RemoveOverride) { |
| 198 | // Before we start the test we have to call RemoveOverride at least once to |
| 199 | // clear any overrides that might have been left from other tests. |
| 200 | PathService::RemoveOverride(base::DIR_TEMP); |
| 201 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 202 | base::FilePath original_user_data_dir; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 203 | EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir)); |
| 204 | EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP)); |
| 205 | |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 206 | base::ScopedTempDir temp_dir; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 207 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 208 | EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 209 | base::FilePath new_user_data_dir; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 210 | EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); |
| 211 | EXPECT_NE(original_user_data_dir, new_user_data_dir); |
| 212 | |
| 213 | EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP)); |
| 214 | EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); |
| 215 | EXPECT_EQ(original_user_data_dir, new_user_data_dir); |
| 216 | } |