-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrislive_test.go
644 lines (609 loc) · 17.9 KB
/
rislive_test.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/golang/protobuf/proto"
"github.com/google/go-cmp/cmp"
)
var (
msg01 = &RisMessageData{Path: []interface{}{1, 2, 3, 4, 5, 6, 7, 8}, Origin: "8"}
msg02 = &RisMessageData{Path: []interface{}{1}, Origin: "1"}
msg03 = &RisMessageData{Path: []interface{}{1, 3, 4, 5, 6, 7, 8}, Origin: "8"}
msg04 = &RisMessageData{Path: []interface{}{1, 3, 2, 4, 5, 6, 7, 8}, Origin: "8"}
msg05 = &RisMessageData{Path: []interface{}{"An", "ASN", "LIST", "HERE"}, Origin: "9"}
msg06 = &RisMessageData{Path: []interface{}{1, 2, 3, []string{"6"}}, Origin: "9"}
)
func TestDigestPath(t *testing.T) {
tests := []struct {
desc string
msg *RisMessageData
want []int32
wantErr bool
}{{
desc: "Success decode",
msg: msg01,
want: []int32{1, 2, 3, 4, 5, 6, 7, 8},
}, {
desc: "Error, path is words",
msg: msg05,
wantErr: true,
}, {
desc: "Error, path interface slice is not slice",
msg: msg06,
wantErr: true,
}}
for _, test := range tests {
err := digestPath(test.msg)
switch {
case err != nil && !test.wantErr:
t.Errorf("[%v]: got error when not expecting: %v", test.desc, err)
case err == nil && test.wantErr:
t.Errorf("[%v]: did not get error when expecting one", test.desc)
case err == nil:
if !cmp.Equal(test.msg.DigestedPath, test.want) {
t.Errorf("[%v]: got/want mismatch:\n%v\n", test.desc, cmp.Diff(test.msg.DigestedPath, test.want))
}
}
}
}
func TestNewRisFilter(t *testing.T) {
tests := []struct {
desc string
aspath []int32
transits map[int32]bool
origins, prefix []string
want *RisFilter
}{{
desc: "Success NewRisFilter",
aspath: []int32{1, 2, 3},
transits: map[int32]bool{1: true, 2: true},
origins: []string{"1", "2"},
prefix: []string{"192.168.1.0/24", "10.1.0.0/16"},
want: &RisFilter{
ASPath: []int32{1, 2, 3},
InvalidTransitAS: map[int32]bool{1: true, 2: true},
Origins: []string{"1", "2"},
Prefix: []string{"192.168.1.0/24", "10.1.0.0/16"},
},
}}
for _, test := range tests {
got := NewRisFilter(test.aspath, test.transits, test.origins, test.prefix)
if !cmp.Equal(got, test.want) {
t.Errorf("[%v]: got/want mismatch diff(-got, +want):\n%v\n", test.desc, cmp.Diff(got, test.want))
}
}
}
func TestNewRisLive(t *testing.T) {
tests := []struct {
desc string
url, ua string
file *string
rf RisFilter
buffer int
want *RisLive
}{{
desc: "Success - nil file",
url: "http://blah",
file: nil,
ua: "foo",
rf: RisFilter{ASPath: []int32{1}},
buffer: 10,
want: &RisLive{
URL: proto.String("http://blah"),
UA: proto.String("foo"),
Filter: &RisFilter{ASPath: []int32{1}},
Chan: make(chan (RisMessage), 10),
},
}}
for _, test := range tests {
got := NewRisLive(&test.url, test.file, &test.ua, &test.rf, &test.buffer)
if !cmp.Equal(got.URL, test.want.URL) && !cmp.Equal(got.UA, test.want.UA) {
t.Errorf("[%v]: got/want mismatch, diff (-got, +want):\n%v\n", test.desc, cmp.Diff(got, test.want))
}
}
}
func TestMatchPrefix(t *testing.T) {
// Example/test announcements.
p4 := &RisAnnouncement{
NextHop: "1.2.3.4",
Prefixes: []string{"192.168.0.0/16", "10.0.0.0/24"},
}
p6 := &RisAnnouncement{
NextHop: "2001:db8:123::1",
Prefixes: []string{"2001:db8::/32", "2001:db8:48::/48"},
}
tests := []struct {
desc string
ann *RisAnnouncement
candidates []string
want bool
}{{
desc: "Success v4",
ann: p4,
candidates: []string{"192.168.0.0/16", "100.64.0.0/10"},
want: true,
}, {
desc: "Success v6",
ann: p6,
candidates: []string{"2001:db8:32::/32", "2001:db8:48::/48"},
want: true,
}, {
desc: "Success v4 match in mixed family",
ann: p6,
candidates: []string{"192.169.0.0/16", "2001:db8:48::/48"},
want: true,
}, {
desc: "Success v6 match in mixed family",
ann: p6,
candidates: []string{"2001:db8::/32", "192.169.0.0/16"},
want: true,
}, {
desc: "Failure v4",
ann: p4,
candidates: []string{"197.168.0.0/16", "10.64.0.0/10"},
want: false,
}, {
desc: "Failure v6",
ann: p6,
candidates: []string{"2001:db8:32::/32", "2001:db9:48::/48"},
want: false,
}, {
desc: "Failure v4 with v6 mach",
candidates: []string{"2001:db8:32::/32", "2001:db8:48::/48"},
ann: p4,
want: false,
}, {
desc: "Failure v6 with v4 match",
ann: p6,
candidates: []string{"192.168.0.0/16", "100.64.0.0/10"},
want: false,
}}
for _, test := range tests {
got := test.ann.MatchPrefix(test.candidates)
if got != test.want {
t.Errorf("[%v]: got/want mismatch, got(%v) / want(%v)", test.desc, got, test.want)
}
}
}
func TestMatchASPath(t *testing.T) {
tests := []struct {
desc string
msg *RisMessageData
candidates []int32
want bool
}{{
desc: "Success find len(1) path",
msg: msg01,
candidates: []int32{3},
want: true,
}, {
desc: "Fail can not find len(1) path",
msg: msg01,
candidates: []int32{10},
want: false,
}, {
desc: "Success can find len(2) path",
msg: msg01,
candidates: []int32{3, 4},
want: true,
}, {
desc: "Success can find len(3) path",
msg: msg01,
candidates: []int32{3, 4, 5},
want: true,
}, {
desc: "Success candidate path too long",
msg: msg02,
candidates: []int32{3, 4, 5},
want: false,
}, {
desc: "Success candidate path not in mesg",
msg: msg03,
candidates: []int32{2, 3, 4},
want: false,
}, {
desc: "Success candidate path in wrong order from mesg",
msg: msg04,
candidates: []int32{2, 3, 4},
want: false,
}}
for _, test := range tests {
err := digestPath(test.msg)
if err != nil {
t.Errorf("[%v]: failed to digest path elements: %v", test.desc, err)
}
got := test.msg.MatchASPath(test.candidates)
if got != test.want {
t.Errorf("[%v]: got/want mismatch, got(%v) / want(%v)", test.desc, got, test.want)
}
}
}
func TestInvalidTransitAS(t *testing.T) {
tests := []struct {
desc string
msg *RisMessageData
candidates map[int32]bool
want bool
}{{
desc: "Success - AS4 in transit position",
msg: msg01,
candidates: map[int32]bool{4: true, 14: true, 0: true},
want: true,
}, {
desc: "Success - AS10 not in transit position",
msg: msg01,
candidates: map[int32]bool{10: true, 14: true, 0: true},
want: true,
}}
for _, test := range tests {
got := test.msg.InvalidTransitAS(test.candidates)
if got != test.want {
}
}
}
func TestCheckASPath(t *testing.T) {
tests := []struct {
desc string
rl *RisLive
data *RisMessageData
want bool
}{{
desc: "Success - second element",
rl: &RisLive{Filter: &RisFilter{ASPath: []int32{57695, 12}}},
data: &RisMessageData{Path: []interface{}{float64(57695), float64(12), float64(2332)}},
want: true,
}, {
desc: "Success - zero matches",
rl: &RisLive{Filter: &RisFilter{ASPath: []int32{57695, 12}}},
data: &RisMessageData{Path: []interface{}{float64(57695), float64(128), float64(2332)}},
want: false,
}, {
desc: "Success - zero to match",
rl: &RisLive{Filter: &RisFilter{ASPath: []int32{}}},
data: &RisMessageData{Path: []interface{}{float64(5769), float64(128), float64(2332)}},
want: true,
}}
for _, test := range tests {
err := digestPath(test.data)
if err != nil {
t.Errorf("[%v]: failed to digest path elements: %v", test.desc, err)
}
got := test.rl.CheckASPath(test.data)
if got != test.want {
t.Errorf("[%v]: got/want mismatch, wanted: %v got: %v", test.desc, test.want, got)
}
}
}
func TestCheckOrigins(t *testing.T) {
tests := []struct {
desc string
msg *RisMessageData
candidates []string
want bool
}{{
desc: "Success found single check: 8",
msg: msg01,
candidates: []string{"8"},
want: true,
}, {
desc: "Success found double check: 8",
msg: msg01,
candidates: []string{"4", "8"},
want: true,
}, {
desc: "Failure not found single check: 4",
msg: msg01,
candidates: []string{"4"},
want: false,
}, {
desc: "Failure not found double check: 4",
msg: msg01,
candidates: []string{"4", "5"},
want: false,
}}
for _, test := range tests {
got := test.msg.CheckOrigins(test.candidates)
if got != test.want {
t.Errorf("[%v]: got/want mismatch got: %v want: %v", test.desc, got, test.want)
}
}
}
func TestCheckInvalidTransitAS(t *testing.T) {
tests := []struct {
desc string
rl *RisLive
msg *RisMessageData
want bool
}{{
desc: "Success - Transit-AS found",
rl: &RisLive{Filter: &RisFilter{InvalidTransitAS: map[int32]bool{32: true, 1: true}}},
msg: &RisMessageData{Path: []interface{}{12, 701, 1, 4}},
want: true,
}, {
desc: "Success - Transit-AS not found",
rl: &RisLive{Filter: &RisFilter{InvalidTransitAS: map[int32]bool{32: true, 1: true}}},
msg: &RisMessageData{Path: []interface{}{12, 701, 5, 4}},
want: false,
}, {
desc: "Success - InvalidTransitAS is zero length - false return",
rl: &RisLive{Filter: &RisFilter{InvalidTransitAS: map[int32]bool{}}},
msg: &RisMessageData{Path: []interface{}{12, 701, 5, 4}},
want: false,
}}
for _, test := range tests {
err := digestPath(test.msg)
if err != nil {
t.Errorf("[%v]: failed to digest path elements: %v", test.desc, err)
}
got := test.rl.CheckInvalidTransitAS(test.msg)
if got != test.want {
t.Errorf("[%v]: got(%v)/want(%v) mismatch", test.desc, got, test.want)
}
}
}
// Because there are CheckOrigins in both the RisLive and RisMessageData bits.
func TestCheckOriginsRisLive(t *testing.T) {
tests := []struct {
desc string
rl *RisLive
msg *RisMessageData
want bool
}{{
desc: "Success - Origin Match",
rl: &RisLive{Filter: &RisFilter{Origins: []string{"1", "701", "7018"}}},
msg: &RisMessageData{Origin: "701"},
want: true,
}, {
desc: "Success - Origins not found - false match",
rl: &RisLive{Filter: &RisFilter{Origins: []string{"1", "7018", "3356"}}},
msg: &RisMessageData{Origin: "701"},
want: false,
}, {
desc: "Success - Origins zero length - false match",
rl: &RisLive{Filter: &RisFilter{Origins: []string{}}},
msg: &RisMessageData{Origin: "701"},
want: false,
}}
for _, test := range tests {
got := test.rl.CheckOrigins(test.msg)
if got != test.want {
t.Errorf("[%v]: got(%v)/want(%v) mismatch", test.desc, got, test.want)
}
}
}
func TestCheckPrefix(t *testing.T) {
tests := []struct {
desc string
rm *RisMessageData
rl *RisLive
want bool
}{{
desc: "Simple prefix match",
rm: &RisMessageData{
Announcements: []*RisAnnouncement{
&RisAnnouncement{
Prefixes: []string{"192.168.0.0/16"},
},
},
},
rl: &RisLive{Filter: &RisFilter{Prefix: []string{"192.168.0.0/16"}}},
want: true,
}, {
desc: "Match a subnet announcement",
rm: &RisMessageData{
Announcements: []*RisAnnouncement{
&RisAnnouncement{
Prefixes: []string{"192.168.0.0/24"},
},
},
},
rl: &RisLive{Filter: &RisFilter{Prefix: []string{"192.168.0.0/16"}}},
want: true,
}, {
desc: "RisLive data is improper",
rm: &RisMessageData{
Announcements: []*RisAnnouncement{
&RisAnnouncement{
Prefixes: []string{"192.168.0.0/24"},
},
},
},
rl: &RisLive{Filter: &RisFilter{Prefix: []string{"192.b.0.0/16"}}},
want: false,
}, {
desc: "RisMessageData is improper",
rm: &RisMessageData{
Announcements: []*RisAnnouncement{
&RisAnnouncement{
Prefixes: []string{"192.b.0.0/24"},
},
},
},
rl: &RisLive{Filter: &RisFilter{Prefix: []string{"192.168.0.0/16"}}},
want: false,
}}
for _, test := range tests {
got := test.rl.CheckPrefix(test.rm)
if got != test.want {
t.Errorf("[%v]: got/want mismatch: got %v wanted %v", test.desc, got, test.want)
}
}
}
func testServer(f string) *httptest.Server {
fd, err := ioutil.ReadFile(f)
if err != nil {
return nil
}
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, string(fd))
}))
}
func TestListen(t *testing.T) {
tests := []struct {
desc string
file *string
remote bool
recNum int
want RisMessage
}{{
desc: "Successful read of 1 message",
file: proto.String("testdata/1-msg"),
recNum: 0,
want: RisMessage{
Type: "ris_message",
Data: &RisMessageData{
Timestamp: 1.55862004708e+09,
Peer: "196.60.9.165",
PeerASN: "57695",
ID: "196.60.9.165-1558620047.08-11924763",
Host: "rrc19",
Type: "UPDATE",
Path: []interface{}{float64(57695), float64(37650)},
Community: [][]int32{{57695, 12000}, {57695, 12001}},
Origin: "igp",
DigestedPath: []int32{int32(57695), int32(37650)},
Announcements: []*RisAnnouncement{
&RisAnnouncement{
NextHop: "196.60.9.165",
Prefixes: []string{"196.50.70.0/24"}}},
Raw: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003E02000000234001010040020A02020000E15F00009312400304C43C09A5E00808E15F2EE0E15F2EE118C43246",
}},
}, {
desc: "Successfully read 1 http msg",
file: proto.String("testdata/1-msg"),
remote: true,
recNum: 0,
want: RisMessage{
Type: "ris_message",
Data: &RisMessageData{
Timestamp: 1.55862004708e+09,
Peer: "196.60.9.165",
PeerASN: "57695",
ID: "196.60.9.165-1558620047.08-11924763",
Host: "rrc19",
Type: "UPDATE",
Path: []interface{}{float64(57695), float64(37650)},
Community: [][]int32{{57695, 12000}, {57695, 12001}},
Origin: "igp",
DigestedPath: []int32{int32(57695), int32(37650)},
Announcements: []*RisAnnouncement{
&RisAnnouncement{
NextHop: "196.60.9.165",
Prefixes: []string{"196.50.70.0/24"}}},
Raw: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003E02000000234001010040020A02020000E15F00009312400304C43C09A5E00808E15F2EE0E15F2EE118C43246",
}},
}, {
desc: "Successful read of 6th message",
file: proto.String("testdata/10-msg"),
recNum: 5,
want: RisMessage{
Type: "ris_message",
Data: &RisMessageData{
Timestamp: 1.55862004706e+09,
Peer: "2001:7f8:d:ff::226",
PeerASN: "24482",
ID: "2001:7f8:d:ff::226-1558620047.06-51675230",
Host: "rrc07",
Type: "UPDATE",
Path: []interface{}{float64(24482), float64(6453), float64(174), float64(513), float64(513), float64(12654)},
Community: [][]int32{{6453, 86}, {6453, 1000}, {6453, 1400}, {6453, 1402}, {6453, 2000}, {6453, 4000}, {24482, 1}, {24482, 12020}, {24482, 12021}, {24482, 20200}, {24482, 20300}, {24482, 64601}},
Origin: "igp",
DigestedPath: []int32{int32(24482), int32(6453), int32(174), int32(513), int32(513), int32(12654)},
Announcements: []*RisAnnouncement{
&RisAnnouncement{
NextHop: "2001:7f8:d:ff::226",
Prefixes: []string{"2001:7fb:fe04::/48"},
},
&RisAnnouncement{
NextHop: "fe80::2a0:a500:0:3e6",
Prefixes: []string{"2001:7fb:fe04::/48"},
},
},
Raw: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00AD02000000964001010040021A020600005FA200001935000000AE00000201000002010000316E800404000007D4C007080000FD090A1DA9C0C0083019350056193503E8193505781935057A193507D019350FA05FA200015FA22EF45FA22EF55FA24EE85FA24F4C5FA2FC59900E002C00020120200107F8000D00FF0000000000000226FE8000000000000002A0A500000003E60030200107FBFE04"},
},
}, {
desc: "Fail reading an as-set in path",
file: proto.String("testdata/fail-as-set"),
recNum: 0,
want: RisMessage{
Type: "ris_message",
Data: &RisMessageData{
Timestamp: 1.57383086172e+09,
Peer: "2001:504:1::a500:2497:1",
PeerASN: "2497",
ID: "11-2001-504-1-a500-2497-1-439516",
Host: "rrc11",
Type: "UPDATE",
Path: []interface{}{float64(2497), float64(6453), float64(18705), float64(26281), []interface{}{float64(13340)}},
DigestedPath: []int32{int32(2497), int32(6453), int32(18705), int32(26281), int32(13340)},
Origin: "incomplete",
Announcements: []*RisAnnouncement{
&RisAnnouncement{
NextHop: "2001:504:1::a500:2497:1",
Prefixes: []string{"2607:ffc0:1000::/36"}},
&RisAnnouncement{
NextHop: "fe80::86c1:c1ff:fe7d:6298",
Prefixes: []string{"2607:ffc0:1000::/36"},
},
},
Raw: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00700200000059400101024002180204000009C10000193500004911000066A901010000341CC00708000066A90A010021900E002B0002012020010504000100000000A50024970001FE8000000000000086C1C1FFFE7D629800242607FFC010"},
},
}}
for _, test := range tests {
f := test.file
if test.remote {
f = proto.String("")
}
r := &RisLive{
File: f,
Filter: &RisFilter{},
Chan: make(chan RisMessage, 10),
}
if test.remote {
ts := testServer(*test.file)
r.URL = &ts.URL
r.UA = proto.String("")
}
go r.Listen()
for x := 0; x < test.recNum; x++ {
_ = <-r.Chan
}
got := <-r.Chan
if !cmp.Equal(got, test.want) {
t.Errorf("[%v]: got/want differ(+got/-want):\n%v\n", test.desc, cmp.Diff(got, test.want))
}
}
}
func TestGet(t *testing.T) {
tests := []struct {
desc string
file string
filter *RisFilter
want string
}{{
desc: "Success simple filter: prefix",
filter: &RisFilter{
Prefix: []string{"196.50.70.0/24"},
ASPath: []int32{int32(57695)},
Origins: []string{"37650"},
InvalidTransitAS: map[int32]bool{int32(57695): true},
},
file: "testdata/1-msg",
want: "Done",
}}
for _, test := range tests {
r := &RisLive{
File: proto.String(test.file),
Filter: test.filter,
Chan: make(chan RisMessage, 10),
}
go r.Listen()
got := r.Get(r.Filter)
if !cmp.Equal(got, test.want) {
t.Errorf("[%v]: got/want mismatch:\n%v\n", test.desc, cmp.Diff(got, test.want))
}
}
}