blob: b56bfa284f2c5939799a952b009795503ab520a4 [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masoned0ceb8c2011-06-02 10:05:39 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Chris Masone8fe2c7e2011-06-09 15:51:19 -07005#include "shill/dbus_adaptor.h"
Chris Masoned0ceb8c2011-06-02 10:05:39 -07006
Chris Masone8fe2c7e2011-06-09 15:51:19 -07007#include <map>
Chris Masoned0ceb8c2011-06-02 10:05:39 -07008#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07009#include <vector>
Chris Masoned0ceb8c2011-06-02 10:05:39 -070010
11#include <dbus-c++/dbus.h>
12#include <gtest/gtest.h>
13#include <gmock/gmock.h>
14
Paul Stewart26b327e2011-10-19 11:38:09 -070015#include "shill/event_dispatcher.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070016#include "shill/key_value_store.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070017#include "shill/manager.h"
18#include "shill/mock_control.h"
19#include "shill/mock_device.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070020#include "shill/mock_glib.h"
21#include "shill/mock_profile.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070022#include "shill/mock_property_store.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070023#include "shill/mock_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070024#include "shill/property_store_unittest.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070025
26using std::map;
27using std::string;
28using std::vector;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070029using ::testing::_;
Gaurav Shahb790aa22012-10-23 12:51:12 -070030using ::testing::ContainerEq;
mukesh agrawal8abd2f62012-01-30 14:56:14 -080031using ::testing::DoAll;
32using ::testing::Invoke;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070033using ::testing::Return;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070034using ::testing::StrEq;
35using ::testing::Test;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070036
Chris Masone8fe2c7e2011-06-09 15:51:19 -070037namespace shill {
38
Chris Masone3bd3c8c2011-06-13 08:20:26 -070039class DBusAdaptorTest : public PropertyStoreTest {
Chris Masoned0ceb8c2011-06-02 10:05:39 -070040 public:
Chris Masone8fe2c7e2011-06-09 15:51:19 -070041 DBusAdaptorTest()
Chris Masoneb925cc82011-06-22 15:39:57 -070042 : ex_bool_(true),
43 ex_byte_(0xff),
44 ex_uint16_(65535),
45 ex_uint32_(2000000),
Paul Stewarte18c33b2012-07-10 20:48:44 -070046 ex_uint64_(8589934591LL),
Chris Masoneb925cc82011-06-22 15:39:57 -070047 ex_int16_(-32768),
48 ex_int32_(-65536),
mukesh agrawal32399322011-09-01 10:53:43 -070049 ex_path_("/"),
Chris Masoneb925cc82011-06-22 15:39:57 -070050 ex_string_("something"),
51 ex_strings_(1, ex_string_),
52 bool_v_(DBusAdaptor::BoolToVariant(ex_bool_)),
53 byte_v_(DBusAdaptor::ByteToVariant(ex_byte_)),
Chris Masoneb925cc82011-06-22 15:39:57 -070054 int16_v_(DBusAdaptor::Int16ToVariant(ex_int16_)),
55 int32_v_(DBusAdaptor::Int32ToVariant(ex_int32_)),
mukesh agrawal32399322011-09-01 10:53:43 -070056 path_v_(DBusAdaptor::PathToVariant(ex_path_)),
Chris Masoneb925cc82011-06-22 15:39:57 -070057 string_v_(DBusAdaptor::StringToVariant(ex_string_)),
58 strings_v_(DBusAdaptor::StringsToVariant(ex_strings_)),
mukesh agrawalf60e4062011-05-27 13:13:41 -070059 uint16_v_(DBusAdaptor::Uint16ToVariant(ex_uint16_)),
60 uint32_v_(DBusAdaptor::Uint32ToVariant(ex_uint32_)),
Paul Stewarte18c33b2012-07-10 20:48:44 -070061 uint64_v_(DBusAdaptor::Uint64ToVariant(ex_uint64_)),
Chris Masone2176a882011-09-14 22:29:15 -070062 device_(new MockDevice(control_interface(),
63 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080064 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -070065 manager(),
Chris Masone8fe2c7e2011-06-09 15:51:19 -070066 "mock",
Chris Masone626719f2011-08-18 16:58:48 -070067 "addr0",
Chris Masone8fe2c7e2011-06-09 15:51:19 -070068 0)),
Chris Masone2176a882011-09-14 22:29:15 -070069 service_(new MockService(control_interface(),
70 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080071 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -070072 manager())) {
Paul Stewartced6a0b2011-11-08 15:32:04 -080073 ex_bytearrays_.push_back(ByteArray());
74 bytearrays_v_ = DBusAdaptor::ByteArraysToVariant(ex_bytearrays_);
75
Chris Masoneb925cc82011-06-22 15:39:57 -070076 ex_stringmap_[ex_string_] = ex_string_;
77 stringmap_v_ = DBusAdaptor::StringmapToVariant(ex_stringmap_);
mukesh agrawal32399322011-09-01 10:53:43 -070078
Gaurav Shahb790aa22012-10-23 12:51:12 -070079 ex_stringmaps_.push_back(ex_stringmap_);
80 stringmaps_v_ = DBusAdaptor::StringmapsToVariant(ex_stringmaps_);
81
mukesh agrawal2366eed2012-03-20 18:21:50 -070082 ex_paths_.push_back(ex_path_);
83 paths_v_ = DBusAdaptor::PathsToVariant(ex_paths_);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070084 }
85
Chris Masoned0ceb8c2011-06-02 10:05:39 -070086 virtual ~DBusAdaptorTest() {}
Chris Masone8fe2c7e2011-06-09 15:51:19 -070087
Chris Masone8fe2c7e2011-06-09 15:51:19 -070088 protected:
Chris Masoneb925cc82011-06-22 15:39:57 -070089 bool ex_bool_;
90 uint8 ex_byte_;
Paul Stewartced6a0b2011-11-08 15:32:04 -080091 ByteArrays ex_bytearrays_;
Chris Masoneb925cc82011-06-22 15:39:57 -070092 uint16 ex_uint16_;
93 uint32 ex_uint32_;
Paul Stewarte18c33b2012-07-10 20:48:44 -070094 uint64 ex_uint64_;
Chris Masoneb925cc82011-06-22 15:39:57 -070095 int16 ex_int16_;
96 int32 ex_int32_;
mukesh agrawal32399322011-09-01 10:53:43 -070097 ::DBus::Path ex_path_;
mukesh agrawal2366eed2012-03-20 18:21:50 -070098 vector< ::DBus::Path> ex_paths_;
Chris Masoneb925cc82011-06-22 15:39:57 -070099 string ex_string_;
100 map<string, string> ex_stringmap_;
101 vector<map<string, string> > ex_stringmaps_;
102 vector<string> ex_strings_;
103
104 ::DBus::Variant bool_v_;
105 ::DBus::Variant byte_v_;
Paul Stewartced6a0b2011-11-08 15:32:04 -0800106 ::DBus::Variant bytearrays_v_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700107 ::DBus::Variant int16_v_;
108 ::DBus::Variant int32_v_;
mukesh agrawal32399322011-09-01 10:53:43 -0700109 ::DBus::Variant path_v_;
mukesh agrawal2366eed2012-03-20 18:21:50 -0700110 ::DBus::Variant paths_v_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700111 ::DBus::Variant string_v_;
112 ::DBus::Variant stringmap_v_;
113 ::DBus::Variant stringmaps_v_;
114 ::DBus::Variant strings_v_;
115 ::DBus::Variant uint16_v_;
116 ::DBus::Variant uint32_v_;
Paul Stewarte18c33b2012-07-10 20:48:44 -0700117 ::DBus::Variant uint64_v_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700118
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700119 DeviceRefPtr device_;
120 ServiceRefPtr service_;
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700121};
122
123TEST_F(DBusAdaptorTest, Conversions) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700124 EXPECT_EQ(0, PropertyStoreTest::kBoolV.reader().get_bool());
125 EXPECT_EQ(ex_bool_, bool_v_.reader().get_bool());
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700126
Chris Masoneb925cc82011-06-22 15:39:57 -0700127 EXPECT_EQ(0, PropertyStoreTest::kByteV.reader().get_byte());
128 EXPECT_EQ(ex_byte_, byte_v_.reader().get_byte());
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700129
Paul Stewartced6a0b2011-11-08 15:32:04 -0800130 EXPECT_TRUE(ex_bytearrays_ == bytearrays_v_.operator ByteArrays());
131
Chris Masoneb925cc82011-06-22 15:39:57 -0700132 EXPECT_EQ(0, PropertyStoreTest::kUint16V.reader().get_uint16());
133 EXPECT_EQ(ex_uint16_, uint16_v_.reader().get_uint16());
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700134
Chris Masoneb925cc82011-06-22 15:39:57 -0700135 EXPECT_EQ(0, PropertyStoreTest::kInt16V.reader().get_int16());
136 EXPECT_EQ(ex_int16_, int16_v_.reader().get_int16());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700137
Chris Masoneb925cc82011-06-22 15:39:57 -0700138 EXPECT_EQ(0, PropertyStoreTest::kUint32V.reader().get_uint32());
139 EXPECT_EQ(ex_uint32_, uint32_v_.reader().get_uint32());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700140
Paul Stewarte18c33b2012-07-10 20:48:44 -0700141 EXPECT_EQ(0, PropertyStoreTest::kUint64V.reader().get_uint64());
142 EXPECT_EQ(ex_uint64_, uint64_v_.reader().get_uint64());
143
Chris Masoneb925cc82011-06-22 15:39:57 -0700144 EXPECT_EQ(0, PropertyStoreTest::kInt32V.reader().get_int32());
145 EXPECT_EQ(ex_int32_, int32_v_.reader().get_int32());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700146
mukesh agrawal32399322011-09-01 10:53:43 -0700147 EXPECT_EQ(ex_path_, path_v_.reader().get_path());
148
mukesh agrawal2366eed2012-03-20 18:21:50 -0700149 EXPECT_EQ(ex_path_, paths_v_.operator vector< ::DBus::Path>()[0]);
mukesh agrawal32399322011-09-01 10:53:43 -0700150
Chris Masoneb925cc82011-06-22 15:39:57 -0700151 EXPECT_EQ(string(""), PropertyStoreTest::kStringV.reader().get_string());
152 EXPECT_EQ(ex_string_, string_v_.reader().get_string());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700153
Gaurav Shahb790aa22012-10-23 12:51:12 -0700154 EXPECT_THAT(ex_stringmap_,
155 ContainerEq(stringmap_v_.operator map<string, string>()));
156 EXPECT_THAT(ex_strings_,
157 ContainerEq(strings_v_.operator vector<string>()));
158 EXPECT_THAT(
159 ex_stringmaps_,
160 ContainerEq(stringmaps_v_.operator vector<map<string, string> >()));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700161}
162
163TEST_F(DBusAdaptorTest, Signatures) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700164 EXPECT_TRUE(DBusAdaptor::IsBool(bool_v_.signature()));
Paul Stewartced6a0b2011-11-08 15:32:04 -0800165 EXPECT_TRUE(DBusAdaptor::IsByteArrays(bytearrays_v_.signature()));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700166 EXPECT_TRUE(DBusAdaptor::IsByte(byte_v_.signature()));
167 EXPECT_TRUE(DBusAdaptor::IsInt16(int16_v_.signature()));
168 EXPECT_TRUE(DBusAdaptor::IsInt32(int32_v_.signature()));
mukesh agrawal32399322011-09-01 10:53:43 -0700169 EXPECT_TRUE(DBusAdaptor::IsPath(path_v_.signature()));
mukesh agrawal2366eed2012-03-20 18:21:50 -0700170 EXPECT_TRUE(DBusAdaptor::IsPaths(paths_v_.signature()));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700171 EXPECT_TRUE(DBusAdaptor::IsString(string_v_.signature()));
172 EXPECT_TRUE(DBusAdaptor::IsStringmap(stringmap_v_.signature()));
173 EXPECT_TRUE(DBusAdaptor::IsStrings(strings_v_.signature()));
174 EXPECT_TRUE(DBusAdaptor::IsUint16(uint16_v_.signature()));
175 EXPECT_TRUE(DBusAdaptor::IsUint32(uint32_v_.signature()));
Paul Stewarte18c33b2012-07-10 20:48:44 -0700176 EXPECT_TRUE(DBusAdaptor::IsUint64(uint64_v_.signature()));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700177
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700178 EXPECT_FALSE(DBusAdaptor::IsBool(byte_v_.signature()));
179 EXPECT_FALSE(DBusAdaptor::IsStrings(string_v_.signature()));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700180}
181
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800182TEST_F(DBusAdaptorTest, SetProperty) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700183 MockPropertyStore store;
Paul Stewarte18c33b2012-07-10 20:48:44 -0700184 ::DBus::Error e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700185
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700186 EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(true));
187 EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(true));
188 EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(true));
189 EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(true));
190 EXPECT_CALL(store, SetStringProperty("", _, _))
191 .WillOnce(Return(true));
192 EXPECT_CALL(store, SetStringmapProperty("", _, _))
193 .WillOnce(Return(true));
194 EXPECT_CALL(store, SetStringsProperty("", _, _))
195 .WillOnce(Return(true));
196 EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(true));
197 EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(true));
198 EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(true));
Paul Stewarte18c33b2012-07-10 20:48:44 -0700199 EXPECT_CALL(store, SetUint64Property("", _, _)).WillOnce(Return(true));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700200
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700201 string string_path("/false/path");
202 ::DBus::Path path(string_path);
203 ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path);
204 EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _))
205 .WillOnce(Return(true));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700206
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800207 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", bool_v_, &e1));
208 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", path_v, &e2));
209 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", string_v_, &e3));
210 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", strings_v_, &e4));
211 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", int16_v_, &e5));
212 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", int32_v_, &e6));
213 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint16_v_, &e7));
214 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint32_v_, &e8));
Paul Stewarte18c33b2012-07-10 20:48:44 -0700215 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint64_v_, &e9));
216 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", stringmap_v_, &e10));
217 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", byte_v_, &e11));
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700218}
219
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700220// SetProperty method should propagate failures. This should happen
221// even if error isn't set. (This is to accomodate the fact that, if
222// the property already has the desired value, the store will return
223// false, without setting an error.)
224TEST_F(DBusAdaptorTest, SetPropertyFailure) {
225 MockPropertyStore store;
226 ::DBus::Error e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11;
227
228 EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(false));
229 EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(false));
230 EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(false));
231 EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(false));
232 EXPECT_CALL(store, SetStringProperty("", _, _))
233 .WillOnce(Return(false));
234 EXPECT_CALL(store, SetStringmapProperty("", _, _))
235 .WillOnce(Return(false));
236 EXPECT_CALL(store, SetStringsProperty("", _, _))
237 .WillOnce(Return(false));
238 EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(false));
239 EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(false));
240 EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(false));
241 EXPECT_CALL(store, SetUint64Property("", _, _)).WillOnce(Return(false));
242
243 string string_path("/false/path");
244 ::DBus::Path path(string_path);
245 ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path);
246 EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _))
247 .WillOnce(Return(false));
248
249 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", bool_v_, &e1));
250 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", path_v, &e2));
251 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", string_v_, &e3));
252 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", strings_v_, &e4));
253 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", int16_v_, &e5));
254 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", int32_v_, &e6));
255 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint16_v_, &e7));
256 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint32_v_, &e8));
257 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint64_v_, &e9));
258 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", stringmap_v_, &e10));
259 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", byte_v_, &e11));
260}
261
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800262void SetError(const string &/*name*/, Error *error) {
263 error->Populate(Error::kInvalidProperty);
264}
265
266TEST_F(DBusAdaptorTest, ClearProperty) {
267 MockPropertyStore store;
268 ::DBus::Error e1, e2;
269
270 EXPECT_CALL(store, ClearProperty("valid property", _)).
271 WillOnce(Return(true));
272 EXPECT_CALL(store, ClearProperty("invalid property", _)).
273 WillOnce(DoAll(Invoke(SetError),
274 Return(false)));
275 EXPECT_TRUE(DBusAdaptor::ClearProperty(&store, "valid property", &e1));
276 EXPECT_FALSE(DBusAdaptor::ClearProperty(&store, "invalid property", &e2));
277
278}
279
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700280TEST_F(DBusAdaptorTest, ArgsToKeyValueStore) {
Darin Petkov25665aa2012-05-21 14:08:12 +0200281 map<string, ::DBus::Variant> args;
282 KeyValueStore args_kv;
283 Error error;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700284
Darin Petkov25665aa2012-05-21 14:08:12 +0200285 args["string_arg"].writer().append_string("string");
286 args["bool_arg"].writer().append_bool(true);
287 DBusAdaptor::ArgsToKeyValueStore(args, &args_kv, &error);
288 EXPECT_TRUE(error.IsSuccess());
289 EXPECT_EQ("string", args_kv.GetString("string_arg"));
290 EXPECT_EQ(true, args_kv.GetBool("bool_arg"));
291}
292
293TEST_F(DBusAdaptorTest, KeyValueStoreToVariant) {
294 static const char kStringKey[] = "StringKey";
295 static const char kStringValue[] = "StringValue";
296 static const char kBoolKey[] = "BoolKey";
297 const bool kBoolValue = true;
298 KeyValueStore store;
299 store.SetString(kStringKey, kStringValue);
300 store.SetBool(kBoolKey, kBoolValue);
301 DBus::Variant var = DBusAdaptor::KeyValueStoreToVariant(store);
302 ASSERT_TRUE(DBusAdaptor::IsKeyValueStore(var.signature()));
303 DBusPropertiesMap props = var.operator DBusPropertiesMap();
304 // Sanity test the result.
305 EXPECT_EQ(2, props.size());
306 string string_value;
307 EXPECT_TRUE(DBusProperties::GetString(props, kStringKey, &string_value));
308 EXPECT_EQ(kStringValue, string_value);
309 bool bool_value = !kBoolValue;
310 EXPECT_TRUE(DBusProperties::GetBool(props, kBoolKey, &bool_value));
311 EXPECT_EQ(kBoolValue, bool_value);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700312}
313
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700314} // namespace shill