blob: a9aac6bc44d70978fcb3160cc5a967609ab5dd7f [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
5#include <gtest/gtest.h>
6
7#include "shill/dbus_properties.h"
8
9using std::string;
Jason Glasgow9c09e362012-04-18 15:16:29 -040010using std::vector;
Darin Petkove0a312e2011-07-20 13:45:28 -070011using testing::Test;
12
13namespace shill {
14
15class DBusPropertiesTest : public Test {
16};
17
Jason Glasgow9c09e362012-04-18 15:16:29 -040018TEST_F(DBusPropertiesTest, GetObjectPath) {
19 static const char kTestProperty[] = "RandomKey";
20 static const char kTestValue[] = "/random/path";
21 static const char kOldValue[] = "/random/oldpath";
22 string value = kOldValue;
23 DBusPropertiesMap props;
24 EXPECT_FALSE(DBusProperties::GetObjectPath(props, kTestProperty, &value));
25 EXPECT_EQ(kOldValue, value);
26 props[kTestProperty].writer().append_path(kTestValue);
27 EXPECT_TRUE(DBusProperties::GetObjectPath(props, kTestProperty, &value));
28 EXPECT_EQ(kTestValue, value);
29}
30
Darin Petkove0a312e2011-07-20 13:45:28 -070031TEST_F(DBusPropertiesTest, GetString) {
32 static const char kTestProperty[] = "RandomKey";
33 static const char kTestValue[] = "random-value";
34 static const char kOldValue[] = "old-value";
35 string value = kOldValue;
36 DBusPropertiesMap props;
37 EXPECT_FALSE(DBusProperties::GetString(props, kTestProperty, &value));
38 EXPECT_EQ(kOldValue, value);
39 props[kTestProperty].writer().append_string(kTestValue);
40 EXPECT_TRUE(DBusProperties::GetString(props, kTestProperty, &value));
41 EXPECT_EQ(kTestValue, value);
42}
43
Jason Glasgow9c09e362012-04-18 15:16:29 -040044TEST_F(DBusPropertiesTest, GetStrings) {
45 static const char kTestProperty[] = "RandomKey";
46 static const char kTestValue[] = "random-value";
47 static const char kOldValue[] = "old-value";
48 vector<string> test_values;
49 test_values.push_back(kTestValue);
50 test_values.push_back(kTestValue);
51
52 vector<string> values;
53 DBusPropertiesMap props;
54 EXPECT_FALSE(DBusProperties::GetStrings(props, kTestProperty, &values));
55 EXPECT_EQ(0, values.size());
56 values.push_back(kOldValue);
57 EXPECT_FALSE(DBusProperties::GetStrings(props, kTestProperty, &values));
58 EXPECT_EQ(1, values.size());
59 EXPECT_EQ(kOldValue, values[0]);
60 ::DBus::Variant v;
61 ::DBus::MessageIter writer = v.writer();
62 writer << test_values;
63 props[kTestProperty] = v;
64 EXPECT_TRUE(DBusProperties::GetStrings(props, kTestProperty, &values));
65 EXPECT_EQ(2, values.size());
66 EXPECT_EQ(kTestValue, values[0]);
67 EXPECT_EQ(kTestValue, values[1]);
68}
69
Darin Petkove0a312e2011-07-20 13:45:28 -070070TEST_F(DBusPropertiesTest, GetUint32) {
71 static const char kTestProperty[] = "AKey";
72 const uint32 kTestValue = 35;
73 const uint32 kOldValue = 74;
74 uint32 value = kOldValue;
75 DBusPropertiesMap props;
76 EXPECT_FALSE(DBusProperties::GetUint32(props, kTestProperty, &value));
77 EXPECT_EQ(kOldValue, value);
78 props[kTestProperty].writer().append_uint32(kTestValue);
79 EXPECT_TRUE(DBusProperties::GetUint32(props, kTestProperty, &value));
80 EXPECT_EQ(kTestValue, value);
81}
82
Darin Petkovceb68172011-07-29 14:47:48 -070083TEST_F(DBusPropertiesTest, GetUint16) {
84 static const char kTestProperty[] = "version";
85 const uint16 kTestValue = 77;
86 const uint16 kOldValue = 88;
87 uint16 value = kOldValue;
88 DBusPropertiesMap props;
89 EXPECT_FALSE(DBusProperties::GetUint16(props, kTestProperty, &value));
90 EXPECT_EQ(kOldValue, value);
91 props[kTestProperty].writer().append_uint16(kTestValue);
92 EXPECT_TRUE(DBusProperties::GetUint16(props, kTestProperty, &value));
93 EXPECT_EQ(kTestValue, value);
94}
95
Darin Petkove4b27022012-05-16 13:28:50 +020096TEST_F(DBusPropertiesTest, GetRpcIdentifiers) {
97 static const char kTestProperty[] = "paths";
98 static const char kOldTestPath0[] = "/org/chromium/Something/Old";
99 static const char kOldTestPath1[] = "/org/chromium/Else/Old";
100 static const char kTestPath0[] = "/org/chromium/Something";
101 static const char kTestPath1[] = "/org/chromium/Else";
102
Darin Petkov9893d9c2012-05-17 15:27:31 -0700103 RpcIdentifiers value;
Darin Petkove4b27022012-05-16 13:28:50 +0200104 value.push_back(kOldTestPath0);
105 value.push_back(kOldTestPath1);
106 DBusPropertiesMap props;
107 EXPECT_FALSE(DBusProperties::GetRpcIdentifiers(props, kTestProperty, &value));
108 ASSERT_EQ(2, value.size());
109 EXPECT_EQ(kOldTestPath0, value[0]);
110 EXPECT_EQ(kOldTestPath1, value[1]);
111
112 vector<DBus::Path> paths;
113 paths.push_back(kTestPath0);
114 paths.push_back(kTestPath1);
115 DBus::MessageIter writer = props[kTestProperty].writer();
116 writer << paths;
117 EXPECT_TRUE(DBusProperties::GetRpcIdentifiers(props, kTestProperty, &value));
118 ASSERT_EQ(2, value.size());
119 EXPECT_EQ(kTestPath0, value[0]);
120 EXPECT_EQ(kTestPath1, value[1]);
121}
122
Darin Petkov9893d9c2012-05-17 15:27:31 -0700123TEST_F(DBusPropertiesTest, ConvertPathsToRpcIdentifiers) {
124 static const char kOldTestPath[] = "/org/chromium/Something/Old";
125 static const char kTestPath0[] = "/org/chromium/Something";
126 static const char kTestPath1[] = "/org/chromium/Else";
127
128 RpcIdentifiers ids(1, kOldTestPath);
129 vector<DBus::Path> paths;
130 paths.push_back(kTestPath0);
131 paths.push_back(kTestPath1);
132 DBusProperties::ConvertPathsToRpcIdentifiers(paths, &ids);
133 ASSERT_EQ(2, ids.size());
134 EXPECT_EQ(kTestPath0, ids[0]);
135 EXPECT_EQ(kTestPath1, ids[1]);
136}
137
Darin Petkov25665aa2012-05-21 14:08:12 +0200138TEST_F(DBusPropertiesTest, ConvertKeyValueStoreToMap) {
139 static const char kStringKey[] = "StringKey";
140 static const char kStringValue[] = "StringValue";
141 static const char kBoolKey[] = "BoolKey";
142 const bool kBoolValue = true;
143 static const char kInt32Key[] = "Int32Key";
144 const int32 kInt32Value = 123;
145 static const char kUint32Key[] = "Uint32Key";
146 const uint32 kUint32Value = 654;
147 KeyValueStore store;
148 store.SetString(kStringKey, kStringValue);
149 store.SetBool(kBoolKey, kBoolValue);
150 store.SetInt(kInt32Key, kInt32Value);
151 store.SetUint(kUint32Key, kUint32Value);
152 DBusPropertiesMap props;
153 props["RandomKey"].writer().append_string("RandomValue");
154 DBusProperties::ConvertKeyValueStoreToMap(store, &props);
155 EXPECT_EQ(4, props.size());
156 string string_value;
157 EXPECT_TRUE(DBusProperties::GetString(props, kStringKey, &string_value));
158 EXPECT_EQ(kStringValue, string_value);
159 bool bool_value = !kBoolValue;
160 EXPECT_TRUE(DBusProperties::GetBool(props, kBoolKey, &bool_value));
161 EXPECT_EQ(kBoolValue, bool_value);
162 int32 int32_value = ~kInt32Value;
163 EXPECT_TRUE(DBusProperties::GetInt32(props, kInt32Key, &int32_value));
164 EXPECT_EQ(kInt32Value, int32_value);
165 uint32 uint32_value = ~kUint32Value;
166 EXPECT_TRUE(DBusProperties::GetUint32(props, kUint32Key, &uint32_value));
167 EXPECT_EQ(kUint32Value, uint32_value);
168}
169
Darin Petkove0a312e2011-07-20 13:45:28 -0700170} // namespace shill