blob: 1d99940f326edec3b79a129db9e77ee15616537a [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 <sdbusplus/exception.hpp>
Patrick Williams4fe85a32016-09-08 15:03:56 -05004#include <sdbusplus/message.hpp>
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -07005#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Williams127b8ab2020-05-21 15:24:19 -05006
7#include <cerrno>
8#include <map>
Ed Tanous28dc36d2018-02-21 12:22:54 -08009#include <set>
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070010#include <string>
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070011#include <tuple>
12#include <unordered_map>
William A. Kennington III4274c112018-11-26 09:50:13 -080013#include <variant>
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070014#include <vector>
Patrick Williams4fe85a32016-09-08 15:03:56 -050015
Patrick Venture95269db2018-08-31 09:19:17 -070016#include <gmock/gmock.h>
17#include <gtest/gtest.h>
18
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070019namespace
Patrick Williams4fe85a32016-09-08 15:03:56 -050020{
Patrick Williams4fe85a32016-09-08 15:03:56 -050021
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070022using testing::DoAll;
23using testing::Return;
24using testing::StrEq;
25
26ACTION_TEMPLATE(AssignReadVal, HAS_1_TEMPLATE_PARAMS(typename, T),
27 AND_1_VALUE_PARAMS(val))
28{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070029 *static_cast<T*>(arg2) = val;
Patrick Williams4fe85a32016-09-08 15:03:56 -050030}
31
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070032class ReadTest : public testing::Test
Patrick Williams4fe85a32016-09-08 15:03:56 -050033{
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070034 protected:
35 testing::StrictMock<sdbusplus::SdBusMock> mock;
Patrick Williams4fe85a32016-09-08 15:03:56 -050036
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070037 void SetUp() override
Patrick Williams4fe85a32016-09-08 15:03:56 -050038 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070039 EXPECT_CALL(mock, sd_bus_message_new_method_call(testing::_, testing::_,
40 nullptr, nullptr,
41 nullptr, nullptr))
42 .WillRepeatedly(Return(0));
43 };
Patrick Williams4fe85a32016-09-08 15:03:56 -050044
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070045 sdbusplus::message::message new_message()
46 {
47 return sdbusplus::get_mocked_new(&mock).new_method_call(
48 nullptr, nullptr, nullptr, nullptr);
49 }
50
William A. Kennington III13367e42018-06-26 15:23:13 -070051 void expect_basic_error(char type, int ret)
52 {
53 EXPECT_CALL(mock, sd_bus_message_read_basic(nullptr, type, testing::_))
54 .WillOnce(Return(ret));
55 }
56
Patrick Venture2b238af2018-08-31 12:45:01 -070057 template <typename T>
58 void expect_basic(char type, T val)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070059 {
60 EXPECT_CALL(mock, sd_bus_message_read_basic(nullptr, type, testing::_))
61 .WillOnce(DoAll(AssignReadVal<T>(val), Return(0)));
62 }
63
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070064 void expect_verify_type(char type, const char* contents, int ret)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070065 {
66 EXPECT_CALL(mock,
67 sd_bus_message_verify_type(nullptr, type, StrEq(contents)))
68 .WillOnce(Return(ret));
69 }
70
71 void expect_at_end(bool complete, int ret)
72 {
73 EXPECT_CALL(mock, sd_bus_message_at_end(nullptr, complete))
74 .WillOnce(Return(ret));
75 }
76
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070077 void expect_skip(const char* contents, int ret = 0)
William A. Kennington III60a72832018-06-26 14:31:15 -070078 {
79 EXPECT_CALL(mock, sd_bus_message_skip(nullptr, StrEq(contents)))
William A. Kennington III13367e42018-06-26 15:23:13 -070080 .WillOnce(Return(ret));
William A. Kennington III60a72832018-06-26 14:31:15 -070081 }
82
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070083 void expect_enter_container(char type, const char* contents, int ret = 0)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070084 {
85 EXPECT_CALL(mock, sd_bus_message_enter_container(nullptr, type,
86 StrEq(contents)))
William A. Kennington III13367e42018-06-26 15:23:13 -070087 .WillOnce(Return(ret));
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070088 }
89
William A. Kennington III13367e42018-06-26 15:23:13 -070090 void expect_exit_container(int ret = 0)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070091 {
92 EXPECT_CALL(mock, sd_bus_message_exit_container(nullptr))
William A. Kennington III13367e42018-06-26 15:23:13 -070093 .WillOnce(Return(ret));
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -070094 }
95};
96
97TEST_F(ReadTest, Int)
98{
99 const int i = 1;
100 expect_basic<int>(SD_BUS_TYPE_INT32, i);
101 int ret;
102 new_message().read(ret);
103 EXPECT_EQ(i, ret);
104}
105
106TEST_F(ReadTest, Bool)
107{
108 const bool b = true;
109 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, b);
110 bool ret;
111 new_message().read(ret);
112 EXPECT_EQ(b, ret);
113}
114
115TEST_F(ReadTest, Double)
116{
117 const double d = 1.1;
118 expect_basic<double>(SD_BUS_TYPE_DOUBLE, d);
119 double ret;
120 new_message().read(ret);
121 EXPECT_EQ(d, ret);
122}
123
124TEST_F(ReadTest, CString)
125{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700126 const char* const s = "asdf";
127 expect_basic<const char*>(SD_BUS_TYPE_STRING, s);
128 const char* ret;
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700129 new_message().read(ret);
130 EXPECT_EQ(s, ret);
131}
132
133TEST_F(ReadTest, String)
134{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700135 const char* const s = "fsda";
136 expect_basic<const char*>(SD_BUS_TYPE_STRING, s);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700137 std::string ret;
138 new_message().read(ret);
139 // Pointer comparison here is intentional as we don't expect a copy
140 EXPECT_EQ(s, ret);
141}
142
143TEST_F(ReadTest, ObjectPath)
144{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700145 const char* const s = "/fsda";
146 expect_basic<const char*>(SD_BUS_TYPE_OBJECT_PATH, s);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700147 sdbusplus::message::object_path ret;
148 new_message().read(ret);
149 EXPECT_EQ(s, ret.str);
150}
151
152TEST_F(ReadTest, Signature)
153{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700154 const char* const s = "{ii}";
155 expect_basic<const char*>(SD_BUS_TYPE_SIGNATURE, s);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700156 sdbusplus::message::signature ret;
157 new_message().read(ret);
158 EXPECT_EQ(s, ret.str);
159}
160
161TEST_F(ReadTest, CombinedBasic)
162{
163 const double a = 2.2;
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700164 const char* const b = "ijkd";
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700165 const bool c = false;
166 const int d = 18;
167
168 {
169 testing::InSequence seq;
170 expect_basic<double>(SD_BUS_TYPE_DOUBLE, a);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700171 expect_basic<const char*>(SD_BUS_TYPE_STRING, b);
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700172 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, c);
173 expect_basic<int>(SD_BUS_TYPE_INT32, d);
174 }
175
176 double ret_a;
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700177 const char* ret_b;
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700178 bool ret_c;
179 int ret_d;
180 new_message().read(ret_a, ret_b, ret_c, ret_d);
181 EXPECT_EQ(a, ret_a);
182 EXPECT_EQ(b, ret_b);
183 EXPECT_EQ(c, ret_c);
184 EXPECT_EQ(d, ret_d);
185}
186
William A. Kennington III13367e42018-06-26 15:23:13 -0700187TEST_F(ReadTest, BasicError)
188{
189 expect_basic_error(SD_BUS_TYPE_INT32, -EINVAL);
190 int ret;
191 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
192}
193
194TEST_F(ReadTest, BasicStringError)
195{
196 expect_basic_error(SD_BUS_TYPE_STRING, -EINVAL);
197 std::string ret;
198 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
199}
200
201TEST_F(ReadTest, BasicStringWrapperError)
202{
203 expect_basic_error(SD_BUS_TYPE_SIGNATURE, -EINVAL);
204 sdbusplus::message::signature ret;
205 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
206}
207
208TEST_F(ReadTest, BasicBoolError)
209{
210 expect_basic_error(SD_BUS_TYPE_BOOLEAN, -EINVAL);
211 bool ret;
212 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
213}
214
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700215TEST_F(ReadTest, Vector)
216{
217 const std::vector<int> vi{1, 2, 3, 4};
218
219 {
220 testing::InSequence seq;
221 expect_enter_container(SD_BUS_TYPE_ARRAY, "i");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700222 for (const auto& i : vi)
Patrick Williams4fe85a32016-09-08 15:03:56 -0500223 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700224 expect_at_end(false, 0);
225 expect_basic<int>(SD_BUS_TYPE_INT32, i);
Patrick Williams4fe85a32016-09-08 15:03:56 -0500226 }
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700227 expect_at_end(false, 1);
228 expect_exit_container();
229 }
Patrick Williams4fe85a32016-09-08 15:03:56 -0500230
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700231 std::vector<int> ret_vi;
232 new_message().read(ret_vi);
233 EXPECT_EQ(vi, ret_vi);
234}
235
William A. Kennington III13367e42018-06-26 15:23:13 -0700236TEST_F(ReadTest, VectorEnterError)
237{
238 {
239 testing::InSequence seq;
240 expect_enter_container(SD_BUS_TYPE_ARRAY, "i", -EINVAL);
241 }
242
243 std::vector<int> ret;
244 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
245}
246
247TEST_F(ReadTest, VectorIterError)
248{
249 {
250 testing::InSequence seq;
251 expect_enter_container(SD_BUS_TYPE_ARRAY, "i");
252 expect_at_end(false, 0);
253 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
254 expect_at_end(false, -EINVAL);
255 }
256
257 std::vector<int> ret;
258 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
259}
260
261TEST_F(ReadTest, VectorExitError)
262{
263 {
264 testing::InSequence seq;
265 expect_enter_container(SD_BUS_TYPE_ARRAY, "i");
266 expect_at_end(false, 0);
267 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
268 expect_at_end(false, 0);
269 expect_basic<int>(SD_BUS_TYPE_INT32, 2);
270 expect_at_end(false, 1);
271 expect_exit_container(-EINVAL);
272 }
273
274 std::vector<int> ret;
275 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
276}
277
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700278TEST_F(ReadTest, Set)
279{
280 const std::set<std::string> ss{"one", "two", "eight"};
281
282 {
283 testing::InSequence seq;
284 expect_enter_container(SD_BUS_TYPE_ARRAY, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700285 for (const auto& s : ss)
Patrick Williams4fe85a32016-09-08 15:03:56 -0500286 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700287 expect_at_end(false, 0);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700288 expect_basic<const char*>(SD_BUS_TYPE_STRING, s.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700289 }
290 expect_at_end(false, 1);
291 expect_exit_container();
292 }
Patrick Williams4fe85a32016-09-08 15:03:56 -0500293
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700294 std::set<std::string> ret_ss;
295 new_message().read(ret_ss);
296 EXPECT_EQ(ss, ret_ss);
297}
298
299TEST_F(ReadTest, Map)
300{
301 const std::map<int, std::string> mis{
302 {1, "a"},
303 {2, "bc"},
304 {3, "def"},
305 {4, "ghij"},
306 };
307
308 {
309 testing::InSequence seq;
310 expect_enter_container(SD_BUS_TYPE_ARRAY, "{is}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700311 for (const auto& is : mis)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700312 {
313 expect_at_end(false, 0);
314 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "is");
315 expect_basic<int>(SD_BUS_TYPE_INT32, is.first);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700316 expect_basic<const char*>(SD_BUS_TYPE_STRING, is.second.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700317 expect_exit_container();
318 }
319 expect_at_end(false, 1);
320 expect_exit_container();
321 }
322
323 std::map<int, std::string> ret_mis;
324 new_message().read(ret_mis);
325 EXPECT_EQ(mis, ret_mis);
326}
327
William A. Kennington III13367e42018-06-26 15:23:13 -0700328TEST_F(ReadTest, MapEnterError)
329{
330 {
331 testing::InSequence seq;
332 expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}", -EINVAL);
333 }
334
335 std::map<std::string, int> ret;
336 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
337}
338
339TEST_F(ReadTest, MapEntryEnterError)
340{
341 {
342 testing::InSequence seq;
343 expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}");
344 expect_at_end(false, 0);
345 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si", -EINVAL);
346 }
347
348 std::map<std::string, int> ret;
349 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
350}
351
352TEST_F(ReadTest, MapEntryExitError)
353{
354 {
355 testing::InSequence seq;
356 expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}");
357 expect_at_end(false, 0);
358 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700359 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700360 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
361 expect_exit_container(-EINVAL);
362 }
363
364 std::map<std::string, int> ret;
365 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
366}
367
368TEST_F(ReadTest, MapIterError)
369{
370 {
371 testing::InSequence seq;
372 expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}");
373 expect_at_end(false, 0);
374 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700375 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700376 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
377 expect_exit_container();
378 expect_at_end(false, -EINVAL);
379 }
380
381 std::map<std::string, int> ret;
382 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
383}
384
385TEST_F(ReadTest, MapExitError)
386{
387 {
388 testing::InSequence seq;
389 expect_enter_container(SD_BUS_TYPE_ARRAY, "{si}");
390 expect_at_end(false, 0);
391 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "si");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700392 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700393 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
394 expect_exit_container();
395 expect_at_end(false, 1);
396 expect_exit_container(-EINVAL);
397 }
398
399 std::map<std::string, int> ret;
400 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
401}
402
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700403TEST_F(ReadTest, UnorderedMap)
404{
405 const std::unordered_map<int, std::string> mis{
406 {1, "a"},
407 {2, "bc"},
408 {3, "def"},
409 {4, "ghij"},
410 };
411
412 {
413 testing::InSequence seq;
414 expect_enter_container(SD_BUS_TYPE_ARRAY, "{is}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700415 for (const auto& is : mis)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700416 {
417 expect_at_end(false, 0);
418 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "is");
419 expect_basic<int>(SD_BUS_TYPE_INT32, is.first);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700420 expect_basic<const char*>(SD_BUS_TYPE_STRING, is.second.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700421 expect_exit_container();
422 }
423 expect_at_end(false, 1);
424 expect_exit_container();
425 }
426
427 std::unordered_map<int, std::string> ret_mis;
428 new_message().read(ret_mis);
429 EXPECT_EQ(mis, ret_mis);
430}
431
432TEST_F(ReadTest, Tuple)
433{
434 const std::tuple<int, std::string, bool> tisb{3, "hi", false};
435
436 {
437 testing::InSequence seq;
438 expect_enter_container(SD_BUS_TYPE_STRUCT, "isb");
439 expect_basic<int>(SD_BUS_TYPE_INT32, std::get<0>(tisb));
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700440 expect_basic<const char*>(SD_BUS_TYPE_STRING,
441 std::get<1>(tisb).c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700442 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, std::get<2>(tisb));
443 expect_exit_container();
444 }
445
446 std::tuple<int, std::string, bool> ret_tisb;
447 new_message().read(ret_tisb);
448 EXPECT_EQ(tisb, ret_tisb);
449}
450
William A. Kennington III13367e42018-06-26 15:23:13 -0700451TEST_F(ReadTest, TupleEnterError)
452{
453 {
454 testing::InSequence seq;
455 expect_enter_container(SD_BUS_TYPE_STRUCT, "bis", -EINVAL);
456 }
457
458 std::tuple<bool, int, std::string> ret;
459 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
460}
461
462TEST_F(ReadTest, TupleExitError)
463{
464 {
465 testing::InSequence seq;
466 expect_enter_container(SD_BUS_TYPE_STRUCT, "bis");
467 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false);
468 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700469 expect_basic<const char*>(SD_BUS_TYPE_STRING, "ab");
William A. Kennington III13367e42018-06-26 15:23:13 -0700470 expect_exit_container(-EINVAL);
471 }
472
473 std::tuple<bool, int, std::string> ret;
474 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
475}
476
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700477TEST_F(ReadTest, Variant)
478{
479 const bool b1 = false;
480 const std::string s2{"asdf"};
William A. Kennington III4274c112018-11-26 09:50:13 -0800481 const std::variant<int, std::string, bool> v1{b1}, v2{s2};
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700482
483 {
484 testing::InSequence seq;
485 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false);
486 expect_verify_type(SD_BUS_TYPE_VARIANT, "s", false);
487 expect_verify_type(SD_BUS_TYPE_VARIANT, "b", true);
488 expect_enter_container(SD_BUS_TYPE_VARIANT, "b");
489 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, b1);
490 expect_exit_container();
491 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false);
492 expect_verify_type(SD_BUS_TYPE_VARIANT, "s", true);
493 expect_enter_container(SD_BUS_TYPE_VARIANT, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700494 expect_basic<const char*>(SD_BUS_TYPE_STRING, s2.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700495 expect_exit_container();
496 }
497
William A. Kennington III4274c112018-11-26 09:50:13 -0800498 std::variant<int, std::string, bool> ret_v1, ret_v2;
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700499 new_message().read(ret_v1, ret_v2);
500 EXPECT_EQ(v1, ret_v1);
501 EXPECT_EQ(v2, ret_v2);
502}
503
William A. Kennington III13367e42018-06-26 15:23:13 -0700504TEST_F(ReadTest, VariantVerifyError)
505{
506 {
507 testing::InSequence seq;
508 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", -EINVAL);
509 }
510
William A. Kennington III4274c112018-11-26 09:50:13 -0800511 std::variant<int, bool> ret;
William A. Kennington III13367e42018-06-26 15:23:13 -0700512 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
513}
514
William A. Kennington III60a72832018-06-26 14:31:15 -0700515TEST_F(ReadTest, VariantSkipUnmatched)
516{
517 {
518 testing::InSequence seq;
519 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false);
520 expect_verify_type(SD_BUS_TYPE_VARIANT, "b", false);
521 expect_skip("v");
522 }
William A. Kennington III13367e42018-06-26 15:23:13 -0700523
William A. Kennington III4274c112018-11-26 09:50:13 -0800524 std::variant<int, bool> ret;
William A. Kennington III60a72832018-06-26 14:31:15 -0700525 new_message().read(ret);
526}
527
William A. Kennington III13367e42018-06-26 15:23:13 -0700528TEST_F(ReadTest, VariantSkipError)
529{
530 {
531 testing::InSequence seq;
532 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false);
533 expect_verify_type(SD_BUS_TYPE_VARIANT, "b", false);
534 expect_skip("v", -EINVAL);
535 }
536
William A. Kennington III4274c112018-11-26 09:50:13 -0800537 std::variant<int, bool> ret;
William A. Kennington III13367e42018-06-26 15:23:13 -0700538 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
539}
540
541TEST_F(ReadTest, VariantEnterError)
542{
543 {
544 testing::InSequence seq;
545 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", true);
546 expect_enter_container(SD_BUS_TYPE_VARIANT, "i", -EINVAL);
547 }
548
William A. Kennington III4274c112018-11-26 09:50:13 -0800549 std::variant<int, bool> ret;
William A. Kennington III13367e42018-06-26 15:23:13 -0700550 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
551}
552
553TEST_F(ReadTest, VariantExitError)
554{
555 {
556 testing::InSequence seq;
557 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", true);
558 expect_enter_container(SD_BUS_TYPE_VARIANT, "i");
559 expect_basic<int>(SD_BUS_TYPE_INT32, 10);
560 expect_exit_container(-EINVAL);
561 }
562
William A. Kennington III4274c112018-11-26 09:50:13 -0800563 std::variant<int, bool> ret;
William A. Kennington III13367e42018-06-26 15:23:13 -0700564 EXPECT_THROW(new_message().read(ret), sdbusplus::exception::SdBusError);
565}
566
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700567TEST_F(ReadTest, LargeCombo)
568{
569 const std::vector<std::set<std::string>> vas{
570 {"a", "b", "c"},
571 {"d", "", "e"},
572 };
William A. Kennington III4274c112018-11-26 09:50:13 -0800573 const std::map<std::string, std::variant<int, double>> msv = {
574 {"a", 3.3}, {"b", 1}, {"c", 4.4}};
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700575
576 {
577 testing::InSequence seq;
578
579 expect_enter_container(SD_BUS_TYPE_ARRAY, "as");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700580 for (const auto& as : vas)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700581 {
582 expect_at_end(false, 0);
583 expect_enter_container(SD_BUS_TYPE_ARRAY, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700584 for (const auto& s : as)
Patrick Williams4fe85a32016-09-08 15:03:56 -0500585 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700586 expect_at_end(false, 0);
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700587 expect_basic<const char*>(SD_BUS_TYPE_STRING, s.c_str());
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700588 }
589 expect_at_end(false, 1);
590 expect_exit_container();
591 }
592 expect_at_end(false, 1);
593 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500594
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700595 expect_enter_container(SD_BUS_TYPE_ARRAY, "{sv}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700596 for (const auto& sv : msv)
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700597 {
598 expect_at_end(false, 0);
599 expect_enter_container(SD_BUS_TYPE_DICT_ENTRY, "sv");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700600 expect_basic<const char*>(SD_BUS_TYPE_STRING, sv.first.c_str());
William A. Kennington III4274c112018-11-26 09:50:13 -0800601 if (std::holds_alternative<int>(sv.second))
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700602 {
603 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", true);
604 expect_enter_container(SD_BUS_TYPE_VARIANT, "i");
William A. Kennington III4274c112018-11-26 09:50:13 -0800605 expect_basic<int>(SD_BUS_TYPE_INT32, std::get<int>(sv.second));
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700606 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500607 }
608 else
609 {
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700610 expect_verify_type(SD_BUS_TYPE_VARIANT, "i", false);
611 expect_verify_type(SD_BUS_TYPE_VARIANT, "d", true);
612 expect_enter_container(SD_BUS_TYPE_VARIANT, "d");
613 expect_basic<double>(SD_BUS_TYPE_DOUBLE,
William A. Kennington III4274c112018-11-26 09:50:13 -0800614 std::get<double>(sv.second));
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_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500618 }
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700619 expect_at_end(false, 1);
620 expect_exit_container();
Patrick Williams4fe85a32016-09-08 15:03:56 -0500621 }
Patrick Williams696e3152016-11-04 17:03:39 -0500622
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700623 std::vector<std::set<std::string>> ret_vas;
William A. Kennington III4274c112018-11-26 09:50:13 -0800624 std::map<std::string, std::variant<int, double>> ret_msv;
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700625 new_message().read(ret_vas, ret_msv);
626 EXPECT_EQ(vas, ret_vas);
627 EXPECT_EQ(msv, ret_msv);
Patrick Williams4fe85a32016-09-08 15:03:56 -0500628}
629
William A. Kennington IIIb4b4fa12018-06-25 17:20:06 -0700630} // namespace