github.com/dubbogo/gost@v1.14.0/net/net_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxnet
    19  
    20  import (
    21  	"net"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  import (
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestGetLocalIP(t *testing.T) {
    31  	ip, err := GetLocalIP()
    32  	assert.NoError(t, err)
    33  	t.Log(ip)
    34  }
    35  
    36  func TestIsSameAddr(t *testing.T) {
    37  	addr1 := net.TCPAddr{
    38  		IP:   []byte("192.168.0.1"),
    39  		Port: 80,
    40  		Zone: "cn/shanghai",
    41  	}
    42  	addr2 := net.TCPAddr{
    43  		IP:   []byte("192.168.0.2"),
    44  		Port: 80,
    45  		Zone: "cn/shanghai",
    46  	}
    47  
    48  	assert.True(t, IsSameAddr(&addr1, &addr1))
    49  	assert.False(t, IsSameAddr(&addr1, &addr2))
    50  
    51  	addr1.IP = []byte("2001:4860:0:2001::68")
    52  	addr1.Zone = ""
    53  
    54  	addr2.IP = []byte("2001:4860:0000:2001:0000:0000:0000:0068")
    55  	addr2.Zone = ""
    56  	assert.True(t, IsSameAddr(&addr1, &addr1))
    57  }
    58  
    59  func TestListenOnTCPRandomPort(t *testing.T) {
    60  	l, err := ListenOnTCPRandomPort("")
    61  	assert.Nil(t, err)
    62  	t.Logf("a tcp server listen on a random addr:%s", l.Addr())
    63  	l.Close()
    64  
    65  	localIP, err := GetLocalIP()
    66  	if err == nil {
    67  		l, err = ListenOnTCPRandomPort(localIP)
    68  		assert.Nil(t, err)
    69  		assert.True(t, strings.Contains(l.Addr().String(), localIP))
    70  		t.Logf("a tcp server listen on a random addr:%s", l.Addr())
    71  		l.Close()
    72  	}
    73  }
    74  
    75  func TestListenOnUDPRandomPort(t *testing.T) {
    76  	l, err := ListenOnUDPRandomPort("")
    77  	assert.Nil(t, err)
    78  	t.Logf("a udp peer listen on a random addr:%s", l.LocalAddr())
    79  	l.Close()
    80  
    81  	localIP, err := GetLocalIP()
    82  	if err == nil {
    83  		l, err = ListenOnUDPRandomPort(localIP)
    84  		assert.Nil(t, err)
    85  		assert.True(t, strings.Contains(l.LocalAddr().String(), localIP))
    86  		t.Logf("a udp server listen on a random addr:%s", l.LocalAddr())
    87  		l.Close()
    88  	}
    89  }
    90  
    91  func TestMatchIpIpv4Equal(t *testing.T) {
    92  	assert.True(t, MatchIP("192.168.0.1:8080", "192.168.0.1", "8080"))
    93  	assert.False(t, MatchIP("192.168.0.1:8081", "192.168.0.1", "8080"))
    94  	assert.True(t, MatchIP("*", "192.168.0.1", "8080"))
    95  	assert.True(t, MatchIP("*", "192.168.0.1", ""))
    96  	assert.True(t, MatchIP("*.*.*.*", "192.168.0.1", "8080"))
    97  	assert.False(t, MatchIP("*", "", ""))
    98  }
    99  
   100  func TestMatchIpIpv4Subnet(t *testing.T) {
   101  	assert.True(t, MatchIP("206.0.68.0/23", "206.0.68.123", "8080"))
   102  	assert.False(t, MatchIP("206.0.68.0/23", "207.0.69.123", "8080"))
   103  }
   104  
   105  func TestMatchIpIpv4Range(t *testing.T) {
   106  	assert.True(t, MatchIP("206.*.68.0", "206.0.68.0", "8080"))
   107  	assert.False(t, MatchIP("206.*.68.0", "206.0.69.0", "8080"))
   108  	assert.True(t, MatchIP("206.0.68-69.0", "206.0.68.0", "8080"))
   109  	assert.False(t, MatchIP("206.0.68-69.0", "206.0.70.0", "8080"))
   110  }
   111  
   112  func TestMatchIpIpv6Equal(t *testing.T) {
   113  	assert.True(t, MatchIP("[1fff:0:a88:85a3::ac1f]:8080", "1fff:0:a88:85a3::ac1f", "8080"))
   114  	assert.False(t, MatchIP("[1fff:0:a88:85a3::ac1f]:8081", "1fff:0:a88:85a3::ac1f", "8080"))
   115  	assert.True(t, MatchIP("*", "1fff:0:a88:85a3::ac1f", "8080"))
   116  	assert.True(t, MatchIP("*", "1fff:0:a88:85a3::ac1f", ""))
   117  	assert.True(t, MatchIP("*.*.*.*", "1fff:0:a88:85a3::ac1f", "8080"))
   118  	assert.False(t, MatchIP("*", "", ""))
   119  }
   120  
   121  func TestMatchIpIpv6Subnet(t *testing.T) {
   122  	assert.True(t, MatchIP("1fff:0:a88:85a3::ac1f/64", "1fff:0000:0a88:85a3:0000:0000:0000:0000", "8080"))
   123  	assert.False(t, MatchIP("1fff:0:a88:85a3::ac1f/64", "2fff:0000:0a88:85a3:0000:0000:0000:0000", "8080"))
   124  }
   125  
   126  func TestMatchIpIpv6Range(t *testing.T) {
   127  	assert.True(t, MatchIP("234e:0:4567:0:0:0:3d:*", "234e:0:4567:0:0:0:3d:4", "8080"))
   128  	assert.False(t, MatchIP("234e:0:4567:0:0:0:3d:*", "234e:0:4567:0:0:0:2d:4", "8080"))
   129  	assert.True(t, MatchIP("234e:0:4567:0:0:0:3d:1-2", "234e:0:4567:0:0:0:3d:1", "8080"))
   130  	assert.False(t, MatchIP("234e:0:4567:0:0:0:3d:1-2", "234e:0:4567:0:0:0:3d:3", "8080"))
   131  }
   132  
   133  func TestHostAddress(t *testing.T) {
   134  	assert.Equal(t, "127.0.0.1:8080", HostAddress("127.0.0.1", 8080))
   135  }
   136  
   137  func TestWSHostAddress(t *testing.T) {
   138  	assert.Equal(t, "ws://127.0.0.1:8080ws", WSHostAddress("127.0.0.1", 8080, "ws"))
   139  }
   140  
   141  func TestWSSHostAddress(t *testing.T) {
   142  	assert.Equal(t, "wss://127.0.0.1:8080wss", WSSHostAddress("127.0.0.1", 8080, "wss"))
   143  }
   144  
   145  func TestHostAddress2(t *testing.T) {
   146  	assert.Equal(t, "127.0.0.1:8080", HostAddress2("127.0.0.1", "8080"))
   147  }
   148  
   149  func TestWSHostAddress2(t *testing.T) {
   150  	assert.Equal(t, "ws://127.0.0.1:8080ws", WSHostAddress2("127.0.0.1", "8080", "ws"))
   151  }
   152  
   153  func TestWSSHostAddress2(t *testing.T) {
   154  	assert.Equal(t, "wss://127.0.0.1:8080wss", WSSHostAddress2("127.0.0.1", "8080", "wss"))
   155  	assert.False(t, WSSHostAddress2("127.0.0.1", "8080", "wss") == "wss://127.0.0.1:8081wss")
   156  }
   157  
   158  func TestHostPort(t *testing.T) {
   159  	host, port, err := HostPort("127.0.0.1:8080")
   160  	assert.Equal(t, "127.0.0.1", host)
   161  	assert.Equal(t, "8080", port)
   162  	assert.Nil(t, err)
   163  }