aboutsummaryrefslogtreecommitdiff
path: root/pkg/state/single_test.go
blob: 8e8902003c30088132979516e608aab998bc0fab (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
package state

import (
	"context"
	"crypto"
	"crypto/ed25519"
	"crypto/rand"
	"fmt"
	"reflect"
	"testing"
	"time"

	db "git.sigsum.org/log-go/pkg/db/mocks"
	"git.sigsum.org/log-go/pkg/state/mocks"
	"git.sigsum.org/sigsum-go/pkg/types"
	"github.com/golang/mock/gomock"
)

func TestNewStateManagerSingle(t *testing.T) {
	signerOk := &mocks.TestSigner{types.PublicKey{}, types.Signature{}, nil}
	signerErr := &mocks.TestSigner{types.PublicKey{}, types.Signature{}, fmt.Errorf("err")}
	for _, table := range []struct {
		description string
		signer      crypto.Signer
		rsp         types.TreeHead
		err         error
		wantErr     bool
		wantSth     types.SignedTreeHead
	}{
		{
			description: "invalid: backend failure",
			signer:      signerOk,
			err:         fmt.Errorf("something went wrong"),
			wantErr:     true,
		},
		{
			description: "invalid: signer failure",
			signer:      signerErr,
			rsp:         types.TreeHead{},
			wantErr:     true,
		},
		{
			description: "valid",
			signer:      signerOk,
			rsp:         types.TreeHead{},
			wantSth:     types.SignedTreeHead{},
		},
	} {
		func() {
			ctrl := gomock.NewController(t)
			defer ctrl.Finish()
			client := db.NewMockClient(ctrl)
			client.EXPECT().GetTreeHead(gomock.Any()).Return(&table.rsp, table.err)

			sm, err := NewStateManagerSingle(client, table.signer, time.Duration(0), time.Duration(0))
			if got, want := err != nil, table.wantErr; got != want {
				t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err)
			}
			if err != nil {
				return
			}
			if got, want := sm.signedTreeHead, &table.wantSth; !reflect.DeepEqual(got, want) {
				t.Errorf("got to-cosign tree head\n\t%v\nbut wanted\n\t%v\nin test %q", got, want, table.description)
			}
			if got := sm.cosignedTreeHead; got != nil {
				t.Errorf("got cosigned tree head but should have none in test %q", table.description)
			}
		}()
	}
}

func TestToCosignTreeHead(t *testing.T) {
	want := &types.SignedTreeHead{}
	sm := StateManagerSingle{
		signedTreeHead: want,
	}
	sth, err := sm.ToCosignTreeHead(context.Background())
	if err != nil {
		t.Errorf("should not fail with error: %v", err)
		return
	}
	if got := sth; !reflect.DeepEqual(got, want) {
		t.Errorf("got signed tree head\n\t%v\nbut wanted\n\t%v", got, want)
	}
}

func TestCosignedTreeHead(t *testing.T) {
	want := &types.CosignedTreeHead{
		Cosignature: make([]types.Signature, 1),
		KeyHash:     make([]types.Hash, 1),
	}
	sm := StateManagerSingle{
		cosignedTreeHead: want,
	}
	cth, err := sm.CosignedTreeHead(context.Background())
	if err != nil {
		t.Errorf("should not fail with error: %v", err)
		return
	}
	if got := cth; !reflect.DeepEqual(got, want) {
		t.Errorf("got cosigned tree head\n\t%v\nbut wanted\n\t%v", got, want)
	}

	sm.cosignedTreeHead = nil
	cth, err = sm.CosignedTreeHead(context.Background())
	if err == nil {
		t.Errorf("should fail without a cosigned tree head")
		return
	}
}

func TestAddCosignature(t *testing.T) {
	secret, public := mustKeyPair(t)
	for _, table := range []struct {
		desc    string
		signer  crypto.Signer
		vk      types.PublicKey
		wantErr bool
	}{
		{
			desc:    "invalid: wrong public key",
			signer:  secret,
			vk:      types.PublicKey{},
			wantErr: true,
		},
		{
			desc:   "valid",
			signer: secret,
			vk:     public,
		},
	} {
		sm := &StateManagerSingle{
			namespace:      *types.HashFn(nil),
			signedTreeHead: &types.SignedTreeHead{},
			events:         make(chan *event, 1),
		}
		defer close(sm.events)

		sth := mustSign(t, table.signer, &sm.signedTreeHead.TreeHead, &sm.namespace)
		ctx := context.Background()
		err := sm.AddCosignature(ctx, &table.vk, &sth.Signature)
		if got, want := err != nil, table.wantErr; got != want {
			t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.desc, err)
		}
		if err != nil {
			continue
		}

		ctx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
		defer cancel()
		if err := sm.AddCosignature(ctx, &table.vk, &sth.Signature); err == nil {
			t.Errorf("expected full channel in test %q", table.desc)
		}
		if got, want := len(sm.events), 1; got != want {
			t.Errorf("wanted %d cosignatures but got %d in test %q", want, got, table.desc)
		}
	}
}

func TestRotate(t *testing.T) {
	sth := &types.SignedTreeHead{}
	nextSTH := &types.SignedTreeHead{TreeHead: types.TreeHead{Timestamp: 1}}
	ev := &event{
		keyHash:     &types.Hash{},
		cosignature: &types.Signature{},
	}
	wantCTH := &types.CosignedTreeHead{
		SignedTreeHead: *sth,
		KeyHash:        []types.Hash{*ev.keyHash},
		Cosignature:    []types.Signature{*ev.cosignature},
	}
	sm := &StateManagerSingle{
		signedTreeHead: sth,
		cosignatures:   make(map[types.Hash]*types.Signature),
		events:         make(chan *event, 1),
	}
	defer close(sm.events)

	sm.events <- ev
	sm.rotate(nextSTH)
	if got, want := sm.signedTreeHead, nextSTH; !reflect.DeepEqual(got, want) {
		t.Errorf("got to-cosign tree head\n\t%v\nbut wanted\n\t%v", got, want)
	}
	if got, want := sm.cosignedTreeHead, wantCTH; !reflect.DeepEqual(got, want) {
		t.Errorf("got cosigned tree head\n\t%v\nbut wanted\n\t%v", got, want)
	}

	sth = nextSTH
	nextSTH = &types.SignedTreeHead{TreeHead: types.TreeHead{Timestamp: 2}}
	sm.rotate(nextSTH)
	if got, want := sm.signedTreeHead, nextSTH; !reflect.DeepEqual(got, want) {
		t.Errorf("got to-cosign tree head\n\t%v\nbut wanted\n\t%v", got, want)
	}
	if got := sm.cosignedTreeHead; got != nil {
		t.Errorf("expected no cosignatures to be available")
	}
}

func mustKeyPair(t *testing.T) (crypto.Signer, types.PublicKey) {
	t.Helper()
	vk, sk, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Fatal(err)
	}
	var pub types.PublicKey
	copy(pub[:], vk[:])
	return sk, pub
}

func mustSign(t *testing.T, s crypto.Signer, th *types.TreeHead, kh *types.Hash) *types.SignedTreeHead {
	t.Helper()
	sth, err := th.Sign(s, kh)
	if err != nil {
		t.Fatal(err)
	}
	return sth
}