| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // RemovableDeviceNotificationsLinux unit tests. | 5 // RemovableDeviceNotificationsLinux unit tests. |
| 6 | 6 |
| 7 #include "chrome/browser/system_monitor/removable_device_notifications_linux.h" | 7 #include "chrome/browser/system_monitor/removable_device_notifications_linux.h" |
| 8 | 8 |
| 9 #include <mntent.h> | 9 #include <mntent.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| 11 | 11 |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
| 18 #include "base/scoped_temp_dir.h" | 18 #include "base/scoped_temp_dir.h" |
| 19 #include "base/system_monitor/system_monitor.h" | 19 #include "base/system_monitor/system_monitor.h" |
| 20 #include "base/test/mock_devices_changed_observer.h" | 20 #include "base/test/mock_devices_changed_observer.h" |
| 21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 22 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" |
| 22 #include "chrome/browser/system_monitor/media_storage_util.h" | 23 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 23 #include "chrome/browser/system_monitor/removable_device_constants.h" | 24 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 24 #include "content/public/test/test_browser_thread.h" | 25 #include "content/public/test/test_browser_thread.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 27 |
| 27 namespace chrome { | 28 namespace chrome { |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 using testing::_; | 32 using testing::_; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 45 | 46 |
| 46 const char kMountPointA[] = "mnt_a"; | 47 const char kMountPointA[] = "mnt_a"; |
| 47 const char kMountPointB[] = "mnt_b"; | 48 const char kMountPointB[] = "mnt_b"; |
| 48 const char kMountPointC[] = "mnt_c"; | 49 const char kMountPointC[] = "mnt_c"; |
| 49 | 50 |
| 50 struct TestDeviceData { | 51 struct TestDeviceData { |
| 51 const char* device_path; | 52 const char* device_path; |
| 52 const char* unique_id; | 53 const char* unique_id; |
| 53 const char* device_name; | 54 const char* device_name; |
| 54 MediaStorageUtil::Type type; | 55 MediaStorageUtil::Type type; |
| 56 uint64 partition_size_in_bytes; |
| 55 }; | 57 }; |
| 56 | 58 |
| 57 const TestDeviceData kTestDeviceData[] = { | 59 const TestDeviceData kTestDeviceData[] = { |
| 58 { kDeviceDCIM1, "UUID:FFF0-000F", "TEST_USB_MODEL_1", | 60 { kDeviceDCIM1, "UUID:FFF0-000F", "TEST_USB_MODEL_1", |
| 59 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM}, | 61 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, 88997788 }, |
| 60 { kDeviceDCIM2, "VendorModelSerial:ComName:Model2010:8989", | 62 { kDeviceDCIM2, "VendorModelSerial:ComName:Model2010:8989", |
| 61 "TEST_USB_MODEL_2", MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM}, | 63 "TEST_USB_MODEL_2", MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, |
| 64 8773 }, |
| 62 { kDeviceDCIM3, "VendorModelSerial:::WEM319X792", "TEST_USB_MODEL_3", | 65 { kDeviceDCIM3, "VendorModelSerial:::WEM319X792", "TEST_USB_MODEL_3", |
| 63 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM}, | 66 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, 22377892837 }, |
| 64 { kDeviceNoDCIM, "UUID:ABCD-1234", "TEST_USB_MODEL_4", | 67 { kDeviceNoDCIM, "UUID:ABCD-1234", "TEST_USB_MODEL_4", |
| 65 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM}, | 68 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, 512 }, |
| 66 { kDeviceFixed, "UUID:743A91FD2349", "TEST_USB_MODEL_5", | 69 { kDeviceFixed, "UUID:743A-2349", "743A-2349", |
| 67 MediaStorageUtil::FIXED_MASS_STORAGE}, | 70 MediaStorageUtil::FIXED_MASS_STORAGE, 172892 }, |
| 68 }; | 71 }; |
| 69 | 72 |
| 70 void GetDeviceInfo(const FilePath& device_path, std::string* id, | 73 void GetDeviceInfo(const FilePath& device_path, |
| 71 string16* name, bool* removable) { | 74 std::string* id, |
| 75 string16* name, |
| 76 bool* removable, |
| 77 uint64* partition_size_in_bytes) { |
| 72 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { | 78 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { |
| 73 if (device_path.value() == kTestDeviceData[i].device_path) { | 79 if (device_path.value() == kTestDeviceData[i].device_path) { |
| 74 if (id) | 80 if (id) |
| 75 *id = kTestDeviceData[i].unique_id; | 81 *id = kTestDeviceData[i].unique_id; |
| 76 if (name) | 82 if (name) |
| 77 *name = ASCIIToUTF16(kTestDeviceData[i].device_name); | 83 *name = ASCIIToUTF16(kTestDeviceData[i].device_name); |
| 78 if (removable) { | 84 if (removable) { |
| 79 MediaStorageUtil::Type type = kTestDeviceData[i].type; | 85 MediaStorageUtil::Type type = kTestDeviceData[i].type; |
| 80 *removable = | 86 *removable = |
| 81 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM) || | 87 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM) || |
| 82 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM); | 88 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM); |
| 83 } | 89 } |
| 90 if (partition_size_in_bytes) |
| 91 *partition_size_in_bytes = kTestDeviceData[i].partition_size_in_bytes; |
| 84 return; | 92 return; |
| 85 } | 93 } |
| 86 } | 94 } |
| 87 NOTREACHED(); | 95 NOTREACHED(); |
| 88 } | 96 } |
| 89 | 97 |
| 98 uint64 GetDevicePartitionSize(const std::string& device) { |
| 99 for (size_t i = 0; i < arraysize(kTestDeviceData); ++i) { |
| 100 if (device == kTestDeviceData[i].device_path) |
| 101 return kTestDeviceData[i].partition_size_in_bytes; |
| 102 } |
| 103 return 0; |
| 104 } |
| 105 |
| 90 std::string GetDeviceId(const std::string& device) { | 106 std::string GetDeviceId(const std::string& device) { |
| 91 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { | 107 for (size_t i = 0; i < arraysize(kTestDeviceData); ++i) { |
| 92 if (device == kTestDeviceData[i].device_path) { | 108 if (device == kTestDeviceData[i].device_path) { |
| 93 return MediaStorageUtil::MakeDeviceId(kTestDeviceData[i].type, | 109 return MediaStorageUtil::MakeDeviceId(kTestDeviceData[i].type, |
| 94 kTestDeviceData[i].unique_id); | 110 kTestDeviceData[i].unique_id); |
| 95 } | 111 } |
| 96 } | 112 } |
| 97 if (device == kInvalidDevice) { | 113 if (device == kInvalidDevice) { |
| 98 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::FIXED_MASS_STORAGE, | 114 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::FIXED_MASS_STORAGE, |
| 99 kInvalidDevice); | 115 kInvalidDevice); |
| 100 } | 116 } |
| 101 return std::string(); | 117 return std::string(); |
| 102 } | 118 } |
| 119 |
| 120 string16 GetDeviceNameWithSizeDetails(const std::string& device) { |
| 121 for (size_t i = 0; i < arraysize(kTestDeviceData); ++i) { |
| 122 if (device == kTestDeviceData[i].device_path) { |
| 123 return GetDisplayNameForDevice( |
| 124 kTestDeviceData[i].partition_size_in_bytes, |
| 125 ASCIIToUTF16(kTestDeviceData[i].device_name)); |
| 126 } |
| 127 } |
| 128 return string16(); |
| 129 } |
| 130 |
| 103 string16 GetDeviceName(const std::string& device) { | 131 string16 GetDeviceName(const std::string& device) { |
| 104 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { | 132 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { |
| 105 if (device == kTestDeviceData[i].device_path) | 133 if (device == kTestDeviceData[i].device_path) |
| 106 return ASCIIToUTF16(kTestDeviceData[i].device_name); | 134 return ASCIIToUTF16(kTestDeviceData[i].device_name); |
| 107 } | 135 } |
| 108 return string16(); | 136 return string16(); |
| 109 } | 137 } |
| 110 | 138 |
| 111 class RemovableDeviceNotificationsLinuxTestWrapper | 139 class RemovableDeviceNotificationsLinuxTestWrapper |
| 112 : public RemovableDeviceNotificationsLinux { | 140 : public RemovableDeviceNotificationsLinux { |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 testing::Sequence mock_sequence; | 337 testing::Sequence mock_sequence; |
| 310 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); | 338 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); |
| 311 ASSERT_FALSE(test_path.empty()); | 339 ASSERT_FALSE(test_path.empty()); |
| 312 MtabTestData test_data[] = { | 340 MtabTestData test_data[] = { |
| 313 MtabTestData(kDeviceDCIM2, test_path.value(), kValidFS), | 341 MtabTestData(kDeviceDCIM2, test_path.value(), kValidFS), |
| 314 MtabTestData(kDeviceFixed, kInvalidPath, kValidFS), | 342 MtabTestData(kDeviceFixed, kInvalidPath, kValidFS), |
| 315 }; | 343 }; |
| 316 // Only |kDeviceDCIM2| should be attached, since |kDeviceFixed| has a bad | 344 // Only |kDeviceDCIM2| should be attached, since |kDeviceFixed| has a bad |
| 317 // path. | 345 // path. |
| 318 EXPECT_CALL(observer(), | 346 EXPECT_CALL(observer(), |
| 319 OnRemovableStorageAttached(GetDeviceId(kDeviceDCIM2), | 347 OnRemovableStorageAttached( |
| 320 GetDeviceName(kDeviceDCIM2), | 348 GetDeviceId(kDeviceDCIM2), |
| 321 test_path.value())) | 349 GetDeviceNameWithSizeDetails(kDeviceDCIM2), |
| 350 test_path.value())) |
| 322 .InSequence(mock_sequence); | 351 .InSequence(mock_sequence); |
| 323 AppendToMtabAndRunLoop(test_data, arraysize(test_data)); | 352 AppendToMtabAndRunLoop(test_data, arraysize(test_data)); |
| 324 | 353 |
| 325 // |kDeviceDCIM2| should be detached here. | 354 // |kDeviceDCIM2| should be detached here. |
| 326 EXPECT_CALL(observer(), OnRemovableStorageDetached(GetDeviceId(kDeviceDCIM2))) | 355 EXPECT_CALL(observer(), OnRemovableStorageDetached(GetDeviceId(kDeviceDCIM2))) |
| 327 .InSequence(mock_sequence); | 356 .InSequence(mock_sequence); |
| 328 WriteEmptyMtabAndRunLoop(); | 357 WriteEmptyMtabAndRunLoop(); |
| 329 } | 358 } |
| 330 | 359 |
| 331 // Only removable devices are recognized. | 360 // Only removable devices are recognized. |
| 332 TEST_F(RemovableDeviceNotificationLinuxTest, Removable) { | 361 TEST_F(RemovableDeviceNotificationLinuxTest, Removable) { |
| 333 testing::Sequence mock_sequence; | 362 testing::Sequence mock_sequence; |
| 334 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); | 363 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
| 335 ASSERT_FALSE(test_path_a.empty()); | 364 ASSERT_FALSE(test_path_a.empty()); |
| 336 MtabTestData test_data1[] = { | 365 MtabTestData test_data1[] = { |
| 337 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS), | 366 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS), |
| 338 }; | 367 }; |
| 339 // |kDeviceDCIM1| should be attached as expected. | 368 // |kDeviceDCIM1| should be attached as expected. |
| 340 EXPECT_CALL(observer(), | 369 EXPECT_CALL(observer(), |
| 341 OnRemovableStorageAttached(GetDeviceId(kDeviceDCIM1), | 370 OnRemovableStorageAttached( |
| 342 GetDeviceName(kDeviceDCIM1), | 371 GetDeviceId(kDeviceDCIM1), |
| 343 test_path_a.value())) | 372 GetDeviceNameWithSizeDetails(kDeviceDCIM1), |
| 373 test_path_a.value())) |
| 344 .InSequence(mock_sequence); | 374 .InSequence(mock_sequence); |
| 345 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); | 375 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
| 346 | 376 |
| 347 // This should do nothing, since |kDeviceFixed| is not removable. | 377 // This should do nothing, since |kDeviceFixed| is not removable. |
| 348 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); | 378 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); |
| 349 ASSERT_FALSE(test_path_b.empty()); | 379 ASSERT_FALSE(test_path_b.empty()); |
| 350 MtabTestData test_data2[] = { | 380 MtabTestData test_data2[] = { |
| 351 MtabTestData(kDeviceFixed, test_path_b.value(), kValidFS), | 381 MtabTestData(kDeviceFixed, test_path_b.value(), kValidFS), |
| 352 }; | 382 }; |
| 353 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); | 383 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
| 354 | 384 |
| 355 // |kDeviceDCIM1| should be detached as expected. | 385 // |kDeviceDCIM1| should be detached as expected. |
| 356 EXPECT_CALL(observer(), OnRemovableStorageDetached(GetDeviceId(kDeviceDCIM1))) | 386 EXPECT_CALL(observer(), OnRemovableStorageDetached(GetDeviceId(kDeviceDCIM1))) |
| 357 .InSequence(mock_sequence); | 387 .InSequence(mock_sequence); |
| 358 WriteEmptyMtabAndRunLoop(); | 388 WriteEmptyMtabAndRunLoop(); |
| 359 | 389 |
| 360 // |kDeviceNoDCIM| should be attached as expected. | 390 // |kDeviceNoDCIM| should be attached as expected. |
| 361 EXPECT_CALL(observer(), | 391 EXPECT_CALL(observer(), |
| 362 OnRemovableStorageAttached(GetDeviceId(kDeviceNoDCIM), | 392 OnRemovableStorageAttached( |
| 363 GetDeviceName(kDeviceNoDCIM), | 393 GetDeviceId(kDeviceNoDCIM), |
| 364 test_path_b.value())) | 394 GetDeviceNameWithSizeDetails(kDeviceNoDCIM), |
| 395 test_path_b.value())) |
| 365 .InSequence(mock_sequence); | 396 .InSequence(mock_sequence); |
| 366 MtabTestData test_data3[] = { | 397 MtabTestData test_data3[] = { |
| 367 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS), | 398 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS), |
| 368 }; | 399 }; |
| 369 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3)); | 400 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3)); |
| 370 | 401 |
| 371 // |kDeviceNoDCIM| should be detached as expected. | 402 // |kDeviceNoDCIM| should be detached as expected. |
| 372 EXPECT_CALL(observer(), | 403 EXPECT_CALL(observer(), |
| 373 OnRemovableStorageDetached(GetDeviceId(kDeviceNoDCIM))) | 404 OnRemovableStorageDetached(GetDeviceId(kDeviceNoDCIM))) |
| 374 .InSequence(mock_sequence); | 405 .InSequence(mock_sequence); |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_a, &device_info)); | 658 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_a, &device_info)); |
| 628 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id); | 659 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id); |
| 629 | 660 |
| 630 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_b, &device_info)); | 661 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_b, &device_info)); |
| 631 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id); | 662 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id); |
| 632 | 663 |
| 633 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_c, &device_info)); | 664 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_c, &device_info)); |
| 634 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id); | 665 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id); |
| 635 } | 666 } |
| 636 | 667 |
| 668 TEST_F(RemovableDeviceNotificationLinuxTest, DevicePartitionSize) { |
| 669 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
| 670 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); |
| 671 ASSERT_FALSE(test_path_a.empty()); |
| 672 ASSERT_FALSE(test_path_b.empty()); |
| 673 |
| 674 MtabTestData test_data1[] = { |
| 675 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS), |
| 676 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS), |
| 677 MtabTestData(kDeviceFixed, kInvalidPath, kInvalidFS), |
| 678 }; |
| 679 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(2); |
| 680 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0); |
| 681 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
| 682 |
| 683 EXPECT_EQ(GetDevicePartitionSize(kDeviceDCIM1), |
| 684 notifier()->GetStorageSize(test_path_a.value())); |
| 685 EXPECT_EQ(GetDevicePartitionSize(kDeviceNoDCIM), |
| 686 notifier()->GetStorageSize(test_path_b.value())); |
| 687 EXPECT_EQ(GetDevicePartitionSize(kInvalidPath), |
| 688 notifier()->GetStorageSize(kInvalidPath)); |
| 689 } |
| 690 |
| 637 } // namespace | 691 } // namespace |
| 638 | 692 |
| 639 } // namespace chrome | 693 } // namespace chrome |
| OLD | NEW |