@@ -10,13 +10,11 @@ import (
10
10
"crypto/sha256"
11
11
"crypto/x509"
12
12
"encoding/base64"
13
- "errors"
14
13
"flag"
15
14
mathrand "math/rand"
16
15
"reflect"
17
16
"slices"
18
17
"strconv"
19
- "strings"
20
18
"sync/atomic"
21
19
"testing"
22
20
"time"
@@ -29,11 +27,9 @@ import (
29
27
30
28
var globalTime = time .Now ().UnixMilli ()
31
29
32
- func init () {
33
- ctlog .SetTimeNowUnixMilli (func () int64 {
34
- return atomic .AddInt64 (& globalTime , 1 )
35
- })
36
- }
30
+ func monotonicTime () int64 { return atomic .AddInt64 (& globalTime , 1 ) }
31
+
32
+ func init () { ctlog .SetTimeNowUnixMilli (monotonicTime ) }
37
33
38
34
var longFlag = flag .Bool ("long" , false , "run especially slow tests" )
39
35
@@ -428,24 +424,78 @@ func TestReloadWrongKey(t *testing.T) {
428
424
}
429
425
}
430
426
431
- func TestFatalError (t * testing.T ) {
427
+ func TestStagingCollision (t * testing.T ) {
432
428
tl := NewEmptyTestLog (t )
433
429
addCertificate (t , tl )
434
- addCertificate (t , tl )
435
430
fatalIfErr (t , tl .Log .Sequence ())
436
431
437
- lockErr := errors .New ("lock replace error" )
438
- fail := func (old ctlog.LockedCheckpoint , new []byte ) (bool , error ) {
439
- return false , lockErr
432
+ time := monotonicTime ()
433
+ ctlog .SetTimeNowUnixMilli (func () int64 { return time })
434
+ t .Cleanup (func () { ctlog .SetTimeNowUnixMilli (monotonicTime ) })
435
+
436
+ addCertificateExpectFailureWithSeed (t , tl , 'A' )
437
+ addCertificateExpectFailureWithSeed (t , tl , 'B' )
438
+
439
+ tl .Config .Lock .(* MemoryLockBackend ).ReplaceCallback = failLockAndNotPersist
440
+ sequenceExpectFailure (t , tl )
441
+ tl .CheckLog (1 )
442
+
443
+ tl .Config .Lock .(* MemoryLockBackend ).ReplaceCallback = nil
444
+ tl = ReloadLog (t , tl )
445
+ tl .CheckLog (1 )
446
+
447
+ // First, cause the exact same staging bundle to be uploaded.
448
+
449
+ addCertificateWithSeed (t , tl , 'A' )
450
+ addCertificateWithSeed (t , tl , 'B' )
451
+ fatalIfErr (t , tl .Log .Sequence ())
452
+ tl .CheckLog (3 )
453
+
454
+ // Again, but now due to a staging bundle upload error.
455
+
456
+ time ++
457
+
458
+ addCertificateExpectFailureWithSeed (t , tl , 'C' )
459
+ addCertificateExpectFailureWithSeed (t , tl , 'D' )
460
+
461
+ tl .Config .Backend .(* MemoryBackend ).UploadCallback = failStagingButPersist
462
+ fatalIfErr (t , tl .Log .Sequence ())
463
+ tl .CheckLog (3 )
464
+
465
+ tl .Config .Backend .(* MemoryBackend ).UploadCallback = nil
466
+
467
+ addCertificateWithSeed (t , tl , 'C' )
468
+ addCertificateWithSeed (t , tl , 'D' )
469
+ fatalIfErr (t , tl .Log .Sequence ())
470
+ tl .CheckLog (5 )
471
+
472
+ // We wanted to also test reaching the same point through two different
473
+ // sequencing paths, as suggested in
474
+ // https://github.com/FiloSottile/sunlight/pull/18#discussion_r1704174301
475
+ // but that doesn't seem possible since time is required to move forward at
476
+ // each sequencing.
477
+ }
478
+
479
+ func sequenceExpectFailure (t * testing.T , tl * TestLog ) {
480
+ t .Helper ()
481
+ if err := tl .Log .Sequence (); err == nil {
482
+ t .Error ("expected error, got nil" )
440
483
}
484
+ }
441
485
442
- tl .Config .Lock .(* MemoryLockBackend ).ReplaceCallback = fail
486
+ func TestFatalError (t * testing.T ) {
487
+ tl := NewEmptyTestLog (t )
488
+ addCertificate (t , tl )
489
+ addCertificate (t , tl )
490
+ fatalIfErr (t , tl .Log .Sequence ())
491
+
492
+ tl .Config .Lock .(* MemoryLockBackend ).ReplaceCallback = failLockAndNotPersist
443
493
addCertificateExpectFailure (t , tl )
444
494
445
495
ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
446
496
defer cancel ()
447
- if err := tl .Log .RunSequencer (ctx , 1 * time .Millisecond ); ! errors . Is ( err , lockErr ) {
448
- t .Errorf ("expected fatal error, got %v" , err )
497
+ if err := tl .Log .RunSequencer (ctx , 1 * time .Millisecond ); err == nil {
498
+ t .Errorf ("expected fatal error, got nil" )
449
499
}
450
500
tl .CheckLog (2 )
451
501
@@ -474,10 +524,7 @@ func TestSequenceErrors(t *testing.T) {
474
524
// retried, and the same tiles are generated and uploaded again.
475
525
name : "LockUpload" ,
476
526
breakSeq : func (tl * TestLog ) {
477
- fail := func (old ctlog.LockedCheckpoint , new []byte ) (bool , error ) {
478
- return false , errors .New ("lock replace error" )
479
- }
480
- tl .Config .Lock .(* MemoryLockBackend ).ReplaceCallback = fail
527
+ tl .Config .Lock .(* MemoryLockBackend ).ReplaceCallback = failLockAndNotPersist
481
528
},
482
529
unbreakSeq : func (tl * TestLog ) {
483
530
tl .Config .Lock .(* MemoryLockBackend ).ReplaceCallback = nil
@@ -490,10 +537,7 @@ func TestSequenceErrors(t *testing.T) {
490
537
// persisted anyway, such as a response timeout.
491
538
name : "LockUploadPersisted" ,
492
539
breakSeq : func (tl * TestLog ) {
493
- fail := func (old ctlog.LockedCheckpoint , new []byte ) (bool , error ) {
494
- return true , errors .New ("lock response timeout" )
495
- }
496
- tl .Config .Lock .(* MemoryLockBackend ).ReplaceCallback = fail
540
+ tl .Config .Lock .(* MemoryLockBackend ).ReplaceCallback = failLockButPersist
497
541
},
498
542
unbreakSeq : func (tl * TestLog ) {
499
543
tl .Config .Lock .(* MemoryLockBackend ).ReplaceCallback = nil
@@ -504,13 +548,7 @@ func TestSequenceErrors(t *testing.T) {
504
548
{
505
549
name : "CheckpointUpload" ,
506
550
breakSeq : func (tl * TestLog ) {
507
- fail := func (key string , data []byte ) (apply bool , err error ) {
508
- if key == "checkpoint" {
509
- return false , errors .New ("checkpoint upload error" )
510
- }
511
- return true , nil
512
- }
513
- tl .Config .Backend .(* MemoryBackend ).UploadCallback = fail
551
+ tl .Config .Backend .(* MemoryBackend ).UploadCallback = failCheckpointAndNotPersist
514
552
},
515
553
unbreakSeq : func (tl * TestLog ) {
516
554
tl .Config .Backend .(* MemoryBackend ).UploadCallback = nil
@@ -521,13 +559,7 @@ func TestSequenceErrors(t *testing.T) {
521
559
{
522
560
name : "CheckpointUploadPersisted" ,
523
561
breakSeq : func (tl * TestLog ) {
524
- fail := func (key string , data []byte ) (apply bool , err error ) {
525
- if key == "checkpoint" {
526
- return true , errors .New ("checkpoint upload error" )
527
- }
528
- return true , nil
529
- }
530
- tl .Config .Backend .(* MemoryBackend ).UploadCallback = fail
562
+ tl .Config .Backend .(* MemoryBackend ).UploadCallback = failCheckpointButPersist
531
563
},
532
564
unbreakSeq : func (tl * TestLog ) {
533
565
tl .Config .Backend .(* MemoryBackend ).UploadCallback = nil
@@ -538,13 +570,7 @@ func TestSequenceErrors(t *testing.T) {
538
570
{
539
571
name : "StagingUpload" ,
540
572
breakSeq : func (tl * TestLog ) {
541
- fail := func (key string , data []byte ) (apply bool , err error ) {
542
- if strings .HasPrefix (key , "staging/" ) {
543
- return false , errors .New ("staging upload error" )
544
- }
545
- return true , nil
546
- }
547
- tl .Config .Backend .(* MemoryBackend ).UploadCallback = fail
573
+ tl .Config .Backend .(* MemoryBackend ).UploadCallback = failStagingAndNotPersist
548
574
},
549
575
unbreakSeq : func (tl * TestLog ) {
550
576
tl .Config .Backend .(* MemoryBackend ).UploadCallback = nil
@@ -555,13 +581,7 @@ func TestSequenceErrors(t *testing.T) {
555
581
{
556
582
name : "StagingUploadPersisted" ,
557
583
breakSeq : func (tl * TestLog ) {
558
- fail := func (key string , data []byte ) (apply bool , err error ) {
559
- if strings .HasPrefix (key , "staging/" ) {
560
- return true , errors .New ("staging upload error" )
561
- }
562
- return true , nil
563
- }
564
- tl .Config .Backend .(* MemoryBackend ).UploadCallback = fail
584
+ tl .Config .Backend .(* MemoryBackend ).UploadCallback = failStagingButPersist
565
585
},
566
586
unbreakSeq : func (tl * TestLog ) {
567
587
tl .Config .Backend .(* MemoryBackend ).UploadCallback = nil
@@ -572,13 +592,7 @@ func TestSequenceErrors(t *testing.T) {
572
592
{
573
593
name : "DataTileUpload" ,
574
594
breakSeq : func (tl * TestLog ) {
575
- fail := func (key string , data []byte ) (apply bool , err error ) {
576
- if strings .HasPrefix (key , "tile/data/" ) {
577
- return false , errors .New ("tile upload error" )
578
- }
579
- return true , nil
580
- }
581
- tl .Config .Backend .(* MemoryBackend ).UploadCallback = fail
595
+ tl .Config .Backend .(* MemoryBackend ).UploadCallback = failDataTileAndNotPersist
582
596
},
583
597
unbreakSeq : func (tl * TestLog ) {
584
598
tl .Config .Backend .(* MemoryBackend ).UploadCallback = nil
@@ -589,13 +603,7 @@ func TestSequenceErrors(t *testing.T) {
589
603
{
590
604
name : "DataTileUploadPersisted" ,
591
605
breakSeq : func (tl * TestLog ) {
592
- fail := func (key string , data []byte ) (apply bool , err error ) {
593
- if strings .HasPrefix (key , "tile/data/" ) {
594
- return true , errors .New ("tile upload error" )
595
- }
596
- return true , nil
597
- }
598
- tl .Config .Backend .(* MemoryBackend ).UploadCallback = fail
606
+ tl .Config .Backend .(* MemoryBackend ).UploadCallback = failDataTileButPersist
599
607
},
600
608
unbreakSeq : func (tl * TestLog ) {
601
609
tl .Config .Backend .(* MemoryBackend ).UploadCallback = nil
@@ -606,13 +614,7 @@ func TestSequenceErrors(t *testing.T) {
606
614
{
607
615
name : "Level0TileUpload" ,
608
616
breakSeq : func (tl * TestLog ) {
609
- fail := func (key string , data []byte ) (apply bool , err error ) {
610
- if strings .HasPrefix (key , "tile/0/" ) {
611
- return false , errors .New ("tile upload error" )
612
- }
613
- return true , nil
614
- }
615
- tl .Config .Backend .(* MemoryBackend ).UploadCallback = fail
617
+ tl .Config .Backend .(* MemoryBackend ).UploadCallback = failTile0AndNotPersist
616
618
},
617
619
unbreakSeq : func (tl * TestLog ) {
618
620
tl .Config .Backend .(* MemoryBackend ).UploadCallback = nil
@@ -623,13 +625,7 @@ func TestSequenceErrors(t *testing.T) {
623
625
{
624
626
name : "Level0TileUploadPersisted" ,
625
627
breakSeq : func (tl * TestLog ) {
626
- fail := func (key string , data []byte ) (apply bool , err error ) {
627
- if strings .HasPrefix (key , "tile/0/" ) {
628
- return true , errors .New ("tile upload error" )
629
- }
630
- return true , nil
631
- }
632
- tl .Config .Backend .(* MemoryBackend ).UploadCallback = fail
628
+ tl .Config .Backend .(* MemoryBackend ).UploadCallback = failTile0ButPersist
633
629
},
634
630
unbreakSeq : func (tl * TestLog ) {
635
631
tl .Config .Backend .(* MemoryBackend ).UploadCallback = nil
0 commit comments