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