From 65669272fdb3ba4a6f3bc08dd2f77dbad71ae46a Mon Sep 17 00:00:00 2001 From: Djalal Harouni Date: Mon, 27 Oct 2014 16:06:44 +0100 Subject: [PATCH] test-fd: add more passfd+close tests to fdpassing Signed-off-by: Djalal Harouni --- test/test-fd.c | 52 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/test/test-fd.c b/test/test-fd.c index fb38047..48336ea 100644 --- a/test/test-fd.c +++ b/test/test-fd.c @@ -355,8 +355,19 @@ int kdbus_test_fd_passing(struct kdbus_test_env *env) ret = send_fds(conn_src, KDBUS_DST_ID_BROADCAST, fds, 1); ASSERT_RETURN(ret == -ENOTUNIQ); + /* Try to broadcast memfd. This must succeed. */ + ret = send_memfds(conn_src, KDBUS_DST_ID_BROADCAST, (int *)&memfd, 1); + ASSERT_RETURN(ret == 0); + + /* Open code this loop */ +loop_send_fds: + + /* + * Send the read end of the pipe and close it. + */ ret = send_fds(conn_src, conn_dst->id, fds, 1); ASSERT_RETURN(ret == 0); + close(fds[0]); ret = kdbus_msg_recv(conn_dst, &msg, NULL); ASSERT_RETURN(ret == 0); @@ -369,24 +380,47 @@ int kdbus_test_fd_passing(struct kdbus_test_env *env) ASSERT_RETURN(nfds == 1); i = read(item->fds[0], tmp, sizeof(tmp)); - ASSERT_RETURN(i == sizeof(tmp)); - ASSERT_RETURN(memcmp(tmp, str, sizeof(tmp)) == 0); - + if (i != 0) { + ASSERT_RETURN(i == sizeof(tmp)); + ASSERT_RETURN(memcmp(tmp, str, sizeof(tmp)) == 0); + + /* Write EOF */ + close(fds[1]); + + /* + * Resend the read end of the pipe, + * the receiver still holds a reference + * to it... + */ + goto loop_send_fds; + } + + /* Got EOF */ + + /* + * Close the last reference to the read end + * of the pipe, other references are + * automatically closed just after send. + */ close(item->fds[0]); } } - kdbus_msg_free(msg); + /* + * Try to resend the read end of the pipe. Must fail with + * -EBADF since both the sender and receiver closed their + * references to it. We assume the above since sender and + * receiver are on the same process. + */ + ret = send_fds(conn_src, conn_dst->id, fds, 1); + ASSERT_RETURN(ret == -EBADF); - /* Try to broadcast memfd. This must succeed. */ - ret = send_memfds(conn_src, KDBUS_DST_ID_BROADCAST, (int *)&memfd, 1); - ASSERT_RETURN(ret == 0); + /* Then we clear out received any data... */ + kdbus_msg_free(msg); ret = kdbus_send_multiple_fds(conn_src, conn_dst); ASSERT_RETURN(ret == 0); - close(fds[0]); - close(fds[1]); close(sock_pair[0]); close(sock_pair[1]); close(memfd); -- 2.34.1