blob: 8c15039de95b00f344f340600ac5f20903d4a146 [file] [log] [blame]
Chris Masoneb925cc82011-06-22 15:39:57 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// 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(),
71 glib(),
Chris Masone9d779932011-08-25 16:33:41 -070072 run_path(),
73 storage_path(),
74 string()) {
Chris Masoneb925cc82011-06-22 15:39:57 -070075}
76
77PropertyStoreTest::~PropertyStoreTest() {}
78
Chris Masone9d779932011-08-25 16:33:41 -070079void PropertyStoreTest::SetUp() {
80 ASSERT_FALSE(run_path().empty());
81 ASSERT_FALSE(storage_path().empty());
82}
83
Chris Masone8a56ad62011-07-02 15:27:57 -070084TEST_P(PropertyStoreTest, TestProperty) {
85 // Ensure that an attempt to write unknown properties returns InvalidProperty.
86 PropertyStore store;
87 ::DBus::Error error;
88 EXPECT_FALSE(DBusAdaptor::DispatchOnType(&store, "", GetParam(), &error));
Chris Masone9d779932011-08-25 16:33:41 -070089 EXPECT_EQ(invalid_prop(), error.name());
Chris Masone8a56ad62011-07-02 15:27:57 -070090}
91
92INSTANTIATE_TEST_CASE_P(
93 PropertyStoreTestInstance,
94 PropertyStoreTest,
95 Values(PropertyStoreTest::kBoolV,
96 PropertyStoreTest::kByteV,
97 PropertyStoreTest::kStringV,
98 PropertyStoreTest::kInt16V,
99 PropertyStoreTest::kInt32V,
Chris Masone8a56ad62011-07-02 15:27:57 -0700100 PropertyStoreTest::kStringmapV,
Chris Masone889666b2011-07-03 12:58:50 -0700101 PropertyStoreTest::kStringsV,
102 PropertyStoreTest::kStrIntPairV,
103 PropertyStoreTest::kUint16V,
104 PropertyStoreTest::kUint32V));
Chris Masone8a56ad62011-07-02 15:27:57 -0700105
mukesh agrawalffa3d042011-10-06 15:26:10 -0700106TEST_F(PropertyStoreTest, SetStringmapsProperty) {
107 PropertyStore store;
108 ::DBus::Error error;
109 EXPECT_FALSE(DBusAdaptor::DispatchOnType(
110 &store, "", PropertyStoreTest::kStringmapsV, &error));
111 EXPECT_EQ(internal_error(), error.name());
112}
113
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800114TEST_F(PropertyStoreTest, WriteOnlyProperties) {
115 // Test that properties registered as write-only are not returned
116 // when using Get*PropertiesIter().
117 PropertyStore store;
118 Error error;
119 {
120 const string keys[] = {"boolp1", "boolp2"};
121 bool values[] = {true, true};
122 store.RegisterWriteOnlyBool(keys[0], &values[0]);
123 store.RegisterBool(keys[1], &values[1]);
124
125 ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter();
126 error.Reset();
127 EXPECT_FALSE(it.AtEnd());
128 EXPECT_EQ(keys[1], it.Key());
129 EXPECT_TRUE(values[1] == it.Value(&error));
130 EXPECT_TRUE(error.IsSuccess());
131 it.Advance();
132 EXPECT_TRUE(it.AtEnd());
133 }
134 {
135 const string keys[] = {"int16p1", "int16p2"};
136 int16 values[] = {127, 128};
137 store.RegisterWriteOnlyInt16(keys[0], &values[0]);
138 store.RegisterInt16(keys[1], &values[1]);
139
140 ReadablePropertyConstIterator<int16> it = store.GetInt16PropertiesIter();
141 error.Reset();
142 EXPECT_FALSE(it.AtEnd());
143 EXPECT_EQ(keys[1], it.Key());
144 EXPECT_EQ(values[1], it.Value(&error));
145 EXPECT_TRUE(error.IsSuccess());
146 it.Advance();
147 EXPECT_TRUE(it.AtEnd());
148 }
149 {
150 const string keys[] = {"int32p1", "int32p2"};
151 int32 values[] = {127, 128};
152 store.RegisterWriteOnlyInt32(keys[0], &values[0]);
153 store.RegisterInt32(keys[1], &values[1]);
154
155 ReadablePropertyConstIterator<int32> it = store.GetInt32PropertiesIter();
156 error.Reset();
157 EXPECT_FALSE(it.AtEnd());
158 EXPECT_EQ(keys[1], it.Key());
159 EXPECT_EQ(values[1], it.Value(&error));
160 EXPECT_TRUE(error.IsSuccess());
161 it.Advance();
162 EXPECT_TRUE(it.AtEnd());
163 }
164 {
165 const string keys[] = {"stringp1", "stringp2"};
166 string values[] = {"noooo", "yesss"};
167 store.RegisterWriteOnlyString(keys[0], &values[0]);
168 store.RegisterString(keys[1], &values[1]);
169
170 ReadablePropertyConstIterator<string> it = store.GetStringPropertiesIter();
171 error.Reset();
172 EXPECT_FALSE(it.AtEnd());
173 EXPECT_EQ(keys[1], it.Key());
174 EXPECT_EQ(values[1], it.Value(&error));
175 EXPECT_TRUE(error.IsSuccess());
176 it.Advance();
177 EXPECT_TRUE(it.AtEnd());
178 }
179 {
180 const string keys[] = {"stringmapp1", "stringmapp2"};
181 Stringmap values[2];
182 values[0]["noooo"] = "yesss";
183 values[1]["yesss"] = "noooo";
184 store.RegisterWriteOnlyStringmap(keys[0], &values[0]);
185 store.RegisterStringmap(keys[1], &values[1]);
186
187 ReadablePropertyConstIterator<Stringmap> it =
188 store.GetStringmapPropertiesIter();
189 error.Reset();
190 EXPECT_FALSE(it.AtEnd());
191 EXPECT_EQ(keys[1], it.Key());
192 EXPECT_TRUE(values[1] == it.Value(&error));
193 EXPECT_TRUE(error.IsSuccess());
194 it.Advance();
195 EXPECT_TRUE(it.AtEnd());
196 }
197 {
198 const string keys[] = {"stringmapsp1", "stringmapsp2"};
199 Stringmaps values[2];
200 Stringmap element;
201 element["noooo"] = "yesss";
202 values[0].push_back(element);
203 element["yesss"] = "noooo";
204 values[1].push_back(element);
205
206 store.RegisterWriteOnlyStringmaps(keys[0], &values[0]);
207 store.RegisterStringmaps(keys[1], &values[1]);
208
209 ReadablePropertyConstIterator<Stringmaps> it =
210 store.GetStringmapsPropertiesIter();
211 error.Reset();
212 EXPECT_FALSE(it.AtEnd());
213 EXPECT_EQ(keys[1], it.Key());
214 EXPECT_TRUE(values[1] == it.Value(&error));
215 EXPECT_TRUE(error.IsSuccess());
216 it.Advance();
217 EXPECT_TRUE(it.AtEnd());
218 }
219 {
220 const string keys[] = {"stringsp1", "stringsp2"};
221 Strings values[2];
222 string element;
223 element = "noooo";
224 values[0].push_back(element);
225 element = "yesss";
226 values[1].push_back(element);
227 store.RegisterWriteOnlyStrings(keys[0], &values[0]);
228 store.RegisterStrings(keys[1], &values[1]);
229
230 ReadablePropertyConstIterator<Strings> it =
231 store.GetStringsPropertiesIter();
232 error.Reset();
233 EXPECT_FALSE(it.AtEnd());
234 EXPECT_EQ(keys[1], it.Key());
235 EXPECT_TRUE(values[1] == it.Value(&error));
236 EXPECT_TRUE(error.IsSuccess());
237 it.Advance();
238 EXPECT_TRUE(it.AtEnd());
239 }
240 {
241 const string keys[] = {"uint8p1", "uint8p2"};
242 uint8 values[] = {127, 128};
243 store.RegisterWriteOnlyUint8(keys[0], &values[0]);
244 store.RegisterUint8(keys[1], &values[1]);
245
246 ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter();
247 error.Reset();
248 EXPECT_FALSE(it.AtEnd());
249 EXPECT_EQ(keys[1], it.Key());
250 EXPECT_EQ(values[1], it.Value(&error));
251 EXPECT_TRUE(error.IsSuccess());
252 it.Advance();
253 EXPECT_TRUE(it.AtEnd());
254 }
255 {
256 const string keys[] = {"uint16p", "uint16p1"};
257 uint16 values[] = {127, 128};
258 store.RegisterWriteOnlyUint16(keys[0], &values[0]);
259 store.RegisterUint16(keys[1], &values[1]);
260
261 ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter();
262 error.Reset();
263 EXPECT_FALSE(it.AtEnd());
264 EXPECT_EQ(keys[1], it.Key());
265 EXPECT_EQ(values[1], it.Value(&error));
266 EXPECT_TRUE(error.IsSuccess());
267 it.Advance();
268 EXPECT_TRUE(it.AtEnd());
269 }
270}
271
Chris Masoneb925cc82011-06-22 15:39:57 -0700272} // namespace shill