github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/sysfs/sock_test.go (about) 1 package sysfs 2 3 import ( 4 "net" 5 "testing" 6 "time" 7 8 "github.com/bananabytelabs/wazero/experimental/sys" 9 "github.com/bananabytelabs/wazero/internal/fsapi" 10 "github.com/bananabytelabs/wazero/internal/testing/require" 11 ) 12 13 func TestTcpConnFile_Write(t *testing.T) { 14 listen, err := net.Listen("tcp", "127.0.0.1:0") 15 require.NoError(t, err) 16 defer listen.Close() 17 18 tcpAddr, err := net.ResolveTCPAddr("tcp", listen.Addr().String()) 19 require.NoError(t, err) 20 tcp, err := net.DialTCP("tcp", nil, tcpAddr) 21 require.NoError(t, err) 22 defer tcp.Close() //nolint 23 24 file := newTcpConn(tcp) 25 errno := sys.Errno(0) 26 // Ensure we don't interrupt until we get a non-zero errno, 27 // and we retry on EAGAIN (i.e. when nonblocking is true). 28 for { 29 _, errno = file.Write([]byte("wazero")) 30 if errno != sys.EAGAIN { 31 break 32 } 33 time.Sleep(100 * time.Millisecond) 34 } 35 require.Zero(t, errno) 36 37 conn, err := listen.Accept() 38 require.NoError(t, err) 39 defer conn.Close() 40 41 bytes := make([]byte, 4) 42 43 n, err := conn.Read(bytes) 44 require.NoError(t, err) 45 require.NotEqual(t, 0, n) 46 47 require.Equal(t, "waze", string(bytes)) 48 } 49 50 func TestTcpConnFile_Read(t *testing.T) { 51 listen, err := net.Listen("tcp", "127.0.0.1:0") 52 require.NoError(t, err) 53 defer listen.Close() 54 55 tcpAddr, err := net.ResolveTCPAddr("tcp", listen.Addr().String()) 56 require.NoError(t, err) 57 tcp, err := net.DialTCP("tcp", nil, tcpAddr) 58 require.NoError(t, err) 59 defer tcp.Close() //nolint 60 61 n, err := tcp.Write([]byte("wazero")) 62 require.NoError(t, err) 63 require.NotEqual(t, 0, n) 64 65 conn, err := listen.Accept() 66 require.NoError(t, err) 67 defer conn.Close() 68 69 bytes := make([]byte, 4) 70 71 require.NoError(t, err) 72 errno := sys.Errno(0) 73 file := newTcpConn(conn.(*net.TCPConn)) 74 // Ensure we don't interrupt until we get a non-zero errno, 75 // and we retry on EAGAIN (i.e. when nonblocking is true). 76 for { 77 _, errno = file.Read(bytes) 78 if errno != sys.EAGAIN { 79 break 80 } 81 time.Sleep(100 * time.Millisecond) 82 } 83 require.Zero(t, errno) 84 require.NoError(t, err) 85 require.Equal(t, "waze", string(bytes)) 86 } 87 88 func TestTcpConnFile_Stat(t *testing.T) { 89 listen, err := net.Listen("tcp", "127.0.0.1:0") 90 require.NoError(t, err) 91 defer listen.Close() 92 93 tcpAddr, err := net.ResolveTCPAddr("tcp", listen.Addr().String()) 94 require.NoError(t, err) 95 tcp, err := net.DialTCP("tcp", nil, tcpAddr) 96 require.NoError(t, err) 97 defer tcp.Close() //nolint 98 99 conn, err := listen.Accept() 100 require.NoError(t, err) 101 defer conn.Close() 102 103 file := newTcpConn(tcp) 104 _, errno := file.Stat() 105 require.Zero(t, errno, "Stat should not fail") 106 } 107 108 func TestTcpConnFile_SetNonblock(t *testing.T) { 109 listen, err := net.Listen("tcp", "127.0.0.1:0") 110 require.NoError(t, err) 111 defer listen.Close() 112 113 lf := newTCPListenerFile(listen.(*net.TCPListener)) 114 115 tcpAddr, err := net.ResolveTCPAddr("tcp", listen.Addr().String()) 116 require.NoError(t, err) 117 tcp, err := net.DialTCP("tcp", nil, tcpAddr) 118 require.NoError(t, err) 119 defer tcp.Close() //nolint 120 121 nblf := fsapi.Adapt(lf) 122 errno := nblf.SetNonblock(true) 123 require.EqualErrno(t, 0, errno) 124 require.True(t, nblf.IsNonblock()) 125 126 conn, errno := lf.Accept() 127 require.EqualErrno(t, 0, errno) 128 defer conn.Close() 129 130 file := fsapi.Adapt(newTcpConn(tcp)) 131 errno = file.SetNonblock(true) 132 require.EqualErrno(t, 0, errno) 133 require.True(t, file.IsNonblock()) 134 }