#!/bin/sh
#
# Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
# (Royal Institute of Technology, Stockholm, Sweden). 
# All rights reserved. 
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met: 
#
# 1. Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer. 
#
# 2. Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the distribution. 
#
# 3. Neither the name of the Institute nor the names of its contributors 
#    may be used to endorse or promote products derived from this software 
#    without specific prior written permission. 
#
# THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
# SUCH DAMAGE. 

env_setup="@env_setup@"
objdir="@objdir@"
srcdir="@srcdir@"

testfailed="echo test failed; cat messages.log; exit 1"

. ${env_setup}

# If there is no useful db support compiled in, disable test
${have_db} || exit 77

R=TEST.H5L.SE

uuspid=

port=@port@

kadmin="${TESTS_ENVIRONMENT} ../../kadmin/kadmin -l -A -r $R"
kdc="${TESTS_ENVIRONMENT} ../../kdc/kdc --addresses=localhost -P $port"

cache1="FILE:${objdir}/cache1.krb5"
cache2="FILE:${objdir}/cache2.krb5"

kinit1="${TESTS_ENVIRONMENT} ../../kuser/kinit -c $cache1 ${afs_no_afslog}"
kinit2="${TESTS_ENVIRONMENT} ../../kuser/kinit -c $cache2 ${afs_no_afslog}"
kdestroy1="${TESTS_ENVIRONMENT} ../../kuser/kdestroy -c $cache1 ${afs_no_unlog}"
kdestroy2="${TESTS_ENVIRONMENT} ../../kuser/kdestroy -c $cache2 ${afs_no_unlog}"
uu_server="${TESTS_ENVIRONMENT} ../../appl/test/uu_server"
uu_client="${TESTS_ENVIRONMENT} ../../appl/test/uu_client"

KRB5_CONFIG="${objdir}/krb5.conf"
export KRB5_CONFIG

rm -f current-db*
rm -f out-*
rm -f mkey.file*

> messages.log

echo Creating database
${kadmin} <<EOF || exit 1
init --realm-max-ticket-life=1day --realm-max-renewable-life=1month ${R}
add -p foo --use-defaults user1@${R}
add -p foo --use-defaults user2@${R}
check ${R}
EOF

echo foo > ${objdir}/foopassword

echo Starting kdc ; > messages.log
${kdc} --detach --testing || { echo "kdc failed to start"; cat messages.log; exit 1; }
kdcpid=`getpid kdc`

cleanup() {
    echo signal killing kdc
    kill -9 ${kdcpid} ${uuspid} 2>/dev/null
    trap '' EXIT INT TERM
    cat messages.log
    exit 1
}
trap cleanup EXIT INT TERM

ec=0

echo "Getting client initial tickets user1"; > messages.log
${kinit1} --password-file=${objdir}/foopassword user1@$R || \
	{ ec=1 ; eval "${testfailed}"; }

echo "Getting client initial tickets user2"; > messages.log
${kinit2} --password-file=${objdir}/foopassword user2@$R || \
	{ ec=1 ; eval "${testfailed}"; }


echo "starting uu server (using user1)"
KRB5CCNAME=$cache1 ${uu_server} > uuserver.log &
uuspid=$!
sleep 5

echo "trying to contact server with client (using user2)"
KRB5CCNAME=$cache2 ${uu_client} localhost > messages.log 2>&1 || \
    { ec=1; eval "${testfailed}"; }

sleep 5

echo "checking if server got the right message"
cmp uuserver.log ${srcdir}/uuserver.txt || \
    { ec=1; eval "${testfailed}"; }

uuspid=""

${kdestroy1}
${kdestroy2}

echo "killing kdc uu_server (${kdcpid} ${uuspid})"
sh ${leaks_kill} kdc $kdcpid || exit 1


trap "" EXIT

exit $ec
