From 7ab9bc0a109b148f8fd5724d17da7d2d5a65e353 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Fri, 27 Apr 2018 17:29:02 +0930 Subject: [PATCH] Replace netcat6 with OpenBSD netcat Netcat6 has been discontinued in favour of OpenBSD netcat. --- testdefs.sh | 52 +++++++++++++++++++++++++++++++----------------- testframework.sh | 2 +- tests/msp | 16 +++++++-------- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/testdefs.sh b/testdefs.sh index 36ca640f..cca92111 100644 --- a/testdefs.sh +++ b/testdefs.sh @@ -975,36 +975,52 @@ curl() { } # Setup function: -# - ensure that the netcat6 nc6(1) utility is available -setup_netcat6() { - NETCAT6=$(type -P nc6) || error "nc6(1) command is not present" - local minversion="${1:-1.0}" - local ver="$("$NETCAT6" --version | tr '\n' ' ')" +# - ensure that a suitable version of the OpenBSD netcat utility is available +setup_netcat() { + local try=(nc.openbsd nc) + NETCAT= + for exe in "${try[@]}"; do + NETCAT=$(type -P "$exe") && break + done + [ -x "$NETCAT" ] || error "OpenBSD netcat command is not present: tried ${try[*]}" + local minversion="${1:-1.130-3}" + local ver="$("$NETCAT" -h 2>&1 | tr '\n' ' ')" case "$ver" in - nc6\ version\ *) + 'OpenBSD netcat (Debian patchlevel '*')'*) set -- $ver - tfw_cmp_version "$3" "$minversion" + local version="${5%)}" + tfw_cmp_version "$version" "$minversion" case $? in 0|2) - export NETCAT6 + export NETCAT return 0 ;; esac - error "$NETCAT6 version $3 is not adequate (expecting $minversion or higher)" + error "$NETCAT version $version is not adequate (require $minversion or higher)" + ;; + '[v'*']'*) + error "$NETCAT is the traditional netcat (require the OpenBSD netcat, version $minversion or higher)" + ;; + *) + error "cannot parse output of $NETCAT -h: $ver" ;; esac - error "cannot parse output of $NETCAT6 --version: $ver" +} + +# Utility function: +# - invoke the OpenBSD netcat utility +netcat() { + [ -x "$NETCAT" ] || error "missing call to setup_netcat in the fixture" + "$NETCAT" "$@" } # Guard functions. -NETCAT6= -nc6() { - [ -x "$NETCAT6" ] || error "missing call to setup_netcat6 in the fixture" - "$NETCAT6" "$@" -} nc() { - error "do not use nc; instead use nc6(1) after calling setup_netcat6 in the fixture" + error "do not use nc directly; instead use netcat() after calling setup_netcat() in the fixture" } -netcat() { - error "do not use netcat; instead use nc6(1) after calling setup_netcat6 in the fixture" +setup_netcat6() { + error "use of netcat6 is discontinued; use setup_netcat() instead" +} +nc6() { + error "use of netcat6 is discontinued; use netcat() instead" } diff --git a/testframework.sh b/testframework.sh index 8033d811..815e6dd4 100644 --- a/testframework.sh +++ b/testframework.sh @@ -2132,7 +2132,7 @@ assertGrep() { # Compare the two arguments as dotted ascii decimal version strings. # Return 0 if they are equal, 1 if arg1 < arg2, 2 if arg1 > arg2 tfw_cmp_version() { - local IFS=. + local IFS=.- local i=0 a=($1) b=($2) for (( i=0; i < ${#a[@]} || i < ${#b[@]}; ++i )); do local ai="${a[i]:-0}" diff --git a/tests/msp b/tests/msp index 5ab913b9..60858e12 100755 --- a/tests/msp +++ b/tests/msp @@ -181,7 +181,7 @@ test_keep_alive() { doc_forward="Forward TCP connections to a remote server" setup_forward() { setup_common - setup_netcat6 + setup_netcat start_servald_instances +A +B } client_forward() { @@ -195,7 +195,7 @@ test_forward() { set_instance +B fork %connect client_forward 2048 "$SIDA" sleep 1 - executeOk nc6 -q 30 -v 127.0.0.1 2048 < <(echo "Hello from the client") + executeOk netcat -q 30 -v 127.0.0.1 2048 < <(echo "Hello from the client") assertStdoutGrep --matches=1 "^Hello from the server$" fork_wait %listen %connect } @@ -203,7 +203,7 @@ test_forward() { doc_tcp_tunnel="Tunnel a tcp connection" setup_tcp_tunnel() { setup_common - setup_netcat6 + setup_netcat start_servald_instances +A +B } server_forward() { @@ -212,7 +212,7 @@ server_forward() { tfw_cat --stderr } nc_listen() { - execute nc6 -q -1 -v -l -p "$1" < <(echo "Hello from the server") + execute netcat -q -1 -v -l -p "$1" < <(echo "Hello from the server") tfw_cat --stdout --stderr } test_tcp_tunnel() { @@ -222,7 +222,7 @@ test_tcp_tunnel() { set_instance +B fork %client client_forward 6001 "$SIDA" sleep 1 - executeOk nc6 -q 10 -v 127.0.0.1 6001 < <(echo "Hello from the client") + executeOk netcat -q 10 -v 127.0.0.1 6001 < <(echo "Hello from the client") tfw_cat --stdout --stderr assertStdoutGrep --matches=1 "^Hello from the server$" fork_wait %listen %server %client @@ -287,7 +287,7 @@ test_refused(){ doc_terminate="Terminate a connection mid stream" setup_terminate() { setup_common - setup_netcat6 + setup_netcat start_servald_instances +A +B } client_terminate() { @@ -306,8 +306,8 @@ test_terminate() { set_instance +B fork %mspconnect client_terminate sleep 1 -# todo capture nc6 output nicely, while still being able to terminate it - fork %ncconnect nc6 -q 0 -v 127.0.0.1 3001 +# todo capture netcat output nicely, while still being able to terminate it + fork %ncconnect netcat -q 0 -v 127.0.0.1 3001 fork_terminate %ncconnect fork_wait %msplisten %mspconnect %ncconnect }