Newer
Older
"github.com/bio-routing/bio-rd/routingtable"
"testing"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/stretchr/testify/assert"
)
func TestOpenMsgReceived(t *testing.T) {
tests := []struct {
asn uint32
name string
msg packet.BGPOpen
wantIdle bool
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
}{
{
name: "valid open message (16bit ASN)",
asn: 12345,
msg: packet.BGPOpen{
HoldTime: 90,
BGPIdentifier: 1,
Version: 4,
ASN: 12345,
},
},
{
name: "valid open message (32bit ASN)",
asn: 202739,
msg: packet.BGPOpen{
HoldTime: 90,
BGPIdentifier: 1,
Version: 4,
ASN: 23456,
OptParmLen: 1,
OptParams: []packet.OptParam{
{
Type: packet.CapabilitiesParamType,
Length: 6,
Value: packet.Capabilities{
packet.Capability{
Code: packet.ASN4CapabilityCode,
Length: 4,
Value: packet.ASN4Capability{
ASN4: 202739,
},
},
},
},
},
},
},
{
name: "open message does not match configured remote ASN",
asn: 12345,
msg: packet.BGPOpen{
HoldTime: 90,
BGPIdentifier: 1,
Version: 4,
ASN: 54321,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
conA, conB := net.Pipe()
fsm.con = conB
go func() {
for {
buf := make([]byte, 1)
_, err := conA.Read(buf)
if err != nil {
return
}
}
}()
s := &openSentState{
fsm: fsm,
}
state, _ := s.handleOpenMessage(&test.msg)
if test.wantIdle {
assert.IsType(t, &idleState{}, state, "state")
return
}
assert.IsType(t, &openConfirmState{}, state, "state")
assert.Equal(t, test.asn, s.peerASNRcvd, "asn")
})
}
}
func TestProcessMultiProtocolCapability(t *testing.T) {
tests := []struct {
name string
peer *peer
caps []packet.MultiProtocolCapability
expectIPv4MultiProtocol bool
expectIPv6MultiProtocol bool
}{
{
name: "IPv4 only without multi protocol configuration",
peer: &peer{
ipv4: &peerAddressFamily{},
},
caps: []packet.MultiProtocolCapability{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
},
},
},
{
name: "IPv4 only with multi protocol configuration",
peer: &peer{
ipv4MultiProtocolAdvertised: true,
},
caps: []packet.MultiProtocolCapability{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
},
},
expectIPv4MultiProtocol: true,
},
{
name: "IPv6 only",
peer: &peer{
ipv6: &peerAddressFamily{},
},
caps: []packet.MultiProtocolCapability{
AFI: packet.IPv6AFI,
SAFI: packet.UnicastSAFI,
},
},
expectIPv6MultiProtocol: true,
},
{
name: "IPv4 and IPv6, only IPv6 configured as multi protocol",
peer: &peer{
ipv4: &peerAddressFamily{},
ipv6: &peerAddressFamily{},
},
caps: []packet.MultiProtocolCapability{
AFI: packet.IPv6AFI,
SAFI: packet.UnicastSAFI,
},
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
},
},
expectIPv6MultiProtocol: true,
},
{
name: "IPv4 and IPv6 configured as multi protocol",
peer: &peer{
ipv4: &peerAddressFamily{},
ipv6: &peerAddressFamily{},
ipv4MultiProtocolAdvertised: true,
},
caps: []packet.MultiProtocolCapability{
AFI: packet.IPv6AFI,
SAFI: packet.UnicastSAFI,
},
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
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
},
},
expectIPv4MultiProtocol: true,
expectIPv6MultiProtocol: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fsm := newFSM(test.peer)
fsm.con = &btesting.MockConn{}
s := &openSentState{
fsm: fsm,
}
for _, cap := range test.caps {
s.processMultiProtocolCapability(cap)
}
if fsm.ipv4Unicast != nil {
assert.Equal(t, test.expectIPv4MultiProtocol, fsm.ipv4Unicast.multiProtocol)
}
if fsm.ipv6Unicast != nil {
assert.Equal(t, test.expectIPv6MultiProtocol, fsm.ipv6Unicast.multiProtocol)
}
})
}
}
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
func TestProcessAddPathCapabilityTX(t *testing.T) {
tests := []struct {
name string
peer *peer
caps []packet.AddPathCapability
expected routingtable.ClientOptions
}{
{
name: "Add-Path enabled and cap received",
peer: &peer{
ipv4: &peerAddressFamily{
addPathSend: routingtable.ClientOptions{MaxPaths: 3},
},
},
caps: []packet.AddPathCapability{
{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
SendReceive: packet.AddPathReceive,
},
},
expected: routingtable.ClientOptions{MaxPaths: 3},
},
{
name: "Add-Path enabled and cap not received",
peer: &peer{
ipv4: &peerAddressFamily{
addPathSend: routingtable.ClientOptions{MaxPaths: 3},
},
},
caps: []packet.AddPathCapability{},
expected: routingtable.ClientOptions{BestOnly: true},
},
{
name: "Add-Path disabled and cap received",
peer: &peer{
ipv4: &peerAddressFamily{
addPathSend: routingtable.ClientOptions{BestOnly: true},
},
},
caps: []packet.AddPathCapability{
{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
SendReceive: packet.AddPathReceive,
},
},
expected: routingtable.ClientOptions{BestOnly: true},
},
{
name: "Add-Path disabled and cap not received",
peer: &peer{
ipv4: &peerAddressFamily{
addPathSend: routingtable.ClientOptions{BestOnly: true},
},
},
caps: []packet.AddPathCapability{},
expected: routingtable.ClientOptions{BestOnly: true},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fsm := newFSM(test.peer)
fsm.con = &btesting.MockConn{}
s := &openSentState{
fsm: fsm,
}
for _, cap := range test.caps {
s.processAddPathCapability(cap)
}
assert.Equal(t, test.expected, fsm.ipv4Unicast.addPathTX)
})
}
}