#! /bin/sh
# $OpenLDAP$
## This work is part of OpenLDAP Software <http://www.openldap.org/>.
##
## Copyright 1998-2026 The OpenLDAP Foundation.
## All rights reserved.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted only as authorized by the OpenLDAP
## Public License.
##
## A copy of this license is available in the file LICENSE in the
## top-level directory of the distribution or, alternatively, at
## <http://www.OpenLDAP.org/license.html>.

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

echo ""

if test $BACKASYNCMETA = asyncmetano ; then
	echo "asyncmeta backend not available, test skipped"
	exit 0
fi

if test $BACKLDAP = ldapno ; then
	echo "ldap backend not available, test skipped"
	exit 0
fi

rm -rf $TESTDIR

mkdir -p $TESTDIR $DBDIR1 $DBDIR2

$SLAPPASSWD -g -n >$CONFIGPWF
echo "rootpw `$SLAPPASSWD -T $CONFIGPWF`" >$TESTDIR/configpw.conf

echo "Starting slapd on TCP/IP port $PORT1..."
. $CONFFILTER $BACKEND < $METACONF1 > $CONF1
$SLAPD -f $CONF1 -h $URI1 -d $LVL > $LOG1 2>&1 &
PID=$!
if test $WAIT != 0 ; then
    echo PID $PID
    read foo
fi
KILLPIDS="$PID"

sleep $SLEEP0

echo "Using ldapsearch to check that slapd is running..."
for i in 0 1 2 3 4 5; do
	$LDAPSEARCH -s base -b "$MONITOR" -H $URI1 \
		'objectclass=*' > /dev/null 2>&1
	RC=$?
	if test $RC = 0 ; then
		break
	fi
	echo "Waiting $SLEEP1 seconds for slapd to start..."
	sleep $SLEEP1
done
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Using ldapadd to populate the database..."
$LDAPADD -D "$MANAGERDN" -H $URI1 -w $PASSWD < \
	$LDIFORDERED > $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapadd failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Starting slapd on TCP/IP port $PORT2..."
. $CONFFILTER $BACKEND < $METACONF2 > $CONF2
$SLAPD -f $CONF2 -h $URI2 -d $LVL > $LOG2 2>&1 &
PID=$!
if test $WAIT != 0 ; then
    echo PID $PID
    read foo
fi
KILLPIDS="$KILLPIDS $PID"

sleep $SLEEP0

echo "Using ldapsearch to check that slapd is running..."
for i in 0 1 2 3 4 5; do
	$LDAPSEARCH -s base -b "$MONITOR" -H $URI2 \
		'objectclass=*' > /dev/null 2>&1
	RC=$?
	if test $RC = 0 ; then
		break
	fi
	echo "Waiting $SLEEP1 seconds for slapd to start..."
	sleep $SLEEP1
done
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Using ldapadd to populate the database..."
$LDAPADD -D "$METAMANAGERDN" -H $URI2 -w $PASSWD < \
	$LDIFMETA >> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapadd failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Starting slapd on TCP/IP port $PORT3..."
. $CONFFILTER $BACKEND < $ASYNCMETACONF2 > $CONF3
$SLAPD -f $CONF3 -h $URI3 -d $LVL > $LOG3 2>&1 &
PID=$!
if test $WAIT != 0 ; then
    echo PID $PID
    read foo
fi
KILLPIDS="$KILLPIDS $PID"

sleep $SLEEP0

echo "Using ldapsearch to check that slapd is running..."
for i in 0 1 2 3 4 5; do
	$LDAPSEARCH -s base -b "$MONITOR" -H $URI3 \
		'objectclass=*' > /dev/null 2>&1
	RC=$?
	if test $RC = 0 ; then
		break
	fi
	echo "Waiting $SLEEP1 seconds for slapd to start..."
	sleep $SLEEP1
done
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

cat /dev/null > $SEARCHOUT

