interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / cbor / test / run.sh

177 lines · 7483 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# CBOR test suite. Usage: sh om/cbor/test/run.sh OMC
4# Builds blake2s.om + cbor.om + test/driver.om with the pinned omc into build/test/cbor/, then checks:
5# rfc8949-appA RFC 8949 Appendix A: accept/reject code, and canonical re-encoding of accepted rows
6# tka-structs 1200 tka AUM / NodeKeySignature / State encodings: decode, re-encode byte for byte,
7# BLAKE2s-256 = tka AUM.Hash, key-1 lookup
8# hostile hostile corpus: error code = fxamacker Wellformed, per-class duties (see README)
9# fuzz 2000 mutations: error code = fxamacker Wellformed
10# utf8 2000 text strings: cbrutf8 = Go utf8.Valid
11# A driver trap (exit >= 100) or a short output fails the check. Then every mutant must be detected:
12# its driver runs must all complete normally and at least one case must differ.
13# With CBOR_GO set to a Go toolchain >= 1.26.5, the oracle vectors are regenerated and must equal the
14# committed ones (else that step prints NOT_RUN). Exit 0 only when everything passes.
15set -eu
16omc=${1:?usage: run.sh OMC}
17here=$(cd "$(dirname "$0")" && pwd)
18unit=$(dirname "$here")
19root=$(cd "$unit/../.." && pwd)
20out="$root/build/test/cbor"
21vec="$here/vectors"
22rm -rf "$out"
23mkdir -p "$out"
24fail=0
25
26build() { # build CBOR_SOURCE OUTPUT
27 cat "$root/om/blake2s/blake2s.om" "$1" "$here/driver.om" > "$2.om"
28 "$omc" < "$2.om" > "$2" 2> "$2.err" || {
29 echo "cbor: omc failed on $1: $(cat "$2.err")"
30 return 1
31 }
32 chmod +x "$2"
33}
34
35# inputs: one hex column of each vector file, comments dropped
36awk '!/^#/ { print $2 }' "$vec/rfc8949-appA.txt" > "$out/appA.in"
37awk '!/^#/ { print $2 }' "$vec/tka-structs.txt" > "$out/tka.in"
38awk '!/^#/ { print $6 }' "$vec/hostile.txt" > "$out/hostile.in"
39awk '!/^#/ { print $4 }' "$vec/fuzz.txt" > "$out/fuzz.in"
40awk '!/^#/ { print $2 }' "$vec/utf8.txt" > "$out/utf8.in"
41
42# judge NAME VECTORFILE GOT: compare the driver output GOT with the vector file, line by line.
43# Prints "PASS/FAIL count" lines. Returns 0 when every case matches, 1 when the output is complete
44# and at least one case differs, 2 when the output does not have one line per case.
45judge() {
46 awk -v name="$1" '
47 FNR == NR { if ($0 !~ /^#/) v[++nv] = $0; next }
48 { g[++ng] = $0 }
49 function want(wf) { return (wf == 0) ? "ok" : "e" wf }
50 END {
51 bad = 0
52 if (ng != nv) { printf "cbor: %s FAIL (%d output lines for %d cases)\n", name, ng, nv; exit 2 }
53 for (i = 1; i <= nv; i++) {
54 split(v[i], a, " "); split(g[i], b, " "); ok = 1
55 if (name == "rfc8949-appA") {
56 ok = (b[1] == want(a[1])) && (a[1] != 0 || b[6] == a[2])
57 } else if (name == "tka-structs") {
58 ok = b[1] == "ok" && b[3] == 0 && b[4] == 1 && b[5] == a[4] && b[6] == a[2] && b[7] == a[3]
59 n[a[1]]++; if (ok) p[a[1]]++
60 } else if (name == "hostile") {
61 c = a[1]; ok = b[1] == want(a[3])
62 if (c == "D") ok = ok && b[3] == 1
63 if (c == "U") ok = ok && b[4] == 0
64 if (c == "R") ok = ok && b[6] == a[5]
65 n[c]++; if (ok) p[c]++
66 } else if (name == "fuzz") {
67 ok = b[1] == want(a[1])
68 if (a[2] == 1 || a[3] == 1) ok = ok && b[1] == "ok"
69 } else if (name == "utf8") {
70 ok = b[1] == "ok" && b[4] == a[1]
71 }
72 if (!ok) { bad++; if (bad <= 3) printf "cbor: %s MISMATCH case %d: want [%s] got [%s]\n", name, i, substr(v[i], 1, 160), substr(g[i], 1, 160) }
73 }
74 detail = ""
75 for (k in n) detail = detail sprintf(" %s %d/%d", k, p[k], n[k])
76 printf "cbor: %s %d/%d %s%s\n", name, nv - bad, nv, bad ? "FAIL" : "PASS", detail
77 exit bad != 0
78 }
79 ' "$2" "$3"
80}
81
82# suite BINARY TAG: run every vector file through BINARY. Sets srun=1 when any run was abnormal
83# (driver exit status not 0: trap 100+N, signal 128+N, launch failure 126/127, malformed-line 2; or
84# output without one line per case) and smis=1 when a complete run had a case that differs.
85# Returns 0 only when every run was normal and every case matched.
86suite() {
87 bin=$1
88 tag=$2
89 srun=0
90 smis=0
91 for c in "rfc8949-appA appA" "tka-structs tka" "hostile hostile" "fuzz fuzz" "utf8 utf8"; do
92 name=${c% *}
93 f=${c#* }
94 st=0
95 "$bin" < "$out/$f.in" > "$out/$f.$tag.got" 2> /dev/null || st=$?
96 if [ "$st" -ne 0 ]; then
97 echo "cbor: $name driver exit $st ABNORMAL FAIL"
98 srun=1
99 fi
100 jr=0
101 judge "$name" "$vec/$name.txt" "$out/$f.$tag.got" || jr=$?
102 if [ "$jr" -eq 2 ]; then
103 srun=1
104 elif [ "$jr" -ne 0 ] && [ "$st" -eq 0 ]; then
105 smis=1
106 fi
107 done
108 [ "$srun" -eq 0 ] && [ "$smis" -eq 0 ]
109}
110
111drv="$out/driver"
112build "$unit/cbor.om" "$drv"
113echo "cbor: omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)"
114echo "cbor: cbor.om sha256 $(sha256sum "$unit/cbor.om" | cut -d' ' -f1)"
115echo "cbor: driver sha256 $(sha256sum "$drv" | cut -d' ' -f1)"
116suite "$drv" main || fail=1
117
118# oracle reproducibility (needs Go >= 1.26.5 and the module proxy or a warm module cache)
119if [ -n "${CBOR_GO:-}" ]; then
120 mkdir -p "$out/regen"
121 if sh "$here/gen/regen.sh" "$CBOR_GO" "$out/regen" > "$out/regen.log" 2>&1; then
122 same=1
123 for f in tka-structs hostile fuzz utf8; do
124 grep -v '^#' "$vec/$f.txt" > "$out/regen/$f.want"
125 grep -v '^#' "$out/regen/$f.txt" | cmp -s - "$out/regen/$f.want" || { same=0; echo "cbor: regen $f differs FAIL"; }
126 done
127 [ "$same" -eq 1 ] && echo "cbor: oracle regeneration identical PASS" || fail=1
128 else
129 echo "cbor: oracle regeneration FAIL: $(tail -n 3 "$out/regen.log")"
130 fail=1
131 fi
132else
133 echo "cbor: NOTE CBOR_GO not set, oracle regeneration NOT_RUN (committed vectors checked above)"
134fi
135
136# expectation mutant: one hash digit of the first tka structure flipped must be reported
137awk 'BEGIN { done = 0 } !done && !/^#/ { c = substr($3, 1, 1); $3 = ((c == "0") ? "1" : "0") substr($3, 2); done = 1 } { print }' \
138 "$vec/tka-structs.txt" > "$out/mut-expect.txt"
139if judge tka-structs "$out/mut-expect.txt" "$out/tka.main.got" > /dev/null; then
140 echo "cbor: mutant expected-hash NOT detected FAIL"
141 fail=1
142else
143 echo "cbor: mutant expected-hash detected PASS"
144fi
145
146# source mutants: each replaces every line equal to FROM (at least one) and must fail the suite
147mutant() { # mutant NAME FROM TO
148 m="$out/mut-$1"
149 awk -v from="$2" -v to="$3" '$0 == from { $0 = to; hit++ } { print } END { exit hit < 1 }' \
150 "$unit/cbor.om" > "$m.cbor.om" || { echo "cbor: mutant $1 did not apply FAIL"; fail=1; return 0; }
151 build "$m.cbor.om" "$m" || { fail=1; return 0; }
152 suite "$m" "$1" > "$m.log" 2>&1 || true
153 # detected only when every driver run completed normally and some case differs
154 if [ "$srun" -ne 0 ]; then
155 echo "cbor: mutant $1 run ABNORMAL (trap, signal or short output), not a detection FAIL"
156 fail=1
157 elif [ "$smis" -eq 0 ]; then
158 echo "cbor: mutant $1 NOT detected FAIL"
159 fail=1
160 else
161 echo "cbor: mutant $1 detected PASS ($(grep -c 'FAIL' "$m.log") failing checks)"
162 fi
163}
164mutant depth-17 "if ge dep 16" "if ge dep 17"
165mutant array-4097 "if and eq m 4 gt v 4096" "if and eq m 4 gt v 4097"
166mutant map-1023 "if and eq m 5 gt v 1024" "if and eq m 5 gt v 1023"
167mutant tags-allowed "if eq m 6" "if eq m 8"
168mutant extra-bytes "if ne p n" "if lt n p"
169mutant simple-lt32 "if and eq m 7 and eq a 24 lt v 32" "if and eq m 7 and eq a 24 lt v 31"
170mutant len-overflow "if gt v sub n q" "if gt v n"
171mutant shortest-head "if lt v 24" "if lt v 23"
172mutant sort-reversed "ret gt x y" "ret lt x y"
173mutant sort-off "if and eq cbrmaj t i 5 gt cbrval t i 1" "if and eq cbrmaj t i 5 gt cbrval t i 1024"
174mutant int64-keys "ret not cbrval t i" "ret cbrval t i"
175mutant utf8-surrogates "ret 159" "ret 191"
176
177exit "$fail"