Skip to content

Commit 10874ae

Browse files
committed
Nth attempt to resolve port collisions once-and-for-all
1 parent 215fe13 commit 10874ae

6 files changed

Lines changed: 272 additions & 56 deletions

File tree

scripts/ocsp-stapling.test

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -482,20 +482,56 @@ fi
482482
fi
483483

484484
# need a unique port since may run the same time as testsuite
485+
# Track ports already assigned in this script run to prevent intra-run collisions
486+
used_ports=()
487+
485488
generate_port() {
486489
#-------------------------------------------------------------------------#
487-
# Generate a random port number
490+
# Generate a random port number, guaranteed unique within this script run.
491+
# Checks both the intra-run used_ports list and system-level bound ports.
488492
#-------------------------------------------------------------------------#
493+
local attempts=0 collision p
494+
495+
while true; do
496+
if [[ "$OSTYPE" == "linux"* || "$OSTYPE" == "msys"
497+
|| "$OSTYPE" == "cygwin"* ]]; then
498+
p=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
499+
elif [[ "$OSTYPE" == "darwin"* ]]; then
500+
p=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
501+
else
502+
echo "Unknown OS TYPE"
503+
exit 1
504+
fi
489505

490-
if [[ "$OSTYPE" == "linux"* || "$OSTYPE" == "msys"
491-
|| "$OSTYPE" == "cygwin"* ]]; then
492-
port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
493-
elif [[ "$OSTYPE" == "darwin"* ]]; then
494-
port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
495-
else
496-
echo "Unknown OS TYPE"
497-
exit 1
498-
fi
506+
# Check against ports already assigned in this run
507+
collision=0
508+
for up in "${used_ports[@]}"; do
509+
if [ "$up" = "$p" ]; then
510+
collision=1
511+
break
512+
fi
513+
done
514+
515+
# Also check if the port is already bound on this system
516+
if [ $collision -eq 0 ]; then
517+
if command -v ss &>/dev/null; then
518+
ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
519+
elif command -v netstat &>/dev/null; then
520+
netstat -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
521+
fi
522+
fi
523+
524+
[ $collision -eq 0 ] && break
525+
526+
((attempts++))
527+
if [ $attempts -ge 100 ]; then
528+
echo "ERROR: generate_port could not find a free port after 100 attempts"
529+
exit 1
530+
fi
531+
done
532+
533+
port=$p
534+
used_ports+=("$p")
499535
}
500536

501537
# Start OpenSSL server that has no OCSP responses to return

scripts/ocsp-stapling2.test

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -478,20 +478,56 @@ fi
478478
printf '%s\n\n' "Test successfully REVOKED!"
479479

480480
# need a unique port since may run the same time as testsuite
481+
# Track ports already assigned in this script run to prevent intra-run collisions
482+
used_ports=()
483+
481484
generate_port() {
482485
#-------------------------------------------------------------------------#
483-
# Generate a random port number
486+
# Generate a random port number, guaranteed unique within this script run.
487+
# Checks both the intra-run used_ports list and system-level bound ports.
484488
#-------------------------------------------------------------------------#
489+
local attempts=0 collision p
490+
491+
while true; do
492+
if [[ "$OSTYPE" == "linux"* || "$OSTYPE" == "msys"
493+
|| "$OSTYPE" == "cygwin" ]]; then
494+
p=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
495+
elif [[ "$OSTYPE" == "darwin"* ]]; then
496+
p=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
497+
else
498+
echo "Unknown OS TYPE"
499+
exit 1
500+
fi
485501

486-
if [[ "$OSTYPE" == "linux"* || "$OSTYPE" == "msys"
487-
|| "$OSTYPE" == "cygwin" ]]; then
488-
port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
489-
elif [[ "$OSTYPE" == "darwin"* ]]; then
490-
port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
491-
else
492-
echo "Unknown OS TYPE"
493-
exit 1
494-
fi
502+
# Check against ports already assigned in this run
503+
collision=0
504+
for up in "${used_ports[@]}"; do
505+
if [ "$up" = "$p" ]; then
506+
collision=1
507+
break
508+
fi
509+
done
510+
511+
# Also check if the port is already bound on this system
512+
if [ $collision -eq 0 ]; then
513+
if command -v ss &>/dev/null; then
514+
ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
515+
elif command -v netstat &>/dev/null; then
516+
netstat -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
517+
fi
518+
fi
519+
520+
[ $collision -eq 0 ] && break
521+
522+
((attempts++))
523+
if [ $attempts -ge 100 ]; then
524+
echo "ERROR: generate_port could not find a free port after 100 attempts"
525+
exit 1
526+
fi
527+
done
528+
529+
port=$p
530+
used_ports+=("$p")
495531
}
496532

