github.com/osrg/gobgp/v3@v3.30.0/pkg/apiutil/attribute_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 apiutil 17 18 import ( 19 "bytes" 20 "net" 21 "testing" 22 23 "google.golang.org/protobuf/proto" 24 apb "google.golang.org/protobuf/types/known/anypb" 25 26 api "github.com/osrg/gobgp/v3/api" 27 "github.com/osrg/gobgp/v3/pkg/packet/bgp" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func Test_OriginAttribute(t *testing.T) { 32 assert := assert.New(t) 33 34 input := &api.OriginAttribute{ 35 Origin: 0, // IGP 36 } 37 a, err := apb.New(input) 38 assert.Nil(err) 39 n, err := UnmarshalAttribute(a) 40 assert.Nil(err) 41 42 output, _ := NewOriginAttributeFromNative(n.(*bgp.PathAttributeOrigin)) 43 assert.Equal(input.Origin, output.Origin) 44 } 45 46 func Test_AsPathAttribute(t *testing.T) { 47 assert := assert.New(t) 48 49 input := &api.AsPathAttribute{ 50 Segments: []*api.AsSegment{ 51 { 52 Type: 1, // SET 53 Numbers: []uint32{100, 200}, 54 }, 55 { 56 Type: 2, // SEQ 57 Numbers: []uint32{300, 400}, 58 }, 59 }, 60 } 61 62 a, err := apb.New(input) 63 assert.Nil(err) 64 n, err := UnmarshalAttribute(a) 65 assert.Nil(err) 66 67 output, _ := NewAsPathAttributeFromNative(n.(*bgp.PathAttributeAsPath)) 68 assert.Equal(2, len(output.Segments)) 69 for i := 0; i < 2; i++ { 70 assert.True(proto.Equal(input.Segments[i], output.Segments[i])) 71 } 72 } 73 74 func Test_NextHopAttribute(t *testing.T) { 75 assert := assert.New(t) 76 77 input := &api.NextHopAttribute{ 78 NextHop: "192.168.0.1", 79 } 80 81 a, err := apb.New(input) 82 assert.Nil(err) 83 n, err := UnmarshalAttribute(a) 84 assert.Nil(err) 85 86 output, _ := NewNextHopAttributeFromNative(n.(*bgp.PathAttributeNextHop)) 87 assert.Equal(input.NextHop, output.NextHop) 88 } 89 90 func Test_MultiExitDiscAttribute(t *testing.T) { 91 assert := assert.New(t) 92 93 input := &api.MultiExitDiscAttribute{ 94 Med: 100, 95 } 96 97 a, err := apb.New(input) 98 assert.Nil(err) 99 n, err := UnmarshalAttribute(a) 100 assert.Nil(err) 101 102 output, _ := NewMultiExitDiscAttributeFromNative(n.(*bgp.PathAttributeMultiExitDisc)) 103 assert.Equal(input.Med, output.Med) 104 } 105 106 func Test_LocalPrefAttribute(t *testing.T) { 107 assert := assert.New(t) 108 109 input := &api.LocalPrefAttribute{ 110 LocalPref: 100, 111 } 112 113 a, err := apb.New(input) 114 assert.Nil(err) 115 n, err := UnmarshalAttribute(a) 116 assert.Nil(err) 117 118 output, _ := NewLocalPrefAttributeFromNative(n.(*bgp.PathAttributeLocalPref)) 119 assert.Equal(input.LocalPref, output.LocalPref) 120 } 121 122 func Test_AtomicAggregateAttribute(t *testing.T) { 123 assert := assert.New(t) 124 125 input := &api.AtomicAggregateAttribute{} 126 127 a, err := apb.New(input) 128 assert.Nil(err) 129 n, err := UnmarshalAttribute(a) 130 assert.Nil(err) 131 132 output, _ := NewAtomicAggregateAttributeFromNative(n.(*bgp.PathAttributeAtomicAggregate)) 133 // AtomicAggregateAttribute has no value 134 assert.NotNil(output) 135 } 136 137 func Test_AggregatorAttribute(t *testing.T) { 138 assert := assert.New(t) 139 140 input := &api.AggregatorAttribute{ 141 Asn: 65000, 142 Address: "1.1.1.1", 143 } 144 145 a, err := apb.New(input) 146 assert.Nil(err) 147 n, err := UnmarshalAttribute(a) 148 assert.Nil(err) 149 150 output, _ := NewAggregatorAttributeFromNative(n.(*bgp.PathAttributeAggregator)) 151 assert.Equal(input.Asn, output.Asn) 152 assert.Equal(input.Address, output.Address) 153 } 154 155 func Test_CommunitiesAttribute(t *testing.T) { 156 assert := assert.New(t) 157 158 input := &api.CommunitiesAttribute{ 159 Communities: []uint32{100, 200}, 160 } 161 162 a, err := apb.New(input) 163 assert.Nil(err) 164 n, err := UnmarshalAttribute(a) 165 assert.Nil(err) 166 167 output, _ := NewCommunitiesAttributeFromNative(n.(*bgp.PathAttributeCommunities)) 168 assert.Equal(input.Communities, output.Communities) 169 } 170 171 func Test_OriginatorIdAttribute(t *testing.T) { 172 assert := assert.New(t) 173 174 input := &api.OriginatorIdAttribute{ 175 Id: "1.1.1.1", 176 } 177 178 a, err := apb.New(input) 179 assert.Nil(err) 180 n, err := UnmarshalAttribute(a) 181 assert.Nil(err) 182 183 output, _ := NewOriginatorIdAttributeFromNative(n.(*bgp.PathAttributeOriginatorId)) 184 assert.Equal(input.Id, output.Id) 185 } 186 187 func Test_ClusterListAttribute(t *testing.T) { 188 assert := assert.New(t) 189 190 input := &api.ClusterListAttribute{ 191 Ids: []string{"1.1.1.1", "2.2.2.2"}, 192 } 193 194 a, err := apb.New(input) 195 assert.Nil(err) 196 n, err := UnmarshalAttribute(a) 197 assert.Nil(err) 198 199 output, _ := NewClusterListAttributeFromNative(n.(*bgp.PathAttributeClusterList)) 200 assert.Equal(input.Ids, output.Ids) 201 } 202 203 func Test_MpReachNLRIAttribute_IPv4_UC(t *testing.T) { 204 assert := assert.New(t) 205 206 nlris := make([]*apb.Any, 0, 2) 207 a, err := apb.New(&api.IPAddressPrefix{ 208 PrefixLen: 24, 209 Prefix: "192.168.101.0", 210 }) 211 assert.Nil(err) 212 nlris = append(nlris, a) 213 a, err = apb.New(&api.IPAddressPrefix{ 214 PrefixLen: 24, 215 Prefix: "192.168.201.0", 216 }) 217 assert.Nil(err) 218 nlris = append(nlris, a) 219 220 input := &api.MpReachNLRIAttribute{ 221 Family: &api.Family{ 222 Afi: api.Family_AFI_IP, 223 Safi: api.Family_SAFI_UNICAST, 224 }, 225 NextHops: []string{"192.168.1.1"}, 226 Nlris: nlris, 227 } 228 229 a, err = apb.New(input) 230 assert.Nil(err) 231 n, err := UnmarshalAttribute(a) 232 assert.Nil(err) 233 234 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 235 assert.Equal(input.Family.Afi, output.Family.Afi) 236 assert.Equal(input.Family.Safi, output.Family.Safi) 237 assert.Equal(input.NextHops, output.NextHops) 238 assert.Equal(2, len(output.Nlris)) 239 for idx, inputNLRI := range input.Nlris { 240 outputNLRI := output.Nlris[idx] 241 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 242 assert.Equal(inputNLRI.Value, outputNLRI.Value) 243 } 244 } 245 246 func Test_MpReachNLRIAttribute_IPv6_UC(t *testing.T) { 247 assert := assert.New(t) 248 249 nlris := make([]*apb.Any, 0, 2) 250 a, err := apb.New(&api.IPAddressPrefix{ 251 PrefixLen: 64, 252 Prefix: "2001:db8:1::", 253 }) 254 assert.Nil(err) 255 nlris = append(nlris, a) 256 a, err = apb.New(&api.IPAddressPrefix{ 257 PrefixLen: 64, 258 Prefix: "2001:db8:2::", 259 }) 260 assert.Nil(err) 261 nlris = append(nlris, a) 262 263 input := &api.MpReachNLRIAttribute{ 264 Family: &api.Family{ 265 Afi: api.Family_AFI_IP6, 266 Safi: api.Family_SAFI_UNICAST, 267 }, 268 NextHops: []string{"2001:db8::1", "fe80::1"}, 269 Nlris: nlris, 270 } 271 272 a, err = apb.New(input) 273 assert.Nil(err) 274 n, err := UnmarshalAttribute(a) 275 assert.Nil(err) 276 277 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 278 assert.Equal(input.Family.Afi, output.Family.Afi) 279 assert.Equal(input.Family.Safi, output.Family.Safi) 280 assert.Equal(input.NextHops, output.NextHops) 281 assert.Equal(2, len(output.Nlris)) 282 for idx, inputNLRI := range input.Nlris { 283 outputNLRI := output.Nlris[idx] 284 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 285 assert.Equal(inputNLRI.Value, outputNLRI.Value) 286 } 287 } 288 289 func Test_MpReachNLRIAttribute_IPv4_MPLS(t *testing.T) { 290 assert := assert.New(t) 291 292 nlris := make([]*apb.Any, 0, 2) 293 a, err := apb.New(&api.LabeledIPAddressPrefix{ 294 Labels: []uint32{100}, 295 PrefixLen: 24, 296 Prefix: "192.168.101.0", 297 }) 298 assert.Nil(err) 299 nlris = append(nlris, a) 300 a, err = apb.New(&api.LabeledIPAddressPrefix{ 301 Labels: []uint32{200}, 302 PrefixLen: 24, 303 Prefix: "192.168.201.0", 304 }) 305 assert.Nil(err) 306 nlris = append(nlris, a) 307 308 input := &api.MpReachNLRIAttribute{ 309 Family: &api.Family{ 310 Afi: api.Family_AFI_IP, 311 Safi: api.Family_SAFI_MPLS_LABEL, 312 }, 313 NextHops: []string{"192.168.1.1"}, 314 Nlris: nlris, 315 } 316 317 a, err = apb.New(input) 318 assert.Nil(err) 319 n, err := UnmarshalAttribute(a) 320 assert.Nil(err) 321 322 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 323 assert.Equal(input.Family.Afi, output.Family.Afi) 324 assert.Equal(input.Family.Safi, output.Family.Safi) 325 assert.Equal(input.NextHops, output.NextHops) 326 assert.Equal(2, len(output.Nlris)) 327 for idx, inputNLRI := range input.Nlris { 328 outputNLRI := output.Nlris[idx] 329 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 330 assert.Equal(inputNLRI.Value, outputNLRI.Value) 331 } 332 } 333 334 func Test_MpReachNLRIAttribute_IPv6_MPLS(t *testing.T) { 335 assert := assert.New(t) 336 337 nlris := make([]*apb.Any, 0, 2) 338 a, err := apb.New(&api.LabeledIPAddressPrefix{ 339 Labels: []uint32{100}, 340 PrefixLen: 64, 341 Prefix: "2001:db8:1::", 342 }) 343 assert.Nil(err) 344 nlris = append(nlris, a) 345 a, err = apb.New(&api.LabeledIPAddressPrefix{ 346 Labels: []uint32{200}, 347 PrefixLen: 64, 348 Prefix: "2001:db8:2::", 349 }) 350 assert.Nil(err) 351 nlris = append(nlris, a) 352 353 input := &api.MpReachNLRIAttribute{ 354 Family: &api.Family{ 355 Afi: api.Family_AFI_IP6, 356 Safi: api.Family_SAFI_MPLS_LABEL, 357 }, 358 NextHops: []string{"2001:db8::1"}, 359 Nlris: nlris, 360 } 361 362 a, err = apb.New(input) 363 assert.Nil(err) 364 n, err := UnmarshalAttribute(a) 365 assert.Nil(err) 366 367 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 368 assert.Equal(input.Family.Afi, output.Family.Afi) 369 assert.Equal(input.Family.Safi, output.Family.Safi) 370 assert.Equal(input.NextHops, output.NextHops) 371 assert.Equal(2, len(output.Nlris)) 372 for idx, inputNLRI := range input.Nlris { 373 outputNLRI := output.Nlris[idx] 374 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 375 assert.Equal(inputNLRI.Value, outputNLRI.Value) 376 } 377 } 378 379 func Test_MpReachNLRIAttribute_IPv4_ENCAP(t *testing.T) { 380 assert := assert.New(t) 381 382 nlris := make([]*apb.Any, 0, 2) 383 a, err := apb.New(&api.EncapsulationNLRI{ 384 Address: "192.168.101.1", 385 }) 386 assert.Nil(err) 387 nlris = append(nlris, a) 388 a, err = apb.New(&api.EncapsulationNLRI{ 389 Address: "192.168.201.1", 390 }) 391 assert.Nil(err) 392 nlris = append(nlris, a) 393 394 input := &api.MpReachNLRIAttribute{ 395 Family: &api.Family{ 396 Afi: api.Family_AFI_IP, 397 Safi: api.Family_SAFI_ENCAPSULATION, 398 }, 399 NextHops: []string{"192.168.1.1"}, 400 Nlris: nlris, 401 } 402 403 a, err = apb.New(input) 404 assert.Nil(err) 405 n, err := UnmarshalAttribute(a) 406 assert.Nil(err) 407 408 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 409 assert.Equal(input.Family.Afi, output.Family.Afi) 410 assert.Equal(input.Family.Safi, output.Family.Safi) 411 assert.Equal(input.NextHops, output.NextHops) 412 assert.Equal(2, len(output.Nlris)) 413 for idx, inputNLRI := range input.Nlris { 414 outputNLRI := output.Nlris[idx] 415 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 416 assert.Equal(inputNLRI.Value, outputNLRI.Value) 417 } 418 } 419 420 func Test_MpReachNLRIAttribute_IPv6_ENCAP(t *testing.T) { 421 assert := assert.New(t) 422 423 nlris := make([]*apb.Any, 0, 2) 424 a, err := apb.New(&api.EncapsulationNLRI{ 425 Address: "2001:db8:1::1", 426 }) 427 assert.Nil(err) 428 nlris = append(nlris, a) 429 a, err = apb.New(&api.EncapsulationNLRI{ 430 Address: "2001:db8:2::1", 431 }) 432 assert.Nil(err) 433 nlris = append(nlris, a) 434 435 input := &api.MpReachNLRIAttribute{ 436 Family: &api.Family{ 437 Afi: api.Family_AFI_IP6, 438 Safi: api.Family_SAFI_ENCAPSULATION, 439 }, 440 NextHops: []string{"2001:db8::1"}, 441 Nlris: nlris, 442 } 443 444 a, err = apb.New(input) 445 assert.Nil(err) 446 n, err := UnmarshalAttribute(a) 447 assert.Nil(err) 448 449 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 450 assert.Equal(input.Family.Afi, output.Family.Afi) 451 assert.Equal(input.Family.Safi, output.Family.Safi) 452 assert.Equal(input.NextHops, output.NextHops) 453 assert.Equal(2, len(output.Nlris)) 454 for idx, inputNLRI := range input.Nlris { 455 outputNLRI := output.Nlris[idx] 456 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 457 assert.Equal(inputNLRI.Value, outputNLRI.Value) 458 } 459 } 460 461 func Test_MpReachNLRIAttribute_VPLS(t *testing.T) { 462 assert := assert.New(t) 463 464 nlris := make([]*apb.Any, 0, 1) 465 rd, err := apb.New(&api.RouteDistinguisherTwoOctetASN{ 466 Admin: 65000, 467 Assigned: 100, 468 }) 469 assert.Nil(err) 470 a, err := apb.New(&api.VPLSNLRI{ 471 Rd: rd, 472 VeId: 101, 473 VeBlockOffset: 100, 474 VeBlockSize: 10, 475 LabelBlockBase: 1000, 476 }) 477 assert.Nil(err) 478 nlris = append(nlris, a) 479 480 input := &api.MpReachNLRIAttribute{ 481 Family: &api.Family{ 482 Afi: api.Family_AFI_L2VPN, 483 Safi: api.Family_SAFI_VPLS, 484 }, 485 NextHops: []string{"192.168.1.1"}, 486 Nlris: nlris, 487 } 488 489 a, err = apb.New(input) 490 assert.Nil(err) 491 n, err := UnmarshalAttribute(a) 492 assert.Nil(err) 493 494 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 495 assert.Equal(input.Family.Afi, output.Family.Afi) 496 assert.Equal(input.Family.Safi, output.Family.Safi) 497 assert.Equal(input.NextHops, output.NextHops) 498 assert.Equal(1, len(output.Nlris)) 499 for idx, inputNLRI := range input.Nlris { 500 outputNLRI := output.Nlris[idx] 501 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 502 assert.Equal(inputNLRI.Value, outputNLRI.Value) 503 } 504 } 505 506 func Test_MpReachNLRIAttribute_EVPN_AD_Route(t *testing.T) { 507 assert := assert.New(t) 508 509 nlris := make([]*apb.Any, 0, 1) 510 rd, err := apb.New(&api.RouteDistinguisherTwoOctetASN{ 511 Admin: 65000, 512 Assigned: 100, 513 }) 514 assert.Nil(err) 515 esi := &api.EthernetSegmentIdentifier{ 516 Type: 0, 517 Value: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}, 518 } 519 a, err := apb.New(&api.EVPNEthernetAutoDiscoveryRoute{ 520 Rd: rd, 521 Esi: esi, 522 EthernetTag: 100, 523 Label: 200, 524 }) 525 assert.Nil(err) 526 nlris = append(nlris, a) 527 528 input := &api.MpReachNLRIAttribute{ 529 Family: &api.Family{ 530 Afi: api.Family_AFI_L2VPN, 531 Safi: api.Family_SAFI_EVPN, 532 }, 533 NextHops: []string{"192.168.1.1"}, 534 Nlris: nlris, 535 } 536 537 a, err = apb.New(input) 538 assert.Nil(err) 539 n, err := UnmarshalAttribute(a) 540 assert.Nil(err) 541 542 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 543 assert.Equal(input.Family.Afi, output.Family.Afi) 544 assert.Equal(input.Family.Safi, output.Family.Safi) 545 assert.Equal(input.NextHops, output.NextHops) 546 assert.Equal(1, len(output.Nlris)) 547 for idx, inputNLRI := range input.Nlris { 548 outputNLRI := output.Nlris[idx] 549 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 550 assert.Equal(inputNLRI.Value, outputNLRI.Value) 551 } 552 } 553 554 func Test_MpReachNLRIAttribute_EVPN_MAC_IP_Route(t *testing.T) { 555 assert := assert.New(t) 556 557 nlris := make([]*apb.Any, 0, 1) 558 rd, err := apb.New(&api.RouteDistinguisherIPAddress{ 559 Admin: "1.1.1.1", 560 Assigned: 100, 561 }) 562 assert.Nil(err) 563 esi := &api.EthernetSegmentIdentifier{ 564 Type: 0, 565 Value: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}, 566 } 567 a, err := apb.New(&api.EVPNMACIPAdvertisementRoute{ 568 Rd: rd, 569 Esi: esi, 570 EthernetTag: 100, 571 MacAddress: "aa:bb:cc:dd:ee:ff", 572 IpAddress: "192.168.101.1", 573 Labels: []uint32{200}, 574 }) 575 assert.Nil(err) 576 nlris = append(nlris, a) 577 578 input := &api.MpReachNLRIAttribute{ 579 Family: &api.Family{ 580 Afi: api.Family_AFI_L2VPN, 581 Safi: api.Family_SAFI_EVPN, 582 }, 583 NextHops: []string{"192.168.1.1"}, 584 Nlris: nlris, 585 } 586 587 a, err = apb.New(input) 588 assert.Nil(err) 589 n, err := UnmarshalAttribute(a) 590 assert.Nil(err) 591 592 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 593 assert.Equal(input.Family.Afi, output.Family.Afi) 594 assert.Equal(input.Family.Safi, output.Family.Safi) 595 assert.Equal(input.NextHops, output.NextHops) 596 assert.Equal(1, len(output.Nlris)) 597 for idx, inputNLRI := range input.Nlris { 598 outputNLRI := output.Nlris[idx] 599 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 600 assert.Equal(inputNLRI.Value, outputNLRI.Value) 601 } 602 } 603 604 func Test_MpReachNLRIAttribute_EVPN_MC_Route(t *testing.T) { 605 assert := assert.New(t) 606 607 nlris := make([]*apb.Any, 0, 1) 608 rd, err := apb.New(&api.RouteDistinguisherFourOctetASN{ 609 Admin: 65000, 610 Assigned: 100, 611 }) 612 assert.Nil(err) 613 a, err := apb.New(&api.EVPNInclusiveMulticastEthernetTagRoute{ 614 Rd: rd, 615 EthernetTag: 100, 616 IpAddress: "192.168.101.1", 617 }) 618 assert.Nil(err) 619 nlris = append(nlris, a) 620 621 input := &api.MpReachNLRIAttribute{ 622 Family: &api.Family{ 623 Afi: api.Family_AFI_L2VPN, 624 Safi: api.Family_SAFI_EVPN, 625 }, 626 NextHops: []string{"192.168.1.1"}, 627 Nlris: nlris, 628 } 629 630 a, err = apb.New(input) 631 assert.Nil(err) 632 n, err := UnmarshalAttribute(a) 633 assert.Nil(err) 634 635 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 636 assert.Equal(input.Family.Afi, output.Family.Afi) 637 assert.Equal(input.Family.Safi, output.Family.Safi) 638 assert.Equal(input.NextHops, output.NextHops) 639 assert.Equal(1, len(output.Nlris)) 640 for idx, inputNLRI := range input.Nlris { 641 outputNLRI := output.Nlris[idx] 642 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 643 assert.Equal(inputNLRI.Value, outputNLRI.Value) 644 } 645 } 646 647 func Test_MpReachNLRIAttribute_EVPN_ES_Route(t *testing.T) { 648 assert := assert.New(t) 649 650 nlris := make([]*apb.Any, 0, 1) 651 rd, err := apb.New(&api.RouteDistinguisherIPAddress{ 652 Admin: "1.1.1.1", 653 Assigned: 100, 654 }) 655 assert.Nil(err) 656 esi := &api.EthernetSegmentIdentifier{ 657 Type: 0, 658 Value: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}, 659 } 660 a, err := apb.New(&api.EVPNEthernetSegmentRoute{ 661 Rd: rd, 662 Esi: esi, 663 IpAddress: "192.168.101.1", 664 }) 665 assert.Nil(err) 666 nlris = append(nlris, a) 667 668 input := &api.MpReachNLRIAttribute{ 669 Family: &api.Family{ 670 Afi: api.Family_AFI_L2VPN, 671 Safi: api.Family_SAFI_EVPN, 672 }, 673 NextHops: []string{"192.168.1.1"}, 674 Nlris: nlris, 675 } 676 677 a, err = apb.New(input) 678 assert.Nil(err) 679 n, err := UnmarshalAttribute(a) 680 assert.Nil(err) 681 682 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 683 assert.Equal(input.Family.Afi, output.Family.Afi) 684 assert.Equal(input.Family.Safi, output.Family.Safi) 685 assert.Equal(input.NextHops, output.NextHops) 686 assert.Equal(1, len(output.Nlris)) 687 for idx, inputNLRI := range input.Nlris { 688 outputNLRI := output.Nlris[idx] 689 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 690 assert.Equal(inputNLRI.Value, outputNLRI.Value) 691 } 692 } 693 694 func Test_MpReachNLRIAttribute_EVPN_Prefix_Route(t *testing.T) { 695 assert := assert.New(t) 696 697 nlris := make([]*apb.Any, 0, 1) 698 rd, err := apb.New(&api.RouteDistinguisherIPAddress{ 699 Admin: "1.1.1.1", 700 Assigned: 100, 701 }) 702 assert.Nil(err) 703 esi := &api.EthernetSegmentIdentifier{ 704 Type: 0, 705 Value: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}, 706 } 707 a, err := apb.New(&api.EVPNIPPrefixRoute{ 708 Rd: rd, 709 Esi: esi, 710 EthernetTag: 100, 711 IpPrefixLen: 24, 712 IpPrefix: "192.168.101.0", 713 Label: 200, 714 GwAddress: "172.16.101.1", 715 }) 716 assert.Nil(err) 717 nlris = append(nlris, a) 718 719 input := &api.MpReachNLRIAttribute{ 720 Family: &api.Family{ 721 Afi: api.Family_AFI_L2VPN, 722 Safi: api.Family_SAFI_EVPN, 723 }, 724 NextHops: []string{"192.168.1.1"}, 725 Nlris: nlris, 726 } 727 728 a, err = apb.New(input) 729 assert.Nil(err) 730 n, err := UnmarshalAttribute(a) 731 assert.Nil(err) 732 733 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 734 assert.Equal(input.Family.Afi, output.Family.Afi) 735 assert.Equal(input.Family.Safi, output.Family.Safi) 736 assert.Equal(input.NextHops, output.NextHops) 737 assert.Equal(1, len(output.Nlris)) 738 for idx, inputNLRI := range input.Nlris { 739 outputNLRI := output.Nlris[idx] 740 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 741 assert.Equal(inputNLRI.Value, outputNLRI.Value) 742 } 743 } 744 745 func Test_MpReachNLRIAttribute_IPv4_VPN(t *testing.T) { 746 assert := assert.New(t) 747 748 nlris := make([]*apb.Any, 0, 1) 749 rd, err := apb.New(&api.RouteDistinguisherIPAddress{ 750 Admin: "1.1.1.1", 751 Assigned: 100, 752 }) 753 assert.Nil(err) 754 a, err := apb.New(&api.LabeledVPNIPAddressPrefix{ 755 Labels: []uint32{100, 200}, 756 Rd: rd, 757 PrefixLen: 24, 758 Prefix: "192.168.101.0", 759 }) 760 assert.Nil(err) 761 nlris = append(nlris, a) 762 763 input := &api.MpReachNLRIAttribute{ 764 Family: &api.Family{ 765 Afi: api.Family_AFI_IP, 766 Safi: api.Family_SAFI_MPLS_VPN, 767 }, 768 NextHops: []string{"192.168.1.1"}, 769 Nlris: nlris, 770 } 771 772 a, err = apb.New(input) 773 assert.Nil(err) 774 n, err := UnmarshalAttribute(a) 775 assert.Nil(err) 776 777 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 778 assert.Equal(input.Family.Afi, output.Family.Afi) 779 assert.Equal(input.Family.Safi, output.Family.Safi) 780 assert.Equal(input.NextHops, output.NextHops) 781 assert.Equal(1, len(output.Nlris)) 782 for idx, inputNLRI := range input.Nlris { 783 outputNLRI := output.Nlris[idx] 784 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 785 assert.Equal(inputNLRI.Value, outputNLRI.Value) 786 } 787 } 788 789 func Test_MpReachNLRIAttribute_IPv6_VPN(t *testing.T) { 790 assert := assert.New(t) 791 792 nlris := make([]*apb.Any, 0, 1) 793 rd, err := apb.New(&api.RouteDistinguisherIPAddress{ 794 Admin: "1.1.1.1", 795 Assigned: 100, 796 }) 797 assert.Nil(err) 798 a, err := apb.New(&api.LabeledVPNIPAddressPrefix{ 799 Labels: []uint32{100, 200}, 800 Rd: rd, 801 PrefixLen: 64, 802 Prefix: "2001:db8:1::", 803 }) 804 assert.Nil(err) 805 nlris = append(nlris, a) 806 807 input := &api.MpReachNLRIAttribute{ 808 Family: &api.Family{ 809 Afi: api.Family_AFI_IP6, 810 Safi: api.Family_SAFI_MPLS_VPN, 811 }, 812 NextHops: []string{"2001:db8::1"}, 813 Nlris: nlris, 814 } 815 816 a, err = apb.New(input) 817 assert.Nil(err) 818 n, err := UnmarshalAttribute(a) 819 assert.Nil(err) 820 821 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 822 assert.Equal(input.Family.Afi, output.Family.Afi) 823 assert.Equal(input.Family.Safi, output.Family.Safi) 824 assert.Equal(input.NextHops, output.NextHops) 825 assert.Equal(1, len(output.Nlris)) 826 for idx, inputNLRI := range input.Nlris { 827 outputNLRI := output.Nlris[idx] 828 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 829 assert.Equal(inputNLRI.Value, outputNLRI.Value) 830 } 831 } 832 833 func Test_MpReachNLRIAttribute_RTC_UC(t *testing.T) { 834 assert := assert.New(t) 835 836 nlris := make([]*apb.Any, 0, 1) 837 rt, err := apb.New(&api.IPv4AddressSpecificExtended{ 838 IsTransitive: true, 839 SubType: 0x02, // Route Target 840 Address: "1.1.1.1", 841 LocalAdmin: 100, 842 }) 843 assert.Nil(err) 844 a, err := apb.New(&api.RouteTargetMembershipNLRI{ 845 Asn: 65000, 846 Rt: rt, 847 }) 848 assert.Nil(err) 849 nlris = append(nlris, a) 850 851 input := &api.MpReachNLRIAttribute{ 852 Family: &api.Family{ 853 Afi: api.Family_AFI_IP, 854 Safi: api.Family_SAFI_ROUTE_TARGET_CONSTRAINTS, 855 }, 856 NextHops: []string{"192.168.1.1"}, 857 Nlris: nlris, 858 } 859 860 a, err = apb.New(input) 861 assert.Nil(err) 862 n, err := UnmarshalAttribute(a) 863 assert.Nil(err) 864 865 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 866 assert.Equal(input.Family.Afi, output.Family.Afi) 867 assert.Equal(input.Family.Safi, output.Family.Safi) 868 assert.Equal(input.NextHops, output.NextHops) 869 assert.Equal(1, len(output.Nlris)) 870 for idx, inputNLRI := range input.Nlris { 871 outputNLRI := output.Nlris[idx] 872 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 873 assert.Equal(inputNLRI.Value, outputNLRI.Value) 874 } 875 } 876 877 func Test_MpReachNLRIAttribute_FS_IPv4_UC(t *testing.T) { 878 assert := assert.New(t) 879 880 rules := make([]*apb.Any, 0, 3) 881 rule, err := apb.New(&api.FlowSpecIPPrefix{ 882 Type: 1, // Destination Prefix 883 PrefixLen: 24, 884 Prefix: "192.168.101.0", 885 }) 886 assert.Nil(err) 887 rules = append(rules, rule) 888 rule, err = apb.New(&api.FlowSpecIPPrefix{ 889 Type: 2, // Source Prefix 890 PrefixLen: 24, 891 Prefix: "192.168.201.0", 892 }) 893 assert.Nil(err) 894 rules = append(rules, rule) 895 rule, err = apb.New(&api.FlowSpecComponent{ 896 Type: 3, // IP Protocol 897 Items: []*api.FlowSpecComponentItem{ 898 { 899 Op: 0x80 | 0x01, // End, EQ 900 Value: 6, // TCP 901 }, 902 }, 903 }) 904 assert.Nil(err) 905 rules = append(rules, rule) 906 907 nlris := make([]*apb.Any, 0, 1) 908 a, err := apb.New(&api.FlowSpecNLRI{ 909 Rules: rules, 910 }) 911 assert.Nil(err) 912 nlris = append(nlris, a) 913 914 input := &api.MpReachNLRIAttribute{ 915 Family: &api.Family{ 916 Afi: api.Family_AFI_IP, 917 Safi: api.Family_SAFI_FLOW_SPEC_UNICAST, 918 }, 919 // NextHops: // No nexthop required 920 Nlris: nlris, 921 } 922 923 a, err = apb.New(input) 924 assert.Nil(err) 925 n, err := UnmarshalAttribute(a) 926 assert.Nil(err) 927 928 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 929 assert.Equal(input.Family.Afi, output.Family.Afi) 930 assert.Equal(input.Family.Safi, output.Family.Safi) 931 assert.Equal(input.NextHops, output.NextHops) 932 assert.Equal(1, len(output.Nlris)) 933 for idx, inputNLRI := range input.Nlris { 934 outputNLRI := output.Nlris[idx] 935 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 936 assert.Equal(inputNLRI.Value, outputNLRI.Value) 937 } 938 } 939 940 func Test_MpReachNLRIAttribute_FS_IPv4_VPN(t *testing.T) { 941 assert := assert.New(t) 942 943 rd, err := apb.New(&api.RouteDistinguisherIPAddress{ 944 Admin: "1.1.1.1", 945 Assigned: 100, 946 }) 947 assert.Nil(err) 948 949 rules := make([]*apb.Any, 0, 3) 950 rule, err := apb.New(&api.FlowSpecIPPrefix{ 951 Type: 1, // Destination Prefix 952 PrefixLen: 24, 953 Prefix: "192.168.101.0", 954 }) 955 assert.Nil(err) 956 rules = append(rules, rule) 957 rule, err = apb.New(&api.FlowSpecIPPrefix{ 958 Type: 2, // Source Prefix 959 PrefixLen: 24, 960 Prefix: "192.168.201.0", 961 }) 962 assert.Nil(err) 963 rules = append(rules, rule) 964 rule, err = apb.New(&api.FlowSpecComponent{ 965 Type: 3, // IP Protocol 966 Items: []*api.FlowSpecComponentItem{ 967 { 968 Op: 0x80 | 0x01, // End, EQ 969 Value: 6, // TCP 970 }, 971 }, 972 }) 973 assert.Nil(err) 974 rules = append(rules, rule) 975 976 nlris := make([]*apb.Any, 0, 1) 977 a, err := apb.New(&api.VPNFlowSpecNLRI{ 978 Rd: rd, 979 Rules: rules, 980 }) 981 assert.Nil(err) 982 nlris = append(nlris, a) 983 984 input := &api.MpReachNLRIAttribute{ 985 Family: &api.Family{ 986 Afi: api.Family_AFI_IP, 987 Safi: api.Family_SAFI_FLOW_SPEC_VPN, 988 }, 989 // NextHops: // No nexthop required 990 Nlris: nlris, 991 } 992 993 a, err = apb.New(input) 994 assert.Nil(err) 995 n, err := UnmarshalAttribute(a) 996 assert.Nil(err) 997 998 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 999 assert.Equal(input.Family.Afi, output.Family.Afi) 1000 assert.Equal(input.Family.Safi, output.Family.Safi) 1001 assert.Equal(input.NextHops, output.NextHops) 1002 assert.Equal(1, len(output.Nlris)) 1003 for idx, inputNLRI := range input.Nlris { 1004 outputNLRI := output.Nlris[idx] 1005 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 1006 assert.Equal(inputNLRI.Value, outputNLRI.Value) 1007 } 1008 } 1009 1010 func Test_MpReachNLRIAttribute_FS_IPv6_UC(t *testing.T) { 1011 assert := assert.New(t) 1012 1013 rules := make([]*apb.Any, 0, 3) 1014 rule, err := apb.New(&api.FlowSpecIPPrefix{ 1015 Type: 1, // Destination Prefix 1016 PrefixLen: 64, 1017 Prefix: "2001:db8:1::", 1018 }) 1019 assert.Nil(err) 1020 rules = append(rules, rule) 1021 rule, err = apb.New(&api.FlowSpecIPPrefix{ 1022 Type: 2, // Source Prefix 1023 PrefixLen: 64, 1024 Prefix: "2001:db8:2::", 1025 }) 1026 assert.Nil(err) 1027 rules = append(rules, rule) 1028 rule, err = apb.New(&api.FlowSpecComponent{ 1029 Type: 3, // Next Header 1030 Items: []*api.FlowSpecComponentItem{ 1031 { 1032 Op: 0x80 | 0x01, // End, EQ 1033 Value: 6, // TCP 1034 }, 1035 }, 1036 }) 1037 assert.Nil(err) 1038 rules = append(rules, rule) 1039 1040 nlris := make([]*apb.Any, 0, 1) 1041 a, err := apb.New(&api.FlowSpecNLRI{ 1042 Rules: rules, 1043 }) 1044 assert.Nil(err) 1045 nlris = append(nlris, a) 1046 1047 input := &api.MpReachNLRIAttribute{ 1048 Family: &api.Family{ 1049 Afi: api.Family_AFI_IP6, 1050 Safi: api.Family_SAFI_FLOW_SPEC_UNICAST, 1051 }, 1052 // NextHops: // No nexthop required 1053 Nlris: nlris, 1054 } 1055 1056 a, err = apb.New(input) 1057 assert.Nil(err) 1058 n, err := UnmarshalAttribute(a) 1059 assert.Nil(err) 1060 1061 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 1062 assert.Equal(input.Family.Afi, output.Family.Afi) 1063 assert.Equal(input.Family.Safi, output.Family.Safi) 1064 assert.Equal(input.NextHops, output.NextHops) 1065 assert.Equal(1, len(output.Nlris)) 1066 for idx, inputNLRI := range input.Nlris { 1067 outputNLRI := output.Nlris[idx] 1068 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 1069 assert.Equal(inputNLRI.Value, outputNLRI.Value) 1070 } 1071 } 1072 1073 func Test_MpReachNLRIAttribute_FS_IPv6_VPN(t *testing.T) { 1074 assert := assert.New(t) 1075 1076 rd, err := apb.New(&api.RouteDistinguisherIPAddress{ 1077 Admin: "1.1.1.1", 1078 Assigned: 100, 1079 }) 1080 assert.Nil(err) 1081 1082 rules := make([]*apb.Any, 0, 3) 1083 rule, err := apb.New(&api.FlowSpecIPPrefix{ 1084 Type: 1, // Destination Prefix 1085 PrefixLen: 64, 1086 Prefix: "2001:db8:1::", 1087 }) 1088 assert.Nil(err) 1089 rules = append(rules, rule) 1090 rule, err = apb.New(&api.FlowSpecIPPrefix{ 1091 Type: 2, // Source Prefix 1092 PrefixLen: 64, 1093 Prefix: "2001:db8:2::", 1094 }) 1095 assert.Nil(err) 1096 rules = append(rules, rule) 1097 rule, err = apb.New(&api.FlowSpecComponent{ 1098 Type: 3, // Next Header 1099 Items: []*api.FlowSpecComponentItem{ 1100 { 1101 Op: 0x80 | 0x01, // End, EQ 1102 Value: 6, // TCP 1103 }, 1104 }, 1105 }) 1106 assert.Nil(err) 1107 rules = append(rules, rule) 1108 1109 nlris := make([]*apb.Any, 0, 1) 1110 a, err := apb.New(&api.VPNFlowSpecNLRI{ 1111 Rd: rd, 1112 Rules: rules, 1113 }) 1114 assert.Nil(err) 1115 nlris = append(nlris, a) 1116 1117 input := &api.MpReachNLRIAttribute{ 1118 Family: &api.Family{ 1119 Afi: api.Family_AFI_IP6, 1120 Safi: api.Family_SAFI_FLOW_SPEC_VPN, 1121 }, 1122 // NextHops: // No nexthop required 1123 Nlris: nlris, 1124 } 1125 1126 a, err = apb.New(input) 1127 assert.Nil(err) 1128 n, err := UnmarshalAttribute(a) 1129 assert.Nil(err) 1130 1131 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 1132 assert.Equal(input.Family.Afi, output.Family.Afi) 1133 assert.Equal(input.Family.Safi, output.Family.Safi) 1134 assert.Equal(input.NextHops, output.NextHops) 1135 assert.Equal(1, len(output.Nlris)) 1136 for idx, inputNLRI := range input.Nlris { 1137 outputNLRI := output.Nlris[idx] 1138 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 1139 assert.Equal(inputNLRI.Value, outputNLRI.Value) 1140 } 1141 } 1142 1143 func Test_MpReachNLRIAttribute_FS_L2_VPN(t *testing.T) { 1144 assert := assert.New(t) 1145 1146 rd, err := apb.New(&api.RouteDistinguisherIPAddress{ 1147 Admin: "1.1.1.1", 1148 Assigned: 100, 1149 }) 1150 assert.Nil(err) 1151 1152 rules := make([]*apb.Any, 0, 3) 1153 rule, err := apb.New(&api.FlowSpecMAC{ 1154 Type: 15, // Source MAC 1155 Address: "aa:bb:cc:11:22:33", 1156 }) 1157 assert.Nil(err) 1158 rules = append(rules, rule) 1159 rule, err = apb.New(&api.FlowSpecMAC{ 1160 Type: 16, // Destination MAC 1161 Address: "dd:ee:ff:11:22:33", 1162 }) 1163 assert.Nil(err) 1164 rules = append(rules, rule) 1165 rule, err = apb.New(&api.FlowSpecComponent{ 1166 Type: 21, // VLAN ID 1167 Items: []*api.FlowSpecComponentItem{ 1168 { 1169 Op: 0x80 | 0x01, // End, EQ 1170 Value: 100, 1171 }, 1172 }, 1173 }) 1174 assert.Nil(err) 1175 rules = append(rules, rule) 1176 1177 nlris := make([]*apb.Any, 0, 1) 1178 a, err := apb.New(&api.VPNFlowSpecNLRI{ 1179 Rd: rd, 1180 Rules: rules, 1181 }) 1182 assert.Nil(err) 1183 nlris = append(nlris, a) 1184 1185 input := &api.MpReachNLRIAttribute{ 1186 Family: &api.Family{ 1187 Afi: api.Family_AFI_L2VPN, 1188 Safi: api.Family_SAFI_FLOW_SPEC_VPN, 1189 }, 1190 // NextHops: // No nexthop required 1191 Nlris: nlris, 1192 } 1193 1194 a, err = apb.New(input) 1195 assert.Nil(err) 1196 n, err := UnmarshalAttribute(a) 1197 assert.Nil(err) 1198 1199 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 1200 assert.Equal(input.Family.Afi, output.Family.Afi) 1201 assert.Equal(input.Family.Safi, output.Family.Safi) 1202 assert.Equal(input.NextHops, output.NextHops) 1203 assert.Equal(1, len(output.Nlris)) 1204 for idx, inputNLRI := range input.Nlris { 1205 outputNLRI := output.Nlris[idx] 1206 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 1207 assert.Equal(inputNLRI.Value, outputNLRI.Value) 1208 } 1209 } 1210 1211 func Test_MpReachNLRIAttribute_MUPInterworkSegmentDiscoveryRoute(t *testing.T) { 1212 assert := assert.New(t) 1213 1214 nlris := make([]*apb.Any, 0, 1) 1215 rd, err := apb.New(&api.RouteDistinguisherTwoOctetASN{ 1216 Admin: 65000, 1217 Assigned: 100, 1218 }) 1219 assert.Nil(err) 1220 a, err := apb.New(&api.MUPInterworkSegmentDiscoveryRoute{ 1221 Rd: rd, 1222 Prefix: "10.0.0.0/24", 1223 }) 1224 assert.Nil(err) 1225 nlris = append(nlris, a) 1226 1227 input := &api.MpReachNLRIAttribute{ 1228 Family: &api.Family{ 1229 Afi: api.Family_AFI_IP, 1230 Safi: api.Family_SAFI_MUP, 1231 }, 1232 NextHops: []string{"0.0.0.0"}, 1233 Nlris: nlris, 1234 } 1235 1236 a, err = apb.New(input) 1237 assert.Nil(err) 1238 n, err := UnmarshalAttribute(a) 1239 assert.Nil(err) 1240 1241 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 1242 assert.Equal(input.Family.Afi, output.Family.Afi) 1243 assert.Equal(input.Family.Safi, output.Family.Safi) 1244 assert.Equal(input.NextHops, output.NextHops) 1245 assert.Equal(1, len(output.Nlris)) 1246 for idx, inputNLRI := range input.Nlris { 1247 outputNLRI := output.Nlris[idx] 1248 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 1249 assert.Equal(inputNLRI.Value, outputNLRI.Value) 1250 } 1251 } 1252 1253 func Test_MpReachNLRIAttribute_MUPDirectSegmentDiscoveryRoute(t *testing.T) { 1254 assert := assert.New(t) 1255 1256 nlris := make([]*apb.Any, 0, 1) 1257 rd, err := apb.New(&api.RouteDistinguisherTwoOctetASN{ 1258 Admin: 65000, 1259 Assigned: 100, 1260 }) 1261 assert.Nil(err) 1262 a, err := apb.New(&api.MUPDirectSegmentDiscoveryRoute{ 1263 Rd: rd, 1264 Address: "10.0.0.1", 1265 }) 1266 assert.Nil(err) 1267 nlris = append(nlris, a) 1268 1269 input := &api.MpReachNLRIAttribute{ 1270 Family: &api.Family{ 1271 Afi: api.Family_AFI_IP, 1272 Safi: api.Family_SAFI_MUP, 1273 }, 1274 NextHops: []string{"0.0.0.0"}, 1275 Nlris: nlris, 1276 } 1277 1278 a, err = apb.New(input) 1279 assert.Nil(err) 1280 n, err := UnmarshalAttribute(a) 1281 assert.Nil(err) 1282 1283 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 1284 assert.Equal(input.Family.Afi, output.Family.Afi) 1285 assert.Equal(input.Family.Safi, output.Family.Safi) 1286 assert.Equal(input.NextHops, output.NextHops) 1287 assert.Equal(1, len(output.Nlris)) 1288 for idx, inputNLRI := range input.Nlris { 1289 outputNLRI := output.Nlris[idx] 1290 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 1291 assert.Equal(inputNLRI.Value, outputNLRI.Value) 1292 } 1293 } 1294 1295 func Test_MpReachNLRIAttribute_MUPType1SessionTransformedRoute(t *testing.T) { 1296 assert := assert.New(t) 1297 rd, err := apb.New(&api.RouteDistinguisherTwoOctetASN{ 1298 Admin: 65000, 1299 Assigned: 100, 1300 }) 1301 assert.Nil(err) 1302 tests := []struct { 1303 name string 1304 in *api.MUPType1SessionTransformedRoute 1305 }{ 1306 { 1307 name: "IPv4", 1308 in: &api.MUPType1SessionTransformedRoute{ 1309 Rd: rd, 1310 Prefix: "192.168.100.1/32", 1311 Teid: 12345, 1312 Qfi: 9, 1313 EndpointAddressLength: 32, 1314 EndpointAddress: "10.0.0.1", 1315 }, 1316 }, 1317 { 1318 name: "IPv4_with_SourceAddress", 1319 in: &api.MUPType1SessionTransformedRoute{ 1320 Rd: rd, 1321 Prefix: "192.168.100.1/32", 1322 Teid: 12345, 1323 Qfi: 9, 1324 EndpointAddressLength: 32, 1325 EndpointAddress: "10.0.0.1", 1326 SourceAddressLength: 32, 1327 SourceAddress: "10.0.0.2", 1328 }, 1329 }, 1330 } 1331 for _, tt := range tests { 1332 a, err := apb.New(tt.in) 1333 assert.Nil(err) 1334 nlris := []*apb.Any{a} 1335 1336 input := &api.MpReachNLRIAttribute{ 1337 Family: &api.Family{ 1338 Afi: api.Family_AFI_IP, 1339 Safi: api.Family_SAFI_MUP, 1340 }, 1341 NextHops: []string{"0.0.0.0"}, 1342 Nlris: nlris, 1343 } 1344 1345 a, err = apb.New(input) 1346 assert.Nil(err) 1347 n, err := UnmarshalAttribute(a) 1348 assert.Nil(err) 1349 1350 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 1351 assert.Equal(input.Family.Afi, output.Family.Afi) 1352 assert.Equal(input.Family.Safi, output.Family.Safi) 1353 assert.Equal(input.NextHops, output.NextHops) 1354 assert.Equal(1, len(output.Nlris)) 1355 for idx, inputNLRI := range input.Nlris { 1356 outputNLRI := output.Nlris[idx] 1357 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 1358 assert.Equal(inputNLRI.Value, outputNLRI.Value) 1359 } 1360 } 1361 } 1362 1363 func Test_MpReachNLRIAttribute_MUPType2SessionTransformedRoute(t *testing.T) { 1364 assert := assert.New(t) 1365 1366 nlris := make([]*apb.Any, 0, 1) 1367 rd, err := apb.New(&api.RouteDistinguisherTwoOctetASN{ 1368 Admin: 65000, 1369 Assigned: 100, 1370 }) 1371 assert.Nil(err) 1372 a, err := apb.New(&api.MUPType2SessionTransformedRoute{ 1373 Rd: rd, 1374 Teid: 12345, 1375 EndpointAddressLength: 64, 1376 EndpointAddress: "10.0.0.1", 1377 }) 1378 assert.Nil(err) 1379 nlris = append(nlris, a) 1380 1381 input := &api.MpReachNLRIAttribute{ 1382 Family: &api.Family{ 1383 Afi: api.Family_AFI_IP, 1384 Safi: api.Family_SAFI_MUP, 1385 }, 1386 NextHops: []string{"0.0.0.0"}, 1387 Nlris: nlris, 1388 } 1389 1390 a, err = apb.New(input) 1391 assert.Nil(err) 1392 n, err := UnmarshalAttribute(a) 1393 assert.Nil(err) 1394 1395 output, _ := NewMpReachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpReachNLRI)) 1396 assert.Equal(input.Family.Afi, output.Family.Afi) 1397 assert.Equal(input.Family.Safi, output.Family.Safi) 1398 assert.Equal(input.NextHops, output.NextHops) 1399 assert.Equal(1, len(output.Nlris)) 1400 for idx, inputNLRI := range input.Nlris { 1401 outputNLRI := output.Nlris[idx] 1402 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 1403 assert.Equal(inputNLRI.Value, outputNLRI.Value) 1404 } 1405 } 1406 1407 func Test_MpUnreachNLRIAttribute_IPv4_UC(t *testing.T) { 1408 assert := assert.New(t) 1409 1410 nlris := make([]*apb.Any, 0, 2) 1411 a, err := apb.New(&api.IPAddressPrefix{ 1412 PrefixLen: 24, 1413 Prefix: "192.168.101.0", 1414 }) 1415 assert.Nil(err) 1416 nlris = append(nlris, a) 1417 a, err = apb.New(&api.IPAddressPrefix{ 1418 PrefixLen: 24, 1419 Prefix: "192.168.201.0", 1420 }) 1421 assert.Nil(err) 1422 nlris = append(nlris, a) 1423 1424 input := &api.MpUnreachNLRIAttribute{ 1425 Family: &api.Family{ 1426 Afi: api.Family_AFI_IP, 1427 Safi: api.Family_SAFI_UNICAST, 1428 }, 1429 Nlris: nlris, 1430 } 1431 1432 a, err = apb.New(input) 1433 assert.Nil(err) 1434 n, err := UnmarshalAttribute(a) 1435 assert.Nil(err) 1436 1437 output, _ := NewMpUnreachNLRIAttributeFromNative(n.(*bgp.PathAttributeMpUnreachNLRI)) 1438 assert.Equal(input.Family.Afi, output.Family.Afi) 1439 assert.Equal(input.Family.Safi, output.Family.Safi) 1440 assert.Equal(2, len(output.Nlris)) 1441 for idx, inputNLRI := range input.Nlris { 1442 outputNLRI := output.Nlris[idx] 1443 assert.Equal(inputNLRI.TypeUrl, outputNLRI.TypeUrl) 1444 assert.Equal(inputNLRI.Value, outputNLRI.Value) 1445 } 1446 } 1447 1448 func Test_ExtendedCommunitiesAttribute(t *testing.T) { 1449 assert := assert.New(t) 1450 1451 communities := make([]*apb.Any, 0, 19) 1452 a, err := apb.New(&api.TwoOctetAsSpecificExtended{ 1453 IsTransitive: true, 1454 SubType: 0x02, // ROUTE_TARGET 1455 Asn: 65001, 1456 LocalAdmin: 100, 1457 }) 1458 assert.Nil(err) 1459 communities = append(communities, a) 1460 a, err = apb.New(&api.IPv4AddressSpecificExtended{ 1461 IsTransitive: true, 1462 SubType: 0x02, // ROUTE_TARGET 1463 Address: "2.2.2.2", 1464 LocalAdmin: 200, 1465 }) 1466 assert.Nil(err) 1467 communities = append(communities, a) 1468 a, err = apb.New(&api.FourOctetAsSpecificExtended{ 1469 IsTransitive: true, 1470 SubType: 0x02, // ROUTE_TARGET 1471 Asn: 65003, 1472 LocalAdmin: 300, 1473 }) 1474 assert.Nil(err) 1475 communities = append(communities, a) 1476 a, err = apb.New(&api.ValidationExtended{ 1477 State: 0, // VALID 1478 }) 1479 assert.Nil(err) 1480 communities = append(communities, a) 1481 a, err = apb.New(&api.ColorExtended{ 1482 Color: 400, 1483 }) 1484 assert.Nil(err) 1485 communities = append(communities, a) 1486 a, err = apb.New(&api.EncapExtended{ 1487 TunnelType: 8, // VXLAN 1488 }) 1489 assert.Nil(err) 1490 communities = append(communities, a) 1491 a, err = apb.New(&api.DefaultGatewayExtended{ 1492 // No value 1493 }) 1494 assert.Nil(err) 1495 communities = append(communities, a) 1496 a, err = apb.New(&api.OpaqueExtended{ 1497 IsTransitive: true, 1498 Value: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}, 1499 }) 1500 assert.Nil(err) 1501 communities = append(communities, a) 1502 a, err = apb.New(&api.ESILabelExtended{ 1503 IsSingleActive: true, 1504 Label: 500, 1505 }) 1506 assert.Nil(err) 1507 communities = append(communities, a) 1508 a, err = apb.New(&api.ESImportRouteTarget{ 1509 EsImport: "aa:bb:cc:dd:ee:ff", 1510 }) 1511 assert.Nil(err) 1512 communities = append(communities, a) 1513 a, err = apb.New(&api.MacMobilityExtended{ 1514 IsSticky: true, 1515 SequenceNum: 1, 1516 }) 1517 assert.Nil(err) 1518 communities = append(communities, a) 1519 a, err = apb.New(&api.RouterMacExtended{ 1520 Mac: "ff:ee:dd:cc:bb:aa", 1521 }) 1522 assert.Nil(err) 1523 communities = append(communities, a) 1524 a, err = apb.New(&api.TrafficRateExtended{ 1525 Asn: 65004, 1526 Rate: 100.0, 1527 }) 1528 assert.Nil(err) 1529 communities = append(communities, a) 1530 a, err = apb.New(&api.TrafficActionExtended{ 1531 Terminal: true, 1532 Sample: false, 1533 }) 1534 assert.Nil(err) 1535 communities = append(communities, a) 1536 a, err = apb.New(&api.RedirectTwoOctetAsSpecificExtended{ 1537 Asn: 65005, 1538 LocalAdmin: 500, 1539 }) 1540 assert.Nil(err) 1541 communities = append(communities, a) 1542 a, err = apb.New(&api.RedirectIPv4AddressSpecificExtended{ 1543 Address: "6.6.6.6", 1544 LocalAdmin: 600, 1545 }) 1546 assert.Nil(err) 1547 communities = append(communities, a) 1548 a, err = apb.New(&api.RedirectFourOctetAsSpecificExtended{ 1549 Asn: 65007, 1550 LocalAdmin: 700, 1551 }) 1552 assert.Nil(err) 1553 communities = append(communities, a) 1554 a, err = apb.New(&api.TrafficRemarkExtended{ 1555 Dscp: 0x0a, // AF11 1556 }) 1557 assert.Nil(err) 1558 communities = append(communities, a) 1559 a, err = apb.New(&api.MUPExtended{ 1560 SegmentId2: 10, 1561 SegmentId4: 100, 1562 }) 1563 assert.Nil(err) 1564 communities = append(communities, a) 1565 a, err = apb.New(&api.UnknownExtended{ 1566 Type: 0xff, // Max of uint8 1567 Value: []byte{1, 2, 3, 4, 5, 6, 7}, 1568 }) 1569 assert.Nil(err) 1570 communities = append(communities, a) 1571 a, err = apb.New(&api.LinkBandwidthExtended{ 1572 Asn: 65004, 1573 Bandwidth: 125000.0, 1574 }) 1575 assert.Nil(err) 1576 communities = append(communities, a) 1577 a, err = apb.New(&api.VPLSExtended{ 1578 ControlFlags: 0x00, 1579 Mtu: 1500, 1580 }) 1581 assert.Nil(err) 1582 communities = append(communities, a) 1583 1584 input := &api.ExtendedCommunitiesAttribute{ 1585 Communities: communities, 1586 } 1587 1588 a, err = apb.New(input) 1589 assert.Nil(err) 1590 n, err := UnmarshalAttribute(a) 1591 assert.Nil(err) 1592 1593 output, _ := NewExtendedCommunitiesAttributeFromNative(n.(*bgp.PathAttributeExtendedCommunities)) 1594 assert.Equal(22, len(output.Communities)) 1595 for idx, inputCommunity := range input.Communities { 1596 outputCommunity := output.Communities[idx] 1597 assert.Equal(inputCommunity.TypeUrl, outputCommunity.TypeUrl) 1598 assert.Equal(inputCommunity.Value, outputCommunity.Value) 1599 } 1600 } 1601 1602 func Test_As4PathAttribute(t *testing.T) { 1603 assert := assert.New(t) 1604 1605 input := &api.As4PathAttribute{ 1606 Segments: []*api.AsSegment{ 1607 { 1608 Type: 1, // SET 1609 Numbers: []uint32{100, 200}, 1610 }, 1611 { 1612 Type: 2, // SEQ 1613 Numbers: []uint32{300, 400}, 1614 }, 1615 }, 1616 } 1617 1618 a, err := apb.New(input) 1619 assert.Nil(err) 1620 n, err := UnmarshalAttribute(a) 1621 assert.Nil(err) 1622 1623 output, _ := NewAs4PathAttributeFromNative(n.(*bgp.PathAttributeAs4Path)) 1624 assert.Equal(2, len(output.Segments)) 1625 for i := 0; i < 2; i++ { 1626 assert.True(proto.Equal(input.Segments[i], output.Segments[i])) 1627 } 1628 } 1629 1630 func Test_As4AggregatorAttribute(t *testing.T) { 1631 assert := assert.New(t) 1632 1633 input := &api.As4AggregatorAttribute{ 1634 Asn: 65000, 1635 Address: "1.1.1.1", 1636 } 1637 1638 a, err := apb.New(input) 1639 assert.Nil(err) 1640 n, err := UnmarshalAttribute(a) 1641 assert.Nil(err) 1642 1643 output, _ := NewAs4AggregatorAttributeFromNative(n.(*bgp.PathAttributeAs4Aggregator)) 1644 assert.Equal(input.Asn, output.Asn) 1645 assert.Equal(input.Address, output.Address) 1646 } 1647 1648 func Test_PmsiTunnelAttribute(t *testing.T) { 1649 assert := assert.New(t) 1650 1651 input := &api.PmsiTunnelAttribute{ 1652 Flags: 0x01, // IsLeafInfoRequired = true 1653 Type: 6, // INGRESS_REPL 1654 Label: 100, 1655 Id: net.ParseIP("1.1.1.1").To4(), // IngressReplTunnelID with IPv4 1656 } 1657 1658 a, err := apb.New(input) 1659 assert.Nil(err) 1660 n, err := UnmarshalAttribute(a) 1661 assert.Nil(err) 1662 1663 output, _ := NewPmsiTunnelAttributeFromNative(n.(*bgp.PathAttributePmsiTunnel)) 1664 assert.Equal(input.Flags, output.Flags) 1665 assert.Equal(input.Type, output.Type) 1666 assert.Equal(input.Label, output.Label) 1667 assert.Equal(input.Id, output.Id) 1668 } 1669 1670 func Test_TunnelEncapAttribute(t *testing.T) { 1671 assert := assert.New(t) 1672 1673 subTlvs := make([]*apb.Any, 0, 4) 1674 a, err := apb.New(&api.TunnelEncapSubTLVEncapsulation{ 1675 Key: 100, 1676 Cookie: []byte{0x11, 0x22, 0x33, 0x44}, 1677 }) 1678 assert.Nil(err) 1679 subTlvs = append(subTlvs, a) 1680 a, err = apb.New(&api.TunnelEncapSubTLVProtocol{ 1681 Protocol: 200, 1682 }) 1683 assert.Nil(err) 1684 subTlvs = append(subTlvs, a) 1685 a, err = apb.New(&api.TunnelEncapSubTLVColor{ 1686 Color: 300, 1687 }) 1688 assert.Nil(err) 1689 subTlvs = append(subTlvs, a) 1690 a, err = apb.New(&api.TunnelEncapSubTLVUDPDestPort{ 1691 Port: 400, 1692 }) 1693 assert.Nil(err) 1694 subTlvs = append(subTlvs, a) 1695 a, err = apb.New(&api.TunnelEncapSubTLVUnknown{ 1696 Type: 0xff, // Max of uint8 1697 Value: []byte{0x55, 0x66, 0x77, 0x88}, 1698 }) 1699 assert.Nil(err) 1700 subTlvs = append(subTlvs, a) 1701 1702 input := &api.TunnelEncapAttribute{ 1703 Tlvs: []*api.TunnelEncapTLV{ 1704 { 1705 Type: 8, // VXLAN 1706 Tlvs: subTlvs, 1707 }, 1708 }, 1709 } 1710 1711 a, err = apb.New(input) 1712 assert.Nil(err) 1713 n, err := UnmarshalAttribute(a) 1714 assert.Nil(err) 1715 1716 output, _ := NewTunnelEncapAttributeFromNative(n.(*bgp.PathAttributeTunnelEncap)) 1717 assert.Equal(1, len(output.Tlvs)) 1718 assert.Equal(input.Tlvs[0].Type, output.Tlvs[0].Type) 1719 assert.Equal(len(output.Tlvs[0].Tlvs), len(output.Tlvs[0].Tlvs)) 1720 for idx, inputSubTlv := range input.Tlvs[0].Tlvs { 1721 outputSubTlv := output.Tlvs[0].Tlvs[idx] 1722 assert.Equal(inputSubTlv.TypeUrl, outputSubTlv.TypeUrl) 1723 assert.Equal(inputSubTlv.Value, outputSubTlv.Value) 1724 } 1725 } 1726 1727 func Test_IP6ExtendedCommunitiesAttribute(t *testing.T) { 1728 assert := assert.New(t) 1729 1730 communities := make([]*apb.Any, 0, 2) 1731 a, err := apb.New(&api.IPv6AddressSpecificExtended{ 1732 IsTransitive: true, 1733 SubType: 0xff, // Max of uint8 1734 Address: "2001:db8:1::1", 1735 LocalAdmin: 100, 1736 }) 1737 assert.Nil(err) 1738 communities = append(communities, a) 1739 a, err = apb.New(&api.RedirectIPv6AddressSpecificExtended{ 1740 Address: "2001:db8:2::1", 1741 LocalAdmin: 200, 1742 }) 1743 assert.Nil(err) 1744 communities = append(communities, a) 1745 1746 input := &api.IP6ExtendedCommunitiesAttribute{ 1747 Communities: communities, 1748 } 1749 1750 a, err = apb.New(input) 1751 assert.Nil(err) 1752 n, err := UnmarshalAttribute(a) 1753 assert.Nil(err) 1754 1755 output, _ := NewIP6ExtendedCommunitiesAttributeFromNative(n.(*bgp.PathAttributeIP6ExtendedCommunities)) 1756 assert.Equal(2, len(output.Communities)) 1757 for idx, inputCommunity := range input.Communities { 1758 outputCommunity := output.Communities[idx] 1759 assert.Equal(inputCommunity.TypeUrl, outputCommunity.TypeUrl) 1760 assert.Equal(inputCommunity.Value, outputCommunity.Value) 1761 } 1762 } 1763 1764 func Test_AigpAttribute(t *testing.T) { 1765 assert := assert.New(t) 1766 1767 tlvs := make([]*apb.Any, 0, 2) 1768 a, err := apb.New(&api.AigpTLVIGPMetric{ 1769 Metric: 50, 1770 }) 1771 assert.Nil(err) 1772 tlvs = append(tlvs, a) 1773 a, err = apb.New(&api.AigpTLVUnknown{ 1774 Type: 0xff, // Max of uint8 1775 Value: []byte{0x11, 0x22, 0x33, 0x44}, 1776 }) 1777 assert.Nil(err) 1778 tlvs = append(tlvs, a) 1779 1780 input := &api.AigpAttribute{ 1781 Tlvs: tlvs, 1782 } 1783 1784 a, err = apb.New(input) 1785 assert.Nil(err) 1786 n, err := UnmarshalAttribute(a) 1787 assert.Nil(err) 1788 1789 output, _ := NewAigpAttributeFromNative(n.(*bgp.PathAttributeAigp)) 1790 assert.Equal(2, len(output.Tlvs)) 1791 for idx, inputTlv := range input.Tlvs { 1792 outputTlv := output.Tlvs[idx] 1793 assert.Equal(inputTlv.TypeUrl, outputTlv.TypeUrl) 1794 assert.Equal(inputTlv.Value, outputTlv.Value) 1795 } 1796 } 1797 1798 func Test_LargeCommunitiesAttribute(t *testing.T) { 1799 assert := assert.New(t) 1800 1801 input := &api.LargeCommunitiesAttribute{ 1802 Communities: []*api.LargeCommunity{ 1803 { 1804 GlobalAdmin: 65001, 1805 LocalData1: 100, 1806 LocalData2: 200, 1807 }, 1808 { 1809 GlobalAdmin: 65002, 1810 LocalData1: 300, 1811 LocalData2: 400, 1812 }, 1813 }, 1814 } 1815 1816 a, err := apb.New(input) 1817 assert.Nil(err) 1818 n, err := UnmarshalAttribute(a) 1819 assert.Nil(err) 1820 1821 output, _ := NewLargeCommunitiesAttributeFromNative(n.(*bgp.PathAttributeLargeCommunities)) 1822 assert.Equal(2, len(output.Communities)) 1823 for i := 0; i < 2; i++ { 1824 assert.True(proto.Equal(input.Communities[i], output.Communities[i])) 1825 } 1826 } 1827 1828 func Test_UnknownAttribute(t *testing.T) { 1829 assert := assert.New(t) 1830 1831 input := &api.UnknownAttribute{ 1832 Flags: (1 << 6) | (1 << 7), // OPTIONAL and TRANSITIVE 1833 Type: 0xff, 1834 Value: []byte{0x11, 0x22, 0x33, 0x44}, 1835 } 1836 1837 a, err := apb.New(input) 1838 assert.Nil(err) 1839 n, err := UnmarshalAttribute(a) 1840 assert.Nil(err) 1841 1842 output, _ := NewUnknownAttributeFromNative(n.(*bgp.PathAttributeUnknown)) 1843 assert.Equal(input.Flags, output.Flags) 1844 assert.Equal(input.Type, output.Type) 1845 assert.Equal(input.Value, output.Value) 1846 } 1847 1848 func TestFullCyclePrefixSID(t *testing.T) { 1849 tests := []struct { 1850 name string 1851 input []byte 1852 }{ 1853 { 1854 name: "srv6 prefix sid", 1855 input: []byte{0xc0, 0x28, 0x25, 0x05, 0x00, 0x22, 0x00, 0x01, 0x00, 0x1e, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x00, 0x06, 0x28, 0x18, 0x10, 0x00, 0x10, 0x40}, 1856 }, 1857 } 1858 for _, tt := range tests { 1859 t.Run(tt.name, func(t *testing.T) { 1860 attribute := bgp.PathAttributePrefixSID{} 1861 if err := attribute.DecodeFromBytes(tt.input); err != nil { 1862 t.Fatalf("test failed with error: %+v", err) 1863 } 1864 // Converting from Native to API 1865 apiPrefixSID, err := NewPrefixSIDAttributeFromNative(&attribute) 1866 if err != nil { 1867 t.Fatalf("test failed with error: %+v", err) 1868 } 1869 // Converting back from API to Native 1870 recoveredPrefixSID, err := UnmarshalPrefixSID(apiPrefixSID) 1871 if err != nil { 1872 t.Fatalf("test failed with error: %+v", err) 1873 } 1874 if recoveredPrefixSID.Len() != attribute.Len() { 1875 t.Fatalf("recovered attribute length (%d) is not matching original attribute length (%d)", recoveredPrefixSID.Len(), attribute.Len()) 1876 } 1877 recovered, err := recoveredPrefixSID.Serialize() 1878 if err != nil { 1879 t.Fatalf("test failed with error: %+v", err) 1880 } 1881 if !bytes.Equal(tt.input, recovered) { 1882 t.Fatalf("round trip conversion test failed as expected prefix sid attribute %+v does not match actual: %+v", tt.input, recovered) 1883 } 1884 }) 1885 } 1886 } 1887 1888 func TestFullCycleSRv6SIDStructureSubSubTLV(t *testing.T) { 1889 tests := []struct { 1890 name string 1891 input []byte 1892 }{ 1893 { 1894 name: "srv6 prefix sid", 1895 input: []byte{0x01, 0x00, 0x06, 0x28, 0x18, 0x10, 0x00, 0x10, 0x40}, 1896 }, 1897 } 1898 for _, tt := range tests { 1899 t.Run(tt.name, func(t *testing.T) { 1900 sstlv := bgp.SRv6SIDStructureSubSubTLV{} 1901 if err := sstlv.DecodeFromBytes(tt.input); err != nil { 1902 t.Fatalf("test failed with error: %+v", err) 1903 } 1904 // Converting from Native to API 1905 apiPrefixSID, _ := MarshalSRv6SubSubTLVs([]bgp.PrefixSIDTLVInterface{&sstlv}) 1906 // Converting back from API to Native 1907 _, recoveredPrefixSID, err := UnmarshalSubSubTLVs(apiPrefixSID) 1908 if err != nil { 1909 t.Fatalf("test failed with error: %+v", err) 1910 } 1911 recovered, err := recoveredPrefixSID[0].Serialize() 1912 if err != nil { 1913 t.Fatalf("test failed with error: %+v", err) 1914 } 1915 if !bytes.Equal(tt.input, recovered) { 1916 t.Fatalf("round trip conversion test failed as expected prefix sid attribute %+v does not match actual: %+v", tt.input, recovered) 1917 } 1918 }) 1919 } 1920 } 1921 1922 func TestFullCycleSRv6InformationSubTLV(t *testing.T) { 1923 tests := []struct { 1924 name string 1925 input []byte 1926 }{ 1927 { 1928 name: "srv6 prefix sid informationw sub tlv", 1929 input: []byte{0x01, 0x00, 0x1e, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x00, 0x06, 0x28, 0x18, 0x10, 0x00, 0x10, 0x40}, 1930 }, 1931 } 1932 for _, tt := range tests { 1933 t.Run(tt.name, func(t *testing.T) { 1934 stlv := bgp.SRv6InformationSubTLV{} 1935 if err := stlv.DecodeFromBytes(tt.input); err != nil { 1936 t.Fatalf("test failed with error: %+v", err) 1937 } 1938 // Converting from Native to API 1939 apiPrefixSID, _ := MarshalSRv6SubTLVs([]bgp.PrefixSIDTLVInterface{&stlv}) 1940 // Converting back from API to Native 1941 _, recoveredPrefixSID, err := UnmarshalSubTLVs(apiPrefixSID) 1942 if err != nil { 1943 t.Fatalf("test failed with error: %+v", err) 1944 } 1945 recovered, err := recoveredPrefixSID[0].Serialize() 1946 if err != nil { 1947 t.Fatalf("test failed with error: %+v", err) 1948 } 1949 if !bytes.Equal(tt.input, recovered) { 1950 t.Fatalf("round trip conversion test failed as expected prefix sid attribute %+v does not match actual: %+v", tt.input, recovered) 1951 } 1952 }) 1953 } 1954 }