]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: test-file-create-locked - Use the new test-subprocess API
authorStephan Bosch <stephan.bosch@open-xchange.com>
Mon, 27 Oct 2025 21:04:45 +0000 (22:04 +0100)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Wed, 5 Nov 2025 10:17:48 +0000 (10:17 +0000)
src/lib/test-file-create-locked.c
src/lib/test-lib.c
src/lib/test-lib.h

index f5b4f6a991c04973280c015fcf50cbe1d348fa1d..d96d8c76f596e66cd7a4308906f777b0fe8fe723 100644 (file)
@@ -39,7 +39,7 @@ static bool wait_for_file(pid_t pid, const char *path)
        return FALSE;
 }
 
-static void test_file_create_locked_basic(void)
+static int test_file_create_locked_basic_child(void *context ATTR_UNUSED)
 {
        struct file_create_settings set = {
                .lock_timeout_secs = 0,
@@ -48,43 +48,56 @@ static void test_file_create_locked_basic(void)
                },
        };
        const char *path = ".test-file-create-locked";
-       struct file_lock *lock;
+       struct file_lock *lock = NULL;
        const char *error;
        bool created;
-       pid_t pid;
        int fd;
 
+       /* child */
+       fd = file_create_locked(path, &set, &lock, &created, &error);
+       test_assert(fd > 0);
+       test_assert(created);
+       if (test_has_failed())
+              return 1;
+       create_file(".test-temp-file-create-locked-child");
+       i_sleep_intr_secs(60);
+       if (lock != NULL)
+               file_unlock(&lock);
+       i_close_fd(&fd);
+       return 0;
+}
+
+static void test_file_create_locked_basic(void)
+{
+       struct file_create_settings set = {
+               .lock_timeout_secs = 0,
+               .lock_settings = {
+                       .lock_method = FILE_LOCK_METHOD_FCNTL,
+               },
+       };
+       const char *path = ".test-file-create-locked";
+       struct file_lock *lock = NULL;
+       const char *error;
+       pid_t pid;
+       bool created;
+
        test_begin("file_create_locked()");
 
        i_unlink_if_exists(path);
        i_unlink_if_exists(".test-temp-file-create-locked-child");
-       pid = fork();
-       switch (pid) {
-       case (pid_t)-1:
-               i_error("fork() failed: %m");
-               break;
-       case 0:
-               /* child */
-               fd = file_create_locked(path, &set, &lock, &created, &error);
-               test_assert(fd > 0);
-               test_assert(created);
-               if (test_has_failed())
-                       lib_exit(1);
-               create_file(".test-temp-file-create-locked-child");
-               sleep(60);
-               i_close_fd(&fd);
-               lib_exit(0);
-       default:
-               /* parent */
-               test_assert(wait_for_file(pid, ".test-temp-file-create-locked-child"));
-               if (test_has_failed())
-                       break;
-               test_assert(file_create_locked(path, &set, &lock, &created, &error) == -1);
+       pid = test_subprocess_fork(test_file_create_locked_basic_child, NULL,
+                                  TRUE);
+
+       /* parent */
+       test_assert(wait_for_file(pid, ".test-temp-file-create-locked-child"));
+       if (!test_has_failed()) {
+               test_assert(file_create_locked(path, &set,
+                                              &lock, &created, &error) == -1);
                test_assert(errno == EAGAIN);
-               if (kill(pid, SIGKILL) < 0)
-                       i_error("kill(SIGKILL) failed: %m");
-               break;
+               if (lock != NULL)
+                       file_unlock(&lock);
        }
+       test_subprocess_kill_all(20);
        i_unlink_if_exists(".test-temp-file-create-locked-child");
        i_unlink_if_exists(path);
        test_end();
index 6ca3367e96abf3b38ab4583dacd997591719697a..73b41bb17d94ccfdbaf34ede6288bc3b6a4b84b5 100644 (file)
@@ -26,6 +26,7 @@ int main(int argc, char **argv)
        };
 
        test_dir_init("lib");
+       test_subprocesses_init();
 
        return test_run_named_with_fatals(match, test_functions, fatal_functions);
 }
index 02867b84300b35585c9d03749ff19c630b9fecca..4f2ad1ef04cc931582c77c75e06b6aebfe67a9e3 100644 (file)
@@ -4,6 +4,7 @@
 #include "lib.h"
 #include "test-common.h"
 #include "test-dir.h"
+#include "test-subprocess.h"
 
 #define TEST(x) TEST_DECL(x)
 #define FATAL(x) FATAL_DECL(x)