#!/bin/ksh
#
#ident	"@(#)nettest.stf	1.13	05/05/25 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This is the NETWORK TEST MODULE
#
#########################################################################
# NOTES ON OPERATION AND ERROR HANDLING:
# This script is called by staf.eng, which is the main test harness.
# Standard output gets recorded in the test module log file by staf.eng.          
#
# Variable maxwarn is checked by staf.eng in the "chk_err()" monitoring process,               
# by grep'ing the log file of this test module.
# If maxwarn is exceeded, the staf.eng script will kill this test module.
# There are two formats for the error echos to the log files;
#
# 1.   echo "!ERROR: blah blah foo bar";
#      which will terminate the test.  If the test harness detects
#      a string match with "!ERROR", then all testing terminates.
#
# 2.   echo "!WARNING: blah blah foo bar";
#       The above format "!WARNING:" will make the log file entry,
#       but all testing will continue, no termination of tests,
#       UNLESS maxwarnings variable is exceeded.
#
# DANGER!!:  Even if you do not intend to use WARNING messages, variable
# maxwarn *MUST* be defined and the "maxwarnings=" line *MUST* be
# echo'ed to the logfile.  This syntax is mandatory.
#########################################################################

# variables
prgdir=/opt/SUNWstaf/prgs	# program directory
iosize=$2              		# i/o MB size
outdir=                 	# output directory
hname=$(hostname)
l_path=/export/home/ftp/tmp/
r_path=tmp/
nfile1=net1.$hname.$$.hcts	# net file 1
nfile2=net2.$hname.$$.hcts	# net file 2
nfile3=net3.$hname.$$.hcts	# net file 3
nfile4=net4.$hname.$$.hcts	# net file 4
ftpin=ftpin.$hname.$$.hcts	# ftp input file
tmpmnt=mnt.$hname.$$.hcts	# temp mount point
rhost=$1			# remote hostname
err=				# network error
starttime=0
endtime=0

#maxwarn=80              # max number of nonfatal warnings to tolerate.

level=$(cat /tmp/level.dat)
# network interface
int=$(netstat -i | awk 'NR==3' | awk '{print $1}') 



# functions

clean_up_local()
{
	rm -f ${l_path}$nfile1 ${l_path}$nfile2 ${l_path}$nfile3 ${l_path}$nfile4
}

clean_up_remote()
{
	ssh $rhost "rm -f ${l_path}$nfile1 ${l_path}$nfile2 ${l_path}$nfile3 ${l_path}$nfile4 ${l_path}$ftpin; umount ${l_path}$tmpmnt; rm -rf ${l_path}$tmpmnt"
}

net_test()
{
	# test if client on net
	err=`/usr/sbin/ping $rhost 2>&1`
	if [ $? -ne 0 ]
	then
       	 	echo "!ERROR: Cannot ping -> $rhost\n$err"
		echo "!ERROR: Must have network test host to run."
		echo "!ERROR: Aborting HCTS test run."
        	exit 1
	fi
	# test for remote access
	err=`ssh $rhost "/bin/echo" 2>&1`
	if [ $? -ne 0 ]
	then 
       		echo "!ERROR: Cannot run programs on -> $rhost\n$err" 
		echo "!ERROR: Must have network test host to run."
		echo "!ERROR: Check permissions files hosts.equiv and .rhosts"
		echo "!ERROR: Aborting HCTS test run."
        	exit 1
	fi
}

scp_test()
{
	# create input file
	$prgdir/rwutil /dev/zero ${l_path}$nfile1 $iosize
	# check for errors
	if [ $? -ne 0 ]
	then
		echo "!ERROR: Network test module nettest.stf"
        	echo "!ERROR: Network write file to disk error -> ${l_path}$nfile1"
        	exit 1
	fi
	# copy file to client
	scp ${l_path}$nfile1 $rhost:${l_path}$nfile1 
	# check for errors
	if [ $? -ne 0 ]
	then
		echo "!ERROR: Network test module nettest.stf"
        	echo "!ERROR: Copy file to $rhost error -> ${l_path}$nfile1"
        	exit 1
	fi
	# copy file from client
	scp $rhost:${l_path}$nfile1 ${l_path}$nfile2
	# check for errors
	if [ $? -ne 0 ]
	then
		echo "!ERROR: Network test module nettest.stf"
        	echo "!ERROR: Copy file from $rhost error -> $nfile2"
        	exit 1
	fi
	# check sum
	diff ${l_path}$nfile1 ${l_path}$nfile2 >/dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo "!ERROR: Network test module nettest.stf"
        	echo "!ERROR: Bad scp check sum between ${l_path}$nfile1 and ${l_path}$nfile2."
        	exit 1
	fi
	# clean up files
	clean_up_local
}

