1#!/bin/sh
2################################################################################
3##                                                                            ##
4## Copyright (c) International Business Machines  Corp., 2001                 ##
5##                                                                            ##
6## This program is free software;  you can redistribute it and#or modify      ##
7## it under the terms of the GNU General Public License as published by       ##
8## the Free Software Foundation; either version 2 of the License, or          ##
9## (at your option) any later version.                                        ##
10##                                                                            ##
11## This program is distributed in the hope that it will be useful, but        ##
12## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
13## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
14## for more details.                                                          ##
15##                                                                            ##
16## You should have received a copy of the GNU General Public License          ##
17## along with this program;  if not, write to the Free Software               ##
18## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
19##                                                                            ##
20################################################################################
21#
22# File :         traceroute_tests.sh
23#
24# Description:   Test Basic functionality of traceroute command.
25#                Test #1: execute traceroute on hostname, expected number of
26#                hops is 1.
27#
28# Author:        Manoj Iyer, manjo@mail.utexas.edu
29#
30# History:       Mar 03 2003 - Created - Manoj Iyer.
31#
32# Function:     chk_ifexists
33#
34# Description:  - Check if command required for this test exits.
35#
36# Input:        - $1 - calling test case.
37#               - $2 - command that needs to be checked.
38#
39# Return:       - zero on success.
40#               - non-zero on failure.
41chk_ifexists()
42{
43    RC=0
44
45    which $2 > $LTPTMP/tst_traceroute.err 2>&1 || RC=$?
46    if [ $RC -ne 0 ]
47    then
48        tst_brkm TBROK NULL "$1: command $2 not found."
49    fi
50    return $RC
51}
52
53
54# Function: init
55#
56# Description:  - Check if command required for this test exits.
57#               - Create temporary directories required for this test.
58#               - Initialize global variables.
59#
60# Return:       - zero on success.
61#               - non-zero on failure.
62init()
63{
64    # Initialize global variables.
65    export RC=0
66    export TST_TOTAL=2
67    export TCID="traceroute"
68    export TST_COUNT=0
69
70    # Inititalize cleanup function.
71    trap "cleanup" 0
72
73    # create the temporary directory used by this testcase
74    if [ -z $TMP ]
75    then
76        LTPTMP=/tmp/tst_traceroute.$$
77    else
78        LTPTMP=$TMP/tst_traceroute.$$
79    fi
80
81    mkdir -p $LTPTMP > /dev/null 2>&1 || RC=$?
82    if [ $RC -ne 0 ]
83    then
84         tst_brkm TBROK "INIT: Unable to create temporary directory"
85         return $RC
86    fi
87
88    # check if commands tst_*, traceroute, awk exists.
89    chk_ifexists INIT tst_resm   || return $RC
90    chk_ifexists INIT traceroute || return $RC
91    chk_ifexists INIT awk        || return $RC
92    chk_ifexists INIT head       || return $RC
93    chk_ifexists INIT cat        || return $RC
94    chk_ifexists INIT diff       || return $RC
95
96    # Create expected file.
97    cat > $LTPTMP/tst_traceroute.exp <<-EOF || RC=$?
98    traceroute to $(hostname) ($(hostname -i)), 30 hops max, 38 byte packets
99	EOF
100
101    if [ $RC -ne 0 ]
102    then
103        tst_brkm TBROK  NULL \
104            "INIT: unable to create expected file $LTPTMP/tst_traceroute.exp"
105        return $RC
106    fi
107    return $RC
108}
109
110
111# Function:     cleanup
112#
113# Description:  - remove temporaty files and directories.
114#
115# Return:       - zero on success.
116#               - non-zero on failure.
117cleanup()
118{
119    # remove all the temporary files created by this test.
120    tst_resm TINFO "CLEAN: removing $LTPTMP"
121    rm -fr $LTPTMP
122}
123
124
125# Function:     test01
126#
127# Description:  - Test that traceroute hostname will trace route of an IP
128#                 packet to that host.
129#
130# Return:       - zero on success.
131#               - non-zero on failure.
132test01()
133{
134    TCID=traceroute01
135    TST_COUNT=1
136    nhops=0             # Number of hops required to get to host.
137    RC=0                # Return value from commands.
138
139    tst_resm TINFO "Test #1: Execute traceroute on hostname."
140    tst_resm TINFO "Test #1: traceroute returns the path taken by IP packet"
141    tst_resm TINFO "Test #1: to that host."
142
143    traceroute `hostname` 38 > $LTPTMP/tst_traceroute.out 2>&1 || RC=$?
144    if [ $RC -ne 0 ]
145    then
146        tst_res TFAIL $LTPTMP/tst_traceroute.out \
147            "Test #1: traceroute command failed: return = $RC. Details:"
148        return $RC
149    fi
150
151    cat $LTPTMP/tst_traceroute.out | head -n 1 > $LTPTMP/tst_traceroute.out.1 2>&1
152    diff -iwB $LTPTMP/tst_traceroute.out.1 $LTPTMP/tst_traceroute.exp \
153        > $LTPTMP/tst_traceroute.err 2>&1 || RC=$?
154    if [ $RC -ne 0 ]
155    then
156        tst_res TFAIL $LTPTMP/tst_traceroute.err \
157            "Test #1: unexpected output. Details:"
158        return $RC
159    else
160        # Only one hop is required to get to hostname.
161        nhops=$(cat $LTPTMP/tst_traceroute.out | tail -n 1 | awk '{print $1}')
162        if [ $nhops -ne 1 ]
163        then
164            tst_resm TFAIL "Test #1: $hops number of hops unexpected"
165        else
166            tst_resm TPASS \
167                "Test #1: traceroute $hostname traced route correctly"
168        fi
169    fi
170
171    return $RC
172}
173
174
175# Function:    main
176#
177# Description:    - Execute all tests and report results.
178#
179# Exit:            - zero on success
180#               - non-zero on failure.
181
182RC=0
183init || exit $?
184
185test01 || RC=$?
186
187exit $RC
188