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