blob: 9ebcb185cf03736c0c95fc5e93816b1c0374f337 [file] [log] [blame]
asanka655d1112015-03-07 05:33:411// 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
dcheng4af48582016-04-19 00:29:357#include <memory>
8
asanka655d1112015-03-07 05:33:419#include "base/bind.h"
10#include "base/callback.h"
Lei Zhang1f46798c2021-05-26 01:35:1811#include "base/cxx17_backports.h"
asanka655d1112015-03-07 05:33:4112#include "base/files/file_util.h"
13#include "base/files/scoped_temp_dir.h"
asanka655d1112015-03-07 05:33:4114#include "base/run_loop.h"
avib896c712015-12-26 02:10:4315#include "build/build_config.h"
Yuta Hijikata235fc62b2020-12-08 03:48:3216#include "build/chromeos_buildflags.h"
asanka655d1112015-03-07 05:33:4117#include "chrome/browser/platform_util_internal.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
Yuta Hijikata235fc62b2020-12-08 03:48:3220#if BUILDFLAG(IS_CHROMEOS_ASH)
asanka655d1112015-03-07 05:33:4121#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"
hashimotoa53e7e82016-10-26 06:30:4726#include "chrome/browser/chromeos/fileapi/file_system_backend_delegate.h"
asanka655d1112015-03-07 05:33:4127#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"
asanka655d1112015-03-07 05:33:4131#include "extensions/browser/extension_registry.h"
32#include "extensions/common/extension.h"
DongJun Kimfebb3c22019-10-21 02:08:0633#include "storage/browser/file_system/external_mount_points.h"
pwnall343665e72017-04-13 04:04:4034#include "storage/browser/test/mock_special_storage_policy.h"
DongJun Kimd6930ea2019-10-24 08:49:2535#include "storage/common/file_system/file_system_types.h"
asanka655d1112015-03-07 05:33:4136#else
Gabriel Charettec7108742019-08-23 03:31:4037#include "content/public/test/browser_task_environment.h"
asanka655d1112015-03-07 05:33:4138#endif
39
40namespace platform_util {
41
42namespace {
43
Yuta Hijikata235fc62b2020-12-08 03:48:3244#if BUILDFLAG(IS_CHROMEOS_ASH)
asanka655d1112015-03-07 05:33:4145
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.
tfarina2176f4b2015-09-17 07:09:2649class PlatformUtilTestContentBrowserClient : public ChromeContentBrowserClient {
asanka655d1112015-03-07 05:33:4150 public:
51 void GetAdditionalFileSystemBackends(
52 content::BrowserContext* browser_context,
53 const base::FilePath& storage_partition_path,
avid6d88b912017-01-13 00:16:0054 std::vector<std::unique_ptr<storage::FileSystemBackend>>*
55 additional_backends) override {
asanka655d1112015-03-07 05:33:4156 storage::ExternalMountPoints* external_mount_points =
Lukasz Anforowiczeda637d22021-05-20 00:32:2357 browser_context->GetMountPoints();
asanka655d1112015-03-07 05:33:4158
59 // New FileSystemBackend that uses our MockSpecialStoragePolicy.
avid6d88b912017-01-13 00:16:0060 additional_backends->push_back(
Jeremy Romanec48d7a2018-03-01 17:35:0961 std::make_unique<chromeos::FileSystemBackend>(
David Black8f8c1eb2020-12-09 04:02:2362 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. Mistry7694c362020-03-17 23:33:3469 external_mount_points,
avid6d88b912017-01-13 00:16:0070 storage::ExternalMountPoints::GetSystemInstance()));
asanka655d1112015-03-07 05:33:4171 }
72};
73
74// Base test fixture class to be used on Chrome OS.
75class PlatformUtilTestBase : public BrowserWithTestWindowTest {
76 protected:
77 void SetUpPlatformFixture(const base::FilePath& test_directory) {
Peter Boström6b701822021-04-15 03:53:0878 content_browser_client_ =
79 std::make_unique<PlatformUtilTestContentBrowserClient>();
asanka655d1112015-03-07 05:33:4180 old_content_browser_client_ =
81 content::SetBrowserClientForTesting(content_browser_client_.get());
82
83 // The test_directory needs to be mounted for it to be accessible.
Lukasz Anforowiczeda637d22021-05-20 00:32:2384 GetProfile()->GetMountPoints()->RegisterFileSystem(
85 "test", storage::kFileSystemTypeLocal, storage::FileSystemMountOption(),
86 test_directory);
asanka655d1112015-03-07 05:33:4187
88 // To test opening a file, we are going to register a mock extension that
89 // handles .txt files. The extension doesn't actually need to exist due to
90 // the DisableShellOperationsForTesting() call which prevents the extension
91 // from being invoked.
92 std::string error;
93 int error_code = 0;
94
95 std::string json_manifest =
96 "{"
97 " \"manifest_version\": 2,"
98 " \"name\": \"Test extension\","
99 " \"version\": \"0\","
100 " \"app\": { \"background\": { \"scripts\": [\"main.js\"] }},"
101 " \"file_handlers\": {"
102 " \"text\": {"
103 " \"extensions\": [ \"txt\" ],"
104 " \"title\": \"Text\""
105 " }"
106 " }"
107 "}";
108 JSONStringValueDeserializer json_string_deserializer(json_manifest);
dcheng4af48582016-04-19 00:29:35109 std::unique_ptr<base::Value> manifest =
olli.raulaba045252015-10-16 06:16:40110 json_string_deserializer.Deserialize(&error_code, &error);
asanka655d1112015-03-07 05:33:41111 base::DictionaryValue* manifest_dictionary;
112
113 manifest->GetAsDictionary(&manifest_dictionary);
114 ASSERT_TRUE(manifest_dictionary);
115
116 scoped_refptr<extensions::Extension> extension =
117 extensions::Extension::Create(
118 test_directory.AppendASCII("invalid-extension"),
Gyuyoung Kimabc23382021-03-18 03:09:18119 extensions::mojom::ManifestLocation::kInvalidLocation,
120 *manifest_dictionary, extensions::Extension::NO_FLAGS, &error);
asanka655d1112015-03-07 05:33:41121 ASSERT_TRUE(error.empty()) << error;
122 extensions::ExtensionRegistry::Get(GetProfile())->AddEnabled(extension);
123 }
124
Dominick Ng51154652019-09-25 07:44:20125 void SetUp() override {
126 BrowserWithTestWindowTest::SetUp();
127 base::RunLoop().RunUntilIdle();
128 }
129
asanka655d1112015-03-07 05:33:41130 void TearDown() override {
131 content::ContentBrowserClient* content_browser_client =
132 content::SetBrowserClientForTesting(old_content_browser_client_);
133 old_content_browser_client_ = nullptr;
134 DCHECK_EQ(static_cast<content::ContentBrowserClient*>(
135 content_browser_client_.get()),
136 content_browser_client)
137 << "ContentBrowserClient changed during test.";
138 BrowserWithTestWindowTest::TearDown();
139 }
140
141 private:
dcheng4af48582016-04-19 00:29:35142 std::unique_ptr<content::ContentBrowserClient> content_browser_client_;
asanka655d1112015-03-07 05:33:41143 content::ContentBrowserClient* old_content_browser_client_ = nullptr;
144};
145
146#else
147
148// Test fixture used by all desktop platforms other than Chrome OS.
149class PlatformUtilTestBase : public testing::Test {
150 protected:
151 Profile* GetProfile() { return nullptr; }
152 void SetUpPlatformFixture(const base::FilePath&) {}
153
154 private:
Gabriel Charette798fde72019-08-20 22:24:04155 content::BrowserTaskEnvironment task_environment_;
asanka655d1112015-03-07 05:33:41156};
157
158#endif
159
160class PlatformUtilTest : public PlatformUtilTestBase {
161 public:
162 void SetUp() override {
163 ASSERT_NO_FATAL_FAILURE(PlatformUtilTestBase::SetUp());
164
165 static const char kTestFileData[] = "Cow says moo!";
Avi Drissman5f0fb8c2018-12-25 23:20:49166 const int kTestFileDataLength = base::size(kTestFileData) - 1;
asanka655d1112015-03-07 05:33:41167
Dominick Ng51154652019-09-25 07:44:20168 // This prevents platform_util from invoking any shell or external APIs
asanka655d1112015-03-07 05:33:41169 // during tests. Doing so may result in external applications being launched
170 // and intefering with tests.
171 internal::DisableShellOperationsForTesting();
172
173 ASSERT_TRUE(directory_.CreateUniqueTempDir());
174
175 // A valid file.
vabr8023d872016-09-15 08:12:22176 existing_file_ = directory_.GetPath().AppendASCII("test_file.txt");
asanka655d1112015-03-07 05:33:41177 ASSERT_EQ(
178 kTestFileDataLength,
179 base::WriteFile(existing_file_, kTestFileData, kTestFileDataLength));
180
181 // A valid folder.
vabr8023d872016-09-15 08:12:22182 existing_folder_ = directory_.GetPath().AppendASCII("test_folder");
asanka655d1112015-03-07 05:33:41183 ASSERT_TRUE(base::CreateDirectory(existing_folder_));
184
185 // A non-existent path.
vabr8023d872016-09-15 08:12:22186 nowhere_ = directory_.GetPath().AppendASCII("nowhere");
asanka655d1112015-03-07 05:33:41187
vabr8023d872016-09-15 08:12:22188 SetUpPlatformFixture(directory_.GetPath());
asanka655d1112015-03-07 05:33:41189 }
190
191 OpenOperationResult CallOpenItem(const base::FilePath& path,
192 OpenItemType item_type) {
193 base::RunLoop run_loop;
194 OpenOperationResult result = OPEN_SUCCEEDED;
195 OpenOperationCallback callback =
Alexander Cooper71fa2b02020-07-16 17:49:36196 base::BindOnce(&OnOpenOperationDone, run_loop.QuitClosure(), &result);
197 OpenItem(GetProfile(), path, item_type, std::move(callback));
asanka655d1112015-03-07 05:33:41198 run_loop.Run();
199 return result;
200 }
201
202 base::FilePath existing_file_;
203 base::FilePath existing_folder_;
204 base::FilePath nowhere_;
205
206 protected:
207 base::ScopedTempDir directory_;
208
209 private:
dcheng4af48582016-04-19 00:29:35210 std::unique_ptr<base::RunLoop> run_loop_;
asanka655d1112015-03-07 05:33:41211
Alexander Cooperbc87af32020-07-15 15:59:45212 static void OnOpenOperationDone(base::OnceClosure closure,
asanka655d1112015-03-07 05:33:41213 OpenOperationResult* store_result,
214 OpenOperationResult result) {
215 *store_result = result;
Alexander Cooperbc87af32020-07-15 15:59:45216 std::move(closure).Run();
asanka655d1112015-03-07 05:33:41217 }
218};
219
220} // namespace
221
222TEST_F(PlatformUtilTest, OpenFile) {
223 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(existing_file_, OPEN_FILE));
224 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
225 CallOpenItem(existing_folder_, OPEN_FILE));
226 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenItem(nowhere_, OPEN_FILE));
227}
228
Sam McNally663715c2019-09-24 20:12:24229TEST_F(PlatformUtilTest, OpenFolder) {
asanka655d1112015-03-07 05:33:41230 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(existing_folder_, OPEN_FOLDER));
231 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
232 CallOpenItem(existing_file_, OPEN_FOLDER));
233 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenItem(nowhere_, OPEN_FOLDER));
234}
235
236#if defined(OS_POSIX)
237// Symbolic links are currently only supported on Posix. Windows technically
238// supports it as well, but not on Windows XP.
239class PlatformUtilPosixTest : public PlatformUtilTest {
240 public:
241 void SetUp() override {
242 ASSERT_NO_FATAL_FAILURE(PlatformUtilTest::SetUp());
243
vabr8023d872016-09-15 08:12:22244 symlink_to_file_ = directory_.GetPath().AppendASCII("l_file.txt");
asanka655d1112015-03-07 05:33:41245 ASSERT_TRUE(base::CreateSymbolicLink(existing_file_, symlink_to_file_));
vabr8023d872016-09-15 08:12:22246 symlink_to_folder_ = directory_.GetPath().AppendASCII("l_folder");
asanka655d1112015-03-07 05:33:41247 ASSERT_TRUE(base::CreateSymbolicLink(existing_folder_, symlink_to_folder_));
vabr8023d872016-09-15 08:12:22248 symlink_to_nowhere_ = directory_.GetPath().AppendASCII("l_nowhere");
asanka655d1112015-03-07 05:33:41249 ASSERT_TRUE(base::CreateSymbolicLink(nowhere_, symlink_to_nowhere_));
250 }
251
252 protected:
253 base::FilePath symlink_to_file_;
254 base::FilePath symlink_to_folder_;
255 base::FilePath symlink_to_nowhere_;
256};
257#endif // OS_POSIX
258
Yuta Hijikata235fc62b2020-12-08 03:48:32259#if BUILDFLAG(IS_CHROMEOS_ASH)
asanka655d1112015-03-07 05:33:41260// ChromeOS doesn't follow symbolic links in sandboxed filesystems. So all the
261// symbolic link tests should return PATH_NOT_FOUND.
262
Luciano Pachecoa25b7d42019-09-25 01:53:27263TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinksChromeOS) {
asanka655d1112015-03-07 05:33:41264 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
265 CallOpenItem(symlink_to_file_, OPEN_FILE));
266 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
267 CallOpenItem(symlink_to_folder_, OPEN_FILE));
268 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
269 CallOpenItem(symlink_to_nowhere_, OPEN_FILE));
270}
271
272TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinksChromeOS) {
273 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
274 CallOpenItem(symlink_to_folder_, OPEN_FOLDER));
275 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
276 CallOpenItem(symlink_to_file_, OPEN_FOLDER));
277 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
278 CallOpenItem(symlink_to_nowhere_, OPEN_FOLDER));
279}
280
Luciano Pachecoa25b7d42019-09-25 01:53:27281TEST_F(PlatformUtilTest, OpenFileWithUnhandledFileType) {
asanka655d1112015-03-07 05:33:41282 base::FilePath unhandled_file =
vabr8023d872016-09-15 08:12:22283 directory_.GetPath().AppendASCII("myfile.filetype");
asanka655d1112015-03-07 05:33:41284 ASSERT_EQ(3, base::WriteFile(unhandled_file, "cat", 3));
285 EXPECT_EQ(OPEN_FAILED_NO_HANLDER_FOR_FILE_TYPE,
286 CallOpenItem(unhandled_file, OPEN_FILE));
287}
Yuta Hijikata235fc62b2020-12-08 03:48:32288#endif // BUILDFLAG(IS_CHROMEOS_ASH)
asanka655d1112015-03-07 05:33:41289
Yuta Hijikata235fc62b2020-12-08 03:48:32290#if defined(OS_POSIX) && !BUILDFLAG(IS_CHROMEOS_ASH)
asanka655d1112015-03-07 05:33:41291// On all other Posix platforms, the symbolic link tests should work as
292// expected.
293
294TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinks) {
295 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(symlink_to_file_, OPEN_FILE));
296 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
297 CallOpenItem(symlink_to_folder_, OPEN_FILE));
298 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
299 CallOpenItem(symlink_to_nowhere_, OPEN_FILE));
300}
301
302TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinks) {
303 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(symlink_to_folder_, OPEN_FOLDER));
304 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
305 CallOpenItem(symlink_to_file_, OPEN_FOLDER));
306 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
307 CallOpenItem(symlink_to_nowhere_, OPEN_FOLDER));
308}
309#endif // OS_POSIX && !OS_CHROMEOS
310
311} // namespace platform_util