github.com/Finschia/ostracon@v1.1.5/privval/internal/ip_filter_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"net"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  type addrStub struct {
    11  	address string
    12  }
    13  
    14  func (a addrStub) Network() string {
    15  	return ""
    16  }
    17  
    18  func (a addrStub) String() string {
    19  	return a.address
    20  }
    21  
    22  func TestFilterRemoteConnectionByIP(t *testing.T) {
    23  	type fields struct {
    24  		allowIP    string
    25  		remoteAddr net.Addr
    26  		expected   net.Addr
    27  	}
    28  	tests := []struct {
    29  		name   string
    30  		fields fields
    31  	}{
    32  		{
    33  			"should allow correct ip",
    34  			struct {
    35  				allowIP    string
    36  				remoteAddr net.Addr
    37  				expected   net.Addr
    38  			}{"127.0.0.1", addrStub{"127.0.0.1:45678"}, addrStub{"127.0.0.1:45678"}},
    39  		},
    40  		{
    41  			"should not allow different ip",
    42  			struct {
    43  				allowIP    string
    44  				remoteAddr net.Addr
    45  				expected   net.Addr
    46  			}{"127.0.0.1", addrStub{"10.0.0.2:45678"}, nil},
    47  		},
    48  		{
    49  			"should works for IPv6 with correct ip",
    50  			struct {
    51  				allowIP    string
    52  				remoteAddr net.Addr
    53  				expected   net.Addr
    54  			}{"2001:db8::1", addrStub{"[2001:db8::1]:80"}, addrStub{"[2001:db8::1]:80"}},
    55  		},
    56  		{
    57  			"should works for IPv6 with incorrect ip",
    58  			struct {
    59  				allowIP    string
    60  				remoteAddr net.Addr
    61  				expected   net.Addr
    62  			}{"2001:db8::2", addrStub{"[2001:db8::1]:80"}, nil},
    63  		},
    64  		{
    65  			"empty allowIP should deny all",
    66  			struct {
    67  				allowIP    string
    68  				remoteAddr net.Addr
    69  				expected   net.Addr
    70  			}{"", addrStub{"127.0.0.1:45678"}, nil},
    71  		},
    72  	}
    73  	for _, tt := range tests {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			cut := NewIpFilter([]string{tt.fields.allowIP}, nil)
    76  			assert.Equalf(t, tt.fields.expected, cut.Filter(tt.fields.remoteAddr), tt.name)
    77  		})
    78  	}
    79  }
    80  
    81  func TestFilterRemoteConnectionByIPWithMultipleAllowIPs(t *testing.T) {
    82  	type fields struct {
    83  		allowList  []string
    84  		remoteAddr net.Addr
    85  		expected   net.Addr
    86  	}
    87  	tests := []struct {
    88  		name   string
    89  		fields fields
    90  	}{
    91  		{
    92  			"should allow the one in the allow list",
    93  			struct {
    94  				allowList  []string
    95  				remoteAddr net.Addr
    96  				expected   net.Addr
    97  			}{[]string{"127.0.0.1", "192.168.1.1"}, addrStub{"192.168.1.1:45678"}, addrStub{"192.168.1.1:45678"}},
    98  		},
    99  		{
   100  			"should not allow any ip which is not in the allow list",
   101  			struct {
   102  				allowList  []string
   103  				remoteAddr net.Addr
   104  				expected   net.Addr
   105  			}{[]string{"127.0.0.1", "192.168.1.1"}, addrStub{"10.0.0.2:45678"}, nil},
   106  		},
   107  		{
   108  			"should works for IPv6 with one of correct ip in the allow list",
   109  			struct {
   110  				allowList  []string
   111  				remoteAddr net.Addr
   112  				expected   net.Addr
   113  			}{[]string{"2001:db8::1", "2001:db8::2"}, addrStub{"[2001:db8::1]:80"}, addrStub{"[2001:db8::1]:80"}},
   114  		},
   115  	}
   116  	for _, tt := range tests {
   117  		t.Run(tt.name, func(t *testing.T) {
   118  			cut := NewIpFilter(tt.fields.allowList, nil)
   119  			assert.Equalf(t, tt.fields.expected, cut.Filter(tt.fields.remoteAddr), tt.name)
   120  		})
   121  	}
   122  }
   123  
   124  func TestIpFilterShouldSetAllowAddress(t *testing.T) {
   125  	expected := []string{"192.168.0.1"}
   126  
   127  	cut := NewIpFilter(expected, nil)
   128  
   129  	assert.Equal(t, expected, cut.allowList)
   130  }
   131  
   132  func TestIpFilterStringShouldReturnsIP(t *testing.T) {
   133  	expected := []string{"127.0.0.1", "192.168.1.10"}
   134  	assert.Equal(t, strings.Join(expected, ","), NewIpFilter(expected, nil).String())
   135  }