]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/test-bus-cleanup.c
d5601fc57b5e3d15c7d577ffd8afaea38d23749c
[thirdparty/systemd.git] / src / libsystemd / sd-bus / test-bus-cleanup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <stdio.h>
22
23 #include "sd-bus.h"
24
25 #include "bus-internal.h"
26 #include "bus-message.h"
27 #include "bus-util.h"
28 #include "refcnt.h"
29
30 static void test_bus_new(void) {
31 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
32
33 assert_se(sd_bus_new(&bus) == 0);
34 printf("after new: refcount %u\n", REFCNT_GET(bus->n_ref));
35 }
36
37 static int test_bus_open(void) {
38 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
39 int r;
40
41 r = sd_bus_open_user(&bus);
42 if (IN_SET(r, -ECONNREFUSED, -ENOENT))
43 return r;
44
45 assert_se(r >= 0);
46 printf("after open: refcount %u\n", REFCNT_GET(bus->n_ref));
47
48 return 0;
49 }
50
51 static void test_bus_new_method_call(void) {
52 sd_bus *bus = NULL;
53 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
54
55 assert_se(sd_bus_open_user(&bus) >= 0);
56
57 assert_se(sd_bus_message_new_method_call(bus, &m, "a.service.name", "/an/object/path", "an.interface.name", "AMethodName") >= 0);
58
59 printf("after message_new_method_call: refcount %u\n", REFCNT_GET(bus->n_ref));
60
61 sd_bus_flush_close_unref(bus);
62 printf("after bus_flush_close_unref: refcount %u\n", m->n_ref);
63 }
64
65 static void test_bus_new_signal(void) {
66 sd_bus *bus = NULL;
67 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
68
69 assert_se(sd_bus_open_user(&bus) >= 0);
70
71 assert_se(sd_bus_message_new_signal(bus, &m, "/an/object/path", "an.interface.name", "Name") >= 0);
72
73 printf("after message_new_signal: refcount %u\n", REFCNT_GET(bus->n_ref));
74
75 sd_bus_flush_close_unref(bus);
76 printf("after bus_flush_close_unref: refcount %u\n", m->n_ref);
77 }
78
79 int main(int argc, char **argv) {
80 int r;
81
82 log_parse_environment();
83 log_open();
84
85 test_bus_new();
86 r = test_bus_open();
87 if (r < 0) {
88 log_info("Failed to connect to bus, skipping tests.");
89 return EXIT_TEST_SKIP;
90 }
91
92 test_bus_new_method_call();
93 test_bus_new_signal();
94
95 return EXIT_SUCCESS;
96 }