Add e2e script to the repo, Dockerfile, and a Nix flake with a NixOS module
- scripts/e2e.sh (+ scripts/e2e/*.ts): the two-node end-to-end test, now self-contained and cleaning up after itself; `make e2e`, part of `make check` - Dockerfile/.dockerignore: static Go build in a non-root alpine image, data in /data, defaults to region u1j; `make docker` - flake.nix, nix/: package (buildGoModule), dev shell, NixOS module services.allmende (hardened systemd unit that waits for `allmende-admin init`), and a NixOS VM test run by `nix flake check` - README: running a node with Docker and NixOS Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Executable
+129
@@ -0,0 +1,129 @@
|
||||
#!/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"
|
||||
}
|
||||
DIFF=10
|
||||
COMMON="--region u1x --no-mdns --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 test "$(curl -s http://127.0.0.1:18101/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["peers"])')" -ge 1
|
||||
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 test "$(count_b)" = 1
|
||||
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"
|
||||
wait_for "B has the renewed version" 15 test "$(curl -s http://127.0.0.1:18102/api/v1/listings/$AUTHOR0/$ID | python3 -c 'import json,sys; print(json.load(sys.stdin)["seq"])')" = 3
|
||||
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"
|
||||
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 test "$(inbox_a)" = 1
|
||||
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 test "$(dev_on_b)" = "$NEW_DEVICE"
|
||||
ok "node B now serves the listing re-signed by the new device (old device revoked network-wide)"
|
||||
echo "ALL E2E CHECKS PASSED"
|
||||
Reference in New Issue
Block a user