Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include <math.h>
4 : #include <stdlib.h>
5 :
6 : #if HAVE_GLIB
7 : #include <gio/gio.h>
8 : #endif
9 :
10 : #if HAVE_DBUS
11 : #include <dbus/dbus.h>
12 : #endif
13 :
14 : #include "sd-bus.h"
15 :
16 : #include "alloc-util.h"
17 : #include "bus-dump.h"
18 : #include "bus-label.h"
19 : #include "bus-message.h"
20 : #include "bus-util.h"
21 : #include "escape.h"
22 : #include "fd-util.h"
23 : #include "fileio.h"
24 : #include "log.h"
25 : #include "tests.h"
26 : #include "util.h"
27 :
28 1 : static void test_bus_path_encode_unique(void) {
29 1 : _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
30 :
31 1 : assert_se(bus_path_encode_unique(NULL, "/foo/bar", "some.sender", "a.suffix", &a) >= 0 && streq_ptr(a, "/foo/bar/some_2esender/a_2esuffix"));
32 1 : assert_se(bus_path_decode_unique(a, "/foo/bar", &b, &c) > 0 && streq_ptr(b, "some.sender") && streq_ptr(c, "a.suffix"));
33 1 : assert_se(bus_path_decode_unique(a, "/bar/foo", &d, &d) == 0 && !d);
34 1 : assert_se(bus_path_decode_unique("/foo/bar/onlyOneSuffix", "/foo/bar", &d, &d) == 0 && !d);
35 1 : assert_se(bus_path_decode_unique("/foo/bar/_/_", "/foo/bar", &d, &e) > 0 && streq_ptr(d, "") && streq_ptr(e, ""));
36 1 : }
37 :
38 1 : static void test_bus_path_encode(void) {
39 1 : _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
40 :
41 1 : assert_se(sd_bus_path_encode("/foo/bar", "waldo", &a) >= 0 && streq(a, "/foo/bar/waldo"));
42 1 : assert_se(sd_bus_path_decode(a, "/waldo", &b) == 0 && b == NULL);
43 1 : assert_se(sd_bus_path_decode(a, "/foo/bar", &b) > 0 && streq(b, "waldo"));
44 :
45 1 : assert_se(sd_bus_path_encode("xxxx", "waldo", &c) < 0);
46 1 : assert_se(sd_bus_path_encode("/foo/", "waldo", &c) < 0);
47 :
48 1 : assert_se(sd_bus_path_encode("/foo/bar", "", &c) >= 0 && streq(c, "/foo/bar/_"));
49 1 : assert_se(sd_bus_path_decode(c, "/foo/bar", &d) > 0 && streq(d, ""));
50 :
51 1 : assert_se(sd_bus_path_encode("/foo/bar", "foo.bar", &e) >= 0 && streq(e, "/foo/bar/foo_2ebar"));
52 1 : assert_se(sd_bus_path_decode(e, "/foo/bar", &f) > 0 && streq(f, "foo.bar"));
53 1 : }
54 :
55 1 : static void test_bus_path_encode_many(void) {
56 1 : _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
57 :
58 1 : assert_se(sd_bus_path_decode_many("/foo/bar", "/prefix/%", NULL) == 0);
59 1 : assert_se(sd_bus_path_decode_many("/prefix/bar", "/prefix/%bar", NULL) == 1);
60 1 : assert_se(sd_bus_path_decode_many("/foo/bar", "/prefix/%/suffix", NULL) == 0);
61 1 : assert_se(sd_bus_path_decode_many("/prefix/foobar/suffix", "/prefix/%/suffix", &a) == 1 && streq_ptr(a, "foobar"));
62 1 : assert_se(sd_bus_path_decode_many("/prefix/one_foo_two/mid/three_bar_four/suffix", "/prefix/one_%_two/mid/three_%_four/suffix", &b, &c) == 1 && streq_ptr(b, "foo") && streq_ptr(c, "bar"));
63 1 : assert_se(sd_bus_path_decode_many("/prefix/one_foo_two/mid/three_bar_four/suffix", "/prefix/one_%_two/mid/three_%_four/suffix", NULL, &d) == 1 && streq_ptr(d, "bar"));
64 :
65 1 : assert_se(sd_bus_path_decode_many("/foo/bar", "/foo/bar/%", NULL) == 0);
66 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/bar%", NULL) == 0);
67 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%/bar", NULL) == 0);
68 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%bar", NULL) == 0);
69 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/bar/suffix") == 1);
70 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%%/suffix", NULL, NULL) == 0); /* multiple '%' are treated verbatim */
71 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%/suffi", NULL) == 0);
72 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%/suffix", &e) == 1 && streq_ptr(e, "bar"));
73 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%/%", NULL, NULL) == 1);
74 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/%/%/%", NULL, NULL, NULL) == 1);
75 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "%/%/%", NULL, NULL, NULL) == 0);
76 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/%/%", NULL, NULL) == 0);
77 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/%/%/", NULL, NULL) == 0);
78 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/%/", NULL) == 0);
79 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/%", NULL) == 0);
80 1 : assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "%", NULL) == 0);
81 :
82 1 : assert_se(sd_bus_path_encode_many(&f, "/prefix/one_%_two/mid/three_%_four/suffix", "foo", "bar") >= 0 && streq_ptr(f, "/prefix/one_foo_two/mid/three_bar_four/suffix"));
83 1 : }
84 :
85 7 : static void test_bus_label_escape_one(const char *a, const char *b) {
86 7 : _cleanup_free_ char *t = NULL, *x = NULL, *y = NULL;
87 :
88 7 : assert_se(t = bus_label_escape(a));
89 7 : assert_se(streq(t, b));
90 :
91 7 : assert_se(x = bus_label_unescape(t));
92 7 : assert_se(streq(a, x));
93 :
94 7 : assert_se(y = bus_label_unescape(b));
95 7 : assert_se(streq(a, y));
96 7 : }
97 :
98 1 : static void test_bus_label_escape(void) {
99 1 : test_bus_label_escape_one("foo123bar", "foo123bar");
100 1 : test_bus_label_escape_one("foo.bar", "foo_2ebar");
101 1 : test_bus_label_escape_one("foo_2ebar", "foo_5f2ebar");
102 1 : test_bus_label_escape_one("", "_");
103 1 : test_bus_label_escape_one("_", "_5f");
104 1 : test_bus_label_escape_one("1", "_31");
105 1 : test_bus_label_escape_one(":1", "_3a1");
106 1 : }
107 :
108 1 : int main(int argc, char *argv[]) {
109 1 : _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *copy = NULL;
110 : int r, boolean;
111 : const char *x, *x2, *y, *z, *a, *b, *c, *d, *a_signature;
112 : uint8_t u, v;
113 1 : void *buffer = NULL;
114 : size_t sz;
115 1 : _cleanup_free_ char *h = NULL;
116 1 : const int32_t integer_array[] = { -1, -2, 0, 1, 2 }, *return_array;
117 : char *s;
118 1 : _cleanup_free_ char *first = NULL, *second = NULL, *third = NULL;
119 1 : _cleanup_fclose_ FILE *ms = NULL;
120 1 : size_t first_size = 0, second_size = 0, third_size = 0;
121 1 : _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
122 : double dbl;
123 : uint64_t u64;
124 :
125 1 : test_setup_logging(LOG_INFO);
126 :
127 1 : r = sd_bus_default_user(&bus);
128 1 : if (r < 0)
129 0 : r = sd_bus_default_system(&bus);
130 1 : if (r < 0)
131 0 : return log_tests_skipped("Failed to connect to bus");
132 :
133 1 : r = sd_bus_message_new_method_call(bus, &m, "foobar.waldo", "/", "foobar.waldo", "Piep");
134 1 : assert_se(r >= 0);
135 :
136 1 : r = sd_bus_message_append(m, "");
137 1 : assert_se(r >= 0);
138 :
139 1 : r = sd_bus_message_append(m, "s", "a string");
140 1 : assert_se(r >= 0);
141 :
142 1 : r = sd_bus_message_append(m, "s", NULL);
143 1 : assert_se(r >= 0);
144 :
145 1 : r = sd_bus_message_append(m, "asg", 2, "string #1", "string #2", "sba(tt)ss");
146 1 : assert_se(r >= 0);
147 :
148 1 : r = sd_bus_message_append(m, "sass", "foobar", 5, "foo", "bar", "waldo", "piep", "pap", "after");
149 1 : assert_se(r >= 0);
150 :
151 1 : r = sd_bus_message_append(m, "a{yv}", 2, 3, "s", "foo", 5, "s", "waldo");
152 1 : assert_se(r >= 0);
153 :
154 1 : r = sd_bus_message_append(m, "y(ty)y(yt)y", 8, 777ULL, 7, 9, 77, 7777ULL, 10);
155 1 : assert_se(r >= 0);
156 :
157 1 : r = sd_bus_message_append(m, "()");
158 1 : assert_se(r == -EINVAL);
159 :
160 1 : r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3");
161 1 : assert_se(r >= 0);
162 :
163 1 : r = sd_bus_message_open_container(m, 'a', "s");
164 1 : assert_se(r >= 0);
165 :
166 1 : r = sd_bus_message_append_basic(m, 's', "foobar");
167 1 : assert_se(r >= 0);
168 :
169 1 : r = sd_bus_message_append_basic(m, 's', "waldo");
170 1 : assert_se(r >= 0);
171 :
172 1 : r = sd_bus_message_close_container(m);
173 1 : assert_se(r >= 0);
174 :
175 1 : r = sd_bus_message_append_string_space(m, 5, &s);
176 1 : assert_se(r >= 0);
177 1 : strcpy(s, "hallo");
178 :
179 1 : r = sd_bus_message_append_array(m, 'i', integer_array, sizeof(integer_array));
180 1 : assert_se(r >= 0);
181 :
182 1 : r = sd_bus_message_append_array(m, 'u', NULL, 0);
183 1 : assert_se(r >= 0);
184 :
185 1 : r = sd_bus_message_append(m, "a(stdo)", 1, "foo", 815ULL, 47.0, "/");
186 1 : assert_se(r >= 0);
187 :
188 1 : r = sd_bus_message_seal(m, 4711, 0);
189 1 : assert_se(r >= 0);
190 :
191 1 : bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
192 :
193 1 : ms = open_memstream_unlocked(&first, &first_size);
194 1 : bus_message_dump(m, ms, 0);
195 1 : fflush(ms);
196 1 : assert_se(!ferror(ms));
197 :
198 1 : r = bus_message_get_blob(m, &buffer, &sz);
199 1 : assert_se(r >= 0);
200 :
201 1 : h = cescape_length(buffer, sz);
202 1 : assert_se(h);
203 1 : log_info("message size = %zu, contents =\n%s", sz, h);
204 :
205 : #if HAVE_GLIB
206 : /* Work-around for asan bug. See c8d980a3e962aba2ea3a4cedf75fa94890a6d746. */
207 : #if !HAS_FEATURE_ADDRESS_SANITIZER
208 : {
209 : GDBusMessage *g;
210 : char *p;
211 :
212 : #if !defined(GLIB_VERSION_2_36)
213 : g_type_init();
214 : #endif
215 :
216 1 : g = g_dbus_message_new_from_blob(buffer, sz, 0, NULL);
217 1 : p = g_dbus_message_print(g, 0);
218 1 : log_info("%s", p);
219 1 : g_free(p);
220 1 : g_object_unref(g);
221 : }
222 : #endif
223 : #endif
224 :
225 : #if HAVE_DBUS
226 : {
227 : DBusMessage *w;
228 : DBusError error;
229 :
230 1 : dbus_error_init(&error);
231 :
232 1 : w = dbus_message_demarshal(buffer, sz, &error);
233 1 : if (!w)
234 0 : log_error("%s", error.message);
235 : else
236 1 : dbus_message_unref(w);
237 :
238 1 : dbus_error_free(&error);
239 : }
240 : #endif
241 :
242 1 : m = sd_bus_message_unref(m);
243 :
244 1 : r = bus_message_from_malloc(bus, buffer, sz, NULL, 0, NULL, &m);
245 1 : assert_se(r >= 0);
246 :
247 1 : bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
248 :
249 1 : fclose(ms);
250 1 : ms = open_memstream_unlocked(&second, &second_size);
251 1 : bus_message_dump(m, ms, 0);
252 1 : fflush(ms);
253 1 : assert_se(!ferror(ms));
254 1 : assert_se(first_size == second_size);
255 1 : assert_se(memcmp(first, second, first_size) == 0);
256 :
257 1 : assert_se(sd_bus_message_rewind(m, true) >= 0);
258 :
259 1 : r = sd_bus_message_read(m, "ssasg", &x, &x2, 2, &y, &z, &a_signature);
260 1 : assert_se(r > 0);
261 1 : assert_se(streq(x, "a string"));
262 1 : assert_se(streq(x2, ""));
263 1 : assert_se(streq(y, "string #1"));
264 1 : assert_se(streq(z, "string #2"));
265 1 : assert_se(streq(a_signature, "sba(tt)ss"));
266 :
267 1 : r = sd_bus_message_read(m, "sass", &x, 5, &y, &z, &a, &b, &c, &d);
268 1 : assert_se(r > 0);
269 1 : assert_se(streq(x, "foobar"));
270 1 : assert_se(streq(y, "foo"));
271 1 : assert_se(streq(z, "bar"));
272 1 : assert_se(streq(a, "waldo"));
273 1 : assert_se(streq(b, "piep"));
274 1 : assert_se(streq(c, "pap"));
275 1 : assert_se(streq(d, "after"));
276 :
277 1 : r = sd_bus_message_read(m, "a{yv}", 2, &u, "s", &x, &v, "s", &y);
278 1 : assert_se(r > 0);
279 1 : assert_se(u == 3);
280 1 : assert_se(streq(x, "foo"));
281 1 : assert_se(v == 5);
282 1 : assert_se(streq(y, "waldo"));
283 :
284 1 : r = sd_bus_message_read(m, "y(ty)", &v, &u64, &u);
285 1 : assert_se(r > 0);
286 1 : assert_se(v == 8);
287 1 : assert_se(u64 == 777);
288 1 : assert_se(u == 7);
289 :
290 1 : r = sd_bus_message_read(m, "y(yt)", &v, &u, &u64);
291 1 : assert_se(r > 0);
292 1 : assert_se(v == 9);
293 1 : assert_se(u == 77);
294 1 : assert_se(u64 == 7777);
295 :
296 1 : r = sd_bus_message_read(m, "y", &v);
297 1 : assert_se(r > 0);
298 1 : assert_se(v == 10);
299 :
300 1 : r = sd_bus_message_read(m, "()");
301 1 : assert_se(r < 0);
302 :
303 1 : r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d);
304 1 : assert_se(r > 0);
305 1 : assert_se(boolean);
306 1 : assert_se(streq(x, "aaa"));
307 1 : assert_se(streq(y, "1"));
308 1 : assert_se(streq(a, "bbb"));
309 1 : assert_se(streq(b, "2"));
310 1 : assert_se(streq(c, "ccc"));
311 1 : assert_se(streq(d, "3"));
312 :
313 1 : assert_se(sd_bus_message_verify_type(m, 'a', "s") > 0);
314 :
315 1 : r = sd_bus_message_read(m, "as", 2, &x, &y);
316 1 : assert_se(r > 0);
317 1 : assert_se(streq(x, "foobar"));
318 1 : assert_se(streq(y, "waldo"));
319 :
320 1 : r = sd_bus_message_read_basic(m, 's', &s);
321 1 : assert_se(r > 0);
322 1 : assert_se(streq(s, "hallo"));
323 :
324 1 : r = sd_bus_message_read_array(m, 'i', (const void**) &return_array, &sz);
325 1 : assert_se(r > 0);
326 1 : assert_se(sz == sizeof(integer_array));
327 1 : assert_se(memcmp(integer_array, return_array, sz) == 0);
328 :
329 1 : r = sd_bus_message_read_array(m, 'u', (const void**) &return_array, &sz);
330 1 : assert_se(r > 0);
331 1 : assert_se(sz == 0);
332 :
333 1 : r = sd_bus_message_read(m, "a(stdo)", 1, &x, &u64, &dbl, &y);
334 1 : assert_se(r > 0);
335 1 : assert_se(streq(x, "foo"));
336 1 : assert_se(u64 == 815ULL);
337 1 : assert_se(fabs(dbl - 47.0) < 0.1);
338 1 : assert_se(streq(y, "/"));
339 :
340 1 : r = sd_bus_message_peek_type(m, NULL, NULL);
341 1 : assert_se(r == 0);
342 :
343 1 : r = sd_bus_message_new_method_call(bus, ©, "foobar.waldo", "/", "foobar.waldo", "Piep");
344 1 : assert_se(r >= 0);
345 :
346 1 : r = sd_bus_message_rewind(m, true);
347 1 : assert_se(r >= 0);
348 :
349 1 : r = sd_bus_message_copy(copy, m, true);
350 1 : assert_se(r >= 0);
351 :
352 1 : r = sd_bus_message_seal(copy, 4712, 0);
353 1 : assert_se(r >= 0);
354 :
355 1 : fclose(ms);
356 1 : ms = open_memstream_unlocked(&third, &third_size);
357 1 : bus_message_dump(copy, ms, 0);
358 1 : fflush(ms);
359 1 : assert_se(!ferror(ms));
360 :
361 1 : printf("<%.*s>\n", (int) first_size, first);
362 1 : printf("<%.*s>\n", (int) third_size, third);
363 :
364 1 : assert_se(first_size == third_size);
365 1 : assert_se(memcmp(first, third, third_size) == 0);
366 :
367 1 : r = sd_bus_message_rewind(m, true);
368 1 : assert_se(r >= 0);
369 :
370 1 : assert_se(sd_bus_message_verify_type(m, 's', NULL) > 0);
371 :
372 1 : r = sd_bus_message_skip(m, "ssasg");
373 1 : assert_se(r > 0);
374 :
375 1 : assert_se(sd_bus_message_verify_type(m, 's', NULL) > 0);
376 :
377 1 : r = sd_bus_message_skip(m, "sass");
378 1 : assert_se(r >= 0);
379 :
380 1 : assert_se(sd_bus_message_verify_type(m, 'a', "{yv}") > 0);
381 :
382 1 : r = sd_bus_message_skip(m, "a{yv}y(ty)y(yt)y");
383 1 : assert_se(r >= 0);
384 :
385 1 : assert_se(sd_bus_message_verify_type(m, 'b', NULL) > 0);
386 :
387 1 : r = sd_bus_message_read(m, "b", &boolean);
388 1 : assert_se(r > 0);
389 1 : assert_se(boolean);
390 :
391 1 : r = sd_bus_message_enter_container(m, 0, NULL);
392 1 : assert_se(r > 0);
393 :
394 1 : r = sd_bus_message_read(m, "(ss)", &x, &y);
395 1 : assert_se(r > 0);
396 :
397 1 : r = sd_bus_message_read(m, "(ss)", &a, &b);
398 1 : assert_se(r > 0);
399 :
400 1 : r = sd_bus_message_read(m, "(ss)", &c, &d);
401 1 : assert_se(r > 0);
402 :
403 1 : r = sd_bus_message_read(m, "(ss)", &x, &y);
404 1 : assert_se(r == 0);
405 :
406 1 : r = sd_bus_message_exit_container(m);
407 1 : assert_se(r >= 0);
408 :
409 1 : assert_se(streq(x, "aaa"));
410 1 : assert_se(streq(y, "1"));
411 1 : assert_se(streq(a, "bbb"));
412 1 : assert_se(streq(b, "2"));
413 1 : assert_se(streq(c, "ccc"));
414 1 : assert_se(streq(d, "3"));
415 :
416 1 : test_bus_label_escape();
417 1 : test_bus_path_encode();
418 1 : test_bus_path_encode_unique();
419 1 : test_bus_path_encode_many();
420 :
421 1 : return 0;
422 : }
|