blob: 4c32fe4df3f49621d73120f90b4682aa52ee02ac [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"
11#include "base/files/file_util.h"
12#include "base/files/scoped_temp_dir.h"
asanka655d1112015-03-07 05:33:4113#include "base/run_loop.h"
Avi Drissman5f0fb8c2018-12-25 23:20:4914#include "base/stl_util.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 =
57 content::BrowserContext::GetMountPoints(browser_context);
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) {
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())
Austin Sullivan92338512021-01-28 23:32:5984 ->RegisterFileSystem("test", storage::kFileSystemTypeLocal,
asanka655d1112015-03-07 05:33:4185 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);
dcheng4af48582016-04-19 00:29:35108 std::unique_ptr<base::Value> manifest =
olli.raulaba045252015-10-16 06:16:40109 json_string_deserializer.Deserialize(&error_code, &error);
asanka655d1112015-03-07 05:33:41110 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"),
Gyuyoung Kimabc23382021-03-18 03:09:18118 extensions::mojom::ManifestLocation::kInvalidLocation,
119 *manifest_dictionary, extensions::Extension::NO_FLAGS, &error);
asanka655d1112015-03-07 05:33:41120 ASSERT_TRUE(error.empty()) << error;
121 extensions::ExtensionRegistry::Get(GetProfile())->AddEnabled(extension);
122 }
123
Dominick Ng51154652019-09-25 07:44:20124 void SetUp() override {
125 BrowserWithTestWindowTest::SetUp();
126 base::RunLoop().RunUntilIdle();
127 }
128
asanka655d1112015-03-07 05:33:41129 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:
dcheng4af48582016-04-19 00:29:35141 std::unique_ptr<content::ContentBrowserClient> content_browser_client_;
asanka655d1112015-03-07 05:33:41142 content::ContentBrowserClient* old_content_browser_client_ = nullptr;
143};
144
145#else
146
147// Test fixture used by all desktop platforms other than Chrome OS.
148class PlatformUtilTestBase : public testing::Test {
149 protected:
150 Profile* GetProfile() { return nullptr; }
151 void SetUpPlatformFixture(const base::FilePath&) {}
152
153 private:
Gabriel Charette798fde72019-08-20 22:24:04154 content::BrowserTaskEnvironment task_environment_;
asanka655d1112015-03-07 05:33:41155};
156
157#endif
158
159class 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 Drissman5f0fb8c2018-12-25 23:20:49165 const int kTestFileDataLength = base::size(kTestFileData) - 1;
asanka655d1112015-03-07 05:33:41166
Dominick Ng51154652019-09-25 07:44:20167 // This prevents platform_util from invoking any shell or external APIs
asanka655d1112015-03-07 05:33:41168 // 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.
vabr8023d872016-09-15 08:12:22175 existing_file_ = directory_.GetPath().AppendASCII("test_file.txt");
asanka655d1112015-03-07 05:33:41176 ASSERT_EQ(
177 kTestFileDataLength,
178 base::WriteFile(existing_file_, kTestFileData, kTestFileDataLength));
179
180 // A valid folder.
vabr8023d872016-09-15 08:12:22181 existing_folder_ = directory_.GetPath().AppendASCII("test_folder");
asanka655d1112015-03-07 05:33:41182 ASSERT_TRUE(base::CreateDirectory(existing_folder_));
183
184 // A non-existent path.
vabr8023d872016-09-15 08:12:22185 nowhere_ = directory_.GetPath().AppendASCII("nowhere");
asanka655d1112015-03-07 05:33:41186
vabr8023d872016-09-15 08:12:22187 SetUpPlatformFixture(directory_.GetPath());
asanka655d1112015-03-07 05:33:41188 }
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 Cooper71fa2b02020-07-16 17:49:36195 base::BindOnce(&OnOpenOperationDone, run_loop.QuitClosure(), &result);
196 OpenItem(GetProfile(), path, item_type, std::move(callback));
asanka655d1112015-03-07 05:33:41197 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:
dcheng4af48582016-04-19 00:29:35209 std::unique_ptr<base::RunLoop> run_loop_;
asanka655d1112015-03-07 05:33:41210
Alexander Cooperbc87af32020-07-15 15:59:45211 static void OnOpenOperationDone(base::OnceClosure closure,
asanka655d1112015-03-07 05:33:41212 OpenOperationResult* store_result,
213 OpenOperationResult result) {
214 *store_result = result;
Alexander Cooperbc87af32020-07-15 15:59:45215 std::move(closure).Run();
asanka655d1112015-03-07 05:33:41216 }
217};
218
219} // namespace
220
221TEST_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 McNally663715c2019-09-24 20:12:24228TEST_F(PlatformUtilTest, OpenFolder) {
asanka655d1112015-03-07 05:33:41229 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.
238class PlatformUtilPosixTest : public PlatformUtilTest {
239 public:
240 void SetUp() override {
241 ASSERT_NO_FATAL_FAILURE(PlatformUtilTest::SetUp());
242
vabr8023d872016-09-15 08:12:22243 symlink_to_file_ = directory_.GetPath().AppendASCII("l_file.txt");
asanka655d1112015-03-07 05:33:41244 ASSERT_TRUE(base::CreateSymbolicLink(existing_file_, symlink_to_file_));
vabr8023d872016-09-15 08:12:22245 symlink_to_folder_ = directory_.GetPath().AppendASCII("l_folder");
asanka655d1112015-03-07 05:33:41246 ASSERT_TRUE(base::CreateSymbolicLink(existing_folder_, symlink_to_folder_));
vabr8023d872016-09-15 08:12:22247 symlink_to_nowhere_ = directory_.GetPath().AppendASCII("l_nowhere");
asanka655d1112015-03-07 05:33:41248 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 Hijikata235fc62b2020-12-08 03:48:32258#if BUILDFLAG(IS_CHROMEOS_ASH)
asanka655d1112015-03-07 05:33:41259// ChromeOS doesn't follow symbolic links in sandboxed filesystems. So all the
260// symbolic link tests should return PATH_NOT_FOUND.
261
Luciano Pachecoa25b7d42019-09-25 01:53:27262TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinksChromeOS) {
asanka655d1112015-03-07 05:33:41263 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
271TEST_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 Pachecoa25b7d42019-09-25 01:53:27280TEST_F(PlatformUtilTest, OpenFileWithUnhandledFileType) {
asanka655d1112015-03-07 05:33:41281 base::FilePath unhandled_file =
vabr8023d872016-09-15 08:12:22282 directory_.GetPath().AppendASCII("myfile.filetype");
asanka655d1112015-03-07 05:33:41283 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 Hijikata235fc62b2020-12-08 03:48:32287#endif // BUILDFLAG(IS_CHROMEOS_ASH)
asanka655d1112015-03-07 05:33:41288
Yuta Hijikata235fc62b2020-12-08 03:48:32289#if defined(OS_POSIX) && !BUILDFLAG(IS_CHROMEOS_ASH)
asanka655d1112015-03-07 05:33:41290// On all other Posix platforms, the symbolic link tests should work as
291// expected.
292
293TEST_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
301TEST_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