Files
allmende/scripts/e2e.sh
T
nilsandClaude Sonnet 5.5 e7bec4fb63 Add bootstrap nodes, DNS-based discovery, a DHT per region and NAT traversal
- Bootstrap: built-in /dnsaddr/allmende.stinnesbeck.com (TXT records at
  _dnsaddr.<name>), resolved at every redial with the last good answer kept;
  --bootstrap takes /p2p/ or /dnsaddr/ entries, --no-default-bootstrap drops
  the built-in one. Stopping never waits for a stuck resolver.
- Remembered peers in peers.json (saved every 5 min and on shutdown, redialled
  at the next start)
- Private Kademlia DHT (/allmende prefix), advertised and looked up per region
  with quick first attempts and a backoff; --no-dht turns it off
- NAT traversal: port mapping, hole punching, AutoRelay through bootstrap nodes
- --public: DHT server and relay for a bootstrap node; prints the TXT records
- Docker, NixOS module (public, defaultBootstrap, QUIC/UDP) and docs updated
- e2e: bootstrap node scenario, seed cache restart, and polling that really polls

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 22:14:34 +01:00

180 lines
13 KiB
Bash
Executable File

#!/usr/bin/env bash
# End-to-end test with two real nodes: publish, edit, renew, replicate,
# encrypted messaging with an address grant, and recovery after a "stolen" machine.
# The buyer is played by scripts/e2e/buyer.ts, which runs the browser's own
# crypto code under Node.js. Needs Go, Node.js (22.18+) and `make web-test`
# once (or `cd web && npm ci`) for the web dependencies. Run: make e2e
set -u
ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
[ -d "$ROOT/web/node_modules" ] || { echo "web dependencies missing: run 'cd web && npm ci' first"; exit 1; }
(cd "$ROOT" && go build -o bin/allmende ./cmd/allmende) || exit 1
BIN=$ROOT/bin/allmende
W=$(mktemp -d "${TMPDIR:-/tmp}/allmende-e2e.XXXXXX")
A=$W/a; B=$W/b
PIDS=()
cleanup() {
for p in "${PIDS[@]}"; do kill "$p" 2>/dev/null; done
wait 2>/dev/null
if [ -n "${E2E_KEEP:-}" ]; then echo "kept $W"; else rm -rf "$W"; fi
}
trap cleanup EXIT
ok() { echo "PASS: $*"; }
bad() { echo "FAIL: $*"; echo "--- node A log"; tail -15 "$W/a.log"; echo "--- node B log"; tail -15 "$W/b.log"; exit 1; }
wait_for() { # description, timeout, command...
local what=$1 t=$2; shift 2
for _ in $(seq 1 $((t*10))); do if "$@" >/dev/null 2>&1; then return 0; fi; sleep 0.1; done
bad "timed out: $what"
}
# check OP WANT COMMAND...: runs COMMAND each time it is polled and compares its output.
# (Writing test "$(command)" = x inside wait_for would run the command only once.)
check() {
local op=$1 want=$2 got; shift 2
got=$("$@" 2>/dev/null) || return 1
case $op in
eq) [ "$got" = "$want" ] ;;
ge) [ "$got" -ge "$want" ] 2>/dev/null ;;
esac
}
peers() { curl -s http://127.0.0.1:$1/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["peers"])'; }
DIFF=10
# --no-default-bootstrap: these nodes must not look up the real bootstrap name in DNS.
COMMON="--region u1x --no-mdns --no-default-bootstrap --difficulty $DIFF"
echo "== init"
$BIN init --data $A > $W/init-a.txt || bad "init A"
$BIN init --data $B > $W/init-b.txt || bad "init B"
grep -q "12 words" $W/init-a.txt && ok "init prints the recovery phrase"
PHRASE=$(grep -E '^ +[0-9]+\.' $W/init-a.txt | sed -E 's/[0-9]+\.//g' | tr -s ' \n' ' ' | sed 's/^ //; s/ $//')
[ "$(echo $PHRASE | wc -w)" = 12 ] && ok "captured 12-word phrase" || bad "phrase parse: '$PHRASE'"
[ ! -e $A/root.key ] && ! grep -rqi "$(echo $PHRASE | cut -d' ' -f1-3)" $A && ok "phrase is not stored in the data dir"
[ "$(stat -c %a $A/device.key)" = 600 ] && [ "$(stat -c %a $A/admin.token)" = 600 ] && ok "key and token are mode 0600"
$BIN init --data $A >/dev/null 2>&1 && bad "init must refuse to run twice" || ok "init refuses to overwrite an existing identity"
echo "== run two nodes"
$BIN run --data $A $COMMON --http 127.0.0.1:18101 --listen /ip4/127.0.0.1/tcp/14101 2> $W/a.log &
PIDS+=($!)
wait_for "A http" 10 curl -sf http://127.0.0.1:18101/api/v1/status
PEER=$(curl -s http://127.0.0.1:18101/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["peer_id"])')
$BIN run --data $B $COMMON --http 127.0.0.1:18102 --listen /ip4/127.0.0.1/tcp/14102 \
--bootstrap /ip4/127.0.0.1/tcp/14101/p2p/$PEER 2> $W/b.log &
PIDS+=($!)
wait_for "B http" 10 curl -sf http://127.0.0.1:18102/api/v1/status
wait_for "nodes connected" 15 check ge 1 peers 18101
ok "nodes connected"
echo "== publish on A, read on B"
TOKEN=$(cat $A/admin.token)
DRAFT='{"kind":"rent","category":"apartment","geohash":"u1xjhx","price":{"amount_cents":95000,"currency":"EUR","period":"month"},"area_m2":48,"rooms_x2":4,"title":"Helle Wohnung","description":"Ruhig."}'
curl -s -o /dev/null -w "%{http_code}" -X POST -d "$DRAFT" http://127.0.0.1:18101/api/v1/listings | grep -q 401 && ok "publishing without the token is refused"
RESP=$(curl -s -X POST -H "Authorization: Bearer $TOKEN" -d "$DRAFT" http://127.0.0.1:18101/api/v1/listings)
ID=$(echo "$RESP" | python3 -c 'import json,sys; print(json.load(sys.stdin)["id"])') || bad "create: $RESP"
ok "published listing $ID"
count_b() { curl -s "http://127.0.0.1:18102/api/v1/listings?geohash=u1x" | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))'; }
wait_for "B receives the listing" 15 check eq 1 count_b
ok "node B has the listing via gossip"
echo "== UI-style edit and renew on A (draft built by the UI's own code)"
RENEW=$ROOT/scripts/e2e/renew.ts
AUTHOR0=$(curl -s http://127.0.0.1:18101/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["author"])')
CUR=$(curl -s http://127.0.0.1:18101/api/v1/listings/$AUTHOR0/$ID)
EDIT=$(echo "$CUR" | node $RENEW "Helle Wohnung, renoviert" 20) || bad "draft from listing"
PUTR=$(curl -s -X PUT -H "Authorization: Bearer $TOKEN" -d "$EDIT" http://127.0.0.1:18101/api/v1/listings/$ID)
echo "$PUTR" | python3 -c 'import json,sys; l=json.load(sys.stdin); assert l["title"]=="Helle Wohnung, renoviert" and l["seq"]==2, l' && ok "edit accepted: new version, seq 2" || bad "edit refused: $PUTR"
RENEWED=$(echo "$PUTR" | node $RENEW "" 30) || bad "renew draft"
PUTR=$(curl -s -X PUT -H "Authorization: Bearer $TOKEN" -d "$RENEWED" http://127.0.0.1:18101/api/v1/listings/$ID)
echo "$PUTR" | python3 -c 'import json,sys; l=json.load(sys.stdin); assert l["seq"]==3 and l["expires_at"]-l["created_at"]>29*86400, l' && ok "renew accepted: seq 3, fresh 30-day term" || bad "renew refused: $PUTR"
seq_on_b() { curl -s http://127.0.0.1:18102/api/v1/listings/$AUTHOR0/$ID | python3 -c 'import json,sys; print(json.load(sys.stdin)["seq"])'; }
wait_for "B has the renewed version" 15 check eq 3 seq_on_b
ok "node B serves the renewed version"
MOVED=$(echo "$PUTR" | node $RENEW "" 30 | python3 -c 'import json,sys; d=json.load(sys.stdin); d["geohash"]="u28xyz"; print(json.dumps(d))')
curl -s -o /dev/null -w "%{http_code}" -X PUT -H "Authorization: Bearer $TOKEN" -d "$MOVED" http://127.0.0.1:18101/api/v1/listings/$ID | grep -q 400 && ok "moving a listing to another region is refused" || bad "region change was not refused"
echo "== messaging: buyer -> B -> A, reply A -> B -> buyer"
BUYER=$ROOT/scripts/e2e/buyer.ts
AUTHOR=$(curl -s http://127.0.0.1:18101/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["author"])')
SECRET=$(node $BUYER send http://127.0.0.1:18102 $AUTHOR $ID) || bad "buyer could not send via B"
ok "buyer posted an encrypted message to node B"
# Nobody has called A's API yet: the node itself must notice and tell its owner.
wait_for "A's terminal announces the new conversation" 30 grep -q 'msg="new conversation"' "$W/a.log"
grep 'msg="new conversation"' "$W/a.log" | grep -q 'listing="Helle Wohnung, renoviert"' && ok "owner's terminal names the listing" || bad "notice does not name the listing"
grep 'msg="new conversation"' "$W/a.log" | grep -q "open=http://127.0.0.1:18101/#/mine/$ID/" && ok "and links to the conversation" || bad "notice has no link"
grep -q "Ist die Wohnung noch frei" "$W/a.log" && bad "the buyer's text was printed to the terminal"
ok "the notice does not show what the buyer wrote"
[ "$(grep -c 'msg="new conversation"' "$W/a.log")" = 1 ] || bad "the notice was repeated"
inbox_a() { curl -s -H "Authorization: Bearer $TOKEN" http://127.0.0.1:18101/api/v1/inbox | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))'; }
wait_for "A receives the message" 20 check eq 1 inbox_a
ok "owner's node A decrypted it (via gossip through B)"
THREAD=$(curl -s -H "Authorization: Bearer $TOKEN" http://127.0.0.1:18101/api/v1/inbox | python3 -c 'import json,sys; print(json.load(sys.stdin)[0]["thread"])')
curl -s -H "Authorization: Bearer $TOKEN" http://127.0.0.1:18101/api/v1/inbox/$ID/$THREAD | grep -q "Ist die Wohnung noch frei" && ok "owner reads the buyer's text"
curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:18101/api/v1/inbox | grep -q 401 && ok "inbox needs the admin token"
curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:18101/api/v1/mine | grep -q 401 && ok "my-listings overview needs the admin token"
curl -s -H "Authorization: Bearer $TOKEN" http://127.0.0.1:18101/api/v1/mine | python3 -c 'import json,sys; m=json.load(sys.stdin); assert len(m)==1 and m[0]["threads"]==1 and m[0]["needs_reply"]==1, m' && ok "owner overview counts the request that needs a reply"
curl -s -o /dev/null -w "%{http_code}" -X PUT -H "Authorization: Bearer $TOKEN" -d '{"address":"Musterweg 7, 20095 Hamburg"}' http://127.0.0.1:18101/api/v1/listings/$ID/address | grep -qE "^(200|204)$" && ok "address stored privately on A"
curl -s http://127.0.0.1:18102/api/v1/listings/$AUTHOR/$ID | grep -q "Musterweg" && bad "address leaked into the public listing"
ok "address is not part of the public listing"
curl -s -o /dev/null -w "%{http_code}" -X POST -H "Authorization: Bearer $TOKEN" -d '{"type":"grant","address":"Musterweg 7, 20095 Hamburg","note":"Samstag 10 Uhr","valid_days":7}' http://127.0.0.1:18101/api/v1/inbox/$ID/$THREAD | grep -qE "^(200|201|202)$" || bad "grant reply refused"
ok "owner sent a signed address grant"
wait_for "buyer sees the grant via B" 20 bash -c "node $BUYER read http://127.0.0.1:18102 $AUTHOR $ID $SECRET | grep -q 'OK grant Musterweg'"
ok "buyer's browser code verified and read the grant from B"
node $BUYER read http://127.0.0.1:18102 $AUTHOR $ID $(node -e 'console.log("77".repeat(32))') | grep -q . && bad "another thread key saw a reply"
ok "a different thread key sees nothing"
echo "== recover on A (simulate a stolen machine)"
OLD_DEVICE=$(curl -s http://127.0.0.1:18101/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["device"])')
kill ${PIDS[0]}; wait ${PIDS[0]} 2>/dev/null
echo "$PHRASE" | $BIN recover --data $A --difficulty $DIFF > $W/recover.txt 2>&1 || { cat $W/recover.txt; bad "recover"; }
grep -q "1 of your listings were re-signed" $W/recover.txt && ok "recover re-signed the owner's listing" || { cat $W/recover.txt; bad "recover output"; }
ls $A | grep -q "device.key.revoked-" && ok "old device key was moved aside, not destroyed"
echo "wrong words here" | $BIN recover --data $A >/dev/null 2>&1 && bad "bad phrase accepted" || ok "recover rejects a wrong phrase"
# The recovered node has a new peer ID, so B's bootstrap entry for the old one
# is stale. Like a real recovered node, A dials out to its own bootstrap peer.
PEER_B=$(curl -s http://127.0.0.1:18102/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["peer_id"])')
$BIN run --data $A $COMMON --http 127.0.0.1:18101 --listen /ip4/127.0.0.1/tcp/14101 \
--bootstrap /ip4/127.0.0.1/tcp/14102/p2p/$PEER_B 2> $W/a.log &
PIDS[0]=$!
wait_for "A http again" 10 curl -sf http://127.0.0.1:18101/api/v1/status
NEW_DEVICE=$(curl -s http://127.0.0.1:18101/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["device"])')
[ "$OLD_DEVICE" != "$NEW_DEVICE" ] && ok "A runs with a new device key" || bad "device did not change"
OLDTOKEN=$TOKEN
curl -s -o /dev/null -w "%{http_code}" -X POST -H "Authorization: Bearer $OLDTOKEN" -d "$DRAFT" http://127.0.0.1:18101/api/v1/listings | grep -q 401 && ok "old admin token no longer works"
# B should learn of the revocation and the re-signed listing (seq 2, new device).
dev_on_b() { curl -s "http://127.0.0.1:18102/api/v1/listings?geohash=u1x" | python3 -c '
import json,sys
l=json.load(sys.stdin)
print(l[0]["device"] if len(l)==1 else "none")'; }
wait_for "B switches to the new device's version" 60 check eq "$NEW_DEVICE" dev_on_b
ok "node B now serves the listing re-signed by the new device (old device revoked network-wide)"
echo "== bootstrap node: two nodes that only know it find each other"
C=$W/c; X=$W/x; Y=$W/y
for d in $C $X $Y; do $BIN init --data $d >/dev/null || bad "init $d"; done
$BIN run --data $C $COMMON --public --http 127.0.0.1:18103 --listen /ip4/127.0.0.1/tcp/14103 2> $W/c.log &
PIDS+=($!)
wait_for "bootstrap node http" 10 curl -sf http://127.0.0.1:18103/api/v1/status
PEER_C=$(curl -s http://127.0.0.1:18103/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["peer_id"])')
grep -q "no public address" $W/c.log && ok "--public on loopback says it has no public address to publish" || bad "no hint about the missing public address"
$BIN run --data $X $COMMON --http 127.0.0.1:18104 --listen /ip4/127.0.0.1/tcp/14104 \
--bootstrap /ip4/127.0.0.1/tcp/14103/p2p/$PEER_C 2> $W/x.log &
XPID=$!; PIDS+=($XPID)
$BIN run --data $Y $COMMON --http 127.0.0.1:18105 --listen /ip4/127.0.0.1/tcp/14105 \
--bootstrap /ip4/127.0.0.1/tcp/14103/p2p/$PEER_C 2> $W/y.log &
PIDS+=($!)
wait_for "X and Y connected to the bootstrap node" 20 check ge 2 peers 18103
# Each of X and Y has two peers only if they found each other (C is the other one).
wait_for "X finds Y through the DHT" 60 check ge 2 peers 18104
wait_for "Y finds X through the DHT" 20 check ge 2 peers 18105
ok "X and Y found each other through the DHT, introduced by nobody"
echo "== a node remembers its peers: restart X without any bootstrap"
kill $XPID; wait $XPID 2>/dev/null
[ -s $X/peers.json ] && grep -q "$PEER_C" $X/peers.json && ok "peers were saved on shutdown" || bad "peers.json missing or without the bootstrap node"
$BIN run --data $X $COMMON --http 127.0.0.1:18104 --listen /ip4/127.0.0.1/tcp/14104 2> $W/x2.log &
PIDS+=($!)
wait_for "X restarted" 10 curl -sf http://127.0.0.1:18104/api/v1/status
wait_for "X reconnects from its remembered peers" 30 check ge 1 peers 18104
ok "X rejoined the network with no bootstrap configured"
echo "ALL E2E CHECKS PASSED"