interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / codec / test / run.sh

171 lines · 6574 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# om/codec test suite. Usage: sh om/codec/test/run.sh OMC
4# Builds codec.om + test/driver.om with the pinned omc into build/test/codec/, then runs:
5# - the RFC 4648 section 10 vectors (encode and decode, base64, base32 without padding, base16);
6# - the committed malformed-input corpus with Go go1.26.0's results (vectors/malformed.txt);
7# - when a Go toolchain can run go1.26.0, the seeded differential corpus of gen/oracle;
8# - six over-capacity calls, each of which must trap 3 (exit 103);
9# - one expectation mutant and nine library mutants, each of which must be detected.
10# Exit 0 only when every check passes. CODEC_SEED sets the differential seed (default 4648);
11# GO names the go command (default go).
12set -eu
13omc=${1:?usage: run.sh OMC}
14here=$(cd "$(dirname "$0")" && pwd)
15unit=$(dirname "$here")
16root=$(cd "$unit/../.." && pwd)
17out="$root/build/test/codec"
18vec="$here/vectors"
19seed=${CODEC_SEED:-4648}
20go=${GO:-go}
21rm -rf "$out"
22mkdir -p "$out"
23fail=0
24
25build() { # build LIBRARY OUTPUT
26 cat "$1" "$here/driver.om" | "$omc" > "$2" 2> "$2.err" || {
27 echo "codec: omc failed on $1: $(cat "$2.err")"
28 return 1
29 }
30 chmod +x "$2"
31}
32
33# runcases BINARY INPUT OUTPUT: feed INPUT in chunks of 1000 lines (the driver holds 16 MiB).
34# Returns 1 when any chunk exits non-zero (a trap exits 100 + code, a signal 128 + number, a launch
35# failure 126 or 127); rc then holds that exit status.
36runcases() {
37 rc=0
38 rm -f "$out"/chunk.*
39 split -l 1000 "$2" "$out/chunk."
40 : > "$3"
41 for c in "$out"/chunk.*; do
42 [ -f "$c" ] || continue
43 "$1" < "$c" >> "$3" 2> "$3.err" || { rc=$?; return 1; }
44 done
45}
46
47# detects NAME BINARY EXPECTED: a mutant is detected only when BINARY ran every static case to
48# completion (every chunk exit 0, one result line per case) and at least one result differs from
49# EXPECTED. A trap, signal, launch failure or short output fails the suite instead.
50detects() {
51 if ! runcases "$2" "$out/all.in" "$out/$1.got"; then
52 echo "codec: mutant $1 did not run to completion (exit $rc) FAIL"
53 fail=1
54 return 0
55 fi
56 want=$(wc -l < "$3" | tr -d ' ')
57 got=$(wc -l < "$out/$1.got" | tr -d ' ')
58 if [ "$got" -ne "$want" ]; then
59 echo "codec: mutant $1 gave $got of $want results FAIL"
60 fail=1
61 elif cmp -s "$out/$1.got" "$3"; then
62 echo "codec: mutant $1 NOT detected FAIL"
63 fail=1
64 else
65 d=$(paste -d '|' "$out/$1.got" "$3" | awk -F'|' '$1 != $2' | wc -l | tr -d ' ')
66 echo "codec: mutant $1 detected PASS ($d of $want results differ)"
67 fi
68}
69
70# check NAME INPUT EXPECTED: the driver's output must equal EXPECTED byte for byte
71check() {
72 n=$(wc -l < "$3" | tr -d ' ')
73 if runcases "$drv" "$2" "$out/$1.got" && cmp -s "$out/$1.got" "$3"; then
74 echo "codec: $1 $n/$n PASS ($(grep -c '^ok' "$3") ok, $(grep -c '^err' "$3") err)"
75 else
76 echo "codec: $1 FAIL ($(cmp "$out/$1.got" "$3" 2>&1 || true))"
77 fail=1
78 fi
79}
80
81drv="$out/driver"
82build "$unit/codec.om" "$drv"
83echo "codec: omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)"
84echo "codec: codec.om sha256 $(sha256sum "$unit/codec.om" | cut -d' ' -f1)"
85echo "codec: driver sha256 $(sha256sum "$drv" | cut -d' ' -f1)"
86
87# RFC 4648 section 10: NAME("input") = "output"
88awk -F'"' '
89 BEGIN { for (i = 32; i < 127; i++) ord[sprintf("%c", i)] = i }
90 function hex(s, r, i) {
91 if (s == "") return "-"
92 r = ""
93 for (i = 1; i <= length(s); i++) r = r sprintf("%02x", ord[substr(s, i, 1)])
94 return r
95 }
96 function ok(s) { return "ok " ((s == "") ? "-" : s) }
97 /^BASE64\(/ {
98 print "b64e", hex($2) > in_; print ok($4) > out_
99 print "b64d", hex($4) > in_; print ok(hex($2)) > out_
100 }
101 /^BASE32\(/ {
102 s = $4; gsub(/=/, "", s)
103 print "b32e", hex($2) > in_; print ok(s) > out_
104 print "b32d", hex(s) > in_; print ok(hex($2)) > out_
105 if (s != $4) { print "b32d", hex($4) > in_; print "err" > out_ }
106 }
107 /^BASE16\(/ {
108 print "hexe", hex($2) > in_; print ok(tolower($4)) > out_
109 print "hexd", hex($4) > in_; print ok(hex($2)) > out_
110 }
111' in_="$out/rfc.in" out_="$out/rfc.out" "$vec/rfc4648.txt"
112[ "$(wc -l < "$out/rfc.out")" -eq 47 ] || { echo "codec: rfc4648 parse FAIL"; fail=1; }
113check rfc4648 "$out/rfc.in" "$out/rfc.out"
114
115# committed malformed-input corpus: OP ARG RESULT...
116awk '/^#/ { next } { print $1, $2 > in_; $1 = ""; $2 = ""; sub(/^ /, ""); print > out_ }' \
117 in_="$out/mal.in" out_="$out/mal.out" "$vec/malformed.txt"
118check malformed "$out/mal.in" "$out/mal.out"
119
120# differential corpus against the go1.26.0 oracle
121if command -v "$go" > /dev/null 2>&1 &&
122 (cd "$here/gen/oracle" && GOTOOLCHAIN=go1.26.0 "$go" build -o "$out/oracle" .) > "$out/oracle.log" 2>&1; then
123 echo "codec: oracle $(GOTOOLCHAIN=go1.26.0 "$go" version), seed $seed"
124 "$out/oracle" rand "$seed" > "$out/diff.in"
125 "$out/oracle" < "$out/diff.in" > "$out/diff.out"
126 check differential "$out/diff.in" "$out/diff.out"
127else
128 echo "codec: NOTE go1.26.0 oracle unavailable ($go), differential corpus NOT_RUN"
129fi
130
131# over-capacity calls trap 3: program exit 103
132for t in 1 2 3 4 5 6; do
133 set +e
134 "$drv" "$t" > /dev/null 2>&1
135 rc=$?
136 set -e
137 if [ "$rc" -eq 103 ]; then
138 echo "codec: over-capacity call $t traps 3 PASS"
139 else
140 echo "codec: over-capacity call $t exit $rc FAIL"
141 fail=1
142 fi
143done
144
145# every static case in one file, for the mutants
146cat "$out/rfc.in" "$out/mal.in" > "$out/all.in"
147cat "$out/rfc.out" "$out/mal.out" > "$out/all.out"
148
149# expectation mutant: one flipped expected result must be reported by the unmodified driver
150awk 'NR == 1 { $0 = "err" } { print }' "$out/all.out" > "$out/mut-expect.out"
151detects expected-result "$drv" "$out/mut-expect.out"
152
153# library mutants: each must build, run every static case, and disagree with at least one
154mutant() { # mutant NAME FROM TO
155 m="$out/mut-$1"
156 awk -v from="$2" -v to="$3" '$0 == from { $0 = to; hit++ } { print } END { exit hit != 1 }' \
157 "$unit/codec.om" > "$m.om" || { echo "codec: mutant $1 did not apply FAIL"; fail=1; return 0; }
158 build "$m.om" "$m" || { fail=1; return 0; }
159 detects "$1" "$m" "$out/all.out"
160}
161mutant b64-lower-value "ret sub c 71" "ret sub c 70"
162mutant b64-no-crlf-skip "if or eq c 10 eq c 13" "if eq c 13"
163mutant b64-pad-after-1 "if eq j 3" "if eq j 1"
164mutant b32-digit-char "ret uchar add v 24" "ret uchar add v 23"
165mutant b32-pad-byte-eq "if and eq c 255 and ge j 2 lt sub l p 8" "if and eq c 61 and ge j 2 lt sub l p 8"
166mutant b32-pad-dlen6 "if or eq j 3 eq j 6" "if eq j 3"
167mutant hex-upper-value "ret sub c 55" "ret sub c 54"
168mutant aum-length "if ne n 52" "if gt n 52"
169mutant nodekey-prefix "const cdnkpfx ulong 7957689453343242554" "const cdnkpfx ulong 7957689453343242555"
170
171exit "$fail"