1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #include "sd-bus.h"
3
4 #include "main-func.h"
5 #include "tests.h"
6
test_ref_unref(void)7 static int test_ref_unref(void) {
8 sd_bus_message *m = NULL;
9 sd_bus *bus = NULL;
10 int r;
11
12 /* This test will result in a memory leak in <= v240, but not on v241. Hence to be really useful it
13 * should be run through a leak tracker such as valgrind. */
14
15 r = sd_bus_open_system(&bus);
16 if (r < 0)
17 return log_tests_skipped("Failed to connect to bus");
18
19 /* Create a message and enqueue it (this shouldn't send it though as the connection setup is not complete yet) */
20 assert_se(sd_bus_message_new_method_call(bus, &m, "foo.bar", "/foo", "quux.quux", "waldo") >= 0);
21 assert_se(sd_bus_send(bus, m, NULL) >= 0);
22
23 /* Let's now unref the message first and the bus second. */
24 m = sd_bus_message_unref(m);
25 bus = sd_bus_unref(bus);
26
27 /* We should have a memory leak now on <= v240. Let's do this again, but destroy in the opposite
28 * order. On v240 that too should be a leak. */
29
30 r = sd_bus_open_system(&bus);
31 if (r < 0)
32 return log_tests_skipped("Failed to connect to bus");
33
34 assert_se(sd_bus_message_new_method_call(bus, &m, "foo.bar", "/foo", "quux.quux", "waldo") >= 0);
35 assert_se(sd_bus_send(bus, m, NULL) >= 0);
36
37 /* Let's now unref things in the opposite order */
38 bus = sd_bus_unref(bus);
39 m = sd_bus_message_unref(m);
40
41 return 0;
42 }
43
run(int argc,char * argv[])44 static int run(int argc, char *argv[]) {
45 int r;
46
47 test_setup_logging(LOG_INFO);
48
49 r = test_ref_unref();
50 if (r < 0)
51 return r;
52
53 return 0;
54 }
55
56 DEFINE_MAIN_FUNCTION(run);
57