1 // Copyright 2012 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 <stddef.h>
6 #include <stdint.h>
7 
8 #include <memory>
9 
10 #include "ash/public/cpp/ash_features.h"
11 #include "base/base64.h"
12 #include "base/bind.h"
13 #include "base/path_service.h"
14 #include "base/run_loop.h"
15 #include "base/stl_util.h"
16 #include "chrome/browser/chromeos/crostini/crostini_manager.h"
17 #include "chrome/browser/chromeos/crostini/crostini_pref_names.h"
18 #include "chrome/browser/chromeos/crostini/fake_crostini_features.h"
19 #include "chrome/browser/chromeos/drive/drivefs_test_support.h"
20 #include "chrome/browser/chromeos/extensions/file_manager/event_router.h"
21 #include "chrome/browser/chromeos/extensions/file_manager/event_router_factory.h"
22 #include "chrome/browser/chromeos/extensions/file_manager/private_api_misc.h"
23 #include "chrome/browser/chromeos/file_manager/file_watcher.h"
24 #include "chrome/browser/chromeos/file_manager/mount_test_util.h"
25 #include "chrome/browser/chromeos/file_manager/path_util.h"
26 #include "chrome/browser/chromeos/file_system_provider/icon_set.h"
27 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
28 #include "chrome/browser/extensions/extension_apitest.h"
29 #include "chrome/browser/extensions/extension_function_test_utils.h"
30 #include "chrome/browser/signin/identity_manager_factory.h"
31 #include "chrome/common/chrome_features.h"
32 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.h"
33 #include "chromeos/constants/chromeos_features.h"
34 #include "chromeos/dbus/concierge/concierge_service.pb.h"
35 #include "chromeos/dbus/cros_disks_client.h"
36 #include "chromeos/disks/disk.h"
37 #include "chromeos/disks/mock_disk_mount_manager.h"
38 #include "components/drive/drive_pref_names.h"
39 #include "components/prefs/pref_service.h"
40 #include "components/signin/public/identity_manager/identity_test_utils.h"
41 #include "content/public/test/browser_test.h"
42 #include "extensions/common/extension.h"
43 #include "extensions/common/install_warning.h"
44 #include "google_apis/drive/test_util.h"
45 #include "storage/browser/file_system/external_mount_points.h"
46 
47 using ::testing::_;
48 using ::testing::ReturnRef;
49 
50 using chromeos::disks::Disk;
51 using chromeos::disks::DiskMountManager;
52 using chromeos::disks::FormatFileSystemType;
53 
54 namespace {
55 
56 struct TestDiskInfo {
57   const char* file_path;
58   bool write_disabled_by_policy;
59   const char* device_label;
60   const char* drive_label;
61   const char* vendor_id;
62   const char* vendor_name;
63   const char* product_id;
64   const char* product_name;
65   const char* fs_uuid;
66   const char* storage_device_path;
67   chromeos::DeviceType device_type;
68   uint64_t size_in_bytes;
69   bool is_parent;
70   bool is_read_only_hardware;
71   bool has_media;
72   bool on_boot_device;
73   bool on_removable_device;
74   bool is_hidden;
75   const char* file_system_type;
76   const char* base_mount_path;
77 };
78 
79 struct TestMountPoint {
80   std::string source_path;
81   std::string mount_path;
82   chromeos::MountType mount_type;
83   chromeos::disks::MountCondition mount_condition;
84 
85   // -1 if there is no disk info.
86   int disk_info_index;
87 };
88 
89 TestDiskInfo kTestDisks[] = {{"file_path1",
90                               false,
91                               "device_label1",
92                               "drive_label1",
93                               "0123",
94                               "vendor1",
95                               "abcd",
96                               "product1",
97                               "FFFF-FFFF",
98                               "storage_device_path1",
99                               chromeos::DEVICE_TYPE_USB,
100                               1073741824,
101                               false,
102                               false,
103                               false,
104                               false,
105                               false,
106                               false,
107                               "exfat",
108                               ""},
109                              {"file_path2",
110                               false,
111                               "device_label2",
112                               "drive_label2",
113                               "4567",
114                               "vendor2",
115                               "cdef",
116                               "product2",
117                               "0FFF-FFFF",
118                               "storage_device_path2",
119                               chromeos::DEVICE_TYPE_MOBILE,
120                               47723,
121                               true,
122                               true,
123                               true,
124                               true,
125                               false,
126                               false,
127                               "exfat",
128                               ""},
129                              {"file_path3",
130                               true,  // write_disabled_by_policy
131                               "device_label3",
132                               "drive_label3",
133                               "89ab",
134                               "vendor3",
135                               "ef01",
136                               "product3",
137                               "00FF-FFFF",
138                               "storage_device_path3",
139                               chromeos::DEVICE_TYPE_OPTICAL_DISC,
140                               0,
141                               true,
142                               false,  // is_hardware_read_only
143                               false,
144                               true,
145                               false,
146                               false,
147                               "exfat",
148                               ""}};
149 
AddLocalFileSystem(Profile * profile,base::FilePath root)150 void AddLocalFileSystem(Profile* profile, base::FilePath root) {
151   const char kLocalMountPointName[] = "local";
152   const char kTestFileContent[] = "The five boxing wizards jumped quickly";
153 
154   {
155     base::ScopedAllowBlockingForTesting allow_io;
156     ASSERT_TRUE(base::CreateDirectory(root.AppendASCII("test_dir")));
157     ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
158         root.AppendASCII("test_dir").AppendASCII("test_file.txt"),
159         kTestFileContent));
160   }
161 
162   ASSERT_TRUE(
163       content::BrowserContext::GetMountPoints(profile)->RegisterFileSystem(
164           kLocalMountPointName, storage::kFileSystemTypeNativeLocal,
165           storage::FileSystemMountOption(), root));
166   file_manager::VolumeManager::Get(profile)->AddVolumeForTesting(
167       root, file_manager::VOLUME_TYPE_TESTING, chromeos::DEVICE_TYPE_UNKNOWN,
168       false /* read_only */);
169 }
170 
171 }  // namespace
172 
173 class FileManagerPrivateApiTest : public extensions::ExtensionApiTest {
174  public:
FileManagerPrivateApiTest()175   FileManagerPrivateApiTest() : disk_mount_manager_mock_(nullptr) {
176     InitMountPoints();
177   }
178 
~FileManagerPrivateApiTest()179   ~FileManagerPrivateApiTest() override {
180     DCHECK(!disk_mount_manager_mock_);
181     DCHECK(!event_router_);
182   }
183 
SetUpUserDataDirectory()184   bool SetUpUserDataDirectory() override {
185     return drive::SetUpUserDataDirectoryForDriveFsTest();
186   }
187 
SetUpOnMainThread()188   void SetUpOnMainThread() override {
189     extensions::ExtensionApiTest::SetUpOnMainThread();
190     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
191 
192     event_router_ = file_manager::EventRouterFactory::GetForProfile(profile());
193   }
194 
TearDownOnMainThread()195   void TearDownOnMainThread() override {
196     event_router_ = nullptr;
197     extensions::ExtensionApiTest::TearDownOnMainThread();
198   }
199 
200   // ExtensionApiTest override
SetUpInProcessBrowserTestFixture()201   void SetUpInProcessBrowserTestFixture() override {
202     extensions::ExtensionApiTest::SetUpInProcessBrowserTestFixture();
203     disk_mount_manager_mock_ = new chromeos::disks::MockDiskMountManager;
204     chromeos::disks::DiskMountManager::InitializeForTesting(
205         disk_mount_manager_mock_);
206     disk_mount_manager_mock_->SetupDefaultReplies();
207 
208     // override mock functions.
209     ON_CALL(*disk_mount_manager_mock_, FindDiskBySourcePath(_)).WillByDefault(
210         Invoke(this, &FileManagerPrivateApiTest::FindVolumeBySourcePath));
211     EXPECT_CALL(*disk_mount_manager_mock_, disks())
212         .WillRepeatedly(ReturnRef(volumes_));
213     EXPECT_CALL(*disk_mount_manager_mock_, mount_points())
214         .WillRepeatedly(ReturnRef(mount_points_));
215   }
216 
217   // ExtensionApiTest override
TearDownInProcessBrowserTestFixture()218   void TearDownInProcessBrowserTestFixture() override {
219     chromeos::disks::DiskMountManager::Shutdown();
220     disk_mount_manager_mock_ = nullptr;
221 
222     extensions::ExtensionApiTest::TearDownInProcessBrowserTestFixture();
223   }
224 
225  private:
InitMountPoints()226   void InitMountPoints() {
227     const TestMountPoint kTestMountPoints[] = {
228       {
229         "device_path1",
230         chromeos::CrosDisksClient::GetRemovableDiskMountPoint().AppendASCII(
231             "mount_path1").AsUTF8Unsafe(),
232         chromeos::MOUNT_TYPE_DEVICE,
233         chromeos::disks::MOUNT_CONDITION_NONE,
234         0
235       },
236       {
237         "device_path2",
238         chromeos::CrosDisksClient::GetRemovableDiskMountPoint().AppendASCII(
239             "mount_path2").AsUTF8Unsafe(),
240         chromeos::MOUNT_TYPE_DEVICE,
241         chromeos::disks::MOUNT_CONDITION_NONE,
242         1
243       },
244       {
245         "device_path3",
246         chromeos::CrosDisksClient::GetRemovableDiskMountPoint().AppendASCII(
247             "mount_path3").AsUTF8Unsafe(),
248         chromeos::MOUNT_TYPE_DEVICE,
249         chromeos::disks::MOUNT_CONDITION_NONE,
250         2
251       },
252       {
253         // Set source path inside another mounted volume.
254         chromeos::CrosDisksClient::GetRemovableDiskMountPoint().AppendASCII(
255             "mount_path3/archive.zip").AsUTF8Unsafe(),
256         chromeos::CrosDisksClient::GetArchiveMountPoint().AppendASCII(
257             "archive_mount_path").AsUTF8Unsafe(),
258         chromeos::MOUNT_TYPE_ARCHIVE,
259         chromeos::disks::MOUNT_CONDITION_NONE,
260         -1
261       }
262     };
263 
264     for (size_t i = 0; i < base::size(kTestMountPoints); i++) {
265       mount_points_.insert(DiskMountManager::MountPointMap::value_type(
266           kTestMountPoints[i].mount_path,
267           DiskMountManager::MountPointInfo(kTestMountPoints[i].source_path,
268                                            kTestMountPoints[i].mount_path,
269                                            kTestMountPoints[i].mount_type,
270                                            kTestMountPoints[i].mount_condition)
271       ));
272       int disk_info_index = kTestMountPoints[i].disk_info_index;
273       if (kTestMountPoints[i].disk_info_index >= 0) {
274         EXPECT_GT(base::size(kTestDisks), static_cast<size_t>(disk_info_index));
275         if (static_cast<size_t>(disk_info_index) >= base::size(kTestDisks))
276           return;
277 
278         std::unique_ptr<Disk> disk =
279             Disk::Builder()
280                 .SetDevicePath(kTestMountPoints[i].source_path)
281                 .SetMountPath(kTestMountPoints[i].mount_path)
282                 .SetWriteDisabledByPolicy(
283                     kTestDisks[disk_info_index].write_disabled_by_policy)
284                 .SetFilePath(kTestDisks[disk_info_index].file_path)
285                 .SetDeviceLabel(kTestDisks[disk_info_index].device_label)
286                 .SetDriveLabel(kTestDisks[disk_info_index].drive_label)
287                 .SetVendorId(kTestDisks[disk_info_index].vendor_id)
288                 .SetVendorName(kTestDisks[disk_info_index].vendor_name)
289                 .SetProductId(kTestDisks[disk_info_index].product_id)
290                 .SetProductName(kTestDisks[disk_info_index].product_name)
291                 .SetFileSystemUUID(kTestDisks[disk_info_index].fs_uuid)
292                 .SetStorageDevicePath(
293                     kTestDisks[disk_info_index].storage_device_path)
294                 .SetDeviceType(kTestDisks[disk_info_index].device_type)
295                 .SetSizeInBytes(kTestDisks[disk_info_index].size_in_bytes)
296                 .SetIsParent(kTestDisks[disk_info_index].is_parent)
297                 .SetIsReadOnlyHardware(
298                     kTestDisks[disk_info_index].is_read_only_hardware)
299                 .SetHasMedia(kTestDisks[disk_info_index].has_media)
300                 .SetOnBootDevice(kTestDisks[disk_info_index].on_boot_device)
301                 .SetOnRemovableDevice(
302                     kTestDisks[disk_info_index].on_removable_device)
303                 .SetIsHidden(kTestDisks[disk_info_index].is_hidden)
304                 .SetFileSystemType(kTestDisks[disk_info_index].file_system_type)
305                 .SetBaseMountPath(kTestDisks[disk_info_index].base_mount_path)
306                 .Build();
307 
308         volumes_.insert(DiskMountManager::DiskMap::value_type(
309             kTestMountPoints[i].source_path, std::move(disk)));
310       }
311     }
312   }
313 
FindVolumeBySourcePath(const std::string & source_path)314   const Disk* FindVolumeBySourcePath(const std::string& source_path) {
315     auto volume_it = volumes_.find(source_path);
316     return (volume_it == volumes_.end()) ? nullptr : volume_it->second.get();
317   }
318 
319  protected:
SshfsMount(const std::string & source_path,const std::string & source_format,const std::string & mount_label,const std::vector<std::string> & mount_options,chromeos::MountType type,chromeos::MountAccessMode access_mode)320   void SshfsMount(const std::string& source_path,
321                   const std::string& source_format,
322                   const std::string& mount_label,
323                   const std::vector<std::string>& mount_options,
324                   chromeos::MountType type,
325                   chromeos::MountAccessMode access_mode) {
326     disk_mount_manager_mock_->NotifyMountEvent(
327         chromeos::disks::DiskMountManager::MountEvent::MOUNTING,
328         chromeos::MountError::MOUNT_ERROR_NONE,
329         chromeos::disks::DiskMountManager::MountPointInfo(
330             source_path, "/media/fuse/" + mount_label,
331             chromeos::MountType::MOUNT_TYPE_NETWORK_STORAGE,
332             chromeos::disks::MountCondition::MOUNT_CONDITION_NONE));
333   }
334 
ExpectCrostiniMount()335   void ExpectCrostiniMount() {
336     std::string known_hosts;
337     base::Base64Encode("[hostname]:2222 pubkey", &known_hosts);
338     std::string identity;
339     base::Base64Encode("privkey", &identity);
340     std::vector<std::string> mount_options = {
341         "UserKnownHostsBase64=" + known_hosts, "IdentityBase64=" + identity,
342         "Port=2222"};
343     EXPECT_CALL(*disk_mount_manager_mock_,
344                 MountPath("sshfs://testuser@hostname:", "",
345                           "crostini_user_termina_penguin", mount_options,
346                           chromeos::MOUNT_TYPE_NETWORK_STORAGE,
347                           chromeos::MOUNT_ACCESS_MODE_READ_WRITE))
348         .WillOnce(Invoke(this, &FileManagerPrivateApiTest::SshfsMount));
349   }
350 
351   base::ScopedTempDir temp_dir_;
352   chromeos::disks::MockDiskMountManager* disk_mount_manager_mock_;
353   DiskMountManager::DiskMap volumes_;
354   DiskMountManager::MountPointMap mount_points_;
355   file_manager::EventRouter* event_router_ = nullptr;
356 };
357 
358 // Parameterize by whether holding space feature is enabled.
359 class FileManagerPrivateHoldingSpaceApiTest
360     : public FileManagerPrivateApiTest,
361       public testing::WithParamInterface<bool> {
362  public:
FileManagerPrivateHoldingSpaceApiTest()363   FileManagerPrivateHoldingSpaceApiTest() {
364     scoped_feature_list_.InitWithFeatureState(
365         ash::features::kTemporaryHoldingSpace, GetParam());
366   }
367   ~FileManagerPrivateHoldingSpaceApiTest() override = default;
368 
369  private:
370   base::test::ScopedFeatureList scoped_feature_list_;
371 };
372 
373 INSTANTIATE_TEST_SUITE_P(HoldingSpaceEnabled,
374                          FileManagerPrivateHoldingSpaceApiTest,
375                          testing::Bool());
376 
IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest,Mount)377 IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest, Mount) {
378   using chromeos::file_system_provider::IconSet;
379   profile()->GetPrefs()->SetBoolean(drive::prefs::kDisableDrive, true);
380 
381   // Add a provided file system, to test passing the |configurable| and
382   // |source| flags properly down to Files app.
383   IconSet icon_set;
384   icon_set.SetIcon(IconSet::IconSize::SIZE_16x16,
385                    GURL("chrome://resources/testing-provider-id-16.jpg"));
386   icon_set.SetIcon(IconSet::IconSize::SIZE_32x32,
387                    GURL("chrome://resources/testing-provider-id-32.jpg"));
388   chromeos::file_system_provider::ProvidedFileSystemInfo info(
389       "testing-provider-id", chromeos::file_system_provider::MountOptions(),
390       base::FilePath(), true /* configurable */, false /* watchable */,
391       extensions::SOURCE_NETWORK, icon_set);
392 
393   file_manager::VolumeManager::Get(browser()->profile())
394       ->AddVolumeForTesting(file_manager::Volume::CreateForProvidedFileSystem(
395           info, file_manager::MOUNT_CONTEXT_AUTO));
396 
397   // We will call fileManagerPrivate.unmountVolume once. To test that method, we
398   // check that UnmountPath is really called with the same value.
399   EXPECT_CALL(*disk_mount_manager_mock_, UnmountPath(_, _)).Times(0);
400   EXPECT_CALL(
401       *disk_mount_manager_mock_,
402       UnmountPath(chromeos::CrosDisksClient::GetRemovableDiskMountPoint()
403                       .AppendASCII("mount_path1")
404                       .AsUTF8Unsafe(),
405                   _))
406       .Times(1);
407   EXPECT_CALL(*disk_mount_manager_mock_,
408               UnmountPath(chromeos::CrosDisksClient::GetArchiveMountPoint()
409                               .AppendASCII("archive_mount_path")
410                               .AsUTF8Unsafe(),
411                           _))
412       .Times(1);
413 
414   ASSERT_TRUE(RunComponentExtensionTest("file_browser/mount_test"))
415       << message_;
416 }
417 
IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest,FormatVolume)418 IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest, FormatVolume) {
419   // Catch-all rule to ensure that FormatMountedDevice does not get called with
420   // other arguments that don't match the rules below.
421   EXPECT_CALL(*disk_mount_manager_mock_, FormatMountedDevice(_, _, _)).Times(0);
422 
423   EXPECT_CALL(*disk_mount_manager_mock_,
424               FormatMountedDevice(
425                   chromeos::CrosDisksClient::GetRemovableDiskMountPoint()
426                       .AppendASCII("mount_path1")
427                       .AsUTF8Unsafe(),
428                   FormatFileSystemType::kVfat, "NEWLABEL1"))
429       .Times(1);
430   EXPECT_CALL(*disk_mount_manager_mock_,
431               FormatMountedDevice(
432                   chromeos::CrosDisksClient::GetRemovableDiskMountPoint()
433                       .AppendASCII("mount_path2")
434                       .AsUTF8Unsafe(),
435                   FormatFileSystemType::kExfat, "NEWLABEL2"))
436       .Times(1);
437   EXPECT_CALL(*disk_mount_manager_mock_,
438               FormatMountedDevice(
439                   chromeos::CrosDisksClient::GetRemovableDiskMountPoint()
440                       .AppendASCII("mount_path3")
441                       .AsUTF8Unsafe(),
442                   FormatFileSystemType::kNtfs, "NEWLABEL3"))
443       .Times(1);
444 
445   ASSERT_TRUE(RunComponentExtensionTest("file_browser/format_test"));
446 }
447 
IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest,Permissions)448 IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest, Permissions) {
449   EXPECT_TRUE(
450       RunExtensionTestIgnoreManifestWarnings("file_browser/permissions"));
451   const extensions::Extension* extension = GetSingleLoadedExtension();
452   ASSERT_TRUE(extension);
453   ASSERT_EQ(1u, extension->install_warnings().size());
454   const extensions::InstallWarning& warning = extension->install_warnings()[0];
455   EXPECT_EQ("fileManagerPrivate", warning.key);
456 }
457 
IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest,ContentChecksum)458 IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest, ContentChecksum) {
459   AddLocalFileSystem(browser()->profile(), temp_dir_.GetPath());
460 
461   ASSERT_TRUE(RunComponentExtensionTest("file_browser/content_checksum_test"));
462 }
463 
IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest,Recent)464 IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest, Recent) {
465   const base::FilePath downloads_dir = temp_dir_.GetPath();
466 
467   ASSERT_TRUE(file_manager::VolumeManager::Get(browser()->profile())
468                   ->RegisterDownloadsDirectoryForTesting(downloads_dir));
469 
470   // Create test files.
471   {
472     base::ScopedAllowBlockingForTesting allow_io;
473     base::File image_file(downloads_dir.Append("all-justice.jpg"),
474                           base::File::FLAG_CREATE | base::File::FLAG_WRITE);
475     ASSERT_TRUE(image_file.IsValid());
476     base::File audio_file(downloads_dir.Append("all-justice.mp3"),
477                           base::File::FLAG_CREATE | base::File::FLAG_WRITE);
478     ASSERT_TRUE(audio_file.IsValid());
479     base::File video_file(downloads_dir.Append("all-justice.mp4"),
480                           base::File::FLAG_CREATE | base::File::FLAG_WRITE);
481     ASSERT_TRUE(video_file.IsValid());
482   }
483 
484   ASSERT_TRUE(RunComponentExtensionTest("file_browser/recent_test"));
485 }
486 
IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest,MediaMetadata)487 IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest, MediaMetadata) {
488   const base::FilePath test_dir = temp_dir_.GetPath();
489   AddLocalFileSystem(browser()->profile(), test_dir);
490 
491   // Get source media/test/data directory path.
492   base::FilePath root_dir;
493   CHECK(base::PathService::Get(base::DIR_SOURCE_ROOT, &root_dir));
494   const base::FilePath media_test_data_dir =
495       root_dir.AppendASCII("media").AppendASCII("test").AppendASCII("data");
496 
497   // Returns a path to a media/test/data test file.
498   auto get_media_test_data_file = [&](const std::string& file) {
499     return media_test_data_dir.Append(base::FilePath::FromUTF8Unsafe(file));
500   };
501 
502   // Create media test files.
503   {
504     base::ScopedAllowBlockingForTesting allow_io;
505 
506     const base::FilePath video = get_media_test_data_file("90rotation.mp4");
507     ASSERT_TRUE(base::CopyFile(video, test_dir.Append(video.BaseName())));
508 
509     const base::FilePath audio = get_media_test_data_file("id3_png_test.mp3");
510     ASSERT_TRUE(base::CopyFile(audio, test_dir.Append(audio.BaseName())));
511   }
512 
513   // Get source chrome/test/data/chromeos/file_manager directory path.
514   const base::FilePath files_test_data_dir = root_dir.AppendASCII("chrome")
515                                                  .AppendASCII("test")
516                                                  .AppendASCII("data")
517                                                  .AppendASCII("chromeos")
518                                                  .AppendASCII("file_manager");
519 
520   // Returns a path to a chrome/test/data/chromeos/file_manager test file.
521   auto get_files_test_data_dir = [&](const std::string& file) {
522     return files_test_data_dir.Append(base::FilePath::FromUTF8Unsafe(file));
523   };
524 
525   // Create files test files.
526   {
527     base::ScopedAllowBlockingForTesting allow_io;
528 
529     const base::FilePath broke = get_files_test_data_dir("broken.jpg");
530     ASSERT_TRUE(base::CopyFile(broke, test_dir.Append(broke.BaseName())));
531 
532     const base::FilePath empty = get_files_test_data_dir("empty.txt");
533     ASSERT_TRUE(base::CopyFile(empty, test_dir.Append(empty.BaseName())));
534 
535     const base::FilePath image = get_files_test_data_dir("image3.jpg");
536     ASSERT_TRUE(base::CopyFile(image, test_dir.Append(image.BaseName())));
537   }
538 
539   ASSERT_TRUE(RunComponentExtensionTest("file_browser/media_metadata"));
540 }
541 
IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest,Crostini)542 IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest, Crostini) {
543   crostini::FakeCrostiniFeatures crostini_features;
544   crostini_features.set_ui_allowed(true);
545   crostini_features.set_enabled(true);
546 
547   // Setup CrostiniManager for testing.
548   crostini::CrostiniManager* crostini_manager =
549       crostini::CrostiniManager::GetForProfile(browser()->profile());
550   crostini_manager->set_skip_restart_for_testing();
551   crostini_manager->AddRunningVmForTesting(crostini::kCrostiniDefaultVmName);
552   crostini_manager->AddRunningContainerForTesting(
553       crostini::kCrostiniDefaultVmName,
554       crostini::ContainerInfo(crostini::kCrostiniDefaultContainerName,
555                               "testuser", "/home/testuser", "PLACEHOLDER_IP"));
556 
557   ExpectCrostiniMount();
558 
559   // Add 'testing' volume with 'test_dir', create 'share_dir' in Downloads.
560   AddLocalFileSystem(browser()->profile(), temp_dir_.GetPath());
561   base::FilePath downloads;
562   ASSERT_TRUE(
563       storage::ExternalMountPoints::GetSystemInstance()->GetRegisteredPath(
564           file_manager::util::GetDownloadsMountPointName(browser()->profile()),
565           &downloads));
566   // Setup prefs guest_os.paths_shared_to_vms.
567   base::FilePath shared1 = downloads.AppendASCII("shared1");
568   base::FilePath shared2 = downloads.AppendASCII("shared2");
569   {
570     base::ScopedAllowBlockingForTesting allow_io;
571     ASSERT_TRUE(base::CreateDirectory(downloads.AppendASCII("share_dir")));
572     ASSERT_TRUE(base::CreateDirectory(shared1));
573     ASSERT_TRUE(base::CreateDirectory(shared2));
574   }
575   guest_os::GuestOsSharePath* guest_os_share_path =
576       guest_os::GuestOsSharePath::GetForProfile(browser()->profile());
577   guest_os_share_path->RegisterPersistedPath(crostini::kCrostiniDefaultVmName,
578                                              shared1);
579   guest_os_share_path->RegisterPersistedPath(crostini::kCrostiniDefaultVmName,
580                                              shared2);
581 
582   ASSERT_TRUE(RunComponentExtensionTest("file_browser/crostini_test"));
583 }
584 
IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest,CrostiniIncognito)585 IN_PROC_BROWSER_TEST_F(FileManagerPrivateApiTest, CrostiniIncognito) {
586   crostini::FakeCrostiniFeatures crostini_features;
587   crostini_features.set_ui_allowed(true);
588   crostini_features.set_enabled(true);
589 
590   // Setup CrostiniManager for testing.
591   crostini::CrostiniManager* crostini_manager =
592       crostini::CrostiniManager::GetForProfile(browser()->profile());
593   crostini_manager->set_skip_restart_for_testing();
594   crostini_manager->AddRunningVmForTesting(crostini::kCrostiniDefaultVmName);
595   crostini_manager->AddRunningContainerForTesting(
596       crostini::kCrostiniDefaultVmName,
597       crostini::ContainerInfo(crostini::kCrostiniDefaultContainerName,
598                               "testuser", "/home/testuser", "PLACEHOLDER_IP"));
599 
600   ExpectCrostiniMount();
601 
602   scoped_refptr<extensions::FileManagerPrivateMountCrostiniFunction> function(
603       new extensions::FileManagerPrivateMountCrostiniFunction());
604   // Use incognito profile.
605   function->set_browser_context(browser()->profile()->GetPrimaryOTRProfile());
606 
607   extensions::api_test_utils::SendResponseHelper response_helper(
608       function.get());
609   function->RunWithValidation()->Execute();
610   response_helper.WaitForResponse();
611   EXPECT_TRUE(response_helper.GetResponse());
612 }
613 
IN_PROC_BROWSER_TEST_P(FileManagerPrivateHoldingSpaceApiTest,HoldingSpace)614 IN_PROC_BROWSER_TEST_P(FileManagerPrivateHoldingSpaceApiTest, HoldingSpace) {
615   const base::FilePath test_dir = temp_dir_.GetPath();
616   AddLocalFileSystem(browser()->profile(), test_dir);
617 
618   {
619     base::ScopedAllowBlockingForTesting allow_io;
620     base::File image_file(test_dir.Append("test_image.jpg"),
621                           base::File::FLAG_CREATE | base::File::FLAG_WRITE);
622     ASSERT_TRUE(image_file.IsValid());
623     base::File audio_file(test_dir.Append("test_audio.mp3"),
624                           base::File::FLAG_CREATE | base::File::FLAG_WRITE);
625     ASSERT_TRUE(audio_file.IsValid());
626     base::File video_file(test_dir.Append("test_video.mp4"),
627                           base::File::FLAG_CREATE | base::File::FLAG_WRITE);
628     ASSERT_TRUE(video_file.IsValid());
629   }
630 
631   if (GetParam()) {
632     EXPECT_TRUE(RunComponentExtensionTest("file_browser/holding_space"));
633   } else {
634     EXPECT_TRUE(
635         RunComponentExtensionTest("file_browser/holding_space_disabled"));
636   }
637 }
638