test-sync: add test for cancelling a sync send with an eventfd
authorDaniel Mack <daniel@zonque.org>
Fri, 19 Dec 2014 18:27:18 +0000 (19:27 +0100)
committerDaniel Mack <daniel@zonque.org>
Fri, 19 Dec 2014 18:32:17 +0000 (19:32 +0100)
Fork a process, issue a blocking send with a cancelfd, and kill the
command by writing to the cancel fd.

Signed-off-by: Daniel Mack <daniel@zonque.org>
test/test-sync.c

index 311b163f8c3753feebbe726e10c9a9bd7a4e1649..a71c376fae379ab937990df706c598e710c3be08 100644 (file)
@@ -13,6 +13,7 @@
 #include <stdbool.h>
 #include <signal.h>
 #include <sys/wait.h>
+#include <sys/eventfd.h>
 
 #include "kdbus-test.h"
 #include "kdbus-util.h"
@@ -121,6 +122,49 @@ static int interrupt_sync(struct kdbus_conn *conn_src,
        return (status == EXIT_SUCCESS) ? TEST_OK : TEST_ERR;
 }
 
+static int cancel_sync(struct kdbus_conn *conn_src,
+                         struct kdbus_conn *conn_dst)
+{
+       pid_t pid;
+       int cancel_fd;
+       int ret, status;
+       uint64_t counter = 1;
+       struct kdbus_msg *msg = NULL;
+
+       cancel_fd = eventfd(0, 0);
+       ASSERT_RETURN_VAL(cancel_fd >= 0, cancel_fd);
+
+       cookie++;
+       pid = fork();
+       ASSERT_RETURN_VAL(pid >= 0, pid);
+
+       if (pid == 0) {
+               ret = kdbus_msg_send_sync(conn_dst, NULL, cookie,
+                                         KDBUS_MSG_EXPECT_REPLY,
+                                         100000000ULL, 0, conn_src->id,
+                                         cancel_fd);
+               ASSERT_EXIT(ret == -ECANCELED);
+
+               _exit(EXIT_SUCCESS);
+       }
+
+       ret = kdbus_msg_recv_poll(conn_src, 100, &msg, NULL);
+       ASSERT_RETURN(ret == 0 && msg->cookie == cookie);
+
+       kdbus_msg_free(msg);
+
+       ret = write(cancel_fd, &counter, sizeof(counter));
+       ASSERT_RETURN(ret == sizeof(counter));
+
+       ret = waitpid(pid, &status, 0);
+       ASSERT_RETURN_VAL(ret >= 0, ret);
+
+       if (WIFSIGNALED(status))
+               return TEST_ERR;
+
+       return (status == EXIT_SUCCESS) ? TEST_OK : TEST_ERR;
+}
+
 static void *run_thread_reply(void *data)
 {
        int ret;
@@ -173,6 +217,9 @@ int kdbus_test_sync_reply(struct kdbus_test_env *env)
        ret = interrupt_sync(conn_a, conn_b);
        ASSERT_RETURN(ret == 0);
 
+       ret = cancel_sync(conn_a, conn_b);
+       ASSERT_RETURN(ret == 0);
+
        kdbus_printf("-- closing bus connections\n");
 
        kdbus_conn_free(conn_a);