ff6046
From 8798fcef9052d2946637fa2d4376844fad6b5f8c Mon Sep 17 00:00:00 2001
ff6046
From: Filipe Brandenburger <filbranden@google.com>
ff6046
Date: Tue, 24 Jul 2018 20:15:55 -0700
ff6046
Subject: [PATCH] test-socket-util: Add tests for receive_fd_iov() and friends.
ff6046
ff6046
Test it when sending an FD without any contents, or an FD and some contents,
ff6046
or only contents and no FD (using a bare send().)
ff6046
ff6046
Also fix the previous test which forked but was missing an _exit() at the
ff6046
end of the child execution code.
ff6046
ff6046
(cherry picked from commit 8a3386ab4fea9c4efa9c72e7c149cf510a46f03e)
ff6046
ff6046
Resolves: #1683319
ff6046
---
ff6046
 src/test/test-socket-util.c | 215 ++++++++++++++++++++++++++++++++++++
ff6046
 1 file changed, 215 insertions(+)
ff6046
ff6046
diff --git a/src/test/test-socket-util.c b/src/test/test-socket-util.c
ff6046
index 588485d881..19c5395b92 100644
ff6046
--- a/src/test/test-socket-util.c
ff6046
+++ b/src/test/test-socket-util.c
ff6046
@@ -6,8 +6,11 @@
ff6046
 
ff6046
 #include "alloc-util.h"
ff6046
 #include "async.h"
ff6046
+#include "exit-status.h"
ff6046
 #include "fd-util.h"
ff6046
+#include "fileio.h"
ff6046
 #include "in-addr-util.h"
ff6046
+#include "io-util.h"
ff6046
 #include "log.h"
ff6046
 #include "macro.h"
ff6046
 #include "process-util.h"
ff6046
@@ -484,9 +487,215 @@ static void test_getpeercred_getpeergroups(void) {
ff6046
                 }
ff6046
 
ff6046
                 safe_close_pair(pair);
ff6046
+                _exit(EXIT_SUCCESS);
ff6046
         }
ff6046
 }
ff6046
 
