github.com/haraldrudell/parl@v0.4.176/pnet/addr_test.go (about)

     1  /*
     2  © 2020–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pnet
     7  
     8  import (
     9  	"net"
    10  	"net/netip"
    11  	"reflect"
    12  	"testing"
    13  )
    14  
    15  func TestAddrToIPAddr(t *testing.T) {
    16  	//t.Errorf("logging on")
    17  	var ipv6 = netip.MustParseAddr("fe80::%eth0")
    18  	// ipv6: fe80::%eth0
    19  	t.Logf("ipv6: %s", ipv6)
    20  	var netIPAddr = net.IPAddr{IP: ipv6.AsSlice(), Zone: ipv6.Zone()}
    21  	//network: "ip" string: "fe80::%eth0"
    22  	t.Logf("network: %q string: %q", netIPAddr.Network(), &netIPAddr)
    23  
    24  	var ipv6Socket = netip.MustParseAddrPort("[fe80::%eth0]:80")
    25  	// ipv6Socket: [fe80::%eth0]:80
    26  	t.Logf("ipv6Socket: %s", ipv6Socket)
    27  	var tcp = net.TCPAddr{
    28  		IP:   ipv6Socket.Addr().AsSlice(),
    29  		Port: int(ipv6Socket.Port()),
    30  		Zone: ipv6Socket.Addr().Zone(),
    31  	}
    32  	// tcp network: "tcp" string: "[fe80::%eth0]:80"
    33  	t.Logf("tcp network: %q string: %q", tcp.Network(), &tcp)
    34  
    35  	type args struct {
    36  		addr netip.Addr
    37  	}
    38  	tests := []struct {
    39  		name              string
    40  		args              args
    41  		wantAddrInterface net.Addr
    42  	}{
    43  		{"IPv6", args{ipv6}, &netIPAddr},
    44  	}
    45  	for _, tt := range tests {
    46  		t.Run(tt.name, func(t *testing.T) {
    47  			if gotAddrInterface := AddrToIPAddr(tt.args.addr); !reflect.DeepEqual(gotAddrInterface, tt.wantAddrInterface) {
    48  				t.Errorf("AddrToIPAddr() = %v, want %v", gotAddrInterface, tt.wantAddrInterface)
    49  			}
    50  		})
    51  	}
    52  }