1
0
mirror of https://github.com/golang/go synced 2024-09-23 11:10:12 -06:00

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>
This commit is contained in:
Achille Roussel 2023-06-10 13:13:05 -07:00 committed by Gopher Robot
parent dac75b6675
commit 54c75b40a2
2 changed files with 42 additions and 2 deletions

View 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,
}
}

View 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)
}
}