From 4b70aedc4a595feaad0c43b8f10dc6782ef0546c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 17 Jan 2019 19:45:12 +0100 Subject: [PATCH] test: add test for new sd-bus refcnt logic --- .../sd-bus/test-bus-queue-ref-cycle.c | 44 +++++++++++++++++++ src/test/meson.build | 4 ++ 2 files changed, 48 insertions(+) create mode 100644 src/libsystemd/sd-bus/test-bus-queue-ref-cycle.c diff --git a/src/libsystemd/sd-bus/test-bus-queue-ref-cycle.c b/src/libsystemd/sd-bus/test-bus-queue-ref-cycle.c new file mode 100644 index 00000000000..70901c30b0e --- /dev/null +++ b/src/libsystemd/sd-bus/test-bus-queue-ref-cycle.c @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include "main-func.h" +#include "sd-bus.h" +#include "tests.h" + +static int run(int argc, char *argv[]) { + sd_bus_message *m = NULL; + sd_bus *bus = NULL; + int r; + + /* This test will result in a memory leak in <= v240, but not on v241. Hence to be really useful it + * should be run through a leak tracker such as valgrind. */ + + r = sd_bus_open_system(&bus); + if (r < 0) + return log_tests_skipped("Failed to connect to bus"); + + /* Create a message and enqueue it (this shouldn't send it though as the connection setup is not complete yet) */ + assert_se(sd_bus_message_new_method_call(bus, &m, "foo.bar", "/foo", "quux.quux", "waldo") >= 0); + assert_se(sd_bus_send(bus, m, NULL) >= 0); + + /* Let's now unref the message first and the bus second. */ + m = sd_bus_message_unref(m); + bus = sd_bus_unref(bus); + + /* We should have a memory leak now on <= v240. Let's do this again, but destory in the opposite + * order. On v240 that too should be a leak. */ + + r = sd_bus_open_system(&bus); + if (r < 0) + return log_tests_skipped("Failed to connect to bus"); + + assert_se(sd_bus_message_new_method_call(bus, &m, "foo.bar", "/foo", "quux.quux", "waldo") >= 0); + assert_se(sd_bus_send(bus, m, NULL) >= 0); + + /* Let's now unref things in the opposite order */ + bus = sd_bus_unref(bus); + m = sd_bus_message_unref(m); + + return 0; +} + +DEFINE_MAIN_FUNCTION(run); diff --git a/src/test/meson.build b/src/test/meson.build index 86d8a30afa4..c53b9653f95 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -867,6 +867,10 @@ tests += [ [], [threads]], + [['src/libsystemd/sd-bus/test-bus-queue-ref-cycle.c'], + [], + [threads]], + [['src/libsystemd/sd-bus/test-bus-watch-bind.c'], [], [threads], '', 'timeout=120'], -- 2.47.3