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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
package db
import (
"bytes"
"context"
"fmt"
"reflect"
"testing"
"time"
"git.sigsum.org/log-go/pkg/db/mocks"
"git.sigsum.org/sigsum-go/pkg/merkle"
"git.sigsum.org/sigsum-go/pkg/requests"
"git.sigsum.org/sigsum-go/pkg/types"
"github.com/golang/mock/gomock"
"github.com/google/trillian"
ttypes "github.com/google/trillian/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestAddLeaf(t *testing.T) {
req := &requests.Leaf{
ShardHint: 0,
Message: merkle.Hash{},
Signature: types.Signature{},
PublicKey: types.PublicKey{},
DomainHint: "example.com",
}
for _, table := range []struct {
description string
req *requests.Leaf
rsp *trillian.QueueLeafResponse
err error
wantErr bool
}{
{
description: "invalid: backend failure",
req: req,
err: fmt.Errorf("something went wrong"),
wantErr: true,
},
{
description: "invalid: no response",
req: req,
wantErr: true,
},
{
description: "invalid: no queued leaf",
req: req,
rsp: &trillian.QueueLeafResponse{},
wantErr: true,
},
{
description: "invalid: leaf is already queued or included",
req: req,
rsp: &trillian.QueueLeafResponse{
QueuedLeaf: &trillian.QueuedLogLeaf{
Leaf: &trillian.LogLeaf{
LeafValue: []byte{0}, // does not matter for test
},
Status: status.New(codes.AlreadyExists, "duplicate").Proto(),
},
},
wantErr: true,
},
{
description: "valid",
req: req,
rsp: &trillian.QueueLeafResponse{
QueuedLeaf: &trillian.QueuedLogLeaf{
Leaf: &trillian.LogLeaf{
LeafValue: []byte{0}, // does not matter for test
},
Status: status.New(codes.OK, "ok").Proto(),
},
},
},
} {
// Run deferred functions at the end of each iteration
func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
grpc := mocks.NewMockTrillianLogClient(ctrl)
grpc.EXPECT().QueueLeaf(gomock.Any(), gomock.Any()).Return(table.rsp, table.err)
client := TrillianClient{GRPC: grpc}
err := client.AddLeaf(context.Background(), table.req)
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)
}
}()
}
}
func TestGetTreeHead(t *testing.T) {
// valid root
root := &ttypes.LogRootV1{
TreeSize: 0,
RootHash: make([]byte, merkle.HashSize),
TimestampNanos: 1622585623133599429,
}
buf, err := root.MarshalBinary()
if err != nil {
t.Fatalf("must marshal log root: %v", err)
}
// invalid root
root.RootHash = make([]byte, merkle.HashSize+1)
bufBadHash, err := root.MarshalBinary()
if err != nil {
t.Fatalf("must marshal log root: %v", err)
}
for _, table := range []struct {
description string
rsp *trillian.GetLatestSignedLogRootResponse
err error
wantErr bool
wantTh *types.TreeHead
}{
{
description: "invalid: backend failure",
err: fmt.Errorf("something went wrong"),
wantErr: true,
},
{
description: "invalid: no response",
wantErr: true,
},
{
description: "invalid: no signed log root",
rsp: &trillian.GetLatestSignedLogRootResponse{},
wantErr: true,
},
{
description: "invalid: no log root",
rsp: &trillian.GetLatestSignedLogRootResponse{
SignedLogRoot: &trillian.SignedLogRoot{},
},
wantErr: true,
},
{
description: "invalid: no log root: unmarshal failed",
rsp: &trillian.GetLatestSignedLogRootResponse{
SignedLogRoot: &trillian.SignedLogRoot{
LogRoot: buf[1:],
},
},
wantErr: true,
},
{
description: "invalid: unexpected hash length",
rsp: &trillian.GetLatestSignedLogRootResponse{
SignedLogRoot: &trillian.SignedLogRoot{
LogRoot: bufBadHash,
},
},
wantErr: true,
},
{
description: "valid",
rsp: &trillian.GetLatestSignedLogRootResponse{
SignedLogRoot: &trillian.SignedLogRoot{
LogRoot: buf,
},
},
wantTh: &types.TreeHead{
Timestamp: 1622585623,
TreeSize: 0,
RootHash: merkle.Hash{},
},
},
} {
// Run deferred functions at the end of each iteration
func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
grpc := mocks.NewMockTrillianLogClient(ctrl)
grpc.EXPECT().GetLatestSignedLogRoot(gomock.Any(), gomock.Any()).Return(table.rsp, table.err)
client := TrillianClient{GRPC: grpc}
th, err := client.GetTreeHead(context.Background())
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
}
// we would need a clock that can be mocked to make a nicer test
now := uint64(time.Now().Unix())
if got, wantLow, wantHigh := th.Timestamp, now-5, now+5; got < wantLow || got > wantHigh {
t.Errorf("got tree head with timestamp %d but wanted between [%d, %d] in test %q",
got, wantLow, wantHigh, table.description)
}
if got, want := th.TreeSize, table.wantTh.TreeSize; got != want {
t.Errorf("got tree head with tree size %d but wanted %d in test %q", got, want, table.description)
}
if got, want := th.RootHash[:], table.wantTh.RootHash[:]; !bytes.Equal(got, want) {
t.Errorf("got root hash %x but wanted %x in test %q", got, want, table.description)
}
}()
}
}
func TestGetConsistencyProof(t *testing.T) {
req := &requests.ConsistencyProof{
OldSize: 1,
NewSize: 3,
}
for _, table := range []struct {
description string
req *requests.ConsistencyProof
rsp *trillian.GetConsistencyProofResponse
err error
wantErr bool
wantProof *types.ConsistencyProof
}{
{
description: "invalid: backend failure",
req: req,
err: fmt.Errorf("something went wrong"),
wantErr: true,
},
{
description: "invalid: no response",
req: req,
wantErr: true,
},
{
description: "invalid: no consistency proof",
req: req,
rsp: &trillian.GetConsistencyProofResponse{},
wantErr: true,
},
{
description: "invalid: not a consistency proof (1/2)",
req: req,
rsp: &trillian.GetConsistencyProofResponse{
Proof: &trillian.Proof{
Hashes: [][]byte{},
},
},
wantErr: true,
},
{
description: "invalid: not a consistency proof (2/2)",
req: req,
rsp: &trillian.GetConsistencyProofResponse{
Proof: &trillian.Proof{
Hashes: [][]byte{
make([]byte, merkle.HashSize),
make([]byte, merkle.HashSize+1),
},
},
},
wantErr: true,
},
{
description: "valid",
req: req,
rsp: &trillian.GetConsistencyProofResponse{
Proof: &trillian.Proof{
Hashes: [][]byte{
make([]byte, merkle.HashSize),
make([]byte, merkle.HashSize),
},
},
},
wantProof: &types.ConsistencyProof{
OldSize: 1,
NewSize: 3,
Path: []merkle.Hash{
merkle.Hash{},
merkle.Hash{},
},
},
},
} {
// Run deferred functions at the end of each iteration
func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
grpc := mocks.NewMockTrillianLogClient(ctrl)
grpc.EXPECT().GetConsistencyProof(gomock.Any(), gomock.Any()).Return(table.rsp, table.err)
client := TrillianClient{GRPC: grpc}
proof, err := client.GetConsistencyProof(context.Background(), table.req)
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 := proof, table.wantProof; !reflect.DeepEqual(got, want) {
t.Errorf("got proof\n\t%v\nbut wanted\n\t%v\nin test %q", got, want, table.description)
}
}()
}
}
func TestGetInclusionProof(t *testing.T) {
req := &requests.InclusionProof{
TreeSize: 4,
LeafHash: merkle.Hash{},
}
for _, table := range []struct {
description string
req *requests.InclusionProof
rsp *trillian.GetInclusionProofByHashResponse
err error
wantErr bool
wantProof *types.InclusionProof
}{
{
description: "invalid: backend failure",
req: req,
err: fmt.Errorf("something went wrong"),
wantErr: true,
},
{
description: "invalid: no response",
req: req,
wantErr: true,
},
{
description: "invalid: bad proof count",
req: req,
rsp: &trillian.GetInclusionProofByHashResponse{
Proof: []*trillian.Proof{
&trillian.Proof{},
&trillian.Proof{},
},
},
wantErr: true,
},
{
description: "invalid: not an inclusion proof (1/2)",
req: req,
rsp: &trillian.GetInclusionProofByHashResponse{
Proof: []*trillian.Proof{
&trillian.Proof{
LeafIndex: 1,
Hashes: [][]byte{},
},
},
},
wantErr: true,
},
{
description: "invalid: not an inclusion proof (2/2)",
req: req,
rsp: &trillian.GetInclusionProofByHashResponse{
Proof: []*trillian.Proof{
&trillian.Proof{
LeafIndex: 1,
Hashes: [][]byte{
make([]byte, merkle.HashSize),
make([]byte, merkle.HashSize+1),
},
},
},
},
wantErr: true,
},
{
description: "valid",
req: req,
rsp: &trillian.GetInclusionProofByHashResponse{
Proof: []*trillian.Proof{
&trillian.Proof{
LeafIndex: 1,
Hashes: [][]byte{
make([]byte, merkle.HashSize),
make([]byte, merkle.HashSize),
},
},
},
},
wantProof: &types.InclusionProof{
TreeSize: 4,
LeafIndex: 1,
Path: []merkle.Hash{
merkle.Hash{},
merkle.Hash{},
},
},
},
} {
// Run deferred functions at the end of each iteration
func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
grpc := mocks.NewMockTrillianLogClient(ctrl)
grpc.EXPECT().GetInclusionProofByHash(gomock.Any(), gomock.Any()).Return(table.rsp, table.err)
client := TrillianClient{GRPC: grpc}
proof, err := client.GetInclusionProof(context.Background(), table.req)
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 := proof, table.wantProof; !reflect.DeepEqual(got, want) {
t.Errorf("got proof\n\t%v\nbut wanted\n\t%v\nin test %q", got, want, table.description)
}
}()
}
}
func TestGetLeaves(t *testing.T) {
req := &requests.Leaves{
StartSize: 1,
EndSize: 2,
}
firstLeaf := &types.Leaf{
Statement: types.Statement{
ShardHint: 0,
Checksum: merkle.Hash{},
},
Signature: types.Signature{},
KeyHash: merkle.Hash{},
}
secondLeaf := &types.Leaf{
Statement: types.Statement{
ShardHint: 0,
Checksum: merkle.Hash{},
},
Signature: types.Signature{},
KeyHash: merkle.Hash{},
}
for _, table := range []struct {
description string
req *requests.Leaves
rsp *trillian.GetLeavesByRangeResponse
err error
wantErr bool
wantLeaves *types.Leaves
}{
{
description: "invalid: backend failure",
req: req,
err: fmt.Errorf("something went wrong"),
wantErr: true,
},
{
description: "invalid: no response",
req: req,
wantErr: true,
},
{
description: "invalid: unexpected number of leaves",
req: req,
rsp: &trillian.GetLeavesByRangeResponse{
Leaves: []*trillian.LogLeaf{
&trillian.LogLeaf{
LeafValue: firstLeaf.ToBinary(),
LeafIndex: 1,
},
},
},
wantErr: true,
},
{
description: "invalid: unexpected leaf (1/2)",
req: req,
rsp: &trillian.GetLeavesByRangeResponse{
Leaves: []*trillian.LogLeaf{
&trillian.LogLeaf{
LeafValue: firstLeaf.ToBinary(),
LeafIndex: 1,
},
&trillian.LogLeaf{
LeafValue: secondLeaf.ToBinary(),
LeafIndex: 3,
},
},
},
wantErr: true,
},
{
description: "invalid: unexpected leaf (2/2)",
req: req,
rsp: &trillian.GetLeavesByRangeResponse{
Leaves: []*trillian.LogLeaf{
&trillian.LogLeaf{
LeafValue: firstLeaf.ToBinary(),
LeafIndex: 1,
},
&trillian.LogLeaf{
LeafValue: secondLeaf.ToBinary()[1:],
LeafIndex: 2,
},
},
},
wantErr: true,
},
{
description: "valid",
req: req,
rsp: &trillian.GetLeavesByRangeResponse{
Leaves: []*trillian.LogLeaf{
&trillian.LogLeaf{
LeafValue: firstLeaf.ToBinary(),
LeafIndex: 1,
},
&trillian.LogLeaf{
LeafValue: secondLeaf.ToBinary(),
LeafIndex: 2,
},
},
},
wantLeaves: &types.Leaves{
*firstLeaf,
*secondLeaf,
},
},
} {
// Run deferred functions at the end of each iteration
func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
grpc := mocks.NewMockTrillianLogClient(ctrl)
grpc.EXPECT().GetLeavesByRange(gomock.Any(), gomock.Any()).Return(table.rsp, table.err)
client := TrillianClient{GRPC: grpc}
leaves, err := client.GetLeaves(context.Background(), table.req)
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 := leaves, table.wantLeaves; !reflect.DeepEqual(got, want) {
t.Errorf("got leaves\n\t%v\nbut wanted\n\t%v\nin test %q", got, want, table.description)
}
}()
}
}
|