Index: trunk/test/testrun_2.sh
===================================================================
--- trunk/test/testrun_2.sh	(revision 1)
+++ trunk/test/testrun_2.sh	(revision 19)
@@ -1,66 +1,249 @@
 #! /bin/sh
 
-
-# The following two are the ANSI sequences for start and end embolden
-case $TERM in
-vt*|ansi*|con*|xterm*|linux*|screen*)
-  S=[1m
-  E=[m
-  ;;
-*)
-  S=
-  E=
-  ;;
-esac
-
-PW_DIR=`pwd`
-
-if test x$UID != x; then
-  TRUST="--with-trusted=0,2,$UID"
-else
-  TRUST="--with-trusted=0,2"
-fi
-
-echo; echo "${S}__ STARTING TEST RUN CLIENT/SERVER __${E}"; echo;
-echo Working directory: $PW_DIR
-
-MAKE=`which gmake`
-if test "x$?" = x1 ; then
-    MAKE="make -s"
-else
-    MAKE=`which gmake | sed -e "s%\([a-z:]\) .*%\1%g"` 
-    if test "x$MAKE" = x; then
-	MAKE="make -s"
-    elif test "x$MAKE" = xno; then
-	MAKE="make -s"
-    else
-	if test "x$MAKE" = "xwhich:"; then
-		MAKE="make -s"
-	else
-		MAKE="gmake -s"
-		gmake -v >/dev/null 2>&1 || MAKE="make -s"
-	fi
-    fi
-fi
-
-echo MAKE is $MAKE
-echo
-
-# --with-database=postgresql
-# export MYLIB=-L/usr/local/pgsql/lib
-# export MYINC=-I/usr/local/pgsql/include
-# export LD_LIBRARY_PATH=/usr/local/pgsql/lib
-
-# --with-database=mysql
-# export MYLIB=-L/usr/local/mysql/lib
-# export MYINC=-I/usr/local/mysql/include
-# export LD_LIBRARY_PATH=/usr/local/mysql/lib
-
-testrun2 ()
+LOGFILE="$PW_DIR/.samhain_log"; export LOGFILE
+RCFILE="$PW_DIR/testrc_2";  export RCFILE
+
+
+do_test_1 () {
+
+	[ -z "$verbose" ] || { 
+	    echo; 
+	    echo "${S}Start Server${E}: ./yule -l info -p none &"; 
+	    echo; 
+	}
+	./yule -l info -p none >/dev/null 2>&1 &
+	PROC_Y=$!
+	sleep 5
+
+	[ -z "$verbose" ] || { 
+	    echo; 
+	    echo "${S}Start Client${E}: ./samhain.new -l none -p none -e info -t check"; 
+	    echo; 
+	}
+
+	./samhain.new -t check -p none -l none -e info --bind-address=127.0.0.1 >/dev/null 2>&1
+	if test x$? = x0; then
+	    [ -z "$verbose" ] || log_msg_ok    "samhain.new -t check";
+	else
+	    [ -z "$quiet" ]   && log_msg_fail  "samhain.new -t check";
+	    kill $PROC_Y
+	    return 1
+	fi
+
+	kill $PROC_Y
+	sleep 5
+
+	egrep "START(>|\").*Yule(>|\")" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Server start";
+	    return 1
+	fi
+	egrep "NEW CLIENT" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Client connect";
+	    return 1
+	fi
+	egrep "remote_host.*Checking.*/bin" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Client file check";
+	    return 1
+	fi
+	egrep "remote_host.*EXIT.*Samhain" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Client exit";
+	    return 1
+	fi
+	egrep "EXIT.*Yule.*SIGTERM" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Server exit";
+	    return 1
+	fi
+	
+
+	return 0
+}
+
+do_test_2 () {
+
+        ORIGINAL="UseSeparateLogs=no"
+	REPLACEMENT="UseSeparateLogs=yes"
+        ex $RCFILE <<EOF
+:%s/$ORIGINAL/$REPLACEMENT/g
+:wq
+EOF
+# :%s is the "ex" substitution command.
+# :wq is write-and-quit.
+	[ -z "$verbose" ] || { 
+	    echo; 
+	    echo "${S}Start Server${E}: ./yule -l info -p none &"; 
+	    echo; 
+	}
+
+	rm -f $LOGFILE
+
+	./yule -l info -p none >/dev/null 2>&1 &
+	PROC_Y=$!
+	sleep 5
+
+	[ -z "$verbose" ] || { 
+	    echo; 
+	    echo "${S}Start Client${E}: ./samhain.new -l none -p none -e info -t check"; 
+	    echo; 
+	}
+
+	./samhain.new -t check -p none -l none -e info --bind-address=127.0.0.1 >/dev/null 2>&1
+	if test x$? = x0; then
+	    [ -z "$verbose" ] || log_msg_ok    "samhain.new -t check";
+	else
+	    [ -z "$quiet" ]   && log_msg_fail  "samhain.new -t check";
+	    kill $PROC_Y
+	    return 1
+	fi
+
+	kill $PROC_Y
+	sleep 5
+
+	if [ -f ${LOGFILE}.${SH_LOCALHOST} ]; then
+	    remhost=${SH_LOCALHOST}
+	else
+	    remhost=`echo $SH_LOCALHOST | sed 's,\..*,,'`
+	fi
+	if [ -f ${LOGFILE}.${remhost} ]; then
+	    CLIENTLOG="${LOGFILE}.${remhost}"
+	else
+	    CLIENTLOG=`ls -1 ${LOGFILE}.* | tail -n 1`
+	fi
+
+	egrep "START(>|\").*Yule(>|\")" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Server start";
+	    return 1
+	fi
+	egrep "NEW CLIENT" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Client connect";
+	    return 1
+	fi
+	egrep "remote_host.*Checking.*/bin" ${CLIENTLOG} >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Client file check";
+	    return 1
+	fi
+	egrep "remote_host.*EXIT.*Samhain" ${CLIENTLOG} >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Client exit";
+	    return 1
+	fi
+	egrep "EXIT.*Yule.*SIGTERM" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Server exit";
+	    return 1
+	fi
+	
+	rm -f ${LOGFILE}.${remhost}
+	return 0
+}
+
+do_test_3 () {
+
+        ORIGINAL_1="ExportSeverity=none"
+        REPLACEMENT_1="ExportSeverity=mark"
+	ORIGINAL_2="UseSeparateLogs=yes"
+	REPLACEMENT_2="UseSeparateLogs=no"
+	ORIGINAL_3="LogSeverity=none"
+	REPLACEMENT_3="LogSeverity=debug"
+	ORIGINAL_4="# SetClientTimeLimit=1800"
+	REPLACEMENT_4="SetClientTimeLimit=20"
+        ex $RCFILE <<EOF
+:%s/${ORIGINAL_1}/${REPLACEMENT_1}/g
+:%s/${ORIGINAL_2}/${REPLACEMENT_2}/g
+:%s/${ORIGINAL_3}/${REPLACEMENT_3}/g
+:%s/${ORIGINAL_4}/${REPLACEMENT_4}/g
+:wq
+EOF
+# :%s is the "ex" substitution command.
+# :wq is write-and-quit.
+	[ -z "$verbose" ] || { 
+	    echo; 
+	    echo "${S}Start Server${E}: ./yule -p none &"; 
+	    echo; 
+	}
+
+	rm -f $LOGFILE
+
+	./yule -p none >/dev/null 2>&1 &
+	PROC_Y=$!
+	sleep 5
+
+	[ -z "$verbose" ] || { 
+	    echo; 
+	    echo "${S}Start Client${E}: ./samhain.new -l none -p none -t check"; 
+	    echo; 
+	}
+
+	./samhain.new -t check -p none -l none --forever --bind-address=127.0.0.1 >/dev/null 2>&1 &
+	if test x$? = x0; then
+	    PROC_S=$!
+	    # echo "PID is ${PROC_S}"
+	    [ -z "$verbose" ] || log_msg_ok    "samhain.new -t check";
+	    sleep 5
+	    # Redirect the shells (un-)helpful job monitoring messages.
+	    # The 'disown' buildin is not portable. 
+	    { kill -9 ${PROC_S}; sleep 40; } >/dev/null 2>&1
+	else
+	    [ -z "$quiet" ]   && log_msg_fail  "samhain.new -t check";
+	    kill $PROC_Y
+	    return 1
+	fi
+
+	if [ -t 0 ]; then
+	    # enable monitor mode again if interactive
+	    set -m
+	fi
+
+
+	kill $PROC_Y
+	sleep 5
+
+	egrep "START(>|\").*Yule(>|\")" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Server start";
+	    return 1
+	fi
+	egrep "NEW CLIENT" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Client connect";
+	    return 1
+	fi
+	egrep "remote_host.*File check completed.*" ${LOGFILE} >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Client file check";
+	    return 1
+	fi
+	egrep "Time limit exceeded" ${LOGFILE} >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Client dead detection";
+	    return 1
+	fi
+	egrep "EXIT.*Yule.*SIGTERM" $LOGFILE >/dev/null 2>&1
+	if [ $? -ne 0 ]; then
+	    [ -z "$verbose" ] || log_msg_fail "Server exit";
+	    return 1
+	fi
+	
+	rm -f ${LOGFILE}.${remhost}
+	return 0
+}
+
+testrun2_internal ()
 {
-	#
-	# test standalone compilation
-	#
-	echo "${S}Building client and server${E}"; echo;
+        [ -z "$verbose" ] || { 
+	    echo; 
+	    echo Working directory: $PW_DIR; echo MAKE is $MAKE; 
+	    echo; 
+	}
+	#
+	#
+	[ -z "$verbose" ] || { echo; echo "${S}Building client and server${E}"; echo; }
 	#
 	if test -r "Makefile"; then
@@ -68,48 +251,47 @@
 	fi
 	#
-	${TOP_SRCDIR}/configure --quiet  $TRUST --enable-network=client  --enable-xml-log --enable-login-watch --prefix=$PW_DIR --localstatedir=$PW_DIR --with-config-file=$PW_DIR/testrc_2  --with-log-file=$PW_DIR/.samhain_log --with-pid-file=$PW_DIR/.samhain_lock --with-data-file=$PW_DIR/.samhain_file --enable-encrypt=2
-	#
-	if test x$? = x0; then
-		echo "configure completed, exit status 0"; echo;
-		$MAKE  > /dev/null
+	${TOP_SRCDIR}/configure --quiet  $TRUST --enable-debug --enable-network=client  --enable-xml-log --enable-login-watch --prefix=$PW_DIR --localstatedir=$PW_DIR --with-config-file=$RCFILE  --with-log-file=$LOGFILE --with-pid-file=$PW_DIR/.samhain_lock --with-data-file=$PW_DIR/.samhain_file --enable-encrypt=2
+	#
+	if test x$? = x0; then
+		[ -z "$verbose" ] ||     log_msg_ok "configure..."; 
+		$MAKE  > /dev/null 2>>test_log
 		if test x$? = x0; then
-		    echo "$MAKE completed, exit status $?"; echo;
+		    [ -z "$verbose" ] || log_msg_ok "make..."; 
 		else
-		    echo "${S}ERROR:${E} make failed, exit status $?"; echo;
-		    exit
+		    [ -z "$quiet" ] &&   log_msg_fail "make..."; 
+		    return 1
 		fi
 
 	else
-		echo "${S}ERROR:${E} configure failed, exit status $?"; echo;
-		exit
+		[ -z "$quiet" ] &&       log_msg_fail "configure...";
+		return 1
 	fi
 
 	# save binary and build server
 	#
-	cp samhain samhain.build
-	make clean
-
-	${TOP_SRCDIR}/configure --quiet  $TRUST --enable-debug --enable-network=server  --enable-xml-log --enable-login-watch --prefix=$PW_DIR --localstatedir=$PW_DIR --with-config-file=$PW_DIR/testrc_2  --with-log-file=$PW_DIR/.samhain_log --with-pid-file=$PW_DIR/.samhain_lock --with-data-file=$PW_DIR/.samhain_file --enable-identity=rainer --enable-encrypt=2
-	#
-	if test x$? = x0; then
-		echo "configure completed, exit status 0"; echo;
-		$MAKE  > /dev/null
+	cp samhain samhain.build || return 1
+	make clean >/dev/null || return 1
+
+	${TOP_SRCDIR}/configure --quiet  $TRUST --enable-debug --enable-network=server  --enable-xml-log --enable-login-watch --prefix=$PW_DIR --localstatedir=$PW_DIR --with-config-file=$RCFILE  --with-log-file=$LOGFILE --with-pid-file=$PW_DIR/.samhain_lock --with-data-file=$PW_DIR/.samhain_file --enable-identity=rainer --enable-encrypt=2
+	#
+	if test x$? = x0; then
+		[ -z "$verbose" ] ||     log_msg_ok "configure..."; 
+		$MAKE  > /dev/null 2>>test_log
 		if test x$? = x0; then
-		    echo "$MAKE completed, exit status $?"; echo;
+		    [ -z "$verbose" ] || log_msg_ok "make..."; 
 		else
-		    echo "${S}ERROR:${E} make failed, exit status $?"; echo;
-		    exit
+		    [ -z "$quiet" ] &&   log_msg_fail "make..."; 
+		    return 1
 		fi
 
 	else
-		echo "${S}ERROR:${E} configure failed, exit status $?"; echo;
-		exit
-	fi
+		[ -z "$quiet" ] &&       log_msg_fail "configure...";
+		return 1
+	fi
+
 
 	#####################################################################
 	#
 	#
-	echo "${S}Initialize database${E}"; echo;
-
 	rm -f ./.samhain_file
 	rm -f ./.samhain_log
@@ -118,8 +300,12 @@
 	cp ${SCRIPTDIR}/testrc_2.in testrc_2
 
-	echo "./samhain.build -t init"
-	./samhain.build -t init
-
-	SHINI=$?
+	./samhain.build -t init -p none
+
+	if test x$? = x0; then
+	    [ -z "$verbose" ] || log_msg_ok    "init...";
+	else
+	    [ -z "$quiet" ]   && log_msg_fail  "init...";
+	    return 1
+	fi
 
 	# Create a password
@@ -127,14 +313,20 @@
 	SHPW=`./yule -G`
 	if test x"$SHPW" = x; then
-	    echo "password not generated -- aborting"
-	    exit 1
+	    [ -z "$quiet" ]   && log_msg_fail  "password not generated -- aborting"
+	    return 1
 	fi
 
 	# Set in client
 
-	echo "./samhain_setpwd samhain.build new $SHPW"
-	./samhain_setpwd samhain.build new $SHPW
-
-	mv samhain.build.new  samhain.new
+	./samhain_setpwd samhain.build new $SHPW >/dev/null
+
+	if test x$? = x0; then
+	    [ -z "$verbose" ] || log_msg_ok    "./samhain_setpwd samhain.build new $SHPW";
+	else
+	    [ -z "$quiet" ]   && log_msg_fail  "./samhain_setpwd samhain.build new $SHPW";
+	    return 1
+	fi
+
+	mv samhain.build.new  samhain.new || return 1
 
 	rm -f ./.samhain_log.*
@@ -143,48 +335,54 @@
 	SHCLT=`./yule -P $SHPW | sed s%HOSTNAME%${SH_LOCALHOST}%`
 
+	if test x$? = x0; then
+	    [ -z "$verbose" ] || log_msg_ok    "yule -P $SHPW";
+	else
+	    [ -z "$quiet" ]   && log_msg_fail  "yule -P $SHPW";
+	    return 1
+	fi
+
  	echo $SHCLT >> testrc_2
 
-	if test x$SHINI = x0; then
-		echo; echo "${S}Start Server${E}: ./yule  &"; echo;
-		./yule -l info &
-		PROC_Y=$!
-		sleep 5
-
-		echo; echo "${S}Start Client${E}: ./samhain.new -l none -p none -e info -t check";
-
-		./samhain.new -t check -p none -l none -e info
-		if test x$? = x0; then
-		    echo "${S}check completed${E}"
-		else
-		    echo "${S}ERROR:${E} -- check -- failed, exit status $?"
-		    echo
-		    kill $PROC_Y
-		    exit
-		fi
-
-		kill $PROC_Y
-		sleep 5
-	else
-		echo "${S}ERROR:${E} -- init -- failed, exit status $SHINI"; 
-		echo;
-		exit
-	fi
-	    
-}
-
-    if test x$1 = x; then
-	echo
-	echo "Please provide the local hostname as argument."
-	echo
-	exit 8
+	do_test_1
+	if [ $? -eq 0 ]; then
+	    [ -z "$quiet" ] && log_ok   1 ${MAXTEST} "Client logging";
+	else
+	    [ -z "$quiet" ] && log_fail 1 ${MAXTEST} "Client logging";
+	fi
+
+	do_test_2
+	if [ $? -eq 0 ]; then
+	    [ -z "$quiet" ] && log_ok   2 ${MAXTEST} "Client logging, seperate logfiles";
+	else
+	    [ -z "$quiet" ] && log_fail 2 ${MAXTEST} "Client logging, seperate logfiles";
+	fi
+
+	do_test_3
+	if [ $? -eq 0 ]; then
+	    [ -z "$quiet" ] && log_ok   3 ${MAXTEST} "Dead client detection";
+	else
+	    [ -z "$quiet" ] && log_fail 3 ${MAXTEST} "Dead client detection";
+	fi
+
+	return $?
+}
+
+MAXTEST=3; export MAXTEST
+
+testrun2 ()
+{
+    log_start "RUN CLIENT/SERVER"
+
+    if [ x"$1" = x ]; then
+	[ -z "$quiet" ] && log_msg_fail "Missing hostname"
     fi
-
+    #
     SH_LOCALHOST=$1; export SH_LOCALHOST
-
-    testrun2
-
-
-echo; echo "${S}__ END TEST RUN CLIENT/SERVER __${E}"; echo;
-
-exit
-
+    #
+    testrun2_internal
+    #
+    log_end "RUN CLIENT/SERVER"
+
+    return 0
+}
+
