#! /bin/sh
unset LIBPATH
#
#   Copyright (c) International Business Machines  Corp., 2000
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this pronram;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
#  FILE   : perf_lan
#
#  PURPOSE: Generates LAN traffic using ICMP tst_resm TINFO packets.
#           
#
#  SETUP: The home directory of root on the machine exported as "RHOST"
#         MUST have a ".rhosts" file with the hostname of the client 
#         machine, where the test is executed. 
#
#  HISTORY:
#    06/06/03 Manoj Iyer manjo@mail.utexas.edu
#      - Modified testcase to use Test API and fixed bugs.
#    03/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Ported
#
#
#***********************************************************************
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

TC=slan
RHOST=${RHOST:-`hostname`}
TCbin=${TCbin:-`pwd`}
TCsrc=${TCsrc:-$TCbin}
TCtmp=${TCtmp:-$TCsrc/perf_lan$$}
CLEANUP=${CLEANUP:-ON}

PACKETSIZE=${PACKETSIZE:-512}
NUMPACKETS=${NUMPACKETS:-1}
NUMLOOPS=${NUMLOOPS:-10}
EXECUTABLES="pingpong"

trap "interrupt_testcase" 2 15 16 30

do_setup()
{
  export TCID="perf_lan"
  export TST_TOTAL=1
  export TST_COUNT=1
  mkdir -p $TCtmp
}

#-----------------------------------------------------------------------
#
# FUNCTION:  do_test
# INPUT:     None.
# OUTPUT:    Informational messages are logged into the run log.
#
#-----------------------------------------------------------------------

do_test() 
{
   $trace_logic
   tst_resm TINFO "$this_file: doing $0."

   TLOOP=1
   COUNT=1
   [ $NUMLOOPS -gt 0 ] || TLOOP=$NUMLOOPS
   while [ $TLOOP -le $NUMLOOPS ]
   do
      $TCbin/$EXECUTABLES $RHOST $PACKETSIZE $NUMPACKETS
      RETVALUE=$?

      case "$RETVALUE" in
         0) tst_resm TINFO "Successful execution for loop $COUNT" ;;
         1) end_testcase "Error trying to send to host" ;;
         2) end_testcase "Error - Data length is too long";;
         3) end_testcase "Usage error ";;
         5) end_testcase "Error - Socket setting" ;;
         6) end_testcase "Error - Pipe broken";;
         7) end_testcase "Error - Fork fail";;
         10)end_testcase "Error - wrong Protocol" ;;
         *) end_testcase "Return code from $EXECUTABLES is $RETVALUE" ;;
      esac
      if [ $NUMLOOPS -gt 0 ] ; then TLOOP=$(( $TLOOP + 1 )); fi
      COUNT=$(( $COUNT + 1 ))
   done
}

#-----------------------------------------------------------------------
#
# FUNCTION:     do_cleanup
# PURPOSE:      Called when the testcase is interrupted by the user
#               or by interrupt_testcase() when time limit expired
# INPUT:        None.
# OUTPUT:       None.
#
#-----------------------------------------------------------------------

do_cleanup()
{
   $trace_logic
   tst_resm TINFO "$this_file: doing $0."

   cd /
   if [ $TCtmp != $TCsrc ]; then
      rm -rf $TCtmp
   fi
}

#=============================================================================
# FUNCTION NAME:        end_testcase
# 
# FUNCTION DESCRIPTION: Clean up
# 
# PARAMETERS:           string, IF AND ONLY IF the testcase fails
# 
# RETURNS:              None.
#=============================================================================

end_testcase()
{  
   $trace_logic
   tst_resm TINFO "$this_file: doing $0."
   
   # Call other cleanup functions
   [ $CLEANUP = "ON" ] && do_cleanup
   
   [ $# -eq 0 ] && { tst_resm TPASS "Test Successful"; exit 0; }
   tst_resm TFAIL "Test Failed: $@"
   exit 1
}

#-----------------------------------------------------------------------
#
# FUNCTION:     interrupt_testcase
# PURPOSE:      Finish up the testcase - called when the time has expired
# INPUT:        None.
# OUTPUT:       None.
#
#-----------------------------------------------------------------------

interrupt_testcase()
{
   $trace_logic
   tst_resm TINFO "$this_file: doing $0."

   tst_resm TINFO "Testcase interrupted - cleaning up"
   tst_resm TINFO "Killing background process if it is still running"
   kill -9 $CHILDPID
   end_testcase
}

#-----------------------------------------------------------------------
#
# FUNCTION:  MAIN
# PURPOSE:   To invoke functions that perform the tasks as described in
#	     the design in the prolog above.
# INPUT:     See SETUP in the prolog above.
# OUTPUT:    Logged run results written to testcase run log
#
#-----------------------------------------------------------------------
do_setup
do_test
end_testcase
