]> git.proxmox.com Git - systemd.git/blame - src/libsystemd/sd-bus/test-bus-cleanup.c
New upstream version 236
[systemd.git] / src / libsystemd / sd-bus / test-bus-cleanup.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
60f067b4
JS
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"
db2df898 24
60f067b4
JS
25#include "bus-internal.h"
26#include "bus-message.h"
db2df898 27#include "bus-util.h"
60f067b4
JS
28#include "refcnt.h"
29
30static void test_bus_new(void) {
4c89c718 31 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
60f067b4
JS
32
33 assert_se(sd_bus_new(&bus) == 0);
34 printf("after new: refcount %u\n", REFCNT_GET(bus->n_ref));
35}
36
5eef597e 37static int test_bus_open(void) {
4c89c718 38 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
5eef597e 39 int r;
60f067b4 40
f5e65279
MB
41 r = sd_bus_open_user(&bus);
42 if (IN_SET(r, -ECONNREFUSED, -ENOENT))
5eef597e
MP
43 return r;
44
45 assert_se(r >= 0);
60f067b4 46 printf("after open: refcount %u\n", REFCNT_GET(bus->n_ref));
5eef597e
MP
47
48 return 0;
60f067b4
JS
49}
50
51static void test_bus_new_method_call(void) {
52 sd_bus *bus = NULL;
4c89c718 53 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
60f067b4 54
f5e65279 55 assert_se(sd_bus_open_user(&bus) >= 0);
60f067b4
JS
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
4c89c718
MP
61 sd_bus_flush_close_unref(bus);
62 printf("after bus_flush_close_unref: refcount %u\n", m->n_ref);
60f067b4
JS
63}
64
65static void test_bus_new_signal(void) {
66 sd_bus *bus = NULL;
4c89c718 67 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
60f067b4 68
f5e65279 69 assert_se(sd_bus_open_user(&bus) >= 0);
60f067b4
JS
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
4c89c718
MP
75 sd_bus_flush_close_unref(bus);
76 printf("after bus_flush_close_unref: refcount %u\n", m->n_ref);
60f067b4
JS
77}
78
79int main(int argc, char **argv) {
5eef597e
MP
80 int r;
81
60f067b4
JS
82 log_parse_environment();
83 log_open();
84
85 test_bus_new();
5eef597e
MP
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
60f067b4
JS
92 test_bus_new_method_call();
93 test_bus_new_signal();
5eef597e
MP
94
95 return EXIT_SUCCESS;
60f067b4 96}