#!/bin/sh
#
# $Id: test-indimail-mta,v 1.83 2025-02-02 16:41:13+05:30 Cprogrammer Exp mbhangui $
#
start=$(date +'%s')
user=$(whoami)
testuser=tuser
blockeduser=blocked
outarchive=outarchive
incarchive=incarchive
myuid=$(id -u)
mygid=$(id -g)
local_domain1=local.indimail-local1.org
local_domain2=local.indimail-local2.org
remote_domain1=remote.indimail-remote1.org
remote_domain2=remote.indimail-remote2.org
real_domain=argos.indimail.org
srs_domain=srs.indimail.org
vdomain=virtual.indimail.org
ext_domain=gmail.com
dkim_key=/etc/indimail/control/domainkeys/$real_domain/$(echo $real_domain | cut -d. -f1)
#
#
verbose=0
testdir=/tmp/qmail-test
maildir=$testdir/$user/Maildir
servicedir=$testdir/service
sysconfdir=$testdir/etc/indimail
cntrldir=$sysconfdir/control
certdir=$sysconfdir/certs
qmaildir=$testdir/indimail
logdir=$testdir/logs
bindir=/usr/bin
sbindir=/usr/sbin
libexecdir=/usr/libexec/indimail
tmpdir=$testdir/tmp
qmail_newu=$sbindir/qmail-newu
qmail_start=$sbindir/qmail-start
slowq_start=$sbindir/slowq-start
qmta_send=$sbindir/qmta-send
qmail_inject=$bindir/qmail-inject
qmail_smtpd=$sbindir/qmail-smtpd
qmail_qmqpd=$sbindir/qmail-qmqpd
qmail_qmtpd=$sbindir/qmail-qmtpd
qmail_qmqpc=$sbindir/qmail-qmqpc
qmail_remote=$sbindir/qmail-remote
qmail_getpw=$sbindir/qmail-getpw
svscan=$sbindir/svscan
surblqueue=$sbindir/surblqueue
sys_pwd=$sbindir/sys-checkpwd
svctool=$sbindir/svctool
setuidgid=$bindir/setuidgid
tcpserver=$bindir/tcpserver
e822header=$bindir/822header
e822body=$bindir/822body
qmail_queue=$sbindir/qmail-queue
qmail_multi=$sbindir/qmail-multi
dkim=$bindir/dkim
qmail_dkim=$sbindir"/qmail-dkim"
dknewkey=$bindir/dknewkey
bogofilter=$bindir/bogofilter
srsfilter=$bindir/srsfilter
filterit=/usr/bin/filterit
autoresponder=$bindir/autoresponder
envdir=$bindir/envdir
multilog=$sbindir/multilog
s_nail=/usr/bin/s-nail
smtp_port=2050
qmqp_port=2628
qmtp_port=2209
smtp_socket=$testdir/run/smtpd
sleep_int=0.5
sleep_restart=1
HOSTNAME=$(uname -n)
domainkey_dir=$cntrldir/domainkeys
real_domain_rsa2048=/etc/indimail/control/domainkeys/argos.indimail.org/argos
real_domain_rsa4096=/etc/indimail/control/domainkeys/argos.indimail.org/b4096
real_domain_ed25519=/etc/indimail/control/domainkeys/argos.indimail.org/ed25519
nokeyerror=35
out=/dev/null

trap do_cleanup_failed 2 EXIT

print_pct()
{
	pct=$(echo $tcount $total_tests | awk '{printf("%0.2f\n", ($1 * 100)/$2)}')
	printf "%90s Test No %5d [%6.2f   %%]" " " $tcount $pct
}

terminate_send()
{
	if [ -n "$2" ] ; then
		shift
		sudo kill $*
		sleep $sleep_int
	fi
	case $1 in
		"qmail-send")
		unset send_pid
		sudo /bin/rm -f $logdir/qmail-send/qmail-send.log $logdir/qmail-send/current
		;;
		"qmail-send-2")
		unset send_pid2
		sudo /bin/rm -f $logdir/qmail-send/qmail-send2.log $logdir/qmail-send/current
		;;
		"slowq-send-1")
		unset slowq_pid1
		sudo /bin/rm -f $logdir/slowq-send/slowq1-send.log $logdir/slowq-send/current
		;;
		"slowq-send"-2)
		unset slowq_pid2
		sudo /bin/rm -f $logdir/slowq-send/slowq2-send.log $logdir/slowq-send/current
		;;
		"qmta-send-1")
		unset qmta_pid1
		sudo /bin/rm -f $logdir/qmta-send/qmta1-send.log $logdir/qmta-send/current
		;;
		"qmta-send-2")
		unset qmta_pid2
		sudo /bin/rm -f $logdir/qmta-send/qmta2-send.log $logdir/qmta-send/current
		;;
	esac
}

do_cleanup_failed()
{
	if [ $failed -eq 0 ] ; then
		return 0
	fi
	svpid=$(sed -n '$p' $servicedir/.svscan.pid 2>/dev/null)
	if [ -n "$svpid" ] ; then
		shutdown_svscan
		sleep $sleep_int
	else
		smtp_pid=$(/bin/ps -ef|grep $smtp_port|grep qmail-smtpd|awk '{print $2}')
		if [ -z "$smtp_pid" ] ; then
			smtp_pid=$(/bin/ps -ef|grep $smtp_socket|grep qmail-smtpd|awk '{print $2}')
		fi
		if [ -n "$smtp_pid" ] ; then
			terminate_smtp_qmtp $smtp_pid
			smtp_pid=""
		fi
		if [ -n "$qmtp_pid" ] ; then
			terminate_smtp_qmtp $qmtp_pid
			qmtp_pid=""
		fi
		[ -z "$send_pid" ] && send_pid=$(get_send_pid qmail-send $testdir/queue)
		terminate_send qmail-send "$send_pid" && send_pid=""

		[ -z "$slowq_pid1" ] && slowq_pid1=$(get_send_pid slowq-send $testdir/slowq1)
		terminate_send slowq-send-1 "$slowq_pid1" && slowq_pid1=""

		[ -z "$slowq_pid2" ] && slowq_pid2=$(get_send_pid slowq-send $testdir/slowq2)
		terminate_send slowq-send-2 "$slowq_pid2" && slowq_pid2=""

		[ -z "$qmta_pid1" ] && qmta_pid1=$(get_send_pid qmta-send $testdir/qmta1)
		terminate_send qmta-send "$qmta_pid1" && qmta_pid1=""

		[ -z "$qmta_pid2" ] && qmta_pid2=$(get_send_pid qmta-send $testdir/qmta2)
		terminate_send qmta-send "$qmta_pid2" && qmta_pid2=""
	fi
	sudo /bin/rm -rf $servicedir $logdir \
		$testdir/queue $testdir/slowq1 $testdir/slowq2 $testdir/qmta $testdir/qmta1 $testdir/qmta2 \
		$testdir/domainqueue $testdir/bin $tmpdir $testdir/run $testdir/sleep.out $testdir/svscan.out \
		$testdir/tcpclient.smtp $testdir/.vacation.dir/* $testdir/.vacation.msg \
		$testdir/$user/Maildir/new/* $testdir/$testuser/Maildir/new/* \
		$testdir/$testuser/Maildir/.Quarantine $testdir/$testuser/Maildir/.Spam \
		$testdir/$blockeduser/Maildir $qmaildir/domains \
		$qmaildir/alias/Maildir/new/* $qmaildir/autoturn/$vdomain/Maildir/new/* \
		$qmaildir/autoturn/$vdomain/Maildir/cur/* $sysconfdir/users $testdir/variables
	sudo /bin/rm -f  $cntrldir/mailarch $cntrldir/accesslist $cntrldir/from.envrules \
		$cntrldir/badmailfrom.custom $cntrldir/dkimkeys $cntrldir/smtproutes $cntrldir/recipients \
		$cntrldir/remote-auth $cntrldir/remote_auth.cdb $cntrldir/filterargs

	if [ -d $maildir ] ; then
		find $maildir -type f -exec /bin/rm -f {} \;
	fi
	if [ -d $testdir/$testuser/Maildir ] ; then
		find $testdir/$testuser/Maildir -type f -exec /bin/rm -f {} \;
	fi
	if [ -d $qmaildir/alias/Maildir ] ; then
		sudo find $qmaildir/alias/Maildir -type f -exec /bin/rm -f {} \;
	fi
}

check_mail_count()
{
	no_delete=0
	tx1=$(date +"%s.%4N")
	if [ $# -ne 2 ] ; then
		echo "check_mail_count dir count" 1>&2
		failed=1
		exit 1
	fi
	dir=$1
	count=0
	ret=1
	mail_file=""
	while true
	do
		mcount=$(ls $dir/new/* $dir/cur/* 2>/dev/null | wc -l)
		if [ $mcount -eq $2 ] ; then
			mail_file=$(ls -lt $dir/new|head -2|tail -1|awk '{print $9}')
			break
		fi
		sleep 0.5
		count=$(expr $count + 1)
		if [ $count -gt 10 ] ; then
			echo "check_mail_count: count=$count: Failed to receive Mail" 1>&2
			return 1
		fi
	done
	tx2=$(date +"%s.%4N")
	sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
	mail_file=$(ls -lt $dir/new|head -2|tail -1|awk '{print $9}')
	return 0
}

check_mail()
{
	no_delete=0
	tx1=$(date +"%s.%4N")
	if [ $# -eq 0 ] ; then
		dir=$maildir
	elif [ $# -eq 1 ] ; then
		dir=$1
	elif [ $# -eq 2 ] ; then
		dir=$1
		no_delete=1
	fi
	count=0
	ret=1
	mail_file=""
	while true
	do
		mcount=$(ls $dir/new 2>/dev/null | wc -l)
		if [ $mcount -gt 0 ] ; then
			mail_file=$(ls -lt $dir/new|head -2|tail -1|awk '{print $9}')
			if [ $no_delete -eq 0 ] ; then
				sudo /bin/rm -f $dir/new/$mail_file
			fi
			break
		fi
		sleep 0.5
		count=$(expr "$count" + 1)
		if [ $count -gt 10 ] ; then
			echo "check_mail: Failed to receive Mail" 1>&2
			tx2=$(date +"%s.%4N")
			sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
			return 1
		fi
	done
	tx2=$(date +"%s.%4N")
	sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
	if [ $no_delete -eq 0 ] ; then
		mail_file=$(ls -lt $dir/new|head -2|tail -1|awk '{print $9}')
	fi
	return 0
}

check_mail_header()
{
	if [ $# -lt 2 ] ; then
		echo "check_mail_header maildir string delete" 1>&2
		return 1
	fi
	dir=$1
	str=$2

	count=0
	ret=1
	tx1=$(date +"%s.%4N")
	header=""
	mail_file=""
	while true
	do
		mcount=$(ls $dir/new | wc -l)
		if [ $mcount -gt 0 ] ; then
			mail_file=$(ls -lt $dir/new|head -2|tail -1|awk '{print $9}')
			break
		fi
		sleep 0.5
		count=$(expr "$count" + 1)
		if [ $count -gt 10 ] ; then
			echo "Failed to receive Mail" 1>&2
			tx2=$(date +"%s.%4N")
			sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
			return 1
		fi
	done
	tx2=$(date +"%s.%4N")
	sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
	sudo chmod 644 $dir/new/$mail_file
	header=$($e822header -I $str < $dir/new/$mail_file)
	if [ $# -eq 3 ] ; then
		if [ $3 -eq 1 ] ; then
			sudo /bin/rm -f $dir/new/$mail_file
		fi
	fi
	mail_file=$(ls -lt $dir/new|head -2|tail -1|awk '{print $9}')
	return 0
}

remove_mail()
{
	if [ -d $maildir ] ; then
		find $maildir -type f -exec /bin/rm -f {} \;
	fi
	if [ -d $testdir/$testuser/Maildir ] ; then
		find $testdir/$testuser/Maildir -type f -exec /bin/rm -f {} \;
	fi
	if [ -d $testdir/$blockeduser/Maildir ] ; then
		find $testdir/$blockeduser/Maildir -type f -exec /bin/rm -f {} \;
	fi
}

shutdown_svscan()
{
	tx1=$(date +"%s.%4N")
	count=0
	svpid=$(sed -n '$p' $servicedir/.svscan.pid)
	while true
	do
		if [ -z "$svpid" -o ! -f $servicedir/.svscan.pid ] ; then
			break
		fi
		sudo kill $svpid
		sleep 0.5
		count=$(expr $count + 1)
		if [ $count -gt 10 ] ; then
			if [ -d $servicedir/.svscan/log ] ; then
				sudo svc -dx $servicedir/* $servicedir/*/log $servicedir/.svscan/log
			else
				sudo svc -dx $servicedir/* $servicedir/*/log
			fi
			echo "shutdown_svscan: count=$count: Failed to stop svsan" 1>&2
			return 1
		fi
	done
	if [ -d $servicedir/.svscan/log ] ; then
		sudo svc -dx $servicedir/* $servicedir/*/log $servicedir/.svscan/log
	else
		sudo svc -dx $servicedir/* $servicedir/*/log
	fi
	tx2=$(date +"%s.%4N")
	sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
	return 0
}

setup_maildir()
{
	if [ -d $maildir ] ; then
		find $maildir -type f -exec /bin/rm -f {} \;
	fi
	if [ -d $testdir/$testuser/Maildir ] ; then
		find $testdir/$testuser/Maildir -type f -exec /bin/rm -f {} \;
	fi
	if [ -d $testdir/$blockeduser/Maildir ] ; then
		find $testdir/$blockeduser/Maildir -type f -exec /bin/rm -f {} \;
	fi
	if [ -d $testdir/$incarchive ] ; then
		find $testdir/$incarchive/Maildir -type f -exec /bin/rm -f {} \;
	fi
	if [ -d $testdir/$outarchive ] ; then
		find $testdir/$outarchive/Maildir -type f -exec /bin/rm -f {} \;
	fi
	if [ -d $qmaildir/alias/Maildir ] ; then
		sudo find $qmaildir/alias/Maildir -type f -exec /bin/rm -f {} \;
	fi
	mkdir -p $cntrldir
	mkdir -p $sysconfdir/users
	for i in cur new tmp
	do
		mkdir -p $maildir/$i
		mkdir -p $testdir/$testuser/Maildir/$i
		mkdir -p $testdir/$testuser/Maildir/.Quarantine/$i
		mkdir -p $testdir/$testuser/Maildir/.Spam/$i
		mkdir -p $testdir/$blockeduser/Maildir/$i
		mkdir -p $testdir/blackholed/Maildir/$i
		mkdir -p $testdir/$incarchive/Maildir/$i
		mkdir -p $testdir/$outarchive/Maildir/$i
	done
}

setup_assign()
{
	t1=$(date +"%s.%4N")
	u=$(id -u qmaild)
	g=$(grep "^nofiles:" /etc/group | awk -F: '{print $3}')
	(
	echo "=$user:$user:$myuid:$mygid:$testdir/$user:::"
	echo "+$user-:$user:$myuid:$mygid:$testdir/$user:-::"
	echo "=$testuser:$testuser:$myuid:$mygid:$testdir/$testuser:::"
	echo "+$testuser-:$testuser:$myuid:$mygid:$testdir/$testuser:-::"
	echo "=$blockeduser:$blockeduser:$myuid:$mygid:$testdir/$blockeduser:::"
	echo "+$blockeduser-:$blockeduser:$myuid:$mygid:$testdir/$blockeduser:-::"
	echo "=$outarchive:$outarchive:$myuid:$mygid:$testdir/$outarchive:::"
	echo "+$outarchive-:$outarchive:$myuid:$mygid:$testdir/$outarchive:-::"
	echo "=$incarchive:$incarchive:$myuid:$mygid:$testdir/$incarchive:::"
	echo "+$incarchive-:$incarchive:$myuid:$mygid:$testdir/$incarchive:-::"
	echo "+$vdomain-:$vdomain:$myuid:$mygid:$qmaildir/autoturn/$vdomain:-::"
	echo "+autoturn-:autoturn:$u:$g:$qmaildir/autoturn:-::"
	echo "."
	) > $sysconfdir/users/assign
	$qmail_newu $sysconfdir/users
	if [ -n "$u" -a -n "$g" -a $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing create assign/user.cdb succeeded %61s [%.4f sec]\n" " " $secs
		print_pct
	else
		echo "u=$u, g=$g"
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s" " "
		printf "\r  testing create assign/user.cdb failed [%.4f sec]\n" $secs
		failed=1
		exit 1
	fi
}

setup_queue()
{
	t1=$(date +"%s.%4N")
	sudo queue-fix -s 23 -b 0 $testdir/queue
	if [ $? -ne 0 ] ; then
		echo "testing create qmail queue failed for $testdir/queue"
		failed=1
		exit 1
	fi
	sudo queue-fix -m -s 23 -b 0 $testdir/qmta1
	if [ $? -ne 0 ] ; then
		echo "testing create qmail queue failed for $testdir/qmta1"
		failed=1
		exit 1
	fi
	sudo queue-fix -s 23 -b 0 $testdir/qmta2
	if [ $? -ne 0 ] ; then
		echo "testing create qmail queue failed for $testdir/qmta2"
		failed=1
		exit 1
	fi

	for i in 1 2
	do
		sudo queue-fix -r -s 23 -b 0 $testdir/slowq1 && sudo queue-fix -r -s 23 -b 0 $testdir/slowq$i
		if [ $? -ne 0 ] ; then
			echo "testing create qmail queue failed for $testdir/slowq1|$testdir/slowq"$i""
			failed=1
			exit 1
		fi
	done
	t2=$(date +"%s.%4N")
	secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
	tcount=$(expr $tcount + 1)
	printf "\r  testing create qmail queue succeeded for qmail-send, slowq-send, qmta-send %27s [%.4f sec]\n" " " $secs
	print_pct
}

setup_config()
{
	t1=$(date +"%s.%4N")
	mkdir -p $testdir/setup
	(
	env CONTROLDIR=$cntrldir config-fast $HOSTNAME
	env CONTROLDIR=$cntrldir config-fast $local_domain1
	env CONTROLDIR=$cntrldir config-fast $local_domain2
	mkdir -p $qmaildir/alias
	sudo env QmailHOME=$qmaildir $svctool --cntrldir=$cntrldir --config=qmail \
		--postmaster=$user@$HOSTNAME
	sudo chown alias:qmail $qmaildir/alias
	sudo chmod 775 $qmaildir/alias
	)>$testdir/setup/setup.log 2>&1
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail configuration succeeded %64s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r%118s" " "
		printf "\r  testing qmail configuration failed [%.4f sec]\n" $secs
		cat $testdir/setup/setup.log|less
		failed=1
		exit 1
	fi
	if [ ! -f $certdir/servercert.pem ] ; then
		t1=$(date +"%s.%4N")
		sudo $svctool --certdir=$certdir    --config=cert \
			--postmaster=postmaster@$HOSTNAME --common_name=$HOSTNAME >$testdir/setup/setup.log 2>&1
		if [ -f $certdir/servercert.pem ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing create self signed certificate succeeded %53s [%.4f sec]\n" " " $secs
			print_pct
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			tcount=$(expr $tcount + 1)
			printf "\r%118s" " "
			printf "\r  testing create self signed certificate failed [%.4f sec]\n" $secs
			cat $testdir/setup/setup.log|less
			failed=1
			exit 1
		fi
	fi
	if [ ! -f $sysconfdir/bogofilter.cf ] ; then
		t1=$(date +"%s.%4N")
		cp /etc/indimail/bogofilter.cf.example $sysconfdir
		sudo $svctool --sysconfdir=$sysconfdir --config=bogofilter
		if [ -f $sysconfdir/bogofilter.cf ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing Create bogofilter configuration succeeded %52s [%.4f sec]\n" " " $secs
			print_pct
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing Create bogofilter configuration failed [%.4f sec]\n" $secs
		fi
	fi
	if [ ! -f $sysconfdir/wordlist.db ] ; then
		cp /etc/indimail/wordlist.db $sysconfdir
	fi
	sudo chown -R $user $cntrldir
	tr -dc 'A-Za-z0-9' </dev/urandom | head -c 16 > $cntrldir/batvkey
	echo ./Maildir/                           > $cntrldir/defaultdelivery
	echo TLSv1_2:TLSv1_3                      > $cntrldir/tlsclientmethod
	echo TLSv1_2:TLSv1_3                      > $cntrldir/tlsservermethod
	echo blackholed@$HOSTNAME                 > $cntrldir/blackholedsender
	echo blackholed@$HOSTNAME                 > $cntrldir/blackholedrcpt
	echo 10                                   > $cntrldir/timeoutremote
	echo 10                                   > $cntrldir/timeoutsmtpd
	(
	echo @$ext_domain
	echo @$HOSTNAME
	echo @$local_domain1
	echo @$local_domain2
	echo @$remote_domain1
	echo @$remote_domain2
	echo @$srs_domain
	echo @$vdomain
	echo @etrn1.dom
	echo @etrn2.dom
	echo @atrn.dom
	# These are for helo to succeed / fail
	echo @israel
	echo @palestine
	echo @argos.indimail.org
	echo @serialsmtp
	echo @AutoTURN # for etrn, atrn
	)                                         > $cntrldir/nodnscheck
	(
	echo etrn1.dom
	echo etrn2.dom
	echo atrn.dom
	)                                         > $cntrldir/etrnhosts
	echo ".*:d:$testuser@$HOSTNAME:remote"    > $cntrldir/redirectremote
	(
	echo "etrn1.dom"
	echo "etrn2.dom"
	echo "atrn.dom"
	) >> $cntrldir/rcpthosts
	sort -u $cntrldir/rcpthosts -o $cntrldir/rcpthosts
	/bin/rm -f $cntrldir/libindimail
}

test_tcpclient_script()
{
	t1=$(date +"%s.%4N")
	(
	echo "#!/bin/sh"
	echo "exec 0<&6"
	echo "exec 1>&7"
	echo "read key"
	echo "greeting=\$(echo \$key | awk '{print \$1}')"
	echo "if [ \$greeting -ne 220 ] ; then"
	echo "	echo \"Greeting failed\" 1>&2"
	echo "	printf \"QUIT\r\n\""
	echo "fi"
	echo "printf \"EHLO\r\n\""
	echo "while true"
	echo "do"
	echo "	read line"
	echo "	echo \$line |grep \"250 \" >/dev/null"
	echo "	if [ \$? -eq 0 ] ; then"
	echo "		echo \$line 1>&2"
	echo "		break"
	echo "	fi"
	echo "	echo \$line 1>&2"
	echo "done"
	echo "printf \"MAIL FROM:<$user@$HOSTNAME>\r\n\" 1>&2"
	echo "printf \"MAIL FROM:<$user@$HOSTNAME>\r\n\""
	echo "read key"
	echo "echo \$key 1>&2"
	echo "code=\$(echo \$key |cut -c1-3)"
	echo "if [ \$code -ne 250 ] ; then"
	echo "	echo \"MAIL From failed: code=\$code\" 1>&2"
	echo "	exit 1"
	echo "fi"
	echo "printf \"RCPT TO:<$user@$HOSTNAME>\r\n\" 1>&2"
	echo "printf \"RCPT TO:<$user@$HOSTNAME>\r\n\""
	echo "read key"
	echo "echo \$key 1>&2"
	echo "code=\$(echo \$key |cut -c1-3)"
	echo "if [ \$code -ne 250 ] ; then"
	echo "	echo \"RCPT To failed: code=\$code\" 1>&2"
	echo "	exit 1"
	echo "fi"
	echo "printf \"DATA\r\n\" 1>&2"
	echo "printf \"DATA\r\n\""
	echo "read key"
	echo "echo \$key 1>&2"
	echo "code=\$(echo \$key |cut -c1-3)"
	echo "if [ \$code -ne 354 ] ; then"
	echo "	echo \"DATA command: code=\$code\" 1>&2"
	echo "	exit 1"
	echo "fi"
	echo "printf \"To: $user@$HOSTNAME\r\n\" 1>&2"
	echo "printf \"To: $user@$HOSTNAME\r\n\""
	echo "printf \"From: $user@$HOSTNAME\r\n\" 1>&2"
	echo "printf \"From: $user@$HOSTNAME\r\n\""
	echo "printf \"Subject: Test Message\r\n\" 1>&2"
	echo "printf \"Subject: Test Message\r\n\""
	echo "printf \"Date: \$(date -R)\r\n\" 1>&2"
	echo "printf \"Date: \$(date -R)\r\n\""
	echo "printf \"\r\n\" 1>&2"
	echo "printf \"\r\n\""
	echo "printf \"This is a test message\r\n\" 1>&2"
	echo "printf \"This is a test message\r\n\""
	echo "printf \".\r\n\" 1>&2"
	echo "printf \".\r\n\""
	echo "read key"
	echo "echo \$key 1>&2"
	echo "code=\$(echo \$key |cut -c1-3)"
	echo "if [ \$code -ne 250 ] ; then"
	echo "	echo \"SMTP Transaction failed: code=\$code\" 1>&2"
	echo "	exit 1"
	echo "fi"
	echo "printf \"QUIT\r\n\" 1>&2"
	echo "printf \"QUIT\r\n\""
	echo "read key"
	echo "echo \$key 1>&2"
	echo "exit 0"
	) > $testdir/tcpclient.smtp
	chmod +x $testdir/tcpclient.smtp
	case $1 in
		1)
		tcpclient -vDHR 127.0.0.1 $smtp_port $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
		;;
		2)
		if [ $# -eq 2 ] ; then
			if [ "$2" = "TLSv1_2" ] ; then
				t=$cntrldir/clientcipherlist
			elif [ "$2" = "TLSv1_3" ] ; then
				t=$cntrldir/clientciphersuite
			fi
			tcpclient -f $t -M $2 -vDHRn $certdir/servercert.pem 127.0.0.1 \
				$smtp_port $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
		else
			t=$cntrldir/clientciphersuite
			tcpclient -vDHRn $certdir/servercert.pem 127.0.0.1 \
				$smtp_port $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
		fi
		;;
		3)
		tcpclient -v $smtp_socket $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
		;;
		4)
		tcpclient -vn $certdir/servercert.pem $smtp_socket $testdir/tcpclient.smtp \
			> $logdir/tcpclient/tcpclient.log 2>&1
		;;
	esac
	/bin/rm -f $testdir/tcpclient.smtp
	if [ $# -eq 2 ] ; then
		tls="$2"
	else
		tls="auto"
	fi
	check_mail
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail doing SMTP with tcpclient TLS=%-7s succeeded %41s [%.4f sec]\n" "$tls" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail doing SMTPS with tcpclient TLS=$tls failed [$secs sec]"
		(
		echo "tcpclient.log"
		cat $logdir/tcpclient/tcpclient.log
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		)|less
		failed=1
		exit 1
	fi
}

create_qmail_getpw()
{
	if [ -x $testdir/bin/qmail-getpw ] ; then
		return 0
	fi
	(
	echo "#!/bin/sh"
	echo "uid=\$(grep \"^\$1\" $testdir/etc/passwd |awk -F: '{print \$3}')"
	echo "if [ -z \"\$uid\" -o \"\$uid\" = \"0\" ] ; then"
	echo "  l=\$(grep \"^alias:\" $testdir/etc/passwd | awk -F: '{print \$1\" \"\$3\" \"\$4\" \"\$6}')"
	/bin/echo "  echo \$l \$1 | awk '{print \$1\"\\0\"\$2\"\\0\"\$3\"\\0\"\$4\"\\0-\\0\"\$5\"\\0\"}'"
	echo "else"
	/bin/echo "  grep \"^\$1:\" $testdir/etc/passwd |awk -F: '{print \$1\"\\0\"\$3\"\\0\"\$4\"\\0\"\$6\"\\0\\0\\0\"}'"
	echo "fi"
	echo "if [ \$? -eq 0 ] ; then"
	echo "  exit 0"
	echo "else"
	echo "  exit 100"
	echo "fi"
	) > $testdir/bin/qmail-getpw
	chmod 755 $testdir/bin/qmail-getpw
	return $?
}

run_qmail_send()
{
	create_qmail_getpw
	if [ $# -eq 0 -o $# -gt 2 ] ; then
		echo "run_qmail_send qmail-send|slowq-send-1|slowq-send-2|qmta-send-1|qmta-send-2 [option]" 1>&2
		return 1
	fi
	if [ "$1" = "qmail-send" ] ; then
		start_pg=$qmail_start
		pg1=qmail-send
		pg2=slowq-send
		logfn=qmail-send.log
		queuedir=$testdir/queue
		todo_proc=0 # not used by qmail-send
		opt="-s"
		pcount=6
		send_pid=""
	elif [ "$1" = "slowq-send-1" ] ; then
		start_pg=$slowq_start
		pg1=slowq-send
		pg2=qmail-send
		logfn=slowq1-send.log
		queuedir=$testdir/slowq1
		todo_proc=1
		opt=""
		pcount=5
		slowq_pid1=""
	elif [ "$1" = "slowq-send-2" ] ; then
		start_pg=$slowq_start
		pg1=slowq-send
		pg2=qmail-send
		logfn=slowq2-send.log
		queuedir=$testdir/slowq2
		todo_proc=0
		opt=""
		pcount=4
		slowq_pid2=""
	elif [ "$1" = "qmta-send-1" ] ; then
		start_pg=$qmta_send
		pg1=qmta-send
		pg2=qmail-send
		logfn=qmta1-send.log
		queuedir=$testdir/qmta1
		todo_proc=0
		opt="-d"
		pcount=3
		qmta_pid1=""
	elif [ "$1" = "qmta-send-2" ] ; then
		start_pg=$qmta_send
		pg1=qmta-send
		pg2=qmail-send
		logfn=qmta2-send.log
		queuedir=$testdir/qmta2
		todo_proc=0
		opt="-dlrc"
		pcount=4
		qmta_pid2=""
	else
		echo "run_qmail_send qmail-send|slowq-send-1|slowq-send-2|qmta-send-1|qmta-send-2 [option]" 1>&2
		return 1
	fi
	t1=$(date +"%s.%4N")
	(
	mkdir -p $logdir/$pg1
	exec 1>$logdir/$pg1/$logfn
	exec 2>$logdir/$pg1/$logfn
	exec 0<&2
	unset QREGEX MATCH_RECIPIENT_DOMAIN MATCH_SENDER_DOMAIN
	if [ $# -eq 2 ] ; then
		case "$2" in
			1)
			QREGEX=1
			;;
			2)
			MATCH_RECIPIENT_DOMAIN=1
			MATCH_SENDER_DOMAIN=1
			;;
		esac
	fi
	/bin/rm -rf $testdir/variables
	mkdir -p $testdir/variables
	echo "/bin:/usr/bin:/usr/sbin"  > $testdir/variables/PATH
	echo "0"                        > $testdir/variables/BIGTODO
	echo "1"                        > $testdir/variables/USE_QPWGR
	echo "$cntrldir"                > $testdir/variables/CONTROLDIR
	echo "$certdir"                 > $testdir/variables/CERTDIR
	echo "23"                       > $testdir/variables/CONFSPLIT
	echo "$testdir/bin/qmail-getpw" > $testdir/variables/QMAILGETPW
	echo "$queuedir"                > $testdir/variables/QUEUEDIR
	echo "$sysconfdir/users"        > $testdir/variables/ASSIGNDIR
	echo ""                         > $testdir/variables/RELAYCLIENT
	echo "$todo_proc"               > $testdir/variables/TODO_PROCESSOR
	echo "$testdir/etc/passwd"      > $testdir/variables/PASSWD_FILE
	echo "$sbindir/spawn-filter"    > $testdir/variables/QMAILLOCAL
	echo "$sbindir/spawn-filter"    > $testdir/variables/QMAILREMOTE
	echo "$sbindir/rd-remote"       > $testdir/variables/QREMOTE

	[ -n "$QREGEX" ]                 && echo 1 > $testdir/variables/QREGEX                 || > $testdir/variables/QREGEX
	[ -n "$MATCH_RECIPIENT_DOMAIN" ] && echo 1 > $testdir/variables/MATCH_RECIPIENT_DOMAIN || > $testdir/variables/MATCH_RECIPIENT_DOMAIN
	[ -n "$MATCH_SENDER_DOMAIN" ]    && echo 1 > $testdir/variables/MATCH_SENDER_DOMAIN    || > $testdir/variables/MATCH_SENDER_DOMAIN
	# for ezmlm
	echo "$qmail_queue"      > $testdir/variables/EZMLMQUEUE
	echo "$sysconfdir/ezmlm" > $testdir/variables/EZMLM_ETC
	exec sudo envdir -c $testdir/variables $start_pg $opt ./Maildir/
	) &
	tx1=$(date +"%s.%4N")
	try_count=0
	while true
	do
		pid=$(get_send_pid $pg1 $queuedir)
		if [ "$1" = "qmail-send" ] ; then
			send_pid=$pid
		elif [ "$1" = "slowq-send-1" ] ; then
			slowq_pid1=$pid
		elif [ "$1" = "slowq-send-2" ] ; then
			slowq_pid2=$pid
		elif [ "$1" = "qmta-send-1" ] ; then
			qmta_pid1=$pid
		elif [ "$1" = "qmta-send-2" ] ; then
			qmta_pid2=$pid
		fi
		sleep $sleep_int
		if [ "$1" = "qmta-send-1" ] ; then
			count=$(sudo pstree -ac $qmta_pid1 | wc -l)
		elif [ "$1" = "qmta-send-2" ] ; then
			count=$(sudo pstree -ac $qmta_pid2 | wc -l)
		else
			count=$(ps -ef | grep -E -v $pg2 | grep -E "$pg1|todo-proc|slowq-todo|qmail-lspawn|qmail-rspawn|qmail-clean" | grep $queuedir | wc -l)
		fi
		if [ -n "$pid" -a -n "$count" -a -n "$count" ] ; then
			if [ $pid -gt 0 -a $count -eq $pcount ] ; then
				break
			fi
		fi
		try_count=$(expr $try_count + 1)
		if [ $try_count -gt 10 ] ; then
			break
		fi
	done
	tx2=$(date +"%s.%4N")
	sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
	if [ -z "$pid" ] ; then
		pid=0
	fi
	if [ -n "$pid" -a $pid -gt 0 -a -n "$count" -a $count -eq $pcount ] ; then
		if [ -z "$silent" ] ; then
			silent=0
		fi
		[ $silent -eq 1 ] && return 0
		t2=$(date +"%s.%4N")
		tcount=$(expr $tcount + 1)
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		case "$1" in
			"qmail-send")
			printf "\r  testing %-10s startup with external todo-processor succeeded %36s [%.4f sec]\n" $pg1 " " $secs
			;;
			"slowq-send-1")
			printf "\r  testing %-10s startup with internal todo-processor succeeded %36s [%.4f sec]\n" $pg1 " " $secs
			;;
			"slowq-send-2")
			printf "\r  testing %-10s startup without       todo-processor succeeded %36s [%.4f sec]\n" $pg1 " " $secs
			;;
			"qmta-send-1")
			printf "\r  testing %-10s startup with internal lrc daemons    succeeded %36s [%.4f sec]\n" $pg1 " " $secs
			;;
			"qmta-send-2")
			printf "\r  testing %-10s startup with external lrc daemons    succeeded %36s [%.4f sec]\n" $pg1 " " $secs
			;;
		esac
	else
		echo "testing $pg1 option ($1) startup failed "$pg1"_pid=$pid, count=$count, pcount=$pcount"
		echo "$pg1 log"
		cat $logdir/$pg1/$logfn
		failed=1
		exit 1
	fi
}

