chiark / gitweb /
remove unused variables
[elogind.git] / src / libsystemd / sd-bus / test-bus-marshal.c
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 #ifdef HAVE_DBUS
31 #include <dbus/dbus.h>
32 #endif
33
34 #include "log.h"
35 #include "util.h"
36
37 #include "sd-bus.h"
38 #include "bus-message.h"
39 #include "bus-util.h"
40 #include "bus-dump.h"
41 #include "bus-label.h"
42
43 static void test_bus_path_encode(void) {
44         _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
45
46         assert_se(sd_bus_path_encode("/foo/bar", "waldo", &a) >= 0 && streq(a, "/foo/bar/waldo"));
47         assert_se(sd_bus_path_decode(a, "/waldo", &b) == 0 && b == NULL);
48         assert_se(sd_bus_path_decode(a, "/foo/bar", &b) > 0 && streq(b, "waldo"));
49
50         assert_se(sd_bus_path_encode("xxxx", "waldo", &c) < 0);
51         assert_se(sd_bus_path_encode("/foo/", "waldo", &c) < 0);
52
53         assert_se(sd_bus_path_encode("/foo/bar", "", &c) >= 0 && streq(c, "/foo/bar/_"));
54         assert_se(sd_bus_path_decode(c, "/foo/bar", &d) > 0 && streq(d, ""));
55
56         assert_se(sd_bus_path_encode("/foo/bar", "foo.bar", &e) >= 0 && streq(e, "/foo/bar/foo_2ebar"));
57         assert_se(sd_bus_path_decode(e, "/foo/bar", &f) > 0 && streq(f, "foo.bar"));
58 }
59
60 static void test_bus_label_escape_one(const char *a, const char *b) {
61         _cleanup_free_ char *t = NULL, *x = NULL, *y = NULL;
62
63         assert_se(t = bus_label_escape(a));
64         assert_se(streq(t, b));
65
66         assert_se(x = bus_label_unescape(t));
67         assert_se(streq(a, x));
68
69         assert_se(y = bus_label_unescape(b));
70         assert_se(streq(a, y));
71 }
72
73 static void test_bus_label_escape(void) {
74         test_bus_label_escape_one("foo123bar", "foo123bar");
75         test_bus_label_escape_one("foo.bar", "foo_2ebar");
76         test_bus_label_escape_one("foo_2ebar", "foo_5f2ebar");
77         test_bus_label_escape_one("", "_");
78         test_bus_label_escape_one("_", "_5f");
79         test_bus_label_escape_one("1", "_31");
80         test_bus_label_escape_one(":1", "_3a1");
81 }
82
83 int main(int argc, char *argv[]) {
84         _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *copy = NULL;
85         int r, boolean;
86         const char *x, *x2, *y, *z, *a, *b, *c, *d, *a_signature;
87         uint8_t u, v;
88         void *buffer = NULL;
89         size_t sz;
90         char *h;
91         const int32_t integer_array[] = { -1, -2, 0, 1, 2 }, *return_array;
92         char *s;
93         _cleanup_free_ char *first = NULL, *second = NULL, *third = NULL;
94         _cleanup_fclose_ FILE *ms = NULL;
95         size_t first_size = 0, second_size = 0, third_size = 0;
96         _cleanup_bus_unref_ sd_bus *bus = NULL;
97
98         r = sd_bus_default_system(&bus);
99         if (r < 0)
100                 return EXIT_TEST_SKIP;
101
102         r = sd_bus_message_new_method_call(bus, &m, "foobar.waldo", "/", "foobar.waldo", "Piep");
103         assert_se(r >= 0);
104
105         r = sd_bus_message_append(m, "");
106         assert_se(r >= 0);
107
108         r = sd_bus_message_append(m, "s", "a string");
109         assert_se(r >= 0);
110
111         r = sd_bus_message_append(m, "s", NULL);
112         assert_se(r >= 0);
113
114         r = sd_bus_message_append(m, "asg", 2, "string #1", "string #2", "sba(tt)ss");
115         assert_se(r >= 0);
116
117         r = sd_bus_message_append(m, "sass", "foobar", 5, "foo", "bar", "waldo", "piep", "pap", "after");
118         assert_se(r >= 0);
119
120         r = sd_bus_message_append(m, "a{yv}", 2, 3, "s", "foo", 5, "s", "waldo");
121         assert_se(r >= 0);
122
123         r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3");
124         assert_se(r >= 0);
125
126         r = sd_bus_message_open_container(m, 'a', "s");
127         assert_se(r >= 0);
128
129         r = sd_bus_message_append_basic(m, 's', "foobar");
130         assert_se(r >= 0);
131
132         r = sd_bus_message_append_basic(m, 's', "waldo");
133         assert_se(r >= 0);
134
135         r = sd_bus_message_close_container(m);
136         assert_se(r >= 0);
137
138         r = sd_bus_message_append_string_space(m, 5, &s);
139         assert_se(r >= 0);
140         strcpy(s, "hallo");
141
142         r = sd_bus_message_append_array(m, 'i', integer_array, sizeof(integer_array));
143         assert_se(r >= 0);
144
145         r = sd_bus_message_append_array(m, 'u', NULL, 0);
146         assert_se(r >= 0);
147
148         r = bus_message_seal(m, 4711, 0);
149         assert_se(r >= 0);
150
151         bus_message_dump(m, stdout, true);
152
153         ms = open_memstream(&first, &first_size);
154         bus_message_dump(m, ms, false);
155         fflush(ms);
156         assert_se(!ferror(ms));
157
158         r = bus_message_get_blob(m, &buffer, &sz);
159         assert_se(r >= 0);
160
161         h = hexmem(buffer, sz);
162         assert_se(h);
163
164         log_info("message size = %zu, contents =\n%s", sz, h);
165         free(h);
166
167 #ifdef HAVE_GLIB
168         {
169                 GDBusMessage *g;
170                 char *p;
171
172 #if !defined(GLIB_VERSION_2_36)
173                 g_type_init();
174 #endif
175
176                 g = g_dbus_message_new_from_blob(buffer, sz, 0, NULL);
177                 p = g_dbus_message_print(g, 0);
178                 log_info("%s", p);
179                 g_free(p);
180                 g_object_unref(g);
181         }
182 #endif
183
184 #ifdef HAVE_DBUS
185         {
186                 DBusMessage *w;
187                 DBusError error;
188
189                 dbus_error_init(&error);
190
191                 w = dbus_message_demarshal(buffer, sz, &error);
192                 if (!w)
193                         log_error("%s", error.message);
194                 else
195                         dbus_message_unref(w);
196         }
197 #endif
198
199         m = sd_bus_message_unref(m);
200
201         r = bus_message_from_malloc(bus, buffer, sz, NULL, 0, NULL, NULL, &m);
202         assert_se(r >= 0);
203
204         bus_message_dump(m, stdout, true);
205
206         fclose(ms);
207         ms = open_memstream(&second, &second_size);
208         bus_message_dump(m, ms, false);
209         fflush(ms);
210         assert_se(!ferror(ms));
211         assert_se(first_size == second_size);
212         assert_se(memcmp(first, second, first_size) == 0);
213
214         assert_se(sd_bus_message_rewind(m, true) >= 0);
215
216         r = sd_bus_message_read(m, "ssasg", &x, &x2, 2, &y, &z, &a_signature);
217         assert_se(r > 0);
218         assert_se(streq(x, "a string"));
219         assert_se(streq(x2, ""));
220         assert_se(streq(y, "string #1"));
221         assert_se(streq(z, "string #2"));
222         assert_se(streq(a_signature, "sba(tt)ss"));
223
224         r = sd_bus_message_read(m, "sass", &x, 5, &y, &z, &a, &b, &c, &d);
225         assert_se(r > 0);
226         assert_se(streq(x, "foobar"));
227         assert_se(streq(y, "foo"));
228         assert_se(streq(z, "bar"));
229         assert_se(streq(a, "waldo"));
230         assert_se(streq(b, "piep"));
231         assert_se(streq(c, "pap"));
232         assert_se(streq(d, "after"));
233
234         r = sd_bus_message_read(m, "a{yv}", 2, &u, "s", &x, &v, "s", &y);
235         assert_se(r > 0);
236         assert_se(u == 3);
237         assert_se(streq(x, "foo"));
238         assert_se(v == 5);
239         assert_se(streq(y, "waldo"));
240
241         r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d);
242         assert_se(r > 0);
243         assert_se(boolean);
244         assert_se(streq(x, "aaa"));
245         assert_se(streq(y, "1"));
246         assert_se(streq(a, "bbb"));
247         assert_se(streq(b, "2"));
248         assert_se(streq(c, "ccc"));
249         assert_se(streq(d, "3"));
250
251         assert_se(sd_bus_message_verify_type(m, 'a', "s") > 0);
252
253         r = sd_bus_message_read(m, "as", 2, &x, &y);
254         assert_se(r > 0);
255         assert_se(streq(x, "foobar"));
256         assert_se(streq(y, "waldo"));
257
258         r = sd_bus_message_read_basic(m, 's', &s);
259         assert_se(r > 0);
260         assert_se(streq(s, "hallo"));
261
262         r = sd_bus_message_read_array(m, 'i', (const void**) &return_array, &sz);
263         assert_se(r > 0);
264         assert_se(sz == sizeof(integer_array));
265         assert_se(memcmp(integer_array, return_array, sz) == 0);
266
267         r = sd_bus_message_read_array(m, 'u', (const void**) &return_array, &sz);
268         assert_se(r > 0);
269         assert_se(sz == 0);
270
271         r = sd_bus_message_peek_type(m, NULL, NULL);
272         assert_se(r == 0);
273
274         r = sd_bus_message_new_method_call(bus, &copy, "foobar.waldo", "/", "foobar.waldo", "Piep");
275         assert_se(r >= 0);
276
277         r = sd_bus_message_rewind(m, true);
278         assert_se(r >= 0);
279
280         r = sd_bus_message_copy(copy, m, true);
281         assert_se(r >= 0);
282
283         r = bus_message_seal(copy, 4712, 0);
284         assert_se(r >= 0);
285
286         fclose(ms);
287         ms = open_memstream(&third, &third_size);
288         bus_message_dump(copy, ms, false);
289         fflush(ms);
290         assert_se(!ferror(ms));
291
292         printf("<%.*s>\n", (int) first_size, first);
293         printf("<%.*s>\n", (int) third_size, third);
294
295         assert_se(first_size == third_size);
296         assert_se(memcmp(first, third, third_size) == 0);
297
298         r = sd_bus_message_rewind(m, true);
299         assert_se(r >= 0);
300
301         assert_se(sd_bus_message_verify_type(m, 's', NULL) > 0);
302
303         r = sd_bus_message_skip(m, "ssasg");
304         assert_se(r > 0);
305
306         assert_se(sd_bus_message_verify_type(m, 's', NULL) > 0);
307
308         r = sd_bus_message_skip(m, "sass");
309         assert_se(r >= 0);
310
311         assert_se(sd_bus_message_verify_type(m, 'a', "{yv}") > 0);
312
313         r = sd_bus_message_skip(m, "a{yv}");
314         assert_se(r >= 0);
315
316         assert_se(sd_bus_message_verify_type(m, 'b', NULL) > 0);
317
318         r = sd_bus_message_read(m, "b", &boolean);
319         assert_se(r > 0);
320         assert_se(boolean);
321
322         r = sd_bus_message_enter_container(m, 0, NULL);
323         assert_se(r > 0);
324
325         r = sd_bus_message_read(m, "(ss)", &x, &y);
326         assert_se(r > 0);
327
328         r = sd_bus_message_read(m, "(ss)", &a, &b);
329         assert_se(r > 0);
330
331         r = sd_bus_message_read(m, "(ss)", &c, &d);
332         assert_se(r > 0);
333
334         r = sd_bus_message_read(m, "(ss)", &x, &y);
335         assert_se(r == 0);
336
337         r = sd_bus_message_exit_container(m);
338         assert_se(r >= 0);
339
340         assert_se(streq(x, "aaa"));
341         assert_se(streq(y, "1"));
342         assert_se(streq(a, "bbb"));
343         assert_se(streq(b, "2"));
344         assert_se(streq(c, "ccc"));
345         assert_se(streq(d, "3"));
346
347         test_bus_label_escape();
348         test_bus_path_encode();
349
350         return 0;
351 }