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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/disks/disk.h"
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>
#include "chromeos/ash/components/dbus/cros_disks/cros_disks_client.h"
#include "dbus/message.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace ash {
namespace disks {
namespace {
const char kDevicePath[] = "/sys/device/path";
const char kDeviceFile[] = "/dev/sdb1";
const char kMountPath1[] = "/media/removable/UNTITLED";
const char kMountPath2[] = "/media/removable/second_mount_path";
const char kDriveModel[] = "DriveModel";
const char kIdLabel[] = "UNTITLED";
const char kIdUuid[] = "XXXX-YYYY";
const char kStorageDevicePath[] =
"/sys/devices/pci0000:00/0000:00:14.0/usb2/2-8/2-8:1.0/host14/target14:0:0/"
"14:0:0:0";
const char kProductId[] = "1234";
const char kProductName[] = "Product Name";
const char kVendorId[] = "0000";
const char kVendorName[] = "Vendor Name";
const char kFileSystemType[] = "exfat";
const int kBusNumber = 2;
const int kDeviceNumber = 3;
const uint64_t kDeviceSize = 16005464064;
const uint32_t kDeviceMediaType = static_cast<uint32_t>(ash::DeviceType::kSD);
// Appends a boolean entry to a dictionary of type "a{sv}"
void AppendBoolDictEntry(dbus::MessageWriter* array_writer,
const std::string& key,
bool value) {
dbus::MessageWriter entry_writer(nullptr);
array_writer->OpenDictEntry(&entry_writer);
entry_writer.AppendString(key);
entry_writer.AppendVariantOfBool(value);
array_writer->CloseContainer(&entry_writer);
}
// Appends a string entry to a dictionary of type "a{sv}"
void AppendStringDictEntry(dbus::MessageWriter* array_writer,
const std::string& key,
const std::string& value) {
dbus::MessageWriter entry_writer(nullptr);
array_writer->OpenDictEntry(&entry_writer);
entry_writer.AppendString(key);
entry_writer.AppendVariantOfString(value);
array_writer->CloseContainer(&entry_writer);
}
// Appends a uint64 entry to a dictionary of type "a{sv}"
void AppendUint64DictEntry(dbus::MessageWriter* array_writer,
const std::string& key,
uint64_t value) {
dbus::MessageWriter entry_writer(nullptr);
array_writer->OpenDictEntry(&entry_writer);
entry_writer.AppendString(key);
entry_writer.AppendVariantOfUint64(value);
array_writer->CloseContainer(&entry_writer);
}
// Appends a uint32 entry to a dictionary of type "a{sv}"
void AppendUint32DictEntry(dbus::MessageWriter* array_writer,
const std::string& key,
uint64_t value) {
dbus::MessageWriter entry_writer(nullptr);
array_writer->OpenDictEntry(&entry_writer);
entry_writer.AppendString(key);
entry_writer.AppendVariantOfUint32(value);
array_writer->CloseContainer(&entry_writer);
}
// Appends a Int32 entry to a dictionary of type "a{sv}"
void AppendInt32DictEntry(dbus::MessageWriter* array_writer,
const std::string& key,
int value) {
dbus::MessageWriter entry_writer(nullptr);
array_writer->OpenDictEntry(&entry_writer);
entry_writer.AppendString(key);
entry_writer.AppendVariantOfInt32(value);
array_writer->CloseContainer(&entry_writer);
}
void AppendBasicProperties(dbus::MessageWriter* array_writer) {
AppendStringDictEntry(array_writer, cros_disks::kDeviceFile, kDeviceFile);
AppendStringDictEntry(array_writer, cros_disks::kDriveModel, kDriveModel);
AppendStringDictEntry(array_writer, cros_disks::kIdLabel, kIdLabel);
AppendStringDictEntry(array_writer, cros_disks::kIdUuid, kIdUuid);
AppendStringDictEntry(array_writer, cros_disks::kStorageDevicePath,
kStorageDevicePath);
AppendStringDictEntry(array_writer, cros_disks::kProductId, kProductId);
AppendStringDictEntry(array_writer, cros_disks::kProductName, kProductName);
AppendStringDictEntry(array_writer, cros_disks::kVendorId, kVendorId);
AppendStringDictEntry(array_writer, cros_disks::kVendorName, kVendorName);
AppendStringDictEntry(array_writer, cros_disks::kFileSystemType,
kFileSystemType);
AppendInt32DictEntry(array_writer, cros_disks::kBusNumber, kBusNumber);
AppendInt32DictEntry(array_writer, cros_disks::kDeviceNumber, kDeviceNumber);
AppendUint64DictEntry(array_writer, cros_disks::kDeviceSize, kDeviceSize);
AppendUint32DictEntry(array_writer, cros_disks::kDeviceMediaType,
kDeviceMediaType);
}
// Builds a dbus reponse with a common set of fields.
std::unique_ptr<dbus::Response> BuildBasicDbusResponse() {
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
dbus::MessageWriter writer(response.get());
dbus::MessageWriter array_writer(nullptr);
writer.OpenArray("{sv}", &array_writer);
AppendBasicProperties(&array_writer);
writer.CloseContainer(&array_writer);
return response;
}
TEST(DiskTest, ConstructFromDiskInfo) {
const char kBaseMountpath[] = "/base/mount/path";
std::unique_ptr<dbus::Response> response = BuildBasicDbusResponse();
DiskInfo disk_info(kDevicePath, response.get());
Disk disk(disk_info, false /* write_disabled_by_policy */, kBaseMountpath);
EXPECT_EQ(kDevicePath, disk.device_path());
EXPECT_EQ(kDeviceFile, disk.file_path());
EXPECT_EQ(kIdLabel, disk.device_label());
EXPECT_EQ(kDriveModel, disk.drive_label());
EXPECT_EQ(kVendorId, disk.vendor_id());
EXPECT_EQ(kVendorName, disk.vendor_name());
EXPECT_EQ(kProductId, disk.product_id());
EXPECT_EQ(kProductName, disk.product_name());
EXPECT_EQ(kIdUuid, disk.fs_uuid());
EXPECT_EQ(kBusNumber, disk.bus_number());
EXPECT_EQ(kDeviceNumber, disk.device_number());
EXPECT_EQ(kDeviceSize, disk.total_size_in_bytes());
EXPECT_EQ(DeviceType::kSD, disk.device_type());
EXPECT_EQ(kStorageDevicePath, disk.storage_device_path());
EXPECT_EQ(kBaseMountpath, disk.base_mount_path());
EXPECT_FALSE(disk.is_parent());
EXPECT_FALSE(disk.is_read_only());
EXPECT_FALSE(disk.is_read_only_hardware());
EXPECT_FALSE(disk.has_media());
EXPECT_FALSE(disk.on_boot_device());
EXPECT_FALSE(disk.on_removable_device());
EXPECT_FALSE(disk.is_mounted());
EXPECT_FALSE(disk.IsStatefulPartition());
EXPECT_FALSE(disk.is_auto_mountable());
EXPECT_TRUE(disk.is_first_mount());
// Drives are hidden by default.
EXPECT_TRUE(disk.is_hidden());
}
std::unique_ptr<Disk> BuildDiskWithProperty(const std::string& property,
bool value) {
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
{
dbus::MessageWriter writer(response.get());
dbus::MessageWriter array_writer(nullptr);
writer.OpenArray("{sv}", &array_writer);
AppendBasicProperties(&array_writer);
AppendBoolDictEntry(&array_writer, property, value);
writer.CloseContainer(&array_writer);
}
DiskInfo disk_info(kDevicePath, response.get());
return std::make_unique<Disk>(disk_info, false, "");
}
TEST(DiskTest, ConstructFromDiskInfo_BoolProperties) {
{
auto disk = BuildDiskWithProperty(cros_disks::kDeviceIsDrive, true);
EXPECT_TRUE(disk->is_parent());
}
{
auto disk = BuildDiskWithProperty(cros_disks::kDeviceIsReadOnly, true);
EXPECT_TRUE(disk->is_read_only());
EXPECT_TRUE(disk->is_read_only_hardware());
}
{
auto disk =
BuildDiskWithProperty(cros_disks::kDeviceIsMediaAvailable, true);
EXPECT_TRUE(disk->has_media());
}
{
auto disk = BuildDiskWithProperty(cros_disks::kDeviceIsOnBootDevice, true);
EXPECT_TRUE(disk->on_boot_device());
}
{
auto disk =
BuildDiskWithProperty(cros_disks::kDeviceIsOnRemovableDevice, true);
EXPECT_TRUE(disk->on_removable_device());
}
{
auto disk = BuildDiskWithProperty(cros_disks::kIsAutoMountable, true);
EXPECT_TRUE(disk->is_auto_mountable());
}
}
TEST(DiskTest, ConstructFromDiskInfo_WriteDisabledByPolicy) {
std::unique_ptr<dbus::Response> response = BuildBasicDbusResponse();
DiskInfo disk_info(kDevicePath, response.get());
Disk disk(disk_info, true /* write_disabled_by_policy */, "");
EXPECT_TRUE(disk.is_read_only());
EXPECT_FALSE(disk.is_read_only_hardware());
}
TEST(DiskTest, ConstructFromDiskInfo_Mounted) {
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
{
dbus::MessageWriter writer(response.get());
dbus::MessageWriter array_writer(nullptr);
writer.OpenArray("{sv}", &array_writer);
AppendBasicProperties(&array_writer);
{
std::vector<std::string> mounted_paths = {kMountPath1, kMountPath2};
dbus::MessageWriter entry_writer(nullptr);
array_writer.OpenDictEntry(&entry_writer);
entry_writer.AppendString(cros_disks::kDeviceMountPaths);
dbus::MessageWriter variant_writer(nullptr);
entry_writer.OpenVariant("as", &variant_writer);
variant_writer.AppendArrayOfStrings(mounted_paths);
entry_writer.CloseContainer(&variant_writer);
array_writer.CloseContainer(&entry_writer);
}
writer.CloseContainer(&array_writer);
}
DiskInfo disk_info(kDevicePath, response.get());
Disk disk(disk_info, false, "");
EXPECT_TRUE(disk.is_mounted());
EXPECT_EQ(kMountPath1, disk.mount_path());
}
TEST(DiskTest, SetMountPath) {
std::unique_ptr<dbus::Response> response = BuildBasicDbusResponse();
DiskInfo disk_info(kDevicePath, response.get());
Disk disk(disk_info, false /* write_disabled_by_policy */, "");
EXPECT_EQ("", disk.mount_path());
EXPECT_EQ("", disk.base_mount_path());
EXPECT_FALSE(disk.is_mounted());
disk.SetMountPath(kMountPath1);
EXPECT_EQ(kMountPath1, disk.mount_path());
EXPECT_EQ(kMountPath1, disk.base_mount_path());
EXPECT_FALSE(disk.is_mounted());
disk.set_mounted(true);
EXPECT_TRUE(disk.is_mounted());
}
} // namespace
} // namespace disks
} // namespace ash
|