#! /bin/sh
# $OpenLDAP: pkg/ldap/tests/scripts/test019-proxycaching,v 1.4.2.1 2003/09/18 15:43:41 kurt Exp $

CACHETTL=60
CACHE_ENTRY_LIMIT=10

SRCDIR="."
if test $# -ge 1 ; then
	SRCDIR=$1; shift
fi

. $SRCDIR/scripts/args.sh $*

if test $PROXYCACHE = no; then 
	echo "Proxy caching requires back-meta AND (back-ldbm OR back-bdb)" 
	exit 0
fi 

echo "running defines.sh"
. $SRCDIR/scripts/defines.sh

# Test proxy caching:
# - start master
# - start proxy cache
# - populate master 
# - perform first set of searches at the proxy
# - verify cacheability
# - perform second set of searches at the proxy 
# - verify answerability

#if test ! -x $SLAPD ; then
#	echo ">>>>> $SLAPD is not executable or does not exist."
#	echo ">>>>> Test skipped."
#	exit 0
#fi

if test ! -d $DBDIR
then 
	mkdir $DBDIR
fi

if test ! -d $CACHEDIR
then 
	mkdir $CACHEDIR
fi

echo "Cleaning up in $DBDIR..."
rm -f $DBDIR/[!C]*
echo "Cleaning up in $CACHEDIR..."
rm -rf $CACHEDIR/[!C]*
echo $DBDIR

echo "Starting master slapd on TCP/IP port $PORT..."
. $CONFFILTER < $CACHEMASTERCONF > $DBCONF
$SLAPD -f $DBCONF -h $MASTERURI -d $LVL > $MASTERLOG 2>&1 &
PID=$!
if test $WAIT != 0 ; then
	echo PID $PID
	read foo
fi

sleep 10

echo "Using ldapadd to populate the master directory..."
$LDAPADD -x -D "$MANAGERDN" -h $LOCALHOST -p $PORT -w $PASSWD < \
	$LDIFORDERED > /dev/null 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapadd failed ($RC)!"
	kill -HUP $PID 
	exit $RC
fi

echo "Starting proxy cache on TCP/IP port $SLAVEPORT..."
. $CONFFILTER < $PROXYCACHECONF > $CACHECONF
$SLAPD -f $CACHECONF -h $SLAVEURI -d $LVL > $SLAVELOG 2>&1 &
CACHEPID=$!
if test $WAIT != 0 ; then
	echo CACHEPID $CACHEPID
	read foo
fi

sleep 8
echo "Making queries on the proxy cache..." 
echo "Query 1: filter:(sn=Jon) attrs: all" 
$LDAPSEARCH -x -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'sn=Jon' > $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3

echo "Query 2: filter:(|(cn=*Jon*)(sn=Jon*)) attrs:cn sn title uid"  
$LDAPSEARCH -x -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'(|(cn=*Jon*)(sn=Jon*))' cn sn title uid >> $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3

echo "Query 3: filter:(sn=Smith*) attrs:cn sn title uid"  
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'sn=Smith*' cn sn title uid >> $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3

echo "Query 4: filter:(sn=Doe*) attrs:cn sn title uid"  
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'sn=Doe' cn sn title uid >> $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3
 
echo "Query 5: filter:(uid=bjorn) attrs:mail postaladdress telephonenumber cn uid"  
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'uid=bjorn' mail postaladdress telephonenumber cn uid >> $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3

echo "Query 6: filter:(mail=*@example.com) cn sn title uid"  
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'mail=*@mail.alumni.example.com' cn sn title uid >> $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3

echo "Query 7: filter:(mail=*) cn sn title uid"  
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'mail=*' cn sn title uid >> $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3
# queries 2-6 are cacheable
CACHEABILITY=0111110
grep CACHEABLE $SLAVELOG | awk '{ 
				  if ($2 == "NOT") 
				    printf "Query %d not cacheable\n",NR
				  else 
				    printf "Query %d cacheable\n",NR
				}' 
CACHED=`grep CACHEABLE $SLAVELOG | awk '{ 
					  if ($2 == "NOT") 
					    printf "0" 
					  else 
					    printf "1" 
					}'`

if test $CACHEABILITY = $CACHED
then
	echo "successfully verified cacheability"
else 
	echo "error in verifying cacheability"
	kill -HUP $PID $CACHEPID
	exit 1
fi

echo "Query 8: filter:(|(cn=*Jones)(sn=Jones)) attrs:cn sn title uid"  
$LDAPSEARCH -x -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'(|(cn=*Jones)(sn=Jones))' cn sn title uid >> $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3

echo "Query 9: filter:(sn=Smith) attrs:cn sn title uid"  
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'sn=Smith' cn sn title uid >> $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3

echo "Query 10: filter:(uid=bjorn) attrs:mail postaladdress telephonenumber cn uid"  
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'uid=bjorn' mail postaladdress telephonenumber cn uid >> $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3

echo "Query 11: filter:(mail=*@example.com) cn sn title uid"  
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $SLAVEPORT \
	'mail=jaj@mail.alumni.example.com' cn sn title uid >> $SLAVEOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	kill -HUP $PID $CACHEPID
	exit $RC
fi
sleep 3
#queries 8-11 are answerable
ANSWERABILITY=1111
grep ANSWERABLE $SLAVELOG | awk '{ 
				    if (NR > 7) { 
				      if ($2 == "NOT") 
					printf "Query %d not answerable\n",NR
				      else 
					printf "Query %d answerable\n",NR 
				     }
				  }' 
ANSWERED=`grep ANSWERABLE $SLAVELOG | awk '{ 
					     if (NR > 7) { 
					       if ($2 == "NOT") 
						 printf "0" 
 					       else 
				                 printf "1"
					     } 
					   }'`
if test $ANSWERABILITY = $ANSWERED
then
	echo "successfully verified answerability"
else 
	echo "error in verifying answerability"
	kill -HUP $PID $CACHEPID
	exit 1
fi

echo "Proxy cache successfully tested" 
kill -HUP $PID $CACHEPID
