#!/bin/sh
# $Id: smtp1.sh,v 1.73 2007/10/23 04:00:48 ca Exp $
# Copyright (c) 2003-2006 Sendmail, Inc. and its suppliers.
#	All rights reserved.
#
# By using this file, you agree to the terms and conditions set
# forth in the LICENSE file which can be found at the top level of
# the sendmail distribution.
#
#
# test program running the full system:
# qmgr, smar, smtpc, smtps
# using smtps and smtpc from statethreads/examples as source/sink
# this program tests some failure modes, e.g., temporary/permanent
# errors from the sink.
#
# This is a fairly versatile test program that can be used by other
# scripts.  It also accepts filenames that contain the expected content
# of DEFEDB such that a caller can pass its own data.
#
test -s stop && exit 1
#
# source directory of this (and other) shell script
SD=`dirname $0`
if test "${SD}" = "."
then
   SD=../chkmts
fi
#
EDBOUT=edb.out
EDBROUT=edbr.out
EDBOK=edbr.ok
EDBOKIN=""
EDBOKIN2=""
EDBROPT=""
EDBRCHK=true
# default settings
TAS=1
THREADS=1
RCPTS=1
TOTAL=1
TR=1
TRQS=1
TTAS=-1
MULTIPLE=1
INIT=true
RMDEFEDB=true
RMIBDB=true
RCPTADDR=""
SENDER_DFLT="a@b.c"
SENDER="${SENDER_DFLT}"
# initial delay for retries by QMGR
DELAY_TRY=600
# after this amount of time the SMTP sink will accept all mails
OKDELAY=""
# other options to pass to QMGR
QOPTS=""
# other options to pass to SMTPC
COPTS=""
SINK=smtps2
# other options to pass to SMTP sink
SINKOPTS=""
# other options to pass to smtpc2 (sender/source)
SRCOPTS=""
SOURCE=""
# time to sleep after running test before terminating programs
SLEEP=1
# debug flags
D="-d 7"
LGLVL=""
SNDUSR1=false
LMTPSINK=""
LMTPSINKOPTS=""
# server options
SRVOPTS=${MTA_SERVER_OPTIONS}
SMAROPT=""
# port for smtp source
SRCPORT=""
LOGERROR=""
WAITAFTERMTACHECK=0

