Fix the race condition with fakesmtp so it works without needing retries.
[mmh] / test / mhmail / test-mhmail
index e8fbac8..3745207 100755 (executable)
@@ -18,35 +18,18 @@ fi
 # $1 is expected output file, provided by caller
 # $2 is mhmail switches, except for -body
 # $3 of -b signifies use -body switch, | signifies provide body on stdin
-# $4 contains message body.  When using stdin, can contain printf(1) format
-#    specifiers.
+# $4 contains the message body.
 test_mhmail ()
 {
-    "${MH_OBJ_DIR}/test/fakesmtp" "$actual" $localport &
-    pid="$!"
-
-    # The server doesn't always come up fast enough, so sleep and
-    # retry a few times if it fails...
-    status=1
-    for i in 0 1 2 3 4 5 6 7 8 9; do
-        if [ $3 = '|' ]; then
-          if printf "$4" | mhmail recipient@example.com $2 \
-             -server 127.0.0.1 -port $localport; then
-              status=0
-              break
-          fi
-        else
-          if mhmail recipient@example.com $2 -body "$4" \
-             -server 127.0.0.1 -port $localport; then
-              status=0
-              break
-          fi
-        fi
-        sleep 1
-    done
-    [ $status -eq 0 ] || exit 1
-
-    wait ${pid}
+    pid=`"${MH_OBJ_DIR}/test/fakesmtp" "$actual" $localport`
+
+    if [ $3 = '|' ]; then
+       printf '%s' "$4" | mhmail recipient@example.com $2 \
+                       -server 127.0.0.1 -port $localport
+    else
+       mhmail recipient@example.com $2 -body "$4" \
+             -server 127.0.0.1 -port $localport
+    fi
 
     #
     # It's hard to calculate the exact Date: header post is going to
@@ -57,8 +40,7 @@ test_mhmail ()
 
     sed -e 's/^Date:.*/Date:/' \
         -e 's/^Resent-Date:.*/Resent-Date:/' \
-        -e 's/^Message-ID:.*/Message-ID:/' \
-        -e 's/^Content-ID:.*/Content-ID:/' "$actual" > "$actual".nodate
+        -e 's/^Message-ID:.*/Message-ID:/' "$actual" > "$actual".nodate
     rm -f "$actual"
 
     check "$actual".nodate "$1"
@@ -73,13 +55,13 @@ actual_err=$MH_TEST_DIR/test-mhmail$$.actual_err
 # check -help
 # Verified behavior consistent with compiled sendmail.
 cat >$expected <<EOF
-Usage: mhmail [addrs ... [switches]]
+Usage: mhmail [-t(o)] addrs ... [switches]
   switches are:
   -at(tach) file [-at(tach) file] ...
   -b(ody) text
   -c(c) addrs ...
   -f(rom) addr
-  -hea(der-field) name:field [-hea(der-field) name:field] ...
+  -hea(derfield) name:value [-hea(derfield) name:value] ...
   -su(bject) text
   -r(esent)
   -pr(ofile)
@@ -88,6 +70,7 @@ Usage: mhmail [addrs ... [switches]]
   -v(ersion)
   -hel(p)
   and all post(8)/send(1) switches
+  mhmail with no arguments is equivalent to inc
 EOF
 
 mhmail -help >$actual 2>&1
@@ -98,19 +81,33 @@ check $expected $actual
 # Verified same behavior as compiled mhmail.
 case `mhmail -v` in
   mhmail\ --*) ;;
-  *           ) echo "$0: mhmail -v generated unexpected output" 1>&2
-                failed=`expr ${failed:-0} + 1`;;
+  *          ) printf '%s: mhmail -v generated unexpected output\n' "$0" >&2
+               failed=`expr ${failed:-0} + 1`;;
 esac
 
-
-# check with no arguments
+# check for missing argument to switches that require them
+for switch in attach body cc from headerfield subject to; do
+  run_test "mhmail recipient -$switch" \
+           "mhmail: missing argument to -$switch"
+done
+for switch in attach body cc from headerfield subject to; do
+  run_test "mhmail recipient -$switch -nosend" \
+           "mhmail: missing argument to -$switch"
+done
+for switch in attach body cc from headerfield subject to; do
+  run_test "mhmail recipient -$switch -server 127.0.0.1" \
+           "mhmail: missing argument to -$switch"
+done
+
+
+# check with no switches
 # That will just run inc, which we don't want to do anything,
 # so tell inc to just display its version.
 # Verified same behavior as compiled mhmail.
-printf "inc: -version\n" >> $MH
+printf 'inc: -version\n' >> $MH
 case `mhmail` in
   inc\ --*) ;;
-  *           ) echo "$0: mhmail generated unexpected output" 1>&2
+  *           ) echo "$0: mhmail generated unexpected output" >&2
                 failed=`expr ${failed:-0} + 1`;;
 esac
 
