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