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