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 / osh / bashdiff / bashdiff.sh

128 lines · 7441 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# bashdiff.sh: run each case under REAL GNU bash 5.2 and under osh and compare (omega#344; grown from the #340 review
18# runner handoffs/osh-audit/L-diff.sh). Usage: bashdiff.sh OSH CASES [name-regex]
19#
20# Both shells run in a fresh scratch directory under the same pinned environment: env -i, LC_ALL=C, TZ=UTC,
21# PATH=/usr/bin:/bin, HOME=<scratch>/home. osh gets `--caps` with every effect allowed under / and a private journal.
22# Case file: blocks headed `### NAME | MODE | EXPECT | ARGS`, then the body lines.
23# MODE c SH -c BODY SH ARGS f SH s.sh ARGS (body in s.sh) s SH < s.sh
24# EXPECT match stdout, exit status, files and stderr (prefix-normalized) byte for byte
25# class stdout, exit status and files byte for byte; stderr only empty versus non-empty
26# refuse=T osh must refuse by name: its stderr contains T, stdout is empty, status is nonzero, and the
27# scratch directory is unchanged (no side effect); the bash result is shown, not compared
28# A body line `%%` splits it: lines before it are setup, run by bash in the scratch directory before each shell; the
29# "unchanged" check of refuse= compares against the state after setup. Lines starting `#` before the first case are
30# comments. stderr normalization: a leading `SH: `, `bash: `, `osh: `, `s.sh: `, the bash or osh path, with an optional `line N: `, becomes `SH: `.
31# BASHDIFF_NEGCTL=1: negative control. Every expectation is deliberately corrupted (bash stdout gets one extra byte;
32# a refuse= text gets a suffix no shell prints); every case must then FAIL, and the run passes only if all did.
33# Prints one line per case (PASS/REFUSED/FAIL) and `BASHDIFF PASS cases=N` or `BASHDIFF FAIL ...`; exit 0 only on PASS.
34set -u
35[ $# -ge 2 ] || { echo "usage: bashdiff.sh OSH CASES [name-regex]" >&2; exit 2; }
36OSH=$(readlink -f "$1"); CF=$(readlink -f "$2"); ONLY=${3:-.}
37[ -x "$OSH" ] || { echo "bashdiff: $1 is not executable" >&2; exit 2; }
38BASH_REF=/usr/bin/bash
39ver=$("$BASH_REF" -c 'echo $BASH_VERSION')
40case $ver in 5.2.*) ;; *) echo "bashdiff: need GNU bash 5.2.x at $BASH_REF, have $ver" >&2; exit 2 ;; esac
41NEG=${BASHDIFF_NEGCTL:-0}
42W=$(mktemp -d "${TMPDIR:-/tmp}/bashdiff.XXXXXX")
43trap 'chmod -R u+rwx "$W" 2>/dev/null; rm -rf "$W"' EXIT
44printf 'principal 1\nallow spawn /\nallow read /\nallow write /\nallow chdir /\n' > "$W/policy"
45mkdir -m 700 "$W/j"
46echo "bashdiff: bash $ver ($BASH_REF), env -i LC_ALL=C TZ=UTC PATH=/usr/bin:/bin, osh $OSH"
47pass=0; fail=0; refused=0; total=0
48norm() { sed -E "s#^(SH|bash|osh|s\\.sh|$BASH_REF|$OSH)(: line [0-9]+)?: #SH: #"; }
49snap() { ( cd "$1" && find . -path ./home -prune -o -print | LC_ALL=C sort | while IFS= read -r f; do
50 if [ -L "$f" ]; then printf 'L %s -> %s\n' "$f" "$(readlink "$f")"
51 elif [ -d "$f" ]; then printf 'D %s\n' "$f"
52 else printf 'F %s %s\n' "$f" "$(od -An -c "$f" 2>/dev/null | tr -s ' \n' ' ')"; fi; done ); }
53runone() { # $1 label $2 shell-kind (bash|osh) $3 mode, rest args; body in $W/body, setup in $W/setup
54 local pre=$W/$1 kind=$2 mode=$3; shift 3
55 local d=$W/run
56 chmod -R u+rwx "$d" 2>/dev/null; rm -rf "$d"; mkdir -p "$d/home"
57 ( cd "$d" && env -i PATH=/usr/bin:/bin HOME="$d/home" LC_ALL=C TZ=UTC "$BASH_REF" "$W/setup" ) >/dev/null 2>&1
58 cp "$W/body" "$d/s.sh"
59 snap "$d" > "$pre.before"
60 local -a sh
61 if [ "$kind" = bash ]; then sh=("$BASH_REF"); else sh=("$OSH" --caps "$W/policy" --journal "$W/j/effects.journal"); fi
62 ( cd "$d" && case $mode in
63 c) exec env -i PATH=/usr/bin:/bin HOME="$d/home" LC_ALL=C TZ=UTC "${sh[@]}" -c "$(cat s.sh)" SH "$@" ;;
64 f) exec env -i PATH=/usr/bin:/bin HOME="$d/home" LC_ALL=C TZ=UTC "${sh[@]}" s.sh "$@" ;;
65 s) exec env -i PATH=/usr/bin:/bin HOME="$d/home" LC_ALL=C TZ=UTC "${sh[@]}" < s.sh ;;
66 *) echo "bashdiff: bad mode $mode" >&2; exit 99 ;;
67 esac ) > "$pre.out" 2> "$pre.rawerr" < /dev/null
68 echo $? > "$pre.rc"
69 sed "s#$d#DIR#g" "$pre.rawerr" | norm > "$pre.err"
70 sed -i "s#$d#DIR#g" "$pre.out"
71 snap "$d" > "$pre.files"
72}
73show() { local k; for k in "$@"; do cmp -s "$W/b.$k" "$W/o.$k" && continue
74 echo " -- $k bash:"; head -c 600 "$W/b.$k" | sed 's/^/ /'; echo " -- $k osh:"; head -c 600 "$W/o.$k" | sed 's/^/ /'; done; }
75runcase() {
76 local name=$1 mode=$2 expect=$3; shift 3
77 total=$((total + 1))
78 runone b bash "$mode" "$@"
79 runone o osh "$mode" "$@"
80 local ok=1
81 case $expect in
82 match|class)
83 [ "$NEG" = 1 ] && printf 'X' >> "$W/b.out"
84 cmp -s "$W/b.out" "$W/o.out" && cmp -s "$W/b.rc" "$W/o.rc" && cmp -s "$W/b.files" "$W/o.files" || ok=0
85 if [ "$expect" = match ]; then cmp -s "$W/b.err" "$W/o.err" || ok=0
86 else [ -s "$W/b.err" ] && [ -s "$W/o.err" ] || { [ ! -s "$W/b.err" ] && [ ! -s "$W/o.err" ]; } || ok=0; fi
87 if [ $ok = 1 ]; then pass=$((pass + 1)); echo "PASS $name"
88 else fail=$((fail + 1)); echo "FAIL $name ($expect)"; show out err rc files; fi ;;
89 refuse=*)
90 local t=${expect#refuse=}
91 [ "$NEG" = 1 ] && t="$t<negctl>"
92 grep -qF -- "$t" "$W/o.rawerr" || ok=0
93 [ -s "$W/o.out" ] && ok=0
94 [ "$(cat "$W/o.rc")" != 0 ] || ok=0
95 cmp -s "$W/o.before" "$W/o.files" || ok=0
96 if [ $ok = 1 ]; then refused=$((refused + 1)); pass=$((pass + 1)); echo "REFUSED $name :: $(head -c 160 "$W/o.rawerr" | tr '\n' '|')"
97 else fail=$((fail + 1)); echo "FAIL $name (refuse=$t)"
98 echo " osh rc=$(cat "$W/o.rc") stdout=$(head -c 200 "$W/o.out" | tr '\n' '|') stderr=$(head -c 300 "$W/o.rawerr" | tr '\n' '|')"
99 cmp -s "$W/o.before" "$W/o.files" || { echo " osh changed the scratch directory:"; diff "$W/o.before" "$W/o.files" | head -20 | sed 's/^/ /'; }
100 echo " bash rc=$(cat "$W/b.rc") stdout=$(head -c 200 "$W/b.out" | tr '\n' '|')"; fi ;;
101 *) echo "bashdiff: case $name: bad expectation '$expect'" >&2; exit 2 ;;
102 esac
103}
104name=; mode=; expect=; args=()
105flush() {
106 [ -n "$name" ] || return 0
107 [[ $name =~ $ONLY ]] || return 0
108 if grep -qx '%%' "$W/raw"; then sed '/^%%$/,$d' "$W/raw" > "$W/setup"; sed '1,/^%%$/d' "$W/raw" > "$W/body"
109 else : > "$W/setup"; cp "$W/raw" "$W/body"; fi
110 runcase "$name" "$mode" "$expect" "${args[@]}"
111}
112: > "$W/raw"
113while IFS= read -r line || [ -n "$line" ]; do
114 if [[ $line == '### '* ]]; then
115 flush
116 IFS='|' read -r name mode expect rest <<< "${line#\#\#\# }"
117 name=${name// /}; mode=${mode// /}; expect=${expect# }; expect=${expect% }
118 eval "args=(${rest:-})"; : > "$W/raw"
119 elif [ -n "$name" ]; then printf '%s\n' "$line" >> "$W/raw"
120 fi
121done < "$CF"
122flush
123if [ "$NEG" = 1 ]; then
124 if [ "$total" -gt 0 ] && [ "$pass" -eq 0 ]; then echo "BASHDIFF NEGCTL PASS cases=$total detected=$fail"; exit 0; fi
125 echo "BASHDIFF NEGCTL FAIL cases=$total undetected=$pass"; exit 1
126fi
127if [ "$total" -gt 0 ] && [ "$fail" -eq 0 ]; then echo "BASHDIFF PASS cases=$total refused=$refused"; exit 0; fi
128echo "BASHDIFF FAIL cases=$total pass=$pass fail=$fail refused=$refused"; exit 1