forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
beacondbtest.go
808 lines (754 loc) · 26.3 KB
/
beacondbtest.go
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
// Copyright 2019 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package beacondbtest
import (
"context"
"fmt"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/scionproto/scion/go/beacon_srv/internal/beacon"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/ctrl/seg"
"github.com/scionproto/scion/go/lib/ctrl/seg/mock_seg"
"github.com/scionproto/scion/go/lib/infra"
"github.com/scionproto/scion/go/lib/spath"
"github.com/scionproto/scion/go/lib/util"
"github.com/scionproto/scion/go/proto"
)
var (
ia311 = addr.IA{I: 1, A: 0xff0000000311}
ia330 = addr.IA{I: 1, A: 0xff0000000330}
ia331 = addr.IA{I: 1, A: 0xff0000000331}
ia332 = addr.IA{I: 1, A: 0xff0000000332}
Info1 = []IfInfo{
{
IA: ia311,
Egress: 10,
},
}
Info2 = []IfInfo{
{
IA: ia330,
Egress: 4,
},
{
IA: ia331,
Ingress: 1,
Egress: 4,
Peers: []PeerEntry{{IA: ia311, Ingress: 4}},
},
}
Info3 = []IfInfo{
{
IA: ia330,
Egress: 5,
},
{
IA: ia331,
Ingress: 2,
Egress: 3,
Peers: []PeerEntry{{IA: ia311, Ingress: 6}},
},
{
IA: ia332,
Ingress: 1,
Egress: 7,
},
}
timeout = 3 * time.Second
)
// Testable extends the beacon db interface with methods that are needed for
// testing.
type Testable interface {
beacon.DB
// Prepare should reset the internal state so that the DB is empty and is
// ready to be tested.
Prepare(t *testing.T, ctx context.Context)
}
// Test should be used to test any implementation of the BeaconDB interface. An
// implementation of the BeaconDB interface should at least have one test
// method that calls this test-suite.
func Test(t *testing.T, db Testable) {
testWrapper := func(test func(*testing.T, *gomock.Controller,
beacon.DBReadWrite)) func(t *testing.T) {
return func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
prepareCtx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
db.Prepare(t, prepareCtx)
test(t, ctrl, db)
}
}
tableWrapper := func(inTx bool, test func(*testing.T, Testable, bool)) func(t *testing.T) {
return func(t *testing.T) {
test(t, db, inTx)
}
}
t.Run("BeaconSources should report all sources",
testWrapper(testBeaconSources))
t.Run("InsertBeacon should correctly insert a new beacon",
testWrapper(testInsertBeacon))
t.Run("InsertBeacon should correctly update a new beacon",
testWrapper(testUpdateExisting))
t.Run("InsertBeacon should correctly ignore an older beacon",
testWrapper(testUpdateOlderIgnored))
t.Run("CandidateBeacons returns the expected beacons",
tableWrapper(false, testCandidateBeacons))
t.Run("DeleteExpired should delete expired segments",
testWrapper(testDeleteExpiredBeacons))
t.Run("DeleteRevokedBeacons",
tableWrapper(false, testDeleteRevokedBeacons))
t.Run("AllRevocations",
tableWrapper(false, testAllRevocations))
t.Run("InsertRevocation updates existing rev",
testWrapper(testInsertUpdateRevocation))
t.Run("DeleteRevocations should delete revocations",
tableWrapper(false, testDeleteRevocation2))
t.Run("DeleteExpiredRevocations should delete expired revocations",
testWrapper(testDeleteExpiredRevocations))
txTestWrapper := func(test func(*testing.T, *gomock.Controller,
beacon.DBReadWrite)) func(t *testing.T) {
return func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
db.Prepare(t, ctx)
tx, err := db.BeginTransaction(ctx, nil)
require.NoError(t, err)
test(t, ctrl, tx)
err = tx.Commit()
require.NoError(t, err)
}
}
t.Run("WithTransaction", func(t *testing.T) {
t.Run("BeaconSources should report all sources",
txTestWrapper(testBeaconSources))
t.Run("InsertBeacon should correctly insert a new beacon",
txTestWrapper(testInsertBeacon))
t.Run("InsertBeacon should correctly update a new beacon",
txTestWrapper(testUpdateExisting))
t.Run("InsertBeacon should correctly ignore an older beacon",
txTestWrapper(testUpdateOlderIgnored))
t.Run("CandidateBeacons returns the expected beacons",
tableWrapper(true, testCandidateBeacons))
t.Run("DeleteExpired should delete expired segments",
txTestWrapper(testDeleteExpiredBeacons))
t.Run("DeleteRevokedBeacons",
tableWrapper(true, testDeleteRevokedBeacons))
t.Run("AllRevocations",
tableWrapper(true, testAllRevocations))
t.Run("InsertRevocation updates existing rev",
txTestWrapper(testInsertUpdateRevocation))
t.Run("DeleteRevocations should delete revocations",
tableWrapper(true, testDeleteRevocation2))
t.Run("DeleteExpiredRevocations should delete expired revocations",
txTestWrapper(testDeleteExpiredRevocations))
t.Run("Test transaction rollback", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
prepareCtx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
db.Prepare(t, prepareCtx)
testRollback(t, ctrl, db)
})
})
}
func testBeaconSources(t *testing.T, ctrl *gomock.Controller, db beacon.DBReadWrite) {
for i, info := range [][]IfInfo{Info3, Info2, Info1} {
InsertBeacon(t, ctrl, db, info, 12, uint32(i), beacon.UsageProp)
}
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
ias, err := db.BeaconSources(ctx)
require.NoError(t, err)
assert.ElementsMatch(t, []addr.IA{ia311, ia330}, ias)
}
func testInsertBeacon(t *testing.T, ctrl *gomock.Controller, db beacon.DBReadWrite) {
TS := uint32(10)
b, _ := AllocBeacon(t, ctrl, Info3, 12, TS)
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
inserted, err := db.InsertBeacon(ctx, b, beacon.UsageProp)
require.NoError(t, err)
exp := beacon.InsertStats{Inserted: 1, Updated: 0}
assert.Equal(t, exp, inserted)
// Fetch the candidate beacons
results, err := db.CandidateBeacons(ctx, 10, beacon.UsageProp, addr.IA{})
require.NoError(t, err)
// There should only be one candidate beacon, and it should match the inserted.
CheckResult(t, results, b)
for _, usage := range []beacon.Usage{beacon.UsageUpReg, beacon.UsageDownReg,
beacon.UsageCoreReg} {
results, err = db.CandidateBeacons(ctx, 10, usage, addr.IA{})
CheckEmpty(t, usage.String(), results, err)
}
}
func testUpdateExisting(t *testing.T, ctrl *gomock.Controller, db beacon.DBReadWrite) {
oldTS := uint32(10)
oldB, oldId := AllocBeacon(t, ctrl, Info3, 12, oldTS)
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
inserted, err := db.InsertBeacon(ctx, oldB, beacon.UsageProp)
require.NoError(t, err)
exp := beacon.InsertStats{Inserted: 1, Updated: 0}
assert.Equal(t, exp, inserted)
newTS := uint32(20)
newB, newId := AllocBeacon(t, ctrl, Info3, 12, newTS)
assert.Equal(t, oldId, newId, "IDs should match")
inserted, err = db.InsertBeacon(ctx, newB, beacon.UsageDownReg)
require.NoError(t, err)
exp = beacon.InsertStats{Inserted: 0, Updated: 1}
assert.Equal(t, exp, inserted)
// Fetch the candidate beacons
results, err := db.CandidateBeacons(ctx, 10, beacon.UsageDownReg, addr.IA{})
require.NoError(t, err, "CandidateBeacons err")
// There should only be one candidate beacon, and it should match the inserted.
CheckResult(t, results, newB)
for _, usage := range []beacon.Usage{beacon.UsageUpReg, beacon.UsageProp,
beacon.UsageCoreReg} {
results, err = db.CandidateBeacons(ctx, 10, usage, addr.IA{})
CheckEmpty(t, usage.String(), results, err)
}
}
func testUpdateOlderIgnored(t *testing.T, ctrl *gomock.Controller, db beacon.DBReadWrite) {
newTS := uint32(20)
newB, newId := AllocBeacon(t, ctrl, Info3, 12, newTS)
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
inserted, err := db.InsertBeacon(ctx, newB, beacon.UsageProp)
require.NoError(t, err)
exp := beacon.InsertStats{Inserted: 1, Updated: 0}
assert.Equal(t, exp, inserted, "Inserted new")
oldTS := uint32(10)
oldB, oldId := AllocBeacon(t, ctrl, Info3, 12, oldTS)
assert.Equal(t, oldId, newId, "IDs should match")
inserted, err = db.InsertBeacon(ctx, oldB, beacon.UsageDownReg)
require.NoError(t, err)
exp = beacon.InsertStats{Inserted: 0, Updated: 0}
assert.Equal(t, exp, inserted, "Inserted old")
// Fetch the candidate beacons
results, err := db.CandidateBeacons(ctx, 10, beacon.UsageProp, addr.IA{})
require.NoError(t, err)
// There should only be one candidate beacon, and it should match the inserted.
CheckResult(t, results, newB)
for _, usage := range []beacon.Usage{beacon.UsageUpReg, beacon.UsageDownReg,
beacon.UsageCoreReg} {
results, err = db.CandidateBeacons(ctx, 10, usage, addr.IA{})
CheckEmpty(t, usage.String(), results, err)
}
}
func testCandidateBeacons(t *testing.T, db Testable, inTx bool) {
rootCtrl := gomock.NewController(t)
defer rootCtrl.Finish()
// Insert beacons from longest to shortest path such that the insertion
// order is not sorted the same as the expected outcome.
var beacons []beacon.Beacon
insertBeacons := func(t *testing.T, db beacon.DBReadWrite) {
for i, info := range [][]IfInfo{Info3, Info2, Info1} {
b := InsertBeacon(t, rootCtrl, db, info, 12, uint32(i), beacon.UsageProp)
// Prepend to get beacons sorted from shortest to longest path.
beacons = append([]beacon.Beacon{b}, beacons...)
}
}
insertBeacons(t, db)
tests := map[string]struct {
PrepareDB func(t *testing.T, ctx context.Context, db beacon.DBReadWrite)
Src addr.IA
Expected []beacon.Beacon
}{
"If no source ISD-AS is specified, all beacons are returned": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {
insertBeacons(t, db)
},
Expected: beacons,
},
"Only beacons with matching source ISD-AS are returned, if specified": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {
insertBeacons(t, db)
},
Src: beacons[0].Segment.FirstIA(),
Expected: []beacon.Beacon{beacons[0]},
},
"Revoked beacons are not returned": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {
insertBeacons(t, db)
sRev, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: Info3[2].Ingress,
RawIsdas: Info3[2].IA.IAInt(),
LinkType: proto.LinkType_child,
RawTimestamp: util.TimeToSecs(time.Now().Add(-5 * time.Second)),
RawTTL: 10,
}, infra.NullSigner)
require.NoError(t, err)
InsertRevocation(t, db, sRev)
},
// last beacon (info3) is revoked
Expected: beacons[:2],
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
db.Prepare(t, ctx)
test.PrepareDB(t, ctx, db)
results, err := db.CandidateBeacons(ctx, 10, beacon.UsageProp, test.Src)
require.NoError(t, err)
CheckResults(t, results, test.Expected)
})
}
}
func testDeleteExpiredBeacons(t *testing.T, ctrl *gomock.Controller, db beacon.DBReadWrite) {
ts1 := uint32(10)
ts2 := uint32(20)
// defaultExp is the default expiry of the hopfields.
defaultExp := spath.DefaultHopFExpiry.ToDuration()
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
InsertBeacon(t, ctrl, db, Info3, 12, ts1, beacon.UsageProp)
InsertBeacon(t, ctrl, db, Info2, 13, ts2, beacon.UsageProp)
// No expired beacon
deleted, err := db.DeleteExpiredBeacons(ctx, time.Unix(10, 0).Add(defaultExp))
require.NoError(t, err)
assert.Equal(t, 0, deleted, "Deleted")
// 1 expired
deleted, err = db.DeleteExpiredBeacons(ctx, time.Unix(20, 0).Add(defaultExp))
require.NoError(t, err)
assert.Equal(t, 1, deleted, "Deleted")
// 1 expired
deleted, err = db.DeleteExpiredBeacons(ctx, time.Unix(30, 0).Add(defaultExp))
require.NoError(t, err)
assert.Equal(t, 1, deleted, "Deleted")
}
func testDeleteRevokedBeacons(t *testing.T, db Testable, inTx bool) {
rootCtrl := gomock.NewController(t)
defer rootCtrl.Finish()
ts := uint32(10)
now := time.Unix(int64(ts)+2, 0)
b3 := InsertBeacon(t, rootCtrl, db, Info3, 12, ts, beacon.UsageProp)
b2 := InsertBeacon(t, rootCtrl, db, Info2, 13, ts, beacon.UsageProp)
srev1, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: Info3[2].Ingress,
RawIsdas: Info3[2].IA.IAInt(),
LinkType: proto.LinkType_child,
RawTimestamp: ts,
RawTTL: 10,
}, infra.NullSigner)
require.NoError(t, err)
srev2, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: Info2[1].Ingress,
RawIsdas: Info2[1].IA.IAInt(),
LinkType: proto.LinkType_child,
RawTimestamp: ts,
RawTTL: 10,
}, infra.NullSigner)
require.NoError(t, err)
tests := map[string]struct {
PrepareDB func(t *testing.T, ctx context.Context, db beacon.DBReadWrite)
ExpectedDeleted int
ExpectedBeacons []beacon.Beacon
}{
"DeleteRevokedBeacons with no revocations should not delete anything": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {},
ExpectedBeacons: []beacon.Beacon{b2, b3},
},
"DeleteRevokedBeacon with revocation on one beacon should delete it": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {
InsertRevocation(t, db, srev1)
},
ExpectedDeleted: 1,
ExpectedBeacons: []beacon.Beacon{b2},
},
"DeleteRevokedBeacon with revocation on both beacons should delete both": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {
InsertRevocation(t, db, srev1)
InsertRevocation(t, db, srev2)
},
ExpectedDeleted: 2,
ExpectedBeacons: []beacon.Beacon{},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
db.Prepare(t, ctx)
// reinsert b3 and b2 since the prepare above deletes them.
InsertBeacon(t, rootCtrl, db, Info3, 12, ts, beacon.UsageProp)
InsertBeacon(t, rootCtrl, db, Info2, 13, ts, beacon.UsageProp)
test.PrepareDB(t, ctx, db)
var deleted int
if inTx {
tx, err := db.BeginTransaction(ctx, nil)
require.NoError(t, err)
deleted, err = tx.DeleteRevokedBeacons(ctx, now)
require.NoError(t, err)
require.NoError(t, tx.Commit())
} else {
var err error
deleted, err = db.DeleteRevokedBeacons(ctx, now)
require.NoError(t, err)
}
assert.Equal(t, test.ExpectedDeleted, deleted)
results, err := db.CandidateBeacons(ctx, 10, beacon.UsageProp, addr.IA{})
require.NoError(t, err)
CheckResults(t, results, test.ExpectedBeacons)
})
}
}
func testAllRevocations(t *testing.T, db Testable, inTx bool) {
ts := util.TimeToSecs(time.Now().Add(-5 * time.Second))
srev1, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: Info3[2].Ingress,
RawIsdas: Info3[2].IA.IAInt(),
LinkType: proto.LinkType_child,
RawTimestamp: ts,
RawTTL: 10,
}, infra.NullSigner)
require.NoError(t, err)
tests := map[string]struct {
PrepareDB func(t *testing.T, ctx context.Context, db beacon.DBReadWrite)
ExpectedRevs []*path_mgmt.SignedRevInfo
}{
"AllRevocations on empty db should return an empty channel": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {},
},
"AllRevocations returns revocations in db": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {
InsertRevocation(t, db, srev1)
},
ExpectedRevs: []*path_mgmt.SignedRevInfo{srev1},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
db.Prepare(t, ctx)
test.PrepareDB(t, ctx, db)
if inTx {
tx, err := db.BeginTransaction(ctx, nil)
require.NoError(t, err)
revs, err := tx.AllRevocations(ctx)
require.NoError(t, err)
CheckRevs(t, revs, test.ExpectedRevs)
require.NoError(t, tx.Commit())
} else {
revs, err := db.AllRevocations(ctx)
require.NoError(t, err)
CheckRevs(t, revs, test.ExpectedRevs)
}
})
}
}
func testInsertUpdateRevocation(t *testing.T, _ *gomock.Controller, db beacon.DBReadWrite) {
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
ts := util.TimeToSecs(time.Now().Add(-5 * time.Second))
srev1, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: Info3[2].Ingress,
RawIsdas: Info3[2].IA.IAInt(),
LinkType: proto.LinkType_child,
RawTimestamp: ts,
RawTTL: 20,
}, infra.NullSigner)
require.NoError(t, err)
err = db.InsertRevocation(ctx, srev1)
require.NoError(t, err)
// insert newer revocation should override old one
srev2, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: Info3[2].Ingress,
RawIsdas: Info3[2].IA.IAInt(),
LinkType: proto.LinkType_child,
RawTimestamp: ts + 1,
RawTTL: 10,
}, infra.NullSigner)
require.NoError(t, err)
err = db.InsertRevocation(ctx, srev2)
require.NoError(t, err)
revs, err := db.AllRevocations(ctx)
require.NoError(t, err)
CheckRevs(t, revs, []*path_mgmt.SignedRevInfo{srev2})
// older revocation should keep the newer one.
err = db.InsertRevocation(ctx, srev1)
require.NoError(t, err)
revs, err = db.AllRevocations(ctx)
require.NoError(t, err)
CheckRevs(t, revs, []*path_mgmt.SignedRevInfo{srev2})
}
func testDeleteRevocation2(t *testing.T, db Testable, inTx bool) {
ts := util.TimeToSecs(time.Now().Add(-5 * time.Second))
srev1, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: Info3[2].Ingress,
RawIsdas: Info3[2].IA.IAInt(),
LinkType: proto.LinkType_child,
RawTimestamp: ts,
RawTTL: 10,
}, infra.NullSigner)
require.NoError(t, err)
tests := map[string]struct {
PrepareDB func(t *testing.T, ctx context.Context, db beacon.DBReadWrite)
Delete func(ctx context.Context, db beacon.DBReadWrite) error
ExpectedRevs []*path_mgmt.SignedRevInfo
}{
"Delete on empty db": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {},
Delete: func(ctx context.Context, db beacon.DBReadWrite) error {
return db.DeleteRevocation(ctx, Info3[2].IA, Info3[2].Ingress)
},
},
"Deleting an existing revocation removes it": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {
InsertRevocation(t, db, srev1)
},
Delete: func(ctx context.Context, db beacon.DBReadWrite) error {
return db.DeleteRevocation(ctx, Info3[2].IA, Info3[2].Ingress)
},
},
"Deleting non-existing other revocation does not delete existing": {
PrepareDB: func(t *testing.T, ctx context.Context, db beacon.DBReadWrite) {
InsertRevocation(t, db, srev1)
},
Delete: func(ctx context.Context, db beacon.DBReadWrite) error {
return db.DeleteRevocation(ctx, Info3[2].IA, Info3[2].Egress)
},
ExpectedRevs: []*path_mgmt.SignedRevInfo{srev1},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
db.Prepare(t, ctx)
test.PrepareDB(t, ctx, db)
if inTx {
tx, err := db.BeginTransaction(ctx, nil)
require.NoError(t, err)
err = test.Delete(ctx, tx)
require.NoError(t, err)
revs, err := tx.AllRevocations(ctx)
require.NoError(t, err)
CheckRevs(t, revs, test.ExpectedRevs)
require.NoError(t, tx.Commit())
} else {
err := test.Delete(ctx, db)
require.NoError(t, err)
revs, err := db.AllRevocations(ctx)
require.NoError(t, err)
CheckRevs(t, revs, test.ExpectedRevs)
}
})
}
}
func testDeleteExpiredRevocations(t *testing.T, _ *gomock.Controller, db beacon.DBReadWrite) {
now := time.Now()
ts := util.TimeToSecs(now.Add(-5 * time.Second))
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
// delete on empty should work
cnt, err := db.DeleteExpiredRevocations(ctx, now)
require.NoError(t, err)
assert.Zero(t, cnt)
revs, err := db.AllRevocations(ctx)
CheckEmptyRevs(t, revs, err)
// delete with non empty db:
srev1, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: Info3[2].Ingress,
RawIsdas: Info3[2].IA.IAInt(),
LinkType: proto.LinkType_child,
RawTimestamp: ts,
RawTTL: 10,
}, infra.NullSigner)
require.NoError(t, err)
InsertRevocation(t, db, srev1)
// non-expired revocation is not deleted.
cnt, err = db.DeleteExpiredRevocations(ctx, now)
require.NoError(t, err)
assert.Zero(t, cnt)
revs, err = db.AllRevocations(ctx)
require.NoError(t, err)
CheckRevs(t, revs, []*path_mgmt.SignedRevInfo{srev1})
// expired is deleted
cnt, err = db.DeleteExpiredRevocations(ctx, now.Add(6*time.Second))
require.NoError(t, err)
assert.Equal(t, 1, cnt, "Deletion expected")
revs, err = db.AllRevocations(ctx)
CheckEmptyRevs(t, revs, err)
}
func testRollback(t *testing.T, ctrl *gomock.Controller, db beacon.DB) {
ctx, cancelF := context.WithTimeout(context.Background(), time.Second)
defer cancelF()
tx, err := db.BeginTransaction(ctx, nil)
require.NoError(t, err)
b, _ := AllocBeacon(t, ctrl, Info3, 12, uint32(10))
inserted, err := tx.InsertBeacon(ctx, b, beacon.UsageProp)
require.NoError(t, err)
exp := beacon.InsertStats{Inserted: 1, Updated: 0}
assert.Equal(t, exp, inserted, "Insert should succeed")
err = tx.Rollback()
require.NoError(t, err)
results, err := db.CandidateBeacons(ctx, 10, beacon.UsageProp, addr.IA{})
CheckEmpty(t, beacon.UsageProp.String(), results, err)
}
// CheckResult checks that the expected beacon is returned in results, and
// that it is the only returned beacon
func CheckResult(t *testing.T, results <-chan beacon.BeaconOrErr, expected beacon.Beacon) {
CheckResults(t, results, []beacon.Beacon{expected})
}
func CheckResults(t *testing.T, results <-chan beacon.BeaconOrErr,
expectedBeacons []beacon.Beacon) {
for i, expected := range expectedBeacons {
select {
case res := <-results:
assert.NoError(t, res.Err, "Beacon %d err", i)
require.NotNil(t, res.Beacon.Segment, "Beacon %d segment", i)
// Make sure the segment is properly initialized.
_, err := res.Beacon.Segment.ID()
require.NoError(t, err)
_, err = res.Beacon.Segment.FullId()
require.NoError(t, err)
assert.Equal(t, expected.Segment, res.Beacon.Segment, "Segment %d should match", i)
assert.Equal(t, expected.InIfId, res.Beacon.InIfId, "InIfId %d should match", i)
case <-time.After(timeout):
t.Fatalf("Beacon %d took too long", i)
}
}
CheckEmpty(t, "", results, nil)
}
// CheckEmpty checks that no beacon is in the result channel.
func CheckEmpty(t *testing.T, name string, results <-chan beacon.BeaconOrErr, err error) {
t.Helper()
assert.NoError(t, err, name)
res, more := <-results
assert.False(t, more)
assert.Zero(t, res)
}
func CheckRevs(t *testing.T, results <-chan beacon.RevocationOrErr,
expectedRevs []*path_mgmt.SignedRevInfo) {
for i, expected := range expectedRevs {
select {
case res := <-results:
require.NoError(t, res.Err, fmt.Sprintf("Rev %d err", i))
require.NotNil(t, res.Rev, fmt.Sprintf("Rev %d nil", i))
// make sure revinfo is initialized so comparison works.
_, err := res.Rev.RevInfo()
require.NoError(t, err)
assert.Equal(t, expected, res.Rev, fmt.Sprintf("Rev %d rev", i))
case <-time.After(timeout):
t.Fatalf("Rev %d took too long", i)
}
}
CheckEmptyRevs(t, results, nil)
}
func CheckEmptyRevs(t *testing.T, results <-chan beacon.RevocationOrErr, err error) {
t.Helper()
assert.NoError(t, err)
res, more := <-results
assert.False(t, more)
assert.Zero(t, res)
}
func InsertBeacon(t *testing.T, ctrl *gomock.Controller, db beacon.DBReadWrite, ases []IfInfo,
inIfId common.IFIDType, infoTS uint32, allowed beacon.Usage) beacon.Beacon {
b, _ := AllocBeacon(t, ctrl, ases, inIfId, infoTS)
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
_, err := db.InsertBeacon(ctx, b, allowed)
require.NoError(t, err)
return b
}
func InsertRevocation(t *testing.T, db beacon.DBReadWrite, sRev *path_mgmt.SignedRevInfo) {
ctx, cancelF := context.WithTimeout(context.Background(), timeout)
defer cancelF()
err := db.InsertRevocation(ctx, sRev)
require.NoError(t, err)
}
type PeerEntry struct {
IA addr.IA
Ingress common.IFIDType
}
type IfInfo struct {
IA addr.IA
Ingress common.IFIDType
Egress common.IFIDType
Peers []PeerEntry
}
func AllocBeacon(t *testing.T, ctrl *gomock.Controller, ases []IfInfo, inIfId common.IFIDType,
infoTS uint32) (beacon.Beacon, common.RawBytes) {
entries := make([]*seg.ASEntry, len(ases))
for i, as := range ases {
prev := addr.IA{}
if i > 0 {
prev = ases[i-1].IA
}
next := addr.IA{}
if i < len(ases)-1 {
next = ases[i+1].IA
}
hops := []*seg.HopEntry{
allocHopEntry(prev, next, allocRawHop(as.Ingress, as.Egress)),
}
for _, peer := range as.Peers {
hops = append(hops, allocHopEntry(peer.IA, next, allocRawHop(peer.Ingress, as.Egress)))
}
entries[i] = &seg.ASEntry{
RawIA: as.IA.IAInt(),
HopEntries: hops,
}
}
info := &spath.InfoField{
TsInt: infoTS,
ISD: 1,
Hops: uint8(len(ases)),
}
pseg, err := seg.NewSeg(info)
require.NoError(t, err)
signer := mock_seg.NewMockSigner(ctrl)
signer.EXPECT().Sign(gomock.AssignableToTypeOf(common.RawBytes{})).Return(
&proto.SignS{}, nil).AnyTimes()
for _, entry := range entries {
err := pseg.AddASEntry(entry, signer)
require.NoError(t, err)
}
segID, err := pseg.ID()
require.NoError(t, err)
_, err = pseg.FullId()
require.NoError(t, err)
return beacon.Beacon{Segment: pseg, InIfId: inIfId}, segID
}
func allocRawHop(ingress, egress common.IFIDType) common.RawBytes {
raw := make([]byte, 8)
hf := spath.HopField{
ConsIngress: ingress,
ConsEgress: egress,
ExpTime: spath.DefaultHopFExpiry,
}
hf.Write(raw)
return raw
}
func allocHopEntry(inIA, outIA addr.IA, hopF common.RawBytes) *seg.HopEntry {
return &seg.HopEntry{
RawInIA: inIA.IAInt(),
RawOutIA: outIA.IAInt(),
RawHopField: hopF,
}
}