test: add test for sync message ping-pong
authorDaniel Mack <zonque@gmail.com>
Fri, 17 Jan 2014 22:35:49 +0000 (23:35 +0100)
committerDaniel Mack <zonque@gmail.com>
Fri, 17 Jan 2014 22:35:49 +0000 (23:35 +0100)
.gitignore
test/Makefile
test/kdbus-util.c
test/test-kdbus-sync.c [new file with mode: 0644]

index 18413eaf9549cd1f929acb546dcdadadc0ba3c2a..fc49417c013c0f3e2b873fcfb1c24835a1e01d55 100644 (file)
@@ -17,3 +17,4 @@ test/test-kdbus-activator
 test/test-kdbus-chat
 test/test-kdbus-timeout
 test/test-kdbus-prio
+test/test-kdbus-sync
index d668b5ccb322f12e776d23d4e00cc46bbd2ecf87..4a99ae11a2f56f66f78b0b85ae70e90e7bce2a9c 100644 (file)
@@ -6,6 +6,7 @@ CFLAGS          += -std=gnu99 -Wall -Wextra -g \
                   -Wcast-align \
                   -Wsign-compare \
                   -Wno-missing-field-initializers
+LDFLAGS                := -lpthread
 TEST_COMMON    := kdbus-enum.o kdbus-util.o
 CC             := $(CROSS_COMPILE)gcc
 
@@ -18,6 +19,7 @@ TESTS= \
        test-kdbus-monitor \
        test-kdbus-chat \
        test-kdbus-timeout \
+       test-kdbus-sync \
        test-kdbus-prio
 
 all: $(TESTS)
@@ -28,7 +30,7 @@ all: $(TESTS)
 
 test-%: $(TEST_COMMON) test-%.o
        @echo '  TARGET_LD $@'
-       @$(CC) $(CFLAGS) $^ -o $@
+       @$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
 
 clean::
        rm -f *.o $(TESTS)
index 8979635d0931bf27e06c73c6694db73f4ef07bc9..2f375857798f66d574bd3b89fbdcea3c03e0f39f 100644 (file)
@@ -221,6 +221,22 @@ int msg_send(const struct conn *conn,
 
        if (memfd >= 0)
                close(memfd);
+
+       if (flags & KDBUS_MSG_FLAGS_SYNC_REPLY) {
+               struct kdbus_msg *reply;
+
+               printf("SYNC REPLY @offset %llu:\n", msg->offset_reply);
+               reply = (struct kdbus_msg *)(conn->buf + msg->offset_reply);
+               msg_dump(conn, reply);
+
+               ret = ioctl(conn->fd, KDBUS_CMD_FREE, &msg->offset_reply);
+               if (ret < 0) {
+                       ret = -errno;
+                       fprintf(stderr, "error free message: %d (%m)\n", ret);
+                       return ret;
+               }
+       }
+
        free(msg);
 
        return 0;
diff --git a/test/test-kdbus-sync.c b/test/test-kdbus-sync.c
new file mode 100644 (file)
index 0000000..9a95217
--- /dev/null
@@ -0,0 +1,117 @@
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <errno.h>
+#include <assert.h>
+#include <poll.h>
+#include <sys/ioctl.h>
+#include <pthread.h>
+
+#include "kdbus-util.h"
+#include "kdbus-enum.h"
+
+static struct conn *conn_a, *conn_b;
+static unsigned int cookie = 0xdeadbeef;
+
+static void *run_thread(void *data)
+{
+       struct pollfd fd;
+       int ret;
+
+       fd.fd = conn_a->fd;
+       fd.events = POLLIN | POLLPRI | POLLHUP;
+       fd.revents = 0;
+
+       ret = poll(&fd, 1, 3000);
+       if (ret <= 0)
+               return NULL;
+
+       if (fd.revents & POLLIN) {
+               printf("Thread received message, sending reply ...\n");
+               msg_recv(conn_a);
+               msg_send(conn_a, NULL, 0, 0, cookie, 0, conn_b->id);
+       }
+
+       return NULL;
+}
+
+int main(int argc, char *argv[])
+{
+       struct {
+               struct kdbus_cmd_make head;
+
+               /* bloom size item */
+               struct {
+                       uint64_t size;
+                       uint64_t type;
+                       uint64_t bloom_size;
+               } bs;
+
+               /* name item */
+               uint64_t n_size;
+               uint64_t n_type;
+               char name[64];
+       } bus_make;
+       struct kdbus_msg *msg;
+       pthread_t thread;
+       int fdc, ret;
+       char *bus;
+
+       printf("-- opening /dev/" KBUILD_MODNAME "/control\n");
+       fdc = open("/dev/" KBUILD_MODNAME "/control", O_RDWR|O_CLOEXEC);
+       if (fdc < 0) {
+               fprintf(stderr, "--- error %d (%m)\n", fdc);
+               return EXIT_FAILURE;
+       }
+
+       memset(&bus_make, 0, sizeof(bus_make));
+       bus_make.head.flags = KDBUS_MAKE_POLICY_OPEN;
+       bus_make.bs.size = sizeof(bus_make.bs);
+       bus_make.bs.type = KDBUS_ITEM_BLOOM_SIZE;
+       bus_make.bs.bloom_size = 64;
+
+       snprintf(bus_make.name, sizeof(bus_make.name), "%u-testbus", getuid());
+       bus_make.n_type = KDBUS_ITEM_MAKE_NAME;
+       bus_make.n_size = KDBUS_ITEM_HEADER_SIZE + strlen(bus_make.name) + 1;
+
+       bus_make.head.size = sizeof(struct kdbus_cmd_make) +
+                            sizeof(bus_make.bs) +
+                            bus_make.n_size;
+
+       printf("-- creating bus '%s'\n", bus_make.name);
+       ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
+       if (ret) {
+               fprintf(stderr, "--- error %d (%m)\n", ret);
+               return EXIT_FAILURE;
+       }
+
+       if (asprintf(&bus, "/dev/" KBUILD_MODNAME "/%s/bus", bus_make.name) < 0)
+               return EXIT_FAILURE;
+
+       conn_a = connect_to_bus(bus, 0);
+       conn_b = connect_to_bus(bus, 0);
+       if (!conn_a || !conn_b)
+               return EXIT_FAILURE;
+
+       pthread_create(&thread, NULL, run_thread, NULL);
+
+       msg_send(conn_b, NULL, cookie, KDBUS_MSG_FLAGS_EXPECT_REPLY | KDBUS_MSG_FLAGS_SYNC_REPLY,
+                5000000000ULL, 0, conn_a->id);
+
+       printf("-- closing bus connections\n");
+       close(conn_a->fd);
+       close(conn_b->fd);
+       free(conn_a);
+       free(conn_b);
+
+       printf("-- closing bus master\n");
+       close(fdc);
+       free(bus);
+
+       return EXIT_SUCCESS;
+}