k8s.io/kubernetes@v1.29.3/pkg/proxy/util/iptables/traffic_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package iptables
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestNoOpLocalDetector(t *testing.T) {
    25  	localDetector := NewNoOpLocalDetector()
    26  	if localDetector.IsImplemented() {
    27  		t.Error("NoOpLocalDetector returns true for IsImplemented")
    28  	}
    29  
    30  	ifLocal := localDetector.IfLocal()
    31  	if len(ifLocal) != 0 {
    32  		t.Errorf("NoOpLocalDetector returns %v for IsLocal (expected nil)", ifLocal)
    33  	}
    34  
    35  	ifNotLocal := localDetector.IfNotLocal()
    36  	if len(ifNotLocal) != 0 {
    37  		t.Errorf("NoOpLocalDetector returns %v for IsNotLocal (expected nil)", ifNotLocal)
    38  	}
    39  }
    40  
    41  func TestNewDetectLocalByCIDR(t *testing.T) {
    42  	cases := []struct {
    43  		cidr        string
    44  		errExpected bool
    45  	}{
    46  		{
    47  			cidr:        "10.0.0.0/14",
    48  			errExpected: false,
    49  		},
    50  		{
    51  			cidr:        "2002::1234:abcd:ffff:c0a8:101/64",
    52  			errExpected: false,
    53  		},
    54  		{
    55  			cidr:        "10.0.0.0",
    56  			errExpected: true,
    57  		},
    58  		{
    59  			cidr:        "2002::1234:abcd:ffff:c0a8:101",
    60  			errExpected: true,
    61  		},
    62  		{
    63  			cidr:        "",
    64  			errExpected: true,
    65  		},
    66  		{
    67  			cidr:        "",
    68  			errExpected: true,
    69  		},
    70  	}
    71  	for i, c := range cases {
    72  		r, err := NewDetectLocalByCIDR(c.cidr)
    73  		if c.errExpected {
    74  			if err == nil {
    75  				t.Errorf("Case[%d] expected error, but succeeded with: %q", i, r)
    76  			}
    77  			continue
    78  		}
    79  		if err != nil {
    80  			t.Errorf("Case[%d] failed with error: %v", i, err)
    81  		}
    82  	}
    83  }
    84  
    85  func TestDetectLocalByCIDR(t *testing.T) {
    86  	cases := []struct {
    87  		cidr                     string
    88  		expectedIfLocalOutput    []string
    89  		expectedIfNotLocalOutput []string
    90  	}{
    91  		{
    92  			cidr:                     "10.0.0.0/14",
    93  			expectedIfLocalOutput:    []string{"-s", "10.0.0.0/14"},
    94  			expectedIfNotLocalOutput: []string{"!", "-s", "10.0.0.0/14"},
    95  		},
    96  		{
    97  			cidr:                     "2002::1234:abcd:ffff:c0a8:101/64",
    98  			expectedIfLocalOutput:    []string{"-s", "2002::1234:abcd:ffff:c0a8:101/64"},
    99  			expectedIfNotLocalOutput: []string{"!", "-s", "2002::1234:abcd:ffff:c0a8:101/64"},
   100  		},
   101  	}
   102  	for _, c := range cases {
   103  		localDetector, err := NewDetectLocalByCIDR(c.cidr)
   104  		if err != nil {
   105  			t.Errorf("Error initializing localDetector: %v", err)
   106  			continue
   107  		}
   108  		if !localDetector.IsImplemented() {
   109  			t.Error("DetectLocalByCIDR returns false for IsImplemented")
   110  		}
   111  
   112  		ifLocal := localDetector.IfLocal()
   113  		ifNotLocal := localDetector.IfNotLocal()
   114  
   115  		if !reflect.DeepEqual(ifLocal, c.expectedIfLocalOutput) {
   116  			t.Errorf("IfLocal, expected: '%v', but got: '%v'", c.expectedIfLocalOutput, ifLocal)
   117  		}
   118  
   119  		if !reflect.DeepEqual(ifNotLocal, c.expectedIfNotLocalOutput) {
   120  			t.Errorf("IfNotLocal, expected: '%v', but got: '%v'", c.expectedIfNotLocalOutput, ifNotLocal)
   121  		}
   122  	}
   123  }
   124  
   125  func TestNewDetectLocalByBridgeInterface(t *testing.T) {
   126  	cases := []struct {
   127  		ifaceName   string
   128  		errExpected bool
   129  	}{
   130  		{
   131  			ifaceName:   "avz",
   132  			errExpected: false,
   133  		},
   134  		{
   135  			ifaceName:   "",
   136  			errExpected: true,
   137  		},
   138  	}
   139  	for i, c := range cases {
   140  		r, err := NewDetectLocalByBridgeInterface(c.ifaceName)
   141  		if c.errExpected {
   142  			if err == nil {
   143  				t.Errorf("Case[%d] expected error, but succeeded with: %q", i, r)
   144  			}
   145  			continue
   146  		}
   147  		if err != nil {
   148  			t.Errorf("Case[%d] failed with error: %v", i, err)
   149  		}
   150  	}
   151  }
   152  
   153  func TestNewDetectLocalByInterfaceNamePrefix(t *testing.T) {
   154  	cases := []struct {
   155  		ifacePrefix string
   156  		errExpected bool
   157  	}{
   158  		{
   159  			ifacePrefix: "veth",
   160  			errExpected: false,
   161  		},
   162  		{
   163  			ifacePrefix: "cbr0",
   164  			errExpected: false,
   165  		},
   166  		{
   167  			ifacePrefix: "",
   168  			errExpected: true,
   169  		},
   170  	}
   171  	for i, c := range cases {
   172  		r, err := NewDetectLocalByInterfaceNamePrefix(c.ifacePrefix)
   173  		if c.errExpected {
   174  			if err == nil {
   175  				t.Errorf("Case[%d] expected error, but succeeded with: %q", i, r)
   176  			}
   177  			continue
   178  		}
   179  		if err != nil {
   180  			t.Errorf("Case[%d] failed with error: %v", i, err)
   181  		}
   182  	}
   183  }
   184  
   185  func TestDetectLocalByBridgeInterface(t *testing.T) {
   186  	cases := []struct {
   187  		ifaceName               string
   188  		expectedJumpIfOutput    []string
   189  		expectedJumpIfNotOutput []string
   190  	}{
   191  		{
   192  			ifaceName:               "eth0",
   193  			expectedJumpIfOutput:    []string{"-i", "eth0"},
   194  			expectedJumpIfNotOutput: []string{"!", "-i", "eth0"},
   195  		},
   196  	}
   197  	for _, c := range cases {
   198  		localDetector, err := NewDetectLocalByBridgeInterface(c.ifaceName)
   199  		if err != nil {
   200  			t.Errorf("Error initializing localDetector: %v", err)
   201  			continue
   202  		}
   203  		if !localDetector.IsImplemented() {
   204  			t.Error("DetectLocalByBridgeInterface returns false for IsImplemented")
   205  		}
   206  
   207  		ifLocal := localDetector.IfLocal()
   208  		ifNotLocal := localDetector.IfNotLocal()
   209  
   210  		if !reflect.DeepEqual(ifLocal, c.expectedJumpIfOutput) {
   211  			t.Errorf("IfLocal, expected: '%v', but got: '%v'", c.expectedJumpIfOutput, ifLocal)
   212  		}
   213  
   214  		if !reflect.DeepEqual(ifNotLocal, c.expectedJumpIfNotOutput) {
   215  			t.Errorf("IfNotLocal, expected: '%v', but got: '%v'", c.expectedJumpIfNotOutput, ifNotLocal)
   216  		}
   217  	}
   218  }
   219  
   220  func TestDetectLocalByInterfaceNamePrefix(t *testing.T) {
   221  	cases := []struct {
   222  		ifacePrefix             string
   223  		chain                   string
   224  		args                    []string
   225  		expectedJumpIfOutput    []string
   226  		expectedJumpIfNotOutput []string
   227  	}{
   228  		{
   229  			ifacePrefix:             "eth0",
   230  			expectedJumpIfOutput:    []string{"-i", "eth0+"},
   231  			expectedJumpIfNotOutput: []string{"!", "-i", "eth0+"},
   232  		},
   233  	}
   234  	for _, c := range cases {
   235  		localDetector, err := NewDetectLocalByInterfaceNamePrefix(c.ifacePrefix)
   236  		if err != nil {
   237  			t.Errorf("Error initializing localDetector: %v", err)
   238  			continue
   239  		}
   240  		if !localDetector.IsImplemented() {
   241  			t.Error("DetectLocalByInterfaceNamePrefix returns false for IsImplemented")
   242  		}
   243  
   244  		ifLocal := localDetector.IfLocal()
   245  		ifNotLocal := localDetector.IfNotLocal()
   246  
   247  		if !reflect.DeepEqual(ifLocal, c.expectedJumpIfOutput) {
   248  			t.Errorf("IfLocal, expected: '%v', but got: '%v'", c.expectedJumpIfOutput, ifLocal)
   249  		}
   250  
   251  		if !reflect.DeepEqual(ifNotLocal, c.expectedJumpIfNotOutput) {
   252  			t.Errorf("IfNotLocal, expected: '%v', but got: '%v'", c.expectedJumpIfNotOutput, ifNotLocal)
   253  		}
   254  	}
   255  }