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 kDeviceIsReadOnly = true;
     47   const uint32 kDeviceMediaType = cros_disks::DEVICE_MEDIA_SD;
     48   const std::string kMountPath = "/media/removable/UNTITLED";
     49   const bool kDevicePresentationHide = false;
     50   const uint64 kDeviceSize = 16005464064;
     51   const std::string kDriveModel = "DriveModel";
     52   const std::string kIdLabel = "UNTITLED";
     53   const std::string kIdUuid = "XXXX-YYYY";
     54   const std::string kNativePath = "/sys/devices/.../sdb/sdb1";
     55   const std::string kProductId = "1234";
     56   const std::string kProductName = "Product Name";
     57   const std::string kVendorId = "0000";
     58   const std::string kVendorName = "Vendor Name";
     59 
     60   // Construct a fake response of GetDeviceProperties().
     61   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
     62   {
     63     dbus::MessageWriter writer(response.get());
     64     dbus::MessageWriter array_writer(NULL);
     65     writer.OpenArray("{sv}", &array_writer);
     66 
     67     AppendStringDictEntry(&array_writer, cros_disks::kDeviceFile, kDeviceFile);
     68     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsDrive,
     69                         kDeviceIsDrive);
     70     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsMediaAvailable,
     71                         kDeviceIsMediaAvailable);
     72     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsMounted,
     73                         kDeviceIsMounted);
     74     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsOnBootDevice,
     75                         kDeviceIsOnBootDevice);
     76     AppendBoolDictEntry(&array_writer, cros_disks::kDeviceIsReadOnly,
     77                         kDeviceIsReadOnly);
     78     {
     79       dbus::MessageWriter entry_writer(NULL);
     80       array_writer.OpenDictEntry(&entry_writer);
     81       entry_writer.AppendString(cros_disks::kDeviceMediaType);
     82       entry_writer.AppendVariantOfUint32(kDeviceMediaType);
     83       array_writer.CloseContainer(&entry_writer);
     84     }
     85     {
     86       std::vector<std::string> mounted_paths;
     87       mounted_paths.push_back(kMountPath);
     88 
     89       dbus::MessageWriter entry_writer(NULL);
     90       array_writer.OpenDictEntry(&entry_writer);
     91       entry_writer.AppendString(cros_disks::kDeviceMountPaths);
     92       dbus::MessageWriter variant_writer(NULL);
     93       entry_writer.OpenVariant("as", &variant_writer);
     94       variant_writer.AppendArrayOfStrings(mounted_paths);
     95       entry_writer.CloseContainer(&variant_writer);
     96       array_writer.CloseContainer(&entry_writer);
     97     }
     98     AppendBoolDictEntry(&array_writer, cros_disks::kDevicePresentationHide,
     99                         kDevicePresentationHide);
    100     {
    101       dbus::MessageWriter entry_writer(NULL);
    102       array_writer.OpenDictEntry(&entry_writer);
    103       entry_writer.AppendString(cros_disks::kDeviceSize);
    104       entry_writer.AppendVariantOfUint64(kDeviceSize);
    105       array_writer.CloseContainer(&entry_writer);
    106     }
    107     AppendStringDictEntry(&array_writer, cros_disks::kDriveModel, kDriveModel);
    108     AppendStringDictEntry(&array_writer, cros_disks::kIdLabel, kIdLabel);
    109     AppendStringDictEntry(&array_writer, cros_disks::kIdUuid, kIdUuid);
    110     AppendStringDictEntry(&array_writer, cros_disks::kNativePath, kNativePath);
    111     AppendStringDictEntry(&array_writer, cros_disks::kProductId, kProductId);
    112     AppendStringDictEntry(&array_writer, cros_disks::kProductName,
    113                           kProductName);
    114     AppendStringDictEntry(&array_writer, cros_disks::kVendorId, kVendorId);
    115     AppendStringDictEntry(&array_writer, cros_disks::kVendorName, kVendorName);
    116 
    117     writer.CloseContainer(&array_writer);
    118   }
    119 
    120   // Construct DiskInfo.
    121   DiskInfo result(kDeviceFile, response.get());
    122   EXPECT_EQ(kDeviceFile, result.device_path());
    123   EXPECT_EQ(kDeviceIsDrive, result.is_drive());
    124   EXPECT_EQ(kDeviceIsReadOnly, result.is_read_only());
    125   // Use EXPECT_TRUE(kDevicePresentationHide == result.is_hidden()) instead of
    126   // EXPECT_EQ(kDevicePresentationHide, result.is_hidden()) as gcc 4.7 issues
    127   // the following warning on EXPECT_EQ(false, x), which is turned into an error
    128   // with -Werror=conversion-null:
    129   //
    130   //   converting 'false' to pointer type for argument 1 of
    131   //   'char testing::internal::IsNullLiteralHelper(testing::internal::Secret*)'
    132   EXPECT_TRUE(kDevicePresentationHide == result.is_hidden());
    133   EXPECT_EQ(kDeviceIsMediaAvailable, result.has_media());
    134   EXPECT_EQ(kDeviceIsOnBootDevice, result.on_boot_device());
    135   EXPECT_EQ(kNativePath, result.system_path());
    136   EXPECT_EQ(kDeviceFile, result.file_path());
    137   EXPECT_EQ(kVendorId, result.vendor_id());
    138   EXPECT_EQ(kVendorName, result.vendor_name());
    139   EXPECT_EQ(kProductId, result.product_id());
    140   EXPECT_EQ(kProductName, result.product_name());
    141   EXPECT_EQ(kDriveModel, result.drive_label());
    142   EXPECT_EQ(kIdLabel, result.label());
    143   EXPECT_EQ(kIdUuid, result.uuid());
    144   EXPECT_EQ(kDeviceSize, result.total_size_in_bytes());
    145   EXPECT_EQ(DEVICE_TYPE_SD, result.device_type());
    146   EXPECT_EQ(kMountPath, result.mount_path());
    147 }
    148 
    149 }  // namespace chromeos
    150