manifest_test.go raw
1 package manifest_test
2
3 import (
4 "bytes"
5 "crypto/ed25519"
6 "encoding/json"
7 "testing"
8
9 "git.smesh.lol/btcd-lite/internal/chunk"
10 "git.smesh.lol/btcd-lite/internal/manifest"
11 )
12
13 func key(t *testing.T) ed25519.PrivateKey {
14 t.Helper()
15 // A fixed seed: a manifest test must fail the same way twice.
16 seed := make([]byte, ed25519.SeedSize)
17 for i := range seed {
18 seed[i] = byte(i)
19 }
20 return ed25519.NewKeyFromSeed(seed)
21 }
22
23 func sample(t *testing.T) *manifest.Manifest {
24 t.Helper()
25 m, err := manifest.New("a", key(t))
26 if err != nil {
27 t.Fatal(err)
28 }
29 if err := m.Add(chunk.Blocks, "btc", 0, 100, []string{"aa", "bb"}); err != nil {
30 t.Fatal(err)
31 }
32 if err := m.Add(chunk.Headers, "btc", 0, 200, []string{"cc"}); err != nil {
33 t.Fatal(err)
34 }
35 if err := m.Sign(key(t)); err != nil {
36 t.Fatal(err)
37 }
38 return m
39 }
40
41 // TestSignVerify: a manifest signed with the pinned key verifies, and its
42 // published bytes are the ones that were signed.
43 func TestSignVerify(t *testing.T) {
44 m := sample(t)
45 raw, err := m.Encode()
46 if err != nil {
47 t.Fatal(err)
48 }
49 pub := key(t).Public().(ed25519.PublicKey)
50 back, err := manifest.Verify(raw, pub)
51 if err != nil {
52 t.Fatalf("verify: %v", err)
53 }
54 if back.Instance != "a" {
55 t.Fatalf("instance %q", back.Instance)
56 }
57 if c, ok := back.ChainTip("btc"); !ok || c.Start != 0 || c.Tip != 200 {
58 t.Fatalf("coverage %+v", c)
59 }
60 }
61
62 // TestTamperRefused: one changed byte in a chunk name or a range fails the
63 // signature, which is the whole point of signing it.
64 func TestTamperRefused(t *testing.T) {
65 pub := key(t).Public().(ed25519.PublicKey)
66 cases := map[string]func(*manifest.Manifest){
67 "chunk name": func(m *manifest.Manifest) { m.Runs[0].Chunks[0] = "ff" },
68 "range": func(m *manifest.Manifest) { m.Runs[0].To = 101 },
69 "coverage": func(m *manifest.Manifest) { m.Chains["btc"] = manifest.Coverage{Start: 0, Tip: 999} },
70 "instance": func(m *manifest.Manifest) { m.Instance = "evil" },
71 }
72 for name, change := range cases {
73 m := sample(t)
74 raw, _ := m.Encode()
75 var doc map[string]any
76 if err := json.Unmarshal(raw, &doc); err != nil {
77 t.Fatal(err)
78 }
79 // Round trip through a fresh manifest so the change is made on a copy
80 // with the signature still in place.
81 var m2 manifest.Manifest
82 raw2, _ := json.Marshal(doc)
83 if err := json.Unmarshal(raw2, &m2); err != nil {
84 t.Fatal(err)
85 }
86 change(&m2)
87 tampered, _ := json.Marshal(&m2)
88 if _, err := manifest.Verify(tampered, pub); err == nil {
89 t.Fatalf("%s: a tampered manifest verified", name)
90 }
91 }
92 // A manifest signed by another key is refused even if its signature is
93 // valid: the pinned key is the identity.
94 other := ed25519.NewKeyFromSeed(bytes.Repeat([]byte{7}, ed25519.SeedSize))
95 m := sample(t)
96 if err := m.Sign(other); err != nil {
97 t.Fatal(err)
98 }
99 raw, _ := m.Encode()
100 if _, err := manifest.Verify(raw, pub); err == nil {
101 t.Fatal("a manifest signed by another key verified")
102 }
103 }
104
105 // TestCanonicalOrder: the same run added in a different order gives the same
106 // canonical bytes and the same signature.
107 func TestCanonicalOrder(t *testing.T) {
108 a, _ := manifest.New("x", key(t))
109 _ = a.Add(chunk.Blocks, "btc", 0, 10, []string{"a", "b"})
110 _ = a.Add(chunk.Headers, "btc", 0, 10, []string{"c"})
111 b, _ := manifest.New("x", key(t))
112 _ = b.Add(chunk.Headers, "btc", 0, 10, []string{"c"})
113 _ = b.Add(chunk.Blocks, "btc", 0, 10, []string{"b", "a"})
114 ca, errA := a.Canonical()
115 cb, errB := b.Canonical()
116 if errA != nil || errB != nil {
117 t.Fatal(errA, errB)
118 }
119 if !bytes.Equal(ca, cb) {
120 t.Fatalf("canonical bytes differ:\n%s\n%s", ca, cb)
121 }
122 }
123
124 // TestRunLookup: a run answers only for a range it covers.
125 func TestRunLookup(t *testing.T) {
126 m := sample(t)
127 if _, ok := m.Run(chunk.Blocks, "btc", 10, 50); !ok {
128 t.Fatal("run for 10-50 not found")
129 }
130 if _, ok := m.Run(chunk.Blocks, "btc", 50, 150); ok {
131 t.Fatal("run for 50-150 found past its end")
132 }
133 if _, ok := m.Run(chunk.Blocks, "xbt", 0, 10); ok {
134 t.Fatal("btc run found for xbt")
135 }
136 }
137