get_send_pid()
{
	count=0
	if [ $# -ne 2 ] ; then
		echo "get_send_pid qmail-send|slowq-send|qmta-send queuedir" 1>&2
		return
	fi
	pg=$1
	if [ "$1" = "qmail-send" ] ; then
		logfn="qmail-send.log"
	elif [ "$1" = "slowq-send-1" ] ; then
		logfn="slowq1-send.log"
	elif [ "$1" = "slowq-send-2" ] ; then
		logfn="slowq2-send.log"
	elif [ "$1" = "qmta-send" ] ; then
		if [ "$2" = "$testdir/qmta1" ] ; then
			logfn="qmta1-send.log"
		elif [ "$2" = "$testdir/qmta2" ] ; then
			logfn="qmta2-send.log"
		fi
		sudo fuser $2/lock/trigger 2>/dev/null|sed 's{ {{g'
		return $?
	fi
	sleep_secs=0
	while true
	do
		pid=$(/bin/ps -ef | grep $pg | grep $2 |grep -E -v "qmail-clean|grep" | awk '{print $2}')
		if [ -n "$pid" ] ; then
			break
		fi
		sleep 1
		count=$(expr $count + 1)
		if [ $count -gt 5 ] ; then
			if [ -f $logdir/$pg/$logfn ] ; then
				cat $logdir/$pg/$logfn 1>&2
				echo "Failed to start $pg" 1>&2
			fi
			return 1
		fi
	done
	echo $pid
}

test_qmail_inject()
{
	# $1 - From
	# $2 - To
	# #3 - queuedir
	for i in 1 2
	do
		t1=$(date +"%s.%4N")
		if [ $i -eq 1 ] ; then # $user present in /etc/passwd but not in assign
			echo "." > $sysconfdir/users/assign
		else # $user  is present in /etc/passwd and assign
			(
			echo "=$user:$user:$myuid:$mygid:$testdir/$user:::"
			echo "+$user-:$user:$myuid:$mygid:$testdir/$user:-::"
			echo "."
			) > $sysconfdir/users/assign
		fi
		$qmail_newu $sysconfdir/users
		(
		echo "From: $1"
		echo "To: $2"
		echo "Subject: Test"
		echo "Date: $(date -R)"
		echo
		echo "Test message"
		) | env - \
			QUEUEDIR=$3 \
			QMAILQUEUE=$qmail_queue \
			CONTROLDIR=$cntrldir \
			BIGTODO=0 \
			CONFSPLIT=23 \
			$qmail_inject $2
		check_mail
		if [ $? -eq 0 ] ; then
			q=$(basename $3)
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			tcount=$(expr $tcount + 1)
			if [ $i -eq 1 ] ; then
				printf "\r  testing qmail-inject queue %-6s /etc/passwd user, qmail send+receive succeeded %21s [%.4f sec]\n" $q " " $secs
			else
				printf "\r  testing qmail-inject queue %-6s qmail       user, qmail send+receive succeeded %21s [%.4f sec]\n" $q " " $secs
			fi
			print_pct
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			printf "\r%118s\n" " "
			if [ $i -eq 1 ] ; then
				echo "  testing qmail-inject queue $3 /etc/passwd user, qmail send+receive failed [$secs sec]"
			else
				echo "  testing qmail-inject queue $3 qmail user, qmail send+receive failed [$secs sec]"
			fi
			cat $logdir/qmail-send/qmail-send.log|less
			failed=1
			exit 1
		fi
	done
}

test_domainqueue()
{
	echo ".*:QUEUEDIR=$testdir/domainqueue" > $cntrldir/domainqueue
	(
	echo "$HOSTNAME:127.0.0.1:$smtp_port"
	echo "gmail.com:127.0.0.1:$smtp_port"
	) > $cntrldir/smtproutes
	sudo queue-fix -s 23 -b 0 $testdir/domainqueue
	# redirect remote mail from gmail.com to $testuser@$HOSTNAME
	echo "gmail.com:d:$testuser@$HOSTNAME:remote"  > $cntrldir/redirectremote
	(
	exec 1>$logdir/qmail-send/qmail-send2.log
	exec 2>&1
	exec 0<&2
	exec sudo env - PATH=/bin:/usr/bin:/usr/sbin \
			USE_QPWGR=1 \
			BIGTODO=0 \
			CONFSPLIT=23 \
			CONTROLDIR=$cntrldir \
			CERTDIR=$certdir \
			QUEUEDIR=$testdir/domainqueue \
			ASSIGNDIR=$sysconfdir/users \
			QMAILREMOTE=$sbindir/spawn-filter \
			QREMOTE=$sbindir/rd-remote \
			QREGEX=1 \
			PASSWD_FILE=$testdir/etc/passwd \
		$qmail_start -s ./Maildir/
	) &
	job_pid=$(jobs -p | tail -1)
	send_pid2=$(get_send_pid qmail-send $testdir/domainqueue)

	(
	echo
	echo "$$: Testing SMTP domainqueue"
	) > $tmpdir/mail.txt

	# Test SMTP domainqueue
	swaks -S --data - --to $user@$ext_domain --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" \
		< $tmpdir/mail.txt >$tmpdir/swaks.$$
	/bin/rm -f $tmpdir/mail.txt
	t1=$(date +"%s.%4N")
	check_mail $testdir/$testuser/Maildir 1 2>/dev/null
	ret=$?
	if [ -n "$mail_file" ] ; then
		body=$($e822body < $testdir/$testuser/Maildir/new/$mail_file)
		/bin/rm -f $testdir/$testuser/Maildir/new/$mail_file
	else
		body=""
	fi
	if [ "$body" = "$$: Testing SMTP domainqueue" -a $ret -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing SMTP         domainqueue succeeded %59s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		(
		echo "body=[$body], ret=$ret"
		echo "  testing SMTP domainqueue failed [$secs sec]"
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		echo "qmail-send.log"
		echo $logdir/qmail-send/qmail-send.log
		) |less
		/bin/rm -f $cntrldir/domainqueue $cntrldir/smtproutes
		terminate_send qmail-send-2 $send_pid2
		send_pid2=""
		sudo kill $job_pid 2>/dev/null
		failed=1
		exit 1
	fi

	# Test qmail-inject domainqueue
	/bin/rm -rf $testdir/variables
	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	echo $qmail_queue   > $testdir/variables/QMAILQUEUE
	do_qmail_inject "$user@$HOSTNAME" "$user@$ext_domain" "Testing qmail-inject domainqueue" "$$: Testing qmail-inject domainqueue"
	t1=$(date +"%s.%4N")
	check_mail $testdir/$testuser/Maildir 1 2>/dev/null
	ret=$?
	if [ -n "$mail_file" ] ; then
		body=$($e822body < $testdir/$testuser/Maildir/new/$mail_file)
		/bin/rm -f $testdir/$testuser/Maildir/new/$mail_file
	else
		body=""
	fi
	terminate_send qmail-send-2 $send_pid2
	send_pid2=""
	sudo kill $job_pid 2>/dev/null
	if [ "$body" = "$$: Testing qmail-inject domainqueue" -a $ret -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-inject domainqueue succeeded %59s [%.4f sec]\n" " " $secs
		print_pct
	else
		echo "body=[$body], ret=$ret"
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-inject domainqueue failed [$secs sec]"
		(
		echo "qmail-send.log"
		echo $logdir/qmail-send/qmail-send.log
		) |less
		/bin/rm -f $cntrldir/domainqueue $cntrldir/smtproutes
		failed=1
		exit 1
	fi
	/bin/rm -f $cntrldir/domainqueue $cntrldir/smtproutes
}

# usage
# do_qmail_inject from to subject body
do_qmail_inject()
{
	# $1 : From
	# $2 : To
	# $3 : Subject
	# $4 : Body
	(
	echo "From: $1"
	echo "To: $2"
	echo "Subject: $3"
	echo "Date: $(date -R)"
	echo "X-Remove-Header1: This header will be removed"
	echo "X-Remove-Header2: This header will also be removed"
	echo "X-Env-Header1: This is env-header1"
	echo "X-Env-Header2: This is env-header2"
	echo "X-Log-Test: Test Header"
	echo
	echo "$4"
	) > $tmpdir/mail.txt
	envdir -c $testdir/variables $qmail_inject -f$1 $2 <$tmpdir/mail.txt
	ret=$?
	/bin/rm -f $tmpdir/mail.txt
	return $ret
}

create_authsmtp()
{
	if [ -f $testdir/bin/authsmtp ] ; then
		return 0
	fi
	(
	echo "#!/bin/sh"
	echo "if [ \$# -lt 1 ] ; then"
	echo "	exit 2"
	echo "fi"
	/bin/echo "var=\`tr [\"\\0\"] [\"\\n\"] 0<&3\`"
	echo "user=\`echo \$var | cut -d ' ' -f1\`"
	echo "pass=\`echo \$var | cut -d ' ' -f2\`"
	echo "exec 4>$tmpdir/authsmtp.\$\$ 3<$tmpdir/authsmtp.\$\$"
	echo "/bin/rm -f $tmpdir/authsmtp.\$\$"
	echo "echo \"authsmtp: user=\$user, pass=\$pass\" 1>&2"
	/bin/echo "echo \$var | tr [\" \"] [\"\\0\"] 1>&4"
	echo "if [ \"\$user\" = \"$user@$HOSTNAME\" -a \"\$pass\" = \"abcd12345678\" ] ; then"
	echo "	exit 0"
	echo "elif [ \"\$user\" = \"$testuser@$HOSTNAME\" -a \"\$pass\" = \"abcd87654321\" ] ; then"
	echo "	exit 0"
	echo "fi"
	echo "exec \$*"
	) > $testdir/bin/authsmtp
	chmod +x $testdir/bin/authsmtp
}

start_tcpserver_smtp()
{
	t1=$(date +"%s.%4N")
	mkdir -p $testdir/smtpd/variables
	echo 1                 > $testdir/smtpd/variables/USE_QPWGR
	echo 0                 > $testdir/smtpd/variables/BIGTODO
	echo 1                 > $testdir/smtpd/variables/STARTTLS
	echo 23                > $testdir/smtpd/variables/CONFSPLIT
	echo $testdir/queue    > $testdir/smtpd/variables/QUEUEDIR
	echo $cntrldir         > $testdir/smtpd/variables/CONTROLDIR
	echo $certdir          > $testdir/smtpd/variables/CERTDIR
	echo $sysconfdir/users > $testdir/smtpd/variables/ASSIGNDIR
	echo $smtp_port        > $testdir/smtpd/variables/PORT
	echo 1                 > $testdir/smtpd/variables/ENABLE_CRAM
	echo 1                 > $testdir/smtpd/variables/DEBUG
	echo 1                 > $testdir/smtpd/variables/DEBUG_LOGIN
	echo 0                 > $testdir/smtpd/variables/SPAMEXITCODE
	echo 0                 > $testdir/smtpd/variables/REJECTSPAM
	echo 1                 > $testdir/smtpd/variables/MAKE_SEEKABLE
	echo ""                > $testdir/smtpd/variables/DKIMVERIFY
	echo 1                 > $testdir/smtpd/variables/BADHELOCHECK
	echo 0                 > $testdir/smtpd/variables/PENALTY
	echo "$sbindir/qmail-dkim $sbindir/qmail-spamfilter" > $testdir/smtpd/variables/QMAILQUEUE
	(
	if [ $# -eq 1 ] ; then
		if [ $1 -eq 4 ] ; then
			t="-4"
		else
			t=""
		fi
	else
		t=""
	fi
	sudo envdir -c $testdir/smtpd/variables \
		$tcpserver $t -u qmaild -g qmail -l $HOSTNAME -HR 0 $smtp_port $qmail_smtpd $HOSTNAME \
			$testdir/bin/authsmtp $sys_pwd /bin/false
	) > $logdir/smtpd/smtpd.log 2>&1 &

	count=0
	tx1=$(date +"%s.%4N")
	while true
	do
		smtp_pid=$(/bin/ps -ef|grep -v sudo|grep $smtp_port|grep qmail-smtpd|awk '{print $2}')
		if [ -n "$smtp_pid" ] ; then
			break
		fi
		sleep 1
	done
	tx2=$(date +"%s.%4N")
	sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
	sudo kill -0 $smtp_pid
	if [ $? -eq 0 ] ; then
		if [ $# -eq 0 ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing SMTP on port %4d smtp_port using tcpserver succeeded %40s [%.4f sec]\n" $smtp_port " " $secs
			print_pct
		fi
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing SMTP on port $smtp_port using tcpserver failed [$secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		)|less
		failed=1
		exit 1
	fi
}

start_tcpserver_smtp_turn()
{
	mkdir -p $testdir/smtpd/variables
	/bin/rm -f $testdir/smtpd/variables/*
	echo 1                 > $testdir/smtpd/variables/USE_QPWGR
	echo 0                 > $testdir/smtpd/variables/BIGTODO
	echo 23                > $testdir/smtpd/variables/CONFSPLIT
	echo $testdir/queue    > $testdir/smtpd/variables/QUEUEDIR
	echo $cntrldir         > $testdir/smtpd/variables/CONTROLDIR
	echo $sysconfdir/users > $testdir/smtpd/variables/ASSIGNDIR
	echo $smtp_port        > $testdir/smtpd/variables/PORT
	echo 1                 > $testdir/smtpd/variables/MAKE_SEEKABLE
	echo "$qmail_queue"    > $testdir/smtpd/variables/QMAILQUEUE
	(
	sudo envdir -c $testdir/smtpd/variables \
		$tcpserver -l $HOSTNAME -HR 0 $smtp_port sh -c "setuidgid -g qmail qmaild $qmail_smtpd;$testdir/bin/qmta"
	) > $logdir/smtpd/smtpd.log 2>&1 &

	count=0
	tx1=$(date +"%s.%4N")
	while true
	do
		smtp_pid=$(/bin/ps -ef|grep -v sudo|grep $smtp_port|grep qmail-smtpd|awk '{print $2}')
		if [ -n "$smtp_pid" ] ; then
			break
		fi
		sleep 1
	done
	tx2=$(date +"%s.%4N")
	sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
	sudo kill -0 $smtp_pid
	ret=$?
	return $ret
}

start_tcpserver_smtp_unix()
{
	t1=$(date +"%s.%4N")
	printf "\r%118s\n" " "
	printf "\rStarting SMTP on socket %s using tcpserver\n" $smtp_socket
	(
	env - \
		USE_QPWGR=1 \
		STARTTLS=1 \
		BIGTODO=0 \
		CONFSPLIT=23 \
		QUEUEDIR=$testdir/queue \
		CONTROLDIR=$cntrldir \
		CERTDIR=$certdir \
		ENABLE_CRAM=1 \
		DEBUG=1 \
		DEBUG_LOGIN=1 \
		QMAILQUEUE="$sbindir/qmail-dkim $sbindir/qmail-spamfilter" \
		SPAMFILTER="$bogofilter -p -d $sysconfdir" \
		SPAMEXITCODE=0 \
		REJECTSPAM=0 \
		MAKE_SEEKABLE=1 \
		DKIMVERIFY="" \
		$tcpserver $smtp_socket $qmail_smtpd $HOSTNAME \
			$sys_pwd $testdir/bin/authsmtp /bin/false
	) > $logdir/smtpd/smtpd.log 2>&1 &

	smtp_pid=$(/bin/ps -ef|grep $smtp_socket|grep qmail-smtpd|awk '{print $2}')
	kill -0 $smtp_pid
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing SMTP on socket %-26s using tcpserver succeeded %26s [%.4f sec]\n" $smtp_socket " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "  testing SMTP on socket $smtp_socket using tcpserver failed [$secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		)|less
		failed=1
		exit 1
	fi
}

start_tcpserver_qmtp()
{
	t1=$(date +"%s.%4N")
	(
	sudo env - \
		USE_QPWGR=1 \
		BIGTODO=0 \
		STARTTLS=1 \
		CONFSPLIT=23 \
		QUEUEDIR=$testdir/queue \
		CONTROLDIR=$cntrldir \
		QMAILQUEUE="$sbindir/qmail-queue" \
		$tcpserver -u qmaild -g qmail -l $HOSTNAME -HR 0 $qmtp_port $qmail_qmtpd
	) > $logdir/qmtpd/qmtpd.log 2>&1 &

	count=0
	tx1=$(date +"%s.%4N")
	while true
	do
		qmtp_pid=$(/bin/ps -ef|grep -v sudo|grep $qmtp_port|grep qmail-qmtpd|awk '{print $2}')
		if [ -n "$qmtp_pid" ] ; then
			break
		fi
		sleep 1
	done
	tx2=$(date +"%s.%4N")
	sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
	sudo kill -0 $qmtp_pid
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing QMTP on port %4d qmtp_port using tcpserver succeeded %40s [%.4f sec]\n" $qmtp_port " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing QMTP on port $qmtp_port using tcpserver failed [$secs sec]"
		(
		echo "$logdir/smtpd/qmtpd.log"
		cat $logdir/qmtpd/qmtpd.log
		)|less
		failed=1
		exit 1
	fi
}

test_smtp_tls()
{
	t1=$(date +"%s.%4N")
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port -tls
	check_mail
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail using SMTP with    STARTTLS succeeded %51s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail using SMTP with STARTTLS failed [$secs sec]"
		(
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		)|less
		failed=1
		exit 1
	fi
}

test_smtp_notls()
{
	t1=$(date +"%s.%4N")
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port
	check_mail
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail using SMTP without STARTTLS succeeded %51s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail using SMTP without STARTTLS failed [$secs sec]"
		(
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		echo "qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		exit 1
	fi
}

test_smtp_dkimverify()
{
	t1=$(date +"%s.%4N")
	(
	echo "To: $user@$HOSTNAME"
	echo "From: $user@$real_domain"
	echo "Subject: Testing DKIM"
	echo "Date: $(date -R)"
	echo
	echo "This is an email to test DKIM"
	) > $testdir/mail1.txt
	(
	cat $testdir/mail1.txt|dkim -z 2 -s $dkim_key
	cat $testdir/mail1.txt && rm -f $testdir/mail1.txt
	) >$tmpdir/mail2.txt
	swaks --data - -n -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port < $tmpdir/mail2.txt
	check_mail_header $maildir "DKIM-Status" 0
	echo $header | grep "DKIM-Status: good" > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail using SMTP for DKIM verification succeeded %46s [%.4f sec]\n" " " $secs
		print_pct
		/bin/rm -f $tmpdir/mail1.txt $tmpdir/mail2.txt $maildir/new/$mail_file
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail using SMTP for DKIM verification failed [$secs sec]"
		(
		echo "mail.txt"
		cat $tmpdir/mail2.txt
		echo "----------------------------------------"
		echo "$maildir/new/$mail_file"
		cat $maildir/new/$mail_file
		echo "----------------------------------------"
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		)|less
		/bin/rm -f $tmpdir/mail1.txt $tmpdir/mail2.txt $maildir/new/$mail_file
		failed=1
		exit 1
	fi
}

test_smtp_spamfilter()
{
	t1=$(date +"%s.%4N")
	echo "$bogofilter -p -d $sysconfdir" > $testdir/smtpd/variables/SPAMFILTER
	terminate_smtp_qmtp $smtp_pid
	smtp_pid=""
	s1=$sleep_secs
	start_tcpserver_smtp 4
	sleep $sleep_int
	(
	echo "To: $user@$HOSTNAME"
	echo "From: $user@$HOSTNAME"
	echo "Subject: Testing DKIM"
	echo "Date: $(date -R)"
	echo
	echo "This is an email to test DKIM"
	) | swaks --data - -n -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port
	check_mail_header $maildir "X-Bogosity" 1
	echo $header| grep "X-Bogosity" > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $sleep_int $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail using SMTP  for spamfilter succeeded %52s [%.4f sec]\n" " " $secs
		print_pct
		/bin/rm -f $testdir/smtpd/variables/SPAMFILTER
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail using SMTP for spamfilter failed [$secs sec]"
		(
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		)|less
		failed=1
		/bin/rm -f $testdir/smtpd/variables/SPAMFILTER
		exit 1
	fi
	terminate_smtp_qmtp $smtp_pid
	smtp_pid=""
	start_tcpserver_smtp 4
}

test_smtps()
{
	t1=$(date +"%s.%4N")
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port --tls-on-connect
	check_mail
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		str="$1 succeeded"
		printf "\r  testing %-94s [%.4f sec]\n" "$str" $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing $1 failed [$secs sec]"
		(
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		)|less
		failed=1
		exit 1
	fi
}

# don't be like EXIM's infamous AUTH NTLM
test_smtp_invalid_auth()
{
	t1=$(date +"%s.%4N")
	for i in NTLM efgh
	do
		swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
			--a $i -au $user@$HOSTNAME -ap "abcd12345678" >$tmpdir/swaks.$$ 2>&1
		l1=$(sed -n 1p < $tmpdir/swaks.$$)
		if [ "$l1" != "\*\*\* EFGH is not a recognized auth type, skipping" \
			-a "$l1" != "\*\*\* Auth not attempted, requested type not available" ] ; then
			break
		fi
	done
	/bin/rm -f $tmpdir/swaks.$$
	if [ "$l1" != "\*\*\* EFGH is not a recognized auth type, skipping" \
		-a "$l1" != "\*\*\* Auth not attempted, requested type not available" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail using SMTP with invalid AUTH type rejection succeeded %35s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "  testing mail using SMTP with invalid AUTH type rejection failed [$secs sec]"
		echo $l1
		failed=1
		exit 1
	fi
}

test_smtp_auth_swaks()
{
	t1=$(date +"%s.%4N")
	case "$1" in
		"LOGIN"|"PLAIN"|"XOAUTH2")
		pass="abcd12345678"
		swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port --a "$1" -au $user@$HOSTNAME -ap "$pass"
		;;
		"CRAM-MD5"|"CRAM-SHA1"|"CRAM-SHA224"|"CRAM-SHA256"|"CRAM-SHA384"|"CRAM-SHA512"|"CRAM-RIPEMD")
		pass=$(sudo grep $user /etc/shadow|cut -d: -f2)
		swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port --a "$1" -au "$user" -ap "$pass"
		;;
		"DIGEST-MD5")
		pass=$(sudo grep $user /etc/shadow|cut -d: -f2)
		swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port --a "$1" -au $user -ap "$pass" 2>/dev/null
		;;
	esac
	check_mail
	if [ $? -eq 0 ] ; then
		last_line=$(tail -1 $logdir/smtpd/smtpd.log|grep "AUTH $1:")
		if [ $? -eq 0 ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing mail using SMTP with %-10s authentication succeeded %37s [%.4f sec]\n" $1 " " $secs
			print_pct
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			printf "\r%118s\n" " "
			echo "  testing mail using SMTP with $1 authentication failed [$secs sec]"
			(
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
			)|less
			failed=1
			exit 1
		fi
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail using SMTP with $1 authentication failed [$secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		)|less
		failed=1
		exit 1
	fi
}

test_spamfilter()
{
	(
	echo "#!/bin/sh"
	echo "tmpdir=$tmpdir"
	echo "cat > \$tmpdir/scanner.\$\$"
	echo "grep \"I am spamming you\" \$tmpdir/scanner.\$\$ > /dev/null"
	echo "ret=\$?"
	echo "exec 0<\$tmpdir/scanner.\$\$"
	echo "/bin/cat"
	echo "/bin/rm -f \$tmpdir/scanner.\$\$"
	echo "if [ \$ret -ne 0 ] ; then"
	echo "	exit 24"
	echo "else"
	echo "	exit 25"
	echo "fi"
	) > $testdir/bin/scanner
	chmod +x $testdir/bin/scanner
	/bin/rm -f $cntrldir/globalspamredirect

	# Test reject spam
	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: Testing spamfilter"
	echo "Date: $(date -R)"
	echo
	echo "Testing spamfilter"
	echo "I am spamming you"
	) | env - \
		SPAMFILTER="$testdir/bin/scanner" \
		HAMEXITCODE=24 \
		SPAMEXITCODE=25 \
		REJECTSPAM=1 \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$sbindir/qmail-spamfilter \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $testuser@$HOSTNAME 2>$tmpdir/inject.error
	ret1=$?
	l1=$(cat $tmpdir/inject.error)
	/bin/rm -f $tmpdir/inject.error
	check_mail $testdir/$testuser/Maildir 2>/dev/null
	ret2=$?
	s1=$sleep_secs

	# Test accept non-spam
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: Testing spamfilter"
	echo "Date: $(date -R)"
	echo
	echo "Testing spamfilter"
	echo "I am not spamming you"
	) | env - \
		SPAMFILTER="$testdir/bin/scanner" \
		HAMEXITCODE=24 \
		SPAMEXITCODE=25 \
		REJECTSPAM=1 \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$sbindir/qmail-spamfilter \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $testuser@$HOSTNAME
	ret3=$?
	check_mail $testdir/$testuser/Maildir 2>/dev/null
	ret4=$?
	s2=$sleep_secs
	/bin/rm -f $tmpdir/inject.error

	if [ $ret1 -eq 100 -a $ret2 -ne 0 -a $ret3 -eq 0 -a $ret4 -eq 0 -a "$l1" = "qmail-inject: fatal: qq spam or junk mail threshold exceeded (#5.7.1)" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-spamfilter for REJECTSPAM=1 succeeded %50s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "  testing qmail-spamfilter for REJECTSPAM=1 failed ret1=$ret1, ret2=$ret2, ret3=$ret3, ret4=$ret4 l1=[$l1]"
		(
		echo $logdir/qmail-send/qmail-send.log
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		/bin/rm -f $testdir/smtpd/variables/SPAMFILTER
		exit 1
	fi

	echo "$testuser@$HOSTNAME" > $cntrldir/globalspamredirect
	# Test globalspamredirect
	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $outarchive@$HOSTNAME"
	echo "Subject: Testing spamfilter"
	echo "Date: $(date -R)"
	echo
	echo "Testing spamfilter"
	echo "I am spamming you"
	) | env - \
		SPAMFILTER="$testdir/bin/scanner" \
		HAMEXITCODE=24 \
		SPAMEXITCODE=25 \
		REJECTSPAM=1 \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$sbindir/qmail-spamfilter \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $outarchive@$HOSTNAME 2>$tmpdir/inject.error
	ret1=$?
	l1=$(cat $tmpdir/inject.error)
	check_mail $testdir/$outarchive/Maildir 2>/dev/null
	ret2=$?
	s1=$sleep_secs
	check_mail $testdir/$testuser/Maildir
	ret3=$?
	s2=$sleep_secs
	if [ $ret1 -eq 100 -a $ret2 -ne 0 -a $ret3 -eq 0 -a "$l1" = "qmail-inject: fatal: qq spam or junk mail threshold exceeded (#5.7.1)" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-spamfilter for globalspamredirect+rejectspam=1 succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "  testing qmail-spamfilter for globalspamredirect+rejectspam=1 failed ret1=$ret1, ret2=$ret2, ret3=$ret3 l1=[$l1]"
		(
		echo $logdir/qmail-send/qmail-send.log
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		/bin/rm -f $testdir/smtpd/variables/SPAMFILTER
		exit 1
	fi

	# Test globalspamredirect without REJECTSPAM
	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $outarchive@$HOSTNAME"
	echo "Subject: Testing spamfilter"
	echo "Date: $(date -R)"
	echo
	echo "Testing spamfilter"
	echo "I am spamming you"
	) | env - \
		SPAMFILTER="$testdir/bin/scanner" \
		HAMEXITCODE=24 \
		SPAMEXITCODE=25 \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$sbindir/qmail-spamfilter \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $outarchive@$HOSTNAME
	ret1=$?
	check_mail $testdir/$outarchive/Maildir 2>/dev/null
	ret2=$?
	s1=$sleep_secs
	check_mail $testdir/$testuser/Maildir
	ret3=$?
	s2=$sleep_secs
	/bin/rm -f $cntrldir/globalspamredirect
	/bin/rm -f $testdir/bin/scanner
	if [ $ret1 -eq 0 -a $ret2 -ne 0 -a $ret3 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-spamfilter for globalspamredirect+rejectspam=0 succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "  testing qmail-spamfilter for globalspamredirect+rejectspam=0 failed ret1=$ret1, ret2=$ret2, ret3=$ret3 l1=[$l1]"
		(
		echo $logdir/qmail-send/qmail-send.log
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		/bin/rm -f $testdir/smtpd/variables/SPAMFILTER
		exit 1
	fi
	/bin/rm -f $testdir/smtpd/variables/SPAMFILTER
}

test_qhpsi()
{
	(
	echo "#!/bin/sh"
	echo "grep \"I am a virus\" \$1 > /dev/null"
	echo "if [ \$? -ne 0 ] ; then"
	echo "	exit 24"
	echo "else"
	echo "	exit 25"
	echo "fi"
	) > $testdir/bin/scanner
	chmod +x $testdir/bin/scanner

	# Test reject virus
	t1=$(date +"%s.%4N")

	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: Testing QHPSI"
	echo "Date: $(date -R)"
	echo
	echo "Testing QHPSI"
	echo "I am a virus"
	) | env - \
		QHPSI="$testdir/bin/scanner %s" \
		QHPSIRN=24 \
		QHPSIRC=25 \
		REJECTVIRUS=1 \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $testuser@$HOSTNAME 2>$tmpdir/inject.error
	ret1=$?
	l1=$(cat $tmpdir/inject.error)
	/bin/rm -f $tmpdir/inject.error
	check_mail $testdir/$testuser/Maildir 2>/dev/null
	ret2=$?
	s1=$sleep_secs

	# Test accept nonvirus
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: Testing QHPSI"
	echo "Date: $(date -R)"
	echo
	echo "Testing QHPSI"
	echo "I am not a virus"
	) | env - \
		QHPSI="$testdir/bin/scanner %s" \
		QHPSIRN=24 \
		QHPSIRC=25 \
		REJECTVIRUS=1 \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $testuser@$HOSTNAME
	ret3=$?
	check_mail $testdir/$testuser/Maildir 2>/dev/null
	ret4=$?
	s2=$sleep_secs

	if [ $ret1 -eq 100 -a $ret2 -ne 0 -a $ret3 -eq 0 -a $ret4 -eq 0 -a "$l1" = "qmail-inject: fatal: qq message contains virus (#5.7.1)" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing QHPSI for REJECTVIRUS=1 succeeded %60s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "  testing QHPSI for REJECTVIRUS=1 failed ret1=$ret1, ret2=$ret2 l1=[$l1]"
		(
		echo $logdir/qmail-send/qmail-send.log
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		exit 1
	fi

	# Test Header X-QHPSI
	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: Testing QHPSI"
	echo "Date: $(date -R)"
	echo
	echo "Testing QHPSI"
	echo "I am a virus"
	) | env - \
		QHPSI="$testdir/bin/scanner %s" \
		QHPSIRN=24 \
		QHPSIRC=25 \
		REJECTVIRUS=0 \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $testuser@$HOSTNAME
	ret1=$?
	check_mail_header $testdir/$testuser/Maildir X-QHPSI 1
	ret2=$?
	if [ $ret1 -eq 0 -a $ret2 -eq 0 -a "$header" = "X-QHPSI: Virus found" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing QHPSI for REJECTVIRUS=0 succeeded %60s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "  testing QHPSI for REJECTVIRUS=0 failed ret1=$ret1, ret2=$ret2 l1=[$header]"
		(
		echo $logdir/qmail-send/qmail-send.log
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		exit 1
	fi

	# Test VIRUSFORWARD
	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: Testing QHPSI"
	echo "Date: $(date -R)"
	echo
	echo "Testing QHPSI"
	echo "I am a virus"
	) | env - \
		QHPSI="$testdir/bin/scanner %s" \
		QHPSIRN=24 \
		QHPSIRC=25 \
		REJECTVIRUS=0 \
		VIRUSFORWARD=$outarchive@$HOSTNAME \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $testuser@$HOSTNAME
	ret1=$?
	check_mail $testdir/$testuser/Maildir 2>/dev/null
	ret2=$?
	s1=$sleep_secs
	check_mail_header $testdir/$outarchive/Maildir X-Quarantine-ID 0
	ret3=$?
	s2=$sleep_secs
	h1=$header
	check_mail_header $testdir/$outarchive/Maildir X-QHPSI 1
	ret4=$?
	s3=$sleep_secs
	h2=$header
	if [ $ret1 -eq 0 -a $ret2 -ne 0 -a $ret3 -eq 0 -a $ret4 -eq 0 -a "$h1" = "X-Quarantine-ID: $outarchive@$HOSTNAME,$testuser@$HOSTNAME" -a "$h2" = "X-QHPSI: Virus found" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 $s3 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing QHPSI for VIRUSFORWARD succeeded %61s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "  testing QHPSI for VIRUSFORWARD failed ret1=$ret1, ret2=$ret2, ret3=$ret3, ret4=$ret4, h1=$h1, h2=$h2"
		(
		echo $logdir/qmail-send/qmail-send.log
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		exit 1
	fi
	/bin/rm -f $testdir/bin/scanner
}

test_qqeh()
{
	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $user@$HOSTNAME"
	echo "Subject: Testing QQEH for qmail-local"
	echo "Date: $(date -R)"
	echo
	echo "Testing QQEH"
	) > $tmpdir/mail.txt
	env - \
			QQEH="X-Foobar: Test QQEH for qmail-local" \
			QUEUEDIR=$testdir/queue \
			QMAILQUEUE=$qmail_queue \
			CONTROLDIR=$cntrldir \
			BIGTODO=0 \
			CONFSPLIT=23 \
		$qmail_inject -f$user@$HOSTNAME $user@$HOSTNAME <$tmpdir/mail.txt

	check_mail_header $maildir X-Foobar 1
	if [ $? -eq 0 -a "$header" = "X-Foobar: Test QQEH for qmail-local" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing QQEH header insertion for qmail-local succeeded %46s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing QQEH header insertion for qmail-local failed [$secs sec]"
		failed=1
		/bin/rm -f $tmpdir/mail.txt
		exit 1
	fi

	echo "$HOSTNAME:127.0.0.1:$smtp_port" > $cntrldir/smtproutes
	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $user@$ext_domain"
	echo "Subject: Testing QQEH for qmail-remote"
	echo "Date: $(date -R)"
	echo
	echo "Testing QQEH"
	) > $tmpdir/mail.txt
	env - \
			QQEH="X-Foobar: Test QQEH for qmail-remote" \
			QUEUEDIR=$testdir/queue \
			QMAILQUEUE=$qmail_queue \
			CONTROLDIR=$cntrldir \
			BIGTODO=0 \
			CONFSPLIT=23 \
		$qmail_inject -f$user@$HOSTNAME $user@$ext_domain <$tmpdir/mail.txt
	/bin/rm -f $tmpdir/mail.txt
	check_mail_header $testdir/$testuser/Maildir X-Foobar 1
	if [ $? -eq 0 -a "$header" = "X-Foobar: Test QQEH for qmail-remote" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing QQEH header insertion for qmail-remote succeeded %45s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing QQEH header for qmail-remote insertion failed [$secs sec]"
		/bin/rm -f $cntrldir/smtproutes
		failed=1
		exit 1
	fi
	/bin/rm -f $cntrldir/smtproutes
}

test_smtp_secure_auth()
{
	# Test secure auth
	#
	/bin/rm -f $servicedir/smtpd/variables/SMTPS
	echo 1 > $servicedir/smtpd/variables/STARTTLS
	echo 1 > $servicedir/smtpd/variables/SECURE_AUTH
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart

	t1=$(date +"%s.%4N")
	# this should fail without using SMTPS or STARTTLS
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
		--a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" >$tmpdir/swaks.$$ 2>&1
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	echo "$l1"|grep '*** Auth not attempted, requested type not available' >/dev/null
	if [ $? -eq 0 ] ; then
		ret2=0
	else
		ret2=1
	fi

	# this should succeed by using SMTPS or STARTTLS
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
		--a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" --tls >/dev/null
	check_mail
	ret3=$?
	if [ $ret1 -ne 0 -a $ret2 -eq 0 -a $ret3 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail using SECURE  AUTH SMTP authentication succeeded %40s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail using SECURE AUTH SMTP authentication failed [$secs sec]"
		(
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi

	/bin/rm -f $servicedir/smtpd/variables/STARTTLS $servicedir/smtpd/variables/SECURE_AUTH
	echo 1 > $servicedir/smtpd/variables/SMTPS
	echo 1 > $servicedir/smtpd/variables/REQUIREAUTH
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart

	# Test REQUIREAUTH
	t1=$(date +"%s.%4N")
	# this should fail without using authentication
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --tls-on-connect >$tmpdir/swaks.$$ 2>&1
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	echo "$l1"|grep ".*530 authentication required (#5.7.1)" >/dev/null
	if [ $? -eq 0 ] ; then
		ret2=0
	else
		ret2=1
	fi
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
		--a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" --tls-on-connect >/dev/null
	check_mail
	ret3=$?
	if [ $ret1 -ne 0 -a $ret2 -eq 0 -a $ret3 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail using REQUIRE AUTH SMTP authentication succeeded %40s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail using REQUIRE AUTH SMTP authentication failed [$secs sec]"
		(
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi

	/bin/rm -f $servicedir/smtpd/variables/REQUIREAUTH
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart

	# Test authdomains
	t1=$(date +"%s.%4N")
	echo "@$HOSTNAME" > $cntrldir/authdomains
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
		--tls-on-connect > $tmpdir/swaks.$$ 2>&1
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	echo $l1 | grep ".*553 sorry, that domain isn't allowed to be relayed thru this MTA without authentication; no valid cert for gateway #5.7.1" > /dev/null
	ret2=$?
	/bin/rm -f $cntrldir/authdomains
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
		--tls-on-connect
	check_mail
	ret3=$?

	if [ $ret1 -ne 0 -a $ret2 -eq 0 -a $ret3 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail using authdomains control file succeeded %48s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail using authdomains control file failed [$secs sec]"
		(
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3, l1=[$l1]"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi

	# Test AUTH_ALL
	echo 1 > $servicedir/smtpd/variables/AUTH_ALL
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
		--tls-on-connect > $tmpdir/swaks.$$ 2>&1
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	echo $l1 | grep ".*553 sorry, that domain isn't allowed to be relayed thru this MTA without authentication; no valid cert for gateway #5.7.1" > /dev/null
	ret2=$?
	if [ $ret1 -ne 0 -a $ret2 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail using AUTH_ALL env variable succeeded %51s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		printf "\r  testing mail using AUTH_ALL env variable failed [$secs sec]"
		(
		echo "ret1=$ret1, ret2=$ret2, l1=[$l1]"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi

	/bin/rm -f $servicedir/smtpd/variables/AUTH_ALL
	sudo svc -r $servicedir/smtpd
	sleep $sleep_int
}

test_smtp_cugmail()
{
	echo 1 > $servicedir/smtpd/variables/CUGMAIL
	       > $servicedir/smtpd/variables/QUERY_CACHE
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	> $servicedir/smtpd/variables/CUGMAIL

	t1=$(date +"%s.%4N")
	# Test1: This should fail
	swaks -S --to $user@$HOSTNAME --from $user@$ext_domain --server 127.0.0.1 \
		--port $smtp_port >$tmpdir/swaks.$$
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$

	# Test2: This should fail
	swaks -S --to $user@$ext_domain --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" >$tmpdir/swaks.$$
	ret2=$?
	l2=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$

	# Test3: This should fail
	swaks -S --to $user@$ext_domain --from $user@$ext_domain --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" >$tmpdir/swaks.$$
	ret3=$?
	l3=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$

	# Test4: This should succeed
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $user@$HOSTNAME -ap "abcd12345678"
	ret4=$?
	if [ "$l1" = "$l2" -a "$l1" = "$l3" -a "$l1" = "<** 553 sorry, this MTA is a closed user group system (#5.7.1)" ] ; then
		ret5=0
	else
		ret5=1
	fi
	check_mail
	if [ $? -eq 0 -a $ret1 -ne 0 -a $ret2 -ne 0 -a $ret3 -ne 0 -a $ret4 -eq 0 -a $ret5 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 4)
		printf "\r  testing mail using SMTP with CUGMAIL     succeeded %51s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail using SMTP with CUGMAIL failed [$secs sec]"
		(
		echo "l1=[$l1]"
		echo "l2=[$l2]"
		echo "l3=[$l3]"
		echo "ret1=$ret1, ret2=$ret3, ret3=$ret3, ret4=$ret4, ret5=$ret5"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi
}

test_smtp_chk_sender()
{
	echo 0 > $servicedir/smtpd/variables/CHECKSENDER
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")

	# First: This should work
	# because remote mail will be redirected
	# to $HOSTNAME using $sbindir/rd-remote
	# Also, $sbindir/rd-remote redirects mail to
	# to $testuser@$HOSTNAME
	# we require authenticted SMTP because of CHECKSENDER
	find $testdir/$testuser/Maildir -type f -exec /bin/rm -f {} \;
	echo "$HOSTNAME:127.0.0.1:$smtp_port $user@$HOSTNAME abcd12345678" > $cntrldir/smtproutes
	swaks -S --to $user@$ext_domain --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $user@$HOSTNAME -ap "abcd12345678"
	ret1=$?
	check_mail $testdir/$testuser/Maildir
	ret2=$?
	s1=$sleep_secs

	# Second: This should be denied without MASQUERADE
	swaks -S --to $user@$HOSTNAME --from $user@$ext_domain --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" >$tmpdir/swaks.$$
	ret3=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$
	echo "$l1" | grep ".*553 sorry, this MTA does not accept masquerading/forging,.*" >/dev/null
	if [ $? -eq 0 ] ; then
		ret4=0
	else
		ret4=1
	fi

	# Third: This should work
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $user@$HOSTNAME -ap "abcd12345678"
	check_mail
	ret5=$?
	s2=$sleep_secs

	# fourth: This should be denied
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port >$tmpdir/swaks.$$
	ret6=$?
	l2=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$
	echo "$l2" | grep ".*530 authentication required (#5.7.1)" >/dev/null
	if [ $? -eq 0 ] ; then
		ret7=0
	else
		ret7=1
	fi

	if [ $ret1 -eq 0 -a $ret2 -eq 0 -a $ret3 -ne 0 -a $ret4 -eq 0 -a $ret5 -eq 0 -a $ret6 -ne 0 -a $ret7 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		tcount=$(expr $tcount + 4)
		printf "\r  testing mail using SMTP with CHECKSENDER succeeded %51s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		> $servicedir/smtpd/variables/CHECKSENDER
		printf "\r%118s\n" " "
		echo "  testing mail using SMTP with CHECKSENDER failed [$secs sec]"
		(
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3, ret4=$ret4, ret5=$ret5, ret6=$ret6, ret7=$ret7"
		echo "l1=$l1"
		echo "l2=$l2"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		echo "$logdir/qmail-send/current"
		cat $logdir/qmail-send/current
		)|less
		failed=1
		exit 1
	fi

	# fifth: This should be allowed with MASQUERADE
	echo "" > $servicedir/smtpd/variables/MASQUERADE
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")
	swaks -S --to $user@$HOSTNAME --from $user@$ext_domain --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" > $tmpdir/swaks.$$
	check_mail
	ret1=$?
	if [ $ret1 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 4)
		printf "\r  testing MASQUERADE any  option succeeded %61s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing MASQUERADE any option failed [$secs sec]"
		(
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		echo "$logdir/qmail-send/current"
		cat $logdir/qmail-send/current
		)|less
		failed=1
		exit 1
	fi

	# sixth: This should be denied with MASQUERADE
	echo $testuser@$HOSTNAME > $servicedir/smtpd/variables/MASQUERADE
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart

	t1=$(date +"%s.%4N")
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $testuser@$HOSTNAME -ap "abcd87654321" >$tmpdir/swaks.$$
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	if [ $ret1 -eq 0 ] ; then
		check_mail
	fi
	echo "$l1" | grep ".*553 sorry, this MTA does not accept masquerading/forging, auth <$testuser@$HOSTNAME> #5.7.1" > /dev/null
	ret2=$?

	# seventh: This should be allowed with MASQUERADE
	echo $user@$HOSTNAME > $servicedir/smtpd/variables/MASQUERADE
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $testuser@$HOSTNAME -ap "abcd87654321"
	ret3=$?
	check_mail
	ret4=$?
	s1=$sleep_secs
	/bin/rm -f $tmpdir/swaks.$$

	if [ $ret1 -ne 0 -a $ret2 -eq 0 -a $ret3 -eq 0 -a $ret4 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $sleep_restart | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 4)
		printf "\r  testing MASQUERADE user option succeeded %61s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 $sleep_restart | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		printf "\r%118s\n" " "
		echo "  testing MASQUERADE user option failed %61s [$secs sec]"
		(
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3, ret4=$ret4"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		echo "$logdir/qmail-send/current"
		cat $logdir/qmail-send/current
		)|less
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	# now test checksenderdomains
	touch $cntrldir/chksenderdomains
	# First: This should be allowed
	swaks -S --to $user@$HOSTNAME --from $user@$ext_domain --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" >$tmpdir/swaks.$$
	ret1=$?
	check_mail
	/bin/rm -f $tmpdir/swaks.$$

	echo "@$HOSTNAME" > $cntrldir/chksenderdomains
	/bin/rm -f $servicedir/smtpd/variables/MASQUERADE
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	# second: This should be denied
	swaks -S --to $user@$HOSTNAME --from $user@$ext_domain --server 127.0.0.1 \
		--port $smtp_port --a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" >$tmpdir/swaks.$$
	ret2=$?
	if [ $ret1 -eq 0 -a $ret2 -ne 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs $sleep_restart | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 4)
		printf "\r  testing chksender control file succeeded %61s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs $sleep_restart | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		echo "  testing chksender control file failed [$secs sec]"
		(
		echo "ret1=$ret1, ret2=$ret2"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		echo "$logdir/qmail-send/current"
		cat $logdir/qmail-send/current
		)|less
		failed=1
		exit 1
	fi
	/bin/rm -f $cntrldir/chksenderdomains $tmpdir/swaks.$$
	> $servicedir/smtpd/variables/CHECKSENDER
	sudo svc -r $servicedir/smtpd
	sleep $sleep_int
}

test_smtp_surbl()
{
	echo $surblqueue > $servicedir/smtpd/variables/QMAILQUEUE
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")
	(
	echo This is a test message for surbl
	echo http://surbl-org-permanent-test-point.com/
	) | swaks -S  --header "Subject: Test Mail" --to $user@$HOSTNAME --from $user@$HOSTNAME \
		--server 127.0.0.1 --port $smtp_port --body - >$tmpdir/swaks.$$
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$
	if [ "$l1" = "<** 554 message contains an URL listed in SURBL blocklist [http://surbl-org-permanent-test-point.com/]" ] ; then
		ret2=0
	else
		ret2=1
	fi

	(
	echo This is a test message for surbl
	echo without surb blacklisted url
	echo https://github.com/indimail/indimail-mta
	) | swaks -S  --header "Subject: Test Mail" --to $user@$HOSTNAME --from $user@$HOSTNAME \
		--server 127.0.0.1 --port $smtp_port --body - >$tmpdir/swaks.$$
	check_mail
	ret3=$?

	/bin/rm -f $servicedir/smtpd/variables/QMAILQUEUE $tmpdir/swaks.$$
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	if [ $ret1 -ne 0 -a $ret2 -eq 0 -a $ret3 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs $sleep_restart | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 2)
		printf "\r  testing mail using SMTP for SURBL succeeded %58s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs $sleep_restart | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		echo "  testing mail using SMTP for SURBL failed [$secs sec]"
		(
		echo "l1=$l1"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi
}

test_envrules()
{
	echo $user@$HOSTNAME > $cntrldir/badmailfrom.custom
	echo "$user@$HOSTNAME:BADMAILFROM=badmailfrom.custom" > $cntrldir/from.envrules
	if [ $1 -eq 0 ] ; then
		> $servicedir/smtpd/variables/QREGEX
	else
		echo 1 > $servicedir/smtpd/variables/QREGEX
	fi
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")
	swaks -S --to $testuser@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --tls-on-connect > $tmpdir/swaks.$$
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	echo $l1 | grep ".*553 sorry, your envelope sender has been denied (#5.7.1)" > /dev/null
	ret2=$?

	/bin/rm -f $cntrldir/from.envrules $cntrldir/badmailfrom.custom
	swaks -S --to $testuser@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --tls-on-connect > $tmpdir/swaks.$$
	ret3=$?
	check_mail $testdir/$testuser/Maildir
	ret4=$?
	if [ $ret1 -ne 0 -a $ret2 -eq 0 -a $ret3 -eq 0 -a $ret4 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		if [ $1 -eq 0 ] ; then
			tcount=$(expr $tcount + 1)
			printf "\r  testing envrules using wildmat succeeded %61s [%.4f sec]\n" " " $secs
		else
			tcount=$(expr $tcount + 1)
			printf "\r  testing envrules using REGEXP  succeeded %61s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3, ret4=$ret4"
		if [ $1 -eq 0 ] ; then
			echo "  testing envrules using wildmat failed [$secs sec]"
		else
			echo "  testing envrules using REGEXP failed [$secs sec]"
		fi
	fi
	/bin/rm -f $cntrldir/from.envrules $cntrldir/badmailfrom.custom $servicedir/smtpd/variables/QREGEX
}

test_smtp_accesslist()
{
	(
	echo "rcpt:ceo@$HOSTNAME:$user@$HOSTNAME"
	echo "from:$user@$vdomain:$testuser@$HOSTNAME"
	) > $cntrldir/accesslist
	if [ $1 -eq 0 ] ; then
		> $servicedir/smtpd/variables/QREGEX
	else
		echo 1 > $servicedir/smtpd/variables/QREGEX
	fi
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")
	swaks -S --to $user@$HOSTNAME --from $testuser@$vdomain --server 127.0.0.1 \
		--port $smtp_port --tls-on-connect > $tmpdir/swaks.$$
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	echo $l1 | grep ".*553 sorry, sites access list denies transaction (#5.7.1)" > /dev/null
	ret2=$?
	swaks -S --to $user@$HOSTNAME --from ceo@$HOSTNAME --server 127.0.0.1 \
		--port $smtp_port --tls-on-connect > $tmpdir/swaks.$$
	ret3=$?

	if [ $ret1 -ne 0 -a $ret2 -eq 0 -a $ret3 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		if [ $1 -eq 0 ] ; then
			tcount=$(expr $tcount + 1)
			printf "\r  testing SMTP RECIPIENT accesslist using wildmat succeeded %44s [%.4f sec]\n" " " $secs
		else
			tcount=$(expr $tcount + 1)
			printf "\r  testing SMTP RECIPIENT accesslist using REGEXP  succeeded %44s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		if [ $1 -eq 0 ] ; then
			echo "  testing SMTP RECIPIENT accesslist using wildmat failed [$secs sec]"
		else
			echo "  testing SMTP RECIPIENT accesslist using REGEXP failed [$secs sec]"
		fi
	fi
	swaks -S --to $user@$HOSTNAME --from $user@$vdomain --server 127.0.0.1 \
		--port $smtp_port --tls-on-connect > $tmpdir/swaks.$$
	ret1=$?
	l2=$(sed -n 1p < $tmpdir/swaks.$$)
	echo $l1 | grep ".*553 sorry, sites access list denies transaction (#5.7.1)" > /dev/null
	ret2=$?
	swaks -S --to $testuser@$HOSTNAME --from $user@$vdomain --server 127.0.0.1 \
		--port $smtp_port --tls-on-connect > $tmpdir/swaks.$$
	check_mail $testdir/$testuser/Maildir
	ret3=$?
	if [ $ret1 -ne 0 -a $ret2 -eq 0 -a $ret3 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		if [ $1 -eq 0 ] ; then
			tcount=$(expr $tcount + 1)
			printf "\r  testing SMTP SENDER    accesslist using wildmat succeeded %44s [%.4f sec]\n" " " $secs
		else
			tcount=$(expr $tcount + 1)
			printf "\r  testing SMTP SENDER    accesslist using REGEXP  succeeded %44s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		if [ $1 -eq 0 ] ; then
			echo "  testing SMTP SENDER accesslist using wildmat failed [$secs sec]"
		else
			echo "  testing SMTP SENDER accesslist using REGEXP failed [$secs sec]"
		fi
	fi
	/bin/rm -f $cntrldir/accesslist $servicedir/smtpd/variables/QREGEX
}

test_qmail_remote_auth()
{
	t1=$(date +"%s.%4N")
	case $1 in
		"CRAM-MD5"|"CRAM-SHA1"|"CRAM-SHA224"|"CRAM-SHA256"|"CRAM-SHA384"|"CRAM-SHA512"|"CRAM-RIPEMD"|"DIGEST-MD5")
		u=$user
		pass=$(sudo grep $user /etc/shadow|cut -d: -f2)
		;;
		*)
		u=$user@$HOSTNAME
		pass="abcd12345678"
		;;
	esac
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $user@$HOSTNAME"
	echo "Subject: Test"
	echo "Date: $(date -R)"
	echo
	echo "Test message"
	) > $tmpdir/mail.txt

	env - \
		SMTPROUTE="$HOSTNAME:127.0.0.1:$smtp_port $u $pass" \
		AUTH_SMTP="$1" \
		CONTROLDIR="$cntrldir" \
		$qmail_remote $HOSTNAME $user@$HOSTNAME "" 10 $user@$HOSTNAME >$tmpdir/qremote.$$ <$tmpdir/mail.txt
	cat $tmpdir/qremote.$$ >> $logdir/qmail-remote/qmail-remote.log
	addr=$(/usr/libexec/indimail/batv -k $(cat $cntrldir/batvkey) -v $(sed -n 1p $tmpdir/qremote.$$|awk '{print $2}'|sed 's/^.\(.*\).$/\1/') 2>/dev/null)
	batv_status=$?
	check_mail
	if [ $? -eq 0 ] ; then
		last_line=$(tail -1 $logdir/smtpd/smtpd.log|grep "AUTH $1:")
		if [ $? -eq 0 ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing qmail-remote AUTH %-11s qmail-remote send+receive succeeded %28s [%.4f sec]\n" "$1" " " $secs
			print_pct
			/bin/rm -f $testdir/tmpdir.$$ $tmpdir/mail.txt
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			printf "\r%118s\n" " "
			echo "  testing qmail-remote AUTH $1, qmail-remote send+receive failed [$secs sec]"
			(
			echo "Last line does not have AUTH"
			if [ $log_type -eq 1 ] ; then
				echo "$logdir/smtpd/smtpd.log"
				cat $logdir/smtpd/smtpd.log
			elif [ -f $logdir/smtpd/current ] ; then
				echo "$logdir/smtpd/current"
				cat $logdir/smtpd/current
			fi
			echo "qmail-remote.log"
			cat $tmpdir/qremote.$$
			)|less
			/bin/rm -f $tmpdir/qremote.$$ $tmpdir/mail.txt
			failed=1
			exit 1
		fi
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-remote AUTH $1, qmail-remote send+receive failed [$secs sec]"
		(
		echo "Failed to receive mail"
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		echo "qmail-remote.log"
		cat $tmpdir/qremote.$$
		)|less
		/bin/rm -f $tmpdir/qremote.$$ $tmpdir/mail.txt
		failed=1
		exit 1
	fi
	if [ " $1" = " DIGEST-MD5" ] ; then
		if [ $batv_status -eq 0 -a "$addr" = "$user@$HOSTNAME" ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing BATV signing+verification succeeded %58s [%.4f sec]\n" " " $secs
			print_pct
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			printf "\r%118s\n" " "
			echo "  testing BATV signing+verification failed [$secs sec]"
			(
			echo "batv_status=$batv_status, addr=$addr"
			if [ $log_type -eq 1 ] ; then
				echo "$logdir/smtpd/smtpd.log"
				cat $logdir/smtpd/smtpd.log
			elif [ -f $logdir/smtpd/current ] ; then
				echo "$logdir/smtpd/current"
				cat $logdir/smtpd/current
			fi
			echo "qmail-remote.log"
			cat $tmpdir/qremote.$$
			)|less
			failed=1
			exit 1
		fi
	fi
}

test_qmail_remote_routing()
{
	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $user@$HOSTNAME"
	echo "Subject: Test"
	echo "Date: $(date -R)"
	echo
	echo "Test message"
	) > $tmpdir/mail.txt

	/bin/rm -f $cntrldir/batvkey
	env - \
		SMTPROUTE="$HOSTNAME:127.0.0.1:$smtp_port" \
		CONTROLDIR=$cntrldir \
		$qmail_remote $HOSTNAME $user@$HOSTNAME "" 10 $user@$HOSTNAME 1>$testdir/qremote.$$ <$tmpdir/mail.txt
	cat $testdir/qremote.$$ >> $logdir/qmail-remote/qmail-remote.log
	check_mail
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-remote SMTPROUTE env qmail-remote send+receive succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
		/bin/rm -f $testdir/qremote.$$
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-remote SMTPROUTE env qmail-remote send+receive failed [$secs sec]"
		(
		echo "qmail-remote.log"
		$testdir/qremote.$$
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		)|less
		/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
		failed=1
		exit 1
	fi
	s1=$sleep_secs
	echo "$HOSTNAME:127.0.0.1:$smtp_port" > $cntrldir/smtproutes
	env - \
		CONTROLDIR=$cntrldir \
		$qmail_remote $HOSTNAME $user@$HOSTNAME "" 10 $user@$HOSTNAME 1>$testdir/qremote.$$ <$tmpdir/mail.txt
	cat $testdir/qremote.$$ >> $logdir/qmail-remote/qmail-remote.log
	check_mail
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-remote smtproutes control file qmail-remote send+receive succeeded %21s [%.4f sec]\n" " " $secs
		print_pct
		/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-remote smtproutes control file qmail-remote send+receive failed [$secs sec]"
		(
		echo "Failed to receive mail"
		echo "qmail-remote.log"
		$testdir/qremote.$$
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		)|less
		/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
		failed=1
		exit 1
	fi
	/bin/rm -f $cntrldir/smtproutes
}

test_qmail_remote_auth_cdb()
{
	t1=$(date +"%s.%4N")
	echo "$HOSTNAME:127.0.0.1:$smtp_port /s" > $cntrldir/smtproutes
	(
	echo "=$user@$HOSTNAME:$user@$HOSTNAME abcd12345678 AUTH_SMTP=plain:"
	echo "."
	) > $cntrldir/remote_auth
	env CONTROLDIR=$cntrldir /usr/sbin/cdb-database remote_auth
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing cdb-database for qmail-remote authentication succeeded %39s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "  testing cdb-database for qmail-remote authentication failed [$secs sec]"
		failed=1
		exit 1
	fi
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $user@$HOSTNAME"
	echo "Subject: Test"
	echo "Date: $(date -R)"
	echo
	echo "Test message"
	) > $tmpdir/mail.txt

	env - \
		SMTPROUTE="$HOSTNAME:127.0.0.1:$smtp_port /s" \
		AUTH_SMTP="" \
		CONTROLDIR=$cntrldir \
		$qmail_remote $HOSTNAME $user@$HOSTNAME "" 10 $user@$HOSTNAME 1>$testdir/qremote.$$ <$tmpdir/mail.txt
	cat $testdir/qremote.$$ >> $logdir/qmail-remote/qmail-remote.log
	check_mail
	if [ $? -eq 0 ] ; then
		last_line=$(tail -1 $logdir/smtpd/smtpd.log|grep "AUTH PLAIN:")
		if [ $? -eq 0 ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing qmail-remote using remote_auth.cdb qmail-remote send+receive succeeded %23s [%.4f sec]\n" " " $secs
			print_pct
			/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			printf "\r%118s\n" " "
			echo "  testing qmail-remote using remote_auth.cdb qmail-remote send+receive failed [$secs sec]"
			(
			echo "Last does not have AUTH PLAIN"
			echo "qmail-remote.log"
			cat $testdir/qremote.$$
			)|less
			/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
			failed=1
			exit 1
		fi
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		printf "  testing qmail-remote using remote_auth.cdb qmail-remote send+receive failed %26s [%.4f sec]\n" " " $secs
		(
		echo "Failed to receive mail"
		echo "qmail-remote.log"
		cat $testdir/qremote.$$
		)|less
		/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
		failed=1
		exit 1
	fi
	/bin/rm -f $cntrldir/smtproutes
}

test_qmail_remote_starttls()
{
	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $user@$HOSTNAME"
	echo "Subject: Test"
	echo "Date: $(date -R)"
	echo
	echo "Test message"
	) > $tmpdir/mail.txt
	env - \
		SMTPROUTE="$HOSTNAME:127.0.0.1:$smtp_port" \
		CONTROLDIR="$cntrldir" \
		$qmail_remote $HOSTNAME $user@$HOSTNAME "" 10 $user@$HOSTNAME >$testdir/qremote.$$ <$tmpdir/mail.txt
	cat $testdir/qremote.$$ >> $logdir/qmail-remote/qmail-remote.log
	check_mail
	if [ $? -eq 0 ] ; then
		last_line=$(tail -1 $logdir/smtpd/smtpd.log|grep "TLS=TLSv.*")
		if [ $? -eq 0 ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			last_line=$(echo $last_line|awk '{print $17}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing qmail-remote STARTTLS %-11s, qmail-remote send+receive succeeded %23s [%.4f sec]\n" $last_line " " $secs
			print_pct
			/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			printf "\r%118s\n" " "
			echo "  testing qmail-remote STARTTLS, qmail-remote send+receive failed [$secs sec]"
			(
			echo "Last line does not have TLS"
			if [ $log_type -eq 1 ] ; then
				echo "$logdir/smtpd/smtpd.log"
				cat $logdir/smtpd/smtpd.log
			elif [ -f $logdir/smtpd/current ] ; then
				echo "$logdir/smtpd/current"
				cat $logdir/smtpd/current
			fi
			echo "qmail-remote.log"
			cat $testdir/qremote.$$
			)|less
			/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
			failed=1
			exit 1
		fi
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-remote STARTTLS, qmail-remote send+receive failed [$secs sec]"
		(
		echo "Failed to receive mail"
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		echo "qmail-remote.log"
		cat $testdir/qremote.$$
		)|less
		/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
		failed=1
		exit 1
	fi
}

test_qmail_remote_smtps()
{
	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $user@$HOSTNAME"
	echo "Subject: Test"
	echo "Date: $(date -R)"
	echo
	echo "Test message"
	) > $tmpdir/mail.txt
	env - \
		SMTPROUTE="$HOSTNAME:127.0.0.1:$smtp_port" \
		SMTPS=1 \
		CONTROLDIR="$cntrldir" \
		$qmail_remote $HOSTNAME $user@$HOSTNAME "" 10 $user@$HOSTNAME >$testdir/qremote.$$ <$tmpdir/mail.txt
	cat $testdir/qremote.$$ >> $logdir/qmail-remote/qmail-remote.log
	check_mail
	if [ $? -eq 0 ] ; then
		last_line=$(tail -1 $logdir/smtpd/smtpd.log|grep "TLS=TLSv.*")
		if [ $? -eq 0 ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			last_line=$(echo $last_line|awk '{print $17}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing qmail-remote SMTPS %11s, qmail-remote send+receive succeeded %26s [%.4f sec]\n" $last_line " " $secs
			print_pct
			/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
			printf "\r%118s\n" " "
			echo "  testing qmail-remote SMTPS, qmail-remote send+receive failed [$secs sec]"
			(
			echo "Last line does not have TLS"
			if [ $log_type -eq 1 ] ; then
				echo "$logdir/smtpd/smtpd.log"
				cat $logdir/smtpd/smtpd.log
			elif [ -f $logdir/smtpd/current ] ; then
				echo "$logdir/smtpd/current"
				cat $logdir/smtpd/current
			fi
			echo "qmail-remote.log"
			cat $testdir/qremote.$$
			)|less
			/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
			failed=1
			exit 1
		fi
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-remote SMTPS, qmail-remote send+receive failed [$secs sec]"
		(
		echo "Failed to receive mail"
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
		elif [ -f $logdir/smtpd/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
		fi
		echo "qmail-remote.log"
		cat $testdir/qremote.$$
		)|less
		/bin/rm -f $testdir/qremote.$$ $tmpdir/mail.txt
		failed=1
		exit 1
	fi
}

terminate_smtp_qmtp()
{
	if [ -n "$1" ] ; then
		sudo kill $*
		sleep $sleep_int
		sleep_secs=$sleep_int
	else
		sleep_secs=0
	fi
}

start_tcpserver_smtps()
{
	t1=$(date +"%s.%4N")
	if [ $# -eq 0 ] ; then
		tls_ver=TLSv1_3
	elif [ -n "$1" ] ; then
		tls_ver=$1
	else
		tls_ver=""
	fi
	if [ "$tls_ver" = "TLSv1_3"  ] ; then
		echo TLSv1_2:TLSv1_3         > $cntrldir/tlsclientmethod
		echo TLSv1_2:TLSv1_3         > $cntrldir/tlsservermethod
	elif [ "$tls_ver" = "TLSv1_2"  ] ; then
		echo TLSv1_2                 > $cntrldir/tlsclientmethod
		echo TLSv1_2                 > $cntrldir/tlsservermethod
	else
		/bin/rm -f                     $cntrldir/tlsclientmethod
		/bin/rm -f                     $cntrldir/tlsservermethod
	fi
	printf "\r%118s\n" " "
	if [ -n "$tls_ver" ] ; then
		printf "\rStarting SMTPS on port %5d TLS=%-7s using tcpserver %69s\n" $smtp_port $tls_ver ""
	else
		printf "\rStarting SMTPS on port %5d TLS=%-7s using tcpserver %69s\n" $smtp_port "auto" ""
	fi
	(
	sudo env - \
		USE_QPWGR=1 \
		SMTPS=1 \
		BIGTODO=0 \
		CONFSPLIT=23 \
		QUEUEDIR=$testdir/queue \
		CONTROLDIR=$cntrldir \
		CERTDIR=$certdir \
		$tcpserver -u qmaild -g qmail -l $HOSTNAME -HR 0 $smtp_port $qmail_smtpd
	) > $logdir/smtpd/smtpd.log 2>&1 &

	count=0
	tx1=$(date +"%s.%4N")
	while true
	do
		smtp_pid=$(/bin/ps -ef|grep -v sudo|grep $smtp_port|grep qmail-smtpd|awk '{print $2}')
		if [ -n "$smtp_pid" ] ; then
			break
		fi
		sleep 1
	done
	tx2=$(date +"%s.%4N")
	sleep_secs=$(echo $tx1 $tx2 | awk '{printf("%0.4f\n", $2-$1)}')
	sudo kill -0 $smtp_pid
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing SMTPS on port %4d using tcpserver succeeded %49s [%.4f sec]\n" $smtp_port " " $secs
		print_pct
	else
		sudo kill $send_pid $smtp_pid
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing SMTPS on port $smtp_port using tcpserver failed [$secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		)|less
		failed=1
		exit 1
	fi
}

start_tcpserver_smtps_unix()
{
	t1=$(date +"%s.%4N")
	printf "\r%118s\n" " "
	echo "Starting SMTPS on socket $smtp_socket using tcpserver"
	(
	env - \
		USE_QPWGR=1 \
		SMTPS=1 \
		BIGTODO=0 \
		CONFSPLIT=23 \
		QUEUEDIR=$testdir/queue \
		CONTROLDIR=$cntrldir \
		CERTDIR=$certdir \
		$tcpserver $smtp_socket $qmail_smtpd
	) > $logdir/smtpd/smtpd.log 2>&1 &

	smtp_pid=$(/bin/ps -ef|grep $smtp_socket|grep qmail-smtpd|awk '{print $2}')
	kill -0 $smtp_pid
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing SMTPS on socket %26s using tcpserver succeeded %25s [%.4f sec]\n" $smtp_socket " " $secs
		print_pct
	else
		sudo kill $send_pid $smtp_pid
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "  testing SMTPS on socket $smtp_socket using tcpserver failed [$secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		)|less
		failed=1
		exit 1
	fi
}

setup_svscan_basic()
{
	t1=$(date +"%s.%4N")
	sudo /bin/rm -rf $servicedir $logdir/smtpd $logdir/qmail-send $logdir/slowq-send

	mkdir -p $servicedir/smtpd/variables
	mkdir -p $servicedir/smtpd/log
	echo 1              > $servicedir/smtpd/variables/USE_QPWGR
	echo 1              > $servicedir/smtpd/variables/SMTPS
	echo 0              > $servicedir/smtpd/variables/BIGTODO
	echo 23             > $servicedir/smtpd/variables/CONFSPLIT
	echo $cntrldir      > $servicedir/smtpd/variables/CONTROLDIR
	echo $testdir/queue > $servicedir/smtpd/variables/QUEUEDIR
	echo $certdir       > $servicedir/smtpd/variables/CERTDIR
	echo 0              > $servicedir/smtpd/variables/PENALTY
	(
	printf "#!/bin/sh\nexec $envdir ./variables $tcpserver -u qmaild -g qmail -v -l $HOSTNAME -HR 0 "
	printf "$smtp_port $qmail_smtpd $HOSTNAME $testdir/bin/authsmtp "
	printf "/bin/false 2>&1\n"
	) > $servicedir/smtpd/run
	printf "#!/bin/sh\nexec $multilog t $logdir/smtpd\n" > $servicedir/smtpd/log/run
	chmod +x $servicedir/smtpd/run
	chmod +x $servicedir/smtpd/log/run

	mkdir -p $servicedir/qmail-send/variables
	mkdir -p $servicedir/qmail-send/log
	echo /bin:/usr/sbin            > $servicedir/qmail-send/variables/PATH
	echo 0                         > $servicedir/qmail-send/variables/BIGTODO
	echo 1                         > $servicedir/qmail-send/variables/USE_QPWGR
	echo 23                        > $servicedir/qmail-send/variables/CONFSPLIT
	echo $cntrldir                 > $servicedir/qmail-send/variables/CONTROLDIR
	echo $testdir/queue            > $servicedir/qmail-send/variables/QUEUEDIR
	echo $sysconfdir/users         > $servicedir/qmail-send/variables/ASSIGNDIR
	echo $certdir                  > $servicedir/qmail-send/variables/CERTDIR
	echo $testdir/bin/qmail-getpw  > $servicedir/qmail-send/variables/QMAILGETPW
	echo $sbindir/rd-remote        > $servicedir/qmail-send/variables/QMAILREMOTE
	echo $testdir/etc/passwd       > $servicedir/qmail-send/variables/PASSWD_FILE
	echo 1                         > $servicedir/qmail-send/variables/QREGEX
	echo login                     > $servicedir/qmail-send/variables/AUTH_SMTP
	printf "#!/bin/sh\nexec $envdir ./variables $qmail_start -s ./Maildir/ 2>&1\n" > $servicedir/qmail-send/run
	printf "#!/bin/sh\nexec $multilog t $logdir/qmail-send\n" > $servicedir/qmail-send/log/run
	chmod +x $servicedir/qmail-send/run
	chmod +x $servicedir/qmail-send/log/run
}

start_svscan_without_svscanlog()
{
	t1=$(date +"%s.%4N")
	(
	sudo env - \
		DISABLE_RUN=1 \
		SILENT=1 \
		PATH=/bin:/usr/bin:/usr/sbin \
		$svscan $servicedir
	) > $logdir/svscan/svscan.log 2>&1 &
	sleep 1
	svpid=$(sed -n '$p' $servicedir/.svscan.pid)
	sudo kill -0 $svpid
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing svscan (without svscanlog) startup succeeded %49s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-1)}')
		printf "\r%118s\n" " "
		echo "  testing svscan (without svscanlog) startup failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/svscan.log
		)|less
		failed=1
		exit 1
	fi
	for i in smtpd smtpd/log qmail-send qmail-send/log
	do
		sudo svok $servicedir/smtpd
		if [ $? -eq 0 ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-1)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing service %-14s startup succeeded %53s [%.4f sec]\n" $i " " $secs
			print_pct
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-1)}')
			printf "\r%118s\n" " "
			echo "  testing service $i startup failed [$secs sec]"
			(
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
			echo "qmail-send.log"
			cat $logdir/qmail-send/current
			)|less
			failed=1
			exit 1
		fi
	done
}

test_stop_smtps()
{
	t1=$(date +"%s.%4N")
	sudo svc -d $servicedir/smtpd
	sleep $sleep_int
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port --tls-on-connect 2>/dev/null
	if [ $? -eq 2 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing stopping of service using svc succeeded %54s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing stopping of service using svc failed [$secs sec]"
		(
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi
}

test_stop_supervise()
{
	t1=$(date +"%s.%4N")
	sudo svc -dx $servicedir/smtpd
	sleep $sleep_int
	sudo svok $servicedir/smtpd
	if [ $? -eq 100 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing stopping of supervise using svc succeeded %52s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing stopping of supervise using svc failed [$secs sec]"
		(
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi
}

test_restart_service_using_hup()
{
	t1=$(date +"%s.%4N")
	sudo kill -1 $svpid
	sleep $sleep_int
	sudo svok $servicedir/smtpd
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing restarting of supervise by sending hup to svscan succeeded %35s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing restarting of supervise by sending hup to svscan failed [$secs sec]"
		(
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi
}

test_restart_service_using_svc()
{
	t1=$(date +"%s.%4N")
	tcpserver_pid1=$(sudo svstat $servicedir/smtpd|awk '{print $6}')
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	tcpserver_pid2=$(sudo svstat $servicedir/smtpd|awk '{print $6}')

	if [ $tcpserver_pid2 -ne $tcpserver_pid1 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_restart | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing restarting of supervise using svc succeeded %50s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_restart | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing restarting of supervise using svc failed [$secs sec]"
		(
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi
}

test_multilog()
{
	t1=$(date +"%s.%4N")
	grep "HELO <.*> MAIL from <$user@$HOSTNAME> RCPT <$user@$HOSTNAME> AUTH <local-rcpt> Size: .* TLS=TLSv1.3" $logdir/smtpd/current >/dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing logging using multilog succeeded %61s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "  testing logging using multilog failed [$secs sec]"
		(
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi
}

test_blocked()
{
	t1=$(date +"%s.%4N")
	case $1 in
		1|3|5|7|9|10|11|12)
		swaks -S --to blocked@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port >$tmpdir/swaks.$$
		;;
		2|4|6|8)
		swaks -S --to $user@$HOSTNAME  --from blocked@$HOSTNAME -server 127.0.0.1 --port $smtp_port >$tmpdir/swaks.$$
		;;
	esac
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$
	case $1 in
		1|3|5|7)
		echo "$l1" | grep ".*553 sorry, your envelope recipient has been denied (#5.7.1).*" >/dev/null
		if [ $? -eq 0 ] ; then
			ret=0
		else
			ret=1
		fi
		;;
		2|4|6|8)
		echo "$l1" | grep ".*553 sorry, your envelope sender has been denied (#5.7.1).*" >/dev/null
		if [ $? -eq 0 ] ; then
			ret=0
		else
			ret=1
		fi
		;;
		9|10|11|12)
		check_mail $testdir/$blockeduser/Maildir
		ret=$?
		/bin/rm -f $cntrldir/goodrcptto $cntrldir/goodrcptpatterns
		;;
	esac
	if [ $ret -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		tcount=$(expr $tcount + 1)
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		if [ $1 -eq 1 ] ; then
			printf "\r  testing badrcptto     exact match succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 2 ] ; then
			printf "\r  testing badmailfrom   exact match succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 3 ] ; then
			printf "\r  testing badrcptto   wildmat match succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 4 ] ; then
			printf "\r  testing badmailfrom wildmat match succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 5 ] ; then
			printf "\r  testing badrcptto     regex match succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 6 ] ; then
			printf "\r  testing badmailfrom   regex match succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 7 ] ; then
			printf "\r  testing badrcptpatterns   wildmat succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 8 ] ; then
			printf "\r  testing badmailpatterns   wildmat succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 9 ] ; then
			printf "\r  testing goodrcptto    exact match succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 10 ] ; then
			printf "\r  testing goodrcptto    regex match succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 11 ] ; then
			printf "\r  testing goodrcptto        wildmat succeeded %58s [%.4f sec]\n" " " $secs
		elif [ $1 -eq 12 ] ; then
			printf "\r  testing goodrcpttpatterns wildmat succeeded %58s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		if [ $1 -eq 1 ] ; then
			echo "  testing badrcptto  exact match failed [$secs sec]"
		elif [ $1 -eq 2 ] ; then
			echo "  testing badmailfrom exact match failed [$secs sec]"
		elif [ $1 -eq 3 ] ; then
			echo "  testing badrcptto wildmat match failed [$secs sec]"
		elif [ $1 -eq 4 ] ; then
			echo "  testing badmailfrom wildmat match failed [$secs sec]"
		elif [ $1 -eq 5 ] ; then
			echo "  testing badrcptto regex match failed [$secs sec]"
		elif [ $1 -eq 6 ] ; then
			echo "  testing badmailfrom regex match failed [$secs sec]"
		elif [ $1 -eq 7 ] ; then
			echo "  testing badrcptpatterns wildmat match failed [$secs sec]"
		elif [ $1 -eq 8 ] ; then
			echo "  testing badmailpatterns wildmat match failed [$secs sec]"
		elif [ $1 -eq 9 ] ; then
			printf "\r  testing goodrcptto exact match failed [%.4f sec]\n" $secs
		elif [ $1 -eq 10 ] ; then
			printf "\r  testing goodrcptto regex match failed [%.4f sec]\n" $secs
		elif [ $1 -eq 11 ] ; then
			printf "\r  testing goodrcptto wildmat match failed [%.4f sec]\n" $secs
		elif [ $1 -eq 12 ] ; then
			printf "\r  testing goodrcptpatterns wildmat failed [%.4f sec]\n" $secs
		fi
		(
		echo "l1=$l1, ret=$ret"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi
}

test_blackholed()
{
	t1=$(date +"%s.%4N")
	sed -i '$d' $sysconfdir/users/assign
	(
		echo "=blackholed:blackholed:$myuid:$mygid:$testdir/blackholed:::"
		echo "+blackholed-:blackholed:$myuid:$mygid:$testdir/blackholed:-::"
		echo "."
	) >> $sysconfdir/users/assign
	$qmail_newu $sysconfdir/users

	if [ $# -eq 0 ] ; then
		swaks -S --to blackholed@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port >$tmpdir/swaks.$$
	else
		swaks -S --to $user@$HOSTNAME  --from blackholed@$HOSTNAME -server 127.0.0.1 --port $smtp_port >$tmpdir/swaks.$$
	fi
	ret1=$?
	/bin/rm -f $tmpdir/swaks.$$
	if [ $# -eq 0 ] ; then
		check_mail $testdir/blackholed/Maildir 2>/dev/null
	else
		check_mail 2>/dev/null
	fi
	ret2=$?
	s1=$sleep_secs
	if [ $# -eq 0 ] ; then
		/bin/rm -f $cntrldir/blackholedrcpt
	else
		/bin/rm -f $cntrldir/blackholedsender
	fi
	if [ $# -eq 0 ] ; then
		swaks -S --to blackholed@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port >$tmpdir/swaks.$$
	else
		swaks -S --to $user@$HOSTNAME  --from blackholed@$HOSTNAME -server 127.0.0.1 --port $smtp_port >$tmpdir/swaks.$$
	fi
	ret3=$?
	if [ $# -eq 0 ] ; then
		check_mail $testdir/blackholed/Maildir
	else
		check_mail
	fi
	ret4=$?
	s2=$sleep_secs
	if [ $ret1 -eq 0 -a $ret2 -ne 0 -a $ret3 -eq 0 -a $ret4 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		if [ $# -eq 0 ] ; then
			tcount=$(expr $tcount + 1)
			printf "\r  testing blackholedrcpt   succeeded %67s [%.4f sec]\n" " " $secs
		else
			tcount=$(expr $tcount + 1)
			printf "\r  testing blackholedsender succeeded %67s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3, ret4=$ret4"
		if [ $# -eq 0 ] ; then
			echo "  testing blackholedrcpt failed [$secs sec]"
		else
			echo "  testing blackholedsender failed [$secs sec]"
		fi
		(
		echo "$logdir/smtpd/current"
		echo "$logdir/smtpd/current"
		cat $logdir/smtpd/current
		echo "$logdir/qmail-send/current"
		cat $logdir/qmail-send/current
		)|less
		failed=1
		exit 1
	fi
}

setup_svscan_with_svscanlog()
{
	sudo /bin/rm -rf $logdir/svscan
	mkdir -p $logdir/svscan
	sudo /bin/rm -f $testdir/svscan.out
	mkdir -p $servicedir/.svscan/log
	mkdir -p $servicedir/.svscan/variables
	printf "#!/bin/sh\nexec echo hello world > $testdir/svscan.out\n" > $servicedir/.svscan/run
	printf "#!/bin/sh\nexec echo goodbye world > $testdir/svscan.out\n" > $servicedir/.svscan/shutdown
	printf "#!/bin/sh\nexec $multilog t $logdir/svscan\n" > $servicedir/.svscan/log/run
	chmod +x $servicedir/.svscan/run
	chmod +x $servicedir/.svscan/shutdown
	chmod +x $servicedir/.svscan/log/run
	echo 1 > $servicedir/.svscan/variables/DISABLE_RUN
	echo 1 > $servicedir/.svscan/variables/SCANLOG
	echo 1 > $servicedir/.svscan/variables/SILENT
	echo   > $servicedir/.svscan/variables/INITCMD
	echo "/bin:/usr/sbin" > $servicedir/.svscan/variables/PATH
}

start_svscan_with_svscanlog()
{
	t1=$(date +"%s.%4N")
	(
	cd $servicedir/.svscan
	exec sudo $envdir -c variables $svscan $servicedir
	) 2>$logdir/svscan/svscan_startup.log &
	sleep $sleep_int
	svpid=$(sed -n '$p' $servicedir/.svscan.pid)
	sudo kill -0 $svpid
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing svscan (with svscanlog) startup succeeded %52s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing svscan (with svscanlog) startup failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	fi
}

test_svscan_log()
{
	t1=$(date +"%s.%4N")
	grep "@.* svscan: info: pid: .*: starting..." $logdir/svscan/current >/dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing svscan log succeeded %73s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "  testing svscan log failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	fi
}

test_svscan_run()
{
	t1=$(date +"%s.%4N")
	if [ "$(cat $testdir/svscan.out 2>/dev/null)" = "hello world" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing svscan run script execution succeeded %56s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "  testing svscan run script execution failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	fi
}

test_svscan_shutdown()
{
	t1=$(date +"%s.%4N")
	shutdown_svscan
	if [ "$(cat $testdir/svscan.out 2>/dev/null)" = "goodbye world" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing svscan shutdown script execution succeeded %51s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		shutdown_svscan
		printf "\r%118s\n" " "
		echo "  testing svscan shutdown script execution failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	fi
}

start_new_service()
{
	t1=$(date +"%s.%4N")
	sudo /bin/rm -rf $servicedir/sleep
	/bin/rm -f $testdir/sleep.out
	mkdir -p $servicedir/sleep/log
	printf "#!/bin/sh\nexec sleep 2243\n" > $servicedir/sleep/run
	printf "#!/bin/sh\nexec $multilog t $logdir/sleep\n" > $servicedir/sleep/log/run
	printf "#!/bin/sh\nexec echo shutdown sleep service > $testdir/sleep.out\n" > $servicedir/sleep/shutdown
	chmod +x $servicedir/sleep/run
	chmod +x $servicedir/sleep/log/run
	chmod +x $servicedir/sleep/shutdown
	if [ $# -eq 0 ] ; then # HUP
		sudo kill -1 $svpid
		sleep 5
	else
		sleep 6
	fi
	sudo svok $servicedir/sleep
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		if [ $# -eq 0 ] ; then
			tcount=$(expr $tcount + 1)
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-5)}')
			printf "\r  testing starting of supervise for new service by sending hup to svscan succeeded %21s [%.4f sec]\n" " " $secs
		else
			tcount=$(expr $tcount + 1)
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-6)}')
			printf "\r  testing starting of supervise for new service using svscan autoscan succeeded %24s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		t2=$(date +"%s.%4N")
		printf "\r%118s\n" " "
		if [ $# -eq 0 ] ; then
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-5)}')
			echo "  testing starting of supervise for new service by sending hup to svscan failed [$secs sec]"
		else
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-6)}')
			echo "  testing starting of supervise for new service using svscan autoscan failed [$secs sec]"
		fi
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	fi
	ps -ef |grep "sleep 2243"|grep -v grep > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		if [ $# -eq 0 ] ; then
			tcount=$(expr $tcount + 1)
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-5)}')
			printf "\r  testing starting of new service by sending hup to svscan succeeded %35s [%.4f sec]\n" " " $secs
		else
			tcount=$(expr $tcount + 1)
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-6)}')
			printf "\r  testing starting of new service using svscan autoscan succeeded %38s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		t2=$(date +"%s.%4N")
		printf "\r%118s\n" " "
		if [ $# -eq 0 ] ; then
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-5)}')
			echo "  testing starting of new service by sending hup to svscan failed [$secs sec]"
		else
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1-6)}')
			echo "  testing starting of new service using svscan autoscan failed [$secs sec]"
		fi
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	fi
	t1=$(date +"%s.%4N")
	sudo svc -d $servicedir/sleep
	sleep $sleep_int
	ps -ef |grep "sleep 2243"|grep -v grep > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing stopping of new service failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing stopping of new service succeeded %60s [%.4f sec]\n" " " $secs
		print_pct
	fi
	if [ "$(cat $testdir/sleep.out 2>/dev/null)" = "shutdown sleep service" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing shutdown script of new service succeeded %53s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing shutdown script of new service failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	fi
}

remove_new_service()
{
	t1=$(date +"%s.%4N")
	mv $servicedir/sleep $servicedir/.sleep
	sudo svc -dx $servicedir/.sleep $servicedir/.sleep/log
	sleep $sleep_int
	ps -ef |grep -E "sleep 2243|sleep 2244"|grep -v grep > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing removal of new service failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	else
		sudo /bin/rm -rf $servicedir/.sleep
		tcount=$(expr $tcount + 1)
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r  testing removal of new service succeeded %61s [%.4f sec]\n" " " $secs
		print_pct
	fi
}

test_supervise_subreaper()
{
	t1=$(date +"%s.%4N")
	printf "#!/bin/sh\nsleep 2244 &\n" > $servicedir/sleep/run
	chmod +t $servicedir/sleep/run
	sudo svc -u $servicedir/sleep
	sleep $sleep_int
	ps ajx |grep "sleep 2244"|grep -v grep > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing starting of new service in supbreaper mode succeeded %41s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing starting of new service in supbreaper mode failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	fi
	pgid=$(ps ajx|grep "sleep 2244"|grep -v grep|awk '{print $3}')
	if [ $pgid -ne $$ ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing setting of PGID (%7d != %7d) in subreaper mode succeeded %29s [%.4f sec]\n" $pgid "$$" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing setting of PGID ($pgid = $$) in subreaper mode failed [$secs sec]"
		echo "  testing starting of new service in supbreaper mode failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	fi
	sudo svc -d $servicedir/sleep
	sleep $sleep_int
	ps -ef |grep "sleep 2244"|grep -v grep > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-(2*$3))}')
		printf "\r%118s\n" " "
		echo "  testing stopping of new service in subreaper mode failed [$secs sec]"
		(
		echo "svscan.log"
		cat $logdir/svscan/current
		)|less
		failed=1
		exit 1
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int | awk '{printf("%0.4f\n", $2-$1-(2*$3))}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing stopping of new service in subreaper mode succeeded %42s [%.4f sec]\n" " " $secs
		print_pct
	fi
}

create_tcpclient_smtp()
{
	list=$*
	(
	echo "#!/bin/sh"
	echo "exec 0<&6"
	echo "exec 1>&7"
	echo "read key"
	echo "greeting=\$(echo \$key | awk '{print \$1}')"
	echo "if [ \$greeting -ne 220 ] ; then"
	echo "	echo \"Greeting failed\" 1>&2"
	echo "	printf \"QUIT\r\n\""
	echo "fi"
	echo "printf \"EHLO\r\n\""
	echo "while true"
	echo "do"
	echo "	read line"
	echo "	echo \$line |grep \"250 \" >/dev/null"
	echo "	if [ \$? -eq 0 ] ; then"
	echo "		echo \$line 1>&2"
	echo "		break"
	echo "	fi"
	echo "	echo \$line 1>&2"
	echo "done"
	echo "printf \"MAIL FROM:<$user@$HOSTNAME>\r\n\" 1>&2"
	echo "printf \"MAIL FROM:<$user@$HOSTNAME>\r\n\""
	echo "read key"
	echo "echo \$key 1>&2"
	echo "code=\$(echo \$key |cut -c1-3)"
	echo "if [ \$code -ne 250 ] ; then"
	echo "	echo \"MAIL From failed: code=\$code\" 1>&2"
	echo "	exit 1"
	echo "fi"
	for i in $list
	do
		echo "printf \"RCPT TO:<$i>\r\n\" 1>&2"
		echo "printf \"RCPT TO:<$i>\r\n\""
		echo "read key"
		echo "echo \$key 1>&2"
		echo "code=\$(echo \$key |cut -c1-3)"
		echo "if [ \$code -ne 250 ] ; then"
		echo "	echo \"RCPT To failed: code=\$code\" 1>&2"
		echo "	exit 1"
		echo "fi"
	done
	echo "printf \"QUIT\r\n\" 1>&2"
	echo "printf \"QUIT\r\n\""
	echo "read key"
	echo "echo \$key 1>&2"
	echo "exit 0"
	) > $testdir/tcpclient.smtp
	chmod +x $testdir/tcpclient.smtp
}

test_recipients()
{
	/bin/rm -f $servicedir/smtpd/variables/SMTPS
	(
	echo "apollo.indimail.org"
	echo "mediacenter.indimail.org"
	echo "nocheck.com"
	echo "etrn1.dom"
	echo "etrn2.dom"
	echo "atrn.dom"
	) >> $cntrldir/rcpthosts
	sort -u $cntrldir/rcpthosts -o $cntrldir/rcpthosts
	echo           3 > $servicedir/smtpd/variables/CHECKRECIPIENT
	echo $sysconfdir > $servicedir/smtpd/variables/SYSCONFDIR
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")

	(
	echo "!nocheck.com"
	echo "@$HOSTNAME:users/recipients.cdb"
	echo "indimail.org:users/extra.cdb"
	echo "*:users/recipients.cdb"
	echo "users/legacy.cdb"
	) > $cntrldir/recipients

	(
	echo user1@$HOSTNAME
	echo user2@mediacenter.indimail.org
	) > $sysconfdir/users/recipients

	echo user1@apollo.indimail.org > $sysconfdir/users/extra
	echo user3@apollo.indimail.org > $sysconfdir/users/legacy
	env SYSCONFDIR=$sysconfdir /usr/sbin/qmail-cdb -r
	r1=$?
	env SYSCONFDIR=$sysconfdir /usr/sbin/qmail-cdb -r extra
	r2=$?
	env SYSCONFDIR=$sysconfdir /usr/sbin/qmail-cdb -r legacy
	r3=$?
	if [ $r1 -eq 0 -a $r2 -eq 0 -a $r3 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-cdb succeeded %74s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-cdb failed [$secs sec]"
		> $servicedir/smtpd/variables/CHECKRECIPIENT
		failed=1
		exit 1
	fi

	# Pass1: This should succeed
	create_tcpclient_smtp user1@nocheck.com user1@$HOSTNAME user1@apollo.indimail.org \
		user2@mediacenter.indimail.org user3@apollo.indimail.org
	t1=$(date +"%s.%4N")
	tcpclient -vDHR 0 $smtp_port $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
	ret1=$?
	if [ $ret1 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing recipient extension (positive match) succeeded %47s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "  testing recipient extension (positive match) failed [$secs sec]"
		(
		echo "tcpclient.log"
		cat $logdir/tcpclient/tcpclient.log
		)|less
		> $servicedir/smtpd/variables/CHECKRECIPIENT
		failed=1
		exit 1
	fi

	# Pass2: This should fail
	create_tcpclient_smtp user3@$HOSTNAME
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")
	tcpclient -vDHR 0 $smtp_port $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
	ret2=$?
	l1=$(tail -2 $logdir/tcpclient/tcpclient.log|sed -n 1p)
	echo "$l1"|grep "550 sorry, $user@$HOSTNAME mailbox <user3@$HOSTNAME> is absent on this domain (#5.1.1)" > /dev/null
	if [ $? -eq 0 ] ; then
		ret3=0
	else
		ret3=1
	fi
	if [ $ret2 -eq 1 -a $ret3 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing recipient extension (negative match) succeeded %47s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "  testing recipient extension (negative match) failed [$secs sec]"
		(
		echo "l1=$l1"
		echo "ret2=$ret2, ret3=$ret3"
		echo "tcpclient.log"
		cat $logdir/tcpclient/tcpclient.log
		)|less
		> $servicedir/smtpd/variables/CHECKRECIPIENT
		failed=1
		exit 1
	fi

	# Pass3: This should succeed
	echo "!*" >> $cntrldir/recipients
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")
	tcpclient -vDHR 0 $smtp_port $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
	ret4=$?

	if [ $ret4 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing recipient extension (wildcard match) succeeded %47s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		> $servicedir/smtpd/variables/CHECKRECIPIENT
		printf "\r%118s\n" " "
		echo "  testing recipient extension (wildcard match) failed [$secs sec]"
		(
		echo "tcpclient.log"
		cat $logdir/tcpclient/tcpclient.log
		)|less
		failed=1
		exit 1
	fi

	sed -i '$d' $cntrldir/recipients
	create_tcpclient_smtp user3@$HOSTNAME
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart

	# This should fail
	t1=$(date +"%s.%4N")
	tcpclient -vDHR 0 $smtp_port $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
	ret1=$?
	l1=$(tail -2 $logdir/tcpclient/tcpclient.log|sed -n 1p)
	echo "$l1"|grep "550 sorry, $user@$HOSTNAME mailbox <user3@$HOSTNAME> is absent on this domain (#5.1.1)" > /dev/null
	if [ $? -eq 0 ] ; then
		ret2=0
	else
		ret2=1
	fi
	if [ $ret1 -ne 1 -o $ret2 -ne 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		> $servicedir/smtpd/variables/CHECKRECIPIENT
		printf "\r%118s\n" " "
		echo "  testing recipient extension (chkrcptdomains control file) failed [$secs sec]"
		(
		echo "l1=$l1"
		echo "ret1=$ret1, ret2=$ret2"
		echo "tcpclient.log"
		cat $logdir/tcpclient/tcpclient.log
		)|less
		failed=1
		exit 1
	fi

	# now test checkrcptdomains
	touch $cntrldir/chkrcptdomains
	# This should work
	t1=$(date +"%s.%4N")
	tcpclient -vDHR 0 $smtp_port $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
	ret1=$?
	if [ $ret1 -ne 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		> $servicedir/smtpd/variables/CHECKRECIPIENT
		/bin/rm -f $cntrldir/chkrcptdomains
		printf "\r%118s\n" " "
		echo "  testing recipient extension (chkrcptdomains control file) failed [$secs sec]"
		(
		echo "tcpclient.log"
		cat $logdir/tcpclient/tcpclient.log
		)|less
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	echo "@$HOSTNAME" > $cntrldir/chkrcptdomains
	# This should fail
	tcpclient -vDHR 0 $smtp_port $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
	ret1=$?
	l1=$(tail -2 $logdir/tcpclient/tcpclient.log|sed -n 1p)
	echo "$l1"|grep "550 sorry, $user@$HOSTNAME mailbox <user3@$HOSTNAME> is absent on this domain (#5.1.1)" > /dev/null
	if [ $? -eq 0 ] ; then
		ret2=0
	else
		ret2=1
	fi
	/bin/rm -f $cntrldir/chkrcptdomains
	> $servicedir/smtpd/variables/CHECKRECIPIENT
	if [ $ret1 -eq 1 -a $ret2 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing recipient extension (chkrcptdomains control file) succeeded %34s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "  testing recipient extension (chkrcptdomains control file) failed [$secs sec]"
		(
		echo "l1=$l1"
		echo "ret1=$ret1, ret2=$ret2"
		echo "tcpclient.log"
		cat $logdir/tcpclient/tcpclient.log
		)|less
		> $servicedir/smtpd/variables/CHECKRECIPIENT
		failed=1
		exit 1
	fi
}

do_setup()
{
	t1=$(date +"%s.%4N")
	if [ -f $certdir/servercert.pem ] ; then
		echo "Starting Setup (this will take a moment)"
	else
		echo "Starting Setup (this will take some time)"
	fi
	# basic setup for maildir, assign config and queue
	sudo /bin/rm -rf $logdir
	mkdir -p $logdir/qmail-remote
	mkdir -p $logdir/mail
	mkdir -p $logdir/tcpclient
	mkdir -p $logdir/qmail-send
	mkdir -p $logdir/svscan
	mkdir -p $logdir/smtpd
	mkdir -p $logdir/qmtpd
	mkdir -p $tmpdir
	chmod 1777 $tmpdir
	mkdir -p $testdir/bin
	mkdir -p $testdir/run
	setup_maildir
	sed -e "s|/var/indimail|$qmaildir|" -e "s|/home/$user|$testdir/$user|" \
		/etc/passwd > $testdir/etc/passwd
	setup_assign
	setup_config
	setup_queue
}

setup_srs()
{
	if [ $1 -eq 1 ] ; then
		echo "$srs_domain"      > $cntrldir/srs_domain
		echo "$srs_domain:srs"  > $cntrldir/virtualdomains
		sudo sh -c "echo \"|$srsfilter\" > $qmaildir/alias/.qmail-srs-default"
		tr -dc 'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' \
			</dev/urandom | head -c 24 > $cntrldir/srs_secrets
		echo 7 > $cntrldir/srs_maxage
		echo 4 > $cntrldir/srs_hashlength
		echo 4 > $cntrldir/srs_hashmin
		echo = > $cntrldir/srs_separator
		echo 0 > $cntrldir/srs_alwaysrewrite
	else
		/bin/rm -f $cntrldir/srs_domain
		/bin/rm -f $cntrldir/srs_secrets
		/bin/rm -f $cntrldir/srs_maxage
		/bin/rm -f $cntrldir/srs_hashlength
		/bin/rm -f $cntrldir/srs_hashmin
		/bin/rm -f $cntrldir/srs_separator
		/bin/rm -f $cntrldir/srs_alwaysrewrite
		/bin/rm -f $cntrldir/virtualdomains
		sudo /bin/rm -f $qmaildir/alias/.qmail-srs-default
	fi
}

setup_virtual()
{
	if [ $1 -eq 1 ] ; then
		localip=$(/usr/libexec/indimail/ipmeprint | grep -E -v "ipv6|ipv4 -> 127|ipv4 -> 0"|head -1|awk '{print $3}')
		u=$(id -u qmaild)
		g=$(grep "^nofiles:" /etc/group | awk -F: '{print $3}')
		(
		echo "$vdomain:$vdomain"
		echo "etrn1.dom:autoturn-$localip"
		echo "etrn2.dom:autoturn-etrn2.dom"
		echo "atrn.dom:autoturn-atrn.dom"
		) > $cntrldir/virtualdomains
		if [ ! -d $qmaildir/autoturn/$vdomain ] ; then
			mkdir -p $qmaildir/autoturn/$vdomain
		fi
		if [ ! -d $qmaildir/autoturn/$vdomain/Maildir ] ; then
			maildirmake $qmaildir/autoturn/$vdomain/Maildir
		fi
		echo "$qmaildir/autoturn/$vdomain/Maildir/" > $qmaildir/autoturn/$vdomain/.qmail-default
		for i in $localip etrn2.dom atrn.dom
		do
			if [ ! -d $qmaildir/autoturn/$i ] ; then
				mkdir -p $qmaildir/autoturn/$i
			fi
			if [ ! -d $qmaildir/autoturn/$i/Maildir ] ; then
				maildirmake $qmaildir/autoturn/$i/Maildir
			fi
			qfn=$(echo $i | sed -e 's{\.{:{g')
			sudo sh -c "echo $qmaildir/autoturn/$i/Maildir/ > $qmaildir/autoturn/.qmail-$qfn-default"
			sudo chown -R $u:$g $qmaildir/autoturn/$i $qmaildir/autoturn/.qmail-$qfn-default
		done
		send_pid=$(get_send_pid qmail-send $testdir/queue)
		if [ -n "$send_pid" ] ; then
			sudo kill -1 $send_pid
		fi
	else
		for i in $localip etrn2.dom atrn.dom
		do
			qfn=$(echo $i | sed -e 's{\.{:{g')
			if [ "$i" != "$vdomain" ] ; then
				sudo /bin/rm -rf $qmaildir/autoturn/$i $qmaildir/autoturn/.qmail-$qfn-default
			fi
		done
		/bin/rm -rf $qmaildir/autoturn/$vdomain $cntrldir/virtualdomains
		send_pid=$(get_send_pid qmail-send $testdir/queue)
		if [ -n "$send_pid" ] ; then
			sudo kill -1 $send_pid
		fi
	fi
}

test_maildirserial()
{
	for i in 1 2 3 4 5
	do
		t1=$(date +"%s.%4N")
		(
		echo "From: $user@$HOSTNAME"
		echo "To: $user@$vdomain"
		echo "Subject: Test No $i $$"
		echo "Date: $(date -R)"
		echo
		echo "Test Message $i $$"
		) | env - \
			QUEUEDIR=$testdir/queue \
			QMAILQUEUE=$qmail_queue \
			CONTROLDIR=$cntrldir \
			BIGTODO=0 \
			CONFSPLIT=23 \
			$qmail_inject -f$user@$HOSTNAME $user@$vdomain
		check_mail_header $qmaildir/autoturn/$vdomain/Maildir Subject 0
		s1=$sleep_secs
		if [ -n "$header" ] ; then
			subj_header=$header
		else
			subj_header=""
		fi
		check_mail_header $qmaildir/autoturn/$vdomain/Maildir Delivered-To 0
		s2=$sleep_secs
		total=$(echo $s1 $s2 | awk '{printf("%0.4f\n", $1+$2)}')
		if [ -n "$header" ] ; then
			dto_header=$header
		else
			dto_header=""
		fi
		if [ -n "$mail_file" ] ; then
			body=$($e822body < $qmaildir/autoturn/$vdomain/Maildir/new/$mail_file)
			sed -i "s/$vdomain-$user@$vdomain/$vdomain-$user@$HOSTNAME/" \
				$qmaildir/autoturn/$vdomain/Maildir/new/$mail_file
			mv $qmaildir/autoturn/$vdomain/Maildir/new/$mail_file \
				$qmaildir/autoturn/$vdomain/Maildir/cur/$mail_file
		fi
		if [ -n "$mail_file" -a "$dto_header" = "Delivered-To: $vdomain-$user@$vdomain" -a "$subj_header" = "Subject: Test No $i $$"  -a "$body" = "Test Message $i $$" ] ; then
			continue
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
			printf "\r%118s\n" " "
			echo "  testing qmail-inject virtual user, qmail send+receive failed [$secs sec]"
			(
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
			echo "$logdir/qmail-send/qmail-send.log"
			cat $logdir/qmail-send/qmail-send.log
			)|less
			failed=1
			exit 1
		fi
	done
	check_mail_count $qmaildir/autoturn/$vdomain/Maildir 5
	ret=$?
	if [ $ret -eq 0 -a $mcount -eq 5 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $total $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-inject virtual user, qmail send+receive succeeded %38s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $total $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-inject virtual user, qmail send+receive failed [$secs sec]"
		(
		echo "ret=$ret, mcount1=$mcount"
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		echo "$logdir/qmail-send/qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		exit 1
	fi

	mkdir $qmaildir/autoturn/$vdomain/Maildir/bak
	#cp -rp $qmaildir/autoturn/$vdomain/Maildir/cur $qmaildir/autoturn/$vdomain/Maildir/bak
	cp -p $qmaildir/autoturn/$vdomain/Maildir/cur/* $qmaildir/autoturn/$vdomain/Maildir/bak
	sleep 1 # else maildirserial picks up files older than 1 sec
	t1=$(date +"%s.%4N")
	echo "$HOSTNAME:127.0.0.1:$smtp_port $user@$HOSTNAME abcd12345678" > $cntrldir/smtproutes
	tcpclient 127.0.0.1 $smtp_port maildirserial -t1209600 $qmaildir/autoturn/$vdomain/Maildir "$vdomain-" \
		serialsmtp "$vdomain-" serialsmtp >$tmpdir/tcpclient.log 2>&1
	check_mail_count $maildir 5
	ret=$?
	if [ $ret -eq 0 -a $mcount -eq 5 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing maildirserial, serialsmtp succeeded %58s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing maildirserial, serialsmtp failed [$secs sec]"
		(
		echo "ret=$ret, mcount=$mcount"
		echo "$tmpdir/tcpclient.log"
		cat $tmpdir/tcpclient.log
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		echo "$logdir/qmail-send/qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		/bin/rm -f $tmpdir/tcpclient.log
		failed=1
		exit 1
	fi
	t1=$(date +"%s.%4N")
	/bin/rm -rf $qmaildir/autoturn/$vdomain/Maildir/new
	mv $qmaildir/autoturn/$vdomain/Maildir/bak $qmaildir/autoturn/$vdomain/Maildir/new
	find $maildir -type f -exec /bin/rm -f {} \;
	tcpclient 127.0.0.1 $qmtp_port maildirserial -t1209600 $qmaildir/autoturn/$vdomain/Maildir "$vdomain-" \
		serialqmtp "$vdomain-" serialqmtp >$tmpdir/tcpclient.log 2>&1
	check_mail_count $maildir 5
	ret=$?
	if [ $ret -eq 0 -a $mcount -eq 5 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing maildirserial, serialqmtp succeeded %58s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing maildirserial, serialqmtp failed [$secs sec]"
		(
		echo "ret=$ret, mcount=$mcount"
		echo "$tmpdir/tcpclient.log"
		cat $tmpdir/tcpclient.log
		echo "$logdir/qmtpd/qmtpd.log"
		cat $logdir/qmtpd/qmtpd.log
		echo "$logdir/qmail-send/qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		/bin/rm -f $tmpdir/tcpclient.log
		exit 1
	fi
	/bin/rm -f $tmpdir/tcpclient.log
	if [ -d $maildir ] ; then
		find $maildir -type f -exec /bin/rm -f {} \;
	fi
}

test_autoresponder()
{
	remove_mail
	t1=$(date +"%s.%4N")
	if [ $# -eq 1 ] ; then
		do_srs=$1
	else
		do_srs=0
	fi
	if [ -d $testdir/.vacation.dir ] ; then
		/bin/rm -f $testdir/.vacation.dir/*
	else
		mkdir -p $testdir/.vacation.dir
	fi
	echo "I'm on leave" > $testdir/.vacation.msg
	(
	echo "|$autoresponder -q $testdir/.vacation.msg $testdir/.vacation.dir"
	echo "$testdir/$testuser/Maildir/"
	) > $testdir/$testuser/.qmail
	subject="Testing Autoresponder"
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: $subject"
	echo "Date: $(date -R)"
	echo
	echo "Test message"
	) > $tmpdir/mail.txt

	if [ $do_srs -eq 0 ] ; then
		env - \
			QUEUEDIR=$testdir/queue \
			QMAILQUEUE=$qmail_queue \
			CONTROLDIR=$cntrldir \
			BIGTODO=0 \
			CONFSPLIT=23 \
		$qmail_inject -f$user@$HOSTNAME $testuser <$tmpdir/mail.txt
	else
		srs_addr=$(srs -k `cat $cntrldir/srs_secrets` -i $srs_domain $user@$HOSTNAME)
		env - \
			QUEUEDIR=$testdir/queue \
			QMAILQUEUE=$qmail_queue \
			CONTROLDIR=$cntrldir \
			BIGTODO=0 \
			CONFSPLIT=23 \
		$qmail_inject -f$srs_addr $testuser <$tmpdir/mail.txt
	fi
	/bin/rm -f $tmpdir/mail.txt
	reply_subject="Subject: Autoreply: Re: $subject"
	count=0
	ret=1
	subject=""
	check_mail_header $maildir Subject 1
	subject=$header
	if [ "$reply_subject" = "$subject" ] ; then
		/bin/rm -f $testdir/$testuser/Maildir/new/*
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		if [ $do_srs -eq 1 ] ; then
			printf "\r  testing autoresponder with SRS decode succeeded %54s [%.4f sec]\n" " " $secs
		else
			printf "\r  testing autoresponder succeeded %70s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		/bin/rm -f $testdir/$testuser/Maildir/new/*
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		if [ $do_srs -eq 1 ] ; then
			printf "  testing autoresponder with SRS decode failed [%.4f sec]\n" $secs
		else
			printf "\r  testing autoresponder succeeded [%.4f sec]\n" $secs
			print_pct
		fi
		(
		echo "Expected subject as $reply_subject but got $subject"
		echo "qmail-send.log"
		if [ -d $logdir/qmail-send/qmail-send.log ] ; then
			cat $logdir/qmail-send/qmail-send.log
		else
			cat $logdir/qmail-send/current
		fi
		)|less
		failed=1
		exit 1
	fi
	/bin/rm -f $testdir/.vacation.dir/* $testdir/$testuser/.qmail
}

test_srs()
{
	t1=$(date +"%s.%4N")
	srs_addr=$(srs -k `cat $cntrldir/srs_secrets` -i $srs_domain $user@$HOSTNAME)
	echo $srs_addr | grep "SRS0=.*=.*=$HOSTNAME=$user@$srs_domain" > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing create SRS forward address succeeded %57s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		printf "  testing create SRS forward address failed [%.4f sec]\n" $secs
		failed=1
		exit 1
	fi
	t1=$(date +"%s.%4N")
	srs_addr=$(srs -k `cat $cntrldir/srs_secrets` -v $srs_addr)
	if [ "$srs_addr" = "$user@$HOSTNAME" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing SRS decode address succeeded %65s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		printf "  testing SRS decode address failed [%.4f sec]\n" $secs
		failed=1
		exit 1
	fi
	t1=$(date +"%s.%4N")
	subject="Testing SRS rewrite using qmail-inject"
	(
	echo "From: $user@$ext_domain"
	echo "To: $user@$HOSTNAME"
	echo "Subject: $subject"
	echo "Date: $(date -R)"
	echo
	echo "Test message"
	) | env - \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
		QMAILINJECT_FORCE_SRS=1 \
	$qmail_inject -f$user@$ext_domain $user@$HOSTNAME
	check_mail_header $maildir Return-Path 1
	srs_addr=$(srs -k `cat $cntrldir/srs_secrets` -i $srs_domain $user@$ext_domain)
	if [ "$header" = "Return-Path: <$srs_addr>" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing SRS rewrite qmail-inject succeeded %59s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		printf "  testing SRS rewrite qmail-inject failed [%.4f sec]\n" $secs
		echo "$header != Return-Path: <$srs_addr>"
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	srs_addr=$(srs -k `cat $cntrldir/srs_secrets` -i $srs_domain $user@$HOSTNAME)
	echo 1 > $cntrldir/srs_alwaysrewrite
	swaks -S --to $srs_addr --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port
	echo 0 > $cntrldir/srs_alwaysrewrite
	check_mail_header $maildir Delivered-To 1
	if [ "$header" = "Delivered-To: $user@$HOSTNAME" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing SRS decode RCPT address qmail-smtpd succeeded %48s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		echo "$header != Delivered-To: $user@$HOSTNAME"
		printf "\r%118s\n" " "
		printf "  testing SRS decode RCPT address qmail-smtpd failed [%.4f sec]\n" $secs
		failed=1
		exit 1
	fi
	test_autoresponder 1
	t1=$(date +"%s.%4N")
	subject="Testing SRS decode using srsfilter"
	(
	echo "From: $user@$ext_domain"
	echo "To: $user@$HOSTNAME"
	echo "Subject: $subject"
	echo "Date: $(date -R)"
	echo
	echo "Test message"
	) | env - \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
		SENDER="" \
		EXT2=$srs_addr \
		HOST=$HOSTNAME \
	$srsfilter >/dev/null 2>&1
	srs_ret=$?
	check_mail_header $maildir Subject 1
	if [ $srs_ret -eq 0 -a "$header" = "Subject: $subject" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing SRS decode using srsfilter succeeded %57s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		printf "  testing SRS decode using srsfilter failed [%.4f sec]\n" $secs
		echo "ret=$ret, \"$header\" != \"Subject: $subject\""
		failed=1
		exit 1
	fi
}

test_alias()
{
	for alias in bumblebee mailer-daemon root
	do
		t1=$(date +"%s.%4N")
		if [ "$alias" = "root" ] ; then
			sudo /bin/rm -f $qmaildir/alias/.qmail-root
			(
			echo "$qmaildir/alias/Maildir/"
			echo "$user@$HOSTNAME"
			) > $tmpdir/.qmail-root
			sudo mv $tmpdir/.qmail-root $qmaildir/alias
			sudo chown root:qmail $qmaildir/alias/.qmail-root
		fi
		subject="Testing alias mechanism for $alias"
		(
		echo "From: $user@$HOSTNAME"
		echo "To: $alias@$HOSTNAME"
		echo "Subject: $subject"
		echo "Date: $(date -R)"
		echo
		echo "Test message for $alias"
		) | env - \
			QUEUEDIR=$testdir/queue \
			QMAILQUEUE=$qmail_queue \
			CONTROLDIR=$cntrldir \
			BIGTODO=0 \
			CONFSPLIT=23 \
		$qmail_inject -f$user@$HOSTNAME $alias@$HOSTNAME
		if [ "$alias" = "bumblebee" ] ; then
			check_mail_header $maildir From 0
			s1=$sleep_secs
			from_header=$header
			check_mail_header $maildir Subject 1
			s2=$sleep_secs
			subj_header=$header
			if [ "$from_header" = "From: MAILER-DAEMON@$HOSTNAME" -a "$subj_header" = "Subject: failure notice" ] ; then
				a_ret=0
			else
				a_ret=1
			fi
		else
			check_mail_header $maildir To 1
			s1=$sleep_secs
			s2=0.0
			a_ret=$?
			if [ "$header" != "To: $alias@$HOSTNAME" ] ; then
				a_ret=1
			fi
		fi
		if [ $a_ret -eq 0 ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
			tcount=$(expr $tcount + 1)
			if [ "$alias" = "bumblebee" ] ; then
				printf "\r  testing non-existing alias    for %13s succeeded %44s [%.4f sec]\n" $alias " " $secs
			else
				printf "\r  testing     existing alias    for %13s succeeded %44s [%.4f sec]\n" $alias " " $secs
			fi
			print_pct
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
			printf "\r%118s\n" " "
			if [ "$alias" = "bumblebee" ] ; then
				echo "  testing non-existing alias for $alias failed [$secs sec]"
			else
				echo "  testing existing alias for $alias failed [$secs sec]"
			fi
			(
			echo "ret=$a_ret, \"$header\" != \"To: $alias@$HOSTNAME\""
			cat $logdir/qmail-send/qmail-send.log
			)|less
			failed=1
			exit 1
		fi
		if [ "$alias" = "root" ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing alternate  QMAILGETPW for %13s succeeded %44s [%.4f sec]\n" $alias " " $secs
			print_pct
		fi
	done
}

test_lspawn_rspawn_filter()
{
	# test QMAILLOCAL through qmail-lspawn. Default is to match recipient domain in filterargs
	# hence filter 2 should be in effect
	(
	echo "#!/bin/sh"
	echo "sed -e 's/abcd/$HOSTNAME/' -e 's/Subject: .*/Subject: Testing spawn-filter using filter1/'"
	echo "exit 0"
	) > $testdir/bin/filter1
	(
	echo "#!/bin/sh"
	echo "sed -e 's/abcd/$HOSTNAME/' -e 's/Subject: .*/Subject: Testing spawn-filter using filter2/'"
	echo "exit 0"
	) > $testdir/bin/filter2
	chmod +x $testdir/bin/filter1 $testdir/bin/filter2

	sub1=$(echo $local_domain1 | cut -d. -f2-) # sender    domain
	sub2=$(echo $local_domain2 | cut -d. -f2-) # recipient domain
	if [ $1 -eq 1 ] ; then # regular exp for pattern match
		(
		echo ".*\.$sub1:local:$testdir/bin/filter1"
		echo ".*\.$sub2:local:$testdir/bin/filter2"
		) > $cntrldir/filterargs
	else # wildmat for pattern match
		(
		echo "*.$sub1:local:$testdir/bin/filter1"
		echo "*.$sub2:local:$testdir/bin/filter2"
		) > $cntrldir/filterargs
	fi

	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$local_domain1"
	echo "To: $user@$local_domain2"
	echo "Subject: Test message using qmail-inject"
	echo "Date: $(date -R)"
	echo
	echo "Test message from abcd"
	) | env - \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$local_domain1 $user@$local_domain2
	check_mail $maildir 1
	if [ -n "$mail_file" ] ; then
		body=$($e822body < $maildir/new/$mail_file)
		subject=$($e822header -I Subject < $maildir/new/$mail_file)
		/bin/rm -f $maildir/new/$mail_file
	else
		body=""
		subject=""
	fi
	if [ "$body" = "Test message from $HOSTNAME" -a "$subject" = "Subject: Testing spawn-filter using filter2" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		if [ $1 -eq 1 ] ; then
			printf "\r  testing spawn-filter REGEXP  qmail-lspawn using QMAILLOCAL  (recipient-match) succeeded %14s [%.4f sec]\n" " " $secs
		else
			printf "\r  testing spawn-filter wildmat qmail-lspawn using QMAILLOCAL  (recipient-match) succeeded %14s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		printf "\r%118s\n" " "
		if [ $1 -eq 1 ] ; then
			echo "testing spawn-filter REGEXP qmail-lspawn using (recipient-match) QMAILLOCAL failed"
		else
			echo "testing spawn-filter wildmat qmail-lspawn using (recipient-match) QMAILLOCAL failed"
		fi
		(
		echo "body=[$body], subject=[$subject]"
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/qmail-send/qmail-send.log"
			cat $logdir/qmail-send/qmail-send.log
		elif [ -f $logdir/qmail-send/current ] ; then
			echo "$logdir/qmail-send/current"
			cat $logdir/qmail-send/current
		fi
		)|less
		failed=1
		/bin/rm -f $cntrldir/filterargs $cntrldir/smtproutes $testdir/bin/filter1 $testdir/bin/filter2
		exit 1
	fi

	# test QMAILREMOTE through qmail-rspawn. Default is to match sender domain in filterargs
	# during setup we re-routed all mails to for external domains to $domain
	# using $sbindir/rd-remote and $cntrldir/smtproutes
	#
	t1=$(date +"%s.%4N")
	(
	echo "$remote_domain1:127.0.0.1:$smtp_port"
	echo "$remote_domain2:127.0.0.1:$smtp_port"
	echo "$HOSTNAME:127.0.0.1:$smtp_port"
	) > $cntrldir/smtproutes
	sub1=$(echo $remote_domain1 | cut -d. -f2-)
	sub2=$(echo $remote_domain2 | cut -d. -f2-)
	if [ $1 -eq 1 ] ; then # regular exp for pattern match
		echo ".*:d:$testuser@$HOSTNAME:remote"    > $cntrldir/redirectremote
		(
		echo ".*\.$sub1:remote:$testdir/bin/filter1"
		echo ".*\.$sub2:remote:$testdir/bin/filter2"
		) > $cntrldir/filterargs
	else # wildmat for pattern match
		echo "*:d:$testuser@$HOSTNAME:remote"    > $cntrldir/redirectremote
		(
		echo "*.$sub1:remote:$testdir/bin/filter1"
		echo "*.$sub2:remote:$testdir/bin/filter2"
		) > $cntrldir/filterargs
	fi

	(
	echo "From: $user@$remote_domain1"
	echo "To: $user@$remote_domain2"
	echo "Subject: Test message using qmail-inject"
	echo "Date: $(date -R)"
	echo
	echo "Test message from abcd"
	) | env - \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$remote_domain1 $user@$remote_domain2
	check_mail_header $testdir/$testuser/Maildir Subject 0
	if [ -z "$mail_file" ] ; then
		check_mail_header $testdir/$testuser/Maildir/.Spam Subject 0
		if [ -f $testdir/$testuser/Maildir/.Spam/new/$mail_file ] ; then
			mv $testdir/$testuser/Maildir/.Spam/new/$mail_file $testdir/$testuser/Maildir/new
		fi
	fi
	if [ -n "$mail_file" ] ; then
		subject=$header
		body=$($e822body < $testdir/$testuser/Maildir/new/$mail_file)
		/bin/rm -f $testdir/$testuser/Maildir/new/$mail_file
	else
		body=""
		subject=""
	fi
	rd_remote_flag=0
	if [ "$body" = "Test message from $HOSTNAME" -a "$subject" = "Subject: Testing spawn-filter using filter1" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		if [ $1 -eq 1 ] ; then
			rd_remote_flag=1
			printf "\r  testing spawn-filter REGEXP  qmail-rspawn using QMAILREMOTE (sender-match)    succeeded %14s [%.4f sec]\n" " " $secs
		else
			rd_remote_flag=1
			printf "\r  testing spawn-filter wildmat qmail-rspawn using QMAILREMOTE (sender-match)    succeeded %14s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		printf "\r%118s\n" " "
		if [ $1 -eq 1 ] ; then
			echo "testing spawn-filter REGEXP qmail-rspawn using QMAILREMOTE (sender-match) failed"
		else
			echo "testing spawn-filter wildmat qmail-rspawn using QMAILREMOTE (sender-match) failed"
		fi
		(
		echo "body=$body, subject=$subject"
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
			echo "$logdir/qmail-send/qmail-send.log"
			cat $logdir/qmail-send/qmail-send.log
		elif [ -f $logdir/qmail-send/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
			echo "$logdir/qmail-send/current"
			cat $logdir/qmail-send/current
		fi
		)|less
		failed=1
		/bin/rm -f $cntrldir/filterargs $cntrldir/smtproutes $testdir/bin/filter1 $testdir/bin/filter2
		exit 1
	fi
	/bin/rm -f $cntrldir/filterargs $cntrldir/smtproutes $testdir/bin/filter1 $testdir/bin/filter2
	if [ $rd_remote_flag -eq 1 ] ; then
		printf "\r  testing rd-remote qmail-rspawn using QREMOTE succeeded %47s [%.4f sec]\n" " " $secs
		print_pct
		tcount=$(expr $tcount + 1)
	else
		printf "\r%118s\n" " "
		echo "testing rd-remote qmail-rspawn using QREMOTE failed"
		(
		echo "body=$body, subject=$subject"
		if [ $log_type -eq 1 ] ; then
			echo "$logdir/smtpd/smtpd.log"
			cat $logdir/smtpd/smtpd.log
			echo "$logdir/qmail-send/qmail-send.log"
			cat $logdir/qmail-send/qmail-send.log
		elif [ -f $logdir/qmail-send/current ] ; then
			echo "$logdir/smtpd/current"
			cat $logdir/smtpd/current
			echo "$logdir/qmail-send/current"
			cat $logdir/qmail-send/current
		fi
		)|less
		failed=1
		exit 1
	fi
}

test_direct_spawn_filter()
{
	/bin/rm -rf $testdir/variables
	mkdir -p $testdir/variables
	echo "$testdir/bin/spawn-cat" > $testdir/variables/QLOCAL
	echo "$testdir/bin/spawn-cat" > $testdir/variables/QREMOTE
	echo "$cntrldir"              > $testdir/variables/CONTROLDIR
	# test QMAILLOCAL through qmail-lspawn. Default is to match recipient domain in filterargs
	(
	echo "#!/bin/sh"
	echo "sed -e 's/abcd/$HOSTNAME/' -e 's/Subject: .*/Subject: Testing spawn-filter using filter1/'"
	echo "exit 0"
	) > $testdir/bin/filter1
	(
	echo "#!/bin/sh"
	echo "sed -e 's/abcd/$HOSTNAME/' -e 's/Subject: .*/Subject: Testing spawn-filter using filter2/'"
	echo "exit 0"
	) > $testdir/bin/filter2
	(
		echo "#!/bin/sh"
		echo "cat"
		echo "exit 0"
	) > $testdir/bin/spawn-cat
	chmod +x $testdir/bin/filter1 $testdir/bin/filter2 $testdir/bin/spawn-cat
	sub1=$(echo $local_domain1 | cut -d. -f2-)
	sub2=$(echo $local_domain2 | cut -d. -f2-)
	for i in 1 2 3 4
	do
		if [ "$i" -eq 1 -o "$i" -eq 3 ] ; then # regular exp for pattern match
			(
				echo ".*\.$sub1:local:$testdir/bin/filter1"
				echo ".*\.$sub2:local:$testdir/bin/filter2"
			) > $cntrldir/filterargs
			echo 1 > $testdir/variables/QREGEX
		elif [ "$i" -eq 2 -o "$i" -eq 4 ] ; then # wildmat for pattern match
			(
				echo "*.$sub1:local:$testdir/bin/filter1"
				echo "*.$sub2:local:$testdir/bin/filter2"
			) > $cntrldir/filterargs
			echo 0 > $testdir/variables/QREGEX
		fi
		if [ "$i" -eq 1 -o "$i" -eq 2 ] ; then
			filter=filter2
			> $testdir/variables/MATCH_SENDER_DOMAIN
		elif [ "$i" -eq 3 -o "$i" -eq 4 ] ; then
			filter=filter1
			echo 1 > $testdir/variables/MATCH_SENDER_DOMAIN
		fi

		(
			echo "From: $user@$local_domain1"
			echo "To: $user@$local_domain2"
			echo "Subject: Test message using qmail-inject"
			echo "Date: $(date -R)"
			echo
			echo "Test message from abcd"
		) > $tmpdir/mail.txt
		# qmail-local [ -nN ] user homedir local dash ext domain sender defaultdelivery qqeh
		t1=$(date +"%s.%4N")
		envdir -c $testdir/variables sh -c \
			"exec -a qmail-local $sbindir/spawn-filter -- $user $testdir/$user $user \"\" \"\" $local_domain2 $user@$local_domain1 ./Maildir/ \"\"" < $tmpdir/mail.txt > $tmpdir/mail.out
		body=$($e822body < $tmpdir/mail.out)
		subject=$($e822header -I Subject < $tmpdir/mail.out)
		if [ "$body" = "Test message from $HOSTNAME" -a "$subject" = "Subject: Testing spawn-filter using $filter" ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
			tcount=$(expr $tcount + 1)
			if [ $i -eq 1 ] ; then
				printf "\r  testing spawn-filter REGEXP  qmail-local  (recipient-match) succeeded %32s [%.4f sec]\n" " " $secs
			elif [ $i -eq 2 ] ; then
				printf "\r  testing spawn-filter wildmat qmail-local  (recipient-match) succeeded %32s [%.4f sec]\n" " " $secs
			elif [ $i -eq 3 ] ; then
				printf "\r  testing spawn-filter REGEXP  qmail-local  (sender-match)    succeeded %32s [%.4f sec]\n" " " $secs
			elif [ $i -eq 4 ] ; then
				printf "\r  testing spawn-filter wildmat qmail-local  (sender-match)    succeeded %32s [%.4f sec]\n" " " $secs
			fi
			print_pct
		else
			printf "\r%118s\n" " "
			if [ $i -eq 1 ] ; then
				echo "testing spawn-filter qmail-local REGEXP (recipient-match) failed"
			elif [ $i -eq 2 ] ; then
				echo "testing spawn-filter qmail-local wildmat (recipient-match) failed"
			elif [ $i -eq 3 ] ; then
				echo "testing spawn-filter qmail-local REGEXP (sender-match) failed"
			elif [ $i -eq 4 ] ; then
				echo "testing spawn-filter qmail-local wildmat (sender-match) failed"
			fi
			echo body=$body, subject=$subject
			/bin/rm -f $tmpdir/mail.out $tmpdir/mail.txt $cntrldir/filterargs \
				$testdir/bin/filter1 $testdir/bin/filter2 $testdir/bin/spawn-cat
			exit 1
		fi
		/bin/rm -f $tmpdir/mail.out $tmpdir/mail.txt
	done
	sub1=$(echo $remote_domain1 | cut -d. -f2-)
	sub2=$(echo $remote_domain2 | cut -d. -f2-)
	for i in 1 2 3 4
	do
		if [ "$i" -eq 1 -o "$i" -eq 3 ] ; then # regular exp for pattern match
			(
				echo ".*\.$sub1:remote:$testdir/bin/filter1"
				echo ".*\.$sub2:remote:$testdir/bin/filter2"
			) > $cntrldir/filterargs
			echo 1 > $testdir/variables/QREGEX
		elif [ "$i" -eq 2 -o "$i" -eq 4 ] ; then # wildmat for pattern match
			(
				echo "*.$sub1:remote:$testdir/bin/filter1"
				echo "*.$sub2:remote:$testdir/bin/filter2"
			) > $cntrldir/filterargs
			echo 0 > $testdir/variables/QREGEX
		fi
		if [ "$i" -eq 1 -o "$i" -eq 2 ] ; then
			filter=filter1
			> $testdir/variables/MATCH_RECIPIENT_DOMAIN
		elif [ "$i" -eq 3 -o "$i" -eq 4 ] ; then
			filter=filter2
			echo 1 > $testdir/variables/MATCH_RECIPIENT_DOMAIN
		fi

		(
			echo "From: $user@$remote_domain1"
			echo "To: $user@$remote_domain2"
			echo "Subject: Test message using qmail-inject"
			echo "Date: $(date -R)"
			echo
			echo "Test message from abcd"
		) > $tmpdir/mail.txt
		# qmail-remote host sender qqeh size recip
		#exec -a qmail-remote $sbindir/spawn-filter $1  postmaster@$1 postmaster@$1
		size=$(ls -l $tmpdir/mail.txt|awk '{print $5}')
		t1=$(date +"%s.%4N")
		envdir -c $testdir/variables sh -c \
			"exec -a qmail-remote $sbindir/spawn-filter $remote_domain2 $user@$remote_domain1 \"\" $size $user@$remote_domain2" < $tmpdir/mail.txt > $tmpdir/mail.out
		body=$($e822body < $tmpdir/mail.out)
		subject=$($e822header -I Subject < $tmpdir/mail.out)
		if [ "$body" = "Test message from $HOSTNAME" -a "$subject" = "Subject: Testing spawn-filter using $filter" ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
			tcount=$(expr $tcount + 1)
			if [ $i -eq 1 ] ; then
				printf "\r  testing spawn-filter REGEXP  qmail-remote (sender-match)    succeeded %32s [%.4f sec]\n" " " $secs
			elif [ $i -eq 2 ] ; then
				printf "\r  testing spawn-filter wildmat qmail-remote (sender-match)    succeeded %32s [%.4f sec]\n" " " $secs
			elif [ $i -eq 3 ] ; then
				printf "\r  testing spawn-filter REGEXP  qmail-remote (recipient-match) succeeded %32s [%.4f sec]\n" " " $secs
			elif [ $i -eq 4 ] ; then
				printf "\r  testing spawn-filter wildmat qmail-remote (recipient-match) succeeded %32s [%.4f sec]\n" " " $secs
			fi
			print_pct
		else
			printf "\r%118s\n" " "
			if [ $i -eq 1 ] ; then
				echo "testing spawn-filter qmail-remote REGEXP (sender-match) failed"
			elif [ $i -eq 2 ] ; then
				echo "testing spawn-filter qmail-remote wildmat (sender-match) failed"
			elif [ $i -eq 3 ] ; then
				echo "testing spawn-filter qmail-remote REGEXP (recipient-match) failed"
			elif [ $i -eq 4 ] ; then
				echo "testing spawn-filter qmail-remote wildmat (recipient-match) failed"
			fi
			echo body=$body, subject=$subject
			/bin/rm -f $tmpdir/mail.out $tmpdir/mail.txt $cntrldir/filterargs \
				$testdir/bin/filter1 $testdir/bin/filter2 $testdir/bin/spawn-cat
			exit 1
		fi
		/bin/rm -f $tmpdir/mail.out $tmpdir/mail.txt
	done
	/bin/rm -f $testdir/variables/MATCH_SENDER_DOMAIN $testdir/variables/MATCH_RECIPIENT_DOMAIN
	for i in 1 2 3 4
	do
		sub1=$(echo $local_domain1 | cut -d. -f2-)
		sub2=$(echo $local_domain2 | cut -d. -f2-)
		if [ $i -eq 1 -o $i -eq 3 ] ; then
			(
				echo ".*\.$sub1:$testdir/bin/filter1"
				echo ".*\.$sub2:$testdir/bin/filter2"
			) > $cntrldir/filterargs
			echo 1 > $testdir/variables/QREGEX
		else
			(
				echo "*.$sub1:$testdir/bin/filter1"
				echo "*.$sub2:$testdir/bin/filter2"
			) > $cntrldir/filterargs
			echo 0 > $testdir/variables/QREGEX
		fi
		if [ "$i" -eq 1 -o "$i" -eq 2 ] ; then
			lfilter=filter2
			rfilter=filter1
			> $testdir/variables/MATCH_RECIPIENT_DOMAIN
			> $testdir/variables/MATCH_SENDER_DOMAIN
		elif [ "$i" -eq 3 -o "$i" -eq 4 ] ; then
			lfilter=filter1
			rfilter=filter2
			echo 1 > $testdir/variables/MATCH_RECIPIENT_DOMAIN
			echo 1 > $testdir/variables/MATCH_SENDER_DOMAIN
		fi
		(
			echo "From: $user@$local_domain1"
			echo "To: $user@$local_domain2"
			echo "Subject: Test message using qmail-inject"
			echo "Date: $(date -R)"
			echo
			echo "Test message from abcd"
		) > $tmpdir/mail.txt
		t1=$(date +"%s.%4N")
		envdir -c $testdir/variables sh -c \
			"exec -a qmail-local $sbindir/spawn-filter -- $user \ $testdir/$user $user \"\" \"\" $local_domain2 $user@$local_domain1 ./Maildir/ \"\"" < $tmpdir/mail.txt > $tmpdir/mail.out
		body=$($e822body < $tmpdir/mail.out)
		subject=$($e822header -I Subject < $tmpdir/mail.out)
		if [ "$body" = "Test message from $HOSTNAME" -a "$subject" = "Subject: Testing spawn-filter using $lfilter" ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
			tcount=$(expr $tcount + 1)
			if [ $i -eq 1 ] ; then
				printf "\r  testing spawn-filter REGEXP  qmail-local  (recipient-match) no local/remote directive succeeded %6s [%.4f sec]\n" " " $secs
			elif [ $i -eq 2 ] ; then
				printf "\r  testing spawn-filter wildmat qmail-local  (recipient-match) no local/remote directive succeeded %6s [%.4f sec]\n" " " $secs
			elif [ $i -eq 3 ] ; then
				printf "\r  testing spawn-filter REGEXP  qmail-local  (sender-match)    no local/remote directive succeeded %6s [%.4f sec]\n" " " $secs
			elif [ $i -eq 4 ] ; then
				printf "\r  testing spawn-filter wildmat qmail-local  (sender-match)    no local/remote directive succeeded %6s [%.4f sec]\n" " " $secs
			fi
			print_pct
		else
			printf "\r%118s\n" " "
			if [ $i -eq 1 ] ; then
				echo "testing spawn-filter qmail-local REGEXP (recipient-match) no local/remote directive failed"
			elif [ $i -eq 2 ] ; then
				echo "testing spawn-filter qmail-local wildmat (recipient-match) no local/remote directive failed"
			elif [ $i -eq 3 ] ; then
				echo "testing spawn-filter qmail-local REGEXP (sender-match) no local/remote directive failed"
			elif [ $i -eq 4 ] ; then
				echo "testing spawn-filter qmail-local wildmat (sender-match) no local/remote directive failed"
			fi
			echo body=$body, subject=$subject
			/bin/rm -f $tmpdir/mail.out $tmpdir/mail.txt $cntrldir/filterargs \
				$testdir/bin/filter1 $testdir/bin/filter2 $testdir/bin/spawn-cat
			exit 1
		fi

		sub1=$(echo $remote_domain1 | cut -d. -f2-)
		sub2=$(echo $remote_domain2 | cut -d. -f2-)
		(
			echo "From: $user@$remote_domain1"
			echo "To: $user@$remote_domain2"
			echo "Subject: Test message using qmail-inject"
			echo "Date: $(date -R)"
			echo
			echo "Test message from abcd"
		) > $tmpdir/mail.txt
		if [ $i -eq 1 -o $i -eq 3 ] ; then
			(
				echo ".*\.$sub1:$testdir/bin/filter1"
				echo ".*\.$sub2:$testdir/bin/filter2"
			) > $cntrldir/filterargs
			echo 1 > $testdir/variables/QREGEX
		else
			(
				echo "*.$sub1:$testdir/bin/filter1"
				echo "*.$sub2:$testdir/bin/filter2"
			) > $cntrldir/filterargs
			echo 0 > $testdir/variables/QREGEX
		fi
		t1=$(date +"%s.%4N")
		size=$(ls -l $tmpdir/mail.txt|awk '{print $5}')
		envdir -c $testdir/variables sh -c \
			"exec -a qmail-remote $sbindir/spawn-filter $remote_domain2 $user@$remote_domain1 \"\" $size $user@$remote_domain2" < $tmpdir/mail.txt > $tmpdir/mail.out
		body=$($e822body < $tmpdir/mail.out)
		subject=$($e822header -I Subject < $tmpdir/mail.out)
		if [ "$body" = "Test message from $HOSTNAME" -a "$subject" = "Subject: Testing spawn-filter using $rfilter" ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
			tcount=$(expr $tcount + 1)
			if [ $i -eq 1 ] ; then
				printf "\r  testing spawn-filter REGEXP  qmail-remote (sender-match)    no local/remote directive succeeded %6s [%.4f sec]\n" " " $secs
			elif [ $i -eq 2 ] ; then
				printf "\r  testing spawn-filter wildmat qmail-remote (sender-match)    no local/remote directive succeeded %6s [%.4f sec]\n" " " $secs
			elif [ $i -eq 3 ] ; then
				printf "\r  testing spawn-filter REGEXP  qmail-remote (recipient-match) no local/remote directive succeeded %6s [%.4f sec]\n" " " $secs
			elif [ $i -eq 4 ] ; then
				printf "\r  testing spawn-filter wildmat qmail-remote (recipient-match) no local/remote directive succeeded %6s [%.4f sec]\n" " " $secs
			fi
			print_pct
		else
			printf "\r%118s\n" " "
			if [ $i -eq 1 ] ; then
				echo "testing spawn-filter qmail-remote REGEXP (sender-match) no local/remote directive failed"
			elif [ $i -eq 2 ] ; then
				echo "testing spawn-filter qmail-remote wildmat (sender-match) no local/remote directive failed"
			elif [ $i -eq 3 ] ; then
				echo "testing spawn-filter qmail-remote REGEXP (recipient-match) no local/remote directive failed"
			elif [ $i -eq 4 ] ; then
				echo "testing spawn-filter qmail-remote wildmat (recipient-match) no local/remote directive failed"
			fi
			echo body=$body, subject=$subject
			/bin/rm -f $tmpdir/mail.out $tmpdir/mail.txt $cntrldir/filterargs \
				$testdir/bin/filter1 $testdir/bin/filter2 $testdir/bin/spawn-cat
			exit 1
		fi
	done
	/bin/rm -f $cntrldir/filterargs

	t1=$(date +"%s.%4N")
	echo "*:local:$bogofilter -p -d $sysconfdir -u:SPAMEXITCODE=0" > $cntrldir/spamfilter
	envdir -c $testdir/variables sh -c \
		"exec -a qmail-local $sbindir/spawn-filter -- $user \ $testdir/$user $user \"\" \"\" $local_domain2 $user@$local_domain1 ./Maildir/ \"\"" < $tmpdir/mail.txt > $tmpdir/mail.out
	bogosity=$($e822header -I X-Bogosity < $tmpdir/mail.out)
	echo $bogosity | grep "^X-Bogosity" > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing spawn-filter SPAM filtering using spamfilter control file succeeded %26s [%.4f sec]\n" " " $secs
	else
		echo "bogosity=[$bogosity]"
		echo "testing spawn-filter spam filtering using spamfilter control file failed"
		exit 1
	fi

	t1=$(date +"%s.%4N")
	/bin/rm -f $cntrldir/spamfilter
	echo "$bogofilter -p -d $sysconfdir" > $testdir/variables/SPAMFILTER
	echo "0"                             > $testdir/variables/SPAMEXITCODE
	envdir -c $testdir/variables sh -c \
		"exec -a qmail-local $sbindir/spawn-filter -- $user \ $testdir/$user $user \"\" \"\" $local_domain2 $user@$local_domain1 ./Maildir/ \"\"" < $tmpdir/mail.txt > $tmpdir/mail.out
	bogosity=$($e822header -I X-Bogosity < $tmpdir/mail.out)
	echo $bogosity | grep "^X-Bogosity" > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing spawn-filter SPAM filtering using SPAMFILTER environment variable succeeded %18s [%.4f sec]\n" " " $secs
	else
		echo "bogosity=[$bogosity]"
		echo "testing spawn-filter SPAM filtering using SPAMFILTER environment variable failed"
		exit 1
	fi

	/bin/rm -f $tmpdir/mail.out $tmpdir/mail.txt $cntrldir/filterargs \
		$testdir/bin/filter1 $testdir/bin/filter2 $testdir/bin/spawn-cat
}

test_qmail_multi()
{
	t1=$(date +"%s.%4N")
	/bin/rm -rf $testdir/variables
	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	echo $qmail_multi   > $testdir/variables/QMAILQUEUE
	do_qmail_inject $user@$HOSTNAME $user@$HOSTNAME "Test qmail-multi" "Test Message for qmail-multi"
	check_mail
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-multi succeeded %72s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		echo "  testing qmail-multi failed [$secs sec]"
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "#!/bin/sh"
	echo "sed -e 's{I am a{I am not a{'"
	) > $testdir/bin/filter
	chmod +x $testdir/bin/filter
	echo $testdir/bin/filter > $testdir/variables/FILTERARGS
	do_qmail_inject $user@$HOSTNAME $user@$HOSTNAME "Test qmail-multi" "I am a fool"
	check_mail $maildir 1
	if [ -n "$mail_file" ] ; then
		body=$($e822body < $maildir/new/$mail_file)
		/bin/rm -f $maildir/new/$mail_file
	else
		body=""
	fi
	if [ "$body" = "I am not a fool" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-multi with FILTERARGS                         succeeded %32s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		echo "  testing qmail-multi with FILTERARGS failed [$secs sec]"
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "#!/bin/sh"
	echo "exit 100"
	) > $testdir/bin/filter
	chmod +x $testdir/bin/filter
	echo $testdir/bin/filter > $testdir/variables/FILTERARGS
	do_qmail_inject $user@$HOSTNAME $user@$HOSTNAME "Test qmail-multi" "Tesing exit 100" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	if [ $ret -eq 100 -a "$err" = "qmail-inject: fatal: qq mail server permanently rejected message (#5.3.0)" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-multi with FILTERARGS        permanent reject succeeded %32s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		echo "  testing qmail-multi with FILTERARGS permanent reject failed [$secs sec]"
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "#!/bin/sh"
	echo "exit 111"
	) > $testdir/bin/filter
	chmod +x $testdir/bin/filter
	echo $testdir/bin/filter > $testdir/variables/FILTERARGS
	do_qmail_inject $user@$HOSTNAME $user@$HOSTNAME "Test qmail-multi" "Tesing exit 111" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	if [ $ret -eq 111 -a "$err" = "qmail-inject: fatal: qq mail server temporarily rejected message (#4.3.0)" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-multi with FILTERARGS        temporary reject succeeded %32s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		echo "  testing qmail-multi with FILTERARGS temporary reject failed [$secs sec]"
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "#!/bin/sh"
	echo "echo \"DThis mail is unacceptable\" 1>&2"
	echo "exit 88"
	) > $testdir/bin/filter
	chmod +x $testdir/bin/filter
	echo $testdir/bin/filter > $testdir/variables/FILTERARGS
	do_qmail_inject $user@$HOSTNAME $user@$HOSTNAME "Test qmail-multi" "Tesing exit 88" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	if [ $ret -eq 100 -a "$err" = "qmail-inject: fatal: This mail is unacceptable" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-multi with FILTERARGS custom permanent reject succeeded %32s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret err=$err"
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		echo "  testing qmail-multi with FILTERARGS custom permanent reject failed [$secs sec]"
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "#!/bin/sh"
	echo "echo \"ZThis mail is unacceptable\" 1>&2"
	echo "exit 88"
	) > $testdir/bin/filter
	chmod +x $testdir/bin/filter
	echo $testdir/bin/filter > $testdir/variables/FILTERARGS
	do_qmail_inject $user@$HOSTNAME $user@$HOSTNAME "Test qmail-multi" "Tesing exit 88" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	if [ $ret -eq 111 -a "$err" = "qmail-inject: fatal: This mail is unacceptable" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-multi with FILTERARGS custom temporary reject succeeded %32s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret err=$err"
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		echo "  testing qmail-multi with FILTERARGS custom temporary reject failed [$secs sec]"
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	mkdir -p $cntrldir/filters.d
	(
	echo "#!/bin/sh"
	echo ""
	echo "[ -n \"\$ENVSIZE\" -a -n \"\$MSGSIZE\" ] && qmail_qfilter=1 || qmail_qfilter=0"
	echo "[ -z \"\$CONTROLDIR\" ] && CONTROLDIR=/etc/indimail/control"
	echo "# save the email"
	echo "out=\$(mktemp -t badfromXXXXXXXX)"
	echo "cat > \$out"
	echo "from=\$(822addr From < \$out | tr [\"\0\"] [\" \"] | cut -d+ -f2)"
	echo "grep -w \$from \$CONTROLDIR/badfrom >/dev/null"
	echo "ret=\$?"
	echo "exec 0<\$out"
	echo "/bin/rm -f \$out"
	echo "if [ \$ret -eq 0 ] ; then"
	echo "	echo \"Dmail rejected by badfrom\" 1>&2"
	echo "	exit 100"
	echo "fi"
	echo "[ \$qmail_qfilter -eq 1 ] && exit 0 || exec /bin/cat"
	) > $cntrldir/filters.d/qf-bad-from
	chmod +x $cntrldir/filters.d/qf-bad-from
	echo "$testuser@$HOSTNAME" > $cntrldir/badfrom

	mkdir -p $testdir/variables
	/bin/rm -f $testdir/variables/FILTERARGS
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	echo "$qmail_multi /usr/libexec/indimail/qfrontend" > $testdir/variables/QMAILQUEUE
	touch $cntrldir/qfilters
	do_qmail_inject $testuser@$HOSTNAME $user@$HOSTNAME "Test qfrontend" "Tesing qfrontend" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	/bin/rm -f $tmpdir/inject.error
	if [ $ret -eq 100 -a "$err" = "qmail-inject: fatal: qq mail server permanently rejected message (#5.3.0)" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-multi with qfrontend         permanent reject succeeded %32s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret err=$err"
		echo "testing qmail-multi with qfrontend permanent reject failed"
		failed=1
		/bin/rm -rf $servicedir/smtpd/variables/QMAILQUEUE $testdir/variables/QMAILQUEUE \
			$testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2 $cntrldir/filters.d
		exit 1
	fi

	t1=$(date +"%s.%4N")
	mkdir -p $cntrldir/filters.d
	(
	echo "#!/bin/sh"
	echo ""
	echo "[ -n \"\$ENVSIZE\" -a -n \"\$MSGSIZE\" ] && qmail_qfilter=1 || qmail_qfilter=0"
	echo "[ -z \"\$CONTROLDIR\" ] && CONTROLDIR=/etc/indimail/control"
	echo "# save the email"
	echo "out=\$(mktemp -t badfromXXXXXXXX)"
	echo "cat > \$out"
	echo "from=\$(822addr From < \$out | tr [\"\0\"] [\" \"] | cut -d+ -f2)"
	echo "grep -w \$from \$CONTROLDIR/badfrom >/dev/null"
	echo "ret=\$?"
	echo "exec 0<\$out"
	echo "/bin/rm -f \$out"
	echo "if [ \$ret -eq 0 ] ; then"
	echo "	echo \"Dmail rejected by badfrom\" 1>&2"
	echo "	exit 111"
	echo "fi"
	echo "[ \$qmail_qfilter -eq 1 ] && exit 0 || exec /bin/cat"
	) > $cntrldir/filters.d/qf-bad-from
	chmod +x $cntrldir/filters.d/qf-bad-from
	echo "$testuser@$HOSTNAME" > $cntrldir/badfrom

	mkdir -p $testdir/variables
	/bin/rm -f $testdir/variables/FILTERARGS
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	echo "$qmail_multi /usr/libexec/indimail/qfrontend" > $testdir/variables/QMAILQUEUE
	touch $cntrldir/qfilters
	do_qmail_inject $testuser@$HOSTNAME $user@$HOSTNAME "Test qfrontend" "Tesing qfrontend" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	/bin/rm -f $tmpdir/inject.error
	if [ $ret -eq 111 -a "$err" = "qmail-inject: fatal: qq mail server temporarily rejected message (#4.3.0)" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-multi with qfrontend         temporary reject succeeded %32s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret err=$err"
		echo "testing qmail-multi with qfrontend temporary reject failed"
		failed=1
		/bin/rm -rf $servicedir/smtpd/variables/QMAILQUEUE $testdir/variables/QMAILQUEUE \
			$testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2 $cntrldir/filters.d
		exit 1
	fi

	t1=$(date +"%s.%4N")
	mkdir -p $cntrldir/filters.d
	(
	echo "#!/bin/sh"
	echo ""
	echo "[ -n \"\$ENVSIZE\" -a -n \"\$MSGSIZE\" ] && qmail_qfilter=1 || qmail_qfilter=0"
	echo "[ -z \"\$CONTROLDIR\" ] && CONTROLDIR=/etc/indimail/control"
	echo "# save the email"
	echo "out=\$(mktemp -t badfromXXXXXXXX)"
	echo "cat > \$out"
	echo "from=\$(822addr From < \$out | tr [\"\0\"] [\" \"] | cut -d+ -f2)"
	echo "grep -w \$from \$CONTROLDIR/badfrom >/dev/null"
	echo "ret=\$?"
	echo "exec 0<\$out"
	echo "/bin/rm -f \$out"
	echo "if [ \$ret -eq 0 ] ; then"
	echo "	echo \"Dmail rejected by badfrom\" 1>&2"
	echo "	exit 88"
	echo "fi"
	echo "[ \$qmail_qfilter -eq 1 ] && exit 0 || exec /bin/cat"
	) > $cntrldir/filters.d/qf-bad-from
	chmod +x $cntrldir/filters.d/qf-bad-from
	echo "$testuser@$HOSTNAME" > $cntrldir/badfrom

	mkdir -p $testdir/variables
	/bin/rm -f $testdir/variables/FILTERARGS
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	echo "$qmail_multi /usr/libexec/indimail/qfrontend" > $testdir/variables/QMAILQUEUE
	touch $cntrldir/qfilters
	do_qmail_inject $testuser@$HOSTNAME $user@$HOSTNAME "Test qfrontend" "Tesing qfrontend" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	/bin/rm -f $tmpdir/inject.error
	if [ $ret -eq 100 -a "$err" = "qmail-inject: fatal: mail rejected by badfrom" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-multi with qfrontend  custom permanent reject succeeded %32s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret err=$err"
		echo "testing qmail-multi with qfrontend custom permanent reject failed"
		failed=1
		/bin/rm -rf $servicedir/smtpd/variables/QMAILQUEUE $testdir/variables/QMAILQUEUE \
			$testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2 $cntrldir/filters.d
		exit 1
	fi

	(
	echo "#!/bin/sh"
	echo ""
	echo "[ -n \"\$ENVSIZE\" -a -n \"\$MSGSIZE\" ] && qmail_qfilter=1 || qmail_qfilter=0"
	echo "[ -z \"\$CONTROLDIR\" ] && CONTROLDIR=/etc/indimail/control"
	echo "# save the email"
	echo "out=\$(mktemp -t badfromXXXXXXXX)"
	echo "cat > \$out"
	echo "from=\$(822addr From < \$out | tr [\"\0\"] [\" \"] | cut -d+ -f2)"
	echo "grep -w \$from \$CONTROLDIR/badfrom >/dev/null"
	echo "ret=\$?"
	echo "exec 0<\$out"
	echo "/bin/rm -f \$out"
	echo "if [ \$ret -eq 0 ] ; then"
	echo "	echo \"Zmail rejected by badfrom\" 1>&2"
	echo "	exit 88"
	echo "fi"
	echo "[ \$qmail_qfilter -eq 1 ] && exit 0 || exec /bin/cat"
	) > $cntrldir/filters.d/qf-bad-from
	chmod +x $cntrldir/filters.d/qf-bad-from
	do_qmail_inject $testuser@$HOSTNAME $user@$HOSTNAME "Test qfrontend" "Tesing qfrontend" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	/bin/rm -f $tmpdir/inject.error
	if [ $ret -eq 111 -a "$err" = "qmail-inject: fatal: mail rejected by badfrom" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-multi with qfrontend  custom temporary reject succeeded %32s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret err=$err"
		echo "testing qmail-multi with qfrontend custom temporary reject failed"
		failed=1
		/bin/rm -rf $servicedir/smtpd/variables/QMAILQUEUE $testdir/variables/QMAILQUEUE \
			$testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2 $cntrldir/filters.d
		exit 1
	fi

	/bin/rm -f $testdir/bin/filter $servicedir/smtpd/variables/QMAILQUEUE $testdir/variables/QMAILQUEUE \
			$testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2
	/bin/rm -rf $testdir/variables $cntrldir/filters.d
}

test_badhelo()
{
	t1=$(date +"%s.%4N")
	echo "*israel*" > $cntrldir/badhelo
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
		-h israel >$tmpdir/swaks.$$
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	echo $l1 | grep ".*553 sorry, your HELO/EHLO greeting is in my badhelo list (#5.7.1)">/dev/null
	ret2=$?
	check_mail 2>/dev/null
	ret3=$?
	s1=$sleep_secs
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
		-h palestine >$tmpdir/swaks.$$
	ret4=$?
	check_mail 2>/dev/null
	ret5=$?
	s2=$sleep_secs
	/bin/rm -f $tmpdir/swaks.$$

	if [ $ret1 -ne 0 -a $ret2 -eq 0 -a $ret3 -ne 0 -a $ret4 -eq 0 -a $ret5 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing SMTP badhelo check succeeded %65s [%.4f sec]\n" " " $secs
		print_pct
	else
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3, ret4=$ret4, ret5=$ret5, l1=[$l1]"
		echo "  testing SMTP badhelo check failed [#secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		)|less
		failed=1
		exit 1
	fi
	/bin/rm -f $cntrldir/badhelo
}

test_dnsmx()
{
	t1=$(date +"%s.%4N")
	from="abcdefgh@kfjadjff.com"
	swaks -S --to $user@$HOSTNAME --from $from --server 127.0.0.1 --port $smtp_port \
		>$tmpdir/swaks.$$.1
	ret1=$?
	l1=$(sed -n 1p < $tmpdir/swaks.$$.1)
	l2=$(sed -n '$p' $logdir/smtpd/smtpd.log)
	echo "$l1" | grep ".*553 Bad sender's system address (#5.1.8)" >/dev/null
	ret2=$?
	echo "$l2" | grep ".*Non-existing DNS_MX: MAIL FROM:<$from>" >/dev/null
	ret3=$?
	echo 1 > $testdir/smtpd/variables/NODNSCHECK
	terminate_smtp_qmtp $smtp_pid
	smtp_pid=""
	s1=$sleep_secs
	start_tcpserver_smtp 4
	sleep $sleep_int
	swaks -S --to $user@$HOSTNAME --from $from --server 127.0.0.1 --port $smtp_port \
		>$tmpdir/swaks.$$.2
	ret4=$?
	check_mail
	ret5=$?

	if [ $ret1 -ne 0 -a $ret2 -eq 0 -a $ret3 -eq 0 -a $ret4 -eq 0 -a $ret5 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $sleep_secs $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing SMTP NODNSCHECK    succeeded %65s [%.4f sec]\n" " " $secs
		print_pct
		/bin/rm -f $tmpdir/swaks.$$.* $testdir/smtpd/variables/NODNSCHECK
	else
		printf "\r%118s\n" " "
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3, ret4=$ret4, ret5=$ret5"
		echo "l1=$l1"
		echo "l2=$l2"
		echo "  testing SMTP NOODNSCHECK failed [$secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		echo "Swaks log 1"
		cat $tmpdir/swaks.$$.1
		echo "Swaks log 2"
		cat $tmpdir/swaks.$$.2
		)|less
		failed=1
		/bin/rm -f $tmpdir/swaks.$$ $testdir/smtpd/variables/NODNSCHECK
		terminate_smtp_qmtp $smtp_pid
		exit 1
	fi
	terminate_smtp_qmtp $smtp_pid
	start_tcpserver_smtp 4
	sleep $sleep_int
}

test_etrn1()
{
	t1=$(date +"%s.%4N")
	mail_file=""
	addr=$(/usr/libexec/indimail/batv -k $(cat $cntrldir/batvkey) -s $testuser@$HOSTNAME 2>/dev/null)
	echo ".*:d:$addr:remote"  > $cntrldir/redirectremote
	echo "$HOSTNAME:127.0.0.1:$smtp_port" > $cntrldir/smtproutes

	# create env variables for qmail-inject
	/bin/rm -rf $testdir/variables
	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	do_qmail_inject "$user@$ext_domain" $testuser@etrn1.dom "Testing IP based ETRN" "$$: Testing IP based ETRN"
	localip=$(/usr/libexec/indimail/ipmeprint | grep -E -v "ipv6|ipv4 -> 127|ipv4 -> 0"|head -1|awk '{print $3}')
	sudo chmod 755 $qmaildir/autoturn/$localip $qmaildir/autoturn/$localip/Maildir \
		$qmaildir/autoturn/$localip/Maildir/new
	check_mail $qmaildir/autoturn/$localip/Maildir 1 2>/dev/null
	ret1=$?
	mail_file1=$mail_file
	s1=$sleep_secs
	if [ $ret1 -eq 0 ] ; then
		(
		echo "set postmaster \"$user\""
		echo "set bouncemail"
		echo "set no spambounce"
		echo "set no softbounce"
		echo "set properties \"\""
		echo "poll $localip with proto ETRN port $smtp_port timeout 120 interval 1 and options no dns"
		echo "    fetchdomains etrn1.dom"
		) > $sysconfdir/fetchmailrc
		chmod 600 $sysconfdir/fetchmailrc
		sleep 1.1 # maildirserial has resolution of 1 seconds
		fetchmail -f $sysconfdir/fetchmailrc -s
		ret2=$?
		mail_file=""
		if [ $ret2 -eq 0 ] ; then
			c=0
			s2=0
			while true
			do
				check_mail $qmaildir/autoturn/$localip/Maildir 1
				ret3=$?
				s2=$(echo $s2 $sleep_secs | awk '{printf("%0.4f\n", $1+$2)}')
				if [ $ret3 -ne 0 ] ; then
					break
				fi
				mail_file2=$mail_file
				if [ "$mail_file1" != "$mail_file2" ] ; then
					break
				fi
				c=$(expr $c + 1)
				sleep 0.5
				s2=$(echo $s2 0.5 | awk '{printf("%0.4f\n", $1+$2)}')
				if [ $c -ge 10 ] ; then
					break
				fi
			done
		else
			ret2=111
			ret3=111
		fi
	else
		ret1=1
		ret2=111
		ret3=111
	fi
	echo ".*:d:$testuser@$HOSTNAME:remote"    > $cntrldir/redirectremote
	if [ $ret1 -eq 0 -a $ret2 -eq 0 -a $ret3 -eq 0 -a -n "$mail_file1" -a -n "$mail_file2" -a "$mail_file1" != "$mail_file2" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 1.1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing IP based ETRN succeeded %70s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3, fn1=$mail_file1, fn2=$mail_file2"
		echo "  testing IP based ETRN failed [$secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		echo ---------------------------------------------
		echo "$logdir/qmail-send/qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		/bin/rm -f $cntrldir/smtproutes
		failed=1
		exit 1
	fi
	/bin/rm -f $cntrldir/smtproutes
}

test_etrn2()
{
	t1=$(date +"%s.%4N")
	mail_file=""
	addr=$(/usr/libexec/indimail/batv -k $(cat $cntrldir/batvkey) -s $testuser@$HOSTNAME 2>/dev/null)
	echo ".*:d:$addr:remote"  > $cntrldir/redirectremote
	echo "$HOSTNAME:127.0.0.1:$smtp_port" > $cntrldir/smtproutes

	# create env variables for qmail-inject
	/bin/rm -rf $testdir/variables
	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	do_qmail_inject "$user@$ext_domain" $testuser@etrn2.dom "Testing Domain based ETRN" "$$: Testing Domain based ETRN"
	localip=$(/usr/libexec/indimail/ipmeprint | grep -E -v "ipv6|ipv4 -> 127|ipv4 -> 0"|head -1|awk '{print $3}')
	sudo chmod 755 $qmaildir/autoturn/etrn2.dom $qmaildir/autoturn/etrn2.dom/Maildir \
		$qmaildir/autoturn/etrn2.dom/Maildir/new
	check_mail $qmaildir/autoturn/etrn2.dom/Maildir 1 2>/dev/null
	ret1=$?
	mail_file1=$mail_file
	s1=$sleep_secs
	if [ $ret1 -eq 0 ] ; then
		sudo sh -c "echo $localip > $qmaildir/autoturn/etrn2.dom/ipauth"
		(
		echo "set postmaster \"$user\""
		echo "set bouncemail"
		echo "set no spambounce"
		echo "set no softbounce"
		echo "set properties \"\""
		echo "poll $localip with proto ETRN port $smtp_port timeout 120 interval 1 and options no dns"
		echo "    fetchdomains etrn2.dom"
		) > $sysconfdir/fetchmailrc
		chmod 600 $sysconfdir/fetchmailrc
		sleep 1.1 # maildirserial has resolution of 1 seconds
		fetchmail -f $sysconfdir/fetchmailrc -s
		ret2=$?
		mail_file=""
		if [ $ret2 -eq 0 ] ; then
			c=0
			s2=0
			while true
			do
				check_mail $qmaildir/autoturn/etrn2.dom/Maildir 1
				ret3=$?
				s2=$(echo $s2 $sleep_secs | awk '{printf("%0.4f\n", $1+$2)}')
				if [ $ret3 -ne 0 ] ; then
					break
				fi
				mail_file2=$mail_file
				if [ "$mail_file1" != "$mail_file2" ] ; then
					break
				fi
				c=$(expr $c + 1)
				sleep 0.5
				s2=$(echo $s2 0.5 | awk '{printf("%0.4f\n", $1+$2)}')
				if [ $c -ge 10 ] ; then
					break
				fi
			done
		else
			ret2=111
			ret3=111
		fi
	else
		ret1=1
		ret2=111
		ret3=111
	fi
	echo ".*:d:$testuser@$HOSTNAME:remote"    > $cntrldir/redirectremote
	if [ $ret1 -eq 0 -a $ret2 -eq 0 -a -n "$mail_file1" -a -n "$mail_file2" -a "$mail_file1" != "$mail_file2" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 1.1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing Domain based ETRN succeeded %66s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret1=$ret1, ret2=$ret2, fn1=$mail_file1, fn2=$mail_file2"
		echo "  testing Domain based ETRN failed [$secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		echo ---------------------------------------------
		echo "$logdir/qmail-send/qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		/bin/rm -f $cntrldir/smtproutes
		exit 1
	fi
	/bin/rm -f $cntrldir/smtproutes
}

test_atrn()
{
	t1=$(date +"%s.%4N")
	mail_file=""
	addr=$(/usr/libexec/indimail/batv -k $(cat $cntrldir/batvkey) -s $testuser@$HOSTNAME 2>/dev/null)
	echo ".*:d:$addr:remote"               > $cntrldir/redirectremote # for redirected remote bounce mails to local
	echo "$HOSTNAME:127.0.0.1:$smtp_port"  > $cntrldir/smtproutes
	echo "$user:atrn.dom"                  > $cntrldir/atrnaccess
	# create env variables for qmail-inject
	/bin/rm -rf $testdir/variables
	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	do_qmail_inject "$user@$ext_domain" $testuser@atrn.dom "Testing ATRN (ODMR)" "$$: Testing ATRN (ODMR)"
	localip=$(/usr/libexec/indimail/ipmeprint | grep -E -v "ipv6|ipv4 -> 127|ipv4 -> 0"|head -1|awk '{print $3}')
	sudo chmod 755 $qmaildir/autoturn/atrn.dom $qmaildir/autoturn/atrn.dom/Maildir \
		$qmaildir/autoturn/atrn.dom/Maildir/new
	check_mail $qmaildir/autoturn/atrn.dom/Maildir 1 2>/dev/null
	ret1=$?
	mail_file1=$mail_file
	s1=$sleep_secs
	if [ $ret1 -eq 0 ] ; then
		(
		echo "set postmaster \"$user\""
		echo "set bouncemail"
		echo "set no spambounce"
		echo "set no softbounce"
		echo "set properties \"\""
		echo "poll $localip with proto ODMR port $smtp_port timeout 120 interval 1 and options no dns"
		echo "user '$user@$HOSTNAME' there with password 'abcd12345678' smtphost $localip/$smtp_port options"
		echo "	no rewrite forcecr pass8bits fetchlimit 50 batchlimit 250 expunge 50"
    	echo "	fetchdomains atrn.dom"
		) > $sysconfdir/fetchmailrc
		chmod 600 $sysconfdir/fetchmailrc
		echo 1 > $testdir/smtpd/variables/ODMR
		terminate_smtp_qmtp $smtp_pid
		start_tcpserver_smtp 4
		s2=$sleep_secs
		sleep 1.1 # maildirserial has resolution of 1 seconds
		fetchmail -f $sysconfdir/fetchmailrc -s
		ret2=$?
		mail_file=""
		if [ $ret2 -eq 0 ] ; then
			sleep $sleep_int
			c=0
			s3=0
			while true
			do
				check_mail $qmaildir/autoturn/atrn.dom/Maildir 1
				ret3=$?
				s3=$(echo $s3 $sleep_secs | awk '{printf("%0.4f\n", $1+$2)}')
				if [ $ret3 -ne 0 ] ; then
					break
				fi
				mail_file2=$mail_file
				if [ "$mail_file1" != "$mail_file2" ] ; then
					break
				fi
				c=$(expr $c + 1)
				sleep 0.5
				s3=$(echo $s3 0.5 | awk '{printf("%0.4f\n", $1+$2)}')
				if [ $c -ge 10 ] ; then
					break
				fi
			done
		else
			ret2=111
			ret3=111
		fi
	else
		ret1=1
		ret2=111
		ret3=111
	fi
	echo ".*:d:$testuser@$HOSTNAME:remote" > $cntrldir/redirectremote
	if [ $ret1 -eq 0 -a $ret2 -eq 0 -a -n "$mail_file1" -a -n "$mail_file2" -a ! " $mail_file1" = " $mail_file2" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $sleep_int $s2 1.2 $s3 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5-$6)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing ATRN service (ODMR) succeeded %64s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret1=$ret1, ret2=$ret2, fn1=$mail_file1, fn2=$mail_file2"
		echo "  testing ATRN service (ODMR) failed [$secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		echo "$logdir/qmail-send/qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		/bin/rm -f $testdir/variables/ODMR $cntrldir/smtproutes $cntrldir/atrnaccess
		exit 1
	fi
	/bin/rm -f $testdir/variables/ODMR $cntrldir/smtproutes $cntrldir/atrnaccess
}

test_autoturn()
{
	t1=$(date +"%s.%4N")

	sudo queue-fix -m -s 23 -b 0 $testdir/qmta
	echo "$HOSTNAME:QUEUEDIR=$testdir/qmta,CONTROLDIR=$cntrldir,BIGTODO=0,CONFSPLIT=23" > $cntrldir/domainqueue

	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
		>$tmpdir/swaks.$$
	ret1=$?
	/bin/rm -f $cntrldir/domainqueue
	(
	echo "env - \\"
	echo "	PATH=/bin:/usr/bin:/usr/sbin \\"
	echo "	QUEUEDIR=$testdir/qmta \\"
	echo "	BIGTODO=0 \\"
	echo "	CONFSPLIT=23 \\"
	echo "	USE_QPWGR=1 \\"
	echo "	CONTROLDIR=$cntrldir \\"
	echo "	QMAILGETPW=$testdir/bin/qmail-getpw \\"
	echo "	ASSIGNDIR=$sysconfdir/users \\"
	echo "	PASSWD_FILE=$testdir/etc/passwd \\"
	echo "	$sbindir/qmta-send ./Maildir/"
	) > $testdir/bin/qmta
	chmod +x $testdir/bin/qmta
	terminate_smtp_qmtp $smtp_pid
	ret2=$?
	s1=$sleep_secs
	start_tcpserver_smtp_turn
	ret3=$?
	s2=$sleep_secs
	(
	echo "#!/bin/sh"
	echo "exec 0<&6"
	echo "exec 1>&7"
	echo "read key"
	echo "greeting=\$(echo \$key | awk '{print \$1}')"
	echo "if [ \$greeting -ne 220 ] ; then"
	echo "	echo \"Greeting failed\" 1>&2"
	echo "	printf \"QUIT\r\n\""
	echo "fi"
	echo "printf \"QUIT\r\n\""
	echo "read key"
	echo "echo \$key 1>&2"
	echo "exit 0"
	) > $testdir/tcpclient.smtp
	chmod +x $testdir/tcpclient.smtp

	# trigger AUTOTURN
	tcpclient -vDHR 127.0.0.1 $smtp_port $testdir/tcpclient.smtp > $logdir/tcpclient/tcpclient.log 2>&1
	ret4=$?
	check_mail
	ret5=$?
	s3=$sleep_secs
	/bin/rm -f $testdir/bin/qmta
	if [ $ret1 -eq 0 -a $ret2 -eq 0 -a $ret3 -eq 0 -a $ret4 -eq 0 -a $ret5 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 $s3 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing AUTOTURN service succeeded %67s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3, ret4=$ret4, ret5=$ret5"
		echo "  testing AUTOTURN service failed [$secs sec]"
		(
		echo "$logdir/smtpd/smtpd.log"
		cat $logdir/smtpd/smtpd.log
		echo "$logdir/qmail-send/qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		exit 1
	fi
	terminate_smtp_qmtp $smtp_pid
	start_tcpserver_smtp 1
}

do_without_svscan()
{
	printf "\r%118s\n" " "
	echo "Testing commands run without using svscan"
	# use qmail-start, slowq-sart to start qmail-send, slowq-send
	run_qmail_send qmail-send 1
	run_qmail_send slowq-send-1
	run_qmail_send slowq-send-2
	run_qmail_send qmta-send-1
	run_qmail_send qmta-send-2
	test_qmail_inject $user $user $testdir/queue
	test_qmail_inject $user $user $testdir/slowq1
	test_qmail_inject $user $user $testdir/slowq2
	test_qmail_inject $user $user $testdir/qmta1
	test_qmail_inject $user $user $testdir/qmta2
	setup_assign # restore users/assign
	test_alias

	t1x=$(date +"%s.%4N")
	test_filterit 1
	x1=$filterit_test1
	x2=$filterit_test2
	x3=$filterit_test3
	t2x=$(date +"%s.%4N")
	test_filterit 2
	t3x=$(date +"%s.%4N")
	x1=$(echo "$filterit_test1 >= $x1" | bc -l)
	x2=$(echo "$filterit_test2 >= $x2" | bc -l)
	x3=$(echo "$filterit_test3 >= $x3" | bc -l)
	diff=$(echo $t1x $t2x $t3x | awk '{print $3-$2-($2-$1)}')
	t=$(echo "$diff > 0.0" | bc -l)
	if [ $t -eq 1 ] ; then
		secs=$(echo $t1x $t3x | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r%118s" " "
		printf "\r  testing filterit internal faster by %2.4f sec than external command succeeded %23s [%.4f sec]\n" $diff " " $secs
		print_pct
	else
		secs=$(echo $t1x $t3x | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\r" " "
		echo "  testing filterit internal faster [$diff sec] than external command failed [$secs sec]"
	fi

	# start smtp using tcpserver
	start_tcpserver_smtp
	# start qmtp using tcpserver
	start_tcpserver_qmtp
	sleep $sleep_int

	test_dnsmx
	test_badhelo
	test_spamfilter
	test_smtp_spamfilter
	test_qhpsi
	test_qqeh
	test_direct_spawn_filter
	for i in 1 2
	do
		test_lspawn_rspawn_filter $i
		if [ $i -eq 1 ] ; then
			terminate_send qmail-send "$send_pid"
			send_pid=""
			silent=1 run_qmail_send qmail-send
		fi
	done
	terminate_send qmail-send "$send_pid"
	send_pid=""
	silent=1 run_qmail_send qmail-send 1
	create_authsmtp
	test_domainqueue
	test_qmail_queue 0
	test_qmail_queue 1
	test_qmail_multi

	setup_srs 1
	test_srs
	setup_srs 0

	setup_virtual 1
	test_maildirserial
	test_etrn1
	test_etrn2
	test_atrn
	test_autoturn
	setup_virtual 0
	terminate_smtp_qmtp $qmtp_pid

	test_qmail_remote_auth "PLAIN"
	test_qmail_remote_auth "LOGIN"
	test_qmail_remote_auth "XOAUTH2"
	(
	printf "#!/bin/sh\n"
	echo "exec $sys_pwd \$*"
	) > $testdir/bin/authsmtp
	chmod +x $testdir/bin/authsmtp
	test_qmail_remote_auth "CRAM-MD5"
	test_qmail_remote_auth "CRAM-SHA1"
	test_qmail_remote_auth "CRAM-SHA224"
	test_qmail_remote_auth "CRAM-SHA256"
	test_qmail_remote_auth "CRAM-SHA384"
	test_qmail_remote_auth "CRAM-SHA512"
	test_qmail_remote_auth "CRAM-RIPEMD"
	test_qmail_remote_auth "DIGEST-MD5"
	/bin/rm -f $testdir/bin/authsmtp
	create_authsmtp
	test_qmail_remote_routing
	test_qmail_remote_auth_cdb
	test_qmail_remote_starttls
	test_smtp_tls
	test_smtp_notls
	if [ -f $dkim_key ] ; then
		test_smtp_dkimverify
	fi
	test_smtp_invalid_auth
	test_smtp_auth_swaks "LOGIN"
	test_smtp_auth_swaks "PLAIN"
	test_smtp_auth_swaks "CRAM-MD5"
	test_smtp_auth_swaks "CRAM-SHA1"
	(
	printf "#!/bin/sh\n"
	echo "exec $sys_pwd \$*"
	) > $testdir/bin/authsmtp
	test_smtp_auth_swaks "DIGEST-MD5"
	/bin/rm -f $testdir/bin/authsmtp
	create_authsmtp
	test_tcpclient_script 1 # test smtp using tcpclient
	terminate_smtp_qmtp $smtp_pid

	# start smtps using tcpserver
	for i in TLSv1_2 TLSv1_3
	do
		start_tcpserver_smtps $i
		if [ -n "$i" ] ; then
			test_smtps "mail doing SMTPS with swaks     qmail-smtpd-TLS=$i"
		else
			test_smtps "mail doing SMTPS with swaks     qmail-smtpd-TLS=auto"
		fi
		echo $i > $cntrldir/tlsclientmethod
		test_qmail_remote_smtps
		for j in TLSv1_2 TLSv1_3
		do
			test_tcpclient_script 2 $j # test smtps using tcpclient
		done
		terminate_smtp_qmtp $smtp_pid
	done

	start_tcpserver_smtp_unix
	smtp_pid=$(/bin/ps -ef|grep $smtp_socket|grep qmail-smtpd|awk '{print $2}')
	test_tcpclient_script 3 # test smtp using tcpclient
	terminate_smtp_qmtp $smtp_pid

	start_tcpserver_smtps_unix
	smtp_pid=$(/bin/ps -ef|grep $smtp_socket|grep qmail-smtpd|awk '{print $2}')
	test_tcpclient_script 4 # test smtps using tcpclient
	terminate_smtp_qmtp $smtp_pid

	# terminate qmail-send, slowq-send
	terminate_send qmail-send "$send_pid"
	terminate_send slowq-send-1 "$slowq_pid1"
	terminate_send slowq-send-2 "$slowq_pid2"
	terminate_send qmta-send "$qmta_pid1"
	terminate_send qmta-send "$qmta_pid2"
	send_pid=""
	slowq_pid1=""
	slowq_pid2=""
	qmta_pid1=""
	qmta_pid2=""
}

test_qmqp()
{
	t1=$(date +"%s.%4N")
	svpid=$(sed -n '$p' $servicedir/.svscan.pid)
	if [ -z "$svpid" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		echo "  testing qmail-qmqpd startup failed (svscan not running) [$secs sec]"
		failed=1
		exit 1
	fi
	mkdir -p $servicedir/qmqpd/variables
	mkdir -p $servicedir/qmqpd/log
	echo 1              > $servicedir/qmqpd/variables/USE_QPWGR
	echo 0              > $servicedir/qmqpd/variables/BIGTODO
	echo 23             > $servicedir/qmqpd/variables/CONFSPLIT
	echo $cntrldir      > $servicedir/qmqpd/variables/CONTROLDIR
	echo $testdir/queue > $servicedir/qmqpd/variables/QUEUEDIR
	echo "127.0.0.1"    > $cntrldir/qmqpservers
	(
	printf "#!/bin/sh\nexec $envdir ./variables $tcpserver -u qmaild -g qmail -v -HR 0 "
	printf "$qmqp_port $qmail_qmqpd 2>&1\n"
	) > $servicedir/qmqpd/run
	printf "#!/bin/sh\nexec $multilog t $logdir/qmqpd\n" > $servicedir/qmqpd/log/run
	chmod +x $servicedir/qmqpd/run
	chmod +x $servicedir/qmqpd/log/run
	sudo kill -1 $svpid
	sleep $sleep_int
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: Testing QMQP protocol $$"
	echo "Date: $(date -R)"
	echo
	echo "QMQP Test message from $HOSTNAME"
	) | env - \
			QUEUEDIR=$testdir/queue \
			QMAILQUEUE=$qmail_qmqpc \
			CONTROLDIR=$cntrldir \
			BIGTODO=0 \
			CONFSPLIT=23 \
			PORT_QMQP=$qmqp_port \
	$qmail_inject -f$user@$HOSTNAME $testuser@$HOSTNAME
	check_mail_header $testdir/$testuser/Maildir Subject 0
	if [ -n "$mail_file" ] ; then
		subject=$header
		body=$($e822body < $testdir/$testuser/Maildir/new/$mail_file)
		/bin/rm -f $testdir/$testuser/Maildir/new/$mail_file
	else
		body=""
		subject=""
	fi
	if [ "$body" = "QMQP Test message from $HOSTNAME" -a "$subject" = "Subject: Testing QMQP protocol $$" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-qmqpc,  qmail-qmqpd succeeded %58s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-qmqpc, qmail-qmqpd failed [$secs sec]"
		(
		echo "body=$body"
		echo "subject$subject"
		cat $logdir/qmqpd/current
		)|less
		failed=1
		exit 1
	fi
}

test_qmtp()
{
	t1=$(date +"%s.%4N")
	svpid=$(sed -n '$p' $servicedir/.svscan.pid)
	if [ -z "$svpid" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs $sleep_int | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-qmtpd startup failed (svscan not running) [$secs sec]"
		failed=1
		exit 1
	fi
	mkdir -p $servicedir/qmtpd/variables
	mkdir -p $servicedir/qmtpd/log
	echo 1              > $servicedir/qmtpd/variables/USE_QPWGR
	echo 0              > $servicedir/qmtpd/variables/BIGTODO
	echo 23             > $servicedir/qmtpd/variables/CONFSPLIT
	echo $cntrldir      > $servicedir/qmtpd/variables/CONTROLDIR
	echo $testdir/queue > $servicedir/qmtpd/variables/QUEUEDIR
	(
	printf "#!/bin/sh\nexec $envdir ./variables $tcpserver -u qmaild -g qmail -v -HR 0 "
	printf "$qmtp_port $qmail_qmtpd 2>&1\n"
	) > $servicedir/qmtpd/run
	printf "#!/bin/sh\nexec $multilog t $logdir/qmtpd\n" > $servicedir/qmtpd/log/run
	chmod +x $servicedir/qmtpd/run
	chmod +x $servicedir/qmtpd/log/run
	sudo kill -1 $svpid
	sleep $sleep_int

	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: Testing QMTP protocol $$"
	echo "Date: $(date -R)"
	echo
	echo "QMTP Test message from $HOSTNAME"
	) > $tmpdir/mail.txt

	size=$(ls -l $tmpdir/mail.txt|awk '{print $5}')
	env - QMTPROUTE="$HOSTNAME:127.0.0.1:$qmtp_port" CONTROLDIR="$cntrldir" \
		$qmail_remote $HOSTNAME $user@$HOSTNAME "" $size $testuser@$HOSTNAME </$tmpdir/mail.txt \
		> $logdir/qmail-remote/qmail-remote.log
	/bin/rm -f $tmpdir/mail.txt
	check_mail_header $testdir/$testuser/Maildir Subject 0
	if [ -n "$mail_file" ] ; then
		subject=$header
		body=$($e822body < $testdir/$testuser/Maildir/new/$mail_file)
		/bin/rm -f $testdir/$testuser/Maildir/new/$mail_file
	else
		body=""
		subject=""
	fi
	if [ "$body" = "QMTP Test message from $HOSTNAME" -a "$subject" = "Subject: Testing QMTP protocol $$" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-remote, qmail-qmtpd succeeded %58s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-remote, qmail-qmtpd failed [$secs sec]"
		(
		echo "body=$body"
		echo "subject=$subject"
		cat $logdir/qmtpd/current
		)|less
		failed=1
		exit 1
	fi
}

test_hide_host_ip()
{
	echo 1 > $servicedir/smtpd/variables/HIDE_HOST
	/bin/rm -f $servicedir/smtpd/variables/SMTPS
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port \
		--a LOGIN -au $user@$HOSTNAME -ap "abcd12345678" >/dev/null
	check_mail_header $maildir Received 0
	if [ -n "$mail_file" ] ; then
		$e822header -I Received < $maildir/new/$mail_file > $tmpdir/header
		/bin/rm -f $mail_file
		l1=$(sed -n 1p < $tmpdir/header)
		l2=$(sed -n 2p < $tmpdir/header)
		l3=$(sed -n 4p < $tmpdir/header)
		echo $l1 | grep $HOSTNAME > /dev/null
		ret1=$?
		echo $l2 | grep 127.0.0.1 > /dev/null
		ret2=$?
		echo $l3 | grep $HOSTNAME > /dev/null
		ret3=$?
		echo $l3 | grep 127.0.0.1 > /dev/null
		ret4=$?
	else
		ret1=0
		ret2=0
		ret3=0
		ret4=0
	fi
	if [ $ret1 -ne 0 -a $ret2 -ne 0 -a $ret3 -ne 0 -a $ret4 -ne 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing hide host, ip in received headers succeeded %50s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_int $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		echo "  testing hide host, ip in received headers failed [$secs sec]"
		(
		echo "l1=[$l1] (host), l2=[$l2] (ip), l3=[$l3] (host+ip)"
		echo "ret1=$ret1, ret2=$ret2, ret3=$ret3, ret4=$ret4"
		echo "header$header"
		cat $logdir/smtpd/current
		)|less
		failed=1
		exit 1
	fi
	/bin/rm -f $servicedir/smtpd/variables/HIDE_HOST
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
}

do_svscan_without_svscanlog()
{
	t1=$(date +"%s.%4N")
	# start svscan without svscanlog
	printf "\r%118s\n" " "
	echo "Starting svscan services without svscanlog"
	setup_svscan_basic
	start_svscan_without_svscanlog
	svpid=$(sed -n '$p' $servicedir/.svscan.pid)
	create_authsmtp
	test_smtps "mail using SMTPS by qmail-smtpd"
	test_smtp_accesslist 0
	test_smtp_accesslist 1
	test_envrules 0
	test_envrules 1
	test_qmqp
	test_qmtp
	test_smtp_secure_auth
	test_recipients
	test_smtp_cugmail
	test_smtp_chk_sender
	test_smtp_surbl
	test_autoresponder 0
	test_qmail_qfilter
	test_blackholed
	test_blackholed 1
	test_stop_smtps
	test_stop_supervise
	test_restart_service_using_hup
	test_restart_service_using_svc
	test_multilog
	start_new_service
	remove_new_service

	# hide ip
	test_hide_host_ip

	sudo svc -a $servicedir/smtpd/log
	echo blocked@$HOSTNAME > $cntrldir/badmailfrom
	echo blocked@$HOSTNAME > $cntrldir/badrcptto
	test_blocked 1 #badrcptto
	test_blocked 2 #badmailfrom

	echo blocked@$HOSTNAME > $cntrldir/goodrcptto
	test_blocked 9 #goodrcptto exact match
	#/bin/rm -f $cntrldir/goodrcptto

	echo 1 > $servicedir/smtpd/variables/QREGEX
	echo block.*@$HOSTNAME > $cntrldir/goodrcptto
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	test_blocked 10 #goodrcptto regex match

	echo 0 > $servicedir/smtpd/variables/QREGEX
	echo block*@$HOSTNAME > $cntrldir/goodrcptto
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	test_blocked 11 #goodrcptto wildmat

	echo block*@$HOSTNAME > $cntrldir/goodrcptpatterns
	test_blocked 12 #goodrcptpatterns wildmat
	/bin/rm -f $cntrldir/goodrcptpatterns

	/bin/rm -f $cntrldir/badmailfrom
	/bin/rm -f $cntrldir/badrcptto
	echo block*@$HOSTNAME > $cntrldir/badmailpatterns
	echo block*@$HOSTNAME > $cntrldir/badrcptpatterns
	test_blocked 7 #badrcptpatterns
	test_blocked 8 #badmailpatterns

	echo 0 > $servicedir/smtpd/variables/QREGEX
	echo block*@$HOSTNAME > $cntrldir/badmailfrom
	echo block*@$HOSTNAME > $cntrldir/badrcptto
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	test_blocked 3 #badrcptto   wildmat
	test_blocked 4 #badmailfrom wildmat

	echo 1 > $servicedir/smtpd/variables/QREGEX
	echo block.*@$HOSTNAME > $cntrldir/badmailfrom
	echo block.*@$HOSTNAME > $cntrldir/badrcptto
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	test_blocked 5 #badrcptto   wildmat
	test_blocked 6 #badmailfrom wildmat
	/bin/rm -f $servicedir/smtpd/variables/QREGEX
	echo 1 > $servicedir/smtpd/variables/SMTPS
}

do_svscan_with_svscanlog()
{
	t1=$(date +"%s.%4N")
	# start svscan with svscanlog
	printf "\r%118s\n" " "
	setup_svscan_basic
	setup_svscan_with_svscanlog
	(
	printf "#!/bin/sh\nexec $envdir ./variables $tcpserver -n $certdir/servercert.pem -u qmaild -g qmail -HR 0 "
	printf "$smtp_port $qmail_smtpd $HOSTNAME $testdir/bin/authsmtp "
	printf "/bin/false 2>&1\n"
	) > $servicedir/smtpd/run
	/bin/rm -f $servicedir/smtpd/variables/SMTPS
	echo 1 > $servicedir/.svscan/variables/VERBOSE
	echo "Starting svscan services with svscanlog"
	start_svscan_with_svscanlog
	svpid=$(sed -n '$p' $servicedir/.svscan.pid)
	test_svscan_log
	sudo kill -USR1 $svpid
	test_svscan_run
	sleep $sleep_int
	test_smtps "mail using SMTPS by tcpserver"
	test_svscan_shutdown

	# start svscan with svscanlog and AUTOSCAN
	/bin/rm -f $servicedir/.svscan/variables/VERBOSE
	echo 1 > $servicedir/.svscan/variables/AUTOSCAN
	echo 5 > $servicedir/.svscan/variables/SCANINTERVAL
	start_svscan_with_svscanlog
	svpid=$(sed -n '$p' $servicedir/.svscan.pid)
	start_new_service 1
	test_supervise_subreaper
	remove_new_service
}

test_qmail_qfilter()
{
	echo $testdir/bin/qfrontend > $servicedir/smtpd/variables/QMAILQUEUE
	printf "#!/bin/sh\nexec $envdir ./variables $tcpserver -u qmaild -g qmail -HR 0 $smtp_port $qmail_smtpd 2>&1\n" \
		> $servicedir/smtpd/run
	(
	echo "#!/bin/sh"
	echo "exec qmail-qfilter $testdir/bin/filter1 -- $testdir/bin/filter2"
	) > $testdir/bin/qfrontend
	(
	echo "#!/bin/sh"
	echo "sed -e 's/abcd/$HOSTNAME/' -e 's/Subject: .*/Subject: Testing qmail-qfilter/'"
	echo "exit 0"
	) > $testdir/bin/filter1
	(
	echo "#!/bin/sh"
	echo "cat"
	echo "echo Recipients"
	echo "echo \$QMAILRCPTS"
	echo "echo \"Envelope Size \$ENVSIZE\""
	echo "echo \"Message Size \$MSGSIZE\""
	echo "echo Regards Manny"
	echo "exit 0"
	) > $testdir/bin/filter2

	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	t1=$(date +"%s.%4N")
	chmod +x $testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2
	(
	echo "Sending mail from abcd"
	) | swaks -S --header "Subject: Test Mail" --to $user@$HOSTNAME --from $user@$HOSTNAME \
		--server 127.0.0.1 --port $smtp_port --body -
	check_mail_header $maildir Subject 1
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-qfilter           succeeded %60s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing qmail-qfilter failed [$secs sec]"
		failed=1
		/bin/rm -f $servicedir/smtpd/variables/QMAILQUEUE $testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "#!/bin/sh"
	echo "exec qmail-qfilter $testdir/bin/filter1"
	) > $testdir/bin/qfrontend
	(
	echo "#!/bin/sh"
	echo "exit 100"
	) > $testdir/bin/filter1
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port >$tmpdir/swaks.$$ 2>&1
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$
	echo $l1 | grep ".*554 qq mail server permanently rejected message (#5.3.0)" > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-qfilter           test        permanent reject succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "l1=$l1"
		echo "testing qmail-qfilter test permanent reject failed"
		failed=1
		/bin/rm -f $servicedir/smtpd/variables/QMAILQUEUE $testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "#!/bin/sh"
	echo "exit 111"
	) > $testdir/bin/filter1
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port >$tmpdir/swaks.$$ 2>&1
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$
	echo $l1 | grep ".*451 qq mail server temporarily rejected message (#4.3.0)" > /dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-qfilter           test        temporary reject succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		printf "\r%118s\n" " "
		echo "l1=$l1"
		echo "testing qmail-qfilter test temporary reject failed"
		failed=1
		/bin/rm -f $servicedir/smtpd/variables/QMAILQUEUE $testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "#!/bin/sh"
	echo "exec qmail-qfilter $testdir/bin/filter1"
	) > $testdir/bin/qfrontend
	(
	echo "#!/bin/sh"
	echo "echo \"DThis mail is permanently unacceptable\" 1>&2"
	echo "exit 88"
	) > $testdir/bin/filter1
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port >$tmpdir/swaks.$$ 2>&1
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$
	echo $l1 | grep ".*554 This mail is permanently unacceptable" >/dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-qfilter           test custom permanent reject succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		printf "\r%118s\n" " "
		echo "l1=$l1"
		echo "testing qmail-qfilter test custom permanent reject failed"
		failed=1
		/bin/rm -f $servicedir/smtpd/variables/QMAILQUEUE $testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "#!/bin/sh"
	echo "exec qmail-qfilter $testdir/bin/filter1"
	) > $testdir/bin/qfrontend
	(
	echo "#!/bin/sh"
	echo "echo \"ZThis mail is temporarily unacceptable\" 1>&2"
	echo "exit 88"
	) > $testdir/bin/filter1
	swaks -S --to $user@$HOSTNAME --from $user@$HOSTNAME --server 127.0.0.1 --port $smtp_port >$tmpdir/swaks.$$ 2>&1
	l1=$(sed -n 1p < $tmpdir/swaks.$$)
	/bin/rm -f $tmpdir/swaks.$$
	echo $l1 | grep ".*451 This mail is temporarily unacceptable" >/dev/null
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-qfilter           test custom temporary reject succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		printf "\r%118s\n" " "
		echo "l1=$l1"
		echo "testing qmail-qfilter test custom permanent reject failed"
		failed=1
		/bin/rm -f $servicedir/smtpd/variables/QMAILQUEUE $testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2
		exit 1
	fi

	t1=$(date +"%s.%4N")
	mkdir -p $cntrldir/filters.d
	(
	echo "#!/bin/sh"
	echo ""
	echo "[ -n \"\$ENVSIZE\" -a -n \"\$MSGSIZE\" ] && qmail_qfilter=1 || qmail_qfilter=0"
	echo "[ -z \"\$CONTROLDIR\" ] && CONTROLDIR=/etc/indimail/control"
	echo "# save the email"
	echo "out=\$(mktemp -t badfromXXXXXXXX)"
	echo "cat > \$out"
	echo "from=\$(822addr From < \$out | tr [\"\0\"] [\" \"] | cut -d+ -f2)"
	echo "grep -w \$from \$CONTROLDIR/badfrom >/dev/null"
	echo "ret=\$?"
	echo "exec 0<\$out"
	echo "/bin/rm -f \$out"
	echo "if [ \$ret -eq 0 ] ; then"
	echo "	echo \"Dmail rejected by badfrom\" 1>&2"
	echo "	exit 100"
	echo "fi"
	echo "[ \$qmail_qfilter -eq 1 ] && exit 0 || exec /bin/cat"
	) > $cntrldir/filters.d/qf-bad-from
	chmod +x $cntrldir/filters.d/qf-bad-from
	echo "$testuser@$HOSTNAME" > $cntrldir/badfrom

	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	echo "$qmail_multi /usr/libexec/indimail/qfrontend" > $testdir/variables/QMAILQUEUE
	echo 1              > $testdir/variables/USE_QMAILQFILTER
	touch $cntrldir/qfilters
	do_qmail_inject $testuser@$HOSTNAME $user@$HOSTNAME "Test qfrontend" "Tesing qfrontend" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	/bin/rm -f $tmpdir/inject.error
	if [ $ret -eq 100 -a "$err" = "qmail-inject: fatal: qq mail server permanently rejected message (#5.3.0)" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-qfilter qfrontend test        permanent reject succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret err=$err"
		echo "testing qmail-qfilter qfrontend test permanent reject failed"
		failed=1
		/bin/rm -rf $servicedir/smtpd/variables/QMAILQUEUE $testdir/variables/QMAILQUEUE \
			$testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2 $cntrldir/filters.d
		exit 1
	fi

	t1=$(date +"%s.%4N")
	mkdir -p $cntrldir/filters.d
	(
	echo "#!/bin/sh"
	echo ""
	echo "[ -n \"\$ENVSIZE\" -a -n \"\$MSGSIZE\" ] && qmail_qfilter=1 || qmail_qfilter=0"
	echo "[ -z \"\$CONTROLDIR\" ] && CONTROLDIR=/etc/indimail/control"
	echo "# save the email"
	echo "out=\$(mktemp -t badfromXXXXXXXX)"
	echo "cat > \$out"
	echo "from=\$(822addr From < \$out | tr [\"\0\"] [\" \"] | cut -d+ -f2)"
	echo "grep -w \$from \$CONTROLDIR/badfrom >/dev/null"
	echo "ret=\$?"
	echo "exec 0<\$out"
	echo "/bin/rm -f \$out"
	echo "if [ \$ret -eq 0 ] ; then"
	echo "	echo \"Dmail rejected by badfrom\" 1>&2"
	echo "	exit 111"
	echo "fi"
	echo "[ \$qmail_qfilter -eq 1 ] && exit 0 || exec /bin/cat"
	) > $cntrldir/filters.d/qf-bad-from
	chmod +x $cntrldir/filters.d/qf-bad-from
	echo "$testuser@$HOSTNAME" > $cntrldir/badfrom

	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	echo "$qmail_multi /usr/libexec/indimail/qfrontend" > $testdir/variables/QMAILQUEUE
	echo 1              > $testdir/variables/USE_QMAILQFILTER
	touch $cntrldir/qfilters
	do_qmail_inject $testuser@$HOSTNAME $user@$HOSTNAME "Test qfrontend" "Tesing qfrontend" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	/bin/rm -f $tmpdir/inject.error
	if [ $ret -eq 111 -a "$err" = "qmail-inject: fatal: qq mail server temporarily rejected message (#4.3.0)" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-qfilter qfrontend test        temporary reject succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret err=$err"
		echo "testing qmail-qfilter qfrontend test temporary reject failed"
		failed=1
		/bin/rm -rf $servicedir/smtpd/variables/QMAILQUEUE $testdir/variables/QMAILQUEUE \
			$testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2 $cntrldir/filters.d
		exit 1
	fi

	t1=$(date +"%s.%4N")
	mkdir -p $cntrldir/filters.d
	(
	echo "#!/bin/sh"
	echo ""
	echo "[ -n \"\$ENVSIZE\" -a -n \"\$MSGSIZE\" ] && qmail_qfilter=1 || qmail_qfilter=0"
	echo "[ -z \"\$CONTROLDIR\" ] && CONTROLDIR=/etc/indimail/control"
	echo "# save the email"
	echo "out=\$(mktemp -t badfromXXXXXXXX)"
	echo "cat > \$out"
	echo "from=\$(822addr From < \$out | tr [\"\0\"] [\" \"] | cut -d+ -f2)"
	echo "grep -w \$from \$CONTROLDIR/badfrom >/dev/null"
	echo "ret=\$?"
	echo "exec 0<\$out"
	echo "/bin/rm -f \$out"
	echo "if [ \$ret -eq 0 ] ; then"
	echo "	echo \"Dmail rejected by badfrom\" 1>&2"
	echo "	exit 88"
	echo "fi"
	echo "[ \$qmail_qfilter -eq 1 ] && exit 0 || exec /bin/cat"
	) > $cntrldir/filters.d/qf-bad-from
	chmod +x $cntrldir/filters.d/qf-bad-from
	echo "$testuser@$HOSTNAME" > $cntrldir/badfrom

	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	echo "$qmail_multi /usr/libexec/indimail/qfrontend" > $testdir/variables/QMAILQUEUE
	echo 1              > $testdir/variables/USE_QMAILQFILTER
	touch $cntrldir/qfilters
	do_qmail_inject $testuser@$HOSTNAME $user@$HOSTNAME "Test qfrontend" "Tesing qfrontend" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	/bin/rm -f $tmpdir/inject.error
	if [ $ret -eq 100 -a "$err" = "qmail-inject: fatal: mail rejected by badfrom" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-qfilter qfrontend test custom permanent reject succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret err=$err"
		echo "testing qmail-qfilter qfrontend test custom permanent reject failed"
		failed=1
		/bin/rm -rf $servicedir/smtpd/variables/QMAILQUEUE $testdir/variables/QMAILQUEUE \
			$testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2 $cntrldir/filters.d
		exit 1
	fi

	t1=$(date +"%s.%4N")
	mkdir -p $cntrldir/filters.d
	(
	echo "#!/bin/sh"
	echo ""
	echo "[ -n \"\$ENVSIZE\" -a -n \"\$MSGSIZE\" ] && qmail_qfilter=1 || qmail_qfilter=0"
	echo "[ -z \"\$CONTROLDIR\" ] && CONTROLDIR=/etc/indimail/control"
	echo "# save the email"
	echo "out=\$(mktemp -t badfromXXXXXXXX)"
	echo "cat > \$out"
	echo "from=\$(822addr From < \$out | tr [\"\0\"] [\" \"] | cut -d+ -f2)"
	echo "grep -w \$from \$CONTROLDIR/badfrom >/dev/null"
	echo "ret=\$?"
	echo "exec 0<\$out"
	echo "/bin/rm -f \$out"
	echo "if [ \$ret -eq 0 ] ; then"
	echo "	echo \"Zmail rejected by badfrom\" 1>&2"
	echo "	exit 88"
	echo "fi"
	echo "[ \$qmail_qfilter -eq 1 ] && exit 0 || exec /bin/cat"
	) > $cntrldir/filters.d/qf-bad-from
	chmod +x $cntrldir/filters.d/qf-bad-from
	echo "$testuser@$HOSTNAME" > $cntrldir/badfrom

	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	echo "$qmail_multi /usr/libexec/indimail/qfrontend" > $testdir/variables/QMAILQUEUE
	echo 1              > $testdir/variables/USE_QMAILQFILTER
	touch $cntrldir/qfilters
	do_qmail_inject $testuser@$HOSTNAME $user@$HOSTNAME "Test qfrontend" "Tesing qfrontend" 2>$tmpdir/inject.error
	err=$(cat $tmpdir/inject.error)
	/bin/rm -f $tmpdir/inject.error
	if [ $ret -eq 111 -a "$err" = "qmail-inject: fatal: mail rejected by badfrom" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing qmail-qfilter qfrontend test custom temporary reject succeeded %31s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret err=$err"
		echo "testing qmail-qfilter qfrontend test custom temporary reject failed"
		failed=1
		/bin/rm -rf $servicedir/smtpd/variables/QMAILQUEUE $testdir/variables/QMAILQUEUE \
			$testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2 $cntrldir/filters.d
		exit 1
	fi

	/bin/rm -rf $servicedir/smtpd/variables/QMAILQUEUE $testdir/variables/QMAILQUEUE \
		$testdir/bin/qfrontend $testdir/bin/filter1 $testdir/bin/filter2 $cntrldir/filters.d
	(
	printf "#!/bin/sh\nexec $envdir ./variables $tcpserver -u qmaild -g qmail -v -HR 0 "
	printf "$smtp_port $qmail_smtpd $HOSTNAME $testdir/bin/authsmtp "
	printf "/bin/false 2>&1\n"
	) > $servicedir/smtpd/run
	sudo svc -r $servicedir/smtpd
	sleep $sleep_restart
	return 0
}

test_filterit()
{
	t1=$(date +"%s.%4N")
	(
	if [ $1 -eq 1 ] ; then
		echo "| filterit -xr -h X-QHPSI -k clean -c Equals -a Maildir -A ./Maildir/.Quarantine/ -d exit -D 0"
		echo "| filterit -x -h X-Bogosity -k Yes -c \"Starts with\" -a Maildir -A ./Maildir/.Spam/ -d exit -D 0"
	else
		echo "| $filterit -xr -h X-QHPSI -k clean -c Equals -a Maildir -A ./Maildir/.Quarantine/ -d exit -D 0"
		echo "| $filterit -x -h X-Bogosity -k Yes -c \"Starts with\" -a Maildir -A ./Maildir/.Spam/ -d exit -D 0"
	fi
	echo "./Maildir/"
	) > $testdir/$testuser/.qmail
	if [ $1 -eq 1 ] ; then
	subject="Testing qmail-local internal filterit command"
	else
	subject="Testing qmail-local external filterit command"
	fi
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: $subject"
	echo "Date: $(date -R)"
	echo "X-QHPSI: clean"
	echo
	echo "Test message"
	) | env - \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $testuser
	check_mail $testdir/$testuser/Maildir
	if [ $? -eq 0 ] ; then
		tcount=$(expr $tcount + 1)
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		filterit_test1=$secs
		if [ $1 -eq 1 ] ; then
			printf "\r  testing internal filterit with no-match                         succeeded %28s [%.4f sec]\n" " " $secs
		else
			printf "\r  testing external filterit with no-match                         succeeded %28s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		sudo kill $send_pid
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		if [ $1 -eq 1 ] ; then
			echo "  testing internal filterit with no-match failed [$secs sec]"
		else
			echo "  testing external filterit with no-match failed [$secs sec]"
		fi
		(
		echo "qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: $subject"
	echo "Date: $(date -R)"
	echo "X-QHPSI: virus infected"
	echo
	echo "Test message"
	) | env - \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $testuser
	check_mail $testdir/$testuser/Maildir/.Quarantine
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		filterit_test2=$secs
		tcount=$(expr $tcount + 1)
		if [ $1 -eq 1 ] ; then
			printf "\r  testing internal filterit with negative match deliver to folder succeeded %28s [%.4f sec]\n" " " $secs
		else
			printf "\r  testing external filterit with negative match deliver to folder succeeded %28s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		sudo kill $send_pid
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		if [ $1 -eq 1 ] ; then
			echo "  testing internal filterit with negative match deliver to folder failed [$secs sec]"
		else
			echo "  testing external filterit with negative match deliver to folder failed [$secs sec]"
		fi
		(
		echo "qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		exit 1
	fi

	t1=$(date +"%s.%4N")
	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: $subject"
	echo "Date: $(date -R)"
	echo "X-Bogosity: No, spamicity=0.520000, cutoff=9.90e-01, ham_cutoff=0.00e+00,version=1.0.0,register-No 42 words 1 msg"
	echo
	echo "Test message"
	) | env - \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $testuser
	check_mail $testdir/$testuser/Maildir
	ret1=$?
	s1=$sleep_secs

	(
	echo "From: $user@$HOSTNAME"
	echo "To: $testuser@$HOSTNAME"
	echo "Subject: $subject"
	echo "Date: $(date -R)"
	echo "X-Bogosity: Yes, spamicity=0.520000, cutoff=9.90e-01, ham_cutoff=0.00e+00,version=1.0.0,register-No 42 words 1 msg"
	echo
	echo "Test message"
	) | env - \
		QUEUEDIR=$testdir/queue \
		QMAILQUEUE=$qmail_queue \
		CONTROLDIR=$cntrldir \
		BIGTODO=0 \
		CONFSPLIT=23 \
	$qmail_inject -f$user@$HOSTNAME $testuser
	check_mail $testdir/$testuser/Maildir/.Spam
	ret2=$?
	s2=$sleep_secs

	if [ $ret1 -eq 0 -a $ret2 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		filterit_test3=$secs
		tcount=$(expr $tcount + 1)
		if [ $1 -eq 1 ] ; then
			printf "\r  testing internal filterit with positive match deliver to folder succeeded %28s [%.4f sec]\n" " " $secs
		else
			printf "\r  testing external filterit with positive match deliver to folder succeeded %28s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		sudo kill $send_pid
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		if [ $1 -eq 1 ] ; then
			echo "  testing internal filterit with positive match deliver to folder failed [$secs sec]"
		else
			echo "  testing external filterit with positive match deliver to folder failed [$secs sec]"
		fi
		(
		echo "qmail-send.log"
		cat $logdir/qmail-send/qmail-send.log
		)|less
		failed=1
		exit 1
	fi
}

test_spf()
{
	echo "Starting SPF Tests"
	count=1
	while read line
	do
		t1=$(date +"%s.%4N")
		first=${line:0:1}
		if [ "$first" = "#" ] ; then
			count=$(expr $count + 1)
			continue
		fi
		exit_val=$(echo $line |cut -d: -f1)
		cmd=$(echo $line |cut -d: -f2-)
		$cmd > $tmpdir/spf.out 2>$tmpdir/spf.err
		ret=$?
		if [ $ret -eq $exit_val ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
			tcount=$(expr $tcount + 1)
			if [ $verbose -gt 0 ] ; then
				l1=$(cat $tmpdir/spf.out|sed -n 1p)
				l2=$(cat $tmpdir/spf.out|sed -n 2p)
				echo "  $l1"
				echo "  $l2"
			fi
			printf "\r  testing spf test%02d succeded ret=%d == %d %63s [%0.4f sec]\n" $count $ret $exit_val " " $secs
			print_pct
		else
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
			if [ $verbose -gt 0 ] ; then
				l1=$(cat $tmpdir/spf.out|sed -n 1p)
				l2=$(cat $tmpdir/spf.out|sed -n 2p)
				echo "  $l1"
				echo "  $l2"
			fi
			printf "\r  testing spf test%02d failed   ret=%d != %d %63s [%0.4f sec]\n" $count $ret $exit_val " " $secs
			print_pct
			#cat $tmpdir/spf.err
			#l1=$(cat $tmpdir/spf.out|sed -n 1p)
			#l2=$(cat $tmpdir/spf.out|sed -n 2p)
			#echo "  $l1"
			#echo "  $l2"
			#/bin/rm -f $tmpdir/spf.out $tmpdir/spf.err
			#failed=1
			#exit 1
		fi
		count=$(expr $count + 1)
	done < spfquery.txt
	/bin/rm -f $tmpdir/spf.out $tmpdir/spf.err
}

test_qmail_queue()
{
	# sender rule
	t1=$(date +"%s.%4N")
	if [ $1 -eq 0 ] ; then
	echo "F:*@$HOSTNAME:$outarchive@$HOSTNAME" > $cntrldir/mailarchive
	else
	echo "F:.*@$HOSTNAME$:$outarchive@$HOSTNAME" > $cntrldir/mailarchive
	fi
	/bin/rm -rf $testdir/variables
	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	if [ $1 -eq 0 ] ; then
	                    > $testdir/variables/QREGEX
	else
	echo 1              > $testdir/variables/QREGEX
	fi
	do_qmail_inject $user@$HOSTNAME $user@$HOSTNAME "Test outgoing Mail Archival" "Test Message to test outgoing Mail Archival"
	check_mail $testdir/$outarchive/Maildir
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		if [ $1 -eq 0 ] ; then
		printf "\r  testing outgoing mail archival with sender based wildmat rule succeeded %30s [%.4f sec]\n" " " $secs
		else
		printf "\r  testing outgoing mail archival with sender based REGEXP rule succeeded %31s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		if [ $1 -eq 0 ] ; then
		echo "  testing outgoing mail archival with sender based wildmat rule failed [$secs sec]"
		else
		echo "  testing outgoing mail archival with sender based REGEXP rule failed [$secs sec]"
		fi
		failed=1
		exit 1
	fi

	# sender negative rule
	t1=$(date +"%s.%4N")
	if [ $1 -eq 0 ] ; then
	echo "F:!*@$HOSTNAME:$incarchive@$HOSTNAME" > $cntrldir/mailarchive
	else
	echo "F:!.*@$HOSTNAME$:$incarchive@$HOSTNAME" > $cntrldir/mailarchive
	fi
	do_qmail_inject $user@$vdomain $user@$HOSTNAME "Test Mail Archival" "Test Message for Mail Archival"
	check_mail $testdir/$incarchive/Maildir
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		if [ $1 -eq 0 ] ; then
		printf "\r  testing incoming mail archival with sender based wildmat rule succeeded %30s [%.4f sec]\n" " " $secs
		else
		printf "\r  testing incoming mail archival with sender based REGEXP rule succeeded %31s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		if [ $1 -eq 0 ] ; then
		echo "  testing incoming mail archival with sender based wildmat rule failed [$secs sec]"
		else
		echo "  testing incoming mail archival with sender based REGEXP rule failed [$secs sec]"
		fi
		failed=1
		exit 1
	fi

	# recipient rule
	t1=$(date +"%s.%4N")
	if [ $1 -eq 0 ] ; then
	echo "T:*@$HOSTNAME:$incarchive@$HOSTNAME" > $cntrldir/mailarchive
	else
	echo "T:.*@$HOSTNAME$:$incarchive@$HOSTNAME" > $cntrldir/mailarchive
	fi
	do_qmail_inject $user@$vdomain $user@$HOSTNAME "Test incoming Mail Archival" "Test Message to test incoming Mail Archival"
	check_mail $testdir/$incarchive/Maildir
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		if [ $1 -eq 0 ] ; then
		printf "\r  testing incoming mail archival with recipient based wildmat rule succeeded %27s [%.4f sec]\n" " " $secs
		else
		printf "\r  testing incoming mail archival with recipient based REGEXP rule succeeded %28s [%.4f sec]\n" " " $secs
		fi
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		if [ $1 -eq 0 ] ; then
		echo "  testing incoming mail archival with recipient based wildmat rule failed [$secs sec]"
		else
		echo "  testing incoming mail archival with recipient based REGEXP rule failed [$secs sec]"
		fi
		failed=1
		exit 1
	fi

	# receipient negative rule
	t1=$(date +"%s.%4N")
	setup_virtual 1
	if [ $1 -eq 0 ] ; then
	echo "T:!*@$HOSTNAME:$outarchive@$HOSTNAME"   > $cntrldir/mailarchive
	else
	echo "T:!.*@$HOSTNAME$:$outarchive@$HOSTNAME" > $cntrldir/mailarchive
	fi
	do_qmail_inject $user@$HOSTNAME $user@$vdomain "Testing outgoing Mail Archival" "Test Message to test outgoing Mail Archival"
	check_mail $testdir/$outarchive/Maildir
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		if [ $1 -eq 0 ] ; then
		printf "\r  testing outgoing mail archival with recipient based wildmat rule succeeded %27s [%.4f sec]\n" " " $secs
		else
		printf "\r  testing outgoing mail archival with recipient based REGEXP rule succeeded %28s [%.4f sec]\n" " " $secs
		fi
		print_pct
		/bin/rm -f $cntrldir/mailarchive
		setup_virtual 0
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		if [ $1 -eq 0 ] ; then
		echo "  testing outgoing mail archival with recipient based wilmat rule failed [$secs sec]"
		else
		echo "  testing outgoing mail archival with recipient based REGEXP rule failed [$secs sec]"
		fi
		failed=1
		/bin/rm -f $cntrldir/mailarchive
		setup_virtual 0
		exit 1
	fi

	if [ $1 -eq 0 ] ; then
		return 0
	fi
	# invalid regex in mailarchive
	t1=$(date +"%s.%4N")
	echo "T:*@$vdomain$:$incarchive@$HOSTNAME" > $cntrldir/mailarchive
	do_qmail_inject $user@$vdomain $user@$HOSTNAME "Test incoming Mail Archival" "Test Message to test incoming Mail Archival"
	check_mail $testdir/$incarchive/Maildir 2>/dev/null
	if [ $? -ne 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail archival with invalid recipient based rule succeeded %36s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $sleep_secs | awk '{printf("%0.4f\n", $2-$1-$3)}')
		printf "\r%118s\n" " "
		echo "  testing mail archival with invalid recipient based rule failed [$secs sec]"
		failed=1
		exit 1
	fi

	# test extra queue
	t1=$(date +"%s.%4N")
	(
	echo "$incarchive"
	echo "$outarchive"
	) > $cntrldir/extraqueue
	do_qmail_inject $user@$HOSTNAME $user@$HOSTNAME "Testing extraqueue" "Test Message for extraqueue"
	check_mail $testdir/$incarchive/Maildir
	ret1=$?
	s1=$sleep_secs
	check_mail $testdir/$outarchive/Maildir
	ret2=$?
	s2=$sleep_secs
	if [ $ret1 -eq 0 -a $ret2 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail for extraqueue feature succeeded %56s [%.4f sec]\n" " " $secs
		print_pct
	else
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
		printf "\r%118s\n" " "
		echo "  testing mail for extraqueue feature failed [$secs sec]"
		failed=1
		/bin/rm -f $cntrldir/extraqueue
		exit 1
	fi
	/bin/rm -f $cntrldir/extraqueue

	if [ ! -f /etc/debian_version ] ; then
		# test logheader
		t1=$(date +"%s.%4N")
		echo "X-Log-Test" > $cntrldir/logheaders
		exec 45>$tmpdir/logheaderfd
		echo 45 > $testdir/variables/LOGHEADERFD
		do_qmail_inject $user@$HOSTNAME $user@$HOSTNAME "Testing logheader" "Test Message for logheader"
		check_mail_header $maildir X-Remove-Header1 0
		h4=$header
		s1=$sleep_secs
		check_mail_header $maildir X-Remove-Header2 0
		h5=$header
		s2=$sleep_secs
		h=$(cat $tmpdir/logheaderfd)
		if [ "$h" = "X-Log-Test: Test Header" ] ; then
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
			tcount=$(expr $tcount + 1)
			printf "\r  testing mail for logheader feature succeeded %57s [%.4f sec]\n" " " $secs
			print_pct
		else
			echo "h=[$h] h4=[$h4] h5=[$h5]"
			t2=$(date +"%s.%4N")
			secs=$(echo $t1 $t2 $s1 $s2 | awk '{printf("%0.4f\n", $2-$1-$3-$4)}')
			printf "\r%118s\n" " "
			echo "  testing mail for logheader feature failed [$secs sec]"
			failed=1
			/bin/rm -f $cntrldir/logheaders
			exit 1
		fi
		check_mail
		/bin/rm -f $cntrldir/logheaders
		if [ "$h4" = "X-Remove-Header1: This header will be removed" -a "$h5" = "X-Remove-Header2: This header will also be removed" ] ; then
			ret1=0
		else
			ret1=1
		fi
	fi

	# test removeheaders
	t1=$(date +"%s.%4N")
	(
	echo "X-Remove-Header1"
	echo "X-Remove-Header2"
	) > $cntrldir/removeheaders
	/bin/rm -f $testdir/variables/LOGHEADERFD
	do_qmail_inject $user@$HOSTNAME $user@$HOSTNAME "Testing removeheaders" "Test Message for removeheaders"
	check_mail_header $maildir X-Remove-Header1 0
	s1=$sleep_secs
	h1=$header
	check_mail_header $maildir X-Remove-Header2 0
	s2=$sleep_secs
	h2=$header
	check_mail_header $maildir X-Log-Test 0
	s3=$sleep_secs
	h3=$header
	if [ $ret1 -eq 0 -a -z "$h1" -a -z "$h2" -a "$h3" = "X-Log-Test: Test Header" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 $s3 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail for removeheaders feature succeeded %53s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret1, h1=$h1, h2=$h2, h3=[$h3], h4=$h4, h5=$h5"
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 $s3 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		echo "  testing mail for removeheaders feature failed [$secs sec]"
		failed=1
		/bin/rm -f $cntrldir/removeheaders
		exit 1
	fi
	check_mail
	/bin/rm -f $cntrldir/removeheaders

	# test envheaders
	t1=$(date +"%s.%4N")
	(
	echo "X-Env-Header1"
	echo "X-Env-Header2"
	echo "Subject"
	) > $cntrldir/envheaders

	(
	echo "#!/bin/sh"
	echo "if [ "\$X_Env_Header1" != "This is env-header1" ] ; then"
	echo "  echo \"This email does not contain X_Env_Header1\" 1>&2"
	echo "  exit 100"
	echo "fi"
	echo "if [ "\$X_Env_Header2" != "This is env-header2" ] ; then"
	echo "  echo \"This email does not contain X_Env_Header2\" 1>&2"
	echo "  exit 100"
	echo "fi"
	echo "echo \"X-Env-Header3: This is env-header3\""
	echo "exec /bin/cat"
	) > $testdir/bin/envheaders.sh
	chmod +x $testdir/bin/envheaders.sh
	echo ".*:local:$testdir/bin/envheaders.sh" > $cntrldir/filterargs
	do_qmail_inject $user@$HOSTNAME $testuser@$HOSTNAME "Testing envheaders" "Test Message for envheaders"
	check_mail_header $testdir/$testuser/Maildir X-Env-Header1 0
	ret1=$?
	s1=$sleep_secs
	h1=$header
	check_mail_header $testdir/$testuser/Maildir X-Env-Header2 0
	s2=$sleep_secs
	h2=$header
	check_mail_header $testdir/$testuser/Maildir X-Env-Header3 0
	s3=$sleep_secs
	h3=$header
	if [ $ret1 -eq 0 -a "$h1" = "X-Env-Header1: This is env-header1" -a "$h2" = "X-Env-Header2: This is env-header2" -a "$h3" = "X-Env-Header3: This is env-header3" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 $s3 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing mail for envheaders feature succeeded %56s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "ret=$ret1, h1=$h1, h2=$h2, h3=[$h3]"
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 $s1 $s2 $s3 | awk '{printf("%0.4f\n", $2-$1-$3-$4-$5)}')
		echo "  testing mail for envheaders feature failed [$secs sec]"
		failed=1
		/bin/rm -rf $testdir/variables $testdir/bin/envheaders.sh $cntrldir/filterargs
		exit 1
	fi
	check_mail $testdir/$testuser/Maildir
	/bin/rm -rf $testdir/variables $testdir/bin/envheaders.sh $cntrldir/filterargs
}

test_ezmlm_idx()
{
	echo "$vdomain:$vdomain" > $cntrldir/virtualdomains
	echo "$vdomain" >> $cntrldir/rcpthosts
	(
	echo "=$user:$user:$myuid:$mygid:$testdir/$user:::"
	echo "+$user-:$user:$myuid:$mygid:$testdir/$user:-::"
	echo "=$testuser:$testuser:$myuid:$mygid:$testdir/$testuser:::"
	echo "+$testuser-:$testuser:$myuid:$mygid:$testdir/$testuser:-::"
	echo "+$vdomain-:$vdomain:$myuid:$mygid:$qmaildir/domains/$vdomain:-::"
	echo "."
	) > $sysconfdir/users/assign

	if [ ! -d $sysconfdir/ezmlm ] ; then
		cp -rp /etc/indimail/ezmlm $sysconfdir/ezmlm
		/bin/rm -rf $sysconfdir/ezmlm/global_vars
		mkdir -p $sysconfdir/ezmlm/global_vars
		ln -srf $sysconfdir/control/global_vars $sysconfdir/ezmlm/global_vars/.envdir
		echo 0              > $sysconfdir/ezmlm/global_vars/BIGTODO
		echo 23             > $sysconfdir/ezmlm/global_vars/CONFSPLIT
		echo $testdir/queue > $sysconfdir/ezmlm/global_vars/QUEUEDIR
	fi
	sudo chown -R $myuid:$mygid $logdir/qmail-send
	/bin/rm -f $logdir/qmail-send/*
	silent=1 run_qmail_send qmail-send 1

	/bin/rm -rf $testdir/variables
	mkdir -p $testdir/variables
	echo $testdir/queue > $testdir/variables/QUEUEDIR
	echo $cntrldir      > $testdir/variables/CONTROLDIR
	echo 0              > $testdir/variables/BIGTODO
	echo 23             > $testdir/variables/CONFSPLIT
	echo $qmail_queue   > $testdir/variables/QMAILQUEUE
	$qmail_newu $sysconfdir/users
	ml=fun
	mkdir -p $qmaildir/domains/$vdomain

	t1=$(date +"%s.%4N")
	envdir -c $testdir/variables ezmlm-make $qmaildir/domains/$vdomain/$ml $qmaildir/domains/$vdomain/.qmail-$ml $ml $vdomain
	if [ $? -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing ezmlm-make  succeeded %72s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "testing ezmlm-make failed"
		failed=1
		/bin/rm -rf $qmaildir/domains
		exit 1
	fi
	t1=$(date +"%s.%4N")
	envdir -c $testdir/variables ezmlm-sub $qmaildir/domains/$vdomain/$ml $user@$HOSTNAME
	ret1=$?
	envdir -c $testdir/variables ezmlm-sub $qmaildir/domains/$vdomain/$ml $testuser@$HOSTNAME
	ret2=$?
	if [ $ret1 -eq 0 -a $ret2 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing ezmlm-sub   succeeded %72s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "testing ezmlm-sub failed"
		failed=1
		/bin/rm -rf $qmaildir/domains
		exit 1
	fi
	t1=$(date +"%s.%4N")
	list=$(ezmlm-list $qmaildir/domains/$vdomain/$ml)
	ret1=1
	ret2=1
	for i in $list
	do
		echo $i | grep $user@$HOSTNAME >/dev/null
		if [ $? -eq 0 ] ; then
			ret1=0
		fi
		echo $i | grep $testuser@$HOSTNAME >/dev/null
		if [ $? -eq 0 ] ; then
			ret2=0
		fi
	done
	if [ $ret1 -eq 0 -a $ret2 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing ezmlm-list  succeeded %72s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "lines=$lines, ret1=$ret1, ret2=$ret2, list=$list"
		echo "testing ezmlm-list failed"
		failed=1
		/bin/rm -rf $qmaildir/domains
		exit 1
	fi

	/bin/rm -rf $maildir/new/* $testdir/$testuser/Maildir/new/*
	echo "$testdir/$user/Maildir/" > $qmaildir/domains/$vdomain/.qmail-default
	t1=$(date +"%s.%4N")
	do_qmail_inject $user@$HOSTNAME "$ml@$vdomain" "Testing qmail-inject to mailing list" "Testing qmail-inject to mailing list"
	check_mail_header $maildir "Subject" 1
	ret1=$?
	header1=$header
	check_mail_header $testdir/$testuser/Maildir "Subject" 1
	ret2=$?
	header2=$header
	if [ $ret1 -eq 0 -a $ret2 -eq 0 -a "$header1" = "Subject: Testing qmail-inject to mailing list" -a "$header2" = "Subject: Testing qmail-inject to mailing list" ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing ezmlm-send  succeeded %72s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "header1=$header1, header2=$header2, ret1=$ret1, ret2=$ret2, list=$list"
		echo "testing ezmlm-send failed"
		failed=1
		/bin/rm -rf $qmaildir/domains
		exit 1
	fi

	t1=$(date +"%s.%4N")
	envdir -c $testdir/variables ezmlm-unsub $qmaildir/domains/$vdomain/$ml $testuser@$HOSTNAME
	ret1=$?
	list=$(ezmlm-list $qmaildir/domains/$vdomain/$ml)
	ret2=1
	ret3=0
	for i in $list
	do
		echo $i | grep $user@$HOSTNAME >/dev/null
		if [ $? -eq 0 ] ; then
			ret2=0
		fi
		echo $i | grep $testuser@$HOSTNAME >/dev/null
		if [ $? -eq 0 ] ; then
			ret3=1
		fi
	done
	if [ $ret1 -eq 0 -a $ret2 -eq 0 -a $ret3 -eq 0 ] ; then
		t2=$(date +"%s.%4N")
		secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
		tcount=$(expr $tcount + 1)
		printf "\r  testing ezmlm-unsub succeeded %72s [%.4f sec]\n" " " $secs
		print_pct
	else
		printf "\r%118s\n" " "
		echo "lines=$lines, ret1=$ret1, ret2=$ret2, list=$list"
		echo "  testing ezmlm-unsub failed"
		failed=1
		/bin/rm -rf $qmaildir/domains
		exit 1
	fi
}

test_dkim()
{
	t1=$(date +"%s.%4N")
	printf "\r%126s\r" " "
	echo "Starting DKIM Tests"
	mkdir -p $testdir
	if [ ! -d $domainkey_dir ] ; then
		mkdir -p $domainkey_dir
	fi
	/bin/rm -f $cntrldir/dkimkeys
	for i in example.com example.org
	do
		if [ ! -d $domainkey_dir/$i ] ; then
			sudo mkdir -p $domainkey_dir/$i
		fi
		if [ ! -f $domainkey_dir/$i/default ] ; then
			echo "Creating private key bits 4096 $domainkey_dir/$i/default"
			sudo $dknewkey -e -b 4096 $domainkey_dir/$i/default >$out
			sudo chmod 644 $domainkey_dir/$i/default
		fi
		if [ ! -f $domainkey_dir/$i/dkimkeys1 ] ; then
			echo "Creating private key bits 2048 $domainkey_dir/$i/dkimkeys1"
			sudo $dknewkey -e -b 2048 $domainkey_dir/$i/dkimkeys1 >$out
			sudo chmod 644 $domainkey_dir/$i/dkimkeys1
		fi
		if [ ! -f $domainkey_dir/$i/dkimkeys2 ] ; then
			echo "Creating private key $domainkey_dir/$i/dkimkeys2"
			sudo $dknewkey -e -t ed25519 $domainkey_dir/$i/dkimkeys2 >$out
			sudo chmod 644 $domainkey_dir/$i/dkimkeys2
		fi
	done

	for i in test default
	do
		if [ ! -f $domainkey_dir/$i ] ; then
			echo "Creating private key bits 4096 $domainkey_dir/$i"
			sudo $dknewkey -e -b 4096 $domainkey_dir/$i >$out
			sudo chmod 644 $domainkey_dir/$i
		fi
	done

	for i in example.com example.org
	do
		if [ ! -d $domainkey_dir/$i ] ; then
			sudo mkdir -p $domainkey_dir/$i
		fi
		if [ ! -f $domainkey_dir/$i/ed25519 ] ; then
			echo "Creating private key $domainkey_dir/$i/ed25519"
			sudo $dknewkey -e -t ed25519 $domainkey_dir/$i/ed25519 >$out
			sudo chmod 644 $domainkey_dir/$i/ed25519
		fi
	done
	mkdir -p $cntrldir

	# Test No 1
	failed=0
	printf "\r%126s\r" " "
	echo "testing real domain $real_domain (rsa2048,rsa4096,ed25519) using dkim with selector from dns"
	for k in $real_domain_rsa2048 $real_domain_rsa4096 $real_domain_ed25519
	do
		case $k in
			$real_domain_rsa2048)
			printf "\r%126s\r" " "
			echo "    testing rsa2048"
			;;
			$real_domain_rsa4096)
			printf "\r%126s\r" " "
			echo "    testing rsa4096"
			;;
			$real_domain_ed25519)
			printf "\r%126s\r" " "
			echo "    testing ed25519"
			;;
		esac
		if [ $verbose -gt 0 ] ; then
			echo "testing real domain $real_domain $ktype DKIM record"
		fi
		ktype=$(cat $k.pub| grep "k="|sed 's/v=DKIM1;//'|cut -d= -f2|cut -d';' -f1)
		case $ktype in
			"rsa")
			enc="2"
			;;
			"ed25519")
			enc="4"
			;;
		esac
		(
		echo "From: postmaster@$real_domain"
		echo "To: postmaster@$real_domain"
		echo "Subject: Test"
		echo "Date: $(date -R)"
		echo
		echo "Test message"
		) > $tmpdir/mail.txt
		(cat $tmpdir/mail.txt | $bindir/dkim -z $enc -s "$k"; cat $tmpdir/mail.txt) | $bindir/dkim -v > $out
		ret=$?
		/bin/rm -f $tmpdir/mail.txt
		if [ $ret -eq 0 ] ; then
			tcount=$(expr $tcount + 1)
			if [ $verbose -gt 0 ] ; then
				echo "SUCCESS: [ret=$ret] domain=$real_domain key=$k"
				echo
			fi
			echo SUCCESS ======================== >$out
			print_pct
		else
			echo "FAILED : [ret=$ret] domain=$real_domain key=$k"
			echo
			echo FAILURE [$ret] ======================== >$out
			echo >>$out
			failed=1
		fi
	done

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for real domain $real_domain (rsa2048,rsa4096,ed25519) using $dkim with selector from dns"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for real domain $real_domain (rsa2048,rsa4096,ed25519) using $dkim with selector from dns"
	fi

	# Test No 2
	failed=0
	printf "\r%126s\r" " "
	echo "testing multi-signature real domain $real_domain (rsa4096+ed25519) signing+verification by dkim with selector from dns"
	if [ $verbose -gt 0 ] ; then
		echo "testing real domain $real_domain multi DKIM record"
	fi
	(
	echo "From: postmaster@$real_domain"
	echo "To: postmaster@$real_domain"
	echo "Subject: Test"
	echo "Date: $(date -R)"
	echo
	echo "Test message"
	) > $tmpdir/mail.txt
	(cat $tmpdir/mail.txt | $bindir/dkim -z 2 -s $real_domain_rsa4096 -z 4 -s $real_domain_ed25519; cat $tmpdir/mail.txt) | $bindir/dkim -vV >$out 2>$tmpdir/err.out
	ret=$?
	/bin/rm -f $tmpdir/mail.txt
	grep Signature $tmpdir/err.out | sed -e 's/Signature #/    Signature/g' \
		-e 's/01/rsa-256/' -e 's/02/ed25519/' -e 's/Failure.*/Failure - Correct/'
	if [ $ret -eq 0 ] ; then
		tcount=$(expr $tcount + 1)
		if [ $verbose -gt 0 ] ; then
			echo "SUCCESS: [ret=$ret] domain=$real_domain key=$real_domain_rsa4096+$real_domain_ed25519"
			echo
		fi
		echo SUCCESS ======================== >$out
		print_pct
	else
		echo "FAILED : [ret=$ret] domain=$real_domain key=$real_domain_rsa4096+$real_domain_ed25519"
		echo
		echo FAILURE [$ret] ======================== >$out
		echo >>$out
		failed=1
	fi

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for multi-signature real domain $real_domain (rsa4096+ed25519) using $dkim with selector from dns"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for multi-signature real domain $real_domain (rsa4096+ed25519) using $dkim with selector from dns"
	fi

	# Test No 3
	failed=0
	printf "\r%126s\r" " "
	echo "testing multi-signature real domain $real_domain (rsa4096+ed25519) signing by qmail-dkim, verification by dkim with selector from dns"
	if [ $verbose -gt 0 ] ; then
		echo "testing real domain $real_domain multi DKIM record"
	fi
	(
	echo "From: postmaster@$real_domain"
	echo "To: postmaster@$real_domain"
	echo "Subject: Test"
	echo "Date: $(date -R)"
	echo
	echo "Test message"
	) | env - \
			CONTROLDIR=/tmp \
			DKIMSIGN="$real_domain_rsa4096" \
			DKIMSIGNOPTIONS="-z 2" \
			DKIMSIGNEXTRA="$real_domain_ed25519" \
			DKIMSIGNOPTIONSEXTRA="-z 4" \
			DKIMQUEUE=/bin/cat \
			$qmail_dkim | $bindir/dkim -vV >$out 2>$tmpdir/err.out
	ret=$?
	grep Signature $tmpdir/err.out | sed -e 's/Signature #/    Signature/g' \
		-e 's/01/ed25519/' -e 's/02/rsa-256/' -e 's/Failure.*/Failure - Correct/'
	if [ $ret -eq 0 ] ; then
		tcount=$(expr $tcount + 1)
		if [ $verbose -gt 0 ] ; then
			echo "SUCCESS: [ret=$ret] domain=$real_domain key=$real_domain_rsa4096+$real_domain_ed25519"
			echo
		fi
		echo SUCCESS ======================== >$out
		print_pct
	else
		echo "FAILED : [ret=$ret] domain=$real_domain key=$real_domain_rsa4096+$real_domain_ed25519"
		echo
		echo FAILURE [$ret] ======================== >$out
		echo >>$out
		failed=1
	fi

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for multi-signature real domain $real_domain (rsa4096+ed25519) using $dkim with selector from dns"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for multi-signature real domain $real_domain (rsa4096+ed25519) using $dkim with selector from dns"
	fi

	# Test No 4
	failed=0
	printf "\r%126s\r" " "
	echo "testing real domain $real_domain (rsa2048+rsa4096+ed25519) using qmail-dkim with selector from dns"
	for k in $real_domain_rsa2048 $real_domain_rsa4096 $real_domain_ed25519
	do
		if [ $verbose -gt 0 ] ; then
			echo "testing real domain $real_domain"
		fi
		case $k in
			$real_domain_rsa2048)
			printf "\r%126s\r" " "
			echo "    testing rsa2048"
			;;
			$real_domain_rsa4096)
			printf "\r%126s\r" " "
			echo "    testing rsa4096"
			;;
			$real_domain_ed25519)
			printf "\r%126s\r" " "
			echo "    testing ed25519"
			;;
		esac
		ktype=$(cat $k.pub| grep "k="|sed 's/v=DKIM1;//'|cut -d= -f2|cut -d';' -f1)
		case $ktype in
			"rsa")
			enc="2"
			;;
			"ed25519")
			enc="4"
			;;
		esac
		(
		echo "From: postmaster@$real_domain"
		echo "To: postmaster@$real_domain"
		echo "Subject: Test"
		echo "Date: $(date -R)"
		echo
		echo "Test message"
		) | env - \
			DKIMSIGN="$k" \
			DKIMSIGNOPTIONS="-z $enc" \
			DKIMQUEUE=/bin/cat \
			$qmail_dkim | env - \
				DKIMVERIFY="" \
				DKIMQUEUE=/bin/cat \
				$qmail_dkim > $out
		ret=$?
		if [ $ret -eq 0 ] ; then
			tcount=$(expr $tcount + 1)
			if [ $verbose -gt 0 ] ; then
				echo "SUCCESS: [ret=$ret] domain=$real_domain key=$real_domain_key"
				echo
			fi
			echo SUCCESS ======================== >$out
			print_pct
		else
			echo "FAILED : [ret=$ret] domain=$real_domain key=$real_domain_key"
			echo
			echo FAILURE [$ret] ======================== >$out
			echo >>$out
			failed=1
		fi
	done

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for real domain $real_domain (rsa2048,rsa4096,ed25519) using $qmail_dkim with selector from dns"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for real domain $real_domain (rsa2048,rsa4096,ed25519) using $qmail_dkim with selector from dns"
	fi

	# Test No 5
	printf "\r%126s\r" " "
	echo "testing dkim with different key types (rsa2048,rsa4096,ed25519) signing+verification by dkim"
	failed=0
	for i in dkimkeys1 default ed25519
	do
		if [ $verbose -gt 0 ] ; then
			echo testing key type $i
		fi
		if [ "$i" = "ed25519" ] ; then
			enc=4
		else
			enc=2
		fi
		case $i in
			dkimkeys1)
			echo "    testing rsa2048"
			;;
			default)
			echo "    testing rsa4096"
			;;
			ed25519)
			echo "    testing ed25519"
			;;
		esac
		for j in example.com example.org
		do
			printf "\r%126s\r" " "
			selector=$(cat $domainkey_dir/$j/$i.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
			if [ $verbose -gt 0 ] ; then
				echo "($dkim -z $enc -s $domainkey_dir/$j/$i <$tmpdir/mail.txt;cat $tmpdir/mail.txt) | $dkim -v -T $selector"
			fi
			(
			echo "From: postmaster@$j"
			echo "To: postmaster@$j"
			echo "Subject: Test"
			echo "Date: $(date -R)"
			echo
			echo "Test message"
			) > $tmpdir/mail.txt
			(cat $tmpdir/mail.txt | $dkim -z $enc -s $domainkey_dir/$j/$i; cat $tmpdir/mail.txt) | $dkim -v -T "$selector" > $out
			ret=$?
			/bin/rm -f $tmpdir/mail.txt
			if [ $ret -eq 0 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret]  $selector"
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				echo "FAILED : [ret=$ret] domain=$j key=$domainkey_dir/$j/$i $selector"
				echo
				echo FAILURE [$ret] ======================== >$out
				echo >>$out
				failed=1
			fi
		done
		printf "\r%126s\r" " "
	done

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for $dkim with different key types (rsa,ed25519)"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for $dkim with different key types (rsa,ed25519)"
	fi

	# Test No 6
	printf "\r%126s\r" " "
	echo "testing multi-signature signing+verification by dkim"
	failed=0
	for i in example.com example.org
	do
		key1=$domainkey_dir/$i/default
		key2=$domainkey_dir/$i/ed25519
		(
		echo "From: postmaster@$i"
		echo "To: postmaster@$i"
		echo "Subject: Test"
		echo "Date: $(date -R)"
		echo
		echo "Test message"
		) > $tmpdir/mail.txt
		(cat $tmpdir/mail.txt | $dkim -z 2 -s $key1 -z 4 -s $key2; cat $tmpdir/mail.txt) > $tmpdir/result.out
		/bin/rm -f $tmpdir/mail.txt
		for j in default ed25519
		do
			printf "\r%126s\r" " "
			if [ $verbose -gt 0 ] ; then
				echo key $j domain $i
			fi
			selector=$(cat $domainkey_dir/$i/$j.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
			case $j in
				default)
				echo "  rsa+ed25519 with rsa-256 selector"
				;;
				ed25519)
				echo "  rsa+ed25519 with ed25519 selector"
				;;
			esac
			sig_count=$(grep DKIM-Signature $tmpdir/result.out|wc -l)
			$dkim -vV -T "$selector" > $out 2>$tmpdir/err.out < $tmpdir/result.out
			ret=$?
			grep Signature $tmpdir/err.out | sed -e 's/Signature #/    Signature/g' \
				-e 's/01/rsa-256/' -e 's/02/ed25519/' -e 's/Failure.*/Failure - Correct/'
			if [ $ret -eq 0 -a $sig_count -eq 2 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret]  dkim -s $key1 -s $key2..."
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				echo "FAILED : [ret=$ret] domain=$i sig_count=$sig_count dkim -s $key1 -s $key2..."
				echo
				echo FAILURE [$ret] ======================== >$out
				echo >>$out
				failed=1
			fi
		done
		/bin/rm -f $tmpdir/result.out $tmpdir/err.out
		printf "\r%126s\r" " "
	done

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for  multi-signature signing+verification by $dkim"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for  multi-signature signing+verification by $dkim"
	fi

	# Test No 7
	printf "\r%126s\r" " "
	echo "testing multi-signature signing by qmail-dkim + verification by dkim DKIMSIGNEXTRA in env"
	failed=0
	key1=$domainkey_dir/%/default
	key2=$domainkey_dir/%/ed25519
	for i in example.com example.org
	do
		(
		echo "From: postmaster@$i"
		echo "To: postmaster@$i"
		echo "Subject: Test"
		echo "Date: $(date -R)"
		echo
		echo "Test message"
		) | env \
			CONTROLDIR=$cntrldir \
			DKIMSIGN="$key1" \
			DKIMSIGNOPTIONS="-z 2" \
			DKIMQUEUE=/bin/cat \
			DKIMSIGNEXTRA="$key2" \
			DKIMSIGNOPTIONSEXTRA="-z 4" \
			ERR_FD=2 \
		$qmail_dkim > $tmpdir/result.out
		sig_count=$(grep DKIM-Signature $tmpdir/result.out|wc -l)
		for j in default ed25519
		do
			printf "\r%126s\r" " "
			if [ $verbose -gt 0 ] ; then
				echo key $j domain $i
			fi
			selector=$(cat $domainkey_dir/$i/$j.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
			case $j in
				default)
				echo "  rsa+ed25519 with rsa-256 selector"
				;;
				ed25519)
				echo "  rsa+ed25519 with ed25519 selector"
				;;
			esac
			$dkim -vV -T "$selector" >$out 2>$tmpdir/err.out < $tmpdir/result.out
			ret=$?
			grep Signature $tmpdir/err.out | sed -e 's/Signature #/    Signature/g' \
				-e 's/01/ed25519/' -e 's/02/rsa-256/' -e 's/Failure.*/Failure - Correct/'
			if [ $ret -eq 0 -a $sig_count -eq 2 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret] domain=$i sig_count=$sig_count $qmail_dkim"
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				echo "FAILED : [ret=$ret] domain=$i sig_count=$sig_count $qmail_dkim"
				echo
				echo FAILURE [$ret] ======================== >$out
				echo >>$out
				failed=1
			fi
		done
		/bin/rm -f $tmpdir/result.out $tmpdir/err.out
		printf "\r%126s\r" " "
	done

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for  multi-signature signing by $qmail_dkim + verification by $dkim DKIMSIGNEXTRA in env"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for  multi-signature signing by $qmail_dkim + verification by $dkim DKIMSIGNEXTRA in env"
	fi

	# Test No 8
	printf "\r%126s\r" " "
	echo "testing multi-signature signing by qmail-dkim + verification by dkim DKIMSIGNEXTRA in dkimkeys"
	(
	echo "ex.*.org$:$domainkey_dir/%/dkimkeys1:QREGEX=1,DKIMSIGNEXTRA=$domainkey_dir/%/dkimkeys2,DKIMSIGNOPTIONS=-z 4"
	echo "ex*.com:$domainkey_dir/%/dkimkeys1:QREGEX=0,DKIMSIGNEXTRA=$domainkey_dir/%/dkimkeys2,DKIMSIGNOPTIONS=-z 4"
	) >$cntrldir/dkimkeys
	failed=0
	for i in example.com example.org
	do
		(
		echo "From: postmaster@$i"
		echo "To: postmaster@$i"
		echo "Subject: Test"
		echo "Date: $(date -R)"
		echo
		echo "Test message"
		) | env \
		CONTROLDIR=$cntrldir \
		DKIMSIGN="" \
		DKIMSIGNOPTIONS="-z 2" \
		DKIMQUEUE=/bin/cat \
		ERR_FD=2 \
		$qmail_dkim > $tmpdir/result.out
		sig_count=$(grep DKIM-Signature $tmpdir/result.out|wc -l)
		for j in dkimkeys1 dkimkeys2
		do
			printf "\r%126s\r" " "
			if [ $verbose -gt 0 ] ; then
				echo key $j domain $i
			fi
			selector=$(cat $domainkey_dir/$i/$j.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
			case $j in
				dkimkeys1)
				echo "  rsa+ed25519 with rsa-256 selector"
				;;
				dkimkeys2)
				echo "  rsa+ed25519 with ed25519 selector"
				;;
			esac
			$dkim -vV -T "$selector" >$out 2>$tmpdir/err.out < $tmpdir/result.out
			ret=$?
			grep Signature $tmpdir/err.out | sed -e 's/Signature #/    Signature/g' \
				-e 's/01/ed25519/' -e 's/02/rsa-256/' -e 's/Failure.*/Failure - Correct/'
			if [ $ret -eq 0 -a $sig_count -eq 2 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret] domain=$i sig_count=$sig_count dkimkeys $qmail_dkim"
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				echo "FAILED1 : [ret=$ret] domain=$i sig_count=$sig_count dkimkeys $qmail_dkim"
				echo
				echo FAILURE [$ret] ======================== >$out
				echo >>$out
				failed=1
			fi
		done
		/bin/rm -f $tmpdir/result.out $tmpdir/err.out
		printf "\r%126s\r" " "
	done
	/bin/rm -f $cntrldir/dkimkeys

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for  multi-signature signing by $qmail_dkim + verification by $dkim DKIMSIGNEXTRA in dkimkeys"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for  multi-signature signing by $qmail_dkim + verification by $dkim DKIMSIGNEXTRA in dkimkeys"
	fi

	# Test No 9
	printf "\r%126s\r" " "
	echo "testing multi-signature signing+verification by qmail-dkim"
	failed=0
	key1=$domainkey_dir/%/default
	key2=$domainkey_dir/%/ed25519
	for i in example.com example.org
	do
		(
		echo "From: postmaster@$i"
		echo "To: postmaster@$i"
		echo "Subject: Test"
		echo "Date: $(date -R)"
		echo
		echo "Test message"
		) | env \
			CONTROLDIR=$cntrldir \
			DKIMSIGN="$key1" \
			DKIMSIGNOPTIONS="-z 2" \
			DKIMQUEUE=/bin/cat \
			DKIMSIGNEXTRA="$key2" \
			DKIMSIGNOPTIONSEXTRA="-z 4" \
			ERR_FD=2 \
			$qmail_dkim > $tmpdir/result.out
		sig_count=$(grep DKIM-Signature $tmpdir/result.out|wc -l)
		for j in default ed25519
		do
			printf "\r%126s\r" " "
			if [ $verbose -gt 0 ] ; then
				echo key $j domain $i
			fi
			selector=$(cat $domainkey_dir/$i/$j.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
			case $j in
				default)
				echo "  rsa+ed25519 with rsa-256 selector"
				;;
				ed25519)
				echo "  rsa+ed25519 with ed25519 selector"
				;;
			esac
			cat $tmpdir/result.out | env - \
					DKIMVERIFY="" \
					SELECTOR_DATA="$selector" \
					DKIMQUEUE=/bin/cat \
					VERBOSE=1 \
					$qmail_dkim >$out 2>$tmpdir/err.out
			ret=$?
			grep Signature $tmpdir/err.out | sed -e 's/Signature #/    Signature/g' \
				-e 's/01/ed25519/' -e 's/02/rsa-256/' -e 's/Failure.*/Failure - Correct/'
			if [ $ret -eq 0 -a $sig_count -eq 2 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret] domain=$i sig_count=$sig_count env - DKIMSIGN=$key1 DKIMSIGNEXTRA=$key2... $qmail_dkim"
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				echo "FAILED : [ret=$ret] domain=$i sig_count=$sig_count env - DKIMSIGN=$key1 DKIMSIGNEXTRA=$key2... $qmail_dkim"
				echo
				echo FAILURE [$ret] ======================== >$out
				echo >>$out
				failed=1
			fi
		done
		printf "\r%126s\r" " "
	done

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for  multi-signature signing+verification by $qmail_dkim"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for  multi-signature signing+verification by $qmail_dkim"
	fi

	# Test No 10
	printf "\r%126s\r" " "
	echo "testing qmail-dkim signing+verification by qmail-dkim"
	failed=0
	for i in $domainkey_dir/test $domainkey_dir/%/default $domainkey_dir/%/test $domainkey_dir/%/ed25519
	do
		for j in example.com example.org
		do
			printf "\r%126s\r" " "
			selector=""
			if [ "$i" = "$domainkey_dir/test" -a -f $i.pub ] ; then
				selector=$(cat $i.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
			fi
			if [ -z "$selector" ] ; then
				case $i in
				$domainkey_dir/%/default)
				enc="-z 2"
				if [ -f $domainkey_dir/$j/default.pub ] ; then
					selector=$(cat $domainkey_dir/$j/default.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
				elif [ -f $domainkey_dir/default.pub ] ; then
					selector=$(cat $domainkey_dir/default.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
				fi
				;;
				$domainkey_dir/%/test)
				enc="-z 2"
				if [ -f $domainkey_dir/$j/test.pub ] ; then
					selector=$(cat $domainkey_dir/$j/test.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
				elif [ -f $domainkey_dir/test.pub ] ; then
					selector=$(cat $domainkey_dir/test.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
				fi
				;;
				$domainkey_dir/%/ed25519)
				enc="-z 4"
				if [ -f $domainkey_dir/$j/ed25519.pub ] ; then
					selector=$(cat $domainkey_dir/$j/ed25519.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
				elif [ -f $domainkey_dir/ed25519.pub ] ; then
					selector=$(cat $domainkey_dir/ed25519.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
				fi
				;;
				esac
			fi
			if [ $verbose -gt 0 ] ; then
				echo key=$i domain=$j
			fi
			(
			echo "From: postmaster@$j"
			echo "To: postmaster@$j"
			echo "Subject: Test"
			echo "Date: $(date -R)"
			echo
			echo "Test message"
			) | env - \
				CONTROLDIR=$cntrldir \
				DKIMSIGN="$i" \
				DKIMSIGNOPTIONS="$enc" \
				DKIMQUEUE=/bin/cat \
				$qmail_dkim | env - \
					DKIMVERIFY="" \
					SELECTOR_DATA="$selector" \
					DKIMQUEUE=/bin/cat \
					$qmail_dkim > $out
			ret=$?
			if [ $ret -eq 0 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret]  env - DKIMSIGN=$i DKIMQUEUE=/bin/cat $qmail_dkim"
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				echo "FAILED : [ret=$ret] domain=$j key=$i env - DKIMSIGN=$i DKIMQUEUE=/bin/cat $qmail_dkim"
				echo
				echo FAILURE [$ret] domain=$j ======================== >$out
				echo >>$out
				failed=1
			fi
		done
		printf "\r%126s\r" " "
	done

	for i in $domainkey_dir/%/notexist $domainkey_dir/%
	do
		for j in example.com example.org
		do
			printf "\r%126s\r" " "
			if [ $verbose -gt 0 ] ; then
				echo key=$i domain=$j
			fi
			(
			echo "From: postmaster@$j"
			echo "To: postmaster@$j"
			echo "Subject: Test"
			echo "Date: $(date -R)"
			echo
			echo "Test message"
			) | env - \
				CONTROLDIR=$cntrldir \
				DKIMSIGN="$i" \
				DKIMQUEUE=/bin/cat \
				$qmail_dkim >$out
			ret=$?
			if [ $ret -eq 0 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret]  env - DKIMSIGN=$i DKIMQUEUE=/bin/cat $qmail_dkim"
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				echo "FAILED : [ret=$ret] domain=$j key=$i env - DKIMSIGN=$i DKIMQUEUE=/bin/cat $qmail_dkim"
				echo
				echo FAILURE [$ret] domain=$j ======================== >$out
				echo >>$out
				failed=1
			fi
		done
		printf "\r%126s\r" " "
	done

	for i in $domainkey_dir/notexist
	do
		for j in example.com example.org
		do
			printf "\r%126s\r" " "
			if [ $verbose -gt 0 ] ; then
				echo key $i domain $j
			fi
			(
			echo "From: postmaster@$j"
			echo "To: postmaster@$j"
			echo "Subject: Test"
			echo "Date: $(date -R)"
			echo
			echo "Test message"
			) | env - \
				CONTROLDIR=$cntrldir \
				DKIMSIGN="$i" \
				DKIMQUEUE=/bin/cat \
				ERROR_FD=2 \
				$qmail_dkim >$out 2>&1
			ret=$?
			if [ $ret -eq 0 ] ; then
				echo "FAILED : [ret=$ret] domain=$j key=$i env - DKIMSIGN=$i DKIMQUEUE=/bin/cat $qmail_dkim"
				echo
				echo FAILURE [$ret] domain=$j ======================== > $out
				echo >>$out
				failed=1
			else
				if [ $ret -ne $nokeyerror -a $ret -ne 88 ] ; then
					echo "FAILED : [ret=$ret] domain=$j key=$i env - DKIMSIGN=$i DKIMQUEUE=/bin/cat $dkim"
					echo
					echo FAILURE [$ret] domain=$j ======================== > $out
					echo >>$out
					failed=1
				else
					tcount=$(expr $tcount + 1)
					if [ $verbose -gt 0 ] ; then
						echo "SUCCESS: [ret=$ret] env - DKIMSIGN=$i DKIMQUEUE=/bin/cat $dkim"
					fi
					echo SUCCESS ======================== >$out
					print_pct
				fi
			fi
		done
		printf "\r%126s\r" " "
	done

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test failed for $qmail_dkim signing+verification by $qmail_dkim"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for $qmail_dkim signing+verification by $qmail_dkim"
	fi

	# Test No 11
	printf "\r%126s\r" " "
	echo "testing dkimkeys control file feature"
	failed=0
	if [ ! -d $cntrldir ] ; then
		mkdir -p $cntrldir
	fi

	(
	echo "ex*.com:$domainkey_dir/%/dkimkeys1"
	echo "ex.*.org:$domainkey_dir/%/dkimkeys2:QREGEX=1,DKIMSIGNOPTIONS=-z 4"
	) >$cntrldir/dkimkeys
	for i in example.com example.org
	do
		printf "\r%126s\r" " "
		if [ $verbose -gt 0 ] ; then
			echo domain $i
		fi
		if [ "$i" = "example.com" ] ; then
			selector=$(cat $domainkey_dir/$i/dkimkeys1.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
		else
			selector=$(cat $domainkey_dir/$i/dkimkeys2.pub |sed -e 's/.*(//' -e 's/).*//' -e '/^$/d' -e 's/"//g' -e 's/\t//g' | tr -d '\n')
		fi
		(
		echo "From: postmaster@$i"
		echo "To: postmaster@$i"
		echo "Subject: Test"
		echo "Date: $(date -R)"
		echo
		echo "Test message"
		) | env -  \
			DKIMSIGN="" \
			CONTROLDIR=$cntrldir \
			DKIMQUEUE=/bin/cat \
			ERROR_FD=2 \
			$qmail_dkim | $dkim -v -T "$selector" > $out
		ret=$?
		if [ $ret -eq 0 ] ; then
			tcount=$(expr $tcount + 1)
			if [ $verbose -gt 0 ] ; then
				echo "SUCCESS: [ret=$ret]  dkimkeys"
				echo
			fi
			echo SUCCESS ======================== >$out
			print_pct
		else
			echo "FAILED : [ret=$ret] domain=$i dkimkeys"
			echo
			echo FAILURE [$ret] domain=$i ======================== >$out
			echo >>$out
			failed=1
		fi
	done

	(
	echo "ex*.com:$domainkey_dir/notexists"
	echo "ex*.org:$domainkey_dir/notexists:QREGEX=1"
	) >$cntrldir/dkimkeys
	for i in example.com example.org
	do
		printf "\r%126s\r" " "
		if [ $verbose -gt 0 ] ; then
			echo domain $i
		fi
		(
		echo "From: postmaster@$i"
		echo "To: postmaster@$i"
		echo "Subject: Test"
		echo "Date: $(date -R)"
		echo
		echo "Test message"
		) | env -  \
			DKIMSIGN="" \
			CONTROLDIR=$cntrldir \
			DKIMQUEUE=/bin/cat \
			ERROR_FD=2 \
			$qmail_dkim > $out 2>&1
		ret=$?
		if [ $ret -eq 0 ] ; then
			echo "FAILED : [ret=$ret] domain=$i key=dkimkeys"
			echo
			echo FAILURE [$ret] domain=$j ======================== > $out
			echo >>$out
			failed=1
		else
			if [ $ret -ne $nokeyerror -a $ret -ne 88 ] ; then
				echo "FAILED : [ret=$ret] domain=$i key=dkimkeys"
				echo
				echo FAILURE [$ret] domain=$j ======================== > $out
				echo >>$out
				failed=1
			else
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret] dkimkeys"
				fi
				echo SUCCESS ======================== >$out
				print_pct
			fi
		fi
	done

	if [ $verbose -eq 0 -a $failed -eq 0 ] ; then
		printf "\r  Success%118s\n" " "
		echo
	fi
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for dkimkeys"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "All Tests succeeded for dkimkeys"
	fi

	# Test No 12
	key1=$domainkey_dir/example.com/dkimkeys1
	qmail_inject=$bindir/qmail-inject
	qmail_queue=$sbindir/qmail-queue
	printf "\r%126s\r" " "
	echo "testing qmail-dkim custom error feature for permanent(100)/temporary(111) failures"
	(
		echo "From: postmaster@$real_domain"
		echo "To: $user@$real_domain"
		echo "Subject: Test"
		echo "Date: $(date -R)"
		echo
		echo "Test message"
	) > $tmpdir/mail.txt
	for i in PQ pq
	do
		printf "\r%126s\r" " "
		echo "  testing return value  for DKIMVERIFY=$i"
		(cat $tmpdir/mail.txt | $bindir/dkim -z 2 -s $key1; cat $tmpdir/mail.txt) | env - \
			QUEUEDIR=/var/indimail/queue/queue1 \
			CONFSPLIT=23 \
			BIGTODO=0 \
			DKIMVERIFY="$i" \
			QMAILQUEUE=$qmail_dkim \
			FASTQUEUE=1 \
			ERROR_FD=4 \
			$qmail_inject -a $user 2>$tmpdir/out.$$
		ret=$?
		echo "    return value=$ret"
		case $i in
			PQ)
			if [ $ret -eq 100 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret] DKIMVERIFY=$i qmail-dkim custom error"
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				failed=1
				echo "FAILED : [ret=$ret] DKIMVERIFY=$i qmail-dkim custom error"
				echo
				echo FAILURE [$ret] ======================== >$out
				echo >>$out
			fi
			printf "\r%126s\r" " "
			echo "  testing error message for DKIMVERIFY=$i"
			echo "    error message=$(cat $tmpdir/out.$$)"
			grep "qmail-inject: fatal: qmail-dkim: signature error: permanent dns failure requesting selector (#5.7.0)" $tmpdir/out.$$ > /dev/null
			if [ $? -eq 0 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret] DKIMVERIFY=$i qmail-dkim custom error message"
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				failed=1
				echo "FAILED : [ret=$ret] DKIMVERIFY=$i qmail-dkim custom error message"
				echo
				echo FAILURE [$ret] ======================== >$out
				echo >>$out
			fi
			;;
			pq)
			if [ $ret -eq 111 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret] DKIMVERIFY=$i qmail-dkim custom error"
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				failed=1
				echo "FAILED : [ret=$ret] DKIMVERIFY=$i qmail-dkim custom error"
				echo
				echo FAILURE [$ret] ======================== >$out
				echo >>$out
			fi
			printf "\r%126s\r" " "
			echo "  testing error message for DKIMVERIFY=$i"
			echo "    error message=$(cat $tmpdir/out.$$)"
			grep "qmail-inject: fatal: qmail-dkim: signature error: permanent dns failure requesting selector (#4.7.0)" $tmpdir/out.$$ > /dev/null
			if [ $? -eq 0 ] ; then
				tcount=$(expr $tcount + 1)
				if [ $verbose -gt 0 ] ; then
					echo "SUCCESS: [ret=$ret] DKIMVERIFY=$i qmail-dkim custom error message"
					echo
				fi
				echo SUCCESS ======================== >$out
				print_pct
			else
				failed=1
				echo "FAILED : [ret=$ret] DKIMVERIFY=$i qmail-dkim custom error message"
				echo
				echo FAILURE [$ret] ======================== >$out
				echo >>$out
			fi
			;;
		esac
	done
	/bin/rm -f $tmpdir/mail.txt $tmpdir/out.$$
	if [ $failed -eq 1 ] ; then
		echo "Test Failed for qmail-dkim custom error"
		exit 1
	elif [ $verbose -ne 0 ] ; then
		echo "Test succeeded for qmail-dkim custom error"
	fi
	t2=$(date +"%s.%4N")
	secs=$(echo $t1 $t2 | awk '{printf("%0.4f\n", $2-$1)}')
	printf "\r%126s\r" " "
	echo "DKIM tests [$secs sec]"
}

do_cleanup()
{
sudo /bin/rm -rf $servicedir
sudo /bin/rm -rf $logdir
sudo /bin/rm -rf $testdir/queue
sudo /bin/rm -rf $testdir/sleep.out
sudo /bin/rm -rf $testdir/svscan.out
sudo /bin/rm -rf $testdir/tcpclient.smtp
sudo /bin/rm -rf $qmaildir/domains
sudo /bin/rm -rf $sysconfdir/users
if [ -d $maildir ] ; then
	find $maildir -type f -exec /bin/rm -f {} \;
fi
if [ -d $testdir/$testuser/Maildir ] ; then
	find $testdir/$testuser/Maildir -type f -exec /bin/rm -f {} \;
fi
if [ -d $qmaildir/alias/Maildir ] ; then
	sudo find $qmaildir/alias/Maildir -type f -exec /bin/rm -f {} \;
fi
}

usage()
{
	echo "test-indimail-mta [-h|--help]" 1>&2
}

display_tests()
{
	echo "test svscan startup without svscan log"
	echo "test service startup (qmail-smtpd, qmail-send)"
	echo "test qmail-inject"
	echo "test alias mechanism"
	echo "test SMTPS using swaks"
	echo "test recipient extension"
	echo "test autoresponder"
	echo "test qmail-qfilter"
	echo "test spawn-filter QMAILLOCAL and QMAILREMOTE"
	echo "test CHECKRECIPIENT feature"
	echo "test CHECKSENDER feature"
	echo "test Sender Rewriting Scheme for qmail-smtpd, qmail-inject, autoresponder, srsfilter"
	echo "test stopping of service using svc"
	echo "test startup of new service by sending HUP to svscan"
	echo "test restart of service by using svc -r"
	echo "test starting of service by using svc -u"
	echo "test stopping of service by using svc -d"
	echo "test multilog"
	echo "test svscan startup with svscan log"
	echo "test svscan execution of run /service/.svscan/run"
	echo "test svscan start services configured in /service"
	echo "test SMTPS service and mail submission using swaks"
	echo "test domainqueue"
	echo "test authdomains"
	echo "test SMTP accesslist"
	echo "test SMTP surbl"
	echo "test SMTP QQEH"
	echo "test svscan autoscan"
	echo "test svscan subreaper feature"
	echo "test blocking (badmailfrom, badrcptto)"
	echo "test blocking (badmailfrompatterns, badrcptpatterns)"
	echo "test unblocking (goodrcpt, goodrcptpatterns)"
	echo "test blachole (blackholedrcpt, blackholedsender)"
	echo "test SPF"
	echo "test DKIM using dkim, qmail-dkim"
}

failed=0
options=$(getopt -a -n test-indimail-mta -o "h" -l help -- "$@")
if [ $? != 0 ]; then
  usage
  exit 1
fi
eval set -- "$options"
while :
do
	case "$1" in
		-h | --help)
		usage
		display_tests
  		exit 1
		;;
		--) # end of options
		shift
		break
		;;
		*)
		echo "Unexpected option: $1 - this should'nt happen." 1>&2
		usage
  		exit 1
		;;
	esac
done
# end getopt

sudo ls /tmp > /dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo "This script requires sudo privileges" 1>&2
	exit 1
fi
if [ -f $testdir/qtotal.count ] ; then
	total_tests=$(cat $testdir/qtotal.count)
else
	total_tests=327
fi
failed=0
do_setup
log_type=1
do_without_svscan
log_type=2
do_svscan_without_svscanlog
shutdown_svscan
do_svscan_with_svscanlog
shutdown_svscan

wait

printf "\r%118s\n" " "
echo "Starting ezmlm-idx tests"
test_ezmlm_idx
if [ ! -f /etc/debian_version -a -f ./spfquery.txt ] ; then
	printf "\r%118s\n" " "
	test_spf
fi
secs=0.0
printf "\r%118s\n" " "
for i in QMAILQUEUE envdir tcpclient-tcp tcpclient-ssl tcpclient-unix tcpserver-ssl \
	maildirdeliver bounce 822header 822body qmail-getpw nodnscheck rd-remote \
	maildirsmtp fetchmail
do
	tcount=$(expr $tcount + 1)
	printf "\r  implicit test %20s succeeded %57s [%.4f sec]\n" $i " " $secs
	print_pct
done
printf "\r%118s\n" " "
[ -z "$send_pid" ] && send_pid=$(get_send_pid qmail-send $testdir/queue)
terminate_send qmail-send "$send_pid"
send_pid=""
non_dkim_tcount=$tcount
if [ -f $dkim_key ] ; then
	test_dkim
fi

# Cleanup
do_cleanup

echo
dkim_count=$(expr $tcount - $non_dkim_tcount)
echo "All $tcount tests succeeded (indimail-mta tests=$non_dkim_tcount + dkim tests=$dkim_count)"
end=$(date +'%s')
secs=$(($end-$start))
printf '%d days, %d hr, %d min, %d sec\n' $((secs/86400)) $((secs%86400/3600)) $((secs%3600/60)) \
	  $((secs%60))
echo $tcount > $testdir/qtotal.count
exit 0

#
# $Log: test-indimail-mta,v $
# Revision 1.83  2025-02-02 16:41:13+05:30  Cprogrammer
# fixed indentation at few places
#
# Revision 1.82  2024-12-12 21:24:18+05:30  Cprogrammer
# updated test script for svscan log
#
# Revision 1.81  2024-07-17 19:55:27+05:30  Cprogrammer
# added test for CRAM-SHA1, DIGEST-MD5 using swaks
#
# Revision 1.80  2024-07-16 00:19:03+05:30  Cprogrammer
# added test for CRAM, XOAUTH2 auth methods
#
# Revision 1.79  2024-05-21 20:59:25+05:30  Cprogrammer
# fixed display of time taken
#
# Revision 1.78  2024-05-21 08:51:26+05:30  Cprogrammer
# use sleep_start when restarting service
#
# Revision 1.77  2024-05-12 21:22:10+05:30  Cprogrammer
# create env variable PENALTY
#
# Revision 1.76  2024-05-12 00:20:03+05:30  mbhangui
# fix function prototypes
#
# Revision 1.75  2024-04-30 08:19:52+05:30  Cprogrammer
# updated cleanup
#
# Revision 1.74  2024-03-01 16:40:03+05:30  Cprogrammer
# minor change in formatting of success message
#
# Revision 1.73  2024-02-23 21:42:56+05:30  Cprogrammer
# added tests for goodrcpt, goodrcptpatterns
#
# Revision 1.72  2024-02-23 01:31:31+05:30  Cprogrammer
# added tests for badmailpatterns, badrcptpatterns
#
# Revision 1.71  2024-02-21 13:16:34+05:30  Cprogrammer
# added qfrontend tests
#
# Revision 1.70  2024-02-20 01:39:50+05:30  Cprogrammer
# added additional tests for FILTERARGS, qmail-qfilter
#
# Revision 1.69  2024-02-14 23:13:23+05:30  Cprogrammer
# added tests for spam filtering by spawn-filter
#
# Revision 1.68  2024-02-14 22:09:31+05:30  Cprogrammer
# removed unused code
#
# Revision 1.67  2024-02-14 20:45:48+05:30  Cprogrammer
# added test for ezmlm-unsub
#
# Revision 1.66  2024-02-14 12:49:02+05:30  Cprogrammer
# added ezmlm-idx tests
#
# Revision 1.65  2024-02-09 15:53:31+05:30  Cprogrammer
# added tests for envheader, filterargs without local/remote directive
#
# Revision 1.64  2024-02-07 22:55:53+05:30  Cprogrammer
# added additional spawn-filter tests
#
# Revision 1.63  2024-01-20 23:43:08+05:30  Cprogrammer
# fixes for debian
#
# Revision 1.62  2024-01-11 12:38:16+05:30  Cprogrammer
# updated test script
#
# Revision 1.61  2024-01-09 19:52:56+05:30  Cprogrammer
# removed dk-filter
#
# Revision 1.60  2023-12-26 00:33:22+05:30  Cprogrammer
# display mail if dkim verification fails
#
# Revision 1.59  2023-12-17 21:36:23+05:30  Cprogrammer
# added tests for qmta-send
#
# Revision 1.58  2023-12-16 22:21:07+05:30  Cprogrammer
# updated test script
#
# Revision 1.57  2023-12-16 12:31:15+05:30  Cprogrammer
# added test for qmta-send
#
# Revision 1.56  2023-12-15 23:06:18+05:30  Cprogrammer
# added tests for slowq-send
#
# Revision 1.55  2023-12-11 18:49:43+05:30  Cprogrammer
# added test for qmail-inject domainqueue
#
# Revision 1.54  2023-12-11 00:17:11+05:30  Cprogrammer
# remove libindimail control file
#
# Revision 1.53  2023-12-10 22:18:13+05:30  Cprogrammer
# added test for AUTOTURN
#
# Revision 1.52  2023-12-09 23:52:06+05:30  Cprogrammer
# added tests for ETRN and ATRN
#
# Revision 1.51  2023-12-03 22:21:31+05:30  Cprogrammer
# updated virtual domain setup
#
# Revision 1.50  2023-12-01 00:42:40+05:30  Cprogrammer
# updated test script
#
# Revision 1.49  2023-11-13 10:38:46+05:30  Cprogrammer
# added test for nodnscheck
#
# Revision 1.48  2023-11-05 06:45:53+05:30  Cprogrammer
# updated test script
#
# Revision 1.47  2023-11-03 05:25:26+05:30  Cprogrammer
# added badhelo test
#
# Revision 1.46  2023-11-02 06:31:01+05:30  Cprogrammer
# fixed QMAILREMOTE
#
# Revision 1.45  2023-11-01 21:59:39+05:30  Cprogrammer
# added test for domainqueue
#
# Revision 1.44  2023-10-30 21:10:56+05:30  Cprogrammer
# added test for smtp access list
#
# Revision 1.43  2023-10-30 16:22:19+05:30  Cprogrammer
# use tmpdir for all temporary files
#
# Revision 1.42  2023-10-30 01:27:19+05:30  Cprogrammer
# added test to use wildmat instead of regexp for mailarchival
#
# Revision 1.41  2023-10-29 18:28:20+05:30  Cprogrammer
# added test case to test invalid regex expression
#
# Revision 1.40  2023-10-28 20:46:14+05:30  Cprogrammer
# added qmail-multi test
#
# Revision 1.39  2023-10-28 07:49:28+05:30  Cprogrammer
# fixed test_smtp_surbl
#
# Revision 1.38  2023-10-26 23:43:05+05:30  Cprogrammer
# added spamfilter tests
#
# Revision 1.37  2023-10-26 11:43:33+05:30  Cprogrammer
# added test for QQEH
#
# Revision 1.36  2023-10-26 00:02:38+05:30  Cprogrammer
# added qmail-queue feature tests
#
# Revision 1.35  2023-10-16 20:55:17+05:30  Cprogrammer
# added test for authdomains, AUTH_ALL env variable setting
#
# Revision 1.34  2023-10-16 18:38:12+05:30  Cprogrammer
# added test for SECURE AUTH and REQUIREAUTH
#
# Revision 1.33  2023-10-11 21:02:53+05:30  Cprogrammer
# minor modification
#
# Revision 1.32  2023-10-11 20:39:20+05:30  Cprogrammer
# minor fixes
#
# Revision 1.31  2023-10-11 19:17:35+05:30  Cprogrammer
# added test progress
#
# Revision 1.30  2023-10-11 14:55:19+05:30  Cprogrammer
# added test for MASQUERADE
#
# Revision 1.29  2023-10-07 08:43:39+05:30  Cprogrammer
# reduce test time
#
# Revision 1.28  2023-10-07 01:29:06+05:30  Cprogrammer
# added test for hiding IP, host in received headers
#
# Revision 1.27  2023-10-06 02:23:28+05:30  Cprogrammer
# updated test script
#
# Revision 1.26  2023-10-05 23:38:27+05:30  Cprogrammer
# added tests for maildirserial, serialsmtp, serialqmtp
#
# Revision 1.25  2023-10-04 21:01:42+05:30  Cprogrammer
# added test for invalid auth type
#
# Revision 1.24  2023-10-04 18:39:31+05:30  Cprogrammer
# added test for QMQP, QMTP
#
# Revision 1.23  2023-10-04 09:31:42+05:30  Cprogrammer
# updated test_blocked script
#
# Revision 1.22  2023-10-04 09:09:18+05:30  Cprogrammer
# updated test script
#
# Revision 1.21  2023-10-02 22:49:08+05:30  Cprogrammer
# added alias tests
#
# Revision 1.20  2023-10-02 21:24:03+05:30  Cprogrammer
# added SRS tests
#
# Revision 1.19  2023-09-30 13:38:16+05:30  Cprogrammer
# display timings for setup
#
# Revision 1.18  2023-09-29 23:39:53+05:30  Cprogrammer
# display test time at fixed column
#
# Revision 1.17  2023-09-29 13:55:32+05:30  Cprogrammer
# round decimal to 4 digits for times
#
# Revision 1.16  2023-09-29 08:50:11+05:30  Cprogrammer
# round secs to 3 decimal places
#
# Revision 1.15  2023-09-28 21:13:11+05:30  Cprogrammer
# fixed test times
#
# Revision 1.14  2023-09-28 13:13:49+05:30  Cprogrammer
# display time taken in secs.milliseconds
#
# Revision 1.13  2023-09-28 01:11:06+05:30  Cprogrammer
# display time taken for tests
#
# Revision 1.12  2023-09-27 18:43:30+05:30  Cprogrammer
# updated CHECKSENDER, SURB tests
#
# Revision 1.11  2023-09-27 01:52:08+05:30  Cprogrammer
# added test for CUGMAIL, CHECKSENDER, SURBL
#
# Revision 1.10  2023-09-25 18:09:35+05:30  Cprogrammer
# updated RCS
#
# Revision 1.9  2023-09-19 01:17:35+05:30  Cprogrammer
# added filterit tests
#
# Revision 1.8  2023-09-08 16:11:12+05:30  Cprogrammer
# added dkim tests
#
# Revision 1.7  2023-09-08 15:42:28+05:30  Cprogrammer
# added test for autoresponder
#
# Revision 1.6  2023-09-08 00:54:00+05:30  Cprogrammer
# added test for qmail-qfilter
#
# Revision 1.5  2023-08-26 23:18:32+05:30  Cprogrammer
# use default cipherlist, ciphersuite created by svctool
#
# Revision 1.4  2023-08-25 08:27:05+05:30  Cprogrammer
# fixed queue-fix
# removed use of hardcoded uid, gid in assign file
#
# Revision 1.3  2023-08-22 20:25:14+05:30  Cprogrammer
# added tests for TLSv1.2, TLSv1.3
#
# Revision 1.2  2023-08-04 00:09:43+05:30  Cprogrammer
# removed bashism
#
# Revision 1.1  2023-07-26 23:56:10+05:30  Cprogrammer
# Initial revision
#
#