497533
# Start OpenSSL server that has no OCSP responses to return

scripts/ocsp-stapling_tls13multi.test

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -519,19 +519,55 @@ if [ "$dtls13" == "yes" ]; then
519519
fi
520520

521521
# need a unique port since may run the same time as testsuite
522+
# Track ports already assigned in this script run to prevent intra-run collisions
523+
used_ports=()
524+
522525
generate_port() {
523526
#-------------------------------------------------------------------------#
524-
# Generate a random port number
527+
# Generate a random port number, guaranteed unique within this script run.
528+
# Checks both the intra-run used_ports list and system-level bound ports.
525529
#-------------------------------------------------------------------------#
530+
local attempts=0 collision p
531+
532+
while true; do
533+
if [[ "$OSTYPE" == "linux"* ]]; then
534+
p=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
535+
elif [[ "$OSTYPE" == "darwin"* ]]; then
536+
p=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
537+
else
538+
echo "Unknown OS TYPE"
539+
exit 1
540+
fi
526541

527-
if [[ "$OSTYPE" == "linux"* ]]; then
528-
port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
529-
elif [[ "$OSTYPE" == "darwin"* ]]; then
530-
port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
531-
else
532-
echo "Unknown OS TYPE"
533-
exit 1
534-
fi
542+
# Check against ports already assigned in this run
543+
collision=0
544+
for up in "${used_ports[@]}"; do
545+
if [ "$up" = "$p" ]; then
546+
collision=1
547+
break
548+
fi
549+
done
550+
551+
# Also check if the port is already bound on this system
552+
if [ $collision -eq 0 ]; then
553+
if command -v ss &>/dev/null; then
554+
ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
555+
elif command -v netstat &>/dev/null; then
556+
netstat -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
557+
fi
558+
fi
559+
560+
[ $collision -eq 0 ] && break
561+
562+
((attempts++))
563+
if [ $attempts -ge 100 ]; then
564+
echo "ERROR: generate_port could not find a free port after 100 attempts"
565+
exit 1
566+
fi
567+
done
568+
569+
port=$p
570+
used_ports+=("$p")
535571
}
536572

537573
printf '%s\n\n' "------------------- TESTS COMPLETE ---------------------------"

scripts/openssl.test

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,55 @@ fi
3131
echo "WOLFSSL_OPENSSL_TEST set, running test..."
3232

3333
# need a unique port since may run the same time as testsuite
34+
# Track ports already assigned in this script run to prevent intra-run collisions
35+
used_ports=()
36+
3437
generate_port() {
3538
#-------------------------------------------------------------------------#
36-
# Generate a random port number
39+
# Generate a random port number, guaranteed unique within this script run.
40+
# Checks both the intra-run used_ports list and system-level bound ports.
3741
#-------------------------------------------------------------------------#
42+
local attempts=0 collision p
3843

39-
if [[ "$OSTYPE" == "linux"* ]]; then
40-
port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
41-
elif [[ "$OSTYPE" == "darwin"* ]]; then
42-
port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
43-
else
44-
echo "Unknown OS TYPE"
45-
exit 1
46-
fi
44+
while true; do
45+
if [[ "$OSTYPE" == "linux"* ]]; then
46+
p=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
47+
elif [[ "$OSTYPE" == "darwin"* ]]; then
48+
p=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
49+
else
50+
echo "Unknown OS TYPE"
51+
exit 1
52+
fi
53+
54+
# Check against ports already assigned in this run
55+
collision=0
56+
for up in "${used_ports[@]}"; do
57+
if [ "$up" = "$p" ]; then
58+
collision=1
59+
break
60+
fi
61+
done
62+
63+
# Also check if the port is already bound on this system
64+
if [ $collision -eq 0 ]; then
65+
if command -v ss &>/dev/null; then
66+
ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
67+
elif command -v netstat &>/dev/null; then
68+
netstat -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
69+
fi
70+
fi
71+
72+
[ $collision -eq 0 ] && break
73+
74+
((attempts++))
75+
if [ $attempts -ge 100 ]; then
76+
echo "ERROR: generate_port could not find a free port after 100 attempts"
77+
exit 1
78+
fi
79+
done
80+
81+
port=$p
82+
used_ports+=("$p")
4783
}
4884

