Newer
Older
"github.com/stretchr/testify/assert"
"github.com/taktv6/tflow2/convert"
bnet "github.com/bio-routing/bio-rd/net"
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
)
type test struct {
testNum int
input []byte
wantFail bool
expected interface{}
}
type decodeFunc func(*bytes.Buffer) (interface{}, error)
func BenchmarkDecodeUpdateMsg(b *testing.B) {
input := []byte{0, 5, 8, 10, 16, 192, 168,
0, 53, // Total Path Attribute Length
255, // Attribute flags
1, // Attribute Type code (ORIGIN)
0, 1, // Length
2, // INCOMPLETE
0, // Attribute flags
2, // Attribute Type code (AS Path)
12, // Length
2, // Type = AS_SEQUENCE
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
1, // Type = AS_SET
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
0, // Attribute flags
3, // Attribute Type code (Next Hop)
4, // Length
10, 11, 12, 13, // Next Hop
0, // Attribute flags
4, // Attribute Type code (MED)
4, // Length
0, 0, 1, 0, // MED 256
0, // Attribute flags
5, // Attribute Type code (Local Pref)
4, // Length
0, 0, 1, 0, // Local Pref 256
0, // Attribute flags
6, // Attribute Type code (Atomic Aggregate)
0, // Length
0, // Attribute flags
7, // Attribute Type code (Atomic Aggregate)
6, // Length
1, 2, // ASN
10, 11, 12, 13, // Address
8, 11, // 11.0.0.0/8
}
for i := 0; i < b.N; i++ {
buf := bytes.NewBuffer(input)
_, err := decodeUpdateMsg(buf, uint16(len(input)), &DecodeOptions{})
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
if err != nil {
fmt.Printf("decodeUpdateMsg failed: %v\n", err)
}
//buf.Next(1)
}
}
func TestDecode(t *testing.T) {
tests := []test{
{
// Proper packet
testNum: 1,
input: []byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // Marker
0, 19, // Length
4, // Type = Keepalive
},
wantFail: false,
expected: &BGPMessage{
Header: &BGPHeader{
Length: 19,
Type: 4,
},
},
},
{
// Invalid marker
testNum: 2,
input: []byte{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, // Marker
0, 19, // Length
4, // Type = Keepalive
},
wantFail: true,
expected: &BGPMessage{
Header: &BGPHeader{
Length: 19,
Type: 4,
},
},
},
{
// Proper NOTIFICATION packet
testNum: 3,
input: []byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // Marker
0, 21, // Length
3, // Type = Notification
1, 1, // Message Header Error, Connection Not Synchronized.
},
wantFail: false,
expected: &BGPMessage{
Header: &BGPHeader{
Length: 21,
Type: 3,
},
Body: &BGPNotification{
ErrorCode: 1,
ErrorSubcode: 1,
},
},
},
{
// Proper OPEN packet
testNum: 4,
input: []byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // Marker
0, 29, // Length
1, // Type = Open
4, // Version
0, 200, //ASN,
0, 15, // Holdtime
10, 20, 30, 40, // BGP Identifier
0, // Opt Parm Len
},
wantFail: false,
expected: &BGPMessage{
Header: &BGPHeader{
Length: 29,
Type: 1,
},
Body: &BGPOpen{
Version: 4,
HoldTime: 15,
BGPIdentifier: uint32(169090600),
OptParmLen: 0,
OptParams: []OptParam{},
},
},
},
{
// Incomplete OPEN packet
testNum: 5,
input: []byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // Marker
0, 28, // Length
1, // Type = Open
4, // Version
0, 200, //ASN,
0, 15, // Holdtime
0, 0, 0, 100, // BGP Identifier
},
wantFail: true,
expected: &BGPMessage{
Header: &BGPHeader{
Length: 28,
Type: 1,
},
Body: &BGPOpen{
Version: 4,
HoldTime: 15,
BGPIdentifier: uint32(100),
},
},
},
{
testNum: 6,
input: []byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // Marker
0, 28, // Length
2, // Type = Update
0, 5, 8, 10, 16, 192, 168, 0, 0, // 2 withdraws
},
wantFail: false,
expected: &BGPMessage{
Header: &BGPHeader{
Length: 28,
Type: 2,
},
Body: &BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &NLRI{
Pfxlen: 16,
},
},
},
},
},
{
testNum: 7,
input: []byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // Marker
0, 28, // Length
5, // Type = Invalid
0, 5, 8, 10, 16, 192, 168, 0, 0, // Some more stuff
},
wantFail: true,
},
{
testNum: 8,
input: []byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
0, 21,
3,
6, 4,
},
wantFail: false,
expected: &BGPMessage{
Header: &BGPHeader{
Length: 21,
Type: 3,
},
Body: &BGPNotification{
ErrorCode: 6,
ErrorSubcode: 4,
},
},
},
}
for _, test := range tests {
buf := bytes.NewBuffer(test.input)
msg, err := Decode(buf, &DecodeOptions{})
if err != nil && !test.wantFail {
t.Errorf("Unexpected error in test %d: %v", test.testNum, err)
continue
}
if err == nil && test.wantFail {
t.Errorf("Expected error did not happen in test %d", test.testNum)
continue
}
if err != nil && test.wantFail {
continue
}
if msg == nil {
t.Errorf("Unexpected nil result in test %d. Expected: %v", test.testNum, test.expected)
continue
}
assert.Equalf(t, test.expected, msg, "Test: %d", test.testNum)
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
}
}
func TestDecodeNotificationMsg(t *testing.T) {
tests := []struct {
name string
input []byte
wantFail bool
expected interface{}
}{
{
name: "Invalid error code",
input: []byte{0, 0},
wantFail: true,
},
{
name: "Invalid error code #2",
input: []byte{7, 0},
wantFail: true,
},
{
name: "Invalid ErrSubCode (Header)",
input: []byte{1, 0},
wantFail: true,
},
{
name: "Invalid ErrSubCode (Header) #2",
input: []byte{1, 4},
wantFail: true,
},
{
name: "Invalid ErrSubCode (Open)",
input: []byte{2, 0},
wantFail: true,
},
{
name: "Invalid ErrSubCode (Open) #2",
input: []byte{2, 7},
wantFail: true,
},
{
name: "Invalid ErrSubCode (Open) #3",
input: []byte{2, 5},
wantFail: true,
},
{
name: "Invalid ErrSubCode (Update)",
input: []byte{3, 0},
wantFail: true,
},
{
name: "Invalid ErrSubCode (Update) #2",
input: []byte{3, 12},
wantFail: true,
},
{
name: "Invalid ErrSubCode (Update) #3",
input: []byte{3, 7},
wantFail: true,
},
{
name: "Valid Notification",
input: []byte{2, 2},
wantFail: false,
expected: &BGPNotification{
ErrorCode: 2,
ErrorSubcode: 2,
},
},
{
name: "Empty input",
input: []byte{},
wantFail: true,
},
{
name: "Hold Timer Expired",
input: []byte{4, 0},
wantFail: false,
expected: &BGPNotification{
ErrorCode: 4,
ErrorSubcode: 0,
},
},
{
name: "Hold Timer Expired (invalid subcode)",
input: []byte{4, 1},
wantFail: true,
},
{
name: "FSM Error",
input: []byte{5, 0},
wantFail: false,
expected: &BGPNotification{
ErrorCode: 5,
ErrorSubcode: 0,
},
},
{
name: "FSM Error (invalid subcode)",
input: []byte{5, 1},
wantFail: true,
},
{
name: "Cease",
input: []byte{6, 0},
wantFail: false,
expected: &BGPNotification{
ErrorCode: 6,
ErrorSubcode: 0,
},
},
{
name: "Cease (invalid subcode)",
input: []byte{6, 1},
wantFail: true,
},
}
for _, test := range tests {
res, err := decodeNotificationMsg(bytes.NewBuffer(test.input))
if test.wantFail {
if err != nil {
continue
}
t.Errorf("Expected error did not happen for test %q", test.name)
continue
}
if err != nil {
t.Errorf("Unexpected failure for test %q: %v", test.name, err)
continue
}
assert.Equal(t, test.expected, res)
}
}
func TestDecodeUpdateMsg(t *testing.T) {
tests := []struct {
testNum int
input []byte
explicitLength uint16
wantFail bool
expected interface{}
}{
{
// 2 withdraws only, valid update
testNum: 1,
input: []byte{0, 5, 8, 10, 16, 192, 168, 0, 0},
wantFail: false,
expected: &BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &NLRI{
Pfxlen: 16,
},
},
},
},
{
// 2 withdraws with one path attribute (ORIGIN), valid update
testNum: 2,
input: []byte{
0, 5, // Withdrawn Routes Length
8, 10, // 10.0.0.0/8
16, 192, 168, // 192.168.0.0/16
0, 5, // Total Path Attribute Length
255, // Attribute flags
1, // Attribute Type code
0, 1, // Length
2, // INCOMPLETE
},
wantFail: false,
expected: &BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &NLRI{
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
Pfxlen: 16,
},
},
TotalPathAttrLen: 5,
PathAttributes: &PathAttribute{
Optional: true,
Transitive: true,
Partial: true,
ExtendedLength: true,
Length: 1,
TypeCode: 1,
Value: uint8(2),
},
},
},
{
// 2 withdraws with two path attributes (ORIGIN + ASPath), valid update
testNum: 3,
input: []byte{0, 5, 8, 10, 16, 192, 168,
0, 14, // Total Path Attribute Length
255, // Attribute flags
1, // Attribute Type code (ORIGIN)
0, 1, // Length
2, // INCOMPLETE
0, // Attribute flags
2, // Attribute Type code (AS Path)
6, // Length
2, // Type = AS_SEQUENCE
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
},
wantFail: false,
expected: &BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &NLRI{
Pfxlen: 16,
},
},
TotalPathAttrLen: 14,
PathAttributes: &PathAttribute{
Optional: true,
Transitive: true,
Partial: true,
ExtendedLength: true,
Length: 1,
TypeCode: 1,
Value: uint8(2),
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 6,
TypeCode: 2,
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
ASNs: []uint32{
15169,
3320,
},
},
},
},
},
},
},
{
// 2 withdraws with two path attributes (ORIGIN + ASPath), invalid AS Path segment type
testNum: 4,
input: []byte{0, 5, 8, 10, 16, 192, 168,
0, 13, // Total Path Attribute Length
255, // Attribute flags
1, // Attribute Type code (ORIGIN)
0, 1, // Length
2, // INCOMPLETE
0, // Attribute flags
2, // Attribute Type code (AS Path)
6, // Length
1, // Type = AS_SET
0, // Path Segement Length
},
wantFail: true,
},
{
// 2 withdraws with two path attributes (ORIGIN + ASPath), invalid AS Path segment member count
testNum: 5,
input: []byte{0, 5, 8, 10, 16, 192, 168,
0, 13, // Total Path Attribute Length
255, // Attribute flags
1, // Attribute Type code (ORIGIN)
0, 1, // Length
2, // INCOMPLETE
0, // Attribute flags
2, // Attribute Type code (AS Path)
6, // Length
3, // Type = INVALID
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
},
wantFail: true,
},
{
// 2 withdraws with two path attributes (ORIGIN + ASPath), valid update
testNum: 6,
input: []byte{0, 5, 8, 10, 16, 192, 168,
0, 20, // Total Path Attribute Length
255, // Attribute flags
1, // Attribute Type code (ORIGIN)
0, 1, // Length
2, // INCOMPLETE
0, // Attribute flags
2, // Attribute Type code (AS Path)
12, // Length
2, // Type = AS_SEQUENCE
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
1, // Type = AS_SET
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
},
wantFail: false,
expected: &BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &NLRI{
Pfxlen: 16,
},
},
TotalPathAttrLen: 20,
PathAttributes: &PathAttribute{
Optional: true,
Transitive: true,
Partial: true,
ExtendedLength: true,
Length: 1,
TypeCode: 1,
Value: uint8(2),
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 12,
TypeCode: 2,
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
ASNs: []uint32{
15169,
3320,
},
},
},
},
},
},
},
{
// 2 withdraws with 3 path attributes (ORIGIN + ASPath, NH), valid update
testNum: 7,
input: []byte{0, 5, 8, 10, 16, 192, 168,
0, 27, // Total Path Attribute Length
255, // Attribute flags
1, // Attribute Type code (ORIGIN)
0, 1, // Length
2, // INCOMPLETE
0, // Attribute flags
2, // Attribute Type code (AS Path)
12, // Length
2, // Type = AS_SEQUENCE
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
1, // Type = AS_SET
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
0, // Attribute flags
3, // Attribute Type code (Next Hop)
4, // Length
10, 11, 12, 13, // Next Hop
},
wantFail: false,
expected: &BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &NLRI{
Pfxlen: 16,
},
},
TotalPathAttrLen: 27,
PathAttributes: &PathAttribute{
Optional: true,
Transitive: true,
Partial: true,
ExtendedLength: true,
Length: 1,
TypeCode: 1,
Value: uint8(2),
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 12,
TypeCode: 2,
ASNs: []uint32{
15169,
3320,
},
},
},
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 4,
TypeCode: 3,
Value: bnet.IPv4FromOctets(10, 11, 12, 13),
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
},
},
},
},
},
{
// 2 withdraws with 4 path attributes (ORIGIN + ASPath, NH, MED), valid update
testNum: 8,
input: []byte{0, 5, 8, 10, 16, 192, 168,
0, 34, // Total Path Attribute Length
255, // Attribute flags
1, // Attribute Type code (ORIGIN)
0, 1, // Length
2, // INCOMPLETE
0, // Attribute flags
2, // Attribute Type code (AS Path)
12, // Length
2, // Type = AS_SEQUENCE
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
1, // Type = AS_SET
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
0, // Attribute flags
3, // Attribute Type code (Next Hop)
4, // Length
10, 11, 12, 13, // Next Hop
0, // Attribute flags
4, // Attribute Type code (Next Hop)
4, // Length
0, 0, 1, 0, // MED 256
},
wantFail: false,
expected: &BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &NLRI{
Pfxlen: 16,
},
},
TotalPathAttrLen: 34,
PathAttributes: &PathAttribute{
Optional: true,
Transitive: true,
Partial: true,
ExtendedLength: true,
Length: 1,
TypeCode: 1,
Value: uint8(2),
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 12,
TypeCode: 2,
ASNs: []uint32{
15169,
3320,
},
},
},
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 4,
TypeCode: 3,
Value: bnet.IPv4FromOctets(10, 11, 12, 13),
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 4,
TypeCode: 4,
Value: uint32(256),
},
},
},
},
},
},
{
// 2 withdraws with 4 path attributes (ORIGIN + ASPath, NH, MED, Local Pref), valid update
testNum: 9,
input: []byte{0, 5, 8, 10, 16, 192, 168,
0, 41, // Total Path Attribute Length
255, // Attribute flags
1, // Attribute Type code (ORIGIN)
0, 1, // Length
2, // INCOMPLETE
0, // Attribute flags
2, // Attribute Type code (AS Path)
12, // Length
2, // Type = AS_SEQUENCE
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
1, // Type = AS_SET
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
0, // Attribute flags
3, // Attribute Type code (Next Hop)
4, // Length
10, 11, 12, 13, // Next Hop
0, // Attribute flags
4, // Attribute Type code (MED)
4, // Length
0, 0, 1, 0, // MED 256
0, // Attribute flags
5, // Attribute Type code (Local Pref)
4, // Length
0, 0, 1, 0, // Local Pref 256
},
wantFail: false,
expected: &BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &NLRI{
Pfxlen: 16,
},
},
TotalPathAttrLen: 41,
PathAttributes: &PathAttribute{
Optional: true,
Transitive: true,
Partial: true,
ExtendedLength: true,
Length: 1,
TypeCode: 1,
Value: uint8(2),
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 12,
TypeCode: 2,
ASNs: []uint32{
15169,
3320,
},
},
},
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 4,
TypeCode: 3,
Value: bnet.IPv4FromOctets(10, 11, 12, 13),
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 4,
TypeCode: 4,
Value: uint32(256),
Next: &PathAttribute{
Optional: false,
Transitive: false,
Partial: false,
ExtendedLength: false,
Length: 4,
TypeCode: 5,
Value: uint32(256),
},
},
},
},
},
},
},
{
// 2 withdraws with 6 path attributes (ORIGIN, ASPath, NH, MED, Local Pref, Atomi Aggregate), valid update
testNum: 9,
input: []byte{0, 5, 8, 10, 16, 192, 168,
0, 44, // Total Path Attribute Length
255, // Attribute flags
1, // Attribute Type code (ORIGIN)
0, 1, // Length
2, // INCOMPLETE
0, // Attribute flags
2, // Attribute Type code (AS Path)
12, // Length
2, // Type = AS_SEQUENCE
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
1, // Type = AS_SET
2, // Path Segement Length
59, 65, // AS15169
12, 248, // AS3320
0, // Attribute flags
3, // Attribute Type code (Next Hop)
4, // Length
10, 11, 12, 13, // Next Hop
0, // Attribute flags
4, // Attribute Type code (MED)
4, // Length
0, 0, 1, 0, // MED 256
0, // Attribute flags
5, // Attribute Type code (Local Pref)
4, // Length
0, 0, 1, 0, // Local Pref 256
0, // Attribute flags
6, // Attribute Type code (Atomic Aggregate)
0, // Length
},
wantFail: false,
expected: &BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &NLRI{