blob: 4456b23d8d79e676b4b09c70ab3fca9eeffc548b [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 =
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:41