Skip to content
pi-c Artifact [5fba337664]
Login

Artifact [5fba337664]

Artifact 5fba337664ed2714204ca90d3c3f9ae36428ddf1bc188ad145e6ec3a048fbfeb:


#!/bin/sh
# All transports are fake; the suite never contacts a host or needs privileges.
set -eu
ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
TEST_ROOT=$(mktemp -d "${TMPDIR:-/tmp}/pi-c-remote-test.XXXXXX")
trap 'rm -rf -- "$TEST_ROOT"' EXIT HUP INT TERM
mkdir "$TEST_ROOT/bin"
printf '#!/bin/sh\nprintf "unexpected transport\\n" >&2\nexit 99\n' > "$TEST_ROOT/bin/ssh"
chmod +x "$TEST_ROOT/bin/ssh"
PATH="$TEST_ROOT/bin:$PATH"
export PATH
RUN="$ROOT/tools/remote/run.sh"
nonce=0123456789abcdef

sh "$RUN" prepare "$TEST_ROOT/run" "$nonce" noop
[ -f "$TEST_ROOT/run/manifest" ]
grep -Fx 'host=hetzner-hel' "$TEST_ROOT/run/manifest"
grep -Fx 'owner=pi-c-s0-0123456789abcdef-owner.service' "$TEST_ROOT/run/manifest"
grep -Fx 'worker_seconds=300' "$TEST_ROOT/run/manifest"
grep -Fx 'lease_seconds=600' "$TEST_ROOT/run/manifest"
if sh "$RUN" prepare "$TEST_ROOT/bad" 'bad;echo unsafe' noop; then
    printf 'FAIL: unsafe nonce accepted\n' >&2
    exit 1
fi
if sh "$RUN" prepare "$TEST_ROOT/run" "$nonce" noop; then
    printf 'FAIL: local collision accepted\n' >&2
    exit 1
fi
printf 'remote lifecycle: manifest validation passed\n'

# Execute the exact cleanup body locally, faking only Linux identity/stat output.
printf '#!/bin/sh\nprintf "1000\\n"\n' > "$TEST_ROOT/bin/id"
cat > "$TEST_ROOT/bin/stat" <<'STAT'
#!/bin/sh
set -eu
[ "$1" = -c ]
shift 2
[ "$1" != -- ] || shift
path=$1
if [ "${BAD_OWNER:-}" = "$path" ]; then
    printf '0:0:700\n'
    exit
fi
mode=$(/usr/bin/stat -f '%Lp' "$path")
printf '1000:1000:%s\n' "$mode"
STAT
chmod +x "$TEST_ROOT/bin/id" "$TEST_ROOT/bin/stat"
TEST_ROOT=$(CDPATH='' cd -- "$TEST_ROOT" && pwd -P)
home="$TEST_ROOT/home"
mkdir -m 700 "$home"
workspace="$home/.pi-c-s0-$nonce"
CLEAN="$ROOT/tools/remote/cleanup.sh"
sh "$CLEAN" "$home" "$workspace" "$nonce"
mkdir -m 700 "$workspace"
printf '%s\n' "$nonce" > "$workspace/.pi-c-owner"
chmod 600 "$workspace/.pi-c-owner"
printf 'evidence\n' > "$workspace/log"
BAD_OWNER=$workspace
export BAD_OWNER
if sh "$CLEAN" "$home" "$workspace" "$nonce"; then exit 1; fi
[ -f "$workspace/log" ]
unset BAD_OWNER
sh "$CLEAN" "$home" "$workspace" "$nonce"
[ ! -e "$workspace" ]
sh "$CLEAN" "$home" "$workspace" "$nonce"
mkdir -m 700 "$TEST_ROOT/outside"
ln -s "$TEST_ROOT/outside" "$workspace"
if sh "$CLEAN" "$home" "$workspace" "$nonce"; then exit 1; fi
[ -d "$TEST_ROOT/outside" ]
rm "$workspace"
mkdir -m 700 "$workspace"
printf 'different\n' > "$workspace/.pi-c-owner"
chmod 600 "$workspace/.pi-c-owner"
if sh "$CLEAN" "$home" "$workspace" "$nonce"; then exit 1; fi
[ -d "$workspace" ]
printf 'remote lifecycle: cleanup refusal and idempotence passed\n'

