go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2202/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 vpp2202_test 16 17 import ( 18 "encoding/base64" 19 "testing" 20 21 . "github.com/onsi/gomega" 22 23 vpp_ifs "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/interface" 24 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/ip_types" 25 vpp_wg "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/wireguard" 26 27 ifs "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces" 28 ) 29 30 func TestAddWgTunnelInterface(t *testing.T) { 31 ctx, ifHandler := ifTestSetup(t) 32 defer ctx.TeardownTestCtx() 33 34 ctx.MockVpp.MockReply(&vpp_wg.WireguardInterfaceCreateReply{ 35 SwIfIndex: 2, 36 }) 37 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{}) 38 39 wgLink := &ifs.WireguardLink{ 40 PrivateKey: "gIjXzrQfIFf80d0O8Hd2KhcfkKLRncc+8C70OjotIW8=", 41 Port: 12312, 42 SrcAddr: "10.0.0.1", 43 } 44 45 index, err := ifHandler.AddWireguardTunnel("wg1", wgLink) 46 Expect(err).To(BeNil()) 47 Expect(index).To(Equal(uint32(2))) 48 var msgCheck bool 49 for _, msg := range ctx.MockChannel.Msgs { 50 vppMsg, ok := msg.(*vpp_wg.WireguardInterfaceCreate) 51 if ok { 52 privKeyBin, _ := base64.StdEncoding.DecodeString("gIjXzrQfIFf80d0O8Hd2KhcfkKLRncc+8C70OjotIW8=") 53 Expect(vppMsg.GenerateKey).To(BeEquivalentTo(false)) 54 Expect(vppMsg.Interface.PrivateKey).To(BeEquivalentTo(privKeyBin)) 55 Expect(vppMsg.Interface.Port).To(BeEquivalentTo(12312)) 56 Expect(vppMsg.Interface.SrcIP.Un.GetIP4()).To(BeEquivalentTo(ip_types.IP4Address{10, 0, 0, 1})) 57 msgCheck = true 58 } 59 } 60 Expect(msgCheck).To(BeTrue()) 61 } 62 63 func TestAddWgTunnelInterfaceWithGenKey(t *testing.T) { 64 ctx, ifHandler := ifTestSetup(t) 65 defer ctx.TeardownTestCtx() 66 67 ctx.MockVpp.MockReply(&vpp_wg.WireguardInterfaceCreateReply{ 68 SwIfIndex: 2, 69 }) 70 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{}) 71 72 wgLink := &ifs.WireguardLink{ 73 Port: 12312, 74 SrcAddr: "10.0.0.1", 75 } 76 77 index, err := ifHandler.AddWireguardTunnel("wg1", wgLink) 78 Expect(err).To(BeNil()) 79 Expect(index).To(Equal(uint32(2))) 80 var msgCheck bool 81 for _, msg := range ctx.MockChannel.Msgs { 82 vppMsg, ok := msg.(*vpp_wg.WireguardInterfaceCreate) 83 if ok { 84 Expect(vppMsg.GenerateKey).To(BeEquivalentTo(true)) 85 Expect(vppMsg.Interface.Port).To(BeEquivalentTo(12312)) 86 Expect(vppMsg.Interface.SrcIP.Un.GetIP4()).To(BeEquivalentTo(ip_types.IP4Address{10, 0, 0, 1})) 87 msgCheck = true 88 } 89 } 90 Expect(msgCheck).To(BeTrue()) 91 } 92 93 func TestDeleteWgTunnelInterface(t *testing.T) { 94 ctx, ifHandler := ifTestSetup(t) 95 defer ctx.TeardownTestCtx() 96 ctx.MockVpp.MockReply(&vpp_wg.WireguardInterfaceDeleteReply{}) 97 ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{}) 98 99 err := ifHandler.DeleteWireguardTunnel("wg1", 2) 100 101 Expect(err).To(BeNil()) 102 }