Debian Codegraph @

tests/telnet-localhost.sh

raw
1 #!/bin/sh
2
3 # Copyright (C) 2011-2026 Free Software Foundation, Inc.
4 #
5 # This file is part of GNU Inetutils.
6 #
7 # GNU Inetutils is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or (at
10 # your option) any later version.
11 #
12 # GNU Inetutils is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see `http://www.gnu.org/licenses/'.
19
20 # Test the TELNET client as connection vehicle!
21 #
22 # Written by Mats Erik Andersson.
23
24 # Prerequisites:
25 #
26 # * Shell: SVR4 Bourne shell, or newer.
27 #
28 # * id(1), kill(1), mktemp(1), tty(1).
29 #
30 # * Accessed by launched Inetd:
31 # /etc/nsswitch.conf, /etc/passwd, /etc/protocols.
32 #
33 # OpenBSD uses /etc/services directly, not via /etc/nsswitch.conf.
34
35 # Is usage explanation in demand?
36 #
37 if test "$1" = "-h" || test "$1" = "--help" || test "$1" = "--usage"; then
38 cat <<HERE
39 Minimal test for TELNET client.
40
41 The following environment variables are used:
42
43 VERBOSE Be verbose, if set.
44 TARGET Receiving IPv4 address.
45 TARGET6 Receiving IPv6 address.
46 TARGET46 Receiving IPv4-mapped-IPV6 address.
47
48 HERE
49 exit 0
50 fi
51
52 . ./tools.sh
53
54 $need_mktemp || exit_no_mktemp
55
56 # The executables under test.
57 #
58 INETD=${INETD:-../src/inetd$EXEEXT}
59 TELNET=${TELNET:-../telnet/telnet$EXEEXT}
60 ADDRPEEK=${ADDRPEEK:-./addrpeek$EXEEXT}
61
62 # Selected targets.
63 TARGET=${TARGET:-127.0.0.1}
64 TARGET6=${TARGET6:-::1}
65 TARGET46=${TARGET46:-::ffff:$TARGET}
66
67 # Step into `tests/', should the invocation
68 # have been made outside of it.
69 #
70 test -d src && test -f tests/telnet-localhost.sh && cd tests/
71
72 if test -n "$VERBOSE"; then
73 set -x
74 $INETD --version | $SED '1q'
75 $TELNET --version | $SED '1q'
76 fi
77
78 # The use of telnet is portable only with a connected TTY.
79 if tty >/dev/null; then
80 :
81 else
82 echo 'No TTY assigned to this process. Skipping test.' >&2
83 exit 77
84 fi
85
86 if test ! -x $INETD; then
87 echo "Missing executable '$INETD'. Skipping test." >&2
88 exit 77
89 fi
90
91 if test ! -x $TELNET; then
92 echo "Missing executable '$TELNET'. Skipping test." >&2
93 exit 77
94 fi
95
96 if test ! -x $ADDRPEEK; then
97 echo "Missing executable '$ADDRPEEK'. Skipping test." >&2
98 exit 77
99 fi
100
101 if test "$TEST_IPV4" = "no" && test "$TEST_IPV6" = "no"; then
102 echo >&2 "Inet socket testing is disabled. Skipping test."
103 exit 77
104 fi
105
106 # Check regression of crash reported here:
107 # https://lists.gnu.org/archive/html/bug-inetutils/2022-02/msg00014.html
108 echo 'help z ! ? z ! ? z ! ? z ! ? z ! ? z ! ? z !' | $TELNET > /dev/null 2>&1
109 if test $? -ne 0; then
110 echo "Regression of telnet crash bug." >&2
111 exit 1
112 fi
113
114 # Check regression of crash reported here:
115 # https://lists.gnu.org/archive/html/bug-inetutils/2022-02/msg00009.html
116 echo 'help help' | $TELNET > /dev/null 2>&1
117 if test $? -ne 0; then
118 echo "Regression of telnet second crash bug." >&2
119 exit 1
120 fi
121
122 # Check regression of crash reported here:
123 # https://lists.gnu.org/archive/html/bug-inetutils/2022-02/msg00007.html
124 echo "unset ' '" | $TELNET > /dev/null 2>&1
125 if test $? -ne 0; then
126 echo "Regression of telnet third crash bug." >&2
127 exit 1
128 fi
129
130 # Check regression of crash reported here:
131 # https://lists.gnu.org/archive/html/bug-inetutils/2022-02/msg00010.html
132 echo "set ' ' foo" | $TELNET > /dev/null 2>&1
133 if test $? -ne 0; then
134 echo "Regression of telnet fourth crash bug." >&2
135 exit 1
136 fi
137
138 errno=0
139
140 # In non-verbose mode the variables `display' and `display_err'
141 # redirect output streams to `/dev/null'.
142
143 test -n "${VERBOSE+yes}" || display='>/dev/null'
144 test -n "${VERBOSE+yes}" || display_err='2>/dev/null'
145
146 # Check regression of integer overflow check reported in:
147 # https://lists.gnu.org/archive/html/bug-inetutils/2024-08/msg00007.html
148 for value in -2147483649 -1 256 2147483648 9223372036854775808; do
149 for cmd in "do" "dont" "will" "wont"; do
150 output=`echo "send $cmd $value" | $TELNET 2>&1`
151 echo "$output" | eval "$GREP ': bad value ' $display"
152 if test $? -ne 0; then
153 errno=1
154 echo "Failed to catch bad value '$value'." >&2
155 fi
156 done
157 done
158
159 # Portability fix for SVR4
160 PWD="${PWD:-`pwd`}"
161
162 # For file creation below IU_TESTDIR.
163 umask 0077
164
165 USER=`func_id_user`
166
167 # Random base directory at testing time.
168 TMPDIR=`$MKTEMP -d $PWD/tmp.XXXXXXXXXX` ||
169 {
170 echo 'Failed at creating test directory. Aborting.' >&2
171 exit 1
172 }
173
174 INETD_CONF="$TMPDIR/inetd.conf"
175 INETD_PID="$TMPDIR/inetd.pid.$$"
176
177 posttesting () {
178 if test -n "$TMPDIR" && test -f "$INETD_PID" \
179 && test -r "$INETD_PID" \
180 && kill -0 "`cat $INETD_PID`" >/dev/null 2>&1
181 then
182 kill "`cat $INETD_PID`" >/dev/null 2>&1 ||
183 kill -9 "`cat $INETD_PID`" >/dev/null 2>&1
184 fi
185 test -n "$TMPDIR" && test -d "$TMPDIR" \
186 && rm -rf "$TMPDIR" $FILELIST
187 }
188
189 trap posttesting EXIT HUP INT QUIT TERM
190
191 PORT=`expr 4973 + ${RANDOM:-$$} % 973`
192
193 # Create an empty configuration file for inetd.
194 : > "$INETD_CONF" 2>/dev/null ||
195 {
196 echo 'Could not create configuration file for Inetd. Aborting.' >&2
197 exit 1
198 }
199
200 # This target is used for plain IPv4 and for IPv4-mapped addressing.
201 if test "$TEST_IPV4" != "no"; then
202 cat >> "$INETD_CONF" <<-EOF
203 $TARGET:$PORT stream tcp4 nowait $USER $ADDRPEEK addrpeek addr
204 EOF
205 fi
206
207 if test "$TEST_IPV6" != "no"; then
208 cat >> "$INETD_CONF" <<-EOF
209 $TARGET6:$PORT stream tcp6 nowait $USER $ADDRPEEK addrpeek addr
210 EOF
211 fi
212
213 # Must use '-d' consistently to prevent daemonizing, but we
214 # would like to suppress the verbose output.
215
216 eval "$INETD -d -p'$INETD_PID' '$INETD_CONF' $display_err &"
217
218 # Debug mode allows the shell to recover PID of Inetd.
219 spawned_pid=$!
220
221 # Wait somewhat for the service to settle.
222 sleep 2
223
224 inetd_pid="`cat $INETD_PID 2>/dev/null`" ||
225 {
226 cat <<-EOT >&2
227 Inetd did not create a PID-file. Aborting test,
228 but losing control whether an Inetd process is
229 still around.
230 EOT
231 exit 1
232 }
233
234 test -z "$VERBOSE" || echo "Launched Inetd as process $inetd_pid." >&2
235
236 telnet_opts="--no-rc --no-escape --no-login"
237
238 if test "$TEST_IPV4" != "no" && test -n "$TARGET"; then
239 output=`$TELNET $telnet_opts $TARGET $PORT 2>/dev/null`
240 echo "$output" | eval "$GREP 'Your address is $TARGET.' $display"
241 if test $? -ne 0; then
242 errno=1
243 echo "Failed at '$TARGET'." >&2
244 fi
245 fi # TEST_IPV4 && TARGET
246
247 if test "$TEST_IPV6" != "no" && test -n "$TARGET6"; then
248 output=`$TELNET $telnet_opts $TARGET6 $PORT 2>/dev/null`
249 echo "$output" | eval "$GREP 'Your address is $TARGET6.' $display"
250 if test $? -ne 0; then
251 errno=1
252 echo "Failed at '$TARGET6'." >&2
253 fi
254 fi # TEST_IPV6 && TARGET6
255
256 if test "$TEST_IPV4" != "no" && test "$TEST_IPV6" != "no" \
257 && test -n "$TARGET46"; then
258 output=`$TELNET $telnet_opts $TARGET46 $PORT 2>/dev/null`
259 echo "$output" | eval "$GREP 'Your address is .*$TARGET.' $display"
260 if test $? -ne 0; then
261 echo "Informational: Unsuccessful with mapped address '$TARGET46'." >&2
262 fi
263 fi # TEST_IPV4 && TEST_IPV6 && TARGET46
264
265 exit $errno