# works only with ksh on OSF/1: how to test for it?
while getopts 13:A:a:B:bC:c:E:eDF:i:IK:LMm:n:O:o:Q:R:rS:s:T:t:v:V:W:X:x:Y:y:Z:z: FLAG
do
  case "${FLAG}" in
    1)
       SNDUSR1=true;;
    3)
       # remove the socket
       rm -f ${OPTARG};
       LMTP="-L ${OPTARG}"
       LMTPSINK="unix:${OPTARG}";;
    A)
       TOTAL="${OPTARG}"
       ;;
    a)
       SMAROPT="${OPTARG}"
       ;;
    B)
       LMTPSINKOPTS="${OPTARG}"
       ;;
    b)
       EDBROPT="-r"
       ;;
    c)
       SRCOPTS="${SRCOPTS} ${OPTARG}"
       ;;
    C)
       COPTS="${OPTARG}"
       ;;
    D) INIT=false;;
    E)
       TR="${OPTARG}"
       ;;
    e) EDBRCHK=false;;
    F)
       SRCPORT="${OPTARG}"
       ;;
    i)
       DELAY_TRY="${OPTARG}"
       ;;
    I) RMIBDB=false;;
    K)
	# expected results for t-edbr (DEFEDB)
       EDBOKIN2="${OPTARG}"
       ;;
    L) SINKOPTS="${SINKOPTS} -L unix:lmtpsock";;
    m)
       MULTIPLE="${OPTARG}"
       ;;
    M) RMDEFEDB=false;;
    n)
       RCPTS="${OPTARG}"
       ;;
    o)
       OKDELAY="-o ${OPTARG}"
       ;;
    O)
	# expected results for t-edbr (DEFEDB)
       EDBOKIN="${OPTARG}"
       ;;
    Q)
       QOPTS="${QOPTS} ${OPTARG}"
       ;;
    R)
       RCPTADDR="${RCPTADDR} -R${OPTARG}"
       ;;
    r)
       SINK="smtps3"
       SMTPDIR="smtpdir/"
       SINKOPTS="-C ${SMTPDIR} ${SINKOPTS}"
       export SMTPDIR
       ;;
    s)
       SLEEP="${OPTARG}"
       ;;
    S)
       SENDER="${OPTARG}"
       ;;
    T)
       TAS="${OPTARG}"
       ;;
    t)
       THREADS="${OPTARG}"
       ;;
    v)
       LGLVL="-v ${OPTARG}"
       ;;
    V)
       SRVOPTS="${OPTARG}"
       ;;
    W)
       WAITAFTERMTACHECK="${OPTARG}"
       ;;
    x)
       TTAS="${OPTARG}"
       ;;
    X)
       TRQS="${OPTARG}"
       ;;
    Y)
       SINKOPTS="${SINKOPTS} ${OPTARG}"
       ;;
    Z)
       SOURCE="${OPTARG}"
       ;;
    z)
       LOGERROR="${LOGERROR} ${OPTARG}"
       ;;
    ?)
       echo "$0: unknown option ${FLAG}"
       echo "$0: options:"
       echo "-1         send USR1 signal to all MTS components before shutdown"
       echo "-3 socket  also start LMTP sink (smtps3) listening on socket"
       echo "-A n       expected number of mails sent (source)"
       echo "-B opts    use opts for smtps3 (requires -3 socket)"
       echo "-b         use -r for t-edbr-0"
       echo "-C opts    additional SMTP client options"
       echo "-c opts    additional options for source (sender) program"
       #echo "-d n      debug level"
       echo "-D         do not initialize test environment"
       echo "-E n       expected number of recipients (sink)"
       echo "-e         turn off edbr check"
       echo "-F port    use port for SMTP source program"
       echo "-I         do not remove IBDB"
       echo "-i n       initial delay for retries in QMGR scheduler"
       echo "-K file    alternative to -O for tests which depend on ordering"
       echo "-m n       send n messages in separate sessions"
       echo "-M         do not remove defedb"
       #echo "-n n      n transactions are expected"
       echo "-o n       after n seconds accept all mails in SMTP sink"
       echo "-O file    file contains expected results from t-edbr-0"
       echo "-Q opts    options for QMGR"
       echo "-R address recipient address"
       echo "-r         use SMTP sink smtps3"
       echo "-s n       sleep n seconds before terminating system"
       echo "-S address sender address"
       #echo "-T n      n transactions"
       #echo "-t n      n threads"
       echo "-v n       set loglevel to n"
       echo "-V opts    additional SMTP server options"
       echo "-W n       wait n seconds after checking that SMTP server is running"
       echo "-X n       n requests are expected (sink)"
       echo "-Y opts    options for SMTP sink"
       echo "-Z source  source (sender) program"
       echo "-z log     allow ERROR to show up in logfile log [a, c, q, s]"
       exit 1
       ;;
  esac
done
shift `expr ${OPTIND} - 1`

#
# setup/initialize system
. ${SD}/mta-setup.sh
#

# set SRCPORT if not done yet; this must be done after mta-setup
if test "X"${SRCPORT} = X
then
  SRCPORT=${SRVPORT}
fi

killit()
{
if test -s ${PIDS}
then
  # stop MTA components
  for i in `cat ${PIDS}`
  do
    kill ${i}
  done
fi
# ----------------
# thanks kids for not following the POSIX standard...
if test X"`uname`" = "XLinux"
then
  killall qmgr smar
fi
if test X"${SRVPID}" != "X"
then
  kill -TERM ${SRVPID}
fi
if test X"${LMTPPID}" != "X"
then
  kill -TERM ${LMTPPID}
fi
exit 1
}

SRVPID=""
LMTPPID=""
# store the PIDs for the started programs
rm -f ${PIDS}
trap killit 2 15

SRCPRG=${STE}/smtpc
if test X"${SOURCE}" != "X"
then
  SRCPRG=${STE}/${SOURCE}
fi

# start SMTP sink
${STE}/${SINK} ${PIPELINING} ${SINKOPTS} ${OKDELAY} -i -l . -b ${H}:${SNKPORT} > ${SRVL} 2>&1 &
status=$?
SRVPID=$!
if test "x${status}" != "x0"
then
  echo "$0: FAIL: cannot start server ${status}"
  exit ${status}
fi
sleep 1

# start LMTP sink
if test X"${LMTPSINK}" != "X" -a X"${LMTPDIR}" != "X"
then
${STE}/smtps3 ${PIPELINING} -C ${LMTPDIR} ${LMTPSINKOPTS} -L ${LMTPSINK} > ${LMTPL} 2>&1 &
status=$?
LMTPPID=$!
if test "x${status}" != "x0"
then
  echo "$0: FAIL: cannot start LMTP sink ${status}"
  exit ${status}
