blob: 9dc0b31606f93eba3eab13ca8beeb1ea634f2fdc [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masoneb925cc82011-06-22 15:39:57 -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 "shill/property_store_unittest.h"
6
7#include <map>
8#include <string>
Chris Masone889666b2011-07-03 12:58:50 -07009#include <utility>
Chris Masoneb925cc82011-06-22 15:39:57 -070010#include <vector>
11
12#include <dbus-c++/dbus.h>
13#include <gtest/gtest.h>
14#include <gmock/gmock.h>
15
16#include "shill/dbus_adaptor.h"
17#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070018#include "shill/event_dispatcher.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070019#include "shill/manager.h"
20#include "shill/mock_control.h"
21#include "shill/property_store.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070022
Chris Masoneb925cc82011-06-22 15:39:57 -070023using std::map;
Chris Masone889666b2011-07-03 12:58:50 -070024using std::string;
Chris Masoneb925cc82011-06-22 15:39:57 -070025using std::vector;
Chris Masone8a56ad62011-07-02 15:27:57 -070026using ::testing::Values;
Chris Masoneb925cc82011-06-22 15:39:57 -070027
28namespace shill {
29
30// static
31const ::DBus::Variant PropertyStoreTest::kBoolV = DBusAdaptor::BoolToVariant(0);
32// static
33const ::DBus::Variant PropertyStoreTest::kByteV = DBusAdaptor::ByteToVariant(0);
34// static
35const ::DBus::Variant PropertyStoreTest::kInt16V =
36 DBusAdaptor::Int16ToVariant(0);
37// static
38const ::DBus::Variant PropertyStoreTest::kInt32V =
39 DBusAdaptor::Int32ToVariant(0);
40// static
Darin Petkov63138a92012-02-06 14:09:15 +010041const ::DBus::Variant PropertyStoreTest::kKeyValueStoreV =
42 DBusAdaptor::KeyValueStoreToVariant(KeyValueStore());
43// static
Chris Masoneb925cc82011-06-22 15:39:57 -070044const ::DBus::Variant PropertyStoreTest::kStringV =
45 DBusAdaptor::StringToVariant("");
46// static
47const ::DBus::Variant PropertyStoreTest::kStringmapV =
Chris Masonea8a2c252011-06-27 22:16:30 -070048 DBusAdaptor::StringmapToVariant(Stringmap());
Chris Masoneb925cc82011-06-22 15:39:57 -070049// static
50const ::DBus::Variant PropertyStoreTest::kStringmapsV =
Chris Masone889666b2011-07-03 12:58:50 -070051 DBusAdaptor::StringmapsToVariant(Stringmaps());
Chris Masoneb925cc82011-06-22 15:39:57 -070052// static
53const ::DBus::Variant PropertyStoreTest::kStringsV =
Chris Masonea8a2c252011-06-27 22:16:30 -070054 DBusAdaptor::StringsToVariant(Strings(1, ""));
Chris Masoneb925cc82011-06-22 15:39:57 -070055// static
56const ::DBus::Variant PropertyStoreTest::kUint16V =
57 DBusAdaptor::Uint16ToVariant(0);
58// static
59const ::DBus::Variant PropertyStoreTest::kUint32V =
60 DBusAdaptor::Uint32ToVariant(0);
61
62PropertyStoreTest::PropertyStoreTest()
mukesh agrawalffa3d042011-10-06 15:26:10 -070063 : internal_error_(Error::GetName(Error::kInternalError)),
64 invalid_args_(Error::GetName(Error::kInvalidArguments)),
Chris Masone9d779932011-08-25 16:33:41 -070065 invalid_prop_(Error::GetName(Error::kInvalidProperty)),
66 path_(dir_.CreateUniqueTempDir() ? dir_.path().value() : ""),
Chris Masone2176a882011-09-14 22:29:15 -070067 manager_(control_interface(),
68 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080069 metrics(),
Chris Masone2176a882011-09-14 22:29:15 -070070 glib(),
Chris Masone9d779932011-08-25 16:33:41 -070071 run_path(),
72 storage_path(),
73 string()) {
Chris Masoneb925cc82011-06-22 15:39:57 -070074}
75
76PropertyStoreTest::~PropertyStoreTest() {}
77
Chris Masone9d779932011-08-25 16:33:41 -070078void PropertyStoreTest::SetUp() {
79 ASSERT_FALSE(run_path().empty());
80 ASSERT_FALSE(storage_path().empty());
81}
82
Chris Masone8a56ad62011-07-02 15:27:57 -070083TEST_P(PropertyStoreTest, TestProperty) {
84 // Ensure that an attempt to write unknown properties returns InvalidProperty.
85 PropertyStore store;
86 ::DBus::Error error;
87 EXPECT_FALSE(DBusAdaptor::DispatchOnType(&store, "", GetParam(), &error));
Chris Masone9d779932011-08-25 16:33:41 -070088 EXPECT_EQ(invalid_prop(), error.name());
Chris Masone8a56ad62011-07-02 15:27:57 -070089}
90
91INSTANTIATE_TEST_CASE_P(
92 PropertyStoreTestInstance,
93 PropertyStoreTest,
94 Values(PropertyStoreTest::kBoolV,
95 PropertyStoreTest::kByteV,
Chris Masone8a56ad62011-07-02 15:27:57 -070096 PropertyStoreTest::kInt16V,
97 PropertyStoreTest::kInt32V,
Darin Petkov63138a92012-02-06 14:09:15 +010098 PropertyStoreTest::kKeyValueStoreV,
99 PropertyStoreTest::kStringV,
Chris Masone8a56ad62011-07-02 15:27:57 -0700100 PropertyStoreTest::kStringmapV,
Chris Masone889666b2011-07-03 12:58:50 -0700101 PropertyStoreTest::kStringsV,
Chris Masone889666b2011-07-03 12:58:50 -0700102 PropertyStoreTest::kUint16V,
103 PropertyStoreTest::kUint32V));
Chris Masone8a56ad62011-07-02 15:27:57 -0700104
mukesh agrawalffa3d042011-10-06 15:26:10 -0700105TEST_F(PropertyStoreTest, SetStringmapsProperty) {
106 PropertyStore store;
107 ::DBus::Error error;
108 EXPECT_FALSE(DBusAdaptor::DispatchOnType(
109 &store, "", PropertyStoreTest::kStringmapsV, &error));
110 EXPECT_EQ(internal_error(), error.name());
111}
112
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800113TEST_F(PropertyStoreTest, WriteOnlyProperties) {
114 // Test that properties registered as write-only are not returned
115 // when using Get*PropertiesIter().
116 PropertyStore store;
117 Error error;
118 {
119 const string keys[] = {"boolp1", "boolp2"};
120 bool values[] = {true, true};
121 store.RegisterWriteOnlyBool(keys[0], &values[0]);
122 store.RegisterBool(keys[1], &values[1]);
123
124 ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter();
125 error.Reset();
126 EXPECT_FALSE(it.AtEnd());
127 EXPECT_EQ(keys[1], it.Key());
128 EXPECT_TRUE(values[1] == it.Value(&error));
129 EXPECT_TRUE(error.IsSuccess());
130 it.Advance();
131 EXPECT_TRUE(it.AtEnd());
132 }
133 {
134 const string keys[] = {"int16p1", "int16p2"};
135 int16 values[] = {127, 128};
136 store.RegisterWriteOnlyInt16(keys[0], &values[0]);
137 store.RegisterInt16(keys[1], &values[1]);
138
139 ReadablePropertyConstIterator<int16> it = store.GetInt16PropertiesIter();
140 error.Reset();
141 EXPECT_FALSE(it.AtEnd());
142 EXPECT_EQ(keys[1], it.Key());
143 EXPECT_EQ(values[1], it.Value(&error));
144 EXPECT_TRUE(error.IsSuccess());
145 it.Advance();
146 EXPECT_TRUE(it.AtEnd());
147 }
148 {
149 const string keys[] = {"int32p1", "int32p2"};
150 int32 values[] = {127, 128};
151 store.RegisterWriteOnlyInt32(keys[0], &values[0]);
152 store.RegisterInt32(keys[1], &values[1]);
153
154 ReadablePropertyConstIterator<int32> it = store.GetInt32PropertiesIter();
155 error.Reset();
156 EXPECT_FALSE(it.AtEnd());
157 EXPECT_EQ(keys[1], it.Key());
158 EXPECT_EQ(values[1], it.Value(&error));
159 EXPECT_TRUE(error.IsSuccess());
160 it.Advance();
161 EXPECT_TRUE(it.AtEnd());
162 }
163 {
164 const string keys[] = {"stringp1", "stringp2"};
165 string values[] = {"noooo", "yesss"};
166 store.RegisterWriteOnlyString(keys[0], &values[0]);
167 store.RegisterString(keys[1], &values[1]);
168
169 ReadablePropertyConstIterator<string> it = store.GetStringPropertiesIter();
170 error.Reset();
171 EXPECT_FALSE(it.AtEnd());
172 EXPECT_EQ(keys[1], it.Key());
173 EXPECT_EQ(values[1], it.Value(&error));
174 EXPECT_TRUE(error.IsSuccess());
175 it.Advance();
176 EXPECT_TRUE(it.AtEnd());
177 }
178 {
179 const string keys[] = {"stringmapp1", "stringmapp2"};
180 Stringmap values[2];
181 values[0]["noooo"] = "yesss";
182 values[1]["yesss"] = "noooo";
183 store.RegisterWriteOnlyStringmap(keys[0], &values[0]);
184 store.RegisterStringmap(keys[1], &values[1]);
185
186 ReadablePropertyConstIterator<Stringmap> it =
187 store.GetStringmapPropertiesIter();
188 error.Reset();
189 EXPECT_FALSE(it.AtEnd());
190 EXPECT_EQ(keys[1], it.Key());
191 EXPECT_TRUE(values[1] == it.Value(&error));
192 EXPECT_TRUE(error.IsSuccess());
193 it.Advance();
194 EXPECT_TRUE(it.AtEnd());
195 }
196 {
197 const string keys[] = {"stringmapsp1", "stringmapsp2"};
198 Stringmaps values[2];
199 Stringmap element;
200 element["noooo"] = "yesss";
201 values[0].push_back(element);
202 element["yesss"] = "noooo";
203 values[1].push_back(element);
204
205 store.RegisterWriteOnlyStringmaps(keys[0], &values[0]);
206 store.RegisterStringmaps(keys[1], &values[1]);
207
208 ReadablePropertyConstIterator<Stringmaps> it =
209 store.GetStringmapsPropertiesIter();
210 error.Reset();
211 EXPECT_FALSE(it.AtEnd());
212 EXPECT_EQ(keys[1], it.Key());
213 EXPECT_TRUE(values[1] == it.Value(&error));
214 EXPECT_TRUE(error.IsSuccess());
215 it.Advance();
216 EXPECT_TRUE(it.AtEnd());
217 }
218 {
219 const string keys[] = {"stringsp1", "stringsp2"};
220 Strings values[2];
221 string element;
222 element = "noooo";
223 values[0].push_back(element);
224 element = "yesss";
225 values[1].push_back(element);
226 store.RegisterWriteOnlyStrings(keys[0], &values[0]);
227 store.RegisterStrings(keys[1], &values[1]);
228
229 ReadablePropertyConstIterator<Strings> it =
230 store.GetStringsPropertiesIter();
231 error.Reset();
232 EXPECT_FALSE(it.AtEnd());
233 EXPECT_EQ(keys[1], it.Key());
234 EXPECT_TRUE(values[1] == it.Value(&error));
235 EXPECT_TRUE(error.IsSuccess());
236 it.Advance();
237 EXPECT_TRUE(it.AtEnd());
238 }
239 {
240 const string keys[] = {"uint8p1", "uint8p2"};
241 uint8 values[] = {127, 128};
242 store.RegisterWriteOnlyUint8(keys[0], &values[0]);
243 store.RegisterUint8(keys[1], &values[1]);
244
245 ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter();
246 error.Reset();
247 EXPECT_FALSE(it.AtEnd());
248 EXPECT_EQ(keys[1], it.Key());
249 EXPECT_EQ(values[1], it.Value(&error));
250 EXPECT_TRUE(error.IsSuccess());
251 it.Advance();
252 EXPECT_TRUE(it.AtEnd());
253 }
254 {
255 const string keys[] = {"uint16p", "uint16p1"};
256 uint16 values[] = {127, 128};
257 store.RegisterWriteOnlyUint16(keys[0], &values[0]);
258 store.RegisterUint16(keys[1], &values[1]);
259
260 ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter();
261 error.Reset();
262 EXPECT_FALSE(it.AtEnd());
263 EXPECT_EQ(keys[1], it.Key());
264 EXPECT_EQ(values[1], it.Value(&error));
265 EXPECT_TRUE(error.IsSuccess());
266 it.Advance();
267 EXPECT_TRUE(it.AtEnd());
268 }
269}
270
Chris Masoneb925cc82011-06-22 15:39:57 -0700271} // namespace shill