| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | vectors="$vectorDir" |
| 5 | |
| 6 | # One vector written out and compared, quietly, so the |
| 7 | # perturbed run at the end can expect it to fail without |
| 8 | # saying so. |
| 9 | writes() { attest render "$1/input.json" | cmp -s - "$1/statement.txt"; } |
| 10 | |
| 11 | found=0 |
| 12 | signed=0 |
| 13 | for dir in "$vectors"/vectors/*/; do |
| 14 | name="$(basename "$dir")" |
| 15 | found=$((found + 1)) |
| 16 | |
| 17 | if ! writes "$dir"; then |
| 18 | echo "attest-conformance: $name is not written as its recorded bytes" >&2 |
| 19 | diff -u "$dir/statement.txt" <(attest render "$dir/input.json") >&2 || true |
| 20 | exit 1 |
| 21 | fi |
| 22 | |
| 23 | # The written form is a fixed point, or a statement written |
| 24 | # out again anywhere downstream stops matching what was |
| 25 | # signed. |
| 26 | if ! attest render "$dir/statement.txt" | cmp -s - "$dir/statement.txt"; then |
| 27 | echo "attest-conformance: $name is not a fixed point of the written form" >&2 |
| 28 | exit 1 |
| 29 | fi |
| 30 | |
| 31 | # Every vector is a whole statement — order is defined per |
| 32 | # predicate type, so a fragment has no written form — and is |
| 33 | # held to the schema too, so the set cannot drift into |
| 34 | # documents no verifier would accept. |
| 35 | cue vet -c "$schema" "$dir/input.json" |
| 36 | |
| 37 | # The signed vector pins the whole path from a statement to |
| 38 | # a note something else can check, not just the written |
| 39 | # form. No --repo: the subject tree is not what this check |
| 40 | # is about. |
| 41 | if [ -e "$dir/statement.note" ]; then |
| 42 | signed=$((signed + 1)) |
| 43 | attest verify --note "$dir/statement.note" \ |
| 44 | --known-keys "$vectors/known_keys" > "$name.verify" |
| 45 | grep -q '^signature ok' "$name.verify" |
| 46 | grep -q '^text ok' "$name.verify" |
| 47 | |
| 48 | # The text a verifier reads back is the text on disk: a |
| 49 | # note is its statement, and reading one gives the other. |
| 50 | sed '/^$/q' "$dir/statement.note" | sed '$d' | cmp - "$dir/statement.txt" |
| 51 | fi |
| 52 | done |
| 53 | |
| 54 | # Two signers over one statement is the property the note |
| 55 | # format is here for, so the set must actually hold one. |
| 56 | siblings="$(grep -c '^— ' "$vectors"/vectors/04-two-signers/statement.note)" |
| 57 | if [ "$siblings" -ne 2 ]; then |
| 58 | echo "attest-conformance: the two-signer vector carries $siblings signatures" >&2 |
| 59 | exit 1 |
| 60 | fi |
| 61 | attest verify --note "$vectors"/vectors/04-two-signers/statement.note \ |
| 62 | --known-keys "$vectors/known_keys" \ |
| 63 | --signer conformance.the-valley.invalid/attestations \ |
| 64 | --signer witness.the-valley.invalid/attestations > siblings.verify |
| 65 | if [ "$(grep -c '^signature ok' siblings.verify)" -ne 2 ]; then |
| 66 | echo "attest-conformance: both sibling signatures must verify" >&2 |
| 67 | cat siblings.verify >&2 |
| 68 | exit 1 |
| 69 | fi |
| 70 | |
| 71 | if [ "$found" -lt 4 ] || [ "$signed" -lt 2 ]; then |
| 72 | echo "attest-conformance: $found vectors, $signed signed — the set has been emptied" >&2 |
| 73 | exit 1 |
| 74 | fi |
| 75 | |
| 76 | # The other half of the contract: what must not be written or |
| 77 | # read at all. A document with no written form, and text that |
| 78 | # is not the written form of anything. |
| 79 | refused=0 |
| 80 | for file in "$vectors"/refused/*; do |
| 81 | refused=$((refused + 1)) |
| 82 | if attest render "$file" > /dev/null 2> "$(basename "$file").err"; then |
| 83 | echo "attest-conformance: $(basename "$file") was accepted" >&2 |
| 84 | exit 1 |
| 85 | fi |
| 86 | done |
| 87 | if [ "$refused" -lt 30 ]; then |
| 88 | echo "attest-conformance: only $refused refusals — the set has been emptied" >&2 |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
| 92 | # The comparison must be able to fail. One recorded byte |
| 93 | # sequence with a single value changed is what a divergent |
| 94 | # implementation looks like, and it must not pass. |
| 95 | mkdir -p perturbed |
| 96 | cp "$vectors/vectors/01-statement-minimal/input.json" perturbed/ |
| 97 | sed 's/^predicate.result passed$/predicate.result failed/' \ |
| 98 | "$vectors/vectors/01-statement-minimal/statement.txt" > perturbed/statement.txt |
| 99 | cmp -s perturbed/statement.txt "$vectors/vectors/01-statement-minimal/statement.txt" \ |
| 100 | && { echo "attest-conformance: the perturbation changed nothing" >&2; exit 1; } |
| 101 | if writes perturbed; then |
| 102 | echo "attest-conformance: a perturbed vector passed; the comparison proves nothing" >&2 |
| 103 | exit 1 |
| 104 | fi |
| 105 | |
| 106 | # And so must the signature check. One byte of the signed |
| 107 | # text changed is a signature that no longer covers it. |
| 108 | sed 's/a sealed environment/a sealed environmenT/' \ |
| 109 | "$vectors"/vectors/04-two-signers/statement.note > perturbed/statement.note |
| 110 | if attest verify --note perturbed/statement.note \ |
| 111 | --known-keys "$vectors/known_keys" > perturbed.verify 2>&1; then |
| 112 | echo "attest-conformance: a note whose text was edited still verified" >&2 |
| 113 | exit 1 |
| 114 | fi |
| 115 | grep -q 'does not check out over this text' perturbed.verify |
| 116 | |
| 117 | echo "attest-conformance: $found vectors written byte for byte, $signed signed, $refused refused" |
| 118 | touch "$out" |