Newer
Older
bnet "github.com/bio-routing/bio-rd/net"
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
"github.com/stretchr/testify/assert"
"github.com/taktv6/tflow2/convert"
)
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{})
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
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,
Loading
Loading full blame...