• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/property_store.h"
18 
19 #include "shill/property_store_unittest.h"
20 
21 #include <map>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include <base/macros.h>
27 #if defined(__ANDROID__)
28 #include <dbus/service_constants.h>
29 #else
30 #include <chromeos/dbus/service_constants.h>
31 #endif  // __ANDROID__
32 #include <gtest/gtest.h>
33 #include <gmock/gmock.h>
34 
35 #include "shill/error.h"
36 #include "shill/event_dispatcher.h"
37 #include "shill/manager.h"
38 #include "shill/mock_control.h"
39 #include "shill/property_accessor.h"
40 
41 using base::Bind;
42 using base::Unretained;
43 using std::map;
44 using std::string;
45 using std::vector;
46 using ::testing::_;
47 using ::testing::Return;
48 using ::testing::Values;
49 
50 namespace shill {
51 
52 // static
53 const brillo::Any PropertyStoreTest::kBoolV = brillo::Any(false);
54 // static
55 const brillo::Any PropertyStoreTest::kByteV =
56     brillo::Any(uint8_t(0));
57 // static
58 const brillo::Any PropertyStoreTest::kInt16V = brillo::Any(int16_t(0));
59 // static
60 const brillo::Any PropertyStoreTest::kInt32V = brillo::Any(int32_t(0));
61 // static
62 const brillo::Any PropertyStoreTest::kKeyValueStoreV =
63     brillo::Any(brillo::VariantDictionary());
64 // static
65 const brillo::Any PropertyStoreTest::kStringV = brillo::Any(string());
66 // static
67 const brillo::Any PropertyStoreTest::kStringmapV = brillo::Any(Stringmap());
68 // static
69 const brillo::Any PropertyStoreTest::kStringmapsV =
70     brillo::Any(Stringmaps());
71 // static
72 const brillo::Any PropertyStoreTest::kStringsV = brillo::Any(Strings());
73 // static
74 const brillo::Any PropertyStoreTest::kUint16V = brillo::Any(uint16_t(0));
75 // static
76 const brillo::Any PropertyStoreTest::kUint16sV = brillo::Any(Uint16s());
77 // static
78 const brillo::Any PropertyStoreTest::kUint32V = brillo::Any(uint32_t(0));
79 // static
80 const brillo::Any PropertyStoreTest::kUint64V = brillo::Any(uint64_t(0));
81 
PropertyStoreTest()82 PropertyStoreTest::PropertyStoreTest()
83     : internal_error_(kErrorResultInternalError),
84       invalid_args_(kErrorResultInvalidArguments),
85       invalid_prop_(kErrorResultInvalidProperty),
86       path_(dir_.CreateUniqueTempDir() ? dir_.path().value() : ""),
87       metrics_(dispatcher()),
88       default_technology_order_{Technology::kVPN,
89                                 Technology::kEthernet,
90                                 Technology::kWifi,
91                                 Technology::kWiMax,
92                                 Technology::kCellular},
93       manager_(control_interface(),
94                dispatcher(),
95                metrics(),
96                run_path(),
97                storage_path(),
98                string()) {
99 }
100 
~PropertyStoreTest()101 PropertyStoreTest::~PropertyStoreTest() {}
102 
SetUp()103 void PropertyStoreTest::SetUp() {
104   ASSERT_FALSE(run_path().empty());
105   ASSERT_FALSE(storage_path().empty());
106 }
107 
TEST_P(PropertyStoreTest,SetPropertyNonexistent)108 TEST_P(PropertyStoreTest, SetPropertyNonexistent) {
109   // Ensure that an attempt to write unknown properties returns
110   // InvalidProperty, and does not yield a PropertyChange callback.
111   PropertyStore store(Bind(&PropertyStoreTest::TestCallback,
112                            Unretained(this)));
113   Error error;
114   EXPECT_CALL(*this, TestCallback(_)).Times(0);
115   EXPECT_FALSE(store.SetAnyProperty("", GetParam(), &error));
116   EXPECT_EQ(Error::kInvalidProperty, error.type());
117 }
118 
119 INSTANTIATE_TEST_CASE_P(
120     PropertyStoreTestInstance,
121     PropertyStoreTest,
122     Values(PropertyStoreTest::kBoolV,
123            PropertyStoreTest::kByteV,
124            PropertyStoreTest::kInt16V,
125            PropertyStoreTest::kInt32V,
126            PropertyStoreTest::kStringV,
127            PropertyStoreTest::kStringmapV,
128            PropertyStoreTest::kStringsV,
129            PropertyStoreTest::kUint16V,
130            PropertyStoreTest::kUint16sV,
131            PropertyStoreTest::kUint32V,
132            PropertyStoreTest::kUint64V));
133 
134 template <typename T>
135 class PropertyStoreTypedTest : public PropertyStoreTest {
136  protected:
137   bool SetProperty(
138       PropertyStore* store, const string& name, Error* error);
139 };
140 
141 TYPED_TEST_CASE(PropertyStoreTypedTest, PropertyStoreTest::PropertyTypes);
142 
TYPED_TEST(PropertyStoreTypedTest,RegisterProperty)143 TYPED_TEST(PropertyStoreTypedTest, RegisterProperty) {
144   PropertyStore store(Bind(&PropertyStoreTest::TestCallback,
145                            Unretained(this)));
146   Error error;
147   TypeParam property;
148   PropertyStoreTest::RegisterProperty(&store, "some property", &property);
149   EXPECT_TRUE(store.Contains("some property"));
150 }
151 
TYPED_TEST(PropertyStoreTypedTest,GetProperty)152 TYPED_TEST(PropertyStoreTypedTest, GetProperty) {
153   PropertyStore store(Bind(&PropertyStoreTest::TestCallback,
154                            Unretained(this)));
155   Error error;
156   TypeParam property{};  // value-initialize primitives
157   PropertyStoreTest::RegisterProperty(&store, "some property", &property);
158 
159   TypeParam read_value;
160   EXPECT_CALL(*this, TestCallback(_)).Times(0);
161   EXPECT_TRUE(PropertyStoreTest::GetProperty(
162       store, "some property", &read_value, &error));
163   EXPECT_EQ(property, read_value);
164 }
165 
TYPED_TEST(PropertyStoreTypedTest,ClearProperty)166 TYPED_TEST(PropertyStoreTypedTest, ClearProperty) {
167   PropertyStore store(Bind(&PropertyStoreTest::TestCallback,
168                            Unretained(this)));
169   Error error;
170   TypeParam property;
171   PropertyStoreTest::RegisterProperty(&store, "some property", &property);
172   EXPECT_CALL(*this, TestCallback(_));
173   EXPECT_TRUE(store.ClearProperty("some property", &error));
174 }
175 
TYPED_TEST(PropertyStoreTypedTest,SetProperty)176 TYPED_TEST(PropertyStoreTypedTest, SetProperty) {
177   PropertyStore store(Bind(&PropertyStoreTest::TestCallback,
178                            Unretained(this)));
179   Error error;
180   TypeParam property{};  // value-initialize primitives
181   PropertyStoreTest::RegisterProperty(&store, "some property", &property);
182 
183   // Change the value from the default (initialized above).  Should
184   // generate a change callback. The second SetProperty, however,
185   // should not. Hence, we should get exactly one callback.
186   EXPECT_CALL(*this, TestCallback(_)).Times(1);
187   EXPECT_TRUE(this->SetProperty(&store, "some property", &error));
188   EXPECT_FALSE(this->SetProperty(&store, "some property", &error));
189 }
190 
SetProperty(PropertyStore * store,const string & name,Error * error)191 template<> bool PropertyStoreTypedTest<bool>::SetProperty(
192     PropertyStore* store, const string& name, Error* error) {
193   bool new_value = true;
194   return store->SetBoolProperty(name, new_value, error);
195 }
196 
SetProperty(PropertyStore * store,const string & name,Error * error)197 template<> bool PropertyStoreTypedTest<int16_t>::SetProperty(
198     PropertyStore* store, const string& name, Error* error) {
199   int16_t new_value = 1;
200   return store->SetInt16Property(name, new_value, error);
201 }
202 
SetProperty(PropertyStore * store,const string & name,Error * error)203 template<> bool PropertyStoreTypedTest<int32_t>::SetProperty(
204     PropertyStore* store, const string& name, Error* error) {
205   int32_t new_value = 1;
206   return store->SetInt32Property(name, new_value, error);
207 }
208 
SetProperty(PropertyStore * store,const string & name,Error * error)209 template<> bool PropertyStoreTypedTest<string>::SetProperty(
210     PropertyStore* store, const string& name, Error* error) {
211   string new_value = "new value";
212   return store->SetStringProperty(name, new_value, error);
213 }
214 
SetProperty(PropertyStore * store,const string & name,Error * error)215 template<> bool PropertyStoreTypedTest<Stringmap>::SetProperty(
216     PropertyStore* store, const string& name, Error* error) {
217   Stringmap new_value;
218   new_value["new key"] = "new value";
219   return store->SetStringmapProperty(name, new_value, error);
220 }
221 
SetProperty(PropertyStore * store,const string & name,Error * error)222 template<> bool PropertyStoreTypedTest<Stringmaps>::SetProperty(
223     PropertyStore* store, const string& name, Error* error) {
224   Stringmaps new_value(1);
225   new_value[0]["new key"] = "new value";
226   return store->SetStringmapsProperty(name, new_value, error);
227 }
228 
SetProperty(PropertyStore * store,const string & name,Error * error)229 template<> bool PropertyStoreTypedTest<Strings>::SetProperty(
230     PropertyStore* store, const string& name, Error* error) {
231   Strings new_value(1);
232   new_value[0] = "new value";
233   return store->SetStringsProperty(name, new_value, error);
234 }
235 
SetProperty(PropertyStore * store,const string & name,Error * error)236 template<> bool PropertyStoreTypedTest<uint8_t>::SetProperty(
237     PropertyStore* store, const string& name, Error* error) {
238   uint8_t new_value = 1;
239   return store->SetUint8Property(name, new_value, error);
240 }
241 
SetProperty(PropertyStore * store,const string & name,Error * error)242 template<> bool PropertyStoreTypedTest<uint16_t>::SetProperty(
243     PropertyStore* store, const string& name, Error* error) {
244   uint16_t new_value = 1;
245   return store->SetUint16Property(name, new_value, error);
246 }
247 
SetProperty(PropertyStore * store,const string & name,Error * error)248 template<> bool PropertyStoreTypedTest<Uint16s>::SetProperty(
249     PropertyStore* store, const string& name, Error* error) {
250   Uint16s new_value{1};
251   return store->SetUint16sProperty(name, new_value, error);
252 }
253 
SetProperty(PropertyStore * store,const string & name,Error * error)254 template<> bool PropertyStoreTypedTest<uint32_t>::SetProperty(
255     PropertyStore* store, const string& name, Error* error) {
256   uint32_t new_value = 1;
257   return store->SetUint32Property(name, new_value, error);
258 }
259 
TEST_F(PropertyStoreTest,ClearBoolProperty)260 TEST_F(PropertyStoreTest, ClearBoolProperty) {
261   // We exercise both possibilities for the default value here,
262   // to ensure that Clear actually resets the property based on
263   // the property's initial value (rather than the language's
264   // default value for the type).
265   static const bool kDefaults[] = {true, false};
266   for (size_t i = 0; i < arraysize(kDefaults); ++i) {
267     PropertyStore store;
268     Error error;
269 
270     const bool default_value = kDefaults[i];
271     bool flag = default_value;
272     store.RegisterBool("some bool", &flag);
273 
274     EXPECT_TRUE(store.ClearProperty("some bool", &error));
275     EXPECT_EQ(default_value, flag);
276   }
277 }
278 
TEST_F(PropertyStoreTest,ClearPropertyNonexistent)279 TEST_F(PropertyStoreTest, ClearPropertyNonexistent) {
280   PropertyStore store(Bind(&PropertyStoreTest::TestCallback,
281                            Unretained(this)));
282   Error error;
283 
284   EXPECT_CALL(*this, TestCallback(_)).Times(0);
285   EXPECT_FALSE(store.ClearProperty("", &error));
286   EXPECT_EQ(Error::kInvalidProperty, error.type());
287 }
288 
289 // Separate from SetPropertyNonexistent, because
290 // SetAnyProperty doesn't support Stringmaps.
TEST_F(PropertyStoreTest,SetStringmapsProperty)291 TEST_F(PropertyStoreTest, SetStringmapsProperty) {
292   PropertyStore store(Bind(&PropertyStoreTest::TestCallback,
293                            Unretained(this)));
294 
295   Error error;
296   EXPECT_CALL(*this, TestCallback(_)).Times(0);
297   EXPECT_FALSE(store.SetAnyProperty(
298       "", PropertyStoreTest::kStringmapsV, &error));
299   EXPECT_EQ(Error::kInternalError, error.type());
300 }
301 
302 // KeyValueStoreProperty is only defined for derived types so handle
303 // this case manually here.
TEST_F(PropertyStoreTest,KeyValueStorePropertyNonExistent)304 TEST_F(PropertyStoreTest, KeyValueStorePropertyNonExistent) {
305   PropertyStore store(Bind(&PropertyStoreTest::TestCallback,
306                            Unretained(this)));
307   Error error;
308   EXPECT_CALL(*this, TestCallback(_)).Times(0);
309   EXPECT_FALSE(store.SetAnyProperty(
310       "", PropertyStoreTest::kKeyValueStoreV, &error));
311   EXPECT_EQ(Error::kInvalidProperty, error.type());
312 }
313 
TEST_F(PropertyStoreTest,KeyValueStoreProperty)314 TEST_F(PropertyStoreTest, KeyValueStoreProperty) {
315   PropertyStore store(Bind(&PropertyStoreTest::TestCallback,
316                            Unretained(this)));
317   const char kKey[] = "key";
318   EXPECT_CALL(*this, GetKeyValueStoreCallback(_))
319       .WillOnce(Return(KeyValueStore()));
320   store.RegisterDerivedKeyValueStore(
321       kKey,
322       KeyValueStoreAccessor(
323           new CustomAccessor<PropertyStoreTest, KeyValueStore>(
324               this, &PropertyStoreTest::GetKeyValueStoreCallback,
325               &PropertyStoreTest::SetKeyValueStoreCallback)));
326   EXPECT_CALL(*this, TestCallback(_));
327   EXPECT_CALL(*this, SetKeyValueStoreCallback(_, _)).WillOnce(Return(true));
328   Error error;
329   EXPECT_TRUE(store.SetAnyProperty(kKey, kKeyValueStoreV, &error));
330 }
331 
TEST_F(PropertyStoreTest,WriteOnlyProperties)332 TEST_F(PropertyStoreTest, WriteOnlyProperties) {
333   // Test that properties registered as write-only are not returned
334   // when using Get*PropertiesIter().
335   PropertyStore store;
336   {
337     const string keys[]  = {"boolp1", "boolp2"};
338     bool values[] = {true, true};
339     store.RegisterWriteOnlyBool(keys[0], &values[0]);
340     store.RegisterBool(keys[1], &values[1]);
341 
342     ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter();
343     EXPECT_FALSE(it.AtEnd());
344     EXPECT_EQ(keys[1], it.Key());
345     EXPECT_TRUE(values[1] == it.value());
346     it.Advance();
347     EXPECT_TRUE(it.AtEnd());
348 
349     Error errors[2];
350     EXPECT_FALSE(store.GetBoolProperty(keys[0], nullptr, &errors[0]));
351     EXPECT_EQ(Error::kPermissionDenied, errors[0].type());
352     bool test_value;
353     EXPECT_TRUE(store.GetBoolProperty(keys[1], &test_value, &errors[1]));
354     EXPECT_TRUE(errors[1].IsSuccess());
355     EXPECT_EQ(values[1], test_value);
356   }
357   {
358     const string keys[] = {"int16p1", "int16p2"};
359     int16_t values[] = {127, 128};
360     store.RegisterWriteOnlyInt16(keys[0], &values[0]);
361     store.RegisterInt16(keys[1], &values[1]);
362 
363     ReadablePropertyConstIterator<int16_t> it = store.GetInt16PropertiesIter();
364     EXPECT_FALSE(it.AtEnd());
365     EXPECT_EQ(keys[1], it.Key());
366     EXPECT_EQ(values[1], it.value());
367     it.Advance();
368     EXPECT_TRUE(it.AtEnd());
369 
370     Error errors[2];
371     EXPECT_FALSE(store.GetInt16Property(keys[0], nullptr, &errors[0]));
372     EXPECT_EQ(Error::kPermissionDenied, errors[0].type());
373     int16_t test_value;
374     EXPECT_TRUE(store.GetInt16Property(keys[1], &test_value, &errors[1]));
375     EXPECT_TRUE(errors[1].IsSuccess());
376     EXPECT_EQ(values[1], test_value);
377   }
378   {
379     const string keys[] = {"int32p1", "int32p2"};
380     int32_t values[] = {127, 128};
381     store.RegisterWriteOnlyInt32(keys[0], &values[0]);
382     store.RegisterInt32(keys[1], &values[1]);
383 
384     ReadablePropertyConstIterator<int32_t> it = store.GetInt32PropertiesIter();
385     EXPECT_FALSE(it.AtEnd());
386     EXPECT_EQ(keys[1], it.Key());
387     EXPECT_EQ(values[1], it.value());
388     it.Advance();
389     EXPECT_TRUE(it.AtEnd());
390 
391     Error errors[2];
392     EXPECT_FALSE(store.GetInt32Property(keys[0], nullptr, &errors[0]));
393     EXPECT_EQ(Error::kPermissionDenied, errors[0].type());
394     int32_t test_value;
395     EXPECT_TRUE(store.GetInt32Property(keys[1], &test_value, &errors[1]));
396     EXPECT_TRUE(errors[1].IsSuccess());
397     EXPECT_EQ(values[1], test_value);
398   }
399   {
400     const string keys[] = {"stringp1", "stringp2"};
401     string values[] = {"noooo", "yesss"};
402     store.RegisterWriteOnlyString(keys[0], &values[0]);
403     store.RegisterString(keys[1], &values[1]);
404 
405     ReadablePropertyConstIterator<string> it = store.GetStringPropertiesIter();
406     EXPECT_FALSE(it.AtEnd());
407     EXPECT_EQ(keys[1], it.Key());
408     EXPECT_EQ(values[1], it.value());
409     it.Advance();
410     EXPECT_TRUE(it.AtEnd());
411 
412     Error errors[2];
413     EXPECT_FALSE(store.GetStringProperty(keys[0], nullptr, &errors[0]));
414     EXPECT_EQ(Error::kPermissionDenied, errors[0].type());
415     string test_value;
416     EXPECT_TRUE(store.GetStringProperty(keys[1], &test_value, &errors[1]));
417     EXPECT_TRUE(errors[1].IsSuccess());
418     EXPECT_EQ(values[1], test_value);
419   }
420   {
421     const string keys[] = {"stringmapp1", "stringmapp2"};
422     Stringmap values[2];
423     values[0]["noooo"] = "yesss";
424     values[1]["yesss"] = "noooo";
425     store.RegisterWriteOnlyStringmap(keys[0], &values[0]);
426     store.RegisterStringmap(keys[1], &values[1]);
427 
428     ReadablePropertyConstIterator<Stringmap> it =
429         store.GetStringmapPropertiesIter();
430     EXPECT_FALSE(it.AtEnd());
431     EXPECT_EQ(keys[1], it.Key());
432     EXPECT_TRUE(values[1] == it.value());
433     it.Advance();
434     EXPECT_TRUE(it.AtEnd());
435 
436     Error errors[2];
437     EXPECT_FALSE(store.GetStringmapProperty(keys[0], nullptr, &errors[0]));
438     EXPECT_EQ(Error::kPermissionDenied, errors[0].type());
439     Stringmap test_value;
440     EXPECT_TRUE(store.GetStringmapProperty(keys[1], &test_value, &errors[1]));
441     EXPECT_TRUE(errors[1].IsSuccess());
442     EXPECT_TRUE(values[1] == test_value);
443   }
444   {
445     const string keys[] = {"stringmapsp1", "stringmapsp2"};
446     Stringmaps values[2];
447     Stringmap element;
448     element["noooo"] = "yesss";
449     values[0].push_back(element);
450     element["yesss"] = "noooo";
451     values[1].push_back(element);
452 
453     store.RegisterWriteOnlyStringmaps(keys[0], &values[0]);
454     store.RegisterStringmaps(keys[1], &values[1]);
455 
456     ReadablePropertyConstIterator<Stringmaps> it =
457         store.GetStringmapsPropertiesIter();
458     EXPECT_FALSE(it.AtEnd());
459     EXPECT_EQ(keys[1], it.Key());
460     EXPECT_TRUE(values[1] == it.value());
461     it.Advance();
462     EXPECT_TRUE(it.AtEnd());
463 
464     Error errors[2];
465     EXPECT_FALSE(store.GetStringmapsProperty(keys[0], nullptr, &errors[0]));
466     EXPECT_EQ(Error::kPermissionDenied, errors[0].type());
467     Stringmaps test_value;
468     EXPECT_TRUE(store.GetStringmapsProperty(keys[1], &test_value, &errors[1]));
469     EXPECT_TRUE(errors[1].IsSuccess());
470     EXPECT_TRUE(values[1] == test_value);
471   }
472   {
473     const string keys[] = {"stringsp1", "stringsp2"};
474     Strings values[2];
475     string element;
476     element = "noooo";
477     values[0].push_back(element);
478     element = "yesss";
479     values[1].push_back(element);
480     store.RegisterWriteOnlyStrings(keys[0], &values[0]);
481     store.RegisterStrings(keys[1], &values[1]);
482 
483     ReadablePropertyConstIterator<Strings> it =
484         store.GetStringsPropertiesIter();
485     EXPECT_FALSE(it.AtEnd());
486     EXPECT_EQ(keys[1], it.Key());
487     EXPECT_TRUE(values[1] == it.value());
488     it.Advance();
489     EXPECT_TRUE(it.AtEnd());
490 
491     Error errors[2];
492     EXPECT_FALSE(store.GetStringsProperty(keys[0], nullptr, &errors[0]));
493     EXPECT_EQ(Error::kPermissionDenied, errors[0].type());
494     Strings test_value;
495     EXPECT_TRUE(store.GetStringsProperty(keys[1], &test_value, &errors[1]));
496     EXPECT_TRUE(errors[1].IsSuccess());
497     EXPECT_TRUE(values[1] == test_value);
498   }
499   {
500     const string keys[] = {"uint8p1", "uint8p2"};
501     uint8_t values[] = {127, 128};
502     store.RegisterWriteOnlyUint8(keys[0], &values[0]);
503     store.RegisterUint8(keys[1], &values[1]);
504 
505     ReadablePropertyConstIterator<uint8_t> it = store.GetUint8PropertiesIter();
506     EXPECT_FALSE(it.AtEnd());
507     EXPECT_EQ(keys[1], it.Key());
508     EXPECT_EQ(values[1], it.value());
509     it.Advance();
510     EXPECT_TRUE(it.AtEnd());
511 
512     Error errors[2];
513     EXPECT_FALSE(store.GetUint8Property(keys[0], nullptr, &errors[0]));
514     EXPECT_EQ(Error::kPermissionDenied, errors[0].type());
515     uint8_t test_value;
516     EXPECT_TRUE(store.GetUint8Property(keys[1], &test_value, &errors[1]));
517     EXPECT_TRUE(errors[1].IsSuccess());
518     EXPECT_EQ(values[1], test_value);
519   }
520   {
521     const string keys[] = {"uint16p", "uint16p1"};
522     uint16_t values[] = {127, 128};
523     store.RegisterWriteOnlyUint16(keys[0], &values[0]);
524     store.RegisterUint16(keys[1], &values[1]);
525 
526     ReadablePropertyConstIterator<uint16_t> it =
527         store.GetUint16PropertiesIter();
528     EXPECT_FALSE(it.AtEnd());
529     EXPECT_EQ(keys[1], it.Key());
530     EXPECT_EQ(values[1], it.value());
531     it.Advance();
532     EXPECT_TRUE(it.AtEnd());
533 
534     Error errors[2];
535     EXPECT_FALSE(store.GetUint16Property(keys[0], nullptr, &errors[0]));
536     EXPECT_EQ(Error::kPermissionDenied, errors[0].type());
537     uint16_t test_value;
538     EXPECT_TRUE(store.GetUint16Property(keys[1], &test_value, &errors[1]));
539     EXPECT_TRUE(errors[1].IsSuccess());
540     EXPECT_EQ(values[1], test_value);
541   }
542 }
543 
TEST_F(PropertyStoreTest,SetAnyProperty)544 TEST_F(PropertyStoreTest, SetAnyProperty) {
545   // Test that registered properties can be set using brillo::Any variant
546   // type.
547   PropertyStore store;
548   {
549     // Register property value.
550     const string key = "boolp";
551     bool value = true;
552     store.RegisterBool(key, &value);
553 
554     // Verify property value.
555     bool test_value;
556     Error error;
557     EXPECT_TRUE(store.GetBoolProperty(key, &test_value, &error));
558     EXPECT_EQ(value, test_value);
559 
560     // Set property using brillo::Any variant type.
561     bool new_value = false;
562     EXPECT_TRUE(store.SetAnyProperty(key, brillo::Any(new_value), &error));
563     EXPECT_TRUE(store.GetBoolProperty(key, &test_value, &error));
564     EXPECT_EQ(new_value, test_value);
565   }
566   {
567     // Register property value.
568     const string key = "int16p";
569     int16_t value = 127;
570     store.RegisterInt16(key, &value);
571 
572     // Verify property value.
573     int16_t test_value;
574     Error error;
575     EXPECT_TRUE(store.GetInt16Property(key, &test_value, &error));
576     EXPECT_EQ(value, test_value);
577 
578     // Set property using brillo::Any variant type.
579     int16_t new_value = 128;
580     EXPECT_TRUE(store.SetAnyProperty(key, brillo::Any(new_value), &error));
581     EXPECT_TRUE(store.GetInt16Property(key, &test_value, &error));
582     EXPECT_EQ(new_value, test_value);
583   }
584   {
585     // Register property value.
586     const string key = "int32p";
587     int32_t value = 127;
588     store.RegisterInt32(key, &value);
589 
590     // Verify property value.
591     int32_t test_value;
592     Error error;
593     EXPECT_TRUE(store.GetInt32Property(key, &test_value, &error));
594     EXPECT_EQ(value, test_value);
595 
596     // Set property using brillo::Any variant type.
597     int32_t new_value = 128;
598     EXPECT_TRUE(store.SetAnyProperty(key, brillo::Any(new_value), &error));
599     EXPECT_TRUE(store.GetInt32Property(key, &test_value, &error));
600     EXPECT_EQ(new_value, test_value);
601   }
602   {
603     // Register property value.
604     const string key = "stringp";
605     string value = "noooo";
606     store.RegisterString(key, &value);
607 
608     // Verify property value.
609     string test_value;
610     Error error;
611     EXPECT_TRUE(store.GetStringProperty(key, &test_value, &error));
612     EXPECT_EQ(value, test_value);
613 
614     // Set property using brillo::Any variant type.
615     string new_value = "yesss";
616     EXPECT_TRUE(store.SetAnyProperty(key, brillo::Any(new_value), &error));
617     EXPECT_TRUE(store.GetStringProperty(key, &test_value, &error));
618     EXPECT_EQ(new_value, test_value);
619   }
620   {
621     // Register property value.
622     const string key = "stringmapp";
623     Stringmap value;
624     value["noooo"] = "yesss";
625     store.RegisterStringmap(key, &value);
626 
627     // Verify property value.
628     Stringmap test_value;
629     Error error;
630     EXPECT_TRUE(store.GetStringmapProperty(key, &test_value, &error));
631     EXPECT_TRUE(value == test_value);
632 
633     // Set property using brillo::Any variant type.
634     Stringmap new_value;
635     new_value["yesss"] = "noooo";
636     EXPECT_TRUE(store.SetAnyProperty(key, brillo::Any(new_value), &error));
637     EXPECT_TRUE(store.GetStringmapProperty(key, &test_value, &error));
638     EXPECT_TRUE(new_value == test_value);
639   }
640   {
641     // Register property value.
642     const string key = "stringsp";
643     Strings value;
644     string element;
645     element = "noooo";
646     value.push_back(element);
647     store.RegisterStrings(key, &value);
648 
649     // Verify property value.
650     Strings test_value;
651     Error error;
652     EXPECT_TRUE(store.GetStringsProperty(key, &test_value, &error));
653     EXPECT_TRUE(value == test_value);
654 
655     // Set property using brillo::Any variant type.
656     Strings new_value;
657     string new_element;
658     new_element = "yesss";
659     new_value.push_back(new_element);
660     EXPECT_TRUE(store.SetAnyProperty(key, brillo::Any(new_value), &error));
661     EXPECT_TRUE(store.GetStringsProperty(key, &test_value, &error));
662     EXPECT_TRUE(new_value == test_value);
663   }
664   {
665     // Register property value.
666     const string key = "uint8p";
667     uint8_t value = 127;
668     store.RegisterUint8(key, &value);
669 
670     // Verify property value.
671     uint8_t test_value;
672     Error error;
673     EXPECT_TRUE(store.GetUint8Property(key, &test_value, &error));
674     EXPECT_EQ(value, test_value);
675 
676     // Set property using brillo::Any variant type.
677     uint8_t new_value = 128;
678     EXPECT_TRUE(store.SetAnyProperty(key, brillo::Any(new_value), &error));
679     EXPECT_TRUE(store.GetUint8Property(key, &test_value, &error));
680     EXPECT_EQ(new_value, test_value);
681   }
682   {
683     // Register property value.
684     const string key = "uint16p";
685     uint16_t value = 127;
686     store.RegisterUint16(key, &value);
687 
688     // Verify property value.
689     uint16_t test_value;
690     Error error;
691     EXPECT_TRUE(store.GetUint16Property(key, &test_value, &error));
692     EXPECT_EQ(value, test_value);
693 
694     // Set property using brillo::Any variant type.
695     uint16_t new_value = 128;
696     EXPECT_TRUE(store.SetAnyProperty(key, brillo::Any(new_value), &error));
697     EXPECT_TRUE(store.GetUint16Property(key, &test_value, &error));
698     EXPECT_EQ(new_value, test_value);
699   }
700   {
701     // Register property value.
702     const string key = "uint32p";
703     uint32_t value = 127;
704     store.RegisterUint32(key, &value);
705 
706     // Verify property value.
707     uint32_t test_value;
708     Error error;
709     EXPECT_TRUE(store.GetUint32Property(key, &test_value, &error));
710     EXPECT_EQ(value, test_value);
711 
712     // Set property using brillo::Any variant type.
713     uint32_t new_value = 128;
714     EXPECT_TRUE(store.SetAnyProperty(key, brillo::Any(new_value), &error));
715     EXPECT_TRUE(store.GetUint32Property(key, &test_value, &error));
716     EXPECT_EQ(new_value, test_value);
717   }
718   {
719     // KeyValueStoreProperty is only defined for derived types so handle
720     // this case manually here.
721     const string key = "keyvaluestorep";
722     EXPECT_CALL(*this, GetKeyValueStoreCallback(_))
723         .WillOnce(Return(KeyValueStore()));
724     store.RegisterDerivedKeyValueStore(
725         key,
726         KeyValueStoreAccessor(
727             new CustomAccessor<PropertyStoreTest, KeyValueStore>(
728                 this, &PropertyStoreTest::GetKeyValueStoreCallback,
729                 &PropertyStoreTest::SetKeyValueStoreCallback)));
730 
731     brillo::VariantDictionary value;
732     EXPECT_CALL(*this, SetKeyValueStoreCallback(_, _)).WillOnce(Return(true));
733     Error error;
734     EXPECT_TRUE(store.SetAnyProperty(key, brillo::Any(value), &error));
735   }
736 }
737 
TEST_F(PropertyStoreTest,SetAndGetProperties)738 TEST_F(PropertyStoreTest, SetAndGetProperties) {
739   PropertyStore store;
740 
741   // Register properties.
742   const string kBoolKey = "boolp";
743   const string kKeyValueStoreKey = "keyvaluestorep";
744   const string kInt16Key = "int16p";
745   const string kInt32Key = "int32p";
746   const string kStringKey = "stringp";
747   const string kStringsKey = "stringsp";
748   const string kStringmapKey = "stringmapp";
749   const string kUint8Key = "uint8p";
750   const string kUint16Key = "uint16p";
751   const string kUint32Key = "uint32p";
752   bool bool_value = true;
753   int16_t int16_value = 16;
754   int32_t int32_value = 32;
755   string string_value = "string";
756   Stringmap stringmap_value;
757   stringmap_value["noooo"] = "yesss";
758   Strings strings_value;
759   strings_value.push_back("yesss");
760   uint8_t uint8_value = 8;
761   uint16_t uint16_value = 16;
762   uint32_t uint32_value = 32;
763 
764   store.RegisterBool(kBoolKey, &bool_value);
765   store.RegisterInt16(kInt16Key, &int16_value);
766   store.RegisterInt32(kInt32Key, &int32_value);
767   store.RegisterString(kStringKey, &string_value);
768   store.RegisterStrings(kStringsKey, &strings_value);
769   store.RegisterStringmap(kStringmapKey, &stringmap_value);
770   store.RegisterUint8(kUint8Key, &uint8_value);
771   store.RegisterUint16(kUint16Key, &uint16_value);
772   store.RegisterUint32(kUint32Key, &uint32_value);
773 
774   // Special handling for KeyValueStore property.
775   EXPECT_CALL(*this, GetKeyValueStoreCallback(_))
776       .WillOnce(Return(KeyValueStore()));
777   store.RegisterDerivedKeyValueStore(
778       kKeyValueStoreKey,
779       KeyValueStoreAccessor(
780           new CustomAccessor<PropertyStoreTest, KeyValueStore>(
781               this, &PropertyStoreTest::GetKeyValueStoreCallback,
782               &PropertyStoreTest::SetKeyValueStoreCallback)));
783 
784   // Update properties.
785   bool new_bool_value = false;
786   brillo::VariantDictionary new_key_value_store_value;
787   int16_t new_int16_value = 17;
788   int32_t new_int32_value = 33;
789   string new_string_value = "strings";
790   Stringmap new_stringmap_value;
791   new_stringmap_value["yesss"] = "noooo";
792   Strings new_strings_value;
793   new_strings_value.push_back("noooo");
794   uint8_t new_uint8_value = 9;
795   uint16_t new_uint16_value = 17;
796   uint32_t new_uint32_value = 33;
797 
798   brillo::VariantDictionary dict;
799   dict.insert(std::make_pair(kBoolKey, brillo::Any(new_bool_value)));
800   dict.insert(std::make_pair(kKeyValueStoreKey,
801                              brillo::Any(new_key_value_store_value)));
802   dict.insert(std::make_pair(kInt16Key, brillo::Any(new_int16_value)));
803   dict.insert(std::make_pair(kInt32Key, brillo::Any(new_int32_value)));
804   dict.insert(std::make_pair(kStringKey, brillo::Any(new_string_value)));
805   dict.insert(std::make_pair(kStringmapKey,
806                              brillo::Any(new_stringmap_value)));
807   dict.insert(std::make_pair(kStringsKey, brillo::Any(new_strings_value)));
808   dict.insert(std::make_pair(kUint8Key, brillo::Any(new_uint8_value)));
809   dict.insert(std::make_pair(kUint16Key, brillo::Any(new_uint16_value)));
810   dict.insert(std::make_pair(kUint32Key, brillo::Any(new_uint32_value)));
811 
812   EXPECT_CALL(*this, SetKeyValueStoreCallback(_, _)).WillOnce(Return(true));
813   Error error;
814   EXPECT_TRUE(store.SetProperties(dict, &error));
815 
816   // Retrieve properties.
817   EXPECT_CALL(*this, GetKeyValueStoreCallback(_))
818       .WillOnce(Return(KeyValueStore()));
819   brillo::VariantDictionary result_dict;
820   EXPECT_TRUE(store.GetProperties(&result_dict, &error));
821 
822   // Verify property values.
823   EXPECT_EQ(new_bool_value, result_dict[kBoolKey].Get<bool>());
824   EXPECT_EQ(new_int16_value, result_dict[kInt16Key].Get<int16_t>());
825   EXPECT_EQ(new_int32_value, result_dict[kInt32Key].Get<int32_t>());
826   EXPECT_EQ(new_string_value, result_dict[kStringKey].Get<string>());
827   EXPECT_TRUE(
828       new_stringmap_value == result_dict[kStringmapKey].Get<Stringmap>());
829   EXPECT_TRUE(new_strings_value == result_dict[kStringsKey].Get<Strings>());
830   EXPECT_EQ(new_uint8_value, result_dict[kUint8Key].Get<uint8_t>());
831   EXPECT_EQ(new_uint16_value, result_dict[kUint16Key].Get<uint16_t>());
832   EXPECT_EQ(new_uint32_value, result_dict[kUint32Key].Get<uint32_t>());
833 }
834 
835 }  // namespace shill
836