#!/bin/sh
# $Id: t-mt-3.sh,v 1.10 2007/01/10 23:31:00 ca Exp $
# Copyright (c) 2005, 2006 Sendmail, Inc. and its suppliers.
#	All rights reserved.
#
# By using this file, you agree to the terms and conditions set
# forth in the LICENSE file which can be found at the top level of
# the sendmail distribution.
#
# ------------------------------------------------------------
# Description:
# mailertable routing tests by contacting SMAR, using BDB, reload
# uses t-smar-1, startsmar.sh, t-mm
# ------------------------------------------------------------
#
test -s stop && exit 1
#

if test X"`uname`" = "XLinux"
then
  echo "SKIPPED: $0: Linx: need pthread signal behavior"
  exit 0
fi

# path to programs
P=..
SD=`dirname $0`
if test "${SD}" = "."
then
   SD=../smar
fi
. ${SD}/common.sh

OUT=mt-3.out
IP=mt-3.ip
OK=mt-3.ok
TPRG=./t-smar-1
CONF=mt.conf
VERBOSE=false

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

rm -f ${SOCK} ${PIDS}

# create aliases map for smar
rm -f ${ALIMAP}
${MM} <<EOF
user:	local:
EOF
if test $? -ne 0
then
  echo t-mm failed
  exit 1
fi

if test X"${MAPTYPES}" = "X"
then
./smar -l 1 | grep ':A:L:.:F' >${MAPC}
MAPTYPES="`sed -e 's/^\([a-zA-Z][a-zA-Z0-9_]*\):A:L:.:F/\1/' < ${MAPC}`"
fi
# list map classes available for t-mm
${MM} -l 0 > ${MMMAPC}

for maptype in ${MAPTYPES}
do
if grep ${maptype} ${MMMAPC} >/dev/null
then

# prepare mailertable
${CAT} > ${MT} <<EOF
host123.meta1.org	[1.2.3.0]
meta1.org	[1.2.3.2]
.meta1.org	[1.2.3.4]
.org		[1.2.3.6]
.		[1.2.3.9]
EOF
rm -f ${MTMAPDB}

${MM} -t'	' -T ${maptype} -F ${MTMAPDB} < ${MT}
rm -f ${MT}

${CAT} > ${CONF} <<EOF
smar {
 map mt { type=${maptype}; file=${MTMAPDB}; }
 mailertable { name=mt; }
}
EOF

# start SMAR
${SHELL} ${SD}/startsmar.sh -m -f ${CONF} || exit 1

rm -f ${OUT} ${IP} ${OK}

TEST=1
# ----------------------------------------
${VALGRIND} ${TPRG} -M -VVVV '<a@host123.meta1.org>' > ${OUT} 2>&1
grep '^IP' ${OUT} | sort > ${IP}
${CAT} > ${OK} <<EOF
IP[0]=1.2.3.0 [1020300]
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi

# change mailertable
${CAT} > ${MT} <<EOF
host123.meta1.org	[9.2.3.0]
meta1.org	[9.2.3.2]
.meta1.org	[9.2.3.4]
.org		[9.2.3.6]
.		[9.2.3.9]
EOF
mv ${MTMAPDB} ${MTMAPDB}-
${MM} -t'	' -T ${maptype} -F ${MTMAPDB} < ${MT}
rm -f ${MT}

# reload SMAR
for i in `${CAT} ${PIDS}`
do
  kill -USR1 ${i}
done
sleep 1

TEST=2
# ----------------------------------------
${VALGRIND} ${TPRG} -M -VVVV '<a@host123.meta1.org>' > ${OUT} 2>&1
grep '^IP' ${OUT} | sort > ${IP}
${CAT} > ${OK} <<EOF
IP[0]=9.2.3.0 [9020300]
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi

TEST=3
# ----------------------------------------
${VALGRIND} ${TPRG} -M -VVVV '<a@host124.meta1.org>' > ${OUT} 2>&1
grep '^IP' ${OUT} | sort > ${IP}
${CAT} > ${OK} <<EOF
IP[0]=9.2.3.4 [9020304]
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi

TEST=4
# ----------------------------------------
${VALGRIND} ${TPRG} -M -VVVV '<a@meta1.org>' > ${OUT} 2>&1
grep '^IP' ${OUT} | sort > ${IP}
${CAT} > ${OK} <<EOF
IP[0]=9.2.3.2 [9020302]
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi

TEST=5
# ----------------------------------------
${VALGRIND} ${TPRG} -M -VVVV '<a@metaX.org>' > ${OUT} 2>&1
grep '^IP' ${OUT} | sort > ${IP}
${CAT} > ${OK} <<EOF
IP[0]=9.2.3.6 [9020306]
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi

TEST=6
# ----------------------------------------
${VALGRIND} ${TPRG} -M -VVVV '<a@meta1.com>' > ${OUT} 2>&1
grep '^IP' ${OUT} | sort > ${IP}
${CAT} > ${OK} <<EOF
IP[0]=9.2.3.9 [9020309]
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi

rm -f ${OUT} ${IP} ${OK}

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

else
  echo "$0: ${maptype} not available for ${MM}"
fi
done

exit ${ERRS}


syntax highlighted by Code2HTML, v. 0.9.1