#!/bin/sh
# $Id: t-bounce-11.sh,v 1.1 2007/08/09 15:28:15 ca Exp $
#
# ----------------------------------------
# test bounces; multi-line response
# uses MIME format for DSN
# run smtp1.sh with different parameters
# ----------------------------------------
#
test -s stop && exit 1
#

SD=`dirname $0`
if test "${SD}" = "."
then
   SD=../chkmts
fi
. ${SD}/common.sh

I=""
ERRS=0
OK="t-bounce-11.ok"
OUT="t-bounce-11.out"

if test X"`uname`" != "XOSF1"
then
while getopts f:s:V FLAG
do
  case "${FLAG}" in
    s) SKIP="${SKIP} ${OPTARG}";;
    V) VERBOSE=true;;
  esac
done
shift `expr ${OPTIND} - 1`
fi

# cd to working directory for alias file!
cd ../qmgr || exit 1

# create aliases
${CAT} > ${ALI} <<EOF
abuse:	local:
postmaster:	local:
user:	local:
sender: local:
s954: local:
EOF
# create map
rm -f ${ALIMAP}
${MM} < ${ALI}
rm -f ${MTMAP}

# run some test programs

# ----------------------------------------
# permanent failure from SMTP sink; create a bounce; check content
TEST=1
if echo "${SKIP} X" | ${GREP} -v " ${TEST} " > /dev/null
then

${CAT} > ${SMXCNF} <<EOF
qmgr { Log_Level = 12; wait_for_server=4; wait_for_client=4;
DSN_handling { flags =  {MIME_format }; }
}
smar { Log_Level = 12; nameserver = ${NS}; }
smtpc { Log_Level = 12; remote_port=${SNKPORT}; wait_for_server=4; }
smtps {
Log_Level = 14;
daemon_address = localhost:${SRVPORT};
wait_for_server=4;
}
EOF

ERR=false
if ${SHELL} ${SD}/smtpcnf.sh -3 lmtpsock -E 0 -A 1 -X 1 -e 1 -S"s954@local.dom" -Ruser@local.host
then
  if test -s ${B1}
  then
     # get the MIME delimiter
     DELIM=`${EGREP} '^Content-Type: multipart/mixed; boundary=' ${B1} |\
	${SED} -e 's|^Content-Type: multipart/mixed; boundary="\([^"]*\)".*$|\1|'`

     # remove some lines that vary between tests/systems
     ${EGREP} -v '^Date: ' ${B1} |\
	 ${SED} -e '/^Received:/,$d' \
		-e 's/
$//' -e 's/Mailer-Daemon@.*/Mailer-Daemon@/' \
		> ${OUT}

     ${CAT} > ${OK} <<EOF
Return-Path: <>
From: Mailer-Daemon@
Subject: Undeliverable mail
To: <s954@local.dom>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="${DELIM}"

This is a MIME-encapsulated message

--${DELIM}

${DSN_ERR}

Recipient:
<user@local.host>
Remote-MTA: 127.0.0.1
Reason:
554 5.7.0 multi line last one with some other text
during MAIL


--${DELIM}
Content-Type: message/rfc822

EOF
    if ${DIFF} -w ${OK} ${OUT} >/dev/null
    then
      :
    else
      ERR=true
    fi

  else
     ERR=true
  fi
else
  ERR=true
fi
if ${ERR}
then
   echo "$0: test ${TEST} failed"
   ERRS=`expr ${ERRS} + 1 `
   test X"${MTA_STOPONERROR}" != X && exit 1
fi
fi




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


syntax highlighted by Code2HTML, v. 0.9.1