mirror of https://github.com/golang/go.git
net/netip: make Prefix.MarshalText format 4-in-6 IPs consistently
Fixes #50115. Change-Id: Iac76e5b486d3a2a784583345eaeb22c31cc4a36d Reviewed-on: https://go-review.googlesource.com/c/go/+/371134 Trust: Matt Layher <mdlayher@gmail.com> Run-TryBot: Matt Layher <mdlayher@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Trust: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
56817040d5
commit
49b7c9caec
|
|
@ -1421,9 +1421,14 @@ func (p Prefix) AppendTo(b []byte) []byte {
|
||||||
// p.ip is non-nil, because p is valid.
|
// p.ip is non-nil, because p is valid.
|
||||||
if p.ip.z == z4 {
|
if p.ip.z == z4 {
|
||||||
b = p.ip.appendTo4(b)
|
b = p.ip.appendTo4(b)
|
||||||
|
} else {
|
||||||
|
if p.ip.Is4In6() {
|
||||||
|
b = append(b, "::ffff:"...)
|
||||||
|
b = p.ip.Unmap().appendTo4(b)
|
||||||
} else {
|
} else {
|
||||||
b = p.ip.appendTo6(b)
|
b = p.ip.appendTo6(b)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
b = append(b, '/')
|
b = append(b, '/')
|
||||||
b = appendDecimal(b, uint8(p.bits))
|
b = appendDecimal(b, uint8(p.bits))
|
||||||
|
|
|
||||||
|
|
@ -413,6 +413,32 @@ func TestAddrPortMarshalUnmarshalBinary(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrefixMarshalTextString(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in Prefix
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{mustPrefix("1.2.3.4/24"), "1.2.3.4/24"},
|
||||||
|
{mustPrefix("fd7a:115c:a1e0:ab12:4843:cd96:626b:430b/118"), "fd7a:115c:a1e0:ab12:4843:cd96:626b:430b/118"},
|
||||||
|
{mustPrefix("::ffff:c000:0280/96"), "::ffff:192.0.2.128/96"},
|
||||||
|
{mustPrefix("::ffff:c000:0280%eth0/37"), "::ffff:192.0.2.128/37"}, // Zone should be stripped
|
||||||
|
{mustPrefix("::ffff:192.168.140.255/8"), "::ffff:192.168.140.255/8"},
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
if got := tt.in.String(); got != tt.want {
|
||||||
|
t.Errorf("%d. for %v String = %q; want %q", i, tt.in, got, tt.want)
|
||||||
|
}
|
||||||
|
mt, err := tt.in.MarshalText()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%d. for %v MarshalText error: %v", i, tt.in, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if string(mt) != tt.want {
|
||||||
|
t.Errorf("%d. for %v MarshalText = %q; want %q", i, tt.in, mt, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPrefixMarshalUnmarshalBinary(t *testing.T) {
|
func TestPrefixMarshalUnmarshalBinary(t *testing.T) {
|
||||||
type testCase struct {
|
type testCase struct {
|
||||||
prefix Prefix
|
prefix Prefix
|
||||||
|
|
@ -994,7 +1020,6 @@ func TestPrefixMarshalUnmarshal(t *testing.T) {
|
||||||
"0.0.0.0/0",
|
"0.0.0.0/0",
|
||||||
"::/0",
|
"::/0",
|
||||||
"::1/128",
|
"::1/128",
|
||||||
"::ffff:c000:1234/128",
|
|
||||||
"2001:db8::/32",
|
"2001:db8::/32",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue