go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2101/ipsec_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  	"encoding/hex"
    19  	"net"
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ip_types"
    25  	vpp_ipsec "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ipsec"
    26  	ifs "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    27  )
    28  
    29  func TestAddIPSecTunnelInterface(t *testing.T) {
    30  	var ipv4Addr [16]byte
    31  
    32  	ctx, ifHandler := ifTestSetup(t)
    33  	defer ctx.TeardownTestCtx()
    34  	ctx.MockVpp.MockReply(&vpp_ipsec.IpsecTunnelIfAddDelReply{
    35  		SwIfIndex: 2,
    36  	})
    37  
    38  	ipSecLink := &ifs.IPSecLink{
    39  		Esn:             true,
    40  		AntiReplay:      true,
    41  		LocalIp:         "10.10.0.1",
    42  		RemoteIp:        "10.10.0.2",
    43  		LocalSpi:        1500,
    44  		RemoteSpi:       2000,
    45  		CryptoAlg:       9,
    46  		LocalCryptoKey:  "4a506a794f574265564551694d653768",
    47  		RemoteCryptoKey: "9a506a794f574265564551694d653456",
    48  		IntegAlg:        4,
    49  		LocalIntegKey:   "3a506a794f574265564551694d653769",
    50  		RemoteIntegKey:  "8a506a794f574265564551694d653457",
    51  		EnableUdpEncap:  true,
    52  	}
    53  	index, err := ifHandler.AddIPSecTunnelInterface(ctx.Context, "if1", ipSecLink)
    54  	Expect(err).To(BeNil())
    55  	Expect(index).To(Equal(uint32(2)))
    56  
    57  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ipsec.IpsecTunnelIfAddDel)
    58  	Expect(ok).To(BeTrue())
    59  	Expect(vppMsg).ToNot(BeNil())
    60  	localCryptoKey, err := hex.DecodeString("4a506a794f574265564551694d653768")
    61  	Expect(err).To(BeNil())
    62  	remoteCryptoKey, err := hex.DecodeString("9a506a794f574265564551694d653456")
    63  	Expect(err).To(BeNil())
    64  	localIntegKey, err := hex.DecodeString("3a506a794f574265564551694d653769")
    65  	Expect(err).To(BeNil())
    66  	remoteIntegKey, err := hex.DecodeString("8a506a794f574265564551694d653457")
    67  	Expect(err).To(BeNil())
    68  
    69  	Expect(vppMsg.Esn).To(Equal(true))
    70  	Expect(vppMsg.IsAdd).To(Equal(true))
    71  	Expect(vppMsg.AntiReplay).To(Equal(true))
    72  	Expect(vppMsg.LocalIP.Af).To(Equal(ip_types.ADDRESS_IP4))
    73  	copy(ipv4Addr[:], net.ParseIP(ipSecLink.LocalIp)[12:])
    74  	Expect(vppMsg.LocalIP.Un).To(BeEquivalentTo(ip_types.AddressUnion{XXX_UnionData: ipv4Addr}))
    75  	Expect(vppMsg.LocalSpi).To(Equal(uint32(1500)))
    76  	Expect(vppMsg.RemoteSpi).To(Equal(uint32(2000)))
    77  	Expect(vppMsg.CryptoAlg).To(Equal(uint8(9)))
    78  	Expect(vppMsg.LocalCryptoKey).To(BeEquivalentTo(localCryptoKey))
    79  	Expect(vppMsg.LocalCryptoKeyLen).To(Equal(uint8(16)))
    80  	Expect(vppMsg.RemoteCryptoKey).To(BeEquivalentTo(remoteCryptoKey))
    81  	Expect(vppMsg.RemoteCryptoKeyLen).To(Equal(uint8(16)))
    82  	Expect(vppMsg.IntegAlg).To(Equal(uint8(4)))
    83  	Expect(vppMsg.LocalIntegKey).To(BeEquivalentTo(localIntegKey))
    84  	Expect(vppMsg.LocalIntegKeyLen).To(Equal(uint8(16)))
    85  	Expect(vppMsg.RemoteIntegKey).To(BeEquivalentTo(remoteIntegKey))
    86  	Expect(vppMsg.RemoteIntegKeyLen).To(Equal(uint8(16)))
    87  	Expect(vppMsg.UDPEncap).To(Equal(true))
    88  }
    89  
    90  func TestAddIPSecTunnelInterfaceError(t *testing.T) {
    91  	ctx, ifHandler := ifTestSetup(t)
    92  	defer ctx.TeardownTestCtx()
    93  	ctx.MockVpp.MockReply(&vpp_ipsec.IpsecTunnelIfAddDelReply{
    94  		SwIfIndex: 2,
    95  		Retval:    9,
    96  	})
    97  
    98  	index, err := ifHandler.AddIPSecTunnelInterface(ctx.Context, "if1", &ifs.IPSecLink{
    99  		Esn:            true,
   100  		LocalIp:        "10.10.0.1",
   101  		LocalCryptoKey: "4a506a794f574265564551694d653768",
   102  	})
   103  	Expect(err).ToNot(BeNil())
   104  	Expect(index).To(Equal(uint32(0)))
   105  }
   106  
   107  func TestDeleteIPSecTunnelInterface(t *testing.T) {
   108  	ctx, ifHandler := ifTestSetup(t)
   109  	defer ctx.TeardownTestCtx()
   110  	ctx.MockVpp.MockReply(&vpp_ipsec.IpsecTunnelIfAddDelReply{
   111  		SwIfIndex: 2,
   112  	})
   113  
   114  	err := ifHandler.DeleteIPSecTunnelInterface(ctx.Context, "if1", 2, &ifs.IPSecLink{
   115  		Esn:             true,
   116  		LocalIp:         "10.10.0.1",
   117  		RemoteIp:        "10.10.0.2",
   118  		LocalCryptoKey:  "4a506a794f574265564551694d653768",
   119  		RemoteCryptoKey: "9a506a794f574265564551694d653456",
   120  	})
   121  
   122  	Expect(err).To(BeNil())
   123  }
   124  
   125  func TestDeleteIPSecTunnelInterfaceError(t *testing.T) {
   126  	ctx, ifHandler := ifTestSetup(t)
   127  	defer ctx.TeardownTestCtx()
   128  	ctx.MockVpp.MockReply(&vpp_ipsec.IpsecTunnelIfAddDelReply{
   129  		SwIfIndex: 2,
   130  		Retval:    9,
   131  	})
   132  
   133  	err := ifHandler.DeleteIPSecTunnelInterface(ctx.Context, "if1", 2, &ifs.IPSecLink{
   134  		Esn:            true,
   135  		LocalIp:        "10.10.0.1",
   136  		LocalCryptoKey: "4a506a794f574265564551694d653768",
   137  	})
   138  	Expect(err).ToNot(BeNil())
   139  }