test_generate.sh (4726B)
1 #!/bin/sh 2 set -eu 3 HERE=$(cd "$(dirname "$0")" && pwd) 4 ROOT=$(cd "${HERE}/.." && pwd) 5 . "${HERE}/_tap.sh" 6 7 BIN=$(ensure_bin "${ROOT}") 8 TMP=$(mktemp -d) 9 trap 'rm -rf "${TMP}"' EXIT 10 11 mode() { 12 stat -c '%a' "$1" 2>/dev/null || stat -f '%Lp' "$1" 13 } 14 15 bytes() { 16 wc -c <"$1" | tr -d '[:space:]' 17 } 18 19 tap_begin "cli: generate" 20 21 "${BIN}" generate -o "${TMP}/a.hdr" 22 grep -q '^seed: ' "${TMP}/a.hdr" && grep -q '^public-key: ' "${TMP}/a.hdr" && ok=1 || ok=0 23 tap "${ok}" "hdr writes one combined file" "$(cat "${TMP}/a.hdr")" 24 tap "$([ ! -e "${TMP}/a.hdr.pub" ] && echo 1 || echo 0)" "hdr creates no pub file" "a.hdr.pub exists" 25 tap "$([ "$(mode "${TMP}/a.hdr")" = 600 ] && echo 1 || echo 0)" "hdr key file is 0600" "got $(mode "${TMP}/a.hdr")" 26 27 "${BIN}" generate >"${TMP}/stdout.hdr" 28 grep -q '^seed: ' "${TMP}/stdout.hdr" && grep -q '^public-key: ' "${TMP}/stdout.hdr" && ok=1 || ok=0 29 tap "${ok}" "hdr to stdout is the combined file" "$(cat "${TMP}/stdout.hdr")" 30 31 "${BIN}" generate --out-fmt raw -o "${TMP}/b.key" 32 tap "$([ "$(bytes "${TMP}/b.key")" = 32 ] && echo 1 || echo 0)" "raw key holds a 32-byte seed" "got $(bytes "${TMP}/b.key")" 33 tap "$([ "$(bytes "${TMP}/b.pub")" = 32 ] && echo 1 || echo 0)" "raw pub holds a 32-byte public key" "got $(bytes "${TMP}/b.pub")" 34 tap "$([ ! -e "${TMP}/b.key.pub" ] && echo 1 || echo 0)" "the .key suffix is replaced, not appended to" "b.key.pub exists" 35 tap "$([ "$(mode "${TMP}/b.key")" = 600 ] && [ "$(mode "${TMP}/b.pub")" = 644 ] && echo 1 || echo 0)" \ 36 "raw pair is 0600 and 0644" "$(mode "${TMP}/b.key") $(mode "${TMP}/b.pub")" 37 38 "${BIN}" generate --out-fmt raw -o "${TMP}/c.KEY" 39 tap "$([ -e "${TMP}/c.PUB" ] && echo 1 || echo 0)" ".KEY becomes .PUB" "$(ls -1 "${TMP}")" 40 "${BIN}" generate --out-fmt raw -o "${TMP}/d.Key" 41 tap "$([ -e "${TMP}/d.Pub" ] && echo 1 || echo 0)" ".Key becomes .Pub" "$(ls -1 "${TMP}")" 42 "${BIN}" generate --out-fmt hex -o "${TMP}/e.hex" 43 tap "$([ -e "${TMP}/e.hex.pub" ] && echo 1 || echo 0)" "a suffix that is not .key has .pub appended" "$(ls -1 "${TMP}")" 44 45 grep -qE '^[0-9a-f]{64}$' "${TMP}/e.hex" && ok=1 || ok=0 46 tap "${ok}" "hex key file is 64 hex characters" "$(cat "${TMP}/e.hex")" 47 grep -qE '^[0-9a-f]{64}$' "${TMP}/e.hex.pub" && ok=1 || ok=0 48 tap "${ok}" "hex pub file is 64 hex characters" "$(cat "${TMP}/e.hex.pub")" 49 50 "${BIN}" generate --out-fmt raw >"${TMP}/out.raw" 51 tap "$([ "$(bytes "${TMP}/out.raw")" = 32 ] && echo 1 || echo 0)" "raw stdout is seed only" "got $(bytes "${TMP}/out.raw")" 52 53 "${BIN}" generate --out-fmt hex >"${TMP}/out.hex" 54 tap "$([ "$(bytes "${TMP}/out.hex")" = 64 ] && echo 1 || echo 0)" "hex stdout is seed only" "got $(bytes "${TMP}/out.hex")" 55 56 "${BIN}" generate 16 --out-fmt raw -o "${TMP}/s.key" 57 tap "$([ "$(bytes "${TMP}/s.key")" = 16 ] && echo 1 || echo 0)" "a length argument sizes the seed" "got $(bytes "${TMP}/s.key")" 58 59 "${BIN}" generate -L 512 --out-fmt raw -o "${TMP}/h.key" 60 tap "$([ "$(bytes "${TMP}/h.pub")" = 64 ] && echo 1 || echo 0)" "--hash 512 gives a 64-byte pub" "got $(bytes "${TMP}/h.pub")" 61 62 "${BIN}" printkey --in-fmt raw -k "${TMP}/b.key" --public-only --out-fmt rawpub >"${TMP}/b.expect" 63 cmp -s "${TMP}/b.pub" "${TMP}/b.expect" && ok=1 || ok=0 64 tap "${ok}" "the pub matches printkey for the same seed" "differs" 65 66 rc=0 67 "${BIN}" generate -o "${TMP}/a.hdr" >/dev/null 2>&1 || rc=$? 68 tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "refuses an existing key file" "exit ${rc}" 69 70 rc=0 71 "${BIN}" generate -F -o "${TMP}/a.hdr" >/dev/null 2>&1 || rc=$? 72 tap "$([ "${rc}" = 0 ] && echo 1 || echo 0)" "--force overwrites" "exit ${rc}" 73 74 rm "${TMP}/s.key" 75 rc=0 76 "${BIN}" generate --out-fmt raw -o "${TMP}/s.key" >/dev/null 2>&1 || rc=$? 77 tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "refuses when only the pub is taken" "exit ${rc}" 78 tap "$([ ! -e "${TMP}/s.key" ] && echo 1 || echo 0)" "a refused pair writes no key file" "s.key exists" 79 80 rc=0 81 "${BIN}" generate --out-fmt rawpub -o "${TMP}/no.key" >/dev/null 2>&1 || rc=$? 82 tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects a pub-only output format" "exit ${rc}" 83 84 rc=0 85 "${BIN}" generate --out-fmt bogus >/dev/null 2>&1 || rc=$? 86 tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects an unknown format" "exit ${rc}" 87 88 rc=0 89 "${BIN}" generate 0 >/dev/null 2>&1 || rc=$? 90 tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects a zero length" "exit ${rc}" 91 92 rc=0 93 "${BIN}" generate abc >/dev/null 2>&1 || rc=$? 94 tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects a non-numeric length" "exit ${rc}" 95 96 "${BIN}" generate --out-fmt raw -p 2 -o "${TMP}/v.key" 97 "${BIN}" sign -k "${TMP}/v.key" -m hello -P 0011 -o "${TMP}/v.sig" 98 "${BIN}" verify -k "${TMP}/v.pub" -p 2 -m hello -S "${TMP}/v.sig" >/dev/null && ok=1 || ok=0 99 tap "${ok}" "sign with the key, verify with the pub" "failed" 100 101 tap_plan