net: make Dial, Listen{,Packet} for TCP/UDP with invalid port fail

This change makes Dial, Listen and ListenPacket with invalid port fail
whatever GODEBUG=netdns is.

Please be informed that cgoLookupPort with an out of range literal
number may return either the lower or upper bound value, 0 or 65535,
with no error on some platform.

Fixes #11715.

Change-Id: I43f9c4fb5526d1bf50b97698e0eb39d29fd74c35
Reviewed-on: https://go-review.googlesource.com/12447
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Mikio Hara 2015-07-21 12:51:01 +09:00
parent 3d5163cf43
commit b50b21d3e1
7 changed files with 93 additions and 38 deletions

View file

@ -116,8 +116,10 @@ var dialErrorTests = []struct {
{"tcp", "no-such-name:80"},
{"tcp", "mh/astro/r70:http"},
{"tcp", "127.0.0.1:0"},
{"udp", "127.0.0.1:0"},
{"tcp", JoinHostPort("127.0.0.1", "-1")},
{"tcp", JoinHostPort("127.0.0.1", "123456789")},
{"udp", JoinHostPort("127.0.0.1", "-1")},
{"udp", JoinHostPort("127.0.0.1", "123456789")},
{"ip:icmp", "127.0.0.1"},
{"unix", "/path/to/somewhere"},
@ -145,10 +147,23 @@ func TestDialError(t *testing.T) {
for i, tt := range dialErrorTests {
c, err := d.Dial(tt.network, tt.address)
if err == nil {
t.Errorf("#%d: should fail; %s:%s->%s", i, tt.network, c.LocalAddr(), c.RemoteAddr())
t.Errorf("#%d: should fail; %s:%s->%s", i, c.LocalAddr().Network(), c.LocalAddr(), c.RemoteAddr())
c.Close()
continue
}
if tt.network == "tcp" || tt.network == "udp" {
nerr := err
if op, ok := nerr.(*OpError); ok {
nerr = op.Err
}
if sys, ok := nerr.(*os.SyscallError); ok {
nerr = sys.Err
}
if nerr == errOpNotSupported {
t.Errorf("#%d: should fail without %v; %s:%s->", i, nerr, tt.network, tt.address)
continue
}
}
if c != nil {
t.Errorf("Dial returned non-nil interface %T(%v) with err != nil", c, c)
}
@ -198,7 +213,8 @@ var listenErrorTests = []struct {
{"tcp", "no-such-name:80"},
{"tcp", "mh/astro/r70:http"},
{"tcp", "127.0.0.1:0"},
{"tcp", JoinHostPort("127.0.0.1", "-1")},
{"tcp", JoinHostPort("127.0.0.1", "123456789")},
{"unix", "/path/to/somewhere"},
{"unixpacket", "/path/to/somewhere"},
@ -223,10 +239,23 @@ func TestListenError(t *testing.T) {
for i, tt := range listenErrorTests {
ln, err := Listen(tt.network, tt.address)
if err == nil {
t.Errorf("#%d: should fail; %s:%s->", i, tt.network, ln.Addr())
t.Errorf("#%d: should fail; %s:%s->", i, ln.Addr().Network(), ln.Addr())
ln.Close()
continue
}
if tt.network == "tcp" {
nerr := err
if op, ok := nerr.(*OpError); ok {
nerr = op.Err
}
if sys, ok := nerr.(*os.SyscallError); ok {
nerr = sys.Err
}
if nerr == errOpNotSupported {
t.Errorf("#%d: should fail without %v; %s:%s->", i, nerr, tt.network, tt.address)
continue
}
}
if ln != nil {
t.Errorf("Listen returned non-nil interface %T(%v) with err != nil", ln, ln)
}
@ -246,6 +275,9 @@ var listenPacketErrorTests = []struct {
{"udp", "127.0.0.1:☺"},
{"udp", "no-such-name:80"},
{"udp", "mh/astro/r70:http"},
{"udp", JoinHostPort("127.0.0.1", "-1")},
{"udp", JoinHostPort("127.0.0.1", "123456789")},
}
func TestListenPacketError(t *testing.T) {
@ -263,7 +295,7 @@ func TestListenPacketError(t *testing.T) {
for i, tt := range listenPacketErrorTests {
c, err := ListenPacket(tt.network, tt.address)
if err == nil {
t.Errorf("#%d: should fail; %s:%s->", i, tt.network, c.LocalAddr())
t.Errorf("#%d: should fail; %s:%s->", i, c.LocalAddr().Network(), c.LocalAddr())
c.Close()
continue
}