github.com/osrg/gobgp/v3@v3.30.0/pkg/packet/bgp/bgp_test.go (about) 1 // Copyright (C) 2016 Nippon Telegraph and Telephone Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package bgp 17 18 import ( 19 "bytes" 20 "encoding/binary" 21 "math" 22 "net" 23 "os" 24 "path/filepath" 25 "reflect" 26 "strconv" 27 "strings" 28 "testing" 29 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/require" 32 ) 33 34 func keepalive() *BGPMessage { 35 return NewBGPKeepAliveMessage() 36 } 37 38 func notification() *BGPMessage { 39 return NewBGPNotificationMessage(1, 2, nil) 40 } 41 42 func refresh() *BGPMessage { 43 return NewBGPRouteRefreshMessage(1, 2, 10) 44 } 45 46 var result []string 47 48 func BenchmarkNormalizeFlowSpecOpValues(b *testing.B) { 49 var r []string 50 for n := 0; n < b.N; n++ { 51 r = normalizeFlowSpecOpValues([]string{"&<=80"}) 52 } 53 result = r 54 } 55 56 func Test_Message(t *testing.T) { 57 l := []*BGPMessage{keepalive(), notification(), refresh(), NewTestBGPOpenMessage(), NewTestBGPUpdateMessage()} 58 59 for _, m1 := range l { 60 buf1, err := m1.Serialize() 61 assert.NoError(t, err) 62 63 t.Log("LEN =", len(buf1)) 64 m2, err := ParseBGPMessage(buf1) 65 assert.NoError(t, err) 66 67 // FIXME: shouldn't but workaround for some structs. 68 _, err = m2.Serialize() 69 assert.NoError(t, err) 70 71 assert.True(t, reflect.DeepEqual(m1, m2)) 72 } 73 } 74 75 func Test_IPAddrPrefixString(t *testing.T) { 76 ipv4 := NewIPAddrPrefix(24, "129.6.10.0") 77 assert.Equal(t, "129.6.10.0/24", ipv4.String()) 78 ipv4 = NewIPAddrPrefix(24, "129.6.10.1") 79 assert.Equal(t, "129.6.10.0/24", ipv4.String()) 80 ipv4 = NewIPAddrPrefix(22, "129.6.129.0") 81 assert.Equal(t, "129.6.128.0/22", ipv4.String()) 82 83 ipv6 := NewIPv6AddrPrefix(64, "3343:faba:3903::0") 84 assert.Equal(t, "3343:faba:3903::/64", ipv6.String()) 85 ipv6 = NewIPv6AddrPrefix(64, "3343:faba:3903::1") 86 assert.Equal(t, "3343:faba:3903::/64", ipv6.String()) 87 ipv6 = NewIPv6AddrPrefix(63, "3343:faba:3903:129::0") 88 assert.Equal(t, "3343:faba:3903:128::/63", ipv6.String()) 89 } 90 91 func Test_MalformedPrefixLookup(t *testing.T) { 92 assert := assert.New(t) 93 94 var tests = []struct { 95 inPrefix string 96 routeFamily RouteFamily 97 want AddrPrefixInterface 98 err bool 99 }{ 100 {"129.6.128/22", RF_IPv4_UC, nil, true}, 101 {"foo", RF_IPv4_UC, nil, true}, 102 {"3343:faba:3903:128::::/63", RF_IPv6_UC, nil, true}, 103 {"foo", RF_IPv6_UC, nil, true}, 104 } 105 106 for _, test := range tests { 107 afi, safi := RouteFamilyToAfiSafi(RF_IPv4_UC) 108 p, err := NewPrefixFromRouteFamily(afi, safi, test.inPrefix) 109 if test.err { 110 assert.Error(err) 111 } else { 112 assert.Equal(test.want, p) 113 } 114 } 115 116 } 117 118 func Test_IPAddrDecode(t *testing.T) { 119 r := IPAddrPrefixDefault{} 120 b := make([]byte, 16) 121 r.decodePrefix(b, 33, 4) 122 } 123 124 func Test_RouteTargetMembershipNLRIString(t *testing.T) { 125 assert := assert.New(t) 126 127 // TwoOctetAsSpecificExtended 128 buf := make([]byte, 13) 129 buf[0] = 96 // in bit length 130 binary.BigEndian.PutUint32(buf[1:5], 65546) 131 buf[5] = byte(EC_TYPE_TRANSITIVE_TWO_OCTET_AS_SPECIFIC) // typehigh 132 binary.BigEndian.PutUint16(buf[7:9], 65000) 133 binary.BigEndian.PutUint32(buf[9:], 65546) 134 r := &RouteTargetMembershipNLRI{} 135 err := r.DecodeFromBytes(buf) 136 assert.Equal(nil, err) 137 assert.Equal("65546:65000:65546", r.String()) 138 buf, err = r.Serialize() 139 assert.Equal(nil, err) 140 err = r.DecodeFromBytes(buf) 141 assert.Equal(nil, err) 142 assert.Equal("65546:65000:65546", r.String()) 143 144 // IPv4AddressSpecificExtended 145 buf = make([]byte, 13) 146 buf[0] = 96 // in bit length 147 binary.BigEndian.PutUint32(buf[1:5], 65546) 148 buf[5] = byte(EC_TYPE_TRANSITIVE_IP4_SPECIFIC) // typehigh 149 ip := net.ParseIP("10.0.0.1").To4() 150 copy(buf[7:11], []byte(ip)) 151 binary.BigEndian.PutUint16(buf[11:], 65000) 152 r = &RouteTargetMembershipNLRI{} 153 err = r.DecodeFromBytes(buf) 154 assert.Equal(nil, err) 155 assert.Equal("65546:10.0.0.1:65000", r.String()) 156 buf, err = r.Serialize() 157 assert.Equal(nil, err) 158 err = r.DecodeFromBytes(buf) 159 assert.Equal(nil, err) 160 assert.Equal("65546:10.0.0.1:65000", r.String()) 161 162 // FourOctetAsSpecificExtended 163 buf = make([]byte, 13) 164 buf[0] = 96 // in bit length 165 binary.BigEndian.PutUint32(buf[1:5], 65546) 166 buf[5] = byte(EC_TYPE_TRANSITIVE_FOUR_OCTET_AS_SPECIFIC) // typehigh 167 buf[6] = byte(EC_SUBTYPE_ROUTE_TARGET) // subtype 168 binary.BigEndian.PutUint32(buf[7:], 65546) 169 binary.BigEndian.PutUint16(buf[11:], 65000) 170 r = &RouteTargetMembershipNLRI{} 171 err = r.DecodeFromBytes(buf) 172 assert.Equal(nil, err) 173 assert.Equal("65546:1.10:65000", r.String()) 174 buf, err = r.Serialize() 175 assert.Equal(nil, err) 176 err = r.DecodeFromBytes(buf) 177 assert.Equal(nil, err) 178 assert.Equal("65546:1.10:65000", r.String()) 179 180 // OpaqueExtended 181 buf = make([]byte, 13) 182 buf[0] = 96 // in bit length 183 binary.BigEndian.PutUint32(buf[1:5], 65546) 184 buf[5] = byte(EC_TYPE_TRANSITIVE_OPAQUE) // typehigh 185 binary.BigEndian.PutUint32(buf[9:], 1000000) 186 r = &RouteTargetMembershipNLRI{} 187 err = r.DecodeFromBytes(buf) 188 assert.Equal(nil, err) 189 assert.Equal("65546:1000000", r.String()) 190 buf, err = r.Serialize() 191 assert.Equal(nil, err) 192 err = r.DecodeFromBytes(buf) 193 assert.Equal(nil, err) 194 assert.Equal("65546:1000000", r.String()) 195 196 // Unknown 197 buf = make([]byte, 13) 198 buf[0] = 96 // in bit length 199 binary.BigEndian.PutUint32(buf[1:5], 65546) 200 buf[5] = 0x04 // typehigh 201 binary.BigEndian.PutUint32(buf[9:], 1000000) 202 r = &RouteTargetMembershipNLRI{} 203 err = r.DecodeFromBytes(buf) 204 assert.Equal(nil, err) 205 assert.Equal("65546:1000000", r.String()) 206 buf, err = r.Serialize() 207 assert.Equal(nil, err) 208 err = r.DecodeFromBytes(buf) 209 assert.Equal(nil, err) 210 assert.Equal("65546:1000000", r.String()) 211 212 } 213 214 func Test_MalformedUpdateMsg(t *testing.T) { 215 assert := assert.New(t) 216 var bufin []byte 217 var u *BGPUpdate 218 var err error 219 220 // Invalid AS_PATH 221 bufin = []byte{ 222 0x00, 0x00, // Withdraws(0) 223 0x00, 0x16, // Attrs Len(22) 224 0x40, 0x01, 0x01, 0x00, // Attr(ORIGIN) 225 0x40, 0x03, 0x04, 0xc0, // Attr(NEXT_HOP) 226 0xa8, 0x01, 0x64, 227 0x40, 0x02, 0x17, // Attr(AS_PATH) - invalid length 228 0x02, 0x03, 0xfd, 0xe8, 229 0xfd, 0xe8, 0xfd, 0xe8, 230 0x08, 0x0a, // NLRI 231 } 232 233 u = &BGPUpdate{} 234 err = u.DecodeFromBytes(bufin) 235 assert.Error(err) 236 assert.Equal(ERROR_HANDLING_TREAT_AS_WITHDRAW, err.(*MessageError).ErrorHandling) 237 238 // Invalid AGGREGATOR 239 bufin = []byte{ 240 0x00, 0x00, // Withdraws(0) 241 0x00, 0x16, // Attrs Len(22) 242 0xc0, 0x07, 0x05, // Flag, Type(7), Length(5) 243 0x00, 0x00, 0x00, 0x64, // aggregator - invalid length 244 0x00, 245 0x40, 0x01, 0x01, 0x00, // Attr(ORIGIN) 246 0x40, 0x03, 0x04, 0xc0, // Attr(NEXT_HOP) 247 0xa8, 0x01, 0x64, 248 0x40, 0x02, 0x00, // Attr(AS_PATH) 249 } 250 251 u = &BGPUpdate{} 252 err = u.DecodeFromBytes(bufin) 253 assert.Error(err) 254 assert.Equal(ERROR_HANDLING_ATTRIBUTE_DISCARD, err.(*MessageError).ErrorHandling) 255 256 // Invalid MP_REACH_NLRI 257 bufin = []byte{ 258 0x00, 0x00, // Withdraws(0) 259 0x00, 0x27, // Attrs Len(39) 260 0x80, 0x0e, 0x1d, // Flag, Type(14), Length(29) 261 0x00, 0x02, 0x01, // afi(2), safi(1) 262 0x0f, 0x00, 0x00, 0x00, // nexthop - invalid nexthop length 263 0x00, 0x00, 0x00, 0x00, 264 0x00, 0x00, 0x00, 0xff, 265 0xff, 0x0a, 0x00, 0x00, 266 0x00, // SNPA(0) 267 0x40, 0x20, 0x01, 0x0d, // NLRI 268 0xb8, 0x00, 0x01, 0x00, 269 0x00, 270 0x40, 0x01, 0x01, 0x00, // Attr(ORIGIN) 271 0x40, 0x02, 0x00, // Attr(AS_PATH) 272 } 273 274 err = u.DecodeFromBytes(bufin) 275 assert.Error(err) 276 assert.Equal(ERROR_HANDLING_AFISAFI_DISABLE, err.(*MessageError).ErrorHandling) 277 278 // Invalid flag 279 bufin = []byte{ 280 0x00, 0x00, // Withdraws(0) 281 0x00, 0x0e, // Attrs Len(14) 282 0xc0, 0x01, 0x01, 0x00, // Attr(ORIGIN) - invalid flag 283 0x40, 0x03, 0x04, 0xc0, // Attr(NEXT_HOP) 284 0xa8, 0x01, 0x64, 285 0x40, 0x02, 0x00, // Attr(AS_PATH) 286 } 287 288 err = u.DecodeFromBytes(bufin) 289 assert.Error(err) 290 assert.Equal(ERROR_HANDLING_TREAT_AS_WITHDRAW, err.(*MessageError).ErrorHandling) 291 292 // Invalid AGGREGATOR and MULTI_EXIT_DESC 293 bufin = []byte{ 294 0x00, 0x00, // Withdraws(0) 295 0x00, 0x1e, // Attrs Len(30) 296 0xc0, 0x07, 0x05, 0x00, // Attr(AGGREGATOR) - invalid length 297 0x00, 0x00, 0x64, 0x00, 298 0x80, 0x04, 0x05, 0x00, // Attr(MULTI_EXIT_DESC) - invalid length 299 0x00, 0x00, 0x00, 0x64, 300 0x40, 0x01, 0x01, 0x00, // Attr(ORIGIN) 301 0x40, 0x02, 0x00, // Attr(AS_PATH) 302 0x40, 0x03, 0x04, 0xc0, // Attr(NEXT_HOP) 303 0xa8, 0x01, 0x64, 304 0x20, 0xc8, 0xc8, 0xc8, // NLRI 305 0xc8, 306 } 307 308 err = u.DecodeFromBytes(bufin) 309 assert.Error(err) 310 assert.Equal(ERROR_HANDLING_TREAT_AS_WITHDRAW, err.(*MessageError).ErrorHandling) 311 } 312 313 func Test_RFC5512(t *testing.T) { 314 assert := assert.New(t) 315 316 buf := make([]byte, 8) 317 buf[0] = byte(EC_TYPE_TRANSITIVE_OPAQUE) 318 buf[1] = byte(EC_SUBTYPE_COLOR) 319 binary.BigEndian.PutUint32(buf[4:], 1000000) 320 ec, err := ParseExtended(buf) 321 assert.Equal(nil, err) 322 assert.Equal("1000000", ec.String()) 323 buf, err = ec.Serialize() 324 assert.Equal(nil, err) 325 assert.Equal([]byte{0x3, 0xb, 0x0, 0x0, 0x0, 0xf, 0x42, 0x40}, buf) 326 327 buf = make([]byte, 8) 328 buf[0] = byte(EC_TYPE_TRANSITIVE_OPAQUE) 329 buf[1] = byte(EC_SUBTYPE_ENCAPSULATION) 330 binary.BigEndian.PutUint16(buf[6:], uint16(TUNNEL_TYPE_VXLAN)) 331 ec, err = ParseExtended(buf) 332 assert.Equal(nil, err) 333 assert.Equal("VXLAN", ec.String()) 334 buf, err = ec.Serialize() 335 assert.Equal(nil, err) 336 assert.Equal([]byte{0x3, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8}, buf) 337 338 tlv := NewTunnelEncapTLV(TUNNEL_TYPE_VXLAN, []TunnelEncapSubTLVInterface{NewTunnelEncapSubTLVColor(10)}) 339 attr := NewPathAttributeTunnelEncap([]*TunnelEncapTLV{tlv}) 340 341 buf1, err := attr.Serialize() 342 assert.Equal(nil, err) 343 344 p, err := GetPathAttribute(buf1) 345 assert.Equal(nil, err) 346 347 err = p.DecodeFromBytes(buf1) 348 assert.Equal(nil, err) 349 350 buf2, err := p.Serialize() 351 assert.Equal(nil, err) 352 assert.Equal(buf1, buf2) 353 354 n1 := NewEncapNLRI("10.0.0.1") 355 buf1, err = n1.Serialize() 356 assert.Equal(nil, err) 357 358 n2 := NewEncapNLRI("") 359 err = n2.DecodeFromBytes(buf1) 360 assert.Equal(nil, err) 361 assert.Equal("10.0.0.1", n2.String()) 362 363 n3 := NewEncapv6NLRI("2001::1") 364 buf1, err = n3.Serialize() 365 assert.Equal(nil, err) 366 367 n4 := NewEncapv6NLRI("") 368 err = n4.DecodeFromBytes(buf1) 369 assert.Equal(nil, err) 370 assert.Equal("2001::1", n4.String()) 371 } 372 373 func Test_ASLen(t *testing.T) { 374 assert := assert.New(t) 375 376 aspath := AsPathParam{ 377 Num: 2, 378 AS: []uint16{65000, 65001}, 379 } 380 aspath.Type = BGP_ASPATH_ATTR_TYPE_SEQ 381 assert.Equal(2, aspath.ASLen()) 382 383 aspath.Type = BGP_ASPATH_ATTR_TYPE_SET 384 assert.Equal(1, aspath.ASLen()) 385 386 aspath.Type = BGP_ASPATH_ATTR_TYPE_CONFED_SEQ 387 assert.Equal(0, aspath.ASLen()) 388 389 aspath.Type = BGP_ASPATH_ATTR_TYPE_CONFED_SET 390 assert.Equal(0, aspath.ASLen()) 391 392 as4path := As4PathParam{ 393 Num: 2, 394 AS: []uint32{65000, 65001}, 395 } 396 as4path.Type = BGP_ASPATH_ATTR_TYPE_SEQ 397 assert.Equal(2, as4path.ASLen()) 398 399 as4path.Type = BGP_ASPATH_ATTR_TYPE_SET 400 assert.Equal(1, as4path.ASLen()) 401 402 as4path.Type = BGP_ASPATH_ATTR_TYPE_CONFED_SEQ 403 assert.Equal(0, as4path.ASLen()) 404 405 as4path.Type = BGP_ASPATH_ATTR_TYPE_CONFED_SET 406 assert.Equal(0, as4path.ASLen()) 407 408 } 409 410 func Test_MPLSLabelStack(t *testing.T) { 411 assert := assert.New(t) 412 mpls := NewMPLSLabelStack() 413 buf, err := mpls.Serialize() 414 assert.Nil(err) 415 assert.Equal(true, bytes.Equal(buf, []byte{0, 0, 1})) 416 417 mpls = &MPLSLabelStack{} 418 assert.Nil(mpls.DecodeFromBytes(buf)) 419 assert.Equal(1, len(mpls.Labels)) 420 assert.Equal(uint32(0), mpls.Labels[0]) 421 422 mpls = NewMPLSLabelStack(WITHDRAW_LABEL) 423 buf, err = mpls.Serialize() 424 assert.Nil(err) 425 assert.Equal(true, bytes.Equal(buf, []byte{128, 0, 0})) 426 427 mpls = &MPLSLabelStack{} 428 assert.Nil(mpls.DecodeFromBytes(buf)) 429 assert.Equal(1, len(mpls.Labels)) 430 assert.Equal(WITHDRAW_LABEL, mpls.Labels[0]) 431 } 432 433 func Test_FlowSpecNlri(t *testing.T) { 434 assert := assert.New(t) 435 cmp := make([]FlowSpecComponentInterface, 0) 436 cmp = append(cmp, NewFlowSpecDestinationPrefix(NewIPAddrPrefix(24, "10.0.0.0"))) 437 cmp = append(cmp, NewFlowSpecSourcePrefix(NewIPAddrPrefix(24, "10.0.0.0"))) 438 item1 := NewFlowSpecComponentItem(DEC_NUM_OP_EQ, TCP) 439 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_IP_PROTO, []*FlowSpecComponentItem{item1})) 440 item2 := NewFlowSpecComponentItem(DEC_NUM_OP_GT_EQ, 20) 441 item3 := NewFlowSpecComponentItem(DEC_NUM_OP_AND|DEC_NUM_OP_LT_EQ, 30) 442 item4 := NewFlowSpecComponentItem(DEC_NUM_OP_GT_EQ, 10) 443 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_PORT, []*FlowSpecComponentItem{item2, item3, item4})) 444 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_DST_PORT, []*FlowSpecComponentItem{item2, item3, item4})) 445 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_SRC_PORT, []*FlowSpecComponentItem{item2, item3, item4})) 446 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ICMP_TYPE, []*FlowSpecComponentItem{item2, item3, item4})) 447 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ICMP_CODE, []*FlowSpecComponentItem{item2, item3, item4})) 448 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_PKT_LEN, []*FlowSpecComponentItem{item2, item3, item4})) 449 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_DSCP, []*FlowSpecComponentItem{item2, item3, item4})) 450 isFragment := uint64(0x02) 451 lastFragment := uint64(0x08) 452 item5 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_MATCH, isFragment) 453 item6 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_AND, lastFragment) 454 455 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_FRAGMENT, []*FlowSpecComponentItem{item5, item6})) 456 item7 := NewFlowSpecComponentItem(0, TCP_FLAG_ACK) 457 item8 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_AND|BITMASK_FLAG_OP_NOT, TCP_FLAG_URGENT) 458 459 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_TCP_FLAG, []*FlowSpecComponentItem{item7, item8})) 460 n1 := NewFlowSpecIPv4Unicast(cmp) 461 462 buf1, err := n1.Serialize() 463 assert.Nil(err) 464 465 n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_IPv4_UC)) 466 assert.Nil(err) 467 468 err = n2.DecodeFromBytes(buf1) 469 assert.Nil(err) 470 // should be equal 471 assert.Equal(n1, n2) 472 } 473 474 func Test_NewFlowSpecComponentItemLength(t *testing.T) { 475 item := NewFlowSpecComponentItem(0, 0) 476 assert.Equal(t, 1, item.Len()) 477 item = NewFlowSpecComponentItem(0, math.MaxUint8) 478 assert.Equal(t, 1, item.Len()) 479 480 item = NewFlowSpecComponentItem(0, math.MaxUint8+1) 481 assert.Equal(t, 2, item.Len()) 482 item = NewFlowSpecComponentItem(0, math.MaxUint16) 483 assert.Equal(t, 2, item.Len()) 484 485 item = NewFlowSpecComponentItem(0, math.MaxUint16+1) 486 assert.Equal(t, 4, item.Len()) 487 item = NewFlowSpecComponentItem(0, math.MaxUint32) 488 assert.Equal(t, 4, item.Len()) 489 490 item = NewFlowSpecComponentItem(0, math.MaxUint32+1) 491 assert.Equal(t, 8, item.Len()) 492 item = NewFlowSpecComponentItem(0, math.MaxUint64) 493 assert.Equal(t, 8, item.Len()) 494 } 495 496 func Test_LinkBandwidthExtended(t *testing.T) { 497 assert := assert.New(t) 498 exts := make([]ExtendedCommunityInterface, 0) 499 exts = append(exts, NewLinkBandwidthExtended(65001, 125000.0)) 500 m1 := NewPathAttributeExtendedCommunities(exts) 501 buf1, err := m1.Serialize() 502 require.NoError(t, err) 503 504 m2 := NewPathAttributeExtendedCommunities(nil) 505 err = m2.DecodeFromBytes(buf1) 506 require.NoError(t, err) 507 508 _, err = m2.Serialize() 509 require.NoError(t, err) 510 511 assert.Equal(m1, m2) 512 } 513 514 func Test_FlowSpecExtended(t *testing.T) { 515 assert := assert.New(t) 516 exts := make([]ExtendedCommunityInterface, 0) 517 exts = append(exts, NewTrafficRateExtended(100, 9600.0)) 518 exts = append(exts, NewTrafficActionExtended(true, false)) 519 exts = append(exts, NewRedirectTwoOctetAsSpecificExtended(1000, 1000)) 520 exts = append(exts, NewRedirectIPv4AddressSpecificExtended("10.0.0.1", 1000)) 521 exts = append(exts, NewRedirectFourOctetAsSpecificExtended(10000000, 1000)) 522 exts = append(exts, NewTrafficRemarkExtended(10)) 523 m1 := NewPathAttributeExtendedCommunities(exts) 524 buf1, err := m1.Serialize() 525 require.NoError(t, err) 526 527 m2 := NewPathAttributeExtendedCommunities(nil) 528 err = m2.DecodeFromBytes(buf1) 529 require.NoError(t, err) 530 531 _, err = m2.Serialize() 532 require.NoError(t, err) 533 534 assert.Equal(m1, m2) 535 } 536 537 func Test_IP6FlowSpecExtended(t *testing.T) { 538 exts := make([]ExtendedCommunityInterface, 0) 539 exts = append(exts, NewRedirectIPv6AddressSpecificExtended("2001:db8::68", 1000)) 540 m1 := NewPathAttributeIP6ExtendedCommunities(exts) 541 buf1, err := m1.Serialize() 542 require.NoError(t, err) 543 544 m2 := NewPathAttributeIP6ExtendedCommunities(nil) 545 err = m2.DecodeFromBytes(buf1) 546 require.NoError(t, err) 547 548 _, err = m2.Serialize() 549 require.NoError(t, err) 550 551 assert.Equal(t, m1, m2) 552 } 553 554 func Test_FlowSpecNlriv6(t *testing.T) { 555 cmp := make([]FlowSpecComponentInterface, 0) 556 cmp = append(cmp, NewFlowSpecDestinationPrefix6(NewIPv6AddrPrefix(64, "2001::"), 12)) 557 cmp = append(cmp, NewFlowSpecSourcePrefix6(NewIPv6AddrPrefix(64, "2001::"), 12)) 558 item1 := NewFlowSpecComponentItem(DEC_NUM_OP_EQ, TCP) 559 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_IP_PROTO, []*FlowSpecComponentItem{item1})) 560 item2 := NewFlowSpecComponentItem(DEC_NUM_OP_GT_EQ, 20) 561 item3 := NewFlowSpecComponentItem(DEC_NUM_OP_AND|DEC_NUM_OP_LT_EQ, 30) 562 item4 := NewFlowSpecComponentItem(DEC_NUM_OP_EQ, 10) 563 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_PORT, []*FlowSpecComponentItem{item2, item3, item4})) 564 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_DST_PORT, []*FlowSpecComponentItem{item2, item3, item4})) 565 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_SRC_PORT, []*FlowSpecComponentItem{item2, item3, item4})) 566 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ICMP_TYPE, []*FlowSpecComponentItem{item2, item3, item4})) 567 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ICMP_CODE, []*FlowSpecComponentItem{item2, item3, item4})) 568 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_PKT_LEN, []*FlowSpecComponentItem{item2, item3, item4})) 569 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_DSCP, []*FlowSpecComponentItem{item2, item3, item4})) 570 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_LABEL, []*FlowSpecComponentItem{item2, item3, item4})) 571 isFragment := uint64(0x02) 572 item5 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_MATCH, isFragment) 573 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_FRAGMENT, []*FlowSpecComponentItem{item5})) 574 item6 := NewFlowSpecComponentItem(0, TCP_FLAG_ACK) 575 item7 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_AND|BITMASK_FLAG_OP_NOT, TCP_FLAG_URGENT) 576 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_TCP_FLAG, []*FlowSpecComponentItem{item6, item7})) 577 n1 := NewFlowSpecIPv6Unicast(cmp) 578 buf1, err := n1.Serialize() 579 require.NoError(t, err) 580 581 n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_IPv6_UC)) 582 require.NoError(t, err) 583 584 err = n2.DecodeFromBytes(buf1) 585 require.NoError(t, err) 586 587 _, err = n2.Serialize() 588 require.NoError(t, err) 589 590 assert.Equal(t, n1, n2) 591 } 592 593 func Test_Aigp(t *testing.T) { 594 assert := assert.New(t) 595 m := NewAigpTLVIgpMetric(1000) 596 a1 := NewPathAttributeAigp([]AigpTLVInterface{m}) 597 buf1, err := a1.Serialize() 598 require.NoError(t, err) 599 600 a2 := NewPathAttributeAigp(nil) 601 err = a2.DecodeFromBytes(buf1) 602 require.NoError(t, err) 603 604 assert.Equal(a1, a2) 605 } 606 607 func Test_FlowSpecNlriL2(t *testing.T) { 608 assert := assert.New(t) 609 mac, _ := net.ParseMAC("01:23:45:67:89:ab") 610 cmp := make([]FlowSpecComponentInterface, 0) 611 cmp = append(cmp, NewFlowSpecDestinationMac(mac)) 612 cmp = append(cmp, NewFlowSpecSourceMac(mac)) 613 item1 := NewFlowSpecComponentItem(DEC_NUM_OP_EQ, uint64(IPv4)) 614 cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ETHERNET_TYPE, []*FlowSpecComponentItem{item1})) 615 rd, _ := ParseRouteDistinguisher("100:100") 616 n1 := NewFlowSpecL2VPN(rd, cmp) 617 buf1, err := n1.Serialize() 618 assert.Nil(err) 619 n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_L2_VPN)) 620 assert.Nil(err) 621 err = n2.DecodeFromBytes(buf1) 622 assert.Nil(err) 623 624 assert.Equal(n1, n2) 625 } 626 627 func Test_NotificationErrorCode(t *testing.T) { 628 // boundary check 629 t.Log(NewNotificationErrorCode(BGP_ERROR_MESSAGE_HEADER_ERROR, BGP_ERROR_SUB_BAD_MESSAGE_TYPE).String()) 630 t.Log(NewNotificationErrorCode(BGP_ERROR_MESSAGE_HEADER_ERROR, BGP_ERROR_SUB_BAD_MESSAGE_TYPE+1).String()) 631 t.Log(NewNotificationErrorCode(BGP_ERROR_MESSAGE_HEADER_ERROR, 0).String()) 632 t.Log(NewNotificationErrorCode(0, BGP_ERROR_SUB_BAD_MESSAGE_TYPE).String()) 633 t.Log(NewNotificationErrorCode(BGP_ERROR_ROUTE_REFRESH_MESSAGE_ERROR+1, 0).String()) 634 } 635 636 func Test_FlowSpecNlriVPN(t *testing.T) { 637 assert := assert.New(t) 638 cmp := make([]FlowSpecComponentInterface, 0) 639 cmp = append(cmp, NewFlowSpecDestinationPrefix(NewIPAddrPrefix(24, "10.0.0.0"))) 640 cmp = append(cmp, NewFlowSpecSourcePrefix(NewIPAddrPrefix(24, "10.0.0.0"))) 641 rd, _ := ParseRouteDistinguisher("100:100") 642 n1 := NewFlowSpecIPv4VPN(rd, cmp) 643 buf1, err := n1.Serialize() 644 assert.Nil(err) 645 n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_IPv4_VPN)) 646 assert.Nil(err) 647 err = n2.DecodeFromBytes(buf1) 648 require.NoError(t, err) 649 650 assert.Equal(n1, n2) 651 } 652 653 func Test_EVPNIPPrefixRoute(t *testing.T) { 654 assert := assert.New(t) 655 rd, _ := ParseRouteDistinguisher("100:100") 656 r := &EVPNIPPrefixRoute{ 657 RD: rd, 658 ESI: EthernetSegmentIdentifier{ 659 Type: ESI_ARBITRARY, 660 Value: make([]byte, 9), 661 }, 662 ETag: 10, 663 IPPrefixLength: 24, 664 IPPrefix: net.IP{10, 10, 10, 0}, 665 GWIPAddress: net.IP{10, 10, 10, 10}, 666 Label: 1000, 667 } 668 n1 := NewEVPNNLRI(EVPN_IP_PREFIX, r) 669 buf1, err := n1.Serialize() 670 assert.Nil(err) 671 n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_EVPN)) 672 assert.Nil(err) 673 err = n2.DecodeFromBytes(buf1) 674 assert.Nil(err) 675 676 assert.Equal(n1, n2) 677 } 678 679 func Test_CapExtendedNexthop(t *testing.T) { 680 assert := assert.New(t) 681 tuple := NewCapExtendedNexthopTuple(RF_IPv4_UC, AFI_IP6) 682 n1 := NewCapExtendedNexthop([]*CapExtendedNexthopTuple{tuple}) 683 buf1, err := n1.Serialize() 684 assert.Nil(err) 685 n2, err := DecodeCapability(buf1) 686 assert.Nil(err) 687 688 assert.Equal(n1, n2) 689 } 690 691 func Test_AddPath(t *testing.T) { 692 assert := assert.New(t) 693 opt := &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_IPv4_UC: BGP_ADD_PATH_BOTH}} 694 { 695 n1 := NewIPAddrPrefix(24, "10.10.10.0") 696 assert.Equal(n1.PathIdentifier(), uint32(0)) 697 n1.SetPathLocalIdentifier(10) 698 assert.Equal(n1.PathLocalIdentifier(), uint32(10)) 699 bits, err := n1.Serialize(opt) 700 assert.Nil(err) 701 n2 := &IPAddrPrefix{} 702 err = n2.DecodeFromBytes(bits, opt) 703 assert.Nil(err) 704 assert.Equal(n2.PathIdentifier(), uint32(10)) 705 } 706 { 707 n1 := NewIPv6AddrPrefix(64, "2001::") 708 n1.SetPathIdentifier(10) 709 bits, err := n1.Serialize(opt) 710 assert.Nil(err) 711 n2 := NewIPv6AddrPrefix(0, "") 712 err = n2.DecodeFromBytes(bits, opt) 713 assert.Nil(err) 714 assert.Equal(n2.PathIdentifier(), uint32(0)) 715 } 716 opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_IPv4_UC: BGP_ADD_PATH_BOTH, RF_IPv6_UC: BGP_ADD_PATH_BOTH}} 717 { 718 n1 := NewIPv6AddrPrefix(64, "2001::") 719 n1.SetPathLocalIdentifier(10) 720 bits, err := n1.Serialize(opt) 721 assert.Nil(err) 722 n2 := NewIPv6AddrPrefix(0, "") 723 err = n2.DecodeFromBytes(bits, opt) 724 assert.Nil(err) 725 assert.Equal(n2.PathIdentifier(), uint32(10)) 726 } 727 opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_IPv4_VPN: BGP_ADD_PATH_BOTH, RF_IPv6_VPN: BGP_ADD_PATH_BOTH}} 728 { 729 rd, _ := ParseRouteDistinguisher("100:100") 730 labels := NewMPLSLabelStack(100, 200) 731 n1 := NewLabeledVPNIPAddrPrefix(24, "10.10.10.0", *labels, rd) 732 n1.SetPathLocalIdentifier(20) 733 bits, err := n1.Serialize(opt) 734 assert.Nil(err) 735 n2 := NewLabeledVPNIPAddrPrefix(0, "", MPLSLabelStack{}, nil) 736 err = n2.DecodeFromBytes(bits, opt) 737 assert.Nil(err) 738 assert.Equal(n2.PathIdentifier(), uint32(20)) 739 } 740 { 741 rd, _ := ParseRouteDistinguisher("100:100") 742 labels := NewMPLSLabelStack(100, 200) 743 n1 := NewLabeledVPNIPv6AddrPrefix(64, "2001::", *labels, rd) 744 n1.SetPathLocalIdentifier(20) 745 bits, err := n1.Serialize(opt) 746 assert.Nil(err) 747 n2 := NewLabeledVPNIPv6AddrPrefix(0, "", MPLSLabelStack{}, nil) 748 err = n2.DecodeFromBytes(bits, opt) 749 assert.Nil(err) 750 assert.Equal(n2.PathIdentifier(), uint32(20)) 751 } 752 opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_IPv4_MPLS: BGP_ADD_PATH_BOTH, RF_IPv6_MPLS: BGP_ADD_PATH_BOTH}} 753 { 754 labels := NewMPLSLabelStack(100, 200) 755 n1 := NewLabeledIPAddrPrefix(24, "10.10.10.0", *labels) 756 n1.SetPathLocalIdentifier(20) 757 bits, err := n1.Serialize(opt) 758 assert.Nil(err) 759 n2 := NewLabeledIPAddrPrefix(0, "", MPLSLabelStack{}) 760 err = n2.DecodeFromBytes(bits, opt) 761 assert.Nil(err) 762 assert.Equal(n2.PathIdentifier(), uint32(20)) 763 } 764 { 765 labels := NewMPLSLabelStack(100, 200) 766 n1 := NewLabeledIPv6AddrPrefix(64, "2001::", *labels) 767 n1.SetPathLocalIdentifier(20) 768 bits, err := n1.Serialize(opt) 769 assert.Nil(err) 770 n2 := NewLabeledIPv6AddrPrefix(0, "", MPLSLabelStack{}) 771 err = n2.DecodeFromBytes(bits, opt) 772 assert.Nil(err) 773 assert.Equal(n2.PathIdentifier(), uint32(20)) 774 } 775 opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_RTC_UC: BGP_ADD_PATH_BOTH}} 776 { 777 rt, _ := ParseRouteTarget("100:100") 778 n1 := NewRouteTargetMembershipNLRI(65000, rt) 779 n1.SetPathLocalIdentifier(30) 780 bits, err := n1.Serialize(opt) 781 assert.Nil(err) 782 n2 := NewRouteTargetMembershipNLRI(0, nil) 783 err = n2.DecodeFromBytes(bits, opt) 784 assert.Nil(err) 785 assert.Equal(n2.PathIdentifier(), uint32(30)) 786 } 787 opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_EVPN: BGP_ADD_PATH_BOTH}} 788 { 789 n1 := NewEVPNNLRI(EVPN_ROUTE_TYPE_ETHERNET_AUTO_DISCOVERY, 790 &EVPNEthernetAutoDiscoveryRoute{NewRouteDistinguisherFourOctetAS(5, 6), 791 EthernetSegmentIdentifier{ESI_ARBITRARY, make([]byte, 9)}, 2, 2}) 792 n1.SetPathLocalIdentifier(40) 793 bits, err := n1.Serialize(opt) 794 assert.Nil(err) 795 n2 := NewEVPNNLRI(0, nil) 796 err = n2.DecodeFromBytes(bits, opt) 797 assert.Nil(err) 798 assert.Equal(n2.PathIdentifier(), uint32(40)) 799 } 800 opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_IPv4_ENCAP: BGP_ADD_PATH_BOTH}} 801 { 802 n1 := NewEncapNLRI("10.10.10.0") 803 n1.SetPathLocalIdentifier(50) 804 bits, err := n1.Serialize(opt) 805 assert.Nil(err) 806 n2 := NewEncapNLRI("") 807 err = n2.DecodeFromBytes(bits, opt) 808 assert.Nil(err) 809 assert.Equal(n2.PathIdentifier(), uint32(50)) 810 } 811 opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_FS_IPv4_UC: BGP_ADD_PATH_BOTH}} 812 { 813 n1 := NewFlowSpecIPv4Unicast([]FlowSpecComponentInterface{NewFlowSpecDestinationPrefix(NewIPAddrPrefix(24, "10.0.0.0"))}) 814 n1.SetPathLocalIdentifier(60) 815 bits, err := n1.Serialize(opt) 816 assert.Nil(err) 817 n2 := NewFlowSpecIPv4Unicast(nil) 818 err = n2.DecodeFromBytes(bits, opt) 819 assert.Nil(err) 820 assert.Equal(n2.PathIdentifier(), uint32(60)) 821 } 822 opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_OPAQUE: BGP_ADD_PATH_BOTH}} 823 { 824 n1 := NewOpaqueNLRI([]byte("key"), []byte("value")) 825 n1.SetPathLocalIdentifier(70) 826 bits, err := n1.Serialize(opt) 827 assert.Nil(err) 828 n2 := &OpaqueNLRI{} 829 err = n2.DecodeFromBytes(bits, opt) 830 assert.Nil(err) 831 assert.Equal(n2.PathIdentifier(), uint32(70)) 832 } 833 834 } 835 836 func Test_CompareFlowSpecNLRI(t *testing.T) { 837 assert := assert.New(t) 838 cmp, err := ParseFlowSpecComponents(RF_FS_IPv4_UC, "destination 10.0.0.2/32 source 10.0.0.1/32 destination-port ==3128 protocol tcp") 839 assert.Nil(err) 840 // Note: Use NewFlowSpecIPv4Unicast() for the consistent ordered rules. 841 n1 := &NewFlowSpecIPv4Unicast(cmp).FlowSpecNLRI 842 cmp, err = ParseFlowSpecComponents(RF_FS_IPv4_UC, "source 10.0.0.0/24 destination-port ==3128 protocol tcp") 843 assert.Nil(err) 844 n2 := &NewFlowSpecIPv4Unicast(cmp).FlowSpecNLRI 845 r, err := CompareFlowSpecNLRI(n1, n2) 846 assert.Nil(err) 847 assert.True(r > 0) 848 cmp, err = ParseFlowSpecComponents(RF_FS_IPv4_UC, "source 10.0.0.9/32 port ==80 ==8080 destination-port >8080&<8080 ==3128 source-port >1024 protocol ==udp ==tcp") 849 n3 := &NewFlowSpecIPv4Unicast(cmp).FlowSpecNLRI 850 assert.Nil(err) 851 cmp, err = ParseFlowSpecComponents(RF_FS_IPv4_UC, "destination 192.168.0.2/32") 852 n4 := &NewFlowSpecIPv4Unicast(cmp).FlowSpecNLRI 853 assert.Nil(err) 854 r, err = CompareFlowSpecNLRI(n3, n4) 855 assert.Nil(err) 856 assert.True(r < 0) 857 } 858 859 func Test_MpReachNLRIWithIPv4MappedIPv6Prefix(t *testing.T) { 860 assert := assert.New(t) 861 n1 := NewIPv6AddrPrefix(120, "::ffff:10.0.0.0") 862 buf1, err := n1.Serialize() 863 assert.Nil(err) 864 n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_IPv6_UC)) 865 assert.Nil(err) 866 err = n2.DecodeFromBytes(buf1) 867 assert.Nil(err) 868 869 assert.Equal(n1, n2) 870 871 label := NewMPLSLabelStack(2) 872 873 n3 := NewLabeledIPv6AddrPrefix(120, "::ffff:10.0.0.0", *label) 874 buf1, err = n3.Serialize() 875 assert.Nil(err) 876 n4, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_IPv6_MPLS)) 877 assert.Nil(err) 878 err = n4.DecodeFromBytes(buf1) 879 assert.Nil(err) 880 881 assert.Equal(n3, n4) 882 } 883 884 func Test_MpReachNLRIWithIPv6PrefixWithIPv4Peering(t *testing.T) { 885 assert := assert.New(t) 886 bufin := []byte{ 887 0x80, 0x0e, 0x1e, // flags(1), type(1), length(1) 888 0x00, 0x02, 0x01, 0x10, // afi(2), safi(1), nexthoplen(1) 889 0x00, 0x00, 0x00, 0x00, // nexthop(16) 890 0x00, 0x00, 0x00, 0x00, // = "::ffff:172.20.0.1" 891 0x00, 0x00, 0xff, 0xff, 892 0xac, 0x14, 0x00, 0x01, 893 0x00, // reserved(1) 894 0x40, 0x20, 0x01, 0x0d, // nlri(9) 895 0xb8, 0x00, 0x01, 0x00, // = "2001:db8:1:1::/64" 896 0x01, 897 } 898 // Test DecodeFromBytes() 899 p := &PathAttributeMpReachNLRI{} 900 err := p.DecodeFromBytes(bufin) 901 assert.Nil(err) 902 // Test decoded values 903 assert.Equal(BGPAttrFlag(0x80), p.Flags) 904 assert.Equal(BGPAttrType(0xe), p.Type) 905 assert.Equal(uint16(0x1e), p.Length) 906 assert.Equal(uint16(AFI_IP6), p.AFI) 907 assert.Equal(uint8(SAFI_UNICAST), p.SAFI) 908 assert.Equal(net.ParseIP("::ffff:172.20.0.1"), p.Nexthop) 909 assert.Equal(net.ParseIP(""), p.LinkLocalNexthop) 910 value := []AddrPrefixInterface{ 911 NewIPv6AddrPrefix(64, "2001:db8:1:1::"), 912 } 913 assert.Equal(value, p.Value) 914 // Set NextHop as IPv4 address (because IPv4 peering) 915 p.Nexthop = net.ParseIP("172.20.0.1") 916 // Test Serialize() 917 bufout, err := p.Serialize() 918 assert.Nil(err) 919 // Test serialised value 920 assert.Equal(bufin, bufout) 921 } 922 923 func Test_MpReachNLRIWithIPv6(t *testing.T) { 924 assert := assert.New(t) 925 bufin := []byte{ 926 0x90, 0x0e, 0x00, 0x1e, // flags(1), type(1), length(2), 927 0x00, 0x02, 0x01, 0x10, // afi(2), safi(1), nexthoplen(1) 928 0x20, 0x01, 0x0d, 0xb8, // nexthop(16) 929 0x00, 0x01, 0x00, 0x00, // = "2001:db8:1::1" 930 0x00, 0x00, 0x00, 0x00, 931 0x00, 0x00, 0x00, 0x01, 932 0x00, // reserved(1) 933 0x40, 0x20, 0x01, 0x0d, // nlri(9) 934 0xb8, 0x00, 0x53, 0x00, // = "2001:db8:53::/64" 935 0x00, 936 } 937 // Test DecodeFromBytes() 938 p := &PathAttributeMpReachNLRI{} 939 err := p.DecodeFromBytes(bufin) 940 assert.Nil(err) 941 // Test decoded values 942 assert.Equal(BGPAttrFlag(0x90), p.Flags) 943 assert.Equal(BGPAttrType(0xe), p.Type) 944 assert.Equal(uint16(0x1e), p.Length) 945 assert.Equal(uint16(AFI_IP6), p.AFI) 946 assert.Equal(uint8(SAFI_UNICAST), p.SAFI) 947 assert.Equal(net.ParseIP("2001:db8:1::1"), p.Nexthop) 948 value := []AddrPrefixInterface{ 949 NewIPv6AddrPrefix(64, "2001:db8:53::"), 950 } 951 assert.Equal(value, p.Value) 952 } 953 954 func Test_MpUnreachNLRIWithIPv6(t *testing.T) { 955 assert := assert.New(t) 956 bufin := []byte{ 957 0x90, 0x0f, 0x00, 0x0c, // flags(1), type(1), length(2), 958 0x00, 0x02, 0x01, // afi(2), safi(1), 959 0x40, 0x20, 0x01, 0x0d, // nlri(9) 960 0xb8, 0x00, 0x53, 0x00, // = "2001:db8:53::/64" 961 0x00, 962 } 963 // Test DecodeFromBytes() 964 p := &PathAttributeMpUnreachNLRI{} 965 err := p.DecodeFromBytes(bufin) 966 assert.Nil(err) 967 // Test decoded values 968 assert.Equal(BGPAttrFlag(0x90), p.Flags) 969 assert.Equal(BGPAttrType(0xf), p.Type) 970 assert.Equal(uint16(0x0c), p.Length) 971 assert.Equal(uint16(AFI_IP6), p.AFI) 972 assert.Equal(uint8(SAFI_UNICAST), p.SAFI) 973 value := []AddrPrefixInterface{ 974 NewIPv6AddrPrefix(64, "2001:db8:53::"), 975 } 976 assert.Equal(value, p.Value) 977 } 978 979 func Test_MpReachNLRIWithIPv6PrefixWithLinkLocalNexthop(t *testing.T) { 980 assert := assert.New(t) 981 bufin := []byte{ 982 0x80, 0x0e, 0x2c, // flags(1), type(1), length(1) 983 0x00, 0x02, 0x01, 0x20, // afi(2), safi(1), nexthoplen(1) 984 0x20, 0x01, 0x0d, 0xb8, // nexthop(32) 985 0x00, 0x01, 0x00, 0x00, // = "2001:db8:1::1" 986 0x00, 0x00, 0x00, 0x00, 987 0x00, 0x00, 0x00, 0x01, 988 0xfe, 0x80, 0x00, 0x00, // + "fe80::1" (link local) 989 0x00, 0x00, 0x00, 0x00, 990 0x00, 0x00, 0x00, 0x00, 991 0x00, 0x00, 0x00, 0x01, 992 0x00, // reserved(1) 993 0x30, 0x20, 0x10, 0x0a, // nlri(7) 994 0xb8, 0x00, 0x01, // = "2010:ab8:1::/48" 995 } 996 // Test DecodeFromBytes() 997 p := &PathAttributeMpReachNLRI{} 998 err := p.DecodeFromBytes(bufin) 999 assert.Nil(err) 1000 // Test decoded values 1001 assert.Equal(BGPAttrFlag(0x80), p.Flags) 1002 assert.Equal(BGPAttrType(0xe), p.Type) 1003 assert.Equal(uint16(0x2c), p.Length) 1004 assert.Equal(uint16(AFI_IP6), p.AFI) 1005 assert.Equal(uint8(SAFI_UNICAST), p.SAFI) 1006 assert.Equal(net.ParseIP("2001:db8:1::1"), p.Nexthop) 1007 assert.Equal(net.ParseIP("fe80::1"), p.LinkLocalNexthop) 1008 value := []AddrPrefixInterface{ 1009 NewIPv6AddrPrefix(48, "2010:ab8:1::"), 1010 } 1011 assert.Equal(value, p.Value) 1012 // Test Serialize() 1013 bufout, err := p.Serialize() 1014 assert.Nil(err) 1015 // Test serialised value 1016 assert.Equal(bufin, bufout) 1017 } 1018 1019 func Test_MpReachNLRIWithVPNv4Prefix(t *testing.T) { 1020 assert := assert.New(t) 1021 bufin := []byte{ 1022 0x80, 0x0e, 0x20, // flags(1), type(1), length(1) 1023 0x00, 0x01, 0x80, 0x0c, // afi(2), safi(1), nexthoplen(1) 1024 0x00, 0x00, 0x00, 0x00, // nexthop(12) 1025 0x00, 0x00, 0x00, 0x00, // = (rd:"0:0",) "172.20.0.1" 1026 0xac, 0x14, 0x00, 0x01, 1027 0x00, // reserved(1) 1028 0x70, 0x00, 0x01, 0x01, // nlri(15) 1029 0x00, 0x00, 0xfd, 0xe8, // = label:16, rd:"65000:100", prefix:"10.1.1.0/24" 1030 0x00, 0x00, 0x00, 0x64, 1031 0x0a, 0x01, 0x01, 1032 } 1033 // Test DecodeFromBytes() 1034 p := &PathAttributeMpReachNLRI{} 1035 err := p.DecodeFromBytes(bufin) 1036 assert.Nil(err) 1037 // Test decoded values 1038 assert.Equal(BGPAttrFlag(0x80), p.Flags) 1039 assert.Equal(BGPAttrType(0xe), p.Type) 1040 assert.Equal(uint16(0x20), p.Length) 1041 assert.Equal(uint16(AFI_IP), p.AFI) 1042 assert.Equal(uint8(SAFI_MPLS_VPN), p.SAFI) 1043 assert.Equal(net.ParseIP("172.20.0.1").To4(), p.Nexthop) 1044 assert.Equal(net.ParseIP(""), p.LinkLocalNexthop) 1045 value := []AddrPrefixInterface{ 1046 NewLabeledVPNIPAddrPrefix(24, "10.1.1.0", *NewMPLSLabelStack(16), 1047 NewRouteDistinguisherTwoOctetAS(65000, 100)), 1048 } 1049 assert.Equal(value, p.Value) 1050 // Test Serialize() 1051 bufout, err := p.Serialize() 1052 assert.Nil(err) 1053 // Test serialised value 1054 assert.Equal(bufin, bufout) 1055 } 1056 1057 func Test_MpReachNLRIWithVPNv6Prefix(t *testing.T) { 1058 assert := assert.New(t) 1059 bufin := []byte{ 1060 0x80, 0x0e, 0x39, // flags(1), type(1), length(1) 1061 0x00, 0x02, 0x80, 0x18, // afi(2), safi(1), nexthoplen(1) 1062 0x00, 0x00, 0x00, 0x00, // nexthop(24) 1063 0x00, 0x00, 0x00, 0x00, // = (rd:"0:0",) "2001:db8:1::1" 1064 0x20, 0x01, 0x0d, 0xb8, 1065 0x00, 0x01, 0x00, 0x00, 1066 0x00, 0x00, 0x00, 0x00, 1067 0x00, 0x00, 0x00, 0x01, 1068 0x00, // reserved(1) 1069 0xd4, 0x00, 0x01, 0x01, // nlri(28) 1070 0x00, 0x00, 0xfd, 0xe8, // = label:16, rd:"65000:100", prefix:"2001:1::/124" 1071 0x00, 0x00, 0x00, 0x64, 1072 0x20, 0x01, 0x00, 0x01, 1073 0x00, 0x00, 0x00, 0x00, 1074 0x00, 0x00, 0x00, 0x00, 1075 0x00, 0x00, 0x00, 0x00, 1076 } 1077 // Test DecodeFromBytes() 1078 p := &PathAttributeMpReachNLRI{} 1079 err := p.DecodeFromBytes(bufin) 1080 assert.Nil(err) 1081 // Test decoded values 1082 assert.Equal(BGPAttrFlag(0x80), p.Flags) 1083 assert.Equal(BGPAttrType(0xe), p.Type) 1084 assert.Equal(uint16(0x39), p.Length) 1085 assert.Equal(uint16(AFI_IP6), p.AFI) 1086 assert.Equal(uint8(SAFI_MPLS_VPN), p.SAFI) 1087 assert.Equal(net.ParseIP("2001:db8:1::1"), p.Nexthop) 1088 assert.Equal(net.ParseIP(""), p.LinkLocalNexthop) 1089 value := []AddrPrefixInterface{ 1090 NewLabeledVPNIPv6AddrPrefix(124, "2001:1::", *NewMPLSLabelStack(16), 1091 NewRouteDistinguisherTwoOctetAS(65000, 100)), 1092 } 1093 assert.Equal(value, p.Value) 1094 // Test Serialize() 1095 bufout, err := p.Serialize() 1096 assert.Nil(err) 1097 // Test serialised value 1098 assert.Equal(bufin, bufout) 1099 } 1100 1101 func Test_MpReachNLRIWithIPv4PrefixWithIPv6Nexthop(t *testing.T) { 1102 assert := assert.New(t) 1103 bufin := []byte{ 1104 0x80, 0x0e, 0x19, // flags(1), type(1), length(1) 1105 0x00, 0x01, 0x01, 0x10, // afi(1), safi(1), nexthoplen(1) 1106 0x20, 0x01, 0x0d, 0xb8, // nexthop(32) 1107 0x00, 0x01, 0x00, 0x00, // = "2001:db8:1::1" 1108 0x00, 0x00, 0x00, 0x00, 1109 0x00, 0x00, 0x00, 0x01, 1110 0x00, // reserved(1) 1111 0x18, 0xc0, 0xa8, 0x0a, // nlri(7) 1112 } 1113 // Test DecodeFromBytes() 1114 p := &PathAttributeMpReachNLRI{} 1115 err := p.DecodeFromBytes(bufin) 1116 assert.Nil(err) 1117 // Test decoded values 1118 assert.Equal(BGPAttrFlag(0x80), p.Flags) 1119 assert.Equal(BGPAttrType(0xe), p.Type) 1120 assert.Equal(uint16(0x19), p.Length) 1121 assert.Equal(uint16(AFI_IP), p.AFI) 1122 assert.Equal(uint8(SAFI_UNICAST), p.SAFI) 1123 assert.Equal(net.ParseIP("2001:db8:1::1"), p.Nexthop) 1124 value := []AddrPrefixInterface{ 1125 NewIPAddrPrefix(24, "192.168.10.0"), 1126 } 1127 assert.Equal(value, p.Value) 1128 // Test Serialize() 1129 bufout, err := p.Serialize() 1130 assert.Nil(err) 1131 // Test serialised value 1132 assert.Equal(bufin, bufout) 1133 } 1134 1135 func Test_MpReachNLRIWithImplicitPrefix(t *testing.T) { 1136 assert := assert.New(t) 1137 bufin := []byte{ 1138 0x80, 0x0e, 0x11, // flags(1), type(1), length(1) 1139 0x10, // nexthoplen(1) 1140 0x20, 0x01, 0x0d, 0xb8, // nexthop(32) 1141 0x00, 0x01, 0x00, 0x00, // = "2001:db8:1::1" 1142 0x00, 0x00, 0x00, 0x00, 1143 0x00, 0x00, 0x00, 0x01, 1144 } 1145 prefix := NewIPAddrPrefix(24, "192.168.10.0") 1146 // Test DecodeFromBytes() 1147 p := &PathAttributeMpReachNLRI{} 1148 option := &MarshallingOption{ImplicitPrefix: prefix} 1149 err := p.DecodeFromBytes(bufin, option) 1150 assert.Nil(err) 1151 // Test decoded values 1152 assert.Equal(BGPAttrFlag(0x80), p.Flags) 1153 assert.Equal(BGPAttrType(0xe), p.Type) 1154 assert.Equal(uint16(0x11), p.Length) 1155 assert.Equal(prefix.AFI(), p.AFI) 1156 assert.Equal(prefix.SAFI(), p.SAFI) 1157 assert.Equal(net.ParseIP("2001:db8:1::1"), p.Nexthop) 1158 value := []AddrPrefixInterface{prefix} 1159 assert.Equal(value, p.Value) 1160 // Test Serialize() 1161 bufout, err := p.Serialize(option) 1162 assert.Nil(err) 1163 // Test serialised value 1164 assert.Equal(bufin, bufout) 1165 } 1166 1167 func Test_ParseRouteDistinguisher(t *testing.T) { 1168 assert := assert.New(t) 1169 1170 rd, _ := ParseRouteDistinguisher("100:1000") 1171 rdType0, ok := rd.(*RouteDistinguisherTwoOctetAS) 1172 if !ok { 1173 t.Fatal("Type of RD interface is not RouteDistinguisherTwoOctetAS") 1174 } 1175 1176 assert.Equal(uint16(100), rdType0.Admin) 1177 assert.Equal(uint32(1000), rdType0.Assigned) 1178 1179 rd, _ = ParseRouteDistinguisher("10.0.0.0:100") 1180 rdType1, ok := rd.(*RouteDistinguisherIPAddressAS) 1181 if !ok { 1182 t.Fatal("Type of RD interface is not RouteDistinguisherIPAddressAS") 1183 } 1184 1185 assert.Equal("10.0.0.0", rdType1.Admin.String()) 1186 assert.Equal(uint16(100), rdType1.Assigned) 1187 1188 rd, _ = ParseRouteDistinguisher("100.1000:10000") 1189 rdType2, ok := rd.(*RouteDistinguisherFourOctetAS) 1190 if !ok { 1191 t.Fatal("Type of RD interface is not RouteDistinguisherFourOctetAS") 1192 } 1193 1194 assert.Equal(uint32((100<<16)|1000), rdType2.Admin) 1195 assert.Equal(uint16(10000), rdType2.Assigned) 1196 } 1197 1198 func TestParseVPNPrefix(t *testing.T) { 1199 tests := []struct { 1200 name string 1201 prefix string 1202 valid bool 1203 rd RouteDistinguisherInterface 1204 ipPrefix string 1205 }{ 1206 { 1207 name: "test valid RD type 0 VPNv4 prefix", 1208 prefix: "100:100:10.0.0.1/32", 1209 valid: true, 1210 rd: NewRouteDistinguisherTwoOctetAS(uint16(100), uint32(100)), 1211 ipPrefix: "10.0.0.1/32", 1212 }, 1213 { 1214 name: "test valid RD type 1 VPNv4 prefix", 1215 prefix: "1.1.1.1:100:10.0.0.1/32", 1216 valid: true, 1217 rd: NewRouteDistinguisherIPAddressAS("1.1.1.1", uint16(100)), 1218 ipPrefix: "10.0.0.1/32", 1219 }, 1220 { 1221 name: "test valid RD type 2 VPNv4 prefix", 1222 prefix: "0.54233:100:10.0.0.1/32", 1223 valid: true, 1224 rd: NewRouteDistinguisherFourOctetAS(uint32(54233), uint16(100)), 1225 ipPrefix: "10.0.0.1/32", 1226 }, 1227 { 1228 name: "test invalid VPNv4 prefix", 1229 prefix: "100:10.0.0.1/32", 1230 valid: false, 1231 rd: nil, 1232 ipPrefix: "", 1233 }, 1234 { 1235 name: "test valid RD type 0 VPNv6 prefix", 1236 prefix: "100:100:100:1::/64", 1237 valid: true, 1238 rd: NewRouteDistinguisherTwoOctetAS(uint16(100), uint32(100)), 1239 ipPrefix: "100:1::/64", 1240 }, 1241 { 1242 name: "test valid RD type 1 VPNv6 prefix", 1243 prefix: "1.1.1.1:100:100:1::/64", 1244 valid: true, 1245 rd: NewRouteDistinguisherIPAddressAS("1.1.1.1", uint16(100)), 1246 ipPrefix: "100:1::/64", 1247 }, 1248 { 1249 name: "test valid RD type 2 VPNv6 prefix", 1250 prefix: "0.54233:100:100:1::/64", 1251 valid: true, 1252 rd: NewRouteDistinguisherFourOctetAS(uint32(54233), uint16(100)), 1253 ipPrefix: "100:1::/64", 1254 }, 1255 { 1256 name: "test invalid VPNv6 prefix", 1257 prefix: "100:1::/64", 1258 valid: false, 1259 rd: nil, 1260 ipPrefix: "", 1261 }, 1262 } 1263 1264 for _, tt := range tests { 1265 t.Run(tt.name, func(t *testing.T) { 1266 rd, _, network, err := ParseVPNPrefix(tt.prefix) 1267 if !tt.valid { 1268 assert.NotNil(t, err) 1269 return 1270 } 1271 1272 assert.Nil(t, err) 1273 assert.Equal(t, tt.rd, rd) 1274 assert.Equal(t, tt.ipPrefix, network.String()) 1275 }) 1276 } 1277 } 1278 1279 func TestContainsCIDR(t *testing.T) { 1280 tests := []struct { 1281 name string 1282 prefix1 string 1283 prefix2 string 1284 result bool 1285 }{ 1286 { 1287 name: "v4 prefix2 is a subnet of prefix1", 1288 prefix1: "172.17.0.0/16", 1289 prefix2: "172.17.192.0/18", 1290 result: true, 1291 }, 1292 { 1293 name: "v4 prefix2 is a supernet of prefix1", 1294 prefix1: "172.17.191.0/18", 1295 prefix2: "172.17.0.0/16", 1296 result: false, 1297 }, 1298 { 1299 name: "v4 prefix2 is not a subnet of prefix1", 1300 prefix1: "10.10.20.0/30", 1301 prefix2: "10.10.30.3/32", 1302 result: false, 1303 }, 1304 { 1305 name: "v4 prefix2 is equal to prefix1", 1306 prefix1: "10.10.20.0/30", 1307 prefix2: "10.10.20.0/30", 1308 result: true, 1309 }, 1310 { 1311 name: "v6 prefix2 is not a subnet of prefix1", 1312 prefix1: "1::/64", 1313 prefix2: "2::/72", 1314 result: false, 1315 }, 1316 { 1317 name: "v6 prefix2 is a supernet of prefix1", 1318 prefix1: "1::/64", 1319 prefix2: "1::/32", 1320 result: false, 1321 }, 1322 { 1323 name: "v6 prefix2 is a subnet of prefix1", 1324 prefix1: "1::/64", 1325 prefix2: "1::/112", 1326 result: true, 1327 }, 1328 { 1329 name: "v6 prefix2 is equal to prefix1", 1330 prefix1: "100:100::/64", 1331 prefix2: "100:100::/64", 1332 result: true, 1333 }, 1334 } 1335 1336 for _, tt := range tests { 1337 t.Run(tt.name, func(t *testing.T) { 1338 _, prefixNet1, _ := net.ParseCIDR(tt.prefix1) 1339 _, prefixNet2, _ := net.ParseCIDR(tt.prefix2) 1340 1341 result := ContainsCIDR(prefixNet1, prefixNet2) 1342 assert.Equal(t, tt.result, result) 1343 }) 1344 } 1345 } 1346 1347 func Test_ParseEthernetSegmentIdentifier(t *testing.T) { 1348 assert := assert.New(t) 1349 1350 // "single-homed" 1351 esiZero := EthernetSegmentIdentifier{} 1352 args := make([]string, 0) 1353 esi, err := ParseEthernetSegmentIdentifier(args) 1354 assert.Nil(err) 1355 assert.Equal(esiZero, esi) 1356 args = []string{"single-homed"} 1357 esi, err = ParseEthernetSegmentIdentifier(args) 1358 assert.Nil(err) 1359 assert.Equal(esiZero, esi) 1360 1361 // ESI_ARBITRARY 1362 args = []string{"ARBITRARY", "11:22:33:44:55:66:77:88:99"} // omit "ESI_" 1363 esi, err = ParseEthernetSegmentIdentifier(args) 1364 assert.Nil(err) 1365 assert.Equal(EthernetSegmentIdentifier{ 1366 Type: ESI_ARBITRARY, 1367 Value: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 1368 }, esi) 1369 1370 // ESI_LACP 1371 args = []string{"lacp", "aa:bb:cc:dd:ee:ff", strconv.FormatInt(0x1122, 10)} // lower case 1372 esi, err = ParseEthernetSegmentIdentifier(args) 1373 assert.Nil(err) 1374 assert.Equal(EthernetSegmentIdentifier{ 1375 Type: ESI_LACP, 1376 Value: []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x00}, 1377 }, esi) 1378 1379 // ESI_MSTP 1380 args = []string{"esi_mstp", "aa:bb:cc:dd:ee:ff", strconv.FormatInt(0x1122, 10)} // omit "ESI_" + lower case 1381 esi, err = ParseEthernetSegmentIdentifier(args) 1382 assert.Nil(err) 1383 assert.Equal(EthernetSegmentIdentifier{ 1384 Type: ESI_MSTP, 1385 Value: []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x00}, 1386 }, esi) 1387 1388 // ESI_MAC 1389 args = []string{"ESI_MAC", "aa:bb:cc:dd:ee:ff", strconv.FormatInt(0x112233, 10)} 1390 esi, err = ParseEthernetSegmentIdentifier(args) 1391 assert.Nil(err) 1392 assert.Equal(EthernetSegmentIdentifier{ 1393 Type: ESI_MAC, 1394 Value: []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33}, 1395 }, esi) 1396 1397 // ESI_ROUTERID 1398 args = []string{"ESI_ROUTERID", "1.1.1.1", strconv.FormatInt(0x11223344, 10)} 1399 esi, err = ParseEthernetSegmentIdentifier(args) 1400 assert.Nil(err) 1401 assert.Equal(EthernetSegmentIdentifier{ 1402 Type: ESI_ROUTERID, 1403 Value: []byte{0x01, 0x01, 0x01, 0x01, 0x11, 0x22, 0x33, 0x44, 0x00}, 1404 }, esi) 1405 1406 // ESI_AS 1407 args = []string{"ESI_AS", strconv.FormatInt(0xaabbccdd, 10), strconv.FormatInt(0x11223344, 10)} 1408 esi, err = ParseEthernetSegmentIdentifier(args) 1409 assert.Nil(err) 1410 assert.Equal(EthernetSegmentIdentifier{ 1411 Type: ESI_AS, 1412 Value: []byte{0xaa, 0xbb, 0xcc, 0xdd, 0x11, 0x22, 0x33, 0x44, 0x00}, 1413 }, esi) 1414 1415 // Other 1416 args = []string{"99", "11:22:33:44:55:66:77:88:99"} 1417 esi, err = ParseEthernetSegmentIdentifier(args) 1418 assert.Nil(err) 1419 assert.Equal(EthernetSegmentIdentifier{ 1420 Type: ESIType(99), 1421 Value: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 1422 }, esi) 1423 } 1424 1425 func TestParseBogusShortData(t *testing.T) { 1426 var bodies = []BGPBody{ 1427 &BGPOpen{}, 1428 &BGPUpdate{}, 1429 &BGPNotification{}, 1430 &BGPKeepAlive{}, 1431 &BGPRouteRefresh{}, 1432 } 1433 1434 for _, b := range bodies { 1435 b.DecodeFromBytes([]byte{0}) 1436 } 1437 } 1438 1439 func TestFuzzCrashers(t *testing.T) { 1440 var crashers = []string{ 1441 "000000000000000000\x01", 1442 } 1443 1444 for _, f := range crashers { 1445 ParseBGPMessage([]byte(f)) 1446 } 1447 } 1448 1449 func TestParseMessageWithBadLength(t *testing.T) { 1450 type testCase struct { 1451 fname string 1452 data []byte 1453 } 1454 1455 var cases []testCase 1456 root := filepath.Join("testdata", "bad-len") 1457 err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { 1458 if err != nil { 1459 return err 1460 } 1461 if info.IsDir() { 1462 if path == root { 1463 return nil 1464 } 1465 return filepath.SkipDir 1466 } 1467 fname := filepath.Base(path) 1468 if strings.ContainsRune(fname, '.') { 1469 return nil 1470 } 1471 data, err := os.ReadFile(path) 1472 if err != nil { 1473 return err 1474 } 1475 cases = append(cases, testCase{ 1476 fname: fname, 1477 data: data, 1478 }) 1479 return nil 1480 }) 1481 if err != nil { 1482 t.Fatal(err) 1483 } 1484 1485 for _, tt := range cases { 1486 t.Run(tt.fname, func(t *testing.T) { 1487 msg, err := ParseBGPMessage(tt.data) 1488 if err == nil { 1489 _, err = msg.Serialize() 1490 if err != nil { 1491 t.Fatal("failed to serialize:", err) 1492 } 1493 return 1494 } 1495 1496 switch e := err.(type) { 1497 case *MessageError: 1498 switch e.TypeCode { 1499 case BGP_ERROR_MESSAGE_HEADER_ERROR: 1500 if e.SubTypeCode != BGP_ERROR_SUB_BAD_MESSAGE_LENGTH { 1501 t.Fatalf("got unexpected message type and data: %v", e) 1502 } 1503 } 1504 default: 1505 t.Fatalf("got unexpected error type %T: %v", err, err) 1506 } 1507 1508 }) 1509 } 1510 } 1511 1512 func TestNormalizeFlowSpecOpValues(t *testing.T) { 1513 tests := []struct { 1514 msg string 1515 args []string 1516 want []string 1517 }{ 1518 { 1519 msg: "valid match", 1520 args: []string{" & <=80", " tcp != udp ", " =! SA & =U! F", " = is-fragment+last-fragment"}, 1521 want: []string{"<=80", "tcp", "!=udp", "=!SA", "&=U", "!F", "=is-fragment+last-fragment"}, 1522 }, 1523 { 1524 msg: "RFC5575 trims & prefix", 1525 args: []string{"&<=80"}, 1526 want: []string{"<=80"}, 1527 }, 1528 } 1529 1530 for _, tt := range tests { 1531 t.Run(tt.msg, func(t *testing.T) { 1532 got := normalizeFlowSpecOpValues(tt.args) 1533 assert.Equal(t, tt.want, got) 1534 }) 1535 } 1536 } 1537 1538 func Test_PathAttributeNextHop(t *testing.T) { 1539 f := func(addr string) { 1540 b, _ := NewPathAttributeNextHop(addr).Serialize() 1541 p := PathAttributeNextHop{} 1542 p.DecodeFromBytes(b) 1543 assert.Equal(t, addr, p.Value.String()) 1544 } 1545 f("192.0.2.1") 1546 f("2001:db8::68") 1547 } 1548 1549 func Test_LsTLVDecode(t *testing.T) { 1550 assert := assert.New(t) 1551 1552 var tests = []struct { 1553 in []byte 1554 t LsTLVType 1555 l uint16 1556 v []byte 1557 err bool 1558 }{ 1559 {[]byte{0x01, 0x09, 0x00, 0x1, 0xef}, LS_TLV_IP_REACH_INFO, 5, []byte{0xef}, false}, 1560 {[]byte{0x01, 0x09, 0x00, 0x0}, LS_TLV_IP_REACH_INFO, 4, []byte{}, false}, 1561 {[]byte{0x01, 0x09, 0x01, 0xff}, LS_TLV_IP_REACH_INFO, 0, []byte{}, true}, 1562 {[]byte{0x01, 0x09, 0x01}, LS_TLV_L2_BUNDLE_MEMBER_TLV, 1, []byte{}, true}, 1563 } 1564 1565 for _, test := range tests { 1566 tlv := &LsTLV{} 1567 1568 got, err := tlv.DecodeFromBytes(test.in) 1569 if test.err { 1570 assert.Error(err) 1571 continue 1572 } else { 1573 assert.NoError(err) 1574 } 1575 assert.Equal(tlv.Len(), int(test.l)) 1576 assert.Equal(got, test.v) 1577 } 1578 } 1579 1580 func Test_LsTLVSerialize(t *testing.T) { 1581 assert := assert.New(t) 1582 1583 var tests = []struct { 1584 tlv LsTLV 1585 val []byte 1586 want []byte 1587 err bool 1588 }{ 1589 {LsTLV{Type: LS_TLV_SID_LABEL_TLV, Length: 2}, []byte{0x11, 0x22}, []byte{0x04, 0x89, 0x00, 0x02, 0x11, 0x22}, false}, 1590 {LsTLV{Type: LS_TLV_SID_LABEL_TLV, Length: 2}, []byte{0x11}, nil, true}, 1591 {LsTLV{Type: LS_TLV_IGP_FLAGS, Length: 0}, []byte{}, []byte{0x04, 0x80, 0x00, 0x00}, false}, 1592 } 1593 1594 for _, test := range tests { 1595 got, err := test.tlv.Serialize(test.val) 1596 if test.err { 1597 assert.Error(err) 1598 } else { 1599 assert.NoError(err) 1600 } 1601 1602 assert.Equal(got, test.want) 1603 } 1604 } 1605 1606 func Test_LsTLVLinkID(t *testing.T) { 1607 assert := assert.New(t) 1608 1609 var tests = []struct { 1610 in []byte 1611 want string 1612 serialize bool 1613 err bool 1614 }{ 1615 {[]byte{0x01, 0x02, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02}, `{"type":258,"local_link_id":1,"remote_link_id":2}`, true, false}, 1616 {[]byte{0x01, 0x02, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0xFF}, `{"type":258,"local_link_id":1,"remote_link_id":2}`, false, false}, 1617 {[]byte{0x01, 0x02, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, "", false, true}, 1618 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", false, true}, 1619 } 1620 1621 for _, test := range tests { 1622 tlv := LsTLVLinkID{} 1623 if test.err { 1624 assert.Error(tlv.DecodeFromBytes(test.in)) 1625 continue 1626 } else { 1627 assert.NoError(tlv.DecodeFromBytes(test.in)) 1628 } 1629 1630 got, err := tlv.MarshalJSON() 1631 assert.NoError(err) 1632 assert.Equal(got, []byte(test.want)) 1633 1634 if test.serialize { 1635 s, err := tlv.Serialize() 1636 assert.NoError(err) 1637 assert.Equal(test.in, s) 1638 } 1639 } 1640 } 1641 1642 func Test_LsTLVIPv4InterfaceAddr(t *testing.T) { 1643 assert := assert.New(t) 1644 1645 var tests = []struct { 1646 in []byte 1647 want string 1648 serialize bool 1649 err bool 1650 }{ 1651 {[]byte{0x01, 0x03, 0x00, 0x04, 0x01, 0x01, 0x01, 0x01}, `{"type":259,"ipv4_interface_address":"1.1.1.1"}`, true, false}, 1652 {[]byte{0x01, 0x03, 0x00, 0x04, 0x0a, 0x0a, 0x0a, 0x0a, 0x12}, `{"type":259,"ipv4_interface_address":"10.10.10.10"}`, false, false}, 1653 {[]byte{0x01, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00}, "", false, true}, 1654 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", false, true}, 1655 } 1656 1657 for _, test := range tests { 1658 tlv := LsTLVIPv4InterfaceAddr{} 1659 if test.err { 1660 assert.Error(tlv.DecodeFromBytes(test.in)) 1661 continue 1662 } else { 1663 assert.NoError(tlv.DecodeFromBytes(test.in)) 1664 } 1665 1666 got, err := tlv.MarshalJSON() 1667 assert.NoError(err) 1668 assert.Equal(got, []byte(test.want)) 1669 1670 if test.serialize { 1671 s, err := tlv.Serialize() 1672 assert.NoError(err) 1673 assert.Equal(test.in, s) 1674 } 1675 } 1676 } 1677 1678 func Test_LsTLVIPv4NeighborAddr(t *testing.T) { 1679 assert := assert.New(t) 1680 1681 var tests = []struct { 1682 in []byte 1683 want string 1684 serialize bool 1685 err bool 1686 }{ 1687 {[]byte{0x01, 0x04, 0x00, 0x04, 0x01, 0x01, 0x01, 0x01}, `{"type":260,"ipv4_neighbor_address":"1.1.1.1"}`, true, false}, 1688 {[]byte{0x01, 0x04, 0x00, 0x04, 0x0a, 0x0a, 0x0a, 0x0a, 0x12}, `{"type":260,"ipv4_neighbor_address":"10.10.10.10"}`, false, false}, 1689 {[]byte{0x01, 0x04, 0x00, 0x03, 0x00, 0x00, 0x00}, "", false, true}, 1690 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", false, true}, 1691 } 1692 1693 for _, test := range tests { 1694 tlv := LsTLVIPv4NeighborAddr{} 1695 if test.err { 1696 assert.Error(tlv.DecodeFromBytes(test.in)) 1697 continue 1698 } else { 1699 assert.NoError(tlv.DecodeFromBytes(test.in)) 1700 } 1701 1702 got, err := tlv.MarshalJSON() 1703 assert.NoError(err) 1704 assert.Equal(got, []byte(test.want)) 1705 1706 if test.serialize { 1707 s, err := tlv.Serialize() 1708 assert.NoError(err) 1709 assert.Equal(test.in, s) 1710 } 1711 } 1712 } 1713 1714 func Test_LsTLVIPv6InterfaceAddr(t *testing.T) { 1715 assert := assert.New(t) 1716 1717 var tests = []struct { 1718 in []byte 1719 want string 1720 serialize bool 1721 err bool 1722 }{ 1723 {[]byte{0x01, 0x05, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF}, `{"type":261,"ipv6_interface_address":"2001:db8::beef"}`, true, false}, 1724 {[]byte{0x01, 0x05, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF, 0xFF}, `{"type":261,"ipv6_interface_address":"2001:db8::beef"}`, false, false}, 1725 {[]byte{0x01, 0x05, 0x00, 0x10, 0xfe, 0x80, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF}, "", false, true}, 1726 {[]byte{0x01, 0x05, 0x00, 0x03, 0x00, 0x00, 0x00}, "", false, true}, 1727 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", false, true}, 1728 } 1729 1730 for _, test := range tests { 1731 tlv := LsTLVIPv6InterfaceAddr{} 1732 if test.err { 1733 assert.Error(tlv.DecodeFromBytes(test.in)) 1734 continue 1735 } else { 1736 assert.NoError(tlv.DecodeFromBytes(test.in)) 1737 } 1738 1739 got, err := tlv.MarshalJSON() 1740 assert.NoError(err) 1741 assert.Equal(got, []byte(test.want)) 1742 1743 if test.serialize { 1744 s, err := tlv.Serialize() 1745 assert.NoError(err) 1746 assert.Equal(test.in, s) 1747 } 1748 } 1749 } 1750 1751 func Test_LsTLVIPv6NeighborAddr(t *testing.T) { 1752 assert := assert.New(t) 1753 1754 var tests = []struct { 1755 in []byte 1756 want string 1757 serialize bool 1758 err bool 1759 }{ 1760 {[]byte{0x01, 0x06, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF}, `{"type":262,"ipv6_neighbor_address":"2001:db8::beef"}`, true, false}, 1761 {[]byte{0x01, 0x06, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF, 0xFF}, `{"type":262,"ipv6_neighbor_address":"2001:db8::beef"}`, false, false}, 1762 {[]byte{0x01, 0x06, 0x00, 0x10, 0xfe, 0x81, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF}, "", false, true}, 1763 {[]byte{0x01, 0x06, 0x00, 0x03, 0x00, 0x00, 0x00}, "", false, true}, 1764 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", false, true}, 1765 } 1766 1767 for _, test := range tests { 1768 tlv := LsTLVIPv6NeighborAddr{} 1769 if test.err { 1770 assert.Error(tlv.DecodeFromBytes(test.in)) 1771 continue 1772 } else { 1773 assert.NoError(tlv.DecodeFromBytes(test.in)) 1774 } 1775 1776 got, err := tlv.MarshalJSON() 1777 assert.NoError(err) 1778 assert.Equal(got, []byte(test.want)) 1779 1780 if test.serialize { 1781 s, err := tlv.Serialize() 1782 assert.NoError(err) 1783 assert.Equal(test.in, s) 1784 } 1785 } 1786 } 1787 1788 func Test_LsTLVNodeFlagBits(t *testing.T) { 1789 assert := assert.New(t) 1790 1791 var tests = []struct { 1792 in []byte 1793 want string 1794 err bool 1795 }{ 1796 {[]byte{0x04, 0x00, 0x00, 0x01, 0xFF}, `{"type":1024,"node_flags":"{Node Flags: XXVRBETO}"}`, false}, 1797 {[]byte{0x04, 0x00, 0x00, 0x01, 0x80}, `{"type":1024,"node_flags":"{Node Flags: *******O}"}`, false}, 1798 {[]byte{0x04, 0x00, 0x00, 0x01, 0x80, 0xAA}, `{"type":1024,"node_flags":"{Node Flags: *******O}"}`, false}, 1799 {[]byte{0x04, 0x00, 0x00, 0x02, 0x80, 0x44}, "", true}, 1800 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 1801 } 1802 1803 for _, test := range tests { 1804 tlv := LsTLVNodeFlagBits{} 1805 if test.err { 1806 assert.Error(tlv.DecodeFromBytes(test.in)) 1807 continue 1808 } else { 1809 assert.NoError(tlv.DecodeFromBytes(test.in)) 1810 } 1811 1812 got, err := tlv.MarshalJSON() 1813 assert.NoError(err) 1814 assert.Equal(got, []byte(test.want)) 1815 } 1816 } 1817 1818 func Test_LsTLVNodeName(t *testing.T) { 1819 assert := assert.New(t) 1820 1821 var tests = []struct { 1822 in []byte 1823 want string 1824 err bool 1825 }{ 1826 {[]byte{0x04, 0x02, 0x00, 0x03, 0x72, 0x74, 0x72}, `{"type":1026,"node_name":"rtr"}`, false}, 1827 {[]byte{0x04, 0x02, 0x00, 0x03, 0x72, 0x74, 0x72, 0x00}, `{"type":1026,"node_name":"rtr"}`, false}, 1828 {[]byte{0x04, 0x02, 0x00, 0x00}, "", true}, 1829 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 1830 } 1831 1832 for _, test := range tests { 1833 tlv := LsTLVNodeName{} 1834 if test.err { 1835 assert.Error(tlv.DecodeFromBytes(test.in)) 1836 continue 1837 } else { 1838 assert.NoError(tlv.DecodeFromBytes(test.in)) 1839 } 1840 1841 got, err := tlv.MarshalJSON() 1842 assert.NoError(err) 1843 assert.Equal(got, []byte(test.want)) 1844 } 1845 } 1846 1847 func Test_LsTLVIsisArea(t *testing.T) { 1848 assert := assert.New(t) 1849 1850 var tests = []struct { 1851 in []byte 1852 want string 1853 err bool 1854 }{ 1855 {[]byte{0x04, 0x03, 0x00, 0x03, 0x72, 0x74, 0x72}, `{"type":1027,"isis_area_id":"[114 116 114]"}`, false}, 1856 {[]byte{0x04, 0x03, 0x00, 0x03, 0x72, 0x74, 0x72, 0x44}, `{"type":1027,"isis_area_id":"[114 116 114]"}`, false}, 1857 {[]byte{0x04, 0x03, 0x00, 0x00}, "", true}, 1858 {[]byte{0x04, 0x03, 0x00, 0x0E, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, "", true}, 1859 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 1860 } 1861 1862 for _, test := range tests { 1863 tlv := LsTLVIsisArea{} 1864 if test.err { 1865 assert.Error(tlv.DecodeFromBytes(test.in)) 1866 continue 1867 } else { 1868 assert.NoError(tlv.DecodeFromBytes(test.in)) 1869 } 1870 1871 got, err := tlv.MarshalJSON() 1872 assert.NoError(err) 1873 assert.Equal(got, []byte(test.want)) 1874 } 1875 } 1876 1877 func Test_LsTLVLocalIPv4RouterID(t *testing.T) { 1878 assert := assert.New(t) 1879 1880 var tests = []struct { 1881 in []byte 1882 want string 1883 err bool 1884 }{ 1885 {[]byte{0x04, 0x04, 0x00, 0x04, 0x01, 0x01, 0x01, 0x01}, `{"type":1028,"node_local_router_id_ipv4":"1.1.1.1"}`, false}, 1886 {[]byte{0x04, 0x04, 0x00, 0x04, 0x01, 0x01, 0x01, 0x01, 0x12}, `{"type":1028,"node_local_router_id_ipv4":"1.1.1.1"}`, false}, 1887 {[]byte{0x04, 0x04, 0x00, 0x03, 0x00, 0x00, 0x00}, "", true}, 1888 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 1889 } 1890 1891 for _, test := range tests { 1892 tlv := LsTLVLocalIPv4RouterID{} 1893 if test.err { 1894 assert.Error(tlv.DecodeFromBytes(test.in)) 1895 continue 1896 } else { 1897 assert.NoError(tlv.DecodeFromBytes(test.in)) 1898 } 1899 1900 got, err := tlv.MarshalJSON() 1901 assert.NoError(err) 1902 assert.Equal(got, []byte(test.want)) 1903 } 1904 } 1905 1906 func Test_LsTLVRemoteIPv4RouterID(t *testing.T) { 1907 assert := assert.New(t) 1908 1909 var tests = []struct { 1910 in []byte 1911 want string 1912 err bool 1913 }{ 1914 {[]byte{0x04, 0x06, 0x00, 0x04, 0x02, 0x02, 0x02, 0x02}, `{"type":1030,"node_remote_router_id_ipv4":"2.2.2.2"}`, false}, 1915 {[]byte{0x04, 0x06, 0x00, 0x04, 0x02, 0x02, 0x02, 0x02, 0x44}, `{"type":1030,"node_remote_router_id_ipv4":"2.2.2.2"}`, false}, 1916 {[]byte{0x04, 0x06, 0x00, 0x03, 0x00, 0x00, 0x00}, "", true}, 1917 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 1918 } 1919 1920 for _, test := range tests { 1921 tlv := LsTLVRemoteIPv4RouterID{} 1922 if test.err { 1923 assert.Error(tlv.DecodeFromBytes(test.in)) 1924 continue 1925 } else { 1926 assert.NoError(tlv.DecodeFromBytes(test.in)) 1927 } 1928 1929 got, err := tlv.MarshalJSON() 1930 assert.NoError(err) 1931 assert.Equal(got, []byte(test.want)) 1932 } 1933 } 1934 1935 func Test_LsTLVLocalIPv6RouterID(t *testing.T) { 1936 assert := assert.New(t) 1937 1938 var tests = []struct { 1939 in []byte 1940 want string 1941 err bool 1942 }{ 1943 {[]byte{0x04, 0x05, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF}, `{"type":1029,"node_local_router_id_ipv6":"2001:db8::beef"}`, false}, 1944 {[]byte{0x04, 0x05, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF, 0xFF}, `{"type":1029,"node_local_router_id_ipv6":"2001:db8::beef"}`, false}, 1945 {[]byte{0x04, 0x05, 0x00, 0x01, 0x00}, "", true}, 1946 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 1947 } 1948 1949 for _, test := range tests { 1950 tlv := LsTLVLocalIPv6RouterID{} 1951 if test.err { 1952 assert.Error(tlv.DecodeFromBytes(test.in)) 1953 continue 1954 } else { 1955 assert.NoError(tlv.DecodeFromBytes(test.in)) 1956 } 1957 1958 got, err := tlv.MarshalJSON() 1959 assert.NoError(err) 1960 assert.Equal(got, []byte(test.want)) 1961 } 1962 } 1963 1964 func Test_LsTLVRemoteIPv6RouterID(t *testing.T) { 1965 assert := assert.New(t) 1966 1967 var tests = []struct { 1968 in []byte 1969 want string 1970 err bool 1971 }{ 1972 {[]byte{0x04, 0x07, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF}, `{"type":1031,"node_remote_router_id_ipv6":"2001:db8::beef"}`, false}, 1973 {[]byte{0x04, 0x07, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF, 0xFF}, `{"type":1031,"node_remote_router_id_ipv6":"2001:db8::beef"}`, false}, 1974 {[]byte{0x04, 0x07, 0x00, 0x01, 0x00}, "", true}, 1975 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 1976 } 1977 1978 for _, test := range tests { 1979 tlv := LsTLVRemoteIPv6RouterID{} 1980 if test.err { 1981 assert.Error(tlv.DecodeFromBytes(test.in)) 1982 continue 1983 } else { 1984 assert.NoError(tlv.DecodeFromBytes(test.in)) 1985 } 1986 1987 got, err := tlv.MarshalJSON() 1988 assert.NoError(err) 1989 assert.Equal(got, []byte(test.want)) 1990 } 1991 } 1992 1993 func Test_LsTLVOpaqueNodeAttr(t *testing.T) { 1994 assert := assert.New(t) 1995 1996 var tests = []struct { 1997 in []byte 1998 want string 1999 err bool 2000 }{ 2001 {[]byte{0x04, 0x01, 0x00, 0x03, 0x01, 0x02, 0x03}, `{"type":1025,"node_opaque_attribute":"[1 2 3]"}`, false}, 2002 {[]byte{0x04, 0x01, 0x00, 0x03, 0x01, 0x02, 0x03, 0x04}, `{"type":1025,"node_opaque_attribute":"[1 2 3]"}`, false}, 2003 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2004 } 2005 2006 for _, test := range tests { 2007 tlv := LsTLVOpaqueNodeAttr{} 2008 if test.err { 2009 assert.Error(tlv.DecodeFromBytes(test.in)) 2010 continue 2011 } else { 2012 assert.NoError(tlv.DecodeFromBytes(test.in)) 2013 } 2014 2015 got, err := tlv.MarshalJSON() 2016 assert.NoError(err) 2017 assert.Equal(got, []byte(test.want)) 2018 } 2019 } 2020 2021 func Test_LsTLVAutonomousSystem(t *testing.T) { 2022 assert := assert.New(t) 2023 2024 var tests = []struct { 2025 in []byte 2026 want string 2027 err bool 2028 }{ 2029 {[]byte{0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07}, `{"type":512,"asn":117901063}`, false}, 2030 {[]byte{0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, 0xFF}, `{"type":512,"asn":117901063}`, false}, 2031 {[]byte{0x02, 0x00, 0x00, 0x03, 0x07, 0x07, 0x07}, "", true}, 2032 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2033 } 2034 2035 for _, test := range tests { 2036 tlv := LsTLVAutonomousSystem{} 2037 if test.err { 2038 assert.Error(tlv.DecodeFromBytes(test.in)) 2039 continue 2040 } else { 2041 assert.NoError(tlv.DecodeFromBytes(test.in)) 2042 } 2043 2044 got, err := tlv.MarshalJSON() 2045 assert.NoError(err) 2046 assert.Equal(got, []byte(test.want)) 2047 } 2048 } 2049 2050 func Test_LsTLVBgpLsID(t *testing.T) { 2051 assert := assert.New(t) 2052 2053 var tests = []struct { 2054 in []byte 2055 want string 2056 err bool 2057 }{ 2058 {[]byte{0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07}, `{"type":513,"bgp_ls_id":117901063}`, false}, 2059 {[]byte{0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, 0xFF}, `{"type":513,"bgp_ls_id":117901063}`, false}, 2060 {[]byte{0x02, 0x01, 0x00, 0x03, 0x07, 0x07, 0x07}, "", true}, 2061 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2062 } 2063 2064 for _, test := range tests { 2065 tlv := LsTLVBgpLsID{} 2066 if test.err { 2067 assert.Error(tlv.DecodeFromBytes(test.in)) 2068 continue 2069 } else { 2070 assert.NoError(tlv.DecodeFromBytes(test.in)) 2071 } 2072 2073 got, err := tlv.MarshalJSON() 2074 assert.NoError(err) 2075 assert.Equal(got, []byte(test.want)) 2076 } 2077 } 2078 2079 func Test_LsTLVIgpRouterID(t *testing.T) { 2080 assert := assert.New(t) 2081 2082 var tests = []struct { 2083 in []byte 2084 want string 2085 err bool 2086 }{ 2087 {[]byte{0x02, 0x03, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04}, `{"type":515,"igp_router_id":"[1 2 3 4]"}`, false}, 2088 {[]byte{0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, `{"type":515,"igp_router_id":"[1 2 3 4 5 6]"}`, false}, 2089 {[]byte{0x02, 0x03, 0x00, 0x07, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, `{"type":515,"igp_router_id":"[1 2 3 4 5 6 7]"}`, false}, 2090 {[]byte{0x02, 0x03, 0x00, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, `{"type":515,"igp_router_id":"[1 2 3 4 5 6 7 8]"}`, false}, 2091 {[]byte{0x02, 0x03, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05}, "", true}, 2092 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2093 } 2094 2095 for _, test := range tests { 2096 tlv := LsTLVIgpRouterID{} 2097 if test.err { 2098 assert.Error(tlv.DecodeFromBytes(test.in)) 2099 continue 2100 } else { 2101 assert.NoError(tlv.DecodeFromBytes(test.in)) 2102 } 2103 2104 got, err := tlv.MarshalJSON() 2105 assert.NoError(err) 2106 assert.Equal(got, []byte(test.want)) 2107 } 2108 } 2109 2110 func Test_LsTLVBgpRouterID(t *testing.T) { 2111 assert := assert.New(t) 2112 2113 var tests = []struct { 2114 in []byte 2115 want string 2116 err bool 2117 }{ 2118 {[]byte{0x02, 0x04, 0x00, 0x04, 0x0a, 0xff, 0x00, 0x01}, `{"type":516,"bgp_router_id":"10.255.0.1"}`, false}, 2119 {[]byte{0x02, 0x04, 0x00, 0x05, 0x0a, 0xff, 0x00, 0x01, 0x02}, "", true}, 2120 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2121 } 2122 2123 for _, test := range tests { 2124 tlv := LsTLVBgpRouterID{} 2125 if test.err { 2126 assert.Error(tlv.DecodeFromBytes(test.in)) 2127 continue 2128 } else { 2129 assert.NoError(tlv.DecodeFromBytes(test.in)) 2130 } 2131 2132 got, err := tlv.MarshalJSON() 2133 assert.NoError(err) 2134 assert.Equal(got, []byte(test.want)) 2135 } 2136 } 2137 2138 func Test_LsTLVBgpConfederationMember(t *testing.T) { 2139 assert := assert.New(t) 2140 2141 var tests = []struct { 2142 in []byte 2143 want string 2144 err bool 2145 }{ 2146 {[]byte{0x02, 0x05, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07}, `{"type":517,"bgp_confederation_member":117901063}`, false}, 2147 {[]byte{0x02, 0x05, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, 0xFF}, `{"type":517,"bgp_confederation_member":117901063}`, false}, 2148 {[]byte{0x02, 0x05, 0x00, 0x03, 0x07, 0x07, 0x07}, "", true}, 2149 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2150 } 2151 2152 for _, test := range tests { 2153 tlv := LsTLVBgpConfederationMember{} 2154 if test.err { 2155 assert.Error(tlv.DecodeFromBytes(test.in)) 2156 continue 2157 } else { 2158 assert.NoError(tlv.DecodeFromBytes(test.in)) 2159 } 2160 2161 got, err := tlv.MarshalJSON() 2162 assert.NoError(err) 2163 assert.Equal(got, []byte(test.want)) 2164 } 2165 } 2166 2167 func Test_LsTLVOspfAreaID(t *testing.T) { 2168 assert := assert.New(t) 2169 2170 var tests = []struct { 2171 in []byte 2172 want string 2173 err bool 2174 }{ 2175 {[]byte{0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07}, `{"type":514,"ospf_area_id":117901063}`, false}, 2176 {[]byte{0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, 0xFF}, `{"type":514,"ospf_area_id":117901063}`, false}, 2177 {[]byte{0x02, 0x02, 0x00, 0x03, 0x07, 0x07, 0x07}, "", true}, 2178 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2179 } 2180 2181 for _, test := range tests { 2182 tlv := LsTLVOspfAreaID{} 2183 if test.err { 2184 assert.Error(tlv.DecodeFromBytes(test.in)) 2185 continue 2186 } else { 2187 assert.NoError(tlv.DecodeFromBytes(test.in)) 2188 } 2189 2190 got, err := tlv.MarshalJSON() 2191 assert.NoError(err) 2192 assert.Equal(got, []byte(test.want)) 2193 } 2194 } 2195 2196 func Test_LsTLVOspfRouteType(t *testing.T) { 2197 assert := assert.New(t) 2198 2199 var tests = []struct { 2200 in []byte 2201 want string 2202 err bool 2203 }{ 2204 {[]byte{0x01, 0x08, 0x00, 0x01, 0x06}, `{"type":264,"ospf_route_type":"NSSA2"}`, false}, 2205 {[]byte{0x01, 0x08, 0x00, 0x01, 0x01, 0xFF}, `{"type":264,"ospf_route_type":"INTRA-AREA"}`, false}, 2206 {[]byte{0x01, 0x08, 0x00, 0x02, 0x01, 0x01}, "", true}, 2207 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2208 } 2209 2210 for _, test := range tests { 2211 tlv := LsTLVOspfRouteType{} 2212 if test.err { 2213 assert.Error(tlv.DecodeFromBytes(test.in)) 2214 continue 2215 } else { 2216 assert.NoError(tlv.DecodeFromBytes(test.in)) 2217 } 2218 2219 got, err := tlv.MarshalJSON() 2220 assert.NoError(err) 2221 assert.Equal(got, []byte(test.want)) 2222 } 2223 } 2224 2225 func Test_LsTLVIPReachability(t *testing.T) { 2226 assert := assert.New(t) 2227 2228 var tests = []struct { 2229 in []byte 2230 want string 2231 serialize bool 2232 err bool 2233 }{ 2234 {[]byte{0x01, 0x09, 0x00, 0x02, 0x08, 0x0a}, `{"type":265,"prefix_length":8,"prefix":"[10]"}`, true, false}, 2235 {[]byte{0x01, 0x09, 0x00, 0x03, 0x10, 0x0a, 0x0b, 0xFF}, `{"type":265,"prefix_length":16,"prefix":"[10 11]"}`, false, false}, 2236 {[]byte{0x01, 0x09, 0x00, 0x02, 0x08}, ``, false, true}, 2237 {[]byte{0x01, 0x09, 0x00, 0x01, 0x01}, "", false, true}, 2238 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", false, true}, 2239 } 2240 2241 for _, test := range tests { 2242 tlv := LsTLVIPReachability{} 2243 if test.err { 2244 assert.Error(tlv.DecodeFromBytes(test.in)) 2245 continue 2246 } else { 2247 assert.NoError(tlv.DecodeFromBytes(test.in)) 2248 } 2249 2250 got, err := tlv.MarshalJSON() 2251 assert.NoError(err) 2252 assert.Equal(got, []byte(test.want)) 2253 } 2254 } 2255 2256 func Test_LsTLVIPReachabilityToIPNet(t *testing.T) { 2257 assert := assert.New(t) 2258 2259 var tests = []struct { 2260 tlv LsTLVIPReachability 2261 ipv6 bool 2262 want net.IPNet 2263 }{ 2264 { 2265 tlv: LsTLVIPReachability{ 2266 PrefixLength: 8, 2267 Prefix: []byte{0x0a}, 2268 }, 2269 ipv6: false, 2270 want: net.IPNet{ 2271 IP: net.IPv4(10, 0, 0, 0), 2272 Mask: net.CIDRMask(8, 32), 2273 }, 2274 }, 2275 { 2276 tlv: LsTLVIPReachability{ 2277 PrefixLength: 4, 2278 Prefix: []byte{0xaa}, 2279 }, 2280 ipv6: false, 2281 want: net.IPNet{ 2282 IP: net.IPv4(160, 0, 0, 0), 2283 Mask: net.CIDRMask(4, 32), 2284 }, 2285 }, 2286 { 2287 tlv: LsTLVIPReachability{ 2288 PrefixLength: 31, 2289 Prefix: []byte{0x0a, 0x0a, 0x0a, 0xfe}, 2290 }, 2291 ipv6: false, 2292 want: net.IPNet{ 2293 IP: net.IPv4(10, 10, 10, 254), 2294 Mask: net.CIDRMask(31, 32), 2295 }, 2296 }, 2297 { 2298 tlv: LsTLVIPReachability{ 2299 PrefixLength: 16, 2300 Prefix: []byte{0x20, 0x01}, 2301 }, 2302 ipv6: true, 2303 want: net.IPNet{ 2304 IP: net.ParseIP("2001::"), 2305 Mask: net.CIDRMask(16, 128), 2306 }, 2307 }, 2308 { 2309 tlv: LsTLVIPReachability{ 2310 PrefixLength: 24, 2311 Prefix: []byte{0x20, 0x01, 0x0d}, 2312 }, 2313 ipv6: true, 2314 want: net.IPNet{ 2315 IP: net.ParseIP("2001:d00::"), 2316 Mask: net.CIDRMask(24, 128), 2317 }, 2318 }, 2319 } 2320 2321 for _, test := range tests { 2322 got := test.tlv.ToIPNet(test.ipv6) 2323 assert.Equal(test.want.IP.String(), got.IP.String()) 2324 assert.Equal(test.want.Mask.String(), got.Mask.String()) 2325 } 2326 } 2327 2328 func Test_LsTLVAdminGroup(t *testing.T) { 2329 assert := assert.New(t) 2330 2331 var tests = []struct { 2332 in []byte 2333 want string 2334 err bool 2335 }{ 2336 {[]byte{0x04, 0x40, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07}, `{"type":1088,"admin_group":"07070707"}`, false}, 2337 {[]byte{0x04, 0x40, 0x00, 0x04, 0xAE, 0xAE, 0xAE, 0xAE, 0xFF}, `{"type":1088,"admin_group":"aeaeaeae"}`, false}, 2338 {[]byte{0x04, 0x40, 0x00, 0x03, 0x07, 0x07, 0x07}, "", true}, 2339 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2340 } 2341 2342 for _, test := range tests { 2343 tlv := LsTLVAdminGroup{} 2344 if test.err { 2345 assert.Error(tlv.DecodeFromBytes(test.in)) 2346 continue 2347 } else { 2348 assert.NoError(tlv.DecodeFromBytes(test.in)) 2349 } 2350 2351 got, err := tlv.MarshalJSON() 2352 assert.NoError(err) 2353 assert.Equal(got, []byte(test.want)) 2354 } 2355 } 2356 2357 func Test_LsTLVMaxLinkBw(t *testing.T) { 2358 assert := assert.New(t) 2359 2360 var tests = []struct { 2361 in []byte 2362 want string 2363 err bool 2364 }{ 2365 {[]byte{0x04, 0x41, 0x00, 0x04, 0x43, 0xA4, 0xB2, 0x00}, `{"type":1089,"max_link_bw":329.39062}`, false}, 2366 {[]byte{0x04, 0x41, 0x00, 0x04, 0x43, 0xA4, 0xB2, 0x00, 0xFF}, `{"type":1089,"max_link_bw":329.39062}`, false}, 2367 {[]byte{0x04, 0x41, 0x00, 0x03, 0x07, 0x07, 0x07}, "", true}, 2368 {[]byte{0x04, 0x41, 0x00, 0x04, 0x7f, 0x80, 0x00, 0x00}, "", true}, // +Inf 2369 {[]byte{0x04, 0x41, 0x00, 0x04, 0xff, 0x80, 0x00, 0x00}, "", true}, // -Inf 2370 {[]byte{0x04, 0x41, 0x00, 0x04, 0xff, 0xbf, 0xff, 0xff}, "", true}, // NaN 2371 {[]byte{0x04, 0x41, 0x00, 0x04, 0xc2, 0xc8, 0x00, 0x00}, "", true}, // -100 2372 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2373 } 2374 2375 for _, test := range tests { 2376 tlv := LsTLVMaxLinkBw{} 2377 if test.err { 2378 assert.Error(tlv.DecodeFromBytes(test.in)) 2379 continue 2380 } else { 2381 assert.NoError(tlv.DecodeFromBytes(test.in)) 2382 } 2383 2384 got, err := tlv.MarshalJSON() 2385 assert.NoError(err) 2386 assert.Equal(got, []byte(test.want)) 2387 } 2388 } 2389 2390 func Test_LsTLVMaxReservableLinkBw(t *testing.T) { 2391 assert := assert.New(t) 2392 2393 var tests = []struct { 2394 in []byte 2395 want string 2396 err bool 2397 }{ 2398 {[]byte{0x04, 0x42, 0x00, 0x04, 0x43, 0xA4, 0xB2, 0x00}, `{"type":1090,"max_reservable_link_bw":329.39062}`, false}, 2399 {[]byte{0x04, 0x42, 0x00, 0x04, 0x43, 0xA4, 0xB2, 0x00, 0xFF}, `{"type":1090,"max_reservable_link_bw":329.39062}`, false}, 2400 {[]byte{0x04, 0x42, 0x00, 0x03, 0x07, 0x07, 0x07}, "", true}, 2401 {[]byte{0x04, 0x42, 0x00, 0x04, 0x7f, 0x80, 0x00, 0x00}, "", true}, // +Inf 2402 {[]byte{0x04, 0x42, 0x00, 0x04, 0xff, 0x80, 0x00, 0x00}, "", true}, // -Inf 2403 {[]byte{0x04, 0x42, 0x00, 0x04, 0xff, 0xbf, 0xff, 0xff}, "", true}, // NaN 2404 {[]byte{0x04, 0x42, 0x00, 0x04, 0xc2, 0xc8, 0x00, 0x00}, "", true}, // -100 2405 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2406 } 2407 2408 for _, test := range tests { 2409 tlv := LsTLVMaxReservableLinkBw{} 2410 if test.err { 2411 assert.Error(tlv.DecodeFromBytes(test.in)) 2412 continue 2413 } else { 2414 assert.NoError(tlv.DecodeFromBytes(test.in)) 2415 } 2416 2417 got, err := tlv.MarshalJSON() 2418 assert.NoError(err) 2419 assert.Equal(got, []byte(test.want)) 2420 } 2421 } 2422 2423 func Test_LsTLVUnreservedBw(t *testing.T) { 2424 assert := assert.New(t) 2425 2426 var tests = []struct { 2427 in []byte 2428 want string 2429 err bool 2430 }{ 2431 {[]byte{0x04, 0x43, 0x00, 0x20, 2432 0x43, 0xA4, 0xB2, 0x00, 2433 0x43, 0xA4, 0xB2, 0x00, 2434 0x43, 0xA4, 0xB2, 0x00, 2435 0x43, 0xA4, 0xB2, 0x00, 2436 0x43, 0xA4, 0xB2, 0x00, 2437 0x43, 0xA4, 0xB2, 0x00, 2438 0x43, 0xA4, 0xB2, 0x00, 2439 0x43, 0xA4, 0xB3, 0x00}, 2440 `{"type":1091,"unreserved_bw":[329.39062,329.39062,329.39062,329.39062,329.39062,329.39062,329.39062,329.39844]}`, false}, 2441 {[]byte{0x04, 0x43, 0x00, 0x20, 2442 0x43, 0xA4, 0xB2, 0x00, 2443 0x43, 0xA4, 0xB2, 0x00, 2444 0x43, 0xA4, 0xB2, 0x00, 2445 0x43, 0xA4, 0xB2, 0x00, 2446 0x43, 0xA4, 0xB2, 0x00, 2447 0x43, 0xA4, 0xB2, 0x00, 2448 0x43, 0xA4, 0xB2, 0x00, 2449 0x43, 0xA4, 0xB2, 0x00, 0xff}, 2450 `{"type":1091,"unreserved_bw":[329.39062,329.39062,329.39062,329.39062,329.39062,329.39062,329.39062,329.39062]}`, false}, 2451 {[]byte{0x04, 0x43, 0x00, 0x20, 2452 0x7f, 0x80, 0x00, 0x00, 2453 0x43, 0xA4, 0xB2, 0x00, 2454 0x43, 0xA4, 0xB2, 0x00, 2455 0x43, 0xA4, 0xB2, 0x00, 2456 0x43, 0xA4, 0xB2, 0x00, 2457 0x43, 0xA4, 0xB2, 0x00, 2458 0x43, 0xA4, 0xB2, 0x00, 2459 0x43, 0xA4, 0xB3, 0x00}, 2460 "", true}, 2461 {[]byte{0x04, 0x43, 0x00, 0x20, 2462 0xff, 0x80, 0x00, 0x00, 2463 0x43, 0xA4, 0xB2, 0x00, 2464 0x43, 0xA4, 0xB2, 0x00, 2465 0x43, 0xA4, 0xB2, 0x00, 2466 0x43, 0xA4, 0xB2, 0x00, 2467 0x43, 0xA4, 0xB2, 0x00, 2468 0x43, 0xA4, 0xB2, 0x00, 2469 0x43, 0xA4, 0xB3, 0x00}, 2470 "", true}, 2471 {[]byte{0x04, 0x43, 0x00, 0x20, 2472 0x43, 0xA4, 0xB3, 0x00, 2473 0x43, 0xA4, 0xB2, 0x00, 2474 0x43, 0xA4, 0xB2, 0x00, 2475 0xff, 0xbf, 0xff, 0xff, 2476 0x43, 0xA4, 0xB2, 0x00, 2477 0x43, 0xA4, 0xB2, 0x00, 2478 0x43, 0xA4, 0xB2, 0x00, 2479 0x43, 0xA4, 0xB3, 0x00}, 2480 "", true}, 2481 {[]byte{0x04, 0x43, 0x00, 0x20, 2482 0x43, 0xA4, 0xB3, 0x00, 2483 0x43, 0xA4, 0xB2, 0x00, 2484 0x43, 0xA4, 0xB2, 0x00, 2485 0x43, 0xA4, 0xB3, 0x00, 2486 0x43, 0xA4, 0xB2, 0x00, 2487 0x43, 0xA4, 0xB2, 0x00, 2488 0x43, 0xA4, 0xB2, 0x00, 2489 0xc2, 0xc8, 0x00, 0x00}, 2490 "", true}, 2491 {[]byte{0x04, 0x43, 0x00, 0x03, 0x07, 0x07, 0x07}, "", true}, 2492 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2493 } 2494 2495 for _, test := range tests { 2496 tlv := LsTLVUnreservedBw{} 2497 if test.err { 2498 assert.Error(tlv.DecodeFromBytes(test.in)) 2499 continue 2500 } else { 2501 assert.NoError(tlv.DecodeFromBytes(test.in)) 2502 } 2503 2504 got, err := tlv.MarshalJSON() 2505 assert.NoError(err) 2506 assert.Equal(got, []byte(test.want)) 2507 } 2508 } 2509 2510 func Test_LsTLVTEDefaultMetric(t *testing.T) { 2511 assert := assert.New(t) 2512 2513 var tests = []struct { 2514 in []byte 2515 want string 2516 serialize bool 2517 err bool 2518 }{ 2519 {[]byte{0x04, 0x44, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07}, `{"type":1092,"te_default_metric":117901063}`, true, false}, 2520 {[]byte{0x04, 0x44, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, 0xFF}, `{"type":1092,"te_default_metric":117901063}`, false, false}, 2521 {[]byte{0x04, 0x44, 0x00, 0x03, 0x07, 0x07, 0x07}, "", false, true}, 2522 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", false, true}, 2523 } 2524 2525 for _, test := range tests { 2526 tlv := LsTLVTEDefaultMetric{} 2527 if test.err { 2528 assert.Error(tlv.DecodeFromBytes(test.in)) 2529 continue 2530 } else { 2531 assert.NoError(tlv.DecodeFromBytes(test.in)) 2532 } 2533 2534 got, err := tlv.MarshalJSON() 2535 assert.NoError(err) 2536 assert.Equal(got, []byte(test.want)) 2537 2538 if test.serialize { 2539 got, err := tlv.Serialize() 2540 assert.NoError(err) 2541 assert.Equal(test.in, got) 2542 } 2543 } 2544 } 2545 2546 func Test_LsTLVIGPMetric(t *testing.T) { 2547 assert := assert.New(t) 2548 2549 var tests = []struct { 2550 in []byte 2551 want string 2552 serialize bool 2553 err bool 2554 }{ 2555 {[]byte{0x04, 0x47, 0x00, 0x01, 0x01}, `{"type":1095,"igp_metric":1}`, true, false}, 2556 {[]byte{0x04, 0x47, 0x00, 0x01, 0x3F}, `{"type":1095,"igp_metric":63}`, true, false}, 2557 {[]byte{0x04, 0x47, 0x00, 0x01, 0xFF}, `{"type":1095,"igp_metric":63}`, false, false}, 2558 {[]byte{0x04, 0x47, 0x00, 0x02, 0x00, 0x01}, `{"type":1095,"igp_metric":1}`, true, false}, 2559 {[]byte{0x04, 0x47, 0x00, 0x02, 0xff, 0xff}, `{"type":1095,"igp_metric":65535}`, true, false}, 2560 {[]byte{0x04, 0x47, 0x00, 0x03, 0x00, 0x00, 0x01}, `{"type":1095,"igp_metric":1}`, true, false}, 2561 {[]byte{0x04, 0x47, 0x00, 0x03, 0xff, 0xff, 0xff}, `{"type":1095,"igp_metric":16777215}`, true, false}, 2562 {[]byte{0x04, 0x47, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07}, "", false, true}, 2563 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", false, true}, 2564 } 2565 2566 for _, test := range tests { 2567 tlv := LsTLVIGPMetric{} 2568 if test.err { 2569 assert.Error(tlv.DecodeFromBytes(test.in)) 2570 continue 2571 } else { 2572 assert.NoError(tlv.DecodeFromBytes(test.in)) 2573 } 2574 2575 got, err := tlv.MarshalJSON() 2576 assert.NoError(err) 2577 assert.Equal(got, []byte(test.want)) 2578 2579 if test.serialize { 2580 got, err := tlv.Serialize() 2581 assert.NoError(err) 2582 assert.Equal(test.in, got) 2583 } 2584 } 2585 } 2586 2587 func Test_LsTLVNLinkName(t *testing.T) { 2588 assert := assert.New(t) 2589 2590 var tests = []struct { 2591 in []byte 2592 want string 2593 err bool 2594 }{ 2595 {[]byte{0x04, 0x4a, 0x00, 0x03, 0x72, 0x74, 0x72}, `{"type":1098,"link_name":"rtr"}`, false}, 2596 {[]byte{0x04, 0x4a, 0x00, 0x03, 0x72, 0x74, 0x72, 0x00}, `{"type":1098,"link_name":"rtr"}`, false}, 2597 {[]byte{0x04, 0x4a, 0x00, 0x00}, "", true}, 2598 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2599 } 2600 2601 for _, test := range tests { 2602 tlv := LsTLVLinkName{} 2603 if test.err { 2604 assert.Error(tlv.DecodeFromBytes(test.in)) 2605 continue 2606 } else { 2607 assert.NoError(tlv.DecodeFromBytes(test.in)) 2608 } 2609 2610 got, err := tlv.MarshalJSON() 2611 assert.NoError(err) 2612 assert.Equal(got, []byte(test.want)) 2613 } 2614 } 2615 2616 func Test_LsTLVSrAlgorithm(t *testing.T) { 2617 assert := assert.New(t) 2618 2619 var tests = []struct { 2620 in []byte 2621 want string 2622 err bool 2623 }{ 2624 {[]byte{0x04, 0x0b, 0x00, 0x03, 0x01, 0x02, 0x03}, `{"type":1035,"sr_algorithm":"[1 2 3]"}`, false}, 2625 {[]byte{0x04, 0x0b, 0x00, 0x03, 0x01, 0x02, 0x03, 0x04}, `{"type":1035,"sr_algorithm":"[1 2 3]"}`, false}, 2626 {[]byte{0x04, 0x0b, 0x00, 0x00}, "", true}, 2627 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 2628 } 2629 2630 for _, test := range tests { 2631 tlv := LsTLVSrAlgorithm{} 2632 if test.err { 2633 assert.Error(tlv.DecodeFromBytes(test.in)) 2634 continue 2635 } else { 2636 assert.NoError(tlv.DecodeFromBytes(test.in)) 2637 } 2638 2639 got, err := tlv.MarshalJSON() 2640 assert.NoError(err) 2641 assert.Equal(got, []byte(test.want)) 2642 } 2643 } 2644 2645 func Test_LsTLVSrCapabilities(t *testing.T) { 2646 assert := assert.New(t) 2647 2648 var tests = []struct { 2649 in []byte 2650 want string 2651 serialize bool 2652 err bool 2653 }{ 2654 { 2655 []byte{ 2656 0x04, 0x0a, 0x00, 0x0c, // type 1034, length 12 2657 0x00, 0x00, // flags and reserved 2658 0x00, 0x88, 0xb8, // range: 35000 2659 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // SID/Label TLV, SID: 100500 2660 }, 2661 `{"type":1034,"flags":0,"ranges":[{"Range":35000,"FirstLabel":{"type":1161,"sid_label":100500}}]}`, 2662 true, 2663 false, 2664 }, 2665 { 2666 []byte{ 2667 0x04, 0x0a, 0x00, 0x0d, // type 1034, length 13 2668 0x00, 0x00, // flags and reserved 2669 0x00, 0x88, 0xb8, // range: 35000 2670 0x04, 0x89, 0x00, 0x04, 0x04, 0x3B, 0x73, 0x49, // SID/Label TLV, SID: 71005001 2671 }, 2672 `{"type":1034,"flags":0,"ranges":[{"Range":35000,"FirstLabel":{"type":1161,"sid_label":71005001}}]}`, 2673 true, 2674 false, 2675 }, 2676 { 2677 []byte{ 2678 0x04, 0x0a, 0x00, 0x17, // type 1034, length 23 2679 0x00, 0x00, // flags and reserved 2680 0x00, 0x88, 0xb8, // range: 35000 2681 0x04, 0x89, 0x00, 0x04, 0x04, 0x3B, 0x73, 0x49, // SID/Label TLV, SID: 71005001 2682 0x0f, 0x42, 0x40, // range: 1000000 2683 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // SID/Label TLV, SID: 100500 2684 }, 2685 `{"type":1034,"flags":0,"ranges":[{"Range":35000,"FirstLabel":{"type":1161,"sid_label":71005001}},{"Range":1000000,"FirstLabel":{"type":1161,"sid_label":100500}}]}`, 2686 true, 2687 false, 2688 }, 2689 { 2690 []byte{ 2691 0x04, 0x0a, 0x00, 0x17, // type 1034, length 23 2692 0x00, 0x00, // flags and reserved 2693 0x00, 0x88, 0xb8, // range: 35000 2694 0x04, 0x89, 0x00, 0x04, 0x04, 0x3B, 0x73, 0x49, // SID/Label TLV, SID: 71005001 2695 0x0f, 0x42, 0x40, // range: 1000000 2696 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // SID/Label TLV, SID: 100500 2697 0xff, 0xff, 0xff, // some random bytes - should be ignored 2698 }, 2699 `{"type":1034,"flags":0,"ranges":[{"Range":35000,"FirstLabel":{"type":1161,"sid_label":71005001}},{"Range":1000000,"FirstLabel":{"type":1161,"sid_label":100500}}]}`, 2700 false, 2701 false, 2702 }, 2703 { 2704 []byte{ 2705 0x04, 0x0a, 0x00, 0xcc, // type 1034, length 204 (corrupted) 2706 0x00, 0x00, // flags and reserved 2707 0x00, 0x88, 0xb8, // range: 35000 2708 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // SID/Label TLV, SID: 100500 2709 }, 2710 "", 2711 false, 2712 true, 2713 }, 2714 { 2715 []byte{ 2716 0x04, 0x0a, 0x00, 0x11, // type 1034, length 23 2717 0x00, 0x00, // flags and reserved 2718 0x00, 0x88, 0xb8, // range: 35000 2719 0x04, 0x89, 0x00, 0x04, 0x04, 0x3B, 0x73, 0x49, // SID/Label TLV, SID: 71005001 2720 0x0f, 0x42, 0x40, // range: 1000000 2721 0x04, // No SID/Label sub-TLV 2722 }, 2723 "", 2724 false, 2725 true, 2726 }, 2727 } 2728 2729 for _, test := range tests { 2730 tlv := LsTLVSrCapabilities{} 2731 if test.err { 2732 assert.Error(tlv.DecodeFromBytes(test.in)) 2733 continue 2734 } else { 2735 assert.NoError(tlv.DecodeFromBytes(test.in)) 2736 } 2737 2738 got, err := tlv.MarshalJSON() 2739 assert.NoError(err) 2740 assert.Equal(got, []byte(test.want)) 2741 2742 if test.serialize { 2743 s, err := tlv.Serialize() 2744 assert.NoError(err) 2745 assert.Equal(test.in, s) 2746 } 2747 } 2748 } 2749 2750 func Test_LsTLVLocalBlock(t *testing.T) { 2751 assert := assert.New(t) 2752 2753 var tests = []struct { 2754 in []byte 2755 want string 2756 serialize bool 2757 err bool 2758 }{ 2759 { 2760 []byte{ 2761 0x04, 0x0c, 0x00, 0x0c, // type 1036, length 12 2762 0x00, 0x00, // flags and reserved 2763 0x00, 0x88, 0xb8, // range: 35000 2764 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // SID/Label TLV, SID: 100500 2765 }, 2766 `{"type":1036,"flags":0,"ranges":[{"Range":35000,"FirstLabel":{"type":1161,"sid_label":100500}}]}`, 2767 true, 2768 false, 2769 }, 2770 { 2771 []byte{ 2772 0x04, 0x0c, 0x00, 0x0d, // type 1036, length 13 2773 0x00, 0x00, // flags and reserved 2774 0x00, 0x88, 0xb8, // range: 35000 2775 0x04, 0x89, 0x00, 0x04, 0x04, 0x3B, 0x73, 0x49, // SID/Label TLV, SID: 71005001 2776 }, 2777 `{"type":1036,"flags":0,"ranges":[{"Range":35000,"FirstLabel":{"type":1161,"sid_label":71005001}}]}`, 2778 true, 2779 false, 2780 }, 2781 { 2782 []byte{ 2783 0x04, 0x0c, 0x00, 0x17, // type 1036, length 23 2784 0x00, 0x00, // flags and reserved 2785 0x00, 0x88, 0xb8, // range: 35000 2786 0x04, 0x89, 0x00, 0x04, 0x04, 0x3B, 0x73, 0x49, // SID/Label TLV, SID: 71005001 2787 0x0f, 0x42, 0x40, // range: 1000000 2788 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // SID/Label TLV, SID: 100500 2789 }, 2790 `{"type":1036,"flags":0,"ranges":[{"Range":35000,"FirstLabel":{"type":1161,"sid_label":71005001}},{"Range":1000000,"FirstLabel":{"type":1161,"sid_label":100500}}]}`, 2791 true, 2792 false, 2793 }, 2794 { 2795 []byte{ 2796 0x04, 0x0c, 0x00, 0x17, // type 1036, length 23 2797 0x00, 0x00, // flags and reserved 2798 0x00, 0x88, 0xb8, // range: 35000 2799 0x04, 0x89, 0x00, 0x04, 0x04, 0x3B, 0x73, 0x49, // SID/Label TLV, SID: 71005001 2800 0x0f, 0x42, 0x40, // range: 1000000 2801 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // SID/Label TLV, SID: 100500 2802 0xff, 0xff, 0xff, // some random bytes - should be ignored 2803 }, 2804 `{"type":1036,"flags":0,"ranges":[{"Range":35000,"FirstLabel":{"type":1161,"sid_label":71005001}},{"Range":1000000,"FirstLabel":{"type":1161,"sid_label":100500}}]}`, 2805 false, 2806 false, 2807 }, 2808 { 2809 []byte{ 2810 0x04, 0x0c, 0x00, 0xcc, // type 1036, length 204 (corrupted) 2811 0x00, 0x00, // flags and reserved 2812 0x00, 0x88, 0xb8, // range: 35000 2813 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // SID/Label TLV, SID: 100500 2814 }, 2815 "", 2816 false, 2817 true, 2818 }, 2819 { 2820 []byte{ 2821 0x04, 0x0c, 0x00, 0x11, // type 1036, length 23 2822 0x00, 0x00, // flags and reserved 2823 0x00, 0x88, 0xb8, // range: 35000 2824 0x04, 0x89, 0x00, 0x04, 0x04, 0x3B, 0x73, 0x49, // SID/Label TLV, SID: 71005001 2825 0x0f, 0x42, 0x40, // range: 1000000 2826 0x04, // No SID/Label sub-TLV 2827 }, 2828 "", 2829 false, 2830 true, 2831 }, 2832 } 2833 2834 for _, test := range tests { 2835 tlv := LsTLVSrLocalBlock{} 2836 if test.err { 2837 assert.Error(tlv.DecodeFromBytes(test.in)) 2838 continue 2839 } else { 2840 assert.NoError(tlv.DecodeFromBytes(test.in)) 2841 } 2842 2843 got, err := tlv.MarshalJSON() 2844 assert.NoError(err) 2845 assert.Equal(got, []byte(test.want)) 2846 2847 if test.serialize { 2848 s, err := tlv.Serialize() 2849 assert.NoError(err) 2850 assert.Equal(test.in, s) 2851 } 2852 } 2853 } 2854 2855 func Test_LsTLVAdjacencySID(t *testing.T) { 2856 assert := assert.New(t) 2857 2858 var tests = []struct { 2859 in []byte 2860 want string 2861 serialize bool 2862 err bool 2863 }{ 2864 {[]byte{0x04, 0x4b, 0x00, 0x07, 0x01, 0x01, 0x00, 0x00, 0x01, 0x88, 0x94}, `{"type":1099,"adjacency_sid":100500}`, true, false}, 2865 {[]byte{0x04, 0x4b, 0x00, 0x07, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff}, `{"type":1099,"adjacency_sid":1048575}`, false, false}, 2866 {[]byte{0x04, 0x4b, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00, 0x04, 0x3B, 0x73, 0x49}, `{"type":1099,"adjacency_sid":71005001}`, true, false}, 2867 {[]byte{0x04, 0x4b, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x11}, "", false, true}, 2868 {[]byte{0xfe, 0xfe, 0x00, 0x07, 0x04, 0x3B, 0x73, 0x49, 0x05, 0x06, 0x07}, "", false, true}, 2869 } 2870 2871 for _, test := range tests { 2872 tlv := LsTLVAdjacencySID{} 2873 if test.err { 2874 assert.Error(tlv.DecodeFromBytes(test.in)) 2875 continue 2876 } else { 2877 assert.NoError(tlv.DecodeFromBytes(test.in)) 2878 } 2879 2880 got, err := tlv.MarshalJSON() 2881 assert.NoError(err) 2882 assert.Equal(got, []byte(test.want)) 2883 2884 if test.serialize { 2885 s, err := tlv.Serialize() 2886 assert.NoError(err) 2887 assert.Equal(test.in, s) 2888 } 2889 } 2890 } 2891 2892 func Test_LsTLVSIDLabel(t *testing.T) { 2893 assert := assert.New(t) 2894 2895 var tests = []struct { 2896 in []byte 2897 want string 2898 serialize bool 2899 err bool 2900 }{ 2901 {[]byte{0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94}, `{"type":1161,"sid_label":100500}`, true, false}, 2902 {[]byte{0x04, 0x89, 0x00, 0x03, 0x0f, 0xff, 0xff}, `{"type":1161,"sid_label":1048575}`, true, false}, 2903 {[]byte{0x04, 0x89, 0x00, 0x03, 0xff, 0xff, 0xff}, `{"type":1161,"sid_label":1048575}`, false, false}, 2904 {[]byte{0x04, 0x89, 0x00, 0x04, 0x04, 0x3B, 0x73, 0x49}, `{"type":1161,"sid_label":71005001}`, false, false}, 2905 {[]byte{0x04, 0x89, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05}, "", false, true}, 2906 {[]byte{0xfe, 0xfe, 0x00, 0x04, 0x04, 0x3B, 0x73, 0x49}, "", false, true}, 2907 } 2908 2909 for _, test := range tests { 2910 tlv := LsTLVSIDLabel{} 2911 if test.err { 2912 assert.Error(tlv.DecodeFromBytes(test.in)) 2913 continue 2914 } else { 2915 assert.NoError(tlv.DecodeFromBytes(test.in)) 2916 } 2917 2918 got, err := tlv.MarshalJSON() 2919 assert.NoError(err) 2920 assert.Equal(got, []byte(test.want)) 2921 2922 if test.serialize { 2923 s, err := tlv.Serialize() 2924 assert.NoError(err) 2925 assert.Equal(test.in, s) 2926 } 2927 } 2928 } 2929 2930 func Test_LsTLVPrefixSID(t *testing.T) { 2931 assert := assert.New(t) 2932 2933 var tests = []struct { 2934 in []byte 2935 want string 2936 serialize bool 2937 err bool 2938 }{ 2939 {[]byte{0x04, 0x86, 0x00, 0x07, 0x01, 0x01, 0x00, 0x00, 0x01, 0x88, 0x94}, `{"type":1158,"prefix_sid":100500}`, true, false}, 2940 {[]byte{0x04, 0x86, 0x00, 0x07, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff}, `{"type":1158,"prefix_sid":1048575}`, false, false}, 2941 {[]byte{0x04, 0x86, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00, 0x04, 0x3B, 0x73, 0x49}, `{"type":1158,"prefix_sid":71005001}`, true, false}, 2942 {[]byte{0x04, 0x86, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x11}, "", false, true}, 2943 {[]byte{0xfe, 0xfe, 0x00, 0x07, 0x04, 0x3B, 0x73, 0x49, 0x05, 0x06, 0x07}, "", false, true}, 2944 } 2945 2946 for _, test := range tests { 2947 tlv := LsTLVPrefixSID{} 2948 if test.err { 2949 assert.Error(tlv.DecodeFromBytes(test.in)) 2950 continue 2951 } else { 2952 assert.NoError(tlv.DecodeFromBytes(test.in)) 2953 } 2954 2955 got, err := tlv.MarshalJSON() 2956 assert.NoError(err) 2957 assert.Equal(got, []byte(test.want)) 2958 2959 if test.serialize { 2960 s, err := tlv.Serialize() 2961 assert.NoError(err) 2962 assert.Equal(test.in, s) 2963 } 2964 } 2965 } 2966 2967 func Test_LsTLVPeerNodeSID(t *testing.T) { 2968 assert := assert.New(t) 2969 2970 var tests = []struct { 2971 in []byte 2972 want string 2973 serialize bool 2974 err bool 2975 }{ 2976 {[]byte{0x04, 0x4d, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x01, 0x88, 0x94}, `{"type":1101,"peer_node_sid":100500}`, true, false}, 2977 {[]byte{0x04, 0x4d, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff}, `{"type":1101,"peer_node_sid":1048575}`, false, false}, 2978 {[]byte{0x04, 0x4d, 0x00, 0x08, 0xc0, 0x00, 0x00, 0x00, 0x04, 0x3B, 0x73, 0x49}, `{"type":1101,"peer_node_sid":71005001}`, true, false}, 2979 {[]byte{0x04, 0x4d, 0x00, 0x06, 0xc0, 0x02, 0x03, 0x04, 0x05, 0x11}, "", false, true}, 2980 {[]byte{0xfe, 0xfe, 0x00, 0x07, 0x04, 0x3B, 0x73, 0x49, 0x05, 0x06, 0x07}, "", false, true}, 2981 } 2982 2983 for _, test := range tests { 2984 tlv := LsTLVPeerNodeSID{} 2985 if test.err { 2986 assert.Error(tlv.DecodeFromBytes(test.in)) 2987 continue 2988 } else { 2989 assert.NoError(tlv.DecodeFromBytes(test.in)) 2990 } 2991 2992 got, err := tlv.MarshalJSON() 2993 assert.NoError(err) 2994 assert.Equal(got, []byte(test.want)) 2995 2996 if test.serialize { 2997 s, err := tlv.Serialize() 2998 assert.NoError(err) 2999 assert.Equal(test.in, s) 3000 } 3001 } 3002 } 3003 3004 func Test_LsTLVPeerAdjacencySID(t *testing.T) { 3005 assert := assert.New(t) 3006 3007 var tests = []struct { 3008 in []byte 3009 want string 3010 serialize bool 3011 err bool 3012 }{ 3013 {[]byte{0x04, 0x4e, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x01, 0x88, 0x94}, `{"type":1102,"peer_adjacency_sid":100500}`, true, false}, 3014 {[]byte{0x04, 0x4e, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff}, `{"type":1102,"peer_adjacency_sid":1048575}`, false, false}, 3015 {[]byte{0x04, 0x4e, 0x00, 0x08, 0xc0, 0x00, 0x00, 0x00, 0x04, 0x3B, 0x73, 0x49}, `{"type":1102,"peer_adjacency_sid":71005001}`, true, false}, 3016 {[]byte{0x04, 0x4e, 0x00, 0x06, 0xc0, 0x02, 0x03, 0x04, 0x05, 0x11}, "", false, true}, 3017 {[]byte{0xfe, 0xfe, 0x00, 0x07, 0x04, 0x3B, 0x73, 0x49, 0x05, 0x06, 0x07}, "", false, true}, 3018 } 3019 3020 for _, test := range tests { 3021 tlv := LsTLVPeerAdjacencySID{} 3022 if test.err { 3023 assert.Error(tlv.DecodeFromBytes(test.in)) 3024 continue 3025 } else { 3026 assert.NoError(tlv.DecodeFromBytes(test.in)) 3027 } 3028 3029 got, err := tlv.MarshalJSON() 3030 assert.NoError(err) 3031 assert.Equal(got, []byte(test.want)) 3032 3033 if test.serialize { 3034 s, err := tlv.Serialize() 3035 assert.NoError(err) 3036 assert.Equal(test.in, s) 3037 } 3038 } 3039 } 3040 3041 func Test_LsTLVPeerSetSID(t *testing.T) { 3042 assert := assert.New(t) 3043 3044 var tests = []struct { 3045 in []byte 3046 want string 3047 serialize bool 3048 err bool 3049 }{ 3050 {[]byte{0x04, 0x4f, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x01, 0x88, 0x94}, `{"type":1103,"peer_set_sid":100500}`, true, false}, 3051 {[]byte{0x04, 0x4f, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff}, `{"type":1103,"peer_set_sid":1048575}`, false, false}, 3052 {[]byte{0x04, 0x4f, 0x00, 0x08, 0xc0, 0x00, 0x00, 0x00, 0x04, 0x3B, 0x73, 0x49}, `{"type":1103,"peer_set_sid":71005001}`, true, false}, 3053 {[]byte{0x04, 0x4f, 0x00, 0x06, 0xc0, 0x02, 0x03, 0x04, 0x05, 0x11}, "", false, true}, 3054 {[]byte{0xfe, 0xfe, 0x00, 0x07, 0x04, 0x3B, 0x73, 0x49, 0x05, 0x06, 0x07}, "", false, true}, 3055 } 3056 3057 for _, test := range tests { 3058 tlv := LsTLVPeerSetSID{} 3059 if test.err { 3060 assert.Error(tlv.DecodeFromBytes(test.in)) 3061 continue 3062 } else { 3063 assert.NoError(tlv.DecodeFromBytes(test.in)) 3064 } 3065 3066 got, err := tlv.MarshalJSON() 3067 assert.NoError(err) 3068 assert.Equal(got, []byte(test.want)) 3069 3070 if test.serialize { 3071 s, err := tlv.Serialize() 3072 assert.NoError(err) 3073 assert.Equal(test.in, s) 3074 } 3075 } 3076 } 3077 3078 func Test_LsTLVSourceRouterID(t *testing.T) { 3079 assert := assert.New(t) 3080 3081 var tests = []struct { 3082 in []byte 3083 want string 3084 err bool 3085 }{ 3086 {[]byte{0x04, 0x93, 0x00, 0x04, 0x0a, 0x0a, 0x0a, 0x0a}, `{"type":1171,"source_router_id":"10.10.10.10"}`, false}, 3087 {[]byte{0x04, 0x93, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF}, `{"type":1171,"source_router_id":"2001:db8::beef"}`, false}, 3088 {[]byte{0x04, 0x93, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF, 0xFF}, `{"type":1171,"source_router_id":"2001:db8::beef"}`, false}, 3089 {[]byte{0x04, 0x93, 0x00, 0x01, 0x00}, "", true}, 3090 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 3091 } 3092 3093 for _, test := range tests { 3094 tlv := LsTLVSourceRouterID{} 3095 if test.err { 3096 assert.Error(tlv.DecodeFromBytes(test.in)) 3097 continue 3098 } else { 3099 assert.NoError(tlv.DecodeFromBytes(test.in)) 3100 } 3101 3102 got, err := tlv.MarshalJSON() 3103 assert.NoError(err) 3104 assert.Equal(got, []byte(test.want)) 3105 } 3106 } 3107 3108 func Test_LsTLVOpaqueLinkAttr(t *testing.T) { 3109 assert := assert.New(t) 3110 3111 var tests = []struct { 3112 in []byte 3113 want string 3114 err bool 3115 }{ 3116 {[]byte{0x04, 0x49, 0x00, 0x03, 0x01, 0x02, 0x03}, `{"type":1097,"link_opaque_attribute":"[1 2 3]"}`, false}, 3117 {[]byte{0x04, 0x49, 0x00, 0x03, 0x01, 0x02, 0x03, 0x04}, `{"type":1097,"link_opaque_attribute":"[1 2 3]"}`, false}, 3118 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 3119 } 3120 3121 for _, test := range tests { 3122 tlv := LsTLVOpaqueLinkAttr{} 3123 if test.err { 3124 assert.Error(tlv.DecodeFromBytes(test.in)) 3125 continue 3126 } else { 3127 assert.NoError(tlv.DecodeFromBytes(test.in)) 3128 } 3129 3130 got, err := tlv.MarshalJSON() 3131 assert.NoError(err) 3132 assert.Equal(got, []byte(test.want)) 3133 } 3134 } 3135 3136 func Test_LsTLVIGPFlags(t *testing.T) { 3137 assert := assert.New(t) 3138 3139 var tests = []struct { 3140 in []byte 3141 want string 3142 serialize bool 3143 err bool 3144 }{ 3145 {[]byte{0x04, 0x80, 0x00, 0x01, 0xFF}, `{"type":1152,"igp_flags":"{IGP Flags: XXXXPLND}"}`, true, false}, 3146 {[]byte{0x04, 0x80, 0x00, 0x01, 0x80}, `{"type":1152,"igp_flags":"{IGP Flags: *******D}"}`, true, false}, 3147 {[]byte{0x04, 0x80, 0x00, 0x01, 0x80, 0xAA}, `{"type":1152,"igp_flags":"{IGP Flags: *******D}"}`, false, false}, 3148 {[]byte{0x04, 0x80, 0x00, 0x02, 0x80, 0x44}, "", false, true}, 3149 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", false, true}, 3150 } 3151 3152 for _, test := range tests { 3153 tlv := LsTLVIGPFlags{} 3154 if test.err { 3155 assert.Error(tlv.DecodeFromBytes(test.in)) 3156 } else { 3157 assert.NoError(tlv.DecodeFromBytes(test.in)) 3158 got, err := tlv.MarshalJSON() 3159 assert.NoError(err) 3160 assert.Equal(got, []byte(test.want)) 3161 if test.serialize { 3162 got, err := tlv.Serialize() 3163 assert.NoError(err) 3164 assert.Equal(test.in, got) 3165 } 3166 } 3167 } 3168 } 3169 3170 func Test_LsTLVOpaquePrefixAttr(t *testing.T) { 3171 assert := assert.New(t) 3172 3173 var tests = []struct { 3174 in []byte 3175 want string 3176 err bool 3177 }{ 3178 {[]byte{0x04, 0x85, 0x00, 0x03, 0x01, 0x02, 0x03}, `{"type":1157,"prefix_opaque_attribute":"[1 2 3]"}`, false}, 3179 {[]byte{0x04, 0x85, 0x00, 0x03, 0x01, 0x02, 0x03, 0x04}, `{"type":1157,"prefix_opaque_attribute":"[1 2 3]"}`, false}, 3180 {[]byte{0xfe, 0xfe, 0x00, 0x00}, "", true}, 3181 } 3182 3183 for _, test := range tests { 3184 tlv := LsTLVOpaquePrefixAttr{} 3185 if test.err { 3186 assert.Error(tlv.DecodeFromBytes(test.in)) 3187 continue 3188 } else { 3189 assert.NoError(tlv.DecodeFromBytes(test.in)) 3190 } 3191 3192 got, err := tlv.MarshalJSON() 3193 assert.NoError(err) 3194 assert.Equal(got, []byte(test.want)) 3195 } 3196 } 3197 3198 func Test_parseIGPRouterID(t *testing.T) { 3199 assert := assert.New(t) 3200 3201 var tests = []struct { 3202 in []byte 3203 str string 3204 pseudo bool 3205 }{ 3206 {[]byte{1, 2, 3, 4}, "1.2.3.4", false}, 3207 {[]byte{1, 2, 3, 4, 5, 255}, "0102.0304.05ff", false}, 3208 {[]byte{1, 2, 3, 4, 5, 255, 0}, "0102.0304.05ff-00", true}, 3209 {[]byte{1, 2, 3, 4, 5, 6, 7, 8}, "1.2.3.4:5.6.7.8", true}, 3210 } 3211 3212 for _, test := range tests { 3213 str, pseudo := parseIGPRouterID(test.in) 3214 assert.Equal(test.str, str) 3215 assert.Equal(test.pseudo, pseudo) 3216 } 3217 } 3218 3219 func Test_LsNodeDescriptor(t *testing.T) { 3220 assert := assert.New(t) 3221 3222 var tests = []struct { 3223 in []byte 3224 str string 3225 err bool 3226 serialize bool 3227 }{ 3228 {[]byte{ 3229 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3230 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3231 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3232 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3233 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3234 }, "{ASN: 117901063, BGP LS ID: 117901063, OSPF AREA: 117901063, IGP ROUTER ID: 0102.0304.0506}", 3235 false, true}, 3236 {[]byte{ 3237 0x01, 0x01, 0x00, 0x22, // Remote Node Desc 3238 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3239 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3240 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3241 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3242 }, "{ASN: 117901063, BGP LS ID: 117901063, OSPF AREA: 117901063, IGP ROUTER ID: 0102.0304.0506}", 3243 false, true}, 3244 {[]byte{ 3245 0x01, 0x00, 0x00, 0x21, // Truncated Length 3246 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3247 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3248 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3249 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3250 }, "", true, false}, 3251 {[]byte{ 3252 0x01, 0x00, 0x00, 0x22, // Missing mandatory TLV 3253 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3254 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3255 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3256 }, "", true, false}, 3257 {[]byte{ 3258 0x01, 0x00, 0x00, 0x22, // Incorrect TLV order 3259 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3260 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3261 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3262 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3263 }, "", true, false}, 3264 {[]byte{ 3265 0x01, 0x00, 0x00, 0x26, // Unexpected TLV 3266 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3267 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3268 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3269 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3270 0xfe, 0x01, 0x00, 0x00, // Unsupported 3271 }, "{ASN: 117901063, BGP LS ID: 117901063, OSPF AREA: 117901063, IGP ROUTER ID: 0102.0304.0506}", 3272 false, false}, 3273 {[]byte{ 3274 0x01, 0x00, 0x00, 0x0a, // Missing optional TLVs 3275 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3276 }, "{ASN: 0, BGP LS ID: 0, OSPF AREA: 0, IGP ROUTER ID: 0102.0304.0506}", false, true}, 3277 {[]byte{ 3278 0x01, 0x01, 0x00, 0x20, // Remote Node Desc 3279 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3280 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3281 0x02, 0x04, 0x00, 0x04, 0x0a, 0xff, 0x00, 0x01, // TLV BGP ROUTER ID: "10.255.0.1" 3282 0x02, 0x05, 0x00, 0x04, 0x07, 0x07, 0x07, 0x08, // TLV BGP CONFEDERATION MEMBER: 117901064 3283 }, "{ASN: 117901063, BGP LS ID: 117901063, BGP ROUTER ID: 10.255.0.1}", 3284 false, true}, 3285 } 3286 3287 for _, test := range tests { 3288 tlv := LsTLVNodeDescriptor{} 3289 if test.err { 3290 assert.Error(tlv.DecodeFromBytes(test.in)) 3291 } else { 3292 assert.NoError(tlv.DecodeFromBytes(test.in)) 3293 assert.Equal(test.str, tlv.String()) 3294 if test.serialize { 3295 got, err := tlv.Serialize() 3296 assert.NoError(err) 3297 assert.Equal(test.in, got) 3298 } 3299 } 3300 } 3301 } 3302 3303 func Test_LsAddrPrefix(t *testing.T) { 3304 assert := assert.New(t) 3305 3306 var tests = []struct { 3307 in []byte 3308 str string 3309 err bool 3310 serialize bool 3311 }{ 3312 {[]byte{ 3313 0x00, 0x01, 0x00, 0x2f, // Node NLRI, correct length 3314 0x02, // Protocol ISIS Level 2 3315 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3316 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3317 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3318 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3319 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3320 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3321 }, "NLRI { NODE { AS:117901063 BGP-LS ID:117901063 0102.0304.0506 ISIS-L2:0 } }", false, true}, 3322 {[]byte{ 3323 0x00, 0x01, 0x00, 0x2e, // Node NLRI, truncated length 3324 0x02, // Protocol ISIS Level 2 3325 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3326 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3327 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3328 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3329 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3330 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, // TLV IGP Router ID: 0102.0304.05 3331 }, "", true, false}, 3332 {[]byte{ 3333 0x00, 0x01, 0x00, 0x2f, // Node NLRI, correct length 3334 0x02, // Protocol ISIS Level 2 3335 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3336 0x01, 0x01, 0x00, 0x22, // Remote Node Desc (unexpected) 3337 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3338 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3339 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3340 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3341 }, "", true, false}, 3342 {[]byte{ 3343 0x00, 0x01, 0x00, 0x2d, // Node NLRI, correct length 3344 0x02, // Protocol ISIS Level 2 3345 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3346 // Mandatory TLV missing 3347 }, "", true, false}, 3348 {[]byte{ 3349 0x00, 0x02, 0x00, 0x65, // Link NLRI, correct length 3350 0x02, // Protocol ISIS Level 2 3351 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3352 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3353 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3354 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3355 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3356 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3357 0x01, 0x01, 0x00, 0x22, // Remote Node Desc 3358 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3359 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3360 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3361 0x02, 0x03, 0x00, 0x06, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, // TLV IGP Router ID: 0605.0403.0201 3362 0x01, 0x02, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, // LinkID TLV, Local: 1, Remote: 2 3363 }, "NLRI { LINK { LOCAL_NODE: 0102.0304.0506 REMOTE_NODE: 0605.0403.0201 LINK: 1->2} }", false, true}, 3364 {[]byte{ 3365 0x00, 0x02, 0x00, 0x69, // Link NLRI, correct length 3366 0x02, // Protocol ISIS Level 2 3367 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3368 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3369 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3370 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3371 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3372 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3373 0x01, 0x01, 0x00, 0x22, // Remote Node Desc 3374 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3375 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3376 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3377 0x02, 0x03, 0x00, 0x06, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, // TLV IGP Router ID: 0605.0403.0201 3378 0x01, 0x03, 0x00, 0x04, 0x01, 0x01, 0x01, 0x01, // IPv4 Interface Addr TLV: 1.1.1.1 3379 0x01, 0x04, 0x00, 0x04, 0x02, 0x02, 0x02, 0x02, // IPv4 Neighbor Addr TLV: 2.2.2.2 3380 }, "NLRI { LINK { LOCAL_NODE: 0102.0304.0506 REMOTE_NODE: 0605.0403.0201 LINK: 1.1.1.1->2.2.2.2} }", false, true}, 3381 {[]byte{ 3382 0x00, 0x02, 0x00, 0x81, // Link NLRI, correct length 3383 0x02, // Protocol ISIS Level 2 3384 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3385 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3386 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3387 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3388 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3389 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3390 0x01, 0x01, 0x00, 0x22, // Remote Node Desc 3391 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3392 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3393 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3394 0x02, 0x03, 0x00, 0x06, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, // TLV IGP Router ID: 0605.0403.0201 3395 0x01, 0x05, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF, // IPv6 Interface Addr TLV: 2001:db8::beef 3396 0x01, 0x06, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xDE, 0xAD, // IPv6 Interface Addr TLV: 2001:db8::dead 3397 }, "NLRI { LINK { LOCAL_NODE: 0102.0304.0506 REMOTE_NODE: 0605.0403.0201 LINK: 2001:db8::beef->2001:db8::dead} }", false, true}, 3398 {[]byte{ 3399 0x00, 0x02, 0x00, 0x59, // Link NLRI, correct length 3400 0x02, // Protocol ISIS Level 2 3401 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3402 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3403 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3404 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3405 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3406 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3407 0x01, 0x01, 0x00, 0x22, // Remote Node Desc 3408 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3409 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3410 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3411 0x02, 0x03, 0x00, 0x06, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, // TLV IGP Router ID: 0605.0403.0201 3412 }, "NLRI { LINK { LOCAL_NODE: 0102.0304.0506 REMOTE_NODE: 0605.0403.0201 LINK: UNKNOWN} }", false, true}, 3413 {[]byte{ 3414 0x00, 0x02, 0x00, 0x33, // Link NLRI, correct length 3415 0x02, // Protocol ISIS Level 2 3416 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3417 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3418 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3419 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3420 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3421 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3422 // Missing mandatory TLV 3423 }, "", true, false}, 3424 {[]byte{ 3425 0x00, 0x03, 0x00, 0x35, // Prefix IPv4 NLRI, correct length 3426 0x02, // Protocol ISIS Level 2 3427 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3428 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3429 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3430 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3431 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3432 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3433 0x01, 0x09, 0x00, 0x02, 0x08, 0x0a, // IP ReachabilityInfo TLV, 10.0.0.0/8 3434 }, "NLRI { PREFIXv4 { LOCAL_NODE: 0102.0304.0506 PREFIX: [10.0.0.0/8] } }", false, true}, 3435 {[]byte{ 3436 0x00, 0x03, 0x00, 0x43, // Prefix IPv4 NLRI, correct length 3437 0x02, // Protocol ISIS Level 2 3438 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3439 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3440 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3441 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3442 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3443 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3444 0x01, 0x09, 0x00, 0x02, 0x08, 0x0a, // IP ReachabilityInfo TLV, 10.0.0.0/8 3445 0x01, 0x09, 0x00, 0x05, 0x1f, 0xc0, 0xa8, 0x07, 0xfe, // IP ReachabilityInfo TLV, 192.168.7.254/31 3446 0x01, 0x08, 0x00, 0x01, 0x06, // OSPF Route Type TLV (NSSA2) 3447 }, "NLRI { PREFIXv4 { LOCAL_NODE: 0102.0304.0506 PREFIX: [10.0.0.0/8 192.168.7.254/31] OSPF_ROUTE_TYPE:NSSA2 } }", false, true}, 3448 {[]byte{ 3449 0x00, 0x03, 0x00, 0x35, // Prefix IPv4 NLRI, correct length 3450 0x02, // Protocol ISIS Level 2 3451 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3452 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3453 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3454 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3455 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3456 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3457 0x01, 0x09, 0x00, 0x02, 0x08, 0x0a, // IP ReachabilityInfo TLV, 10.0.0.0/8 3458 }, "NLRI { PREFIXv4 { LOCAL_NODE: 0102.0304.0506 PREFIX: [10.0.0.0/8] } }", false, true}, 3459 {[]byte{ 3460 0x00, 0x03, 0x00, 0x2f, // Prefix IPv4 NLRI, correct length 3461 0x02, // Protocol ISIS Level 2 3462 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3463 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3464 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3465 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3466 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3467 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3468 // Missing mandatory TLV (IP Reachability info) 3469 }, "", true, false}, 3470 {[]byte{ 3471 0x00, 0x03, 0x00, 0x39, // Prefix IPv4 NLRI, correct length 3472 0x02, // Protocol ISIS Level 2 3473 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3474 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3475 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3476 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3477 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3478 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3479 // IPv6 IP Reachability info in v4 prefix 3480 0x01, 0x09, 0x00, 0x06, 0x40, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 3481 }, "", true, false}, 3482 {[]byte{ 3483 0x00, 0x04, 0x00, 0x35, // Prefix IPv6 NLRI, correct length 3484 0x02, // Protocol ISIS Level 2 3485 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3486 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3487 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3488 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3489 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3490 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3491 0x01, 0x09, 0x00, 0x02, 0x08, 0x0a, // IP ReachabilityInfo TLV, 10.0.0.0/8 3492 }, "NLRI { PREFIXv6 { LOCAL_NODE: 0102.0304.0506 PREFIX: [a00::/8] } }", false, true}, 3493 {[]byte{ 3494 0x00, 0x04, 0x00, 0x43, // Prefix IPv6 NLRI, correct length 3495 0x02, // Protocol ISIS Level 2 3496 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3497 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3498 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3499 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3500 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3501 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3502 0x01, 0x09, 0x00, 0x02, 0x08, 0x0a, // IP ReachabilityInfo TLV, 10.0.0.0/8 3503 0x01, 0x09, 0x00, 0x05, 0x1f, 0xc0, 0xa8, 0x07, 0xfe, // IP ReachabilityInfo TLV, 192.168.7.254/31 3504 0x01, 0x08, 0x00, 0x01, 0x06, // OSPF Route Type TLV (NSSA2) 3505 }, "NLRI { PREFIXv6 { LOCAL_NODE: 0102.0304.0506 PREFIX: [a00::/8 c0a8:7fe::/31] OSPF_ROUTE_TYPE:NSSA2 } }", false, true}, 3506 {[]byte{ 3507 0x00, 0x04, 0x00, 0x35, // Prefix IPv6 NLRI, correct length 3508 0x02, // Protocol ISIS Level 2 3509 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3510 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3511 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3512 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3513 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3514 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3515 0x01, 0x09, 0x00, 0x02, 0x08, 0x0a, // IP ReachabilityInfo TLV, 10.0.0.0/8 3516 }, "NLRI { PREFIXv6 { LOCAL_NODE: 0102.0304.0506 PREFIX: [a00::/8] } }", false, true}, 3517 {[]byte{ 3518 0x00, 0x04, 0x00, 0x2f, // Prefix IPv6 NLRI, correct length 3519 0x02, // Protocol ISIS Level 2 3520 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3521 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3522 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3523 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3524 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3525 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3526 // Missing mandatory TLV (IP Reachability info) 3527 }, "", true, false}, 3528 {[]byte{ 3529 0x00, 0x04, 0x00, 0x39, // Prefix IPv6 NLRI, correct length 3530 0x02, // Protocol ISIS Level 2 3531 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ID 3532 0x01, 0x00, 0x00, 0x22, // Local Node Desc 3533 0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063 3534 0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063 3535 0x02, 0x02, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV OSPF Area ID: 117901063 3536 0x02, 0x03, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // TLV IGP Router ID: 0102.0304.0506 3537 // IPv6 IP Reachability info in v4 prefix 3538 0x01, 0x09, 0x00, 0x06, 0x40, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 3539 }, "", true, false}, 3540 } 3541 3542 for _, test := range tests { 3543 nlri := LsAddrPrefix{} 3544 if test.err { 3545 assert.Error(nlri.DecodeFromBytes(test.in)) 3546 } else { 3547 assert.NoError(nlri.DecodeFromBytes(test.in)) 3548 assert.Equal(test.str, nlri.String()) 3549 if test.serialize { 3550 got, err := nlri.Serialize() 3551 assert.NoError(err) 3552 assert.Equal(test.in, got) 3553 } 3554 } 3555 } 3556 } 3557 3558 func Test_PathAttributeLs(t *testing.T) { 3559 assert := assert.New(t) 3560 3561 var tests = []struct { 3562 in []byte 3563 str string 3564 json string 3565 serialize bool 3566 err bool 3567 }{ 3568 {[]byte{ 3569 // LS Attribute with all Node-related TLVs. 3570 0x80, 0x29, 0x62, // Optional attribute, BGP_ATTR_TYPE_LS, correct length 3571 0x04, 0x00, 0x00, 0x01, 0xFF, // Node flags (all set) 3572 0x04, 0x01, 0x00, 0x03, 0x01, 0x02, 0x03, // Opaque Node Attr [1 2 3] 3573 0x04, 0x02, 0x00, 0x03, 0x72, 0x74, 0x72, // Node name: "rtr" 3574 0x04, 0x03, 0x00, 0x03, 0x72, 0x74, 0x72, // ISIS area: [114 116 114] 3575 0x04, 0x04, 0x00, 0x04, 0x01, 0x01, 0x01, 0x01, // Local RouterID 1.1.1.1 3576 0x04, 0x05, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF, // Local Router ID 2001:db8::beef 3577 0x04, 0x0a, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x88, 0xb8, 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // Capabilities: Range 35000, first label: 100500 3578 0x04, 0x0b, 0x00, 0x03, 0x01, 0x02, 0x03, // SR ALgorithm [1 2 3] 3579 0x04, 0x0c, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x88, 0xb8, 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // Local block: Range 35000, first label: 100500 3580 0xde, 0xad, 0x00, 0x01, 0xFF, // Unknown TLV 3581 }, 3582 "{LsAttributes: {Node Flags: XXVRBETO} {Opaque attribute: [1 2 3]} {Node Name: rtr} {ISIS Area ID: [114 116 114]} {Local RouterID IPv4: 1.1.1.1} {Local RouterID IPv6: 2001:db8::beef} {SR Capabilities: Flags:0 SRGB Ranges: 100500:135500 } {SR Algorithms: [1 2 3]} {SR LocalBlock: Flags:0 SRGB Ranges: 100500:135500 } }", 3583 `{"type":41,"flags":128,"node":{"flags":{"overload":true,"attached":true,"external":true,"abr":true,"router":true,"v6":true},"opaque":"AQID","name":"rtr","isis_area":"cnRy","local_router_id_ipv4":"1.1.1.1","local_router_id_ipv6":"2001:db8::beef","sr_capabilities":{"ipv4_supported":false,"ipv6_supported":false,"ranges":[{"begin":100500,"end":135500}]},"sr_algorithms":"AQID","sr_local_block":{"ranges":[{"begin":100500,"end":135500}]}},"link":{"local_router_id_ipv4":"1.1.1.1","local_router_id_ipv6":"2001:db8::beef"},"prefix":{},"bgp_peer_segment":{}}`, 3584 false, false}, 3585 {[]byte{ 3586 // LS Attribute with all Node-related TLVs. 3587 0x80, 0x29, 0x5d, // Optional attribute, BGP_ATTR_TYPE_LS, correct length 3588 0x04, 0x00, 0x00, 0x01, 0xFF, // Node flags (all set) 3589 0x04, 0x01, 0x00, 0x03, 0x01, 0x02, 0x03, // Opaque Node Attr [1 2 3] 3590 0x04, 0x02, 0x00, 0x03, 0x72, 0x74, 0x72, // Node name: "rtr" 3591 0x04, 0x03, 0x00, 0x03, 0x72, 0x74, 0x72, // ISIS area: [114 116 114] 3592 0x04, 0x04, 0x00, 0x04, 0x01, 0x01, 0x01, 0x01, // Local RouterID 1.1.1.1 3593 0x04, 0x05, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF, // Local Router ID 2001:db8::beef 3594 0x04, 0x0a, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x88, 0xb8, 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // Capabilities: Range 35000, first label: 100500 3595 0x04, 0x0b, 0x00, 0x03, 0x01, 0x02, 0x03, // SR Algorithm [1 2 3] 3596 0x04, 0x0c, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x88, 0xb8, 0x04, 0x89, 0x00, 0x03, 0x01, 0x88, 0x94, // Local block: Range 35000, first label: 100500 3597 }, 3598 "{LsAttributes: {Node Flags: XXVRBETO} {Opaque attribute: [1 2 3]} {Node Name: rtr} {ISIS Area ID: [114 116 114]} {Local RouterID IPv4: 1.1.1.1} {Local RouterID IPv6: 2001:db8::beef} {SR Capabilities: Flags:0 SRGB Ranges: 100500:135500 } {SR Algorithms: [1 2 3]} {SR LocalBlock: Flags:0 SRGB Ranges: 100500:135500 } }", 3599 `{"type":41,"flags":128,"node":{"flags":{"overload":true,"attached":true,"external":true,"abr":true,"router":true,"v6":true},"opaque":"AQID","name":"rtr","isis_area":"cnRy","local_router_id_ipv4":"1.1.1.1","local_router_id_ipv6":"2001:db8::beef","sr_capabilities":{"ipv4_supported":false,"ipv6_supported":false,"ranges":[{"begin":100500,"end":135500}]},"sr_algorithms":"AQID","sr_local_block":{"ranges":[{"begin":100500,"end":135500}]}},"link":{"local_router_id_ipv4":"1.1.1.1","local_router_id_ipv6":"2001:db8::beef"},"prefix":{},"bgp_peer_segment":{}}`, 3600 true, false}, 3601 {[]byte{ 3602 // LS Attribute with truncated length 3603 0x80, 0x29, 0x04, // Optional attribute, BGP_ATTR_TYPE_LS, truncated length 3604 0x04, 0x00, 0x00, 0x01, 0xFF, // Node flags (all set) 3605 }, "", "", false, true}, 3606 {[]byte{ 3607 // LS Attribute with all Link-related TLVs. 3608 0x80, 0x29, 0x9a, // Optional attribute, BGP_ATTR_TYPE_LS, correct length 3609 0x04, 0x04, 0x00, 0x04, 0x01, 0x01, 0x01, 0x01, // Local RouterID 1.1.1.1 3610 0x04, 0x05, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xEF, // Local Router ID 2001:db8::beef 3611 0x04, 0x06, 0x00, 0x04, 0x02, 0x02, 0x02, 0x02, // Local RouterID 2.2.2.2 3612 0x04, 0x07, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xDE, 0xAD, // Local Router ID 2001:db8::dead 3613 0x04, 0x40, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // Admin Group 0x07070707 3614 0x04, 0x41, 0x00, 0x04, 0x43, 0xA4, 0xB2, 0x00, // Max Link Bandwidth 329.39062 3615 0x04, 0x42, 0x00, 0x04, 0x43, 0xA4, 0xB2, 0x00, // Max Reservable Bandwidth 329.39062 3616 0x04, 0x43, 0x00, 0x20, 0x43, 0xA4, 0xB2, 0x00, 0x43, 0xA4, 0xB2, 0x00, 0x43, 0xA4, 0xB2, 0x00, 0x43, 0xA4, 0xB2, 0x00, 0x43, 0xA4, 0xB2, 0x00, 0x43, 0xA4, 0xB2, 0x00, 0x43, 0xA4, 0xB2, 0x00, 0x43, 0xA4, 0xB2, 0x00, // Unreserved Bandwidth 329.39062 3617 0x04, 0x44, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TE Default Metric: 117901063 3618 0x04, 0x47, 0x00, 0x01, 0x01, // IGP Metric 1 3619 0x04, 0x49, 0x00, 0x03, 0x01, 0x02, 0x03, // Opaque Link Attr: [1 2 3] 3620 0x04, 0x4a, 0x00, 0x03, 0x72, 0x74, 0x72, // Link Name: "rtr" 3621 0x04, 0x4b, 0x00, 0x07, 0x01, 0x01, 0x00, 0x00, 0x01, 0x88, 0x94, // Adjacency SID: 100500 3622 }, 3623 "{LsAttributes: {Local RouterID IPv4: 1.1.1.1} {Local RouterID IPv6: 2001:db8::beef} {Remote RouterID IPv4: 2.2.2.2} {Remote RouterID IPv6: 2001:db8::dead} {Admin Group: 07070707} {Max Link BW: 329.39062} {Max Reservable Link BW: 329.39062} {Unreserved BW: [329.39062 329.39062 329.39062 329.39062 329.39062 329.39062 329.39062 329.39062]} {TE Default metric: 117901063} {IGP metric: 1} {Opaque link attribute: [1 2 3]} {Link Name: rtr} {Adjacency SID: 100500} }", 3624 `{"type":41,"flags":128,"node":{"local_router_id_ipv4":"1.1.1.1","local_router_id_ipv6":"2001:db8::beef"},"link":{"name":"rtr","local_router_id_ipv4":"1.1.1.1","local_router_id_ipv6":"2001:db8::beef","remote_router_id_ipv4":"2.2.2.2","remote_router_id_ipv6":"2001:db8::dead","admin_group":117901063,"default_te_metric":117901063,"igp_metric":1,"opaque":"AQID","bandwidth":329.39062,"reservable_bandwidth":329.39062,"unreserved_bandwidth":[329.39062,329.39062,329.39062,329.39062,329.39062,329.39062,329.39062,329.39062],"adjacency_sid":100500},"prefix":{},"bgp_peer_segment":{}}`, 3625 true, false}, 3626 {[]byte{ 3627 // LS Attribute with all Link-related TLVs. 3628 0x80, 0x29, 0x17, // Optional attribute, BGP_ATTR_TYPE_LS, correct length 3629 0x04, 0x80, 0x00, 0x01, 0xFF, // IGP Flags: PLND 3630 0x04, 0x85, 0x00, 0x03, 0x01, 0x02, 0x03, // Opaque prefix: [1 2 3] 3631 0x04, 0x86, 0x00, 0x07, 0x01, 0x01, 0x00, 0x00, 0x01, 0x88, 0x94, // Prefix SID: 100500 3632 }, 3633 "{LsAttributes: {IGP Flags: XXXXPLND} {Prefix opaque attribute: [1 2 3]} {Prefix SID: 100500} }", 3634 `{"type":41,"flags":128,"node":{},"link":{},"prefix":{"igp_flags":{"down":true,"no_unicast":true,"local_address":true,"propagate_nssa":true},"opaque":"AQID","sr_prefix_sid":100500},"bgp_peer_segment":{}}`, 3635 true, false}, 3636 } 3637 3638 for _, test := range tests { 3639 attr := PathAttributeLs{} 3640 if test.err { 3641 assert.Error(attr.DecodeFromBytes(test.in)) 3642 } else { 3643 assert.NoError(attr.DecodeFromBytes(test.in)) 3644 got, err := attr.MarshalJSON() 3645 assert.NoError(err) 3646 assert.Equal(test.json, string(got)) 3647 assert.Equal(test.str, attr.String()) 3648 3649 if test.serialize { 3650 got, err := attr.Serialize() 3651 assert.NoError(err) 3652 assert.Equal(test.in, got) 3653 } 3654 } 3655 } 3656 } 3657 3658 func Test_BGPOpenDecodeCapabilities(t *testing.T) { 3659 // BGP OPEN message with add-path and long-lived-graceful-restart capabilities, 3660 // in that order. 3661 openBytes := []byte{ 3662 0x04, // version: 4 3663 0xfa, 0x7b, // my as: 64123 3664 0x00, 0xf0, // hold time: 240 seconds 3665 0x7f, 0x00, 0x00, 0x02, // BGP identifier: 127.0.0.2 3666 0x19, // optional parameters length: 25 3667 0x02, // parameter type: capability 3668 0x17, // parameter length: 23 3669 3670 0x05, // capability type: extended next hop 3671 0x06, // caability length: 6 3672 0x00, 0x01, // AFI: IPv4 3673 0x00, 0x01, // SAFI: unicast 3674 0x00, 0x02, // next hop AFI: IPv6 3675 3676 0x45, // capability type: ADD-PATH 3677 0x04, // capability length: 4 3678 0x00, 0x01, // AFI: IPv4 3679 0x01, // SAFI: unicast 3680 0x02, // Send/Receive: Send 3681 3682 0x47, // capability type: Long-lived-graceful-restart 3683 0x07, // capability length: 7 3684 0x00, 0x01, 0x01, 0x00, 0x00, 0x0e, 0x10, 3685 } 3686 3687 open := &BGPOpen{} 3688 err := open.DecodeFromBytes(openBytes) 3689 assert.NoError(t, err) 3690 3691 capMap := make(map[BGPCapabilityCode][]ParameterCapabilityInterface) 3692 for _, p := range open.OptParams { 3693 if paramCap, y := p.(*OptionParameterCapability); y { 3694 t.Logf("parameter capability: %+v", paramCap) 3695 for _, c := range paramCap.Capability { 3696 m, ok := capMap[c.Code()] 3697 if !ok { 3698 m = make([]ParameterCapabilityInterface, 0, 1) 3699 } 3700 capMap[c.Code()] = append(m, c) 3701 } 3702 } 3703 } 3704 3705 assert.Len(t, capMap[BGP_CAP_EXTENDED_NEXTHOP], 1) 3706 nexthopTuples := capMap[BGP_CAP_EXTENDED_NEXTHOP][0].(*CapExtendedNexthop).Tuples 3707 assert.Len(t, nexthopTuples, 1) 3708 assert.Equal(t, nexthopTuples[0].NLRIAFI, uint16(AFI_IP)) 3709 assert.Equal(t, nexthopTuples[0].NLRISAFI, uint16(SAFI_UNICAST)) 3710 assert.Equal(t, nexthopTuples[0].NexthopAFI, uint16(AFI_IP6)) 3711 3712 assert.Len(t, capMap[BGP_CAP_ADD_PATH], 1) 3713 tuples := capMap[BGP_CAP_ADD_PATH][0].(*CapAddPath).Tuples 3714 assert.Len(t, tuples, 1) 3715 assert.Equal(t, tuples[0].RouteFamily, RF_IPv4_UC) 3716 assert.Equal(t, tuples[0].Mode, BGP_ADD_PATH_SEND) 3717 } 3718 3719 func FuzzParseBGPMessage(f *testing.F) { 3720 3721 f.Fuzz(func(t *testing.T, data []byte) { 3722 ParseBGPMessage(data) 3723 }) 3724 } 3725 3726 func FuzzParseFlowSpecComponents(f *testing.F) { 3727 3728 f.Fuzz(func(t *testing.T, data string) { 3729 ParseFlowSpecComponents(RF_FS_IPv4_UC, data) 3730 }) 3731 }