]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: extend the unit test test-notify-recv.c
authorZIHCO <chizobajames21@gmail.com>
Sat, 5 Apr 2025 03:54:10 +0000 (04:54 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 6 Apr 2025 20:28:17 +0000 (05:28 +0900)
src/test/test-notify-recv.c

index be236a3b9f55e20c710380ee4bad54df915a0e0e..80efe0c0f093a1b5b90f040f252cdd4f4cd809d1 100644 (file)
@@ -5,10 +5,12 @@
 #include "sd-daemon.h"
 
 #include "event-util.h"
+#include "fd-util.h"
 #include "notify-recv.h"
 #include "path-util.h"
 #include "process-util.h"
 #include "rm-rf.h"
+#include "socket-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
 
@@ -26,12 +28,18 @@ static void context_done(Context *c) {
 static int on_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
         Context *c = ASSERT_PTR(userdata);
 
+        _cleanup_(fdset_free_asyncp) FDSet *fds = NULL;
         _cleanup_(pidref_done) PidRef sender = PIDREF_NULL;
         _cleanup_strv_free_ char **l = NULL;
-        ASSERT_OK(notify_recv_strv(fd, &l, /* ret_ucred= */ NULL, &sender));
+        struct ucred ucred;
+        ASSERT_OK(notify_recv_with_fds_strv(fd, &l, &ucred, &sender, &fds));
 
         ASSERT_TRUE(pidref_equal(&c->pidref, &sender));
 
+        ASSERT_EQ(ucred.gid, getgid());
+        ASSERT_EQ(ucred.pid, c->pidref.pid);
+        ASSERT_EQ(ucred.uid, getuid());
+
         _cleanup_free_ char *joined = strv_join(l, ", ");
         ASSERT_NOT_NULL(joined);
         log_info("Received message: %s", joined);
@@ -40,10 +48,20 @@ static int on_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata)
                 ASSERT_STREQ(l[0], "FIRST_MESSAGE=1");
                 ASSERT_NULL(l[1]);
                 ASSERT_EQ(c->data, 0u);
+                ASSERT_NULL(fds);
         } else if (strv_contains(l, "SECOND_MESSAGE=1")) {
                 ASSERT_STREQ(l[0], "SECOND_MESSAGE=1");
                 ASSERT_STREQ(l[1], "ADDITIONAL_DATA=hoge");
                 ASSERT_EQ(c->data, 1u);
+                ASSERT_NOT_NULL(fds);
+                ASSERT_EQ(fdset_size(fds), 2u);
+
+                int i;
+                FDSET_FOREACH(i, fds) {
+                        _cleanup_free_ char *path = NULL;
+                        ASSERT_OK(fd_get_path(i, &path));
+                        ASSERT_TRUE(STR_IN_SET(path, "/tmp", "/dev/null"));
+                }
         }
 
         c->data++;
@@ -80,7 +98,16 @@ TEST(notify_socket_prepare) {
         if (r == 0) {
                 ASSERT_OK_ERRNO(setenv("NOTIFY_SOCKET", path, /* overwrite = */ true));
                 ASSERT_OK_POSITIVE(sd_notify(/* unset_environment = */ false, "FIRST_MESSAGE=1"));
-                ASSERT_OK_POSITIVE(sd_notify(/* unset_environment = */ false, "FIRST_MESSAGE=2\nADDITIONAL_DATA=hoge"));
+
+                _cleanup_close_ int fd1 = open("/tmp", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
+                ASSERT_OK_ERRNO(fd1);
+                _cleanup_close_ int fd2 = open("/dev/null", O_RDONLY|O_CLOEXEC);
+                ASSERT_OK_ERRNO(fd2);
+
+                ASSERT_OK_POSITIVE(
+                        sd_pid_notify_with_fds(
+                                0, /* unset_environment = */ false,
+                                "FIRST_MESSAGE=2\nADDITIONAL_DATA=hoge", (int[]) { fd1, fd2 }, 2));
                 _exit(EXIT_SUCCESS);
         }