interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / test_chip_run.sh

447 lines · 29217 bytes Raw Blame History
1#!/bin/bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# tests/test_chip_run.sh -- host-only self-test of tools/chip_run.sh. No GPU, no chip sources.
18# Builds a throwaway fake omega and fake physics (git repos) in a mktemp dir and runs the module
19# against tiny fake chip binaries (compiled with host gcc from 3-20 line C files, because the
20# module's nm check needs real ELF files). Prints one line per case and a final
21# "CHIP_RUN_SELFTEST: PASS" or "CHIP_RUN_SELFTEST: FAIL"; exits nonzero on any failure.
22# tests/test_chip_run.sh run every case
23# tests/test_chip_run.sh --mutants mutation check: for each "# REFUSAL:<id>" line in the
24# module, delete that line in a temp copy and require this test to report "FAIL <id>:"
25# (MODULE=path runs the test against any copy of the module; do this by hand to try a mutant)
26# Needs: bash git jq gcc nm flock coreutils.
27set -u
28TESTS_DIR=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)
29SRC_MODULE=${MODULE:-$TESTS_DIR/../tools/chip_run.sh}
30
31if [ "${1:-}" = --mutants ]; then
32 M=$(mktemp -d) || exit 1; trap 'rm -rf "$M"' EXIT; bad=0; n=0
33 # the unmutated suite must pass first: a failing baseline can print "FAIL <id>:" for every mutant (no arguments, so no recursion)
34 MODULE="$SRC_MODULE" bash "${BASH_SOURCE[0]}" > "$M/base.out" 2>&1 \
35 || { echo "CHIP_RUN_MUTANTS: FAIL baseline self-test does not pass"; grep -E '^(FAIL|CHIP_RUN_SELFTEST)' "$M/base.out" | head -20 | sed 's/^/ | /'; exit 1; }
36 for id in $(grep -o '# REFUSAL:[a-z_]*$' "$SRC_MODULE" | sed 's/.*://' | sort -u); do
37 n=$((n + 1))
38 sed "s/^.*# REFUSAL:$id\$/:/" "$SRC_MODULE" > "$M/chip_run.sh"
39 cmp -s "$M/chip_run.sh" "$SRC_MODULE" && { echo "NOCHANGE $id"; bad=1; continue; }
40 MODULE="$M/chip_run.sh" bash "${BASH_SOURCE[0]}" > "$M/out" 2>&1
41 if grep -q "^FAIL $id:" "$M/out"; then echo "killed $id"; else echo "SURVIVED $id"; bad=1; fi
42 done
43 # the jq array fix has no refusal line to delete: revert the "x" prefix idiom in a copy and need the named case to FAIL
44 # (the copy must contain the plain old idiom, else the sed missed and the "kill" would be for the wrong reason)
45 jqmut() { # NAME CASE SED-SCRIPT OLD-IDIOM-TEXT
46 n=$((n + 1))
47 sed "$3" "$SRC_MODULE" > "$M/chip_run.sh"
48 cmp -s "$M/chip_run.sh" "$SRC_MODULE" && { echo "NOCHANGE $1"; bad=1; return; }
49 grep -qF -- "$4" "$M/chip_run.sh" || { echo "NOCHANGE $1 (old idiom not in the copy)"; bad=1; return; }
50 MODULE="$M/chip_run.sh" bash "${BASH_SOURCE[0]}" > "$M/out" 2>&1
51 if grep -q "^FAIL $2:" "$M/out"; then echo "killed $1"; else echo "SURVIVED $1"; bad=1; fi
52 }
53 jqmut jq_vlines_prefix dash_verdict '/--argjson vlines/{s/ | map(\.\[1:\])//;s|\${VLINES\[@\]/#/x}|${VLINES[@]}|;}' \
54 "'\$ARGS.positional' --args \"\${VLINES[@]}\""
55 jqmut jq_gate_args_prefix happy '/--argjson run_args/{s/ | map(\.\[1:\])//;s|\${GATE_ARGS\[@\]/#/x}|${GATE_ARGS[@]}|;}' \
56 "'\$ARGS.positional' --args \"\${GATE_ARGS[@]}\""
57 [ "$n" -gt 0 ] || bad=1
58 [ "$bad" = 0 ] && { echo "CHIP_RUN_MUTANTS: PASS ($n mutants killed)"; exit 0; }
59 echo "CHIP_RUN_MUTANTS: FAIL"; exit 1
60fi
61
62T=$(mktemp -d) || exit 1
63trap 'rm -rf "$T"' EXIT
64mkdir -p "$T/tmp" "$T/bin"
65PHYS=$T/physics; OM=$T/omega; FLAG=$T/flag; LOCK=$T/gpu.lock; OUT=$T/out.txt; BAD=0; RC=0
66g() { local d=$1; shift; git -C "$d" -c user.email=t@t -c user.name=t -c commit.gpgsign=false "$@"; }
67
68# fake binaries
69cat > "$T/ok.c" <<'EOC'
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73int main(int argc, char **argv) {
74 const char *m = getenv("FAKE_MODE"); if (!m) m = "pass";
75 printf("fake chip args=%d\n", argc - 1);
76 if (getenv("FAKE_CMD") && system(getenv("FAKE_CMD")) != 0) return 9;
77 if (!strcmp(m, "silent")) return 0;
78 if (!strcmp(m, "fail")) { printf("RESULT chip x ok=false\nVERDICT FAIL\n"); return 0; }
79 if (!strcmp(m, "failpass")) { printf("VERDICT FAIL\nVERDICT PASS\n"); return 0; }
80 if (!strcmp(m, "passrc")) { printf("VERDICT PASS\n"); return 3; }
81 if (!strcmp(m, "rc1")) { printf("VERDICT FAIL\n"); return 1; }
82 if (!strcmp(m, "rc2")) { printf("VERDICT PASS\n"); return 2; }
83 if (!strcmp(m, "dash")) { printf("--- VERDICT PASS\n"); return 0; }
84 printf("RESULT chip x ok=true\nVERDICT PASS\n");
85 return 0;
86}
87EOC
88printf '#include <math.h>\n#include <stdio.h>\nint main(void){ volatile float x = 2.0f; printf("VERDICT PASS %%f\\n", expf(x)); return 0; }\n' > "$T/libm.c"
89printf 'extern int cuInit(unsigned) __attribute__((weak));\n#include <stdio.h>\nint main(void){ if (cuInit) cuInit(0); puts("VERDICT PASS"); return 0; }\n' > "$T/cuda.c"
90gcc -O0 -o "$T/bin/ok" "$T/ok.c" && gcc -O0 -fno-builtin -o "$T/bin/libm" "$T/libm.c" -lm && gcc -O0 -o "$T/bin/cuda" "$T/cuda.c" \
91 || { echo "FAIL setup: host gcc could not build the fake binaries"; echo "CHIP_RUN_SELFTEST: FAIL"; exit 1; }
92printf '#!/bin/sh\necho VERDICT PASS\n' > "$T/bin/script"; chmod +x "$T/bin/script"
93
94# fake physics (pinned) and fake omega (clean, with the module under test)
95mkdir -p "$PHYS/nvrm" "$OM/tools" "$OM/src"
96git init -q "$PHYS" && echo "/* fake */" > "$PHYS/nvrm/nvrm.c" && g "$PHYS" add -A && g "$PHYS" commit -q -m physics
97PIN=$(git -C "$PHYS" rev-parse HEAD)
98git init -q "$OM" && cp "$SRC_MODULE" "$OM/tools/chip_run.sh" && echo "$PIN" > "$OM/physics.lock" && echo "/* fake */" > "$OM/src/a.c" && echo "int main(void){ return undefined_thing; }" > "$OM/src/bad.c" \
99 && printf "#include <stdio.h>\\nint main(void){ puts(\"VERDICT PASS\"); return 0; }\\n" > "$OM/src/okchip.c" \
100 && g "$OM" add -A && g "$OM" commit -q -m omega
101OM_HEAD=$(git -C "$OM" rev-parse HEAD)
102
103mkman() { cat > "$1" <<EOM
104GATE=selftest-gate
105TEST_SOURCE=src/a.c
106VERDICT_RE='^RESULT|^VERDICT'
107PASS_LINE='^VERDICT PASS\$'
108OWNER="chip_run selftest"
109EVIDENCE_DIR=$T/ev-default
110RAISE_QUIET=1
111TAKE_GPU_LOCK=1
112REFUSE_DIRTY_OMEGA=1
113RUN_ARGS="--chip"
114HOST_TIER_CMD='echo host-tier-ok'
115RECEIPT_EXTRA_JQ='. + {extra_marker: "x"}'
116${2:-}
117EOM
118}
119mkman "$T/m.sh"; mkman "$T/m-hostfail.sh" "HOST_TIER_CMD=false"
120mkman "$T/m-map.sh" "REFUSE_EXIT=1; REFUSE_VERDICT_LINE='VERDICT NOT_RUN'"
121mkman "$T/m-off.sh" "RAISE_QUIET=0; TAKE_GPU_LOCK=0"
122mkman "$T/m-all.sh" "REQUIRE_ALL_PASS=1; VERDICT_RE='^VERDICT'"
123mkman "$T/m-pt.sh" "CHILD_EXIT_PASSTHROUGH=1"; mkman "$T/m-pt-bad.sh" "CHILD_EXIT_PASSTHROUGH=2"
124mkman "$T/m-dash.sh" "VERDICT_RE='^--- VERDICT'; PASS_LINE='^--- VERDICT PASS\$'"
125CRMAN=$T/m.sh; PD=(--physics-dir "$PHYS")
126
127reset_world() {
128 g "$OM" reset -q --hard "$OM_HEAD"; g "$OM" clean -fdq
129 g "$PHYS" reset -q --hard "$PIN"; g "$PHYS" clean -fdq
130 rm -f "$FLAG"; exec 8>&-
131}
132# cr ID [NAME=VALUE ...] -- [module args]: run the module from outside both trees
133cr() {
134 local id=$1; shift; local extra=() ev=(--evidence-dir "$T/ev-$id"); [ -z "${NOEVID:-}" ] || ev=()
135 while [ "$1" != -- ]; do extra+=("$1"); shift; done; shift
136 ( cd "$T" && env -u PHYSICS_DIR -u PHYSICS CHIPRUN_SELFTEST=1 CHIPRUN_PREBUILT_BIN="$T/bin/ok" CHIPRUN_QUIET_FLAG="$FLAG" \
137 CHIPRUN_GPU_LOCK="$LOCK" CHIPRUN_EST_LOAD_CMD=true FAKE_MODE=pass TMPDIR="$T/tmp" "${extra[@]}" \
138 bash "${RAWMOD:-$OM/tools/chip_run.sh}" "$CRMAN" "${ev[@]}" "$@" ) > "$OUT" 2>&1
139 RC=$?
140}
141bad() { echo "FAIL $1: $2"; sed 's/^/ | /' "$OUT" | head -8; BAD=$((BAD + 1)); }
142# expect ID RC REGEX: exit code and a matching output line
143expect() {
144 if [ "$RC" = "$2" ] && grep -Eq -- "$3" "$OUT"; then echo "ok $1 (exit $RC)"; else bad "$1" "exit $RC, wanted $2 and /$3/"; fi
145}
146# assert ID DESC cmd...: extra condition for a case
147assert() { local id=$1 d=$2; shift 2; if "$@"; then echo "ok $id: $d"; else bad "$id" "$d"; fi; }
148no_flag() { [ ! -e "$FLAG" ]; }
149flag_is_other() { [ "$(cat "$FLAG" 2>/dev/null)" = other ]; }
150receipt_verdict() { [ "$(jq -r .verdict "$T"/ev-"$1"/*.json 2>/dev/null)" = "$2" ]; }
151# jqt FILTER FILE: jq -e with its stdout dropped, so assert still prints its own ok or FAIL line
152jqt() { jq -e "$@" > /dev/null; }
153
154reset_world; cr selftest_override CHIPRUN_SELFTEST=0 -- "${PD[@]}"
155expect selftest_override 2 '^REFUSED: .*self-test override'
156reset_world; cr no_physics -- --physics-dir "$T/nonexistent"
157expect no_physics 2 '^REFUSED: no physics checkout'
158reset_world; CRMAN=$T/m-map.sh cr refuse_map -- --physics-dir "$T/nonexistent"
159expect refuse_map 1 '^VERDICT NOT_RUN$'; CRMAN=$T/m.sh
160reset_world; g "$PHYS" commit -q --allow-empty -m moved; cr physics_pin -- "${PD[@]}"
161expect physics_pin 2 '^REFUSED: physics checkout is at .*physics.lock pins'
162reset_world; echo x > "$PHYS/junk"; cr physics_dirty -- "${PD[@]}"
163expect physics_dirty 2 '^REFUSED: physics checkout .* is dirty'
164reset_world; echo x > "$OM/junk"; cr omega_dirty -- "${PD[@]}"
165expect omega_dirty 2 '^REFUSED: omega tree .* is dirty'
166reset_world; cr evidence_inside -- "${PD[@]}" --evidence-dir "$OM/evid"
167expect evidence_inside 2 '^REFUSED: evidence dir .* inside a candidate tree'
168reset_world; cr evidence_inside_physics -- "${PD[@]}" --evidence-dir "$PHYS/evid"
169expect evidence_inside_physics 2 '^REFUSED: evidence dir .* inside a candidate tree'
170reset_world; echo other > "$FLAG"; cr quiet_flag -- "${PD[@]}"
171expect quiet_flag 2 '^REFUSED: quiet flag is up: other'
172assert quiet_flag "a flag that is not ours is left alone" flag_is_other
173reset_world; cr est_load CHIPRUN_EST_LOAD_CMD='echo 4242' -- "${PD[@]}"
174expect est_load 2 '^REFUSED: an est_load process is running'
175reset_world; exec 8> "$LOCK"; flock -x 8; cr gpu_lock -- "${PD[@]}"; exec 8>&-
176expect gpu_lock 2 '^REFUSED: GPU lock .* is held'
177assert gpu_lock "the quiet flag we raised is dropped on refusal" no_flag
178reset_world; CRMAN=$T/m-hostfail.sh cr host_tier -- "${PD[@]}"
179expect host_tier 2 '^REFUSED: host tier failed'; CRMAN=$T/m.sh
180reset_world; cr nm_unreadable CHIPRUN_PREBUILT_BIN="$T/bin/script" -- "${PD[@]}"
181expect nm_unreadable 2 '^REFUSED: nm cannot read'
182reset_world; cr libm CHIPRUN_PREBUILT_BIN="$T/bin/libm" -- "${PD[@]}"
183expect libm 2 '^REFUSED: libm math symbols'
184reset_world; cr cuda CHIPRUN_PREBUILT_BIN="$T/bin/cuda" -- "${PD[@]}"
185expect cuda 2 '^REFUSED: CUDA symbols'
186reset_world; cr rc_nonzero FAKE_MODE=passrc -- "${PD[@]}"
187expect rc_nonzero 1 '^CHIP_RUN: FAIL binary exit status 3'
188reset_world; cr no_verdict FAKE_MODE=silent -- "${PD[@]}"
189expect no_verdict 1 '^CHIP_RUN: FAIL no verdict line'
190reset_world; cr fail_verdict FAKE_MODE=fail -- "${PD[@]}"
191expect fail_verdict 1 '^CHIP_RUN: FAIL last verdict line does not match PASS_LINE'
192assert fail_verdict "a FAIL receipt is still written" receipt_verdict fail_verdict FAIL
193reset_world; cr omega_dirty_after FAKE_CMD="touch $OM/dirt" -- "${PD[@]}"
194expect omega_dirty_after 1 '^CHIP_RUN: FAIL omega tree dirty after run'
195reset_world; cr physics_dirty_after FAKE_CMD="touch $PHYS/dirt" -- "${PD[@]}"
196expect physics_dirty_after 1 '^CHIP_RUN: FAIL physics tree dirty after run'
197reset_world; cr head_moved FAKE_CMD="git -C $OM -c user.email=t@t -c user.name=t -c commit.gpgsign=false commit -q --allow-empty -m x" -- "${PD[@]}"
198expect head_moved 1 '^CHIP_RUN: FAIL a HEAD moved'
199
200# raw ENV=VALUE... -- args: run the module with every seam variable unset (no CHIPRUN_SELFTEST)
201raw() {
202 local extra=(); while [ "$1" != -- ]; do extra+=("$1"); shift; done; shift
203 ( cd "$T" && env -u PHYSICS_DIR -u PHYSICS -u CHIPRUN_SELFTEST -u CHIPRUN_PREBUILT_BIN -u CHIPRUN_QUIET_FLAG -u CHIPRUN_GPU_LOCK \
204 -u CHIPRUN_EST_LOAD_CMD TMPDIR="$T/tmp" "${extra[@]}" bash "${RAWMOD:-$OM/tools/chip_run.sh}" "$@" ) > "$OUT" 2>&1
205 RC=$?
206}
207# test seam: each of the four variables ALONE, without CHIPRUN_SELFTEST=1, is refused (manifest is m-off: no real flag or lock)
208for sv in "seam_prebuilt CHIPRUN_PREBUILT_BIN $T/bin/ok" "seam_quiet CHIPRUN_QUIET_FLAG $FLAG" "seam_gpu CHIPRUN_GPU_LOCK $LOCK" "seam_est CHIPRUN_EST_LOAD_CMD true"; do
209 read -r sid svar sval <<< "$sv"
210 reset_world; raw "$svar=$sval" -- "$T/m-off.sh" --evidence-dir "$T/ev-$sid" "${PD[@]}"
211 expect "$sid" 2 "^REFUSED: $svar is a self-test override"
212done
213# checked before the manifest is read: a missing manifest still gets the seam message
214reset_world; raw CHIPRUN_PREBUILT_BIN="$T/bin/ok" -- "$T/nope.sh" --evidence-dir "$T/ev-seam_before" "${PD[@]}"
215expect seam_before 2 '^REFUSED: CHIPRUN_PREBUILT_BIN is a self-test override'
216# checked after the manifest is read: a manifest that sets the variable cannot slip it in
217mkman "$T/m-seamset.sh" "RAISE_QUIET=0; TAKE_GPU_LOCK=0; CHIPRUN_PREBUILT_BIN=$T/bin/ok"
218reset_world; raw -- "$T/m-seamset.sh" --evidence-dir "$T/ev-seam_after" "${PD[@]}"
219expect seam_after 2 '^REFUSED: CHIPRUN_PREBUILT_BIN is a self-test override'
220# even with CHIPRUN_SELFTEST=1 in the environment, a manifest cannot change a seam variable or set CHIPRUN_SELFTEST
221mkman "$T/m-seamchg.sh" "RAISE_QUIET=0; TAKE_GPU_LOCK=0; CHIPRUN_PREBUILT_BIN=$T/bin/libm"
222reset_world; raw CHIPRUN_SELFTEST=1 CHIPRUN_PREBUILT_BIN="$T/bin/ok" -- "$T/m-seamchg.sh" --evidence-dir "$T/ev-seam_changed" "${PD[@]}"
223expect seam_changed 2 '^REFUSED: manifest changed CHIPRUN_PREBUILT_BIN'
224mkman "$T/m-selfset.sh" "RAISE_QUIET=0; TAKE_GPU_LOCK=0; CHIPRUN_SELFTEST=1"
225reset_world; raw -- "$T/m-selfset.sh" --evidence-dir "$T/ev-seam_selftest" "${PD[@]}"
226expect seam_selftest 2 '^REFUSED: manifest changed CHIPRUN_SELFTEST'
227# a manifest that assigns QUIET_FLAG, GPU_LOCK and EST_LOAD_CMD is undone after it is sourced. Each hostile value
228# would refuse the run if it leaked (flag already up, lock held, est_load answers); the chip checks the flag is at the seam path.
229mkman "$T/m-restore.sh" "QUIET_FLAG=$T/hostile-flag; GPU_LOCK=$T/hostile.lock; EST_LOAD_CMD='echo 4242'"
230reset_world; rm -f "$LOCK" "$T/hostile.lock"; echo hostile > "$T/hostile-flag"; exec 8> "$T/hostile.lock"; flock -x 8
231CRMAN=$T/m-restore.sh cr restore_vars FAKE_CMD="test -e $FLAG" -- "${PD[@]}"; exec 8>&-; CRMAN=$T/m.sh
232expect restore_vars 0 '^CHIP_RUN: PASS'
233assert restore_vars "the flag is dropped at the seam path after the run" no_flag
234assert restore_vars "the manifest's flag path is left alone" test "$(cat "$T/hostile-flag" 2>/dev/null)" = hostile
235assert restore_vars "the GPU lock was opened at the seam path" test -e "$LOCK"
236
237# usage, arguments, manifest
238reset_world; raw --
239expect usage 2 '^REFUSED: usage'
240reset_world; cr bad_arg -- "${PD[@]}" --bogus
241expect bad_arg 2 '^REFUSED: unknown argument --bogus'
242reset_world; cr arg_value -- --physics-dir
243expect arg_value 2 '^REFUSED: --physics-dir needs a value'
244reset_world; CRMAN=$T/nope.sh cr no_manifest -- "${PD[@]}"
245expect no_manifest 2 '^REFUSED: no manifest at'
246mkman "$T/m-loadfail.sh" "false"
247reset_world; CRMAN=$T/m-loadfail.sh cr manifest_load -- "${PD[@]}"
248expect manifest_load 2 '^REFUSED: manifest .* failed to load'
249mkman "$T/m-novars.sh" 'OWNER=""'
250reset_world; CRMAN=$T/m-novars.sh cr manifest_vars -- "${PD[@]}"
251expect manifest_vars 2 '^REFUSED: manifest does not set OWNER'
252mkman "$T/m-gate.sh" 'GATE="bad/gate"'
253reset_world; CRMAN=$T/m-gate.sh cr gate_chars -- "${PD[@]}"
254expect gate_chars 2 '^REFUSED: GATE .* characters'
255mkman "$T/m-noevid.sh" 'EVIDENCE_DIR=""'
256reset_world; NOEVID=1; CRMAN=$T/m-noevid.sh cr no_evidence -- "${PD[@]}"; unset NOEVID
257expect no_evidence 2 '^REFUSED: no evidence dir'
258# REFUSE_EXIT must be an integer 1..125; REQUIRE_ALL_PASS must be 0 or 1
259for rx in "refuse_exit 0" "refuse_exit_text abc" "refuse_exit_big 126" "refuse_exit_empty ''"; do
260 read -r rid rval <<< "$rx"; mkman "$T/m-$rid.sh" "REFUSE_EXIT=$rval"
261 reset_world; CRMAN=$T/m-$rid.sh cr "$rid" -- "${PD[@]}"
262 expect "$rid" 1 '^CHIP_RUN: BAD_MANIFEST REFUSE_EXIT='
263done
264mkman "$T/m-rx125.sh" "REFUSE_EXIT=125"
265reset_world; CRMAN=$T/m-rx125.sh cr refuse_exit_125 -- --physics-dir "$T/nonexistent"
266expect refuse_exit_125 125 '^REFUSED: no physics checkout'
267mkman "$T/m-rap-bad.sh" "REQUIRE_ALL_PASS=yes"
268reset_world; CRMAN=$T/m-rap-bad.sh cr require_all_value -- "${PD[@]}"
269expect require_all_value 1 '^CHIP_RUN: BAD_MANIFEST REQUIRE_ALL_PASS='
270
271# refusals that need a special world
272mkdir -p "$T/nogit-om/tools" "$T/nogit/nvrm"; cp "$SRC_MODULE" "$T/nogit-om/tools/chip_run.sh"
273reset_world; RAWMOD=$T/nogit-om/tools/chip_run.sh cr omega_head -- "${PD[@]}"
274expect omega_head 2 '^REFUSED: cannot read omega HEAD'
275mkman "$T/m-nolock.sh" "REFUSE_DIRTY_OMEGA=0"
276reset_world; rm -f "$OM/physics.lock"; CRMAN=$T/m-nolock.sh cr physics_lock -- "${PD[@]}"
277expect physics_lock 2 '^REFUSED: cannot read physics.lock'
278reset_world; cr physics_head -- --physics-dir "$T/nogit"
279expect physics_head 2 '^REFUSED: cannot read physics HEAD'
280reset_world; cr mktemp TMPDIR="$T/nonexistent" -- "${PD[@]}"
281expect mktemp 2 '^REFUSED: cannot create a run directory'
282mkman "$T/m-race.sh" "HOST_TIER_CMD='echo other > $FLAG'"
283reset_world; CRMAN=$T/m-race.sh cr flag_create -- "${PD[@]}"
284expect flag_create 2 '^REFUSED: could not create the quiet flag'
285assert flag_create "a flag created by someone else in the race is left alone" flag_is_other
286reset_world; cr gpu_open CHIPRUN_GPU_LOCK="$T/nodir/x.lock" -- "${PD[@]}"
287expect gpu_open 2 '^REFUSED: cannot open'
288assert gpu_open "the quiet flag we raised is dropped on refusal" no_flag
289reset_world; cr prebuilt_not_exec CHIPRUN_PREBUILT_BIN="$T/nonexistent" -- "${PD[@]}"
290expect prebuilt_not_exec 2 '^REFUSED: CHIPRUN_PREBUILT_BIN .* is not executable'
291mkman "$T/m-nosrc.sh" "TEST_SOURCE=src/nope.c"
292reset_world; CRMAN=$T/m-nosrc.sh cr missing_source CHIPRUN_PREBUILT_BIN= -- "${PD[@]}"
293expect missing_source 2 '^REFUSED: missing source src/nope.c'
294mkman "$T/m-badsrc.sh" "TEST_SOURCE=src/bad.c"
295reset_world; CRMAN=$T/m-badsrc.sh cr build_failed CHIPRUN_PREBUILT_BIN= -- "${PD[@]}"
296expect build_failed 2 '^REFUSED: chip build failed'
297mkman "$T/m-oksrc.sh" "TEST_SOURCE=src/okchip.c"
298reset_world; CRMAN=$T/m-oksrc.sh cr build_ok CHIPRUN_PREBUILT_BIN= -- "${PD[@]}"
299expect build_ok 0 '^CHIP_RUN: PASS'
300# quiet flag: dropped only if still ours
301reset_world; cr flag_not_ours FAKE_CMD="echo other > $FLAG" -- "${PD[@]}"
302expect flag_not_ours 0 '^CHIP_RUN: PASS'
303assert flag_not_ours "a flag someone else rewrote during the run is left alone" flag_is_other
304# verdict logic: FAIL line then PASS line
305reset_world; cr last_line_default FAKE_MODE=failpass -- "${PD[@]}"
306expect last_line_default 0 '^CHIP_RUN: PASS'
307reset_world; CRMAN=$T/m-all.sh cr fail_any_verdict FAKE_MODE=failpass -- "${PD[@]}"
308expect fail_any_verdict 1 '^CHIP_RUN: FAIL a verdict line does not match PASS_LINE'
309assert fail_any_verdict "a FAIL receipt is still written" receipt_verdict fail_any_verdict FAIL
310reset_world; CRMAN=$T/m-all.sh cr all_pass_ok FAKE_MODE=pass -- "${PD[@]}"
311expect all_pass_ok 0 '^CHIP_RUN: PASS'
312# child exit passthrough: fake binary exiting 0, 1, 2 (exit 2 is the trap harness NOT_RUN code)
313reset_world; CRMAN=$T/m-pt.sh cr child_exit_pass0 FAKE_MODE=pass -- "${PD[@]}"
314expect child_exit_pass0 0 '^CHIP_RUN: PASS'
315reset_world; CRMAN=$T/m-pt.sh cr child_exit_pass1 FAKE_MODE=rc1 -- "${PD[@]}"
316expect child_exit_pass1 1 '^CHIP_RUN: FAIL'
317reset_world; CRMAN=$T/m-pt.sh cr child_exit_passthrough FAKE_MODE=rc2 -- "${PD[@]}"
318expect child_exit_passthrough 2 '^CHIP_RUN: FAIL binary exit status 2'
319assert child_exit_passthrough "receipt still written with chip_exit_status 2" jq -e '.chip_exit_status == 2 and .verdict == "FAIL"' "$T"/ev-child_exit_passthrough/*.json
320reset_world; CRMAN=$T/m-pt.sh cr child_exit_zero_fail FAKE_MODE=fail -- "${PD[@]}"
321expect child_exit_zero_fail 1 '^CHIP_RUN: FAIL'
322reset_world; CRMAN=$T/m.sh cr child_exit_default FAKE_MODE=rc2 -- "${PD[@]}"
323expect child_exit_default 1 '^CHIP_RUN: FAIL binary exit status 2'
324reset_world; CRMAN=$T/m-pt-bad.sh cr child_exit_value -- "${PD[@]}"
325expect child_exit_value 1 '^CHIP_RUN: BAD_MANIFEST CHILD_EXIT_PASSTHROUGH='; CRMAN=$T/m.sh
326# evidence reuse: a second run keeps one log blob; a tampered blob is caught
327reset_world; cr reuse -- "${PD[@]}"; cr reuse -- "${PD[@]}"
328expect reuse 0 '^CHIP_RUN: PASS'
329assert reuse "one log blob for two identical logs" test "$(ls "$T"/ev-reuse/blobs/*.log 2>/dev/null | wc -l)" = 1
330RB=$(ls "$T"/ev-reuse/blobs/*.log 2>/dev/null | head -1); chmod u+w "$RB" 2>/dev/null; echo tamper >> "$RB"
331reset_world; cr reuse -- "${PD[@]}"
332expect blob_tamper 1 '^CHIP_RUN: FAIL log blob does not match its digest'
333
334# physics dir resolution order: flag, PHYSICS_DIR, PHYSICS
335reset_world; cr order_env_dir PHYSICS="$T/nonexistent" PHYSICS_DIR="$PHYS" --
336expect order_env_dir 0 '^CHIP_RUN: PASS'
337reset_world; cr order_flag PHYSICS_DIR="$T/nonexistent" -- "${PD[@]}"
338expect order_flag 0 '^CHIP_RUN: PASS'
339reset_world; cr order_env_physics PHYSICS="$PHYS" --
340expect order_env_physics 0 '^CHIP_RUN: PASS'
341# optional steps off: flag up and lock held do not matter
342reset_world; echo other > "$FLAG"; exec 8> "$LOCK"; flock -x 8; CRMAN=$T/m-off.sh cr optional_off -- "${PD[@]}"; exec 8>&-; CRMAN=$T/m.sh
343expect optional_off 0 '^CHIP_RUN: PASS'
344assert optional_off "a flag we never raised is untouched" flag_is_other
345
346# seam: RECEIPT_EXTRA_JQ can read the whole HOST_TIER_CMD output as $hostlog (empty without one)
347mkman "$T/m-hostlog.sh" "HOST_TIER_CMD='echo AAA; echo BBB'; RECEIPT_EXTRA_JQ='. + {hl: \$hostlog}'"
348reset_world; CRMAN=$T/m-hostlog.sh cr hostlog -- "${PD[@]}"
349expect hostlog 0 '^CHIP_RUN: PASS'
350assert hostlog "receipt carries the whole host tier output" jq -e '.hl == "AAA\nBBB\n" and .host_tier == "BBB"' "$T"/ev-hostlog/*.json
351mkman "$T/m-hostlog0.sh" "HOST_TIER_CMD=''; RECEIPT_EXTRA_JQ='. + {hl: \$hostlog}'"
352reset_world; CRMAN=$T/m-hostlog0.sh cr hostlog_none -- "${PD[@]}"
353expect hostlog_none 0 '^CHIP_RUN: PASS'
354assert hostlog_none "no host tier gives an empty \$hostlog" jq -e '.hl == ""' "$T"/ev-hostlog_none/*.json
355
356# seam: RECEIPT_EXTRA_JQ may replace a FAIL verdict with another upper-case word; PASS is never forged or removed
357mkman "$T/m-vw.sh" "RECEIPT_EXTRA_JQ='.verdict = \"NOT_RUN\"'"
358reset_world; CRMAN=$T/m-vw.sh cr verdict_word_ok FAKE_MODE=fail -- "${PD[@]}"
359expect verdict_word_ok 1 '^CHIP_RUN: NOT_RUN last verdict line'
360assert verdict_word_ok "receipt verdict is the rewritten word" receipt_verdict verdict_word_ok NOT_RUN
361reset_world; CRMAN=$T/m-vw.sh cr verdict_pass_changed -- "${PD[@]}"
362expect verdict_pass_changed 1 '^CHIP_RUN: FAIL RECEIPT_EXTRA_JQ changed a PASS verdict'
363mkman "$T/m-vf.sh" "RECEIPT_EXTRA_JQ='.verdict = \"PASS\"'"
364reset_world; CRMAN=$T/m-vf.sh cr verdict_forged FAKE_MODE=fail -- "${PD[@]}"
365expect verdict_forged 1 '^CHIP_RUN: FAIL RECEIPT_EXTRA_JQ forged a PASS verdict'
366mkman "$T/m-vb.sh" "RECEIPT_EXTRA_JQ='.verdict = \"not run\"'"
367reset_world; CRMAN=$T/m-vb.sh cr verdict_word FAKE_MODE=fail -- "${PD[@]}"
368expect verdict_word 1 '^CHIP_RUN: FAIL receipt verdict is not an upper-case word'
369
370# seam: ECHO_RE limits the stdout echo of the chip log; the log blob stays complete
371mkman "$T/m-echo.sh" "ECHO_RE='^VERDICT'"
372reset_world; CRMAN=$T/m-echo.sh cr echo_re -- "${PD[@]}"
373expect echo_re 0 '^VERDICT PASS$'
374assert echo_re "default-echoed noise line is not shown with ECHO_RE" bash -c '! grep -q "^fake chip args" "$1"' _ "$OUT"
375assert echo_re "the log blob still holds the whole log" grep -q '^fake chip args' "$T"/ev-echo_re/blobs/*.log
376reset_world; cr echo_default -- "${PD[@]}"
377assert echo_default "without ECHO_RE the whole chip log is echoed" grep -q '^fake chip args' "$OUT"
378
379# seam: FINAL_LINE_STYLE=verdict prints "VERDICT <word>" last (old transc style), also for fatal errors
380mkman "$T/m-fs.sh" "FINAL_LINE_STYLE=verdict; RECEIPT_EXTRA_JQ='.verdict = \"NOT_RUN\"'"
381reset_world; CRMAN=$T/m-fs.sh cr final_style FAKE_MODE=fail -- "${PD[@]}"
382expect final_style 1 '^VERDICT NOT_RUN$'
383assert final_style "no CHIP_RUN: line in verdict style" bash -c '! grep -q "^CHIP_RUN:" "$1"' _ "$OUT"
384assert final_style "the last line is the verdict line" test "$(tail -n 1 "$OUT")" = "VERDICT NOT_RUN"
385mkman "$T/m-fs2.sh" "FINAL_LINE_STYLE=verdict"
386reset_world; CRMAN=$T/m-fs2.sh cr final_style_pass -- "${PD[@]}"
387expect final_style_pass 0 '^VERDICT PASS$'
388mkman "$T/m-fs3.sh" "FINAL_LINE_STYLE=verdict; RECEIPT_EXTRA_JQ='.verdict = \"PASS\"'"
389reset_world; CRMAN=$T/m-fs3.sh cr final_style_fatal FAKE_MODE=fail -- "${PD[@]}"
390expect final_style_fatal 1 '^RECEIPT_EXTRA_JQ forged a PASS verdict$'
391assert final_style_fatal "fatal ends with VERDICT FAIL" test "$(tail -n 1 "$OUT")" = "VERDICT FAIL"
392assert fatal_verdict_style "fatal prints a VERDICT FAIL line and no CHIP_RUN: FAIL line" bash -c '[ "$(tail -n 2 "$1" | head -n 1)" = "RECEIPT_EXTRA_JQ forged a PASS verdict" ] && [ "$(tail -n 1 "$1")" = "VERDICT FAIL" ] && ! grep -q "^CHIP_RUN: FAIL" "$1"' _ "$OUT"
393mkman "$T/m-fs4.sh" "FINAL_LINE_STYLE=bogus"
394reset_world; CRMAN=$T/m-fs4.sh cr final_style_value -- "${PD[@]}"
395expect final_style_value 1 '^CHIP_RUN: BAD_MANIFEST FINAL_LINE_STYLE='
396
397# seam: GPU_LOCK_MODE=fuser refuses early on ANY open handle of the lock file (flock mode does not), then blocks on flock
398mkman "$T/m-fu.sh" "GPU_LOCK_MODE=fuser"
399reset_world; exec 8> "$LOCK"; CRMAN=$T/m-fu.sh cr gpu_fuser -- "${PD[@]}"; exec 8>&-
400expect gpu_fuser 2 "^REFUSED: $LOCK is held\$"
401assert gpu_fuser "refused before the host tier and the quiet flag" no_flag
402reset_world; exec 8> "$LOCK"; cr gpu_fuser_ctl -- "${PD[@]}"; exec 8>&-
403expect gpu_fuser_ctl 0 '^CHIP_RUN: PASS'
404reset_world; CRMAN=$T/m-fu.sh cr gpu_fuser_free -- "${PD[@]}"
405expect gpu_fuser_free 0 '^CHIP_RUN: PASS'
406mkman "$T/m-fu2.sh" "GPU_LOCK_MODE=bogus"
407reset_world; CRMAN=$T/m-fu2.sh cr gpu_mode_value -- "${PD[@]}"
408expect gpu_mode_value 1 '^CHIP_RUN: BAD_MANIFEST GPU_LOCK_MODE='
409# regression (PR 213 review): a bad manifest value must stop the run (fail-open if bad_manifest is undefined), with no receipt and no shell error
410assert bad_manifest_def "bad manifest exits nonzero, no 'command not found'" bash -c '! grep -q "command not found" "$1"' _ "$OUT"
411assert final_style_value "bad manifest wrote no receipt" test -z "$(ls "$T"/ev-final_style_value/*.json 2>/dev/null)"
412assert gpu_mode_value "bad GPU_LOCK_MODE wrote no receipt" test -z "$(ls "$T"/ev-gpu_mode_value/*.json 2>/dev/null)"
413
414# happy path and receipt
415reset_world; cr happy -- "${PD[@]}" -- --x 1
416expect happy 0 '^VERDICT PASS$'
417grep -q '^CHIP_RUN: PASS$' "$OUT" || bad happy "no final CHIP_RUN: PASS line"
418assert happy "quiet flag dropped after the run" no_flag
419R=$(ls "$T"/ev-happy/*.json 2>/dev/null | head -1)
420assert happy "exactly one receipt" test "$(ls "$T"/ev-happy/*.json 2>/dev/null | wc -l)" = 1
421assert happy "receipt mode is 0444" test "$(stat -c %a "$R" 2>/dev/null)" = 444
422assert happy "receipt name is the sha256 of its content" test "$(sha256sum "$R" 2>/dev/null | cut -d' ' -f1).json" = "$(basename "$R")"
423for f in gate owner omega_commit omega_tree_clean_before omega_tree_clean_after omega_commit_unchanged_after physics_commit physics_lock_pin \
424 physics_tree_clean_before physics_tree_clean_after physics_commit_unchanged_after binary_sha256 chip_log_sha256 run_args host_tier \
425 verdict_lines chip_exit_status started_utc finished_utc verdict reason extra_marker; do
426 assert happy "receipt has field $f" jqt --arg f "$f" 'has($f)' "$R"
427done
428assert happy "receipt verdict PASS, run_args from the command line" jqt '.verdict == "PASS" and .run_args == ["--x","1"] and .host_tier == "host-tier-ok" and .omega_commit == "'"$OM_HEAD"'"' "$R"
429LS=$(jq -r .chip_log_sha256 "$R" 2>/dev/null)
430assert happy "log blob exists, mode 0444, named by its sha256" test "$(stat -c %a "$T/ev-happy/blobs/$LS.log" 2>/dev/null)" = 444 -a "$(sha256sum "$T/ev-happy/blobs/$LS.log" 2>/dev/null | cut -d' ' -f1)" = "$LS"
431
432# a verdict line jq 1.7 would read as an option (it starts with "--") must reach the receipt intact;
433# "- VERDICT PASS" (dash, space) would not be one, so it would not catch the bug
434reset_world; CRMAN=$T/m-dash.sh cr dash_verdict FAKE_MODE=dash -- "${PD[@]}"; CRMAN=$T/m.sh
435expect dash_verdict 0 '^CHIP_RUN: PASS'
436RD=$(ls "$T"/ev-dash_verdict/*.json 2>/dev/null | head -1)
437assert dash_verdict "receipt verdict_lines is the dash line, unchanged" jqt '.verdict_lines == ["--- VERDICT PASS"]' "$RD"
438# no run args at all (RUN_ARGS empty, none on the command line): the binary gets none and run_args is []
439mkman "$T/m-noargs.sh" 'RUN_ARGS=""'
440reset_world; CRMAN=$T/m-noargs.sh cr empty_args -- "${PD[@]}"; CRMAN=$T/m.sh
441expect empty_args 0 '^CHIP_RUN: PASS'
442RE=$(ls "$T"/ev-empty_args/*.json 2>/dev/null | head -1)
443assert empty_args "receipt run_args is the empty list" jqt '.run_args == []' "$RE"
444assert empty_args "the binary was started with no arguments" grep -q '^fake chip args=0$' "$T"/ev-empty_args/blobs/*.log
445
446if [ "$BAD" = 0 ]; then echo "CHIP_RUN_SELFTEST: PASS"; exit 0; fi
447echo "CHIP_RUN_SELFTEST: FAIL ($BAD)"; exit 1