tests: isolate the ack directory, fail loudly on a dead fake

test_ack used one fixed /var/tmp path; a leftover or a second run could
delete a request under another. It now mkdtemps a private directory (and
one for the world-writable case) and waits 10 s instead of 5.

test_gateway and test_ui check that the fake Bot API really started
(readiness plus kill -0) and print its log instead of racing a dead
server; the gateway waits 60 s for a turn under load. test_ui lets the
input process the typed keys before Ctrl-L instead of assuming one
delivery order.

Full make check green.
This commit is contained in:
lotherk committed 2026-11-10 08:07:24 +00:00
1 parent 6d9c74e3b1
commit 49d18361e2
3 files changed
+68 -36

No files matched your search

+24 -17
View File
@@ -41,12 +41,14 @@ int
main(void)
{
char out[2048], who[64];
char dir[] = "/var/tmp/splinter-ack-XXXXXX";
char open_dir[] = "/var/tmp/splinter-ack-open-XXXXXX";
struct sp_buf log;
int id, yes;
pthread_t th;
(void)setenv("SPLINTER_ACK_DIR", "/var/tmp/splinter_test_ack", 1);
(void)system("rm -rf /var/tmp/splinter_test_ack");
CHECK(mkdtemp(dir) != NULL, "a private ack directory");
(void)setenv("SPLINTER_ACK_DIR", dir, 1);
id = sp_ack_request("run", "{\"command\":\"ls\"}", "mutate", 5);
CHECK(id > 0, "request is written and returns an id");
@@ -55,7 +57,7 @@ main(void)
answer_id = id;
answer_yes = 1;
(void)pthread_create(&th, NULL, answerer, NULL);
yes = sp_ack_wait(id, 5, who, sizeof(who));
yes = sp_ack_wait(id, 10, who, sizeof(who));
(void)pthread_join(th, NULL);
CHECK(yes == 1, "an answer of yes is read back");
CHECK(strcmp(who, "test-phone") == 0, "the answer says who gave it");
@@ -64,7 +66,7 @@ main(void)
answer_id = id;
answer_yes = 0;
(void)pthread_create(&th, NULL, answerer, NULL);
yes = sp_ack_wait(id, 5, who, sizeof(who));
yes = sp_ack_wait(id, 10, who, sizeof(who));
(void)pthread_join(th, NULL);
CHECK(yes == 0, "an answer of no is read back as no");
@@ -123,21 +125,23 @@ main(void)
* number.
*/
{
const char *chan = "/var/tmp/splinter_test_ack/chan.sh";
const char *got = "/var/tmp/splinter_test_ack/chan.out";
const char *mark = "/var/tmp/splinter_test_ack/pwned";
FILE *f = fopen(chan, "w");
char chan[700], got[700], mark[700], args[1200];
FILE *f;
char msg[1024] = "";
snprintf(chan, sizeof(chan), "%s/chan.sh", dir);
snprintf(got, sizeof(got), "%s/chan.out", dir);
snprintf(mark, sizeof(mark), "%s/pwned", dir);
f = fopen(chan, "w");
if (f != NULL) {
fprintf(f, "#!/bin/sh\nprintf '%%s' \"$1\" > %s\n", got);
fclose(f);
}
(void)chmod(chan, 0700);
(void)setenv("SPLINTER_ACK_CHANNELS", chan, 1);
id = sp_ack_request("run",
"{\"command\":\"x'; touch /var/tmp/splinter_test_ack/pwned; '\"}",
"destructive", 5);
snprintf(args, sizeof(args),
"{\"command\":\"x'; touch %s; '\"}", mark);
id = sp_ack_request("run", args, "destructive", 5);
CHECK(sp_ack_notify(id, NULL) == 1, "the channel was fired");
f = fopen(got, "r");
if (f != NULL) {
@@ -156,15 +160,18 @@ main(void)
}
/* ---- a directory others may write into is not trusted with a yes ---- */
(void)system("rm -rf /var/tmp/splinter_test_ack_open");
(void)system("mkdir -p /var/tmp/splinter_test_ack_open && "
"chmod 0777 /var/tmp/splinter_test_ack_open");
(void)setenv("SPLINTER_ACK_DIR", "/var/tmp/splinter_test_ack_open", 1);
CHECK(mkdtemp(open_dir) != NULL, "a directory for the permission test");
(void)chmod(open_dir, 0777);
(void)setenv("SPLINTER_ACK_DIR", open_dir, 1);
CHECK(sp_ack_request("run", "x", "destructive", 1) < 0,
"a world-writable ack directory is refused");
(void)system("rm -rf /var/tmp/splinter_test_ack_open");
{
char cmd[700];
snprintf(cmd, sizeof(cmd), "rm -rf '%s' '%s'", dir, open_dir);
(void)system(cmd);
}
printf("\n%d checks, %d failures\n", checks, fails);
(void)system("rm -rf /var/tmp/splinter_test_ack");
return fails == 0 ? 0 : 1;
}
+27 -13
View File
@@ -12,6 +12,14 @@ D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-gw-XXXXXX") || exit 1
trap 'rm -rf "$D"' EXIT
fails=0
wait_fake() { # $1 = port; 0 when something accepts a connection
for i in $(seq 1 100); do
curl -s -o /dev/null "http://127.0.0.1:$1/x" && return 0
sleep 0.2
done
return 1
}
run() { # $1 = answer word; leaves $D/sent.log and maybe $D/out.txt
P=$((20000 + ($$ + ${2:-0}) % 30000))
rm -rf "$D/home" "$D/out.txt" "$D/sent.log"
@@ -20,19 +28,22 @@ run() { # $1 = answer word; leaves $D/sent.log and maybe $D/out.txt
chmod +x "$D/home/.splinter/commands/hello"
printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\nplugin = plugins/focus/focus.so\n' \
"$P" > "$D/home/splinter.conf"
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" "$1" &
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" "$1" \
> "$D/fake.out" 2>&1 &
S=$!
for i in $(seq 1 50); do
curl -s -o /dev/null "http://127.0.0.1:$P/x" && break
sleep 0.2
done
if ! wait_fake "$P" || ! kill -0 "$S" 2>/dev/null; then
echo "FAIL fake telegram did not start on $P"
cat "$D/fake.out" 2>/dev/null
fails=$((fails + 1))
return 1
fi
HOME="$D/home" SPLINTER_LOCALE=de SPLINTER_TELEGRAM_TOKEN=tok \
SPLINTER_TELEGRAM_API="http://127.0.0.1:$P" \
SPLINTER_ACK_DIR="$D/home/ack" SPLINTER_ACK_TTL=20 \
./splinter --config "$D/home/splinter.conf" --daemon \
> "$D/gw.out" 2>&1 &
G=$!
for i in $(seq 1 40); do
for i in $(seq 1 120); do
grep -q '^fertig' "$D/sent.log" 2>/dev/null && break
sleep 0.5
done
@@ -73,18 +84,21 @@ run_text() { # $1 = first message, $2 = line to wait for
chmod +x "$D/home/.splinter/commands/hello"
printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\n' \
"$P" > "$D/home/splinter.conf"
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok "$1" &
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok "$1" \
> "$D/fake.out" 2>&1 &
S=$!
for i in $(seq 1 50); do
curl -s -o /dev/null "http://127.0.0.1:$P/x" && break
sleep 0.2
done
if ! wait_fake "$P" || ! kill -0 "$S" 2>/dev/null; then
echo "FAIL fake telegram did not start on $P"
cat "$D/fake.out" 2>/dev/null
fails=$((fails + 1))
return 1
fi
HOME="$D/home" SPLINTER_LOCALE=de SPLINTER_TELEGRAM_TOKEN=tok \
SPLINTER_TELEGRAM_API="http://127.0.0.1:$P" \
./splinter --config "$D/home/splinter.conf" --daemon \
> "$D/gw.out" 2>&1 &
G=$!
for i in $(seq 1 30); do
for i in $(seq 1 120); do
grep -q "$2" "$D/sent.log" 2>/dev/null && break
sleep 0.5
done
@@ -102,7 +116,7 @@ check "an unknown command is an error" \
grep -q '^unbekanntes Kommando: /nosuch' "$D/sent.log"
check "an unknown command does not start a model turn" test ! -e "$D/out.txt"
# /interview asks the seven questions and writes the profile
# /interview asks the six questions and writes the profile
run_text "/interview" '^Profil geschrieben' 30
prof=$(ls "$D/home/.splinter/profile/"*.md 2>/dev/null | head -1)
check "the interview wrote a profile" test -n "$prof"
+17 -6
View File
@@ -41,7 +41,7 @@ trap cleanup EXIT
check() { if "$@"; then :; else echo "FAIL $*"; fails=$((fails + 1)); fi; }
shows() { # wait until the screen contains $1
for i in $(seq 1 30); do
for i in $(seq 1 60); do
tmux capture-pane -t $S -p | grep -q -- "$1" && return 0
sleep 0.5
done
@@ -53,12 +53,19 @@ start() { # a fresh fake model and a fresh UI
P=$((20000 + ($$ + $1) % 30000))
rm -rf "$D/home" "$D/out.txt" "$D/rc"
mkdir -p "$D/home"
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok &
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok \
> "$D/fake.out" 2>&1 &
F=$!
for i in $(seq 1 50); do
curl -s -o /dev/null "http://127.0.0.1:$P/x" && break
ready=0
for i in $(seq 1 100); do
curl -s -o /dev/null "http://127.0.0.1:$P/x" && { ready=1; break; }
sleep 0.2
done
if [ "$ready" -ne 1 ] || ! kill -0 "$F" 2>/dev/null; then
echo "FAIL fake telegram did not start on $P"
cat "$D/fake.out" 2>/dev/null
exit 1
fi
tmux new-session -d -s $S -x 110 -y 30 "HOME=$D/home SPLINTER_LOCALE=en \
./splinter --no-config --endpoint http://127.0.0.1:$P/v1/chat/completions \
--model m --chat 2>/dev/null; echo \$? > $D/rc"
@@ -82,9 +89,13 @@ check shows 'changes files'
key y
check shows 'fertig'
check grep -q 'vom gateway' "$D/out.txt"
typed 'halb'; key C-l; sleep 1
typed 'halb'
check shows 'halb' # let the UI consume the keys
key C-l; sleep 1
check shows '^>halb'
key BSpace BSpace; key C-l; sleep 1
key BSpace BSpace
check shows '^>ha *$' # the backspaces arrived
key C-l; sleep 1
check shows '^>ha *$'
typed 'zeig eine tabelle'; key Enter
check shows '| a | b |'