Home | History | Annotate | Download | only in dbus
      1 // Copyright 2013 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 
      5 #include "chromeos/dbus/cros_disks_client.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "dbus/message.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 #include "third_party/cros_system_api/dbus/service_constants.h"
     11 
     12 namespace chromeos {
     13 
     14 namespace {
     15 
     16 // Appends a boolean entry to a dictionary of type "a{sv}"
     17 void AppendBoolDictEntry(dbus::MessageWriter* array_writer,
     18                          const std::string& key,
     19                          bool value) {
     20   dbus::MessageWriter entry_writer(NULL);
     21   array_writer->OpenDictEntry(&entry_writer);
     22   entry_writer.AppendString(key);
     23   entry_writer.AppendVariantOfBool(value);
     24   array_writer->CloseContainer(&entry_writer);
     25 }
     26 
     27 // Appends a string entry to a dictionary of type "a{sv}"
     28 void AppendStringDictEntry(dbus::MessageWriter* array_writer,
     29                            const std::string& key,
     30                            const std::string& value) {
     31   dbus::MessageWriter entry_writer(NULL);
     32   array_writer->OpenDictEntry(&entry_writer);
     33   entry_writer.AppendString(key);
     34   entry_writer.AppendVariantOfString(value);
     35   array_writer->CloseContainer(&entry_writer);
     36 }
     37 
     38 }  // namespace
     39 
     40 TEST(CrosDisksClientTest, DiskInfo) {
     41   const std::string kDeviceFile = "/dev/sdb1";
     42   const bool kDeviceIsDrive = true;
     43   const bool kDeviceIsMediaAvailable = true;
     44   const bool kDeviceIsMounted = true;
     45   const bool kDeviceIsOnBootDevice = true;
     46   const bool kDeviceIsOnRemovableDevice = true;
     47   const bool kDeviceIsReadOnly = true;
     48   const uint32 kDeviceMediaType = cros_disks::DEVICE_MEDIA_SD;
     49   const std::string kMountPath = "/media/removable/UNTITLED";
     50   const bool kDevicePresentationHide = false;
     51   const uint64 kDeviceSize = 16005464064;
     52   const std::string kDriveModel = "DriveModel";
     53   const std::string kIdLabel = "UNTITLED";
     54   const std::string kIdUuid = "XXXX-YYYY";
     55   const std::string kNativePath = "/sys/devices/.../sdb/sdb1";
     56   const std::string kProductId = "1234";
     57   const std::string kProductName = "Product Name";
     58   const std::string kVendorId = "0000";
     59   const std::string kVendorName = "Vendor Name";
     60 
     61   // Construct a fake response of GetDeviceProperties().
     62   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
     63   {
     64     dbus::MessageWriter writer(response.get());
     65     dbus::MessageWriter array_writer(NULL);
     66     writer.OpenArray("{sv}", &array_writer);
     67 
     68     AppendStringDictEntry(&array_writer, cros_disks::kDeviceFile, kDeviceFile);
     69     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsDrive,
     70                         kDeviceIsDrive);
     71     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsMediaAvailable,
     72                         kDeviceIsMediaAvailable);
     73     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsMounted,
     74                         kDeviceIsMounted);
     75     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsOnBootDevice,
     76                         kDeviceIsOnBootDevice);
     77     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsOnRemovableDevice,
     78                         kDeviceIsOnRemovableDevice);
     79     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsReadOnly,
     80                         kDeviceIsReadOnly);
     81     {
     82       dbus::MessageWriter entry_writer(NULL);
     83       array_writer.OpenDictEntry(&entry_writer);
     84       entry_writer.AppendString(cros_disks::kDeviceMediaType);
     85       entry_writer.AppendVariantOfUint32(kDeviceMediaType);
     86       array_writer.CloseContainer(&entry_writer);
     87     }
     88     {
     89       std::vector<std::string> mounted_paths;
     90       mounted_paths.push_back(kMountPath);
     91 
     92       dbus::MessageWriter entry_writer(NULL);
     93       array_writer.OpenDictEntry(&entry_writer);
     94       entry_writer.AppendString(cros_disks::kDeviceMountPaths);
     95       dbus::MessageWriter variant_writer(NULL);
     96       entry_writer.OpenVariant("as", &variant_writer);
     97       variant_writer.AppendArrayOfStrings(mounted_paths);
     98       entry_writer.CloseContainer(&variant_writer);
     99       array_writer.CloseContainer(&entry_writer);
    100     }
    101     AppendBoolDictEntry(&array_writer, cros_disks::kDevicePresentationHide,
    102                         kDevicePresentationHide);
    103     {
    104       dbus::MessageWriter entry_writer(NULL);
    105       array_writer.OpenDictEntry(&entry_writer);
    106       entry_writer.AppendString(cros_disks::kDeviceSize);
    107       entry_writer.AppendVariantOfUint64(kDeviceSize);
    108       array_writer.CloseContainer(&entry_writer);
    109     }
    110     AppendStringDictEntry(&array_writer, cros_disks::kDriveModel, kDriveModel);
    111     AppendStringDictEntry(&array_writer, cros_disks::kIdLabel, kIdLabel);
    112     AppendStringDictEntry(&array_writer, cros_disks::kIdUuid, kIdUuid);
    113     AppendStringDictEntry(&array_writer, cros_disks::kNativePath, kNativePath);
    114     AppendStringDictEntry(&array_writer, cros_disks::kProductId, kProductId);
    115     AppendStringDictEntry(&array_writer, cros_disks::kProductName,
    116                           kProductName);
    117     AppendStringDictEntry(&array_writer, cros_disks::kVendorId, kVendorId);
    118     AppendStringDictEntry(&array_writer, cros_disks::kVendorName, kVendorName);
    119 
    120     writer.CloseContainer(&array_writer);
    121   }
    122 
    123   // Construct DiskInfo.
    124   DiskInfo result(kDeviceFile, response.get());
    125   EXPECT_EQ(kDeviceFile, result.device_path());
    126   EXPECT_EQ(kDeviceIsDrive, result.is_drive());
    127   EXPECT_EQ(kDeviceIsReadOnly, result.is_read_only());
    128   // Use EXPECT_TRUE(kDevicePresentationHide == result.is_hidden()) instead of
    129   // EXPECT_EQ(kDevicePresentationHide, result.is_hidden()) as gcc 4.7 issues
    130   // the following warning on EXPECT_EQ(false, x), which is turned into an error
    131   // with -Werror=conversion-null:
    132   //
    133   //   converting 'false' to pointer type for argument 1 of
    134   //   'char testing::internal::IsNullLiteralHelper(testing::internal::Secret*)'
    135   EXPECT_TRUE(kDevicePresentationHide == result.is_hidden());
    136   EXPECT_EQ(kDeviceIsMediaAvailable, result.has_media());
    137   EXPECT_EQ(kDeviceIsOnBootDevice, result.on_boot_device());
    138   EXPECT_EQ(kDeviceIsOnRemovableDevice, result.on_removable_device());
    139   EXPECT_EQ(kNativePath, result.system_path());
    140   EXPECT_EQ(kDeviceFile, result.file_path());
    141   EXPECT_EQ(kVendorId, result.vendor_id());
    142   EXPECT_EQ(kVendorName, result.vendor_name());
    143   EXPECT_EQ(kProductId, result.product_id());
    144   EXPECT_EQ(kProductName, result.product_name());
    145   EXPECT_EQ(kDriveModel, result.drive_label());
    146   EXPECT_EQ(kIdLabel, result.label());
    147   EXPECT_EQ(kIdUuid, result.uuid());
    148   EXPECT_EQ(kDeviceSize, result.total_size_in_bytes());
    149   EXPECT_EQ(DEVICE_TYPE_SD, result.device_type());
    150   EXPECT_EQ(kMountPath, result.mount_path());
    151 }
    152 
    153 }  // namespace chromeos
    154