blob: 61dcb29faf23706dd3a99a7648ce31cf3dcf570c [file] [log] [blame]
Patrick Williams1807fa42016-08-01 22:23:30 -05001#include <iostream>
Patrick Williams1807fa42016-08-01 22:23:30 -05002#include <cassert>
Patrick Williams7802c072016-09-02 15:20:22 -05003#include <sdbusplus/message.hpp>
Patrick Williams5b485792016-08-02 07:35:14 -05004#include <sdbusplus/bus.hpp>
Patrick Williams1807fa42016-08-01 22:23:30 -05005
6// Global to share the dbus type string between client and server.
7static std::string verifyTypeString;
8
Patrick Williams07e1d6f2016-09-08 15:54:09 -05009using verifyCallback_t = void(*)(sd_bus_message*);
10verifyCallback_t verifyCallback = nullptr;
11
Patrick Williams24fb2962016-11-04 17:08:59 -050012static constexpr auto SERVICE = "sdbusplus.test.message.append";
Patrick Williams1807fa42016-08-01 22:23:30 -050013static constexpr auto INTERFACE = SERVICE;
14static constexpr auto TEST_METHOD = "test";
15static constexpr auto QUIT_METHOD = "quit";
16
17// Open up the sdbus and claim SERVICE name.
Patrick Williams5b485792016-08-02 07:35:14 -050018auto serverInit()
Patrick Williams1807fa42016-08-01 22:23:30 -050019{
Patrick Williams5b485792016-08-02 07:35:14 -050020 auto b = sdbusplus::bus::new_default();
21 b.request_name(SERVICE);
22
23 return std::move(b);
Patrick Williams1807fa42016-08-01 22:23:30 -050024}
25
26// Thread to run the dbus server.
27void* server(void* b)
28{
Patrick Williams5b485792016-08-02 07:35:14 -050029 auto bus = sdbusplus::bus::bus(reinterpret_cast<sdbusplus::bus::busp_t>(b));
Patrick Williams1807fa42016-08-01 22:23:30 -050030
31 while(1)
32 {
33 // Wait for messages.
Patrick Williams13b97c52016-09-09 09:36:55 -050034 auto m = bus.process().release();
Patrick Williams1807fa42016-08-01 22:23:30 -050035
Patrick Williams5b485792016-08-02 07:35:14 -050036 if(m == nullptr)
Patrick Williams1807fa42016-08-01 22:23:30 -050037 {
Patrick Williams5b485792016-08-02 07:35:14 -050038 bus.wait();
Patrick Williams1807fa42016-08-01 22:23:30 -050039 continue;
40 }
41
42 if (sd_bus_message_is_method_call(m, INTERFACE, TEST_METHOD))
43 {
44 // Verify the message type matches what the test expects.
Patrick Williams1807fa42016-08-01 22:23:30 -050045 assert(verifyTypeString == sd_bus_message_get_signature(m, true));
Patrick Williams07e1d6f2016-09-08 15:54:09 -050046 if (verifyCallback)
47 {
48 verifyCallback(m);
49 verifyCallback = nullptr;
50 }
51 else
52 {
53 std::cout << "Warning: No verification for "
54 << verifyTypeString << std::endl;
55 }
Patrick Williams1807fa42016-08-01 22:23:30 -050056 // Reply to client.
57 sd_bus_reply_method_return(m, nullptr);
58 }
59 else if (sd_bus_message_is_method_call(m, INTERFACE, QUIT_METHOD))
60 {
61 // Reply and exit.
62 sd_bus_reply_method_return(m, nullptr);
63 break;
64 }
65 }
Patrick Williams696e3152016-11-04 17:03:39 -050066
67 return nullptr;
Patrick Williams1807fa42016-08-01 22:23:30 -050068}
69
Patrick Williams7802c072016-09-02 15:20:22 -050070auto newMethodCall__test(sdbusplus::bus::bus& b)
Patrick Williams1807fa42016-08-01 22:23:30 -050071{
72 // Allocate a method-call message for INTERFACE,TEST_METHOD.
Patrick Williams7802c072016-09-02 15:20:22 -050073 return b.new_method_call(SERVICE, "/", INTERFACE, TEST_METHOD);
Patrick Williams1807fa42016-08-01 22:23:30 -050074}
75
76void runTests()
77{
78 using namespace std::literals;
79
Patrick Williams5b485792016-08-02 07:35:14 -050080 auto b = sdbusplus::bus::new_default();
Patrick Williams1807fa42016-08-01 22:23:30 -050081
82 // Test r-value int.
83 {
Patrick Williams7802c072016-09-02 15:20:22 -050084 auto m = newMethodCall__test(b);
85 m.append(1);
Patrick Williams1807fa42016-08-01 22:23:30 -050086 verifyTypeString = "i";
Patrick Williams07e1d6f2016-09-08 15:54:09 -050087
88 struct verify
89 {
90 static void op(sd_bus_message* m)
91 {
92 int32_t i = 0;
93 sd_bus_message_read_basic(m, 'i', &i);
94 assert(i == 1);
95 }
96 };
97 verifyCallback = &verify::op;
98
Patrick Williams5b485792016-08-02 07:35:14 -050099 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -0500100 }
Patrick Williams1807fa42016-08-01 22:23:30 -0500101 // Test l-value int.
102 {
Patrick Williams7802c072016-09-02 15:20:22 -0500103 auto m = newMethodCall__test(b);
Patrick Williams1807fa42016-08-01 22:23:30 -0500104 int a = 1;
Patrick Williams7802c072016-09-02 15:20:22 -0500105 m.append(a, a);
Patrick Williams1807fa42016-08-01 22:23:30 -0500106 verifyTypeString = "ii";
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500107
108 struct verify
109 {
110 static void op(sd_bus_message* m)
111 {
112 int32_t a = 0, b = 0;
113 sd_bus_message_read(m, "ii", &a, &b);
114 assert(a == 1);
115 assert(b == 1);
116 }
117 };
118 verifyCallback = &verify::op;
119
Patrick Williams5b485792016-08-02 07:35:14 -0500120 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -0500121 }
122
123 // Test multiple ints.
124 {
Patrick Williams7802c072016-09-02 15:20:22 -0500125 auto m = newMethodCall__test(b);
126 m.append(1, 2, 3, 4, 5);
Patrick Williams1807fa42016-08-01 22:23:30 -0500127 verifyTypeString = "iiiii";
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500128
129 struct verify
130 {
131 static void op(sd_bus_message* m)
132 {
133 int32_t a = 0, b = 0, c = 0, d = 0, e = 0;
134 sd_bus_message_read(m, "iiiii", &a, &b, &c, &d, &e);
135 assert(a == 1);
136 assert(b == 2);
137 assert(c == 3);
138 assert(d == 4);
139 assert(e == 5);
140 }
141 };
142 verifyCallback = &verify::op;
143
Patrick Williams5b485792016-08-02 07:35:14 -0500144 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -0500145 }
146
Patrick Williams9e3b51e2016-10-18 07:47:29 -0500147 // Test double and bool.
148 {
149 auto m = newMethodCall__test(b);
150 bool t = true;
Patrick Williamsb760a992016-11-28 11:26:55 -0600151 bool f = false;
152 bool f2 = false;
153 m.append(t, true, f, std::move(f2), false, 1.1);
154 verifyTypeString = "bbbbbd";
Patrick Williams9e3b51e2016-10-18 07:47:29 -0500155
156 struct verify
157 {
158 static void op(sd_bus_message* m)
159 {
Patrick Williamsb760a992016-11-28 11:26:55 -0600160 bool t1, t2, f1, f2, f3;
Patrick Williams9e3b51e2016-10-18 07:47:29 -0500161 double d;
Patrick Williamsb760a992016-11-28 11:26:55 -0600162 sd_bus_message_read(m, "bbbbbd", &t1, &t2, &f1, &f2, &f3, &d);
Patrick Williams9e3b51e2016-10-18 07:47:29 -0500163 assert(t1);
164 assert(t2);
165 assert(!f1);
Patrick Williamsb760a992016-11-28 11:26:55 -0600166 assert(!f2);
167 assert(!f3);
Patrick Williams9e3b51e2016-10-18 07:47:29 -0500168 assert(d == 1.1);
169 }
170 };
171 verifyCallback = &verify::op;
172
173 b.call_noreply(m);
174 }
175
Patrick Williams1807fa42016-08-01 22:23:30 -0500176 // Test r-value string.
177 {
Patrick Williams7802c072016-09-02 15:20:22 -0500178 auto m = newMethodCall__test(b);
179 m.append("asdf"s);
Patrick Williams1807fa42016-08-01 22:23:30 -0500180 verifyTypeString = "s";
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500181
182 struct verify
183 {
184 static void op(sd_bus_message* m)
185 {
186 const char* s = nullptr;
187 sd_bus_message_read_basic(m, 's', &s);
188 assert(0 == strcmp("asdf", s));
189 }
190 };
191 verifyCallback = &verify::op;
192
Patrick Williams5b485792016-08-02 07:35:14 -0500193 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -0500194 }
195
196 // Test multiple strings, various forms.
197 {
Patrick Williams7802c072016-09-02 15:20:22 -0500198 auto m = newMethodCall__test(b);
Patrick Williams1807fa42016-08-01 22:23:30 -0500199 auto str = "jkl;"s;
200 auto str2 = "JKL:"s;
Patrick Williams7802c072016-09-02 15:20:22 -0500201 m.append(1, "asdf", "ASDF"s, str,
202 std::move(str2), 5);
Patrick Williams1807fa42016-08-01 22:23:30 -0500203 verifyTypeString = "issssi";
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500204
205 struct verify
206 {
207 static void op(sd_bus_message* m)
208 {
209 int32_t a = 0, b = 0;
210 const char *s0 = nullptr, *s1 = nullptr, *s2 = nullptr,
211 *s3 = nullptr;
212 sd_bus_message_read(m, "issssi", &a, &s0, &s1, &s2, &s3, &b);
213 assert(a == 1);
214 assert(b == 5);
215 assert(0 == strcmp("asdf", s0));
216 assert(0 == strcmp("ASDF", s1));
217 assert(0 == strcmp("jkl;", s2));
218 assert(0 == strcmp("JKL:", s3));
219 }
220 };
221 verifyCallback = &verify::op;
222
Patrick Williams5b485792016-08-02 07:35:14 -0500223 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -0500224 }
225
Patrick Williams930460c2016-07-20 17:52:55 -0500226 // Test vector.
227 {
228 auto m = newMethodCall__test(b);
229 std::vector<std::string> s{ "1", "2", "3"};
230 m.append(1, s, 2);
231 verifyTypeString = "iasi";
232
233 struct verify
234 {
235 static void op(sd_bus_message* m)
236 {
237 int32_t a = 0;
238 sd_bus_message_read(m, "i", &a);
239 assert(a == 1);
240
241 auto rc = sd_bus_message_enter_container(m,
242 SD_BUS_TYPE_ARRAY,
243 "s");
244 assert(0 <= rc);
245
246 const char* s = nullptr;
247 sd_bus_message_read_basic(m, 's', &s);
248 assert(0 == strcmp("1", s));
249 sd_bus_message_read_basic(m, 's', &s);
250 assert(0 == strcmp("2", s));
251 sd_bus_message_read_basic(m, 's', &s);
252 assert(0 == strcmp("3", s));
253 assert(1 == sd_bus_message_at_end(m, false));
254
255 sd_bus_message_exit_container(m);
256
257 sd_bus_message_read(m, "i", &a);
258 assert(a == 2);
259
260 }
261 };
262 verifyCallback = &verify::op;
263
264
265 b.call_noreply(m);
266 }
267
Patrick Williamsbbe0e432016-09-10 08:57:03 -0500268 // Test map.
269 {
270 auto m = newMethodCall__test(b);
271 std::map<std::string, int> s = { { "asdf", 3 }, { "jkl;", 4 } };
272 m.append(1, s, 2);
273 verifyTypeString = "ia{si}i";
274
275 struct verify
276 {
277 static void op(sd_bus_message* m)
278 {
279 int32_t a = 0;
280 sd_bus_message_read(m, "i", &a);
281 assert(a == 1);
282
283 auto rc = sd_bus_message_enter_container(m,
284 SD_BUS_TYPE_ARRAY,
285 "{si}");
286 assert(0 <= rc);
287
288 rc = sd_bus_message_enter_container(m,
289 SD_BUS_TYPE_DICT_ENTRY,
290 "si");
291 assert(0 <= rc);
292
293 const char* s = nullptr;
294 sd_bus_message_read_basic(m, 's', &s);
295 assert(0 == strcmp("asdf", s));
296 sd_bus_message_read_basic(m, 'i', &a);
297 assert(a == 3);
298
299 assert(1 == sd_bus_message_at_end(m, false));
300 sd_bus_message_exit_container(m);
301
302 rc = sd_bus_message_enter_container(m,
303 SD_BUS_TYPE_DICT_ENTRY,
304 "si");
305 assert(0 <= rc);
306
307 sd_bus_message_read_basic(m, 's', &s);
308 assert(0 == strcmp("jkl;", s));
309 sd_bus_message_read_basic(m, 'i', &a);
310 assert(a == 4);
311
312 assert(1 == sd_bus_message_at_end(m, false));
313 sd_bus_message_exit_container(m);
314
315 assert(1 == sd_bus_message_at_end(m, false));
316 sd_bus_message_exit_container(m);
317
318 sd_bus_message_read(m, "i", &a);
319 assert(a == 2);
320 }
321 };
322 verifyCallback = &verify::op;
323
324 b.call_noreply(m);
325 }
326
Patrick Williamsaa2f48a2016-09-10 14:23:34 -0500327 // Test tuple.
328 {
329 auto m = newMethodCall__test(b);
330 std::tuple<int, double, std::string> a{ 3, 4.1, "asdf" };
331 m.append(1, a, 2);
332 verifyTypeString = "i(ids)i";
333
334 struct verify
335 {
336 static void op(sd_bus_message* m)
337 {
338 int32_t a = 0;
339 double b = 0;
340 const char* c = nullptr;
341
342 sd_bus_message_read(m, "i", &a);
343 assert(a == 1);
344
345 auto rc = sd_bus_message_enter_container(m,
346 SD_BUS_TYPE_STRUCT,
347 "ids");
348 assert(0 <= rc);
349
350 sd_bus_message_read(m, "ids", &a, &b, &c);
351 assert(a == 3);
352 assert(b == 4.1);
353 assert(0 == strcmp(c, "asdf"));
354
355 sd_bus_message_exit_container(m);
356
357 sd_bus_message_read(m, "i", &a);
358 assert(a == 2);
359 }
360 };
361 verifyCallback = &verify::op;
362
363 b.call_noreply(m);
364 }
365
Patrick Williams75596782016-09-11 21:14:10 -0500366 // Test variant.
367 {
368 auto m = newMethodCall__test(b);
369 sdbusplus::message::variant<int, double> a1{3.1}, a2{4};
370 m.append(1, a1, a2, 2);
371 verifyTypeString = "ivvi";
372
373 struct verify
374 {
375 static void op(sd_bus_message* m)
376 {
377 int32_t a = 0;
378 double b = 0;
379
380 sd_bus_message_read(m, "i", &a);
381 assert(a == 1);
382
383 sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, "d");
384 sd_bus_message_read(m, "d", &b);
385 assert(b == 3.1);
386 sd_bus_message_exit_container(m);
387
388 sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, "i");
389 sd_bus_message_read(m, "i", &a);
390 assert(a == 4);
391 sd_bus_message_exit_container(m);
392
393 sd_bus_message_read(m, "i", &a);
394 assert(a == 2);
395 }
396 };
397 verifyCallback = &verify::op;
398
399 b.call_noreply(m);
400 }
401
402 // Test map-variant.
403 {
404 auto m = newMethodCall__test(b);
405 std::map<std::string, sdbusplus::message::variant<int, double>> a1 =
406 { { "asdf", 3 }, { "jkl;", 4.1 } };
407 m.append(1, a1, 2);
408 verifyTypeString = "ia{sv}i";
409
410 struct verify
411 {
412 static void op(sd_bus_message* m)
413 {
414 int32_t a = 0;
415 double b = 0;
416 const char* c = nullptr;
417
418 sd_bus_message_read(m, "i", &a);
419 assert(a == 1);
420
421 sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
422 sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv");
423 sd_bus_message_read(m, "s", &c);
424 assert(0 == strcmp("asdf", c));
425 sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, "i");
426 sd_bus_message_read(m, "i", &a);
427 assert(a == 3);
428 sd_bus_message_exit_container(m);
429 sd_bus_message_exit_container(m);
430 sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv");
431 sd_bus_message_read(m, "s", &c);
432 assert(0 == strcmp("jkl;", c));
433 sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, "d");
434 sd_bus_message_read(m, "d", &b);
435 assert(b == 4.1);
436 sd_bus_message_exit_container(m);
437 sd_bus_message_exit_container(m);
438 sd_bus_message_exit_container(m);
439
440 sd_bus_message_read(m, "i", &a);
441 assert(a == 2);
442 }
443 };
444 verifyCallback = &verify::op;
445
446 b.call_noreply(m);
447 }
448
Patrick Williams1807fa42016-08-01 22:23:30 -0500449 // Shutdown server.
Patrick Williams7802c072016-09-02 15:20:22 -0500450 {
451 auto m = b.new_method_call(SERVICE, "/", INTERFACE, QUIT_METHOD);
452 b.call_noreply(m);
453 }
Patrick Williams1807fa42016-08-01 22:23:30 -0500454}
455
456int main()
457{
Patrick Williams07e1d6f2016-09-08 15:54:09 -0500458
Patrick Williams1807fa42016-08-01 22:23:30 -0500459 // Initialize and start server thread.
460 pthread_t t;
461 {
Patrick Williams5b485792016-08-02 07:35:14 -0500462 auto b = serverInit();
463 pthread_create(&t, NULL, server, b.release());
Patrick Williams1807fa42016-08-01 22:23:30 -0500464 }
465
466 runTests();
467
468 // Wait for server thread to exit.
469 pthread_join(t, NULL);
470
471 return 0;
472}