]> Cypherpunks repositories - gostls13.git/commitdiff
net/netip: add test for AddrFromSlice
authorTobias Klauser <tklauser@distanz.ch>
Thu, 29 Sep 2022 08:50:23 +0000 (10:50 +0200)
committerGopher Robot <gobot@golang.org>
Fri, 7 Oct 2022 17:54:18 +0000 (17:54 +0000)
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>
src/net/netip/netip_test.go

index bd1fe0c837e1c1e19402c2c45a28b85ea927bdf4..fcd2501b3e6eb47e44a4d6fef9eb76b05792c73f 100644 (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) {
        if AddrFrom4([4]byte{1, 2, 3, 4}) != MustParseAddr("1.2.3.4") {
                t.Errorf("don't match")