Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS 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 "cros-disks/disk_monitor.h" |
| 6 | |
| 7 | #include <base/files/file_path.h> |
| 8 | #include <base/logging.h> |
| 9 | #include <base/stl_util.h> |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | #include "cros-disks/device_ejector.h" |
| 14 | |
| 15 | namespace cros_disks { |
| 16 | |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 17 | class DiskMonitorTest : public ::testing::Test { |
| 18 | public: |
Ben Chan | fc77d71 | 2019-06-20 12:45:56 -0700 | [diff] [blame] | 19 | DiskMonitorTest() = default; |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 20 | |
| 21 | protected: |
| 22 | DiskMonitor monitor_; |
| 23 | }; |
| 24 | |
| 25 | TEST_F(DiskMonitorTest, EnumerateDisks) { |
| 26 | std::vector<Disk> disks = monitor_.EnumerateDisks(); |
| 27 | } |
| 28 | |
Anand K Mistry | 1044d3d | 2019-09-17 10:19:28 +1000 | [diff] [blame] | 29 | // TODO(crbug.com/1002570): Disabled due to flakiness. This test relies on the |
| 30 | // disk enumeration being stable for the life of the tests. However, if another |
| 31 | // process on the system is modifying disks (i.e. adding/removing loopback |
| 32 | // devices), this test might fail because a device found by EnumerateDisks() may |
| 33 | // no longer exist when GetDiskByDevicePath() is called. |
| 34 | TEST_F(DiskMonitorTest, DISABLED_GetDiskByDevicePath) { |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 35 | std::vector<Disk> disks = monitor_.EnumerateDisks(); |
| 36 | if (disks.empty()) { |
| 37 | LOG(WARNING) << "No disks found to test."; |
| 38 | } |
| 39 | |
| 40 | for (const auto& found_disk : disks) { |
| 41 | std::string device_path = found_disk.device_file; |
| 42 | LOG(INFO) << "Using device_path: " << device_path << "\n"; |
| 43 | |
| 44 | Disk disk; |
| 45 | EXPECT_TRUE( |
| 46 | monitor_.GetDiskByDevicePath(base::FilePath(device_path), &disk)); |
| 47 | EXPECT_EQ(device_path, disk.device_file); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | TEST_F(DiskMonitorTest, GetDiskByNonexistentDevicePath) { |
| 52 | Disk disk; |
| 53 | base::FilePath device_path("/dev/nonexistent-path"); |
| 54 | EXPECT_FALSE(monitor_.GetDiskByDevicePath(device_path, &disk)); |
| 55 | } |
| 56 | |
| 57 | } // namespace cros_disks |