]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
d603324b | 2 | |
94e2523b ZJS |
3 | #include <stdbool.h> |
4 | #include <stddef.h> | |
5 | ||
6 | /* We use system assert.h here, because we don't want to keep macro.h and log.h C++ compatible */ | |
7 | #undef NDEBUG | |
8 | #include <assert.h> | |
f0023756 | 9 | #include <errno.h> |
d603324b | 10 | #include <stdio.h> |
94e2523b ZJS |
11 | |
12 | #include "sd-bus-vtable.h" | |
13 | ||
d603324b ZJS |
14 | #ifndef __cplusplus |
15 | # include "bus-objects.h" | |
16 | #endif | |
0f771be9 | 17 | |
d603324b | 18 | #include "test-vtable-data.h" |
94e2523b | 19 | |
d603324b | 20 | #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket" |
856ad2a8 | 21 | |
11791cde ZJS |
22 | static struct context c = {}; |
23 | static int happy_finder_object = 0; | |
24 | ||
25 | static int happy_finder(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { | |
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 | ||
94e2523b ZJS |
38 | static void test_vtable(void) { |
39 | sd_bus *bus = NULL; | |
f0023756 | 40 | int r; |
94e2523b ZJS |
41 | |
42 | assert(sd_bus_new(&bus) >= 0); | |
43 | ||
d603324b ZJS |
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); | |
856ad2a8 | 46 | /* the cast on the line below is needed to test with the old version of the table */ |
d603324b ZJS |
47 | assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable221", |
48 | (const sd_bus_vtable *)vtable_format_221, &c) >= 0); | |
94e2523b | 49 | |
11791cde ZJS |
50 | assert(sd_bus_add_fallback_vtable(bus, NULL, "/fallback", "org.freedesktop.systemd.testVtable2", test_vtable_2, happy_finder, &c) >= 0); |
51 | ||
94e2523b | 52 | assert(sd_bus_set_address(bus, DEFAULT_BUS_PATH) >= 0); |
f0023756 ZJS |
53 | r = sd_bus_start(bus); |
54 | assert(r == 0 || /* success */ | |
55 | r == -ENOENT /* dbus is inactive */ ); | |
94e2523b | 56 | |
d603324b | 57 | #ifndef __cplusplus |
11791cde | 58 | _cleanup_free_ char *s, *s2; |
d603324b ZJS |
59 | |
60 | assert_se(introspect_path(bus, "/foo", NULL, false, true, NULL, &s, NULL) == 1); | |
61 | fputs(s, stdout); | |
11791cde ZJS |
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); | |
d603324b ZJS |
67 | #endif |
68 | ||
94e2523b ZJS |
69 | sd_bus_unref(bus); |
70 | } | |
71 | ||
72 | int main(int argc, char **argv) { | |
73 | test_vtable(); | |
74 | ||
75 | return 0; | |
76 | } |