diff --git a/pkg/proxy/util/utils_test.go b/pkg/proxy/util/utils_test.go index 41be8629758..fe3d6680fc1 100644 --- a/pkg/proxy/util/utils_test.go +++ b/pkg/proxy/util/utils_test.go @@ -669,6 +669,108 @@ func TestMapIPsByIPFamily(t *testing.T) { } } +func TestMapCIDRsByIPFamily(t *testing.T) { + testCases := []struct { + desc string + ipString []string + wantIPv6 bool + expectCorrect []string + expectIncorrect []string + }{ + { + desc: "empty input IPv4", + ipString: []string{}, + wantIPv6: false, + expectCorrect: nil, + expectIncorrect: nil, + }, + { + desc: "empty input IPv6", + ipString: []string{}, + wantIPv6: true, + expectCorrect: nil, + expectIncorrect: nil, + }, + { + desc: "want IPv4 and receive IPv6", + ipString: []string{"fd00:20::1/64"}, + wantIPv6: false, + expectCorrect: nil, + expectIncorrect: []string{"fd00:20::1/64"}, + }, + { + desc: "want IPv6 and receive IPv4", + ipString: []string{"192.168.200.2/24"}, + wantIPv6: true, + expectCorrect: nil, + expectIncorrect: []string{"192.168.200.2/24"}, + }, + { + desc: "want IPv6 and receive IPv4 and IPv6", + ipString: []string{"192.168.200.2/24", "192.1.34.23/24", "fd00:20::1/64", "2001:db9::3/64"}, + wantIPv6: true, + expectCorrect: []string{"fd00:20::1/64", "2001:db9::3/64"}, + expectIncorrect: []string{"192.168.200.2/24", "192.1.34.23/24"}, + }, + { + desc: "want IPv4 and receive IPv4 and IPv6", + ipString: []string{"192.168.200.2/24", "192.1.34.23/24", "fd00:20::1/64", "2001:db9::3/64"}, + wantIPv6: false, + expectCorrect: []string{"192.168.200.2/24", "192.1.34.23/24"}, + expectIncorrect: []string{"fd00:20::1/64", "2001:db9::3/64"}, + }, + { + desc: "want IPv4 and receive IPv4 only", + ipString: []string{"192.168.200.2/24", "192.1.34.23/24"}, + wantIPv6: false, + expectCorrect: []string{"192.168.200.2/24", "192.1.34.23/24"}, + expectIncorrect: nil, + }, + { + desc: "want IPv6 and receive IPv4 only", + ipString: []string{"192.168.200.2/24", "192.1.34.23/24"}, + wantIPv6: true, + expectCorrect: nil, + expectIncorrect: []string{"192.168.200.2/24", "192.1.34.23/24"}, + }, + { + desc: "want IPv4 and receive IPv6 only", + ipString: []string{"fd00:20::1/64", "2001:db9::3/64"}, + wantIPv6: false, + expectCorrect: nil, + expectIncorrect: []string{"fd00:20::1/64", "2001:db9::3/64"}, + }, + { + desc: "want IPv6 and receive IPv6 only", + ipString: []string{"fd00:20::1/64", "2001:db9::3/64"}, + wantIPv6: true, + expectCorrect: []string{"fd00:20::1/64", "2001:db9::3/64"}, + expectIncorrect: nil, + }, + } + + for _, testcase := range testCases { + t.Run(testcase.desc, func(t *testing.T) { + ipFamily := v1.IPv4Protocol + otherIPFamily := v1.IPv6Protocol + + if testcase.wantIPv6 { + ipFamily = v1.IPv6Protocol + otherIPFamily = v1.IPv4Protocol + } + + cidrMap := MapCIDRsByIPFamily(testcase.ipString) + + if !reflect.DeepEqual(testcase.expectCorrect, cidrMap[ipFamily]) { + t.Errorf("Test %v failed: expected %v, got %v", testcase.desc, testcase.expectCorrect, cidrMap[ipFamily]) + } + if !reflect.DeepEqual(testcase.expectIncorrect, cidrMap[otherIPFamily]) { + t.Errorf("Test %v failed: expected %v, got %v", testcase.desc, testcase.expectIncorrect, cidrMap[otherIPFamily]) + } + }) + } +} + func TestGetClusterIPByFamily(t *testing.T) { testCases := []struct { name string