#!/bin/sh
# start of an automatic testing script

DOPLUTO=dopluto
DOWHACK=dowhack

DFAIL=""
XFAIL=""
SFAIL=""
REFMISSING=""
CFAIL=""

# slow tests: failure takes time.  Not automatically run.
# 	ipsec-wk-rsa
#	ipsec-wk-dnsrsa
# tests requiring ADNS delay impairments (slow):
#	ipsec-oppo-race
#	ipsec-oppo-race-rini-net
#	ipsec-oppo-race-iinr-net
# oddballs:
#	stipple-parallel stipple-serial

case "$#" in
0)	set - 	isakmp-psk isakmp-rsa isakmp-rsa-case \
		ipsec-psk ipsec-rsa \
		ipsec-rsa-time-neg ipsec-rsa-time-trunc \
		ipsec-rsa-time-neg-dontrekey ipsec-rsa-time-trunc-dontrekey \
		ipsec-rsa-delete ipsec-rsa-c \
		ipsec-psk-rw ipsec-psk-id-rw ipsec-rsa-rw \
		isakmp-dnsrsa isakmp-dnsrsa-case \
		ipsec-dnsrsa ipsec-dnsrsa-delete ipsec-dnsrsa-c \
		ipsec-dnsrsa-rw \
		ipsec-oppo ipsec-oppo-seq ipsec-oppo-twice
	;;
esac

for t
do
    echo $t:
    case "$t" in
    shutdown)
    	$DOWHACK shutdown
	;;
    *)
	LD=log/$t
	[ -d "$LD" ] || mkdir -p "$LD"

	# start Initiator pluto (daemon forks to return control)
	$DOPLUTO west >$LD/pi-log 2>&1

	# start Responder pluto (daemon forks to return control)
	$DOPLUTO east >$LD/pr-log 2>&1

	(

	    $DOWHACK d$t || DFAIL="$DFAIL $t $?"

	    case "$t" in
	    *-rsa*) $DOWHACK kall ;;
	    esac

	    $DOWHACK listen

	    $DOWHACK x$t || XFAIL="$XFAIL $t $?"

	    $DOWHACK shutdown || SFAIL="$SFAIL $t $?"
	) >$LD/wi-log

	if [ -f log.ref/$t/wi-log ]
	then
		cmp log.ref/$t/wi-log $LD/wi-log || CFAIL="$CFAIL $t $?"
	else
		REFMISSING="$REFMISSING $t"
	fi
	;;
    esac
done

[ -z "$DFAIL" ] || echo "definition failures:$DFAIL" >&2
[ -z "$XFAIL" ] || echo "execution failures:$XFAIL" >&2
[ -z "$SFAIL" ] || echo "shutdown failures:$SFAIL" >&2
[ -z "$REFMISSING" ] || echo "no reference logs:$REFMISSING" >&2
[ -z "$CFAIL" ] || echo "cmp failures:$CFAIL" >&2
