LCOV - code coverage report
Current view: top level - libsystemd/sd-bus - test-bus-cleanup.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 39 44 88.6 %
Date: 2019-08-23 13:36:53 Functions: 5 5 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 19 42 45.2 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <stdio.h>
       4                 :            : 
       5                 :            : #include "sd-bus.h"
       6                 :            : 
       7                 :            : #include "bus-internal.h"
       8                 :            : #include "bus-message.h"
       9                 :            : #include "bus-util.h"
      10                 :            : #include "tests.h"
      11                 :            : 
      12                 :            : static bool use_system_bus = false;
      13                 :            : 
      14                 :          4 : static void test_bus_new(void) {
      15                 :          4 :         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
      16                 :            : 
      17         [ -  + ]:          4 :         assert_se(sd_bus_new(&bus) == 0);
      18         [ -  + ]:          4 :         assert_se(bus->n_ref == 1);
      19                 :          4 : }
      20                 :            : 
      21                 :          4 : static int test_bus_open(void) {
      22                 :          4 :         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
      23                 :            :         int r;
      24                 :            : 
      25                 :          4 :         r = sd_bus_open_user(&bus);
      26   [ -  +  -  + ]:          4 :         if (IN_SET(r, -ECONNREFUSED, -ENOENT)) {
      27                 :          0 :                 r = sd_bus_open_system(&bus);
      28   [ #  #  #  # ]:          0 :                 if (IN_SET(r, -ECONNREFUSED, -ENOENT))
      29                 :          0 :                         return r;
      30                 :          0 :                 use_system_bus = true;
      31                 :            :         }
      32                 :            : 
      33         [ -  + ]:          4 :         assert_se(r >= 0);
      34         [ -  + ]:          4 :         assert_se(bus->n_ref >= 1); /* we send a hello message when opening, so the count is above 1 */
      35                 :            : 
      36                 :          4 :         return 0;
      37                 :            : }
      38                 :            : 
      39                 :          4 : static void test_bus_new_method_call(void) {
      40                 :          4 :         sd_bus *bus = NULL;
      41                 :          4 :         _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
      42                 :            : 
      43   [ -  +  -  + ]:          4 :         assert_se(use_system_bus ? sd_bus_open_system(&bus) >= 0 : sd_bus_open_user(&bus) >= 0);
      44                 :            : 
      45         [ -  + ]:          4 :         assert_se(sd_bus_message_new_method_call(bus, &m, "a.service.name", "/an/object/path", "an.interface.name", "AMethodName") >= 0);
      46                 :            : 
      47         [ -  + ]:          4 :         assert_se(m->n_ref == 1); /* We hold the only reference to the message */
      48         [ -  + ]:          4 :         assert_se(bus->n_ref >= 2);
      49                 :          4 :         sd_bus_flush_close_unref(bus);
      50         [ -  + ]:          4 :         assert_se(m->n_ref == 1);
      51                 :          4 : }
      52                 :            : 
      53                 :          4 : static void test_bus_new_signal(void) {
      54                 :          4 :         sd_bus *bus = NULL;
      55                 :          4 :         _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
      56                 :            : 
      57   [ -  +  -  + ]:          4 :         assert_se(use_system_bus ? sd_bus_open_system(&bus) >= 0 : sd_bus_open_user(&bus) >= 0);
      58                 :            : 
      59         [ -  + ]:          4 :         assert_se(sd_bus_message_new_signal(bus, &m, "/an/object/path", "an.interface.name", "Name") >= 0);
      60                 :            : 
      61         [ -  + ]:          4 :         assert_se(m->n_ref == 1); /* We hold the only reference to the message */
      62         [ -  + ]:          4 :         assert_se(bus->n_ref >= 2);
      63                 :          4 :         sd_bus_flush_close_unref(bus);
      64         [ -  + ]:          4 :         assert_se(m->n_ref == 1);
      65                 :          4 : }
      66                 :            : 
      67                 :          4 : int main(int argc, char **argv) {
      68                 :          4 :         test_setup_logging(LOG_INFO);
      69                 :            : 
      70                 :          4 :         test_bus_new();
      71                 :            : 
      72         [ -  + ]:          4 :         if (test_bus_open() < 0)
      73                 :          0 :                 return log_tests_skipped("Failed to connect to bus");
      74                 :            : 
      75                 :          4 :         test_bus_new_method_call();
      76                 :          4 :         test_bus_new_signal();
      77                 :            : 
      78                 :          4 :         return EXIT_SUCCESS;
      79                 :            : }

Generated by: LCOV version 1.14