#! /bin/sh
# $OpenLDAP: pkg/ldap/tests/scripts/test046-dds,v 1.4.2.3 2008/02/11 23:26:51 kurt Exp $
## This work is part of OpenLDAP Software <http://www.openldap.org/>.
##
## Copyright 2005-2008 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

if test $DDS = ddsno; then 
	echo "Dynamic Directory Services overlay not available, test skipped"
	exit 0
fi 

mkdir -p $TESTDIR $DBDIR1

echo "Running slapadd to build slapd database..."
. $CONFFILTER $BACKEND $MONITORDB < $MCONF > $ADDCONF
$SLAPADD -f $ADDCONF -l $LDIFORDERED
RC=$?
if test $RC != 0 ; then
	echo "slapadd failed ($RC)!"
	exit $RC
fi

echo "Running slapindex to index slapd database..."
. $CONFFILTER $BACKEND $MONITORDB < $DDSCONF > $CONF1
$SLAPINDEX -f $CONF1
RC=$?
if test $RC != 0 ; then
	echo "warning: slapindex failed ($RC)"
	echo "  assuming no indexing support"
fi

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

sleep 1

echo "Testing slapd searching..."
for i in 0 1 2 3 4 5; do
	$LDAPSEARCH -s base -b "$MONITOR" -h $LOCALHOST -p $PORT1 \
		'(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

echo "Creating a dynamic entry..."
$LDAPADD -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: cn=Dynamic Object,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: dynamicObject
cn: Dynamic Object
sn: Object
EOMODS
RC=$?
if test $RC != 0 ; then
	echo "ldapadd failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Refreshing the newly created dynamic entry..."
$LDAPEXOP -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	"refresh" "cn=Dynamic Object,dc=example,dc=com" "120" \
	>> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapexop failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Modifying the newly created dynamic entry..."
$LDAPMODIFY -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: cn=Dynamic Object,dc=example,dc=com
changetype: modify
add: userPassword
userPassword: dynamic
EOMODS
RC=$?
if test $RC != 0 ; then
	echo "ldapadd failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Binding as the newly created dynamic entry..."
$LDAPWHOAMI -h $LOCALHOST -p $PORT1 \
	-D "cn=Dynamic Object,dc=example,dc=com" -w dynamic
RC=$?
if test $RC != 0 ; then
	echo "ldapwhoami failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Creating a dynamic entry subordinate to another..."
$LDAPADD -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: cn=Subordinate Dynamic Object,cn=Dynamic Object,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: dynamicObject
cn: Subordinate Dynamic Object
sn: Object
userPassword: dynamic
EOMODS
RC=$?
if test $RC != 0 ; then
	echo "ldapadd failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

SEARCH=0

SEARCH=`expr $SEARCH + 1`
echo "# [$SEARCH] Searching the dynamic portion of the database..." >> $SEARCHOUT
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $PORT1 \
	'(objectClass=dynamicObject)' '*' entryTtl \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Creating a static entry subordinate to a dynamic one (should fail)..."
$LDAPADD -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: cn=Subordinate Static Object,cn=Dynamic Object,dc=example,dc=com
objectClass: inetOrgPerson
cn: Subordinate Static Object
sn: Object
userPassword: static
EOMODS
RC=$?
case $RC in
0)
	echo "ldapadd should have failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit -1
	;;
19)
	echo "ldapadd failed ($RC)"
	;;
*)
	echo "ldapadd failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
	;;
esac

echo "Turning a static into a dynamic entry (should fail)..."
$LDAPMODIFY -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: ou=People,dc=example,dc=com
changetype: modify
add: objectClass
objectClass: dynamicObject
EOMODS
RC=$?
case $RC in
0)
	echo "ldapmodify should have failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit -1
	;;
65)
	echo "ldapmodify failed ($RC)"
	;;
*)
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
	;;
esac

echo "Turning a dynamic into a static entry (should fail)..."
$LDAPMODIFY -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: cn=Dynamic Object,dc=example,dc=com
changetype: modify
delete: objectClass
objectClass: dynamicObject
EOMODS
RC=$?
case $RC in
0)
	echo "ldapmodify should have failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit -1
	;;
65)
	echo "ldapmodify failed ($RC)"
	;;
*)
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
	;;
esac

echo "Renaming a dynamic entry..."
$LDAPMODIFY -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: cn=Subordinate Dynamic Object,cn=Dynamic Object,dc=example,dc=com
changetype: modrdn
newrdn: cn=Renamed Dynamic Object
deleteoldrdn: 1
EOMODS
RC=$?
if test $RC != 0 ; then
	echo "ldapmodrdn failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

SEARCH=`expr $SEARCH + 1`
echo "# [$SEARCH] Searching the dynamic portion of the database..." >> $SEARCHOUT
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $PORT1 \
	'(objectClass=dynamicObject)' '*' entryTtl \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Refreshing the initial dynamic entry to make it expire earlier than the subordinate..."
$LDAPEXOP -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	"refresh" "cn=Dynamic Object,dc=example,dc=com" "1" \
	>> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapexop failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

SLEEP=10
echo "Waiting $SLEEP seconds to force a subordinate/superior expiration conflict..."
sleep $SLEEP

echo "Re-vitalizing the initial dynamic entry..."
$LDAPEXOP -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	"refresh" "cn=Dynamic Object,dc=example,dc=com" "120" \
	>> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapexop failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Re-renaming the subordinate dynamic entry (new superior)..."
