summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRical Jasan <ricaljasan@pacific.net>2017-02-17 21:22:19 -0800
committerNikos Mavrogiannopoulos <nmav@redhat.com>2017-02-19 09:44:23 +0100
commit8c0ab41b766002be9631399a210f6dd71874d4f7 (patch)
tree4173dfcb78a4e244c7c3df97f180c0f4923b9610
parent39d4e43edb75bfdc85372b093598f26c1481496a (diff)
downloadgnutls-tmp-work-without-netstat.tar.gz
tests: Improve port-checking infrastructure.tmp-work-without-netstat
The test suite unnecessarily failed on systems without netstat because it was assumed to be present. Instead of simply checking for its presence and indicating an unsupported test, however, the ss utility can be used as a drop-in replacement. When netstat/net-tools is not present, the ss utility from iproute2 still stands a fair chance of existing, and they also have similar enough semantics that they can be used interchangeably in the test suite. The functions in tests/scripts/common.sh that used netstat (wait_for_port, wait_for_free_port) now use new functions, check_if_port_in_use and check_if_port_listening, to abstract the call to netstat/ss. The eval'd variable GETPORT also used netstat, and has been updated accordingly. The new port-checking functions use another new function, have_port_finder, which takes care of the details of selecting ss (preferred) or netstat, or fails otherwise. Signed-off-by: Rical Jasan <ricaljasan@pacific.net> Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
-rw-r--r--tests/scripts/common.sh53
1 files changed, 47 insertions, 6 deletions
diff --git a/tests/scripts/common.sh b/tests/scripts/common.sh
index 3cf841c25b..518d8416bc 100644
--- a/tests/scripts/common.sh
+++ b/tests/scripts/common.sh
@@ -21,9 +21,50 @@
export TZ="UTC"
-GETPORT='rc=0;myrandom=$(date +%N | sed 's/^0*//');while test $rc = 0;do PORT="$(((($$<<15)|$myrandom) % 63001 + 2000))";
- netstat -anl|grep "[\:\.]$PORT" >/dev/null 2>&1;
- rc=$?;done;'
+# Check for a utility to list ports. Both ss and netstat will list
+# ports for normal users, and have similar semantics, so put the
+# command in the caller's PFCMD, or exit, indicating an unsupported
+# test. Prefer ss from iproute2 over the older netstat.
+have_port_finder() {
+ for file in $(which ss) /*bin/ss /usr/*bin/ss /usr/local/*bin/ss;do
+ if test -x "$file";then
+ PFCMD="$file";return 0
+ fi
+ done
+
+ if test -z "$PFCMD";then
+ for file in $(which netstat) /bin/netstat /usr/bin/netstat /usr/local/bin/netstat;do
+ if test -x "$file";then
+ PFCMD="$file";return 0
+ fi
+ done
+ fi
+
+ if test -z "$PFCMD";then
+ echo "neither ss nor netstat found"
+ exit 1
+ fi
+}
+
+check_if_port_in_use() {
+ local PORT="$1"
+ local PFCMD; have_port_finder
+ $PFCMD -an|grep "[\:\.]$PORT" >/dev/null 2>&1
+}
+
+check_if_port_listening() {
+ local PORT="$1"
+ local PFCMD; have_port_finder
+ $PFCMD -anl|grep "[\:\.]$PORT"|grep LISTEN >/dev/null 2>&1
+}
+
+# Find a port number not currently in use.
+GETPORT='rc=0; myrandom=$(date +%N | sed s/^0*//)
+ while test $rc = 0;do
+ PORT="$(((($$<<15)|$myrandom) % 63001 + 2000))"
+ check_if_port_in_use $PORT;rc=$?
+ done
+'
check_for_datefudge() {
TSTAMP=`datefudge -s "2006-09-23" date -u +%s || true`
@@ -49,10 +90,10 @@ wait_for_port()
sleep 4
for i in 1 2 3 4 5 6;do
- netstat -anl|grep "[\:\.]$PORT"|grep LISTEN >/dev/null 2>&1
+ check_if_port_listening ${PORT}
ret=$?
if test $ret != 0;then
- netstat -anl|grep "[\:\.]$PORT"
+ check_if_port_in_use ${PORT}
echo try $i
sleep 2
else
@@ -68,7 +109,7 @@ wait_for_free_port()
local PORT="$1"
for i in 1 2 3 4 5 6;do
- netstat -anl|grep "[\:\.]$PORT" >/dev/null 2>&1
+ check_if_port_in_use ${PORT}
ret=$?
if test $ret != 0;then
break