Replace netcat6 with OpenBSD netcat

Netcat6 has been discontinued in favour of OpenBSD netcat.
This commit is contained in:
Andrew Bettison 2018-04-27 17:29:02 +09:30
parent d1203827e1
commit 7ab9bc0a10
3 changed files with 43 additions and 27 deletions

View File

@ -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"
}

View File

@ -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}"

View File

@ -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
}