LCOV - code coverage report
Current view: top level - libsystemd/sd-bus - test-bus-queue-ref-cycle.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 22 25 88.0 %
Date: 2019-08-22 15:41:25 Functions: 3 3 100.0 %

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

Generated by: LCOV version 1.14