]>
Commit | Line | Data |
---|---|---|
24924cc9 ZJS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include "bus-util.h" | |
4 | #include "log.h" | |
6d7c4033 | 5 | #include "tests.h" |
24924cc9 | 6 | |
869881a6 ZJS |
7 | static int callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
8 | return 1; | |
9 | } | |
10 | ||
11 | static void destroy_callback(void *userdata) { | |
12 | int *n_called = userdata; | |
13 | ||
14 | (*n_called) ++; | |
15 | } | |
16 | ||
17 | static void test_destroy_callback(void) { | |
18 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
19 | sd_bus_slot *slot = NULL; | |
20 | sd_bus_destroy_t t; | |
21 | ||
22 | int r, n_called = 0; | |
23 | ||
24 | log_info("/* %s */", __func__); | |
25 | ||
26 | r = bus_open_system_watch_bind_with_description(&bus, "test-bus"); | |
27 | if (r < 0) { | |
28 | log_error_errno(r, "Failed to connect to bus: %m"); | |
29 | return; | |
30 | } | |
31 | ||
32 | r = sd_bus_request_name_async(bus, &slot, "org.freedesktop.systemd.test-bus-util", 0, callback, &n_called); | |
33 | assert(r == 1); | |
34 | ||
35 | assert_se(sd_bus_slot_get_destroy_callback(slot, NULL) == 0); | |
36 | assert_se(sd_bus_slot_get_destroy_callback(slot, &t) == 0); | |
37 | ||
38 | assert_se(sd_bus_slot_set_destroy_callback(slot, destroy_callback) == 0); | |
39 | assert_se(sd_bus_slot_get_destroy_callback(slot, NULL) == 1); | |
40 | assert_se(sd_bus_slot_get_destroy_callback(slot, &t) == 1); | |
41 | assert_se(t == destroy_callback); | |
42 | ||
43 | /* Force cleanup so we can look at n_called */ | |
44 | assert(n_called == 0); | |
45 | sd_bus_slot_unref(slot); | |
46 | assert(n_called == 1); | |
47 | } | |
48 | ||
24924cc9 | 49 | int main(int argc, char **argv) { |
6d7c4033 | 50 | test_setup_logging(LOG_DEBUG); |
24924cc9 | 51 | |
869881a6 | 52 | test_destroy_callback(); |
24924cc9 ZJS |
53 | |
54 | return 0; | |
55 | } |