summaryrefslogtreecommitdiffhomepage
path: root/tka/chaintest_test.go
blob: c370bf60a2e4cf6c34d6a8ea427cc83b654b56b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package tka

import (
	"bytes"
	"crypto/ed25519"
	"fmt"
	"strconv"
	"strings"
	"testing"
	"text/scanner"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"tailscale.com/types/tkatype"
)

// chaintest_test.go implements test helpers for concisely describing
// chains of possibly signed AUMs, to assist in making tests shorter and
// easier to read.

// parsed representation of a named AUM in a test chain.
type testchainNode struct {
	Name   string
	Parent string
	Uses   []scanner.Position

	HashSeed   int
	Template   string
	SignedWith string

	// When set, uses this hash as the parent hash when
	// Parent is not set.
	//
	// Set when a testChain is based on a different one
	// (in scenario_test.go).
	ParentHash *AUMHash
}

// testChain represents a constructed web of AUMs for testing purposes.
type testChain struct {
	FirstIdent string
	Nodes      map[string]*testchainNode
	AUMs       map[string]AUM
	AUMHashes  map[string]AUMHash

	// Configured by options to NewTestchain()
	Template    map[string]AUM
	Key         map[string]*Key
	KeyPrivs    map[string]ed25519.PrivateKey
	SignAllKeys []string
}

// newTestchain constructs a web of AUMs based on the provided input and
// options.
//
// Input is expected to be a graph & tweaks, looking like this:
//
//	G1 -> A -> B
//	      | -> C
//
// which defines AUMs G1, A, B, and C; with G1 having no parent, A having
// G1 as a parent, and both B & C having A as a parent.
//
// Tweaks are specified like this:
//
//	<AUM>.<tweak> = <value>
//
// for example: G1.hashSeed = 2
//
// There are 3 available tweaks:
//   - hashSeed: Set to an integer to tweak the AUM hash of that AUM.
//   - template: Set to the name of a template provided via optTemplate().
//     The template is copied and use as the content for that AUM.
//   - signedWith: Set to the name of a key provided via optKey(). This
//     key is used to sign that AUM.
func newTestchain(t *testing.T, input string, options ...testchainOpt) *testChain {
	t.Helper()

	var (
		s   scanner.Scanner
		out = testChain{
			Nodes:    map[string]*testchainNode{},
			Template: map[string]AUM{},
			Key:      map[string]*Key{},
			KeyPrivs: map[string]ed25519.PrivateKey{},
		}
	)

	// Process any options
	for _, o := range options {
		if o.Template != nil {
			out.Template[o.Name] = *o.Template
		}
		if o.Key != nil {
			out.Key[o.Name] = o.Key
			out.KeyPrivs[o.Name] = o.Private
		}
		if o.SignAllWith {
			out.SignAllKeys = append(out.SignAllKeys, o.Name)
		}
	}

	s.Init(strings.NewReader(input))
	s.Mode = scanner.ScanIdents | scanner.SkipComments | scanner.ScanComments | scanner.ScanChars | scanner.ScanInts
	s.Whitespace ^= 1 << '\t' // clear tabs
	var (
		lastIdent    string
		lastWasChain bool // if the last token was '->'
	)
	for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
		switch tok {
		case '\t':
			t.Fatalf("tabs disallowed, use spaces (seen at %v)", s.Pos())

		case '.': // tweaks, like <ident>.hashSeed = <val>
			s.Scan()
			tweak := s.TokenText()
			if tok := s.Scan(); tok == '=' {
				s.Scan()
				switch tweak {
				case "hashSeed":
					out.Nodes[lastIdent].HashSeed, _ = strconv.Atoi(s.TokenText())
				case "template":
					out.Nodes[lastIdent].Template = s.TokenText()
				case "signedWith":
					out.Nodes[lastIdent].SignedWith = s.TokenText()
				}
			}

		case scanner.Ident:
			out.recordPos(s.TokenText(), s.Pos())
			// If the last token was '->', that means
			// that the next identifier has a child relationship
			// with the identifier preceding '->'.
			if lastWasChain {
				out.recordParent(t, s.TokenText(), lastIdent)
			}
			lastIdent = s.TokenText()
			if out.FirstIdent == "" {
				out.FirstIdent = s.TokenText()
			}

		case '-': // handle '->'
			switch s.Peek() {
			case '>':
				s.Scan()
				lastWasChain = true
				continue
			}

		case '|': // handle '|'
			line, col := s.Pos().Line, s.Pos().Column
		nodeLoop:
			for _, n := range out.Nodes {
				for _, p := range n.Uses {
					// Find the identifier used right here on the line above.
					if p.Line == line-1 && col <= p.Column && col > p.Column-len(n.Name) {
						lastIdent = n.Name
						out.recordPos(n.Name, s.Pos())
						break nodeLoop
					}
				}
			}
		}
		lastWasChain = false
		// t.Logf("tok = %v, %q", tok, s.TokenText())
	}

	out.buildChain()
	return &out
}

