github.com/ianic/xnet/aio@v0.0.0-20230924160527-cee7f41ab201/tcp_listener_test.go (about)

     1  package aio
     2  
     3  import (
     4  	"syscall"
     5  	"testing"
     6  )
     7  
     8  func TestResolveTCPAddr4(t *testing.T) {
     9  	cases := []struct {
    10  		addr string
    11  		ip   [4]byte
    12  		port int
    13  	}{
    14  		{"127.0.0.1:8080", [4]byte{127, 0, 0, 1}, 8080},
    15  		{"4.4.4.4:0", [4]byte{4, 4, 4, 4}, 0},
    16  	}
    17  	for _, c := range cases {
    18  		so, domain, err := resolveTCPAddr(c.addr)
    19  		if err != nil {
    20  			t.Fatal(err)
    21  		}
    22  		if domain != syscall.AF_INET {
    23  			t.Fatalf("unexpected domain")
    24  		}
    25  		so4, ok := so.(*syscall.SockaddrInet4)
    26  		if !ok {
    27  			t.Fatalf("wrong class")
    28  		}
    29  		if so4.Port != c.port || so4.Addr != c.ip {
    30  			t.Fatalf("parse failed")
    31  		}
    32  	}
    33  }
    34  
    35  func TestResolveTCPAddr6(t *testing.T) {
    36  	cases := []struct {
    37  		addr string
    38  		ip   [16]byte
    39  		port int
    40  	}{
    41  		{"[2001:0000:130F:0000:0000:09C0:876A:130B]:1234", [16]byte{32, 1, 0, 0, 19, 15, 0, 0, 0, 0, 9, 192, 135, 106, 19, 11}, 1234},
    42  	}
    43  	for _, c := range cases {
    44  		so, domain, err := resolveTCPAddr(c.addr)
    45  		if err != nil {
    46  			t.Fatal(err)
    47  		}
    48  		if domain != syscall.AF_INET6 {
    49  			t.Fatalf("unexpected domain")
    50  		}
    51  		so6, ok := so.(*syscall.SockaddrInet6)
    52  		if !ok {
    53  			t.Fatalf("wrong class")
    54  		}
    55  		if so6.Port != c.port || so6.Addr != c.ip {
    56  			t.Fatalf("parse failed %d", so6.Addr)
    57  		}
    58  	}
    59  }