[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] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 8 | #include "base/file_path.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame^] | 9 | #include "base/file_util.h" |
| 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] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 29 | FilePath path; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | bool result = PathService::Get(dir_type, &path); |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 31 | // 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] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 34 | #if defined(OS_POSIX) |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 35 | // 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] | 36 | // exist. |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 37 | if (dir_type == base::DIR_CACHE) |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 38 | check_path_exists = false; |
[email protected] | d8a80d6 | 2010-11-23 22:39:30 | [diff] [blame] | 39 | #endif |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 40 | #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] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 44 | check_path_exists = false; |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 45 | #endif |
| 46 | #if defined(OS_WIN) |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 47 | 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] | 9dbad6f | 2012-11-15 01:01:51 | [diff] [blame] | 60 | if (base::win::GetVersion() < base::win::VERSION_WIN7) |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 61 | check_path_exists = false; |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 62 | } |
| 63 | #endif |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 64 | return result && !path.empty() && (!check_path_exists || |
| 65 | file_util::PathExists(path)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 68 | #if defined(OS_WIN) |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 69 | // 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] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 72 | bool ReturnsInvalidPath(int dir_type) { |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 73 | FilePath path; |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 74 | bool result = PathService::Get(dir_type, &path); |
[email protected] | 0cfda1e | 2008-08-07 23:59:04 | [diff] [blame] | 75 | return !result && path.empty(); |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 76 | } |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 77 | #endif |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 78 | |
| 79 | } // namespace |
| 80 | |
[email protected] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 81 | // On the Mac this winds up using some autoreleased objects, so we need to |
| 82 | // be a PlatformTest. |
| 83 | typedef PlatformTest PathServiceTest; |
| 84 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 85 | // 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] | ed2f233 | 2008-08-20 15:59:49 | [diff] [blame] | 89 | TEST_F(PathServiceTest, Get) { |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 90 | for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) { |
[email protected] | aa91eb9 | 2011-08-26 16:43:59 | [diff] [blame] | 91 | #if defined(OS_ANDROID) |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 92 | if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP) |
| 93 | continue; // Android doesn't implement FILE_MODULE and DIR_USER_DESKTOP; |
[email protected] | ce576fe2 | 2012-09-25 18:16:25 | [diff] [blame] | 94 | #elif defined(OS_IOS) |
| 95 | if (key == base::DIR_USER_DESKTOP) |
| 96 | continue; // iOS doesn't implement DIR_USER_DESKTOP; |
[email protected] | aa91eb9 | 2011-08-26 16:43:59 | [diff] [blame] | 97 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 98 | EXPECT_PRED1(ReturnsValidPath, key); |
| 99 | } |
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 100 | #if defined(OS_WIN) |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 101 | 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] | 102 | 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] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 114 | } |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 115 | |
| 116 | if (valid) |
| 117 | EXPECT_TRUE(ReturnsValidPath(key)) << key; |
| 118 | else |
| 119 | EXPECT_TRUE(ReturnsInvalidPath(key)) << key; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 120 | } |
[email protected] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 121 | #elif defined(OS_MACOSX) |
| 122 | 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] | 123 | 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] | 405a64b | 2009-09-16 21:03:44 | [diff] [blame] | 134 | } |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 135 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 136 | } |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 137 | |
| 138 | // test that all versions of the Override function of PathService do what they |
| 139 | // are supposed to do. |
| 140 | TEST_F(PathServiceTest, Override) { |
| 141 | int my_special_key = 666; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame^] | 142 | base::ScopedTempDir temp_dir; |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 143 | 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] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 161 | |
| 162 | // Check if multiple overrides can co-exist. |
| 163 | TEST_F(PathServiceTest, OverrideMultiple) { |
| 164 | int my_special_key = 666; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame^] | 165 | base::ScopedTempDir temp_dir; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 166 | 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 | |
| 186 | TEST_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] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame^] | 195 | base::ScopedTempDir temp_dir; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 196 | 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 | } |