1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
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.
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.
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/>.
31 #include <dbus/dbus.h>
38 #include "bus-message.h"
42 static void test_bus_label_escape_one(const char *a, const char *b) {
43 _cleanup_free_ char *t = NULL, *x = NULL, *y = NULL;
45 assert_se(t = sd_bus_label_escape(a));
46 assert_se(streq(t, b));
48 assert_se(x = sd_bus_label_unescape(t));
49 assert_se(streq(a, x));
51 assert_se(y = sd_bus_label_unescape(b));
52 assert_se(streq(a, y));
55 static void test_bus_label_escape(void) {
56 test_bus_label_escape_one("foo123bar", "foo123bar");
57 test_bus_label_escape_one("foo.bar", "foo_2ebar");
58 test_bus_label_escape_one("foo_2ebar", "foo_5f2ebar");
59 test_bus_label_escape_one("", "_");
60 test_bus_label_escape_one("_", "_5f");
61 test_bus_label_escape_one("1", "_31");
62 test_bus_label_escape_one(":1", "_3a1");
65 int main(int argc, char *argv[]) {
66 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *copy = NULL;
68 const char *x, *x2, *y, *z, *a, *b, *c, *d, *a_signature;
73 const int32_t integer_array[] = { -1, -2, 0, 1, 2 }, *return_array;
75 _cleanup_free_ char *first = NULL, *second = NULL, *third = NULL;
76 _cleanup_fclose_ FILE *ms = NULL;
77 size_t first_size = 0, second_size = 0, third_size = 0;
79 r = sd_bus_message_new_method_call(NULL, "foobar.waldo", "/", "foobar.waldo", "Piep", &m);
82 r = sd_bus_message_append(m, "s", "a string");
85 r = sd_bus_message_append(m, "s", NULL);
88 r = sd_bus_message_append(m, "asg", 2, "string #1", "string #2", "sba(tt)ss");
91 r = sd_bus_message_append(m, "sass", "foobar", 5, "foo", "bar", "waldo", "piep", "pap", "after");
94 r = sd_bus_message_append(m, "a{yv}", 2, 3, "s", "foo", 5, "s", "waldo");
97 r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3");
100 r = sd_bus_message_open_container(m, 'a', "s");
103 r = sd_bus_message_append_basic(m, 's', "foobar");
106 r = sd_bus_message_append_basic(m, 's', "waldo");
109 r = sd_bus_message_close_container(m);
112 r = sd_bus_message_append_string_space(m, 5, &s);
116 r = sd_bus_message_append_array(m, 'i', integer_array, sizeof(integer_array));
119 r = sd_bus_message_append_array(m, 'u', NULL, 0);
122 r = bus_message_seal(m, 4711, 0);
125 bus_message_dump(m, stdout, true);
127 ms = open_memstream(&first, &first_size);
128 bus_message_dump(m, ms, false);
130 assert_se(!ferror(ms));
132 r = bus_message_get_blob(m, &buffer, &sz);
135 h = hexmem(buffer, sz);
138 log_info("message size = %lu, contents =\n%s", (unsigned long) sz, h);
146 #if !defined(GLIB_VERSION_2_36)
150 g = g_dbus_message_new_from_blob(buffer, sz, 0, NULL);
151 p = g_dbus_message_print(g, 0);
163 dbus_error_init(&error);
165 w = dbus_message_demarshal(buffer, sz, &error);
167 log_error("%s", error.message);
169 dbus_message_unref(w);
173 m = sd_bus_message_unref(m);
175 r = bus_message_from_malloc(NULL, buffer, sz, NULL, 0, NULL, NULL, &m);
178 bus_message_dump(m, stdout, true);
181 ms = open_memstream(&second, &second_size);
182 bus_message_dump(m, ms, false);
184 assert_se(!ferror(ms));
185 assert_se(first_size == second_size);
186 assert_se(memcmp(first, second, first_size) == 0);
188 assert_se(sd_bus_message_rewind(m, true) >= 0);
190 r = sd_bus_message_read(m, "ssasg", &x, &x2, 2, &y, &z, &a_signature);
192 assert_se(streq(x, "a string"));
193 assert_se(streq(x2, ""));
194 assert_se(streq(y, "string #1"));
195 assert_se(streq(z, "string #2"));
196 assert_se(streq(a_signature, "sba(tt)ss"));
198 r = sd_bus_message_read(m, "sass", &x, 5, &y, &z, &a, &b, &c, &d);
200 assert_se(streq(x, "foobar"));
201 assert_se(streq(y, "foo"));
202 assert_se(streq(z, "bar"));
203 assert_se(streq(a, "waldo"));
204 assert_se(streq(b, "piep"));
205 assert_se(streq(c, "pap"));
206 assert_se(streq(d, "after"));
208 r = sd_bus_message_read(m, "a{yv}", 2, &u, "s", &x, &v, "s", &y);
211 assert_se(streq(x, "foo"));
213 assert_se(streq(y, "waldo"));
215 r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d);
218 assert_se(streq(x, "aaa"));
219 assert_se(streq(y, "1"));
220 assert_se(streq(a, "bbb"));
221 assert_se(streq(b, "2"));
222 assert_se(streq(c, "ccc"));
223 assert_se(streq(d, "3"));
225 assert_se(sd_bus_message_verify_type(m, 'a', "s") > 0);
227 r = sd_bus_message_read(m, "as", 2, &x, &y);
229 assert_se(streq(x, "foobar"));
230 assert_se(streq(y, "waldo"));
232 r = sd_bus_message_read_basic(m, 's', &s);
234 assert_se(streq(s, "hallo"));
236 r = sd_bus_message_read_array(m, 'i', (const void**) &return_array, &sz);
238 assert_se(sz == sizeof(integer_array));
239 assert_se(memcmp(integer_array, return_array, sz) == 0);
241 r = sd_bus_message_read_array(m, 'u', (const void**) &return_array, &sz);
245 r = sd_bus_message_peek_type(m, NULL, NULL);
248 r = sd_bus_message_new_method_call(NULL, "foobar.waldo", "/", "foobar.waldo", "Piep", ©);
251 r = sd_bus_message_rewind(m, true);
254 r = sd_bus_message_copy(copy, m, true);
257 r = bus_message_seal(copy, 4712, 0);
261 ms = open_memstream(&third, &third_size);
262 bus_message_dump(copy, ms, false);
264 assert_se(!ferror(ms));
266 printf("<%.*s>\n", (int) first_size, first);
267 printf("<%.*s>\n", (int) third_size, third);
269 assert_se(first_size == third_size);
270 assert_se(memcmp(first, third, third_size) == 0);
272 r = sd_bus_message_rewind(m, true);
275 assert_se(sd_bus_message_verify_type(m, 's', NULL) > 0);
277 r = sd_bus_message_skip(m, "ssasg");
280 assert_se(sd_bus_message_verify_type(m, 's', NULL) > 0);
282 r = sd_bus_message_skip(m, "sass");
285 assert_se(sd_bus_message_verify_type(m, 'a', "{yv}") > 0);
287 r = sd_bus_message_skip(m, "a{yv}");
290 assert_se(sd_bus_message_verify_type(m, 'b', NULL) > 0);
292 r = sd_bus_message_read(m, "b", &boolean);
296 r = sd_bus_message_enter_container(m, 0, NULL);
299 r = sd_bus_message_read(m, "(ss)", &x, &y);
302 r = sd_bus_message_read(m, "(ss)", &a, &b);
305 r = sd_bus_message_read(m, "(ss)", &c, &d);
308 r = sd_bus_message_read(m, "(ss)", &x, &y);
311 r = sd_bus_message_exit_container(m);
314 assert_se(streq(x, "aaa"));
315 assert_se(streq(y, "1"));
316 assert_se(streq(a, "bbb"));
317 assert_se(streq(b, "2"));
318 assert_se(streq(c, "ccc"));
319 assert_se(streq(d, "3"));
321 test_bus_label_escape();