| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/path_service.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/files/file_util.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" | |
| 16 | |
| 17 #if defined(OS_WIN) | |
| 18 #include "base/win/windows_version.h" | |
| 19 #endif | |
| 20 | |
| 21 namespace { | |
| 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) { | |
| 26 base::FilePath path; | |
| 27 bool result = PathService::Get(dir_type, &path); | |
| 28 | |
| 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; | |
| 32 #if defined(OS_POSIX) | |
| 33 // If chromium has never been started on this account, the cache path may not | |
| 34 // exist. | |
| 35 if (dir_type == base::DIR_CACHE) | |
| 36 check_path_exists = false; | |
| 37 #endif | |
| 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) | |
| 42 check_path_exists = false; | |
| 43 #endif | |
| 44 #if defined(OS_WIN) | |
| 45 if (dir_type == base::DIR_TASKBAR_PINS) { | |
| 46 // There is no pinned-to-taskbar shortcuts prior to Win7. | |
| 47 if (base::win::GetVersion() < base::win::VERSION_WIN7) | |
| 48 check_path_exists = false; | |
| 49 } | |
| 50 #endif | |
| 51 #if defined(OS_MACOSX) | |
| 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 | |
| 61 return result && !path.empty() && (!check_path_exists || | |
| 62 base::PathExists(path)); | |
| 63 } | |
| 64 | |
| 65 #if defined(OS_WIN) | |
| 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. | |
| 69 bool ReturnsInvalidPath(int dir_type) { | |
| 70 base::FilePath path; | |
| 71 bool result = PathService::Get(dir_type, &path); | |
| 72 return !result && path.empty(); | |
| 73 } | |
| 74 #endif | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 78 // On the Mac this winds up using some autoreleased objects, so we need to | |
| 79 // be a PlatformTest. | |
| 80 typedef PlatformTest PathServiceTest; | |
| 81 | |
| 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.) | |
| 86 TEST_F(PathServiceTest, Get) { | |
| 87 for (int key = base::PATH_START + 1; key < base::PATH_END; ++key) { | |
| 88 #if defined(OS_ANDROID) | |
| 89 if (key == base::FILE_MODULE || key == base::DIR_USER_DESKTOP || | |
| 90 key == base::DIR_HOME) | |
| 91 continue; // Android doesn't implement these. | |
| 92 #elif defined(OS_IOS) | |
| 93 if (key == base::DIR_USER_DESKTOP) | |
| 94 continue; // iOS doesn't implement DIR_USER_DESKTOP; | |
| 95 #endif | |
| 96 EXPECT_PRED1(ReturnsValidPath, key); | |
| 97 } | |
| 98 #if defined(OS_WIN) | |
| 99 for (int key = base::PATH_WIN_START + 1; key < base::PATH_WIN_END; ++key) { | |
| 100 bool valid = true; | |
| 101 if (key == base::DIR_APP_SHORTCUTS) | |
| 102 valid = base::win::GetVersion() >= base::win::VERSION_WIN8; | |
| 103 | |
| 104 if (valid) | |
| 105 EXPECT_TRUE(ReturnsValidPath(key)) << key; | |
| 106 else | |
| 107 EXPECT_TRUE(ReturnsInvalidPath(key)) << key; | |
| 108 } | |
| 109 #elif defined(OS_MACOSX) | |
| 110 for (int key = base::PATH_MAC_START + 1; key < base::PATH_MAC_END; ++key) { | |
| 111 EXPECT_PRED1(ReturnsValidPath, key); | |
| 112 } | |
| 113 #elif defined(OS_ANDROID) | |
| 114 for (int key = base::PATH_ANDROID_START + 1; key < base::PATH_ANDROID_END; | |
| 115 ++key) { | |
| 116 EXPECT_PRED1(ReturnsValidPath, key); | |
| 117 } | |
| 118 #elif defined(OS_POSIX) | |
| 119 for (int key = base::PATH_POSIX_START + 1; key < base::PATH_POSIX_END; | |
| 120 ++key) { | |
| 121 EXPECT_PRED1(ReturnsValidPath, key); | |
| 122 } | |
| 123 #endif | |
| 124 } | |
| 125 | |
| 126 // Test that all versions of the Override function of PathService do what they | |
| 127 // are supposed to do. | |
| 128 TEST_F(PathServiceTest, Override) { | |
| 129 int my_special_key = 666; | |
| 130 base::ScopedTempDir temp_dir; | |
| 131 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 132 base::FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); | |
| 133 // PathService::Override should always create the path provided if it doesn't | |
| 134 // exist. | |
| 135 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); | |
| 136 EXPECT_TRUE(base::PathExists(fake_cache_dir)); | |
| 137 | |
| 138 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); | |
| 139 // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. | |
| 140 PathService::OverrideAndCreateIfNeeded(my_special_key, | |
| 141 fake_cache_dir2, | |
| 142 false, | |
| 143 false); | |
| 144 EXPECT_FALSE(base::PathExists(fake_cache_dir2)); | |
| 145 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, | |
| 146 fake_cache_dir2, | |
| 147 false, | |
| 148 true)); | |
| 149 EXPECT_TRUE(base::PathExists(fake_cache_dir2)); | |
| 150 | |
| 151 #if defined(OS_POSIX) | |
| 152 base::FilePath non_existent( | |
| 153 base::MakeAbsoluteFilePath(temp_dir.path()).AppendASCII("non_existent")); | |
| 154 EXPECT_TRUE(non_existent.IsAbsolute()); | |
| 155 EXPECT_FALSE(base::PathExists(non_existent)); | |
| 156 #if !defined(OS_ANDROID) | |
| 157 // This fails because MakeAbsoluteFilePath fails for non-existent files. | |
| 158 // Earlier versions of Bionic libc don't fail for non-existent files, so | |
| 159 // skip this check on Android. | |
| 160 EXPECT_FALSE(PathService::OverrideAndCreateIfNeeded(my_special_key, | |
| 161 non_existent, | |
| 162 false, | |
| 163 false)); | |
| 164 #endif | |
| 165 // This works because indicating that |non_existent| is absolute skips the | |
| 166 // internal MakeAbsoluteFilePath call. | |
| 167 EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, | |
| 168 non_existent, | |
| 169 true, | |
| 170 false)); | |
| 171 // Check that the path has been overridden and no directory was created. | |
| 172 EXPECT_FALSE(base::PathExists(non_existent)); | |
| 173 base::FilePath path; | |
| 174 EXPECT_TRUE(PathService::Get(my_special_key, &path)); | |
| 175 EXPECT_EQ(non_existent, path); | |
| 176 #endif | |
| 177 } | |
| 178 | |
| 179 // Check if multiple overrides can co-exist. | |
| 180 TEST_F(PathServiceTest, OverrideMultiple) { | |
| 181 int my_special_key = 666; | |
| 182 base::ScopedTempDir temp_dir; | |
| 183 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 184 base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1")); | |
| 185 EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1)); | |
| 186 EXPECT_TRUE(base::PathExists(fake_cache_dir1)); | |
| 187 ASSERT_EQ(1, base::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1)); | |
| 188 | |
| 189 base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2")); | |
| 190 EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2)); | |
| 191 EXPECT_TRUE(base::PathExists(fake_cache_dir2)); | |
| 192 ASSERT_EQ(1, base::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1)); | |
| 193 | |
| 194 base::FilePath result; | |
| 195 EXPECT_TRUE(PathService::Get(my_special_key, &result)); | |
| 196 // Override might have changed the path representation but our test file | |
| 197 // should be still there. | |
| 198 EXPECT_TRUE(base::PathExists(result.AppendASCII("t1"))); | |
| 199 EXPECT_TRUE(PathService::Get(my_special_key + 1, &result)); | |
| 200 EXPECT_TRUE(base::PathExists(result.AppendASCII("t2"))); | |
| 201 } | |
| 202 | |
| 203 TEST_F(PathServiceTest, RemoveOverride) { | |
| 204 // Before we start the test we have to call RemoveOverride at least once to | |
| 205 // clear any overrides that might have been left from other tests. | |
| 206 PathService::RemoveOverride(base::DIR_TEMP); | |
| 207 | |
| 208 base::FilePath original_user_data_dir; | |
| 209 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &original_user_data_dir)); | |
| 210 EXPECT_FALSE(PathService::RemoveOverride(base::DIR_TEMP)); | |
| 211 | |
| 212 base::ScopedTempDir temp_dir; | |
| 213 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 214 EXPECT_TRUE(PathService::Override(base::DIR_TEMP, temp_dir.path())); | |
| 215 base::FilePath new_user_data_dir; | |
| 216 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); | |
| 217 EXPECT_NE(original_user_data_dir, new_user_data_dir); | |
| 218 | |
| 219 EXPECT_TRUE(PathService::RemoveOverride(base::DIR_TEMP)); | |
| 220 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &new_user_data_dir)); | |
| 221 EXPECT_EQ(original_user_data_dir, new_user_data_dir); | |
| 222 } | |
| 223 | |
| 224 #if defined(OS_WIN) | |
| 225 TEST_F(PathServiceTest, GetProgramFiles) { | |
| 226 base::FilePath programfiles_dir; | |
| 227 #if defined(_WIN64) | |
| 228 // 64-bit on 64-bit. | |
| 229 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES, | |
| 230 &programfiles_dir)); | |
| 231 EXPECT_EQ(programfiles_dir.value(), | |
| 232 FILE_PATH_LITERAL("C:\\Program Files")); | |
| 233 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILESX86, | |
| 234 &programfiles_dir)); | |
| 235 EXPECT_EQ(programfiles_dir.value(), | |
| 236 FILE_PATH_LITERAL("C:\\Program Files (x86)")); | |
| 237 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES6432, | |
| 238 &programfiles_dir)); | |
| 239 EXPECT_EQ(programfiles_dir.value(), | |
| 240 FILE_PATH_LITERAL("C:\\Program Files")); | |
| 241 #else | |
| 242 if (base::win::OSInfo::GetInstance()->wow64_status() == | |
| 243 base::win::OSInfo::WOW64_ENABLED) { | |
| 244 // 32-bit on 64-bit. | |
| 245 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES, | |
| 246 &programfiles_dir)); | |
| 247 EXPECT_EQ(programfiles_dir.value(), | |
| 248 FILE_PATH_LITERAL("C:\\Program Files (x86)")); | |
| 249 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILESX86, | |
| 250 &programfiles_dir)); | |
| 251 EXPECT_EQ(programfiles_dir.value(), | |
| 252 FILE_PATH_LITERAL("C:\\Program Files (x86)")); | |
| 253 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES6432, | |
| 254 &programfiles_dir)); | |
| 255 EXPECT_EQ(programfiles_dir.value(), | |
| 256 FILE_PATH_LITERAL("C:\\Program Files")); | |
| 257 } else { | |
| 258 // 32-bit on 32-bit. | |
| 259 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES, | |
| 260 &programfiles_dir)); | |
| 261 EXPECT_EQ(programfiles_dir.value(), | |
| 262 FILE_PATH_LITERAL("C:\\Program Files")); | |
| 263 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILESX86, | |
| 264 &programfiles_dir)); | |
| 265 EXPECT_EQ(programfiles_dir.value(), | |
| 266 FILE_PATH_LITERAL("C:\\Program Files")); | |
| 267 EXPECT_TRUE(PathService::Get(base::DIR_PROGRAM_FILES6432, | |
| 268 &programfiles_dir)); | |
| 269 EXPECT_EQ(programfiles_dir.value(), | |
| 270 FILE_PATH_LITERAL("C:\\Program Files")); | |
| 271 } | |
| 272 #endif | |
| 273 } | |
| 274 #endif | |
| OLD | NEW |