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).
This commit is contained in:
1 parent
63fedd905a
commit
4774c2c7d8
3 files changed
+143
-5
No files matched your search
@@ -8,7 +8,9 @@
|
||||
* Settings {"mode":"crash"} makes it exit after connect so the manager's
|
||||
* backoff can be tested; {"mode":"garbage"} sends an invalid line;
|
||||
* {"mode":"files"} offers one file after going online and answers accept
|
||||
* with one data chunk and the end.
|
||||
* with one data chunk and the end ({"mode":"badhash"} offers a wrong hash,
|
||||
* {"mode":"latehash"}/{"mode":"badendhash"} send the hash only in the
|
||||
* end, right or wrong).
|
||||
*/
|
||||
#include "dc_internal.h"
|
||||
|
||||
@@ -54,6 +56,8 @@ send_file_offer(void)
|
||||
const char *sha = strcmp(mode, "badhash") == 0 ?
|
||||
"0000000000000000000000000000000000000000000000000000000000000000" :
|
||||
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
|
||||
int late = strcmp(mode, "latehash") == 0 ||
|
||||
strcmp(mode, "badendhash") == 0;
|
||||
|
||||
(void)dc_json_object_set(json, "type",
|
||||
dc_json_new_string("file-offer"));
|
||||
@@ -61,7 +65,9 @@ send_file_offer(void)
|
||||
(void)dc_json_object_set(json, "id", dc_json_new_string("f1"));
|
||||
(void)dc_json_object_set(json, "name", dc_json_new_string("note.txt"));
|
||||
(void)dc_json_object_set(json, "size", dc_json_new_int(11));
|
||||
(void)dc_json_object_set(json, "sha256", dc_json_new_string(sha));
|
||||
if (!late)
|
||||
(void)dc_json_object_set(json, "sha256",
|
||||
dc_json_new_string(sha));
|
||||
send_json(json);
|
||||
}
|
||||
|
||||
@@ -111,7 +117,9 @@ handle(DcJson *cmd)
|
||||
}
|
||||
send_state("online");
|
||||
if (strcmp(mode, "files") == 0 ||
|
||||
strcmp(mode, "badhash") == 0)
|
||||
strcmp(mode, "badhash") == 0 ||
|
||||
strcmp(mode, "latehash") == 0 ||
|
||||
strcmp(mode, "badendhash") == 0)
|
||||
send_file_offer();
|
||||
return;
|
||||
}
|
||||
@@ -190,6 +198,12 @@ handle(DcJson *cmd)
|
||||
(void)dc_json_object_set(end, "id",
|
||||
dc_json_new_string("f1"));
|
||||
(void)dc_json_object_set(end, "size", dc_json_new_int(11));
|
||||
if (strcmp(mode, "latehash") == 0)
|
||||
(void)dc_json_object_set(end, "sha256",
|
||||
dc_json_new_string("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"));
|
||||
else if (strcmp(mode, "badendhash") == 0)
|
||||
(void)dc_json_object_set(end, "sha256",
|
||||
dc_json_new_string("0000000000000000000000000000000000000000000000000000000000000000"));
|
||||
send_json(end);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -386,7 +386,9 @@ static void
|
||||
file_end(DtAccounts *accounts, AccountRun *run, const DcJson *event)
|
||||
{
|
||||
const char *id = dc_json_string(dc_json_get(event, "id"));
|
||||
const char *end_sha = dc_json_string(dc_json_get(event, "sha256"));
|
||||
Incoming *in = id != NULL ? incoming_find(accounts, run, id) : NULL;
|
||||
const char *expect;
|
||||
char line[700];
|
||||
DcConv *convp;
|
||||
|
||||
@@ -396,11 +398,14 @@ file_end(DtAccounts *accounts, AccountRun *run, const DcJson *event)
|
||||
fclose(in->fp);
|
||||
in->fp = NULL;
|
||||
}
|
||||
if (in->sha256[0] != '\0') {
|
||||
/* Telegram and WhatsApp learn the hash only when the transfer is done,
|
||||
* so the file-end may carry the first one we see */
|
||||
expect = in->sha256[0] != '\0' ? in->sha256 : end_sha;
|
||||
if (expect != NULL && *expect != '\0') {
|
||||
char got[65];
|
||||
|
||||
if (dc_file_sha256(in->part, got, NULL) != 0 ||
|
||||
strcasecmp(got, in->sha256) != 0) {
|
||||
strcasecmp(got, expect) != 0) {
|
||||
(void)unlink(in->part);
|
||||
set_account_status(accounts, run,
|
||||
"file rejected: checksum mismatch");
|
||||
|
||||
Executable
+119
@@ -0,0 +1,119 @@
|
||||
#!/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"
|
||||
Reference in new issue
Block a user