#! /bin/sh
## $OpenLDAP$
## This work is part of OpenLDAP Software <http://www.openldap.org/>.
##
## Copyright 2016-2021 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>.
##
## ACKNOWLEDGEMENTS:
## This module was written in 2016 by Ondřej Kuzník for Symas Corp.

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

if test "$SYNCPROV" = syncprovno; then
	echo "Syncrepl provider overlay not available, test skipped"
	exit 0
fi

. ${SCRIPTDIR}/common.sh

if test "$SYNCPROV" = syncprovmod; then
	$LDAPMODIFY -v -D cn=config -H $URI1 -y $CONFIGPWF \
	> $TESTOUT 2>&1 <<EOMOD
dn: cn=module,cn=config
changetype: add
objectClass: olcModuleList
olcModuleLoad: $LDAP_BUILD/servers/slapd/overlays/syncprov.la
EOMOD

	RC=$?
	if test $RC != 0 ; then
		echo "ldapmodify failed ($RC)!"
		test $KILLSERVERS != no && kill -HUP $KILLPIDS
		exit $RC
	fi
fi

mkdir $DBDIR4 $TESTDIR/confdir-consumer

echo "Starting consumer slapd on TCP/IP port $PORT4..."
. $CONFFILTER $BACKEND $MONITORDB < $P1SRCONSUMERCONF > $CONF4

echo "database config" >>$CONF4
echo "rootpw `$SLAPPASSWD -T $CONFIGPWF`" >>$CONF4

$SLAPD -f $CONF4 -F $TESTDIR/confdir-consumer -h $URI4 -d $LVL > $LOG4 2>&1 &
CONSUMERPID=$!
if test $WAIT != 0 ; then
	echo CONSUMERPID $CONSUMERPID
	read foo
fi
KILLPIDS="$KILLPIDS $CONSUMERPID"

sleep $SLEEP0

echo "Setting up overlay on consumer..."
$LDAPMODIFY -v -D cn=config -H $URI4 -y $CONFIGPWF \
	> $TESTOUT 2>&1 <<EOMOD
dn: cn=module,cn=config
changetype: add
objectClass: olcModuleList
olcModuleLoad: `pwd`/../datamorph.la
EOMOD
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Configuring syncprov on provider..."
$LDAPMODIFY -v -D cn=config -H $URI1 -y $CONFIGPWF \
	> $TESTOUT 2>&1 <<EOMOD
dn: olcOverlay={0}syncprov,olcDatabase={1}$BACKEND,cn=config
changetype: add
objectclass: olcSyncProvConfig
EOMOD
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

. $CONFFILTER $BACKEND $MONITORDB < $OVERLAY_CONFIG | \
$LDAPMODIFY -v -D cn=config -H $URI4 -y $CONFIGPWF \
	> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

for i in 0 1 2 3 4 5; do
	$LDAPSEARCH -s base -b "$BASEDN" -H $URI4 \
		'objectclass=*' > /dev/null 2>&1
	RC=$?
	if test $RC = 0 ; then
		break
	fi
	echo "Waiting ${SLEEP1} seconds for consumer to start replication..."
	sleep ${SLEEP1}
done

echo "Waiting ${SLEEP1} seconds for consumer to finish replicating..."
sleep ${SLEEP1}

echo "Testing searches against regular replicated entries..."
echo "# Testing searches against regular replicated entries..." >> $SEARCHOUT
$LDAPSEARCH -b "$BASEDN" -H $URI4 "(|(ou=Groups)(st=*))" \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Testing searches listing replicated transformed attributes..."
echo >> $SEARCHOUT
echo "# Testing searches listing replicated transformed attributes..." >> $SEARCHOUT
$LDAPSEARCH -b "ou=Information Technology Division,ou=People,$BASEDN" -s one \
	-H $URI4 \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Testing searches filtering on replicated transformed attributes..."