// called from the parser to record the location of an
// identifier (a named AUM).
func (c *testChain) recordPos(ident string, pos scanner.Position) {
	n := c.Nodes[ident]
	if n == nil {
		n = &testchainNode{Name: ident}
	}

	n.Uses = append(n.Uses, pos)
	c.Nodes[ident] = n
}

// called from the parser to record a parent relationship between
// two AUMs.
func (c *testChain) recordParent(t *testing.T, child, parent string) {
	if p := c.Nodes[child].Parent; p != "" && p != parent {
		t.Fatalf("differing parent specified for %s: %q != %q", child, p, parent)
	}
	c.Nodes[child].Parent = parent
}

// called after parsing to build the web of AUM structures.
// This method populates c.AUMs and c.AUMHashes.
func (c *testChain) buildChain() {
	pending := make(map[string]*testchainNode, len(c.Nodes))
	for k, v := range c.Nodes {
		pending[k] = v
	}

	// AUMs with a parent need to know their hash, so we
	// only compute AUMs who's parents have been computed
	// each iteration. Since at least the genesis AUM
	// had no parent, theres always a path to completion
	// in O(n+1) where n is the number of AUMs.
	c.AUMs = make(map[string]AUM, len(c.Nodes))
	c.AUMHashes = make(map[string]AUMHash, len(c.Nodes))
	for range len(c.Nodes) + 1 {
		if len(pending) == 0 {
			return
		}

		next := make([]*testchainNode, 0, 10)
		for _, v := range pending {
			if _, parentPending := pending[v.Parent]; !parentPending {
				next = append(next, v)
			}
		}

		for _, v := range next {
			aum := c.makeAUM(v)
			h := aum.Hash()

			c.AUMHashes[v.Name] = h
			c.AUMs[v.Name] = aum
			delete(pending, v.Name)
		}
	}
	panic("unexpected: incomplete despite len(Nodes)+1 iterations")
}

func (c *testChain) makeAUM(v *testchainNode) AUM {
	// By default, the AUM used is just a no-op AUM
	// with a parent hash set (if any).
	//
	// If <AUM>.template is set to the same name as in
	// a provided optTemplate(), the AUM is built
	// from a copy of that instead.
	//
	// If <AUM>.hashSeed = <int> is set, the KeyID is
	// tweaked to effect tweaking the hash. This is useful
	// if you want one AUM to have a lower hash than another.
	aum := AUM{MessageKind: AUMNoOp}
	if template := v.Template; template != "" {
		aum = c.Template[template]
	}
	if v.Parent != "" {
		parentHash := c.AUMHashes[v.Parent]
		aum.PrevAUMHash = parentHash[:]
	} else if v.ParentHash != nil {
		aum.PrevAUMHash = (*v.ParentHash)[:]
	}
	if seed := v.HashSeed; seed != 0 {
		aum.KeyID = []byte{byte(seed)}
	}
	if err := aum.StaticValidate(); err != nil {
		// Usually caused by a test writer specifying a template
		// AUM which is ultimately invalid.
		panic(fmt.Sprintf("aum %+v failed static validation: %v", aum, err))
	}

	sigHash := aum.SigHash()
	for _, key := range c.SignAllKeys {
		aum.Signatures = append(aum.Signatures, tkatype.Signature{
			KeyID:     c.Key[key].MustID(),
			Signature: ed25519.Sign(c.KeyPrivs[key], sigHash[:]),
		})
	}

	// If the aum was specified as being signed by some key, then
	// sign it using that key.
	if key := v.SignedWith; key != "" {
		aum.Signatures = append(aum.Signatures, tkatype.Signature{
			KeyID:     c.Key[key].MustID(),
			Signature: ed25519.Sign(c.KeyPrivs[key], sigHash[:]),
		})
	}

	return aum
}

