diff --git a/scripts/ocsp-stapling.test b/scripts/ocsp-stapling.test index e7598a7e7a..b898dee412 100755 --- a/scripts/ocsp-stapling.test +++ b/scripts/ocsp-stapling.test @@ -482,20 +482,56 @@ fi 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 + # 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 - if [[ "$OSTYPE" == "linux"* || "$OSTYPE" == "msys" - || "$OSTYPE" == "cygwin"* ]]; then - port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512)) - elif [[ "$OSTYPE" == "darwin"* ]]; then - port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512)) - else - echo "Unknown OS TYPE" - exit 1 - fi + while true; do + if [[ "$OSTYPE" == "linux"* || "$OSTYPE" == "msys" + || "$OSTYPE" == "cygwin"* ]]; 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 "Unknown OS TYPE" + exit 1 + 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 + if command -v ss &>/dev/null; then + ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1 + elif command -v netstat &>/dev/null; then + netstat -lnt 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") } # Start OpenSSL server that has no OCSP responses to return diff --git a/scripts/ocsp-stapling2.test b/scripts/ocsp-stapling2.test index cbfdb2e68d..67d4a5df95 100755 --- a/scripts/ocsp-stapling2.test +++ b/scripts/ocsp-stapling2.test @@ -478,20 +478,56 @@ fi printf '%s\n\n' "Test successfully REVOKED!" # 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 + # 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 - if [[ "$OSTYPE" == "linux"* || "$OSTYPE" == "msys" - || "$OSTYPE" == "cygwin" ]]; then - port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512)) - elif [[ "$OSTYPE" == "darwin"* ]]; then - port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512)) - else - echo "Unknown OS TYPE" - exit 1 - fi + while true; do + if [[ "$OSTYPE" == "linux"* || "$OSTYPE" == "msys" + || "$OSTYPE" == "cygwin" ]]; 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 "Unknown OS TYPE" + exit 1 + 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 + if command -v ss &>/dev/null; then + ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1 + elif command -v netstat &>/dev/null; then + netstat -lnt 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") } # Start OpenSSL server that has no OCSP responses to return diff --git a/scripts/ocsp-stapling_tls13multi.test b/scripts/ocsp-stapling_tls13multi.test index dbdcb53f7c..c24a79876d 100755 --- a/scripts/ocsp-stapling_tls13multi.test +++ b/scripts/ocsp-stapling_tls13multi.test @@ -519,19 +519,55 @@ if [ "$dtls13" == "yes" ]; then 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 + # 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 - if [[ "$OSTYPE" == "linux"* ]]; then - port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512)) - elif [[ "$OSTYPE" == "darwin"* ]]; then - port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512)) - else - echo "Unknown OS TYPE" - exit 1 - fi + 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 "Unknown OS TYPE" + exit 1 + 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 + if command -v ss &>/dev/null; then + ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1 + elif command -v netstat &>/dev/null; then + netstat -lnt 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") } printf '%s\n\n' "------------------- TESTS COMPLETE ---------------------------" diff --git a/scripts/openssl.test b/scripts/openssl.test index 71b36386eb..c3ec09650c 100755 --- a/scripts/openssl.test +++ b/scripts/openssl.test @@ -31,19 +31,55 @@ fi echo "WOLFSSL_OPENSSL_TEST set, running test..." # 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 + # 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 - if [[ "$OSTYPE" == "linux"* ]]; then - port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512)) - elif [[ "$OSTYPE" == "darwin"* ]]; then - port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512)) - else - echo "Unknown OS TYPE" - exit 1 - fi + 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 "Unknown OS TYPE" + exit 1 + 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 + if command -v ss &>/dev/null; then + ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1 + elif command -v netstat &>/dev/null; then + netstat -lnt 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") } no_pid=-1 diff --git a/scripts/openssl_srtp.test b/scripts/openssl_srtp.test index 509db8a6c7..6ff9a53bb6 100755 --- a/scripts/openssl_srtp.test +++ b/scripts/openssl_srtp.test @@ -14,19 +14,55 @@ OPENSSL=${OPENSSL:="openssl"} WOLFSSL_CLIENT=${WOLFSSL_CLIENT:="./examples/client/client"} # 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 + # 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 - if [[ "$OSTYPE" == "linux"* ]]; then - port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512)) - elif [[ "$OSTYPE" == "darwin"* ]]; then - port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512)) - else - echo "Unknown OS TYPE" - exit 1 - fi + 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 "Unknown OS TYPE" + exit 1 + 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 + if command -v ss &>/dev/null; then + ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1 + elif command -v netstat &>/dev/null; then + netstat -lnt 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") } # get size of key material based on the profile diff --git a/scripts/rsapss.test b/scripts/rsapss.test index 09f7c394ee..54cb2fc3e3 100755 --- a/scripts/rsapss.test +++ b/scripts/rsapss.test @@ -53,19 +53,55 @@ elif [ "${AM_BWRAPPED-}" != "yes" ]; then 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 + # 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 - if [[ "$OSTYPE" == "linux"* ]]; then - port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512)) - elif [[ "$OSTYPE" == "darwin"* ]]; then - port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512)) - else - echo "skipping due to unsupported OS" - exit 0 - fi + 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 + if command -v ss &>/dev/null; then + ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1 + elif command -v netstat &>/dev/null; then + netstat -lnt 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