4985
no_pid=-1

scripts/openssl_srtp.test

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,55 @@ OPENSSL=${OPENSSL:="openssl"}
1414
WOLFSSL_CLIENT=${WOLFSSL_CLIENT:="./examples/client/client"}
1515

1616
# need a unique port since may run the same time as testsuite
17+
# Track ports already assigned in this script run to prevent intra-run collisions
18+
used_ports=()
19+
1720
generate_port() {
1821
#-------------------------------------------------------------------------#
19-
# Generate a random port number
22+
# Generate a random port number, guaranteed unique within this script run.
23+
# Checks both the intra-run used_ports list and system-level bound ports.
2024
#-------------------------------------------------------------------------#
25+
local attempts=0 collision p
26+
27+
while true; do
28+
if [[ "$OSTYPE" == "linux"* ]]; then
29+
p=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
30+
elif [[ "$OSTYPE" == "darwin"* ]]; then
31+
p=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
32+
else
33+
echo "Unknown OS TYPE"
34+
exit 1
35+
fi
36+
37+
# Check against ports already assigned in this run
38+
collision=0
39+
for up in "${used_ports[@]}"; do
40+
if [ "$up" = "$p" ]; then
41+
collision=1
42+
break
43+
fi
44+
done
45+
46+
# Also check if the port is already bound on this system
47+
if [ $collision -eq 0 ]; then
48+
if command -v ss &>/dev/null; then
49+
ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
50+
elif command -v netstat &>/dev/null; then
51+
netstat -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
52+
fi
53+
fi
54+
55+
[ $collision -eq 0 ] && break
56+
57+
((attempts++))
58+
if [ $attempts -ge 100 ]; then
59+
echo "ERROR: generate_port could not find a free port after 100 attempts"
60+
exit 1
61+
fi
62+
done
2163

22-
if [[ "$OSTYPE" == "linux"* ]]; then
23-
port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
24-
elif [[ "$OSTYPE" == "darwin"* ]]; then
25-
port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
26-
else
27-
echo "Unknown OS TYPE"
28-
exit 1
29-
fi
64+
port=$p
65+
used_ports+=("$p")
3066
}
3167

3268
# get size of key material based on the profile

scripts/rsapss.test

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,55 @@ elif [ "${AM_BWRAPPED-}" != "yes" ]; then
5353
fi
5454

5555
# need a unique port since may run the same time as testsuite
56+
# Track ports already assigned in this script run to prevent intra-run collisions
57+
used_ports=()
58+
5659
generate_port() {
5760
#-------------------------------------------------------------------------#
58-
# Generate a random port number
61+
# Generate a random port number, guaranteed unique within this script run.
62+
# Checks both the intra-run used_ports list and system-level bound ports.
5963
#-------------------------------------------------------------------------#
64+
local attempts=0 collision p
6065

61-
if [[ "$OSTYPE" == "linux"* ]]; then
62-
port=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
63-
elif [[ "$OSTYPE" == "darwin"* ]]; then
64-
port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
65-
else
66-
echo "skipping due to unsupported OS"
67-
exit 0
68-
fi
66+
while true; do
67+
if [[ "$OSTYPE" == "linux"* ]]; then
68+
p=$(($(od -An -N2 /dev/urandom) % (65535-49512) + 49512))
69+
elif [[ "$OSTYPE" == "darwin"* ]]; then
70+
p=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
71+
else
72+
echo "skipping due to unsupported OS"
73+
exit 0
74+
fi
75+
76+
# Check against ports already assigned in this run
77+
collision=0
78+
for up in "${used_ports[@]}"; do
79+
if [ "$up" = "$p" ]; then
80+
collision=1
81+
break
82+
fi
83+
done
84+
85+
# Also check if the port is already bound on this system
86+
if [ $collision -eq 0 ]; then
87+
if command -v ss &>/dev/null; then
88+
ss -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
89+
elif command -v netstat &>/dev/null; then
90+
netstat -lnt 2>/dev/null | grep -q ":${p}[[:space:]]" && collision=1
91+
fi
92+
fi
93+
94+
[ $collision -eq 0 ] && break
95+
96+
((attempts++))
97+
if [ $attempts -ge 100 ]; then
98+
echo "ERROR: generate_port could not find a free port after 100 attempts"
99+
exit 1
100+
fi
101+
done
102+
103+
port=$p
104+
used_ports+=("$p")
69105
}
70106

71107
WOLFSSL_SERVER=./examples/server/server

0 commit comments

Comments
 (0)