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 |
| 36 | #include "content/public/test/test_browser_thread_bundle.h" |
| 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>( |
Sam McNally | d8b7d82 | 2018-08-21 03:18:18 | [diff] [blame] | 61 | nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, |
| 62 | external_mount_points, |
avi | d6d88b91 | 2017-01-13 00:16:00 | [diff] [blame] | 63 | storage::ExternalMountPoints::GetSystemInstance())); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 64 | } |
| 65 | }; |
| 66 | |
| 67 | // Base test fixture class to be used on Chrome OS. |
| 68 | class PlatformUtilTestBase : public BrowserWithTestWindowTest { |
| 69 | protected: |
| 70 | void SetUpPlatformFixture(const base::FilePath& test_directory) { |
| 71 | content_browser_client_.reset(new PlatformUtilTestContentBrowserClient()); |
| 72 | old_content_browser_client_ = |
| 73 | content::SetBrowserClientForTesting(content_browser_client_.get()); |
| 74 | |
| 75 | // The test_directory needs to be mounted for it to be accessible. |
| 76 | content::BrowserContext::GetMountPoints(GetProfile()) |
| 77 | ->RegisterFileSystem("test", storage::kFileSystemTypeNativeLocal, |
| 78 | storage::FileSystemMountOption(), test_directory); |
| 79 | |
| 80 | // To test opening a file, we are going to register a mock extension that |
| 81 | // handles .txt files. The extension doesn't actually need to exist due to |
| 82 | // the DisableShellOperationsForTesting() call which prevents the extension |
| 83 | // from being invoked. |
| 84 | std::string error; |
| 85 | int error_code = 0; |
| 86 | |
| 87 | std::string json_manifest = |
| 88 | "{" |
| 89 | " \"manifest_version\": 2," |
| 90 | " \"name\": \"Test extension\"," |
| 91 | " \"version\": \"0\"," |
| 92 | " \"app\": { \"background\": { \"scripts\": [\"main.js\"] }}," |
| 93 | " \"file_handlers\": {" |
| 94 | " \"text\": {" |
| 95 | " \"extensions\": [ \"txt\" ]," |
| 96 | " \"title\": \"Text\"" |
| 97 | " }" |
| 98 | " }" |
| 99 | "}"; |
| 100 | JSONStringValueDeserializer json_string_deserializer(json_manifest); |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 101 | std::unique_ptr<base::Value> manifest = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 102 | json_string_deserializer.Deserialize(&error_code, &error); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 103 | base::DictionaryValue* manifest_dictionary; |
| 104 | |
| 105 | manifest->GetAsDictionary(&manifest_dictionary); |
| 106 | ASSERT_TRUE(manifest_dictionary); |
| 107 | |
| 108 | scoped_refptr<extensions::Extension> extension = |
| 109 | extensions::Extension::Create( |
| 110 | test_directory.AppendASCII("invalid-extension"), |
| 111 | extensions::Manifest::INVALID_LOCATION, *manifest_dictionary, |
| 112 | extensions::Extension::NO_FLAGS, &error); |
| 113 | ASSERT_TRUE(error.empty()) << error; |
| 114 | extensions::ExtensionRegistry::Get(GetProfile())->AddEnabled(extension); |
| 115 | } |
| 116 | |
| 117 | void TearDown() override { |
| 118 | content::ContentBrowserClient* content_browser_client = |
| 119 | content::SetBrowserClientForTesting(old_content_browser_client_); |
| 120 | old_content_browser_client_ = nullptr; |
| 121 | DCHECK_EQ(static_cast<content::ContentBrowserClient*>( |
| 122 | content_browser_client_.get()), |
| 123 | content_browser_client) |
| 124 | << "ContentBrowserClient changed during test."; |
| 125 | BrowserWithTestWindowTest::TearDown(); |
| 126 | } |
| 127 | |
| 128 | private: |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 129 | std::unique_ptr<content::ContentBrowserClient> content_browser_client_; |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 130 | content::ContentBrowserClient* old_content_browser_client_ = nullptr; |
| 131 | }; |
| 132 | |
| 133 | #else |
| 134 | |
| 135 | // Test fixture used by all desktop platforms other than Chrome OS. |
| 136 | class PlatformUtilTestBase : public testing::Test { |
| 137 | protected: |
| 138 | Profile* GetProfile() { return nullptr; } |
| 139 | void SetUpPlatformFixture(const base::FilePath&) {} |
| 140 | |
| 141 | private: |
| 142 | content::TestBrowserThreadBundle thread_bundle_; |
| 143 | }; |
| 144 | |
| 145 | #endif |
| 146 | |
| 147 | class PlatformUtilTest : public PlatformUtilTestBase { |
| 148 | public: |
| 149 | void SetUp() override { |
| 150 | ASSERT_NO_FATAL_FAILURE(PlatformUtilTestBase::SetUp()); |
| 151 | |
| 152 | static const char kTestFileData[] = "Cow says moo!"; |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame^] | 153 | const int kTestFileDataLength = base::size(kTestFileData) - 1; |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 154 | |
| 155 | // This prevents platfrom_util from invoking any shell or external APIs |
| 156 | // during tests. Doing so may result in external applications being launched |
| 157 | // and intefering with tests. |
| 158 | internal::DisableShellOperationsForTesting(); |
| 159 | |
| 160 | ASSERT_TRUE(directory_.CreateUniqueTempDir()); |
| 161 | |
| 162 | // A valid file. |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 163 | existing_file_ = directory_.GetPath().AppendASCII("test_file.txt"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 164 | ASSERT_EQ( |
| 165 | kTestFileDataLength, |
| 166 | base::WriteFile(existing_file_, kTestFileData, kTestFileDataLength)); |
| 167 | |
| 168 | // A valid folder. |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 169 | existing_folder_ = directory_.GetPath().AppendASCII("test_folder"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 170 | ASSERT_TRUE(base::CreateDirectory(existing_folder_)); |
| 171 | |
| 172 | // A non-existent path. |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 173 | nowhere_ = directory_.GetPath().AppendASCII("nowhere"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 174 | |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 175 | SetUpPlatformFixture(directory_.GetPath()); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | OpenOperationResult CallOpenItem(const base::FilePath& path, |
| 179 | OpenItemType item_type) { |
| 180 | base::RunLoop run_loop; |
| 181 | OpenOperationResult result = OPEN_SUCCEEDED; |
| 182 | OpenOperationCallback callback = |
| 183 | base::Bind(&OnOpenOperationDone, run_loop.QuitClosure(), &result); |
| 184 | OpenItem(GetProfile(), path, item_type, callback); |
| 185 | run_loop.Run(); |
| 186 | return result; |
| 187 | } |
| 188 | |
| 189 | base::FilePath existing_file_; |
| 190 | base::FilePath existing_folder_; |
| 191 | base::FilePath nowhere_; |
| 192 | |
| 193 | protected: |
| 194 | base::ScopedTempDir directory_; |
| 195 | |
| 196 | private: |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 197 | std::unique_ptr<base::RunLoop> run_loop_; |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 198 | |
| 199 | static void OnOpenOperationDone(const base::Closure& closure, |
| 200 | OpenOperationResult* store_result, |
| 201 | OpenOperationResult result) { |
| 202 | *store_result = result; |
| 203 | closure.Run(); |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | } // namespace |
| 208 | |
| 209 | TEST_F(PlatformUtilTest, OpenFile) { |
| 210 | EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(existing_file_, OPEN_FILE)); |
| 211 | EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, |
| 212 | CallOpenItem(existing_folder_, OPEN_FILE)); |
| 213 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenItem(nowhere_, OPEN_FILE)); |
| 214 | } |
| 215 | |
| 216 | TEST_F(PlatformUtilTest, OpenFolder) { |
| 217 | EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(existing_folder_, OPEN_FOLDER)); |
| 218 | EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, |
| 219 | CallOpenItem(existing_file_, OPEN_FOLDER)); |
| 220 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenItem(nowhere_, OPEN_FOLDER)); |
| 221 | } |
| 222 | |
| 223 | #if defined(OS_POSIX) |
| 224 | // Symbolic links are currently only supported on Posix. Windows technically |
| 225 | // supports it as well, but not on Windows XP. |
| 226 | class PlatformUtilPosixTest : public PlatformUtilTest { |
| 227 | public: |
| 228 | void SetUp() override { |
| 229 | ASSERT_NO_FATAL_FAILURE(PlatformUtilTest::SetUp()); |
| 230 | |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 231 | symlink_to_file_ = directory_.GetPath().AppendASCII("l_file.txt"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 232 | ASSERT_TRUE(base::CreateSymbolicLink(existing_file_, symlink_to_file_)); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 233 | symlink_to_folder_ = directory_.GetPath().AppendASCII("l_folder"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 234 | ASSERT_TRUE(base::CreateSymbolicLink(existing_folder_, symlink_to_folder_)); |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 235 | symlink_to_nowhere_ = directory_.GetPath().AppendASCII("l_nowhere"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 236 | ASSERT_TRUE(base::CreateSymbolicLink(nowhere_, symlink_to_nowhere_)); |
| 237 | } |
| 238 | |
| 239 | protected: |
| 240 | base::FilePath symlink_to_file_; |
| 241 | base::FilePath symlink_to_folder_; |
| 242 | base::FilePath symlink_to_nowhere_; |
| 243 | }; |
| 244 | #endif // OS_POSIX |
| 245 | |
| 246 | #if defined(OS_CHROMEOS) |
| 247 | // ChromeOS doesn't follow symbolic links in sandboxed filesystems. So all the |
| 248 | // symbolic link tests should return PATH_NOT_FOUND. |
| 249 | |
| 250 | TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinksChromeOS) { |
| 251 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 252 | CallOpenItem(symlink_to_file_, OPEN_FILE)); |
| 253 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 254 | CallOpenItem(symlink_to_folder_, OPEN_FILE)); |
| 255 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 256 | CallOpenItem(symlink_to_nowhere_, OPEN_FILE)); |
| 257 | } |
| 258 | |
| 259 | TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinksChromeOS) { |
| 260 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 261 | CallOpenItem(symlink_to_folder_, OPEN_FOLDER)); |
| 262 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 263 | CallOpenItem(symlink_to_file_, OPEN_FOLDER)); |
| 264 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 265 | CallOpenItem(symlink_to_nowhere_, OPEN_FOLDER)); |
| 266 | } |
| 267 | |
| 268 | TEST_F(PlatformUtilTest, OpenFileWithUnhandledFileType) { |
| 269 | base::FilePath unhandled_file = |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 270 | directory_.GetPath().AppendASCII("myfile.filetype"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 271 | ASSERT_EQ(3, base::WriteFile(unhandled_file, "cat", 3)); |
| 272 | EXPECT_EQ(OPEN_FAILED_NO_HANLDER_FOR_FILE_TYPE, |
| 273 | CallOpenItem(unhandled_file, OPEN_FILE)); |
| 274 | } |
| 275 | #endif // OS_CHROMEOS |
| 276 | |
| 277 | #if defined(OS_POSIX) && !defined(OS_CHROMEOS) |
| 278 | // On all other Posix platforms, the symbolic link tests should work as |
| 279 | // expected. |
| 280 | |
| 281 | TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinks) { |
| 282 | EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(symlink_to_file_, OPEN_FILE)); |
| 283 | EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, |
| 284 | CallOpenItem(symlink_to_folder_, OPEN_FILE)); |
| 285 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 286 | CallOpenItem(symlink_to_nowhere_, OPEN_FILE)); |
| 287 | } |
| 288 | |
| 289 | TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinks) { |
| 290 | EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(symlink_to_folder_, OPEN_FOLDER)); |
| 291 | EXPECT_EQ(OPEN_FAILED_INVALID_TYPE, |
| 292 | CallOpenItem(symlink_to_file_, OPEN_FOLDER)); |
| 293 | EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, |
| 294 | CallOpenItem(symlink_to_nowhere_, OPEN_FOLDER)); |
| 295 | } |
| 296 | #endif // OS_POSIX && !OS_CHROMEOS |
| 297 | |
| 298 | } // namespace platform_util |