#!/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" < "$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"