1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#!/bin/sh
set -ue
PATH="/usr/bin:/bin"
export PATH
PORT=12345
TIMEOUT=10
if [ -n "${AUTOPKGTEST_TMP+x}" ]; then
NC="$(command -v nc.openbsd)"
TEMPDIR="$AUTOPKGTEST_TMP"
else
NC="./nc"
TEMPDIR="${TMPDIR:-/tmp}"
fi
echo "I: Using NC=$NC and TEMPDIR=$TEMPDIR"
# Basic client/server communication test
check() {
local timeout="$TIMEOUT"
echo "Checking \`$NC [-l|-N] $*\`..."
$NC -l "$@" </dev/null >"$TEMPDIR/stdout" & PID=$!
head -c240 </dev/urandom | base64 >"$TEMPDIR/stdin"
while [ $timeout -ge 0 ] && ! $NC -N "$@" <"$TEMPDIR/stdin" 2>&1; do
# give the server a chance to bind(2) and listen(2)
echo "Retrying..."
sleep 1
timeout=$((timeout - 1))
done
if [ $timeout -le 0 ]; then
echo "Timeout" >&2
kill $PID
exit 1
fi
wait $PID
diff -u -- "$TEMPDIR/stdin" "$TEMPDIR/stdout" || exit 1
}
check 127.0.0.1 $PORT
if $NC -z 127.0.0.1 $PORT; then
echo "Still listening on $PORT" >&2
exit 1
fi
# UNIX domain sockets (blindly assume "$TEMPDIR" is of length <=UNIX_PATH_MAX-23)
sun_path="$TEMPDIR/nc-client-server.sock"
check -U "$sun_path"
if ! test -S "$sun_path"; then
echo "No such socket $sun_path" >&2
exit 1
fi
# abstract UNIX domain socket
uname_s="$(uname -s)"
if [ "$uname_s" = "Linux" ]; then
check -U @"/nonexistent/netcat-openbsd/client-server"
# 108-bytes long sun_path (assume UNIX_PATH_MAX is 108 on Linux) with no NUL byte other than the one at index 0
check -U @"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/autopkgtest/netcat-openbsd/client-server"
fi
exit 0
# vim: set filetype=sh :
|