blob: 12c0bf3a3b3fce9950d6a659d4a6656a55a226e2 [file] [log] [blame]
Patrick Venture95269db2018-08-31 09:19:17 -07001#include <systemd/sd-bus-protocol.h>
2
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -07003#include <array>
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -07004#include <map>
Patrick Williams7802c072016-09-02 15:20:22 -05005#include <sdbusplus/message.hpp>
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -07006#include <sdbusplus/test/sdbus_mock.hpp>
Ed Tanous28dc36d2018-02-21 12:22:54 -08007#include <set>
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -07008#include <string>
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -07009#include <tuple>
10#include <unordered_map>
11#include <vector>
Patrick Williams1807fa42016-08-01 22:23:30 -050012
Patrick Venture95269db2018-08-31 09:19:17 -070013#include <gmock/gmock.h>
14#include <gtest/gtest.h>
15
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070016namespace
Patrick Williams1807fa42016-08-01 22:23:30 -050017{
Patrick Williams5b485792016-08-02 07:35:14 -050018
William A. Kennington III81fa02e2018-10-04 14:56:36 -070019namespace variant_ns = sdbusplus::message::variant_ns;
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070020using testing::Eq;
21using testing::MatcherCast;
22using testing::Pointee;
23using testing::Return;
24using testing::SafeMatcherCast;
25using testing::StrEq;
26
27class AppendTest : public testing::Test
28{
29 protected:
30 testing::StrictMock<sdbusplus::SdBusMock> mock;
31
32 void SetUp() override
33 {
34 EXPECT_CALL(mock, sd_bus_message_new_method_call(testing::_, testing::_,
35 nullptr, nullptr,
36 nullptr, nullptr))
37 .WillRepeatedly(Return(0));
38 };
39
40 sdbusplus::message::message new_message()
41 {
42 return sdbusplus::get_mocked_new(&mock).new_method_call(
43 nullptr, nullptr, nullptr, nullptr);
44 }
45
Patrick Venture2b238af2018-08-31 12:45:01 -070046 template <typename T>
47 void expect_basic(char type, T val)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070048 {
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070049 EXPECT_CALL(mock, sd_bus_message_append_basic(
50 nullptr, type,
51 MatcherCast<const void*>(
52 SafeMatcherCast<const T*>(Pointee(Eq(val))))))
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070053 .WillOnce(Return(0));
54 }
55
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070056 void expect_basic_string(char type, const char* str)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070057 {
58 EXPECT_CALL(mock, sd_bus_message_append_basic(
59 nullptr, type,
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070060 MatcherCast<const void*>(
61 SafeMatcherCast<const char*>(StrEq(str)))))
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070062 .WillOnce(Return(0));
63 }
64
William A. Kennington IIIe0d69652018-08-31 13:09:47 -070065 void expect_open_container(char type, const char* contents)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070066 {
67 EXPECT_CALL(
68 mock, sd_bus_message_open_container(nullptr, type, StrEq(contents)))
69 .WillOnce(Return(0));
70 }
71
72 void expect_close_container()
73 {
74 EXPECT_CALL(mock, sd_bus_message_close_container(nullptr))
75 .WillOnce(Return(0));
76 }
77};
78
79TEST_F(AppendTest, RValueInt)
80{
81 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
82 new_message().append(1);
Patrick Williams1807fa42016-08-01 22:23:30 -050083}
84
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070085TEST_F(AppendTest, LValueInt)
Patrick Williams1807fa42016-08-01 22:23:30 -050086{
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070087 const int a = 1;
88 expect_basic<int>(SD_BUS_TYPE_INT32, a);
89 new_message().append(a);
90}
Patrick Williams1807fa42016-08-01 22:23:30 -050091
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -070092TEST_F(AppendTest, XValueInt)
93{
94 int a = 1;
95 expect_basic<int>(SD_BUS_TYPE_INT32, a);
96 new_message().append(std::move(a));
97}
98
99TEST_F(AppendTest, RValueBool)
100{
101 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, true);
102 new_message().append(true);
103}
104
105TEST_F(AppendTest, LValueBool)
106{
107 const bool a = false;
108 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false);
109 new_message().append(a);
110}
111
112TEST_F(AppendTest, XValueBool)
113{
114 bool a = false;
115 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false);
116 new_message().append(std::move(a));
117}
118
119TEST_F(AppendTest, RValueDouble)
120{
121 expect_basic<double>(SD_BUS_TYPE_DOUBLE, 1.1);
122 new_message().append(1.1);
123}
124
125TEST_F(AppendTest, LValueDouble)
126{
127 const double a = 1.1;
128 expect_basic<double>(SD_BUS_TYPE_DOUBLE, a);
129 new_message().append(a);
130}
131
132TEST_F(AppendTest, XValueDouble)
133{
134 double a = 1.1;
135 expect_basic<double>(SD_BUS_TYPE_DOUBLE, a);
136 new_message().append(std::move(a));
137}
138
139TEST_F(AppendTest, RValueCString)
140{
141 expect_basic_string(SD_BUS_TYPE_STRING, "asdf");
142 new_message().append("asdf");
143}
144
145TEST_F(AppendTest, LValueCString)
146{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700147 const char* const s = "asdf";
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700148 expect_basic_string(SD_BUS_TYPE_STRING, s);
149 new_message().append(s);
150}
151
152TEST_F(AppendTest, XValueCString)
153{
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700154 const char* s = "asdf";
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700155 expect_basic_string(SD_BUS_TYPE_STRING, s);
156 new_message().append(std::move(s));
157}
158
159TEST_F(AppendTest, RValueString)
160{
161 expect_basic_string(SD_BUS_TYPE_STRING, "asdf");
162 new_message().append(std::string{"asdf"});
163}
164
165TEST_F(AppendTest, LValueString)
166{
167 std::string s{"asdf"};
168 expect_basic_string(SD_BUS_TYPE_STRING, s.c_str());
169 new_message().append(s);
170}
171
172TEST_F(AppendTest, XValueString)
173{
174 std::string s{"asdf"};
175 expect_basic_string(SD_BUS_TYPE_STRING, s.c_str());
176 new_message().append(std::move(s));
177}
178
179TEST_F(AppendTest, ObjectPath)
180{
181 sdbusplus::message::object_path o{"/asdf"};
182 expect_basic_string(SD_BUS_TYPE_OBJECT_PATH, o.str.c_str());
183 new_message().append(o);
184}
185
186TEST_F(AppendTest, Signature)
187{
188 sdbusplus::message::signature g{"ii"};
189 expect_basic_string(SD_BUS_TYPE_SIGNATURE, g.str.c_str());
190 new_message().append(g);
191}
192
193TEST_F(AppendTest, CombinedBasic)
194{
195 const int c = 3;
196 const std::string s1{"fdsa"};
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700197 const char* const s2 = "asdf";
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700198
Patrick Williams1807fa42016-08-01 22:23:30 -0500199 {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700200 testing::InSequence seq;
201 expect_basic<int>(SD_BUS_TYPE_INT32, 1);
202 expect_basic<double>(SD_BUS_TYPE_DOUBLE, 2.2);
203 expect_basic<int>(SD_BUS_TYPE_INT32, c);
204 expect_basic_string(SD_BUS_TYPE_STRING, s1.c_str());
205 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false);
206 expect_basic_string(SD_BUS_TYPE_STRING, s2);
207 }
208 new_message().append(1, 2.2, c, s1, false, s2);
209}
Patrick Williams1807fa42016-08-01 22:23:30 -0500210
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700211TEST_F(AppendTest, Array)
212{
213 const std::array<double, 4> a{1.1, 2.2, 3.3, 4.4};
214
215 {
216 testing::InSequence seq;
217 expect_open_container(SD_BUS_TYPE_ARRAY, "d");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700218 for (const auto& i : a)
Patrick Williams1807fa42016-08-01 22:23:30 -0500219 {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700220 expect_basic<double>(SD_BUS_TYPE_DOUBLE, i);
Patrick Williams1807fa42016-08-01 22:23:30 -0500221 }
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700222 expect_close_container();
223 }
224 new_message().append(a);
225}
Patrick Williams1807fa42016-08-01 22:23:30 -0500226
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700227TEST_F(AppendTest, Vector)
228{
229 const std::vector<int> v{1, 2, 3, 4};
230
231 {
232 testing::InSequence seq;
233 expect_open_container(SD_BUS_TYPE_ARRAY, "i");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700234 for (const auto& i : v)
Patrick Williams1807fa42016-08-01 22:23:30 -0500235 {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700236 expect_basic<int>(SD_BUS_TYPE_INT32, i);
237 }
238 expect_close_container();
239 }
240 new_message().append(v);
241}
242
243TEST_F(AppendTest, Set)
244{
245 const std::set<std::string> s{"one", "two", "eight"};
246
247 {
248 testing::InSequence seq;
249 expect_open_container(SD_BUS_TYPE_ARRAY, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700250 for (const auto& i : s)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700251 {
252 expect_basic_string(SD_BUS_TYPE_STRING, i.c_str());
253 }
254 expect_close_container();
255 }
256 new_message().append(s);
257}
258
259TEST_F(AppendTest, Map)
260{
261 const std::map<int, std::string> m{
262 {1, "a"},
263 {2, "bc"},
264 {3, "def"},
265 {4, "ghij"},
266 };
267
268 {
269 testing::InSequence seq;
270 expect_open_container(SD_BUS_TYPE_ARRAY, "{is}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700271 for (const auto& i : m)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700272 {
273 expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "is");
274 expect_basic<int>(SD_BUS_TYPE_INT32, i.first);
275 expect_basic_string(SD_BUS_TYPE_STRING, i.second.c_str());
276 expect_close_container();
277 }
278 expect_close_container();
279 }
280 new_message().append(m);
281}
282
283TEST_F(AppendTest, UnorderedMap)
284{
285 const std::unordered_map<int, bool> m{
286 {1, false},
287 {2, true},
288 {3, true},
289 {4, false},
290 };
291
292 {
293 testing::InSequence seq;
294 expect_open_container(SD_BUS_TYPE_ARRAY, "{ib}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700295 for (const auto& i : m)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700296 {
297 expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "ib");
298 expect_basic<int>(SD_BUS_TYPE_INT32, i.first);
299 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, i.second);
300 expect_close_container();
301 }
302 expect_close_container();
303 }
304 new_message().append(m);
305}
306
307TEST_F(AppendTest, Tuple)
308{
309 const std::tuple<int, std::string, bool> t{5, "asdf", false};
310
311 {
312 testing::InSequence seq;
313 expect_open_container(SD_BUS_TYPE_STRUCT, "isb");
314 expect_basic<int>(SD_BUS_TYPE_INT32, std::get<0>(t));
315 expect_basic_string(SD_BUS_TYPE_STRING, std::get<1>(t).c_str());
316 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, std::get<2>(t));
317 expect_close_container();
318 }
319 new_message().append(t);
320}
321
322TEST_F(AppendTest, Variant)
323{
324 const bool b1 = false;
325 const std::string s2{"asdf"};
326 const sdbusplus::message::variant<int, std::string, bool> v1{b1}, v2{s2};
327
328 {
329 testing::InSequence seq;
330 expect_open_container(SD_BUS_TYPE_VARIANT, "b");
331 expect_basic<int>(SD_BUS_TYPE_BOOLEAN, b1);
332 expect_close_container();
333 expect_open_container(SD_BUS_TYPE_VARIANT, "s");
334 expect_basic_string(SD_BUS_TYPE_STRING, s2.c_str());
335 expect_close_container();
336 }
337 new_message().append(v1, v2);
338}
339
340TEST_F(AppendTest, LargeCombo)
341{
342 std::vector<std::array<std::string, 3>> vas{{"a", "b", "c"},
343 {"d", "", "e"}};
344 std::map<std::string, sdbusplus::message::variant<int, double>> msv = {
345 {"a", 3.3}, {"b", 1}, {"c", 4.4}};
346
347 {
348 testing::InSequence seq;
349
350 expect_open_container(SD_BUS_TYPE_ARRAY, "as");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700351 for (const auto& as : vas)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700352 {
353 expect_open_container(SD_BUS_TYPE_ARRAY, "s");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700354 for (const auto& s : as)
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500355 {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700356 expect_basic_string(SD_BUS_TYPE_STRING, s.c_str());
357 }
358 expect_close_container();
359 }
360 expect_close_container();
361
362 expect_open_container(SD_BUS_TYPE_ARRAY, "{sv}");
William A. Kennington IIIe0d69652018-08-31 13:09:47 -0700363 for (const auto& sv : msv)
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700364 {
365 expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "sv");
366 expect_basic_string(SD_BUS_TYPE_STRING, sv.first.c_str());
William A. Kennington III81fa02e2018-10-04 14:56:36 -0700367 if (variant_ns::holds_alternative<int>(sv.second))
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700368 {
369 expect_open_container(SD_BUS_TYPE_VARIANT, "i");
William A. Kennington III81fa02e2018-10-04 14:56:36 -0700370 expect_basic<int>(SD_BUS_TYPE_INT32,
371 variant_ns::get<int>(sv.second));
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700372 expect_close_container();
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500373 }
374 else
375 {
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700376 expect_open_container(SD_BUS_TYPE_VARIANT, "d");
377 expect_basic<double>(SD_BUS_TYPE_DOUBLE,
William A. Kennington III81fa02e2018-10-04 14:56:36 -0700378 variant_ns::get<double>(sv.second));
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700379 expect_close_container();
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500380 }
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700381 expect_close_container();
Patrick Williams1807fa42016-08-01 22:23:30 -0500382 }
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700383 expect_close_container();
Patrick Williams1807fa42016-08-01 22:23:30 -0500384 }
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700385 new_message().append(vas, msv);
Patrick Williams1807fa42016-08-01 22:23:30 -0500386}
387
William A. Kennington IIIa1e9e2a2018-06-23 13:33:25 -0700388} // namespace