xinetd_tests.sh revision fb8c7b331732cfc0eb37a480c07e7d06bcb68763
1################################################################################
2##                                                                            ##
3## Copyright (c) International Business Machines  Corp., 2001                 ##
4##                                                                            ##
5## This program is free software;  you can redistribute it and#or modify      ##
6## it under the terms of the GNU General Public License as published by       ##
7## the Free Software Foundation; either version 2 of the License, or          ##
8## (at your option) any later version.                                        ##
9##                                                                            ##
10## This program is distributed in the hope that it will be useful, but        ##
11## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
12## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
13## for more details.                                                          ##
14##                                                                            ##
15## You should have received a copy of the GNU General Public License          ##
16## along with this program;  if not, write to the Free Software               ##
17## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    ##
18##                                                                            ##
19################################################################################
20#
21# File :         xinetd_tests.sh
22#
23# Description:   Test Basic functionality of xinetd command.
24#                Test #1: xinetd starts programs that provide Internet services.
25#
26# Author:        Manoj Iyer, manjo@mail.utexas.edu
27#
28# History:       Mar 04 2003 - Created - Manoj Iyer.
29#
30#! /bin/sh
31
32
33
34# Function:     chk_ifexists
35#
36# Description:  - Check if command required for this test exits.
37#
38# Input:        - $1 - calling test case.
39#               - $2 - command that needs to be checked.
40# 
41# Return:       - zero on success.
42#               - non-zero on failure.
43chk_ifexists()
44{
45    RC=0
46
47    which $2 &>$LTPTMP/tst_xinetd.err || RC=$?
48    if [ $RC -ne 0 ]
49    then
50        tst_brkm TBROK NULL "$1: command $2 not found."
51    fi
52    return $RC
53}
54
55
56# Function: init
57#
58# Description:  - Check if command required for this test exits.
59#               - Create temporary directories required for this test. 
60#               - Initialize global variables.
61# 
62# Return:       - zero on success.
63#               - non-zero on failure.
64init()
65{
66    # Initialize global variables.
67    export RC=0
68    export TST_TOTAL=2
69    export TCID="xinetd"
70    export TST_COUNT=0
71
72    # Inititalize cleanup function.
73    trap "cleanup" 0
74
75    # create the temporary directory used by this testcase
76    if [ -z $TMP ]
77    then
78        LTPTMP=/tmp/tst_xinetd.$$
79    else
80        LTPTMP=$TMP/tst_xinetd.$$
81    fi
82
83    mkdir -p $LTPTMP &>/dev/null || RC=$?
84    if [ $RC -ne 0 ]
85    then
86         tst_brkm TBROK NULL "INIT: Unable to create temporary directory"
87         return $RC
88    fi
89
90    # check if commands tst_*, xinetd, awk exists.
91    chk_ifexists INIT tst_resm   || return $RC
92    chk_ifexists INIT xinetd     || return $RC
93    chk_ifexists INIT diff       || return $RC
94    chk_ifexists INIT telnet     || return $RC
95
96	# Create custom xinetd.conf file. 
97	# tst_xinetd.conf.1 config file has telnet service disabled.
98	cat > $LTPTMP/tst_xinetd.conf.1 <<-EOF || RC=$?
99	defaults
100	{
101		instances      = 25
102		log_type       = FILE /var/log/servicelog
103		log_on_success = HOST PID
104		log_on_failure = HOST RECORD
105	    disabled       = telnet
106	}
107	EOF
108
109	# tst_xinetd.conf.2 config file has telnet enabled.
110	cat > $LTPTMP/tst_xinetd.conf.2 <<-EOF || RC=$?
111	defaults
112	{
113		instances      = 25
114		log_type       = FILE /var/log/servicelog
115		log_on_success = HOST PID
116		log_on_failure = HOST RECORD
117		# disabled       = telnet
118	}
119
120	service telnet
121	{
122        socket_type     = stream
123        protocol        = tcp
124        wait            = no
125        user            = root
126        server          = /usr/sbin/in.telnetd
127        server_args     = -n
128        no_access       = 
129    }       
130	EOF
131
132    # Create expected file with telnet disabled.
133    cat > $LTPTMP/tst_xinetd.exp.1 <<-EOF || RC=$?
134	telnet: Unable to connect to remote host: Connection refused
135	EOF
136
137    if [ $RC -ne 0 ]
138    then
139        tst_brkm TBROK  NULL \
140            "INIT: unable to create expected file $LTPTMP/tst_xinetd.exp.1"
141        return $RC
142    fi
143
144    # Create expected file with telnet enabled.
145	cat > $LTPTMP/tst_xinetd.exp.2 <<-EOF || RC=$?
146	Trying 127.0.0.1...
147	Connected to localhost (127.0.0.1).
148	Escape character is '^]'.
149	Connection closed by foreign host.
150	EOF
151
152    if [ $RC -ne 0 ]
153    then
154        tst_brkm TBROK  NULL \
155            "INIT: unable to create expected file $LTPTMP/tst_xinetd.exp.1"
156        return $RC
157    fi
158
159    return $RC
160}
161
162
163# Function:     cleanup
164#
165# Description:  - remove temporaty files and directories. 
166#
167# Return:       - zero on success.
168#               - non-zero on failure.
169cleanup()
170{
171	RC=0
172	# restore the original xinetd.conf if a back up exits.
173	if [ -f /etc/xinetd.conf.orig ]
174	then
175		mv /etc/xinetd.conf.orig /etc/xinetd.�onf \
176			&>$LTPTMP/tst_xinetd.err || RC=$?
177		if [ $RC -ne 0 ]
178		then
179			tst_res TINFO $LTPTMP/tst_xinetd.err \
180			"CLEANUP: failed restoring original xinetd.conf RC=$RC. Details:"
181		fi
182
183		sleep 1s
184
185		# restoring original services
186		/etc/init.d/xinetd restart &>$LTPTMP/tst_xinetd.err || RC=$?
187		if [ $RC -ne 0 ]
188		then
189			tst_res TINFO $LTPTMP/tst_xinetd.err \
190			"CLEANUP: failed restoring original services RC=$RC. Details:"
191		fi
192	fi
193
194    # remove all the temporary files created by this test.
195    tst_resm TINFO "CLEAN: removing $LTPTMP"
196    rm -fr $LTPTMP
197}
198
199
200# Function:     test01
201#
202# Description:  - Test that xinetd reads the configuration file and starts or
203#                 stops services.
204#               - restart xinetd with configuration file with telnet disabled.
205#               - telnet to locahost should fail.
206#               - restart xinetd with configuration file with telnet enabled.
207#               - telnet to locahost should work.
208# 
209# Return:       - zero on success.
210#               - non-zero on failure.
211test01()
212{
213    TCID=xinetd01
214    TST_COUNT=1
215    nhops=0             # Number of hops required to get to host.
216    RC=0                # Return value from commands.
217
218    tst_resm TINFO "Test #1: restart xinetd with telnet disabled."
219	
220	# create a backup of the original xinetd.conf file.
221	mv /etc/xinetd.conf /etc/xinetd.conf.orig &>$LTPTMP/tst_xinetd.err \
222		|| RC=$?
223    if [ $RC -ne 0 ]
224    then
225        tst_brk TBROK $LTPTMP/tst_xinetd.err NULL \
226            "Test #1: Failed while backing up original xinetd.conf. Details"
227        return $RC
228    fi
229
230	# install the new config file with telnet disabled.
231	mv $LTPTMP/tst_xinetd.conf.1 /etc/xinetd.conf &>$LTPTMP/tst_xinetd.err \
232		|| RC=$?
233    if [ $RC -ne 0 ]
234    then
235        tst_brk TBROK $LTPTMP/tst_xinetd.err NULL \
236            "Test #1: Failed installing new xinetd.conf in /etc. Details:"
237        return $RC
238    fi
239
240    tst_resm TINFO "Test #1: new xinetd.conf installed with telnet disabled."
241
242	sleep 1s
243
244	# restart xinetd to re-start the services
245    /etc/init.d/xinetd restart &>$LTPTMP/tst_xinetd.out || RC=$?
246    if [ $RC -ne 0 ]
247    then
248        tst_res TFAIL $LTPTMP/tst_xinetd.out \
249       "Test #1: unable to restart service with telnet disabled. Details:"
250        return $RC
251	else
252		# even if xinetd restart has zero exit value, 
253		# make certain there was no failure.
254		grep -i "fail" $LTPTMP/tst_xinetd.out &>$LTPTMP/tst_xinetd.err || RC=$?
255		if [ $RC -eq 0 ]
256		then
257			tst_res TFAIL $LTPTMP/tst_xinetd.err \
258				"Test #1: xinetd failed to restart. Details"
259			return $RC
260		else
261			RC=0
262			tst_resm TINFO \
263				"Test #1: xinetd re-started successfully with telnet disabled."
264		fi
265	fi
266
267	# Not checking for exit code from telnet command because telnet is 
268	# not terminated by the test gracefully.
269	echo "" | telnet localhost 2>$LTPTMP/tst_xinetd.out 1>/dev/null
270	diff -iwB $LTPTMP/tst_xinetd.out  $LTPTMP/tst_xinetd.exp.1 \
271		&>$LTPTMP/tst_xinetd.err || RC=$?
272    if [ $RC -ne 0 ]
273    then
274        tst_res TFAIL $LTPTMP/tst_xinetd.err \
275            "Test #1: with telnet diabled expected out differs RC=$RC. Details:"
276        return $RC
277    fi
278
279    tst_resm TINFO "Test #1: restart xinetd with telnet enabled."
280	# install the xinetd config file with telnet enabled.
281	mv $LTPTMP/tst_xinetd.conf.2 /etc/xinetd.conf &>$LTPTMP/tst_xinetd.err \
282		|| RC=$?
283    if [ $RC -ne 0 ]
284    then
285        tst_brk TBROK $LTPTMP/tst_xinetd.err NULL \
286            "Test #1: Failed installing new xinetd.conf in /etc. Details:"
287        return $RC
288    fi
289
290    tst_resm TINFO "Test #1: new xinetd.conf installed with telnet enabled."
291	
292	sleep 1s
293
294	# restart services.
295    /etc/init.d/xinetd restart &>$LTPTMP/tst_xinetd.out || RC=$?
296    if [ $RC -ne 0 ]
297    then
298        tst_res TFAIL $LTPTMP/tst_xinetd.out \
299            "Test #1: unable to restart services with telnet enabled. Details:"
300        return $RC
301	else
302		# even if restart has a zero exit value double check for failure.
303		grep -i "fail" $LTPTMP/tst_xinetd.out &>$LTPTMP/tst_xinetd.err || RC=$?
304		if [ $RC -eq 0 ]
305		then
306			tst_res TFAIL $LTPTMP/tst_xinetd.err \
307				"Test #1: xinetd failed to restart. Details"
308			return $RC
309		else
310			RC=0
311			tst_resm TINFO \
312				"Test #1: xinetd re-started successfully with telnet disabled."
313		fi
314	fi
315
316	# Not checking for exit code from telnet command because telnet is 
317	# not terminated by the test gracefully.
318	echo "" | telnet localhost &>$LTPTMP/tst_xinetd.out 
319
320	diff -iwB $LTPTMP/tst_xinetd.out  $LTPTMP/tst_xinetd.exp.2 \
321		&>$LTPTMP/tst_xinetd.err || RC=$?
322    if [ $RC -ne 0 ]
323    then
324        tst_res TFAIL $LTPTMP/tst_xinetd.err \
325            "Test #1: expected output differes from actual. Details:"
326        return $RC
327	else
328		tst_resm TPASS \
329		"Test #1: xinetd reads the config file and starts or stops services."
330    fi
331
332    return $RC    
333}
334
335
336# Function:    main
337#
338# Description:    - Execute all tests and report results.
339#
340# Exit:            - zero on success 
341#               - non-zero on failure.
342
343RC=0
344init || exit $?
345
346test01 || RC=$?
347
348exit $RC
349