chiark / gitweb /
bus: split up overly long sd-bus.c into three files
[elogind.git] / src / libsystemd-bus / test-bus-objects.c
index f3571cb3c42635f1dff21c58a45d67a22b3f8513..1c23c2136ac0bf9900e5ab905d415fbd91fbaf05 100644 (file)
 #include "bus-message.h"
 
 /* Test:
- *
- *   sd_bus_emit_properties_changed()
  *
  *   Add in:
  *
- *   automatic properties
  *   node hierarchy updates during dispatching
  *   emit_interfaces_added/emit_interfaces_removed
  *
@@ -50,6 +47,8 @@ struct context {
         int fds[2];
         bool quit;
         char *something;
+        char *automatic_string_property;
+        uint32_t automatic_integer_property;
 };
 
 static int something_handler(sd_bus *bus, sd_bus_message *m, void *userdata) {
@@ -134,21 +133,35 @@ static int value_handler(sd_bus *bus, const char *path, const char *interface, c
 
         assert_se(PTR_TO_UINT(userdata) == 30);
 
+        return 1;
+}
+
+static int notify_test(sd_bus *bus, sd_bus_message *m, void *userdata) {
+        int r;
+
+        assert_se(sd_bus_emit_properties_changed(bus, m->path, "org.freedesktop.systemd.ValueTest", "Value", NULL) >= 0);
+
+        r = sd_bus_reply_method_return(bus, m, NULL);
+        assert_se(r >= 0);
 
         return 1;
 }
 
 static const sd_bus_vtable vtable[] = {
         SD_BUS_VTABLE_START(0),
-        SD_BUS_METHOD("AlterSomething", "s", "s", 0, something_handler),
-        SD_BUS_METHOD("Exit", "", "", 0, exit_handler),
+        SD_BUS_METHOD("AlterSomething", "s", "s", something_handler, 0),
+        SD_BUS_METHOD("Exit", "", "", exit_handler, 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, 0),
         SD_BUS_VTABLE_END
 };
 
 static const sd_bus_vtable vtable2[] = {
         SD_BUS_VTABLE_START(0),
-        SD_BUS_PROPERTY("Value", "s", value_handler, 10, 0),
+        SD_BUS_METHOD("NotifyTest", "", "", notify_test, 0),
+        SD_BUS_PROPERTY("Value", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_VTABLE_END
 };
 
@@ -226,6 +239,9 @@ static int client(struct context *c) {
         assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
         assert_se(sd_bus_start(bus) >= 0);
 
+        r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "NoOperation", &error, NULL, NULL);
+        assert_se(r >= 0);
+
         r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AlterSomething", &error, &reply, "s", "hallo");
         assert_se(r >= 0);
 
@@ -271,6 +287,16 @@ static int client(struct context *c) {
         sd_bus_message_unref(reply);
         reply = NULL;
 
+        r = sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AutomaticIntegerProperty", &error, "u", 815);
+        assert_se(r >= 0);
+
+        assert_se(c->automatic_integer_property == 815);
+
+        r = sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AutomaticStringProperty", &error, "s", "Du Dödel, Du!");
+        assert_se(r >= 0);
+
+        assert_se(streq(c->automatic_string_property, "Du Dödel, Du!"));
+
         r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, "");
         assert_se(r >= 0);
 
@@ -347,6 +373,18 @@ static int client(struct context *c) {
         sd_bus_message_unref(reply);
         reply = NULL;
 
+        r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.systemd.ValueTest", "NotifyTest", &error, NULL, "");
+        assert_se(r >= 0);
+
+        r = sd_bus_process(bus, &reply);
+        assert_se(r > 0);
+
+        assert_se(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.Properties", "PropertiesChanged"));
+        bus_message_dump(reply);
+
+        sd_bus_message_unref(reply);
+        reply = NULL;
+
         r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Exit", &error, NULL, "");
         assert_se(r >= 0);
 
@@ -363,6 +401,9 @@ int main(int argc, char *argv[]) {
 
         zero(c);
 
+        c.automatic_integer_property = 4711;
+        assert_se(c.automatic_string_property = strdup("dudeldu"));
+
         assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
 
         r = pthread_create(&s, NULL, server, &c);
@@ -382,6 +423,7 @@ int main(int argc, char *argv[]) {
                 return PTR_TO_INT(p);
 
         free(c.something);
+        free(c.automatic_string_property);
 
         return EXIT_SUCCESS;
 }