#! /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   : mc_opts
#
#  PURPOSE: To verify that three of the new options for level IPPROTO_IP
#           Service Interface are initially set to the default values as
#           defined in the documentation and that each of the new options
#           can be set and read properly by the setsockopt and getsockopt
#           routines, respectively.  To test boundary conditions and to 
#           generate errors while exercising the IP Multicast Service 
#           Interface options.
#
#  HISTORY:
#    03/26/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Ported
#
#******************************************************************************
#Uncomment the line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

if [ -z $LTPBIN ]; then
  PATH=../../../bin/:./:$PATH
fi
TC=mc_opts
TCsrc=${TCsrc:-`pwd`}
TCtmp=${TCtmp:-$TCsrc/$TC$$}
CLEANUP=${CLEANUP:-ON}
EXECUTABLES=${EXECUTABLES:-"opts opts_e"}
NUMLOOPS=${NUMLOOPS:-10}
IPADDR=${IPADDR:-$(gethost `hostname`| grep address |awk '{ print $2 }')}

this_file=${0##*/}
trap "interrupt_test" 2

setup()
{
  mkdir -p $TCtmp
  for i in $EXECUTABLES
  do
    cp $TCsrc/$i $TCtmp
    chmod 755 $TCtmp/$i
  done
}

#-----------------------------------------------------------------------
#
# FUNCTION:  do_test
# PURPOSE:   Executes the testcases.
# INPUT:     Number of iterations
# OUTPUT:    Error messages are logged when any verification test
#            fails.
#
#-----------------------------------------------------------------------

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

   COUNT=1
   while [ $COUNT -le $NUMLOOPS ]
   do
      # Run setsockopt/getsockopt test
      echo "Running mc_$EXECUTABLES on $IPADDR"
      for i in $EXECUTABLES
      do
        $TCtmp/$i $IPADDR 
        [ $? = 0 ] || end_testcase "$i $IPADDR failed"
      done

      echo "Running ping with bad values"
      ping -T 777 224.0.0.1 > /dev/null 2>&1
      [ $? = 0 ] && end_testcase "Multicast range should be out of range"

      echo "Running ping on a invalid interface!"
      ping -I 3.3.3.3 224.0.0.1 > /dev/null 2>&1
      [ $? = 0 ] && end_testcase "ping on bogus interface should fail"

      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
   echo "$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
   echo "$this_file: doing $0."

   # Call other cleanup functions
   [ $CLEANUP = "ON" ] && do_cleanup

   [ $# = 0 ] && { echo "Test Successful"; exit 0; }
   echo "Test Failed: $@"
   exit 1
}  

#******************************************************************************
#
# FUNCTION:  interrupt_test
# PURPOSE:   Handle process interrupts set by trap.
# INPUT:     none
# OUTPUT:    none
#
#******************************************************************************

interrupt_test()
{
   echo "test interrupted"
   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
#
#******************************************************************************
setup
do_test
end_testcase
