asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 1 | // Copyright 2015 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 "chrome/browser/platform_util.h" |
| 6 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/callback.h" |
| 11 | #include "base/files/file_util.h" |
| 12 | #include "base/files/scoped_temp_dir.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 14 | #include "base/stl_util.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 15 | #include "build/build_config.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 16 | #include "chrome/browser/platform_util_internal.h" |
| 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | #if defined(OS_CHROMEOS) |
| 20 | #include "base/json/json_string_value_serializer.h" |
| 21 | #include "base/values.h" |
| 22 | #include "chrome/browser/chrome_content_browser_client.h" |
| 23 | #include "chrome/browser/chromeos/file_manager/app_id.h" |
| 24 | #include "chrome/browser/chromeos/fileapi/file_system_backend.h" |
hashimoto | a53e7e8 | 2016-10-26 06:30:47 | [diff] [blame] | 25 | #include "chrome/browser/chromeos/fileapi/file_system_backend_delegate.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 26 | #include "chrome/browser/extensions/extension_special_storage_policy.h" |
| 27 | #include "chrome/test/base/browser_with_test_window_test.h" |
| 28 | #include "content/public/browser/browser_context.h" |
| 29 | #include "content/public/common/content_client.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 30 | #include "extensions/browser/extension_registry.h" |
| 31 | #include "extensions/common/extension.h" |
| 32 | #include "storage/browser/fileapi/external_mount_points.h" |
pwnall | 343665e7 | 2017-04-13 04:04:40 | [diff] [blame] | 33 | #include "storage/browser/test/mock_special_storage_policy.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 34 | #include "storage/common/fileapi/file_system_types.h" |
| 35 | #else |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 36 | #include "content/public/test/browser_task_environment.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | namespace platform_util { |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | #if defined(OS_CHROMEOS) |
| 44 | |
| 45 | // ChromeContentBrowserClient subclass that sets up a custom file system backend |
| 46 | // that allows the test to grant file access to the file manager extension ID |
| 47 | // without having to install the extension. |
tfarina | 2176f4b | 2015-09-17 07:09:26 | [diff] [blame] | 48 | class PlatformUtilTestContentBrowserClient : public ChromeContentBrowserClient { |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 49 | public: |
| 50 | void GetAdditionalFileSystemBackends( |
| 51 | content::BrowserContext* browser_context, |
| 52 | const base::FilePath& storage_partition_path, |
avi | d6d88b91 | 2017-01-13 00:16:00 | [diff] [blame] | 53 | std::vector<std::unique_ptr<storage::FileSystemBackend>>* |
| 54 | additional_backends) override { |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 55 | storage::ExternalMountPoints* external_mount_points = |
| 56 | content::BrowserContext::GetMountPoints(browser_context); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 57 | |
| 58 | // New FileSystemBackend that uses our MockSpecialStoragePolicy. |
avi | d6d88b91 | 2017-01-13 00:16:00 | [diff] [blame] | 59 | additional_backends->push_back( |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 60 | std::make_unique<chromeos::FileSystemBackend>( |
Austin Tankiang | 396756c | 2019-09-18 03:36:39 | [diff] [blame] | 61 | nullptr, nullptr, nullptr, nullptr, nullptr, external_mount_points, |
avi | d6d88b91 | 2017-01-13 00:16:00 | [diff] [blame] | 62 | storage::ExternalMountPoints::GetSystemInstance())); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
| 66 | // Base test fixture class to be used on Chrome OS. |
| 67 | class PlatformUtilTestBase : public BrowserWithTestWindowTest { |
| 68 | protected: |
| 69 | void SetUpPlatformFixture(const base::FilePath& test_directory) { |
| 70 | content_browser_client_.reset(new PlatformUtilTestContentBrowserClient()); |
| 71 | old_content_browser_client_ = |
| 72 | content::SetBrowserClientForTesting(content_browser_client_.get()); |
| 73 | |
| 74 | // The test_directory needs to be mounted for it to be accessible. |
| 75 | content::BrowserContext::GetMountPoints(GetProfile()) |
| 76 | ->RegisterFileSystem("test", storage::kFileSystemTypeNativeLocal, |
| 77 | storage::FileSystemMountOption(), test_directory); |
| 78 | |
| 79 | // To test opening a file, we are going to register a mock extension that |
| 80 | // handles .txt files. The extension doesn't actually need to exist due to |
| 81 | // the DisableShellOperationsForTesting() call which prevents the extension |
| 82 | // from being invoked. |
| 83 | std::string error; |
| 84 | int error_code = 0; |
| 85 | |
| 86 | std::string json_manifest = |
| 87 | "{" |
| 88 | " \"manifest_version\": 2," |
| 89 | " \"name\": \"Test extension\"," |
| 90 | " \"version\": \"0\"," |
| 91 | " \"app\": { \"background\": { \"scripts\": [\"main.js\"] }}," |
| 92 | " \"file_handlers\": {" |
| 93 | " \"text\": {" |
| 94 | " \"extensions\": [ \"txt\" ]," |
| 95 | " \"title\": \"Text\"" |
| 96 | " }" |
| 97 | " }" |
| 98 | "}"; |
| 99 | JSONStringValueDeserializer json_string_deserializer(json_manifest); |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 100 | std::unique_ptr<base::Value> manifest = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 101 | json_string_deserializer.Deserialize(&error_code, &error); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 102 | base::DictionaryValue* manifest_dictionary; |
| 103 | |
| 104 | manifest->GetAsDictionary(&manifest_dictionary); |
| 105 | ASSERT_TRUE(manifest_dictionary); |
| 106 | |
| 107 | scoped_refptr<extensions::Extension> extension = |
| 108 | extensions::Extension::Create( |
| 109 | test_directory.AppendASCII("invalid-extension"), |
| 110 | extensions::Manifest::INVALID_LOCATION, *manifest_dictionary, |
| 111 | extensions::Extension::NO_FLAGS, &error); |
| 112 | ASSERT_TRUE(error.empty()) << error; |
| 113 | extensions::ExtensionRegistry::Get(GetProfile())->AddEnabled(extension); |
| 114 | } |
| 115 | |
| 116 | void TearDown() override { |
| 117 | content::ContentBrowserClient* content_browser_client = |
| 118 | content::SetBrowserClientForTesting(old_content_browser_client_); |
| 119 | old_content_browser_client_ = nullptr; |
| 120 | DCHECK_EQ(static_cast<content::ContentBrowserClient*>( |
| 121 | content_browser_client_.get()), |
| 122 | content_browser_client) |
| 123 | << "ContentBrowserClient changed during test."; |
| 124 | BrowserWithTestWindowTest::TearDown(); |
| 125 | } |
| 126 | |
| 127 | private: |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 128 | std::unique_ptr<content::ContentBrowserClient> content_browser_client_; |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 129 | content::ContentBrowserClient* old_content_browser_client_ = nullptr; |
| 130 | }; |
| 131 | |
| 132 | #else |
| 133 | |
| 134 | // Test fixture used by all desktop platforms other than Chrome OS. |
| 135 | class PlatformUtilTestBase : public testing::Test { |
| 136 | protected: |
| 137 | Profile* GetProfile() { return nullptr; } |
| 138 | void SetUpPlatformFixture(const base::FilePath&) {} |
| 139 | |
| 140 | private: |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 141 | content::BrowserTaskEnvironment task_environment_; |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | #endif |
| 145 | |
| 146 | class PlatformUtilTest : public PlatformUtilTestBase { |
| 147 | public: |
| 148 | void SetUp() override { |
| 149 | ASSERT_NO_FATAL_FAILURE(PlatformUtilTestBase::SetUp()); |
| 150 | |
| 151 | static const char kTestFileData[] = "Cow says moo!"; |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 152 | const int kTestFileDataLength = base::size(kTestFileData) - 1; |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 153 | |
| 154 | // This prevents platfrom_util from invoking any shell or external APIs |
| 155 | // during tests. Doing so may result in external applications being launched |
| 156 | // and intefering with tests. |
| 157 | internal::DisableShellOperationsForTesting(); |
| 158 | |
| 159 | ASSERT_TRUE(directory_.CreateUniqueTempDir()); |
| 160 | |
| 161 | // A valid file. |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 162 | existing_file_ = directory_.GetPath().AppendASCII("test_file.txt"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 163 | ASSERT_EQ( |
| 164 | kTestFileDataLength, |
| 165 | base::WriteFile(existing_file_, kTestFileData, kTestFileDataLength)); |
| 166 | |
| 167 | // A valid folder. |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 168 | existing_folder_ = directory_.GetPath().AppendASCII("test_folder"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 169 | ASSERT_TRUE(base::CreateDirectory(existing_folder_)); |
| 170 | |
| 171 | // A non-existent path. |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 172 | nowhere_ = directory_.GetPath().AppendASCII("nowhere"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 173 | |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 174 | SetUpPlatformFixture(directory_.GetPath()); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | OpenOperationResult CallOpenItem(const base::FilePath& path, |
| 178 | OpenItemType item_type) { |
| 179 | base::RunLoop run_loop; |
| 180 | OpenOperationResult result = OPEN_SUCCEEDED; |
| 181 | OpenOperationCallback callback = |
| 182 | base::Bind(&OnOpenOperationDone, run_loop.QuitClosure(), &result); |
| 183 | OpenItem(GetProfile(), path, item_type, callback); |
| 184 | run_loop.Run(); |
| 185 | return result; |
| 186 | } |
| 187 | |
| 188 | base::FilePath existing_file_; |
| 189 | base::FilePath existing_folder_; |
| 190 | base::FilePath nowhere_; |
| 191 | |
| 192 | protected: |
| 193 | base::ScopedTempDir directory_; |
| 194 | |
| 195 | private: |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 196 | std::unique_ptr<base::RunLoop> run_loop_; |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 197 | |
| 198 | static void OnOpenOperationDone(const base::Closure& closure, |
| 199 | OpenOperationResult* store_result, |
| 200 | OpenOperationResult result) { |
| 201 | *store_result = result; |
| 202 | closure.Run(); |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | } // namespace |
| 207 | |
| 208 | TEST_F(PlatformUtilTest, OpenFile) { |
| 209 | EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(existing_file_, OPEN_FILE)); |
| 210 | EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, |
| 211 | CallOpenItem(existing_folder_, OPEN_FILE)); |
| 212 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenItem(nowhere_, OPEN_FILE)); |
| 213 | } |
| 214 | |
Fergus Dall | 755c65b | 2019-09-17 06:10:50 | [diff] [blame] | 215 | // TODO(crbug.com/1004639) Disabled on Linux due to crashes and timeouts. |
| 216 | #if defined(OS_LINUX) |
| 217 | #define MAYBE_OpenFolder DISABLED_OpenFolder |
| 218 | #else |
| 219 | #define MAYBE_OpenFolder OpenFolder |
| 220 | #endif |
| 221 | TEST_F(PlatformUtilTest, MAYBE_OpenFolder) { |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 222 | EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(existing_folder_, OPEN_FOLDER)); |
| 223 | EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, |
| 224 | CallOpenItem(existing_file_, OPEN_FOLDER)); |
| 225 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenItem(nowhere_, OPEN_FOLDER)); |
| 226 | } |
| 227 | |
| 228 | #if defined(OS_POSIX) |
| 229 | // Symbolic links are currently only supported on Posix. Windows technically |
| 230 | // supports it as well, but not on Windows XP. |
| 231 | class PlatformUtilPosixTest : public PlatformUtilTest { |
| 232 | public: |
| 233 | void SetUp() override { |
| 234 | ASSERT_NO_FATAL_FAILURE(PlatformUtilTest::SetUp()); |
| 235 | |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 236 | symlink_to_file_ = directory_.GetPath().AppendASCII("l_file.txt"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 237 | ASSERT_TRUE(base::CreateSymbolicLink(existing_file_, symlink_to_file_)); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 238 | symlink_to_folder_ = directory_.GetPath().AppendASCII("l_folder"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 239 | ASSERT_TRUE(base::CreateSymbolicLink(existing_folder_, symlink_to_folder_)); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 240 | symlink_to_nowhere_ = directory_.GetPath().AppendASCII("l_nowhere"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 241 | ASSERT_TRUE(base::CreateSymbolicLink(nowhere_, symlink_to_nowhere_)); |
| 242 | } |
| 243 | |
| 244 | protected: |
| 245 | base::FilePath symlink_to_file_; |
| 246 | base::FilePath symlink_to_folder_; |
| 247 | base::FilePath symlink_to_nowhere_; |
| 248 | }; |
| 249 | #endif // OS_POSIX |
| 250 | |
| 251 | #if defined(OS_CHROMEOS) |
| 252 | // ChromeOS doesn't follow symbolic links in sandboxed filesystems. So all the |
| 253 | // symbolic link tests should return PATH_NOT_FOUND. |
| 254 | |
Dominick Ng | 589d071 | 2019-09-24 13:08:59 | [diff] [blame] | 255 | // Very flaky due to memory unsafety: crbug.com/1007240 |
| 256 | TEST_F(PlatformUtilPosixTest, DISABLED_OpenFileWithPosixSymlinksChromeOS) { |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 257 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 258 | CallOpenItem(symlink_to_file_, OPEN_FILE)); |
| 259 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 260 | CallOpenItem(symlink_to_folder_, OPEN_FILE)); |
| 261 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 262 | CallOpenItem(symlink_to_nowhere_, OPEN_FILE)); |
| 263 | } |
| 264 | |
| 265 | TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinksChromeOS) { |
| 266 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 267 | CallOpenItem(symlink_to_folder_, OPEN_FOLDER)); |
| 268 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 269 | CallOpenItem(symlink_to_file_, OPEN_FOLDER)); |
| 270 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 271 | CallOpenItem(symlink_to_nowhere_, OPEN_FOLDER)); |
| 272 | } |
| 273 | |
Dominick Ng | 589d071 | 2019-09-24 13:08:59 | [diff] [blame] | 274 | // Very flaky due to memory unsafety: crbug.com/1007240 |
| 275 | TEST_F(PlatformUtilTest, DISABLED_OpenFileWithUnhandledFileType) { |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 276 | base::FilePath unhandled_file = |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 277 | directory_.GetPath().AppendASCII("myfile.filetype"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 278 | ASSERT_EQ(3, base::WriteFile(unhandled_file, "cat", 3)); |
| 279 | EXPECT_EQ(OPEN_FAILED_NO_HANLDER_FOR_FILE_TYPE, |
| 280 | CallOpenItem(unhandled_file, OPEN_FILE)); |
| 281 | } |
| 282 | #endif // OS_CHROMEOS |
| 283 | |
| 284 | #if defined(OS_POSIX) && !defined(OS_CHROMEOS) |
| 285 | // On all other Posix platforms, the symbolic link tests should work as |
| 286 | // expected. |
| 287 | |
| 288 | TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinks) { |
| 289 | EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(symlink_to_file_, OPEN_FILE)); |
| 290 | EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, |
| 291 | CallOpenItem(symlink_to_folder_, OPEN_FILE)); |
| 292 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 293 | CallOpenItem(symlink_to_nowhere_, OPEN_FILE)); |
| 294 | } |
| 295 | |
| 296 | TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinks) { |
| 297 | EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(symlink_to_folder_, OPEN_FOLDER)); |
| 298 | EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, |
| 299 | CallOpenItem(symlink_to_file_, OPEN_FOLDER)); |
| 300 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 301 | CallOpenItem(symlink_to_nowhere_, OPEN_FOLDER)); |
| 302 | } |
| 303 | #endif // OS_POSIX && !OS_CHROMEOS |
| 304 | |
| 305 | } // namespace platform_util |