Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1 | // 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 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 | |
| 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 Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 18 | #include "shill/event_dispatcher.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 19 | #include "shill/manager.h" |
| 20 | #include "shill/mock_control.h" |
| 21 | #include "shill/property_store.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 22 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 23 | using std::make_pair; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 24 | using std::map; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 25 | using std::string; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 26 | using std::vector; |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 27 | using ::testing::Values; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 28 | |
| 29 | namespace shill { |
| 30 | |
| 31 | // static |
| 32 | const ::DBus::Variant PropertyStoreTest::kBoolV = DBusAdaptor::BoolToVariant(0); |
| 33 | // static |
| 34 | const ::DBus::Variant PropertyStoreTest::kByteV = DBusAdaptor::ByteToVariant(0); |
| 35 | // static |
| 36 | const ::DBus::Variant PropertyStoreTest::kInt16V = |
| 37 | DBusAdaptor::Int16ToVariant(0); |
| 38 | // static |
| 39 | const ::DBus::Variant PropertyStoreTest::kInt32V = |
| 40 | DBusAdaptor::Int32ToVariant(0); |
| 41 | // static |
| 42 | const ::DBus::Variant PropertyStoreTest::kStringV = |
| 43 | DBusAdaptor::StringToVariant(""); |
| 44 | // static |
| 45 | const ::DBus::Variant PropertyStoreTest::kStringmapV = |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 46 | DBusAdaptor::StringmapToVariant(Stringmap()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 47 | // static |
| 48 | const ::DBus::Variant PropertyStoreTest::kStringmapsV = |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 49 | DBusAdaptor::StringmapsToVariant(Stringmaps()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 50 | // static |
| 51 | const ::DBus::Variant PropertyStoreTest::kStringsV = |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 52 | DBusAdaptor::StringsToVariant(Strings(1, "")); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 53 | // static |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 54 | const ::DBus::Variant PropertyStoreTest::kStrIntPairV = |
| 55 | DBusAdaptor::StrIntPairToVariant(StrIntPair(make_pair("", ""), |
| 56 | make_pair("", 12))); |
| 57 | // static |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 58 | const ::DBus::Variant PropertyStoreTest::kUint16V = |
| 59 | DBusAdaptor::Uint16ToVariant(0); |
| 60 | // static |
| 61 | const ::DBus::Variant PropertyStoreTest::kUint32V = |
| 62 | DBusAdaptor::Uint32ToVariant(0); |
| 63 | |
| 64 | PropertyStoreTest::PropertyStoreTest() |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 65 | : internal_error_(Error::GetName(Error::kInternalError)), |
| 66 | invalid_args_(Error::GetName(Error::kInvalidArguments)), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 67 | invalid_prop_(Error::GetName(Error::kInvalidProperty)), |
| 68 | path_(dir_.CreateUniqueTempDir() ? dir_.path().value() : ""), |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 69 | manager_(control_interface(), |
| 70 | dispatcher(), |
| 71 | glib(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 72 | run_path(), |
| 73 | storage_path(), |
| 74 | string()) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | PropertyStoreTest::~PropertyStoreTest() {} |
| 78 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 79 | void PropertyStoreTest::SetUp() { |
| 80 | ASSERT_FALSE(run_path().empty()); |
| 81 | ASSERT_FALSE(storage_path().empty()); |
| 82 | } |
| 83 | |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 84 | TEST_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 Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 89 | EXPECT_EQ(invalid_prop(), error.name()); |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | INSTANTIATE_TEST_CASE_P( |
| 93 | PropertyStoreTestInstance, |
| 94 | PropertyStoreTest, |
| 95 | Values(PropertyStoreTest::kBoolV, |
| 96 | PropertyStoreTest::kByteV, |
| 97 | PropertyStoreTest::kStringV, |
| 98 | PropertyStoreTest::kInt16V, |
| 99 | PropertyStoreTest::kInt32V, |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 100 | PropertyStoreTest::kStringmapV, |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 101 | PropertyStoreTest::kStringsV, |
| 102 | PropertyStoreTest::kStrIntPairV, |
| 103 | PropertyStoreTest::kUint16V, |
| 104 | PropertyStoreTest::kUint32V)); |
Chris Masone | 8a56ad6 | 2011-07-02 15:27:57 -0700 | [diff] [blame] | 105 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 106 | TEST_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 Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 114 | TEST_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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 272 | } // namespace shill |