Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS 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 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 5 | #include "shill/dbus_adaptor.h" |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 6 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 7 | #include <map> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 8 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 9 | #include <vector> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 10 | |
| 11 | #include <dbus-c++/dbus.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | #include <gmock/gmock.h> |
| 14 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 15 | #include "shill/manager.h" |
| 16 | #include "shill/mock_control.h" |
| 17 | #include "shill/mock_device.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 18 | #include "shill/mock_property_store.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 19 | #include "shill/mock_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 20 | #include "shill/property_store_unittest.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 21 | #include "shill/shill_event.h" |
| 22 | |
| 23 | using std::map; |
| 24 | using std::string; |
| 25 | using std::vector; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 26 | using ::testing::_; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 27 | using ::testing::Return; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 28 | using ::testing::StrEq; |
| 29 | using ::testing::Test; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 30 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 31 | namespace shill { |
| 32 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 33 | class DBusAdaptorTest : public PropertyStoreTest { |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 34 | public: |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 35 | DBusAdaptorTest() |
| 36 | : ex_bool(true), |
| 37 | ex_byte(0xff), |
| 38 | ex_uint16(65535), |
| 39 | ex_uint32(2000000), |
| 40 | ex_int16(-32768), |
| 41 | ex_int32(-65536), |
| 42 | ex_string("something"), |
| 43 | ex_strings(1, ex_string), |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 44 | device_(new MockDevice(&control_interface_, |
| 45 | &dispatcher_, |
| 46 | &manager_, |
| 47 | "mock", |
| 48 | 0)), |
| 49 | service_(new MockService(&control_interface_, |
| 50 | &dispatcher_, |
| 51 | device_, |
| 52 | "mock")) { |
| 53 | ex_stringmap[ex_string] = ex_string; |
| 54 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 55 | bool_v_ = DBusAdaptor::BoolToVariant(ex_bool); |
| 56 | byte_v_ = DBusAdaptor::ByteToVariant(ex_byte); |
| 57 | uint16_v_ = DBusAdaptor::Uint16ToVariant(ex_uint16); |
| 58 | uint32_v_ = DBusAdaptor::Uint32ToVariant(ex_uint32); |
| 59 | int16_v_ = DBusAdaptor::Int16ToVariant(ex_int16); |
| 60 | int32_v_ = DBusAdaptor::Int32ToVariant(ex_int32); |
| 61 | string_v_ = DBusAdaptor::StringToVariant(ex_string); |
| 62 | stringmap_v_ = DBusAdaptor::StringmapToVariant(ex_stringmap); |
| 63 | strings_v_ = DBusAdaptor::StringsToVariant(ex_strings); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 66 | virtual ~DBusAdaptorTest() {} |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 67 | |
| 68 | bool ex_bool; |
| 69 | uint8 ex_byte; |
| 70 | uint16 ex_uint16; |
| 71 | uint32 ex_uint32; |
| 72 | int16 ex_int16; |
| 73 | int32 ex_int32; |
| 74 | string ex_string; |
| 75 | map<string, string> ex_stringmap; |
| 76 | vector<string> ex_strings; |
| 77 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 78 | protected: |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 79 | DeviceRefPtr device_; |
| 80 | ServiceRefPtr service_; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | TEST_F(DBusAdaptorTest, Conversions) { |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 84 | EXPECT_EQ(0, DBusAdaptor::BoolToVariant(0).reader().get_bool()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 85 | EXPECT_EQ(ex_bool, bool_v_.reader().get_bool()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 86 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 87 | EXPECT_EQ(0, DBusAdaptor::ByteToVariant(0).reader().get_byte()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 88 | EXPECT_EQ(ex_byte, byte_v_.reader().get_byte()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 89 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 90 | EXPECT_EQ(0, DBusAdaptor::Uint16ToVariant(0).reader().get_uint16()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 91 | EXPECT_EQ(ex_uint16, uint16_v_.reader().get_uint16()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 92 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 93 | EXPECT_EQ(0, DBusAdaptor::Int16ToVariant(0).reader().get_int16()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 94 | EXPECT_EQ(ex_int16, int16_v_.reader().get_int16()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 95 | |
| 96 | EXPECT_EQ(0, DBusAdaptor::Uint32ToVariant(0).reader().get_uint32()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 97 | EXPECT_EQ(ex_uint32, uint32_v_.reader().get_uint32()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 98 | |
| 99 | EXPECT_EQ(0, DBusAdaptor::Int32ToVariant(0).reader().get_int32()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 100 | EXPECT_EQ(ex_int32, int32_v_.reader().get_int32()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 101 | |
| 102 | EXPECT_EQ(string(""), DBusAdaptor::StringToVariant("").reader().get_string()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 103 | EXPECT_EQ(ex_string, string_v_.reader().get_string()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 104 | |
| 105 | EXPECT_EQ(ex_stringmap[ex_string], |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 106 | (stringmap_v_.operator map<string, string>()[ex_string])); |
| 107 | EXPECT_EQ(ex_strings[0], strings_v_.operator vector<string>()[0]); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TEST_F(DBusAdaptorTest, Signatures) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 111 | EXPECT_TRUE(DBusAdaptor::IsBool(bool_v_.signature())); |
| 112 | EXPECT_TRUE(DBusAdaptor::IsByte(byte_v_.signature())); |
| 113 | EXPECT_TRUE(DBusAdaptor::IsInt16(int16_v_.signature())); |
| 114 | EXPECT_TRUE(DBusAdaptor::IsInt32(int32_v_.signature())); |
| 115 | EXPECT_TRUE(DBusAdaptor::IsString(string_v_.signature())); |
| 116 | EXPECT_TRUE(DBusAdaptor::IsStringmap(stringmap_v_.signature())); |
| 117 | EXPECT_TRUE(DBusAdaptor::IsStrings(strings_v_.signature())); |
| 118 | EXPECT_TRUE(DBusAdaptor::IsUint16(uint16_v_.signature())); |
| 119 | EXPECT_TRUE(DBusAdaptor::IsUint32(uint32_v_.signature())); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 120 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 121 | EXPECT_FALSE(DBusAdaptor::IsBool(byte_v_.signature())); |
| 122 | EXPECT_FALSE(DBusAdaptor::IsStrings(string_v_.signature())); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TEST_F(DBusAdaptorTest, Dispatch) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 126 | MockPropertyStore store; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 127 | ::DBus::Error error; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 128 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 129 | EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(true)); |
| 130 | EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(true)); |
| 131 | EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(true)); |
| 132 | EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(true)); |
| 133 | EXPECT_CALL(store, SetStringProperty("", _, _)) |
| 134 | .WillOnce(Return(true)); |
| 135 | EXPECT_CALL(store, SetStringmapProperty("", _, _)) |
| 136 | .WillOnce(Return(true)); |
| 137 | EXPECT_CALL(store, SetStringsProperty("", _, _)) |
| 138 | .WillOnce(Return(true)); |
| 139 | EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(true)); |
| 140 | EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(true)); |
| 141 | EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(true)); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 142 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 143 | string string_path("/false/path"); |
| 144 | ::DBus::Path path(string_path); |
| 145 | ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path); |
| 146 | EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _)) |
| 147 | .WillOnce(Return(true)); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 148 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame^] | 149 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", bool_v_, error)); |
| 150 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", path_v, error)); |
| 151 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", string_v_, error)); |
| 152 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", strings_v_, error)); |
| 153 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", int16_v_, error)); |
| 154 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", int32_v_, error)); |
| 155 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", uint16_v_, error)); |
| 156 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", uint32_v_, error)); |
| 157 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", stringmap_v_, error)); |
| 158 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", byte_v_, error)); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 159 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | } // namespace shill |