tests,meson: add test-bus-vtable, compiled as C and C++
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 13 May 2017 17:23:28 +0000 (13:23 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 13 May 2017 19:50:44 +0000 (15:50 -0400)
This test is mostly a compilation test that checks that various defines in
sd-bus-vtable.h are valid C++. The code is executed, but the results are not
checked (apart from sd-bus functions not returning an error). test-bus-objects
contains pretty extensive tests for this functionality.

The C++ version is only added to meson, since it's simpler there.
Because of the .cc extension, meson will compile the executable with c++.

This test is necessary to properly check the macros in sd-bus-vtable.h. Just
running the headers through g++ is not enough, because the macros are not
exercised.

Follow-up for #5941.

.gitignore
Makefile.am
meson.build
src/libsystemd/sd-bus/test-bus-vtable-cc.cc [new symlink]
src/libsystemd/sd-bus/test-bus-vtable.c [new file with mode: 0644]
src/test/meson.build

index 7cbf0a9..3864249 100644 (file)
 /test-bus-server
 /test-bus-signature
 /test-bus-track
+/test-bus-vtable
+/test-bus-vtable-cc
 /test-bus-zero-copy
 /test-calendarspec
 /test-cap-list
index 771efa4..3c04260 100644 (file)
@@ -3461,6 +3461,7 @@ tests += \
        test-bus-zero-copy \
        test-bus-introspect \
        test-bus-objects \
+       test-bus-vtable \
        test-bus-error \
        test-bus-creds \
        test-bus-gvariant \
@@ -3530,6 +3531,12 @@ test_bus_objects_SOURCES = \
 test_bus_objects_LDADD = \
        libsystemd-shared.la
 
+test_bus_vtable_SOURCES = \
+       src/libsystemd/sd-bus/test-bus-vtable.c
+
+test_bus_vtable_LDADD = \
+       libsystemd-shared.la
+
 test_bus_error_SOURCES = \
        src/libsystemd/sd-bus/test-bus-error.c
 
index bd05757..0f90da1 100644 (file)
@@ -224,6 +224,12 @@ cc = meson.get_compiler('c')
 pkgconfig = import('pkgconfig')
 check_compilation_sh = find_program('tools/meson-check-compilation.sh')
 
+cxx = find_program('c++', required : false)
+if cxx.found()
+        #  Used only for tests
+        add_languages('cpp')
+endif
+
 foreach arg : ['-Wundef',
                '-Wlogical-op',
                '-Wmissing-include-dirs',
diff --git a/src/libsystemd/sd-bus/test-bus-vtable-cc.cc b/src/libsystemd/sd-bus/test-bus-vtable-cc.cc
new file mode 120000 (symlink)
index 0000000..abee398
--- /dev/null
@@ -0,0 +1 @@
+test-bus-vtable.c
\ No newline at end of file
diff --git a/src/libsystemd/sd-bus/test-bus-vtable.c b/src/libsystemd/sd-bus/test-bus-vtable.c
new file mode 100644 (file)
index 0000000..2d894cc
--- /dev/null
@@ -0,0 +1,67 @@
+#include <stdbool.h>
+#include <stddef.h>
+
+/* We use system assert.h here, because we don't want to keep macro.h and log.h C++ compatible */
+#undef NDEBUG
+#include <assert.h>
+
+#include "sd-bus-vtable.h"
+
+#define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
+
+struct context {
+        bool quit;
+        char *something;
+        char *automatic_string_property;
+        uint32_t automatic_integer_property;
+};
+
+static int handler(sd_bus_message *m, void *userdata, sd_bus_error *error) {
+        return 1;
+}
+
+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) {
+        return 1;
+}
+
+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) {
+        return 1;
+}
+
+static const sd_bus_vtable vtable[] = {
+        SD_BUS_VTABLE_START(0),
+        SD_BUS_METHOD("AlterSomething", "s", "s", handler, 0),
+        SD_BUS_METHOD("Exit", "", "", handler, 0),
+        SD_BUS_METHOD_WITH_OFFSET("AlterSomething2", "s", "s", handler, 200, 0),
+        SD_BUS_METHOD_WITH_OFFSET("Exit2", "", "", handler, 200, 0),
+        SD_BUS_WRITABLE_PROPERTY("Something", "s", get_handler, set_handler, 0, 0),
+        SD_BUS_WRITABLE_PROPERTY("AutomaticStringProperty", "s", NULL, NULL,
+                                 offsetof(struct context, automatic_string_property), 0),
+        SD_BUS_WRITABLE_PROPERTY("AutomaticIntegerProperty", "u", NULL, NULL,
+                                 offsetof(struct context, automatic_integer_property), 0),
+        SD_BUS_METHOD("NoOperation", NULL, NULL, NULL, 0),
+        SD_BUS_SIGNAL("DummySignal", "b", 0),
+        SD_BUS_SIGNAL("DummySignal2", "so", 0),
+        SD_BUS_VTABLE_END
+};
+
+static void test_vtable(void) {
+        sd_bus *bus = NULL;
+        struct context c = {};
+
+        assert(sd_bus_new(&bus) >= 0);
+
+        assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable", vtable, &c) >= 0);
+        assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable2", vtable, &c) >= 0);
+
+        assert(sd_bus_set_address(bus, DEFAULT_BUS_PATH) >= 0);
+        assert(sd_bus_start(bus) >= 0);
+
+        sd_bus_unref(bus);
+}
+
+int main(int argc, char **argv) {
+        test_vtable();
+
+        return 0;
+}
index 4ae1210..05d0d1d 100644 (file)
@@ -734,6 +734,10 @@ tests += [
          [],
          [threads]],
 
+        [['src/libsystemd/sd-bus/test-bus-vtable.c'],
+         [],
+         []],
+
         [['src/libsystemd/sd-bus/test-bus-gvariant.c'],
          [],
          [libglib,
@@ -790,6 +794,14 @@ tests += [
          '', 'manual'],
 ]
 
+if cxx.found()
+        tests += [
+                [['src/libsystemd/sd-bus/test-bus-vtable-cc.cc'],
+                 [],
+                 []]
+        ]
+endif
+
 ############################################################
 
 tests += [