# A native compiler stands in only for the Fil-C invocation, not Fil-C safety evidence.
mkdir "$TEST_ROOT/payload"
cp -R "$ROOT/test" "$TEST_ROOT/payload/test"
(
    cd "$TEST_ROOT/payload"
    sh "$ROOT/tools/remote/payload.sh" filc "$(command -v cc)" > worker.log 2>&1
    grep -Fx PASS result.txt
    grep -Fx 'qualification: 10 tests passed' worker.log
    grep -Fx 'oracle-mutant: 8 verdicts checked' worker.log
    grep -F 'replay: 7 files,' worker.log
)
printf 'remote lifecycle: payload fixture interface passed (native stand-in only)\n'

# The bounded-step helper: a hang records a timeout FAIL and the run moves on.
if ! command -v timeout > /dev/null 2>&1; then
    # Minimal stand-in for coreutils `timeout -k K N CMD` where it is absent (macOS).
    cat > "$TEST_ROOT/bin/timeout" <<'SHIM'
#!/bin/sh
shift 2
n=$1
shift
exec perl -e '$n = shift; $p = fork; if (!$p) { exec @ARGV or exit 127 }
$SIG{ALRM} = sub { kill 9, $p; waitpid $p, 0; exit 124 }; alarm $n; waitpid $p, 0; exit $? >> 8' "$n" "$@"
SHIM
    chmod +x "$TEST_ROOT/bin/timeout"
fi
{
    sed -n '/^STEP_SECONDS=/,/^}$/p' "$ROOT/tools/remote/payload.sh" > "$TEST_ROOT/step.sh"
    cat >> "$TEST_ROOT/step.sh" <<'STEPS'
step ok 5 true
step hang 1 sleep 30
step bad 5 sh -c 'exit 3'
PIC_BUDGET_SECONDS=10
step late 5 true
printf 'fails=%s\n' "$fails"
STEPS
    sh "$TEST_ROOT/step.sh" > "$TEST_ROOT/step.out"
    grep -F 'FAIL: hang timed out after 1s' "$TEST_ROOT/step.out"
    grep -F 'FAIL: bad exited 3' "$TEST_ROOT/step.out"
    grep -F 'FAIL: late skipped: overall budget' "$TEST_ROOT/step.out"
    grep -E 'STEP END ok rc=0 secs=[0-9]+$' "$TEST_ROOT/step.out"
    grep -Fx 'fails=3' "$TEST_ROOT/step.out"
    printf 'remote lifecycle: step timeout, budget and timestamps passed\n'
}

rm -rf "$workspace"
FAKE_STATE="$TEST_ROOT/supervisor"
FAKE_HOME=$home
FAKE_WORKSPACE=$workspace
FAKE_NONCE=$nonce
FAKE_CLEANUP=$CLEAN
export FAKE_STATE FAKE_HOME FAKE_WORKSPACE FAKE_NONCE FAKE_CLEANUP
mkdir "$FAKE_STATE"
FAKE_TOOLCHAIN="$TEST_ROOT/toolchain"
FAKE_COMPILER="$TEST_ROOT/compiler"
FAKE_ARCHIVE="$TEST_ROOT/archive"
mkdir -p "$FAKE_TOOLCHAIN/pizfix/lib"
printf 'fixture library\n' > "$FAKE_TOOLCHAIN/pizfix/lib/libc.so"
printf '#!/bin/sh\necho fixture-compiler\n' > "$FAKE_COMPILER"
chmod +x "$FAKE_COMPILER"
printf 'fixture archive\n' > "$FAKE_ARCHIVE"
FAKE_ARCHIVE_SHA=$(shasum -a 256 "$FAKE_ARCHIVE" | cut -d' ' -f1)
FAKE_COMPILER_SHA=$(shasum -a 256 "$FAKE_COMPILER" | cut -d' ' -f1)
export FAKE_TOOLCHAIN FAKE_COMPILER FAKE_ARCHIVE FAKE_ARCHIVE_SHA FAKE_COMPILER_SHA
for tool in sudo systemd-run systemctl; do
    cp "$ROOT/test/remote/fake-supervisor.sh" "$TEST_ROOT/bin/$tool"
    chmod +x "$TEST_ROOT/bin/$tool"
