#!/bin/sh
# File Name : acivattr.sh
# Author: Robert Byrne
# Creation Date : 25/07/01
# Desc :  test vattrs in acis.
#
# Usage : None, should be invoked by tcc
#
# Target : Unix,NT
#

tet_startup="ServerInfo"
tet_cleanup=""
iclist="ic0 ic1 ic2 ic3 ic4 ic5 ic7"
ic0="startup"
# ic1: test real attr: eq,sub,pres filter in aci
ic1="tp1 tp2 tp3 tp4 tp5 tp6"
#ic2: test nsrole: eq,sub,pres filter in aci
ic2="tp100 tp101 tp102 tp103 tp104 tp105"
#ic3: charge the nsrole cache, then redo ic2
ic3="tp200 tp100 tp101 tp102 tp103 tp104 tp105"
#ic4: test cos attr employeetype: eq,sub,pres filter in aci
ic4="tp300 tp301 tp302 tp303 tp304 tp305"
#ic5: test vattr in userdn ldapurl
ic5="tp400 tp401"
ic7="cleanup"

TestSuiteName="acl"
export TestSuiteName

####################
# Global variables #
####################

DNBASE="o=acivattr"
DNBE="acivattr"

BINDIR=$PREFIX/usr/bin
export BINDIR

DATAPATH=$TET_SUITE_ROOT/data
IMPORTF=$DATAPATH/acivattr.ldif
DELETEF=$DATAPATH/acivattr-del.ldif

ENG_USER="cn=enguser1,ou=eng,o=acivattr"
SALES_UESER="cn=salesuser1,ou=sales,o=acivattr"
ENG_MANAGER="cn=engmanager1,ou=eng,o=acivattr"
SALES_MANAGER="cn=salesmanager1,ou=sales,o=acivattr"
SALES_OU="ou=sales,o=acivattr"

REAL_EQ_ACI="(targetattr=\"*\")(targetfilter=\"(cn=engmanager1)\") (version 3.0; acl \"real-eq\"; allow (all) userdn=\"ldap:///cn=enguser1,ou=eng,o=acivattr\";)"
REAL_PRES_ACI="(targetattr=\"*\")(targetfilter=\"(cn=*)\") (version 3.0; acl \"real-pres\"; allow (all) userdn=\"ldap:///cn=enguser1,ou=eng,o=acivattr\";)"
REAL_SUB_ACI="(targetattr=\"*\")(targetfilter=\"(cn=eng*)\") (version 3.0; acl \"real-sub\"; allow (all) userdn=\"ldap:///cn=enguser1,ou=eng,o=acivattr\";)"

ROLE_EQ_ACI="(targetattr=\"*\")(targetfilter=\"(nsrole=cn=filterroleengrole,o=sun.com)\") (version 3.0; acl \"role-eq\"; allow (all) userdn=\"ldap:///cn=enguser1,ou=eng,o=acivattr\";)"
ROLE_PRES_ACI="(targetattr=\"*\")(targetfilter=\"(nsrole=*)\") (version 3.0; acl \"role-pres\"; allow (all) userdn=\"ldap:///cn=enguser1,ou=eng,o=acivattr\";)"
ROLE_SUB_ACI="(targetattr=\"*\")(targetfilter=\"(nsrole=cn=filterroleeng*)\") (version 3.0; acl \"role-sub\"; allow (all) userdn=\"ldap:///cn=enguser1,ou=eng,o=acivattr\";)"

COS_EQ_ACI="(targetattr=\"*\")(targetfilter=\"(employeetype=engtype)\") (version 3.0; acl \"cos-eq\"; allow (all) userdn=\"ldap:///cn=enguser1,ou=eng,o=acivattr\";)"
COS_PRES_ACI="(targetattr=\"*\")(targetfilter=\"(employeetype=*)\") (version 3.0; acl \"cos-pres\"; allow (all) userdn=\"ldap:///cn=enguser1,ou=eng,o=acivattr\";)"
COS_SUB_ACI="(targetattr=\"*\")(targetfilter=\"(employeetype=eng*)\") (version 3.0; acl \"cos-sub\"; allow (all) userdn=\"ldap:///cn=enguser1,ou=eng,o=acivattr\";)"

LDAPURL_ACI="(targetattr=\"*\")(version 3.0; acl \"url\"; allow (all) userdn=\"ldap:///o=acivattr??sub?(nsrole=*eng*)\";)"

Replace_Description()
{
user=$1
pw=$2
entry=$3
$LDAPMODIFY -D "$user" -w "$pw" -h "$LDAPhost" -p $LDAPport << EOF
dn: $entry
changetype: modify
replace: description
description: fred
EOF
rc=$?
}

#####################################
# TEST CASES
#####################################

#####
startup ()
#####
{

StopSlapd
# The server should be up and running to be able to add the suffix
if StartSlapd; then
    :
else
    message "startup: cannot start slapd"	
    return 1
fi

if HasSuffix "$DNBASE"; then
    message "startup: Suffix \"$DNBASE\" already exists - DELETING"
    if DelSuffix "$DNBASE"; then
	:
    else 
	result FAIL
	return 0
    fi
fi

if AddSuffix "$DNBASE" $DNBE; then
    :
else
    message "startup: cannot AddSuffix \"$DNBASE\" $DNBE"    
    return 1
fi
AddIndex_Base $DNBE
AddIndex_DefaultList $DNBE

if RestartSlapd; then
    :
else
    message "startup: cannot restart slapd"
    return 1
fi

message "Import acivattr Specific Test Data"
$LDAPMODIFY -c -p $LDAPport -h "$LDAPhost" -D "$ROOTDN" -w $ROOTDNPW -a -f $IMPORTF

if [ $? -ne 0 ]; then
	result FAIL
	return 1
fi

if [ $? -ne 0 ];
then
    result FAIL
else
    result PASS
fi

return 0
}

