Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 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 Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 9 | #include <utility> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 12 | #include <base/basictypes.h> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 13 | #include <dbus-c++/dbus.h> |
| 14 | #include <gtest/gtest.h> |
| 15 | #include <gmock/gmock.h> |
| 16 | |
| 17 | #include "shill/dbus_adaptor.h" |
| 18 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 19 | #include "shill/event_dispatcher.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 20 | #include "shill/manager.h" |
| 21 | #include "shill/mock_control.h" |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 22 | #include "shill/property_accessor.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 23 | #include "shill/property_store.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 24 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 25 | using base::Bind; |
| 26 | using base::Unretained; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 27 | using std::map; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 28 | using std::string; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 29 | using std::vector; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 30 | using ::testing::_; |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 31 | using ::testing::Values; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 32 | |
| 33 | namespace shill { |
| 34 | |
| 35 | // static |
| 36 | const ::DBus::Variant PropertyStoreTest::kBoolV = DBusAdaptor::BoolToVariant(0); |
| 37 | // static |
| 38 | const ::DBus::Variant PropertyStoreTest::kByteV = DBusAdaptor::ByteToVariant(0); |
| 39 | // static |
| 40 | const ::DBus::Variant PropertyStoreTest::kInt16V = |
| 41 | DBusAdaptor::Int16ToVariant(0); |
| 42 | // static |
| 43 | const ::DBus::Variant PropertyStoreTest::kInt32V = |
| 44 | DBusAdaptor::Int32ToVariant(0); |
| 45 | // static |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 46 | const ::DBus::Variant PropertyStoreTest::kKeyValueStoreV = |
| 47 | DBusAdaptor::KeyValueStoreToVariant(KeyValueStore()); |
| 48 | // static |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 49 | const ::DBus::Variant PropertyStoreTest::kStringV = |
| 50 | DBusAdaptor::StringToVariant(""); |
| 51 | // static |
| 52 | const ::DBus::Variant PropertyStoreTest::kStringmapV = |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 53 | DBusAdaptor::StringmapToVariant(Stringmap()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 54 | // static |
| 55 | const ::DBus::Variant PropertyStoreTest::kStringmapsV = |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 56 | DBusAdaptor::StringmapsToVariant(Stringmaps()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 57 | // static |
| 58 | const ::DBus::Variant PropertyStoreTest::kStringsV = |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 59 | DBusAdaptor::StringsToVariant(Strings(1, "")); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 60 | // static |
| 61 | const ::DBus::Variant PropertyStoreTest::kUint16V = |
| 62 | DBusAdaptor::Uint16ToVariant(0); |
| 63 | // static |
| 64 | const ::DBus::Variant PropertyStoreTest::kUint32V = |
| 65 | DBusAdaptor::Uint32ToVariant(0); |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 66 | // static |
| 67 | const ::DBus::Variant PropertyStoreTest::kUint64V = |
| 68 | DBusAdaptor::Uint64ToVariant(0); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 69 | |
| 70 | PropertyStoreTest::PropertyStoreTest() |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 71 | : internal_error_(Error::GetName(Error::kInternalError)), |
| 72 | invalid_args_(Error::GetName(Error::kInvalidArguments)), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 73 | invalid_prop_(Error::GetName(Error::kInvalidProperty)), |
| 74 | path_(dir_.CreateUniqueTempDir() ? dir_.path().value() : ""), |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 75 | metrics_(dispatcher()), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 76 | manager_(control_interface(), |
| 77 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 78 | metrics(), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 79 | glib(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 80 | run_path(), |
| 81 | storage_path(), |
| 82 | string()) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | PropertyStoreTest::~PropertyStoreTest() {} |
| 86 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 87 | void PropertyStoreTest::SetUp() { |
| 88 | ASSERT_FALSE(run_path().empty()); |
| 89 | ASSERT_FALSE(storage_path().empty()); |
| 90 | } |
| 91 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 92 | TEST_P(PropertyStoreTest, SetPropertyNonexistent) { |
| 93 | // Ensure that an attempt to write unknown properties returns |
| 94 | // InvalidProperty, and does not yield a PropertyChange callback. |
| 95 | PropertyStore store(Bind(&PropertyStoreTest::TestCallback, |
| 96 | Unretained(this))); |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 97 | ::DBus::Error error; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 98 | EXPECT_CALL(*this, TestCallback(_)).Times(0); |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 99 | EXPECT_FALSE(DBusAdaptor::SetProperty(&store, "", GetParam(), &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 100 | EXPECT_EQ(invalid_prop(), error.name()); |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | INSTANTIATE_TEST_CASE_P( |
| 104 | PropertyStoreTestInstance, |
| 105 | PropertyStoreTest, |
| 106 | Values(PropertyStoreTest::kBoolV, |
| 107 | PropertyStoreTest::kByteV, |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 108 | PropertyStoreTest::kInt16V, |
| 109 | PropertyStoreTest::kInt32V, |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 110 | PropertyStoreTest::kStringV, |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 111 | PropertyStoreTest::kStringmapV, |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 112 | PropertyStoreTest::kStringsV, |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 113 | PropertyStoreTest::kUint16V, |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 114 | PropertyStoreTest::kUint32V, |
| 115 | PropertyStoreTest::kUint64V)); |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 116 | |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 117 | template <typename T> |
| 118 | class PropertyStoreTypedTest : public PropertyStoreTest { |
| 119 | protected: |
| 120 | void RegisterProperty( |
| 121 | PropertyStore &store, const string &name, T *storage); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 122 | bool SetProperty( |
| 123 | PropertyStore &store, const string &name, Error *error); |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | typedef ::testing::Types< |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 127 | bool, int16, int32, string, Stringmap, Stringmaps, Strings, uint8, uint16, |
| 128 | uint32> PropertyTypes; |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 129 | TYPED_TEST_CASE(PropertyStoreTypedTest, PropertyTypes); |
| 130 | |
| 131 | TYPED_TEST(PropertyStoreTypedTest, ClearProperty) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 132 | PropertyStore store(Bind(&PropertyStoreTest::TestCallback, |
| 133 | Unretained(this))); |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 134 | Error error; |
| 135 | TypeParam property; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 136 | |
Han Shen | fc34925 | 2012-08-30 11:36:04 -0700 | [diff] [blame] | 137 | // |this| required due to two-phase lookup. |
| 138 | this->RegisterProperty(store, "some property", &property); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 139 | EXPECT_CALL(*this, TestCallback(_)); |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 140 | EXPECT_TRUE(store.ClearProperty("some property", &error)); |
| 141 | } |
| 142 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 143 | TYPED_TEST(PropertyStoreTypedTest, SetProperty) { |
| 144 | PropertyStore store(Bind(&PropertyStoreTest::TestCallback, |
| 145 | Unretained(this))); |
| 146 | Error error; |
| 147 | TypeParam property = TypeParam(); // value-initialize primitives |
| 148 | |
| 149 | // |this| required due to two-phase lookup. |
| 150 | this->RegisterProperty(store, "some property", &property); |
| 151 | |
| 152 | // Change the value from the default (initialized above). Should |
| 153 | // generate a change callback. The second SetProperty, however, |
| 154 | // should not. Hence, we should get exactly one callback. |
| 155 | EXPECT_CALL(*this, TestCallback(_)).Times(1); |
| 156 | EXPECT_TRUE(this->SetProperty(store, "some property", &error)); |
| 157 | EXPECT_FALSE(this->SetProperty(store, "some property", &error)); |
| 158 | } |
| 159 | |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 160 | template<> void PropertyStoreTypedTest<bool>::RegisterProperty( |
| 161 | PropertyStore &store, const string &name, bool *storage) { |
| 162 | store.RegisterBool(name, storage); |
| 163 | } |
| 164 | |
| 165 | template<> void PropertyStoreTypedTest<int16>::RegisterProperty( |
| 166 | PropertyStore &store, const string &name, int16 *storage) { |
| 167 | store.RegisterInt16(name, storage); |
| 168 | } |
| 169 | |
| 170 | template<> void PropertyStoreTypedTest<int32>::RegisterProperty( |
| 171 | PropertyStore &store, const string &name, int32 *storage) { |
| 172 | store.RegisterInt32(name, storage); |
| 173 | } |
| 174 | |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 175 | template<> void PropertyStoreTypedTest<string>::RegisterProperty( |
| 176 | PropertyStore &store, const string &name, string *storage) { |
| 177 | store.RegisterString(name, storage); |
| 178 | } |
| 179 | |
| 180 | template<> void PropertyStoreTypedTest<Stringmap>::RegisterProperty( |
| 181 | PropertyStore &store, const string &name, Stringmap *storage) { |
| 182 | store.RegisterStringmap(name, storage); |
| 183 | } |
| 184 | |
| 185 | template<> void PropertyStoreTypedTest<Stringmaps>::RegisterProperty( |
| 186 | PropertyStore &store, const string &name, Stringmaps *storage) { |
| 187 | store.RegisterStringmaps(name, storage); |
| 188 | } |
| 189 | |
| 190 | template<> void PropertyStoreTypedTest<Strings>::RegisterProperty( |
| 191 | PropertyStore &store, const string &name, Strings *storage) { |
| 192 | store.RegisterStrings(name, storage); |
| 193 | } |
| 194 | |
| 195 | template<> void PropertyStoreTypedTest<uint8>::RegisterProperty( |
| 196 | PropertyStore &store, const string &name, uint8 *storage) { |
| 197 | store.RegisterUint8(name, storage); |
| 198 | } |
| 199 | |
| 200 | template<> void PropertyStoreTypedTest<uint16>::RegisterProperty( |
| 201 | PropertyStore &store, const string &name, uint16 *storage) { |
| 202 | store.RegisterUint16(name, storage); |
| 203 | } |
| 204 | |
| 205 | template<> void PropertyStoreTypedTest<uint32>::RegisterProperty( |
| 206 | PropertyStore &store, const string &name, uint32 *storage) { |
| 207 | store.RegisterUint32(name, storage); |
| 208 | } |
| 209 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 210 | template<> bool PropertyStoreTypedTest<bool>::SetProperty( |
| 211 | PropertyStore &store, const string &name, Error *error) { |
| 212 | bool new_value = true; |
| 213 | return store.SetBoolProperty(name, new_value, error); |
| 214 | } |
| 215 | |
| 216 | template<> bool PropertyStoreTypedTest<int16>::SetProperty( |
| 217 | PropertyStore &store, const string &name, Error *error) { |
| 218 | int16 new_value = 1; |
| 219 | return store.SetInt16Property(name, new_value, error); |
| 220 | } |
| 221 | |
| 222 | template<> bool PropertyStoreTypedTest<int32>::SetProperty( |
| 223 | PropertyStore &store, const string &name, Error *error) { |
| 224 | int32 new_value = 1; |
| 225 | return store.SetInt32Property(name, new_value, error); |
| 226 | } |
| 227 | |
| 228 | template<> bool PropertyStoreTypedTest<string>::SetProperty( |
| 229 | PropertyStore &store, const string &name, Error *error) { |
| 230 | string new_value = "new value"; |
| 231 | return store.SetStringProperty(name, new_value, error); |
| 232 | } |
| 233 | |
| 234 | template<> bool PropertyStoreTypedTest<Stringmap>::SetProperty( |
| 235 | PropertyStore &store, const string &name, Error *error) { |
| 236 | Stringmap new_value; |
| 237 | new_value["new key"] = "new value"; |
| 238 | return store.SetStringmapProperty(name, new_value, error); |
| 239 | } |
| 240 | |
| 241 | template<> bool PropertyStoreTypedTest<Stringmaps>::SetProperty( |
| 242 | PropertyStore &store, const string &name, Error *error) { |
| 243 | Stringmaps new_value(1); |
| 244 | new_value[0]["new key"] = "new value"; |
| 245 | return store.SetStringmapsProperty(name, new_value, error); |
| 246 | } |
| 247 | |
| 248 | template<> bool PropertyStoreTypedTest<Strings>::SetProperty( |
| 249 | PropertyStore &store, const string &name, Error *error) { |
| 250 | Strings new_value(1); |
| 251 | new_value[0] = "new value"; |
| 252 | return store.SetStringsProperty(name, new_value, error); |
| 253 | } |
| 254 | |
| 255 | template<> bool PropertyStoreTypedTest<uint8>::SetProperty( |
| 256 | PropertyStore &store, const string &name, Error *error) { |
| 257 | uint8 new_value = 1; |
| 258 | return store.SetUint8Property(name, new_value, error); |
| 259 | } |
| 260 | |
| 261 | template<> bool PropertyStoreTypedTest<uint16>::SetProperty( |
| 262 | PropertyStore &store, const string &name, Error *error) { |
| 263 | uint16 new_value = 1; |
| 264 | return store.SetUint16Property(name, new_value, error); |
| 265 | } |
| 266 | |
| 267 | template<> bool PropertyStoreTypedTest<uint32>::SetProperty( |
| 268 | PropertyStore &store, const string &name, Error *error) { |
| 269 | uint32 new_value = 1; |
| 270 | return store.SetUint32Property(name, new_value, error); |
| 271 | } |
| 272 | |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 273 | TEST_F(PropertyStoreTest, ClearBoolProperty) { |
| 274 | // We exercise both possibilities for the default value here, |
| 275 | // to ensure that Clear actually resets the property based on |
| 276 | // the property's initial value (rather than the language's |
| 277 | // default value for the type). |
| 278 | static const bool kDefaults[] = {true, false}; |
| 279 | for (size_t i = 0; i < arraysize(kDefaults); ++i) { |
| 280 | PropertyStore store; |
| 281 | Error error; |
| 282 | |
| 283 | const bool default_value = kDefaults[i]; |
| 284 | bool flag = default_value; |
| 285 | store.RegisterBool("some bool", &flag); |
| 286 | |
| 287 | EXPECT_TRUE(store.ClearProperty("some bool", &error)); |
| 288 | EXPECT_EQ(default_value, flag); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | TEST_F(PropertyStoreTest, ClearPropertyNonexistent) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 293 | PropertyStore store(Bind(&PropertyStoreTest::TestCallback, |
| 294 | Unretained(this))); |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 295 | Error error; |
| 296 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 297 | EXPECT_CALL(*this, TestCallback(_)).Times(0); |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 298 | EXPECT_FALSE(store.ClearProperty("", &error)); |
| 299 | EXPECT_EQ(Error::kInvalidProperty, error.type()); |
| 300 | } |
| 301 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 302 | // Separate from SetPropertyNonexistent, because |
| 303 | // DBusAdaptor::SetProperty doesn't support Stringmaps. |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 304 | TEST_F(PropertyStoreTest, SetStringmapsProperty) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 305 | PropertyStore store(Bind(&PropertyStoreTest::TestCallback, |
| 306 | Unretained(this))); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 307 | ::DBus::Error error; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 308 | EXPECT_CALL(*this, TestCallback(_)).Times(0); |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 309 | EXPECT_FALSE(DBusAdaptor::SetProperty( |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 310 | &store, "", PropertyStoreTest::kStringmapsV, &error)); |
| 311 | EXPECT_EQ(internal_error(), error.name()); |
| 312 | } |
| 313 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 314 | // Separate from SetPropertyNonexistent, because |
| 315 | // DBusAdaptor::SetProperty doesn't support KeyValueStore. |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 316 | TEST_F(PropertyStoreTest, SetKeyValueStoreProperty) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 317 | PropertyStore store(Bind(&PropertyStoreTest::TestCallback, |
| 318 | Unretained(this))); |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 319 | ::DBus::Error error; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 320 | EXPECT_CALL(*this, TestCallback(_)).Times(0); |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 321 | EXPECT_FALSE(DBusAdaptor::SetProperty( |
| 322 | &store, "", PropertyStoreTest::kKeyValueStoreV, &error)); |
| 323 | EXPECT_EQ(internal_error(), error.name()); |
| 324 | } |
| 325 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 326 | TEST_F(PropertyStoreTest, WriteOnlyProperties) { |
| 327 | // Test that properties registered as write-only are not returned |
| 328 | // when using Get*PropertiesIter(). |
| 329 | PropertyStore store; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 330 | { |
| 331 | const string keys[] = {"boolp1", "boolp2"}; |
| 332 | bool values[] = {true, true}; |
| 333 | store.RegisterWriteOnlyBool(keys[0], &values[0]); |
| 334 | store.RegisterBool(keys[1], &values[1]); |
| 335 | |
| 336 | ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter(); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 337 | EXPECT_FALSE(it.AtEnd()); |
| 338 | EXPECT_EQ(keys[1], it.Key()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 339 | EXPECT_TRUE(values[1] == it.value()); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 340 | it.Advance(); |
| 341 | EXPECT_TRUE(it.AtEnd()); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 342 | |
| 343 | Error errors[2]; |
| 344 | EXPECT_FALSE(store.GetBoolProperty(keys[0], NULL, &errors[0])); |
| 345 | EXPECT_EQ(Error::kPermissionDenied, errors[0].type()); |
| 346 | bool test_value; |
| 347 | EXPECT_TRUE(store.GetBoolProperty(keys[1], &test_value, &errors[1])); |
| 348 | EXPECT_TRUE(errors[1].IsSuccess()); |
| 349 | EXPECT_EQ(values[1], test_value); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 350 | } |
| 351 | { |
| 352 | const string keys[] = {"int16p1", "int16p2"}; |
| 353 | int16 values[] = {127, 128}; |
| 354 | store.RegisterWriteOnlyInt16(keys[0], &values[0]); |
| 355 | store.RegisterInt16(keys[1], &values[1]); |
| 356 | |
| 357 | ReadablePropertyConstIterator<int16> it = store.GetInt16PropertiesIter(); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 358 | EXPECT_FALSE(it.AtEnd()); |
| 359 | EXPECT_EQ(keys[1], it.Key()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 360 | EXPECT_EQ(values[1], it.value()); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 361 | it.Advance(); |
| 362 | EXPECT_TRUE(it.AtEnd()); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 363 | |
| 364 | Error errors[2]; |
| 365 | EXPECT_FALSE(store.GetInt16Property(keys[0], NULL, &errors[0])); |
| 366 | EXPECT_EQ(Error::kPermissionDenied, errors[0].type()); |
| 367 | int16 test_value; |
| 368 | EXPECT_TRUE(store.GetInt16Property(keys[1], &test_value, &errors[1])); |
| 369 | EXPECT_TRUE(errors[1].IsSuccess()); |
| 370 | EXPECT_EQ(values[1], test_value); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 371 | } |
| 372 | { |
| 373 | const string keys[] = {"int32p1", "int32p2"}; |
| 374 | int32 values[] = {127, 128}; |
| 375 | store.RegisterWriteOnlyInt32(keys[0], &values[0]); |
| 376 | store.RegisterInt32(keys[1], &values[1]); |
| 377 | |
| 378 | ReadablePropertyConstIterator<int32> it = store.GetInt32PropertiesIter(); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 379 | EXPECT_FALSE(it.AtEnd()); |
| 380 | EXPECT_EQ(keys[1], it.Key()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 381 | EXPECT_EQ(values[1], it.value()); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 382 | it.Advance(); |
| 383 | EXPECT_TRUE(it.AtEnd()); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 384 | |
| 385 | Error errors[2]; |
| 386 | EXPECT_FALSE(store.GetInt32Property(keys[0], NULL, &errors[0])); |
| 387 | EXPECT_EQ(Error::kPermissionDenied, errors[0].type()); |
| 388 | int32 test_value; |
| 389 | EXPECT_TRUE(store.GetInt32Property(keys[1], &test_value, &errors[1])); |
| 390 | EXPECT_TRUE(errors[1].IsSuccess()); |
| 391 | EXPECT_EQ(values[1], test_value); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 392 | } |
| 393 | { |
| 394 | const string keys[] = {"stringp1", "stringp2"}; |
| 395 | string values[] = {"noooo", "yesss"}; |
| 396 | store.RegisterWriteOnlyString(keys[0], &values[0]); |
| 397 | store.RegisterString(keys[1], &values[1]); |
| 398 | |
| 399 | ReadablePropertyConstIterator<string> it = store.GetStringPropertiesIter(); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 400 | EXPECT_FALSE(it.AtEnd()); |
| 401 | EXPECT_EQ(keys[1], it.Key()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 402 | EXPECT_EQ(values[1], it.value()); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 403 | it.Advance(); |
| 404 | EXPECT_TRUE(it.AtEnd()); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 405 | |
| 406 | Error errors[2]; |
| 407 | EXPECT_FALSE(store.GetStringProperty(keys[0], NULL, &errors[0])); |
| 408 | EXPECT_EQ(Error::kPermissionDenied, errors[0].type()); |
| 409 | string test_value; |
| 410 | EXPECT_TRUE(store.GetStringProperty(keys[1], &test_value, &errors[1])); |
| 411 | EXPECT_TRUE(errors[1].IsSuccess()); |
| 412 | EXPECT_EQ(values[1], test_value); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 413 | } |
| 414 | { |
| 415 | const string keys[] = {"stringmapp1", "stringmapp2"}; |
| 416 | Stringmap values[2]; |
| 417 | values[0]["noooo"] = "yesss"; |
| 418 | values[1]["yesss"] = "noooo"; |
| 419 | store.RegisterWriteOnlyStringmap(keys[0], &values[0]); |
| 420 | store.RegisterStringmap(keys[1], &values[1]); |
| 421 | |
| 422 | ReadablePropertyConstIterator<Stringmap> it = |
| 423 | store.GetStringmapPropertiesIter(); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 424 | EXPECT_FALSE(it.AtEnd()); |
| 425 | EXPECT_EQ(keys[1], it.Key()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 426 | EXPECT_TRUE(values[1] == it.value()); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 427 | it.Advance(); |
| 428 | EXPECT_TRUE(it.AtEnd()); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 429 | |
| 430 | Error errors[2]; |
| 431 | EXPECT_FALSE(store.GetStringmapProperty(keys[0], NULL, &errors[0])); |
| 432 | EXPECT_EQ(Error::kPermissionDenied, errors[0].type()); |
| 433 | Stringmap test_value; |
| 434 | EXPECT_TRUE(store.GetStringmapProperty(keys[1], &test_value, &errors[1])); |
| 435 | EXPECT_TRUE(errors[1].IsSuccess()); |
| 436 | EXPECT_TRUE(values[1] == test_value); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 437 | } |
| 438 | { |
| 439 | const string keys[] = {"stringmapsp1", "stringmapsp2"}; |
| 440 | Stringmaps values[2]; |
| 441 | Stringmap element; |
| 442 | element["noooo"] = "yesss"; |
| 443 | values[0].push_back(element); |
| 444 | element["yesss"] = "noooo"; |
| 445 | values[1].push_back(element); |
| 446 | |
| 447 | store.RegisterWriteOnlyStringmaps(keys[0], &values[0]); |
| 448 | store.RegisterStringmaps(keys[1], &values[1]); |
| 449 | |
| 450 | ReadablePropertyConstIterator<Stringmaps> it = |
| 451 | store.GetStringmapsPropertiesIter(); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 452 | EXPECT_FALSE(it.AtEnd()); |
| 453 | EXPECT_EQ(keys[1], it.Key()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 454 | EXPECT_TRUE(values[1] == it.value()); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 455 | it.Advance(); |
| 456 | EXPECT_TRUE(it.AtEnd()); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 457 | |
| 458 | Error errors[2]; |
| 459 | EXPECT_FALSE(store.GetStringmapsProperty(keys[0], NULL, &errors[0])); |
| 460 | EXPECT_EQ(Error::kPermissionDenied, errors[0].type()); |
| 461 | Stringmaps test_value; |
| 462 | EXPECT_TRUE(store.GetStringmapsProperty(keys[1], &test_value, &errors[1])); |
| 463 | EXPECT_TRUE(errors[1].IsSuccess()); |
| 464 | EXPECT_TRUE(values[1] == test_value); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 465 | } |
| 466 | { |
| 467 | const string keys[] = {"stringsp1", "stringsp2"}; |
| 468 | Strings values[2]; |
| 469 | string element; |
| 470 | element = "noooo"; |
| 471 | values[0].push_back(element); |
| 472 | element = "yesss"; |
| 473 | values[1].push_back(element); |
| 474 | store.RegisterWriteOnlyStrings(keys[0], &values[0]); |
| 475 | store.RegisterStrings(keys[1], &values[1]); |
| 476 | |
| 477 | ReadablePropertyConstIterator<Strings> it = |
| 478 | store.GetStringsPropertiesIter(); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 479 | EXPECT_FALSE(it.AtEnd()); |
| 480 | EXPECT_EQ(keys[1], it.Key()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 481 | EXPECT_TRUE(values[1] == it.value()); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 482 | it.Advance(); |
| 483 | EXPECT_TRUE(it.AtEnd()); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 484 | |
| 485 | Error errors[2]; |
| 486 | EXPECT_FALSE(store.GetStringsProperty(keys[0], NULL, &errors[0])); |
| 487 | EXPECT_EQ(Error::kPermissionDenied, errors[0].type()); |
| 488 | Strings test_value; |
| 489 | EXPECT_TRUE(store.GetStringsProperty(keys[1], &test_value, &errors[1])); |
| 490 | EXPECT_TRUE(errors[1].IsSuccess()); |
| 491 | EXPECT_TRUE(values[1] == test_value); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 492 | } |
| 493 | { |
| 494 | const string keys[] = {"uint8p1", "uint8p2"}; |
| 495 | uint8 values[] = {127, 128}; |
| 496 | store.RegisterWriteOnlyUint8(keys[0], &values[0]); |
| 497 | store.RegisterUint8(keys[1], &values[1]); |
| 498 | |
| 499 | ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter(); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 500 | EXPECT_FALSE(it.AtEnd()); |
| 501 | EXPECT_EQ(keys[1], it.Key()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 502 | EXPECT_EQ(values[1], it.value()); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 503 | it.Advance(); |
| 504 | EXPECT_TRUE(it.AtEnd()); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 505 | |
| 506 | Error errors[2]; |
| 507 | EXPECT_FALSE(store.GetUint8Property(keys[0], NULL, &errors[0])); |
| 508 | EXPECT_EQ(Error::kPermissionDenied, errors[0].type()); |
| 509 | uint8 test_value; |
| 510 | EXPECT_TRUE(store.GetUint8Property(keys[1], &test_value, &errors[1])); |
| 511 | EXPECT_TRUE(errors[1].IsSuccess()); |
| 512 | EXPECT_EQ(values[1], test_value); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 513 | } |
| 514 | { |
| 515 | const string keys[] = {"uint16p", "uint16p1"}; |
| 516 | uint16 values[] = {127, 128}; |
| 517 | store.RegisterWriteOnlyUint16(keys[0], &values[0]); |
| 518 | store.RegisterUint16(keys[1], &values[1]); |
| 519 | |
| 520 | ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter(); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 521 | EXPECT_FALSE(it.AtEnd()); |
| 522 | EXPECT_EQ(keys[1], it.Key()); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 523 | EXPECT_EQ(values[1], it.value()); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 524 | it.Advance(); |
| 525 | EXPECT_TRUE(it.AtEnd()); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 526 | |
| 527 | Error errors[2]; |
| 528 | EXPECT_FALSE(store.GetUint16Property(keys[0], NULL, &errors[0])); |
| 529 | EXPECT_EQ(Error::kPermissionDenied, errors[0].type()); |
| 530 | uint16 test_value; |
| 531 | EXPECT_TRUE(store.GetUint16Property(keys[1], &test_value, &errors[1])); |
| 532 | EXPECT_TRUE(errors[1].IsSuccess()); |
| 533 | EXPECT_EQ(values[1], test_value); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 537 | } // namespace shill |