anbot

d608636 / test/sync.sh

5661 bytes

#!/bin/sh
set -eu

top="$(git rev-parse --show-toplevel)"
cli="$top/anbot.mjs"
scratch=$(mktemp -d)
portA=${ANBOT_TEST_SYNC_PORT_A:-18795}
portB=${ANBOT_TEST_SYNC_PORT_B:-18796}
shimPort=${ANBOT_TEST_SYNC_SHIM_PORT:-18797}
serverA=
serverB=
shim=
cleanup() {
  for pid in "$serverA" "$serverB" "$shim"; do
    if [ -n "$pid" ]; then
      kill "$pid" 2>/dev/null || :
      wait "$pid" 2>/dev/null || :
    fi
  done
  rm -r "$scratch"
}
trap cleanup EXIT
trap 'exit 1' INT TERM

await_file() {
  attempts=0
  until test -s "$1"; do
    attempts=$((attempts + 1))
    if [ "$attempts" -ge 50 ]; then
      printf 'Timed out waiting for %s\n' "$1" >&2
      return 1
    fi
    sleep 0.1
  done
}

start_shim() {
  node "$top/test/sync-shim.mjs" --upstream "http://127.0.0.1:$portA" --port "$shimPort" --mode "$1" \
    >"$scratch/shim.url" 2>"$scratch/shim.$1.log" &
  shim=$!
  await_file "$scratch/shim.url"
}

stop_shim() {
  kill "$shim" 2>/dev/null || :
  wait "$shim" 2>/dev/null || :
  shim=
  rm -f "$scratch/shim.url"
}

git init -q -b main --object-format=sha1 "$scratch/source"
printf 'v1\n' >"$scratch/source/file.txt"
git -C "$scratch/source" add .
git -C "$scratch/source" -c user.name=Test -c user.email=test@example.invalid commit -q -m v1
head1=$(git -C "$scratch/source" rev-parse HEAD)

node "$cli" publish --data "$scratch/a" --source "$scratch/source" --branch main --port "$portA" >/dev/null
node "$cli" identity --data "$scratch/a" >"$scratch/allowlist"
node "$cli" serve --data "$scratch/a" --port "$portA" --push-allowlist "$scratch/allowlist" >"$scratch/urla" &
serverA=$!
await_file "$scratch/urla"

# (1) Node B syncs from node A with no Git clone, then serves the same commit.
node "$cli" sync --data "$scratch/b" --url "http://127.0.0.1:$portA" >/dev/null
node "$cli" serve --data "$scratch/b" --port "$portB" >"$scratch/urlb" &
serverB=$!
await_file "$scratch/urlb"
git clone -q "$(cat "$scratch/urlb")" "$scratch/clone"
test "$(git -C "$scratch/clone" rev-parse HEAD)" = "$head1"

# (2) A development commit published on A reaches B through sync.
printf 'v2\n' >"$scratch/source/file.txt"
git -C "$scratch/source" add .
git -C "$scratch/source" -c user.name=Test -c user.email=test@example.invalid commit -q -m v2
head2=$(git -C "$scratch/source" rev-parse HEAD)
node "$cli" push --data "$scratch/a" --source "$scratch/source" --url "$(cat "$scratch/urla")"
node "$cli" sync --data "$scratch/b" --url "http://127.0.0.1:$portA" >/dev/null
git -C "$scratch/clone" fetch -q origin
test "$(git -C "$scratch/clone" rev-parse origin/main)" = "$head2"
git -C "$scratch/clone" merge-base --is-ancestor "$head1" "$head2"
curl -fsS "http://127.0.0.1:$portB/feed" | grep -F 'Pushed 1 commit'

restart_b() {
  kill "$serverB" 2>/dev/null || :
  wait "$serverB" 2>/dev/null || :
  serverB=
  rm -rf "$scratch/b/cache"
  rm -f "$scratch/urlb"
  node "$cli" serve --data "$scratch/b" --port "$portB" >"$scratch/urlb" &
  serverB=$!
  await_file "$scratch/urlb"
}

# (3a) A corrupt record offered over sync is rejected; the previous
# selection keeps serving after a restart with the cache deleted.
start_shim tamper-record
if node "$cli" sync --data "$scratch/b" --url "http://127.0.0.1:$shimPort" 2>"$scratch/reject-record.err"; then
  printf 'Sync accepted a corrupt record\n' >&2
  exit 1
fi
grep -F 'tampered record' "$scratch/shim.tamper-record.log"
stop_shim
restart_b
git clone -q "$(cat "$scratch/urlb")" "$scratch/still-old"
test "$(git -C "$scratch/still-old" rev-parse HEAD)" = "$head2"

# (3b) A corrupt block offered over sync is rejected the same way. A third
# publication gives B new blocks to fetch, so the shim is exercised.
printf 'v3\n' >"$scratch/source/file.txt"
git -C "$scratch/source" add .
git -C "$scratch/source" -c user.name=Test -c user.email=test@example.invalid commit -q -m v3
head3=$(git -C "$scratch/source" rev-parse HEAD)
node "$cli" publish --data "$scratch/a" --source "$scratch/source" --branch main --port "$portA" >/dev/null
start_shim tamper-block
if node "$cli" sync --data "$scratch/b" --url "http://127.0.0.1:$shimPort" 2>"$scratch/reject-block.err"; then
  printf 'Sync accepted a corrupt block\n' >&2
  exit 1
fi
grep -F 'tampered block' "$scratch/shim.tamper-block.log"
grep -F 'Corrupt AndFS block' "$scratch/reject-block.err"
stop_shim
restart_b
git clone -q "$(cat "$scratch/urlb")" "$scratch/still-old-2"
test "$(git -C "$scratch/still-old-2" rev-parse HEAD)" = "$head2"

# Retry after failure selects the new tip.
node "$cli" sync --data "$scratch/b" --url "http://127.0.0.1:$portA" >/dev/null
git -C "$scratch/clone" fetch -q origin
test "$(git -C "$scratch/clone" rev-parse origin/main)" = "$head3"
git -C "$scratch/clone" merge-base --is-ancestor "$head2" "$head3"
post=$(node "$cli" post --data "$scratch/a" --text "Post composed on A" |
  node -pe 'JSON.parse(require("node:fs").readFileSync(0)).id')
node "$cli" sync --data "$scratch/b" --url "http://127.0.0.1:$portA" >/dev/null
node "$cli" post --data "$scratch/b" --reply-to "$post" --text "Reply composed on B" >/dev/null
for attempt in 1 2; do
  node "$cli" sync --data "$scratch/a" --url "http://127.0.0.1:$portB" >/dev/null
  node "$cli" sync --data "$scratch/b" --url "http://127.0.0.1:$portA" >/dev/null
done
for port in "$portA" "$portB"; do
  feed=$(curl -fsS "http://127.0.0.1:$port/feed")
  test "$(printf '%s' "$feed" | grep -c 'Post composed on A')" -eq 1
  test "$(printf '%s' "$feed" | grep -c 'Reply composed on B')" -eq 1
  test "$(printf '%s' "$feed" | grep -c 'Pushed 1 commit')" -eq 1
done
printf 'PASS: peer sync served %s, propagated %s, rejected corrupt data, and retried to %s.\n' "$head1" "$head2" "$head3"

Git ยท Social feed