github.com/osrg/gobgp@v2.0.0+incompatible/internal/pkg/table/table_test.go (about) 1 // Copyright (C) 2014 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 table 17 18 import ( 19 "testing" 20 "time" 21 22 "github.com/osrg/gobgp/pkg/packet/bgp" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestTableDeleteDestByNlri(t *testing.T) { 28 peerT := TableCreatePeer() 29 pathT := TableCreatePath(peerT) 30 ipv4t := NewTable(bgp.RF_IPv4_UC) 31 for _, path := range pathT { 32 dest := NewDestination(path.GetNlri(), 0) 33 ipv4t.setDestination(dest) 34 } 35 gdest := ipv4t.GetDestination(pathT[0].GetNlri()) 36 rdest := ipv4t.deleteDestByNlri(pathT[0].GetNlri()) 37 assert.Equal(t, rdest, gdest) 38 } 39 40 func TestTableDeleteDest(t *testing.T) { 41 peerT := TableCreatePeer() 42 pathT := TableCreatePath(peerT) 43 ipv4t := NewTable(bgp.RF_IPv4_UC) 44 for _, path := range pathT { 45 dest := NewDestination(path.GetNlri(), 0) 46 ipv4t.setDestination(dest) 47 } 48 dest := NewDestination(pathT[0].GetNlri(), 0) 49 ipv4t.setDestination(dest) 50 ipv4t.deleteDest(dest) 51 gdest := ipv4t.GetDestination(pathT[0].GetNlri()) 52 assert.Nil(t, gdest) 53 } 54 55 func TestTableGetRouteFamily(t *testing.T) { 56 ipv4t := NewTable(bgp.RF_IPv4_UC) 57 rf := ipv4t.GetRoutefamily() 58 assert.Equal(t, rf, bgp.RF_IPv4_UC) 59 } 60 61 func TestTableSetDestinations(t *testing.T) { 62 peerT := TableCreatePeer() 63 pathT := TableCreatePath(peerT) 64 ipv4t := NewTable(bgp.RF_IPv4_UC) 65 destinations := make(map[string]*Destination) 66 for _, path := range pathT { 67 tableKey := ipv4t.tableKey(path.GetNlri()) 68 dest := NewDestination(path.GetNlri(), 0) 69 destinations[tableKey] = dest 70 } 71 ipv4t.setDestinations(destinations) 72 ds := ipv4t.GetDestinations() 73 assert.Equal(t, ds, destinations) 74 } 75 func TestTableGetDestinations(t *testing.T) { 76 peerT := DestCreatePeer() 77 pathT := DestCreatePath(peerT) 78 ipv4t := NewTable(bgp.RF_IPv4_UC) 79 destinations := make(map[string]*Destination) 80 for _, path := range pathT { 81 tableKey := ipv4t.tableKey(path.GetNlri()) 82 dest := NewDestination(path.GetNlri(), 0) 83 destinations[tableKey] = dest 84 } 85 ipv4t.setDestinations(destinations) 86 ds := ipv4t.GetDestinations() 87 assert.Equal(t, ds, destinations) 88 } 89 90 func TestTableKey(t *testing.T) { 91 tb := NewTable(bgp.RF_IPv4_UC) 92 n1, _ := bgp.NewPrefixFromRouteFamily(bgp.AFI_IP, bgp.SAFI_UNICAST, "0.0.0.0/0") 93 d1 := NewDestination(n1, 0) 94 n2, _ := bgp.NewPrefixFromRouteFamily(bgp.AFI_IP, bgp.SAFI_UNICAST, "0.0.0.0/1") 95 d2 := NewDestination(n2, 0) 96 assert.Equal(t, len(tb.tableKey(d1.GetNlri())), 5) 97 tb.setDestination(d1) 98 tb.setDestination(d2) 99 assert.Equal(t, len(tb.GetDestinations()), 2) 100 } 101 102 func TableCreatePeer() []*PeerInfo { 103 peerT1 := &PeerInfo{AS: 65000} 104 peerT2 := &PeerInfo{AS: 65001} 105 peerT3 := &PeerInfo{AS: 65002} 106 peerT := []*PeerInfo{peerT1, peerT2, peerT3} 107 return peerT 108 } 109 110 func TableCreatePath(peerT []*PeerInfo) []*Path { 111 bgpMsgT1 := updateMsgT1() 112 bgpMsgT2 := updateMsgT2() 113 bgpMsgT3 := updateMsgT3() 114 pathT := make([]*Path, 3) 115 for i, msg := range []*bgp.BGPMessage{bgpMsgT1, bgpMsgT2, bgpMsgT3} { 116 updateMsgT := msg.Body.(*bgp.BGPUpdate) 117 nlriList := updateMsgT.NLRI 118 pathAttributes := updateMsgT.PathAttributes 119 nlri_info := nlriList[0] 120 pathT[i] = NewPath(peerT[i], nlri_info, false, pathAttributes, time.Now(), false) 121 } 122 return pathT 123 } 124 125 func updateMsgT1() *bgp.BGPMessage { 126 127 origin := bgp.NewPathAttributeOrigin(0) 128 aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65000})} 129 aspath := bgp.NewPathAttributeAsPath(aspathParam) 130 nexthop := bgp.NewPathAttributeNextHop("192.168.50.1") 131 med := bgp.NewPathAttributeMultiExitDisc(0) 132 133 pathAttributes := []bgp.PathAttributeInterface{ 134 origin, 135 aspath, 136 nexthop, 137 med, 138 } 139 140 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "10.10.10.0")} 141 return bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri) 142 } 143 144 func updateMsgT2() *bgp.BGPMessage { 145 146 origin := bgp.NewPathAttributeOrigin(0) 147 aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65100})} 148 aspath := bgp.NewPathAttributeAsPath(aspathParam) 149 nexthop := bgp.NewPathAttributeNextHop("192.168.100.1") 150 med := bgp.NewPathAttributeMultiExitDisc(100) 151 152 pathAttributes := []bgp.PathAttributeInterface{ 153 origin, 154 aspath, 155 nexthop, 156 med, 157 } 158 159 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "20.20.20.0")} 160 return bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri) 161 } 162 func updateMsgT3() *bgp.BGPMessage { 163 origin := bgp.NewPathAttributeOrigin(0) 164 aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65100})} 165 aspath := bgp.NewPathAttributeAsPath(aspathParam) 166 nexthop := bgp.NewPathAttributeNextHop("192.168.150.1") 167 med := bgp.NewPathAttributeMultiExitDisc(100) 168 169 pathAttributes := []bgp.PathAttributeInterface{ 170 origin, 171 aspath, 172 nexthop, 173 med, 174 } 175 176 nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "30.30.30.0")} 177 w1 := bgp.NewIPAddrPrefix(23, "40.40.40.0") 178 withdrawnRoutes := []*bgp.IPAddrPrefix{w1} 179 return bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) 180 }