net: protocol specific listen functions return a proper local socket address

When a nil listener address is passed to some protocol specific
listen function, it will create an unnamed, unbound socket because
of the nil listener address. Other listener functions may return
invalid address error.

This CL allows to pass a nil listener address to all protocol
specific listen functions to fix above inconsistency. Also make it
possible to return a proper local socket address in case of a nil
listner address.

Fixes #4190.
Fixes #3847.

R=rsc, iant
CC=golang-dev
https://golang.org/cl/6525048
This commit is contained in:
Mikio Hara 2012-11-13 12:56:28 +09:00
parent 0ae80785e6
commit 677c6e6ee7
8 changed files with 130 additions and 28 deletions

View file

@ -87,3 +87,33 @@ func testWriteToPacketConn(t *testing.T, raddr string) {
t.Fatal("Write should fail")
}
}
var udpConnLocalNameTests = []struct {
net string
laddr *UDPAddr
}{
{"udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)}},
{"udp4", &UDPAddr{}},
{"udp4", nil},
}
func TestUDPConnLocalName(t *testing.T) {
if testing.Short() || !*testExternal {
t.Logf("skipping test to avoid external network")
return
}
for _, tt := range udpConnLocalNameTests {
c, err := ListenUDP(tt.net, tt.laddr)
if err != nil {
t.Errorf("ListenUDP failed: %v", err)
return
}
defer c.Close()
la := c.LocalAddr()
if a, ok := la.(*UDPAddr); !ok || a.Port == 0 {
t.Errorf("got %v; expected a proper address with non-zero port number", la)
return
}
}
}