1
0
mirror of https://github.com/golang/go synced 2024-11-17 09:24:43 -07:00

net/netip: add test for AddrFromSlice

AddrFromSlice is not covered by any other test so far.

Change-Id: I91034c6cac95a023fc419c855873a395b1afdab7
Reviewed-on: https://go-review.googlesource.com/c/go/+/435916
Reviewed-by: Carlos Amedee <carlos@golang.org>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Tobias Klauser 2022-09-29 10:50:23 +02:00 committed by Gopher Robot
parent 77c0e99c19
commit c4674e0134

View File

@ -301,6 +301,41 @@ func TestParseAddr(t *testing.T) {
} }
} }
func TestAddrFromSlice(t *testing.T) {
tests := []struct {
ip []byte
wantAddr Addr
wantOK bool
}{
{
ip: []byte{10, 0, 0, 1},
wantAddr: mustIP("10.0.0.1"),
wantOK: true,
},
{
ip: []byte{0xfe, 0x80, 15: 0x01},
wantAddr: mustIP("fe80::01"),
wantOK: true,
},
{
ip: []byte{0, 1, 2},
wantAddr: Addr{},
wantOK: false,
},
{
ip: nil,
wantAddr: Addr{},
wantOK: false,
},
}
for _, tt := range tests {
addr, ok := AddrFromSlice(tt.ip)
if ok != tt.wantOK || addr != tt.wantAddr {
t.Errorf("AddrFromSlice(%#v) = %#v, %v, want %#v, %v", tt.ip, addr, ok, tt.wantAddr, tt.wantOK)
}
}
}
func TestIPv4Constructors(t *testing.T) { func TestIPv4Constructors(t *testing.T) {
if AddrFrom4([4]byte{1, 2, 3, 4}) != MustParseAddr("1.2.3.4") { if AddrFrom4([4]byte{1, 2, 3, 4}) != MustParseAddr("1.2.3.4") {
t.Errorf("don't match") t.Errorf("don't match")