]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
mlmmj-maintd: resend_queue: fully test
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 8 Mar 2023 14:34:19 +0000 (15:34 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 8 Mar 2023 14:34:19 +0000 (15:34 +0100)
src/mlmmj-maintd.c
tests/mlmmj-maintd.sh

index 98cc1fb9bdaf908bacf489ec963ea0b935563690..620fa40c2b4506340d442da12f910f457f8c3220 100644 (file)
@@ -151,10 +151,9 @@ resend_queue(struct ml *ml, int logfd)
        DIR *queuedir;
        struct dirent *dp;
        char *fromname, *toname, *reptoname, *from, *to, *repto;
-       char *mailname = NULL;
        char *ch;
        struct stat st;
-       int err = 0, qfd;
+       int errn = 0, qfd;
        time_t t, bouncelife;
        bool ret = true;
        struct mail mail;
@@ -162,18 +161,15 @@ resend_queue(struct ml *ml, int logfd)
        opendirat(ml->fd, qfd, queuedir, "queue");
 
        while((dp = readdir(queuedir)) != NULL) {
-               if(fstatat(qfd, dp->d_name, &st, 0) < 0) {
-                       log(" - Could not stat(queue/%s): %s",
-                           dp->d_name, strerror(errno));
-                       ret = false;
+               /* we might have deleted some files like the "mail.*" */
+               if(fstatat(qfd, dp->d_name, &st, 0) < 0)
                        continue;
-               }
 
                if(!S_ISREG(st.st_mode))
                        continue;
 
                if(strchr(dp->d_name, '.')) {
-                       mailname = xstrdup(dp->d_name);
+                       char *mailname = xstrdup(dp->d_name);
                        ch = strrchr(mailname, '.');
                        MY_ASSERT(ch);
                        *ch = '\0';
@@ -196,13 +192,12 @@ resend_queue(struct ml *ml, int logfd)
                xasprintf(&toname, "%s.reciptto", dp->d_name);
                xasprintf(&reptoname, "%s.reply-to", dp->d_name);
 
+               errn = 0;
                from = ctrlvalue(qfd, fromname);
                if (from == NULL)
-                       err = errno;
+                       errn = errno;
                to = ctrlvalue(qfd, toname);
-               free(fromname);
-               free(toname);
-               if((from == NULL && err == ENOENT) ||
+               if((from == NULL && errn == ENOENT) ||
                                (to == NULL && errno == ENOENT)) {
                        /* only delete old files to avoid deleting
                           mail currently being sent */
@@ -216,13 +211,12 @@ resend_queue(struct ml *ml, int logfd)
                                        unlinkat(qfd, reptoname, 0);
                                }
                        }
-                       free(mailname);
                        free(reptoname);
+                       free(fromname);
+                       free(toname);
                        continue;
                }
-
                repto = ctrlvalue(qfd, reptoname);
-               free(reptoname);
 
                /* before we try again, check and see if it's old */
                bouncelife = ctrltimet(ml->ctrlfd, "bouncelife", BOUNCELIFE);
@@ -233,7 +227,10 @@ resend_queue(struct ml *ml, int logfd)
                                    dp->d_name, strerror(errno));
                                ret = false;
                        }
-                       free(mailname);
+                       /* avoid leaving orphans */
+                       unlinkat(qfd, fromname, 0);
+                       unlinkat(qfd, toname, 0);
+                       unlinkat(qfd, reptoname, 0);
                        free(from);
                        free(to);
                        free(repto);
@@ -244,11 +241,19 @@ resend_queue(struct ml *ml, int logfd)
                mail.to = to;
                mail.from = from;
                mail.replyto = repto;
-               mail.fp = fopen(mailname, "r");
-               if (send_single_mail(&mail, ml, false))
-                       save_queue(mailname, &mail);
-               else
-                       unlink(mailname);
+               int mailfd = openat(qfd, dp->d_name, O_RDONLY);
+               mail.fp = fdopen(mailfd, "r");
+               if (mail.fp == NULL)
+                       err(1, "merde");
+               if (send_single_mail(&mail, ml, false)) {
+                       unlinkat(qfd, dp->d_name, 0);
+                       unlinkat(qfd, fromname, 0);
+                       unlinkat(qfd, toname, 0);
+                       unlinkat(qfd, reptoname, 0);
+               }
+               free(reptoname);
+               free(fromname);
+               free(toname);
                fclose(mail.fp);
        }
 
index 96ccd4ad60185214b0140ee79df70df6483edb3c..ad45626e4cd35300f4ea94ea86097918e41dbc21 100755 (executable)
@@ -2,6 +2,8 @@
 
 . $(atf_get_srcdir)/test_env.sh
 
+mlmmjmaintd=$(command -v mlmmj-maintd)
+
 tests_init \
        nolongerbouncing \
        discarded \