ff6046
+static void test_passfd_read(void) {
ff6046
+        static const char file_contents[] = "test contents for passfd";
ff6046
+        _cleanup_close_pair_ int pair[2] = { -1, -1 };
ff6046
+        int r;
ff6046
+
ff6046
+        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
ff6046
+
ff6046
+        r = safe_fork("(passfd_read)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
ff6046
+        assert_se(r >= 0);
ff6046
+
ff6046
+        if (r == 0) {
ff6046
+                /* Child */
ff6046
+                char tmpfile[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
ff6046
+                _cleanup_close_ int tmpfd = -1;
ff6046
+
ff6046
+                pair[0] = safe_close(pair[0]);
ff6046
+
ff6046
+                tmpfd = mkostemp_safe(tmpfile);
ff6046
+                assert_se(tmpfd >= 0);
ff6046
+                assert_se(write(tmpfd, file_contents, strlen(file_contents)) == (ssize_t) strlen(file_contents));
ff6046
+                tmpfd = safe_close(tmpfd);
ff6046
+
ff6046
+                tmpfd = open(tmpfile, O_RDONLY);
ff6046
+                assert_se(tmpfd >= 0);
ff6046
+                assert_se(unlink(tmpfile) == 0);
ff6046
+
ff6046
+                assert_se(send_one_fd(pair[1], tmpfd, MSG_DONTWAIT) == 0);
ff6046
+                _exit(EXIT_SUCCESS);
ff6046
+        }
ff6046
+
ff6046
+        /* Parent */
ff6046
+        char buf[64];
ff6046
+        struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
ff6046
+        _cleanup_close_ int fd = -1;
ff6046
+
ff6046
+        pair[1] = safe_close(pair[1]);
ff6046
+
ff6046
+        assert_se(receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd) == 0);
ff6046
+
ff6046
+        assert_se(fd >= 0);
ff6046
+        r = read(fd, buf, sizeof(buf)-1);
ff6046
+        assert_se(r >= 0);
ff6046
+        buf[r] = 0;
ff6046
+        assert_se(streq(buf, file_contents));
ff6046
+}
ff6046
+
ff6046
+static void test_passfd_contents_read(void) {
ff6046
+        _cleanup_close_pair_ int pair[2] = { -1, -1 };
ff6046
+        static const char file_contents[] = "test contents in the file";
ff6046
+        static const char wire_contents[] = "test contents on the wire";
ff6046
+        int r;
ff6046
+
ff6046
+        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
ff6046
+
ff6046
+        r = safe_fork("(passfd_contents_read)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
ff6046
+        assert_se(r >= 0);
ff6046
+
ff6046
+        if (r == 0) {
ff6046
+                /* Child */
ff6046
+                struct iovec iov = IOVEC_INIT_STRING(wire_contents);
ff6046
+                char tmpfile[] = "/tmp/test-socket-util-passfd-contents-read-XXXXXX";
ff6046
+                _cleanup_close_ int tmpfd = -1;
ff6046
+
ff6046
+                pair[0] = safe_close(pair[0]);
ff6046
+
ff6046
+                tmpfd = mkostemp_safe(tmpfile);
ff6046
+                assert_se(tmpfd >= 0);
ff6046
+                assert_se(write(tmpfd, file_contents, strlen(file_contents)) == (ssize_t) strlen(file_contents));
ff6046
+                tmpfd = safe_close(tmpfd);
ff6046
+
ff6046
+                tmpfd = open(tmpfile, O_RDONLY);
ff6046
+                assert_se(tmpfd >= 0);
ff6046
+                assert_se(unlink(tmpfile) == 0);
ff6046
+
ff6046
+                assert_se(send_one_fd_iov(pair[1], tmpfd, &iov, 1, MSG_DONTWAIT) > 0);
ff6046
+                _exit(EXIT_SUCCESS);
ff6046
+        }
ff6046
+
ff6046
+        /* Parent */
ff6046
+        char buf[64];
ff6046
+        struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
ff6046
+        _cleanup_close_ int fd = -1;
ff6046
+        ssize_t k;
ff6046
+
ff6046
+        pair[1] = safe_close(pair[1]);
ff6046
+
ff6046
+        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd;;
ff6046
+        assert_se(k > 0);
ff6046
+        buf[k] = 0;
ff6046
+        assert_se(streq(buf, wire_contents));
ff6046
+
ff6046
+        assert_se(fd >= 0);
ff6046
+        r = read(fd, buf, sizeof(buf)-1);
ff6046
+        assert_se(r >= 0);
ff6046
+        buf[r] = 0;
ff6046
+        assert_se(streq(buf, file_contents));
ff6046
+}
ff6046
+
ff6046
+static void test_receive_nopassfd(void) {
ff6046
+        _cleanup_close_pair_ int pair[2] = { -1, -1 };
ff6046
+        static const char wire_contents[] = "no fd passed here";
ff6046
+        int r;
ff6046
+
ff6046
+        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
ff6046
+
ff6046
+        r = safe_fork("(receive_nopassfd)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
ff6046
+        assert_se(r >= 0);
ff6046
+
ff6046
+        if (r == 0) {
ff6046
+                /* Child */
ff6046
+                struct iovec iov = IOVEC_INIT_STRING(wire_contents);
ff6046
+
ff6046
+                pair[0] = safe_close(pair[0]);
ff6046
+
ff6046
+                assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) > 0);
ff6046
+                _exit(EXIT_SUCCESS);
ff6046
+        }
ff6046
+
ff6046
+        /* Parent */
ff6046
+        char buf[64];
ff6046
+        struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
ff6046
+        int fd = -999;
ff6046
+        ssize_t k;
ff6046
+
ff6046
+        pair[1] = safe_close(pair[1]);
ff6046
+
ff6046
+        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd;;
ff6046
+        assert_se(k > 0);
ff6046
+        buf[k] = 0;
ff6046
+        assert_se(streq(buf, wire_contents));
ff6046
+
ff6046
+        /* no fd passed here, confirm it was reset */
ff6046
+        assert_se(fd == -1);
ff6046
+}
ff6046
+
ff6046
+static void test_send_nodata_nofd(void) {
ff6046
+        _cleanup_close_pair_ int pair[2] = { -1, -1 };
ff6046
+        int r;
ff6046
+
ff6046
+        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
ff6046
+
ff6046
+        r = safe_fork("(send_nodata_nofd)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
ff6046
+        assert_se(r >= 0);
ff6046
+
ff6046
+        if (r == 0) {
ff6046
+                /* Child */
ff6046
+                pair[0] = safe_close(pair[0]);
ff6046
+
ff6046
+                assert_se(send_one_fd_iov(pair[1], -1, NULL, 0, MSG_DONTWAIT) == -EINVAL);
ff6046
+                _exit(EXIT_SUCCESS);
ff6046
+        }
ff6046
+
ff6046
+        /* Parent */
ff6046
+        char buf[64];
ff6046
+        struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
ff6046
+        int fd = -999;
ff6046
+        ssize_t k;
ff6046
+
ff6046
+        pair[1] = safe_close(pair[1]);
ff6046
+
ff6046
+        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd;;
ff6046
+        /* recvmsg() will return errno EAGAIN if nothing was sent */
ff6046
+        assert_se(k == -EAGAIN);
ff6046
+
ff6046
+        /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
ff6046
+        assert_se(fd == -999);
ff6046
+}
ff6046
+
ff6046
+static void test_send_emptydata(void) {
ff6046
+        _cleanup_close_pair_ int pair[2] = { -1, -1 };
ff6046
+        int r;
ff6046
+
ff6046
+        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
ff6046
+
ff6046
+        r = safe_fork("(send_emptydata)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
ff6046
+        assert_se(r >= 0);
ff6046
+
ff6046
+        if (r == 0) {
ff6046
+                /* Child */
ff6046
+                struct iovec iov = IOVEC_INIT_STRING("");  /* zero-length iov */
ff6046
+                assert_se(iov.iov_len == 0);
ff6046
+
ff6046
+                pair[0] = safe_close(pair[0]);
ff6046
+
ff6046
+                /* This will succeed, since iov is set. */
ff6046
+                assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) == 0);
ff6046
+                _exit(EXIT_SUCCESS);
ff6046
+        }
ff6046
+
ff6046
+        /* Parent */
ff6046
+        char buf[64];
ff6046
+        struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
ff6046
+        int fd = -999;
ff6046
+        ssize_t k;
ff6046
+
ff6046
+        pair[1] = safe_close(pair[1]);
ff6046
+
ff6046
+        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd;;
ff6046
+        /* receive_one_fd_iov() returns -EIO if an fd is not found and no data was returned. */
ff6046
+        assert_se(k == -EIO);
ff6046
+
ff6046
+        /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
ff6046
+        assert_se(fd == -999);
ff6046
+}
ff6046
+
ff6046
 int main(int argc, char *argv[]) {
ff6046
 
ff6046
         log_set_max_level(LOG_DEBUG);
ff6046
@@ -515,5 +724,11 @@ int main(int argc, char *argv[]) {
ff6046
 
ff6046
         test_getpeercred_getpeergroups();
ff6046
 
ff6046
+        test_passfd_read();
ff6046
+        test_passfd_contents_read();
ff6046
+        test_receive_nopassfd();
ff6046
+        test_send_nodata_nofd();
ff6046
+        test_send_emptydata();
ff6046
+
ff6046
         return 0;
ff6046
 }