]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-smtp: unit tests - Use signals to ensure client doesn't start before server is...
authorMarco Bettini <marco.bettini@open-xchange.com>
Thu, 22 Dec 2022 16:06:17 +0000 (16:06 +0000)
committerMarco Bettini <marco.bettini@open-xchange.com>
Thu, 19 Jan 2023 14:13:41 +0000 (14:13 +0000)
src/lib-smtp/test-smtp-client-errors.c
src/lib-smtp/test-smtp-payload.c
src/lib-smtp/test-smtp-server-errors.c
src/lib-smtp/test-smtp-submit.c
src/lib-test/test-subprocess.c
src/lib-test/test-subprocess.h

index 7bdd60d4c2bf77dc9d7447356e1a68d06ac0bec4..93b83ebeb69b3ce01f93a67b00057b7208fdd95f 100644 (file)
@@ -21,6 +21,7 @@
 #include "smtp-client-connection.h"
 #include "smtp-client-transaction.h"
 
+#include <sys/signal.h>
 #include <unistd.h>
 
 #define CLIENT_PROGRESS_TIMEOUT     10
@@ -4204,6 +4205,7 @@ static int test_run_server(struct test_server_data *data)
 
        server_ssl_ctx = NULL;
 
+       test_subprocess_notify_signal_send_parent(SIGUSR1);
        ioloop = io_loop_create();
        data->server_test(data->index);
        io_loop_destroy(&ioloop);
@@ -4224,6 +4226,7 @@ static int test_run_dns(test_dns_init_t dns_test)
        if (debug)
                i_debug("PID=%s", my_pid);
 
+       test_subprocess_notify_signal_send_parent(SIGUSR1);
        ioloop = io_loop_create();
        dns_test();
        io_loop_destroy(&ioloop);
@@ -4281,7 +4284,10 @@ test_run_client_server(const struct smtp_client_settings *client_set,
 
                        /* Fork server */
                        fd_listen = fds[i];
+                       test_subprocess_notify_signal_reset(SIGUSR1);
                        test_subprocess_fork(test_run_server, &data, FALSE);
+                       test_subprocess_notify_signal_wait(
+                               SIGUSR1, TEST_SIGNALS_DEFAULT_TIMEOUT_MS);
                        i_close_fd(&fd_listen);
                }
        }
@@ -4297,7 +4303,10 @@ test_run_client_server(const struct smtp_client_settings *client_set,
 
                /* Fork DNS service */
                fd_listen = fd;
+               test_subprocess_notify_signal_reset(SIGUSR1);
                test_subprocess_fork(test_run_dns, dns_test, FALSE);
+               test_subprocess_notify_signal_wait(SIGUSR1,
+                       TEST_SIGNALS_DEFAULT_TIMEOUT_MS);
                i_close_fd(&fd_listen);
        }
 
index 68455037dad14a87dcb5bf64a1dc5eaa12ca38e0..277d260021bc69d9cd466d00ee4590d0c94ec75e 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/signal.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <dirent.h>
@@ -868,6 +869,7 @@ static int test_run_server(struct test_server_data *data)
        if (debug)
                i_debug("PID=%s", my_pid);
 
+       test_subprocess_notify_signal_send_parent(SIGUSR1);
        ioloop = io_loop_create();
        test_server_init(server_set);
        io_loop_run(ioloop);
