Files
lotherk 4774c2c7d8 dtchat: verify adapter files whose hash arrives at the end (T4.5)
Telegram and WhatsApp learn a document's SHA-256 only when the transfer is
finished, so their file-offer carries none and the digest comes with
file-end.  The UI used to verify only the offer hash, which meant those two
paths wrote the bytes unchecked.  accounts_run now takes the offer's hash
when there is one and the file-end's otherwise, hashes the .part file after
the last chunk and renames only on a match; a mismatch still removes the
part file and reports it in the status line and the transcript.

The fake adapter gained the matching modes: latehash offers without a hash
and sends the right one in file-end, badendhash sends a wrong one there.
tests/file_ui.sh drives all four shapes through the real UI under its own
Xvfb with DTCHAT_TEST_FILE_ACCEPT: files and latehash write the verified
hello world, badhash and badendhash are rejected and leave neither the file
nor a .part.  Measured with the built binaries: four ok, exit 0.

Harness entry DtChat/file_ui added; the full run is 53 ok/0 bad
(make mcde-host exit=0, build/logs/mcde-host-2026-11-17-endhash.log).
2026-11-17 13:08:55 +00:00

120 lines
3.2 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# file_ui.sh -- the UI's adapter file path against the fake adapter
#
# usage: tests/file_ui.sh PATH-TO-DTCHAT PATH-TO-FAKE-ADAPTER
#
# Four transfers, auto-accepted through DTCHAT_TEST_FILE_ACCEPT:
#
# files the offer carries the right SHA-256 -> file written
# badhash the offer carries a wrong SHA-256 -> no file
# latehash only file-end carries the right SHA-256 -> file written
# badendhash only file-end carries a wrong SHA-256 -> no file
#
# The last two are the Telegram/WhatsApp shape: the sender only knows the
# hash when the transfer is done, so the UI has to verify it at file-end.
# A rejected transfer must leave neither the final file nor the .part.
set -u
DTCHAT=${1:?usage: file_ui.sh PATH-TO-DTCHAT PATH-TO-FAKE-ADAPTER}
FAKE=${2:?usage: file_ui.sh PATH-TO-DTCHAT PATH-TO-FAKE-ADAPTER}
case $DTCHAT in
/*) ;;
*) DTCHAT=$(cd "$(dirname "$DTCHAT")" && pwd)/$(basename "$DTCHAT") ;;
esac
case $FAKE in
/*) ;;
*) FAKE=$(cd "$(dirname "$FAKE")" && pwd)/$(basename "$FAKE") ;;
esac
[ -x "$DTCHAT" ] || { echo "file_ui: no $DTCHAT" >&2; exit 2; }
[ -x "$FAKE" ] || { echo "file_ui: no $FAKE" >&2; exit 2; }
TMP=$(mktemp -d /tmp/dtchat-fileui-XXXXXX) || exit 2
DISPLAY_NUM=$((70 + $$ % 20))
XVFB=
APP=
cleanup() {
[ -n "$APP" ] && kill "$APP" 2>/dev/null
[ -n "$XVFB" ] && kill "$XVFB" 2>/dev/null
rm -rf "$TMP"
}
trap cleanup EXIT HUP INT TERM
Xvfb :$DISPLAY_NUM -screen 0 1024x768x24 > "$TMP/xvfb.log" 2>&1 &
XVFB=$!
sleep 1
fail=0
# run MODE: answers 0 when the log showed the expected outcome
run_mode() {
mode=$1
dir=$TMP/$mode
mkdir -p "$dir" || return 1
cat > "$dir/accounts.json" <<EOF
{"accounts":[{"id":"fake","protocol":"fake","label":"Fake $mode",
"adapter":"$FAKE","enabled":true,"settings":{"mode":"$mode"}}]}
EOF
echo '{"accounts":{}}' > "$dir/secrets.json"
DISPLAY=:$DISPLAY_NUM DTCHAT_ACCOUNTS_DEBUG=1 \
DTCHAT_TEST_FILE_ACCEPT=1 \
"$DTCHAT" --config "$dir" > "$TMP/$mode.log" 2>&1 &
APP=$!
i=0
while [ "$i" -lt 100 ]; do
if grep -q "file received:" "$TMP/$mode.log" 2>/dev/null ||
grep -q "checksum mismatch" "$TMP/$mode.log" 2>/dev/null; then
break
fi
kill -0 "$APP" 2>/dev/null || break
sleep 0.1
i=$((i + 1))
done
sleep 0.2
kill "$APP" 2>/dev/null
wait "$APP" 2>/dev/null
APP=
return 0
}
for mode in files badhash latehash badendhash; do
run_mode "$mode"
dir=$TMP/$mode
got=0
if [ -f "$dir/note.txt" ] &&
[ "$(cat "$dir/note.txt")" = "hello world" ]; then
got=1
fi
case $mode in
files|latehash)
if [ "$got" -eq 1 ]; then
echo "ok $mode: the file was written and verified"
else
echo "FAIL $mode: no verified note.txt" >&2
cat "$TMP/$mode.log" >&2
fail=1
fi
;;
*)
if [ "$got" -eq 1 ] ||
! grep -q "file rejected: checksum mismatch" \
"$TMP/$mode.log" 2>/dev/null; then
echo "FAIL $mode: a bad file was not rejected" >&2
cat "$TMP/$mode.log" >&2
fail=1
elif [ -e "$dir/note.txt.part" ]; then
echo "FAIL $mode: the .part file was left behind" >&2
fail=1
else
echo "ok $mode: rejected, no file left"
fi
;;
esac
done
[ "$fail" -eq 0 ] || exit 1
echo "tests/file_ui.sh: all ok"