gunk-dev/the-valley

attest-conformance.sh

source6ee9fbfcf100main's published tip history
4.5 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4vectors="$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.
9writes() { attest render "$1/input.json" | cmp -s - "$1/statement.txt"; }
10
11found=0
12signed=0
13for 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
52done
53
54# Two signers over one statement is the property the note
55# format is here for, so the set must actually hold one.
56siblings="$(grep -c '^— ' "$vectors"/vectors/04-two-signers/statement.note)"
57if [ "$siblings" -ne 2 ]; then
58 echo "attest-conformance: the two-signer vector carries $siblings signatures" >&2
59 exit 1
60fi
61attest 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
65if [ "$(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
69fi
70
71if [ "$found" -lt 6 ] || [ "$signed" -lt 2 ]; then
72 echo "attest-conformance: $found vectors, $signed signed — the set has been emptied" >&2
73 exit 1
74fi
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.
79refused=0
80for 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
86done
87if [ "$refused" -lt 31 ]; then
88 echo "attest-conformance: only $refused refusals — the set has been emptied" >&2
89 exit 1
90fi
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.
95mkdir -p perturbed
96cp "$vectors/vectors/01-statement-minimal/input.json" perturbed/
97sed 's/^predicate.result passed$/predicate.result failed/' \
98 "$vectors/vectors/01-statement-minimal/statement.txt" > perturbed/statement.txt
99cmp -s perturbed/statement.txt "$vectors/vectors/01-statement-minimal/statement.txt" \
100 && { echo "attest-conformance: the perturbation changed nothing" >&2; exit 1; }
101if writes perturbed; then
102 echo "attest-conformance: a perturbed vector passed; the comparison proves nothing" >&2
103 exit 1
104fi
105
106# And so must the signature check. One byte of the signed
107# text changed is a signature that no longer covers it.
108sed 's/a sealed environment/a sealed environmenT/' \
109 "$vectors"/vectors/04-two-signers/statement.note > perturbed/statement.note
110if 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
114fi
115grep -q 'does not check out over this text' perturbed.verify
116
117echo "attest-conformance: $found vectors written byte for byte, $signed signed, $refused refused"
118touch "$out"