]> Cypherpunks repositories - gostls13.git/commitdiff
net: ensure net.Addr values match the connection type on wasip1
authorAchille Roussel <achille.roussel@gmail.com>
Sat, 10 Jun 2023 20:13:05 +0000 (13:13 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 13 Jun 2023 21:06:56 +0000 (21:06 +0000)
net.FileListener returns values of type *net.TCPListener, which can be
asserted by the application. The (*net.TCPListener).Addr method
documents that the underlying type of its return value is *net.TCPAddr,
which is fixed by this change.

Change-Id: Ife9906716d1b512092024ba50797bf7831536b75
Reviewed-on: https://go-review.googlesource.com/c/go/+/502335
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/net/fd_wasip1.go
src/net/file_wasip1_test.go

index a3584e82bd0d15a2977b976c79fb873d728a337d..74d0b0b2e856a5462a46bb9d480451792144a712 100644 (file)
@@ -47,11 +47,31 @@ func newFD(net string, sysfd int) *netFD {
 }
 
 func newPollFD(net string, pfd poll.FD) *netFD {
+       var laddr Addr
+       var raddr Addr
+       // WASI preview 1 does not have functions like getsockname/getpeername,
+       // so we cannot get access to the underlying IP address used by connections.
+       //
+       // However, listeners created by FileListener are of type *TCPListener,
+       // which can be asserted by a Go program. The (*TCPListener).Addr method
+       // documents that the returned value will be of type *TCPAddr, we satisfy
+       // the documented behavior by creating addresses of the expected type here.
+       switch net {
+       case "tcp":
+               laddr = new(TCPAddr)
+               raddr = new(TCPAddr)
+       case "udp":
+               laddr = new(UDPAddr)
+               raddr = new(UDPAddr)
+       default:
+               laddr = unknownAddr{}
+               raddr = unknownAddr{}
+       }
        return &netFD{
                pfd:   pfd,
                net:   net,
-               laddr: unknownAddr{},
-               raddr: unknownAddr{},
+               laddr: laddr,
+               raddr: raddr,
        }
 }
 
index 137574090fdce271b1adf2d6a80d71565de3e966..4f4259069d1f739ee570110bc94125f6bc0fc1c4 100644 (file)
@@ -79,14 +79,34 @@ func TestWasip1FileListenNet(t *testing.T) {
 func TestWasip1NewFileListener(t *testing.T) {
        if l, ok := newFileListener(newFD("tcp", -1)).(*TCPListener); !ok {
                t.Errorf("newFileListener: tcp listener type mismatch: %T", l)
+       } else {
+               testIsTCPAddr(t, "Addr", l.Addr())
        }
 }
 
 func TestWasip1NewFileConn(t *testing.T) {
        if c, ok := newFileConn(newFD("tcp", -1)).(*TCPConn); !ok {
                t.Errorf("newFileConn: tcp conn type mismatch: %T", c)
+       } else {
+               testIsTCPAddr(t, "LocalAddr", c.LocalAddr())
+               testIsTCPAddr(t, "RemoteAddr", c.RemoteAddr())
        }
        if c, ok := newFileConn(newFD("udp", -1)).(*UDPConn); !ok {
                t.Errorf("newFileConn: udp conn type mismatch: %T", c)
+       } else {
+               testIsUDPAddr(t, "LocalAddr", c.LocalAddr())
+               testIsUDPAddr(t, "RemoteAddr", c.RemoteAddr())
+       }
+}
+
+func testIsTCPAddr(t *testing.T, method string, addr Addr) {
+       if _, ok := addr.(*TCPAddr); !ok {
+               t.Errorf("%s: returned address is not a *TCPAddr: %T", method, addr)
+       }
+}
+
+func testIsUDPAddr(t *testing.T, method string, addr Addr) {
+       if _, ok := addr.(*UDPAddr); !ok {
+               t.Errorf("%s: returned address is not a *UDPAddr: %T", method, addr)
        }
 }