]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
mlmmj-send: validate argument for -l option
authorBaptiste Daroussin <bapt@FreeBSD.org>
Mon, 24 Oct 2022 08:19:03 +0000 (10:19 +0200)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Mon, 24 Oct 2022 08:34:35 +0000 (10:34 +0200)
validate the argument for -l option, while here turn the "archive"
into a boolean

configure.ac
src/mlmmj-send.c
tests/Kyuafile.in [new file with mode: 0644]
tests/Makefile.am
tests/mlmmj-send.sh [new file with mode: 0644]
tests/test_env.sh.in [new file with mode: 0644]

index 9e41a53726a8ae94dc6d870a1900182c2bc25864..a94ad43b675743fc8fb0dbcf95e63772665e9416 100644 (file)
@@ -56,4 +56,5 @@ AC_CONFIG_FILES([contrib/Makefile])
 AC_CONFIG_FILES([contrib/receivestrip/Makefile])
 AC_CONFIG_FILES([tests/Makefile])
 AC_CONFIG_FILES([tests/Kyuafile])
+AC_CONFIG_FILES([tests/test_env.sh])
 AC_OUTPUT
index 2a4cc851b1cf068e5c32a79f1dba6c840014b428..8db2385ad0173e67aa714155a212b319dab25643 100644 (file)
@@ -60,6 +60,7 @@
 #include "wrappers.h"
 #include "statctrl.h"
 #include "ctrlvalue.h"
+#include "stdbool.h"
 #include "getaddrsfromfd.h"
 
 static int addtohdr = 0;
