github.com/osrg/gobgp/v3@v3.30.0/internal/pkg/table/path_test.go (about) 1 // path_test.go 2 package table 3 4 import ( 5 "net" 6 "testing" 7 "time" 8 9 "github.com/osrg/gobgp/v3/pkg/config/oc" 10 "github.com/osrg/gobgp/v3/pkg/packet/bgp" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestPathNewIPv4(t *testing.T) { 16 peerP := PathCreatePeer() 17 pathP := PathCreatePath(peerP) 18 ipv4p := NewPath(pathP[0].GetSource(), pathP[0].GetNlri(), true, pathP[0].GetPathAttrs(), time.Now(), false) 19 assert.NotNil(t, ipv4p) 20 } 21 22 func TestPathNewIPv6(t *testing.T) { 23 peerP := PathCreatePeer() 24 pathP := PathCreatePath(peerP) 25 ipv6p := NewPath(pathP[0].GetSource(), pathP[0].GetNlri(), true, pathP[0].GetPathAttrs(), time.Now(), false) 26 assert.NotNil(t, ipv6p) 27 } 28 29 func TestPathGetNlri(t *testing.T) { 30 nlri := bgp.NewIPAddrPrefix(24, "13.2.3.2") 31 pd := &Path{ 32 info: &originInfo{ 33 nlri: nlri, 34 }, 35 } 36 r_nlri := pd.GetNlri() 37 assert.Equal(t, r_nlri, nlri) 38 } 39 40 func TestPathCreatePath(t *testing.T) { 41 peerP := PathCreatePeer() 42 msg := updateMsgP1() 43 updateMsgP := msg.Body.(*bgp.BGPUpdate) 44 nlriList := updateMsgP.NLRI 45 pathAttributes := updateMsgP.PathAttributes 46 nlri_info := nlriList[0] 47 path := NewPath(peerP[0], nlri_info, false, pathAttributes, time.Now(), false) 48 assert.NotNil(t, path) 49 50 } 51 52 func TestPathGetPrefix(t *testing.T) { 53 peerP := PathCreatePeer() 54 pathP := PathCreatePath(peerP) 55 prefix := "10.10.10.0/24" 56 r_prefix := pathP[0].GetPrefix() 57 assert.Equal(t, r_prefix, prefix) 58 } 59 60 func TestPathGetAttribute(t *testing.T) { 61 peerP := PathCreatePeer() 62 pathP := PathCreatePath(peerP) 63 nh := "192.168.50.1" 64 pa := pathP[0].getPathAttr(bgp.BGP_ATTR_TYPE_NEXT_HOP) 65 r_nh := pa.(*bgp.PathAttributeNextHop).Value.String() 66 assert.Equal(t, r_nh, nh) 67 } 68 69 func TestASPathLen(t *testing.T) { 70 assert := assert.New(t) 71 origin := bgp.NewPathAttributeOrigin(0) 72 aspathParam := []bgp.AsPathParamInterface{ 73 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint16{65001, 65002, 65003, 65004, 65004, 65004, 65004, 65004, 65005}), 74 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, []uint16{65001, 65002, 65003, 65004, 65005}), 75 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65100, 65101, 65102}), 76 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65100, 65101})} 77 aspath := bgp.NewPathAttributeAsPath(aspathParam) 78 nexthop := bgp.NewPathAttributeNextHop("192.168.50.1") 79 med := bgp.NewPathAttributeMultiExitDisc(0) 80 81 pathAttributes := []bgp.PathAttributeInterface{ 82 origin, 83 aspath, 84 nexthop, 85 med, 86 } 87 88 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "10.10.10.0")} 89 bgpmsg := bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri) 90 update := bgpmsg.Body.(*bgp.BGPUpdate) 91 UpdatePathAttrs4ByteAs(logger, update) 92 peer := PathCreatePeer() 93 p := NewPath(peer[0], update.NLRI[0], false, update.PathAttributes, time.Now(), false) 94 assert.Equal(10, p.GetAsPathLen()) 95 } 96 97 func TestPathPrependAsnToExistingSeqAttr(t *testing.T) { 98 assert := assert.New(t) 99 origin := bgp.NewPathAttributeOrigin(0) 100 aspathParam := []bgp.AsPathParamInterface{ 101 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint16{65001, 65002, 65003, 65004, 65005}), 102 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, []uint16{65001, 65002, 65003, 65004, 65005}), 103 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65100, 65101, 65102}), 104 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65100, 65101})} 105 aspath := bgp.NewPathAttributeAsPath(aspathParam) 106 nexthop := bgp.NewPathAttributeNextHop("192.168.50.1") 107 108 pathAttributes := []bgp.PathAttributeInterface{ 109 origin, 110 aspath, 111 nexthop, 112 } 113 114 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "10.10.10.0")} 115 bgpmsg := bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri) 116 update := bgpmsg.Body.(*bgp.BGPUpdate) 117 UpdatePathAttrs4ByteAs(logger, update) 118 peer := PathCreatePeer() 119 p := NewPath(peer[0], update.NLRI[0], false, update.PathAttributes, time.Now(), false) 120 121 p.PrependAsn(65000, 1, false) 122 assert.Equal([]uint32{65000, 65001, 65002, 65003, 65004, 65005, 0, 0, 0}, p.GetAsSeqList()) 123 } 124 125 func TestPathPrependAsnToNewAsPathAttr(t *testing.T) { 126 assert := assert.New(t) 127 origin := bgp.NewPathAttributeOrigin(0) 128 nexthop := bgp.NewPathAttributeNextHop("192.168.50.1") 129 130 pathAttributes := []bgp.PathAttributeInterface{ 131 origin, 132 nexthop, 133 } 134 135 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "10.10.10.0")} 136 bgpmsg := bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri) 137 update := bgpmsg.Body.(*bgp.BGPUpdate) 138 UpdatePathAttrs4ByteAs(logger, update) 139 peer := PathCreatePeer() 140 p := NewPath(peer[0], update.NLRI[0], false, update.PathAttributes, time.Now(), false) 141 142 asn := uint32(65000) 143 p.PrependAsn(asn, 1, false) 144 assert.Equal([]uint32{asn}, p.GetAsSeqList()) 145 } 146 147 func TestPathPrependAsnToNewAsPathSeq(t *testing.T) { 148 assert := assert.New(t) 149 origin := bgp.NewPathAttributeOrigin(0) 150 aspathParam := []bgp.AsPathParamInterface{ 151 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, []uint16{65001, 65002, 65003, 65004, 65005}), 152 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65100, 65101, 65102}), 153 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65100, 65101})} 154 aspath := bgp.NewPathAttributeAsPath(aspathParam) 155 nexthop := bgp.NewPathAttributeNextHop("192.168.50.1") 156 157 pathAttributes := []bgp.PathAttributeInterface{ 158 origin, 159 aspath, 160 nexthop, 161 } 162 163 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "10.10.10.0")} 164 bgpmsg := bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri) 165 update := bgpmsg.Body.(*bgp.BGPUpdate) 166 UpdatePathAttrs4ByteAs(logger, update) 167 peer := PathCreatePeer() 168 p := NewPath(peer[0], update.NLRI[0], false, update.PathAttributes, time.Now(), false) 169 170 asn := uint32(65000) 171 p.PrependAsn(asn, 1, false) 172 assert.Equal([]uint32{asn, 0, 0, 0}, p.GetAsSeqList()) 173 } 174 175 func TestPathPrependAsnToEmptyAsPathAttr(t *testing.T) { 176 assert := assert.New(t) 177 origin := bgp.NewPathAttributeOrigin(0) 178 aspathParam := []bgp.AsPathParamInterface{ 179 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint16{}), 180 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, []uint16{65001, 65002, 65003, 65004, 65005}), 181 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65100, 65101, 65102}), 182 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65100, 65101})} 183 aspath := bgp.NewPathAttributeAsPath(aspathParam) 184 nexthop := bgp.NewPathAttributeNextHop("192.168.50.1") 185 186 pathAttributes := []bgp.PathAttributeInterface{ 187 origin, 188 aspath, 189 nexthop, 190 } 191 192 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "10.10.10.0")} 193 bgpmsg := bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri) 194 update := bgpmsg.Body.(*bgp.BGPUpdate) 195 UpdatePathAttrs4ByteAs(logger, update) 196 peer := PathCreatePeer() 197 p := NewPath(peer[0], update.NLRI[0], false, update.PathAttributes, time.Now(), false) 198 199 asn := uint32(65000) 200 p.PrependAsn(asn, 1, false) 201 assert.Equal([]uint32{asn, 0, 0, 0}, p.GetAsSeqList()) 202 } 203 204 func TestPathPrependAsnToFullPathAttr(t *testing.T) { 205 assert := assert.New(t) 206 origin := bgp.NewPathAttributeOrigin(0) 207 208 asns := make([]uint16, 255) 209 for i := range asns { 210 asns[i] = 65000 + uint16(i) 211 } 212 213 aspathParam := []bgp.AsPathParamInterface{ 214 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, asns), 215 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, []uint16{65001, 65002, 65003, 65004, 65005}), 216 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65100, 65101, 65102}), 217 bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65100, 65101})} 218 aspath := bgp.NewPathAttributeAsPath(aspathParam) 219 nexthop := bgp.NewPathAttributeNextHop("192.168.50.1") 220 221 pathAttributes := []bgp.PathAttributeInterface{ 222 origin, 223 aspath, 224 nexthop, 225 } 226 227 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "10.10.10.0")} 228 bgpmsg := bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri) 229 update := bgpmsg.Body.(*bgp.BGPUpdate) 230 UpdatePathAttrs4ByteAs(logger, update) 231 peer := PathCreatePeer() 232 p := NewPath(peer[0], update.NLRI[0], false, update.PathAttributes, time.Now(), false) 233 234 expected := []uint32{65000, 65000} 235 for _, v := range asns { 236 expected = append(expected, uint32(v)) 237 } 238 p.PrependAsn(65000, 2, false) 239 assert.Equal(append(expected, []uint32{0, 0, 0}...), p.GetAsSeqList()) 240 } 241 242 func TestGetPathAttrs(t *testing.T) { 243 paths := PathCreatePath(PathCreatePeer()) 244 path0 := paths[0] 245 path1 := path0.Clone(false) 246 path1.delPathAttr(bgp.BGP_ATTR_TYPE_NEXT_HOP) 247 path2 := path1.Clone(false) 248 path2.setPathAttr(bgp.NewPathAttributeNextHop("192.168.50.1")) 249 assert.NotNil(t, path2.getPathAttr(bgp.BGP_ATTR_TYPE_NEXT_HOP)) 250 } 251 252 func PathCreatePeer() []*PeerInfo { 253 peerP1 := &PeerInfo{AS: 65000} 254 peerP2 := &PeerInfo{AS: 65001} 255 peerP3 := &PeerInfo{AS: 65002} 256 peerP := []*PeerInfo{peerP1, peerP2, peerP3} 257 return peerP 258 } 259 260 func PathCreatePath(peerP []*PeerInfo) []*Path { 261 bgpMsgP1 := updateMsgP1() 262 bgpMsgP2 := updateMsgP2() 263 bgpMsgP3 := updateMsgP3() 264 pathP := make([]*Path, 3) 265 for i, msg := range []*bgp.BGPMessage{bgpMsgP1, bgpMsgP2, bgpMsgP3} { 266 updateMsgP := msg.Body.(*bgp.BGPUpdate) 267 nlriList := updateMsgP.NLRI 268 pathAttributes := updateMsgP.PathAttributes 269 nlri_info := nlriList[0] 270 pathP[i] = NewPath(peerP[i], nlri_info, false, pathAttributes, time.Now(), false) 271 } 272 return pathP 273 } 274 275 func updateMsgP1() *bgp.BGPMessage { 276 277 origin := bgp.NewPathAttributeOrigin(0) 278 aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65000})} 279 aspath := bgp.NewPathAttributeAsPath(aspathParam) 280 nexthop := bgp.NewPathAttributeNextHop("192.168.50.1") 281 med := bgp.NewPathAttributeMultiExitDisc(0) 282 283 pathAttributes := []bgp.PathAttributeInterface{ 284 origin, 285 aspath, 286 nexthop, 287 med, 288 } 289 290 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "10.10.10.0")} 291 return bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri) 292 } 293 294 func updateMsgP2() *bgp.BGPMessage { 295 296 origin := bgp.NewPathAttributeOrigin(0) 297 aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65100})} 298 aspath := bgp.NewPathAttributeAsPath(aspathParam) 299 nexthop := bgp.NewPathAttributeNextHop("192.168.100.1") 300 med := bgp.NewPathAttributeMultiExitDisc(100) 301 302 pathAttributes := []bgp.PathAttributeInterface{ 303 origin, 304 aspath, 305 nexthop, 306 med, 307 } 308 309 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "20.20.20.0")} 310 return bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri) 311 } 312 313 func updateMsgP3() *bgp.BGPMessage { 314 origin := bgp.NewPathAttributeOrigin(0) 315 aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65100})} 316 aspath := bgp.NewPathAttributeAsPath(aspathParam) 317 nexthop := bgp.NewPathAttributeNextHop("192.168.150.1") 318 med := bgp.NewPathAttributeMultiExitDisc(100) 319 320 pathAttributes := []bgp.PathAttributeInterface{ 321 origin, 322 aspath, 323 nexthop, 324 med, 325 } 326 327 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "30.30.30.0")} 328 w1 := bgp.NewIPAddrPrefix(23, "40.40.40.0") 329 withdrawnRoutes := []*bgp.IPAddrPrefix{w1} 330 return bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) 331 } 332 333 func TestRemovePrivateAS(t *testing.T) { 334 aspathParam := []bgp.AsPathParamInterface{bgp.NewAs4PathParam(2, []uint32{64512, 64513, 1, 2})} 335 aspath := bgp.NewPathAttributeAsPath(aspathParam) 336 nlri := bgp.NewIPAddrPrefix(24, "30.30.30.0") 337 path := NewPath(nil, nlri, false, []bgp.PathAttributeInterface{aspath}, time.Now(), false) 338 path.RemovePrivateAS(10, oc.REMOVE_PRIVATE_AS_OPTION_ALL) 339 list := path.GetAsList() 340 assert.Equal(t, len(list), 2) 341 assert.Equal(t, list[0], uint32(1)) 342 assert.Equal(t, list[1], uint32(2)) 343 344 path = NewPath(nil, nlri, false, []bgp.PathAttributeInterface{aspath}, time.Now(), false) 345 path.RemovePrivateAS(10, oc.REMOVE_PRIVATE_AS_OPTION_REPLACE) 346 list = path.GetAsList() 347 assert.Equal(t, len(list), 4) 348 assert.Equal(t, list[0], uint32(10)) 349 assert.Equal(t, list[1], uint32(10)) 350 assert.Equal(t, list[2], uint32(1)) 351 assert.Equal(t, list[3], uint32(2)) 352 } 353 354 func TestReplaceAS(t *testing.T) { 355 aspathParam := []bgp.AsPathParamInterface{bgp.NewAs4PathParam(2, []uint32{64512, 64513, 1, 2})} 356 aspath := bgp.NewPathAttributeAsPath(aspathParam) 357 nlri := bgp.NewIPAddrPrefix(24, "30.30.30.0") 358 path := NewPath(nil, nlri, false, []bgp.PathAttributeInterface{aspath}, time.Now(), false) 359 path = path.ReplaceAS(10, 1) 360 list := path.GetAsList() 361 assert.Equal(t, len(list), 4) 362 assert.Equal(t, list[0], uint32(64512)) 363 assert.Equal(t, list[1], uint32(64513)) 364 assert.Equal(t, list[2], uint32(10)) 365 assert.Equal(t, list[3], uint32(2)) 366 } 367 368 func TestNLRIToIPNet(t *testing.T) { 369 _, n1, _ := net.ParseCIDR("30.30.30.0/24") 370 ipNet := nlriToIPNet(bgp.NewIPAddrPrefix(24, "30.30.30.0")) 371 assert.Equal(t, n1, ipNet) 372 373 _, n2, _ := net.ParseCIDR("2806:106e:19::/48") 374 ipNet = nlriToIPNet(bgp.NewIPv6AddrPrefix(48, "2806:106e:19::")) 375 assert.Equal(t, n2, ipNet) 376 377 labels := bgp.NewMPLSLabelStack(100, 200) 378 _, n3, _ := net.ParseCIDR("30.30.30.0/24") 379 ipNet = nlriToIPNet(bgp.NewLabeledIPAddrPrefix(24, "30.30.30.0", *labels)) 380 assert.Equal(t, n3, ipNet) 381 382 _, n4, _ := net.ParseCIDR("2806:106e:19::/48") 383 ipNet = nlriToIPNet(bgp.NewLabeledIPv6AddrPrefix(48, "2806:106e:19::", *labels)) 384 assert.Equal(t, n4, ipNet) 385 386 rd, _ := bgp.ParseRouteDistinguisher("100:100") 387 _, n5, _ := net.ParseCIDR("40.40.40.0/24") 388 ipNet = nlriToIPNet(bgp.NewLabeledVPNIPAddrPrefix(24, "40.40.40.0", *labels, rd)) 389 assert.Equal(t, n5, ipNet) 390 391 _, n6, _ := net.ParseCIDR("2001:db8:53::/64") 392 ipNet = nlriToIPNet(bgp.NewLabeledVPNIPv6AddrPrefix(64, "2001:db8:53::", *labels, rd)) 393 assert.Equal(t, n6, ipNet) 394 }