chiark / gitweb /
bus: add sd_bus_message_append_string_space() for zero-copy string appending
[elogind.git] / src / libsystemd-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 #include <dbus.h>
31
32 #include "log.h"
33 #include "util.h"
34
35 #include "sd-bus.h"
36 #include "bus-message.h"
37
38 int main(int argc, char *argv[]) {
39         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
40         int r, boolean;
41         const char *x, *y, *z, *a, *b, *c, *d;
42         uint8_t u, v;
43         void *buffer = NULL;
44         size_t sz;
45         char *h;
46         const int32_t integer_array[] = { -1, -2, 0, 1, 2 }, *return_array;
47         char *s;
48
49         r = sd_bus_message_new_method_call(NULL, "foobar.waldo", "/", "foobar.waldo", "Piep", &m);
50         assert_se(r >= 0);
51
52         r = sd_bus_message_append(m, "s", "a string");
53         assert_se(r >= 0);
54
55         r = sd_bus_message_append(m, "s", NULL);
56         assert_se(r < 0);
57
58         r = sd_bus_message_append(m, "as", 2, "string #1", "string #2");
59         assert_se(r >= 0);
60
61         r = sd_bus_message_append(m, "sass", "foobar", 5, "foo", "bar", "waldo", "piep", "pap", "after");
62         assert_se(r >= 0);
63
64         r = sd_bus_message_append(m, "a{yv}", 2, 3, "s", "foo", 5, "s", "waldo");
65         assert_se(r >= 0);
66
67         r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3");
68         assert_se(r >= 0);
69
70         r = sd_bus_message_open_container(m, 'a', "s");
71         assert_se(r >= 0);
72
73         r = sd_bus_message_append_basic(m, 's', "foobar");
74         assert_se(r >= 0);
75
76         r = sd_bus_message_append_basic(m, 's', "waldo");
77         assert_se(r >= 0);
78
79         r = sd_bus_message_close_container(m);
80         assert_se(r >= 0);
81
82         r = sd_bus_message_append_string_space(m, 5, &s);
83         assert_se(r >= 0);
84         strcpy(s, "hallo");
85
86         r = sd_bus_message_append_array(m, 'i', integer_array, sizeof(integer_array));
87         assert_se(r >= 0);
88
89         r = bus_message_seal(m, 4711);
90         assert_se(r >= 0);
91
92         bus_message_dump(m);
93
94         r = bus_message_get_blob(m, &buffer, &sz);
95         assert_se(r >= 0);
96
97         h = hexmem(buffer, sz);
98         assert_se(h);
99
100         log_info("message size = %lu, contents =\n%s", (unsigned long) sz, h);
101         free(h);
102
103 #ifdef HAVE_GLIB
104         {
105                 GDBusMessage *g;
106                 char *p;
107
108 #if !defined(GLIB_VERSION_2_36)
109                 g_type_init();
110 #endif
111
112                 g = g_dbus_message_new_from_blob(buffer, sz, 0, NULL);
113                 p = g_dbus_message_print(g, 0);
114                 log_info("%s", p);
115                 g_free(p);
116                 g_object_unref(g);
117         }
118 #endif
119
120         {
121                 DBusMessage *w;
122                 DBusError error;
123
124                 dbus_error_init(&error);
125
126                 w = dbus_message_demarshal(buffer, sz, &error);
127                 if (!w) {
128                         log_error("%s", error.message);
129                 } else
130                         dbus_message_unref(w);
131         }
132
133         m = sd_bus_message_unref(m);
134
135         r = bus_message_from_malloc(buffer, sz, NULL, 0, NULL, NULL, &m);
136         assert_se(r >= 0);
137
138         bus_message_dump(m);
139
140         assert_se(sd_bus_message_rewind(m, true) >= 0);
141
142         r = sd_bus_message_read(m, "sas", &x, 2, &y, &z);
143         assert_se(r > 0);
144         assert_se(streq(x, "a string"));
145         assert_se(streq(y, "string #1"));
146         assert_se(streq(z, "string #2"));
147
148         r = sd_bus_message_read(m, "sass", &x, 5, &y, &z, &a, &b, &c, &d);
149         assert_se(r > 0);
150         assert_se(streq(x, "foobar"));
151         assert_se(streq(y, "foo"));
152         assert_se(streq(z, "bar"));
153         assert_se(streq(a, "waldo"));
154         assert_se(streq(b, "piep"));
155         assert_se(streq(c, "pap"));
156         assert_se(streq(d, "after"));
157
158         r = sd_bus_message_read(m, "a{yv}", 2, &u, "s", &x, &v, "s", &y);
159         assert_se(r > 0);
160         assert_se(u == 3);
161         assert_se(streq(x, "foo"));
162         assert_se(v == 5);
163         assert_se(streq(y, "waldo"));
164
165         r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d);
166         assert_se(r > 0);
167         assert_se(boolean);
168         assert_se(streq(x, "aaa"));
169         assert_se(streq(y, "1"));
170         assert_se(streq(a, "bbb"));
171         assert_se(streq(b, "2"));
172         assert_se(streq(c, "ccc"));
173         assert_se(streq(d, "3"));
174
175         r = sd_bus_message_read(m, "as", 2, &x, &y);
176         assert_se(r > 0);
177         assert_se(streq(x, "foobar"));
178         assert_se(streq(y, "waldo"));
179
180         r = sd_bus_message_read_basic(m, 's', &s);
181         assert_se(r > 0);
182         assert_se(streq(s, "hallo"));
183
184         r = sd_bus_message_read_array(m, 'i', (const void**) &return_array, &sz);
185         assert_se(r > 0);
186         assert_se(sz == sizeof(integer_array));
187         assert_se(memcmp(integer_array, return_array, sz) == 0);
188
189         r = sd_bus_message_peek_type(m, NULL, NULL);
190         assert_se(r == 0);
191
192         return 0;
193 }