interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / chain / tests / run.sh
| 1 | #!/bin/sh |
| 2 | # chain/tests/run.sh: gate for chain.om (the INTERPLANE ledger). |
| 3 | # OMC=/abs/seed/out/omc OMV=/abs/omega-mv sh chain/tests/run.sh |
| 4 | # The oracle is coreutils sha256sum, never the program under test: every prev, every payload digest |
| 5 | # and the head are recomputed with it. Then the ledger is damaged six ways and each must be refused. |
| 6 | # Negative control: a mutant that skips the prev check must accept a ledger with an edited record. |
| 7 | set -u |
| 8 | HERE=$(cd "$(dirname "$0")" && pwd) |
| 9 | C=$HERE/.. |
| 10 | OMV=${OMV:-$HOME/work/flux/omega-mv} |
| 11 | OMC=${OMC:-$OMV/seed/out/omc} |
| 12 | PIN=3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194 |
| 13 | SHA2=5cf0f98116def6917d3a373f2bd4c3932d46e3ff84d9567bbae9e720366fdb33 |
| 14 | T=$(mktemp -d) || exit 2 |
| 15 | trap 'rm -rf "$T"' EXIT |
| 16 | pass=0 |
| 17 | fail=0 |
| 18 | ok() { pass=$((pass + 1)); echo "PASS $1"; } |
| 19 | no() { fail=$((fail + 1)); echo "FAIL $1"; } |
| 20 | eq() { if [ "$2" = "$3" ]; then ok "$1"; else no "$1: got '$2' want '$3'"; fi; } |
| 21 | sha() { sha256sum | cut -d' ' -f1; } |
| 22 | |
| 23 | got=$(sha256sum "$OMC" 2>/dev/null | cut -d' ' -f1) |
| 24 | if [ "$got" != "$PIN" ]; then |
| 25 | echo "compiler pin mismatch: $got" >&2 |
| 26 | exit 2 |
| 27 | fi |
| 28 | got=$(sha256sum "$OMV/om/tls/sha2.om" 2>/dev/null | cut -d' ' -f1) |
| 29 | if [ "$got" != "$SHA2" ]; then |
| 30 | echo "sha2.om pin mismatch: $got" >&2 |
| 31 | exit 2 |
| 32 | fi |
| 33 | build() { { printf 'om2\n'; cat "$OMV/om/tls/sha2.om" "$1"; } | "$OMC" >"$2" 2>"$2.err" && chmod +x "$2"; } |
| 34 | build "$C/chain.om" "$T/chain" |
| 35 | eq "compile exit" "$?" 0 |
| 36 | L=$T/ledger.tsv |
| 37 | |
| 38 | # three records, three different payloads (text, empty, 5 MiB of bytes) |
| 39 | printf 'READY forge/galaxy head 37ec0fb6\n' >"$T/p1" |
| 40 | : >"$T/p2" |
| 41 | head -c 5242880 /dev/urandom >"$T/p3" |
| 42 | o1=$("$T/chain" add "$L" READY 'mac-main(omp)' 'forge-web 37ec0fb6' <"$T/p1"); eq "add 1 exit" "$?" 0 |
| 43 | o2=$("$T/chain" add "$L" VERDICT 'factory-bay-2(omp)' 'forge-web 4e675a80 MERGE_AFTER_FIX' <"$T/p2"); eq "add 2 exit" "$?" 0 |
| 44 | o3=$("$T/chain" add "$L" MERGE 'merge-desk' 'forge-web b9bc96e6' <"$T/p3"); eq "add 3 exit" "$?" 0 |
| 45 | eq "three lines" "$(wc -l <"$L" | tr -d ' ')" 3 |
| 46 | v=$("$T/chain" verify "$L"); eq "verify exit" "$?" 0 |
| 47 | |
| 48 | # the oracle: sha256sum over each line without its line feed |
| 49 | h1=$(sed -n 1p "$L" | tr -d '\n' | sha); h2=$(sed -n 2p "$L" | tr -d '\n' | sha); h3=$(sed -n 3p "$L" | tr -d '\n' | sha) |
| 50 | eq "record 1 prev is GENESIS" "$(sed -n 1p "$L" | cut -f7)" GENESIS |
| 51 | eq "record 2 prev is sha256sum of line 1" "$(sed -n 2p "$L" | cut -f7)" "$h1" |
| 52 | eq "record 3 prev is sha256sum of line 2" "$(sed -n 3p "$L" | cut -f7)" "$h2" |
| 53 | eq "verify prints the count and sha256sum of the last line" "$v" "$(printf 'OK\t3\t%s' "$h3")" |
| 54 | eq "add 1 printed its head" "$o1" "$(printf 'OK\t1\t%s' "$h1")" |
| 55 | eq "add 3 printed its head" "$o3" "$(printf 'OK\t3\t%s' "$h3")" |
| 56 | eq "payload 1 digest is sha256sum" "$(sed -n 1p "$L" | cut -f6)" "$(sha <"$T/p1")" |
| 57 | eq "empty payload digest is sha256sum" "$(sed -n 2p "$L" | cut -f6)" "$(sha <"$T/p2")" |
| 58 | eq "5 MiB payload digest is sha256sum" "$(sed -n 3p "$L" | cut -f6)" "$(sha <"$T/p3")" |
| 59 | eq "seq column" "$(cut -f1 "$L" | tr '\n' ' ')" "1 2 3 " |
| 60 | now=$(date -u +%s); t3=$(date -u -d "$(sed -n 3p "$L" | cut -f2)" +%s 2>/dev/null || echo 0) |
| 61 | if [ $((now - t3)) -ge 0 ] && [ $((now - t3)) -le 30 ]; then ok "record time is the clock's UTC"; else no "record time is the clock's UTC: $t3 vs $now"; fi |
| 62 | eq "fields kept as given" "$(sed -n 2p "$L" | cut -f3-5)" "$(printf 'VERDICT\tfactory-bay-2(omp)\tforge-web 4e675a80 MERGE_AFTER_FIX')" |
| 63 | |
| 64 | # check: the right payload, a wrong one, a record that does not exist |
| 65 | "$T/chain" check "$L" 1 <"$T/p1" >/dev/null; eq "check right payload" "$?" 0 |
| 66 | "$T/chain" check "$L" 3 <"$T/p3" >/dev/null; eq "check 5 MiB payload" "$?" 0 |
| 67 | "$T/chain" check "$L" 1 <"$T/p3" >/dev/null; eq "check wrong payload" "$?" 4 |
| 68 | printf 'READY forge/galaxy head 37ec0fb7\n' | "$T/chain" check "$L" 1 >/dev/null; eq "check payload one byte off" "$?" 4 |
| 69 | "$T/chain" check "$L" 9 <"$T/p1" >/dev/null; eq "check missing record" "$?" 4 |
| 70 | |
| 71 | # damage: each must be refused, with the line and reason |
| 72 | bad() { r=$("$T/chain" verify "$2"); eq "$1 exit" "$?" 2; eq "$1 says" "$r" "$(printf 'BAD\tline\t%s\t%s' "$3" "$4")"; } |
| 73 | sed '2s/MERGE_AFTER_FIX/MERGE/' "$L" >"$T/d1"; bad "edited record 2" "$T/d1" 3 5 |
| 74 | sed '2d' "$L" >"$T/d2"; bad "removed record 2" "$T/d2" 2 2 |
| 75 | { sed -n 1p "$L"; sed -n 3p "$L"; sed -n 2p "$L"; } >"$T/d3"; bad "swapped records 2 and 3" "$T/d3" 2 2 |
| 76 | { cat "$L"; sed -n 3p "$L" | sed 's/^3/4/'; } >"$T/d4"; bad "forged record 4 with a stale prev" "$T/d4" 4 5 |
| 77 | head -c -1 "$L" >"$T/d5"; bad "last line feed cut off" "$T/d5" 3 7 |
| 78 | sed '1s/GENESIS/'"$h1"'/' "$L" >"$T/d6"; bad "record 1 not on GENESIS" "$T/d6" 1 5 |
| 79 | sed '3s/\t[0-9a-f]\{64\}\t/\tzz\t/' "$L" >"$T/d7"; bad "payload digest not hex" "$T/d7" 3 4 |
| 80 | cp "$T/d1" "$T/d1b" |
| 81 | "$T/chain" add "$T/d1b" NOTE x y <"$T/p1" >/dev/null; eq "add refuses a damaged ledger" "$?" 2 |
| 82 | if cmp -s "$T/d1" "$T/d1b"; then ok "a refused add writes nothing"; else no "a refused add writes nothing"; fi |
| 83 | |
| 84 | # arguments |
| 85 | "$T/chain" add "$T/a.tsv" verdict a b <"$T/p1" >/dev/null 2>&1; eq "lowercase kind refused" "$?" 1 |
| 86 | "$T/chain" add "$T/a.tsv" NOTE "$(printf 'a\tb')" c <"$T/p1" >/dev/null 2>&1; eq "tab in actor refused" "$?" 1 |
| 87 | "$T/chain" add "$T/a.tsv" NOTE a "$(printf 'x\ny')" <"$T/p1" >/dev/null 2>&1; eq "line feed in subject refused" "$?" 1 |
| 88 | "$T/chain" add "$T/a.tsv" NOTE a >/dev/null 2>&1 </dev/null; eq "missing argument refused" "$?" 1 |
| 89 | "$T/chain" frob "$L" >/dev/null 2>&1; eq "unknown command refused" "$?" 1 |
| 90 | eq "refused adds wrote no record" "$(cat "$T/a.tsv" 2>/dev/null | wc -l | tr -d ' ')" 0 |
| 91 | "$T/chain" axx "$T/a.tsv" NOTE a b <"$T/p1" >/dev/null 2>&1; eq "a command that only starts like add is refused" "$?" 1 |
| 92 | "$T/chain" verifx "$L" >/dev/null 2>&1; eq "a command that only starts like verify is refused" "$?" 1 |
| 93 | "$T/chain" addx "$T/a.tsv" NOTE a b <"$T/p1" >/dev/null 2>&1; eq "add with a byte after it is refused" "$?" 1 |
| 94 | "$T/chain" verifyx "$L" >/dev/null 2>&1; eq "verify with a byte after it is refused" "$?" 1 |
| 95 | "$T/chain" checkx "$L" 1 <"$T/p1" >/dev/null 2>&1; eq "check with a byte after it is refused" "$?" 1 |
| 96 | eq "the near-miss command wrote no record" "$(cat "$T/a.tsv" 2>/dev/null | wc -l | tr -d ' ')" 0 |
| 97 | |
| 98 | # verify enforces the format, not only the chain: a hand-made ledger whose record 2 breaks one field |
| 99 | # rule, relinked with sha256sum so only that rule can refuse it |
| 100 | X=$(printf x | sha) |
| 101 | rec() { if [ -s "$1" ]; then p=$(tail -n 1 "$1" | tr -d '\n' | sha); else p=GENESIS; fi; printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$2" "$3" "$4" "$5" "$6" "$X" "$p" >>"$1"; } |
| 102 | two() { f=$T/r$1; : >"$f"; rec "$f" 1 2024-01-01T00:00:00Z NOTE a s; rec "$f" "$3" "$4" "$5" "$6" "$7"; r=$("$T/chain" verify "$f") |
| 103 | if [ "$2" = 0 ]; then eq "$1 accepted" "$(echo "$r" | cut -f1-2)" "$(printf 'OK\t2')"; else eq "$1 refused" "$r" "$(printf 'BAD\tline\t2\t%s' "$2")"; fi; } |
| 104 | K24=ABCDEFGHIJKLMNOPQRSTUVWX; A64=$(printf '%064d' 0); S128=$(printf '%0128d' 0) |
| 105 | two "leap day 2024-02-29" 0 2 2024-02-29T23:59:59Z NOTE a s |
| 106 | two "kind 24, actor 64, subject 128 bytes" 0 2 2024-01-01T00:00:00Z "$K24" "$A64" "$S128" |
| 107 | two "seq with a leading zero" 2 02 2024-01-02T00:00:00Z NOTE a s |
| 108 | two "time earlier than record 1" 3 2 2023-12-31T23:59:59Z NOTE a s |
| 109 | two "month 13" 3 2 2024-13-01T00:00:00Z NOTE a s |
| 110 | two "month 00 (in a later year, so the time order cannot refuse it)" 3 2 2025-00-01T00:00:00Z NOTE a s |
| 111 | two "2025-02-29 (not a leap year)" 3 2 2025-02-29T00:00:00Z NOTE a s |
| 112 | two "hour 24" 3 2 2024-01-02T24:00:00Z NOTE a s |
| 113 | two "2400-02-29 (leap, divisible by 400)" 0 2 2400-02-29T00:00:00Z NOTE a s |
| 114 | two "2100-02-29 (divisible by 100, not a leap year)" 3 2 2100-02-29T00:00:00Z NOTE a s |
| 115 | two "2024-04-31 (a 30-day month)" 3 2 2024-04-31T00:00:00Z NOTE a s |
| 116 | two "day 00" 3 2 2024-02-00T00:00:00Z NOTE a s |
| 117 | two "minute 60" 3 2 2024-01-02T00:60:00Z NOTE a s |
| 118 | two "second 60" 3 2 2024-01-02T00:00:60Z NOTE a s |
| 119 | two "lowercase kind" 8 2 2024-01-02T00:00:00Z note a s |
| 120 | two "empty kind" 8 2 2024-01-02T00:00:00Z '' a s |
| 121 | two "kind 25 bytes" 8 2 2024-01-02T00:00:00Z "${K24}Y" a s |
| 122 | two "actor 65 bytes" 8 2 2024-01-02T00:00:00Z NOTE "${A64}0" s |
| 123 | two "subject 129 bytes" 8 2 2024-01-02T00:00:00Z NOTE a "${S128}0" |
| 124 | two "DEL byte in actor" 1 2 2024-01-02T00:00:00Z NOTE "$(printf 'a\177b')" s |
| 125 | two "non-ASCII byte in subject" 1 2 2024-01-02T00:00:00Z NOTE a "$(printf 'caf\303\251')" |
| 126 | two "carriage return in subject" 1 2 2024-01-02T00:00:00Z NOTE a "$(printf 'a\rb')" |
| 127 | |
| 128 | # add refuses a record earlier than the last one (a clock behind the ledger) and writes nothing |
| 129 | F=$T/future; : >"$F"; rec "$F" 1 2999-01-01T00:00:00Z NOTE a s; cp "$F" "$F.0" |
| 130 | "$T/chain" add "$F" NOTE a b <"$T/p1" >/dev/null 2>&1; eq "add with the clock behind the last record refused" "$?" 1 |
| 131 | if cmp -s "$F" "$F.0"; then ok "the refused add left the ledger unchanged"; else no "the refused add left the ledger unchanged"; fi |
| 132 | |
| 133 | # twenty writers at once: the lock keeps one chain |
| 134 | i=0 |
| 135 | while [ "$i" -lt 20 ]; do printf 'p%s' "$i" | "$T/chain" add "$T/par.tsv" NOTE "w$i" 'parallel' >/dev/null & i=$((i + 1)); done |
| 136 | wait |
| 137 | eq "twenty parallel adds verify" "$("$T/chain" verify "$T/par.tsv" | cut -f1-2)" "$(printf 'OK\t20')" |
| 138 | eq "twenty distinct writers recorded" "$(cut -f4 "$T/par.tsv" | sort -u | wc -l | tr -d ' ')" 20 |
| 139 | |
| 140 | # negative control: skip the prev check; the edited ledger d1 must then verify |
| 141 | sed 's/^if not lprev q lfe q$/if false/' "$C/chain.om" >"$T/mutant.om" |
| 142 | if cmp -s "$C/chain.om" "$T/mutant.om"; then |
| 143 | no "mutant differs from source" |
| 144 | else |
| 145 | build "$T/mutant.om" "$T/mutant" |
| 146 | "$T/mutant" verify "$T/d1" >/dev/null |
| 147 | if [ "$?" -eq 0 ]; then ok "mutant without the prev check accepts the edited ledger (so the check is what refuses it)"; else no "mutant without the prev check accepts the edited ledger"; fi |
| 148 | fi |
| 149 | |
| 150 | # feed: one record per new complete line of a file; only the line's digest enters the ledger |
| 151 | F=$T/feed.tsv |
| 152 | mkdir "$T/src" |
| 153 | S=$T/src/BOARD.md |
| 154 | printf 'first line\n\nthird with a tab\there\n' >"$S" |
| 155 | o=$("$T/chain" feed "$F" "$S" MSG hive-board); eq "feed exit" "$?" 0 |
| 156 | eq "feed wrote three records" "$(wc -l <"$F" | tr -d ' ')" 3 |
| 157 | eq "feed subjects name the file and the line" "$(cut -f5 "$F" | tr '\n' '|')" "BOARD.md line 1|BOARD.md line 2|BOARD.md line 3|" |
| 158 | eq "feed kind and actor" "$(cut -f3,4 "$F" | sort -u)" "$(printf 'MSG\thive-board')" |
| 159 | for n in 1 2 3; do |
| 160 | eq "fed payload $n is sha256sum of the line without its line feed" "$(sed -n "${n}p" "$F" | cut -f6)" "$(sed -n "${n}p" "$S" | tr -d '\n' | sha)" |
| 161 | done |
| 162 | eq "feed printed the count and the head" "$o" "$(printf 'OK\t3\t%s' "$(sed -n 3p "$F" | tr -d '\n' | sha)")" |
| 163 | "$T/chain" feed "$F" "$S" MSG hive-board >/dev/null; eq "feed again exit" "$?" 0 |
| 164 | eq "feed again adds nothing" "$(wc -l <"$F" | tr -d ' ')" 3 |
| 165 | printf 'fourth\nfifth, still being wr' >>"$S" |
| 166 | "$T/chain" feed "$F" "$S" MSG hive-board >/dev/null |
| 167 | eq "a line without its line feed is left for later" "$(cut -f5 "$F" | tail -n 1)" "BOARD.md line 4" |
| 168 | printf 'itten\n' >>"$S" |
| 169 | "$T/chain" feed "$F" "$S" MSG hive-board >/dev/null |
| 170 | eq "the finished line is recorded whole" "$(sed -n 5p "$F" | cut -f5,6)" "$(printf 'BOARD.md line 5\t%s' "$(printf 'fifth, still being written' | sha)")" |
| 171 | printf 'x' | "$T/chain" add "$F" NOTE someone 'BOARD.md line 500' >/dev/null |
| 172 | printf 'x' | "$T/chain" add "$F" MSG someone 'BOARD.md lines 600' >/dev/null |
| 173 | printf 'x' | "$T/chain" add "$F" MSG someone 'XBOARD.md line 700' >/dev/null |
| 174 | printf 'sixth\n' >>"$S" |
| 175 | "$T/chain" feed "$F" "$S" MSG hive-board >/dev/null |
| 176 | eq "feed resumes by its own kind and exact file name" "$(cut -f5 "$F" | tail -n 1)" "BOARD.md line 6" |
| 177 | cp "$S" "$T/src/OTHER.md" |
| 178 | "$T/chain" feed "$F" "$T/src/OTHER.md" MSG hive-board >/dev/null |
| 179 | eq "another file starts at its line 1" "$(awk -F'\t' '$3=="MSG" && $5=="OTHER.md line 1"' "$F" | wc -l | tr -d ' ')" 1 |
| 180 | "$T/chain" feed "$F" "$S" LOG hive-board >/dev/null |
| 181 | eq "another kind starts at line 1" "$(awk -F'\t' '$3=="LOG" && $5=="BOARD.md line 1"' "$F" | wc -l | tr -d ' ')" 1 |
| 182 | "$T/chain" feed "$F" "$S" MS hive-board >/dev/null |
| 183 | eq "a kind that is a prefix of a recorded kind starts at line 1" "$(awk -F'\t' '$3=="MS" && $5=="BOARD.md line 1"' "$F" | wc -l | tr -d ' ')" 1 |
| 184 | i=0 |
| 185 | while [ "$i" -lt 8 ]; do "$T/chain" feed "$T/pfeed.tsv" "$T/src/OTHER.md" MSG "w$i" >/dev/null & i=$((i + 1)); done |
| 186 | wait |
| 187 | eq "eight feeds of one file at once record each line once" "$(cut -f5 "$T/pfeed.tsv" | sort | uniq -d | wc -l | tr -d ' ') $(wc -l <"$T/pfeed.tsv" | tr -d ' ')" "0 $(wc -l <"$T/src/OTHER.md" | tr -d ' ')" |
| 188 | eq "and leave a ledger that verifies" "$("$T/chain" verify "$T/pfeed.tsv" | cut -f1)" OK |
| 189 | { head -c 200000 /dev/urandom | tr '\n' 'x'; printf '\n'; } >"$T/src/BIG.bin" |
| 190 | "$T/chain" feed "$F" "$T/src/BIG.bin" BLOB hive-board >/dev/null; eq "a 200,000-byte line of arbitrary bytes exit" "$?" 0 |
| 191 | eq "its digest is sha256sum of the 200,000 bytes" "$(tail -n 1 "$F" | cut -f5,6)" "$(printf 'BIG.bin line 1\t%s' "$(head -c 200000 "$T/src/BIG.bin" | sha)")" |
| 192 | eq "the fed ledger verifies" "$("$T/chain" verify "$F" | cut -f1)" OK |
| 193 | printf 'first line' | "$T/chain" check "$F" 1 >/dev/null; eq "a fed line checks against its record" "$?" 0 |
| 194 | printf 'first lime' | "$T/chain" check "$F" 1 >/dev/null; eq "an altered line does not" "$?" 4 |
| 195 | cp "$F" "$T/feed.before" |
| 196 | printf 'only one line now\n' >"$S" |
| 197 | "$T/chain" feed "$F" "$S" MSG hive-board >/dev/null; eq "a source with fewer lines than recorded exits 5" "$?" 5 |
| 198 | if cmp -s "$F" "$T/feed.before"; then ok "and nothing is written"; else no "and nothing is written"; fi |
| 199 | "$T/chain" feed "$T/fa.tsv" "$T/src/OTHER.md" msg a >/dev/null 2>&1; eq "feed lowercase kind refused" "$?" 1 |
| 200 | "$T/chain" feed "$T/fa.tsv" "$T/src/OTHER.md" MSG "$(printf 'a\tb')" >/dev/null 2>&1; eq "feed tab in actor refused" "$?" 1 |
| 201 | long=$(awk 'BEGIN { for (i = 0; i < 101; i++) printf "n" }'); cp "$T/src/OTHER.md" "$T/src/$long" |
| 202 | "$T/chain" feed "$T/fa.tsv" "$T/src/$long" MSG a >/dev/null 2>&1; eq "feed file name over 100 bytes refused" "$?" 1 |
| 203 | "$T/chain" feed "$T/fa.tsv" "$T/src/none" MSG a >/dev/null 2>&1; eq "feed unreadable file" "$?" 3 |
| 204 | "$T/chain" feedx "$T/fa.tsv" "$T/src/OTHER.md" MSG a >/dev/null 2>&1; eq "feedx refused" "$?" 1 |
| 205 | eq "refused feeds wrote no record" "$(cat "$T/fa.tsv" 2>/dev/null | wc -l | tr -d ' ')" 0 |
| 206 | cp "$T/d1" "$T/d1c" |
| 207 | "$T/chain" feed "$T/d1c" "$T/src/OTHER.md" MSG a >/dev/null; eq "feed refuses a damaged ledger" "$?" 2 |
| 208 | if cmp -s "$T/d1" "$T/d1c"; then ok "a refused feed writes nothing"; else no "a refused feed writes nothing"; fi |
| 209 | printf 'seed\n' | "$T/chain" add "$T/self.tsv" NOTE a seed >/dev/null; cp "$T/self.tsv" "$T/self.before"; ln -s "$T/self.tsv" "$T/src/self-link" |
| 210 | timeout 10 "$T/chain" feed "$T/self.tsv" "$T/self.tsv" MSG a >/dev/null; eq "feed of the ledger into itself refused" "$?" 1 |
| 211 | timeout 10 "$T/chain" feed "$T/self.tsv" "$T/src/self-link" MSG a >/dev/null; eq "feed of the ledger through a link refused" "$?" 1 |
| 212 | if cmp -s "$T/self.before" "$T/self.tsv"; then ok "the ledger fed into itself is unchanged"; else no "the ledger fed into itself is unchanged"; fi |
| 213 | |
| 214 | # negative controls for feed: forget what was recorded; ignore a source that lost lines |
| 215 | sed 's/^if gt v get gs 6$/if false/' "$C/chain.om" >"$T/fm1.om" |
| 216 | sed 's/^if lt n start$/if false/' "$C/chain.om" >"$T/fm2.om" |
| 217 | if cmp -s "$C/chain.om" "$T/fm1.om" || cmp -s "$C/chain.om" "$T/fm2.om"; then |
| 218 | no "feed mutants differ from source" |
| 219 | else |
| 220 | build "$T/fm1.om" "$T/fm1"; build "$T/fm2.om" "$T/fm2" |
| 221 | "$T/fm1" feed "$T/fm1.tsv" "$T/src/OTHER.md" MSG a >/dev/null; "$T/fm1" feed "$T/fm1.tsv" "$T/src/OTHER.md" MSG a >/dev/null |
| 222 | want=$(wc -l <"$T/src/OTHER.md" | tr -d ' ') |
| 223 | if [ "$(wc -l <"$T/fm1.tsv" | tr -d ' ')" -gt "$want" ]; then ok "mutant that forgets recorded lines records them twice"; else no "mutant that forgets recorded lines records them twice"; fi |
| 224 | "$T/fm2" feed "$T/fm2.tsv" "$T/src/OTHER.md" MSG a >/dev/null; printf 'short\n' >"$T/src/SHORT.md"; mv "$T/src/SHORT.md" "$T/src/OTHER.md" |
| 225 | "$T/fm2" feed "$T/fm2.tsv" "$T/src/OTHER.md" MSG a >/dev/null |
| 226 | eq "mutant without the lost-lines rule exits 0" "$?" 0 |
| 227 | fi |
| 228 | |
| 229 | # one sync per run: add syncs its record; feed syncs once, after its last record (strace counts) |
| 230 | nsync() { strace -f -qq -e trace=fsync -o "$T/st" "$@" >/dev/null 2>&1; grep -c 'fsync(' "$T/st"; } |
| 231 | if command -v strace >/dev/null 2>&1; then |
| 232 | printf 'a\nb\nc\nd\ne\n' >"$T/src/FIVE.md" |
| 233 | eq "feed of five lines syncs once" "$(nsync "$T/chain" feed "$T/sy.tsv" "$T/src/FIVE.md" MSG a)" 1 |
| 234 | eq "and wrote the five records" "$(wc -l <"$T/sy.tsv" | tr -d ' ')" 5 |
| 235 | eq "add syncs its one record" "$(nsync "$T/chain" add "$T/sy.tsv" NOTE a s <"$T/p1")" 1 |
| 236 | awk '/^if ne fsync fd 0$/ { c++; if (c == 2) { print "if false"; next } } { print }' "$C/chain.om" >"$T/sm1.om" |
| 237 | sed 's/^if eq get gs 5 0$/if true/' "$C/chain.om" >"$T/sm2.om" |
| 238 | if cmp -s "$C/chain.om" "$T/sm1.om" || cmp -s "$C/chain.om" "$T/sm2.om"; then |
| 239 | no "sync mutants differ from source" |
| 240 | else |
| 241 | build "$T/sm1.om" "$T/sm1"; build "$T/sm2.om" "$T/sm2" |
| 242 | eq "mutant without the final sync syncs nothing" "$(nsync "$T/sm1" feed "$T/sm1.tsv" "$T/src/FIVE.md" MSG a)" 0 |
| 243 | eq "mutant that syncs every record syncs six times" "$(nsync "$T/sm2" feed "$T/sm2.tsv" "$T/src/FIVE.md" MSG a)" 6 |
| 244 | fi |
| 245 | else |
| 246 | no "strace is needed to count syncs" |
| 247 | fi |
| 248 | |
| 249 | # page: the head as a page anyone can check by hand; the check it prints must itself work |
| 250 | awk 'BEGIN { for (i = 1; i <= 1234; i++) print "message " i }' >"$T/src/LONG.md" |
| 251 | P=$T/page.tsv |
| 252 | o=$("$T/chain" feed "$P" "$T/src/LONG.md" MSG hive-board "$T/page.html"); eq "feed with a page exit" "$?" 0 |
| 253 | HEADP=$(printf %s "$(tail -n 1 "$P")" | sha) |
| 254 | eq "feed with a page prints the count and the head" "$o" "$(printf 'OK\t1234\t%s' "$HEADP")" |
| 255 | eq "page shows the head sha256sum gives" "$(grep -c "<pre class='cmd'>$HEADP</pre>" "$T/page.html")" 1 |
| 256 | eq "page shows the count, digits grouped" "$(grep -c '<dt>Records</dt><dd>1,234</dd>' "$T/page.html")" 1 |
| 257 | eq "page shows the last record's time" "$(grep -c "<dd>$(tail -n 1 "$P" | cut -f2 | tr 'T' ' ' | tr -d 'Z')</dd>" "$T/page.html")" 1 |
| 258 | eq "page has no script and no inline style" "$(grep -ci '<script\|style=' "$T/page.html")" 0 |
| 259 | eq "page left no temporary file" "$(ls "$T" | grep -c '^page\.html\.tmp$')" 0 |
| 260 | "$T/chain" page "$P" "$T/page2.html" >/dev/null; eq "page exit" "$?" 0 |
| 261 | if cmp -s "$T/page.html" "$T/page2.html"; then ok "page and feed write the same page"; else no "page and feed write the same page"; fi |
| 262 | # the commands on the page, taken from the page: entity-decoded, the download replaced by a local copy |
| 263 | cmds() { awk -v n="$1" 'BEGIN { RS = "<pre class=.cmd.>"; FS = "</pre>" } NR == n + 1 { printf "%s\n", $1 }' "$T/page.html" | sed -e 's/"/"/g' -e 's/>/>/g' -e 's/</</g' -e 's/&/\&/g'; } |
| 264 | eq "the page's first command fetches the public ledger" "$(cmds 2 | head -n 1)" "curl -s https://interplane.io/proof/board.tsv -o board.tsv" |
| 265 | mkdir "$T/hand" |
| 266 | cp "$P" "$T/hand/board.tsv" |
| 267 | cmds 2 | tail -n +2 >"$T/hand/head.sh" |
| 268 | cmds 3 >"$T/hand/links.sh" |
| 269 | eq "the page's head command gives the page's head" "$(cd "$T/hand" && sh head.sh | cut -d' ' -f1)" "$HEADP" |
| 270 | eq "the page's link check passes on the ledger" "$(cd "$T/hand" && sh links.sh 2>/dev/null)" "chain intact" |
| 271 | sed -i '600s/hive-board/hive-b0ard/' "$T/hand/board.tsv" |
| 272 | eq "the page's link check fails on a ledger with one edited record" "$(cd "$T/hand" && sh links.sh 2>/dev/null | grep -c 'chain intact')" 0 |
| 273 | # the page's "prove one message" command, from the page, on a board line that typing it by hand |
| 274 | # inside quotes would break: a quote, an apostrophe, parentheses, a dollar sign and a backquote |
| 275 | BL='it'"'"'s "quoted" (a) $HOME `x` line' |
| 276 | printf 'first line\n%s\nlast line\n' "$BL" >"$T/hand/BOARD.md" |
| 277 | cmds 4 | sed 's/Np/2p/' >"$T/hand/one.sh" |
| 278 | eq "the page's prove-one-message command gives the line's SHA-256" "$(cd "$T/hand" && sh one.sh 2>/dev/null | cut -d' ' -f1)" "$(printf %s "$BL" | sha256sum | cut -d' ' -f1)" |
| 279 | eq "the page ends with </html> and a line feed" "$(tail -c 8 "$T/page.html" | tr '\n' '|')" "</html>|" |
| 280 | "$T/chain" page "$T/d1" "$T/bad.html" >/dev/null; eq "page of a damaged ledger exits 2" "$?" 2 |
| 281 | eq "and writes no page" "$(ls "$T" | grep -c '^bad\.html')" 0 |
| 282 | : >"$T/empty.tsv" |
| 283 | "$T/chain" page "$T/empty.tsv" "$T/empty.html" >/dev/null; eq "page of an empty ledger exit" "$?" 0 |
| 284 | eq "it says there are no records yet, twice" "$(grep -o 'no records yet' "$T/empty.html" | wc -l | tr -d ' ')" 2 |
| 285 | longp=$T/$(awk 'BEGIN { for (i = 0; i < 1001; i++) printf "p" }') |
| 286 | cp "$P" "$T/page.before"; printf 'one more\n' >>"$T/src/LONG.md" |
| 287 | "$T/chain" feed "$P" "$T/src/LONG.md" MSG hive-board "$longp" >/dev/null 2>&1; eq "feed with a page name over 1,000 bytes refused" "$?" 1 |
| 288 | if cmp -s "$P" "$T/page.before"; then ok "and it records nothing"; else no "and it records nothing"; fi |
| 289 | "$T/chain" page "$P" >/dev/null 2>&1; eq "page without a name refused" "$?" 1 |
| 290 | "$T/chain" pagex "$P" "$T/x.html" >/dev/null 2>&1; eq "pagex refused" "$?" 1 |
| 291 | # the page never replaces the ledger or the fed file, never writes through a stale OUT.tmp, never leaves one |
| 292 | cp "$P" "$T/same.tsv" |
| 293 | "$T/chain" page "$T/same.tsv" "$T/same.tsv" >/dev/null 2>&1; eq "page named as its own ledger refused" "$?" 1 |
| 294 | if cmp -s "$P" "$T/same.tsv"; then ok "and the ledger is unchanged"; else no "and the ledger is unchanged"; fi |
| 295 | cp "$P" "$T/sl.tmp" |
| 296 | "$T/chain" page "$T/sl.tmp" "$T/sl" >/dev/null 2>&1; eq "page whose OUT.tmp is the ledger refused" "$?" 1 |
| 297 | if cmp -s "$P" "$T/sl.tmp"; then ok "and that ledger is unchanged"; else no "and that ledger is unchanged"; fi |
| 298 | cp "$T/src/LONG.md" "$T/long.before" |
| 299 | "$T/chain" feed "$P" "$T/src/LONG.md" MSG hive-board "$T/src/LONG.md" >/dev/null 2>&1; eq "feed with the fed file as the page refused" "$?" 1 |
| 300 | if cmp -s "$P" "$T/page.before" && cmp -s "$T/src/LONG.md" "$T/long.before"; then ok "and ledger and file are unchanged"; else no "and ledger and file are unchanged"; fi |
| 301 | printf 'victim\n' >"$T/victim"; ln -s "$T/victim" "$T/lk.html.tmp" |
| 302 | "$T/chain" page "$P" "$T/lk.html" >/dev/null 2>&1; eq "page over a stale OUT.tmp link exit" "$?" 0 |
| 303 | eq "it writes the page, not through the link" "$(cat "$T/victim")/$(cmp -s "$T/lk.html" "$T/page.html" && echo same)" "victim/same" |
| 304 | mkdir "$T/dir.html" |
| 305 | "$T/chain" page "$P" "$T/dir.html" >/dev/null 2>&1; eq "page onto a directory fails" "$?" 3 |
| 306 | eq "and leaves no OUT.tmp" "$(ls "$T" | grep -c '^dir\.html\.tmp$')" 0 |
| 307 | # negative control: without the name guard the page replaces its own ledger |
| 308 | awk '{ if ($0 == "if lt lstat go 0 n s 0 128 0") print "if true"; else print }' "$C/chain.om" >"$T/gm.om" |
| 309 | if cmp -s "$C/chain.om" "$T/gm.om"; then |
| 310 | no "guard mutant differs from source" |
| 311 | else |
| 312 | build "$T/gm.om" "$T/gm" |
| 313 | cp "$P" "$T/gm.tsv" |
| 314 | "$T/gm" page "$T/gm.tsv" "$T/gm.tsv" >/dev/null 2>&1 |
| 315 | eq "mutant without the name guard replaces the ledger with the page" "$(grep -c '<!doctype html>' "$T/gm.tsv")" 1 |
| 316 | fi |
| 317 | # negative control: the page is written although the ledger does not verify |
| 318 | awk '/^if ne get gs 1 0$/ { c++ } { l[NR] = $0 } END { for (i = 1; i <= NR; i++) { if (l[i] == "if ne get gs 1 0") { k++; if (k == c) { print "if false"; continue } } print l[i] } }' "$C/chain.om" >"$T/pm.om" |
| 319 | if cmp -s "$C/chain.om" "$T/pm.om"; then |
| 320 | no "page mutant differs from source" |
| 321 | else |
| 322 | build "$T/pm.om" "$T/pm" |
| 323 | "$T/pm" page "$T/d1" "$T/pm.html" >/dev/null 2>&1 |
| 324 | eq "mutant without the verify rule writes a page for a damaged ledger" "$(ls "$T" | grep -c '^pm\.html$')" 1 |
| 325 | fi |
| 326 | LEAN=${LEAN:-$HOME/.elan/bin/lean} |
| 327 | if [ -x "$LEAN" ]; then |
| 328 | eq "Lean checks tamper_evident, no axioms" "$("$LEAN" "$C/Ledger.lean" 2>&1 | grep -c 'does not depend on any axioms')" 1 |
| 329 | else |
| 330 | echo "NOT_RUN Lean check: no lean at $LEAN" |
| 331 | fi |
| 332 | |
| 333 | echo "RESULT pass=$pass fail=$fail" |
| 334 | [ "$fail" -eq 0 ] |