blob: 288199bb8e719778d0f0eb6640a6df79fabfb0fd [file] [log] [blame]
Patrick Venture95269db2018-08-31 09:19:17 -07001#include <systemd/sd-bus-protocol.h>
2
Patrick Williams7802c072016-09-02 15:20:22 -05003#include <sdbusplus/message.hpp>
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -07004#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Williams127b8ab2020-05-21 15:24:19 -05005
6#include <array>
7#include <map>
Ed Tanous28dc36d2018-02-21 12:22:54 -08008#include <set>
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -07009#include <string>
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070010#include <tuple>
11#include <unordered_map>
Patrick Williamsb98bdc62020-06-16 16:15:40 -050012#include <unordered_set>
William A. Kennington III4274c112018-11-26 09:50:13 -080013#include <variant>
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070014#include <vector>
Patrick Williams1807fa42016-08-01 22:23:30 -050015
Patrick Venture95269db2018-08-31 09:19:17 -070016#include <gmock/gmock.h>
17#include <gtest/gtest.h>
18
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070019namespace
Patrick Williams1807fa42016-08-01 22:23:30 -050020{
Patrick Williams5b485792016-08-02 07:35:14 -050021
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070022using testing::Eq;
23using testing::MatcherCast;
24using testing::Pointee;
25using testing::Return;
26using testing::SafeMatcherCast;
27using testing::StrEq;
28
29class AppendTest : public testing::Test
30{
31 protected:
32 testing::StrictMock<sdbusplus::SdBusMock> mock;
33
34 void SetUp() override
35 {
36 EXPECT_CALL(mock, sd_bus_message_new_method_call(testing::_, testing::_,
37 nullptr, nullptr,
38 nullptr, nullptr))
39 .WillRepeatedly(Return(0));
40 };
41
42 sdbusplus::message::message new_message()
43 {
44 return sdbusplus::get_mocked_new(&mock).new_method_call(
45 nullptr, nullptr, nullptr, nullptr);
46 }
47
Patrick Venture2b238af2018-08-31 12:45:01 -070048 template <typename T>
49 void expect_basic(char type, T val)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070050 {
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070051 EXPECT_CALL(mock, sd_bus_message_append_basic(
52 nullptr, type,
53 MatcherCast<const void*>(
54 SafeMatcherCast<const T*>(Pointee(Eq(val))))))
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070055 .WillOnce(Return(0));
56 }
57
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070058 void expect_basic_string(char type, const char* str)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070059 {
60 EXPECT_CALL(mock, sd_bus_message_append_basic(
61 nullptr, type,
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070062 MatcherCast<const void*>(
63 SafeMatcherCast<const char*>(StrEq(str)))))
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070064 .WillOnce(Return(0));
65 }
66
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070067 void expect_open_container(char type, const char* contents)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070068 {
69 EXPECT_CALL(
70 mock, sd_bus_message_open_container(nullptr, type, StrEq(contents)))
71 .WillOnce(Return(0));
72 }
73
74 void expect_close_container()
75 {
76 EXPECT_CALL(mock, sd_bus_message_close_container(nullptr))
77 .WillOnce(Return(0));
78 }
79};
80
81TEST_F(AppendTest, RValueInt)
82{
83 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
84 new_message().append(1);
Patrick Williams1807fa42016-08-01 22:23:30 -050085}
86
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070087TEST_F(AppendTest, LValueInt)
Patrick Williams1807fa42016-08-01 22:23:30 -050088{
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070089 const int a = 1;
90 expect_basic<int>(SD_BUS_TYPE_INT32, a);
91 new_message().append(a);
92}
Patrick Williams1807fa42016-08-01 22:23:30 -050093
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070094TEST_F(AppendTest, XValueInt)
95{
96 int a = 1;
97 expect_basic<int>(SD_BUS_TYPE_INT32, a);
98 new_message().append(std::move(a));
99}
100
101TEST_F(AppendTest, RValueBool)
102{
103 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, true);
104 new_message().append(true);
105}
106
107TEST_F(AppendTest, LValueBool)
108{
109 const bool a = false;
110 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false);
111 new_message().append(a);
112}
113
114TEST_F(AppendTest, XValueBool)
115{
116 bool a = false;
117 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false);
118 new_message().append(std::move(a));
119}
120
121TEST_F(AppendTest, RValueDouble)
122{
123 expect_basic<double>(SD_BUS_TYPE_DOUBLE, 1.1);
124 new_message().append(1.1);
125}
126
127TEST_F(AppendTest, LValueDouble)
128{
129 const double a = 1.1;
130 expect_basic<double>(SD_BUS_TYPE_DOUBLE, a);
131 new_message().append(a);
132}
133
134TEST_F(AppendTest, XValueDouble)
135{
136 double a = 1.1;
137 expect_basic<double>(SD_BUS_TYPE_DOUBLE, a);
138 new_message().append(std::move(a));
139}
140
141TEST_F(AppendTest, RValueCString)
142{
143 expect_basic_string(SD_BUS_TYPE_STRING, "asdf");
144 new_message().append("asdf");
145}
146
147TEST_F(AppendTest, LValueCString)
148{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700149 const char* const s = "asdf";
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700150 expect_basic_string(SD_BUS_TYPE_STRING, s);
151 new_message().append(s);
152}
153
154TEST_F(AppendTest, XValueCString)
155{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700156 const char* s = "asdf";
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700157 expect_basic_string(SD_BUS_TYPE_STRING, s);
158 new_message().append(std::move(s));
159}
160
161TEST_F(AppendTest, RValueString)
162{
163 expect_basic_string(SD_BUS_TYPE_STRING, "asdf");
164 new_message().append(std::string{"asdf"});
165}
166
167TEST_F(AppendTest, LValueString)
168{
169 std::string s{"asdf"};
170 expect_basic_string(SD_BUS_TYPE_STRING, s.c_str());
171 new_message().append(s);
172}
173
174TEST_F(AppendTest, XValueString)
175{
176 std::string s{"asdf"};
177 expect_basic_string(SD_BUS_TYPE_STRING, s.c_str());
178 new_message().append(std::move(s));
179}
180
181TEST_F(AppendTest, ObjectPath)
182{
183 sdbusplus::message::object_path o{"/asdf"};
184 expect_basic_string(SD_BUS_TYPE_OBJECT_PATH, o.str.c_str());
185 new_message().append(o);
186}
187
188TEST_F(AppendTest, Signature)
189{
190 sdbusplus::message::signature g{"ii"};
191 expect_basic_string(SD_BUS_TYPE_SIGNATURE, g.str.c_str());
192 new_message().append(g);
193}
194
195TEST_F(AppendTest, CombinedBasic)
196{
197 const int c = 3;
198 const std::string s1{"fdsa"};
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700199 const char* const s2 = "asdf";
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700200
Patrick Williams1807fa42016-08-01 22:23:30 -0500201 {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700202 testing::InSequence seq;
203 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
204 expect_basic<double>(SD_BUS_TYPE_DOUBLE, 2.2);
205 expect_basic<int>(SD_BUS_TYPE_INT32, c);
206 expect_basic_string(SD_BUS_TYPE_STRING, s1.c_str());
207 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false);
208 expect_basic_string(SD_BUS_TYPE_STRING, s2);
209 }
210 new_message().append(1, 2.2, c, s1, false, s2);
211}
Patrick Williams1807fa42016-08-01 22:23:30 -0500212
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700213TEST_F(AppendTest, Array)
214{
215 const std::array<double, 4> a{1.1, 2.2, 3.3, 4.4};
216
217 {
218 testing::InSequence seq;
219 expect_open_container(SD_BUS_TYPE_ARRAY, "d");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700220 for (const auto& i : a)
Patrick Williams1807fa42016-08-01 22:23:30 -0500221 {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700222 expect_basic<double>(SD_BUS_TYPE_DOUBLE, i);
Patrick Williams1807fa42016-08-01 22:23:30 -0500223 }
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700224 expect_close_container();
225 }
226 new_message().append(a);
227}
Patrick Williams1807fa42016-08-01 22:23:30 -0500228
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700229TEST_F(AppendTest, Vector)
230{
231 const std::vector<int> v{1, 2, 3, 4};
232
233 {
234 testing::InSequence seq;
235 expect_open_container(SD_BUS_TYPE_ARRAY, "i");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700236 for (const auto& i : v)
Patrick Williams1807fa42016-08-01 22:23:30 -0500237 {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700238 expect_basic<int>(SD_BUS_TYPE_INT32, i);
239 }
240 expect_close_container();
241 }
242 new_message().append(v);
243}
244
245TEST_F(AppendTest, Set)
246{
247 const std::set<std::string> s{"one", "two", "eight"};
248
249 {
250 testing::InSequence seq;
251 expect_open_container(SD_BUS_TYPE_ARRAY, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700252 for (const auto& i : s)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700253 {
254 expect_basic_string(SD_BUS_TYPE_STRING, i.c_str());
255 }
256 expect_close_container();
257 }
258 new_message().append(s);
259}
260
Patrick Williamsb98bdc62020-06-16 16:15:40 -0500261TEST_F(AppendTest, UnorderedSet)
262{
263 const std::unordered_set<std::string> s{"one", "two", "eight"};
264
265 {
266 testing::InSequence seq;
267 expect_open_container(SD_BUS_TYPE_ARRAY, "s");
268 for (const auto& i : s)
269 {
270 expect_basic_string(SD_BUS_TYPE_STRING, i.c_str());
271 }
272 expect_close_container();
273 }
274 new_message().append(s);
275}
276
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700277TEST_F(AppendTest, Map)
278{
279 const std::map<int, std::string> m{
280 {1, "a"},
281 {2, "bc"},
282 {3, "def"},
283 {4, "ghij"},
284 };
285
286 {
287 testing::InSequence seq;
288 expect_open_container(SD_BUS_TYPE_ARRAY, "{is}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700289 for (const auto& i : m)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700290 {
291 expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "is");
292 expect_basic<int>(SD_BUS_TYPE_INT32, i.first);
293 expect_basic_string(SD_BUS_TYPE_STRING, i.second.c_str());
294 expect_close_container();
295 }
296 expect_close_container();
297 }
298 new_message().append(m);
299}
300
301TEST_F(AppendTest, UnorderedMap)
302{
303 const std::unordered_map<int, bool> m{
304 {1, false},
305 {2, true},
306 {3, true},
307 {4, false},
308 };
309
310 {
311 testing::InSequence seq;
312 expect_open_container(SD_BUS_TYPE_ARRAY, "{ib}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700313 for (const auto& i : m)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700314 {
315 expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "ib");
316 expect_basic<int>(SD_BUS_TYPE_INT32, i.first);
317 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, i.second);
318 expect_close_container();
319 }
320 expect_close_container();
321 }
322 new_message().append(m);
323}
324
325TEST_F(AppendTest, Tuple)
326{
327 const std::tuple<int, std::string, bool> t{5, "asdf", false};
328
329 {
330 testing::InSequence seq;
331 expect_open_container(SD_BUS_TYPE_STRUCT, "isb");
332 expect_basic<int>(SD_BUS_TYPE_INT32, std::get<0>(t));
333 expect_basic_string(SD_BUS_TYPE_STRING, std::get<1>(t).c_str());
334 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, std::get<2>(t));
335 expect_close_container();
336 }
337 new_message().append(t);
338}
339
340TEST_F(AppendTest, Variant)
341{
342 const bool b1 = false;
343 const std::string s2{"asdf"};
William A. Kennington III4274c112018-11-26 09:50:13 -0800344 const std::variant<int, std::string, bool> v1{b1}, v2{s2};
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700345
346 {
347 testing::InSequence seq;
348 expect_open_container(SD_BUS_TYPE_VARIANT, "b");
349 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, b1);
350 expect_close_container();
351 expect_open_container(SD_BUS_TYPE_VARIANT, "s");
352 expect_basic_string(SD_BUS_TYPE_STRING, s2.c_str());
353 expect_close_container();
354 }
355 new_message().append(v1, v2);
356}
357
358TEST_F(AppendTest, LargeCombo)
359{
360 std::vector<std::array<std::string, 3>> vas{{"a", "b", "c"},
361 {"d", "", "e"}};
William A. Kennington III4274c112018-11-26 09:50:13 -0800362 std::map<std::string, std::variant<int, double>> msv = {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700363 {"a", 3.3}, {"b", 1}, {"c", 4.4}};
364
365 {
366 testing::InSequence seq;
367
368 expect_open_container(SD_BUS_TYPE_ARRAY, "as");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700369 for (const auto& as : vas)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700370 {
371 expect_open_container(SD_BUS_TYPE_ARRAY, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700372 for (const auto& s : as)
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500373 {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700374 expect_basic_string(SD_BUS_TYPE_STRING, s.c_str());
375 }
376 expect_close_container();
377 }
378 expect_close_container();
379
380 expect_open_container(SD_BUS_TYPE_ARRAY, "{sv}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700381 for (const auto& sv : msv)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700382 {
383 expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "sv");
384 expect_basic_string(SD_BUS_TYPE_STRING, sv.first.c_str());
William A. Kennington III4274c112018-11-26 09:50:13 -0800385 if (std::holds_alternative<int>(sv.second))
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700386 {
387 expect_open_container(SD_BUS_TYPE_VARIANT, "i");
William A. Kennington III4274c112018-11-26 09:50:13 -0800388 expect_basic<int>(SD_BUS_TYPE_INT32, std::get<int>(sv.second));
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700389 expect_close_container();
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500390 }
391 else
392 {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700393 expect_open_container(SD_BUS_TYPE_VARIANT, "d");
394 expect_basic<double>(SD_BUS_TYPE_DOUBLE,
William A. Kennington III4274c112018-11-26 09:50:13 -0800395 std::get<double>(sv.second));
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700396 expect_close_container();
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500397 }
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700398 expect_close_container();
Patrick Williams1807fa42016-08-01 22:23:30 -0500399 }
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700400 expect_close_container();
Patrick Williams1807fa42016-08-01 22:23:30 -0500401 }
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700402 new_message().append(vas, msv);
Patrick Williams1807fa42016-08-01 22:23:30 -0500403}
404
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700405} // namespace