istio
103 строки · 2.6 Кб
1// Copyright Istio Authors
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
15package config16
17import (18"net"19"net/netip"20"testing"21)
22
23var tesrLocalIPAddrs = func(ips []netip.Addr) ([]net.Addr, error) {24var IPAddrs []net.Addr25for i := 0; i < len(ips); i++ {26var ipAddr net.Addr27ipNetAddr := &net.IPNet{IP: net.ParseIP(ips[i].String())}28ipAddr = ipNetAddr29IPAddrs = append(IPAddrs, ipAddr)30}31return IPAddrs, nil32}
33
34func TestGetLocalIP(t *testing.T) {35tests := []struct {36name string37lipas func() ([]net.Addr, error)38isDS bool39expected bool40}{41{42name: "ipv4 only local ip addresses",43lipas: func() ([]net.Addr, error) {44return tesrLocalIPAddrs([]netip.Addr{45netip.MustParseAddr("127.0.0.1"),46netip.MustParseAddr("1.2.3.5"),47})48},49isDS: false,50expected: false,51},52{53name: "ipv6 only local ip addresses",54lipas: func() ([]net.Addr, error) {55return tesrLocalIPAddrs([]netip.Addr{56netip.MustParseAddr("::1"),57netip.MustParseAddr("2222:3333::1"),58})59},60isDS: false,61expected: true,62},63{64name: "mixed ipv4 and ipv6 local ip addresses",65lipas: func() ([]net.Addr, error) {66return tesrLocalIPAddrs([]netip.Addr{67netip.MustParseAddr("::1"),68netip.MustParseAddr("127.0.0.1"),69netip.MustParseAddr("1.2.3.5"),70netip.MustParseAddr("2222:3333::1"),71})72},73isDS: false,74expected: false,75},76
77{78name: "mixed ipv4 and ipv6 local ip addresses with dual stack enable",79lipas: func() ([]net.Addr, error) {80return tesrLocalIPAddrs([]netip.Addr{81netip.MustParseAddr("::1"),82netip.MustParseAddr("127.0.0.1"),83netip.MustParseAddr("1.2.3.5"),84netip.MustParseAddr("2222:3333::1"),85})86},87isDS: true,88expected: true,89},90}91for _, tt := range tests {92t.Run(tt.name, func(t *testing.T) {93LocalIPAddrs = tt.lipas94_, isV6, err := getLocalIP(tt.isDS)95if err != nil {96t.Errorf("getLocalIP err: %s", err)97}98if isV6 != tt.expected {99t.Errorf("unexpected EnableInboundIPv6 result, expected: %t got: %t", tt.expected, isV6)100}101})102}103}
104