From b1c96121588cb128f65c8787d2d181b2ecc3e413 Mon Sep 17 00:00:00 2001 From: Vincent Janelle Date: Sat, 7 Jan 2023 00:16:01 -0800 Subject: [PATCH] (#501) make IP.String() method return something sensible --- net/net.go | 5 ++++- net/net_test.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 net/net_test.go diff --git a/net/net.go b/net/net.go index 8654820..f28da84 100644 --- a/net/net.go +++ b/net/net.go @@ -357,7 +357,10 @@ func ParseIP(s string) IP { // String returns the string form of the IP address ip. func (ip IP) String() string { - return string(ip) + if len(ip) < 4 { + return "" + } + return strconv.Itoa(int(ip[0])) + "." + strconv.Itoa(int(ip[1])) + "." + strconv.Itoa(int(ip[2])) + "." + strconv.Itoa(int(ip[3])) } // Conn is a generic stream-oriented network connection. diff --git a/net/net_test.go b/net/net_test.go new file mode 100644 index 0000000..e9ac662 --- /dev/null +++ b/net/net_test.go @@ -0,0 +1,14 @@ +package net + +import ( + "testing" + + qt "github.com/frankban/quicktest" +) + +func TestIPAddressString(t *testing.T) { + c := qt.New(t) + ipaddr := ParseIP("127.0.0.1") + + c.Assert(ipaddr.String(), qt.Equals, "127.0.0.1") +}