github.com/cilium/cilium@v1.16.2/pkg/datapath/linux/routing/info_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package linuxrouting
     5  
     6  import (
     7  	"net"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	ipamOption "github.com/cilium/cilium/pkg/ipam/option"
    13  	"github.com/cilium/cilium/pkg/mac"
    14  )
    15  
    16  func TestParse(t *testing.T) {
    17  	setupLinuxRoutingSuite(t)
    18  
    19  	_, fakeCIDR, err := net.ParseCIDR("192.168.0.0/16")
    20  	require.Nil(t, err)
    21  
    22  	fakeMAC, err := mac.ParseMAC("11:22:33:44:55:66")
    23  	require.Nil(t, err)
    24  
    25  	validCIDRs := []net.IPNet{*fakeCIDR}
    26  
    27  	tests := []struct {
    28  		name      string
    29  		gateway   string
    30  		cidrs     []string
    31  		macAddr   string
    32  		masq      bool
    33  		ifaceNum  string
    34  		wantRInfo *RoutingInfo
    35  		wantErr   bool
    36  	}{
    37  		{
    38  			name:      "invalid gateway",
    39  			gateway:   "",
    40  			cidrs:     []string{"192.168.0.0/16"},
    41  			macAddr:   "11:22:33:44:55:66",
    42  			masq:      true,
    43  			wantRInfo: nil,
    44  			wantErr:   true,
    45  		},
    46  		{
    47  			name:      "invalid cidr",
    48  			gateway:   "192.168.1.1",
    49  			cidrs:     []string{"192.168.0.0/16", "192.168.0.0/33"},
    50  			macAddr:   "11:22:33:44:55:66",
    51  			masq:      true,
    52  			wantRInfo: nil,
    53  			wantErr:   true,
    54  		},
    55  		{
    56  			name:      "empty cidr",
    57  			gateway:   "192.168.1.1",
    58  			cidrs:     []string{},
    59  			macAddr:   "11:22:33:44:55:66",
    60  			masq:      true,
    61  			wantRInfo: nil,
    62  			wantErr:   true,
    63  		},
    64  		{
    65  			name:      "nil cidr",
    66  			gateway:   "192.168.1.1",
    67  			cidrs:     nil,
    68  			macAddr:   "11:22:33:44:55:66",
    69  			masq:      true,
    70  			wantRInfo: nil,
    71  			wantErr:   true,
    72  		},
    73  		{
    74  			name:      "invalid mac address",
    75  			gateway:   "192.168.1.1",
    76  			cidrs:     []string{"192.168.0.0/16"},
    77  			macAddr:   "11:22:33:44:55:zz",
    78  			masq:      true,
    79  			wantRInfo: nil,
    80  			wantErr:   true,
    81  		},
    82  		{
    83  			name:      "empty mac address",
    84  			gateway:   "192.168.1.1",
    85  			cidrs:     []string{"192.168.0.0/16"},
    86  			macAddr:   "",
    87  			masq:      true,
    88  			wantRInfo: nil,
    89  			wantErr:   true,
    90  		},
    91  		{
    92  			name:      "invalid interface number",
    93  			gateway:   "192.168.1.1",
    94  			cidrs:     []string{"192.168.0.0/16"},
    95  			macAddr:   "11:22:33:44:55:zz",
    96  			ifaceNum:  "a",
    97  			wantRInfo: nil,
    98  			wantErr:   true,
    99  		},
   100  		{
   101  			name:     "valid IPv4 input",
   102  			gateway:  "192.168.1.1",
   103  			cidrs:    []string{"192.168.0.0/16"},
   104  			macAddr:  "11:22:33:44:55:66",
   105  			ifaceNum: "1",
   106  			wantRInfo: &RoutingInfo{
   107  				IPv4Gateway:     net.ParseIP("192.168.1.1"),
   108  				IPv4CIDRs:       validCIDRs,
   109  				MasterIfMAC:     fakeMAC,
   110  				InterfaceNumber: 1,
   111  				IpamMode:        ipamOption.IPAMENI,
   112  			},
   113  			wantErr: false,
   114  		},
   115  		{
   116  			name:     "disabled masquerade",
   117  			gateway:  "192.168.1.1",
   118  			cidrs:    []string{},
   119  			macAddr:  "11:22:33:44:55:66",
   120  			masq:     false,
   121  			ifaceNum: "0",
   122  			wantRInfo: &RoutingInfo{
   123  				IPv4Gateway: net.ParseIP("192.168.1.1"),
   124  				IPv4CIDRs:   []net.IPNet{},
   125  				MasterIfMAC: fakeMAC,
   126  				IpamMode:    ipamOption.IPAMENI,
   127  			},
   128  			wantErr: false,
   129  		},
   130  		{
   131  			name:      "masquerade lacking cidrs",
   132  			gateway:   "192.168.1.1",
   133  			cidrs:     []string{},
   134  			macAddr:   "11:22:33:44:55:66",
   135  			masq:      true,
   136  			wantRInfo: nil,
   137  			wantErr:   true,
   138  		},
   139  	}
   140  	for _, tt := range tests {
   141  		t.Run(tt.name, func(t *testing.T) {
   142  			rInfo, err := NewRoutingInfo(tt.gateway, tt.cidrs, tt.macAddr, tt.ifaceNum, ipamOption.IPAMENI, tt.masq)
   143  			require.EqualValues(t, tt.wantRInfo, rInfo)
   144  			require.Equal(t, tt.wantErr, err != nil)
   145  		})
   146  	}
   147  }