fi
sleep 1
fi

# version of output files
V=0
# path to programs
P=..
# file to override settings
# test -f options && . ./options
# go for it
${P}/smar/smar ${SMAROPT} ${D} ${LGLVL} $@ > a${V}.log 2>&1 &
echo $! >> ${PIDS}
#${CHKD}/qmgr-running.sh "${SMARSOCK}" || killit
${P}/qmgr/qmgr -w ${W4S} -W ${W4C} ${D} ${LGLVL} -D ${DELAY_TRY} ${QOPTS} $@ > q${V}.log 2>&1 &
echo $! >> ${PIDS}
# wait a bit for startup such that the sockets are created
#export SMTPCSOCK
#${CHKD}/qmgr-running.sh || killit

${P}/smtpc/smtpc -w ${W4S} ${LGLVL} ${COPTS} -l . -P ${SNKPORT} ${LMTP} ${D} $@ > c${V}.log 2>&1 &
echo $! >> ${PIDS}
${P}/smtps/smtps -w ${W4S} ${LGLVL} ${SRVOPTS} -l . ${D} -b localhost:${SRVPORT} $@ > s${V}.log 2>&1 &
echo $! >> ${PIDS}

# check that the MTA is running
export SRVPORT P
${CHKD}/mta-running.sh || killit
if test "X"${WAITAFTERMTACHECK} != "X" -a ${WAITAFTERMTACHECK} -ge 1
then
sleep ${WAITAFTERMTACHECK}
fi

if test X"${RCPTADDR}" != X
then
  if ${SRCPRG} ${SRCOPTS} -r ${H}:${SRCPORT} -f "${SENDER}" ${RCPTADDR} -n "${RCPTS}" -T "${TAS}" -t "${THREADS}" > ${CLTL} 2>&1
  then
    :
  else
    cat ${CLTL}
    ERRS=`expr ${ERRS} + 1 `
  fi
elif test ${MULTIPLE} -ge 2
then
  r=1
  while test ${r} -le ${MULTIPLE}
  do
  # send messages concurrently
    ${SRCPRG} ${SRCOPTS} -r ${H}:${SRCPORT} -fa-${r}@b.c -Rr-${r}@x.z -n "${RCPTS}" -T "${TAS}" -t "${THREADS}" > ${CLTL}-${r} 2>&1 &
    r=`expr ${r} + 1 `
  done
elif test "${RCPTS}" -ne 1 -a "${SENDER}" != "${SENDER_DFLT}"
then
  if ${SRCPRG} ${SRCOPTS} -r ${H}:${SRCPORT} -f "${SENDER}" -n "${RCPTS}" -T "${TAS}" -t "${THREADS}" > ${CLTL} 2>&1
  then
    :
  else
    cat ${CLTL}
    ERRS=`expr ${ERRS} + 1 `
  fi
else
  if test X"${EDBOKIN}" = X
  then
  cat > ${EDBOK} <<EOF
got transaction
	mail=<${SENDER_DFLT}>
	rcpts_tot=1
	rcpts_left=1
	rcpts_temp=1
	rcpts_perm=0
	state=451
	aqt_rcpts_ar=0
got recipient
	rcpt=<451@x.z>
	aqr_rcpt_idx=0
	aqr_da_idx=0
	aqr_status=451
EOF
  fi
  # send message
  if ${SRCPRG} ${SRCOPTS} -r ${H}:${SRCPORT} -f${SENDER_DFLT} -R451@x.z -n "${RCPTS}" -T "${TAS}" -t "${THREADS}" > ${CLTL} 2>&1
  then
    :
  else
    cat ${CLTL}
    ERRS=`expr ${ERRS} + 1 `
  fi
fi

sleep ${SLEEP}

if ${SNDUSR1}
then
  for i in `cat pids`
  do
    kill -USR1 ${i}
  done
fi

# ----------------
# stop MTA components
for i in `cat pids`
do
  kill ${i}
done
# ----------------
# thanks kids for not following the POSIX standard...
if test X"`uname`" = "XLinux"
then
  killall qmgr smar
fi

# send SMTP server a signal to dump its state
if kill -USR1 ${SRVPID}
then
  :
else
  echo "kill -USR1 failed: $?"
  exit 1
fi
sleep 2

# send LMTP server a signal to dump its state
if test X"${LMTPPID}" != "X"
then
  if kill -USR1 ${LMTPPID}
  then
    :
  else
    echo "kill -USR1 failed: $?"
    exit 1
  fi
  sleep 2
