kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

run.sh (8174B)


      1 #!/bin/sh
      2 # Driver-level checks for the byte-utility tools: xxd, cmp, hash, disas, mc,
      3 # compress.
      4 # Self-checking (no golden files): round-trips and known encodings/digests are
      5 # asserted inline via the shared kit_* verbs (ok/run_ok/run_fail/contains/
      6 # same_file) recorded over $work.
      7 
      8 set -u
      9 
     10 script_dir=$(cd "$(dirname "$0")" && pwd)
     11 repo_root=$(cd "$script_dir/../.." && pwd)
     12 
     13 KIT="${KIT:-$repo_root/build/kit}"
     14 
     15 if [ ! -x "$KIT" ]; then
     16     echo "tools: kit binary not found at $KIT" >&2
     17     exit 2
     18 fi
     19 
     20 work=$(mktemp -d "${TMPDIR:-/tmp}/kit-tools-test.XXXXXX")
     21 trap 'rm -rf "$work"' EXIT
     22 
     23 KIT_KIT_DIR="$repo_root/test/lib"
     24 . "$repo_root/test/lib/kit_sh_kit.sh"
     25 kit_report_init
     26 
     27 # A fixture with text, newlines, NUL and high bytes — exercises the ASCII
     28 # column and binary round-tripping.
     29 printf 'Hello, kit!\nLine two.\n\000\001\377\376' > "$work/data.bin"
     30 
     31 # ---- xxd -------------------------------------------------------------------
     32 "$KIT" xxd "$work/data.bin" > "$work/dump.txt" 2> "$work/xxd.err"
     33 "$KIT" xxd -r "$work/dump.txt" > "$work/rt.bin" 2>> "$work/xxd.err"
     34 same_file xxd-roundtrip "$work/data.bin" "$work/rt.bin"
     35 
     36 "$KIT" xxd -p "$work/data.bin" > "$work/p.txt" 2>> "$work/xxd.err"
     37 "$KIT" xxd -r -p "$work/p.txt" > "$work/rtp.bin" 2>> "$work/xxd.err"
     38 same_file xxd-plain-roundtrip "$work/data.bin" "$work/rtp.bin"
     39 
     40 "$KIT" xxd -i "$work/data.bin" > "$work/inc.txt" 2>> "$work/xxd.err"
     41 contains xxd-include-array "$work/inc.txt" "unsigned char"
     42 contains xxd-include-len "$work/inc.txt" "_len = 26"
     43 
     44 # ---- cmp -------------------------------------------------------------------
     45 cp "$work/data.bin" "$work/data2.bin"
     46 run_ok cmp-identical "$KIT" cmp "$work/data.bin" "$work/data2.bin"
     47 
     48 printf 'Hello, kit!\nLine TWO.\n\000\001\377\376' > "$work/diff.bin"
     49 run_fail cmp-differ-exit "$KIT" cmp "$work/data.bin" "$work/diff.bin"
     50 "$KIT" cmp "$work/data.bin" "$work/diff.bin" > "$work/cmp.out" 2>&1
     51 contains cmp-differ-message "$work/cmp.out" "differ: char"
     52 run_fail cmp-silent-exit "$KIT" cmp -s "$work/data.bin" "$work/diff.bin"
     53 
     54 # ---- hash (CLI smoke; vectors covered by the test-hash unit test) ----------
     55 printf '123456789' > "$work/crc.in"
     56 "$KIT" hash -a crc32 "$work/crc.in" > "$work/crc.out" 2>&1
     57 contains hash-crc32 "$work/crc.out" "cbf43926"
     58 printf 'abc' | "$KIT" hash -a sha256 > "$work/sha.out" 2>&1
     59 contains hash-sha256 "$work/sha.out" \
     60     "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
     61 
     62 # Standard-named aliases pin the algorithm and match `hash -a` exactly.
     63 printf 'abc' | "$KIT" sha256sum > "$work/sha256sum.out" 2>&1
     64 contains alias-sha256sum "$work/sha256sum.out" \
     65     "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad  -"
     66 printf 'abc' | "$KIT" b2sum > "$work/b2sum.out" 2>&1
     67 printf 'abc' | "$KIT" hash -a blake2b > "$work/b2ref.out" 2>&1
     68 same_file alias-b2sum-matches-hash "$work/b2sum.out" "$work/b2ref.out"
     69 "$KIT" crc32 "$work/crc.in" > "$work/crc32.out" 2>&1
     70 contains alias-crc32 "$work/crc32.out" "cbf43926"
     71 # A pinned alias rejects -a (its algorithm is fixed).
     72 run_fail alias-sha256sum-no-a "$KIT" sha256sum -a blake2b "$work/crc.in"
     73 
     74 # ---- disas -----------------------------------------------------------------
     75 "$KIT" disas -target aarch64 -x "1f 20 03 d5" > "$work/d-aa64.out" 2>&1
     76 contains disas-aa64-nop "$work/d-aa64.out" "nop"
     77 "$KIT" disas -target x86_64 -x "c3" > "$work/d-x64.out" 2>&1
     78 contains disas-x64-ret "$work/d-x64.out" "ret"
     79 run_fail disas-bad-hex "$KIT" disas -target aarch64 -x "zz"
     80 
     81 # ---- mc --------------------------------------------------------------------
     82 "$KIT" mc -target aarch64 "add x0, x1, x2" > "$work/mc.out" 2>&1
     83 contains mc-aa64-encoding "$work/mc.out" "encoding: [0x20,0x00,0x02,0x8b]"
     84 
     85 # mc -p (raw hex) piped back through disas must recover the mnemonic.
     86 "$KIT" mc -target aarch64 -p "add x0, x1, x2" > "$work/mc.hex" 2>&1
     87 "$KIT" disas -target aarch64 -x "$(cat "$work/mc.hex")" > "$work/mc-rt.out" 2>&1
     88 contains mc-disas-roundtrip "$work/mc-rt.out" "add x0, x1, x2"
     89 
     90 # A branch to an undefined symbol should surface a relocation, not an error.
     91 "$KIT" mc -target aarch64 "b somewhere" > "$work/mc-rel.out" 2>&1
     92 contains mc-reloc "$work/mc-rel.out" "reloc"
     93 
     94 # ---- compress --------------------------------------------------------------
     95 # gzip + LZ4-frame round-trips (text, empty, binary), -o files, auto-detect,
     96 # error exits, and interop with the system gzip/lz4 CLIs when present.
     97 printf 'hello compression world\nthe quick brown fox jumps\n' > "$work/cz.txt"
     98 : > "$work/cz.empty"
     99 head -c 200000 "$KIT" > "$work/cz.bin" # a real, varied, deterministic blob
    100 
    101 cz_roundtrip() { # FMT LABEL FILE : compress | decompress | compare original
    102     "$KIT" compress -z "$1" "$3" 2> "$work/cz.err" \
    103         | "$KIT" compress -d 2>> "$work/cz.err" > "$work/cz.rt"
    104     same_file "compress-roundtrip-$2" "$3" "$work/cz.rt"
    105 }
    106 for f in txt empty bin; do
    107     cz_roundtrip gzip "gzip-$f" "$work/cz.$f"
    108     cz_roundtrip lz4 "lz4-$f" "$work/cz.$f"
    109 done
    110 
    111 # File input + -o output, then decode.
    112 "$KIT" compress -z gzip -o "$work/cz.gz" "$work/cz.txt"
    113 "$KIT" compress -d -o "$work/cz.ungz" "$work/cz.gz"
    114 same_file compress-file-out-gzip "$work/cz.txt" "$work/cz.ungz"
    115 
    116 # Auto-detect on decompress (no -z) from the container magic bytes.
    117 for fmt in gzip lz4; do
    118     "$KIT" compress -z "$fmt" "$work/cz.txt" | "$KIT" compress -d > "$work/cz.det"
    119     same_file "compress-autodetect-$fmt" "$work/cz.txt" "$work/cz.det"
    120 done
    121 
    122 # Default format is gzip.
    123 "$KIT" compress "$work/cz.txt" | "$KIT" compress -d > "$work/cz.def"
    124 same_file compress-default-gzip "$work/cz.txt" "$work/cz.def"
    125 
    126 # Standard-named aliases: gzip/gunzip and lz4 round-trip through each other and
    127 # through the generic tool; gunzip is just `compress -d` pinned to gzip.
    128 "$KIT" gzip "$work/cz.txt" | "$KIT" gunzip > "$work/cz.gzalias"
    129 same_file alias-gzip-gunzip "$work/cz.txt" "$work/cz.gzalias"
    130 "$KIT" gzip "$work/cz.txt" | "$KIT" compress -d > "$work/cz.gz2generic"
    131 same_file alias-gzip-to-generic "$work/cz.txt" "$work/cz.gz2generic"
    132 "$KIT" lz4 "$work/cz.txt" | "$KIT" lz4 -d > "$work/cz.lz4alias"
    133 same_file alias-lz4-roundtrip "$work/cz.txt" "$work/cz.lz4alias"
    134 "$KIT" lz4c "$work/cz.txt" | "$KIT" lz4 -d > "$work/cz.lz4calias"
    135 same_file alias-lz4c-roundtrip "$work/cz.txt" "$work/cz.lz4calias"
    136 # gzip -d flips direction; the common gzip flags (-c, level -9) are no-ops.
    137 "$KIT" gzip -9 -c "$work/cz.txt" | "$KIT" gzip -d > "$work/cz.compat"
    138 same_file alias-gzip-compat-flags "$work/cz.txt" "$work/cz.compat"
    139 # A pinned alias rejects -z, and gunzip will not auto-detect a foreign container.
    140 run_fail alias-gzip-no-z "$KIT" gzip -z lz4 "$work/cz.txt"
    141 "$KIT" lz4 "$work/cz.txt" > "$work/cz.lz4only"
    142 run_fail alias-gunzip-wrong-format "$KIT" gunzip "$work/cz.lz4only"
    143 
    144 # Error exits: bad format / two inputs / missing file all exit non-zero.
    145 run_fail compress-err-bad-format "$KIT" compress -z bogus "$work/cz.txt"
    146 run_fail compress-err-two-inputs "$KIT" compress "$work/cz.txt" "$work/cz.empty"
    147 run_fail compress-err-missing-file "$KIT" compress "$work/cz.nope"
    148 if printf 'plain text not compressed' | "$KIT" compress -d >/dev/null 2>&1; then
    149     not_ok compress-err-undetectable
    150 else
    151     ok compress-err-undetectable
    152 fi
    153 
    154 # Interop with the system tools, both directions; skip cleanly when absent.
    155 if command -v gzip >/dev/null 2>&1 && command -v gunzip >/dev/null 2>&1; then
    156     "$KIT" compress -z gzip "$work/cz.bin" | gunzip 2>/dev/null > "$work/cz.gunzip"
    157     same_file compress-interop-kit-to-gunzip "$work/cz.bin" "$work/cz.gunzip"
    158     gzip -c "$work/cz.bin" | "$KIT" compress -d > "$work/cz.fromgzip"
    159     same_file compress-interop-gzip-to-kit "$work/cz.bin" "$work/cz.fromgzip"
    160 else
    161     kit_skip_na compress-interop-gzip "system gzip/gunzip not found"
    162 fi
    163 if command -v lz4 >/dev/null 2>&1; then
    164     "$KIT" compress -z lz4 "$work/cz.bin" | lz4 -d 2>/dev/null > "$work/cz.unlz4"
    165     same_file compress-interop-kit-to-lz4 "$work/cz.bin" "$work/cz.unlz4"
    166     lz4 -c "$work/cz.bin" 2>/dev/null | "$KIT" compress -d > "$work/cz.fromlz4"
    167     same_file compress-interop-lz4-to-kit "$work/cz.bin" "$work/cz.fromlz4"
    168 else
    169     kit_skip_na compress-interop-lz4 "system lz4 not found"
    170 fi
    171 
    172 kit_summary tools-driver
    173 kit_exit