go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/wireguardplugin/vppcalls/vpp2106/wireguard_vppcalls_test.go (about) 1 // Copyright (c) 2021 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 vpp2106_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/vpp2106/ip_types" 25 vpp_wg "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/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/vpp2106" 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 58 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_wg.WireguardPeerAdd) 59 Expect(ok).To(BeTrue()) 60 61 pubKeyBin,_:= base64.StdEncoding.DecodeString("dIjXzrQfIFf80d0O8Hd2KhcfkKLRncc+8C70OjotIW8=") 62 Expect(vppMsg.Peer.PublicKey).To(BeEquivalentTo(pubKeyBin)) 63 Expect(vppMsg.Peer.SwIfIndex).To(BeEquivalentTo(2)) 64 Expect(vppMsg.Peer.Port).To(BeEquivalentTo(12314)) 65 Expect(vppMsg.Peer.PersistentKeepalive).To(BeEquivalentTo(10)) 66 Expect(vppMsg.Peer.TableID).To(BeEquivalentTo(0)) 67 Expect(vppMsg.Peer.Endpoint.Un.GetIP4()).To(BeEquivalentTo(ip_types.IP4Address{10, 10, 2, 1})) 68 Expect(vppMsg.Peer.Flags).To(BeEquivalentTo(0)) 69 Expect(vppMsg.Peer.AllowedIps).To(BeEquivalentTo([]ip_types.Prefix{ip_types.Prefix{ 70 Address: ip_types.Address{ 71 Af: ip_types.ADDRESS_IP4, 72 Un: ip_types.AddressUnionIP4(ip_types.IP4Address{10, 10, 0, 0}), 73 }, 74 Len: 24, 75 }})) 76 } 77 78 func TestVppRemovePeer(t *testing.T) { 79 ctx, wgHandler, _ := wgTestSetup(t) 80 defer ctx.TeardownTestCtx() 81 82 ctx.MockVpp.MockReply(&vpp_wg.WireguardPeerRemoveReply{}) 83 err := wgHandler.RemovePeer(0) 84 85 Expect(err).ShouldNot(HaveOccurred()) 86 } 87 88 func wgTestSetup(t *testing.T) (*vppmock.TestCtx, vppcalls.WgVppAPI, ifaceidx.IfaceMetadataIndexRW) { 89 ctx := vppmock.SetupTestCtx(t) 90 log := logrus.NewLogger("test-log") 91 ifIndex := ifaceidx.NewIfaceIndex(log, "wg-test-ifidx") 92 wgHandler := vpp2106.NewWgVppHandler(ctx.MockChannel, ifIndex, log) 93 return ctx, wgHandler, ifIndex 94 }