fi

# check state of SMTP server
if ${GREP} "^Address *${H}:${P}" ${SRVL} > /dev/null
then
  :
else
  echo "failed to listen on ${H}:${P}"
  ERRS=`expr ${ERRS} + 1 `
fi
if ${GREP} "^Thread.* 8/" ${SRVL} > /dev/null
then
  :
else
  echo "failed to start threads"
  ERRS=`expr ${ERRS} + 1 `
fi
# the number of requests can vary based on the scheduler...
RQS=`${GREP} "^Requests served *" ${SRVL} | head -n1 | sed -e 's/^Requests served *//'`
if test "${RQS}" -lt "${TRQS}"
then
  echo "wrong number of requests in SMTP sink: expected=${TRQS} got=${RQS} (-X)"
  ERRS=`expr ${ERRS} + 1 `
fi
if egrep "^Recipients *${TR}$" ${SRVL} > /dev/null
then
  :
else
  echo "wrong number of recipients in SMTP sink: expected=${TR} (-E)"
  ${GREP} "^Recipients" ${SRVL}
  ERRS=`expr ${ERRS} + 1 `
fi

# check SMTP client (source)
if test ${MULTIPLE} -ge 2
then
  r=1
  msgs=0
  while test ${r} -le ${MULTIPLE}
  do
    SENT=`${GREP} "total=" ${CLTL}-${r} | sed -e 's/^.*total=\([0-9]*\) .*/\1/'`
    msgs=`expr ${SENT} + ${msgs}`
    r=`expr ${r} + 1 `
  done
  if test ${TOTAL} = ${msgs}
  then
    :
  else
    echo "wrong number of mails sent: expected=${TOTAL}, got=${msgs} (-A)"
    ERRS=`expr ${ERRS} + 1 `
  fi

  if test ${TTAS} -ge 0
  then
    # the number of transactions can vary based on the scheduler...
    TASS=`${GREP} "^Transactions *" ${SRVL} | sed -e 's/^Transactions *//'`
    if test "${TASS}" -lt "${TTAS}"
    then
      echo "wrong number of transactions: expected=${TTAS} got=${TASS} (-A)"
      ERRS=`expr ${ERRS} + 1 `
    fi
  fi

else
if ${GREP} "total=${TOTAL} " ${CLTL} > /dev/null
then
  :
else
  echo "wrong number of mails sent: expected=${TOTAL} (-A)"
  ${GREP} "total=" ${CLTL}
  ERRS=`expr ${ERRS} + 1 `
fi
fi

# ----------------
kill -TERM ${SRVPID}
if test X"${LMTPPID}" != "X"
then
  kill -TERM ${LMTPPID}
fi

# ----------------
if ${IDBR0} >/dev/null
then
  if test "${ERRS}" = "0"
  then
    rm -f ibdd/ibd00000000*
  fi
else
  echo "t-idbr-0 failed"
  ERRS=`expr ${ERRS} + 1 `
fi

# ----------------
# file for EDBR check provided?
if test X"${EDBOKIN}" = X
then
cp /dev/null ${EDBOK}
else
cp ${EDBOKIN} ${EDBOK}
fi

if ${EDBRCHK}
then
if ${EDBR} ${EDBROPT} > ${EDBROUT} 2>&1
then
  egrep -v 'tests completed successfully|time|(id|cdb|srv_ip4)=' ${EDBROUT} > ${EDBOUT} 2>&1
  if diff ${EDBOUT} ${EDBOK}
  then
    :
  else
    if test X"${EDBOKIN2}" != X
    then
      if diff ${EDBOUT} ${EDBOKIN2}
      then
        :
      else
        ERRS=`expr ${ERRS} + 1 `
      fi
    else
      ERRS=`expr ${ERRS} + 1 `
    fi
  fi
else
  echo "${EDBR} failed"
  ERRS=`expr ${ERRS} + 1 `
fi
fi

# ----------------
for i in a c q s
do
  PAT=${ERRORPAT}
  if echo X"${LOGERROR}" | ${GREP} ${i} >/dev/null
  then
    PAT=${FATALPAT}
  fi
  if egrep "${PAT}" ${i}${V}.log
  then
    ERRS=`expr ${ERRS} + 1 `
  fi
done

# ----------------
if test "${ERRS}" = "0"
then
  exit 0
else
  echo "${ERRS} error(s)"
  exit 1
fi


syntax highlighted by Code2HTML, v. 0.9.1