]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/test-bus-proxy.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / libsystemd / sd-bus / test-bus-proxy.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2015 David Herrmann <dh.herrmann@gmail.com>
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 <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25
26 #include "sd-bus.h"
27
28 #include "bus-dump.h"
29 #include "bus-kernel.h"
30 #include "bus-util.h"
31 #include "log.h"
32 #include "util.h"
33
34 typedef struct {
35 const char *sender;
36 int matched_acquired;
37 } TestProxyMatch;
38
39 static int test_proxy_acquired(sd_bus_message *m, void *userdata, sd_bus_error *error) {
40 TestProxyMatch *match = userdata;
41 const char *name;
42 int r;
43
44 r = sd_bus_message_read(m, "s", &name);
45 assert_se(r >= 0);
46
47 if (!streq_ptr(match->sender, name))
48 return 0;
49
50 ++match->matched_acquired;
51 return 1;
52 }
53
54 static void test_proxy_matched(void) {
55 _cleanup_bus_flush_close_unref_ sd_bus *a = NULL;
56 _cleanup_free_ char *matchstr = NULL;
57 TestProxyMatch match = {};
58 const char *me;
59 int r;
60
61 /* open bus 'a' */
62
63 r = sd_bus_new(&a);
64 assert_se(r >= 0);
65
66 r = sd_bus_set_address(a, "unix:path=/var/run/dbus/system_bus_socket");
67 assert_se(r >= 0);
68
69 r = sd_bus_set_bus_client(a, true);
70 assert_se(r >= 0);
71
72 r = sd_bus_start(a);
73 assert_se(r >= 0);
74
75 r = sd_bus_get_unique_name(a, &me);
76 assert_se(r >= 0);
77
78 matchstr = strjoin("type='signal',"
79 "member='NameAcquired',"
80 "destination='",
81 me,
82 "'",
83 NULL);
84 assert_se(matchstr);
85 r = sd_bus_add_match(a, NULL, matchstr, test_proxy_acquired, &match);
86 assert_se(r >= 0);
87
88 r = sd_bus_get_unique_name(a, &match.sender);
89 assert_se(r >= 0);
90
91 /* barrier to guarantee proxy/dbus-daemon handled the previous data */
92 r = sd_bus_call_method(a,
93 "org.freedesktop.DBus",
94 "/org/freedesktop/DBus",
95 "org.freedesktop.DBus",
96 "GetId",
97 NULL, NULL, NULL);
98 assert_se(r >= 0);
99
100 /* now we can be sure the Name* signals were sent */
101 do {
102 r = sd_bus_process(a, NULL);
103 } while (r > 0);
104 assert_se(r == 0);
105
106 assert_se(match.matched_acquired == 1);
107 }
108
109 int main(int argc, char **argv) {
110 if (access("/var/run/dbus/system_bus_socket", F_OK) < 0)
111 return EXIT_TEST_SKIP;
112
113 log_parse_environment();
114
115 test_proxy_matched();
116
117 return EXIT_SUCCESS;
118 }