]> git.proxmox.com Git - systemd.git/blame - src/libsystemd/sd-bus/test-bus-proxy.c
Imported Upstream version 229
[systemd.git] / src / libsystemd / sd-bus / test-bus-proxy.c
CommitLineData
7035cd9e
MP
1/***
2 This file is part of systemd.
3
4 Copyright 2015 David Herrmann <dh.herrmann@gmail.com>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <errno.h>
21#include <fcntl.h>
22#include <stdlib.h>
23
7035cd9e 24#include "sd-bus.h"
db2df898
MP
25
26#include "alloc-util.h"
27#include "bus-dump.h"
7035cd9e
MP
28#include "bus-kernel.h"
29#include "bus-util.h"
db2df898
MP
30#include "log.h"
31#include "util.h"
7035cd9e
MP
32
33typedef struct {
34 const char *sender;
35 int matched_acquired;
36} TestProxyMatch;
37
38static int test_proxy_acquired(sd_bus_message *m, void *userdata, sd_bus_error *error) {
39 TestProxyMatch *match = userdata;
40 const char *name;
41 int r;
42
43 r = sd_bus_message_read(m, "s", &name);
44 assert_se(r >= 0);
45
46 if (!streq_ptr(match->sender, name))
47 return 0;
48
49 ++match->matched_acquired;
50 return 1;
51}
52
53static void test_proxy_matched(void) {
4c89c718 54 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *a = NULL;
5fd56512 55 _cleanup_free_ char *matchstr = NULL;
7035cd9e 56 TestProxyMatch match = {};
5fd56512 57 const char *me;
7035cd9e
MP
58 int r;
59
60 /* open bus 'a' */
61
62 r = sd_bus_new(&a);
63 assert_se(r >= 0);
64
65 r = sd_bus_set_address(a, "unix:path=/var/run/dbus/system_bus_socket");
66 assert_se(r >= 0);
67
68 r = sd_bus_set_bus_client(a, true);
69 assert_se(r >= 0);
70
71 r = sd_bus_start(a);
72 assert_se(r >= 0);
73
5fd56512
MP
74 r = sd_bus_get_unique_name(a, &me);
75 assert_se(r >= 0);
76
77 matchstr = strjoin("type='signal',"
78 "member='NameAcquired',"
79 "destination='",
80 me,
81 "'",
82 NULL);
83 assert_se(matchstr);
84 r = sd_bus_add_match(a, NULL, matchstr, test_proxy_acquired, &match);
7035cd9e
MP
85 assert_se(r >= 0);
86
87 r = sd_bus_get_unique_name(a, &match.sender);
88 assert_se(r >= 0);
89
90 /* barrier to guarantee proxy/dbus-daemon handled the previous data */
91 r = sd_bus_call_method(a,
92 "org.freedesktop.DBus",
93 "/org/freedesktop/DBus",
94 "org.freedesktop.DBus",
95 "GetId",
96 NULL, NULL, NULL);
97 assert_se(r >= 0);
98
99 /* now we can be sure the Name* signals were sent */
100 do {
101 r = sd_bus_process(a, NULL);
102 } while (r > 0);
103 assert_se(r == 0);
104
105 assert_se(match.matched_acquired == 1);
106}
107
108int main(int argc, char **argv) {
109 if (access("/var/run/dbus/system_bus_socket", F_OK) < 0)
110 return EXIT_TEST_SKIP;
111
112 log_parse_environment();
113
114 test_proxy_matched();
115
116 return EXIT_SUCCESS;
117}