github.com/osrg/gobgp/v3@v3.30.0/pkg/apiutil/capability_test.go (about) 1 // Copyright (C) 2018 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 "testing" 20 21 "google.golang.org/protobuf/proto" 22 apb "google.golang.org/protobuf/types/known/anypb" 23 24 api "github.com/osrg/gobgp/v3/api" 25 "github.com/osrg/gobgp/v3/pkg/packet/bgp" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func Test_MultiProtocolCapability(t *testing.T) { 30 assert := assert.New(t) 31 32 input := &api.MultiProtocolCapability{ 33 Family: &api.Family{ 34 Afi: api.Family_AFI_IP, 35 Safi: api.Family_SAFI_UNICAST, 36 }, 37 } 38 39 a, err := apb.New(input) 40 assert.Nil(err) 41 n, err := unmarshalCapability(a) 42 assert.Nil(err) 43 c := n.(*bgp.CapMultiProtocol) 44 assert.Equal(bgp.RF_IPv4_UC, c.CapValue) 45 46 output := NewMultiProtocolCapability(c) 47 assert.True(proto.Equal(input, output)) 48 } 49 50 func Test_RouteRefreshCapability(t *testing.T) { 51 assert := assert.New(t) 52 53 input := &api.RouteRefreshCapability{} 54 55 a, err := apb.New(input) 56 assert.Nil(err) 57 n, err := unmarshalCapability(a) 58 assert.Nil(err) 59 60 output := NewRouteRefreshCapability(n.(*bgp.CapRouteRefresh)) 61 assert.True(proto.Equal(input, output)) 62 } 63 64 func Test_CarryingLabelInfoCapability(t *testing.T) { 65 assert := assert.New(t) 66 67 input := &api.CarryingLabelInfoCapability{} 68 69 a, err := apb.New(input) 70 assert.Nil(err) 71 n, err := unmarshalCapability(a) 72 assert.Nil(err) 73 74 output := NewCarryingLabelInfoCapability(n.(*bgp.CapCarryingLabelInfo)) 75 assert.True(proto.Equal(input, output)) 76 } 77 78 func Test_ExtendedNexthopCapability(t *testing.T) { 79 assert := assert.New(t) 80 81 input := &api.ExtendedNexthopCapability{ 82 Tuples: []*api.ExtendedNexthopCapabilityTuple{ 83 { 84 NlriFamily: &api.Family{ 85 Afi: api.Family_AFI_IP, 86 Safi: api.Family_SAFI_UNICAST, 87 }, 88 NexthopFamily: &api.Family{ 89 Afi: api.Family_AFI_IP6, 90 Safi: api.Family_SAFI_UNICAST, 91 }, 92 }, 93 }, 94 } 95 96 a, err := apb.New(input) 97 assert.Nil(err) 98 n, err := unmarshalCapability(a) 99 assert.Nil(err) 100 c := n.(*bgp.CapExtendedNexthop) 101 assert.Equal(1, len(c.Tuples)) 102 assert.Equal(uint16(bgp.AFI_IP), c.Tuples[0].NLRIAFI) 103 assert.Equal(uint16(bgp.SAFI_UNICAST), c.Tuples[0].NLRISAFI) 104 assert.Equal(uint16(bgp.AFI_IP6), c.Tuples[0].NexthopAFI) 105 106 output := NewExtendedNexthopCapability(c) 107 assert.True(proto.Equal(input, output)) 108 } 109 110 func Test_GracefulRestartCapability(t *testing.T) { 111 assert := assert.New(t) 112 113 input := &api.GracefulRestartCapability{ 114 Flags: 0x08 | 0x04, // restarting|notification 115 Time: 90, 116 Tuples: []*api.GracefulRestartCapabilityTuple{ 117 { 118 Family: &api.Family{ 119 Afi: api.Family_AFI_IP, 120 Safi: api.Family_SAFI_UNICAST, 121 }, 122 Flags: 0x80, // forward 123 }, 124 }, 125 } 126 127 a, err := apb.New(input) 128 assert.Nil(err) 129 n, err := unmarshalCapability(a) 130 assert.Nil(err) 131 132 c := n.(*bgp.CapGracefulRestart) 133 assert.Equal(1, len(c.Tuples)) 134 assert.Equal(uint8(0x08|0x04), c.Flags) 135 assert.Equal(uint16(90), c.Time) 136 assert.Equal(uint16(bgp.AFI_IP), c.Tuples[0].AFI) 137 assert.Equal(uint8(bgp.SAFI_UNICAST), c.Tuples[0].SAFI) 138 assert.Equal(uint8(0x80), c.Tuples[0].Flags) 139 140 output := NewGracefulRestartCapability(c) 141 assert.True(proto.Equal(input, output)) 142 } 143 144 func Test_FourOctetASNumberCapability(t *testing.T) { 145 assert := assert.New(t) 146 147 input := &api.FourOctetASNCapability{ 148 Asn: 100, 149 } 150 151 a, err := apb.New(input) 152 assert.Nil(err) 153 n, err := unmarshalCapability(a) 154 assert.Nil(err) 155 156 c := n.(*bgp.CapFourOctetASNumber) 157 assert.Equal(uint32(100), c.CapValue) 158 159 output := NewFourOctetASNumberCapability(c) 160 assert.True(proto.Equal(input, output)) 161 } 162 163 func Test_AddPathCapability(t *testing.T) { 164 assert := assert.New(t) 165 166 input := &api.AddPathCapability{ 167 Tuples: []*api.AddPathCapabilityTuple{ 168 { 169 Family: &api.Family{ 170 Afi: api.Family_AFI_IP, 171 Safi: api.Family_SAFI_UNICAST, 172 }, 173 Mode: api.AddPathCapabilityTuple_BOTH, 174 }, 175 }, 176 } 177 178 a, err := apb.New(input) 179 assert.Nil(err) 180 n, err := unmarshalCapability(a) 181 assert.Nil(err) 182 183 c := n.(*bgp.CapAddPath) 184 assert.Equal(1, len(c.Tuples)) 185 assert.Equal(bgp.RF_IPv4_UC, c.Tuples[0].RouteFamily) 186 assert.Equal(bgp.BGP_ADD_PATH_BOTH, c.Tuples[0].Mode) 187 188 output := NewAddPathCapability(c) 189 assert.True(proto.Equal(input, output)) 190 } 191 192 func Test_EnhancedRouteRefreshCapability(t *testing.T) { 193 assert := assert.New(t) 194 195 input := &api.EnhancedRouteRefreshCapability{} 196 197 a, err := apb.New(input) 198 assert.Nil(err) 199 n, err := unmarshalCapability(a) 200 assert.Nil(err) 201 202 output := NewEnhancedRouteRefreshCapability(n.(*bgp.CapEnhancedRouteRefresh)) 203 assert.True(proto.Equal(input, output)) 204 } 205 206 func Test_LongLivedGracefulRestartCapability(t *testing.T) { 207 assert := assert.New(t) 208 209 input := &api.LongLivedGracefulRestartCapability{ 210 Tuples: []*api.LongLivedGracefulRestartCapabilityTuple{ 211 { 212 Family: &api.Family{ 213 Afi: api.Family_AFI_IP, 214 Safi: api.Family_SAFI_UNICAST, 215 }, 216 Flags: 0x80, // forward 217 Time: 90, 218 }, 219 }, 220 } 221 222 a, err := apb.New(input) 223 assert.Nil(err) 224 n, err := unmarshalCapability(a) 225 assert.Nil(err) 226 227 c := n.(*bgp.CapLongLivedGracefulRestart) 228 assert.Equal(1, len(c.Tuples)) 229 assert.Equal(uint16(bgp.AFI_IP), c.Tuples[0].AFI) 230 assert.Equal(uint8(bgp.SAFI_UNICAST), c.Tuples[0].SAFI) 231 assert.Equal(uint8(0x80), c.Tuples[0].Flags) 232 assert.Equal(uint32(90), c.Tuples[0].RestartTime) 233 234 output := NewLongLivedGracefulRestartCapability(c) 235 assert.True(proto.Equal(input, output)) 236 } 237 238 func Test_RouteRefreshCiscoCapability(t *testing.T) { 239 assert := assert.New(t) 240 241 input := &api.RouteRefreshCiscoCapability{} 242 243 a, err := apb.New(input) 244 assert.Nil(err) 245 n, err := unmarshalCapability(a) 246 assert.Nil(err) 247 248 output := NewRouteRefreshCiscoCapability(n.(*bgp.CapRouteRefreshCisco)) 249 assert.True(proto.Equal(input, output)) 250 } 251 252 func Test_UnknownCapability(t *testing.T) { 253 assert := assert.New(t) 254 255 input := &api.UnknownCapability{ 256 Code: 0xff, 257 Value: []byte{0x11, 0x22, 0x33, 0x44}, 258 } 259 260 a, err := apb.New(input) 261 assert.Nil(err) 262 n, err := unmarshalCapability(a) 263 assert.Nil(err) 264 265 c := n.(*bgp.CapUnknown) 266 assert.Equal(bgp.BGPCapabilityCode(0xff), c.CapCode) 267 assert.Equal([]byte{0x11, 0x22, 0x33, 0x44}, c.CapValue) 268 269 output := NewUnknownCapability(c) 270 assert.True(proto.Equal(input, output)) 271 }