Skip to content

Commit e7c8dc2

Browse files
ejohnstownpadelsbach
authored andcommitted
tests: guard stop_wolfsshd so it cannot fail its caller
stop_wolfsshd killed $PID unconditionally. With the daemon already gone the kill failed, and under "set -e" that aborted the caller -- in sshd_forcedcmd_test.sh before PID was cleared, so the ForceCommand-SFTP scenario was silently skipped, the EXIT trap killed the dead pid a second time, and the script exited 1. - Guard on a non-empty PID, ignore a failed kill and return 0, so the function is safe to call from an EXIT trap. - Clear PID after stopping, so a second call cannot kill a recycled pid. - Remove the temp key dir even when no daemon was recorded, so a daemon that failed to start does not leak it. - Collapse sshd_forcedcmd_test.sh's cleanup() wrapper to a bare trap stop_wolfsshd EXIT now that the function guards itself. - Check the cd back to the test directory in sshd_x509_upn_fail.sh; the log it counts after the client run is the one there.
1 parent 8496451 commit e7c8dc2

3 files changed

Lines changed: 30 additions & 24 deletions

File tree

apps/wolfsshd/test/sshd_forcedcmd_test.sh

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@ TEST_PORT="$2"
1616
TEST_HOST="$1"
1717
source ./start_sshd.sh
1818

19-
# Stop the daemon on every exit path. From the "set -e" below onward an aborted
20-
# client run would otherwise leave a root daemon holding the shared test port,
21-
# and every later test in the suite would talk to this config.
22-
cleanup() {
23-
if [ -n "$PID" ]; then
24-
stop_wolfsshd
25-
PID=""
26-
fi
27-
return 0
28-
}
29-
trap cleanup EXIT
19+
# Stop the daemon on every exit path: the shell-login check below exits
20+
# non-zero, and from the "set -e" onward an aborted client run would leave a
21+
# root daemon holding the shared test port, so every later test in the suite
22+
# would talk to this config. stop_wolfsshd clears PID, so this is a no-op after
23+
# each explicit stop below.
24+
trap stop_wolfsshd EXIT
3025

3126
cat <<EOF > sshd_config_test_forcedcmd
3227
Port $TEST_PORT
@@ -66,7 +61,6 @@ echo exit | $TEST_SFTP -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p
6661

6762
cd "$TESTDIR"
6863
stop_wolfsshd
69-
PID=""
7064

7165
# A configured ForceCommand that is not "internal-sftp" must still permit the
7266
# SFTP subsystem. Only a certificate force-command denies file transfer, so a
@@ -94,7 +88,6 @@ echo exit | $TEST_SFTP -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p
9488

9589
cd "$TESTDIR"
9690
stop_wolfsshd
97-
PID=""
9891
exit 0
9992

10093

apps/wolfsshd/test/sshd_x509_upn_fail.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ echo "$TEST_CLIENT -X -c 'pwd' -u $3 -i $PRIVATE_KEY -J $PUBLIC_KEY -A $CA_CERT
4040
$TEST_CLIENT -X -c 'pwd' -u "$3" -i "$PRIVATE_KEY" -J "$PUBLIC_KEY" -A "$CA_CERT" -h "$1" -p "$2"
4141
RESULT=$?
4242

43-
cd "$TESTDIR"
43+
# Back to the test dir: the log counted below is the one here, so a failed cd
44+
# would silently count matches in the repository root's log.txt instead.
45+
cd "$TESTDIR" || exit 1
4446

4547
# Give the daemon child a moment to flush its rejection to the log.
4648
sleep 1

apps/wolfsshd/test/start_sshd.sh

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,33 @@ EOF
100100
printf "SSHD running on PID $PID\n"
101101
}
102102

103-
# closes down the sshd session taking argument $1 as the PID of the session
103+
# closes down the sshd session started by start_wolfsshd, using $PID.
104+
# Idempotent and safe to call from an EXIT trap: with no daemon recorded there
105+
# is nothing to kill, and neither an already-exited daemon nor a missing temp
106+
# dir may become the caller's exit status under "set -e".
104107
stop_wolfsshd() {
105-
printf "Stopping SSHD, killing pid $PID\n"
106-
sudo kill $PID
108+
if [ -n "$PID" ]; then
109+
printf "Stopping SSHD, killing pid $PID\n"
110+
sudo kill $PID || true
107111

108-
# Wait for the process to actually exit so a subsequent start_wolfsshd on
109-
# the same port doesn't race the listening socket's release (EADDRINUSE).
110-
for i in $(seq 1 50); do
111-
sudo kill -0 $PID 2>/dev/null || break
112-
sleep 0.1
113-
done
112+
# Wait for the process to actually exit so a subsequent start_wolfsshd on
113+
# the same port doesn't race the listening socket's release (EADDRINUSE).
114+
for i in $(seq 1 50); do
115+
sudo kill -0 $PID 2>/dev/null || break
116+
sleep 0.1
117+
done
118+
119+
# Cleared so a second call -- an EXIT trap after an explicit stop -- is
120+
# a no-op rather than a kill of whatever pid has since been recycled.
121+
PID=""
122+
fi
114123

115124
# The temp dir is owned by the invoking user, so its root-owned key copies
116-
# can be removed without sudo.
125+
# can be removed without sudo. Done even when no daemon was recorded, so a
126+
# daemon that failed to start does not leak it.
117127
if [ -n "$SSHD_KEYDIR" ]; then
118128
rm -rf "$SSHD_KEYDIR"
119129
SSHD_KEYDIR=""
120130
fi
131+
return 0
121132
}

0 commit comments

Comments
 (0)