From: Tobias Klauser Date: Thu, 29 Sep 2022 08:50:23 +0000 (+0200) Subject: net/netip: add test for AddrFromSlice X-Git-Tag: go1.20rc1~707 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c4674e01343c0db9fad726401de114dac749382d;p=gostls13.git 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 Run-TryBot: Tobias Klauser TryBot-Result: Gopher Robot Auto-Submit: Tobias Klauser Reviewed-by: Damien Neil --- diff --git a/src/net/netip/netip_test.go b/src/net/netip/netip_test.go index bd1fe0c837..fcd2501b3e 100644 --- a/src/net/netip/netip_test.go +++ b/src/net/netip/netip_test.go @@ -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) { if AddrFrom4([4]byte{1, 2, 3, 4}) != MustParseAddr("1.2.3.4") { t.Errorf("don't match")