#! /bin/sh
# $OpenLDAP$
## This work is part of OpenLDAP Software <http://www.openldap.org/>.
##
## Copyright 1998-2025 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 1

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 5 seconds for slapd to start..."
	sleep 5
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 1

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 5 seconds for slapd to start..."
	sleep 5
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 1

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 5 seconds for slapd to start..."
	sleep 5
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 < data/asyncmeta.closed.out > $LDIFFLT
echo "Comparing filter output..."
$CMP $SEARCHFLT $LDIFFLT > $CMPOUT

#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=$?
	#if test $RC != 0 ; then
	#	echo "Search failed ($RC)!"
	#	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	#	exit $RC
	#fi
	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 0
			;;
		*)
			echo "Search failed ($RC)!"
			test $KILLSERVERS != no && kill -HUP $KILLPIDS
			exit $RC
			;;
	esac
	sleep 2
done
# search cn=monitor - no connections should be closed yet
SEARCHDN="cn=Connections,cn=database 1,cn=databases,cn=monitor"
echo "Verifying that all target connections are open..."
cat /dev/null > $SEARCHOUT

echo "	base=\"$SEARCHDN\"..."
echo "# 	base=\"$SEARCHDN\"..." >> $SEARCHOUT
$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 < data/asyncmeta.allopen.out > $LDIFFLT
echo "Comparing filter output..."
$CMP $SEARCHFLT $LDIFFLT > $CMPOUT

# wait 6 seconds and search cn=monitor - connection group 1 is closed
SEARCHDN="cn=Connections,cn=database 1,cn=databases,cn=monitor"
echo "Verifying that connection group 1 is closed..."
sleep 5
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 < data/asyncmeta.1.out > $LDIFFLT
echo "Comparing filter output..."
$CMP $SEARCHFLT $LDIFFLT > $CMPOUT
SEARCHDN="cn=Connections,cn=database 1,cn=databases,cn=monitor"
echo "Verifying that target connection group 2 is closed..."
sleep 6
cat /dev/null > $SEARCHOUT

echo "	base=\"$SEARCHDN\"..."
echo "# 	base=\"$SEARCHDN\"..." >> $SEARCHOUT
$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 < data/asyncmeta.2.out > $LDIFFLT
echo "Comparing filter output..."
$CMP $SEARCHFLT $LDIFFLT > $CMPOUT

SEARCHDN="cn=Connections,cn=database 1,cn=databases,cn=monitor"
echo "Verifying that all target connections are closed..."
# wait 6 seconds, search, all connections should be closed
sleep 6
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 < data/asyncmeta.closed.out > $LDIFFLT
echo "Comparing filter output..."
$CMP $SEARCHFLT $LDIFFLT > $CMPOUT

cat /dev/null > $SEARCHOUT

test $KILLSERVERS != no && kill -HUP $KILLPIDS

echo ">>>>> Test succeeded"

test $KILLSERVERS != no && wait

exit 0
