| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/* We use system assert.h here, because we don't want to keep macro.h and log.h C++ compatible */ |
| 4 |
#undef NDEBUG |
| 5 |
#include <assert.h> |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
#include "sd-bus.h" |
| 9 |
#include "sd-bus-vtable.h" |
| 10 |
|
| 11 |
#ifndef __cplusplus |
| 12 |
# include "alloc-util.h" |
| 13 |
# include "bus-objects.h" |
| 14 |
# include "log.h" |
| 15 |
#endif |
| 16 |
|
| 17 |
#include "test-vtable-data.h" |
| 18 |
|
| 19 |
#define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket" |
| 20 |
|
| 21 |
static struct context c = {}; |
| 22 |
static int happy_finder_object = 0; |
| 23 |
|
| 24 |
static int happy_finder(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *reterr_error) { |
| 25 |
assert(found); |
| 26 |
assert(userdata); |
| 27 |
assert(userdata == &c); |
| 28 |
|
| 29 |
#ifndef __cplusplus |
| 30 |
log_info("%s called", __func__); |
| 31 |
#endif |
| 32 |
|
| 33 |
happy_finder_object++; |
| 34 |
*found = &happy_finder_object; |
| 35 |
return 1; /* found */ |
| 36 |
} |
| 37 |
|
| 38 |
static void test_vtable(void) { |
| 39 |
sd_bus *bus = NULL; |
| 40 |
int r; |
| 41 |
|
| 42 |
assert(sd_bus_new(&bus) >= 0); |
| 43 |
|
| 44 |
assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable", test_vtable_2, &c) >= 0); |
| 45 |
assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable2", test_vtable_2, &c) >= 0); |
| 46 |
/* the cast on the line below is needed to test with the old version of the table */ |
| 47 |
assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable221", |
| 48 |
(const sd_bus_vtable *)vtable_format_221, &c) >= 0); |
| 49 |
|
| 50 |
assert(sd_bus_add_fallback_vtable(bus, NULL, "/fallback", "org.freedesktop.systemd.testVtable2", test_vtable_2, happy_finder, &c) >= 0); |
| 51 |
|
| 52 |
assert(sd_bus_set_address(bus, DEFAULT_BUS_PATH) >= 0); |
| 53 |
r = sd_bus_start(bus); |
| 54 |
assert(r == 0 || /* success */ |
| 55 |
r == -ENOENT /* dbus is inactive */ ); |
| 56 |
|
| 57 |
#ifndef __cplusplus |
| 58 |
_cleanup_free_ char *s, *s2; |
| 59 |
|
| 60 |
assert_se(introspect_path(bus, "/foo", NULL, false, true, NULL, &s, NULL) == 1); |
| 61 |
fputs(s, stdout); |
| 62 |
|
| 63 |
assert_se(introspect_path(bus, "/fallback", NULL, false, true, NULL, &s2, NULL) == 1); |
| 64 |
fputs(s2, stdout); |
| 65 |
|
| 66 |
assert_se(happy_finder_object == 1); |
| 67 |
#endif |
| 68 |
|
| 69 |
sd_bus_unref(bus); |
| 70 |
} |
| 71 |
|
| 72 |
int main(int argc, char **argv) { |
| 73 |
test_vtable(); |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |