net: make IP.{String,MarshalText} return helpful information on address error

This change makes String and MarshalText methods of IP return a
hexadecial form of IP with no punctuation as part of error
notification. It doesn't affect the existing behavior of ParseIP.

Also fixes bad shadowing in ipToSockaddr and makes use of reserved
IP address blocks for documnetation.

Fixes #15052.
Updates #15228.

Change-Id: I9e9ecce308952ed5683066c3d1bb6a7b36458c65
Reviewed-on: https://go-review.googlesource.com/21642
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Mikio Hara 2016-04-07 17:19:29 +09:00
parent a119f88f2c
commit 1f54410a61
4 changed files with 192 additions and 43 deletions

View file

@ -206,6 +206,55 @@ func TestProtocolDialError(t *testing.T) {
}
}
func TestDialAddrError(t *testing.T) {
switch runtime.GOOS {
case "nacl", "plan9":
t.Skipf("not supported on %s", runtime.GOOS)
}
for _, tt := range []struct {
network string
lit string
addr *TCPAddr
}{
{"tcp4", "::1", nil},
{"tcp4", "", &TCPAddr{IP: IPv6loopback}},
// We don't test the {"tcp6", "byte sequence", nil}
// case for now because there is no easy way to
// control name resolution.
{"tcp6", "", &TCPAddr{IP: IP{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}},
} {
var err error
var c Conn
if tt.lit != "" {
c, err = Dial(tt.network, JoinHostPort(tt.lit, "0"))
} else {
c, err = DialTCP(tt.network, nil, tt.addr)
}
if err == nil {
c.Close()
t.Errorf("%s %q/%v: should fail", tt.network, tt.lit, tt.addr)
continue
}
if perr := parseDialError(err); perr != nil {
t.Error(perr)
continue
}
aerr, ok := err.(*OpError).Err.(*AddrError)
if !ok {
t.Errorf("%s %q/%v: should be AddrError: %v", tt.network, tt.lit, tt.addr, err)
continue
}
want := tt.lit
if tt.lit == "" {
want = tt.addr.IP.String()
}
if aerr.Addr != want {
t.Fatalf("%s: got %q; want %q", tt.network, aerr.Addr, want)
}
}
}
var listenErrorTests = []struct {
network, address string
}{