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 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 15 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 16 | #include "shill/key_value_store.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 17 | #include "shill/manager.h" |
| 18 | #include "shill/mock_control.h" |
| 19 | #include "shill/mock_device.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 20 | #include "shill/mock_glib.h" |
| 21 | #include "shill/mock_profile.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 22 | #include "shill/mock_property_store.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 23 | #include "shill/mock_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 24 | #include "shill/property_store_unittest.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 25 | |
| 26 | using std::map; |
| 27 | using std::string; |
| 28 | using std::vector; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 29 | using ::testing::_; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 30 | using ::testing::Return; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 31 | using ::testing::StrEq; |
| 32 | using ::testing::Test; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 33 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 34 | namespace shill { |
| 35 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 36 | class DBusAdaptorTest : public PropertyStoreTest { |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 37 | public: |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 38 | DBusAdaptorTest() |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 39 | : ex_bool_(true), |
| 40 | ex_byte_(0xff), |
| 41 | ex_uint16_(65535), |
| 42 | ex_uint32_(2000000), |
| 43 | ex_int16_(-32768), |
| 44 | ex_int32_(-65536), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 45 | ex_path_("/"), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 46 | ex_string_("something"), |
| 47 | ex_strings_(1, ex_string_), |
| 48 | bool_v_(DBusAdaptor::BoolToVariant(ex_bool_)), |
| 49 | byte_v_(DBusAdaptor::ByteToVariant(ex_byte_)), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 50 | int16_v_(DBusAdaptor::Int16ToVariant(ex_int16_)), |
| 51 | int32_v_(DBusAdaptor::Int32ToVariant(ex_int32_)), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 52 | path_v_(DBusAdaptor::PathToVariant(ex_path_)), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 53 | string_v_(DBusAdaptor::StringToVariant(ex_string_)), |
| 54 | strings_v_(DBusAdaptor::StringsToVariant(ex_strings_)), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 55 | uint16_v_(DBusAdaptor::Uint16ToVariant(ex_uint16_)), |
| 56 | uint32_v_(DBusAdaptor::Uint32ToVariant(ex_uint32_)), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 57 | device_(new MockDevice(control_interface(), |
| 58 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 59 | manager(), |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 60 | "mock", |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 61 | "addr0", |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 62 | 0)), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 63 | service_(new MockService(control_interface(), |
| 64 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 65 | manager())) { |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 66 | ex_bytearrays_.push_back(ByteArray()); |
| 67 | bytearrays_v_ = DBusAdaptor::ByteArraysToVariant(ex_bytearrays_); |
| 68 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 69 | ex_stringmap_[ex_string_] = ex_string_; |
| 70 | stringmap_v_ = DBusAdaptor::StringmapToVariant(ex_stringmap_); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 71 | |
| 72 | ex_patharray_.push_back(ex_path_); |
| 73 | patharray_v_ = DBusAdaptor::PathArrayToVariant(ex_patharray_); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 76 | virtual ~DBusAdaptorTest() {} |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 77 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 78 | protected: |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 79 | bool ex_bool_; |
| 80 | uint8 ex_byte_; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 81 | ByteArrays ex_bytearrays_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 82 | uint16 ex_uint16_; |
| 83 | uint32 ex_uint32_; |
| 84 | int16 ex_int16_; |
| 85 | int32 ex_int32_; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 86 | ::DBus::Path ex_path_; |
| 87 | vector< ::DBus::Path> ex_patharray_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 88 | string ex_string_; |
| 89 | map<string, string> ex_stringmap_; |
| 90 | vector<map<string, string> > ex_stringmaps_; |
| 91 | vector<string> ex_strings_; |
| 92 | |
| 93 | ::DBus::Variant bool_v_; |
| 94 | ::DBus::Variant byte_v_; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 95 | ::DBus::Variant bytearrays_v_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 96 | ::DBus::Variant int16_v_; |
| 97 | ::DBus::Variant int32_v_; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 98 | ::DBus::Variant path_v_; |
| 99 | ::DBus::Variant patharray_v_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 100 | ::DBus::Variant string_v_; |
| 101 | ::DBus::Variant stringmap_v_; |
| 102 | ::DBus::Variant stringmaps_v_; |
| 103 | ::DBus::Variant strings_v_; |
| 104 | ::DBus::Variant uint16_v_; |
| 105 | ::DBus::Variant uint32_v_; |
| 106 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 107 | DeviceRefPtr device_; |
| 108 | ServiceRefPtr service_; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | TEST_F(DBusAdaptorTest, Conversions) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 112 | EXPECT_EQ(0, PropertyStoreTest::kBoolV.reader().get_bool()); |
| 113 | EXPECT_EQ(ex_bool_, bool_v_.reader().get_bool()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 114 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 115 | EXPECT_EQ(0, PropertyStoreTest::kByteV.reader().get_byte()); |
| 116 | EXPECT_EQ(ex_byte_, byte_v_.reader().get_byte()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 117 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 118 | EXPECT_TRUE(ex_bytearrays_ == bytearrays_v_.operator ByteArrays()); |
| 119 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 120 | EXPECT_EQ(0, PropertyStoreTest::kUint16V.reader().get_uint16()); |
| 121 | EXPECT_EQ(ex_uint16_, uint16_v_.reader().get_uint16()); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 122 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 123 | EXPECT_EQ(0, PropertyStoreTest::kInt16V.reader().get_int16()); |
| 124 | EXPECT_EQ(ex_int16_, int16_v_.reader().get_int16()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 125 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 126 | EXPECT_EQ(0, PropertyStoreTest::kUint32V.reader().get_uint32()); |
| 127 | EXPECT_EQ(ex_uint32_, uint32_v_.reader().get_uint32()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 128 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 129 | EXPECT_EQ(0, PropertyStoreTest::kInt32V.reader().get_int32()); |
| 130 | EXPECT_EQ(ex_int32_, int32_v_.reader().get_int32()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 131 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 132 | EXPECT_EQ(ex_path_, path_v_.reader().get_path()); |
| 133 | |
| 134 | EXPECT_EQ(ex_path_, patharray_v_.operator vector< ::DBus::Path>()[0]); |
| 135 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 136 | EXPECT_EQ(string(""), PropertyStoreTest::kStringV.reader().get_string()); |
| 137 | EXPECT_EQ(ex_string_, string_v_.reader().get_string()); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 138 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 139 | EXPECT_EQ(ex_stringmap_[ex_string_], |
| 140 | (stringmap_v_.operator map<string, string>()[ex_string_])); |
| 141 | EXPECT_EQ(ex_strings_[0], strings_v_.operator vector<string>()[0]); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | TEST_F(DBusAdaptorTest, Signatures) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 145 | EXPECT_TRUE(DBusAdaptor::IsBool(bool_v_.signature())); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 146 | EXPECT_TRUE(DBusAdaptor::IsByteArrays(bytearrays_v_.signature())); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 147 | EXPECT_TRUE(DBusAdaptor::IsByte(byte_v_.signature())); |
| 148 | EXPECT_TRUE(DBusAdaptor::IsInt16(int16_v_.signature())); |
| 149 | EXPECT_TRUE(DBusAdaptor::IsInt32(int32_v_.signature())); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 150 | EXPECT_TRUE(DBusAdaptor::IsPath(path_v_.signature())); |
| 151 | EXPECT_TRUE(DBusAdaptor::IsPathArray(patharray_v_.signature())); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 152 | EXPECT_TRUE(DBusAdaptor::IsString(string_v_.signature())); |
| 153 | EXPECT_TRUE(DBusAdaptor::IsStringmap(stringmap_v_.signature())); |
| 154 | EXPECT_TRUE(DBusAdaptor::IsStrings(strings_v_.signature())); |
| 155 | EXPECT_TRUE(DBusAdaptor::IsUint16(uint16_v_.signature())); |
| 156 | EXPECT_TRUE(DBusAdaptor::IsUint32(uint32_v_.signature())); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 157 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 158 | EXPECT_FALSE(DBusAdaptor::IsBool(byte_v_.signature())); |
| 159 | EXPECT_FALSE(DBusAdaptor::IsStrings(string_v_.signature())); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | TEST_F(DBusAdaptorTest, Dispatch) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 163 | MockPropertyStore store; |
mukesh agrawal | a903e41 | 2011-06-28 17:07:11 -0700 | [diff] [blame] | 164 | ::DBus::Error e1, e2, e3, e4, e5, e6, e7, e8, e9, e10; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 165 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 166 | EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(true)); |
| 167 | EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(true)); |
| 168 | EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(true)); |
| 169 | EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(true)); |
| 170 | EXPECT_CALL(store, SetStringProperty("", _, _)) |
| 171 | .WillOnce(Return(true)); |
| 172 | EXPECT_CALL(store, SetStringmapProperty("", _, _)) |
| 173 | .WillOnce(Return(true)); |
| 174 | EXPECT_CALL(store, SetStringsProperty("", _, _)) |
| 175 | .WillOnce(Return(true)); |
| 176 | EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(true)); |
| 177 | EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(true)); |
| 178 | EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(true)); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 179 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 180 | string string_path("/false/path"); |
| 181 | ::DBus::Path path(string_path); |
| 182 | ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path); |
| 183 | EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _)) |
| 184 | .WillOnce(Return(true)); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 185 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 186 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", bool_v_, &e1)); |
| 187 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", path_v, &e2)); |
| 188 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", string_v_, &e3)); |
| 189 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", strings_v_, &e4)); |
| 190 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", int16_v_, &e5)); |
| 191 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", int32_v_, &e6)); |
| 192 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", uint16_v_, &e7)); |
| 193 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", uint32_v_, &e8)); |
| 194 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", stringmap_v_, &e9)); |
| 195 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(&store, "", byte_v_, &e10)); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 196 | } |
| 197 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 198 | TEST_F(DBusAdaptorTest, ArgsToKeyValueStore) { |
| 199 | map<string, ::DBus::Variant> args; |
| 200 | KeyValueStore args_kv; |
| 201 | Error error; |
| 202 | |
| 203 | args["string_arg"].writer().append_string("string"); |
| 204 | args["bool_arg"].writer().append_bool(true); |
| 205 | DBusAdaptor::ArgsToKeyValueStore(args, &args_kv, &error); |
| 206 | EXPECT_TRUE(error.IsSuccess()); |
| 207 | EXPECT_EQ("string", args_kv.GetString("string_arg")); |
| 208 | EXPECT_EQ(true, args_kv.GetBool("bool_arg")); |
| 209 | } |
| 210 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 211 | } // namespace shill |