go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/wireguardplugin/vppcalls/vpp2210/wireguard_vppcalls_test.go (about) 1 // Copyright (c) 2022 Doc.ai 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 vpp2210_test 16 17 import ( 18 "encoding/base64" 19 "testing" 20 21 . "github.com/onsi/gomega" 22 23 "go.ligato.io/cn-infra/v2/logging/logrus" 24 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210/ip_types" 25 vpp_wg "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210/wireguard" 26 "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx" 27 "go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock" 28 "go.ligato.io/vpp-agent/v3/plugins/vpp/wireguardplugin/vppcalls" 29 "go.ligato.io/vpp-agent/v3/plugins/vpp/wireguardplugin/vppcalls/vpp2210" 30 wg "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/wireguard" 31 ) 32 33 func TestVppAddPeer(t *testing.T) { 34 ctx, wgHandler, ifIndex := wgTestSetup(t) 35 defer ctx.TeardownTestCtx() 36 37 ifIndex.Put("wg1", &ifaceidx.IfaceMetadata{SwIfIndex: 2}) 38 39 ctx.MockVpp.MockReply(&vpp_wg.WireguardPeerAddReply{ 40 PeerIndex: 0, 41 }) 42 43 peer := &wg.Peer{ 44 PublicKey: "dIjXzrQfIFf80d0O8Hd2KhcfkKLRncc+8C70OjotIW8=", 45 WgIfName: "wg1", 46 Port: 12314, 47 PersistentKeepalive: 10, 48 Endpoint: "10.10.2.1", 49 Flags: 0, 50 AllowedIps: []string{"10.10.0.0/24"}, 51 } 52 53 index, err := wgHandler.AddPeer(peer) 54 Expect(err).To(BeNil()) 55 Expect(index).To(Equal(uint32(0))) 56 57 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_wg.WireguardPeerAdd) 58 Expect(ok).To(BeTrue()) 59 60 pubKeyBin, _ := base64.StdEncoding.DecodeString("dIjXzrQfIFf80d0O8Hd2KhcfkKLRncc+8C70OjotIW8=") 61 Expect(vppMsg.Peer.PublicKey).To(BeEquivalentTo(pubKeyBin)) 62 Expect(vppMsg.Peer.SwIfIndex).To(BeEquivalentTo(2)) 63 Expect(vppMsg.Peer.Port).To(BeEquivalentTo(12314)) 64 Expect(vppMsg.Peer.PersistentKeepalive).To(BeEquivalentTo(10)) 65 Expect(vppMsg.Peer.TableID).To(BeEquivalentTo(0)) 66 Expect(vppMsg.Peer.Endpoint.Un.GetIP4()).To(BeEquivalentTo(ip_types.IP4Address{10, 10, 2, 1})) 67 Expect(vppMsg.Peer.Flags).To(BeEquivalentTo(0)) 68 Expect(vppMsg.Peer.AllowedIps).To(BeEquivalentTo([]ip_types.Prefix{{ 69 Address: ip_types.Address{ 70 Af: ip_types.ADDRESS_IP4, 71 Un: ip_types.AddressUnionIP4(ip_types.IP4Address{10, 10, 0, 0}), 72 }, 73 Len: 24, 74 }})) 75 } 76 77 func TestVppRemovePeer(t *testing.T) { 78 ctx, wgHandler, _ := wgTestSetup(t) 79 defer ctx.TeardownTestCtx() 80 81 ctx.MockVpp.MockReply(&vpp_wg.WireguardPeerRemoveReply{}) 82 err := wgHandler.RemovePeer(0) 83 84 Expect(err).ShouldNot(HaveOccurred()) 85 } 86 87 func wgTestSetup(t *testing.T) (*vppmock.TestCtx, vppcalls.WgVppAPI, ifaceidx.IfaceMetadataIndexRW) { 88 ctx := vppmock.SetupTestCtx(t) 89 log := logrus.NewLogger("test-log") 90 ifIndex := ifaceidx.NewIfaceIndex(log, "wg-test-ifidx") 91 wgHandler := vpp2210.NewWgVppHandler(ctx.MockChannel, ifIndex, log) 92 return ctx, wgHandler, ifIndex 93 }