-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathspicy_test.go
More file actions
675 lines (561 loc) · 21.2 KB
/
Copy pathspicy_test.go
File metadata and controls
675 lines (561 loc) · 21.2 KB
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
package torchwood_test
import (
"bytes"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"strings"
"testing"
"filippo.io/torchwood"
"golang.org/x/mod/sumdb/note"
"golang.org/x/mod/sumdb/tlog"
)
// setupTestLog creates a test log with a signer/verifier and returns them
// along with a signed checkpoint for a log with the given size and tree hash.
func setupTestLog(t *testing.T, origin string, size int64, hash tlog.Hash) (note.Signer, note.Verifier, []byte) {
t.Helper()
// Generate a test key using note.GenerateKey
skey, vkey, err := note.GenerateKey(rand.Reader, origin)
if err != nil {
t.Fatalf("failed to generate key: %v", err)
}
signer, err := note.NewSigner(skey)
if err != nil {
t.Fatalf("failed to create signer: %v", err)
}
verifier, err := note.NewVerifier(vkey)
if err != nil {
t.Fatalf("failed to create verifier: %v", err)
}
checkpoint := torchwood.Checkpoint{
Origin: origin,
Tree: tlog.Tree{N: size, Hash: hash},
}
signedCheckpoint, err := note.Sign(¬e.Note{Text: checkpoint.String()}, signer)
if err != nil {
t.Fatalf("failed to sign checkpoint: %v", err)
}
return signer, verifier, signedCheckpoint
}
func TestFormatProof(t *testing.T) {
origin := "example.com/test"
hash := tlog.RecordHash([]byte("test data"))
// Create a simple proof
proof := tlog.RecordProof{
tlog.RecordHash([]byte("hash1")),
tlog.RecordHash([]byte("hash2")),
}
_, _, signedCheckpoint := setupTestLog(t, origin, 10, hash)
formatted := torchwood.FormatProof(5, proof, signedCheckpoint)
// Check that it starts with the header
if !bytes.HasPrefix(formatted, []byte("c2sp.org/tlog-proof@v1\n")) {
t.Errorf("formatted proof missing header")
}
// Check that it contains the index
if !bytes.Contains(formatted, []byte("index 5\n")) {
t.Errorf("formatted proof missing index")
}
// Check that it does NOT contain a extra line
if bytes.Contains(formatted, []byte("extra ")) {
t.Errorf("formatted proof should not contain extra")
}
// Check that it contains the proof hashes
for _, h := range proof {
if !bytes.Contains(formatted, []byte(base64.StdEncoding.EncodeToString(h[:]))) {
t.Errorf("formatted proof missing hash %x", h)
}
}
// Check that it contains the checkpoint
if !bytes.Contains(formatted, signedCheckpoint) {
t.Errorf("formatted proof missing signed checkpoint")
}
}
func TestFormatProofWithExtra(t *testing.T) {
origin := "example.com/test"
hash := tlog.RecordHash([]byte("test data"))
extra := []byte("test-extra-data")
proof := tlog.RecordProof{
tlog.RecordHash([]byte("hash1")),
}
_, _, signedCheckpoint := setupTestLog(t, origin, 10, hash)
formatted := torchwood.FormatProofWithExtraData(3, extra, proof, signedCheckpoint)
// Check that it starts with the header
if !bytes.HasPrefix(formatted, []byte("c2sp.org/tlog-proof@v1\n")) {
t.Errorf("formatted proof missing header")
}
// Check that it contains the index
if !bytes.Contains(formatted, []byte("index 3\n")) {
t.Errorf("formatted proof missing index")
}
// Check that it contains the extra
expectedExtra := "extra " + base64.StdEncoding.EncodeToString(extra) + "\n"
if !bytes.Contains(formatted, []byte(expectedExtra)) {
t.Errorf("formatted proof missing or incorrect extra, got %s", formatted)
}
// Check that it contains the checkpoint
if !bytes.Contains(formatted, signedCheckpoint) {
t.Errorf("formatted proof missing signed checkpoint")
}
}
func TestVerifyProof_Valid(t *testing.T) {
origin := "example.com/test"
data := []byte("test data for verification")
recordHash := tlog.RecordHash(data)
// Build a simple tree with one record
var hashes []tlog.Hash
hashReader := tlog.HashReaderFunc(func(indexes []int64) ([]tlog.Hash, error) {
var result []tlog.Hash
for _, idx := range indexes {
if idx < 0 || idx >= int64(len(hashes)) {
return nil, fmt.Errorf("hash index %d out of range", idx)
}
result = append(result, hashes[idx])
}
return result, nil
})
// Add one record to the tree
newHashes, err := tlog.StoredHashes(0, data, hashReader)
if err != nil {
t.Fatalf("failed to compute stored hashes: %v", err)
}
hashes = append(hashes, newHashes...)
treeHash, err := tlog.TreeHash(1, hashReader)
if err != nil {
t.Fatalf("failed to compute tree hash: %v", err)
}
_, verifier, signedCheckpoint := setupTestLog(t, origin, 1, treeHash)
// Generate proof for record 0
proof, err := tlog.ProveRecord(1, 0, hashReader)
if err != nil {
t.Fatalf("failed to generate proof: %v", err)
}
// Format the proof
formattedProof := torchwood.FormatProof(0, proof, signedCheckpoint)
// Verify the proof - use ThresholdPolicy with OriginPolicy and SingleVerifierPolicy
policy := torchwood.ThresholdPolicy(2, torchwood.OriginPolicy(origin), torchwood.SingleVerifierPolicy(verifier))
err = torchwood.VerifyProof(policy, recordHash, formattedProof)
if err != nil {
t.Errorf("VerifyProof failed for valid proof: %v", err)
}
}
func TestVerifyProof_ValidWithExtra(t *testing.T) {
origin := "example.com/test"
data := []byte("test data")
extra := []byte("my-extra")
recordHash := tlog.RecordHash(data)
var hashes []tlog.Hash
hashReader := tlog.HashReaderFunc(func(indexes []int64) ([]tlog.Hash, error) {
var result []tlog.Hash
for _, idx := range indexes {
if idx < 0 || idx >= int64(len(hashes)) {
return nil, fmt.Errorf("hash index %d out of range", idx)
}
result = append(result, hashes[idx])
}
return result, nil
})
newHashes, err := tlog.StoredHashes(0, data, hashReader)
if err != nil {
t.Fatalf("failed to compute stored hashes: %v", err)
}
hashes = append(hashes, newHashes...)
treeHash, err := tlog.TreeHash(1, hashReader)
if err != nil {
t.Fatalf("failed to compute tree hash: %v", err)
}
_, verifier, signedCheckpoint := setupTestLog(t, origin, 1, treeHash)
proof, err := tlog.ProveRecord(1, 0, hashReader)
if err != nil {
t.Fatalf("failed to generate proof: %v", err)
}
formattedProof := torchwood.FormatProofWithExtraData(0, extra, proof, signedCheckpoint)
policy := torchwood.ThresholdPolicy(2, torchwood.OriginPolicy(origin), torchwood.SingleVerifierPolicy(verifier))
err = torchwood.VerifyProof(policy, recordHash, formattedProof)
if err != nil {
t.Errorf("VerifyProof failed for valid proof with extra: %v", err)
}
}
func TestVerifyProof_MissingHeader(t *testing.T) {
proof := []byte("index 0\n\nexample.com/test\n1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n")
err := torchwood.VerifyProof(torchwood.ThresholdPolicy(0), tlog.Hash{}, proof)
if err == nil {
t.Error("VerifyProof should fail for missing header")
}
if !strings.Contains(err.Error(), "missing header") {
t.Errorf("expected 'missing header' error, got: %v", err)
}
}
func TestVerifyProof_InvalidHeader(t *testing.T) {
proof := []byte("wrong-header\nindex 0\n\nexample.com/test\n1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n")
err := torchwood.VerifyProof(torchwood.ThresholdPolicy(0), tlog.Hash{}, proof)
if err == nil {
t.Error("VerifyProof should fail for invalid header")
}
if !strings.Contains(err.Error(), "missing header") {
t.Errorf("expected 'missing header' error, got: %v", err)
}
}
func TestVerifyProof_MissingIndex(t *testing.T) {
proof := []byte("c2sp.org/tlog-proof@v1\n\nexample.com/test\n1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n")
err := torchwood.VerifyProof(torchwood.ThresholdPolicy(0), tlog.Hash{}, proof)
if err == nil {
t.Error("VerifyProof should fail for missing index")
}
if !strings.Contains(err.Error(), "malformed") {
t.Errorf("expected 'malformed' error, got: %v", err)
}
}
func TestVerifyProof_InvalidIndex(t *testing.T) {
proof := []byte("c2sp.org/tlog-proof@v1\nindex notanumber\n\nexample.com/test\n1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n")
err := torchwood.VerifyProof(torchwood.ThresholdPolicy(0), tlog.Hash{}, proof)
if err == nil {
t.Error("VerifyProof should fail for invalid index")
}
if !strings.Contains(err.Error(), "invalid index") {
t.Errorf("expected 'invalid index' error, got: %v", err)
}
}
func TestVerifyProof_NegativeIndex(t *testing.T) {
proof := []byte("c2sp.org/tlog-proof@v1\nindex -1\n\nexample.com/test\n1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n")
err := torchwood.VerifyProof(torchwood.ThresholdPolicy(0), tlog.Hash{}, proof)
if err == nil {
t.Error("VerifyProof should fail for negative index")
}
if !strings.Contains(err.Error(), "negative index") {
t.Errorf("expected 'negative index' error, got: %v", err)
}
}
func TestVerifyProof_InvalidExtra(t *testing.T) {
proof := []byte("c2sp.org/tlog-proof@v1\nextra not-valid-base64!!!\nindex 0\n\nexample.com/test\n1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n")
err := torchwood.VerifyProof(torchwood.ThresholdPolicy(0), tlog.Hash{}, proof)
if err == nil {
t.Error("VerifyProof should fail for invalid extra encoding")
}
if !strings.Contains(err.Error(), "invalid extra") {
t.Errorf("expected 'invalid extra' error, got: %v", err)
}
}
func TestVerifyProof_InvalidHash(t *testing.T) {
proof := []byte("c2sp.org/tlog-proof@v1\nindex 0\nnot-valid-base64!!!\n\nexample.com/test\n1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n")
err := torchwood.VerifyProof(torchwood.ThresholdPolicy(0), tlog.Hash{}, proof)
if err == nil {
t.Error("VerifyProof should fail for invalid hash encoding")
}
if !strings.Contains(err.Error(), "invalid hash") {
t.Errorf("expected 'invalid hash' error, got: %v", err)
}
}
func TestVerifyProof_InvalidHashLength(t *testing.T) {
// Create a base64-encoded hash that's too short (16 bytes instead of 32)
shortHash := base64.StdEncoding.EncodeToString(make([]byte, 16))
proof := []byte(fmt.Sprintf("c2sp.org/tlog-proof@v1\nindex 0\n%s\n\nexample.com/test\n1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n", shortHash))
err := torchwood.VerifyProof(torchwood.ThresholdPolicy(0), tlog.Hash{}, proof)
if err == nil {
t.Error("VerifyProof should fail for invalid hash length")
}
if !strings.Contains(err.Error(), "invalid hash length") {
t.Errorf("expected 'invalid hash length' error, got: %v", err)
}
}
func TestVerifyProof_OriginMismatch(t *testing.T) {
origin := "example.com/test"
wrongOrigin := "wrong.com/test"
// Create a valid-looking proof with wrong origin
var hashes []tlog.Hash
hashReader := tlog.HashReaderFunc(func(indexes []int64) ([]tlog.Hash, error) {
var result []tlog.Hash
for _, idx := range indexes {
if idx < 0 || idx >= int64(len(hashes)) {
return nil, fmt.Errorf("hash index %d out of range", idx)
}
result = append(result, hashes[idx])
}
return result, nil
})
data := []byte("test")
newHashes, err := tlog.StoredHashes(0, data, hashReader)
if err != nil {
t.Fatalf("failed to compute stored hashes: %v", err)
}
hashes = append(hashes, newHashes...)
treeHash, err := tlog.TreeHash(1, hashReader)
if err != nil {
t.Fatalf("failed to compute tree hash: %v", err)
}
// Create checkpoint with wrong origin
_, verifier, signedCheckpoint := setupTestLog(t, wrongOrigin, 1, treeHash)
proof, err := tlog.ProveRecord(1, 0, hashReader)
if err != nil {
t.Fatalf("failed to generate proof: %v", err)
}
formattedProof := torchwood.FormatProof(0, proof, signedCheckpoint)
// Policy expects origin but checkpoint has wrongOrigin
policy := torchwood.ThresholdPolicy(2, torchwood.OriginPolicy(origin), torchwood.SingleVerifierPolicy(verifier))
err = torchwood.VerifyProof(policy, tlog.RecordHash(data), formattedProof)
if err == nil {
t.Error("VerifyProof should fail for origin mismatch")
}
// The threshold policy reports the number of satisfied components
if !strings.Contains(err.Error(), "policy components satisfied") {
t.Errorf("expected 'policy components satisfied' error, got: %v", err)
}
}
func TestVerifyProof_SignatureVerificationFails(t *testing.T) {
origin := "example.com/test"
// Create a valid proof structure but with a bad signature
var hashes []tlog.Hash
hashReader := tlog.HashReaderFunc(func(indexes []int64) ([]tlog.Hash, error) {
var result []tlog.Hash
for _, idx := range indexes {
if idx < 0 || idx >= int64(len(hashes)) {
return nil, fmt.Errorf("hash index %d out of range", idx)
}
result = append(result, hashes[idx])
}
return result, nil
})
data := []byte("test")
newHashes, err := tlog.StoredHashes(0, data, hashReader)
if err != nil {
t.Fatalf("failed to compute stored hashes: %v", err)
}
hashes = append(hashes, newHashes...)
treeHash, err := tlog.TreeHash(1, hashReader)
if err != nil {
t.Fatalf("failed to compute tree hash: %v", err)
}
// Create two different signers
_, verifier1, signedCheckpoint := setupTestLog(t, origin, 1, treeHash)
_, verifier2, _ := setupTestLog(t, origin, 1, treeHash)
proof, err := tlog.ProveRecord(1, 0, hashReader)
if err != nil {
t.Fatalf("failed to generate proof: %v", err)
}
formattedProof := torchwood.FormatProof(0, proof, signedCheckpoint)
// Try to verify with the wrong verifier
wrongPolicy := torchwood.ThresholdPolicy(2, torchwood.OriginPolicy(origin), torchwood.SingleVerifierPolicy(verifier2))
err = torchwood.VerifyProof(wrongPolicy, tlog.RecordHash(data), formattedProof)
if err == nil {
t.Error("VerifyProof should fail when signature verification fails")
}
// Verify it works with the correct verifier
correctPolicy := torchwood.ThresholdPolicy(2, torchwood.OriginPolicy(origin), torchwood.SingleVerifierPolicy(verifier1))
err = torchwood.VerifyProof(correctPolicy, tlog.RecordHash(data), formattedProof)
if err != nil {
t.Errorf("VerifyProof should succeed with correct verifier: %v", err)
}
}
func TestVerifyProof_RecordVerificationFails(t *testing.T) {
origin := "example.com/test"
data := []byte("correct data")
wrongData := []byte("wrong data")
var hashes []tlog.Hash
hashReader := tlog.HashReaderFunc(func(indexes []int64) ([]tlog.Hash, error) {
var result []tlog.Hash
for _, idx := range indexes {
if idx < 0 || idx >= int64(len(hashes)) {
return nil, fmt.Errorf("hash index %d out of range", idx)
}
result = append(result, hashes[idx])
}
return result, nil
})
// Build tree with correct data
newHashes, err := tlog.StoredHashes(0, data, hashReader)
if err != nil {
t.Fatalf("failed to compute stored hashes: %v", err)
}
hashes = append(hashes, newHashes...)
treeHash, err := tlog.TreeHash(1, hashReader)
if err != nil {
t.Fatalf("failed to compute tree hash: %v", err)
}
_, verifier, signedCheckpoint := setupTestLog(t, origin, 1, treeHash)
proof, err := tlog.ProveRecord(1, 0, hashReader)
if err != nil {
t.Fatalf("failed to generate proof: %v", err)
}
formattedProof := torchwood.FormatProof(0, proof, signedCheckpoint)
// Try to verify with wrong data
policy := torchwood.ThresholdPolicy(2, torchwood.OriginPolicy(origin), torchwood.SingleVerifierPolicy(verifier))
err = torchwood.VerifyProof(policy, tlog.RecordHash(wrongData), formattedProof)
if err == nil {
t.Error("VerifyProof should fail when record hash doesn't match")
}
// Check that it returns a VerifyRecordError
var verifyErr *torchwood.VerifyRecordError
if !errors.As(err, &verifyErr) {
t.Errorf("expected VerifyRecordError, got: %T", err)
} else {
if verifyErr.Index != 0 {
t.Errorf("expected Index=0, got Index=%d", verifyErr.Index)
}
if len(verifyErr.Extra) != 0 {
t.Errorf("expected empty Extra, got Extra=%q", verifyErr.Extra)
}
}
}
func TestVerifyRecordError_WithExtra(t *testing.T) {
origin := "example.com/test"
data := []byte("correct data")
wrongData := []byte("wrong data")
extra := []byte("test-extra")
var hashes []tlog.Hash
hashReader := tlog.HashReaderFunc(func(indexes []int64) ([]tlog.Hash, error) {
var result []tlog.Hash
for _, idx := range indexes {
if idx < 0 || idx >= int64(len(hashes)) {
return nil, fmt.Errorf("hash index %d out of range", idx)
}
result = append(result, hashes[idx])
}
return result, nil
})
newHashes, err := tlog.StoredHashes(0, data, hashReader)
if err != nil {
t.Fatalf("failed to compute stored hashes: %v", err)
}
hashes = append(hashes, newHashes...)
treeHash, err := tlog.TreeHash(1, hashReader)
if err != nil {
t.Fatalf("failed to compute tree hash: %v", err)
}
_, verifier, signedCheckpoint := setupTestLog(t, origin, 1, treeHash)
proof, err := tlog.ProveRecord(1, 0, hashReader)
if err != nil {
t.Fatalf("failed to generate proof: %v", err)
}
formattedProof := torchwood.FormatProofWithExtraData(0, extra, proof, signedCheckpoint)
policy := torchwood.ThresholdPolicy(2, torchwood.OriginPolicy(origin), torchwood.SingleVerifierPolicy(verifier))
err = torchwood.VerifyProof(policy, tlog.RecordHash(wrongData), formattedProof)
if err == nil {
t.Error("VerifyProof should fail when record hash doesn't match")
}
// Check that it returns a VerifyRecordError with the extra
var verifyErr *torchwood.VerifyRecordError
if !errors.As(err, &verifyErr) {
t.Errorf("expected VerifyRecordError, got: %T", err)
} else {
if verifyErr.Index != 0 {
t.Errorf("expected Index=0, got Index=%d", verifyErr.Index)
}
if !bytes.Equal(verifyErr.Extra, extra) {
t.Errorf("expected Extra=%q, got Extra=%q", extra, verifyErr.Extra)
}
expectedMsg := "tlog record inclusion proof verification failed for index 0"
if verifyErr.Error() != expectedMsg {
t.Errorf("expected error message %q, got %q", expectedMsg, verifyErr.Error())
}
}
}
func TestVerifyCheckpoint_UnrestrictedOrigin(t *testing.T) {
origin := "example.com/test"
hash := tlog.RecordHash([]byte("test data"))
_, verifier, signedCheckpoint := setupTestLog(t, origin, 10, hash)
// SingleVerifierPolicy alone doesn't check the origin
policy := torchwood.SingleVerifierPolicy(verifier)
_, _, err := torchwood.VerifyCheckpoint(signedCheckpoint, policy)
if err == nil {
t.Error("VerifyCheckpoint should fail when policy doesn't check origin")
}
if !strings.Contains(err.Error(), "not checking the checkpoint origin") {
t.Errorf("expected 'not checking the checkpoint origin' error, got: %v", err)
}
}
func TestOriginPolicy_Check(t *testing.T) {
// Test basic OriginPolicy
policy := torchwood.OriginPolicy("example.com/test")
// Check with matching origin should pass
err := policy.Check("example.com/test", nil)
if err != nil {
t.Errorf("OriginPolicy.Check should pass for matching origin: %v", err)
}
// Check with non-matching origin should fail
err = policy.Check("wrong.com/test", nil)
if err == nil {
t.Error("OriginPolicy.Check should fail for non-matching origin")
}
if !strings.Contains(err.Error(), "origin mismatch") {
t.Errorf("expected 'origin mismatch' error, got: %v", err)
}
}
func TestOriginPolicy_ThresholdWithMultipleOrigins(t *testing.T) {
// Test Threshold(1, OriginPolicy("foo"), OriginPolicy("bar")) should work with foo or bar
policy := torchwood.ThresholdPolicy(1,
torchwood.OriginPolicy("foo.com/log"),
torchwood.OriginPolicy("bar.com/log"),
)
// Should pass with first origin
err := policy.Check("foo.com/log", nil)
if err != nil {
t.Errorf("ThresholdPolicy should pass with first origin: %v", err)
}
// Should pass with second origin
err = policy.Check("bar.com/log", nil)
if err != nil {
t.Errorf("ThresholdPolicy should pass with second origin: %v", err)
}
// Should fail with neither origin
err = policy.Check("other.com/log", nil)
if err == nil {
t.Error("ThresholdPolicy should fail with neither origin")
}
}
func TestOriginPolicy_ThresholdOnlyCountsContributingOrigins(t *testing.T) {
// A Threshold(1, OriginPolicy("foo"), OriginPolicy("bar")) should work with
// either foo or bar, not requiring both to match.
// This tests that only the OriginPolicy that actually matches contributes
// to the threshold.
origin := "foo.com/test"
hash := tlog.RecordHash([]byte("test data"))
_, verifier, signedCheckpoint := setupTestLog(t, origin, 10, hash)
// Policy: threshold 2 of [OriginPolicy(foo), OriginPolicy(bar), SingleVerifierPolicy]
// With origin "foo.com/test", only OriginPolicy(foo) and SingleVerifierPolicy should pass
// So threshold of 2 should be satisfied
policy := torchwood.ThresholdPolicy(2,
torchwood.ThresholdPolicy(1,
torchwood.OriginPolicy("foo.com/test"),
torchwood.OriginPolicy("bar.com/test"),
),
torchwood.SingleVerifierPolicy(verifier),
)
c, _, err := torchwood.VerifyCheckpoint(signedCheckpoint, policy)
if err != nil {
t.Errorf("VerifyCheckpoint should succeed: %v", err)
}
if c.Origin != origin {
t.Errorf("expected origin %q, got %q", origin, c.Origin)
}
// Now test with bar.com/test origin
origin2 := "bar.com/test"
_, verifier2, signedCheckpoint2 := setupTestLog(t, origin2, 10, hash)
policy2 := torchwood.ThresholdPolicy(2,
torchwood.ThresholdPolicy(1,
torchwood.OriginPolicy("foo.com/test"),
torchwood.OriginPolicy("bar.com/test"),
),
torchwood.SingleVerifierPolicy(verifier2),
)
c2, _, err := torchwood.VerifyCheckpoint(signedCheckpoint2, policy2)
if err != nil {
t.Errorf("VerifyCheckpoint should succeed with second origin: %v", err)
}
if c2.Origin != origin2 {
t.Errorf("expected origin %q, got %q", origin2, c2.Origin)
}
// Test that wrong origin fails
origin3 := "wrong.com/test"
_, verifier3, signedCheckpoint3 := setupTestLog(t, origin3, 10, hash)
policy3 := torchwood.ThresholdPolicy(2,
torchwood.ThresholdPolicy(1,
torchwood.OriginPolicy("foo.com/test"),
torchwood.OriginPolicy("bar.com/test"),
),
torchwood.SingleVerifierPolicy(verifier3),
)
_, _, err = torchwood.VerifyCheckpoint(signedCheckpoint3, policy3)
if err == nil {
t.Error("VerifyCheckpoint should fail with wrong origin")
}
}