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 = |
Lukasz Anforowicz | eda637d2 | 2021-05-20 00:32:23 | [diff] [blame] | 57 | browser_context->GetMountPoints(); |
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) { |
Peter Boström | 6b70182 | 2021-04-15 03:53:08 | [diff] [blame] | 78 | content_browser_client_ = |
| 79 | std::make_unique<PlatformUtilTestContentBrowserClient>(); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 80 | 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 Anforowicz | eda637d2 | 2021-05-20 00:32:23 | [diff] [blame] | 84 | GetProfile()->GetMountPoints()->RegisterFileSystem( |
| 85 | "test", storage::kFileSystemTypeLocal, storage::FileSystemMountOption(), |
| 86 | test_directory); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 87 | |
| 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); |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 109 | std::unique_ptr<base::Value> manifest = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 110 | json_string_deserializer.Deserialize(&error_code, &error); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 111 | 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 Kim | abc2338 | 2021-03-18 03:09:18 | [diff] [blame] | 119 | extensions::mojom::ManifestLocation::kInvalidLocation, |
| 120 | *manifest_dictionary, extensions::Extension::NO_FLAGS, &error); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 121 | ASSERT_TRUE(error.empty()) << error; |
| 122 | extensions::ExtensionRegistry::Get(GetProfile())->AddEnabled(extension); |
| 123 | } |
| 124 | |
Dominick Ng | 5115465 | 2019-09-25 07:44:20 | [diff] [blame] | 125 | void SetUp() override { |
| 126 | BrowserWithTestWindowTest::SetUp(); |
| 127 | base::RunLoop().RunUntilIdle(); |
| 128 | } |
| 129 | |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 130 | 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: |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 142 | std::unique_ptr<content::ContentBrowserClient> content_browser_client_; |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 143 | content::ContentBrowserClient* old_content_browser_client_ = nullptr; |
| 144 | }; |
| 145 | |
| 146 | #else |
| 147 | |
| 148 | // Test fixture used by all desktop platforms other than Chrome OS. |
| 149 | class PlatformUtilTestBase : public testing::Test { |
| 150 | protected: |
| 151 | Profile* GetProfile() { return nullptr; } |
| 152 | void SetUpPlatformFixture(const base::FilePath&) {} |
| 153 | |
| 154 | private: |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 155 | content::BrowserTaskEnvironment task_environment_; |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | #endif |
| 159 | |
| 160 | class 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 Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 166 | const int kTestFileDataLength = base::size(kTestFileData) - 1; |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 167 | |
Dominick Ng | 5115465 | 2019-09-25 07:44:20 | [diff] [blame] | 168 | // This prevents platform_util from invoking any shell or external APIs |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 169 | // 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. |
vabr | 8023d87 | 2016-09-15 08:12:22 | [diff] [blame] | 176 | existing_file_ = directory_.GetPath().AppendASCII("test_file.txt"); |
asanka | 655d111 | 2015-03-07 05:33:41 | [
|