# ------------------------------------
# Test cases
# ------------------------------------

######
tp1()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$REAL_EQ_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$ENG_MANAGER"
if [ $? -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}

######
tp2()
######
-------------------------------------------------------------------
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$REAL_EQ_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$SALES_MANAGER"
if [ $rc -eq 50 ]; then
	result PASS	
else 
	result FAIL
fi

}
---------------------------------------------------
######
tp3()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$REAL_PRES_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$ENG_MANAGER"
if [ $rc -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}

######
tp4()
######
---------------------------------------------------------
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$REAL_PRES_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$SALES_OU"
if [ $rc -eq 50 ]; then
	result PASS	
else 
	result FAIL
fi

}
-------------------------------------------------------
######
tp5()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$REAL_SUB_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$ENG_MANAGER"
if [ $rc -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}

######
tp6()
######
{

message "acivattr $tet_thistest"
replaceaci "$DNBASE" "$REAL_SUB_ACI"
addaci

Replace_Description "$ENG_USER" "secret12" "$SALES_MANAGER"
if [ $rc -eq 50 ]; then
	result PASS	
else 
	result FAIL
fi

}
----------------------------------------------------------------
######
tp100()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$ROLE_EQ_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$ENG_MANAGER"
if [ $? -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}
-------------------------------------------------------------------
######
tp101()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$ROLE_EQ_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$SALES_MANAGER"
if [ $rc -eq 50 ]; then
	result PASS	
else 
	result FAIL
fi

}
--------------------------------------------------------
######
tp102()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$ROLE_PRES_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$ENG_MANAGER"
if [ $rc -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}

######
tp103()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$ROLE_PRES_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$SALES_OU"
if [ $rc -eq 50 ]; then
	result PASS	
else 
	result FAIL
fi

}
---------------------------------------------------------------
######
tp104()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$ROLE_SUB_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$ENG_MANAGER"
if [ $rc -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}

######
tp105()
######
{

message "acivattr $tet_thistest"
replaceaci "$DNBASE" "$ROLE_SUB_ACI"
addaci

Replace_Description "$ENG_USER" "secret12" "$SALES_MANAGER"
if [ $rc -eq 50 ]; then
	result PASS	
else 
	result FAIL
fi

}
---------------------------------------------------------------
######
tp200()
######
{

message "acivattr $tet_thistest"
$LDAPSEARCH -p $LDAPport -h "$LDAPhost" -D "$ROOTDN" -w $ROOTDNPW -b "$DNBASE" objectclass=* nsrole
if [ $? -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}
----------------------------------------------------------------------------
######
tp300()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$COS_EQ_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$ENG_MANAGER"
if [ $? -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}

######
tp301()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$COS_EQ_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$SALES_MANAGER"
if [ $rc -eq 50 ]; then
	result PASS	
else 
	result FAIL
fi

}
-----------------------------------------------------
######
tp302()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$COS_PRES_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$ENG_MANAGER"
if [ $rc -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}

######
tp303()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$COS_PRES_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$SALES_OU"
if [ $rc -eq 50 ]; then
	result PASS	
else 
	result FAIL
fi

}
-------------------------------------------------
######
tp304()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$COS_SUB_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$ENG_MANAGER"
if [ $rc -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}

######
tp305()
######
{

message "acivattr $tet_thistest"
replaceaci "$DNBASE" "$COS_SUB_ACI"
addaci

Replace_Description "$ENG_USER" "secret12" "$SALES_MANAGER"
if [ $rc -eq 50 ]; then
	result PASS	
else 
	result FAIL
fi

}
--------------------------------------------------
######
tp400()
######
{

message "acivattr $tet_thistest"
setaci "$DNBASE" "$LDAPURL_ACI"
replaceaci

Replace_Description "$ENG_USER" "secret12" "$ENG_MANAGER"
if [ $rc -ne 0 ]; then
	result FAIL	
else 
	result PASS
fi

}

######
tp401()
######
{

message "acivattr $tet_thistest"
replaceaci "$DNBASE" "$LDAPURL_ACI"
addaci

Replace_Description "$SALES_USER" "secret12" "$SALES_MANAGER"
if [ $rc -eq 50 ]; then
	result PASS	
else 
	result FAIL
fi

}
######
cleanup()
######
{
----------------------------------------------------------
myresult=PASS
myreturn=0

message "Cleaning up DB and removing suffix"
tet_infoline "command is: $LDAPDELETE -c -f $DELETEF" 
$LDAPDELETE -h "$LDAPhost" -p $LDAPport -D "$ROOTDN" -w $ROOTDNPW -c -f $DELETEF
if [ $? -ne 0 ]; then
	message "acivattr cleanup: failed to delete data set"
	myresult=FAIL
	myreturn=1
fi

if DelSuffixWithIndexes "$DNBASE"; then
	:
else
	message "acivattr cleanup: failed to delete be"
	myresult=FAIL
	myreturn=1
fi

result $myresult
return $myreturn

}
 
. $TESTING_SHARED/DS/$VER/sh/appstates.sh
. $TESTING_SHARED/DS/$VER/sh/baselib.sh
. $TESTING_SHARED/DS/$VER/sh/applib.sh
. $TESTING_SHARED/DS/$VER/sh/acllib.sh
. $TET_ROOT/lib/sh/tcm.sh