echo >> $SEARCHOUT
echo "# Testing searches filtering on replicated transformed attributes..." >> $SEARCHOUT
$LDAPSEARCH -b "$BASEDN" -H $URI4 \
	"(|(enumerated=bjensen)(&(signed=-19858)(signed<=0)(signed>=-20000)))" \
	enumerated signed \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Reconfiguring transformation definition..."
. $CONFFILTER $BACKEND $MONITORDB < data/test003-config.ldif | \
sed 's/{0}datamorph/{1}datamorph/' | \
$LDAPMODIFY -v -D cn=config -H $URI1 -y $CONFIGPWF \
	>> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

. $CONFFILTER $BACKEND $MONITORDB < data/test003-config.ldif | \
$LDAPMODIFY -v -D cn=config -H $URI4 -y $CONFIGPWF \
	>> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Testing searches filtering on the new replicated values..."
echo >> $SEARCHOUT
echo "# Testing searches filtering on the new replicated values..." >> $SEARCHOUT
$LDAPSEARCH -b "$BASEDN" -H $URI4 \
	"(|(enumerated=not a value)(enumerated=jaj)(&(signed=45678)(!(signed>=50000))(signed>=44444)))" \
	enumerated signed \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

LDIF=data/test003-out.ldif

echo "Filtering ldapsearch results..."
$LDIFFILTER -s e < $SEARCHOUT > $SEARCHFLT
echo "Filtering expected entries..."
$LDIFFILTER -s e < $LDIF > $LDIFFLT
echo "Comparing filter output..."
$CMP $SEARCHFLT $LDIFFLT > $CMPOUT

if test $? != 0 ; then
	echo "Comparison failed"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit 1
fi

rm $SEARCHOUT

echo "Reverting part of the above configuration for remainder of the test..."
. $CONFFILTER $BACKEND $MONITORDB < data/test007-config.ldif | \
sed 's/{0}datamorph/{1}datamorph/' | \
$LDAPMODIFY -v -D cn=config -H $URI1 -y $CONFIGPWF \
	>> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

. $CONFFILTER $BACKEND $MONITORDB < data/test007-config.ldif | \
$LDAPMODIFY -v -D cn=config -H $URI4 -y $CONFIGPWF \
	>> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Modifying entry..."
$LDAPMODIFY -D $MANAGERDN -H $URI1 -w $PASSWD \
	-f data/test005-changes.ldif >> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Applying invalid changes (should fail)..."
for CHANGE in data/test005-*fail.ldif; do
	echo "... $CHANGE"
	$LDAPMODIFY -D $MANAGERDN -H $URI1 -w $PASSWD \
		-f $CHANGE >> $TESTOUT 2>&1
	RC=$?
	case $RC in
	0)
		echo "ldapmodify should have failed ($RC)!"
		test $KILLSERVERS != no && kill -HUP $KILLPIDS
		exit 1
		;;
	16|19)
		echo "ldapmodify failed ($RC)"
		;;
	*)
		echo "ldapmodify failed ($RC)!"
		test $KILLSERVERS != no && kill -HUP $KILLPIDS
		exit $RC
		;;
	esac
done

echo "Waiting ${SLEEP1} seconds for consumer to finish replicating..."
sleep ${SLEEP1}

echo "Reading affected entries back..."
echo "# Reading affected entries back..." >> $SEARCHOUT
$LDAPSEARCH -b "$BASEDN" -H $URI1 \
	'(|(objectClass=OpenLDAPperson)(ou=people))' \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

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

LDIF=data/test005-out.ldif

echo "Filtering ldapsearch results..."
$LDIFFILTER -s e < $SEARCHOUT > $SEARCHFLT
echo "Filtering expected entries..."
$LDIFFILTER -s e < $LDIF > $LDIFFLT
echo "Comparing filter output..."
$CMP $SEARCHFLT $LDIFFLT > $CMPOUT


if test $? != 0 ; then
	echo "Comparison failed"
	exit 1
fi

echo ">>>>> Test succeeded"

test $KILLSERVERS != no && wait

exit 0
