blob: 70a282bf6ec9caf7241071a6e186b84911316daf [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::_;
mukesh agrawal8abd2f62012-01-30 14:56:14 -080030using ::testing::DoAll;
31using ::testing::Invoke;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070032using ::testing::Return;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070033using ::testing::StrEq;
34using ::testing::Test;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070035
Chris Masone8fe2c7e2011-06-09 15:51:19 -070036namespace shill {
37
Chris Masone3bd3c8c2011-06-13 08:20:26 -070038class DBusAdaptorTest : public PropertyStoreTest {
Chris Masoned0ceb8c2011-06-02 10:05:39 -070039 public:
Chris Masone8fe2c7e2011-06-09 15:51:19 -070040 DBusAdaptorTest()
Chris Masoneb925cc82011-06-22 15:39:57 -070041 : ex_bool_(true),
42 ex_byte_(0xff),
43 ex_uint16_(65535),
44 ex_uint32_(2000000),
Paul Stewarte18c33b2012-07-10 20:48:44 -070045 ex_uint64_(8589934591LL),
Chris Masoneb925cc82011-06-22 15:39:57 -070046 ex_int16_(-32768),
47 ex_int32_(-65536),
mukesh agrawal32399322011-09-01 10:53:43 -070048 ex_path_("/"),
Chris Masoneb925cc82011-06-22 15:39:57 -070049 ex_string_("something"),
50 ex_strings_(1, ex_string_),
51 bool_v_(DBusAdaptor::BoolToVariant(ex_bool_)),
52 byte_v_(DBusAdaptor::ByteToVariant(ex_byte_)),
Chris Masoneb925cc82011-06-22 15:39:57 -070053 int16_v_(DBusAdaptor::Int16ToVariant(ex_int16_)),
54 int32_v_(DBusAdaptor::Int32ToVariant(ex_int32_)),
mukesh agrawal32399322011-09-01 10:53:43 -070055 path_v_(DBusAdaptor::PathToVariant(ex_path_)),
Chris Masoneb925cc82011-06-22 15:39:57 -070056 string_v_(DBusAdaptor::StringToVariant(ex_string_)),
57 strings_v_(DBusAdaptor::StringsToVariant(ex_strings_)),
mukesh agrawalf60e4062011-05-27 13:13:41 -070058 uint16_v_(DBusAdaptor::Uint16ToVariant(ex_uint16_)),
59 uint32_v_(DBusAdaptor::Uint32ToVariant(ex_uint32_)),
Paul Stewarte18c33b2012-07-10 20:48:44 -070060 uint64_v_(DBusAdaptor::Uint64ToVariant(ex_uint64_)),
Chris Masone2176a882011-09-14 22:29:15 -070061 device_(new MockDevice(control_interface(),
62 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080063 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -070064 manager(),
Chris Masone8fe2c7e2011-06-09 15:51:19 -070065 "mock",
Chris Masone626719f2011-08-18 16:58:48 -070066 "addr0",
Chris Masone8fe2c7e2011-06-09 15:51:19 -070067 0)),
Chris Masone2176a882011-09-14 22:29:15 -070068 service_(new MockService(control_interface(),
69 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080070 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -070071 manager())) {
Paul Stewartced6a0b2011-11-08 15:32:04 -080072 ex_bytearrays_.push_back(ByteArray());
73 bytearrays_v_ = DBusAdaptor::ByteArraysToVariant(ex_bytearrays_);
74
Chris Masoneb925cc82011-06-22 15:39:57 -070075 ex_stringmap_[ex_string_] = ex_string_;
76 stringmap_v_ = DBusAdaptor::StringmapToVariant(ex_stringmap_);
mukesh agrawal32399322011-09-01 10:53:43 -070077
mukesh agrawal2366eed2012-03-20 18:21:50 -070078 ex_paths_.push_back(ex_path_);
79 paths_v_ = DBusAdaptor::PathsToVariant(ex_paths_);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070080 }
81
Chris Masoned0ceb8c2011-06-02 10:05:39 -070082 virtual ~DBusAdaptorTest() {}
Chris Masone8fe2c7e2011-06-09 15:51:19 -070083
Chris Masone8fe2c7e2011-06-09 15:51:19 -070084 protected:
Chris Masoneb925cc82011-06-22 15:39:57 -070085 bool ex_bool_;
86 uint8 ex_byte_;
Paul Stewartced6a0b2011-11-08 15:32:04 -080087 ByteArrays ex_bytearrays_;
Chris Masoneb925cc82011-06-22 15:39:57 -070088 uint16 ex_uint16_;
89 uint32 ex_uint32_;
Paul Stewarte18c33b2012-07-10 20:48:44 -070090 uint64 ex_uint64_;
Chris Masoneb925cc82011-06-22 15:39:57 -070091 int16 ex_int16_;
92 int32 ex_int32_;
mukesh agrawal32399322011-09-01 10:53:43 -070093 ::DBus::Path ex_path_;
mukesh agrawal2366eed2012-03-20 18:21:50 -070094 vector< ::DBus::Path> ex_paths_;
Chris Masoneb925cc82011-06-22 15:39:57 -070095 string ex_string_;
96 map<string, string> ex_stringmap_;
97 vector<map<string, string> > ex_stringmaps_;
98 vector<string> ex_strings_;
99
100 ::DBus::Variant bool_v_;
101 ::DBus::Variant byte_v_;
Paul Stewartced6a0b2011-11-08 15:32:04 -0800102 ::DBus::Variant bytearrays_v_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700103 ::DBus::Variant int16_v_;
104 ::DBus::Variant int32_v_;
mukesh agrawal32399322011-09-01 10:53:43 -0700105 ::DBus::Variant path_v_;
mukesh agrawal2366eed2012-03-20 18:21:50 -0700106 ::DBus::Variant paths_v_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700107 ::DBus::Variant string_v_;
108 ::DBus::Variant stringmap_v_;
109 ::DBus::Variant stringmaps_v_;
110 ::DBus::Variant strings_v_;
111 ::DBus::Variant uint16_v_;
112 ::DBus::Variant uint32_v_;
Paul Stewarte18c33b2012-07-10 20:48:44 -0700113 ::DBus::Variant uint64_v_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700114
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700115 DeviceRefPtr device_;
116 ServiceRefPtr service_;
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700117};
118
119TEST_F(DBusAdaptorTest, Conversions) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700120 EXPECT_EQ(0, PropertyStoreTest::kBoolV.reader().get_bool());
121 EXPECT_EQ(ex_bool_, bool_v_.reader().get_bool());
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700122
Chris Masoneb925cc82011-06-22 15:39:57 -0700123 EXPECT_EQ(0, PropertyStoreTest::kByteV.reader().get_byte());
124 EXPECT_EQ(ex_byte_, byte_v_.reader().get_byte());
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700125
Paul Stewartced6a0b2011-11-08 15:32:04 -0800126 EXPECT_TRUE(ex_bytearrays_ == bytearrays_v_.operator ByteArrays());
127
Chris Masoneb925cc82011-06-22 15:39:57 -0700128 EXPECT_EQ(0, PropertyStoreTest::kUint16V.reader().get_uint16());
129 EXPECT_EQ(ex_uint16_, uint16_v_.reader().get_uint16());
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700130
Chris Masoneb925cc82011-06-22 15:39:57 -0700131 EXPECT_EQ(0, PropertyStoreTest::kInt16V.reader().get_int16());
132 EXPECT_EQ(ex_int16_, int16_v_.reader().get_int16());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700133
Chris Masoneb925cc82011-06-22 15:39:57 -0700134 EXPECT_EQ(0, PropertyStoreTest::kUint32V.reader().get_uint32());
135 EXPECT_EQ(ex_uint32_, uint32_v_.reader().get_uint32());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700136
Paul Stewarte18c33b2012-07-10 20:48:44 -0700137 EXPECT_EQ(0, PropertyStoreTest::kUint64V.reader().get_uint64());
138 EXPECT_EQ(ex_uint64_, uint64_v_.reader().get_uint64());
139
Chris Masoneb925cc82011-06-22 15:39:57 -0700140 EXPECT_EQ(0, PropertyStoreTest::kInt32V.reader().get_int32());
141 EXPECT_EQ(ex_int32_, int32_v_.reader().get_int32());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700142
mukesh agrawal32399322011-09-01 10:53:43 -0700143 EXPECT_EQ(ex_path_, path_v_.reader().get_path());
144
mukesh agrawal2366eed2012-03-20 18:21:50 -0700145 EXPECT_EQ(ex_path_, paths_v_.operator vector< ::DBus::Path>()[0]);
mukesh agrawal32399322011-09-01 10:53:43 -0700146
Chris Masoneb925cc82011-06-22 15:39:57 -0700147 EXPECT_EQ(string(""), PropertyStoreTest::kStringV.reader().get_string());
148 EXPECT_EQ(ex_string_, string_v_.reader().get_string());
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700149
Chris Masoneb925cc82011-06-22 15:39:57 -0700150 EXPECT_EQ(ex_stringmap_[ex_string_],
151 (stringmap_v_.operator map<string, string>()[ex_string_]));
152 EXPECT_EQ(ex_strings_[0], strings_v_.operator vector<string>()[0]);
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700153}
154
155TEST_F(DBusAdaptorTest, Signatures) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700156 EXPECT_TRUE(DBusAdaptor::IsBool(bool_v_.signature()));
Paul Stewartced6a0b2011-11-08 15:32:04 -0800157 EXPECT_TRUE(DBusAdaptor::IsByteArrays(bytearrays_v_.signature()));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700158 EXPECT_TRUE(DBusAdaptor::IsByte(byte_v_.signature()));
159 EXPECT_TRUE(DBusAdaptor::IsInt16(int16_v_.signature()));
160 EXPECT_TRUE(DBusAdaptor::IsInt32(int32_v_.signature()));
mukesh agrawal32399322011-09-01 10:53:43 -0700161 EXPECT_TRUE(DBusAdaptor::IsPath(path_v_.signature()));
mukesh agrawal2366eed2012-03-20 18:21:50 -0700162 EXPECT_TRUE(DBusAdaptor::IsPaths(paths_v_.signature()));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700163 EXPECT_TRUE(DBusAdaptor::IsString(string_v_.signature()));
164 EXPECT_TRUE(DBusAdaptor::IsStringmap(stringmap_v_.signature()));
165 EXPECT_TRUE(DBusAdaptor::IsStrings(strings_v_.signature()));
166 EXPECT_TRUE(DBusAdaptor::IsUint16(uint16_v_.signature()));
167 EXPECT_TRUE(DBusAdaptor::IsUint32(uint32_v_.signature()));
Paul Stewarte18c33b2012-07-10 20:48:44 -0700168 EXPECT_TRUE(DBusAdaptor::IsUint64(uint64_v_.signature()));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700169
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700170 EXPECT_FALSE(DBusAdaptor::IsBool(byte_v_.signature()));
171 EXPECT_FALSE(DBusAdaptor::IsStrings(string_v_.signature()));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700172}
173
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800174TEST_F(DBusAdaptorTest, SetProperty) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700175 MockPropertyStore store;
Paul Stewarte18c33b2012-07-10 20:48:44 -0700176 ::DBus::Error e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700177
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700178 EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(true));
179 EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(true));
180 EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(true));
181 EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(true));
182 EXPECT_CALL(store, SetStringProperty("", _, _))
183 .WillOnce(Return(true));
184 EXPECT_CALL(store, SetStringmapProperty("", _, _))
185 .WillOnce(Return(true));
186 EXPECT_CALL(store, SetStringsProperty("", _, _))
187 .WillOnce(Return(true));
188 EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(true));
189 EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(true));
190 EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(true));
Paul Stewarte18c33b2012-07-10 20:48:44 -0700191 EXPECT_CALL(store, SetUint64Property("", _, _)).WillOnce(Return(true));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700192
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700193 string string_path("/false/path");
194 ::DBus::Path path(string_path);
195 ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path);
196 EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _))
197 .WillOnce(Return(true));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700198
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800199 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", bool_v_, &e1));
200 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", path_v, &e2));
201 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", string_v_, &e3));
202 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", strings_v_, &e4));
203 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", int16_v_, &e5));
204 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", int32_v_, &e6));
205 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint16_v_, &e7));
206 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint32_v_, &e8));
Paul Stewarte18c33b2012-07-10 20:48:44 -0700207 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint64_v_, &e9));
208 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", stringmap_v_, &e10));
209 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", byte_v_, &e11));
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700210}
211
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800212void SetError(const string &/*name*/, Error *error) {
213 error->Populate(Error::kInvalidProperty);
214}
215
216TEST_F(DBusAdaptorTest, ClearProperty) {
217 MockPropertyStore store;
218 ::DBus::Error e1, e2;
219
220 EXPECT_CALL(store, ClearProperty("valid property", _)).
221 WillOnce(Return(true));
222 EXPECT_CALL(store, ClearProperty("invalid property", _)).
223 WillOnce(DoAll(Invoke(SetError),
224 Return(false)));
225 EXPECT_TRUE(DBusAdaptor::ClearProperty(&store, "valid property", &e1));
226 EXPECT_FALSE(DBusAdaptor::ClearProperty(&store, "invalid property", &e2));
227
228}
229
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700230TEST_F(DBusAdaptorTest, ArgsToKeyValueStore) {
Darin Petkov25665aa2012-05-21 14:08:12 +0200231 map<string, ::DBus::Variant> args;
232 KeyValueStore args_kv;
233 Error error;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700234
Darin Petkov25665aa2012-05-21 14:08:12 +0200235 args["string_arg"].writer().append_string("string");
236 args["bool_arg"].writer().append_bool(true);
237 DBusAdaptor::ArgsToKeyValueStore(args, &args_kv, &error);
238 EXPECT_TRUE(error.IsSuccess());
239 EXPECT_EQ("string", args_kv.GetString("string_arg"));
240 EXPECT_EQ(true, args_kv.GetBool("bool_arg"));
241}
242
243TEST_F(DBusAdaptorTest, KeyValueStoreToVariant) {
244 static const char kStringKey[] = "StringKey";
245 static const char kStringValue[] = "StringValue";
246 static const char kBoolKey[] = "BoolKey";
247 const bool kBoolValue = true;
248 KeyValueStore store;
249 store.SetString(kStringKey, kStringValue);
250 store.SetBool(kBoolKey, kBoolValue);
251 DBus::Variant var = DBusAdaptor::KeyValueStoreToVariant(store);
252 ASSERT_TRUE(DBusAdaptor::IsKeyValueStore(var.signature()));
253 DBusPropertiesMap props = var.operator DBusPropertiesMap();
254 // Sanity test the result.
255 EXPECT_EQ(2, props.size());
256 string string_value;
257 EXPECT_TRUE(DBusProperties::GetString(props, kStringKey, &string_value));
258 EXPECT_EQ(kStringValue, string_value);
259 bool bool_value = !kBoolValue;
260 EXPECT_TRUE(DBusProperties::GetBool(props, kBoolKey, &bool_value));
261 EXPECT_EQ(kBoolValue, bool_value);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700262}
263
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700264} // namespace shill