#!/bin/sh
# $Id: t-mts-ocr-5.sh,v 1.4 2007/01/31 02:45:13 ca Exp $
# Copyright (c) 2006 Claus Assmann
#
# By using this file, you agree to the terms and conditions set
# forth in the license/LICENSE.3C file which can be found at the
# top level of this source code distribution.
#
# ----------------------------------------
# test outgoing connection restrictions, no sink running, hence connections
# time out.
#
# uses mcpmts.sh
# ----------------------------------------
#
test -s stop && exit 1
#
if test "X"${MTA_TIMING} = "X"
then
  echo "SKIPPED: $0: MTA_TIMING is not set"
  exit 0
fi

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

SCRIPTS=`cd ${SD} && pwd`
I=""

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

# cd to right directory (where the programs will be executed).
cd ../qmgr || exit 1

# create directory to pass socket
if test -d smtps
then
  :
else
  mkdir smtps
fi

killit()
{
rm -f ${QMGRCONFMAP} ${MTMAP}
exit 1
}

# create restrictions
${CAT} > ${QMGRCONF} <<EOF
oci:127.0.0.1	2
ocm:127.0.0.1	4
octo:127.0.0.1	20
EOF
# create map
rm -f ${QMGRCONFMAP}
${MM} -t'	' -F ${QMGRCONFMAP} < ${QMGRCONF}
rm -f ${MTMAP}
trap killit 2

# ----------------------------------------
${CAT} >${SMXCNF} <<EOF
smtps { log_level = 14; pass_fd_socket = smtps/smtpsfd; wait_for_server=4;
	listen_socket { type=inet; port=${SRVPORT}; }
	start_action = pass; user = ${USER};
	path = ../smtps/smtps;
	arguments = "smtps -f ${SMXCNF} ${MTA_SERVER_OPTIONS}";
}
# NOTE: there shouldn't be a process listening on this port!
smtpc { log_level = 14; remote_port = 12345; wait_for_server=4;
	connect_only_to = 127.0.0.1;
	io_timeout=10s;
	start_action = wait; user = ${USER};
	path = "../smtpc/smtpc"; arguments = "smtpc -f ${SMXCNF}";
}
qmgr { log_level = 14; wait_for_server=4; wait_for_client=4; 
	retry_min_delay = 30;
	start_action = wait; user = ${USER};
	# restart_dependencies = { smtps,smtpc,smar, };
	path = "../qmgr/qmgr"; arguments = "qmgr -f ${SMXCNF} -d 2";
}
smar { log_level = 14;
	start_action = wait; user = ${USER};
	# restart_dependencies = { smtps,qmgr };
	path = "../smar/smar"; arguments = "smar -f ${SMXCNF}";
}
EOF

${CAT} > ${MT} <<EOF
dom1.tld	[127.0.0.1]
EOF

for i in smar smtps smtpc qmgr
do
  cp /dev/null ${i}.log
done

# ----------------------------------------
# 
TEST=1
if echo "${SKIP} X" | ${GREP} -v " ${TEST} " > /dev/null
then
ERR=false

if ${SHELL} ${SD}/mcpmts.sh -s 5 -S"sender${TEST}@dom1.tld" -R"rcpt${TEST}@dom1.tld" -A 1 -E 0 -1 -X 0
then
  if ${GREP} "occ_ipv4=127.0.0.1,.*cur_conc=1,"  qmgr.log >/dev/null
  then
    :
  else
    ${VERBOSE} && echo "$0: ${TEST}: missing cur_conc=1 in qmgr.log"
    ERR=true
  fi
else
  ERR=true
fi
if ${ERR}
then
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed"
  test X"${MTA_STOPONERROR}" != X && exit 1
else
  ${VERBOSE} && echo "test ${TEST} succeeded"
fi
fi


rm -f ${QMGRCONFMAP} f ${MTMAP}

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


syntax highlighted by Code2HTML, v. 0.9.1