@@ -273,7 +270,7 @@ test_mhmail "$expected" '-from sender@localhost -subject Test' '|' message
 # 2) use send(1) by added a send switch to the profile and
 # verifying that it gets used.
 # Not supported by compiled mhmail.
-printf "send: -msgid\n" >> $MH
+printf 'send: -msgid\n' >> $MH
 
 cat > "$expected" <<EOF
 EHLO nosuchhost.example.com
@@ -294,7 +291,7 @@ test_mhmail "$expected" '-from sender@localhost -profile' '|' message
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
 
 
-# check repeated -from and -subject arguments
+# check repeated -from and -subject switches
 # Verified same behavior as compiled mhmail.
 cat > "$expected" <<EOF
 EHLO nosuchhost.example.com
@@ -315,7 +312,7 @@ test_mhmail "$expected" '-from sender@localhost -from sender2@localhost '\
 '-subject Subject1 -subject Subject2' -b message
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
 
-# check repeated -body arguments
+# check repeated -body switches
 # Verified same behavior as compiled mhmail.
 cat > "$expected" <<EOF
 EHLO nosuchhost.example.com
@@ -335,7 +332,7 @@ test_mhmail "$expected" "-from sender@localhost -body body1" -b body2
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
 
 
-# check multiple -cc arguments
+# check multiple -cc switches
 # Verified same behavior as compiled mhmail.
 cat > "$expected" <<EOF
 EHLO nosuchhost.example.com
@@ -385,6 +382,33 @@ test_mhmail "$expected" \
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
 
 
+# check -cc switch followed by -to switch
+# Verified same behavior as compiled mhmail.
+cat > "$expected" <<EOF
+EHLO nosuchhost.example.com
+MAIL FROM:<sender@localhost>
+RCPT TO:<recipient@example.com>
+RCPT TO:<recipient2@example.com>
+RCPT TO:<cc1@example.com>
+DATA
+To: recipient@example.com, recipient2@example.com
+Cc: cc1@example.com
+Subject: Test
+From: sender@localhost
+Date:
+
+message
+.
+QUIT
+EOF
+
+test_mhmail "$expected" \
+  "-from sender@localhost -cc cc1@example.com -subject Test \
+   -to recipient2@example.com" \
+  -b message
+[ ${failed:-0} -eq 0 ] || exit ${failed:-0}
+
+
 # check with no newline on stdin
 # Shows different behavior than compiled mhmail, which was silent in this case.
 cat > "$expected" <<EOF
@@ -420,7 +444,8 @@ Date:
 QUIT
 EOF
 
-test_mhmail "$expected" '-from sender@localhost' '|' '\n'
+test_mhmail "$expected" '-from sender@localhost' '|' '
+'
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
 
 
@@ -442,7 +467,10 @@ Date:
 QUIT
 EOF
 
-test_mhmail "$expected" '-from sender@localhost' '|' '\n\n\n'
+test_mhmail "$expected" '-from sender@localhost' '|' '
+
+
+'
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
 
 
@@ -484,7 +512,10 @@ here's some text
 QUIT
 EOF
 
-test_mhmail "$expected" '-from sender@localhost' '|' "here's some text\n\n\n"
+test_mhmail "$expected" '-from sender@localhost' '|' "here's some text
+
+
+"
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
 
 
@@ -690,7 +721,7 @@ please resend this message, 3'
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
 
 
-# check -header-field.
+# check -headerfield.
 # Not supported by compiled mhmail.
 cat > "$expected" <<EOF
 EHLO nosuchhost.example.com
@@ -708,13 +739,13 @@ QUIT
 EOF
 
 test_mhmail "$expected" \
-  '-from sender@example.com -header-field User-Agent:nmh' \
+  '-from sender@example.com -headerfield User-Agent:nmh' \
   -b 'with added header field'
 
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
 
 
-# check multiple -header-fields.
+# check multiple -headerfields.
 # Not supported by compiled mhmail.
 cat > "$expected" <<EOF
 EHLO nosuchhost.example.com
@@ -734,9 +765,9 @@ QUIT
 EOF
 
 test_mhmail "$expected" \
-  "-from sender@example.com -header-field MIME-Version:1.0 \
--header-field Content-Type:text/plain;charset=utf-8 \
--header-field Content-Transfer-Encoding:8bit" \
+  "-from sender@example.com -headerfield MIME-Version:1.0 \
+-headerfield Content-Type:text/plain;charset=utf-8 \
+-headerfield Content-Transfer-Encoding:8bit" \
   -b 'with added header fields'
 
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
@@ -753,7 +784,6 @@ To: recipient@example.com
 From: sender@example.com
 MIME-Version: 1.0
 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"
-Content-ID:
 Date:
 Message-ID: