github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/znet/ip_test.go (about)

     1  package znet
     2  
     3  import (
     4  	"net"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/sohaha/zlsgo"
    11  	"github.com/sohaha/zlsgo/ztype"
    12  )
    13  
    14  func TestNetIP(tt *testing.T) {
    15  	t := zlsgo.NewTest(tt)
    16  	r := newServer()
    17  	r.GET("/ip", func(c *Context) {
    18  		t.Equal("", c.GetClientIP())
    19  		ip := "127.0.0.1"
    20  		ipb := uint(2130706433)
    21  		_, _ = IPToLong("127")
    22  		l, _ := IPToLong(ip)
    23  		t.Equal(ipb, l)
    24  		i, _ := LongToIP(l)
    25  		t.Equal(ip, i)
    26  		ip2P, _ := LongToNetIP(l)
    27  		t.Equal(ip, ztype.ToString(ip2P))
    28  		ip2L, _ := NetIPToLong(ip2P)
    29  		t.Equal(ipb, ip2L)
    30  		t.Equal(true, IsLocalAddrIP(ip))
    31  		tt.Log(RemoteIP(c.Request))
    32  	})
    33  	w := httptest.NewRecorder()
    34  	req, _ := http.NewRequest("GET", "/ip", nil)
    35  	r.ServeHTTP(w, req)
    36  }
    37  
    38  func TestLocalAddrIP(tt *testing.T) {
    39  	t := zlsgo.NewTest(tt)
    40  	t.Log(IsLocalAddrIP("127.0.0.1"))
    41  	t.Log(IsLocalAddrIP("192.168.3.1"))
    42  	t.Log(IsLocalAddrIP("172.31.255.255"))
    43  	t.Log(IsLocalAddrIP("0.0.0.0"))
    44  	t.Log(IsLocalAddrIP("58.247.214.47"))
    45  	t.Log(IsLocalAddrIP("11.11.11.11"))
    46  }
    47  
    48  func TestIsIP(tt *testing.T) {
    49  	t := zlsgo.NewTest(tt)
    50  	t.EqualTrue(IsIP("127.0.0.1"))
    51  	t.EqualTrue(IsIP("172.31.255.255"))
    52  	t.EqualTrue(!IsIP("172.31.255.a"))
    53  }
    54  
    55  func TestGetIPV(tt *testing.T) {
    56  	t := zlsgo.NewTest(tt)
    57  	t.EqualExit(4, GetIPv("127.0.0.1"))
    58  	t.EqualExit(4, GetIPv("172.31.255.255"))
    59  	t.EqualExit(6, GetIPv("2001:db8:1:2::1"))
    60  	t.EqualTrue(GetIPv("2001:db8:1:2::1") != 4)
    61  }
    62  
    63  func TestIP2(tt *testing.T) {
    64  	t := zlsgo.NewTest(tt)
    65  	ip4Str := "127.0.0.1"
    66  	ip4 := net.ParseIP(ip4Str)
    67  	ipLong, _ := NetIPToLong(ip4)
    68  	ip4Raw, _ := LongToNetIP(ipLong)
    69  	t.EqualExit(ip4Str, ip4Raw.String())
    70  
    71  	ip6Str := "2001:db8:1:2::1"
    72  	ip6 := net.ParseIP(ip6Str)
    73  	ipv6Long, _ := NetIPv6ToLong(ip6)
    74  	ip6Raw, _ := LongToNetIPv6(ipv6Long)
    75  	t.EqualExit(ip6Str, ip6Raw.String())
    76  }
    77  
    78  func TestInNetwork(tt *testing.T) {
    79  	t := zlsgo.NewTest(tt)
    80  	sNetwork := "120.85.5.131/24"
    81  	for k, v := range map[string]bool{
    82  		"120.85.5.1":   true,
    83  		"120.85.5.255": true,
    84  		"120.85.5.256": false,
    85  		"120.85.8.131": false,
    86  	} {
    87  		t.Equal(v, InNetwork(k, sNetwork))
    88  	}
    89  }
    90  
    91  func TestGetPort(tt *testing.T) {
    92  	t := zlsgo.NewTest(tt)
    93  	p := 3780
    94  
    95  	port, err := Port(0, true)
    96  	t.EqualNil(err)
    97  	t.EqualTrue(port != p)
    98  	tt.Log(port)
    99  
   100  	port, err = Port(p, true)
   101  	t.EqualNil(err)
   102  
   103  	l, err := net.Listen("tcp", ":"+strconv.Itoa(port))
   104  	t.EqualNil(err)
   105  	defer l.Close()
   106  
   107  	port, err = Port(p, true)
   108  	t.EqualNil(err)
   109  	t.EqualTrue(port != p)
   110  	tt.Log(port)
   111  
   112  	port, err = Port(p, false)
   113  	t.EqualTrue(err != nil)
   114  	tt.Log(port, err)
   115  
   116  	port, err = MultiplePort([]int{p, 1234}, false)
   117  	t.EqualNil(err)
   118  	t.Equal(1234, port)
   119  
   120  	port, err = MultiplePort([]int{p}, false)
   121  	t.EqualTrue(err != nil)
   122  	tt.Log(port, err)
   123  }
   124  
   125  func Test_parseHeadersIP(t *testing.T) {
   126  	tt := zlsgo.NewTest(t)
   127  	tests := []struct {
   128  		args string
   129  		want []string
   130  	}{
   131  		{"", []string{}},
   132  		{"11.11.11.11,1.1.1.1, 2.2.2.2", []string{
   133  			"2.2.2.2",
   134  			"1.1.1.1",
   135  			"11.11.11.11",
   136  		}},
   137  	}
   138  	for _, v := range tests {
   139  		tt.EqualExit(v.want, parseHeadersIP(v.args))
   140  	}
   141  }
   142  
   143  func TestIsLocalAddrIP(t *testing.T) {
   144  	tt := zlsgo.NewTest(t)
   145  	tests := []struct {
   146  		args string
   147  		want bool
   148  	}{
   149  		{"127.0.0.1", true},
   150  		{"192.168.3.199", true},
   151  		{"18.22.1.3", false},
   152  	}
   153  	for _, v := range tests {
   154  		tt.EqualExit(v.want, IsLocalAddrIP(v.args))
   155  	}
   156  
   157  	request, _ := http.NewRequest("POST", "/", nil)
   158  	request.Header.Set("X-Forwarded-For", "  20.20.20.20, 30.30.30.30,10.10.10.10")
   159  	remoteIP := "10.10.10.10"
   160  	t.Log(getTrustedIP(request, remoteIP))
   161  	t.Log(RemoteIP(request))
   162  }