blob: 99fa5f0533154e510c43bcf06d0cf71f242dcaeb [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 Masone889666b2011-07-03 12:58:50 -070023using std::make_pair;
Chris Masoneb925cc82011-06-22 15:39:57 -070024using std::map;
Chris Masone889666b2011-07-03 12:58:50 -070025using std::string;
Chris Masoneb925cc82011-06-22 15:39:57 -070026using std::vector;
Chris Masone8a56ad62011-07-02 15:27:57 -070027using ::testing::Values;
Chris Masoneb925cc82011-06-22 15:39:57 -070028
29namespace shill {
30
31// static
32const ::DBus::Variant PropertyStoreTest::kBoolV = DBusAdaptor::BoolToVariant(0);
33// static
34const ::DBus::Variant PropertyStoreTest::kByteV = DBusAdaptor::ByteToVariant(0);
35// static
36const ::DBus::Variant PropertyStoreTest::kInt16V =
37 DBusAdaptor::Int16ToVariant(0);
38// static
39const ::DBus::Variant PropertyStoreTest::kInt32V =
40 DBusAdaptor::Int32ToVariant(0);
41// static
42const ::DBus::Variant PropertyStoreTest::kStringV =
43 DBusAdaptor::StringToVariant("");
44// static
45const ::DBus::Variant PropertyStoreTest::kStringmapV =
Chris Masonea8a2c252011-06-27 22:16:30 -070046 DBusAdaptor::StringmapToVariant(Stringmap());
Chris Masoneb925cc82011-06-22 15:39:57 -070047// static
48const ::DBus::Variant PropertyStoreTest::kStringmapsV =
Chris Masone889666b2011-07-03 12:58:50 -070049 DBusAdaptor::StringmapsToVariant(Stringmaps());
Chris Masoneb925cc82011-06-22 15:39:57 -070050// static
51const ::DBus::Variant PropertyStoreTest::kStringsV =
Chris Masonea8a2c252011-06-27 22:16:30 -070052 DBusAdaptor::StringsToVariant(Strings(1, ""));
Chris Masoneb925cc82011-06-22 15:39:57 -070053// static
Chris Masone889666b2011-07-03 12:58:50 -070054const ::DBus::Variant PropertyStoreTest::kStrIntPairV =
55 DBusAdaptor::StrIntPairToVariant(StrIntPair(make_pair("", ""),
56 make_pair("", 12)));
57// static
Chris Masoneb925cc82011-06-22 15:39:57 -070058const ::DBus::Variant PropertyStoreTest::kUint16V =
59 DBusAdaptor::Uint16ToVariant(0);
60// static
61const ::DBus::Variant PropertyStoreTest::kUint32V =
62 DBusAdaptor::Uint32ToVariant(0);
63
64PropertyStoreTest::PropertyStoreTest()
mukesh agrawalffa3d042011-10-06 15:26:10 -070065 : internal_error_(Error::GetName(Error::kInternalError)),
66 invalid_args_(Error::GetName(Error::kInvalidArguments)),
Chris Masone9d779932011-08-25 16:33:41 -070067 invalid_prop_(Error::GetName(Error::kInvalidProperty)),
68 path_(dir_.CreateUniqueTempDir() ? dir_.path().value() : ""),
Chris Masone2176a882011-09-14 22:29:15 -070069 manager_(control_interface(),
70 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080071 metrics(),
Chris Masone2176a882011-09-14 22:29:15 -070072 glib(),
Chris Masone9d779932011-08-25 16:33:41 -070073 run_path(),
74 storage_path(),
75 string()) {
Chris Masoneb925cc82011-06-22 15:39:57 -070076}
77
78PropertyStoreTest::~PropertyStoreTest() {}
79
Chris Masone9d779932011-08-25 16:33:41 -070080void PropertyStoreTest::SetUp() {
81 ASSERT_FALSE(run_path().empty());
82 ASSERT_FALSE(storage_path().empty());
83}
84
Chris Masone8a56ad62011-07-02 15:27:57 -070085TEST_P(PropertyStoreTest, TestProperty) {
86 // Ensure that an attempt to write unknown properties returns InvalidProperty.
87 PropertyStore store;
88 ::DBus::Error error;
89 EXPECT_FALSE(DBusAdaptor::DispatchOnType(&store, "", GetParam(), &error));
Chris Masone9d779932011-08-25 16:33:41 -070090 EXPECT_EQ(invalid_prop(), error.name());
Chris Masone8a56ad62011-07-02 15:27:57 -070091}
92
93INSTANTIATE_TEST_CASE_P(
94 PropertyStoreTestInstance,
95 PropertyStoreTest,
96 Values(PropertyStoreTest::kBoolV,
97 PropertyStoreTest::kByteV,
98 PropertyStoreTest::kStringV,
99 PropertyStoreTest::kInt16V,
100 PropertyStoreTest::kInt32V,
Chris Masone8a56ad62011-07-02 15:27:57 -0700101 PropertyStoreTest::kStringmapV,
Chris Masone889666b2011-07-03 12:58:50 -0700102 PropertyStoreTest::kStringsV,
103 PropertyStoreTest::kStrIntPairV,
104 PropertyStoreTest::kUint16V,
105 PropertyStoreTest::kUint32V));
Chris Masone8a56ad62011-07-02 15:27:57 -0700106
mukesh agrawalffa3d042011-10-06 15:26:10 -0700107TEST_F(PropertyStoreTest, SetStringmapsProperty) {
108 PropertyStore store;
109 ::DBus::Error error;
110 EXPECT_FALSE(DBusAdaptor::DispatchOnType(
111 &store, "", PropertyStoreTest::kStringmapsV, &error));
112 EXPECT_EQ(internal_error(), error.name());
113}
114
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800115TEST_F(PropertyStoreTest, WriteOnlyProperties) {
116 // Test that properties registered as write-only are not returned
117 // when using Get*PropertiesIter().
118 PropertyStore store;
119 Error error;
120 {
121 const string keys[] = {"boolp1", "boolp2"};
122 bool values[] = {true, true};
123 store.RegisterWriteOnlyBool(keys[0], &values[0]);
124 store.RegisterBool(keys[1], &values[1]);
125
126 ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter();
127 error.Reset();
128 EXPECT_FALSE(it.AtEnd());
129 EXPECT_EQ(keys[1], it.Key());
130 EXPECT_TRUE(values[1] == it.Value(&error));
131 EXPECT_TRUE(error.IsSuccess());
132 it.Advance();
133 EXPECT_TRUE(it.AtEnd());
134 }
135 {
136 const string keys[] = {"int16p1", "int16p2"};
137 int16 values[] = {127, 128};
138 store.RegisterWriteOnlyInt16(keys[0], &values[0]);
139 store.RegisterInt16(keys[1], &values[1]);
140
141 ReadablePropertyConstIterator<int16> it = store.GetInt16PropertiesIter();
142 error.Reset();
143 EXPECT_FALSE(it.AtEnd());
144 EXPECT_EQ(keys[1], it.Key());
145 EXPECT_EQ(values[1], it.Value(&error));
146 EXPECT_TRUE(error.IsSuccess());
147 it.Advance();
148 EXPECT_TRUE(it.AtEnd());
149 }
150 {
151 const string keys[] = {"int32p1", "int32p2"};
152 int32 values[] = {127, 128};
153 store.RegisterWriteOnlyInt32(keys[0], &values[0]);
154 store.RegisterInt32(keys[1], &values[1]);
155
156 ReadablePropertyConstIterator<int32> it = store.GetInt32PropertiesIter();
157 error.Reset();
158 EXPECT_FALSE(it.AtEnd());
159 EXPECT_EQ(keys[1], it.Key());
160 EXPECT_EQ(values[1], it.Value(&error));
161 EXPECT_TRUE(error.IsSuccess());
162 it.Advance();
163 EXPECT_TRUE(it.AtEnd());
164 }
165 {
166 const string keys[] = {"stringp1", "stringp2"};
167 string values[] = {"noooo", "yesss"};
168 store.RegisterWriteOnlyString(keys[0], &values[0]);
169 store.RegisterString(keys[1], &values[1]);
170
171 ReadablePropertyConstIterator<string> it = store.GetStringPropertiesIter();
172 error.Reset();
173 EXPECT_FALSE(it.AtEnd());
174 EXPECT_EQ(keys[1], it.Key());
175 EXPECT_EQ(values[1], it.Value(&error));
176 EXPECT_TRUE(error.IsSuccess());
177 it.Advance();
178 EXPECT_TRUE(it.AtEnd());
179 }
180 {
181 const string keys[] = {"stringmapp1", "stringmapp2"};
182 Stringmap values[2];
183 values[0]["noooo"] = "yesss";
184 values[1]["yesss"] = "noooo";
185 store.RegisterWriteOnlyStringmap(keys[0], &values[0]);
186 store.RegisterStringmap(keys[1], &values[1]);
187
188 ReadablePropertyConstIterator<Stringmap> it =
189 store.GetStringmapPropertiesIter();
190 error.Reset();
191 EXPECT_FALSE(it.AtEnd());
192 EXPECT_EQ(keys[1], it.Key());
193 EXPECT_TRUE(values[1] == it.Value(&error));
194 EXPECT_TRUE(error.IsSuccess());
195 it.Advance();
196 EXPECT_TRUE(it.AtEnd());
197 }
198 {
199 const string keys[] = {"stringmapsp1", "stringmapsp2"};
200 Stringmaps values[2];
201 Stringmap element;
202 element["noooo"] = "yesss";
203 values[0].push_back(element);
204 element["yesss"] = "noooo";
205 values[1].push_back(element);
206
207 store.RegisterWriteOnlyStringmaps(keys[0], &values[0]);
208 store.RegisterStringmaps(keys[1], &values[1]);
209
210 ReadablePropertyConstIterator<Stringmaps> it =
211 store.GetStringmapsPropertiesIter();
212 error.Reset();
213 EXPECT_FALSE(it.AtEnd());
214 EXPECT_EQ(keys[1], it.Key());
215 EXPECT_TRUE(values[1] == it.Value(&error));
216 EXPECT_TRUE(error.IsSuccess());
217 it.Advance();
218 EXPECT_TRUE(it.AtEnd());
219 }
220 {
221 const string keys[] = {"stringsp1", "stringsp2"};
222 Strings values[2];
223 string element;
224 element = "noooo";
225 values[0].push_back(element);
226 element = "yesss";
227 values[1].push_back(element);
228 store.RegisterWriteOnlyStrings(keys[0], &values[0]);
229 store.RegisterStrings(keys[1], &values[1]);
230
231 ReadablePropertyConstIterator<Strings> it =
232 store.GetStringsPropertiesIter();
233 error.Reset();
234 EXPECT_FALSE(it.AtEnd());
235 EXPECT_EQ(keys[1], it.Key());
236 EXPECT_TRUE(values[1] == it.Value(&error));
237 EXPECT_TRUE(error.IsSuccess());
238 it.Advance();
239 EXPECT_TRUE(it.AtEnd());
240 }
241 {
242 const string keys[] = {"uint8p1", "uint8p2"};
243 uint8 values[] = {127, 128};
244 store.RegisterWriteOnlyUint8(keys[0], &values[0]);
245 store.RegisterUint8(keys[1], &values[1]);
246
247 ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter();
248 error.Reset();
249 EXPECT_FALSE(it.AtEnd());
250 EXPECT_EQ(keys[1], it.Key());
251 EXPECT_EQ(values[1], it.Value(&error));
252 EXPECT_TRUE(error.IsSuccess());
253 it.Advance();
254 EXPECT_TRUE(it.AtEnd());
255 }
256 {
257 const string keys[] = {"uint16p", "uint16p1"};
258 uint16 values[] = {127, 128};
259 store.RegisterWriteOnlyUint16(keys[0], &values[0]);
260 store.RegisterUint16(keys[1], &values[1]);
261
262 ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter();
263 error.Reset();
264 EXPECT_FALSE(it.AtEnd());
265 EXPECT_EQ(keys[1], it.Key());
266 EXPECT_EQ(values[1], it.Value(&error));
267 EXPECT_TRUE(error.IsSuccess());
268 it.Advance();
269 EXPECT_TRUE(it.AtEnd());
270 }
271}
272
Chris Masoneb925cc82011-06-22 15:39:57 -0700273} // namespace shill