$LDAPMODIFY -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: cn=Renamed Dynamic Object,cn=Dynamic Object,dc=example,dc=com
changetype: modrdn
newrdn: cn=Renamed Dynamic Object
deleteoldrdn: 1
newsuperior: dc=example,dc=com
EOMODS
RC=$?
if test $RC != 0 ; then
	echo "ldapmodrdn failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

SEARCH=`expr $SEARCH + 1`
echo "# [$SEARCH] Searching the dynamic portion of the database..." >> $SEARCHOUT
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $PORT1 \
	'(objectClass=dynamicObject)' '*' entryTtl \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Deleting a dynamic entry..."
$LDAPMODIFY -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: cn=Dynamic Object,dc=example,dc=com
changetype: delete
EOMODS
RC=$?
if test $RC != 0 ; then
	echo "ldapdelete failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

SEARCH=`expr $SEARCH + 1`
echo "# [$SEARCH] Searching the dynamic portion of the database..." >> $SEARCHOUT
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $PORT1 \
	'(objectClass=dynamicObject)' '*' entryTtl \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Refreshing the remaining dynamic entry..."
$LDAPEXOP -D $MANAGERDN -w $PASSWD -h $LOCALHOST -p $PORT1 \
	"refresh" "cn=Renamed Dynamic Object,dc=example,dc=com" "1" \
	>> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapexop failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

SEARCH=`expr $SEARCH + 1`
echo "# [$SEARCH] Searching the dynamic portion of the database..." >> $SEARCHOUT
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $PORT1 \
	'(objectClass=dynamicObject)' '*' entryTtl \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

SLEEP=15
echo "Waiting $SLEEP seconds for remaining entry to expire..."
sleep $SLEEP

SEARCH=`expr $SEARCH + 1`
echo "# [$SEARCH] Searching the dynamic portion of the database..." >> $SEARCHOUT
$LDAPSEARCH -S "" -b "$BASEDN" -h $LOCALHOST -p $PORT1 \
	'(objectClass=dynamicObject)' '*' entryTtl \
	>> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapsearch failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

# Meeting
MEETINGDN="cn=Meeting,ou=Groups,dc=example,dc=com"
echo "Creating a meeting as $BJORNSDN..."
$LDAPMODIFY -D "$BJORNSDN" -w bjorn -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: $MEETINGDN
changetype: add
objectClass: groupOfNames
objectClass: dynamicObject
cn: Meeting
member: $BJORNSDN

dn: $MEETINGDN
changetype: modify
add: member
member: $JAJDN
EOMODS
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Refreshing the meeting as $BJORNSDN..."
$LDAPEXOP -D "$BJORNSDN" -w bjorn -h $LOCALHOST -p $PORT1 \
	"refresh" "$MEETINGDN" "120" \
	>> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapexop failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Joining the meeting as $BABSDN..."
$LDAPMODIFY -D "$BABSDN" -w bjensen -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: $MEETINGDN
changetype: modify
add: member
member: $BABSDN
EOMODS
RC=$?
if test $RC != 0 ; then
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Trying to add a member as $BABSDN (should fail)..."
$LDAPMODIFY -D "$BABSDN" -w bjensen -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: $MEETINGDN
changetype: modify
add: member
member: $MELLIOTDN
EOMODS
RC=$?
case $RC in
0)
	echo "ldapmodify should have failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
	;;
50)
	echo "ldapmodify failed ($RC)"
	;;
*)
	echo "ldapmodify failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
	;;
esac

echo "Refreshing the meeting as $BABSDN..."
$LDAPEXOP -D "$BABSDN" -w bjensen -h $LOCALHOST -p $PORT1 \
	"refresh" "$MEETINGDN" "180" \
	>> $TESTOUT 2>&1
RC=$?
if test $RC != 0 ; then
	echo "ldapexop failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Trying to refresh the meeting anonymously (should fail)..."
$LDAPEXOP -h $LOCALHOST -p $PORT1 \
	"refresh" "$MEETINGDN" "240" \
	>> $TESTOUT 2>&1
RC=$?
if test $RC = 0 ; then
	echo "ldapexop should have failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

echo "Trying to delete the meeting as $BABSDN (should fail)..."
$LDAPMODIFY -D "$BABSDN" -w bjensen -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: $MEETINGDN
changetype: delete
EOMODS
RC=$?
case $RC in
0)
	echo "ldapdelete should have failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
	;;
50)
	echo "ldapdelete failed ($RC)"
	;;
*)
	echo "ldapdelete failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
	;;
esac

echo "Deleting the meeting as $BJORNSDN..."
$LDAPMODIFY -D "$BJORNSDN" -w bjorn -h $LOCALHOST -p $PORT1 \
	>> $TESTOUT 2>&1 << EOMODS
dn: $MEETINGDN
changetype: delete
EOMODS
RC=$?
if test $RC != 0 ; then
	echo "ldapdelete failed ($RC)!"
	test $KILLSERVERS != no && kill -HUP $KILLPIDS
	exit $RC
fi

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

LDIF=$DDSOUT

echo "Filtering ldapsearch results..."
. $LDIFFILTER < $SEARCHOUT > $SEARCHFLT
echo "Filtering original ldif used to create database..."
. $LDIFFILTER < $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
