1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // TestVolumeMountWatcherWin implementation.
7 #include "components/storage_monitor/test_volume_mount_watcher_win.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/functional/bind.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/storage_monitor/storage_info.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace storage_monitor {
22 base::FilePath GetTempRoot() {
23 base::ScopedTempDir temp_dir;
24 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
25 base::FilePath temp_root = temp_dir.GetPath();
26 while (temp_root.DirName() != temp_root)
27 temp_root = temp_root.DirName();
31 std::vector<base::FilePath> FakeGetSingleAttachedDevice() {
32 std::vector<base::FilePath> result;
33 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2)); // C
35 // Make sure we are adding the drive on which ScopedTempDir will make
37 base::FilePath temp_root = GetTempRoot();
38 if (temp_root != VolumeMountWatcherWin::DriveNumberToFilePath(2))
39 result.push_back(temp_root);
44 std::vector<base::FilePath> FakeGetAttachedDevices() {
45 std::vector<base::FilePath> result;
46 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(0)); // A
47 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(1)); // B
48 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2)); // C
49 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(3)); // D
50 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(5)); // F
51 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(7)); // H
52 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(13)); // N
53 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(25)); // Z
57 // Gets the details of the mass storage device specified by the |device_path|.
58 // |device_path| inputs of 'A:\' - 'Z:\' are valid. 'N:\' is not removable.
59 // 'C:\' is not removable (so that auto-added paths are correctly handled).
60 bool GetMassStorageDeviceDetails(const base::FilePath& device_path,
64 // Truncate to root path.
65 base::FilePath path(device_path);
66 if (device_path.value().length() > 3)
67 path = base::FilePath(device_path.value().substr(0, 3));
68 base::FilePath::CharType drive_letter = path.value()[0];
69 if (drive_letter < L'A' || drive_letter > L'Z')
72 StorageInfo::Type type = StorageInfo::FIXED_MASS_STORAGE;
73 if (path.value() != L"N:\\" && path.value() != L"C:\\" &&
74 path.value() != GetTempRoot().value()) {
75 type = StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM;
77 std::string unique_id =
78 "\\\\?\\Volume{00000000-0000-0000-0000-000000000000}\\";
79 unique_id[11] = static_cast<char>(drive_letter);
80 std::string device_id = StorageInfo::MakeDeviceId(type, unique_id);
81 std::u16string storage_label = path.Append(L" Drive").LossyDisplayName();
82 *info = StorageInfo(device_id, path.value(), storage_label, std::u16string(),
83 std::u16string(), 1000000);
90 // TestVolumeMountWatcherWin ---------------------------------------------------
92 TestVolumeMountWatcherWin::TestVolumeMountWatcherWin()
93 : attached_devices_fake_(false) {}
95 TestVolumeMountWatcherWin::~TestVolumeMountWatcherWin() {
98 void TestVolumeMountWatcherWin::AddDeviceForTesting(
99 const base::FilePath& device_path,
100 const std::string& device_id,
101 const std::u16string& storage_label,
102 uint64_t total_size_in_bytes) {
103 StorageInfo info(device_id, device_path.value(), storage_label,
104 std::u16string(), std::u16string(), total_size_in_bytes);
105 HandleDeviceAttachEventOnUIThread(device_path, info);
108 void TestVolumeMountWatcherWin::SetAttachedDevicesFake() {
109 attached_devices_fake_ = true;
112 void TestVolumeMountWatcherWin::DeviceCheckComplete(
113 const base::FilePath& device_path) {
114 devices_checked_.push_back(device_path);
115 if (device_check_complete_event_)
116 device_check_complete_event_->Wait();
117 VolumeMountWatcherWin::DeviceCheckComplete(device_path);
120 void TestVolumeMountWatcherWin::BlockDeviceCheckForTesting() {
121 device_check_complete_event_ = std::make_unique<base::WaitableEvent>(
122 base::WaitableEvent::ResetPolicy::AUTOMATIC,
123 base::WaitableEvent::InitialState::NOT_SIGNALED);
124 devices_checked_.clear();
127 void TestVolumeMountWatcherWin::ReleaseDeviceCheck() {
128 device_check_complete_event_->Signal();
132 bool TestVolumeMountWatcherWin::GetDeviceRemovable(
133 const base::FilePath& device_path,
136 bool success = GetMassStorageDeviceDetails(device_path, &info);
137 *removable = StorageInfo::IsRemovableDevice(info.device_id());
141 VolumeMountWatcherWin::GetDeviceDetailsCallbackType
142 TestVolumeMountWatcherWin::GetDeviceDetailsCallback() const {
143 return base::BindOnce(&GetMassStorageDeviceDetails);
146 VolumeMountWatcherWin::GetAttachedDevicesCallbackType
147 TestVolumeMountWatcherWin::GetAttachedDevicesCallback() const {
148 if (attached_devices_fake_)
149 return base::BindOnce(&FakeGetAttachedDevices);
150 return base::BindOnce(&FakeGetSingleAttachedDevice);
153 } // namespace storage_monitor