Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame^] | 1 | #include <systemd/sd-bus-protocol.h> |
| 2 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 3 | #include <cerrno> |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 4 | #include <map> |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 5 | #include <sdbusplus/exception.hpp> |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 6 | #include <sdbusplus/message.hpp> |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 7 | #include <sdbusplus/test/sdbus_mock.hpp> |
Ed Tanous | 28dc36d | 2018-02-21 12:22:54 -0800 | [diff] [blame] | 8 | #include <set> |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 9 | #include <string> |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 10 | #include <tuple> |
| 11 | #include <unordered_map> |
| 12 | #include <vector> |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 13 | |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame^] | 14 | #include <gmock/gmock.h> |
| 15 | #include <gtest/gtest.h> |
| 16 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 17 | namespace |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 18 | { |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 19 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 20 | using testing::DoAll; |
| 21 | using testing::Return; |
| 22 | using testing::StrEq; |
| 23 | |
| 24 | ACTION_TEMPLATE(AssignReadVal, HAS_1_TEMPLATE_PARAMS(typename, T), |
| 25 | AND_1_VALUE_PARAMS(val)) |
| 26 | { |
| 27 | *static_cast<T *>(arg2) = val; |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 28 | } |
| 29 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 30 | class ReadTest : public testing::Test |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 31 | { |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 32 | protected: |
| 33 | testing::StrictMock<sdbusplus::SdBusMock> mock; |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 34 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 35 | void SetUp() override |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 36 | { |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 37 | EXPECT_CALL(mock, sd_bus_message_new_method_call(testing::_, testing::_, |
| 38 | nullptr, nullptr, |
| 39 | nullptr, nullptr)) |
| 40 | .WillRepeatedly(Return(0)); |
| 41 | }; |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 42 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 43 | sdbusplus::message::message new_message() |
| 44 | { |
| 45 | return sdbusplus::get_mocked_new(&mock).new_method_call( |
| 46 | nullptr, nullptr, nullptr, nullptr); |
| 47 | } |
| 48 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 49 | void expect_basic_error(char type, int ret) |
| 50 | { |
| 51 | EXPECT_CALL(mock, sd_bus_message_read_basic(nullptr, type, testing::_)) |
| 52 | .WillOnce(Return(ret)); |
| 53 | } |
| 54 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 55 | template <typename T> void expect_basic(char type, T val) |
| 56 | { |
| 57 | EXPECT_CALL(mock, sd_bus_message_read_basic(nullptr, type, testing::_)) |
| 58 | .WillOnce(DoAll(AssignReadVal<T>(val), Return(0))); |
| 59 | } |
| 60 | |
| 61 | void expect_verify_type(char type, const char *contents, int ret) |
| 62 | { |
| 63 | EXPECT_CALL(mock, |
| 64 | sd_bus_message_verify_type(nullptr, type, StrEq(contents))) |
| 65 | .WillOnce(Return(ret)); |
| 66 | } |
| 67 | |
| 68 | void expect_at_end(bool complete, int ret) |
| 69 | { |
| 70 | EXPECT_CALL(mock, sd_bus_message_at_end(nullptr, complete)) |
| 71 | .WillOnce(Return(ret)); |
| 72 | } |
| 73 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 74 | void expect_skip(const char *contents, int ret = 0) |
William A. Kennington III | 60a7283 | 2018-06-26 14:31:15 -0700 | [diff] [blame] | 75 | { |
| 76 | EXPECT_CALL(mock, sd_bus_message_skip(nullptr, StrEq(contents))) |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 77 | .WillOnce(Return(ret)); |
William A. Kennington III | 60a7283 | 2018-06-26 14:31:15 -0700 | [diff] [blame] | 78 | } |
| 79 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 80 | void expect_enter_container(char type, const char *contents, int ret = 0) |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 81 | { |
| 82 | EXPECT_CALL(mock, sd_bus_message_enter_container(nullptr, type, |
| 83 | StrEq(contents))) |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 84 | .WillOnce(Return(ret)); |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 85 | } |
| 86 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 87 | void expect_exit_container(int ret = 0) |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 88 | { |
| 89 | EXPECT_CALL(mock, sd_bus_message_exit_container(nullptr)) |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 90 | .WillOnce(Return(ret)); |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 91 | } |
| 92 | }; |
| 93 | |
| 94 | TEST_F(ReadTest, Int) |
| 95 | { |
| 96 | const int i = 1; |
| 97 | expect_basic<int>(SD_BUS_TYPE_INT32, i); |
| 98 | int ret; |
| 99 | new_message().read(ret); |
| 100 | EXPECT_EQ(i, ret); |
| 101 | } |
| 102 | |
| 103 | TEST_F(ReadTest, Bool) |
| 104 | { |
| 105 | const bool b = true; |
| 106 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, b); |
| 107 | bool ret; |
| 108 | new_message().read(ret); |
| 109 | EXPECT_EQ(b, ret); |
| 110 | } |
| 111 | |
| 112 | TEST_F(ReadTest, Double) |
| 113 | { |
| 114 | const double d = 1.1; |
| 115 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, d); |
| 116 | double ret; |
| 117 | new_message().read(ret); |
| 118 | EXPECT_EQ(d, ret); |
| 119 | } |
| 120 | |
| 121 | TEST_F(ReadTest, CString) |
| 122 | { |
| 123 | const char *const s = "asdf"; |
| 124 | expect_basic<const char *>(SD_BUS_TYPE_STRING, s); |
| 125 | const char *ret; |
| 126 | new_message().read(ret); |
| 127 | EXPECT_EQ(s, ret); |
| 128 | } |
| 129 | |
| 130 | TEST_F(ReadTest, String) |
| 131 | { |
| 132 | const char *const s = "fsda"; |
| 133 | expect_basic<const char *>(SD_BUS_TYPE_STRING, s); |
| 134 | std::string ret; |
| 135 | new_message().read(ret); |
| 136 | // Pointer comparison here is intentional as we don't expect a copy |
| 137 | EXPECT_EQ(s, ret); |
| 138 | } |
| 139 | |
| 140 | TEST_F(ReadTest, ObjectPath) |
| 141 | { |
| 142 | const char *const s = "/fsda"; |
| 143 | expect_basic<const char *>(SD_BUS_TYPE_OBJECT_PATH, s); |
| 144 | sdbusplus::message::object_path ret; |
| 145 | new_message().read(ret); |
| 146 | EXPECT_EQ(s, ret.str); |
| 147 | } |
| 148 | |
| 149 | TEST_F(ReadTest, Signature) |
| 150 | { |
| 151 | const char *const s = "{ii}"; |
| 152 | expect_basic<const char *>(SD_BUS_TYPE_SIGNATURE, s); |
| 153 | sdbusplus::message::signature ret; |
| 154 | new_message().read(ret); |
| 155 | EXPECT_EQ(s, ret.str); |
| 156 | } |
| 157 | |
| 158 | TEST_F(ReadTest, CombinedBasic) |
| 159 | { |
| 160 | const double a = 2.2; |
| 161 | const char *const b = "ijkd"; |
| 162 | const bool c = false; |
| 163 | const int d = 18; |
| 164 | |
| 165 | { |
| 166 | testing::InSequence seq; |
| 167 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, a); |
| 168 | expect_basic<const char *>(SD_BUS_TYPE_STRING, b); |
| 169 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, c); |
| 170 | expect_basic<int>(SD_BUS_TYPE_INT32, d); |
| 171 | } |
| 172 | |
| 173 | double ret_a; |
| 174 | const char *ret_b; |
| 175 | bool ret_c; |
| 176 | int ret_d; |
| 177 | new_message().read(ret_a, ret_b, ret_c, ret_d); |
| 178 | EXPECT_EQ(a, ret_a); |
| 179 | EXPECT_EQ(b, ret_b); |
| 180 | EXPECT_EQ(c, ret_c); |
| 181 | EXPECT_EQ(d, ret_d); |
| 182 | } |
| 183 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 184 | TEST_F(ReadTest, BasicError) |
| 185 | { |
| 186 | expect_basic_error(SD_BUS_TYPE_INT32, -EINVAL); |
| 187 | int ret; |
| 188 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 189 | } |
| 190 | |
| 191 | TEST_F(ReadTest, BasicStringError) |
| 192 | { |
| 193 | expect_basic_error(SD_BUS_TYPE_STRING, -EINVAL); |
| 194 | std::string ret; |
| 195 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 196 | } |
| 197 | |
| 198 | TEST_F(ReadTest, BasicStringWrapperError) |
| 199 | { |
| 200 | expect_basic_error(SD_BUS_TYPE_SIGNATURE, -EINVAL); |
| 201 | sdbusplus::message::signature ret; |
| 202 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 203 | } |
| 204 | |
| 205 | TEST_F(ReadTest, BasicBoolError) |
| 206 | { |
| 207 | expect_basic_error(SD_BUS_TYPE_BOOLEAN, -EINVAL); |
| 208 | bool ret; |
| 209 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 210 | } |
| 211 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 212 | TEST_F(ReadTest, Vector) |
| 213 | { |
| 214 | const std::vector<int> vi{1, 2, 3, 4}; |
| 215 | |
| 216 | { |
| 217 | testing::InSequence seq; |
| 218 | expect_enter_container(SD_BUS_TYPE_ARRAY, "i"); |
| 219 | for (const auto &i : vi) |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 220 | { |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 221 | expect_at_end(false, 0); |
| 222 | expect_basic<int>(SD_BUS_TYPE_INT32, i); |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 223 | } |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 224 | expect_at_end(false, 1); |
| 225 | expect_exit_container(); |
| 226 | } |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 227 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 228 | std::vector<int> ret_vi; |
| 229 | new_message().read(ret_vi); |
| 230 | EXPECT_EQ(vi, ret_vi); |
| 231 | } |
| 232 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 233 | TEST_F(ReadTest, VectorEnterError) |
| 234 | { |
| 235 | { |
| 236 | testing::InSequence seq; |
| 237 | expect_enter_container(SD_BUS_TYPE_ARRAY, "i", -EINVAL); |
| 238 | } |
| 239 | |
| 240 | std::vector<int> ret; |
| 241 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 242 | } |
| 243 | |
| 244 | TEST_F(ReadTest, VectorIterError) |
| 245 | { |
| 246 | { |
| 247 | testing::InSequence seq; |
| 248 | expect_enter_container(SD_BUS_TYPE_ARRAY, "i"); |
| 249 | expect_at_end(false, 0); |
| 250 | expect_basic<int>(SD_BUS_TYPE_INT32, 1); |
| 251 | expect_at_end(false, -EINVAL); |
| 252 | } |
| 253 | |
| 254 | std::vector<int> ret; |
| 255 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 256 | } |
| 257 | |
| 258 | TEST_F(ReadTest, VectorExitError) |
| 259 | { |
| 260 | { |
| 261 | testing::InSequence seq; |
| 262 | expect_enter_container(SD_BUS_TYPE_ARRAY, "i"); |
| 263 | expect_at_end(false, 0); |
| 264 | expect_basic<int>(SD_BUS_TYPE_INT32, 1); |
| 265 | expect_at_end(false, 0); |
| 266 | expect_basic<int>(SD_BUS_TYPE_INT32, 2); |
| 267 | expect_at_end(false, 1); |
| 268 | expect_exit_container(-EINVAL); |
| 269 | } |
| 270 | |
| 271 | std::vector<int> ret; |
| 272 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 273 | } |
| 274 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 275 | TEST_F(ReadTest, Set) |
| 276 | { |
| 277 | const std::set<std::string> ss{"one", "two", "eight"}; |
| 278 | |
| 279 | { |
| 280 | testing::InSequence seq; |
| 281 | expect_enter_container(SD_BUS_TYPE_ARRAY, "s"); |
| 282 | for (const auto &s : ss) |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 283 | { |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 284 | expect_at_end(false, 0); |
| 285 | expect_basic<const char *>(SD_BUS_TYPE_STRING, s.c_str()); |
| 286 | } |
| 287 | expect_at_end(false, 1); |
| 288 | expect_exit_container(); |
| 289 | } |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 290 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 291 | std::set<std::string> ret_ss; |
| 292 | new_message().read(ret_ss); |
| 293 | EXPECT_EQ(ss, ret_ss); |
| 294 | } |
| 295 | |
| 296 | TEST_F(ReadTest, Map) |
| 297 | { |
| 298 | const std::map<int, std::string> mis{ |
| 299 | {1, "a"}, |
| 300 | {2, "bc"}, |
| 301 | {3, "def"}, |
| 302 | {4, "ghij"}, |
| 303 | }; |
| 304 | |
| 305 | { |
| 306 | testing::InSequence seq; |
| 307 | expect_enter_container(SD_BUS_TYPE_ARRAY, "{is}"); |
| 308 | for (const auto &is : mis) |
| 309 | { |
| 310 | expect_at_end(false, 0); |
| 311 | expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "is"); |
| 312 | expect_basic<int>(SD_BUS_TYPE_INT32, is.first); |
| 313 | expect_basic<const char *>(SD_BUS_TYPE_STRING, is.second.c_str()); |
| 314 | expect_exit_container(); |
| 315 | } |
| 316 | expect_at_end(false, 1); |
| 317 | expect_exit_container(); |
| 318 | } |
| 319 | |
| 320 | std::map<int, std::string> ret_mis; |
| 321 | new_message().read(ret_mis); |
| 322 | EXPECT_EQ(mis, ret_mis); |
| 323 | } |
| 324 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 325 | TEST_F(ReadTest, MapEnterError) |
| 326 | { |
| 327 | { |
| 328 | testing::InSequence seq; |
| 329 | expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}", -EINVAL); |
| 330 | } |
| 331 | |
| 332 | std::map<std::string, int> ret; |
| 333 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 334 | } |
| 335 | |
| 336 | TEST_F(ReadTest, MapEntryEnterError) |
| 337 | { |
| 338 | { |
| 339 | testing::InSequence seq; |
| 340 | expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}"); |
| 341 | expect_at_end(false, 0); |
| 342 | expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si", -EINVAL); |
| 343 | } |
| 344 | |
| 345 | std::map<std::string, int> ret; |
| 346 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 347 | } |
| 348 | |
| 349 | TEST_F(ReadTest, MapEntryExitError) |
| 350 | { |
| 351 | { |
| 352 | testing::InSequence seq; |
| 353 | expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}"); |
| 354 | expect_at_end(false, 0); |
| 355 | expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si"); |
| 356 | expect_basic<const char *>(SD_BUS_TYPE_STRING, "ab"); |
| 357 | expect_basic<int>(SD_BUS_TYPE_INT32, 1); |
| 358 | expect_exit_container(-EINVAL); |
| 359 | } |
| 360 | |
| 361 | std::map<std::string, int> ret; |
| 362 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 363 | } |
| 364 | |
| 365 | TEST_F(ReadTest, MapIterError) |
| 366 | { |
| 367 | { |
| 368 | testing::InSequence seq; |
| 369 | expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}"); |
| 370 | expect_at_end(false, 0); |
| 371 | expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si"); |
| 372 | expect_basic<const char *>(SD_BUS_TYPE_STRING, "ab"); |
| 373 | expect_basic<int>(SD_BUS_TYPE_INT32, 1); |
| 374 | expect_exit_container(); |
| 375 | expect_at_end(false, -EINVAL); |
| 376 | } |
| 377 | |
| 378 | std::map<std::string, int> ret; |
| 379 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 380 | } |
| 381 | |
| 382 | TEST_F(ReadTest, MapExitError) |
| 383 | { |
| 384 | { |
| 385 | testing::InSequence seq; |
| 386 | expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}"); |
| 387 | expect_at_end(false, 0); |
| 388 | expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si"); |
| 389 | expect_basic<const char *>(SD_BUS_TYPE_STRING, "ab"); |
| 390 | expect_basic<int>(SD_BUS_TYPE_INT32, 1); |
| 391 | expect_exit_container(); |
| 392 | expect_at_end(false, 1); |
| 393 | expect_exit_container(-EINVAL); |
| 394 | } |
| 395 | |
| 396 | std::map<std::string, int> ret; |
| 397 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 398 | } |
| 399 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 400 | TEST_F(ReadTest, UnorderedMap) |
| 401 | { |
| 402 | const std::unordered_map<int, std::string> mis{ |
| 403 | {1, "a"}, |
| 404 | {2, "bc"}, |
| 405 | {3, "def"}, |
| 406 | {4, "ghij"}, |
| 407 | }; |
| 408 | |
| 409 | { |
| 410 | testing::InSequence seq; |
| 411 | expect_enter_container(SD_BUS_TYPE_ARRAY, "{is}"); |
| 412 | for (const auto &is : mis) |
| 413 | { |
| 414 | expect_at_end(false, 0); |
| 415 | expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "is"); |
| 416 | expect_basic<int>(SD_BUS_TYPE_INT32, is.first); |
| 417 | expect_basic<const char *>(SD_BUS_TYPE_STRING, is.second.c_str()); |
| 418 | expect_exit_container(); |
| 419 | } |
| 420 | expect_at_end(false, 1); |
| 421 | expect_exit_container(); |
| 422 | } |
| 423 | |
| 424 | std::unordered_map<int, std::string> ret_mis; |
| 425 | new_message().read(ret_mis); |
| 426 | EXPECT_EQ(mis, ret_mis); |
| 427 | } |
| 428 | |
| 429 | TEST_F(ReadTest, Tuple) |
| 430 | { |
| 431 | const std::tuple<int, std::string, bool> tisb{3, "hi", false}; |
| 432 | |
| 433 | { |
| 434 | testing::InSequence seq; |
| 435 | expect_enter_container(SD_BUS_TYPE_STRUCT, "isb"); |
| 436 | expect_basic<int>(SD_BUS_TYPE_INT32, std::get<0>(tisb)); |
| 437 | expect_basic<const char *>(SD_BUS_TYPE_STRING, |
| 438 | std::get<1>(tisb).c_str()); |
| 439 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, std::get<2>(tisb)); |
| 440 | expect_exit_container(); |
| 441 | } |
| 442 | |
| 443 | std::tuple<int, std::string, bool> ret_tisb; |
| 444 | new_message().read(ret_tisb); |
| 445 | EXPECT_EQ(tisb, ret_tisb); |
| 446 | } |
| 447 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 448 | TEST_F(ReadTest, TupleEnterError) |
| 449 | { |
| 450 | { |
| 451 | testing::InSequence seq; |
| 452 | expect_enter_container(SD_BUS_TYPE_STRUCT, "bis", -EINVAL); |
| 453 | } |
| 454 | |
| 455 | std::tuple<bool, int, std::string> ret; |
| 456 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 457 | } |
| 458 | |
| 459 | TEST_F(ReadTest, TupleExitError) |
| 460 | { |
| 461 | { |
| 462 | testing::InSequence seq; |
| 463 | expect_enter_container(SD_BUS_TYPE_STRUCT, "bis"); |
| 464 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false); |
| 465 | expect_basic<int>(SD_BUS_TYPE_INT32, 1); |
| 466 | expect_basic<const char *>(SD_BUS_TYPE_STRING, "ab"); |
| 467 | expect_exit_container(-EINVAL); |
| 468 | } |
| 469 | |
| 470 | std::tuple<bool, int, std::string> ret; |
| 471 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 472 | } |
| 473 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 474 | TEST_F(ReadTest, Variant) |
| 475 | { |
| 476 | const bool b1 = false; |
| 477 | const std::string s2{"asdf"}; |
| 478 | const sdbusplus::message::variant<int, std::string, bool> v1{b1}, v2{s2}; |
| 479 | |
| 480 | { |
| 481 | testing::InSequence seq; |
| 482 | expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false); |
| 483 | expect_verify_type(SD_BUS_TYPE_VARIANT, "s", false); |
| 484 | expect_verify_type(SD_BUS_TYPE_VARIANT, "b", true); |
| 485 | expect_enter_container(SD_BUS_TYPE_VARIANT, "b"); |
| 486 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, b1); |
| 487 | expect_exit_container(); |
| 488 | expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false); |
| 489 | expect_verify_type(SD_BUS_TYPE_VARIANT, "s", true); |
| 490 | expect_enter_container(SD_BUS_TYPE_VARIANT, "s"); |
| 491 | expect_basic<const char *>(SD_BUS_TYPE_STRING, s2.c_str()); |
| 492 | expect_exit_container(); |
| 493 | } |
| 494 | |
| 495 | sdbusplus::message::variant<int, std::string, bool> ret_v1, ret_v2; |
| 496 | new_message().read(ret_v1, ret_v2); |
| 497 | EXPECT_EQ(v1, ret_v1); |
| 498 | EXPECT_EQ(v2, ret_v2); |
| 499 | } |
| 500 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 501 | TEST_F(ReadTest, VariantVerifyError) |
| 502 | { |
| 503 | { |
| 504 | testing::InSequence seq; |
| 505 | expect_verify_type(SD_BUS_TYPE_VARIANT, "i", -EINVAL); |
| 506 | } |
| 507 | |
| 508 | sdbusplus::message::variant<int, bool> ret; |
| 509 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 510 | } |
| 511 | |
William A. Kennington III | 60a7283 | 2018-06-26 14:31:15 -0700 | [diff] [blame] | 512 | TEST_F(ReadTest, VariantSkipUnmatched) |
| 513 | { |
| 514 | { |
| 515 | testing::InSequence seq; |
| 516 | expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false); |
| 517 | expect_verify_type(SD_BUS_TYPE_VARIANT, "b", false); |
| 518 | expect_skip("v"); |
| 519 | } |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 520 | |
William A. Kennington III | 60a7283 | 2018-06-26 14:31:15 -0700 | [diff] [blame] | 521 | sdbusplus::message::variant<int, bool> ret; |
| 522 | new_message().read(ret); |
| 523 | } |
| 524 | |
William A. Kennington III | 13367e4 | 2018-06-26 15:23:13 -0700 | [diff] [blame] | 525 | TEST_F(ReadTest, VariantSkipError) |
| 526 | { |
| 527 | { |
| 528 | testing::InSequence seq; |
| 529 | expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false); |
| 530 | expect_verify_type(SD_BUS_TYPE_VARIANT, "b", false); |
| 531 | expect_skip("v", -EINVAL); |
| 532 | } |
| 533 | |
| 534 | sdbusplus::message::variant<int, bool> ret; |
| 535 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 536 | } |
| 537 | |
| 538 | TEST_F(ReadTest, VariantEnterError) |
| 539 | { |
| 540 | { |
| 541 | testing::InSequence seq; |
| 542 | expect_verify_type(SD_BUS_TYPE_VARIANT, "i", true); |
| 543 | expect_enter_container(SD_BUS_TYPE_VARIANT, "i", -EINVAL); |
| 544 | } |
| 545 | |
| 546 | sdbusplus::message::variant<int, bool> ret; |
| 547 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 548 | } |
| 549 | |
| 550 | TEST_F(ReadTest, VariantExitError) |
| 551 | { |
| 552 | { |
| 553 | testing::InSequence seq; |
| 554 | expect_verify_type(SD_BUS_TYPE_VARIANT, "i", true); |
| 555 | expect_enter_container(SD_BUS_TYPE_VARIANT, "i"); |
| 556 | expect_basic<int>(SD_BUS_TYPE_INT32, 10); |
| 557 | expect_exit_container(-EINVAL); |
| 558 | } |
| 559 | |
| 560 | sdbusplus::message::variant<int, bool> ret; |
| 561 | EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError); |
| 562 | } |
| 563 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 564 | TEST_F(ReadTest, LargeCombo) |
| 565 | { |
| 566 | const std::vector<std::set<std::string>> vas{ |
| 567 | {"a", "b", "c"}, |
| 568 | {"d", "", "e"}, |
| 569 | }; |
| 570 | const std::map<std::string, sdbusplus::message::variant<int, double>> msv = |
| 571 | {{"a", 3.3}, {"b", 1}, {"c", 4.4}}; |
| 572 | |
| 573 | { |
| 574 | testing::InSequence seq; |
| 575 | |
| 576 | expect_enter_container(SD_BUS_TYPE_ARRAY, "as"); |
| 577 | for (const auto &as : vas) |
| 578 | { |
| 579 | expect_at_end(false, 0); |
| 580 | expect_enter_container(SD_BUS_TYPE_ARRAY, "s"); |
| 581 | for (const auto &s : as) |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 582 | { |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 583 | expect_at_end(false, 0); |
| 584 | expect_basic<const char *>(SD_BUS_TYPE_STRING, s.c_str()); |
| 585 | } |
| 586 | expect_at_end(false, 1); |
| 587 | expect_exit_container(); |
| 588 | } |
| 589 | expect_at_end(false, 1); |
| 590 | expect_exit_container(); |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 591 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 592 | expect_enter_container(SD_BUS_TYPE_ARRAY, "{sv}"); |
| 593 | for (const auto &sv : msv) |
| 594 | { |
| 595 | expect_at_end(false, 0); |
| 596 | expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "sv"); |
| 597 | expect_basic<const char *>(SD_BUS_TYPE_STRING, sv.first.c_str()); |
| 598 | if (sv.second.is<int>()) |
| 599 | { |
| 600 | expect_verify_type(SD_BUS_TYPE_VARIANT, "i", true); |
| 601 | expect_enter_container(SD_BUS_TYPE_VARIANT, "i"); |
| 602 | expect_basic<int>(SD_BUS_TYPE_INT32, sv.second.get<int>()); |
| 603 | expect_exit_container(); |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 604 | } |
| 605 | else |
| 606 | { |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 607 | expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false); |
| 608 | expect_verify_type(SD_BUS_TYPE_VARIANT, "d", true); |
| 609 | expect_enter_container(SD_BUS_TYPE_VARIANT, "d"); |
| 610 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, |
| 611 | sv.second.get<double>()); |
| 612 | expect_exit_container(); |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 613 | } |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 614 | expect_exit_container(); |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 615 | } |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 616 | expect_at_end(false, 1); |
| 617 | expect_exit_container(); |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 618 | } |
Patrick Williams | 696e315 | 2016-11-04 17:03:39 -0500 | [diff] [blame] | 619 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 620 | std::vector<std::set<std::string>> ret_vas; |
| 621 | std::map<std::string, sdbusplus::message::variant<int, double>> ret_msv; |
| 622 | new_message().read(ret_vas, ret_msv); |
| 623 | EXPECT_EQ(vas, ret_vas); |
| 624 | EXPECT_EQ(msv, ret_msv); |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 625 | } |
| 626 | |
William A. Kennington III | b4b4fa1 | 2018-06-25 17:20:06 -0700 | [diff] [blame] | 627 | } // namespace |