Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/test_chip_run.sh # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers 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:" line in the 24 # module, delete that line in a temp copy and require this test to report "FAIL :" 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. 27 set -u 28 TESTS_DIR=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd) 29 SRC_MODULE=${MODULE:-$TESTS_DIR/../tools/chip_run.sh} 30 31 if [ "${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 :" 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 60 fi 61 62 T=$(mktemp -d) || exit 1 63 trap 'rm -rf "$T"' EXIT 64 mkdir -p "$T/tmp" "$T/bin" 65 PHYS=$T/physics; OM=$T/omega; FLAG=$T/flag; LOCK=$T/gpu.lock; OUT=$T/out.txt; BAD=0; RC=0 66 g() { local d=$1; shift; git -C "$d" -c user.email=t@t -c user.name=t -c commit.gpgsign=false "$@"; } 67 68 # fake binaries 69 cat > "$T/ok.c" <<'EOC' 70 #include 71 #include 72 #include 73 int 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 } 87 EOC 88 printf '#include \n#include \nint main(void){ volatile float x = 2.0f; printf("VERDICT PASS %%f\\n", expf(x)); return 0; }\n' > "$T/libm.c" 89 printf 'extern int cuInit(unsigned) __attribute__((weak));\n#include \nint main(void){ if (cuInit) cuInit(0); puts("VERDICT PASS"); return 0; }\n' > "$T/cuda.c" 90 gcc -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; } 92 printf '#!/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) 95 mkdir -p "$PHYS/nvrm" "$OM/tools" "$OM/src" 96 git init -q "$PHYS" && echo "/* fake */" > "$PHYS/nvrm/nvrm.c" && g "$PHYS" add -A && g "$PHYS" commit -q -m physics 97 PIN=$(git -C "$PHYS" rev-parse HEAD) 98 git 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 \\nint main(void){ puts(\"VERDICT PASS\"); return 0; }\\n" > "$OM/src/okchip.c" \ 100 && g "$OM" add -A && g "$OM" commit -q -m omega 101 OM_HEAD=$(git -C "$OM" rev-parse HEAD) 102 103 mkman() { cat > "$1" <&- 131 } 132 # cr ID [NAME=VALUE ...] -- [module args]: run the module from outside both trees 133 cr() { 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 } 141 bad() { echo "FAIL $1: $2"; sed 's/^/ | /' "$OUT" | head -8; BAD=$((BAD + 1)); } 142 # expect ID RC REGEX: exit code and a matching output line 143 expect() { 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 147 assert() { local id=$1 d=$2; shift 2; if "$@"; then echo "ok $id: $d"; else bad "$id" "$d"; fi; } 148 no_flag() { [ ! -e "$FLAG" ]; } 149 flag_is_other() { [ "$(cat "$FLAG" 2>/dev/null)" = other ]; } 150 receipt_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 152 jqt() { jq -e "$@" > /dev/null; } 153 154 reset_world; cr selftest_override CHIPRUN_SELFTEST=0 -- "${PD[@]}" 155 expect selftest_override 2 '^REFUSED: .*self-test override' 156 reset_world; cr no_physics -- --physics-dir "$T/nonexistent" 157 expect no_physics 2 '^REFUSED: no physics checkout' 158 reset_world; CRMAN=$T/m-map.sh cr refuse_map -- --physics-dir "$T/nonexistent" 159 expect refuse_map 1 '^VERDICT NOT_RUN$'; CRMAN=$T/m.sh 160 reset_world; g "$PHYS" commit -q --allow-empty -m moved; cr physics_pin -- "${PD[@]}" 161 expect physics_pin 2 '^REFUSED: physics checkout is at .*physics.lock pins' 162 reset_world; echo x > "$PHYS/junk"; cr physics_dirty -- "${PD[@]}" 163 expect physics_dirty 2 '^REFUSED: physics checkout .* is dirty' 164 reset_world; echo x > "$OM/junk"; cr omega_dirty -- "${PD[@]}" 165 expect omega_dirty 2 '^REFUSED: omega tree .* is dirty' 166 reset_world; cr evidence_inside -- "${PD[@]}" --evidence-dir "$OM/evid" 167 expect evidence_inside 2 '^REFUSED: evidence dir .* inside a candidate tree' 168 reset_world; cr evidence_inside_physics -- "${PD[@]}" --evidence-dir "$PHYS/evid" 169 expect evidence_inside_physics 2 '^REFUSED: evidence dir .* inside a candidate tree' 170 reset_world; echo other > "$FLAG"; cr quiet_flag -- "${PD[@]}" 171 expect quiet_flag 2 '^REFUSED: quiet flag is up: other' 172 assert quiet_flag "a flag that is not ours is left alone" flag_is_other 173 reset_world; cr est_load CHIPRUN_EST_LOAD_CMD='echo 4242' -- "${PD[@]}" 174 expect est_load 2 '^REFUSED: an est_load process is running' 175 reset_world; exec 8> "$LOCK"; flock -x 8; cr gpu_lock -- "${PD[@]}"; exec 8>&- 176 expect gpu_lock 2 '^REFUSED: GPU lock .* is held' 177 assert gpu_lock "the quiet flag we raised is dropped on refusal" no_flag 178 reset_world; CRMAN=$T/m-hostfail.sh cr host_tier -- "${PD[@]}" 179 expect host_tier 2 '^REFUSED: host tier failed'; CRMAN=$T/m.sh 180 reset_world; cr nm_unreadable CHIPRUN_PREBUILT_BIN="$T/bin/script" -- "${PD[@]}" 181 expect nm_unreadable 2 '^REFUSED: nm cannot read' 182 reset_world; cr libm CHIPRUN_PREBUILT_BIN="$T/bin/libm" -- "${PD[@]}" 183 expect libm 2 '^REFUSED: libm math symbols' 184 reset_world; cr cuda CHIPRUN_PREBUILT_BIN="$T/bin/cuda" -- "${PD[@]}" 185 expect cuda 2 '^REFUSED: CUDA symbols' 186 reset_world; cr rc_nonzero FAKE_MODE=passrc -- "${PD[@]}" 187 expect rc_nonzero 1 '^CHIP_RUN: FAIL binary exit status 3' 188 reset_world; cr no_verdict FAKE_MODE=silent -- "${PD[@]}" 189 expect no_verdict 1 '^CHIP_RUN: FAIL no verdict line' 190 reset_world; cr fail_verdict FAKE_MODE=fail -- "${PD[@]}" 191 expect fail_verdict 1 '^CHIP_RUN: FAIL last verdict line does not match PASS_LINE' 192 assert fail_verdict "a FAIL receipt is still written" receipt_verdict fail_verdict FAIL 193 reset_world; cr omega_dirty_after FAKE_CMD="touch $OM/dirt" -- "${PD[@]}" 194 expect omega_dirty_after 1 '^CHIP_RUN: FAIL omega tree dirty after run' 195 reset_world; cr physics_dirty_after FAKE_CMD="touch $PHYS/dirt" -- "${PD[@]}" 196 expect physics_dirty_after 1 '^CHIP_RUN: FAIL physics tree dirty after run' 197 reset_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[@]}" 198 expect 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) 201 raw() { 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) 208 for 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" 212 done 213 # checked before the manifest is read: a missing manifest still gets the seam message 214 reset_world; raw CHIPRUN_PREBUILT_BIN="$T/bin/ok" -- "$T/nope.sh" --evidence-dir "$T/ev-seam_before" "${PD[@]}" 215 expect 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 217 mkman "$T/m-seamset.sh" "RAISE_QUIET=0; TAKE_GPU_LOCK=0; CHIPRUN_PREBUILT_BIN=$T/bin/ok" 218 reset_world; raw -- "$T/m-seamset.sh" --evidence-dir "$T/ev-seam_after" "${PD[@]}" 219 expect 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 221 mkman "$T/m-seamchg.sh" "RAISE_QUIET=0; TAKE_GPU_LOCK=0; CHIPRUN_PREBUILT_BIN=$T/bin/libm" 222 reset_world; raw CHIPRUN_SELFTEST=1 CHIPRUN_PREBUILT_BIN="$T/bin/ok" -- "$T/m-seamchg.sh" --evidence-dir "$T/ev-seam_changed" "${PD[@]}" 223 expect seam_changed 2 '^REFUSED: manifest changed CHIPRUN_PREBUILT_BIN' 224 mkman "$T/m-selfset.sh" "RAISE_QUIET=0; TAKE_GPU_LOCK=0; CHIPRUN_SELFTEST=1" 225 reset_world; raw -- "$T/m-selfset.sh" --evidence-dir "$T/ev-seam_selftest" "${PD[@]}" 226 expect 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. 229 mkman "$T/m-restore.sh" "QUIET_FLAG=$T/hostile-flag; GPU_LOCK=$T/hostile.lock; EST_LOAD_CMD='echo 4242'" 230 reset_world; rm -f "$LOCK" "$T/hostile.lock"; echo hostile > "$T/hostile-flag"; exec 8> "$T/hostile.lock"; flock -x 8 231 CRMAN=$T/m-restore.sh cr restore_vars FAKE_CMD="test -e $FLAG" -- "${PD[@]}"; exec 8>&-; CRMAN=$T/m.sh 232 expect restore_vars 0 '^CHIP_RUN: PASS' 233 assert restore_vars "the flag is dropped at the seam path after the run" no_flag 234 assert restore_vars "the manifest's flag path is left alone" test "$(cat "$T/hostile-flag" 2>/dev/null)" = hostile 235 assert restore_vars "the GPU lock was opened at the seam path" test -e "$LOCK" 236 237 # usage, arguments, manifest 238 reset_world; raw -- 239 expect usage 2 '^REFUSED: usage' 240 reset_world; cr bad_arg -- "${PD[@]}" --bogus 241 expect bad_arg 2 '^REFUSED: unknown argument --bogus' 242 reset_world; cr arg_value -- --physics-dir 243 expect arg_value 2 '^REFUSED: --physics-dir needs a value' 244 reset_world; CRMAN=$T/nope.sh cr no_manifest -- "${PD[@]}" 245 expect no_manifest 2 '^REFUSED: no manifest at' 246 mkman "$T/m-loadfail.sh" "false" 247 reset_world; CRMAN=$T/m-loadfail.sh cr manifest_load -- "${PD[@]}" 248 expect manifest_load 2 '^REFUSED: manifest .* failed to load' 249 mkman "$T/m-novars.sh" 'OWNER=""' 250 reset_world; CRMAN=$T/m-novars.sh cr manifest_vars -- "${PD[@]}" 251 expect manifest_vars 2 '^REFUSED: manifest does not set OWNER' 252 mkman "$T/m-gate.sh" 'GATE="bad/gate"' 253 reset_world; CRMAN=$T/m-gate.sh cr gate_chars -- "${PD[@]}" 254 expect gate_chars 2 '^REFUSED: GATE .* characters' 255 mkman "$T/m-noevid.sh" 'EVIDENCE_DIR=""' 256 reset_world; NOEVID=1; CRMAN=$T/m-noevid.sh cr no_evidence -- "${PD[@]}"; unset NOEVID 257 expect no_evidence 2 '^REFUSED: no evidence dir' 258 # REFUSE_EXIT must be an integer 1..125; REQUIRE_ALL_PASS must be 0 or 1 259 for 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=' 263 done 264 mkman "$T/m-rx125.sh" "REFUSE_EXIT=125" 265 reset_world; CRMAN=$T/m-rx125.sh cr refuse_exit_125 -- --physics-dir "$T/nonexistent" 266 expect refuse_exit_125 125 '^REFUSED: no physics checkout' 267 mkman "$T/m-rap-bad.sh" "REQUIRE_ALL_PASS=yes" 268 reset_world; CRMAN=$T/m-rap-bad.sh cr require_all_value -- "${PD[@]}" 269 expect require_all_value 1 '^CHIP_RUN: BAD_MANIFEST REQUIRE_ALL_PASS=' 270 271 # refusals that need a special world 272 mkdir -p "$T/nogit-om/tools" "$T/nogit/nvrm"; cp "$SRC_MODULE" "$T/nogit-om/tools/chip_run.sh" 273 reset_world; RAWMOD=$T/nogit-om/tools/chip_run.sh cr omega_head -- "${PD[@]}" 274 expect omega_head 2 '^REFUSED: cannot read omega HEAD' 275 mkman "$T/m-nolock.sh" "REFUSE_DIRTY_OMEGA=0" 276 reset_world; rm -f "$OM/physics.lock"; CRMAN=$T/m-nolock.sh cr physics_lock -- "${PD[@]}" 277 expect physics_lock 2 '^REFUSED: cannot read physics.lock' 278 reset_world; cr physics_head -- --physics-dir "$T/nogit" 279 expect physics_head 2 '^REFUSED: cannot read physics HEAD' 280 reset_world; cr mktemp TMPDIR="$T/nonexistent" -- "${PD[@]}" 281 expect mktemp 2 '^REFUSED: cannot create a run directory' 282 mkman "$T/m-race.sh" "HOST_TIER_CMD='echo other > $FLAG'" 283 reset_world; CRMAN=$T/m-race.sh cr flag_create -- "${PD[@]}" 284 expect flag_create 2 '^REFUSED: could not create the quiet flag' 285 assert flag_create "a flag created by someone else in the race is left alone" flag_is_other 286 reset_world; cr gpu_open CHIPRUN_GPU_LOCK="$T/nodir/x.lock" -- "${PD[@]}" 287 expect gpu_open 2 '^REFUSED: cannot open' 288 assert gpu_open "the quiet flag we raised is dropped on refusal" no_flag 289 reset_world; cr prebuilt_not_exec CHIPRUN_PREBUILT_BIN="$T/nonexistent" -- "${PD[@]}" 290 expect prebuilt_not_exec 2 '^REFUSED: CHIPRUN_PREBUILT_BIN .* is not executable' 291 mkman "$T/m-nosrc.sh" "TEST_SOURCE=src/nope.c" 292 reset_world; CRMAN=$T/m-nosrc.sh cr missing_source CHIPRUN_PREBUILT_BIN= -- "${PD[@]}" 293 expect missing_source 2 '^REFUSED: missing source src/nope.c' 294 mkman "$T/m-badsrc.sh" "TEST_SOURCE=src/bad.c" 295 reset_world; CRMAN=$T/m-badsrc.sh cr build_failed CHIPRUN_PREBUILT_BIN= -- "${PD[@]}" 296 expect build_failed 2 '^REFUSED: chip build failed' 297 mkman "$T/m-oksrc.sh" "TEST_SOURCE=src/okchip.c" 298 reset_world; CRMAN=$T/m-oksrc.sh cr build_ok CHIPRUN_PREBUILT_BIN= -- "${PD[@]}" 299 expect build_ok 0 '^CHIP_RUN: PASS' 300 # quiet flag: dropped only if still ours 301 reset_world; cr flag_not_ours FAKE_CMD="echo other > $FLAG" -- "${PD[@]}" 302 expect flag_not_ours 0 '^CHIP_RUN: PASS' 303 assert 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 305 reset_world; cr last_line_default FAKE_MODE=failpass -- "${PD[@]}" 306 expect last_line_default 0 '^CHIP_RUN: PASS' 307 reset_world; CRMAN=$T/m-all.sh cr fail_any_verdict FAKE_MODE=failpass -- "${PD[@]}" 308 expect fail_any_verdict 1 '^CHIP_RUN: FAIL a verdict line does not match PASS_LINE' 309 assert fail_any_verdict "a FAIL receipt is still written" receipt_verdict fail_any_verdict FAIL 310 reset_world; CRMAN=$T/m-all.sh cr all_pass_ok FAKE_MODE=pass -- "${PD[@]}" 311 expect 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) 313 reset_world; CRMAN=$T/m-pt.sh cr child_exit_pass0 FAKE_MODE=pass -- "${PD[@]}" 314 expect child_exit_pass0 0 '^CHIP_RUN: PASS' 315 reset_world; CRMAN=$T/m-pt.sh cr child_exit_pass1 FAKE_MODE=rc1 -- "${PD[@]}" 316 expect child_exit_pass1 1 '^CHIP_RUN: FAIL' 317 reset_world; CRMAN=$T/m-pt.sh cr child_exit_passthrough FAKE_MODE=rc2 -- "${PD[@]}" 318 expect child_exit_passthrough 2 '^CHIP_RUN: FAIL binary exit status 2' 319 assert 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 320 reset_world; CRMAN=$T/m-pt.sh cr child_exit_zero_fail FAKE_MODE=fail -- "${PD[@]}" 321 expect child_exit_zero_fail 1 '^CHIP_RUN: FAIL' 322 reset_world; CRMAN=$T/m.sh cr child_exit_default FAKE_MODE=rc2 -- "${PD[@]}" 323 expect child_exit_default 1 '^CHIP_RUN: FAIL binary exit status 2' 324 reset_world; CRMAN=$T/m-pt-bad.sh cr child_exit_value -- "${PD[@]}" 325 expect 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 327 reset_world; cr reuse -- "${PD[@]}"; cr reuse -- "${PD[@]}" 328 expect reuse 0 '^CHIP_RUN: PASS' 329 assert reuse "one log blob for two identical logs" test "$(ls "$T"/ev-reuse/blobs/*.log 2>/dev/null | wc -l)" = 1 330 RB=$(ls "$T"/ev-reuse/blobs/*.log 2>/dev/null | head -1); chmod u+w "$RB" 2>/dev/null; echo tamper >> "$RB" 331 reset_world; cr reuse -- "${PD[@]}" 332 expect blob_tamper 1 '^CHIP_RUN: FAIL log blob does not match its digest' 333 334 # physics dir resolution order: flag, PHYSICS_DIR, PHYSICS 335 reset_world; cr order_env_dir PHYSICS="$T/nonexistent" PHYSICS_DIR="$PHYS" -- 336 expect order_env_dir 0 '^CHIP_RUN: PASS' 337 reset_world; cr order_flag PHYSICS_DIR="$T/nonexistent" -- "${PD[@]}" 338 expect order_flag 0 '^CHIP_RUN: PASS' 339 reset_world; cr order_env_physics PHYSICS="$PHYS" -- 340 expect order_env_physics 0 '^CHIP_RUN: PASS' 341 # optional steps off: flag up and lock held do not matter 342 reset_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 343 expect optional_off 0 '^CHIP_RUN: PASS' 344 assert 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) 347 mkman "$T/m-hostlog.sh" "HOST_TIER_CMD='echo AAA; echo BBB'; RECEIPT_EXTRA_JQ='. + {hl: \$hostlog}'" 348 reset_world; CRMAN=$T/m-hostlog.sh cr hostlog -- "${PD[@]}" 349 expect hostlog 0 '^CHIP_RUN: PASS' 350 assert hostlog "receipt carries the whole host tier output" jq -e '.hl == "AAA\nBBB\n" and .host_tier == "BBB"' "$T"/ev-hostlog/*.json 351 mkman "$T/m-hostlog0.sh" "HOST_TIER_CMD=''; RECEIPT_EXTRA_JQ='. + {hl: \$hostlog}'" 352 reset_world; CRMAN=$T/m-hostlog0.sh cr hostlog_none -- "${PD[@]}" 353 expect hostlog_none 0 '^CHIP_RUN: PASS' 354 assert 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 357 mkman "$T/m-vw.sh" "RECEIPT_EXTRA_JQ='.verdict = \"NOT_RUN\"'" 358 reset_world; CRMAN=$T/m-vw.sh cr verdict_word_ok FAKE_MODE=fail -- "${PD[@]}" 359 expect verdict_word_ok 1 '^CHIP_RUN: NOT_RUN last verdict line' 360 assert verdict_word_ok "receipt verdict is the rewritten word" receipt_verdict verdict_word_ok NOT_RUN 361 reset_world; CRMAN=$T/m-vw.sh cr verdict_pass_changed -- "${PD[@]}" 362 expect verdict_pass_changed 1 '^CHIP_RUN: FAIL RECEIPT_EXTRA_JQ changed a PASS verdict' 363 mkman "$T/m-vf.sh" "RECEIPT_EXTRA_JQ='.verdict = \"PASS\"'" 364 reset_world; CRMAN=$T/m-vf.sh cr verdict_forged FAKE_MODE=fail -- "${PD[@]}" 365 expect verdict_forged 1 '^CHIP_RUN: FAIL RECEIPT_EXTRA_JQ forged a PASS verdict' 366 mkman "$T/m-vb.sh" "RECEIPT_EXTRA_JQ='.verdict = \"not run\"'" 367 reset_world; CRMAN=$T/m-vb.sh cr verdict_word FAKE_MODE=fail -- "${PD[@]}" 368 expect 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 371 mkman "$T/m-echo.sh" "ECHO_RE='^VERDICT'" 372 reset_world; CRMAN=$T/m-echo.sh cr echo_re -- "${PD[@]}" 373 expect echo_re 0 '^VERDICT PASS$' 374 assert echo_re "default-echoed noise line is not shown with ECHO_RE" bash -c '! grep -q "^fake chip args" "$1"' _ "$OUT" 375 assert echo_re "the log blob still holds the whole log" grep -q '^fake chip args' "$T"/ev-echo_re/blobs/*.log 376 reset_world; cr echo_default -- "${PD[@]}" 377 assert 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 " last (old transc style), also for fatal errors 380 mkman "$T/m-fs.sh" "FINAL_LINE_STYLE=verdict; RECEIPT_EXTRA_JQ='.verdict = \"NOT_RUN\"'" 381 reset_world; CRMAN=$T/m-fs.sh cr final_style FAKE_MODE=fail -- "${PD[@]}" 382 expect final_style 1 '^VERDICT NOT_RUN$' 383 assert final_style "no CHIP_RUN: line in verdict style" bash -c '! grep -q "^CHIP_RUN:" "$1"' _ "$OUT" 384 assert final_style "the last line is the verdict line" test "$(tail -n 1 "$OUT")" = "VERDICT NOT_RUN" 385 mkman "$T/m-fs2.sh" "FINAL_LINE_STYLE=verdict" 386 reset_world; CRMAN=$T/m-fs2.sh cr final_style_pass -- "${PD[@]}" 387 expect final_style_pass 0 '^VERDICT PASS$' 388 mkman "$T/m-fs3.sh" "FINAL_LINE_STYLE=verdict; RECEIPT_EXTRA_JQ='.verdict = \"PASS\"'" 389 reset_world; CRMAN=$T/m-fs3.sh cr final_style_fatal FAKE_MODE=fail -- "${PD[@]}" 390 expect final_style_fatal 1 '^RECEIPT_EXTRA_JQ forged a PASS verdict$' 391 assert final_style_fatal "fatal ends with VERDICT FAIL" test "$(tail -n 1 "$OUT")" = "VERDICT FAIL" 392 assert 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" 393 mkman "$T/m-fs4.sh" "FINAL_LINE_STYLE=bogus" 394 reset_world; CRMAN=$T/m-fs4.sh cr final_style_value -- "${PD[@]}" 395 expect 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 398 mkman "$T/m-fu.sh" "GPU_LOCK_MODE=fuser" 399 reset_world; exec 8> "$LOCK"; CRMAN=$T/m-fu.sh cr gpu_fuser -- "${PD[@]}"; exec 8>&- 400 expect gpu_fuser 2 "^REFUSED: $LOCK is held\$" 401 assert gpu_fuser "refused before the host tier and the quiet flag" no_flag 402 reset_world; exec 8> "$LOCK"; cr gpu_fuser_ctl -- "${PD[@]}"; exec 8>&- 403 expect gpu_fuser_ctl 0 '^CHIP_RUN: PASS' 404 reset_world; CRMAN=$T/m-fu.sh cr gpu_fuser_free -- "${PD[@]}" 405 expect gpu_fuser_free 0 '^CHIP_RUN: PASS' 406 mkman "$T/m-fu2.sh" "GPU_LOCK_MODE=bogus" 407 reset_world; CRMAN=$T/m-fu2.sh cr gpu_mode_value -- "${PD[@]}" 408 expect 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 410 assert bad_manifest_def "bad manifest exits nonzero, no 'command not found'" bash -c '! grep -q "command not found" "$1"' _ "$OUT" 411 assert final_style_value "bad manifest wrote no receipt" test -z "$(ls "$T"/ev-final_style_value/*.json 2>/dev/null)" 412 assert 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 415 reset_world; cr happy -- "${PD[@]}" -- --x 1 416 expect happy 0 '^VERDICT PASS$' 417 grep -q '^CHIP_RUN: PASS$' "$OUT" || bad happy "no final CHIP_RUN: PASS line" 418 assert happy "quiet flag dropped after the run" no_flag 419 R=$(ls "$T"/ev-happy/*.json 2>/dev/null | head -1) 420 assert happy "exactly one receipt" test "$(ls "$T"/ev-happy/*.json 2>/dev/null | wc -l)" = 1 421 assert happy "receipt mode is 0444" test "$(stat -c %a "$R" 2>/dev/null)" = 444 422 assert happy "receipt name is the sha256 of its content" test "$(sha256sum "$R" 2>/dev/null | cut -d' ' -f1).json" = "$(basename "$R")" 423 for 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" 427 done 428 assert 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" 429 LS=$(jq -r .chip_log_sha256 "$R" 2>/dev/null) 430 assert 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 434 reset_world; CRMAN=$T/m-dash.sh cr dash_verdict FAKE_MODE=dash -- "${PD[@]}"; CRMAN=$T/m.sh 435 expect dash_verdict 0 '^CHIP_RUN: PASS' 436 RD=$(ls "$T"/ev-dash_verdict/*.json 2>/dev/null | head -1) 437 assert 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 [] 439 mkman "$T/m-noargs.sh" 'RUN_ARGS=""' 440 reset_world; CRMAN=$T/m-noargs.sh cr empty_args -- "${PD[@]}"; CRMAN=$T/m.sh 441 expect empty_args 0 '^CHIP_RUN: PASS' 442 RE=$(ls "$T"/ev-empty_args/*.json 2>/dev/null | head -1) 443 assert empty_args "receipt run_args is the empty list" jqt '.run_args == []' "$RE" 444 assert empty_args "the binary was started with no arguments" grep -q '^fake chip args=0$' "$T"/ev-empty_args/blobs/*.log 445 446 if [ "$BAD" = 0 ]; then echo "CHIP_RUN_SELFTEST: PASS"; exit 0; fi 447 echo "CHIP_RUN_SELFTEST: FAIL ($BAD)"; exit 1