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