summaryrefslogtreecommitdiff
path: root/src/libsystemd/sd-bus/test-bus-proxy.c
blob: 45d0a5ffce6acdaba64889755b3da5983c9c5887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/***
  This file is part of systemd.

  Copyright 2015 David Herrmann <dh.herrmann@gmail.com>

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>

#include "sd-bus.h"

#include "alloc-util.h"
#include "bus-dump.h"
#include "bus-kernel.h"
#include "bus-util.h"
#include "log.h"
#include "util.h"

typedef struct {
        const char *sender;
        int matched_acquired;
} TestProxyMatch;

static int test_proxy_acquired(sd_bus_message *m, void *userdata, sd_bus_error *error) {
        TestProxyMatch *match = userdata;
        const char *name;
        int r;

        r = sd_bus_message_read(m, "s", &name);
        assert_se(r >= 0);

        if (!streq_ptr(match->sender, name))
                return 0;

        ++match->matched_acquired;
        return 1;
}

static void test_proxy_matched(void) {
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *a = NULL;
        _cleanup_free_ char *matchstr = NULL;
        TestProxyMatch match = {};
        const char *me;
        int r;

        /* open bus 'a' */

        r = sd_bus_new(&a);
        assert_se(r >= 0);

        r = sd_bus_set_address(a, "unix:path=/var/run/dbus/system_bus_socket");
        assert_se(r >= 0);

        r = sd_bus_set_bus_client(a, true);
        assert_se(r >= 0);

        r = sd_bus_start(a);
        assert_se(r >= 0);

        r = sd_bus_get_unique_name(a, &me);
        assert_se(r >= 0);

        matchstr = strjoin("type='signal',"
                           "member='NameAcquired',"
                           "destination='",
                           me,
                           "'",
                           NULL);
        assert_se(matchstr);
        r = sd_bus_add_match(a, NULL, matchstr, test_proxy_acquired, &match);
        assert_se(r >= 0);

        r = sd_bus_get_unique_name(a, &match.sender);
        assert_se(r >= 0);

        /* barrier to guarantee proxy/dbus-daemon handled the previous data  */
        r = sd_bus_call_method(a,
                               "org.freedesktop.DBus",
                               "/org/freedesktop/DBus",
                               "org.freedesktop.DBus",
                               "GetId",
                               NULL, NULL, NULL);
        assert_se(r >= 0);

        /* now we can be sure the Name* signals were sent */
        do {
                r = sd_bus_process(a, NULL);
        } while (r > 0);
        assert_se(r == 0);

        assert_se(match.matched_acquired == 1);
}

int main(int argc, char **argv) {
        if (access("/var/run/dbus/system_bus_socket", F_OK) < 0)
                return EXIT_TEST_SKIP;

        log_parse_environment();

        test_proxy_matched();

        return EXIT_SUCCESS;
}