]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-bus/test-bus-marshal.c
bus: implicitly handle peer commands Ping() and GetMachineId()
[thirdparty/systemd.git] / src / libsystemd-bus / test-bus-marshal.c
CommitLineData
de1c301e
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2013 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <assert.h>
23#include <stdlib.h>
24#include <byteswap.h>
25
26#ifdef HAVE_GLIB
27#include <gio/gio.h>
28#endif
29
30#include <dbus.h>
31
32#include "log.h"
33#include "util.h"
34
35#include "sd-bus.h"
36#include "bus-message.h"
37
38int main(int argc, char *argv[]) {
39 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
9a17484d
LP
40 int r, boolean;
41 const char *x, *y, *z, *a, *b, *c, *d;
de1c301e
LP
42 uint8_t u, v;
43 void *buffer = NULL;
44 size_t sz;
45 char *h;
46
47 r = sd_bus_message_new_method_call(NULL, "foobar.waldo", "/", "foobar.waldo", "Piep", &m);
48 assert_se(r >= 0);
49
50 r = sd_bus_message_append(m, "s", "a string");
51 assert_se(r >= 0);
52
53 r = sd_bus_message_append(m, "as", 2, "string #1", "string #2");
54 assert_se(r >= 0);
55
56 r = sd_bus_message_append(m, "sass", "foobar", 5, "foo", "bar", "waldo", "piep", "pap", "after");
57 assert_se(r >= 0);
58
59 r = sd_bus_message_append(m, "a{yv}", 2, 3, "s", "foo", 5, "s", "waldo");
60 assert_se(r >= 0);
61
62 r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3");
63 assert_se(r >= 0);
64
65 r = sd_bus_message_open_container(m, 'a', "s");
66 assert_se(r >= 0);
67
68 r = sd_bus_message_append_basic(m, 's', "foobar");
69 assert_se(r >= 0);
70
71 r = sd_bus_message_append_basic(m, 's', "waldo");
72 assert_se(r >= 0);
73
74 r = sd_bus_message_close_container(m);
75 assert_se(r >= 0);
76
9a17484d 77 r = bus_message_seal(m, 4711);
de1c301e
LP
78 assert_se(r >= 0);
79
9a17484d 80 bus_message_dump(m);
de1c301e
LP
81
82 r = bus_message_get_blob(m, &buffer, &sz);
83 assert_se(r >= 0);
84
85 h = hexmem(buffer, sz);
86 assert_se(h);
87
88 log_info("message size = %lu, contents =\n%s", (unsigned long) sz, h);
89 free(h);
90
91#ifdef HAVE_GLIB
92 {
93 GDBusMessage *g;
94 char *p;
95
96 g_type_init();
97
98 g = g_dbus_message_new_from_blob(buffer, sz, 0, NULL);
99 p = g_dbus_message_print(g, 0);
100 log_info("%s", p);
101 g_free(p);
102 g_object_unref(g);
103 }
104#endif
105
106 {
107 DBusMessage *w;
108 DBusError error;
109
110 dbus_error_init(&error);
111
112 w = dbus_message_demarshal(buffer, sz, &error);
113 if (!w) {
114 log_error("%s", error.message);
115 } else
116 dbus_message_unref(w);
117 }
118
80a46c73
LP
119 m = sd_bus_message_unref(m);
120
121 r = bus_message_from_malloc(buffer, sz, &m);
122 assert_se(r >= 0);
123
124 bus_message_dump(m);
de1c301e 125
9a17484d
LP
126 assert_se(sd_bus_message_rewind(m, true) >= 0);
127
128 r = sd_bus_message_read(m, "sas", &x, 2, &y, &z);
129 assert_se(r > 0);
130 assert_se(streq(x, "a string"));
131 assert_se(streq(y, "string #1"));
132 assert_se(streq(z, "string #2"));
133
134 r = sd_bus_message_read(m, "sass", &x, 5, &y, &z, &a, &b, &c, &d);
135 assert_se(r > 0);
136 assert_se(streq(x, "foobar"));
137 assert_se(streq(y, "foo"));
138 assert_se(streq(z, "bar"));
139 assert_se(streq(a, "waldo"));
140 assert_se(streq(b, "piep"));
141 assert_se(streq(c, "pap"));
142 assert_se(streq(d, "after"));
143
144 r = sd_bus_message_read(m, "a{yv}", 2, &u, "s", &x, &v, "s", &y);
145 assert_se(r > 0);
146 assert_se(u == 3);
147 assert_se(streq(x, "foo"));
148 assert_se(v == 5);
149 assert_se(streq(y, "waldo"));
150
151 r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d);
152 assert_se(r > 0);
153 assert_se(boolean);
154 assert_se(streq(x, "aaa"));
155 assert_se(streq(y, "1"));
156 assert_se(streq(a, "bbb"));
157 assert_se(streq(b, "2"));
158 assert_se(streq(c, "ccc"));
159 assert_se(streq(d, "3"));
160
161 r = sd_bus_message_read(m, "as", 2, &x, &y);
162 assert_se(r > 0);
163 assert_se(streq(x, "foobar"));
164 assert_se(streq(y, "waldo"));
165
166 r = sd_bus_message_peek_type(m, NULL, NULL);
167 assert_se(r == 0);
de1c301e
LP
168
169 return 0;
170}