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.
5 #include "components/storage_monitor/storage_monitor_chromeos.h"
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chromeos/disks/mock_disk_mount_manager.h"
14 #include "components/storage_monitor/mock_removable_storage_observer.h"
15 #include "components/storage_monitor/removable_device_constants.h"
16 #include "components/storage_monitor/storage_info.h"
17 #include "components/storage_monitor/test_media_transfer_protocol_manager_linux.h"
18 #include "components/storage_monitor/test_storage_monitor.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace storage_monitor {
27 using content::BrowserThread;
28 using chromeos::disks::DiskMountManager;
31 const char kDevice1[] = "/dev/d1";
32 const char kDevice1Name[] = "d1";
33 const char kDevice2[] = "/dev/disk/d2";
34 const char kDevice2Name[] = "d2";
35 const char kEmptyDeviceLabel[] = "";
36 const char kMountPointA[] = "mnt_a";
37 const char kMountPointB[] = "mnt_b";
38 const char kSDCardDeviceName1[] = "8.6 MB Amy_SD";
39 const char kSDCardDeviceName2[] = "8.6 MB SD Card";
40 const char kSDCardMountPoint1[] = "media/removable/Amy_SD";
41 const char kSDCardMountPoint2[] = "media/removable/SD Card";
42 const char kProductName[] = "Z101";
43 const char kUniqueId1[] = "FFFF-FFFF";
44 const char kUniqueId2[] = "FFFF-FF0F";
45 const char kVendorName[] = "CompanyA";
47 uint64 kDevice1SizeInBytes = 113048;
48 uint64 kDevice2SizeInBytes = 212312;
49 uint64 kSDCardSizeInBytes = 9000000;
51 std::string GetDCIMDeviceId(const std::string& unique_id) {
52 return StorageInfo::MakeDeviceId(
53 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM,
54 kFSUniqueIdPrefix + unique_id);
57 // A test version of StorageMonitorCros that exposes protected methods to tests.
58 class TestStorageMonitorCros : public StorageMonitorCros {
60 TestStorageMonitorCros() {}
62 virtual ~TestStorageMonitorCros() {}
64 virtual void Init() OVERRIDE {
65 SetMediaTransferProtocolManagerForTest(
66 new TestMediaTransferProtocolManagerLinux());
67 StorageMonitorCros::Init();
70 virtual void OnMountEvent(DiskMountManager::MountEvent event,
71 chromeos::MountError error_code,
72 const DiskMountManager::MountPointInfo& mount_info) OVERRIDE {
73 StorageMonitorCros::OnMountEvent(event, error_code, mount_info);
76 virtual bool GetStorageInfoForPath(const base::FilePath& path,
77 StorageInfo* device_info) const OVERRIDE {
78 return StorageMonitorCros::GetStorageInfoForPath(path, device_info);
80 virtual void EjectDevice(
81 const std::string& device_id,
82 base::Callback<void(EjectStatus)> callback) OVERRIDE {
83 StorageMonitorCros::EjectDevice(device_id, callback);
87 DISALLOW_COPY_AND_ASSIGN(TestStorageMonitorCros);
90 // Wrapper class to test StorageMonitorCros.
91 class StorageMonitorCrosTest : public testing::Test {
93 StorageMonitorCrosTest();
94 virtual ~StorageMonitorCrosTest();
96 void EjectNotify(StorageMonitor::EjectStatus status);
100 virtual void SetUp() OVERRIDE;
101 virtual void TearDown() OVERRIDE;
103 void MountDevice(chromeos::MountError error_code,
104 const DiskMountManager::MountPointInfo& mount_info,
105 const std::string& unique_id,
106 const std::string& device_label,
107 const std::string& vendor_name,
108 const std::string& product_name,
109 chromeos::DeviceType device_type,
110 uint64 device_size_in_bytes);
112 void UnmountDevice(chromeos::MountError error_code,
113 const DiskMountManager::MountPointInfo& mount_info);
115 uint64 GetDeviceStorageSize(const std::string& device_location);
117 // Create a directory named |dir| relative to the test directory.
118 // Set |with_dcim_dir| to true if the created directory will have a "DCIM"
120 // Returns the full path to the created directory on success, or an empty
122 base::FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir);
124 static void PostQuitToUIThread();
125 static void WaitForFileThread();
127 MockRemovableStorageObserver& observer() {
128 return *mock_storage_observer_;
131 TestStorageMonitorCros* monitor_;
133 // Owned by DiskMountManager.
134 chromeos::disks::MockDiskMountManager* disk_mount_manager_mock_;
136 StorageMonitor::EjectStatus status_;
139 content::TestBrowserThreadBundle thread_bundle_;
141 // Temporary directory for created test data.
142 base::ScopedTempDir scoped_temp_dir_;
144 // Objects that talks with StorageMonitorCros.
145 scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_;
147 DISALLOW_COPY_AND_ASSIGN(StorageMonitorCrosTest);
150 StorageMonitorCrosTest::StorageMonitorCrosTest()
152 disk_mount_manager_mock_(NULL),
153 status_(StorageMonitor::EJECT_FAILURE),
154 thread_bundle_(content::TestBrowserThreadBundle::REAL_FILE_THREAD) {
157 StorageMonitorCrosTest::~StorageMonitorCrosTest() {
160 void StorageMonitorCrosTest::SetUp() {
161 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI));
162 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
163 disk_mount_manager_mock_ = new chromeos::disks::MockDiskMountManager();
164 DiskMountManager::InitializeForTesting(disk_mount_manager_mock_);
165 disk_mount_manager_mock_->SetupDefaultReplies();
167 mock_storage_observer_.reset(new MockRemovableStorageObserver);
169 // Initialize the test subject.
170 TestStorageMonitor::Destroy();
171 monitor_ = new TestStorageMonitorCros();
172 scoped_ptr<StorageMonitor> pass_monitor(monitor_);
173 StorageMonitor::SetStorageMonitorForTesting(pass_monitor.Pass());
176 monitor_->AddObserver(mock_storage_observer_.get());
179 void StorageMonitorCrosTest::TearDown() {
180 monitor_->RemoveObserver(mock_storage_observer_.get());
183 disk_mount_manager_mock_ = NULL;
184 DiskMountManager::Shutdown();
188 void StorageMonitorCrosTest::MountDevice(
189 chromeos::MountError error_code,
190 const DiskMountManager::MountPointInfo& mount_info,
191 const std::string& unique_id,
192 const std::string& device_label,
193 const std::string& vendor_name,
194 const std::string& product_name,
195 chromeos::DeviceType device_type,
196 uint64 device_size_in_bytes) {
197 if (error_code == chromeos::MOUNT_ERROR_NONE) {
198 disk_mount_manager_mock_->CreateDiskEntryForMountDevice(
205 device_size_in_bytes,
206 false /* is_parent */,
207 true /* has_media */,
208 false /* on_boot_device */);
210 monitor_->OnMountEvent(DiskMountManager::MOUNTING, error_code, mount_info);
214 void StorageMonitorCrosTest::UnmountDevice(
215 chromeos::MountError error_code,
216 const DiskMountManager::MountPointInfo& mount_info) {
217 monitor_->OnMountEvent(DiskMountManager::UNMOUNTING, error_code, mount_info);
218 if (error_code == chromeos::MOUNT_ERROR_NONE)
219 disk_mount_manager_mock_->RemoveDiskEntryForMountDevice(mount_info);
223 uint64 StorageMonitorCrosTest::GetDeviceStorageSize(
224 const std::string& device_location) {
226 if (!monitor_->GetStorageInfoForPath(base::FilePath(device_location), &info))
229 return info.total_size_in_bytes();
232 base::FilePath StorageMonitorCrosTest::CreateMountPoint(
233 const std::string& dir, bool with_dcim_dir) {
234 base::FilePath return_path(scoped_temp_dir_.path());
235 return_path = return_path.AppendASCII(dir);
236 base::FilePath path(return_path);
238 path = path.Append(kDCIMDirectoryName);
239 if (!base::CreateDirectory(path))
240 return base::FilePath();
245 void StorageMonitorCrosTest::PostQuitToUIThread() {
246 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
247 base::MessageLoop::QuitClosure());
251 void StorageMonitorCrosTest::WaitForFileThread() {
252 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
253 base::Bind(&PostQuitToUIThread));
254 base::MessageLoop::current()->Run();
257 void StorageMonitorCrosTest::EjectNotify(StorageMonitor::EjectStatus status) {
261 // Simple test case where we attach and detach a media device.
262 TEST_F(StorageMonitorCrosTest, BasicAttachDetach) {
263 base::FilePath mount_path1 = CreateMountPoint(kMountPointA, true);
264 ASSERT_FALSE(mount_path1.empty());
265 DiskMountManager::MountPointInfo mount_info(
268 chromeos::MOUNT_TYPE_DEVICE,
269 chromeos::disks::MOUNT_CONDITION_NONE);
270 MountDevice(chromeos::MOUNT_ERROR_NONE,
276 chromeos::DEVICE_TYPE_USB,
277 kDevice1SizeInBytes);
278 EXPECT_EQ(1, observer().attach_calls());
279 EXPECT_EQ(0, observer().detach_calls());
280 EXPECT_EQ(GetDCIMDeviceId(kUniqueId1),
281 observer().last_attached().device_id());
282 EXPECT_EQ(mount_path1.value(), observer().last_attached().location());
284 UnmountDevice(chromeos::MOUNT_ERROR_NONE, mount_info);
285 EXPECT_EQ(1, observer().attach_calls());
286 EXPECT_EQ(1, observer().detach_calls());
287 EXPECT_EQ(GetDCIMDeviceId(kUniqueId1),
288 observer().last_detached().device_id());
290 base::FilePath mount_path2 = CreateMountPoint(kMountPointB, true);
291 ASSERT_FALSE(mount_path2.empty());
292 DiskMountManager::MountPointInfo mount_info2(
295 chromeos::MOUNT_TYPE_DEVICE,
296 chromeos::disks::MOUNT_CONDITION_NONE);
297 MountDevice(chromeos::MOUNT_ERROR_NONE,
303 chromeos::DEVICE_TYPE_USB,
304 kDevice2SizeInBytes);
305 EXPECT_EQ(2, observer().attach_calls());
306 EXPECT_EQ(1, observer().detach_calls());
307 EXPECT_EQ(GetDCIMDeviceId(kUniqueId2),
308 observer().last_attached().device_id());
309 EXPECT_EQ(mount_path2.value(), observer().last_attached().location());
311 UnmountDevice(chromeos::MOUNT_ERROR_NONE, mount_info2);
312 EXPECT_EQ(2, observer().attach_calls());
313 EXPECT_EQ(2, observer().detach_calls());
314 EXPECT_EQ(GetDCIMDeviceId(kUniqueId2),
315 observer().last_detached().device_id());
318 // Removable mass storage devices with no dcim folder are also recognized.
319 TEST_F(StorageMonitorCrosTest, NoDCIM) {
320 testing::Sequence mock_sequence;
321 base::FilePath mount_path = CreateMountPoint(kMountPointA, false);
322 const std::string kUniqueId = "FFFF-FFFF";
323 ASSERT_FALSE(mount_path.empty());
324 DiskMountManager::MountPointInfo mount_info(
327 chromeos::MOUNT_TYPE_DEVICE,
328 chromeos::disks::MOUNT_CONDITION_NONE);
329 const std::string device_id = StorageInfo::MakeDeviceId(
330 StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM,
331 kFSUniqueIdPrefix + kUniqueId);
332 MountDevice(chromeos::MOUNT_ERROR_NONE,
338 chromeos::DEVICE_TYPE_USB,
339 kDevice1SizeInBytes);
340 EXPECT_EQ(1, observer().attach_calls());
341 EXPECT_EQ(0, observer().detach_calls());
342 EXPECT_EQ(device_id, observer().last_attached().device_id());
343 EXPECT_EQ(mount_path.value(), observer().last_attached().location());
346 // Non device mounts and mount errors are ignored.
347 TEST_F(StorageMonitorCrosTest, Ignore) {
348 testing::Sequence mock_sequence;
349 base::FilePath mount_path = CreateMountPoint(kMountPointA, true);
350 const std::string kUniqueId = "FFFF-FFFF";
351 ASSERT_FALSE(mount_path.empty());
354 DiskMountManager::MountPointInfo mount_info(
357 chromeos::MOUNT_TYPE_DEVICE,
358 chromeos::disks::MOUNT_CONDITION_NONE);
359 MountDevice(chromeos::MOUNT_ERROR_UNKNOWN,
365 chromeos::DEVICE_TYPE_USB,
366 kDevice1SizeInBytes);
367 EXPECT_EQ(0, observer().attach_calls());
368 EXPECT_EQ(0, observer().detach_calls());
371 mount_info.mount_type = chromeos::MOUNT_TYPE_ARCHIVE;
372 MountDevice(chromeos::MOUNT_ERROR_NONE,
378 chromeos::DEVICE_TYPE_USB,
379 kDevice1SizeInBytes);
380 EXPECT_EQ(0, observer().attach_calls());
381 EXPECT_EQ(0, observer().detach_calls());
383 // Unsupported file system.
384 mount_info.mount_type = chromeos::MOUNT_TYPE_DEVICE;
385 mount_info.mount_condition =
386 chromeos::disks::MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM;
387 MountDevice(chromeos::MOUNT_ERROR_NONE,
393 chromeos::DEVICE_TYPE_USB,
394 kDevice1SizeInBytes);
395 EXPECT_EQ(0, observer().attach_calls());
396 EXPECT_EQ(0, observer().detach_calls());
399 TEST_F(StorageMonitorCrosTest, SDCardAttachDetach) {
400 base::FilePath mount_path1 = CreateMountPoint(kSDCardMountPoint1, true);
401 ASSERT_FALSE(mount_path1.empty());
402 DiskMountManager::MountPointInfo mount_info1(
405 chromeos::MOUNT_TYPE_DEVICE,
406 chromeos::disks::MOUNT_CONDITION_NONE);
407 MountDevice(chromeos::MOUNT_ERROR_NONE,
413 chromeos::DEVICE_TYPE_SD,
415 EXPECT_EQ(1, observer().attach_calls());
416 EXPECT_EQ(0, observer().detach_calls());
417 EXPECT_EQ(GetDCIMDeviceId(kUniqueId2),
418 observer().last_attached().device_id());
419 EXPECT_EQ(mount_path1.value(), observer().last_attached().location());
421 UnmountDevice(chromeos::MOUNT_ERROR_NONE, mount_info1);
422 EXPECT_EQ(1, observer().attach_calls());
423 EXPECT_EQ(1, observer().detach_calls());
424 EXPECT_EQ(GetDCIMDeviceId(kUniqueId2),
425 observer().last_detached().device_id());
427 base::FilePath mount_path2 = CreateMountPoint(kSDCardMountPoint2, true);
428 ASSERT_FALSE(mount_path2.empty());
429 DiskMountManager::MountPointInfo mount_info2(
432 chromeos::MOUNT_TYPE_DEVICE,
433 chromeos::disks::MOUNT_CONDITION_NONE);
434 MountDevice(chromeos::MOUNT_ERROR_NONE,
440 chromeos::DEVICE_TYPE_SD,
442 EXPECT_EQ(2, observer().attach_calls());
443 EXPECT_EQ(1, observer().detach_calls());
444 EXPECT_EQ(GetDCIMDeviceId(kUniqueId2),
445 observer().last_attached().device_id());
446 EXPECT_EQ(mount_path2.value(), observer().last_attached().location());
448 UnmountDevice(chromeos::MOUNT_ERROR_NONE, mount_info2);
449 EXPECT_EQ(2, observer().attach_calls());
450 EXPECT_EQ(2, observer().detach_calls());
451 EXPECT_EQ(GetDCIMDeviceId(kUniqueId2),
452 observer().last_detached().device_id());
455 TEST_F(StorageMonitorCrosTest, AttachDeviceWithEmptyLabel) {
456 base::FilePath mount_path1 = CreateMountPoint(kMountPointA, true);
457 ASSERT_FALSE(mount_path1.empty());
458 DiskMountManager::MountPointInfo mount_info(
461 chromeos::MOUNT_TYPE_DEVICE,
462 chromeos::disks::MOUNT_CONDITION_NONE);
463 MountDevice(chromeos::MOUNT_ERROR_NONE,
469 chromeos::DEVICE_TYPE_USB,
470 kDevice1SizeInBytes);
471 EXPECT_EQ(1, observer().attach_calls());
472 EXPECT_EQ(0, observer().detach_calls());
473 EXPECT_EQ(GetDCIMDeviceId(kUniqueId1),
474 observer().last_attached().device_id());
475 EXPECT_EQ(mount_path1.value(), observer().last_attached().location());
477 UnmountDevice(chromeos::MOUNT_ERROR_NONE, mount_info);
478 EXPECT_EQ(1, observer().attach_calls());
479 EXPECT_EQ(1, observer().detach_calls());
480 EXPECT_EQ(GetDCIMDeviceId(kUniqueId1),
481 observer().last_detached().device_id());
484 TEST_F(StorageMonitorCrosTest, GetStorageSize) {
485 base::FilePath mount_path1 = CreateMountPoint(kMountPointA, true);
486 ASSERT_FALSE(mount_path1.empty());
487 DiskMountManager::MountPointInfo mount_info(
490 chromeos::MOUNT_TYPE_DEVICE,
491 chromeos::disks::MOUNT_CONDITION_NONE);
492 MountDevice(chromeos::MOUNT_ERROR_NONE,
498 chromeos::DEVICE_TYPE_USB,
499 kDevice1SizeInBytes);
500 EXPECT_EQ(1, observer().attach_calls());
501 EXPECT_EQ(0, observer().detach_calls());
502 EXPECT_EQ(GetDCIMDeviceId(kUniqueId1),
503 observer().last_attached().device_id());
504 EXPECT_EQ(mount_path1.value(), observer().last_attached().location());
506 EXPECT_EQ(kDevice1SizeInBytes, GetDeviceStorageSize(mount_path1.value()));
507 UnmountDevice(chromeos::MOUNT_ERROR_NONE, mount_info);
508 EXPECT_EQ(1, observer().attach_calls());
509 EXPECT_EQ(1, observer().detach_calls());
510 EXPECT_EQ(GetDCIMDeviceId(kUniqueId1),
511 observer().last_detached().device_id());
514 void UnmountFake(const std::string& location,
515 chromeos::UnmountOptions options,
516 const DiskMountManager::UnmountPathCallback& cb) {
517 cb.Run(chromeos::MOUNT_ERROR_NONE);
520 TEST_F(StorageMonitorCrosTest, EjectTest) {
521 base::FilePath mount_path1 = CreateMountPoint(kMountPointA, true);
522 ASSERT_FALSE(mount_path1.empty());
523 DiskMountManager::MountPointInfo mount_info(
526 chromeos::MOUNT_TYPE_DEVICE,
527 chromeos::disks::MOUNT_CONDITION_NONE);
528 MountDevice(chromeos::MOUNT_ERROR_NONE,
534 chromeos::DEVICE_TYPE_USB,
535 kDevice1SizeInBytes);
536 EXPECT_EQ(1, observer().attach_calls());
537 EXPECT_EQ(0, observer().detach_calls());
539 ON_CALL(*disk_mount_manager_mock_, UnmountPath(_, _, _))
540 .WillByDefault(testing::Invoke(&UnmountFake));
541 EXPECT_CALL(*disk_mount_manager_mock_,
542 UnmountPath(observer().last_attached().location(), _, _));
543 monitor_->EjectDevice(observer().last_attached().device_id(),
544 base::Bind(&StorageMonitorCrosTest::EjectNotify,
545 base::Unretained(this)));
546 base::RunLoop().RunUntilIdle();
548 EXPECT_EQ(StorageMonitor::EJECT_OK, status_);
553 } // namespace storage_monitor