Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions scripts/dtls.test
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ print(s.getsockname()[1])
s.close()'
}

# A usable port is a non-zero decimal below 65536. Worth checking rather than
# assuming: a SINGLE_THREADED build never resolves the ephemeral port in
# udp_accept(), so the ready file legitimately contains 0 there, and pointing
# the proxy at port 0 would report every test as a DTLS failure instead.
# A usable port is a non-zero decimal below 65536.
valid_port() {
case "$1" in
''|*[!0-9]*) return 1 ;;
Expand Down Expand Up @@ -162,11 +159,7 @@ if [ "$DTLS_VERSION" = "-v4" ]; then
UDP_PROXY_EXTRA_ARGS="-u"
fi

# Everything below depends on the server being able to report the port it bound.
# A SINGLE_THREADED or WOLFSSL_TIRTOS build never resolves an ephemeral port in
# udp_accept(), and a NO_FILESYSTEM or NETOS build compiles write_ready_file()
# to a no-op, so probe the capability once here rather than rediscovering it on
# every one of the tests below.
# Probe once whether the server can report the port it bound with -p 0 -R.
check_ready_file_support() {
local probe_pid counter port

Expand Down Expand Up @@ -199,7 +192,7 @@ check_ready_file_support() {

if ! check_ready_file_support; then
echo 'skipping dtls.test because the server cannot report an ephemeral port' 1>&2
echo 'with -p 0 -R (needs threads and a filesystem in the build).' 1>&2
echo 'with -p 0 -R (needs a filesystem in the build).' 1>&2
exit 77
fi

Expand Down
12 changes: 3 additions & 9 deletions scripts/dtlscid.test
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,15 @@ WOLFSSL_ROOT=$(pwd)
# runs - a second "make check" in another source tree, say - from sharing one.
READY_FILE="$WOLFSSL_ROOT/wolfssl_dtlscid_ready$$"

# A usable port is a non-zero decimal below 65536. Worth checking rather than
# assuming: a SINGLE_THREADED or WOLFSSL_TIRTOS build never resolves the
# ephemeral port in udp_accept(), so the ready file legitimately contains 0.
# A usable port is a non-zero decimal below 65536.
valid_port() {
case "$1" in
''|*[!0-9]*) return 1 ;;
esac
[ "$1" -gt 0 ] && [ "$1" -lt 65536 ]
}

# The test below depends on the server being able to report the port it bound.
# Besides the SINGLE_THREADED/WOLFSSL_TIRTOS case above, a NO_FILESYSTEM or
# NETOS build compiles write_ready_file() to a no-op and never creates the file
# at all. Probe the capability once and skip, rather than report a build-wide
# limitation as a CID test failure.
# Probe once whether the server can report the port it bound with -p 0 -R.
check_ready_file_support() {
local probe_pid counter port

Expand Down Expand Up @@ -115,7 +109,7 @@ check_ready_file_support() {

if ! check_ready_file_support; then
echo 'skipping dtlscid.test because the server cannot report an ephemeral' 1>&2
echo 'port with -p 0 -R (needs threads and a filesystem in the build).' 1>&2
echo 'port with -p 0 -R (needs a filesystem in the build).' 1>&2
exit 77
fi

Expand Down
100 changes: 24 additions & 76 deletions scripts/rsapss.test
Original file line number Diff line number Diff line change
Expand Up @@ -107,63 +107,6 @@ elif [ "${AM_BWRAPPED-}" != "yes" ]; then
unset AM_BWRAPPED
fi

# need a unique port since may run the same time as testsuite
# Track ports already assigned in this script run to prevent intra-run collisions
used_ports=()

generate_port() {
#-------------------------------------------------------------------------#
# Generate a random port number, guaranteed unique within this script run.
# Checks both the intra-run used_ports list and system-level bound ports.
#-------------------------------------------------------------------------#
local attempts=0 collision p

while true; do
if [[ "$OSTYPE" == "linux"* ]]; then
p=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
elif [[ "$OSTYPE" == "darwin"* ]]; then
p=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
else
echo "skipping due to unsupported OS"
exit 0
fi

# Check against ports already assigned in this run
collision=0
for up in "${used_ports[@]}"; do
if [ "$up" = "$p" ]; then
collision=1
break
fi
done

# Also check if the port is already bound on this system
if [ "$collision" -eq 0 ]; then
# TCP and UDP, established as well as listening, matched on the
# local port. -lnt saw only TCP listeners, so it was blind to the
# UDP sockets the DTLS cases here bind and to a port held by an
# unrelated outbound connection - both of which still block bind.
# A false positive only costs another iteration of this loop.
if command -v ss &>/dev/null; then
[ -n "$(ss -Hantu "sport = :${p}" 2>/dev/null)" ] && collision=1
elif command -v netstat &>/dev/null; then
netstat -an 2>/dev/null | grep -q "[:.]${p}[[:space:]]" && collision=1
fi
fi

[ "$collision" -eq 0 ] && break

((attempts++))
if [ "$attempts" -ge 100 ]; then
echo "ERROR: generate_port could not find a free port after 100 attempts"
exit 1
fi
done

port=$p
used_ports+=("$p")
}

WOLFSSL_SERVER=./examples/server/server
WOLFSSL_CLIENT=./examples/client/client

Expand Down Expand Up @@ -204,7 +147,7 @@ fail_case() {
echo "--- wolfSSL/OpenSSL server output ---"
cat rsapss.server.log
echo "--- client output ---"
cat rsapss.client.log
cat rsapss.client.log 2>/dev/null
fails=$((fails+1))
}

Expand All @@ -219,22 +162,25 @@ no_connection() {
grep -q "can't load" rsapss.server.log
}

# Wait up to five seconds for the server to bind $port: a fixed pause falls
# short on a loaded machine. Without ss or netstat, pause as before.
wait_for_server() {
# Wait up to five seconds for the server to report the port it bound.
wait_for_port() {
local i=0
port=""
while [ "$i" -lt 50 ]; do
if command -v ss >/dev/null 2>&1; then
[ -n "$(ss -Hantu "sport = :$port" 2>/dev/null)" ] && return
elif command -v netstat >/dev/null 2>&1; then
netstat -an 2>/dev/null | grep -q "[:.]${port}[[:space:]]" && return
else
sleep 1
return
fi
port="$("$@" 2>/dev/null)"
[ -n "$port" ] && return 0
kill -0 "$server_pid" 2>/dev/null || return 1
sleep 0.1
i=$((i+1))
done
return 1
}

no_port_case() {
kill $server_pid 2>/dev/null
wait $server_pid 2>/dev/null
fail_case "$1: server reported no port"
rm -f rsapss.server.log rsapss.ready
}

# SignatureScheme code point (RFC 8446 4.2.3) of a scheme name.
Expand Down Expand Up @@ -308,11 +254,12 @@ wire_sig_scheme() {
wolfssl_server_case() {
local name="$1" expect="$2" sargs="$3" cargs="$4" result
cases=$((cases+1))
generate_port
$TIMEOUT_KILL_2M $WOLFSSL_SERVER -p "$port" $SVR_NO_CRL $sargs \
rm -f rsapss.ready
$TIMEOUT_KILL_2M $WOLFSSL_SERVER -p 0 -R rsapss.ready $SVR_NO_CRL $sargs \
> rsapss.server.log 2>&1 &
server_pid=$!
wait_for_server
wait_for_port sed -n '/^[1-9][0-9]*$/p' rsapss.ready ||
{ no_port_case "$name"; return; }
echo "test connection" | $TIMEOUT_KILL_2M $OPENSSL s_client $V4V6_FLAG \
-connect "$LOCALHOST:$port" -msg -cipher DEFAULT:!kRSA \
-legacy_renegotiation $cargs > rsapss.client.log 2>&1
Expand All @@ -332,7 +279,7 @@ wolfssl_server_case() {
"$(scheme_code "$expect")" ]; then
fail_case "$name: server did not sign with $expect"
fi
rm -f rsapss.server.log rsapss.client.log
rm -f rsapss.server.log rsapss.client.log rsapss.ready
}

# OpenSSL s_server with client auth, wolfSSL client: $1 name, $2 scheme the
Expand All @@ -343,13 +290,14 @@ openssl_server_case() {
local name="$1" expect="$2" sargs="$3" cargs="$4" ccert="$5" cca="$6"
local result
cases=$((cases+1))
generate_port
$TIMEOUT_KILL_2M $OPENSSL s_server $V4V6_FLAG -www -accept "$port" \
rm -f rsapss.server.log
$TIMEOUT_KILL_2M $OPENSSL s_server $V4V6_FLAG -www -accept 0 \
-Verify 1 -CAfile "$cca" \
-cert "$CERT_DIR"/server-cert.pem -key "$CERT_DIR"/server-key.pem \
-msg $sargs > rsapss.server.log 2>&1 &
server_pid=$!
wait_for_server
wait_for_port sed -n 's/^ACCEPT .*:\([1-9][0-9]*\)$/\1/p' rsapss.server.log ||
{ no_port_case "$name"; return; }
$TIMEOUT_KILL_2M $WOLFSSL_CLIENT -p "$port" $CLI_NO_CRL -g \
-A "$CERT_DIR"/ca-cert.pem $ccert $cargs > rsapss.client.log 2>&1
result=$?
Expand Down
6 changes: 1 addition & 5 deletions wolfssl/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,7 @@ static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
err_sys_with_errno("tcp bind failed");

#if !defined(WOLFSSL_TIRTOS) && !defined(SINGLE_THREADED)
#if !defined(WOLFSSL_TIRTOS)
if (port == 0) {
socklen_t len = sizeof(addr);
if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
Expand All @@ -1990,7 +1990,6 @@ static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
#endif

if (args != NULL && args->signal != NULL) {
#ifndef SINGLE_THREADED
tcp_ready* ready = args->signal;
#ifdef WOLFSSL_COND
THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
Expand All @@ -2002,7 +2001,6 @@ static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
#endif
#endif /* !SINGLE_THREADED */
}
else {
fprintf(stderr, "args or args->signal was NULL. Not setting ready info.");
Expand Down Expand Up @@ -2032,7 +2030,6 @@ static WC_INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
if(do_listen) {
tcp_listen(sockfd, &port, useAnyAddr, udp, sctp);

#ifndef SINGLE_THREADED
/* signal ready to tcp_accept */
if (args)
ready = args->signal;
Expand All @@ -2047,7 +2044,6 @@ static WC_INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
#endif
}
#endif /* !SINGLE_THREADED */

if (ready_file) {
write_ready_file(args, port);
Expand Down
Loading