github.com/sagernet/tfo-go@v0.0.0-20231209031829-7b5343ac1dc6/tfo_supported_test.go (about) 1 //go:build darwin || freebsd || linux || windows 2 3 package tfo 4 5 import ( 6 "net" 7 "testing" 8 ) 9 10 func testClientWriteReadServerReadWriteTCPAddr(listenTCPAddr, dialLocalTCPAddr *net.TCPAddr, t *testing.T) { 11 t.Logf("c->s payload: %v", helloworld) 12 t.Logf("s->c payload: %v", worldhello) 13 14 lntcp, err := ListenTCP("tcp", listenTCPAddr) 15 if err != nil { 16 t.Fatal(err) 17 } 18 defer lntcp.Close() 19 t.Log("Started listener on", lntcp.Addr()) 20 21 ctrlCh := make(chan struct{}) 22 go func() { 23 conn, err := lntcp.AcceptTCP() 24 if err != nil { 25 t.Error(err) 26 return 27 } 28 defer conn.Close() 29 t.Log("Accepted", conn.RemoteAddr()) 30 31 readUntilEOF(conn, helloworld, t) 32 write(conn, world, t) 33 write(conn, hello, t) 34 conn.CloseWrite() 35 close(ctrlCh) 36 }() 37 38 port := lntcp.Addr().(*net.TCPAddr).Port 39 ip := net.IPv6loopback 40 if listenTCPAddr != nil && listenTCPAddr.IP != nil { 41 ip = listenTCPAddr.IP 42 } 43 44 tc, err := DialTCP("tcp", dialLocalTCPAddr, &net.TCPAddr{ 45 IP: ip, 46 Port: port, 47 }, hello) 48 if err != nil { 49 t.Fatal(err) 50 } 51 defer tc.Close() 52 53 write(tc, world, t) 54 tc.CloseWrite() 55 readUntilEOF(tc, worldhello, t) 56 <-ctrlCh 57 } 58 59 func TestClientWriteReadServerReadWriteTCPAddr(t *testing.T) { 60 for _, c := range []struct { 61 name string 62 listenTCPAddr *net.TCPAddr 63 dialLocalTCPAddr *net.TCPAddr 64 }{ 65 { 66 name: "Unspecified", 67 listenTCPAddr: nil, 68 dialLocalTCPAddr: nil, 69 }, 70 { 71 name: "IPv4Loopback", 72 listenTCPAddr: &net.TCPAddr{ 73 IP: net.IPv4(127, 0, 0, 1), 74 }, 75 dialLocalTCPAddr: nil, 76 }, 77 { 78 name: "IPv6Loopback", 79 listenTCPAddr: &net.TCPAddr{ 80 IP: net.IPv6loopback, 81 }, 82 dialLocalTCPAddr: nil, 83 }, 84 { 85 name: "DialBind", 86 listenTCPAddr: nil, 87 dialLocalTCPAddr: &net.TCPAddr{ 88 IP: net.IPv6loopback, 89 }, 90 }, 91 } { 92 t.Run(c.name, func(t *testing.T) { 93 testClientWriteReadServerReadWriteTCPAddr(c.listenTCPAddr, c.dialLocalTCPAddr, t) 94 }) 95 } 96 }