chiark / gitweb /
test-bus-util: add a simple test for bus_request_name_async_may_reload_dbus()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 4 Jun 2018 13:15:17 +0000 (15:15 +0200)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
This shows a minor memleak:
==1883== 24 bytes in 1 blocks are definitely lost in loss record 1 of 1
==1883==    at 0x4C2DBAB: malloc (vg_replace_malloc.c:299)
==1883==    by 0x4E9D385: malloc_multiply (alloc-util.h:69)
==1883==    by 0x4EA2959: bus_request_name_async_may_reload_dbus (bus-util.c:1841)
==1883==    by ...

The exchange of messages is truncated at two different points: once right
after the first callback is requested, and the second time after the full
sequence has run (usually resulting in an error because of policy).

src/test/meson.build
src/test/test-bus-util.c [new file with mode: 0644]

index 8d694b3796db6ea22796cc2a4bd4792365702a51..b48705b4128b02784eda5255312e8e05d3e07f9e 100644 (file)
@@ -711,6 +711,10 @@ tests += [
           'src/core/umount.h'],
          [],
          [libmount]],
+
+        [['src/test/test-bus-util.c'],
+         [],
+         []],
 ]
 
 ############################################################
@@ -959,7 +963,6 @@ tests += [
 #           'src/libsystemd-network/dhcp-protocol.h',
 #           'src/libsystemd-network/dhcp-internal.h'],
 #          [libshared,
-#           libelogind_network],
 #           libsystemd_network],
 #          []],
 # 
@@ -974,13 +977,11 @@ tests += [
 #           'src/libsystemd-network/dhcp-internal.h',
 #           'src/systemd/sd-dhcp-client.h'],
 #          [libshared,
-#           libelogind_network],
 #           libsystemd_network],
 #          []],
 # 
 #         [['src/libelogind-network/test-dhcp-server.c'],
 #          [libshared,
-#           libelogind_network],
 #           libsystemd_network],
 #          []],
 # 
@@ -988,14 +989,12 @@ tests += [
 #           'src/libsystemd-network/arp-util.h',
 #           'src/systemd/sd-ipv4ll.h'],
 #          [libshared,
-#           libelogind_network],
 #           libsystemd_network],
 #          []],
 # 
 #         [['src/libelogind-network/test-ipv4ll-manual.c',
 #           'src/systemd/sd-ipv4ll.h'],
 #          [libshared,
-#           libelogind_network],
 #           libsystemd_network],
 #          [],
 #          '', 'manual'],
@@ -1014,7 +1013,6 @@ tests += [
 #           'src/systemd/sd-dhcp6-client.h',
 #           'src/systemd/sd-ndisc.h'],
 #          [libshared,
-#           libelogind_network],
 #           libsystemd_network],
 #          []],
 # 
@@ -1031,7 +1029,6 @@ tests += [
 #           'src/libsystemd-network/dhcp6-internal.h',
 #           'src/systemd/sd-dhcp6-client.h'],
 #          [libshared,
-#           libelogind_network],
 #           libsystemd_network],
 #          []],
 # 
diff --git a/src/test/test-bus-util.c b/src/test/test-bus-util.c
new file mode 100644 (file)
index 0000000..608052b
--- /dev/null
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+//#include "bus-util.h"
+//#include "log.h"
+
+static void test_name_async(unsigned n_messages) {
+        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
+        int r;
+        unsigned i;
+
+        log_info("/* %s (%u) */", __func__, n_messages);
+
+        r = bus_open_system_watch_bind_with_description(&bus, "test-bus");
+        if (r < 0) {
+                log_error_errno(r, "Failed to connect to bus: %m");
+                return;
+        }
+
+        r = bus_request_name_async_may_reload_dbus(bus, NULL, "org.freedesktop.elogind.test-bus-util", 0, NULL);
+        if (r < 0) {
+                log_error_errno(r, "Failed to request name: %m");
+                return;
+        }
+
+        for (i = 0; i < n_messages; i++) {
+                r = sd_bus_process(bus, NULL);
+                log_debug("stage %u: sd_bus_process returned %d", i, r);
+                if (r < 0) {
+                        log_notice_errno(r, "Processing failed: %m");
+                        return;
+                }
+
+                if (r > 0 && i + 1 < n_messages)
+                        (void) sd_bus_wait(bus, USEC_PER_SEC / 3);
+        }
+}
+
+int main(int argc, char **argv) {
+        log_set_max_level(LOG_DEBUG);
+        log_parse_environment();
+        log_open();
+
+        test_name_async(0);
+        test_name_async(20);
+
+        return 0;
+}