chiark / gitweb /
Support system_bus_socket to be found in /var/run/dbus as well as /run/dbus.
[elogind.git] / src / libelogind / sd-bus / test-bus-vtable.c
1 #include <stdbool.h>
2 #include <stddef.h>
3
4 /* We use system assert.h here, because we don't want to keep macro.h and log.h C++ compatible */
5 #undef NDEBUG
6 #include <assert.h>
7 #include <errno.h>
8
9 #include "sd-bus-vtable.h"
10
11 #if 0 /// elogind should support both /run/dbus & /var/run/dbus (per Linux FHS)
12 #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
13 #else
14 #if VARRUN_IS_SYMLINK
15   #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
16 #else
17   #define DEFAULT_BUS_PATH "unix:path=/var/run/dbus/system_bus_socket"
18 #endif // VARRUN_IS_SYMLINK
19 #endif // 0
20
21 struct context {
22         bool quit;
23         char *something;
24         char *automatic_string_property;
25         uint32_t automatic_integer_property;
26 };
27
28 static int handler(sd_bus_message *m, void *userdata, sd_bus_error *error) {
29         return 1;
30 }
31
32 static int value_handler(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
33         return 1;
34 }
35
36 static int get_handler(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
37         return 1;
38 }
39
40 static int set_handler(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *value, void *userdata, sd_bus_error *error) {
41         return 1;
42 }
43
44 static const sd_bus_vtable vtable[] = {
45         SD_BUS_VTABLE_START(0),
46         SD_BUS_METHOD("AlterSomething", "s", "s", handler, 0),
47         SD_BUS_METHOD("Exit", "", "", handler, 0),
48         SD_BUS_METHOD_WITH_OFFSET("AlterSomething2", "s", "s", handler, 200, 0),
49         SD_BUS_METHOD_WITH_OFFSET("Exit2", "", "", handler, 200, 0),
50         SD_BUS_PROPERTY("Value", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
51         SD_BUS_PROPERTY("Value2", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
52         SD_BUS_PROPERTY("Value3", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_CONST),
53         SD_BUS_PROPERTY("Value4", "s", value_handler, 10, 0),
54         SD_BUS_PROPERTY("AnExplicitProperty", "s", NULL, offsetof(struct context, something),
55                         SD_BUS_VTABLE_PROPERTY_EXPLICIT|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
56         SD_BUS_WRITABLE_PROPERTY("Something", "s", get_handler, set_handler, 0, 0),
57         SD_BUS_WRITABLE_PROPERTY("AutomaticStringProperty", "s", NULL, NULL,
58                                  offsetof(struct context, automatic_string_property), 0),
59         SD_BUS_WRITABLE_PROPERTY("AutomaticIntegerProperty", "u", NULL, NULL,
60                                  offsetof(struct context, automatic_integer_property), 0),
61         SD_BUS_METHOD("NoOperation", NULL, NULL, NULL, 0),
62         SD_BUS_SIGNAL("DummySignal", "b", 0),
63         SD_BUS_SIGNAL("DummySignal2", "so", 0),
64         SD_BUS_VTABLE_END
65 };
66
67 static void test_vtable(void) {
68         sd_bus *bus = NULL;
69         struct context c = {};
70         int r;
71
72         assert(sd_bus_new(&bus) >= 0);
73
74         assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable", vtable, &c) >= 0);
75         assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable2", vtable, &c) >= 0);
76
77         assert(sd_bus_set_address(bus, DEFAULT_BUS_PATH) >= 0);
78         r = sd_bus_start(bus);
79         assert(r == 0 ||     /* success */
80                r == -ENOENT  /* dbus is inactive */ );
81
82         sd_bus_unref(bus);
83 }
84
85 int main(int argc, char **argv) {
86         test_vtable();
87
88         return 0;
89 }