#search cn=monitor, all connections are closed
SEARCHDN="cn=Connections,cn=database 1,cn=databases,cn=monitor"
echo "Verifying that all target connections are closed..."
cat /dev/null > $SEARCHOUT

echo "	base=\"$SEARCHDN\"..."
echo "# 	base=\"$SEARCHDN\"..." >> $SEARCHOUT
$LDAPSEARCH  -H $URI3 \
			 -b "$SEARCHDN" \
			 "olmTargetConnFlags=closed" 'olmTargetConnFlags' >> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "Unable to read monitor ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi
echo "Filtering ldapsearch results..."
$LDIFFILTER < $SEARCHOUT > $SEARCHFLT
$LDIFFILTER < $DATADIR/asyncmeta.closed.out > $LDIFFLT
echo "Comparing filter output..."
$CMP $SEARCHFLT $LDIFFLT > $CMPOUT
if test $? != 0 ; then
	echo "Comparison failed - the connections are not closed"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit 1
fi
#run 3 searches
for i in 0 1 2; do
	BASEDN="o=Example,c=US"
	echo "Searching base=\"$BASEDN\"..."
	echo "# searching base=\"$BASEDN\"..." >> $SEARCHOUT
	$LDAPSEARCH -S "" -H $URI3 -b "$BASEDN" >> $SEARCHOUT 2>&1
	RC=$?
	case $RC in
		0)
		;;
		51)
			echo "### Hit LDAP_BUSY problem; you may want to re-run the test"
			test $KILLSERVERS != no && { kill -HUP $KILLPIDS || exit $?; }
			exit 0
			;;
		*)
			echo "Search failed ($RC)!"
			test $KILLSERVERS != no && kill -HUP $KILLPIDS
			exit $RC
			;;
	esac
done
# search cn=monitor - no connections should be closed yet
sleep $SLEEP0
echo "Verifying that all target connections are open..."
cat /dev/null > $SEARCHOUT

echo "	base=\"$SEARCHDN\"..."
$LDAPSEARCH  -H $URI3 \
			 -b "$SEARCHDN" \
			 'olmTargetConnFlags' >> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "Unable to read monitor ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi
echo "Filtering ldapsearch results..."
$LDIFFILTER < $SEARCHOUT > $SEARCHFLT
$LDIFFILTER < $DATADIR/asyncmeta.allopen.out > $LDIFFLT

echo "Comparing filter output..."
$CMP $SEARCHFLT $LDIFFLT > $CMPOUT
if test $? != 0 ; then
	echo "Comparison failed - the connections are not open"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit 1
fi
# wait 7 seconds (the con ttl time + 1) and search cn=monitor
# connection group 1 should be closed
# we allow a couple of retrys to compensate for delays
echo "Waiting for the time-to-live time..."
sleep $SLEEP1
SEARCHDN="cn=Connections,cn=database 1,cn=databases,cn=monitor"
echo "Verifying that connection group 1 is closed..."
for i in 0 1 2; do
	cat /dev/null > $SEARCHOUT

	echo "	base=\"cn=Connection Group 1,$SEARCHDN\"..."
	echo "# 	base=\"$SEARCHDN\"..." >> $SEARCHOUT
	$LDAPSEARCH  -H $URI3 \
				 -b "cn=Connection Group 1,$SEARCHDN" \
				 "olmTargetConnFlags=closed" 'olmTargetConnFlags' >> $SEARCHOUT 2>&1
	RC=$?
	if test $RC != 0 ; then
		echo "Unable to read monitor ($RC)!"
		test $KILLSERVERS != no && kill -HUP $KILLPIDS
		exit $RC
	fi
	echo "Filtering ldapsearch results..."
	$LDIFFILTER < $SEARCHOUT > $SEARCHFLT
	$LDIFFILTER < $DATADIR/asyncmeta.1.out > $LDIFFLT
	echo "Comparing filter output..."
	$CMP $SEARCHFLT $LDIFFLT > $CMPOUT
	if test $? != 0 ; then
		if test $i != 2 ; then
			echo "Comparison failed - connection group 1 is not closed, retrying in $SLEEP0 seconds..."
			sleep $SLEEP0
		else
			echo "Comparison failed - connection group 1 is not closed, test failed"
			test $KILLSERVERS != no && kill -HUP $KILLPIDS
			exit 1
		fi
	else
		echo "Comparison successful, all connections are closed"
		break
	fi
