test-bus-cleanup.c revision d4d00020d6ad855d65d31020fefa5003e1bb477f
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering/***
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering This file is part of systemd.
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering systemd is free software; you can redistribute it and/or modify it
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering under the terms of the GNU Lesser General Public License as published by
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering (at your option) any later version.
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering systemd is distributed in the hope that it will be useful, but
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering Lesser General Public License for more details.
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering You should have received a copy of the GNU Lesser General Public License
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering***/
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering#include <stdio.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "sd-bus.h"
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering#include "bus-internal.h"
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering#include "bus-message.h"
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering#include "refcnt.h"
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poetteringstatic void test_bus_new(void) {
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering _cleanup_bus_unref_ sd_bus *bus = NULL;
82659fd7571bda0f3dce9755b89a23c411d53ddaLennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering assert_se(sd_bus_new(&bus) == 0);
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering printf("after new: refcount %u\n", REFCNT_GET(bus->n_ref));
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering}
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poetteringstatic int test_bus_open(void) {
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering _cleanup_bus_unref_ sd_bus *bus = NULL;
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering int r;
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering r = sd_bus_open_system(&bus);
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering if (r == -ECONNREFUSED || r == -ENOENT)
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering return r;
82659fd7571bda0f3dce9755b89a23c411d53ddaLennart Poettering
82659fd7571bda0f3dce9755b89a23c411d53ddaLennart Poettering assert_se(r >= 0);
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering printf("after open: refcount %u\n", REFCNT_GET(bus->n_ref));
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
82659fd7571bda0f3dce9755b89a23c411d53ddaLennart Poettering return 0;
82659fd7571bda0f3dce9755b89a23c411d53ddaLennart Poettering}
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poetteringstatic void test_bus_new_method_call(void) {
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering sd_bus *bus = NULL;
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
58ea275a68cd242ad60161bcb7582614d1d89f13Lennart Poettering assert_se(sd_bus_open_system(&bus) >= 0);
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering assert_se(sd_bus_message_new_method_call(bus, &m, "a.service.name", "/an/object/path", "an.interface.name", "AMethodName") >= 0);
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering printf("after message_new_method_call: refcount %u\n", REFCNT_GET(bus->n_ref));
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering sd_bus_unref(bus);
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering printf("after bus_unref: refcount %u\n", m->n_ref);
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering}
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poetteringstatic void test_bus_new_signal(void) {
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering sd_bus *bus = NULL;
4819ff0358b6317c195fd4b1768e03d09c871070Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
assert_se(sd_bus_open_system(&bus) >= 0);
assert_se(sd_bus_message_new_signal(bus, &m, "/an/object/path", "an.interface.name", "Name") >= 0);
printf("after message_new_signal: refcount %u\n", REFCNT_GET(bus->n_ref));
sd_bus_unref(bus);
printf("after bus_unref: refcount %u\n", m->n_ref);
}
int main(int argc, char **argv) {
int r;
log_parse_environment();
log_open();
test_bus_new();
r = test_bus_open();
if (r < 0) {
log_info("Failed to connect to bus, skipping tests.");
return EXIT_TEST_SKIP;
}
test_bus_new_method_call();
test_bus_new_signal();
return EXIT_SUCCESS;
}