#!/bin/sh
#
#   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   : finger
#
#  PURPOSE: To test the basic functionality of the `finger` command.
#
#  SETUP: The home directory of root on the machine exported as "RHOST"
#         MUST have a ".rhosts" file with the hostname of the machine
#         where the test is executed. Also, both machines MUST have
#         the fingerd daemon active (see inetd or xinetd config file). 
#
#  HISTORY:
#    06/06/03 Manoj Iyer - manjo@mail.utexas.edu
#      - Ported tests to use test macro
#    03/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Ported
#
#-----------------------------------------------------------------------
# Uncomment line below for debugging information
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

RUSER=root
RHOST=${RHOST:-`hostname`}
LHOST=`hostname`
SLEEPTIME=${SLEEPTIME:-1}
LUSER=`id | tr -s '()' '  ' | awk '{print $2}'`

this_file=${0##*/}

#-----------------------------------------------------------------------
#
# FUNCTION:  exists
#
#-----------------------------------------------------------------------

exists()
{
   for cmd in $1
   do
       which $cmd 2>&1 1>/dev/null
	   if [ $? -ne 0 ]
	   then
	       tst_resm TBROK "Test broke: command $cmd not found"
		   exit 1
       fi
   done
}


#-----------------------------------------------------------------------
#
# FUNCTION:  do_setup
#
#-----------------------------------------------------------------------

do_setup ()
{
   $trace_logic

   # Start fingerd locally and remotely.

   export TCID="finger01"
   export TST_TOTAL=3
   export TST_COUNT=1

   exists "finger netstat awk rsh"

   [ "$LUSER" = "root" ] || end_testcase "MUST BE root to run the test!"

   FINGER=`netstat -a | grep "*:finger" |awk '{print $6}'`
   if [ "$FINGER" != "LISTEN" ]; then
      end_testcase "Please start fingerd locally."
   fi

   FINGER=`rsh -n $RHOST netstat -a |grep "*:finger"|awk '{print $6}'`
   if [ "$FINGER" != "LISTEN" ]; then
      end_testcase "Please start fingerd on remote host."
   fi
}

#-----------------------------------------------------------------------
# FUNCTION: do_test1
# PURPOSE: Run finger with no flags 
# INPUT:    None.
# OUTPUT:   Error messages are logged for all discrepancies found. 
#-----------------------------------------------------------------------

do_test1()
{
   $trace_logic
   TCID="finger01"
   TST_COUNT=1
   tst_resm TINFO "finger: do_test1 started"

   finger @$LHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger @$LHOST"
   finger @$RHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger @$RHOST"
	
   # Testing finger command with user argument

   finger $LUSER  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger $LUSER"
   finger $LUSER@$LHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger $LUSER@$LHOST"
   finger $RUSER@$RHOST 2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger @$RUSER"
}

#-----------------------------------------------------------------------
# FUNCTION: do_test2
# PURPOSE:  To run the "finger User@host" commands and verify that the
#           "finger" command is functioning properly.
# INPUT:    None.
# OUTPUT:   Error messages are logged for all discrepancies found.
#-----------------------------------------------------------------------

do_test2()
{
   $trace_logic

   TCID="finger02"
   TST_COUNT=2
   tst_resm TINFO "do_test2 $TC finger -bad flag"
   BADUSER="uuuu"
   BADHOST="xxxx"

   tst_resm TINFO "finger -badflag default "
   finger -x 2>&1 1>/dev/null
   [ $? = 0 ] && end_testcase "finger -x should fail"

   tst_resm TINFO "finger -bad user "
   finger $BADUSER 2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger $BADUSER "
   finger @$BADHOST 2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger @$BADHOST"
   finger $BADUSER@$BADHOST 2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger $BADUSER@$BADHOST"
}

#-----------------------------------------------------------------------
# FUNCTION: do_test3
# PURPOSE:  To run the "finger -flag User@Host" commands and verify that the
#           "finger" command is functioning properly.
# INPUT:    None.
# OUTPUT:   Error messages are logged for all discrepancies found. 
#-----------------------------------------------------------------------

do_test3()
{
   $trace_logic

   TCID="finger03"
   TST_COUNT=3
   tst_resm TINFO "do_test3 $TC finger -f flag"

   tst_resm TINFO "finger -l : gives a long listing"
   finger -l  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -l"
   finger -l $LUSER  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -l $LUSER "
   finger -l @$LHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -l @$LHOST"
   finger -l $LUSER@$LHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -l $LUSER@$LHOST"
   finger -l @$RHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -l @$RHOST"
   finger -l $RUSER@$RHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -l $RUSER@$RHOST"

   tst_resm TINFO "finger -m : assures that user is a user ID discretionary"
   finger -m  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -m"
   finger -m $LUSER  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -m $LUSER "
   finger -m @$LHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -m @$LHOST"
   finger -m $LUSER@$LHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -m $LUSER@$LHOST"
   finger -m @$RHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -m @$RHOST"
   finger -m $RUSER@$RHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -m $RUSER@$RHOST"

   tst_resm TINFO "finger -p:suppresses printing of .plan files on long and brief long formats"
   finger -p  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -p"
   finger -p $LUSER  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -p $LUSER "
   finger -p @$LHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -p @$LHOST"
   finger -p $LUSER@$LHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -p $LUSER@$LHOST"
   finger -p @$RHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -p @$RHOST"
   finger -p $RUSER@$RHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -p $RUSER@$RHOST"

   tst_resm TINFO "finger -s : gives a short format list"
   finger -s  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -s"
   finger -s $LUSER  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -s $LUSER "
   finger -s @$LHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -s @$LHOST"
   finger -s $LUSER@$LHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -s $LUSER@$LHOST"
   finger -s @$RHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -s @$RHOST"
   finger -s $RUSER@$RHOST  2>&1 1>/dev/null
   [ $? = 0 ] || end_testcase "finger -s $RUSER@$RHOST"

}

#=============================================================================
# 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."

   [ $# = 0 ] && { tst_resm TPASS "Test Successful"; exit 0; }
   tst_resm TFAIL "test failed: $@"
   exit 1
}

#-----------------------------------------------------------------------
# FUNCTION: MAIN
# PURPOSE:  To invoke the functions to perform the tasks outlined in the
#           prologue.
# INPUT:    None.
# OUTPUT:   None.
#-----------------------------------------------------------------------
do_setup
do_test1
sleep $SLEEPTIME
do_test2
sleep $SLEEPTIME
do_test3
sleep $SLEEPTIME
end_testcase
