blob: b973ba46a4f4d9b9cb18edd92db63b739237ceec [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>
William A. Kennington III4274c112018-11-26 09:50:13 -080012#include <variant>
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070013#include <vector>
Patrick Williams4fe85a32016-09-08 15:03:56 -050014
Patrick Venture95269db2018-08-31 09:19:17 -070015#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070018namespace
Patrick Williams4fe85a32016-09-08 15:03:56 -050019{
Patrick Williams4fe85a32016-09-08 15:03:56 -050020
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070021using testing::DoAll;
22using testing::Return;
23using testing::StrEq;
24
25ACTION_TEMPLATE(AssignReadVal, HAS_1_TEMPLATE_PARAMS(typename, T),
26 AND_1_VALUE_PARAMS(val))
27{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070028 *static_cast<T*>(arg2) = val;
Patrick Williams4fe85a32016-09-08 15:03:56 -050029}
30
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070031class ReadTest : public testing::Test
Patrick Williams4fe85a32016-09-08 15:03:56 -050032{
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070033 protected:
34 testing::StrictMock<sdbusplus::SdBusMock> mock;
Patrick Williams4fe85a32016-09-08 15:03:56 -050035
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070036 void SetUp() override
Patrick Williams4fe85a32016-09-08 15:03:56 -050037 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070038 EXPECT_CALL(mock, sd_bus_message_new_method_call(testing::_, testing::_,
39 nullptr, nullptr,
40 nullptr, nullptr))
41 .WillRepeatedly(Return(0));
42 };
Patrick Williams4fe85a32016-09-08 15:03:56 -050043
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070044 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 III13367e42018-06-26 15:23:13 -070050 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 Venture2b238af2018-08-31 12:45:01 -070056 template <typename T>
57 void expect_basic(char type, T val)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070058 {
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 IIIe0d69652018-08-31 13:09:47 -070063 void expect_verify_type(char type, const char* contents, int ret)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070064 {
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 IIIe0d69652018-08-31 13:09:47 -070076 void expect_skip(const char* contents, int ret = 0)
William A. Kennington III60a72832018-06-26 14:31:15 -070077 {
78 EXPECT_CALL(mock, sd_bus_message_skip(nullptr, StrEq(contents)))
William A. Kennington III13367e42018-06-26 15:23:13 -070079 .WillOnce(Return(ret));
William A. Kennington III60a72832018-06-26 14:31:15 -070080 }
81
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070082 void expect_enter_container(char type, const char* contents, int ret = 0)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070083 {
84 EXPECT_CALL(mock, sd_bus_message_enter_container(nullptr, type,
85 StrEq(contents)))
William A. Kennington III13367e42018-06-26 15:23:13 -070086 .WillOnce(Return(ret));
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070087 }
88
William A. Kennington III13367e42018-06-26 15:23:13 -070089 void expect_exit_container(int ret = 0)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070090 {
91 EXPECT_CALL(mock, sd_bus_message_exit_container(nullptr))
William A. Kennington III13367e42018-06-26 15:23:13 -070092 .WillOnce(Return(ret));
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070093 }
94};
95
96TEST_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
105TEST_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
114TEST_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
123TEST_F(ReadTest, CString)
124{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700125 const char* const s = "asdf";
126 expect_basic<const char*>(SD_BUS_TYPE_STRING, s);
127 const char* ret;
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700128 new_message().read(ret);
129 EXPECT_EQ(s, ret);
130}
131
132TEST_F(ReadTest, String)
133{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700134 const char* const s = "fsda";
135 expect_basic<const char*>(SD_BUS_TYPE_STRING, s);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700136 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
142TEST_F(ReadTest, ObjectPath)
143{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700144 const char* const s = "/fsda";
145 expect_basic<const char*>(SD_BUS_TYPE_OBJECT_PATH, s);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700146 sdbusplus::message::object_path ret;
147 new_message().read(ret);
148 EXPECT_EQ(s, ret.str);
149}
150
151TEST_F(ReadTest, Signature)
152{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700153 const char* const s = "{ii}";
154 expect_basic<const char*>(SD_BUS_TYPE_SIGNATURE, s);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700155 sdbusplus::message::signature ret;
156 new_message().read(ret);
157 EXPECT_EQ(s, ret.str);
158}
159
160TEST_F(ReadTest, CombinedBasic)
161{
162 const double a = 2.2;
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700163 const char* const b = "ijkd";
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700164 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 IIIe0d69652018-08-31 13:09:47 -0700170 expect_basic<const char*>(SD_BUS_TYPE_STRING, b);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700171 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 IIIe0d69652018-08-31 13:09:47 -0700176 const char* ret_b;
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700177 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 III13367e42018-06-26 15:23:13 -0700186TEST_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
193TEST_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
200TEST_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
207TEST_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 IIIb4b4fa12018-06-25 17:20:06 -0700214TEST_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 IIIe0d69652018-08-31 13:09:47 -0700221 for (const auto& i : vi)
Patrick Williams4fe85a32016-09-08 15:03:56 -0500222 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700223 expect_at_end(false, 0);
224 expect_basic<int>(SD_BUS_TYPE_INT32, i);
Patrick Williams4fe85a32016-09-08 15:03:56 -0500225 }
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700226 expect_at_end(false, 1);
227 expect_exit_container();
228 }
Patrick Williams4fe85a32016-09-08 15:03:56 -0500229
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700230 std::vector<int> ret_vi;
231 new_message().read(ret_vi);
232 EXPECT_EQ(vi, ret_vi);
233}
234
William A. Kennington III13367e42018-06-26 15:23:13 -0700235TEST_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
246TEST_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
260TEST_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 IIIb4b4fa12018-06-25 17:20:06 -0700277TEST_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 IIIe0d69652018-08-31 13:09:47 -0700284 for (const auto& s : ss)
Patrick Williams4fe85a32016-09-08 15:03:56 -0500285 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700286 expect_at_end(false, 0);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700287 expect_basic<const char*>(SD_BUS_TYPE_STRING, s.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700288 }
289 expect_at_end(false, 1);
290 expect_exit_container();
291 }
Patrick Williams4fe85a32016-09-08 15:03:56 -0500292
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700293 std::set<std::string> ret_ss;
294 new_message().read(ret_ss);
295 EXPECT_EQ(ss, ret_ss);
296}
297
298TEST_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 IIIe0d69652018-08-31 13:09:47 -0700310 for (const auto& is : mis)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700311 {
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 IIIe0d69652018-08-31 13:09:47 -0700315 expect_basic<const char*>(SD_BUS_TYPE_STRING, is.second.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700316 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 III13367e42018-06-26 15:23:13 -0700327TEST_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
338TEST_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
351TEST_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 IIIe0d69652018-08-31 13:09:47 -0700358 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700359 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
367TEST_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 IIIe0d69652018-08-31 13:09:47 -0700374 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700375 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
384TEST_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 IIIe0d69652018-08-31 13:09:47 -0700391 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700392 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 IIIb4b4fa12018-06-25 17:20:06 -0700402TEST_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 IIIe0d69652018-08-31 13:09:47 -0700414 for (const auto& is : mis)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700415 {
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 IIIe0d69652018-08-31 13:09:47 -0700419 expect_basic<const char*>(SD_BUS_TYPE_STRING, is.second.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700420 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
431TEST_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 IIIe0d69652018-08-31 13:09:47 -0700439 expect_basic<const char*>(SD_BUS_TYPE_STRING,
440 std::get<1>(tisb).c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700441 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 III13367e42018-06-26 15:23:13 -0700450TEST_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
461TEST_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 IIIe0d69652018-08-31 13:09:47 -0700468 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700469 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 IIIb4b4fa12018-06-25 17:20:06 -0700476TEST_F(ReadTest, Variant)
477{
478 const bool b1 = false;
479 const std::string s2{"asdf"};
William A. Kennington III4274c112018-11-26 09:50:13 -0800480 const std::variant<int, std::string, bool> v1{b1}, v2{s2};
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700481
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 IIIe0d69652018-08-31 13:09:47 -0700493 expect_basic<const char*>(SD_BUS_TYPE_STRING, s2.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700494 expect_exit_container();
495 }
496
William A. Kennington III4274c112018-11-26 09:50:13 -0800497 std::variant<int, std::string, bool> ret_v1, ret_v2;
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700498 new_message().read(ret_v1, ret_v2);
499 EXPECT_EQ(v1, ret_v1);
500 EXPECT_EQ(v2, ret_v2);
501}
502
William A. Kennington III13367e42018-06-26 15:23:13 -0700503TEST_F(ReadTest, VariantVerifyError)
504{
505 {
506 testing::InSequence seq;
507 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", -EINVAL);
508 }
509
William A. Kennington III4274c112018-11-26 09:50:13 -0800510 std::variant<int, bool> ret;
William A. Kennington III13367e42018-06-26 15:23:13 -0700511 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
512}
513
William A. Kennington III60a72832018-06-26 14:31:15 -0700514TEST_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 III13367e42018-06-26 15:23:13 -0700522
William A. Kennington III4274c112018-11-26 09:50:13 -0800523 std::variant<int, bool> ret;
William A. Kennington III60a72832018-06-26 14:31:15 -0700524 new_message().read(ret);
525}
526
William A. Kennington III13367e42018-06-26 15:23:13 -0700527TEST_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 III4274c112018-11-26 09:50:13 -0800536 std::variant<int, bool> ret;
William A. Kennington III13367e42018-06-26 15:23:13 -0700537 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
538}
539
540TEST_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 III4274c112018-11-26 09:50:13 -0800548 std::variant<int, bool> ret;
William A. Kennington III13367e42018-06-26 15:23:13 -0700549 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
550}
551
552TEST_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 III4274c112018-11-26 09:50:13 -0800562 std::variant<int, bool> ret;
William A. Kennington III13367e42018-06-26 15:23:13 -0700563 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
564}
565
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700566TEST_F(ReadTest, LargeCombo)
567{
568 const std::vector<std::set<std::string>> vas{
569 {"a", "b", "c"},
570 {"d", "", "e"},
571 };
William A. Kennington III4274c112018-11-26 09:50:13 -0800572 const std::map<std::string, std::variant<int, double>> msv = {
573 {"a", 3.3}, {"b", 1}, {"c", 4.4}};
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700574
575 {
576 testing::InSequence seq;
577
578 expect_enter_container(SD_BUS_TYPE_ARRAY, "as");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700579 for (const auto& as : vas)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700580 {
581 expect_at_end(false, 0);
582 expect_enter_container(SD_BUS_TYPE_ARRAY, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700583 for (const auto& s : as)
Patrick Williams4fe85a32016-09-08 15:03:56 -0500584 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700585 expect_at_end(false, 0);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700586 expect_basic<const char*>(SD_BUS_TYPE_STRING, s.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700587 }
588 expect_at_end(false, 1);
589 expect_exit_container();
590 }
591 expect_at_end(false, 1);
592 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500593
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700594 expect_enter_container(SD_BUS_TYPE_ARRAY, "{sv}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700595 for (const auto& sv : msv)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700596 {
597 expect_at_end(false, 0);
598 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "sv");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700599 expect_basic<const char*>(SD_BUS_TYPE_STRING, sv.first.c_str());
William A. Kennington III4274c112018-11-26 09:50:13 -0800600 if (std::holds_alternative<int>(sv.second))
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700601 {
602 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", true);
603 expect_enter_container(SD_BUS_TYPE_VARIANT, "i");
William A. Kennington III4274c112018-11-26 09:50:13 -0800604 expect_basic<int>(SD_BUS_TYPE_INT32, std::get<int>(sv.second));
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700605 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500606 }
607 else
608 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700609 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 III4274c112018-11-26 09:50:13 -0800613 std::get<double>(sv.second));
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700614 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500615 }
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700616 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500617 }
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700618 expect_at_end(false, 1);
619 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500620 }
Patrick Williams696e3152016-11-04 17:03:39 -0500621
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700622 std::vector<std::set<std::string>> ret_vas;
William A. Kennington III4274c112018-11-26 09:50:13 -0800623 std::map<std::string, std::variant<int, double>> ret_msv;
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700624 new_message().read(ret_vas, ret_msv);
625 EXPECT_EQ(vas, ret_vas);
626 EXPECT_EQ(msv, ret_msv);
Patrick Williams4fe85a32016-09-08 15:03:56 -0500627}
628
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700629} // namespace