#!/bin/sh
# $Id: t-smar-rvrs-0.sh,v 1.5 2007/01/28 01:08:36 ca Exp $
# Copyright (c) 2005 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:
# Script to test access map restrictions (only IP addresses)
# and reverse lookups (PTR).
# Uses smar, t-smar-3, access map, t-mm, startsmar.sh
# ------------------------------------------------------------
#
test -s stop && exit 1
#

if test X"${MTA_NO_DNS_TEST}" != "X"
then
  echo "SKIPPED: $0: MTA_NO_DNS_TEST is set"
  exit 0
fi
# this test requires DNS entries for 10.0.255.*
if test X"${MTA_HAVE_10_INADDRARPA}" != "Xtrue"
then
  echo "SKIPPED: $0: MTA_HAVE_10_INADDRARPA is not set to true"
  exit 0
fi

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

OUT=smar-rvrs-0.out
IP=smar-rvrs-0.ip
OK=smar-rvrs-0.ok
VERBOSE=false
TPRG=./t-smar-3

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

rm -f ${ACCMAP} ${SOCK} ${PIDS}

# create access map for tests
${MM} -t'	' -F ${ACCMAP} <<EOF
cltresolve:NO	error:450 4.7.1 get a name
cltname:one.meta1.org	error:451 4.7.2 one.meta1.org
EOF

# start SMAR
${SHELL} ${SD}/startsmar.sh || exit 1
rm -f ${OUT} ${IP} ${OK}

# How to test for the various errors?
# temporary lookup failures
# permanent lookup failures
# forward != revervse lookup

TEST=1
# ----------------------------------------
# match
${VALGRIND} ${TPRG} 10.0.255.1 > ${OUT} 2>&1
grep '^react=' ${OUT} > ${IP}
${CAT} > ${OK} <<EOF
react=rvrs_st, ret=0, stat=1
react=rvrs_name, ret=0, name=one.meta1.org.
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi

TEST=2
# ----------------------------------------
# does not match
${VALGRIND} ${TPRG} 10.0.255.2 > ${OUT} 2>&1
grep '^react=' ${OUT} > ${IP}
${CAT} > ${OK} <<EOF
react=statt, ret=0, statt='450 4.7.1 get a name
react=rvrs_st, ret=0, stat=2
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi

TEST=3
# ----------------------------------------
# forward lookup fails
${VALGRIND} ${TPRG} 10.0.255.3 > ${OUT} 2>&1
grep '^react=' ${OUT} > ${IP}
${CAT} > ${OK} <<EOF
react=rvrs_st, ret=0, stat=8
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi

TEST=4
# ----------------------------------------
# reverse lookup fails
${VALGRIND} ${TPRG} 10.0.255.10 > ${OUT} 2>&1
grep '^react=' ${OUT} > ${IP}
${CAT} > ${OK} <<EOF
react=rvrs_st, ret=0, stat=4
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi


# ; two hostnames map to one PTR record
# 2host1ptr1.meta1.org.	IN	A	10.0.255.21
# 2host1ptr2.meta1.org.	IN	A	10.0.255.21
# ; one hostname maps to two PTR record
# 1host2ptr.meta1.org.	IN	A	10.0.255.120
# 1host2ptr.meta1.org.	IN	A	10.0.255.121

TEST=5
# ----------------------------------------
${VALGRIND} ${TPRG} 10.0.255.21 > ${OUT} 2>&1
grep '^react=' ${OUT} > ${IP}
${CAT} > ${OK} <<EOF
react=rvrs_st, ret=0, stat=1
react=rvrs_name, ret=0, name=2host1ptr1.meta1.org.
EOF
if diff ${OK} ${IP} >/dev/null
then
  :
else
  ${CAT} > ${OK} <<EOF
react=rvrs_st, ret=0, stat=1
react=rvrs_name, ret=0, name=2host1ptr2.meta1.org.
EOF
  if diff ${OK} ${IP}
  then
    :
  else
    ERRS=`expr ${ERRS} + 1 `
    echo "$0: test ${TEST} failed."
  fi
fi

TEST=6
# ----------------------------------------
${VALGRIND} ${TPRG} 10.0.255.120 > ${OUT} 2>&1
grep '^react=' ${OUT} > ${IP}
${CAT} > ${OK} <<EOF
react=rvrs_st, ret=0, stat=1
react=rvrs_name, ret=0, name=1host2ptr.meta1.org.
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi

TEST=7
# ----------------------------------------
${VALGRIND} ${TPRG} 10.0.255.121 > ${OUT} 2>&1
grep '^react=' ${OUT} > ${IP}
${CAT} > ${OK} <<EOF
react=rvrs_st, ret=0, stat=1
react=rvrs_name, ret=0, name=1host2ptr.meta1.org.
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
fi


# 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
sleep 3

exit ${ERRS}


syntax highlighted by Code2HTML, v. 0.9.1