@@ -33,11 +35,11 @@ tests_init \
        basics_9 \
        basics_10 \
        basics_11 \
-       resend_requeue
+       resend_requeue \
+       resend_queue
 
 nolongerbouncing_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        mkdir -p lists/ml/bounce
@@ -64,7 +66,6 @@ nolongerbouncing_body()
 
 discarded_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
 
@@ -81,7 +82,6 @@ discarded_body()
 
 basics_0_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
 
@@ -103,7 +103,6 @@ basics_0_body()
 
 basics_1_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -114,7 +113,6 @@ basics_1_body()
 basics_2_body()
 {
        test $(id -u) = 0 && atf_skip "Can only be run as non-root"
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -125,7 +123,6 @@ basics_2_body()
 
 basics_3_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -151,7 +148,6 @@ run_digests
 
 basics_4_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -176,7 +172,6 @@ run_digests
 
 basics_5_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -203,7 +198,6 @@ run_digests
 
 basics_6_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -231,7 +225,6 @@ run_digests
 
 basics_7_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -258,7 +251,6 @@ run_digests
 
 basics_8_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -289,7 +281,6 @@ run_digests
 
 basics_9_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -316,7 +307,6 @@ run_digests
 
 basics_10_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -343,7 +333,6 @@ run_digests
 
 basics_11_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -370,7 +359,6 @@ run_digests
 
 digests_0_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -396,7 +384,6 @@ run_digests
 
 digests_1_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -422,7 +409,6 @@ run_digests
 digests_2_body()
 {
        test $(id -u) = 0 && atf_skip "Can only be run as non-root"
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -448,7 +434,6 @@ run_digests
 
 digests_3_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -475,7 +460,6 @@ run_digests
 digests_4_body()
 {
        test $(id -u) = 0 && atf_skip "Can only be run as non-root"
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml lists/ml
        echo test@mlmmjtest > lists/ml/control/listaddress
@@ -503,7 +487,6 @@ run_digests
 
 unsub_bouncers_0_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        init_ml list
        echo "test@test" > list/control/listaddress
 
@@ -515,7 +498,6 @@ unsub_bouncers_0_body()
 
 unsub_bouncers_1_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        init_ml list
        echo test@mlmmjtest > list/control/listaddress
 
@@ -527,7 +509,6 @@ unsub_bouncers_1_body()
 
 unsub_bouncers_2_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        init_ml list
        echo test@mlmmjtest > list/control/listaddress
 
@@ -539,7 +520,6 @@ unsub_bouncers_2_body()
 
 unsub_bouncers_3_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        init_ml list
        echo test@mlmmjtest > list/control/listaddress
 
@@ -553,7 +533,6 @@ unsub_bouncers_3_body()
 
 unsub_bouncers_4_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        init_ml list
        echo "test@test" > list/control/listaddress
        # filename should contain a = to be properly treated
@@ -564,7 +543,6 @@ unsub_bouncers_4_body()
 
 unsub_bouncers_5_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        init_ml list
        echo "test@test" > list/control/listaddress
 
@@ -592,7 +570,6 @@ run_digests
 
 unsub_bouncers_6_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        init_ml list
        echo "test@test" > list/control/listaddress
 
@@ -623,7 +600,6 @@ run_digests
 
 unsub_bouncers_7_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        mkdir lists
        init_ml list
        echo "test@test" > list/control/listaddress
@@ -636,7 +612,6 @@ unsub_bouncers_7_body()
 
 unsub_bouncers_8_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        init_ml list
        echo "test@test" > list/control/listaddress
 
@@ -647,7 +622,6 @@ unsub_bouncers_8_body()
 
 unsub_bouncers_9_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        init_ml list
        echo "test@test" > list/control/listaddress
 
@@ -661,7 +635,6 @@ unsub_bouncers_9_body()
 
 unsub_bouncers_10_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        init_ml list
        echo "test@test" > list/control/listaddress
 
@@ -702,7 +675,6 @@ run_digests
 
 resend_requeue_body()
 {
-       mlmmjmaintd=$(command -v mlmmj-maintd)
        atf_check $srcdir/fakesmtpd
        trap kill_fakesmtp EXIT TERM
        init_ml list
@@ -900,3 +872,138 @@ EOF
        atf_check -s exit:1 test -f mail-4.txt
        atf_check -s exit:1 test -d list/requeue/1
 }
+
+resend_queue_body()
+{
+       atf_check $srcdir/fakesmtpd
+       trap kill_fakesmtp EXIT TERM
+       init_ml list
+       echo "test@test" > list/control/listaddress
+       echo "25678" > list/control/smtpport
+       echo "heloname" > list/control/smtphelo
+       touch list/queue/file
+
+       # no .mailfrom, no.reciptto no.reply-to but recent
+       atf_check $mlmmjmaintd -L list -F
+       atf_check -s exit:1 test -f mail-1.txt
+       atf_check -s exit:0 test -f list/queue/file
+
+       touch -t 000101010101 list/queue/file
+       # no .mailfrom, no.reciptto no.reply-to but recent
+       atf_check $mlmmjmaintd -L list -F
+       atf_check -s exit:1 test -f mail-1.txt
+       atf_check -s exit:1 test -f list/queue/file
+
+       touch -t 000101010101 list/queue/file
+       echo "bla" > list/queue/file.mailfrom
+       # no.reciptto no.reply-to old recent
+       atf_check $mlmmjmaintd -L list -F
+       atf_check -s exit:1 test -f mail-1.txt
+       atf_check -s exit:1 test -f list/queue/file
+       atf_check -s exit:1 test -f list/queue/file.mailfrom
+
+       touch -t 000101010101 list/queue/file
+       echo "bla" > list/queue/file.mailfrom
+       touch list/queue/file.reply-to
+       # no.reciptto no.reply-to old recent
+       atf_check $mlmmjmaintd -L list -F
+       atf_check -s exit:1 test -f list/queue/file
+       atf_check -s exit:1 test -f list/queue/file.mailfrom
+       atf_check -s exit:1 test -f list/queue/file.reply-to
+
+       touch -t 000101010101 list/queue/file
+       echo "bla" > list/queue/file.mailfrom
+       echo "meh" > list/queue/file.reciptto
+       touch list/queue/file.reply-to
+       atf_check $mlmmjmaintd -L list -F
+       atf_check -s exit:1 test -f list/queue/file
+       atf_check -s exit:1 test -f list/queue/file.mailfrom
+       atf_check -s exit:1 test -f list/queue/file.reciptto
+       atf_check -s exit:1 test -f list/queue/file.reply-to
+
+       touch list/queue/file
+       touch list/queue/file.mailfrom
+       echo "meh" > list/queue/file.reciptto
+       touch list/queue/file.reply-to
+       atf_check $mlmmjmaintd -L list -F
+       atf_check -s exit:0 test -f list/queue/file
+       atf_check -s exit:0 test -f list/queue/file.mailfrom
+       atf_check -s exit:0 test -f list/queue/file.reciptto
+       atf_check -s exit:0 test -f list/queue/file.reply-to
+
+       touch list/queue/file
+       touch list/queue/file.mailfrom
+       touch list/queue/file.reciptto
+       touch list/queue/file.reply-to
+       atf_check $mlmmjmaintd -L list -F
+       atf_check -s exit:0 test -f list/queue/file
+       atf_check -s exit:0 test -f list/queue/file.mailfrom
+       atf_check -s exit:0 test -f list/queue/file.reciptto
+       atf_check -s exit:0 test -f list/queue/file.reply-to
+
+       # actually send the queued email
+       cat > list/queue/file <<EOF
+From: plop
+To: bla
+Subject: hey
+
+Body
+EOF
+       echo "bob@test1" > list/queue/file.mailfrom
+       echo "bob@test2" > list/queue/file.reciptto
+       rm list/queue/file.reply-to
+       atf_check $mlmmjmaintd -L list -F
+       cat > expected.txt <<EOF
+EHLO heloname\r
+MAIL FROM:<bob@test1>\r
+RCPT TO:<bob@test2>\r
+DATA\r
+From: plop\r
+To: bla\r
+Subject: hey\r
+\r
+Body\r
+\r
+.\r
+QUIT\r
+EOF
+       atf_check -o file:expected.txt cat mail-1.txt
+       atf_check -s exit:1 test -f mail-2.txt
+       atf_check -s exit:1 test -f list/queue/file
+       atf_check -s exit:1 test -f list/queue/file.mailfrom
+       atf_check -s exit:1 test -f list/queue/file.reciptto
+
+       # actually send the queued email with reply-to
+       cat > list/queue/file <<EOF
+From: plop
+To: bla
+Subject: hey
+
+Body
+EOF
+       echo "bob@test1" > list/queue/file.mailfrom
+       echo "bob@test2" > list/queue/file.reciptto
+       echo "jane@test2" > list/queue/file.reply-to
+       rm list/queue/file.reply-to
+       atf_check $mlmmjmaintd -L list -F
+       cat > expected.txt <<EOF
+EHLO heloname\r
+MAIL FROM:<bob@test1>\r
+RCPT TO:<bob@test2>\r
+DATA\r
+From: plop\r
+To: bla\r
+Subject: hey\r
+\r
+Body\r
+\r
+.\r
+QUIT\r
+EOF
+       atf_check -o file:expected.txt cat mail-2.txt
+       atf_check -s exit:1 test -f mail-3.txt
+       atf_check -s exit:1 test -f list/queue/file
+       atf_check -s exit:1 test -f list/queue/file.mailfrom
+       atf_check -s exit:1 test -f list/queue/file.reciptto
+       atf_check -s exit:1 test -f list/queue/file.reply-to
+}