blob: 5b9cfa844341998aa39f790534a28aef8e5b5e60 [file] [log] [blame]
Sergei Datsenko16821892019-04-05 11:26:38 +11001// 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
15namespace cros_disks {
16
Sergei Datsenko16821892019-04-05 11:26:38 +110017class DiskMonitorTest : public ::testing::Test {
18 public:
Ben Chanfc77d712019-06-20 12:45:56 -070019 DiskMonitorTest() = default;
Sergei Datsenko16821892019-04-05 11:26:38 +110020
21 protected:
22 DiskMonitor monitor_;
23};
24
25TEST_F(DiskMonitorTest, EnumerateDisks) {
26 std::vector<Disk> disks = monitor_.EnumerateDisks();
27}
28
Anand K Mistry1044d3d2019-09-17 10:19:28 +100029// 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.
34TEST_F(DiskMonitorTest, DISABLED_GetDiskByDevicePath) {
Sergei Datsenko16821892019-04-05 11:26:38 +110035 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
51TEST_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