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