@@ -793,7 +794,8 @@ int main(int argc, char **argv)
 {
        size_t len = 0, hdrslen, bodylen;
        int sockfd = -1, mailfd = 0, opt, mindex = 0, subfd = 0, tmpfd, i;
-       int deletewhensent = 1, sendres = 0, archive = 1, digest = 0;
+       int deletewhensent = 1, sendres = 0, digest = 0;
+       bool archive = true;
        int ctrlarchive, res;
        char *listaddr = NULL, *listdelim = NULL;
        char *mailfilename = NULL, *subfilename = NULL, *omit = NULL;
@@ -831,7 +833,7 @@ int main(int argc, char **argv)
        while ((opt = getopt(argc, argv, "aVDhm:l:L:R:F:T:r:s:o:")) != -1){
                switch(opt) {
                case 'a':
-                       archive = 0;
+                       archive = false;
                        break;
                case 'D':
                        deletewhensent = 0;
@@ -843,7 +845,12 @@ int main(int argc, char **argv)
                        print_help(argv[0]);
                        break;
                case 'l':
+                       if (*optarg <= '0' || *optarg > '7')
+                               errx(EXIT_FAILURE, "-l only accept the "
+                                   "following arguments: 1, 2, 3, 4, 5, 6 or"
+                                   " 7");
                        listctrl = optarg;
+                       archive = false;
                        break;
                case 'L':
                        listdir = optarg;
@@ -929,19 +936,6 @@ int main(int argc, char **argv)
        stl.strs = NULL;
        stl.count = 0;
 
-       switch(listctrl[0]) {
-               case '1':
-               case '2':
-               case '3':
-               case '4':
-               case '5':
-               case '6':
-               case '7':
-                       archive = 0;
-               default:
-                       break;
-       }
-
        if(listdir && listctrl[0] != '5')
                listaddr = getlistaddr(listdir);
        
@@ -1031,7 +1025,7 @@ int main(int argc, char **argv)
                }
                break;
        case '6':
-               archive = 0;
+               archive = false;
                deletewhensent = 0;
                archivefilename = xstrdup(mailfilename);
                bounceaddr = bounce_from_adr(to_addr, listaddr, listdelim,
diff --git a/tests/Kyuafile.in b/tests/Kyuafile.in
new file mode 100644 (file)
index 0000000..4d5c0e4
--- /dev/null
@@ -0,0 +1,6 @@
+syntax(2)
+
+test_suite('mlmmj')
+
+atf_test_program{name='mlmmj'}
+atf_test_program{name='mlmmj-send'}
index 5e61cda7ca8ed5dfeee916d33563fb61ecc8ea03..770e78dc2cb3454dc94ef33582c6124bd0d52cfe 100644 (file)
@@ -7,8 +7,15 @@ mlmmj_SOURCES = mlmmj.c
 AM_CFLAGS = -g -Wall -pedantic -Wsign-compare -DDEFAULTTEXTDIR='"@textlibdir@"' -I$(srcdir)/../include @ATF_CFLAGS@
 mlmmj_LDADD =  $(top_builddir)/src/libmlmmj.a @ATF_LIBS@
 
+test_scripts = mlmmj-send.sh
+check_SCRIPTS= $(test_scripts:.sh=)
+SUFFIXES=      .sh
+
 all: $(check_PROGRAMS) $(check_SCRIPTS)
 
 check: $(check_SCRIPTS) $(check_PROGRAMS)
        kyua --config=none test --kyuafile='$(builddir)/Kyuafile' \
                --build-root='$(builddir)'
+
+.sh:
+       install -m 755 $< $@
diff --git a/tests/mlmmj-send.sh b/tests/mlmmj-send.sh
new file mode 100644 (file)
index 0000000..026b3a7
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/env atf-sh
+
+. $(atf_get_srcdir)/test_env.sh
+
+tests_init \
+       basics
+
+basics_body()
+{
+
+       mlmmjsend=$(command -v mlmmj-send)
+       helptxt="Usage: $mlmmjsend [-L /path/to/list -m /path/to/mail | -l listctrl]
+       [-a] [-D] [-F sender@example.org] [-h] [-o address@example.org]
+       [-r 127.0.0.1] [-R reply@example.org] [-s /path/to/subscribers]
+       [-T recipient@example.org] [-V]
+ -a: Don't archive the mail
+ -D: Don't delete the mail after it's sent
+ -F: What to use as MAIL FROM:
+ -h: This help
+ -l: List control variable:
+    '1' means 'send a single mail'
+    '2' means 'mail to moderators'
+    '3' means 'resend failed list mail'
+    '4' means 'send to file with recipients'
+    '5' means 'bounceprobe'
+    '6' means 'single listmail to single recipient'
+    '7' means 'digest'
+ -L: Full path to list directory
+ -m: Full path to mail file
+ -o: Address to omit from distribution (normal mail only)
+ -r: Relayhost IP address (defaults to 127.0.0.1)
+ -R: What to use as Reply-To: header
+ -s: Subscribers file name
+ -T: What to use as RCPT TO:
+ -V: Print version
+"
+       init_ml ml
+
+       atf_check -s exit:1 -e "inline:mlmmj-send: You have to specify -m and -L or -l\n$mlmmjsend -h for help\n" $mlmmjsend
+       atf_check -s exit:0 -o "inline:mlmmj-send version 1.3.0\n" $mlmmjsend -V
+       atf_check -s exit:0 -o "inline:$helptxt" $mlmmjsend -h
+       atf_check -s exit:1 -e "inline:mlmmj-send: -l only accept the following arguments: 1, 2, 3, 4, 5, 6 or 7\n" $mlmmjsend -l 8
+       atf_check -s exit:1 -e "inline:mlmmj-send: -l only accept the following arguments: 1, 2, 3, 4, 5, 6 or 7\n" $mlmmjsend -l 0
+}
diff --git a/tests/test_env.sh.in b/tests/test_env.sh.in
new file mode 100644 (file)
index 0000000..4681fea
--- /dev/null
@@ -0,0 +1,37 @@
+srcdir="$(atf_get_srcdir)"
+export PATH="$srcdir/../src:${PATH}"
+
+selfdir="@abs_top_srcdir@/tests"
+LIBRARY_PATH_ENV="LIBRARY_PATH"
+SYSROOT_DIR="${selfdir}"
+
+prefix="@prefix@"
+exec_prefix="@exec_prefix@"
+datarootdir="@datarootdir@"
+pcpath="@PKG_DEFAULT_PATH@"
+
+tests_init()
+{
+       TESTS="$@"
+       export TESTS
+       for t ; do
+               atf_test_case $t
+       done
+}
+
+atf_init_test_cases() {
+       for t in ${TESTS}; do
+               atf_add_test_case $t
+       done
+}
+
+init_ml()
+{
+       local ml="$1"
+       mkdir "$ml"
+       for d in incoming queue queue/discarded archive text subconf unsubconf \
+               bounce control moderation subscribers.d digesters.d requeue \
+               nomailsubs.d; do
+               mkdir $ml/$d
+       done
+}