@@ -929,7 +931,9 @@ test_run_client_server(
 
        /* Fork server */
        test_open_server_fd();
+       test_subprocess_notify_signal_reset(SIGUSR1);
        test_subprocess_fork(test_run_server, &data, FALSE);
+       test_subprocess_notify_signal_wait(SIGUSR1, TEST_SIGNALS_DEFAULT_TIMEOUT_MS);
        i_close_fd(&fd_listen);
 
        /* Run client */
index b944a3968dadca6367d82341f730b33fae98b18e..5990d217867449ecfde2faf62c78b8273f64d54c 100644 (file)
@@ -16,6 +16,7 @@
 #include "smtp-reply-parser.h"
 #include "smtp-server.h"
 
+#include <sys/signal.h>
 #include <unistd.h>
 
 #define VALGRIND_TIMEOUT_MULTIPLIER (ON_VALGRIND ? 5 : 1)
@@ -3769,8 +3770,13 @@ static int test_run_client(struct test_client_data *data)
        if (debug)
                i_debug("PID=%s", my_pid);
 
-       /* wait a little for server setup */
-       i_sleep_msecs(100);
+       test_subprocess_notify_signal_reset(SIGUSR1);
+
+       /* signal server that we started */
+       test_subprocess_notify_signal_send_parent(SIGUSR1);
+
+       /* wait server to be ready */
+       test_subprocess_notify_signal_wait(SIGUSR1, TEST_SIGNALS_DEFAULT_TIMEOUT_MS);
 
        ioloop = io_loop_create();
        data->client_test(data->index);
@@ -3796,6 +3802,9 @@ test_run_server(const struct smtp_server_settings *server_set,
        i_zero(&server_callbacks);
 
        server_pending = client_tests_count;
+
+       /* signal clients that server is ready */
+       test_subprocess_notify_signal_all(SIGUSR1);
        ioloop = io_loop_create();
        server_test(server_set);
        io_loop_destroy(&ioloop);
@@ -3824,8 +3833,10 @@ test_run_client_server(const struct smtp_server_settings *server_set,
                data.client_test = client_test;
 
                /* Fork client */
+               test_subprocess_notify_signal_reset(SIGUSR1);
                test_subprocess_fork(test_run_client, &data, FALSE);
-
+               test_subprocess_notify_signal_wait(
+                       SIGUSR1, TEST_SIGNALS_DEFAULT_TIMEOUT_MS);
        }
 
        /* Run server */
index 3aff35b8a40e71edd650d953d521afde14d9b6a4..76c2d6237a216903b493b8495e7de59a6914db2d 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/signal.h>
 #include <fcntl.h>
 
 #define SERVER_KILL_TIMEOUT_SECS    20
@@ -2070,6 +2071,7 @@ static int test_run_server(struct test_server_data *data)
        if (debug)
                i_debug("PID=%s", my_pid);
 
+       test_subprocess_notify_signal_send_parent(SIGUSR1);
        ioloop = io_loop_create();
        data->server_test(data->index);
        io_loop_destroy(&ioloop);
@@ -2131,7 +2133,10 @@ test_run_client_server(const struct smtp_submit_settings *submit_set,
 
                        /* Fork server */
                        fd_listen = fds[i];
+                       test_subprocess_notify_signal_reset(SIGUSR1);
                        test_subprocess_fork(test_run_server, &data, FALSE);
+                       test_subprocess_notify_signal_wait(
+                               SIGUSR1, TEST_SIGNALS_DEFAULT_TIMEOUT_MS);
                        i_close_fd(&fd_listen);
                }
        }
index eeaae801b3693ea160f6738bf28bb304a86e7140..cb906e48fd6ac70399c80a618d5bca0f54695c44 100644 (file)
@@ -23,6 +23,8 @@ static volatile bool test_subprocess_notification_signal_received[SIGUSR1 + 1];
 static struct event *test_subprocess_event = NULL;
 static ARRAY(struct test_subprocess *) test_subprocesses = ARRAY_INIT;
 static void (*test_subprocess_cleanup_callback)(void) = NULL;
+static void
+test_subprocess_notification_signal(const siginfo_t *si, void *context);
 
 static void
 test_subprocess_signal(const siginfo_t *si ATTR_UNUSED,
@@ -63,6 +65,8 @@ test_subprocess_child(int (*func)(void *context), void *context,
        lib_signals_set_handler(SIGINT,
                LIBSIG_FLAG_DELAYED | LIBSIG_FLAG_IOLOOP_AUTOMOVE,
                test_subprocess_signal, NULL);
+       lib_signals_set_handler(SIGUSR1, LIBSIG_FLAG_RESTART,
+               test_subprocess_notification_signal, NULL);
 
        ret = func(context);
 
@@ -312,7 +316,7 @@ void test_subprocess_set_cleanup_callback(void (*callback)(void))
 void test_subprocess_notify_signal_send(int signo, pid_t pid)
 {
        if (kill(pid, signo) < 0)
-               i_fatal("kill(%ld, SIGHUP) failed: %m", (long)pid);
+               i_fatal("kill(%ld, SIG %d) failed: %m", (long)pid, signo);
 }
 
 void test_subprocess_notify_signal_send_parent(int signo)
@@ -320,6 +324,13 @@ void test_subprocess_notify_signal_send_parent(int signo)
        test_subprocess_notify_signal_send(signo, getppid());
 }
 
+void test_subprocess_notify_signal_all(int signo)
+{
+       struct test_subprocess *subprocess;
+       array_foreach_elem(&test_subprocesses, subprocess)
+               test_subprocess_notify_signal_send(signo, subprocess->pid);
+}
+
 void test_subprocess_notify_signal_reset(int signo)
 {
        i_assert(signo >= 0 &&
index 598b735299f3dd8e320ad14ecfb037b86825fa8d..874284787142d32040ad0c41993ba2dc34bc27e4 100644 (file)
@@ -27,8 +27,10 @@ void test_subprocess_set_cleanup_callback(void (*callback)(void));
 
 /* Send a notification signal (SIGHUP) to the given PID */
 void test_subprocess_notify_signal_send(int signo, pid_t pid);
-/* Send a notificatino signal to the parent process. */
+/* Send a notification signal to the parent process. */
 void test_subprocess_notify_signal_send_parent(int signo);
+/* Send a notification signal to all the subprocesses. */
+void test_subprocess_notify_signal_all(int signo);
 /* Reset any previously sent notification signals. */
 void test_subprocess_notify_signal_reset(int signo);
 /* Wait until a notification signal is sent, or return immediately if it was