disk_unittest.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright 2018 The Chromium 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. #include "ash/components/disks/disk.h"
  5. #include <stdint.h>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "chromeos/ash/components/dbus/cros_disks/cros_disks_client.h"
  10. #include "dbus/message.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "third_party/cros_system_api/dbus/service_constants.h"
  13. namespace ash {
  14. namespace disks {
  15. namespace {
  16. const char kDevicePath[] = "/sys/device/path";
  17. const char kDeviceFile[] = "/dev/sdb1";
  18. const char kMountPath1[] = "/media/removable/UNTITLED";
  19. const char kMountPath2[] = "/media/removable/second_mount_path";
  20. const char kDriveModel[] = "DriveModel";
  21. const char kIdLabel[] = "UNTITLED";
  22. const char kIdUuid[] = "XXXX-YYYY";
  23. const char kStorageDevicePath[] =
  24. "/sys/devices/pci0000:00/0000:00:14.0/usb2/2-8/2-8:1.0/host14/target14:0:0/"
  25. "14:0:0:0";
  26. const char kProductId[] = "1234";
  27. const char kProductName[] = "Product Name";
  28. const char kVendorId[] = "0000";
  29. const char kVendorName[] = "Vendor Name";
  30. const char kFileSystemType[] = "exfat";
  31. const int kBusNumber = 2;
  32. const int kDeviceNumber = 3;
  33. const uint64_t kDeviceSize = 16005464064;
  34. const uint32_t kDeviceMediaType = cros_disks::DEVICE_MEDIA_SD;
  35. // Appends a boolean entry to a dictionary of type "a{sv}"
  36. void AppendBoolDictEntry(dbus::MessageWriter* array_writer,
  37. const std::string& key,
  38. bool value) {
  39. dbus::MessageWriter entry_writer(nullptr);
  40. array_writer->OpenDictEntry(&entry_writer);
  41. entry_writer.AppendString(key);
  42. entry_writer.AppendVariantOfBool(value);
  43. array_writer->CloseContainer(&entry_writer);
  44. }
  45. // Appends a string entry to a dictionary of type "a{sv}"
  46. void AppendStringDictEntry(dbus::MessageWriter* array_writer,
  47. const std::string& key,
  48. const std::string& value) {
  49. dbus::MessageWriter entry_writer(nullptr);
  50. array_writer->OpenDictEntry(&entry_writer);
  51. entry_writer.AppendString(key);
  52. entry_writer.AppendVariantOfString(value);
  53. array_writer->CloseContainer(&entry_writer);
  54. }
  55. // Appends a uint64 entry to a dictionary of type "a{sv}"
  56. void AppendUint64DictEntry(dbus::MessageWriter* array_writer,
  57. const std::string& key,
  58. uint64_t value) {
  59. dbus::MessageWriter entry_writer(nullptr);
  60. array_writer->OpenDictEntry(&entry_writer);
  61. entry_writer.AppendString(key);
  62. entry_writer.AppendVariantOfUint64(value);
  63. array_writer->CloseContainer(&entry_writer);
  64. }
  65. // Appends a uint32 entry to a dictionary of type "a{sv}"
  66. void AppendUint32DictEntry(dbus::MessageWriter* array_writer,
  67. const std::string& key,
  68. uint64_t value) {
  69. dbus::MessageWriter entry_writer(nullptr);
  70. array_writer->OpenDictEntry(&entry_writer);
  71. entry_writer.AppendString(key);
  72. entry_writer.AppendVariantOfUint32(value);
  73. array_writer->CloseContainer(&entry_writer);
  74. }
  75. // Appends a Int32 entry to a dictionary of type "a{sv}"
  76. void AppendInt32DictEntry(dbus::MessageWriter* array_writer,
  77. const std::string& key,
  78. int value) {
  79. dbus::MessageWriter entry_writer(nullptr);
  80. array_writer->OpenDictEntry(&entry_writer);
  81. entry_writer.AppendString(key);
  82. entry_writer.AppendVariantOfInt32(value);
  83. array_writer->CloseContainer(&entry_writer);
  84. }
  85. void AppendBasicProperties(dbus::MessageWriter* array_writer) {
  86. AppendStringDictEntry(array_writer, cros_disks::kDeviceFile, kDeviceFile);
  87. AppendStringDictEntry(array_writer, cros_disks::kDriveModel, kDriveModel);
  88. AppendStringDictEntry(array_writer, cros_disks::kIdLabel, kIdLabel);
  89. AppendStringDictEntry(array_writer, cros_disks::kIdUuid, kIdUuid);
  90. AppendStringDictEntry(array_writer, cros_disks::kStorageDevicePath,
  91. kStorageDevicePath);
  92. AppendStringDictEntry(array_writer, cros_disks::kProductId, kProductId);
  93. AppendStringDictEntry(array_writer, cros_disks::kProductName, kProductName);
  94. AppendStringDictEntry(array_writer, cros_disks::kVendorId, kVendorId);
  95. AppendStringDictEntry(array_writer, cros_disks::kVendorName, kVendorName);
  96. AppendStringDictEntry(array_writer, cros_disks::kFileSystemType,
  97. kFileSystemType);
  98. AppendInt32DictEntry(array_writer, cros_disks::kBusNumber, kBusNumber);
  99. AppendInt32DictEntry(array_writer, cros_disks::kDeviceNumber, kDeviceNumber);
  100. AppendUint64DictEntry(array_writer, cros_disks::kDeviceSize, kDeviceSize);
  101. AppendUint32DictEntry(array_writer, cros_disks::kDeviceMediaType,
  102. kDeviceMediaType);
  103. }
  104. // Builds a dbus reponse with a common set of fields.
  105. std::unique_ptr<dbus::Response> BuildBasicDbusResponse() {
  106. std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  107. dbus::MessageWriter writer(response.get());
  108. dbus::MessageWriter array_writer(nullptr);
  109. writer.OpenArray("{sv}", &array_writer);
  110. AppendBasicProperties(&array_writer);
  111. writer.CloseContainer(&array_writer);
  112. return response;
  113. }
  114. TEST(DiskTest, ConstructFromDiskInfo) {
  115. const char kBaseMountpath[] = "/base/mount/path";
  116. std::unique_ptr<dbus::Response> response = BuildBasicDbusResponse();
  117. DiskInfo disk_info(kDevicePath, response.get());
  118. Disk disk(disk_info, false /* write_disabled_by_policy */, kBaseMountpath);
  119. EXPECT_EQ(kDevicePath, disk.device_path());
  120. EXPECT_EQ(kDeviceFile, disk.file_path());
  121. EXPECT_EQ(kIdLabel, disk.device_label());
  122. EXPECT_EQ(kDriveModel, disk.drive_label());
  123. EXPECT_EQ(kVendorId, disk.vendor_id());
  124. EXPECT_EQ(kVendorName, disk.vendor_name());
  125. EXPECT_EQ(kProductId, disk.product_id());
  126. EXPECT_EQ(kProductName, disk.product_name());
  127. EXPECT_EQ(kIdUuid, disk.fs_uuid());
  128. EXPECT_EQ(kBusNumber, disk.bus_number());
  129. EXPECT_EQ(kDeviceNumber, disk.device_number());
  130. EXPECT_EQ(kDeviceSize, disk.total_size_in_bytes());
  131. EXPECT_EQ(DeviceType::kSD, disk.device_type());
  132. EXPECT_EQ(kStorageDevicePath, disk.storage_device_path());
  133. EXPECT_EQ(kBaseMountpath, disk.base_mount_path());
  134. EXPECT_FALSE(disk.is_parent());
  135. EXPECT_FALSE(disk.is_read_only());
  136. EXPECT_FALSE(disk.is_read_only_hardware());
  137. EXPECT_FALSE(disk.has_media());
  138. EXPECT_FALSE(disk.on_boot_device());
  139. EXPECT_FALSE(disk.on_removable_device());
  140. EXPECT_FALSE(disk.is_mounted());
  141. EXPECT_FALSE(disk.IsStatefulPartition());
  142. EXPECT_FALSE(disk.is_auto_mountable());
  143. EXPECT_TRUE(disk.is_first_mount());
  144. // Drives are hidden by default.
  145. EXPECT_TRUE(disk.is_hidden());
  146. }
  147. std::unique_ptr<Disk> BuildDiskWithProperty(const std::string& property,
  148. bool value) {
  149. std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  150. {
  151. dbus::MessageWriter writer(response.get());
  152. dbus::MessageWriter array_writer(nullptr);
  153. writer.OpenArray("{sv}", &array_writer);
  154. AppendBasicProperties(&array_writer);
  155. AppendBoolDictEntry(&array_writer, property, value);
  156. writer.CloseContainer(&array_writer);
  157. }
  158. DiskInfo disk_info(kDevicePath, response.get());
  159. return std::make_unique<Disk>(disk_info, false, "");
  160. }
  161. TEST(DiskTest, ConstructFromDiskInfo_BoolProperties) {
  162. {
  163. auto disk = BuildDiskWithProperty(cros_disks::kDeviceIsDrive, true);
  164. EXPECT_TRUE(disk->is_parent());
  165. }
  166. {
  167. auto disk = BuildDiskWithProperty(cros_disks::kDeviceIsReadOnly, true);
  168. EXPECT_TRUE(disk->is_read_only());
  169. EXPECT_TRUE(disk->is_read_only_hardware());
  170. }
  171. {
  172. auto disk =
  173. BuildDiskWithProperty(cros_disks::kDeviceIsMediaAvailable, true);
  174. EXPECT_TRUE(disk->has_media());
  175. }
  176. {
  177. auto disk = BuildDiskWithProperty(cros_disks::kDeviceIsOnBootDevice, true);
  178. EXPECT_TRUE(disk->on_boot_device());
  179. }
  180. {
  181. auto disk =
  182. BuildDiskWithProperty(cros_disks::kDeviceIsOnRemovableDevice, true);
  183. EXPECT_TRUE(disk->on_removable_device());
  184. }
  185. {
  186. auto disk = BuildDiskWithProperty(cros_disks::kIsAutoMountable, true);
  187. EXPECT_TRUE(disk->is_auto_mountable());
  188. }
  189. }
  190. TEST(DiskTest, ConstructFromDiskInfo_WriteDisabledByPolicy) {
  191. std::unique_ptr<dbus::Response> response = BuildBasicDbusResponse();
  192. DiskInfo disk_info(kDevicePath, response.get());
  193. Disk disk(disk_info, true /* write_disabled_by_policy */, "");
  194. EXPECT_TRUE(disk.is_read_only());
  195. EXPECT_FALSE(disk.is_read_only_hardware());
  196. }
  197. TEST(DiskTest, ConstructFromDiskInfo_Mounted) {
  198. std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  199. {
  200. dbus::MessageWriter writer(response.get());
  201. dbus::MessageWriter array_writer(nullptr);
  202. writer.OpenArray("{sv}", &array_writer);
  203. AppendBasicProperties(&array_writer);
  204. {
  205. std::vector<std::string> mounted_paths = {kMountPath1, kMountPath2};
  206. dbus::MessageWriter entry_writer(nullptr);
  207. array_writer.OpenDictEntry(&entry_writer);
  208. entry_writer.AppendString(cros_disks::kDeviceMountPaths);
  209. dbus::MessageWriter variant_writer(nullptr);
  210. entry_writer.OpenVariant("as", &variant_writer);
  211. variant_writer.AppendArrayOfStrings(mounted_paths);
  212. entry_writer.CloseContainer(&variant_writer);
  213. array_writer.CloseContainer(&entry_writer);
  214. }
  215. writer.CloseContainer(&array_writer);
  216. }
  217. DiskInfo disk_info(kDevicePath, response.get());
  218. Disk disk(disk_info, false, "");
  219. EXPECT_TRUE(disk.is_mounted());
  220. EXPECT_EQ(kMountPath1, disk.mount_path());
  221. }
  222. TEST(DiskTest, SetMountPath) {
  223. std::unique_ptr<dbus::Response> response = BuildBasicDbusResponse();
  224. DiskInfo disk_info(kDevicePath, response.get());
  225. Disk disk(disk_info, false /* write_disabled_by_policy */, "");
  226. EXPECT_EQ("", disk.mount_path());
  227. EXPECT_EQ("", disk.base_mount_path());
  228. EXPECT_FALSE(disk.is_mounted());
  229. disk.SetMountPath(kMountPath1);
  230. EXPECT_EQ(kMountPath1, disk.mount_path());
  231. EXPECT_EQ(kMountPath1, disk.base_mount_path());
  232. EXPECT_FALSE(disk.is_mounted());
  233. disk.set_mounted(true);
  234. EXPECT_TRUE(disk.is_mounted());
  235. }
  236. } // namespace
  237. } // namespace disks
  238. } // namespace ash