Files
splinter/tests/test_args.sh
T
lotherk 4ff2844955 main: a port that is not a port is refused, not guessed
atoi() took anything on --mcp-listen-port, --a2a-listen-port and
--openai-listen-port: "abc" bound port 0 (a random port the operator was
never told) and "-5" bound 65531. Both started a server on a port nobody
chose and said nothing.

port_arg() parses with strtol, requires the whole string, and refuses
anything outside 1..65535, naming the option and the value.

Test: tests/test_args.sh, new, in make check. Six bad values are refused
with the reason; a valid port still starts the gateway and says which one
it took. With the check reverted the test reports 6 failures, with it
"test_args: ok". make check: all tests passed.

docs/splinter.1: the two documented port options say so.
2026-11-23 12:37:42 +00:00

71 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
#
# splinter -- the command line refuses what it cannot honour.
#
# What it guards: a port was read with atoi(), so "--openai-listen-port abc"
# bound port 0 (a random port the operator was never told) and "-5" bound
# 65531. Both started a server on a port nobody chose and said nothing.
# Now a bad port is refused before anything binds, and the message names the
# option and the value.
set -u
cd "$(dirname "$0")/.." || exit 1
. "$(dirname "$0")/sh/port.sh"
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-args-XXXXXX") || exit 1
trap 'rm -rf "$D"' EXIT
fails=0
check() { d=$1; shift; if "$@"; then :; else echo "FAIL $d"; fails=$((fails + 1)); fi; }
# a port that is not a port: the run must fail, and say why
refuses() { # refuses <option> <value>
HOME="$D/home" ./splinter --no-config --script /dev/null "$1" "$2" \
>"$D/out" 2>"$D/err"
rc=$?
if [ "$rc" -eq 0 ]; then
echo "FAIL $1 $2 was accepted (rc=0)"
fails=$((fails + 1))
return 1
fi
if ! grep -q -- "$1 wants a port between 1 and 65535" "$D/err"; then
echo "FAIL $1 $2: the reason is missing"
sed -n '1,3p' "$D/err"
fails=$((fails + 1))
return 1
fi
return 0
}
mkdir -p "$D/home"
refuses --openai-listen-port abc
refuses --openai-listen-port -5
refuses --openai-listen-port 0
refuses --openai-listen-port 65536
refuses --a2a-listen-port ""
refuses --mcp-listen-port 8080x
# a port that IS a port still gets through: the refusal is not blanket, and
# the gateway says which port it took.
GOOD=$(free_port)
HOME="$D/home" ./splinter --no-config --model m \
--endpoint "http://127.0.0.1:1/v1/chat/completions" \
--openai-listen-port "$GOOD" --openai-listen 127.0.0.1 \
>"$D/out" 2>"$D/err" &
sleep 1
kill $! 2>/dev/null
wait $! 2>/dev/null
if grep -q "wants a port between" "$D/err"; then
echo "FAIL a valid port was refused"
fails=$((fails + 1))
fi
if ! grep -q "OpenAI gateway on http://127.0.0.1:$GOOD/v1" "$D/err"; then
echo "FAIL a valid port did not start the gateway"
sed -n '1,3p' "$D/err"
fails=$((fails + 1))
fi
if [ "$fails" -eq 0 ]; then
echo "test_args: ok"
exit 0
fi
echo "test_args: $fails failure(s)"
exit 1