1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
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>
9 #include <errno.h>
10 #include <stdio.h>
11 
12 #include "sd-bus-vtable.h"
13 
14 #ifndef __cplusplus
15 #  include "bus-objects.h"
16 #endif
17 
18 #include "test-vtable-data.h"
19 
20 #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
21 
22 static struct context c = {};
23 static int happy_finder_object = 0;
24 
happy_finder(sd_bus * bus,const char * path,const char * interface,void * userdata,void ** found,sd_bus_error * error)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 
test_vtable(void)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 
main(int argc,char ** argv)72 int main(int argc, char **argv) {
73         test_vtable();
74 
75         return 0;
76 }
77