]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
60f067b4 JS |
2 | |
3 | #include <stdio.h> | |
4 | ||
5 | #include "sd-bus.h" | |
db2df898 | 6 | |
60f067b4 JS |
7 | #include "bus-internal.h" |
8 | #include "bus-message.h" | |
db2df898 | 9 | #include "bus-util.h" |
6e866b33 MB |
10 | #include "tests.h" |
11 | ||
12 | static bool use_system_bus = false; | |
60f067b4 JS |
13 | |
14 | static void test_bus_new(void) { | |
4c89c718 | 15 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; |
60f067b4 JS |
16 | |
17 | assert_se(sd_bus_new(&bus) == 0); | |
bb4f798a | 18 | assert_se(bus->n_ref == 1); |
60f067b4 JS |
19 | } |
20 | ||
5eef597e | 21 | static int test_bus_open(void) { |
4c89c718 | 22 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
5eef597e | 23 | int r; |
60f067b4 | 24 | |
f5e65279 | 25 | r = sd_bus_open_user(&bus); |
6e866b33 MB |
26 | if (IN_SET(r, -ECONNREFUSED, -ENOENT)) { |
27 | r = sd_bus_open_system(&bus); | |
28 | if (IN_SET(r, -ECONNREFUSED, -ENOENT)) | |
29 | return r; | |
30 | use_system_bus = true; | |
31 | } | |
5eef597e MP |
32 | |
33 | assert_se(r >= 0); | |
bb4f798a | 34 | assert_se(bus->n_ref >= 1); /* we send a hello message when opening, so the count is above 1 */ |
5eef597e MP |
35 | |
36 | return 0; | |
60f067b4 JS |
37 | } |
38 | ||
39 | static void test_bus_new_method_call(void) { | |
40 | sd_bus *bus = NULL; | |
4c89c718 | 41 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
60f067b4 | 42 | |
6e866b33 | 43 | assert_se(use_system_bus ? sd_bus_open_system(&bus) >= 0 : sd_bus_open_user(&bus) >= 0); |
60f067b4 JS |
44 | |
45 | assert_se(sd_bus_message_new_method_call(bus, &m, "a.service.name", "/an/object/path", "an.interface.name", "AMethodName") >= 0); | |
46 | ||
bb4f798a MB |
47 | assert_se(m->n_ref == 1); /* We hold the only reference to the message */ |
48 | assert_se(bus->n_ref >= 2); | |
4c89c718 | 49 | sd_bus_flush_close_unref(bus); |
bb4f798a | 50 | assert_se(m->n_ref == 1); |
60f067b4 JS |
51 | } |
52 | ||
53 | static void test_bus_new_signal(void) { | |
54 | sd_bus *bus = NULL; | |
4c89c718 | 55 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
60f067b4 | 56 | |
6e866b33 | 57 | assert_se(use_system_bus ? sd_bus_open_system(&bus) >= 0 : sd_bus_open_user(&bus) >= 0); |
60f067b4 JS |
58 | |
59 | assert_se(sd_bus_message_new_signal(bus, &m, "/an/object/path", "an.interface.name", "Name") >= 0); | |
60 | ||
bb4f798a MB |
61 | assert_se(m->n_ref == 1); /* We hold the only reference to the message */ |
62 | assert_se(bus->n_ref >= 2); | |
4c89c718 | 63 | sd_bus_flush_close_unref(bus); |
bb4f798a | 64 | assert_se(m->n_ref == 1); |
60f067b4 JS |
65 | } |
66 | ||
67 | int main(int argc, char **argv) { | |
6e866b33 | 68 | test_setup_logging(LOG_INFO); |
60f067b4 JS |
69 | |
70 | test_bus_new(); | |
6e866b33 MB |
71 | |
72 | if (test_bus_open() < 0) | |
73 | return log_tests_skipped("Failed to connect to bus"); | |
5eef597e | 74 | |
60f067b4 JS |
75 | test_bus_new_method_call(); |
76 | test_bus_new_signal(); | |
5eef597e MP |
77 | |
78 | return EXIT_SUCCESS; | |
60f067b4 | 79 | } |