go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2101/ip_vppcalls_test.go (about) 1 // Copyright (c) 2019 Cisco and/or its affiliates. 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 implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package vpp2101_test 16 17 import ( 18 "net" 19 "testing" 20 21 . "github.com/onsi/gomega" 22 23 vpp_ifs "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/interface" 24 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ip_types" 25 ) 26 27 func TestAddInterfaceIP(t *testing.T) { 28 var ipv4Addr [4]uint8 29 30 ctx, ifHandler := ifTestSetup(t) 31 defer ctx.TeardownTestCtx() 32 33 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceAddDelAddressReply{}) 34 35 _, ipNet, err := net.ParseCIDR("10.0.0.1/24") 36 Expect(err).To(BeNil()) 37 err = ifHandler.AddInterfaceIP(1, ipNet) 38 39 Expect(err).To(BeNil()) 40 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ifs.SwInterfaceAddDelAddress) 41 Expect(ok).To(BeTrue()) 42 Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(1)) 43 Expect(vppMsg.Prefix.Address.Af).To(BeEquivalentTo(ip_types.ADDRESS_IP4)) 44 copy(ipv4Addr[:], ipNet.IP.To4()) 45 Expect(vppMsg.Prefix.Address.Un.GetIP4()).To(BeEquivalentTo(ipv4Addr)) 46 Expect(vppMsg.Prefix.Len).To(BeEquivalentTo(24)) 47 Expect(vppMsg.DelAll).To(BeFalse()) 48 Expect(vppMsg.IsAdd).To(BeTrue()) 49 } 50 51 func TestAddInterfaceIPv6(t *testing.T) { 52 var ipv6Addr [16]uint8 53 54 ctx, ifHandler := ifTestSetup(t) 55 defer ctx.TeardownTestCtx() 56 57 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceAddDelAddressReply{}) 58 59 _, ipNet, err := net.ParseCIDR("2001:db8:0:1:1:1:1:1/128") 60 Expect(err).To(BeNil()) 61 err = ifHandler.AddInterfaceIP(1, ipNet) 62 63 Expect(err).To(BeNil()) 64 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ifs.SwInterfaceAddDelAddress) 65 Expect(ok).To(BeTrue()) 66 Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(1)) 67 Expect(vppMsg.Prefix.Address.Af).To(BeEquivalentTo(ip_types.ADDRESS_IP6)) 68 copy(ipv6Addr[:], ipNet.IP.To16()) 69 Expect(vppMsg.Prefix.Address.Un.GetIP6()).To(BeEquivalentTo(ipv6Addr)) 70 Expect(vppMsg.Prefix.Len).To(BeEquivalentTo(128)) 71 Expect(vppMsg.DelAll).To(BeFalse()) 72 Expect(vppMsg.IsAdd).To(BeTrue()) 73 } 74 75 func TestAddInterfaceInvalidIP(t *testing.T) { 76 ctx, ifHandler := ifTestSetup(t) 77 defer ctx.TeardownTestCtx() 78 79 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceAddDelAddressReply{}) 80 81 err := ifHandler.AddInterfaceIP(1, &net.IPNet{ 82 IP: []byte("invalid-ip"), 83 }) 84 85 Expect(err).ToNot(BeNil()) 86 } 87 88 func TestAddInterfaceIPError(t *testing.T) { 89 ctx, ifHandler := ifTestSetup(t) 90 defer ctx.TeardownTestCtx() 91 92 _, ipNet, err := net.ParseCIDR("10.0.0.1/24") 93 Expect(err).To(BeNil()) 94 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceAddDelAddress{}) 95 96 err = ifHandler.AddInterfaceIP(1, ipNet) 97 98 Expect(err).ToNot(BeNil()) 99 } 100 101 func TestAddInterfaceIPRetval(t *testing.T) { 102 ctx, ifHandler := ifTestSetup(t) 103 defer ctx.TeardownTestCtx() 104 105 _, ipNet, err := net.ParseCIDR("10.0.0.1/24") 106 Expect(err).To(BeNil()) 107 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceAddDelAddressReply{ 108 Retval: 1, 109 }) 110 111 err = ifHandler.AddInterfaceIP(1, ipNet) 112 113 Expect(err).ToNot(BeNil()) 114 } 115 116 func TestDelInterfaceIP(t *testing.T) { 117 var ipv4Addr [4]uint8 118 119 ctx, ifHandler := ifTestSetup(t) 120 defer ctx.TeardownTestCtx() 121 122 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceAddDelAddressReply{}) 123 124 _, ipNet, err := net.ParseCIDR("10.0.0.1/24") 125 Expect(err).To(BeNil()) 126 err = ifHandler.DelInterfaceIP(1, ipNet) 127 128 Expect(err).To(BeNil()) 129 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ifs.SwInterfaceAddDelAddress) 130 Expect(ok).To(BeTrue()) 131 Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(1)) 132 Expect(vppMsg.Prefix.Address.Af).To(BeEquivalentTo(ip_types.ADDRESS_IP4)) 133 copy(ipv4Addr[:], ipNet.IP.To4()) 134 Expect(vppMsg.Prefix.Address.Un.GetIP4()).To(BeEquivalentTo(ipv4Addr)) 135 Expect(vppMsg.Prefix.Len).To(BeEquivalentTo(24)) 136 Expect(vppMsg.DelAll).To(BeFalse()) 137 Expect(vppMsg.IsAdd).To(BeFalse()) 138 } 139 140 func TestDelInterfaceIPv6(t *testing.T) { 141 var ipv6Addr [16]uint8 142 143 ctx, ifHandler := ifTestSetup(t) 144 defer ctx.TeardownTestCtx() 145 146 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceAddDelAddressReply{}) 147 148 _, ipNet, err := net.ParseCIDR("2001:db8:0:1:1:1:1:1/128") 149 Expect(err).To(BeNil()) 150 err = ifHandler.DelInterfaceIP(1, ipNet) 151 152 Expect(err).To(BeNil()) 153 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ifs.SwInterfaceAddDelAddress) 154 Expect(ok).To(BeTrue()) 155 Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(1)) 156 Expect(vppMsg.Prefix.Address.Af).To(BeEquivalentTo(ip_types.ADDRESS_IP6)) 157 copy(ipv6Addr[:], ipNet.IP.To16()) 158 Expect(vppMsg.Prefix.Address.Un.GetIP6()).To(BeEquivalentTo(ipv6Addr)) 159 Expect(vppMsg.Prefix.Len).To(BeEquivalentTo(128)) 160 Expect(vppMsg.DelAll).To(BeFalse()) 161 Expect(vppMsg.IsAdd).To(BeFalse()) 162 } 163 164 func TestDelInterfaceInvalidIP(t *testing.T) { 165 ctx, ifHandler := ifTestSetup(t) 166 defer ctx.TeardownTestCtx() 167 168 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceAddDelAddressReply{}) 169 170 err := ifHandler.DelInterfaceIP(1, &net.IPNet{ 171 IP: []byte("invalid-ip"), 172 }) 173 174 Expect(err).ToNot(BeNil()) 175 } 176 177 func TestDelInterfaceIPError(t *testing.T) { 178 ctx, ifHandler := ifTestSetup(t) 179 defer ctx.TeardownTestCtx() 180 181 _, ipNet, err := net.ParseCIDR("10.0.0.1/24") 182 Expect(err).To(BeNil()) 183 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceAddDelAddress{}) 184 185 err = ifHandler.DelInterfaceIP(1, ipNet) 186 187 Expect(err).ToNot(BeNil()) 188 } 189 190 func TestDelInterfaceIPRetval(t *testing.T) { 191 ctx, ifHandler := ifTestSetup(t) 192 defer ctx.TeardownTestCtx() 193 194 _, ipNet, err := net.ParseCIDR("10.0.0.1/24") 195 Expect(err).To(BeNil()) 196 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceAddDelAddressReply{ 197 Retval: 1, 198 }) 199 200 err = ifHandler.DelInterfaceIP(1, ipNet) 201 202 Expect(err).ToNot(BeNil()) 203 } 204 205 func TestSetUnnumberedIP(t *testing.T) { 206 ctx, ifHandler := ifTestSetup(t) 207 defer ctx.TeardownTestCtx() 208 209 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceSetUnnumberedReply{}) 210 211 err := ifHandler.SetUnnumberedIP(ctx.Context, 1, 2) 212 213 Expect(err).To(BeNil()) 214 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ifs.SwInterfaceSetUnnumbered) 215 Expect(ok).To(BeTrue()) 216 Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(2)) 217 Expect(vppMsg.UnnumberedSwIfIndex).To(BeEquivalentTo(1)) 218 Expect(vppMsg.IsAdd).To(BeTrue()) 219 } 220 221 func TestSetUnnumberedIPError(t *testing.T) { 222 ctx, ifHandler := ifTestSetup(t) 223 defer ctx.TeardownTestCtx() 224 225 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceSetUnnumbered{}) 226 227 err := ifHandler.SetUnnumberedIP(ctx.Context, 1, 2) 228 229 Expect(err).ToNot(BeNil()) 230 } 231 232 func TestSetUnnumberedIPRetval(t *testing.T) { 233 ctx, ifHandler := ifTestSetup(t) 234 defer ctx.TeardownTestCtx() 235 236 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceSetUnnumberedReply{ 237 Retval: 1, 238 }) 239 240 err := ifHandler.SetUnnumberedIP(ctx.Context, 1, 2) 241 242 Expect(err).ToNot(BeNil()) 243 } 244 245 func TestUnsetUnnumberedIP(t *testing.T) { 246 ctx, ifHandler := ifTestSetup(t) 247 defer ctx.TeardownTestCtx() 248 249 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceSetUnnumberedReply{}) 250 251 err := ifHandler.UnsetUnnumberedIP(ctx.Context, 1) 252 253 Expect(err).To(BeNil()) 254 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ifs.SwInterfaceSetUnnumbered) 255 Expect(ok).To(BeTrue()) 256 Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(0)) 257 Expect(vppMsg.UnnumberedSwIfIndex).To(BeEquivalentTo(1)) 258 Expect(vppMsg.IsAdd).To(BeFalse()) 259 } 260 261 func TestUnsetUnnumberedIPError(t *testing.T) { 262 ctx, ifHandler := ifTestSetup(t) 263 defer ctx.TeardownTestCtx() 264 265 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceSetUnnumbered{}) 266 267 err := ifHandler.UnsetUnnumberedIP(ctx.Context, 1) 268 269 Expect(err).ToNot(BeNil()) 270 } 271 272 func TestUnsetUnnumberedIPRetval(t *testing.T) { 273 ctx, ifHandler := ifTestSetup(t) 274 defer ctx.TeardownTestCtx() 275 276 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceSetUnnumberedReply{ 277 Retval: 1, 278 }) 279 280 err := ifHandler.UnsetUnnumberedIP(ctx.Context, 1) 281 282 Expect(err).ToNot(BeNil()) 283 }