blob: 289367a724ee7b7f8d8cedd17c3480e97242ad66 [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),
mukesh agrawale7c7e652013-06-18 17:19:39 -070044 ex_bytearrays_{ByteArray()},
Chris Masoneb925cc82011-06-22 15:39:57 -070045 ex_uint16_(65535),
mukesh agrawale7c7e652013-06-18 17:19:39 -070046 ex_uint16s_{ex_uint16_},
Chris Masoneb925cc82011-06-22 15:39:57 -070047 ex_uint32_(2000000),
Paul Stewarte18c33b2012-07-10 20:48:44 -070048 ex_uint64_(8589934591LL),
Chris Masoneb925cc82011-06-22 15:39:57 -070049 ex_int16_(-32768),
50 ex_int32_(-65536),
mukesh agrawal32399322011-09-01 10:53:43 -070051 ex_path_("/"),
mukesh agrawale7c7e652013-06-18 17:19:39 -070052 ex_paths_{ex_path_},
Chris Masoneb925cc82011-06-22 15:39:57 -070053 ex_string_("something"),
mukesh agrawale7c7e652013-06-18 17:19:39 -070054 ex_stringmap_{{ex_string_, ex_string_}},
55 ex_stringmaps_{ex_stringmap_},
Chris Masoneb925cc82011-06-22 15:39:57 -070056 ex_strings_(1, ex_string_),
57 bool_v_(DBusAdaptor::BoolToVariant(ex_bool_)),
58 byte_v_(DBusAdaptor::ByteToVariant(ex_byte_)),
mukesh agrawale7c7e652013-06-18 17:19:39 -070059 bytearrays_v_(DBusAdaptor::ByteArraysToVariant(ex_bytearrays_)),
Chris Masoneb925cc82011-06-22 15:39:57 -070060 int16_v_(DBusAdaptor::Int16ToVariant(ex_int16_)),
61 int32_v_(DBusAdaptor::Int32ToVariant(ex_int32_)),
mukesh agrawal32399322011-09-01 10:53:43 -070062 path_v_(DBusAdaptor::PathToVariant(ex_path_)),
mukesh agrawale7c7e652013-06-18 17:19:39 -070063 paths_v_(DBusAdaptor::PathsToVariant(ex_paths_)),
Chris Masoneb925cc82011-06-22 15:39:57 -070064 string_v_(DBusAdaptor::StringToVariant(ex_string_)),
mukesh agrawale7c7e652013-06-18 17:19:39 -070065 stringmap_v_(DBusAdaptor::StringmapToVariant(ex_stringmap_)),
66 stringmaps_v_(DBusAdaptor::StringmapsToVariant(ex_stringmaps_)),
Chris Masoneb925cc82011-06-22 15:39:57 -070067 strings_v_(DBusAdaptor::StringsToVariant(ex_strings_)),
mukesh agrawalf60e4062011-05-27 13:13:41 -070068 uint16_v_(DBusAdaptor::Uint16ToVariant(ex_uint16_)),
mukesh agrawale7c7e652013-06-18 17:19:39 -070069 uint16s_v_(DBusAdaptor::Uint16sToVariant(ex_uint16s_)),
mukesh agrawalf60e4062011-05-27 13:13:41 -070070 uint32_v_(DBusAdaptor::Uint32ToVariant(ex_uint32_)),
Paul Stewarte18c33b2012-07-10 20:48:44 -070071 uint64_v_(DBusAdaptor::Uint64ToVariant(ex_uint64_)),
Chris Masone2176a882011-09-14 22:29:15 -070072 device_(new MockDevice(control_interface(),
73 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080074 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -070075 manager(),
Chris Masone8fe2c7e2011-06-09 15:51:19 -070076 "mock",
Chris Masone626719f2011-08-18 16:58:48 -070077 "addr0",
Chris Masone8fe2c7e2011-06-09 15:51:19 -070078 0)),
Chris Masone2176a882011-09-14 22:29:15 -070079 service_(new MockService(control_interface(),
80 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080081 metrics(),
mukesh agrawale7c7e652013-06-18 17:19:39 -070082 manager())) {}
Chris Masone8fe2c7e2011-06-09 15:51:19 -070083
Chris Masoned0ceb8c2011-06-02 10:05:39 -070084 virtual ~DBusAdaptorTest() {}
Chris Masone8fe2c7e2011-06-09 15:51:19 -070085
Chris Masone8fe2c7e2011-06-09 15:51:19 -070086 protected:
Chris Masoneb925cc82011-06-22 15:39:57 -070087 bool ex_bool_;
Ben Chan7fab8972014-08-10 17:14:46 -070088 uint8_t ex_byte_;
Paul Stewartced6a0b2011-11-08 15:32:04 -080089 ByteArrays ex_bytearrays_;
Ben Chan7fab8972014-08-10 17:14:46 -070090 uint16_t ex_uint16_;
91 vector<uint16_t> ex_uint16s_;
92 uint32_t ex_uint32_;
93 uint64_t ex_uint64_;
94 int16_t ex_int16_;
95 int32_t ex_int32_;
mukesh agrawal32399322011-09-01 10:53:43 -070096 ::DBus::Path ex_path_;
Ben Chane2ee5e02014-09-19 19:29:42 -070097 vector<::DBus::Path> ex_paths_;
Chris Masoneb925cc82011-06-22 15:39:57 -070098 string ex_string_;
99 map<string, string> ex_stringmap_;
Ben Chane2ee5e02014-09-19 19:29:42 -0700100 vector<map<string, string>> ex_stringmaps_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700101 vector<string> ex_strings_;
102
103 ::DBus::Variant bool_v_;
104 ::DBus::Variant byte_v_;
Paul Stewartced6a0b2011-11-08 15:32:04 -0800105 ::DBus::Variant bytearrays_v_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700106 ::DBus::Variant int16_v_;
107 ::DBus::Variant int32_v_;
mukesh agrawal32399322011-09-01 10:53:43 -0700108 ::DBus::Variant path_v_;
mukesh agrawal2366eed2012-03-20 18:21:50 -0700109 ::DBus::Variant paths_v_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700110 ::DBus::Variant string_v_;
111 ::DBus::Variant stringmap_v_;
112 ::DBus::Variant stringmaps_v_;
113 ::DBus::Variant strings_v_;
114 ::DBus::Variant uint16_v_;
mukesh agrawale7c7e652013-06-18 17:19:39 -0700115 ::DBus::Variant uint16s_v_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700116 ::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
Ben Chane2ee5e02014-09-19 19:29:42 -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_,
Ben Chane2ee5e02014-09-19 19:29:42 -0700160 ContainerEq(stringmaps_v_.operator vector<map<string, string>>()));
mukesh agrawale7c7e652013-06-18 17:19:39 -0700161 EXPECT_THAT(ex_uint16s_,
Ben Chan7fab8972014-08-10 17:14:46 -0700162 ContainerEq(uint16s_v_.operator vector<uint16_t>()));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700163}
164
165TEST_F(DBusAdaptorTest, Signatures) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700166 EXPECT_TRUE(DBusAdaptor::IsBool(bool_v_.signature()));
Paul Stewartced6a0b2011-11-08 15:32:04 -0800167 EXPECT_TRUE(DBusAdaptor::IsByteArrays(bytearrays_v_.signature()));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700168 EXPECT_TRUE(DBusAdaptor::IsByte(byte_v_.signature()));
169 EXPECT_TRUE(DBusAdaptor::IsInt16(int16_v_.signature()));
170 EXPECT_TRUE(DBusAdaptor::IsInt32(int32_v_.signature()));
mukesh agrawal32399322011-09-01 10:53:43 -0700171 EXPECT_TRUE(DBusAdaptor::IsPath(path_v_.signature()));
mukesh agrawal2366eed2012-03-20 18:21:50 -0700172 EXPECT_TRUE(DBusAdaptor::IsPaths(paths_v_.signature()));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700173 EXPECT_TRUE(DBusAdaptor::IsString(string_v_.signature()));
174 EXPECT_TRUE(DBusAdaptor::IsStringmap(stringmap_v_.signature()));
175 EXPECT_TRUE(DBusAdaptor::IsStrings(strings_v_.signature()));
176 EXPECT_TRUE(DBusAdaptor::IsUint16(uint16_v_.signature()));
mukesh agrawale7c7e652013-06-18 17:19:39 -0700177 EXPECT_TRUE(DBusAdaptor::IsUint16s(uint16s_v_.signature()));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700178 EXPECT_TRUE(DBusAdaptor::IsUint32(uint32_v_.signature()));
Paul Stewarte18c33b2012-07-10 20:48:44 -0700179 EXPECT_TRUE(DBusAdaptor::IsUint64(uint64_v_.signature()));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700180
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700181 EXPECT_FALSE(DBusAdaptor::IsBool(byte_v_.signature()));
182 EXPECT_FALSE(DBusAdaptor::IsStrings(string_v_.signature()));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700183}
184
mukesh agrawale7c7e652013-06-18 17:19:39 -0700185template <typename T>
186class DBusAdaptorTypedTest : public DBusAdaptorTest {
mukesh agrawale7c7e652013-06-18 17:19:39 -0700187 protected:
Paul Stewart3b30ca52015-06-16 13:13:10 -0700188 T* property() { return &property_; }
mukesh agrawale7c7e652013-06-18 17:19:39 -0700189
190 private:
Alex Vakulenko8a532292014-06-16 17:18:44 -0700191 T property_{}; // value-initialize primitives
mukesh agrawale7c7e652013-06-18 17:19:39 -0700192};
193
194TYPED_TEST_CASE(DBusAdaptorTypedTest, PropertyStoreTest::PropertyTypes);
195
196TYPED_TEST(DBusAdaptorTypedTest, GetProperties) {
197 MockPropertyStore store;
198 const string kPropertyName("some property");
199 PropertyStoreTest::RegisterProperty(&store, kPropertyName, this->property());
200
201 map<string, ::DBus::Variant> properties;
202 ::DBus::Error error;
203 EXPECT_TRUE(DBusAdaptor::GetProperties(store, &properties, &error));
204 EXPECT_TRUE(ContainsKey(properties, kPropertyName));
205}
206
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800207TEST_F(DBusAdaptorTest, SetProperty) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700208 MockPropertyStore store;
mukesh agrawale7c7e652013-06-18 17:19:39 -0700209 ::DBus::Error e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700210
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700211 EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(true));
212 EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(true));
213 EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(true));
214 EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(true));
215 EXPECT_CALL(store, SetStringProperty("", _, _))
216 .WillOnce(Return(true));
217 EXPECT_CALL(store, SetStringmapProperty("", _, _))
218 .WillOnce(Return(true));
219 EXPECT_CALL(store, SetStringsProperty("", _, _))
220 .WillOnce(Return(true));
221 EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(true));
222 EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(true));
mukesh agrawale7c7e652013-06-18 17:19:39 -0700223 EXPECT_CALL(store, SetUint16sProperty("", _, _)).WillOnce(Return(true));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700224 EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(true));
Paul Stewarte18c33b2012-07-10 20:48:44 -0700225 EXPECT_CALL(store, SetUint64Property("", _, _)).WillOnce(Return(true));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700226
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700227 string string_path("/false/path");
228 ::DBus::Path path(string_path);
229 ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path);
230 EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _))
231 .WillOnce(Return(true));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700232
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800233 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", bool_v_, &e1));
234 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", path_v, &e2));
235 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", string_v_, &e3));
236 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", strings_v_, &e4));
237 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", int16_v_, &e5));
238 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", int32_v_, &e6));
239 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint16_v_, &e7));
mukesh agrawale7c7e652013-06-18 17:19:39 -0700240 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint16s_v_, &e8));
241 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint32_v_, &e9));
242 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", uint64_v_, &e10));
243 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", stringmap_v_, &e11));
244 EXPECT_TRUE(DBusAdaptor::SetProperty(&store, "", byte_v_, &e12));
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700245}
246
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700247// SetProperty method should propagate failures. This should happen
248// even if error isn't set. (This is to accomodate the fact that, if
249// the property already has the desired value, the store will return
250// false, without setting an error.)
251TEST_F(DBusAdaptorTest, SetPropertyFailure) {
252 MockPropertyStore store;
mukesh agrawale7c7e652013-06-18 17:19:39 -0700253 ::DBus::Error e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12;
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700254
255 EXPECT_CALL(store, Contains(_)).WillRepeatedly(Return(false));
256 EXPECT_CALL(store, SetBoolProperty("", _, _)).WillOnce(Return(false));
257 EXPECT_CALL(store, SetInt16Property("", _, _)).WillOnce(Return(false));
258 EXPECT_CALL(store, SetInt32Property("", _, _)).WillOnce(Return(false));
259 EXPECT_CALL(store, SetStringProperty("", _, _))
260 .WillOnce(Return(false));
261 EXPECT_CALL(store, SetStringmapProperty("", _, _))
262 .WillOnce(Return(false));
263 EXPECT_CALL(store, SetStringsProperty("", _, _))
264 .WillOnce(Return(false));
265 EXPECT_CALL(store, SetUint8Property("", _, _)).WillOnce(Return(false));
266 EXPECT_CALL(store, SetUint16Property("", _, _)).WillOnce(Return(false));
mukesh agrawale7c7e652013-06-18 17:19:39 -0700267 EXPECT_CALL(store, SetUint16sProperty("", _, _)).WillOnce(Return(false));
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700268 EXPECT_CALL(store, SetUint32Property("", _, _)).WillOnce(Return(false));
269 EXPECT_CALL(store, SetUint64Property("", _, _)).WillOnce(Return(false));
270
271 string string_path("/false/path");
272 ::DBus::Path path(string_path);
273 ::DBus::Variant path_v = DBusAdaptor::PathToVariant(path);
274 EXPECT_CALL(store, SetStringProperty("", StrEq(string_path), _))
275 .WillOnce(Return(false));
276
277 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", bool_v_, &e1));
278 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", path_v, &e2));
279 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", string_v_, &e3));
280 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", strings_v_, &e4));
281 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", int16_v_, &e5));
282 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", int32_v_, &e6));
283 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint16_v_, &e7));
mukesh agrawale7c7e652013-06-18 17:19:39 -0700284 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint16s_v_, &e8));
285 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint32_v_, &e9));
286 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", uint64_v_, &e10));
287 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", stringmap_v_, &e11));
288 EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", byte_v_, &e12));
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700289}
290
Paul Stewart3b30ca52015-06-16 13:13:10 -0700291void SetError(const string& /*name*/, Error* error) {
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800292 error->Populate(Error::kInvalidProperty);
293}
294
295TEST_F(DBusAdaptorTest, ClearProperty) {
296 MockPropertyStore store;
297 ::DBus::Error e1, e2;
298
299 EXPECT_CALL(store, ClearProperty("valid property", _)).
300 WillOnce(Return(true));
301 EXPECT_CALL(store, ClearProperty("invalid property", _)).
302 WillOnce(DoAll(Invoke(SetError),
303 Return(false)));
304 EXPECT_TRUE(DBusAdaptor::ClearProperty(&store, "valid property", &e1));
305 EXPECT_FALSE(DBusAdaptor::ClearProperty(&store, "invalid property", &e2));
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800306}
307
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700308TEST_F(DBusAdaptorTest, ArgsToKeyValueStore) {
Darin Petkov25665aa2012-05-21 14:08:12 +0200309 map<string, ::DBus::Variant> args;
310 KeyValueStore args_kv;
311 Error error;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700312
Paul Stewart99dc9f32013-06-27 07:39:25 -0700313 const bool kBool = true;
314 const char kBoolKey[] = "bool_arg";
315 args[kBoolKey].writer().append_bool(kBool);
Ben Chan7fab8972014-08-10 17:14:46 -0700316 const int32_t kInt32 = 123;
Paul Stewart48231832013-07-17 15:46:19 -0700317 const char kInt32Key[] = "int32_arg";
318 args[kInt32Key].writer().append_int32(kInt32);
Paul Stewart99dc9f32013-06-27 07:39:25 -0700319 const char kString[] = "string";
320 const char kStringKey[] = "string_arg";
321 args[kStringKey].writer().append_string(kString);
Arman Uguray631c7e42013-07-30 16:41:12 -0700322 const map<string, string> kStringmap{ { "key0", "value0" } };
323 const char kStringmapKey[] = "stringmap_key";
324 DBus::MessageIter writer = args[kStringmapKey].writer();
325 writer << kStringmap;
Paul Stewart99dc9f32013-06-27 07:39:25 -0700326 const vector<string> kStrings{ "string0", "string1" };
327 const char kStringsKey[] = "strings_key";
Arman Uguray631c7e42013-07-30 16:41:12 -0700328 writer = args[kStringsKey].writer();
Paul Stewart99dc9f32013-06-27 07:39:25 -0700329 writer << kStrings;
Paul Stewartbb833562015-01-21 23:30:46 -0800330 map<string, ::DBus::Variant> variant_map;
331 const char kVariantMapSubKey[] = "map_sub_key";
332 variant_map[kVariantMapSubKey] = DBusAdaptor::BoolToVariant(true);
333 const char kVariantMapKey[] = "map_key";
334 writer = args[kVariantMapKey].writer();
335 writer << variant_map;
Darin Petkov25665aa2012-05-21 14:08:12 +0200336 DBusAdaptor::ArgsToKeyValueStore(args, &args_kv, &error);
337 EXPECT_TRUE(error.IsSuccess());
Paul Stewart99dc9f32013-06-27 07:39:25 -0700338 EXPECT_EQ(kBool, args_kv.GetBool(kBoolKey));
Paul Stewart48231832013-07-17 15:46:19 -0700339 EXPECT_EQ(kInt32, args_kv.GetInt(kInt32Key));
Paul Stewart99dc9f32013-06-27 07:39:25 -0700340 EXPECT_EQ(kString, args_kv.GetString(kStringKey));
Arman Uguray631c7e42013-07-30 16:41:12 -0700341 EXPECT_EQ(kStringmap, args_kv.GetStringmap(kStringmapKey));
Paul Stewart99dc9f32013-06-27 07:39:25 -0700342 EXPECT_EQ(kStrings, args_kv.GetStrings(kStringsKey));
Paul Stewartbb833562015-01-21 23:30:46 -0800343 KeyValueStore property_map;
344 property_map.SetBool(kVariantMapSubKey, true);
345 EXPECT_TRUE(property_map.Equals(args_kv.GetKeyValueStore(kVariantMapKey)));
Darin Petkov25665aa2012-05-21 14:08:12 +0200346}
347
348TEST_F(DBusAdaptorTest, KeyValueStoreToVariant) {
Arman Uguray631c7e42013-07-30 16:41:12 -0700349 const char kStringmapKey[] = "StringmapKey";
350 const map<string, string> kStringmapValue{ { "key", "value" } };
351 const char kStringsKey[] = "StringsKey";
352 const vector<string> kStringsValue{ "string0", "string1" };
353 const char kStringKey[] = "StringKey";
354 const char kStringValue[] = "StringValue";
355 const char kBoolKey[] = "BoolKey";
Darin Petkov25665aa2012-05-21 14:08:12 +0200356 const bool kBoolValue = true;
357 KeyValueStore store;
Arman Uguray631c7e42013-07-30 16:41:12 -0700358 store.SetStringmap(kStringmapKey, kStringmapValue);
359 store.SetStrings(kStringsKey, kStringsValue);
Darin Petkov25665aa2012-05-21 14:08:12 +0200360 store.SetString(kStringKey, kStringValue);
361 store.SetBool(kBoolKey, kBoolValue);
362 DBus::Variant var = DBusAdaptor::KeyValueStoreToVariant(store);
363 ASSERT_TRUE(DBusAdaptor::IsKeyValueStore(var.signature()));
364 DBusPropertiesMap props = var.operator DBusPropertiesMap();
365 // Sanity test the result.
Arman Uguray631c7e42013-07-30 16:41:12 -0700366 EXPECT_EQ(4, props.size());
367 map<string, string> stringmap_value;
368 EXPECT_TRUE(
369 DBusProperties::GetStringmap(props, kStringmapKey, &stringmap_value));
370 EXPECT_EQ(kStringmapValue, stringmap_value);
371 vector<string> strings_value;
372 EXPECT_TRUE(DBusProperties::GetStrings(props, kStringsKey, &strings_value));
373 EXPECT_EQ(kStringsValue, strings_value);
Darin Petkov25665aa2012-05-21 14:08:12 +0200374 string string_value;
375 EXPECT_TRUE(DBusProperties::GetString(props, kStringKey, &string_value));
376 EXPECT_EQ(kStringValue, string_value);
377 bool bool_value = !kBoolValue;
378 EXPECT_TRUE(DBusProperties::GetBool(props, kBoolKey, &bool_value));
379 EXPECT_EQ(kBoolValue, bool_value);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700380}
381
Paul Stewartf3b38402015-01-16 12:59:03 -0800382TEST_F(DBusAdaptorTest, SanitizePathElement) {
383 EXPECT_EQ("0Ab_y_Z_9_", DBusAdaptor::SanitizePathElement("0Ab/y:Z`9{"));
384 EXPECT_EQ("aB_f_0_Y_z", DBusAdaptor::SanitizePathElement("aB-f/0@Y[z"));
385}
386
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700387} // namespace shill