diff options
| author | Mikio Hara <mikioh.mikioh@gmail.com> | 2015-07-21 12:51:01 +0900 |
|---|---|---|
| committer | Mikio Hara <mikioh.mikioh@gmail.com> | 2015-11-01 05:28:17 +0000 |
| commit | b50b21d3e130cf19de99c2736d038b636dde75c3 (patch) | |
| tree | ff8bac2a3f5b28546610e6f877fd7a2f7cd38850 /src/net/error_test.go | |
| parent | 3d5163cf4348b5e697dcbf09897a62893876ac3c (diff) | |
| download | go-b50b21d3e130cf19de99c2736d038b636dde75c3.tar.xz | |
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>
Diffstat (limited to 'src/net/error_test.go')
| -rw-r--r-- | src/net/error_test.go | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/src/net/error_test.go b/src/net/error_test.go index bf95ff6108..6e85362938 100644 --- a/src/net/error_test.go +++ b/src/net/error_test.go @@ -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 } |
