]> git.proxmox.com Git - systemd.git/blame - src/libsystemd/sd-bus/test-bus-proxy.c
Imported Upstream version 228
[systemd.git] / src / libsystemd / sd-bus / test-bus-proxy.c
CommitLineData
7035cd9e
MP
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
7035cd9e 26#include "sd-bus.h"
db2df898
MP
27
28#include "alloc-util.h"
29#include "bus-dump.h"
7035cd9e
MP
30#include "bus-kernel.h"
31#include "bus-util.h"
db2df898
MP
32#include "log.h"
33#include "util.h"
7035cd9e
MP
34
35typedef struct {
36 const char *sender;
37 int matched_acquired;
38} TestProxyMatch;
39
40static int test_proxy_acquired(sd_bus_message *m, void *userdata, sd_bus_error *error) {
41 TestProxyMatch *match = userdata;
42 const char *name;
43 int r;
44
45 r = sd_bus_message_read(m, "s", &name);
46 assert_se(r >= 0);
47
48 if (!streq_ptr(match->sender, name))
49 return 0;
50
51 ++match->matched_acquired;
52 return 1;
53}
54
55static void test_proxy_matched(void) {
56 _cleanup_bus_flush_close_unref_ sd_bus *a = NULL;
5fd56512 57 _cleanup_free_ char *matchstr = NULL;
7035cd9e 58 TestProxyMatch match = {};
5fd56512 59 const char *me;
7035cd9e
MP
60 int r;
61
62 /* open bus 'a' */
63
64 r = sd_bus_new(&a);
65 assert_se(r >= 0);
66
67 r = sd_bus_set_address(a, "unix:path=/var/run/dbus/system_bus_socket");
68 assert_se(r >= 0);
69
70 r = sd_bus_set_bus_client(a, true);
71 assert_se(r >= 0);
72
73 r = sd_bus_start(a);
74 assert_se(r >= 0);
75
5fd56512
MP
76 r = sd_bus_get_unique_name(a, &me);
77 assert_se(r >= 0);
78
79 matchstr = strjoin("type='signal',"
80 "member='NameAcquired',"
81 "destination='",
82 me,
83 "'",
84 NULL);
85 assert_se(matchstr);
86 r = sd_bus_add_match(a, NULL, matchstr, test_proxy_acquired, &match);
7035cd9e
MP
87 assert_se(r >= 0);
88
89 r = sd_bus_get_unique_name(a, &match.sender);
90 assert_se(r >= 0);
91
92 /* barrier to guarantee proxy/dbus-daemon handled the previous data */
93 r = sd_bus_call_method(a,
94 "org.freedesktop.DBus",
95 "/org/freedesktop/DBus",
96 "org.freedesktop.DBus",
97 "GetId",
98 NULL, NULL, NULL);
99 assert_se(r >= 0);
100
101 /* now we can be sure the Name* signals were sent */
102 do {
103 r = sd_bus_process(a, NULL);
104 } while (r > 0);
105 assert_se(r == 0);
106
107 assert_se(match.matched_acquired == 1);
108}
109
110int main(int argc, char **argv) {
111 if (access("/var/run/dbus/system_bus_socket", F_OK) < 0)
112 return EXIT_TEST_SKIP;
113
114 log_parse_environment();
115
116 test_proxy_matched();
117
118 return EXIT_SUCCESS;
119}