diff options
author | Achille Roussel <achille.roussel@gmail.com> | 2023-06-10 13:13:05 -0700 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-06-13 21:06:56 +0000 |
commit | 54c75b40a2a98a2942b86584b2bff5255d584b1d (patch) | |
tree | 0c59a556e4f937d34ab6cdce8e456743e58ee350 | |
parent | dac75b66754978a89eeb143e2b7cabb830409fd8 (diff) | |
download | go-54c75b40a2a98a2942b86584b2bff5255d584b1d.tar.gz go-54c75b40a2a98a2942b86584b2bff5255d584b1d.zip |
net: ensure net.Addr values match the connection type on wasip1
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>
-rw-r--r-- | src/net/fd_wasip1.go | 24 | ||||
-rw-r--r-- | src/net/file_wasip1_test.go | 20 |
2 files changed, 42 insertions, 2 deletions
diff --git a/src/net/fd_wasip1.go b/src/net/fd_wasip1.go index a3584e82bd..74d0b0b2e8 100644 --- a/src/net/fd_wasip1.go +++ b/src/net/fd_wasip1.go @@ -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, } } diff --git a/src/net/file_wasip1_test.go b/src/net/file_wasip1_test.go index 137574090f..4f4259069d 100644 --- a/src/net/file_wasip1_test.go +++ b/src/net/file_wasip1_test.go @@ -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) } } |