blob: 9186d22ae53a3e778edbe4160d82d3d36baec41a [file] [log] [blame]
Darin Petkove4b27022012-05-16 13:28:50 +02001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkove0a312e2011-07-20 13:45:28 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkove0a312e2011-07-20 13:45:28 -07005#include "shill/dbus_properties.h"
6
Ben Chan0a3a3a52013-07-10 11:34:07 -07007#include <limits>
8
9#include <gtest/gtest.h>
10
Arman Uguray631c7e42013-07-30 16:41:12 -070011using std::map;
Ben Chan0a3a3a52013-07-10 11:34:07 -070012using std::numeric_limits;
Darin Petkove0a312e2011-07-20 13:45:28 -070013using std::string;
Jason Glasgow9c09e362012-04-18 15:16:29 -040014using std::vector;
Darin Petkove0a312e2011-07-20 13:45:28 -070015using testing::Test;
16
17namespace shill {
18
Ben Chan0a3a3a52013-07-10 11:34:07 -070019class DBusPropertiesTest : public Test {};
Darin Petkove4b27022012-05-16 13:28:50 +020020
Darin Petkov9893d9c2012-05-17 15:27:31 -070021TEST_F(DBusPropertiesTest, ConvertPathsToRpcIdentifiers) {
22 static const char kOldTestPath[] = "/org/chromium/Something/Old";
23 static const char kTestPath0[] = "/org/chromium/Something";
24 static const char kTestPath1[] = "/org/chromium/Else";
25
26 RpcIdentifiers ids(1, kOldTestPath);
27 vector<DBus::Path> paths;
28 paths.push_back(kTestPath0);
29 paths.push_back(kTestPath1);
30 DBusProperties::ConvertPathsToRpcIdentifiers(paths, &ids);
31 ASSERT_EQ(2, ids.size());
32 EXPECT_EQ(kTestPath0, ids[0]);
33 EXPECT_EQ(kTestPath1, ids[1]);
34}
35
Darin Petkov25665aa2012-05-21 14:08:12 +020036TEST_F(DBusPropertiesTest, ConvertKeyValueStoreToMap) {
37 static const char kStringKey[] = "StringKey";
38 static const char kStringValue[] = "StringValue";
Arman Uguray631c7e42013-07-30 16:41:12 -070039 static const char kStringmapKey[] = "StringmapKey";
40 const map<string, string> kStringmapValue = { { "key", "value" } };
Ben Chan0a3a3a52013-07-10 11:34:07 -070041 static const char kStringsKey[] = "StringsKey";
42 const vector<string> kStringsValue = {"StringsValue1", "StringsValue2"};
Darin Petkov25665aa2012-05-21 14:08:12 +020043 static const char kBoolKey[] = "BoolKey";
44 const bool kBoolValue = true;
45 static const char kInt32Key[] = "Int32Key";
Ben Chan7fab8972014-08-10 17:14:46 -070046 const int32_t kInt32Value = 123;
Darin Petkov25665aa2012-05-21 14:08:12 +020047 static const char kUint32Key[] = "Uint32Key";
Ben Chan7fab8972014-08-10 17:14:46 -070048 const uint32_t kUint32Value = 654;
Darin Petkov25665aa2012-05-21 14:08:12 +020049 KeyValueStore store;
50 store.SetString(kStringKey, kStringValue);
Arman Uguray631c7e42013-07-30 16:41:12 -070051 store.SetStringmap(kStringmapKey, kStringmapValue);
Ben Chan0a3a3a52013-07-10 11:34:07 -070052 store.SetStrings(kStringsKey, kStringsValue);
Darin Petkov25665aa2012-05-21 14:08:12 +020053 store.SetBool(kBoolKey, kBoolValue);
54 store.SetInt(kInt32Key, kInt32Value);
55 store.SetUint(kUint32Key, kUint32Value);
56 DBusPropertiesMap props;
57 props["RandomKey"].writer().append_string("RandomValue");
58 DBusProperties::ConvertKeyValueStoreToMap(store, &props);
Arman Uguray631c7e42013-07-30 16:41:12 -070059 EXPECT_EQ(6, props.size());
Darin Petkov25665aa2012-05-21 14:08:12 +020060 string string_value;
61 EXPECT_TRUE(DBusProperties::GetString(props, kStringKey, &string_value));
62 EXPECT_EQ(kStringValue, string_value);
Arman Uguray631c7e42013-07-30 16:41:12 -070063 map<string, string> stringmap_value;
64 EXPECT_TRUE(
65 DBusProperties::GetStringmap(props, kStringmapKey, &stringmap_value));
66 EXPECT_EQ(kStringmapValue, stringmap_value);
Ben Chan0a3a3a52013-07-10 11:34:07 -070067 vector<string> strings_value;
68 EXPECT_TRUE(DBusProperties::GetStrings(props, kStringsKey, &strings_value));
69 EXPECT_EQ(kStringsValue, strings_value);
Darin Petkov25665aa2012-05-21 14:08:12 +020070 bool bool_value = !kBoolValue;
71 EXPECT_TRUE(DBusProperties::GetBool(props, kBoolKey, &bool_value));
72 EXPECT_EQ(kBoolValue, bool_value);
Ben Chan7fab8972014-08-10 17:14:46 -070073 int32_t int32_value = ~kInt32Value;
Darin Petkov25665aa2012-05-21 14:08:12 +020074 EXPECT_TRUE(DBusProperties::GetInt32(props, kInt32Key, &int32_value));
75 EXPECT_EQ(kInt32Value, int32_value);
Ben Chan7fab8972014-08-10 17:14:46 -070076 uint32_t uint32_value = ~kUint32Value;
Darin Petkov25665aa2012-05-21 14:08:12 +020077 EXPECT_TRUE(DBusProperties::GetUint32(props, kUint32Key, &uint32_value));
78 EXPECT_EQ(kUint32Value, uint32_value);
79}
80
Ben Chan0a3a3a52013-07-10 11:34:07 -070081template <typename T> class DBusPropertiesGetterTest : public Test {};
82
83template <typename DerivedT, typename ValueT, typename DBusT = ValueT>
84struct TestTraits {
85 typedef ValueT ValueType;
86 typedef DBusT DBusType;
Paul Stewart3b30ca52015-06-16 13:13:10 -070087 typedef bool (*GetterType)(const DBusPropertiesMap& properties,
88 const string& key,
89 ValueType* value);
Ben Chan0a3a3a52013-07-10 11:34:07 -070090
91 static DBusType GetTestValue() { return DerivedT::GetNewValue(); }
92
Paul Stewart3b30ca52015-06-16 13:13:10 -070093 static void CheckValueEqual(const ValueType& expected,
94 const ValueType& actual) {
Ben Chan0a3a3a52013-07-10 11:34:07 -070095 EXPECT_EQ(expected, actual);
96 }
97};
98
99struct BoolTestTraits : public TestTraits<BoolTestTraits, bool> {
100 static bool GetOldValue() { return false; }
101 static bool GetNewValue() { return true; }
102 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetBool;
103};
104
105template <typename DerivedT, typename ValueT>
106struct IntTestTraits : public TestTraits<DerivedT, ValueT> {
107 static ValueT GetOldValue() { return numeric_limits<ValueT>::min(); }
108 static ValueT GetNewValue() { return numeric_limits<ValueT>::max(); }
109};
110
Ben Chan7fab8972014-08-10 17:14:46 -0700111struct Int16TestTraits : public IntTestTraits<Int16TestTraits, int16_t> {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700112 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetInt16;
113};
114
Ben Chan7fab8972014-08-10 17:14:46 -0700115struct Int32TestTraits : public IntTestTraits<Int32TestTraits, int32_t> {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700116 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetInt32;
117};
118
Ben Chan7fab8972014-08-10 17:14:46 -0700119struct Int64TestTraits : public IntTestTraits<Int64TestTraits, int64_t> {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700120 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetInt64;
121};
122
Ben Chan7fab8972014-08-10 17:14:46 -0700123struct Uint8TestTraits : public IntTestTraits<Uint8TestTraits, uint8_t> {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700124 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetUint8;
125};
126
Ben Chan7fab8972014-08-10 17:14:46 -0700127struct Uint16TestTraits : public IntTestTraits<Uint16TestTraits, uint16_t> {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700128 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetUint16;
129};
130
Ben Chan7fab8972014-08-10 17:14:46 -0700131struct Uint32TestTraits : public IntTestTraits<Uint32TestTraits, uint32_t> {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700132 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetUint32;
133};
134
Ben Chan7fab8972014-08-10 17:14:46 -0700135struct Uint64TestTraits : public IntTestTraits<Uint64TestTraits, uint64_t> {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700136 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetUint64;
137};
138
139struct DoubleTestTraits : public TestTraits<DoubleTestTraits, double> {
140 static double GetOldValue() { return -1.1; }
141 static double GetNewValue() { return 3.14; }
142 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetDouble;
143};
144
145struct StringTestTraits : public TestTraits<StringTestTraits, string> {
146 static string GetOldValue() { return "old"; }
147 static string GetNewValue() { return "new"; }
148 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetString;
149};
150
Arman Uguray631c7e42013-07-30 16:41:12 -0700151struct StringmapTestTraits
152 : public TestTraits<StringmapTestTraits, map<string, string>> {
153 static map<string, string> GetOldValue() {
154 return { { "oldKey", "oldValue" } };
155 }
156 static map<string, string> GetNewValue() {
157 return { { "newKey", "newValue" } };
158 }
159 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetStringmap;
160};
161
Ben Chan0a3a3a52013-07-10 11:34:07 -0700162struct StringsTestTraits
163 : public TestTraits<StringsTestTraits, vector<string>> {
164 static vector<string> GetOldValue() { return {"1", "2", "3"}; }
165 static vector<string> GetNewValue() { return {"a", "b"}; }
166 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetStrings;
167};
168
169struct ObjectPathTestTraits
170 : public TestTraits<ObjectPathTestTraits, DBus::Path> {
171 static DBus::Path GetOldValue() { return "/old/path"; }
172 static DBus::Path GetNewValue() { return "/new/path"; }
173 static constexpr GetterType kMethodUnderTest = &DBusProperties::GetObjectPath;
174};
175
176struct RpcIdentifiersTestTraits : public TestTraits<RpcIdentifiersTestTraits,
177 RpcIdentifiers,
178 vector<DBus::Path>> {
179 static RpcIdentifiers GetOldValue() { return {"/device/1", "/service/2"}; }
180 static RpcIdentifiers GetNewValue() { return {"/obj/3", "/obj/4", "/obj/5"}; }
181 static vector<DBus::Path> GetTestValue() {
182 return {"/obj/3", "/obj/4", "/obj/5"};
183 }
184 static constexpr GetterType kMethodUnderTest =
185 &DBusProperties::GetRpcIdentifiers;
186};
187
188struct DBusPropertiesMapTestTraits
189 : public TestTraits<DBusPropertiesMapTestTraits, DBusPropertiesMap> {
190 static DBusPropertiesMap GetOldValue() {
191 DBusPropertiesMap value;
192 value["OldKey"].writer().append_bool(false);
193 return value;
194 }
195
196 static DBusPropertiesMap GetNewValue() {
197 DBusPropertiesMap value;
198 value["BoolKey"].writer().append_bool(true);
Ben Chan7fab8972014-08-10 17:14:46 -0700199 value["Int16Key"].writer().append_int16(numeric_limits<int16_t>::max());
200 value["Int32Key"].writer().append_int32(numeric_limits<int32_t>::max());
201 value["Int64Key"].writer().append_int64(numeric_limits<int64_t>::max());
202 value["Uint8Key"].writer().append_byte(numeric_limits<uint8_t>::max());
203 value["Uint16Key"].writer().append_uint16(numeric_limits<uint16_t>::max());
204 value["Uint32Key"].writer().append_uint32(numeric_limits<uint32_t>::max());
205 value["Uint64Key"].writer().append_uint64(numeric_limits<uint64_t>::max());
Ben Chan0a3a3a52013-07-10 11:34:07 -0700206 value["DoubleKey"].writer().append_double(3.14);
207 value["StringKey"].writer().append_string("new");
208 return value;
209 }
210
211 template <typename T>
Paul Stewart3b30ca52015-06-16 13:13:10 -0700212 static void CheckDBusVariantEqual(const string& key,
213 const DBus::Variant& expected,
214 const DBus::Variant& actual) {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700215 T expected_value = expected.operator T();
216 T actual_value = actual.operator T();
217 EXPECT_EQ(expected_value, actual_value) << "Value mismatch - key: " << key;
218 }
219
Paul Stewart3b30ca52015-06-16 13:13:10 -0700220 static void CheckValueEqual(const DBusPropertiesMap& expected,
221 const DBusPropertiesMap& actual) {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700222 ASSERT_EQ(expected.size(), actual.size()) << "Map size mismatch";
223
Paul Stewart3b30ca52015-06-16 13:13:10 -0700224 for (const auto& key_value_pair : expected) {
225 const string& key = key_value_pair.first;
Ben Chan0a3a3a52013-07-10 11:34:07 -0700226
Paul Stewart3b30ca52015-06-16 13:13:10 -0700227 const auto& actual_it = actual.find(key);
Ben Chan0a3a3a52013-07-10 11:34:07 -0700228 ASSERT_TRUE(actual_it != actual.end()) << "Key '" << key << "' not found";
229
Paul Stewart3b30ca52015-06-16 13:13:10 -0700230 const DBus::Variant& actual_value = actual_it->second;
231 const DBus::Variant& expected_value = key_value_pair.second;
Ben Chan0a3a3a52013-07-10 11:34:07 -0700232
233 string actual_signature = actual_value.signature();
234 string expected_signature = expected_value.signature();
235 ASSERT_EQ(expected_signature, actual_signature)
236 << "Value type mismatch - key: " << key;
237
238 if (expected_signature == DBus::type<bool>::sig()) {
239 CheckDBusVariantEqual<bool>(key, expected_value, actual_value);
Ben Chan7fab8972014-08-10 17:14:46 -0700240 } else if (expected_signature == DBus::type<int16_t>::sig()) {
241 CheckDBusVariantEqual<int16_t>(key, expected_value, actual_value);
242 } else if (expected_signature == DBus::type<int32_t>::sig()) {
243 CheckDBusVariantEqual<int32_t>(key, expected_value, actual_value);
244 } else if (expected_signature == DBus::type<int64_t>::sig()) {
245 CheckDBusVariantEqual<int64_t>(key, expected_value, actual_value);
246 } else if (expected_signature == DBus::type<uint8_t>::sig()) {
247 CheckDBusVariantEqual<uint8_t>(key, expected_value, actual_value);
248 } else if (expected_signature == DBus::type<uint16_t>::sig()) {
249 CheckDBusVariantEqual<uint16_t>(key, expected_value, actual_value);
250 } else if (expected_signature == DBus::type<uint32_t>::sig()) {
251 CheckDBusVariantEqual<uint32_t>(key, expected_value, actual_value);
252 } else if (expected_signature == DBus::type<uint64_t>::sig()) {
253 CheckDBusVariantEqual<uint64_t>(key, expected_value, actual_value);
Ben Chan0a3a3a52013-07-10 11:34:07 -0700254 } else if (expected_signature == DBus::type<double>::sig()) {
255 CheckDBusVariantEqual<double>(key, expected_value, actual_value);
256 } else if (expected_signature == DBus::type<string>::sig()) {
257 CheckDBusVariantEqual<string>(key, expected_value, actual_value);
258 } else if (expected_signature == DBus::type<DBus::Path>::sig()) {
259 CheckDBusVariantEqual<DBus::Path>(key, expected_value, actual_value);
260 } else {
261 // TODO(benchan): Comparison of other types is not yet implemented.
262 FAIL() << "Value type '" << expected_signature << "' not implemented.";
263 }
264 }
265 }
266
267 static constexpr GetterType kMethodUnderTest =
268 &DBusProperties::GetDBusPropertiesMap;
269};
270
271typedef testing::Types<BoolTestTraits,
272 Int16TestTraits,
273 Int32TestTraits,
274 Int64TestTraits,
275 Uint8TestTraits,
276 Uint16TestTraits,
277 Uint32TestTraits,
278 Uint64TestTraits,
279 DoubleTestTraits,
280 StringTestTraits,
Arman Uguray631c7e42013-07-30 16:41:12 -0700281 StringmapTestTraits,
Ben Chan0a3a3a52013-07-10 11:34:07 -0700282 StringsTestTraits,
283 ObjectPathTestTraits,
284 RpcIdentifiersTestTraits,
285 DBusPropertiesMapTestTraits> DBusPropertyValueTypes;
286TYPED_TEST_CASE(DBusPropertiesGetterTest, DBusPropertyValueTypes);
287
288TYPED_TEST(DBusPropertiesGetterTest, GetValue) {
289 static const char kTestProperty[] = "TestProperty";
290 const typename TypeParam::ValueType kOldValue = TypeParam::GetOldValue();
291 const typename TypeParam::ValueType kNewValue = TypeParam::GetNewValue();
292 const typename TypeParam::DBusType kTestValue = TypeParam::GetTestValue();
293 typename TypeParam::ValueType value = kOldValue;
294 DBusPropertiesMap properties;
295
296 // Property key is not found. |value| should remain the initial value.
297 EXPECT_FALSE(TypeParam::kMethodUnderTest(properties, kTestProperty, &value));
298 TypeParam::CheckValueEqual(kOldValue, value);
299
300 // Property value type mismatch. |value| should remain the initial value.
301 properties[kTestProperty].writer().append_fd(1);
302 EXPECT_FALSE(TypeParam::kMethodUnderTest(properties, kTestProperty, &value));
303 TypeParam::CheckValueEqual(kOldValue, value);
304
305 // Property key is found. |value| should be set to the test value.
306 properties.clear();
307 DBus::MessageIter writer = properties[kTestProperty].writer();
308 writer << kTestValue;
309 EXPECT_TRUE(TypeParam::kMethodUnderTest(properties, kTestProperty, &value));
310 TypeParam::CheckValueEqual(kNewValue, value);
311}
312
Darin Petkove0a312e2011-07-20 13:45:28 -0700313} // namespace shill