gunk-dev/the-valley

note_test.go

source75d050a4e31bmain's published tip history
4.0 KiB
1package note
2
3import (
4 "crypto/ed25519"
5 "crypto/rand"
6 "encoding/base64"
7 "encoding/binary"
8 "strings"
9 "testing"
10)
11
12const text = StatementType + "\nsubject.primary valley-tree-v1\nsubject.digest.valley-tree-v1 " +
13 "1e1c4b3a55a0a3e46e0a8f5f5b0e0c47b0b1b2a0c9d8e7f6a5b4c3d2e1f00912\n"
14
15func key(t *testing.T) ed25519.PrivateKey {
16 t.Helper()
17 _, priv, err := ed25519.GenerateKey(rand.Reader)
18 if err != nil {
19 t.Fatal(err)
20 }
21 return priv
22}
23
24func verifier(name string, priv ed25519.PrivateKey) VerifierKey {
25 return NewVerifierKey(name, priv.Public().(ed25519.PublicKey))
26}
27
28// A relayer can change a note's signature block and nothing else: the
29// text is signed. So every way of adding to the block has to be refused
30// by the one reading every reader shares, or a note one reader accepts is
31// a note another refuses — and in a create-only namespace, the first
32// reader's acceptance is permanent. The first case is the one the review
33// that found this wrote down: a genuine note with an unknown signature
34// line appended, whose signer name holds a tab.
35func TestARelayerCannotAppendWhatAnotherReaderRefuses(t *testing.T) {
36 signer := key(t)
37 genuine, err := Sign([]byte(text), "signer/attestations", signer)
38 if err != nil {
39 t.Fatal(err)
40 }
41 known := []VerifierKey{verifier("signer/attestations", signer)}
42 if _, _, err := Open(genuine, known); err != nil {
43 t.Fatalf("the genuine note: %v", err)
44 }
45
46 for what, appended := range map[string]string{
47 "a tab in the signer's name": "— x\t AAAAAAA=\n",
48 "a signature that is not Ed25519": "— x AAAAAAA=\n",
49 "base64 that does not decode": "— x !!!!\n",
50 "no signature at all": "— x\n",
51 "a line that is not a signature": "a comment\n",
52 "a second line by the same signer": strings.SplitAfterN(string(genuine), "\n\n", 2)[1],
53 } {
54 poisoned := append(append([]byte{}, genuine...), appended...)
55 if _, _, err := Split(poisoned); err == nil {
56 t.Errorf("%s: the note split", what)
57 }
58 if _, _, err := Open(poisoned, known); err == nil {
59 t.Errorf("%s: the note opened", what)
60 }
61 }
62}
63
64// A well-formed signature by a key the reader does not hold is a party the
65// reader cannot speak for, not a forgery: it is carried through as
66// unknown, and the note opens on the signature the reader can check.
67func TestAWellFormedSignatureByAnUnknownKeyIsCarriedThrough(t *testing.T) {
68 signer, witness := key(t), key(t)
69 n, err := Sign([]byte(text), "signer/attestations", signer)
70 if err != nil {
71 t.Fatal(err)
72 }
73 if n, err = AddSignature(n, "witness/attestations", witness); err != nil {
74 t.Fatal(err)
75 }
76 _, checked, err := Open(n, []VerifierKey{verifier("signer/attestations", signer)})
77 if err != nil {
78 t.Fatal(err)
79 }
80 if len(checked) != 2 || !checked[0].Verified || checked[1].Known {
81 t.Errorf("checked = %+v, want the signer verified and the witness unknown", checked)
82 }
83
84 // A signature by a key the reader does hold must verify, beside any
85 // other that does.
86 other := verifier("other/attestations", key(t))
87 blob := make([]byte, 4+ed25519.SignatureSize)
88 binary.BigEndian.PutUint32(blob, other.Hash)
89 forged := append(append([]byte{}, n...), Mark+"other/attestations "+base64.StdEncoding.EncodeToString(blob)+"\n"...)
90 if _, _, err := Open(forged, []VerifierKey{verifier("signer/attestations", signer), other}); err == nil {
91 t.Error("a note carrying a signature that does not check out opened")
92 }
93}
94
95func TestStatementSubject(t *testing.T) {
96 s, err := StatementSubject([]byte(text))
97 if err != nil {
98 t.Fatal(err)
99 }
100 if s.Scheme != "valley-tree-v1" || !strings.HasPrefix(s.Digest, "1e1c4b3a") {
101 t.Errorf("subject = %+v", s)
102 }
103 for what, bad := range map[string]string{
104 "another document": "something/else\nsubject.primary x\nsubject.digest.x y\n",
105 "no primary": StatementType + "\nsubject.digest.x y\n",
106 "a digest set twice": text + "subject.digest.valley-tree-v1 00\n",
107 "a line with no value": StatementType + "\nsubject.primary\n",
108 } {
109 if _, err := StatementSubject([]byte(bad)); err == nil {
110 t.Errorf("%s: read a subject", what)
111 }
112 }
113}