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