chk_sums()
{
	# check sum 1 and 2
	diff ${l_path}$nfile1 ${l_path}$nfile2 >/dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo "!ERROR: Network test module nettest.stf"
        	echo "!ERROR: Bad $1 check sum between ${l_path}$nfile1 and ${l_path}$nfile2."
       		exit 1
	fi
	# check sum 1 and 3
	diff ${l_path}$nfile1 ${l_path}$nfile3 >/dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo "!ERROR: Network test module nettest.stf"
        	echo "!ERROR: Bad $1 check sum between ${l_path}$nfile1 and ${l_path}$nfile3."
       		exit 1
	fi
	# check sum 1 and 4
	diff ${l_path}$nfile1 ${l_path}$nfile4 >/dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo "!ERROR: Network test module nettest.stf"
        	echo "!ERROR: Bad $1 check sum between ${l_path}$nfile1 and ${l_path}$nfile4."
       		exit 1
	fi
}

ftp_test()
{
# create ftp input file
cat << EOF > ${l_path}$ftpin 2>/dev/null
user anonymous anonymous
pwd
bin
put ${l_path}$nfile1 ${r_path}$nfile1
put ${l_path}$nfile1 ${r_path}$nfile2
get ${r_path}$nfile1 ${l_path}$nfile2
put ${l_path}$nfile1 ${r_path}$nfile3
get ${r_path}$nfile2 ${l_path}$nfile3
put ${l_path}$nfile1 ${r_path}$nfile4
get ${r_path}$nfile3 ${l_path}$nfile4
get ${r_path}$nfile4 ${l_path}$nfile1
bye
EOF
	# copy and remove local ftp input
	scp ${l_path}$ftpin $rhost:${l_path}$ftpin
	rm -f ${l_path}$ftpin
	# do ftp
	ssh $rhost "ftp -n $hname < ${l_path}$ftpin"
	# check for errors
	if [ $? -ne 0 ]
	then
        	echo "!ERROR: Net test: Bad FTP on $rhost."
        	exit 1
	fi
	# do check sums
	chk_sums FTP
	# clean up files
	clean_up_local
}

nfs_test()
{
	# make mount point
	ssh $rhost "mkdir ${l_path}$tmpmnt; mount $hname:/tmp ${l_path}$tmpmnt;
	cp ${l_path}$nfile1 ${l_path}$tmpmnt; cp ${l_path}$nfile2 ${l_path}$tmpmnt; cp ${l_path}$nfile3 ${l_path}$tmpmnt;
	cp ${l_path}$nfile4 ${l_path}$tmpmnt;"
	mv /tmp/$nfile1 ${l_path}$nfile1; mv /tmp/$nfile2 ${l_path}$nfile2; mv /tmp/$nfile3 ${l_path}$nfile3; mv /tmp/$nfile4 ${l_path}$nfile4;
	# do check sums
	chk_sums NFS
	# clean up files
	clean_up_local
	clean_up_remote
}

spray_test()
{
	sleep 10
	# test system spray client
	err=$(/usr/sbin/spray $rhost -l 512 -c 250 -d 5000 2>&1) 
	# check for errors
	if [ $? -ne 0 ]
	then
        	echo "!ERROR: Problem with spray:$err"
	fi
	sleep 10
	# client spray test system
	err=$(ssh $rhost "/usr/sbin/spray $hname -l 512 -c 250 -d 5000" 2>&1) 
	# check for errors
	if [ $? -ne 0 ]
	then

        	echo "!ERROR: Problem with spray from remote machine:$err"
	fi
}


### MAIN ###

# SET TRAP
trap 'clean_up_local; clean_up_remote; exit' 17


# CHECK ARGS
if [ $# -lt 1 ]
then
        echo "!ERROR: $0 [client hostname] [[I/O MB size]]"
	echo "!ERROR: $0 called with bad arguments."
        exit 1
fi

# CHECK IOSIZE
if [ -z "$iosize" ]
then
        iosize=1
fi

#Added for firstly checking the validity of network connection. 
net_test

cd nettest/bin

sleep $RUNTIME &
sleepid=$!

while [ -d /proc/$sleepid ]
do
	starttime=`../../../../prgs/time_sec`

	echo "Starting Network test..."
        ./nettest -v -o dev=$int,target=$1,packets=8192

        if [ $? -ne 0 ]
        then
                echo "!ERROR during nettest"
		exit 1
        fi

	if [ "$level" == "L2" ]
	then

		# network test
		net_test 

		# remote copy test
		scp_test

		# FTP test
		ftp_test

		# NFS test
		nfs_test

		# spray test
		spray_test
	fi

	endtime=`../../../../prgs/time_sec`

	ltime=`expr $endtime - $starttime`
	if [ $ltime -gt 300 ]
	then
		echo LOOPTIME: $ltime
	fi
done
echo "!PASS: Network test completed"
