github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/memberlist/config_test.go (about)

     1  package memberlist
     2  
     3  import (
     4  	"github.com/stretchr/testify/require"
     5  	"net"
     6  	"testing"
     7  )
     8  
     9  func Test_IsValidAddressDefaults(t *testing.T) {
    10  	tests := []string{
    11  		"127.0.0.1",
    12  		"127.0.0.5",
    13  		"10.0.0.9",
    14  		"172.16.0.7",
    15  		"192.168.2.1",
    16  		"fe80::aede:48ff:fe00:1122",
    17  		"::1",
    18  	}
    19  	config := DefaultLANConfig()
    20  	for _, ip := range tests {
    21  		localV4 := net.ParseIP(ip)
    22  		if err := config.IPAllowed(localV4); err != nil {
    23  			t.Fatalf("IP %s Localhost Should be accepted for LAN", ip)
    24  		}
    25  	}
    26  	config = DefaultWANConfig()
    27  	for _, ip := range tests {
    28  		localV4 := net.ParseIP(ip)
    29  		if err := config.IPAllowed(localV4); err != nil {
    30  			t.Fatalf("IP %s Localhost Should be accepted for WAN", ip)
    31  		}
    32  	}
    33  }
    34  
    35  func Test_IsValidAddressOverride(t *testing.T) {
    36  	t.Parallel()
    37  	cases := []struct {
    38  		name    string
    39  		allow   []string
    40  		success []string
    41  		fail    []string
    42  	}{
    43  		{
    44  			name:    "Default, nil allows all",
    45  			allow:   nil,
    46  			success: []string{"127.0.0.5", "10.0.0.9", "192.168.1.7", "::1"},
    47  			fail:    []string{},
    48  		},
    49  		{
    50  			name:    "Only IPv4",
    51  			allow:   []string{"0.0.0.0/0"},
    52  			success: []string{"127.0.0.5", "10.0.0.9", "192.168.1.7"},
    53  			fail:    []string{"fe80::38bc:4dff:fe62:b1ae", "::1"},
    54  		},
    55  		{
    56  			name:    "Only IPv6",
    57  			allow:   []string{"::0/0"},
    58  			success: []string{"fe80::38bc:4dff:fe62:b1ae", "::1"},
    59  			fail:    []string{"127.0.0.5", "10.0.0.9", "192.168.1.7"},
    60  		},
    61  		{
    62  			name:    "Only 127.0.0.0/8 and ::1",
    63  			allow:   []string{"::1/128", "127.0.0.0/8"},
    64  			success: []string{"127.0.0.5", "::1"},
    65  			fail:    []string{"::2", "178.250.0.187", "10.0.0.9", "192.168.1.7", "fe80::38bc:4dff:fe62:b1ae"},
    66  		},
    67  	}
    68  
    69  	for _, testCase := range cases {
    70  		t.Run(testCase.name, func(t *testing.T) {
    71  			config := DefaultLANConfig()
    72  			var err error
    73  			config.CIDRsAllowed, err = ParseCIDRs(testCase.allow)
    74  			if err != nil {
    75  				t.Fatalf("failed parsing %s", testCase.allow)
    76  			}
    77  			for _, ips := range testCase.success {
    78  				ip := net.ParseIP(ips)
    79  				if err := config.IPAllowed(ip); err != nil {
    80  					t.Fatalf("Test case with %s should pass", ip)
    81  				}
    82  			}
    83  			for _, ips := range testCase.fail {
    84  				ip := net.ParseIP(ips)
    85  				if err := config.IPAllowed(ip); err == nil {
    86  					t.Fatalf("Test case with %s should fail", ip)
    87  				}
    88  			}
    89  		})
    90  
    91  	}
    92  
    93  }
    94  
    95  func TestParseCIDRs(t *testing.T) {
    96  	got, _ := ParseCIDRs([]string{"172.28.0.0/16"})
    97  	config := DefaultWANConfig()
    98  	config.CIDRsAllowed = got
    99  	err := config.AddrAllowed("172.16.238.10")
   100  	require.Error(t, err)
   101  
   102  	LookupIP = func(host string) ([]net.IP, error) {
   103  		return []net.IP{
   104  			net.ParseIP("172.16.238.10"),
   105  		}, nil
   106  	}
   107  	defer func() {
   108  		LookupIP = net.LookupIP
   109  	}()
   110  	err = config.AddrAllowed("this is a dns")
   111  	require.Error(t, err)
   112  
   113  	LookupIP = func(host string) ([]net.IP, error) {
   114  		return nil, nil
   115  	}
   116  	err = config.AddrAllowed("this is a dns")
   117  	require.Error(t, err)
   118  
   119  	_, err = ParseCIDRs([]string{"this is a dns"})
   120  	require.Error(t, err)
   121  }