Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : : #include "sd-bus.h" 3 : : 4 : : #include "main-func.h" 5 : : #include "tests.h" 6 : : 7 : 4 : static int test_ref_unref(void) { 8 : 4 : sd_bus_message *m = NULL; 9 : 4 : 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 : 4 : r = sd_bus_open_system(&bus); 16 [ - + ]: 4 : if (r < 0) 17 : 0 : 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 [ - + ]: 4 : assert_se(sd_bus_message_new_method_call(bus, &m, "foo.bar", "/foo", "quux.quux", "waldo") >= 0); 21 [ - + ]: 4 : assert_se(sd_bus_send(bus, m, NULL) >= 0); 22 : : 23 : : /* Let's now unref the message first and the bus second. */ 24 : 4 : m = sd_bus_message_unref(m); 25 : 4 : 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 : 4 : r = sd_bus_open_system(&bus); 31 [ - + ]: 4 : if (r < 0) 32 : 0 : return log_tests_skipped("Failed to connect to bus"); 33 : : 34 [ - + ]: 4 : assert_se(sd_bus_message_new_method_call(bus, &m, "foo.bar", "/foo", "quux.quux", "waldo") >= 0); 35 [ - + ]: 4 : assert_se(sd_bus_send(bus, m, NULL) >= 0); 36 : : 37 : : /* Let's now unref things in the opposite order */ 38 : 4 : bus = sd_bus_unref(bus); 39 : 4 : m = sd_bus_message_unref(m); 40 : : 41 : 4 : return 0; 42 : : } 43 : : 44 : 4 : static int run(int argc, char *argv[]) { 45 : : int r; 46 : : 47 : 4 : test_setup_logging(LOG_INFO); 48 : : 49 : 4 : r = test_ref_unref(); 50 [ - + ]: 4 : if (r < 0) 51 : 0 : return r; 52 : : 53 : 4 : return 0; 54 : : } 55 : : 56 : 4 : DEFINE_MAIN_FUNCTION(run);