forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
revcachetest.go
360 lines (333 loc) · 14 KB
/
revcachetest.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
// Copyright 2018 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 revcachetest
import (
"context"
"sort"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"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/infra"
"github.com/scionproto/scion/go/lib/revcache"
"github.com/scionproto/scion/go/lib/util"
"github.com/scionproto/scion/go/lib/xtest"
"github.com/scionproto/scion/go/proto"
)
var (
ia110 = xtest.MustParseIA("1-ff00:0:110")
ia120 = xtest.MustParseIA("1-ff00:0:120")
ifId15 = common.IFIDType(15)
ifId19 = common.IFIDType(19)
TimeOut = 5 * time.Second
)
// TestableRevCache extends the RevCache interface with methods that are needed for testing.
type TestableRevCache interface {
revcache.RevCache
// InsertExpired should insert the given expired revocation.
// The testing parameter should be used to fail in case of an error.
// The method is used to test if expired revocations are not returned.
InsertExpired(t *testing.T, ctx context.Context, rev *path_mgmt.SignedRevInfo)
// Prepare should reset the internal state so that the cache is empty and is ready to be tested.
Prepare(t *testing.T, ctx context.Context)
}
// TestRevCache should be used to test any implementation of the RevCache interface.
//
// setup should return a RevCache in a clean state, i.e. no entries in the cache.
// cleanup can be used to release any resources that have been allocated during setup.
func TestRevCache(t *testing.T, revCache TestableRevCache) {
testWrapper := func(test func(*testing.T, TestableRevCache)) func() {
return func() {
prepareCtx, cancelF := context.WithTimeout(context.Background(), TimeOut)
defer cancelF()
revCache.Prepare(t, prepareCtx)
test(t, revCache)
}
}
Convey("InsertGet", testWrapper(testInsertGet))
Convey("GetMultikey", testWrapper(testGetMultikey))
Convey("GetAll", testWrapper(testGetAll))
Convey("GetAllExpired", testWrapper(testGetAllExpired))
Convey("InsertExpired", testWrapper(testInsertExpired))
Convey("InsertNewer", testWrapper(testInsertNewer))
Convey("GetExpired", testWrapper(testGetExpired))
Convey("GetMultikeyExpired", testWrapper(testGetMuliKeysExpired))
Convey("DeleteExpired", testWrapper(testDeleteExpired))
}
func testInsertGet(t *testing.T, revCache TestableRevCache) {
sr, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia110, ifId15), infra.NullSigner)
xtest.FailOnErr(t, err)
ctx, cancelF := context.WithTimeout(context.Background(), TimeOut)
defer cancelF()
inserted, err := revCache.Insert(ctx, sr)
SoMsg("Insert should return true for a new entry", inserted, ShouldBeTrue)
SoMsg("Insert a new entry should not err", err, ShouldBeNil)
key1 := *revcache.NewKey(ia110, ifId15)
revs, err := revCache.Get(ctx, revcache.KeySet{key1: {}})
SoMsg("Get should not err for existing entry", err, ShouldBeNil)
SoMsg("Get should return existing entry", revs, ShouldNotBeEmpty)
SoMsg("Get should return previously inserted value", revs[key1], ShouldResemble, sr)
MustParseRevInfo(t, revs[key1])
inserted, err = revCache.Insert(ctx, sr)
SoMsg("Insert should return false for already existing entry", inserted, ShouldBeFalse)
SoMsg("Insert should not err", err, ShouldBeNil)
revs, err = revCache.Get(ctx, revcache.SingleKey(ia110, ifId19))
SoMsg("Get should not err", err, ShouldBeNil)
SoMsg("Get should return empty result for not present value", revs, ShouldBeEmpty)
}
func testGetMultikey(t *testing.T, revCache TestableRevCache) {
sr1, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia110, ifId15), infra.NullSigner)
xtest.FailOnErr(t, err)
sr2, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia110, ifId19), infra.NullSigner)
xtest.FailOnErr(t, err)
sr3, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia120, ifId15), infra.NullSigner)
xtest.FailOnErr(t, err)
sr4, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia120, common.IFIDType(10)),
infra.NullSigner)
xtest.FailOnErr(t, err)
ctx, cancelF := context.WithTimeout(context.Background(), TimeOut)
defer cancelF()
// First test the empty cache
revs, err := revCache.Get(ctx, revcache.KeySet{})
SoMsg("Get should not err", err, ShouldBeNil)
SoMsg("Should return no revs", revs, ShouldBeEmpty)
_, err = revCache.Insert(ctx, sr1)
xtest.FailOnErr(t, err)
_, err = revCache.Insert(ctx, sr2)
xtest.FailOnErr(t, err)
_, err = revCache.Insert(ctx, sr3)
xtest.FailOnErr(t, err)
_, err = revCache.Insert(ctx, sr4)
xtest.FailOnErr(t, err)
key1 := *revcache.NewKey(ia110, ifId15)
revs, err = revCache.Get(ctx, revcache.KeySet{key1: {}})
SoMsg("Get should not err", err, ShouldBeNil)
SoMsg("Should contain one rev", 1, ShouldEqual, len(revs))
MustParseRevInfo(t, revs[key1])
SoMsg("Get should return revs for the given keys", revs, ShouldResemble,
revcache.Revocations{key1: sr1})
key2 := *revcache.NewKey(ia110, ifId19)
key3 := *revcache.NewKey(ia120, ifId15)
key4 := *revcache.NewKey(ia120, ifId19) // not the key of sr4
searchKeys := revcache.KeySet{key1: {}, key2: {}, key3: {}, key4: {}}
revs, err = revCache.Get(ctx, searchKeys)
SoMsg("Get should not err", err, ShouldBeNil)
loadRevInfos(t, revs)
expectedResult := revcache.Revocations{
key1: sr1, key2: sr2, key3: sr3,
}
SoMsg("Get should return the requested revocations", revs, ShouldResemble,
expectedResult)
}
func testGetAll(t *testing.T, revCache TestableRevCache) {
ctx, cancelF := context.WithTimeout(context.Background(), TimeOut)
defer cancelF()
// Empty cache should return an empty chan
resChan, err := revCache.GetAll(ctx)
SoMsg("No error expected", err, ShouldBeNil)
res, more := <-resChan
SoMsg("No result expected", res, ShouldResemble, revcache.RevOrErr{})
SoMsg("No more entries expected", more, ShouldBeFalse)
// Insert some stuff and query again
sr1, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia110, ifId15), infra.NullSigner)
xtest.FailOnErr(t, err)
sr2, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia110, ifId19), infra.NullSigner)
xtest.FailOnErr(t, err)
sr3, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia120, ifId15), infra.NullSigner)
xtest.FailOnErr(t, err)
sr4, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia120, common.IFIDType(20)),
infra.NullSigner)
xtest.FailOnErr(t, err)
_, err = revCache.Insert(ctx, sr1)
xtest.FailOnErr(t, err)
_, err = revCache.Insert(ctx, sr2)
xtest.FailOnErr(t, err)
_, err = revCache.Insert(ctx, sr3)
xtest.FailOnErr(t, err)
_, err = revCache.Insert(ctx, sr4)
xtest.FailOnErr(t, err)
expectedRevs := []*path_mgmt.SignedRevInfo{sr1, sr2, sr3, sr4}
resChan, err = revCache.GetAll(ctx)
SoMsg("No error expected", err, ShouldBeNil)
revs := make([]*path_mgmt.SignedRevInfo, 0, len(expectedRevs))
for res := range resChan {
SoMsg("No error expected", res.Err, ShouldBeNil)
SoMsg("Revocation expected", res.Rev, ShouldNotBeNil)
revs = append(revs, res.Rev)
}
// we don't care about the order, so sort here to make sure the comparison always works.
sort.Slice(revs, func(i, j int) bool {
iInfo, err := revs[i].RevInfo()
xtest.FailOnErr(t, err)
jInfo, err := revs[j].RevInfo()
xtest.FailOnErr(t, err)
return iInfo.IA().IAInt() < jInfo.IA().IAInt() ||
(iInfo.IA().IAInt() == jInfo.IA().IAInt() && iInfo.IfID < jInfo.IfID)
})
SoMsg("All revocations should have been returned", revs, ShouldResemble, expectedRevs)
}
func testGetAllExpired(t *testing.T, revCache TestableRevCache) {
ctx, cancelF := context.WithTimeout(context.Background(), TimeOut)
defer cancelF()
// insert expired rev
srNew, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: ifId15,
RawIsdas: ia110.IAInt(),
LinkType: proto.LinkType_core,
RawTimestamp: util.TimeToSecs(time.Now().Add(-2 * time.Second)),
RawTTL: 1,
}, infra.NullSigner)
xtest.FailOnErr(t, err)
revCache.InsertExpired(t, ctx, srNew)
// Now test that we don't get the expired rev
resChan, err := revCache.GetAll(ctx)
SoMsg("No error expected", err, ShouldBeNil)
res, more := <-resChan
SoMsg("No result expected", res, ShouldResemble, revcache.RevOrErr{})
SoMsg("No more entries expected", more, ShouldBeFalse)
}
func testInsertExpired(t *testing.T, revCache TestableRevCache) {
r := &path_mgmt.RevInfo{
IfID: ifId15,
RawIsdas: ia110.IAInt(),
LinkType: proto.LinkType_core,
RawTimestamp: util.TimeToSecs(time.Now().Add(-15 * time.Second)),
RawTTL: uint32((time.Duration(10) * time.Second).Seconds()),
}
ctx, cancelF := context.WithTimeout(context.Background(), TimeOut)
defer cancelF()
srev, err := path_mgmt.NewSignedRevInfo(r, infra.NullSigner)
xtest.FailOnErr(t, err)
inserted, err := revCache.Insert(ctx, srev)
SoMsg("Insert should return false for expired rev", inserted, ShouldBeFalse)
SoMsg("Insert should not err", err, ShouldBeNil)
}
func testInsertNewer(t *testing.T, revCache TestableRevCache) {
sr, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia110, ifId15), infra.NullSigner)
xtest.FailOnErr(t, err)
ctx, cancelF := context.WithTimeout(context.Background(), TimeOut)
defer cancelF()
_, err = revCache.Insert(ctx, sr)
xtest.FailOnErr(t, err)
srNew, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: ifId15,
RawIsdas: ia110.IAInt(),
LinkType: proto.LinkType_core,
RawTimestamp: util.TimeToSecs(time.Now().Add(10 * time.Second)),
RawTTL: uint32((time.Duration(10) * time.Second).Seconds()),
}, infra.NullSigner)
xtest.FailOnErr(t, err)
inserted, err := revCache.Insert(ctx, srNew)
SoMsg("Insert should return true for a new entry", inserted, ShouldBeTrue)
SoMsg("Insert a new entry should not err", err, ShouldBeNil)
key1 := *revcache.NewKey(ia110, ifId15)
revs, err := revCache.Get(ctx, revcache.KeySet{key1: {}})
loadRevInfos(t, revs)
SoMsg("Get should not err for existing entry", err, ShouldBeNil)
SoMsg("Get should return non empty map for inserted value", revs, ShouldNotBeEmpty)
SoMsg("Get should return previously inserted value", revs[key1], ShouldResemble, srNew)
MustParseRevInfo(t, revs[key1])
}
func testGetExpired(t *testing.T, revCache TestableRevCache) {
ctx, cancelF := context.WithTimeout(context.Background(), TimeOut)
defer cancelF()
srNew, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: ifId15,
RawIsdas: ia110.IAInt(),
LinkType: proto.LinkType_core,
RawTimestamp: util.TimeToSecs(time.Now().Add(-2 * time.Second)),
RawTTL: 1,
}, infra.NullSigner)
xtest.FailOnErr(t, err)
revCache.InsertExpired(t, ctx, srNew)
revs, err := revCache.Get(ctx, revcache.SingleKey(ia110, ifId15))
SoMsg("Expired entry should not be returned", revs, ShouldBeEmpty)
SoMsg("Should not error for expired entry", err, ShouldBeNil)
}
func testGetMuliKeysExpired(t *testing.T, revCache TestableRevCache) {
ctx, cancelF := context.WithTimeout(context.Background(), TimeOut)
defer cancelF()
srNew, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: ifId15,
RawIsdas: ia110.IAInt(),
LinkType: proto.LinkType_core,
RawTimestamp: util.TimeToSecs(time.Now().Add(-2 * time.Second)),
RawTTL: 1,
}, infra.NullSigner)
xtest.FailOnErr(t, err)
revCache.InsertExpired(t, ctx, srNew)
sr110_19, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia110, ifId19), infra.NullSigner)
xtest.FailOnErr(t, err)
revCache.Insert(ctx, sr110_19)
validKey := *revcache.NewKey(ia110, ifId19)
srCache, err := revCache.Get(ctx, revcache.KeySet{
*revcache.NewKey(ia110, ifId15): {},
validKey: {},
})
SoMsg("Should not error for expired entry", err, ShouldBeNil)
loadRevInfos(t, srCache)
SoMsg("Expired entry should not be returned", srCache, ShouldResemble,
revcache.Revocations{validKey: sr110_19})
}
func testDeleteExpired(t *testing.T, revCache TestableRevCache) {
ctx, cancelF := context.WithTimeout(context.Background(), TimeOut)
defer cancelF()
del, err := revCache.DeleteExpired(ctx)
SoMsg("DeleteExpired on empty should not error", err, ShouldBeNil)
SoMsg("DeleteExpired on empty should delete 0", del, ShouldEqual, 0)
sr110_19, err := path_mgmt.NewSignedRevInfo(defaultRevInfo(ia110, ifId19), infra.NullSigner)
xtest.FailOnErr(t, err)
revCache.Insert(ctx, sr110_19)
del, err = revCache.DeleteExpired(ctx)
SoMsg("DeleteExpired should not error", err, ShouldBeNil)
SoMsg("DeleteExpired should delete 0 if entry is not expired", del, ShouldEqual, 0)
srNew, err := path_mgmt.NewSignedRevInfo(&path_mgmt.RevInfo{
IfID: ifId15,
RawIsdas: ia110.IAInt(),
LinkType: proto.LinkType_core,
RawTimestamp: util.TimeToSecs(time.Now().Add(-2 * time.Second)),
RawTTL: 1,
}, infra.NullSigner)
xtest.FailOnErr(t, err)
revCache.InsertExpired(t, ctx, srNew)
del, err = revCache.DeleteExpired(ctx)
SoMsg("DeleteExpired should not error", err, ShouldBeNil)
SoMsg("DeleteExpired should delete 1 if entry is expired", del, ShouldEqual, 1)
del, err = revCache.DeleteExpired(ctx)
SoMsg("DeleteExpired should not error", err, ShouldBeNil)
SoMsg("DeleteExpired should delete 0 if entry is not expired", del, ShouldEqual, 0)
}
func defaultRevInfo(ia addr.IA, ifId common.IFIDType) *path_mgmt.RevInfo {
return &path_mgmt.RevInfo{
IfID: ifId,
RawIsdas: ia.IAInt(),
LinkType: proto.LinkType_core,
RawTimestamp: util.TimeToSecs(time.Now()),
RawTTL: uint32((time.Duration(10) * time.Second).Seconds()),
}
}
func MustParseRevInfo(t *testing.T, sr *path_mgmt.SignedRevInfo) {
_, err := sr.RevInfo()
xtest.FailOnErr(t, err)
}
func loadRevInfos(t *testing.T, revs revcache.Revocations) {
t.Helper()
for k := range revs {
// init revInfo so that comparison works.
_, err := revs[k].RevInfo()
xtest.FailOnErr(t, err)
}
}