1 // Copyright 2014 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 <stdint.h>
6
7 #include "base/bind.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h"
10 #include "chromeos/disks/mock_disk_mount_manager.h"
11 #include "content/public/test/browser_task_environment.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace extensions {
15
16 namespace {
17
18 const char kDevicePathUSB[] = "/dev/test-usb";
19 const char kDevicePathSD[] = "/dev/test-sd";
20 const char kMountPath[] = "/test-mount";
21 const char kDeviceId[] = "FFFF-FFFF";
22 const char kDeviceName[] = "Test Device Name";
23 const char kVendorName[] = "Test Vendor";
24 const char kProductName[] = "Test Product";
25 const uint64_t kDeviceSize = 1024 * 1024 * 1024;
26 const bool kOnRemovableDevice = true;
27 const char kDiskFileSystemType[] = "vfat";
28
29 const char kUnknownSDDiskModel[] = "SD Card";
30 const char kUnknownUSBDiskModel[] = "USB Drive";
31
32 class RemovableStorageProviderChromeOsUnitTest : public testing::Test {
33 public:
RemovableStorageProviderChromeOsUnitTest()34 RemovableStorageProviderChromeOsUnitTest() {}
SetUp()35 void SetUp() override {
36 disk_mount_manager_mock_ = new chromeos::disks::MockDiskMountManager();
37 chromeos::disks::DiskMountManager::InitializeForTesting(
38 disk_mount_manager_mock_);
39 disk_mount_manager_mock_->SetupDefaultReplies();
40 }
41
TearDown()42 void TearDown() override { chromeos::disks::DiskMountManager::Shutdown(); }
43
DevicesCallback(scoped_refptr<StorageDeviceList> devices)44 void DevicesCallback(scoped_refptr<StorageDeviceList> devices) {
45 devices_ = devices;
46 }
47
CreateDisk(const std::string & device_path,chromeos::DeviceType device_type,bool is_parent,bool has_media,bool on_boot_device)48 void CreateDisk(const std::string& device_path,
49 chromeos::DeviceType device_type,
50 bool is_parent,
51 bool has_media,
52 bool on_boot_device) {
53 return CreateDisk(device_path,
54 kVendorName,
55 kProductName,
56 device_type,
57 is_parent,
58 has_media,
59 on_boot_device);
60 }
61
CreateDisk(const std::string & device_path,const std::string & vendor_name,const std::string & product_name,chromeos::DeviceType device_type,bool is_parent,bool has_media,bool on_boot_device)62 void CreateDisk(const std::string& device_path,
63 const std::string& vendor_name,
64 const std::string& product_name,
65 chromeos::DeviceType device_type,
66 bool is_parent,
67 bool has_media,
68 bool on_boot_device) {
69 chromeos::disks::DiskMountManager::MountPointInfo mount_info(
70 device_path, kMountPath, chromeos::MOUNT_TYPE_DEVICE,
71 chromeos::disks::MOUNT_CONDITION_NONE);
72 disk_mount_manager_mock_->CreateDiskEntryForMountDevice(
73 mount_info, kDeviceId, kDeviceName, vendor_name, product_name,
74 device_type, kDeviceSize, is_parent, has_media, on_boot_device,
75 kOnRemovableDevice, kDiskFileSystemType);
76 }
77
78 // Checks if the DeviceList has a specific entry.
FindDevice(StorageDeviceList * list,const std::string & file_path)79 api::image_writer_private::RemovableStorageDevice* FindDevice(
80 StorageDeviceList* list,
81 const std::string& file_path) {
82 for (auto& device : list->data) {
83 if (device.storage_unit_id == file_path)
84 return &device;
85 }
86 return nullptr;
87 }
88
ExpectDevice(StorageDeviceList * list,const std::string & device_path,const std::string & vendor,const std::string & model,uint64_t capacity)89 void ExpectDevice(StorageDeviceList* list,
90 const std::string& device_path,
91 const std::string& vendor,
92 const std::string& model,
93 uint64_t capacity) {
94 auto* device = FindDevice(devices_.get(), device_path);
95
96 ASSERT_TRUE(device);
97
98 EXPECT_EQ(device_path, device->storage_unit_id);
99 EXPECT_EQ(vendor, device->vendor);
100 EXPECT_EQ(model, device->model);
101 EXPECT_EQ(capacity, device->capacity);
102 }
103
104 content::BrowserTaskEnvironment task_environment_;
105 chromeos::disks::MockDiskMountManager* disk_mount_manager_mock_;
106 scoped_refptr<StorageDeviceList> devices_;
107 };
108
109 } // namespace
110
111 // Tests that GetAllDevices works as expected, only exposing USB and SD cards
112 // that are parents, have media and are not boot devices. Other flags are
113 // uninteresting or should not occur for these device types.
TEST_F(RemovableStorageProviderChromeOsUnitTest,GetAllDevices)114 TEST_F(RemovableStorageProviderChromeOsUnitTest, GetAllDevices) {
115 CreateDisk(kDevicePathUSB, chromeos::DEVICE_TYPE_USB, true, true, false);
116 CreateDisk(kDevicePathSD, chromeos::DEVICE_TYPE_SD, true, true, false);
117 CreateDisk("/dev/NotParent", chromeos::DEVICE_TYPE_USB, false, true, false);
118 CreateDisk("/dev/NoMedia", chromeos::DEVICE_TYPE_USB, true, false, false);
119 CreateDisk("/dev/OnBootDevice", chromeos::DEVICE_TYPE_USB, true, true, true);
120
121 RemovableStorageProvider::GetAllDevices(
122 base::BindOnce(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback,
123 base::Unretained(this)));
124
125 task_environment_.RunUntilIdle();
126
127 ASSERT_EQ(2U, devices_->data.size());
128
129 ExpectDevice(
130 devices_.get(), kDevicePathUSB, kVendorName, kProductName, kDeviceSize);
131 ExpectDevice(
132 devices_.get(), kDevicePathSD, kVendorName, kProductName, kDeviceSize);
133 }
134
135 // Tests that a USB drive with an empty vendor and product gets a generic name.
TEST_F(RemovableStorageProviderChromeOsUnitTest,EmptyProductAndModel)136 TEST_F(RemovableStorageProviderChromeOsUnitTest, EmptyProductAndModel) {
137 CreateDisk(
138 kDevicePathUSB, "", "", chromeos::DEVICE_TYPE_USB, true, true, false);
139 CreateDisk(
140 kDevicePathSD, "", "", chromeos::DEVICE_TYPE_SD, true, true, false);
141
142 RemovableStorageProvider::GetAllDevices(
143 base::BindOnce(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback,
144 base::Unretained(this)));
145
146 task_environment_.RunUntilIdle();
147
148 ASSERT_EQ(2U, devices_->data.size());
149
150 ExpectDevice(
151 devices_.get(), kDevicePathUSB, "", kUnknownUSBDiskModel, kDeviceSize);
152 ExpectDevice(
153 devices_.get(), kDevicePathSD, "", kUnknownSDDiskModel, kDeviceSize);
154 }
155
156 } // namespace extensions
157