blob: eeafd320d5116778876710934d4c63235b97ffce [file] [log] [blame]
Patrick Venture95269db2018-08-31 09:19:17 -07001#include <systemd/sd-bus-protocol.h>
2
William A. Kennington III13367e42018-06-26 15:23:13 -07003#include <cerrno>
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -07004#include <map>
William A. Kennington III13367e42018-06-26 15:23:13 -07005#include <sdbusplus/exception.hpp>
Patrick Williams4fe85a32016-09-08 15:03:56 -05006#include <sdbusplus/message.hpp>
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -07007#include <sdbusplus/test/sdbus_mock.hpp>
Ed Tanous28dc36d2018-02-21 12:22:54 -08008#include <set>
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -07009#include <string>
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070010#include <tuple>
11#include <unordered_map>
12#include <vector>
Patrick Williams4fe85a32016-09-08 15:03:56 -050013
Patrick Venture95269db2018-08-31 09:19:17 -070014#include <gmock/gmock.h>
15#include <gtest/gtest.h>
16
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070017namespace
Patrick Williams4fe85a32016-09-08 15:03:56 -050018{
Patrick Williams4fe85a32016-09-08 15:03:56 -050019
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070020using testing::DoAll;
21using testing::Return;
22using testing::StrEq;
23
24ACTION_TEMPLATE(AssignReadVal, HAS_1_TEMPLATE_PARAMS(typename, T),
25 AND_1_VALUE_PARAMS(val))
26{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070027 *static_cast<T*>(arg2) = val;
Patrick Williams4fe85a32016-09-08 15:03:56 -050028}
29
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070030class ReadTest : public testing::Test
Patrick Williams4fe85a32016-09-08 15:03:56 -050031{
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070032 protected:
33 testing::StrictMock<sdbusplus::SdBusMock> mock;
Patrick Williams4fe85a32016-09-08 15:03:56 -050034
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070035 void SetUp() override
Patrick Williams4fe85a32016-09-08 15:03:56 -050036 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070037 EXPECT_CALL(mock, sd_bus_message_new_method_call(testing::_, testing::_,
38 nullptr, nullptr,
39 nullptr, nullptr))
40 .WillRepeatedly(Return(0));
41 };
Patrick Williams4fe85a32016-09-08 15:03:56 -050042
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070043 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 III13367e42018-06-26 15:23:13 -070049 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
Patrick Venture2b238af2018-08-31 12:45:01 -070055 template <typename T>
56 void expect_basic(char type, T val)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070057 {
58 EXPECT_CALL(mock, sd_bus_message_read_basic(nullptr, type, testing::_))
59 .WillOnce(DoAll(AssignReadVal<T>(val), Return(0)));
60 }
61
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070062 void expect_verify_type(char type, const char* contents, int ret)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070063 {
64 EXPECT_CALL(mock,
65 sd_bus_message_verify_type(nullptr, type, StrEq(contents)))
66 .WillOnce(Return(ret));
67 }
68
69 void expect_at_end(bool complete, int ret)
70 {
71 EXPECT_CALL(mock, sd_bus_message_at_end(nullptr, complete))
72 .WillOnce(Return(ret));
73 }
74
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070075 void expect_skip(const char* contents, int ret = 0)
William A. Kennington III60a72832018-06-26 14:31:15 -070076 {
77 EXPECT_CALL(mock, sd_bus_message_skip(nullptr, StrEq(contents)))
William A. Kennington III13367e42018-06-26 15:23:13 -070078 .WillOnce(Return(ret));
William A. Kennington III60a72832018-06-26 14:31:15 -070079 }
80
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070081 void expect_enter_container(char type, const char* contents, int ret = 0)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070082 {
83 EXPECT_CALL(mock, sd_bus_message_enter_container(nullptr, type,
84 StrEq(contents)))
William A. Kennington III13367e42018-06-26 15:23:13 -070085 .WillOnce(Return(ret));
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070086 }
87
William A. Kennington III13367e42018-06-26 15:23:13 -070088 void expect_exit_container(int ret = 0)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070089 {
90 EXPECT_CALL(mock, sd_bus_message_exit_container(nullptr))
William A. Kennington III13367e42018-06-26 15:23:13 -070091 .WillOnce(Return(ret));
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070092 }
93};
94
95TEST_F(ReadTest, Int)
96{
97 const int i = 1;
98 expect_basic<int>(SD_BUS_TYPE_INT32, i);
99 int ret;
100 new_message().read(ret);
101 EXPECT_EQ(i, ret);
102}
103
104TEST_F(ReadTest, Bool)
105{
106 const bool b = true;
107 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, b);
108 bool ret;
109 new_message().read(ret);
110 EXPECT_EQ(b, ret);
111}
112
113TEST_F(ReadTest, Double)
114{
115 const double d = 1.1;
116 expect_basic<double>(SD_BUS_TYPE_DOUBLE, d);
117 double ret;
118 new_message().read(ret);
119 EXPECT_EQ(d, ret);
120}
121
122TEST_F(ReadTest, CString)
123{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700124 const char* const s = "asdf";
125 expect_basic<const char*>(SD_BUS_TYPE_STRING, s);
126 const char* ret;
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700127 new_message().read(ret);
128 EXPECT_EQ(s, ret);
129}
130
131TEST_F(ReadTest, String)
132{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700133 const char* const s = "fsda";
134 expect_basic<const char*>(SD_BUS_TYPE_STRING, s);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700135 std::string ret;
136 new_message().read(ret);
137 // Pointer comparison here is intentional as we don't expect a copy
138 EXPECT_EQ(s, ret);
139}
140
141TEST_F(ReadTest, ObjectPath)
142{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700143 const char* const s = "/fsda";
144 expect_basic<const char*>(SD_BUS_TYPE_OBJECT_PATH, s);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700145 sdbusplus::message::object_path ret;
146 new_message().read(ret);
147 EXPECT_EQ(s, ret.str);
148}
149
150TEST_F(ReadTest, Signature)
151{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700152 const char* const s = "{ii}";
153 expect_basic<const char*>(SD_BUS_TYPE_SIGNATURE, s);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700154 sdbusplus::message::signature ret;
155 new_message().read(ret);
156 EXPECT_EQ(s, ret.str);
157}
158
159TEST_F(ReadTest, CombinedBasic)
160{
161 const double a = 2.2;
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700162 const char* const b = "ijkd";
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700163 const bool c = false;
164 const int d = 18;
165
166 {
167 testing::InSequence seq;
168 expect_basic<double>(SD_BUS_TYPE_DOUBLE, a);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700169 expect_basic<const char*>(SD_BUS_TYPE_STRING, b);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700170 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, c);
171 expect_basic<int>(SD_BUS_TYPE_INT32, d);
172 }
173
174 double ret_a;
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700175 const char* ret_b;
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700176 bool ret_c;
177 int ret_d;
178 new_message().read(ret_a, ret_b, ret_c, ret_d);
179 EXPECT_EQ(a, ret_a);
180 EXPECT_EQ(b, ret_b);
181 EXPECT_EQ(c, ret_c);
182 EXPECT_EQ(d, ret_d);
183}
184
William A. Kennington III13367e42018-06-26 15:23:13 -0700185TEST_F(ReadTest, BasicError)
186{
187 expect_basic_error(SD_BUS_TYPE_INT32, -EINVAL);
188 int ret;
189 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
190}
191
192TEST_F(ReadTest, BasicStringError)
193{
194 expect_basic_error(SD_BUS_TYPE_STRING, -EINVAL);
195 std::string ret;
196 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
197}
198
199TEST_F(ReadTest, BasicStringWrapperError)
200{
201 expect_basic_error(SD_BUS_TYPE_SIGNATURE, -EINVAL);
202 sdbusplus::message::signature ret;
203 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
204}
205
206TEST_F(ReadTest, BasicBoolError)
207{
208 expect_basic_error(SD_BUS_TYPE_BOOLEAN, -EINVAL);
209 bool ret;
210 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
211}
212
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700213TEST_F(ReadTest, Vector)
214{
215 const std::vector<int> vi{1, 2, 3, 4};
216
217 {
218 testing::InSequence seq;
219 expect_enter_container(SD_BUS_TYPE_ARRAY, "i");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700220 for (const auto& i : vi)
Patrick Williams4fe85a32016-09-08 15:03:56 -0500221 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700222 expect_at_end(false, 0);
223 expect_basic<int>(SD_BUS_TYPE_INT32, i);
Patrick Williams4fe85a32016-09-08 15:03:56 -0500224 }
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700225 expect_at_end(false, 1);
226 expect_exit_container();
227 }
Patrick Williams4fe85a32016-09-08 15:03:56 -0500228
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700229 std::vector<int> ret_vi;
230 new_message().read(ret_vi);
231 EXPECT_EQ(vi, ret_vi);
232}
233
William A. Kennington III13367e42018-06-26 15:23:13 -0700234TEST_F(ReadTest, VectorEnterError)
235{
236 {
237 testing::InSequence seq;
238 expect_enter_container(SD_BUS_TYPE_ARRAY, "i", -EINVAL);
239 }
240
241 std::vector<int> ret;
242 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
243}
244
245TEST_F(ReadTest, VectorIterError)
246{
247 {
248 testing::InSequence seq;
249 expect_enter_container(SD_BUS_TYPE_ARRAY, "i");
250 expect_at_end(false, 0);
251 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
252 expect_at_end(false, -EINVAL);
253 }
254
255 std::vector<int> ret;
256 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
257}
258
259TEST_F(ReadTest, VectorExitError)
260{
261 {
262 testing::InSequence seq;
263 expect_enter_container(SD_BUS_TYPE_ARRAY, "i");
264 expect_at_end(false, 0);
265 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
266 expect_at_end(false, 0);
267 expect_basic<int>(SD_BUS_TYPE_INT32, 2);
268 expect_at_end(false, 1);
269 expect_exit_container(-EINVAL);
270 }
271
272 std::vector<int> ret;
273 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
274}
275
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700276TEST_F(ReadTest, Set)
277{
278 const std::set<std::string> ss{"one", "two", "eight"};
279
280 {
281 testing::InSequence seq;
282 expect_enter_container(SD_BUS_TYPE_ARRAY, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700283 for (const auto& s : ss)
Patrick Williams4fe85a32016-09-08 15:03:56 -0500284 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700285 expect_at_end(false, 0);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700286 expect_basic<const char*>(SD_BUS_TYPE_STRING, s.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700287 }
288 expect_at_end(false, 1);
289 expect_exit_container();
290 }
Patrick Williams4fe85a32016-09-08 15:03:56 -0500291
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700292 std::set<std::string> ret_ss;
293 new_message().read(ret_ss);
294 EXPECT_EQ(ss, ret_ss);
295}
296
297TEST_F(ReadTest, Map)
298{
299 const std::map<int, std::string> mis{
300 {1, "a"},
301 {2, "bc"},
302 {3, "def"},
303 {4, "ghij"},
304 };
305
306 {
307 testing::InSequence seq;
308 expect_enter_container(SD_BUS_TYPE_ARRAY, "{is}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700309 for (const auto& is : mis)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700310 {
311 expect_at_end(false, 0);
312 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "is");
313 expect_basic<int>(SD_BUS_TYPE_INT32, is.first);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700314 expect_basic<const char*>(SD_BUS_TYPE_STRING, is.second.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700315 expect_exit_container();
316 }
317 expect_at_end(false, 1);
318 expect_exit_container();
319 }
320
321 std::map<int, std::string> ret_mis;
322 new_message().read(ret_mis);
323 EXPECT_EQ(mis, ret_mis);
324}
325
William A. Kennington III13367e42018-06-26 15:23:13 -0700326TEST_F(ReadTest, MapEnterError)
327{
328 {
329 testing::InSequence seq;
330 expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}", -EINVAL);
331 }
332
333 std::map<std::string, int> ret;
334 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
335}
336
337TEST_F(ReadTest, MapEntryEnterError)
338{
339 {
340 testing::InSequence seq;
341 expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}");
342 expect_at_end(false, 0);
343 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si", -EINVAL);
344 }
345
346 std::map<std::string, int> ret;
347 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
348}
349
350TEST_F(ReadTest, MapEntryExitError)
351{
352 {
353 testing::InSequence seq;
354 expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}");
355 expect_at_end(false, 0);
356 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700357 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700358 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
359 expect_exit_container(-EINVAL);
360 }
361
362 std::map<std::string, int> ret;
363 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
364}
365
366TEST_F(ReadTest, MapIterError)
367{
368 {
369 testing::InSequence seq;
370 expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}");
371 expect_at_end(false, 0);
372 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700373 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700374 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
375 expect_exit_container();
376 expect_at_end(false, -EINVAL);
377 }
378
379 std::map<std::string, int> ret;
380 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
381}
382
383TEST_F(ReadTest, MapExitError)
384{
385 {
386 testing::InSequence seq;
387 expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}");
388 expect_at_end(false, 0);
389 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700390 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700391 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
392 expect_exit_container();
393 expect_at_end(false, 1);
394 expect_exit_container(-EINVAL);
395 }
396
397 std::map<std::string, int> ret;
398 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
399}
400
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700401TEST_F(ReadTest, UnorderedMap)
402{
403 const std::unordered_map<int, std::string> mis{
404 {1, "a"},
405 {2, "bc"},
406 {3, "def"},
407 {4, "ghij"},
408 };
409
410 {
411 testing::InSequence seq;
412 expect_enter_container(SD_BUS_TYPE_ARRAY, "{is}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700413 for (const auto& is : mis)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700414 {
415 expect_at_end(false, 0);
416 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "is");
417 expect_basic<int>(SD_BUS_TYPE_INT32, is.first);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700418 expect_basic<const char*>(SD_BUS_TYPE_STRING, is.second.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700419 expect_exit_container();
420 }
421 expect_at_end(false, 1);
422 expect_exit_container();
423 }
424
425 std::unordered_map<int, std::string> ret_mis;
426 new_message().read(ret_mis);
427 EXPECT_EQ(mis, ret_mis);
428}
429
430TEST_F(ReadTest, Tuple)
431{
432 const std::tuple<int, std::string, bool> tisb{3, "hi", false};
433
434 {
435 testing::InSequence seq;
436 expect_enter_container(SD_BUS_TYPE_STRUCT, "isb");
437 expect_basic<int>(SD_BUS_TYPE_INT32, std::get<0>(tisb));
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700438 expect_basic<const char*>(SD_BUS_TYPE_STRING,
439 std::get<1>(tisb).c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700440 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, std::get<2>(tisb));
441 expect_exit_container();
442 }
443
444 std::tuple<int, std::string, bool> ret_tisb;
445 new_message().read(ret_tisb);
446 EXPECT_EQ(tisb, ret_tisb);
447}
448
William A. Kennington III13367e42018-06-26 15:23:13 -0700449TEST_F(ReadTest, TupleEnterError)
450{
451 {
452 testing::InSequence seq;
453 expect_enter_container(SD_BUS_TYPE_STRUCT, "bis", -EINVAL);
454 }
455
456 std::tuple<bool, int, std::string> ret;
457 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
458}
459
460TEST_F(ReadTest, TupleExitError)
461{
462 {
463 testing::InSequence seq;
464 expect_enter_container(SD_BUS_TYPE_STRUCT, "bis");
465 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false);
466 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700467 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700468 expect_exit_container(-EINVAL);
469 }
470
471 std::tuple<bool, int, std::string> ret;
472 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
473}
474
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700475TEST_F(ReadTest, Variant)
476{
477 const bool b1 = false;
478 const std::string s2{"asdf"};
479 const sdbusplus::message::variant<int, std::string, bool> v1{b1}, v2{s2};
480
481 {
482 testing::InSequence seq;
483 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false);
484 expect_verify_type(SD_BUS_TYPE_VARIANT, "s", false);
485 expect_verify_type(SD_BUS_TYPE_VARIANT, "b", true);
486 expect_enter_container(SD_BUS_TYPE_VARIANT, "b");
487 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, b1);
488 expect_exit_container();
489 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false);
490 expect_verify_type(SD_BUS_TYPE_VARIANT, "s", true);
491 expect_enter_container(SD_BUS_TYPE_VARIANT, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700492 expect_basic<const char*>(SD_BUS_TYPE_STRING, s2.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700493 expect_exit_container();
494 }
495
496 sdbusplus::message::variant<int, std::string, bool> ret_v1, ret_v2;
497 new_message().read(ret_v1, ret_v2);
498 EXPECT_EQ(v1, ret_v1);
499 EXPECT_EQ(v2, ret_v2);
500}
501
William A. Kennington III13367e42018-06-26 15:23:13 -0700502TEST_F(ReadTest, VariantVerifyError)
503{
504 {
505 testing::InSequence seq;
506 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", -EINVAL);
507 }
508
509 sdbusplus::message::variant<int, bool> ret;
510 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
511}
512
William A. Kennington III60a72832018-06-26 14:31:15 -0700513TEST_F(ReadTest, VariantSkipUnmatched)
514{
515 {
516 testing::InSequence seq;
517 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false);
518 expect_verify_type(SD_BUS_TYPE_VARIANT, "b", false);
519 expect_skip("v");
520 }
William A. Kennington III13367e42018-06-26 15:23:13 -0700521
William A. Kennington III60a72832018-06-26 14:31:15 -0700522 sdbusplus::message::variant<int, bool> ret;
523 new_message().read(ret);
524}
525
William A. Kennington III13367e42018-06-26 15:23:13 -0700526TEST_F(ReadTest, VariantSkipError)
527{
528 {
529 testing::InSequence seq;
530 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false);
531 expect_verify_type(SD_BUS_TYPE_VARIANT, "b", false);
532 expect_skip("v", -EINVAL);
533 }
534
535 sdbusplus::message::variant<int, bool> ret;
536 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
537}
538
539TEST_F(ReadTest, VariantEnterError)
540{
541 {
542 testing::InSequence seq;
543 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", true);
544 expect_enter_container(SD_BUS_TYPE_VARIANT, "i", -EINVAL);
545 }
546
547 sdbusplus::message::variant<int, bool> ret;
548 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
549}
550
551TEST_F(ReadTest, VariantExitError)
552{
553 {
554 testing::InSequence seq;
555 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", true);
556 expect_enter_container(SD_BUS_TYPE_VARIANT, "i");
557 expect_basic<int>(SD_BUS_TYPE_INT32, 10);
558 expect_exit_container(-EINVAL);
559 }
560
561 sdbusplus::message::variant<int, bool> ret;
562 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
563}
564
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700565TEST_F(ReadTest, LargeCombo)
566{
567 const std::vector<std::set<std::string>> vas{
568 {"a", "b", "c"},
569 {"d", "", "e"},
570 };
571 const std::map<std::string, sdbusplus::message::variant<int, double>> msv =
572 {{"a", 3.3}, {"b", 1}, {"c", 4.4}};
573
574 {
575 testing::InSequence seq;
576
577 expect_enter_container(SD_BUS_TYPE_ARRAY, "as");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700578 for (const auto& as : vas)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700579 {
580 expect_at_end(false, 0);
581 expect_enter_container(SD_BUS_TYPE_ARRAY, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700582 for (const auto& s : as)
Patrick Williams4fe85a32016-09-08 15:03:56 -0500583 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700584 expect_at_end(false, 0);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700585 expect_basic<const char*>(SD_BUS_TYPE_STRING, s.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700586 }
587 expect_at_end(false, 1);
588 expect_exit_container();
589 }
590 expect_at_end(false, 1);
591 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500592
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700593 expect_enter_container(SD_BUS_TYPE_ARRAY, "{sv}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700594 for (const auto& sv : msv)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700595 {
596 expect_at_end(false, 0);
597 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "sv");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700598 expect_basic<const char*>(SD_BUS_TYPE_STRING, sv.first.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700599 if (sv.second.is<int>())
600 {
601 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", true);
602 expect_enter_container(SD_BUS_TYPE_VARIANT, "i");
603 expect_basic<int>(SD_BUS_TYPE_INT32, sv.second.get<int>());
604 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500605 }
606 else
607 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700608 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false);
609 expect_verify_type(SD_BUS_TYPE_VARIANT, "d", true);
610 expect_enter_container(SD_BUS_TYPE_VARIANT, "d");
611 expect_basic<double>(SD_BUS_TYPE_DOUBLE,
612 sv.second.get<double>());
613 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500614 }
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700615 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500616 }
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700617 expect_at_end(false, 1);
618 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500619 }
Patrick Williams696e3152016-11-04 17:03:39 -0500620
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700621 std::vector<std::set<std::string>> ret_vas;
622 std::map<std::string, sdbusplus::message::variant<int, double>> ret_msv;
623 new_message().read(ret_vas, ret_msv);
624 EXPECT_EQ(vas, ret_vas);
625 EXPECT_EQ(msv, ret_msv);
Patrick Williams4fe85a32016-09-08 15:03:56 -0500626}
627
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700628} // namespace