ryantimwilson / rpms / systemd

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