// Chonk returns a tailchonk containing all AUMs.
func (c *testChain) Chonk() Chonk {
	out := ChonkMem()
	for _, update := range c.AUMs {
		if err := out.CommitVerifiedAUMs([]AUM{update}); err != nil {
			panic(err)
		}
	}
	return out
}

// ChonkWith returns a tailchonk containing the named AUMs.
func (c *testChain) ChonkWith(names ...string) Chonk {
	out := ChonkMem()
	for _, name := range names {
		update := c.AUMs[name]
		if err := out.CommitVerifiedAUMs([]AUM{update}); err != nil {
			panic(err)
		}
	}
	return out
}

type testchainOpt struct {
	Name        string
	Template    *AUM
	Key         *Key
	Private     ed25519.PrivateKey
	SignAllWith bool
}

func optTemplate(name string, template AUM) testchainOpt {
	return testchainOpt{
		Name:     name,
		Template: &template,
	}
}

func optKey(name string, key Key, priv ed25519.PrivateKey) testchainOpt {
	return testchainOpt{
		Name:    name,
		Key:     &key,
		Private: priv,
	}
}

func optSignAllUsing(keyName string) testchainOpt {
	return testchainOpt{
		Name:        keyName,
		SignAllWith: true,
	}
}

func TestNewTestchain(t *testing.T) {
	c := newTestchain(t, `
        genesis -> B -> C
                   | -> D
                   | -> E -> F

        E.hashSeed = 12 // tweak E to have the lowest hash so its chosen
        F.template = test
    `, optTemplate("test", AUM{MessageKind: AUMNoOp, KeyID: []byte{10}}))

	want := map[string]*testchainNode{
		"genesis": {Name: "genesis", Uses: []scanner.Position{{Line: 2, Column: 16}}},
		"B": {
			Name:   "B",
			Parent: "genesis",
			Uses:   []scanner.Position{{Line: 2, Column: 21}, {Line: 3, Column: 21}, {Line: 4, Column: 21}},
		},
		"C": {Name: "C", Parent: "B", Uses: []scanner.Position{{Line: 2, Column: 26}}},
		"D": {Name: "D", Parent: "B", Uses: []scanner.Position{{Line: 3, Column: 26}}},
		"E": {Name: "E", Parent: "B", HashSeed: 12, Uses: []scanner.Position{{Line: 4, Column: 26}, {Line: 6, Column: 10}}},
		"F": {Name: "F", Parent: "E", Template: "test", Uses: []scanner.Position{{Line: 4, Column: 31}, {Line: 7, Column: 10}}},
	}

	if diff := cmp.Diff(want, c.Nodes, cmpopts.IgnoreFields(scanner.Position{}, "Offset")); diff != "" {
		t.Errorf("decoded state differs (-want, +got):\n%s", diff)
	}
	if !bytes.Equal(c.AUMs["F"].KeyID, []byte{10}) {
		t.Errorf("AUM 'F' missing KeyID from template: %v", c.AUMs["F"])
	}

	// chonk := c.Chonk()
	// authority, err := Open(chonk)
	// if err != nil {
	// 	t.Errorf("failed to initialize from chonk: %v", err)
	// }

	// if authority.Head() != c.AUMHashes["F"] {
	// 	t.Errorf("head = %X, want %X", authority.Head(), c.AUMHashes["F"])
	// }
}