done
# wait 7 seconds (the con ttl interval + 1) and search cn=monitor
# connection group 2 should be closed
echo "Waiting for the connection reset interval..."
sleep $SLEEP1
echo "Verifying that target connection group 2 is closed..."
for i in 0 1 2; do
	cat /dev/null > $SEARCHOUT
	echo "	base=\"cn=Connection Group 2,$SEARCHDN\"..."
	$LDAPSEARCH  -H $URI3 \
				 -b "cn=Connection Group 2,$SEARCHDN" \
				 "olmTargetConnFlags=closed" 'olmTargetConnFlags' >> $SEARCHOUT 2>&1
	RC=$?
	if test $RC != 0 ; then
		echo "Unable to read monitor ($RC)!"
		test $KILLSERVERS != no && kill -HUP $KILLPIDS
		exit $RC
	fi
	echo "Filtering ldapsearch results..."
	$LDIFFILTER < $SEARCHOUT > $SEARCHFLT
	$LDIFFILTER < $DATADIR/asyncmeta.2.out > $LDIFFLT
	echo "Comparing filter output..."
	$CMP $SEARCHFLT $LDIFFLT > $CMPOUT
	if test $? != 0 ; then
		if test $i != 2 ; then
			echo "Comparison failed - connection group 2 is not closed, retrying in $SLEEP0 seconds..."
			sleep $SLEEP0
		else
			echo "Comparison failed - connection group 2 is not closed, test failed"
			test $KILLSERVERS != no && kill -HUP $KILLPIDS
			exit 1
		fi
	else
		echo "Comparison successful, all connections are closed"
		break
	fi
done

# wait 7 seconds (the con ttl interval + 1) and search cn=monitor
# all connections should be closed
# sometimes the timeout loop may be delayed, so we give it a couple of
# retries before giving up
echo "Waiting for the connection reset interval..."
sleep $SLEEP1
echo "Verifying that all target connections are closed..."
for i in 0 1 2; do
	cat /dev/null > $SEARCHOUT

	echo "	base=\"$SEARCHDN\"..."
	echo "# 	base=\"$SEARCHDN\"..." >> $SEARCHOUT
	$LDAPSEARCH  -H $URI3 \
				 -b "$SEARCHDN" \
				 "olmTargetConnFlags=closed" 'olmTargetConnFlags' >> $SEARCHOUT 2>&1
	RC=$?
	if test $RC != 0 ; then
		echo "Unable to read monitor ($RC)!"
		test $KILLSERVERS != no && kill -HUP $KILLPIDS
		exit $RC
	fi
	echo "Filtering ldapsearch results..."
	$LDIFFILTER < $SEARCHOUT > $SEARCHFLT
	$LDIFFILTER < $DATADIR/asyncmeta.closed.out > $LDIFFLT
	echo "Comparing filter output..."
	$CMP $SEARCHFLT $LDIFFLT > $CMPOUT
	if test $? != 0 ; then
		if test $i != 2 ; then
			echo "Comparison failed - the connections are not closed, retrying in $SLEEP0 seconds..."
			sleep $SLEEP0
		else
			echo "Comparison failed - the connections are not closed, test failed"
			test $KILLSERVERS != no && kill -HUP $KILLPIDS
			exit 1
		fi
	else
		echo "Comparison successful, all connections are closed"
		break
	fi
done
cat /dev/null > $SEARCHOUT

test $KILLSERVERS != no && { kill -HUP $KILLPIDS || exit $?; }

echo ">>>>> Test succeeded"

test $KILLSERVERS != no && { wait $KILLPIDS || exit $?; }

exit 0