done
cat > "$TEST_ROOT/host-entry.sh" <<'HOST'
#!/bin/sh
set -eu
home=$FAKE_HOME
toolchain=$FAKE_TOOLCHAIN
compiler=$FAKE_COMPILER
archive=$FAKE_ARCHIVE
archive_sha=$FAKE_ARCHIVE_SHA
compiler_sha=$FAKE_COMPILER_SHA
runtime_libraries="$toolchain/pizfix/lib/libc.so"
source_names=tools/remote/payload.sh
uname() { case "$1" in -s) echo Linux ;; -m) echo x86_64 ;; *) return 1 ;; esac; }
getent() { printf 'fixture:x:1000:1000:fixture:%s:/bin/sh\n' "$home"; }
sha256sum() { shasum -a 256 "$@"; }
cleanup_code=$(cat "$REMOTE_ROOT/tools/remote/cleanup.sh")
. "$REMOTE_ROOT/tools/remote/host.sh"
HOST
REMOTE_ROOT=$ROOT
export REMOTE_ROOT
sh "$TEST_ROOT/host-entry.sh" start "$nonce" noop
[ -d "$workspace" ]
# Transient D-Bus exec properties preserve '%' literally, unlike unit-file text.
if grep -F '%%s' "$FAKE_STATE/pi-c-s0-$nonce-owner.service.args" ||
    grep -F '%%u' "$FAKE_STATE/pi-c-s0-$nonce-owner.service.args"; then
    printf 'FAIL: manager arguments would write literal format specifiers\n' >&2
    exit 1
fi
grep -Fx -- '--on-active=600s' "$FAKE_STATE/pi-c-s0-$nonce-cleanup.service.args"
grep -Fx -- '--property=User=1000' "$FAKE_STATE/pi-c-s0-$nonce-owner.service.args"
grep -Fx -- '--property=Group=1000' "$FAKE_STATE/pi-c-s0-$nonce-cleanup.service.args"
grep -Fx -- '--property=RuntimeMaxSec=600s' "$FAKE_STATE/pi-c-s0-$nonce-owner.service.args"
grep -Fx -- '--property=Conflicts=pi-c-s0-'"$nonce"'-owner.service pi-c-s0-'"$nonce"'-worker.service' \
    "$FAKE_STATE/pi-c-s0-$nonce-cleanup.service.args"
grep -F -- '--property=ExecStopPost=:/bin/sh -c ' \
    "$FAKE_STATE/pi-c-s0-$nonce-owner.service.args" > /dev/null
if sh "$TEST_ROOT/host-entry.sh" start "$nonce" noop; then exit 1; fi
printf '%s\n' "$nonce" > "$workspace/.upload-complete"
sh "$TEST_ROOT/host-entry.sh" work "$nonce" noop
[ "$(/usr/bin/stat -f '%Lp' "$workspace/worker.log")" = 600 ]
printf 'line one\nline two\n' >> "$workspace/worker.log"
sh "$TEST_ROOT/host-entry.sh" tail "$nonce" noop > "$TEST_ROOT/tail.out"
grep -Fx 'line two' "$TEST_ROOT/tail.out"
grep -F 'worker: ' "$TEST_ROOT/tail.out"
[ -f "$workspace/worker.log" ]
sh "$TEST_ROOT/host-entry.sh" teardown "$nonce" noop
[ ! -e "$workspace" ]
sh "$TEST_ROOT/host-entry.sh" teardown "$nonce" noop
printf 'remote lifecycle: fake manager ordering and cleanup passed\n'