github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/conn/addr_test.go (about)

     1  package conn
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"crypto/rand"
     7  	"net/netip"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  // Test zero value.
    13  var (
    14  	addrZero       Addr
    15  	addrZeroPort   uint16
    16  	addrZeroString string
    17  )
    18  
    19  // Test IP address.
    20  var (
    21  	addrIP                = AddrFromIPPort(addrIPAddrPort)
    22  	addrIPAddr            = netip.AddrFrom16([16]byte{0x20, 0x01, 0x0d, 0xb8, 0xfa, 0xd6, 0x05, 0x72, 0xac, 0xbe, 0x71, 0x43, 0x14, 0xe5, 0x7a, 0x6e})
    23  	addrIPPort     uint16 = 1080
    24  	addrIPAddrPort        = netip.AddrPortFrom(addrIPAddr, addrIPPort)
    25  	addrIPHost            = "2001:db8:fad6:572:acbe:7143:14e5:7a6e"
    26  	addrIPString          = "[2001:db8:fad6:572:acbe:7143:14e5:7a6e]:1080"
    27  )
    28  
    29  // Test domain name.
    30  var (
    31  	addrDomain              = MustAddrFromDomainPort(addrDomainHost, addrDomainPort)
    32  	addrDomainHost          = "example.com"
    33  	addrDomainPort   uint16 = 443
    34  	addrDomainString        = "example.com:443"
    35  )
    36  
    37  func TestAddrEquals(t *testing.T) {
    38  	if !addrZero.Equals(addrZero) {
    39  		t.Error("addrZero.Equals(addrZero) returned false.")
    40  	}
    41  	if !addrIP.Equals(addrIP) {
    42  		t.Error("addrIP.Equals(addrIP) returned false.")
    43  	}
    44  	if !addrDomain.Equals(addrDomain) {
    45  		t.Error("addrDomain.Equals(addrDomain) returned false.")
    46  	}
    47  
    48  	if addrZero.Equals(addrIP) {
    49  		t.Error("addrZero.Equals(addrIP) returned true.")
    50  	}
    51  	if addrZero.Equals(addrDomain) {
    52  		t.Error("addrZero.Equals(addrDomain) returned true.")
    53  	}
    54  	if addrIP.Equals(addrDomain) {
    55  		t.Error("addrIP.Equals(addrDomain) returned true.")
    56  	}
    57  
    58  	if addrIP443 := AddrFromIPPort(netip.AddrPortFrom(addrIPAddr, 443)); addrIP443.Equals(addrIP) {
    59  		t.Error("addrIP443.Equals(addrIP) returned true.")
    60  	}
    61  }
    62  
    63  func TestAddrIs(t *testing.T) {
    64  	if addrZero.IsValid() {
    65  		t.Error("addrZero.IsValid() returned true.")
    66  	}
    67  	if addrZero.IsIP() {
    68  		t.Error("addrZero.IsIP() returned true.")
    69  	}
    70  	if addrZero.IsDomain() {
    71  		t.Error("addrZero.IsDomain() returned true.")
    72  	}
    73  
    74  	if !addrIP.IsValid() {
    75  		t.Error("addrIP.IsValid() returned false.")
    76  	}
    77  	if !addrIP.IsIP() {
    78  		t.Error("addrIP.IsIP() returned false.")
    79  	}
    80  	if addrIP.IsDomain() {
    81  		t.Error("addrIP.IsDomain() returned true.")
    82  	}
    83  
    84  	if !addrDomain.IsValid() {
    85  		t.Error("addrDomain.IsValid() returned false.")
    86  	}
    87  	if addrDomain.IsIP() {
    88  		t.Error("addrDomain.IsIP() returned true.")
    89  	}
    90  	if !addrDomain.IsDomain() {
    91  		t.Error("addrDomain.IsDomain() returned false.")
    92  	}
    93  }
    94  
    95  func assertPanic(t *testing.T, f func()) {
    96  	t.Helper()
    97  	defer func() {
    98  		if r := recover(); r == nil {
    99  			t.Error("Expected panic, got none.")
   100  		}
   101  	}()
   102  	f()
   103  }
   104  
   105  func TestAddrIP(t *testing.T) {
   106  	if ip := addrIP.IP(); ip != addrIPAddr {
   107  		t.Errorf("addrIP.IP() returned %s, expected %s.", ip, addrIPAddr)
   108  	}
   109  
   110  	assertPanic(t, func() { addrZero.IP() })
   111  	assertPanic(t, func() { addrDomain.IP() })
   112  }
   113  
   114  func TestAddrDomain(t *testing.T) {
   115  	if domain := addrDomain.Domain(); domain != addrDomainHost {
   116  		t.Errorf("addrDomain.Domain() returned %s, expected %s.", domain, addrDomainHost)
   117  	}
   118  
   119  	assertPanic(t, func() { addrZero.Domain() })
   120  	assertPanic(t, func() { addrIP.Domain() })
   121  }
   122  
   123  func TestAddrPort(t *testing.T) {
   124  	if port := addrZero.Port(); port != addrZeroPort {
   125  		t.Errorf("addrZero.Port() returned %d, expected %d.", port, addrZeroPort)
   126  	}
   127  
   128  	if port := addrIP.Port(); port != addrIPPort {
   129  		t.Errorf("addrIP.Port() returned %d, expected %d.", port, addrIPPort)
   130  	}
   131  
   132  	if port := addrDomain.Port(); port != addrDomainPort {
   133  		t.Errorf("addrDomain.Port() returned %d, expected %d.", port, addrDomainPort)
   134  	}
   135  }
   136  
   137  func TestAddrIPPort(t *testing.T) {
   138  	if ap := addrIP.IPPort(); ap != addrIPAddrPort {
   139  		t.Errorf("addrIP.IPPort() returned %s, expected %s.", ap, addrIPAddrPort)
   140  	}
   141  
   142  	assertPanic(t, func() { addrZero.IPPort() })
   143  	assertPanic(t, func() { addrDomain.IPPort() })
   144  }
   145  
   146  func TestAddrResolveIP(t *testing.T) {
   147  	ctx := context.Background()
   148  
   149  	ip, err := addrIP.ResolveIP(ctx, "ip")
   150  	if err != nil {
   151  		t.Fatal(err)
   152  	}
   153  	if ip != addrIPAddr {
   154  		t.Errorf("addrIP.ResolveIP() returned %s, expected %s.", ip, addrIPAddr)
   155  	}
   156  
   157  	ip, err = addrDomain.ResolveIP(ctx, "ip")
   158  	if err != nil {
   159  		t.Fatal(err)
   160  	}
   161  	if !ip.IsValid() {
   162  		t.Error("addrDomain.ResolveIP() returned invalid IP address.")
   163  	}
   164  
   165  	assertPanic(t, func() { addrZero.ResolveIP(ctx, "ip") })
   166  }
   167  
   168  func TestAddrResolveIPPort(t *testing.T) {
   169  	ctx := context.Background()
   170  
   171  	ipPort, err := addrIP.ResolveIPPort(ctx, "ip")
   172  	if err != nil {
   173  		t.Fatal(err)
   174  	}
   175  	if ipPort != addrIPAddrPort {
   176  		t.Errorf("addrIP.ResolveIPPort() returned %s, expected %s.", ipPort, addrIPAddrPort)
   177  	}
   178  
   179  	ipPort, err = addrDomain.ResolveIPPort(ctx, "ip")
   180  	if err != nil {
   181  		t.Fatal(err)
   182  	}
   183  	if !ipPort.Addr().IsValid() {
   184  		t.Error("addrDomain.ResolveIPPort() returned invalid IP address.")
   185  	}
   186  	if ipPort.Port() != addrDomainPort {
   187  		t.Errorf("addrDomain.ResolveIPPort(false) returned %s, expected port %d.", ipPort, addrDomainPort)
   188  	}
   189  
   190  	assertPanic(t, func() { addrZero.ResolveIPPort(ctx, "ip") })
   191  }
   192  
   193  func TestAddrHost(t *testing.T) {
   194  	if host := addrIP.Host(); host != addrIPHost {
   195  		t.Errorf("addrIP.Host() returned %s, expected %s.", host, addrIPHost)
   196  	}
   197  
   198  	if host := addrDomain.Host(); host != addrDomainHost {
   199  		t.Errorf("addrDomain.Host() returned %s, expected %s.", host, addrDomainHost)
   200  	}
   201  
   202  	assertPanic(t, func() { addrZero.Host() })
   203  }
   204  
   205  func TestAddrString(t *testing.T) {
   206  	if s := addrZero.String(); s != addrZeroString {
   207  		t.Errorf("addrZero.String() returned %s, expected %s.", s, addrZeroString)
   208  	}
   209  
   210  	if s := addrIP.String(); s != addrIPString {
   211  		t.Errorf("addrIP.String() returned %s, expected %s.", s, addrIPString)
   212  	}
   213  
   214  	if s := addrDomain.String(); s != addrDomainString {
   215  		t.Errorf("addrDomain.String() returned %s, expected %s.", s, addrDomainString)
   216  	}
   217  }
   218  
   219  func TestAddrAppendTo(t *testing.T) {
   220  	head := make([]byte, 64)
   221  	_, err := rand.Read(head)
   222  	if err != nil {
   223  		t.Fatal(err)
   224  	}
   225  
   226  	b := make([]byte, 0, 128)
   227  	b = append(b, head...)
   228  	bHead := b
   229  
   230  	b = addrZero.AppendTo(b)
   231  	if !bytes.Equal(bHead, head) {
   232  		t.Error("addrIP.AppendTo() returned modified head.")
   233  	}
   234  	if string(b[64:]) != addrZeroString {
   235  		t.Errorf("addrZero.AppendTo() returned %s, expected %s.", b[64:], addrZeroString)
   236  	}
   237  
   238  	b = addrIP.AppendTo(bHead)
   239  	if !bytes.Equal(bHead, head) {
   240  		t.Error("addrIP.AppendTo() returned modified head.")
   241  	}
   242  	if string(b[64:]) != addrIPString {
   243  		t.Errorf("addrIP.AppendTo() returned %s, expected %s.", b[64:], addrIPString)
   244  	}
   245  
   246  	b = addrDomain.AppendTo(bHead)
   247  	if !bytes.Equal(bHead, head) {
   248  		t.Error("addrDomain.AppendTo() returned modified head.")
   249  	}
   250  	if string(b[64:]) != addrDomainString {
   251  		t.Errorf("addrDomain.AppendTo() returned %s, expected %s.", b[64:], addrDomainString)
   252  	}
   253  }
   254  
   255  func TestAddrMarshalAndUnmarshalText(t *testing.T) {
   256  	text, err := addrZero.MarshalText()
   257  	if err != nil {
   258  		t.Fatal(err)
   259  	}
   260  	if string(text) != addrZeroString {
   261  		t.Errorf("addrZero.MarshalText() returned %s, expected %s.", text, addrZeroString)
   262  	}
   263  
   264  	text, err = addrIP.MarshalText()
   265  	if err != nil {
   266  		t.Fatal(err)
   267  	}
   268  	if string(text) != addrIPString {
   269  		t.Errorf("addrIP.MarshalText() returned %s, expected %s.", text, addrIPString)
   270  	}
   271  
   272  	var addrUnmarshaled Addr
   273  	err = addrUnmarshaled.UnmarshalText(text)
   274  	if err != nil {
   275  		t.Fatal(err)
   276  	}
   277  	if !addrUnmarshaled.Equals(addrIP) {
   278  		t.Errorf("addrIP.UnmarshalText() returned %s, expected %s.", addrUnmarshaled, addrIP)
   279  	}
   280  
   281  	text, err = addrDomain.MarshalText()
   282  	if err != nil {
   283  		t.Fatal(err)
   284  	}
   285  	if string(text) != addrDomainString {
   286  		t.Errorf("addrDomain.MarshalText() returned %s, expected %s.", text, addrDomainString)
   287  	}
   288  
   289  	err = addrUnmarshaled.UnmarshalText(text)
   290  	if err != nil {
   291  		t.Fatal(err)
   292  	}
   293  	if !addrUnmarshaled.Equals(addrDomain) {
   294  		t.Errorf("addrDomain.UnmarshalText() returned %s, expected %s.", addrUnmarshaled, addrDomain)
   295  	}
   296  }
   297  
   298  func TestAddrFromDomainPort(t *testing.T) {
   299  	if _, err := AddrFromDomainPort("", 443); err == nil {
   300  		t.Error("AddrFromDomainPort(\"\", 443) did not return error.")
   301  	}
   302  	s := strings.Repeat(" ", 256)
   303  	if _, err := AddrFromDomainPort(s, 443); err == nil {
   304  		t.Error("AddrFromDomainPort(s, 443) did not return error.")
   305  	}
   306  }
   307  
   308  func TestAddrFromHostPort(t *testing.T) {
   309  	addrFromHostPort, err := AddrFromHostPort(addrIPHost, addrIPPort)
   310  	if err != nil {
   311  		t.Fatal(err)
   312  	}
   313  	if !addrFromHostPort.Equals(addrIP) {
   314  		t.Errorf("AddrFromHostPort() returned %s, expected %s.", addrFromHostPort, addrIP)
   315  	}
   316  
   317  	addrFromHostPort, err = AddrFromHostPort(addrDomainHost, addrDomainPort)
   318  	if err != nil {
   319  		t.Fatal(err)
   320  	}
   321  	if !addrFromHostPort.Equals(addrDomain) {
   322  		t.Errorf("AddrFromHostPort() returned %s, expected %s.", addrFromHostPort, addrDomain)
   323  	}
   324  }
   325  
   326  func TestAddrParsing(t *testing.T) {
   327  	addrParsed, err := ParseAddr(addrIPString)
   328  	if err != nil {
   329  		t.Fatal(err)
   330  	}
   331  	if !addrParsed.Equals(addrIP) {
   332  		t.Errorf("ParseAddr() returned %s, expected %s.", addrParsed, addrIP)
   333  	}
   334  
   335  	addrParsed, err = ParseAddr(addrDomainString)
   336  	if err != nil {
   337  		t.Fatal(err)
   338  	}
   339  	if !addrParsed.Equals(addrDomain) {
   340  		t.Errorf("ParseAddr() returned %s, expected %s.", addrParsed, addrDomain)
   341  	}
   342  }
   343  
   344  var (
   345  	addrPort4    = netip.AddrPortFrom(netip.AddrFrom4([4]byte{127, 0, 0, 1}), 1080)
   346  	addrPort4in6 = netip.AddrPortFrom(netip.AddrFrom16([16]byte{10: 0xff, 11: 0xff, 127, 0, 0, 1}), 1080)
   347  )
   348  
   349  func TestAddrPortMappedEqual(t *testing.T) {
   350  	if !AddrPortMappedEqual(addrPort4, addrPort4) {
   351  		t.Error("AddrPortMappedEqual(addrPort4, addrPort4) returned false.")
   352  	}
   353  
   354  	if !AddrPortMappedEqual(addrPort4, addrPort4in6) {
   355  		t.Error("AddrPortMappedEqual(addrPort4, addrPort4in6) returned false.")
   356  	}
   357  
   358  	if !AddrPortMappedEqual(addrPort4in6, addrPort4in6) {
   359  		t.Error("AddrPortMappedEqual(addrPort4in6, addrPort4in6) returned false.")
   360  	}
   361  
   362  	if AddrPortMappedEqual(addrPort4, addrIPAddrPort) {
   363  		t.Error("AddrPortMappedEqual(addrPort4, addrIPAddrPort) returned true.")
   364  	}
   365  
   366  	if AddrPortMappedEqual(addrPort4in6, addrIPAddrPort) {
   367  		t.Error("AddrPortMappedEqual(addrPort4in6, addrIPAddrPort) returned true.")
   368  	}
   369  }