#!/bin/sh
# $Id: t-rsad-0.sh,v 1.3 2007/02/12 05:29:53 ca Exp $
# ----------------------------------------
# test RSAD, individual RCPT replies after final dot.
# ----------------------------------------
#
# host on which to listen/to which to send
H=localhost
# port number to use
P=${MTA_SRVPORT:-1235}
ERRS=0
SRVL="smtps.log"
CLTL="smtpc.log"
rm -f $SRVL $CLTL
#

SRV=./smtps2
CLT=./smtpc2

# start SMTP server
$SRV -d 5 -9 -i -l . -b $H:$P > $SRVL 2>&1 &
status=$?
SRVPID=$!
if [ "x$status" != "x0" ]
then
  echo "FAIL: cannot start server $status"
  exit $status
fi
sleep 3
if grep "ERROR" $SRVL > /dev/null
then
  echo "found ERROR in $SRVL"
  ERRS=`expr $ERRS + 1 `
fi

# send messages
if ${CLT} -9 -d 4 -R'd250@example2.tld' -R'd451@example4.tld'  -R'd552@example2.tld' -r $H:$P > $CLTL 2>&1
then

  for L in ${SRVL} ${CLTL}
  do
    C=`grep -c '250 deferred' ${L}`
    if test X"${C}" = "X3"
    then
      :
    else
      echo "$0: expected '250 deferred' 3 times, got ${C}"
      cat $CLTL
      ERRS=`expr $ERRS + 1 `
    fi
    for i in '353 RCPT status follows' '250 whatever' '451 whatever' '552 whatever' '250 final'
    do
      if grep "$i" ${L} >/dev/null
      then
        :
      else
        echo "$0: ${i} missing"
        #cat ${L}
        ERRS=`expr $ERRS + 1 `
      fi
    done
  done

else
  cat ${CLTL}
  ERRS=`expr $ERRS + 1 `
fi

# ----------------
kill -TERM $SRVPID


if [ "$ERRS" = "0" ]
then
  exit 0
else
  echo "$ERRS error(s)"
  exit ${ERRS}
fi


syntax highlighted by Code2HTML, v. 0.9.1