1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 // loopbackInterface returns an available logical network interface
13 // for loopback tests. It returns nil if no suitable interface is
15 func loopbackInterface() *Interface
{
16 ift
, err
:= Interfaces()
20 for _
, ifi
:= range ift
{
21 if ifi
.Flags
&FlagLoopback
!= 0 && ifi
.Flags
&FlagUp
!= 0 {
28 // ipv6LinkLocalUnicastAddr returns an IPv6 link-local unicast address
29 // on the given network interface for tests. It returns "" if no
30 // suitable address is found.
31 func ipv6LinkLocalUnicastAddr(ifi
*Interface
) string {
35 ifat
, err
:= ifi
.Addrs()
39 for _
, ifa
:= range ifat
{
40 switch ifa
:= ifa
.(type) {
42 if ifa
.IP
.To4() == nil && ifa
.IP
.IsLinkLocalUnicast() {
43 return ifa
.IP
.String()
46 if ifa
.IP
.To4() == nil && ifa
.IP
.IsLinkLocalUnicast() {
47 return ifa
.IP
.String()
54 func TestInterfaces(t
*testing
.T
) {
55 ift
, err
:= Interfaces()
57 t
.Fatalf("Interfaces failed: %v", err
)
59 t
.Logf("table: len/cap = %v/%v", len(ift
), cap(ift
))
61 for _
, ifi
:= range ift
{
62 ifxi
, err
:= InterfaceByIndex(ifi
.Index
)
64 t
.Fatalf("InterfaceByIndex(%v) failed: %v", ifi
.Index
, err
)
66 if !reflect
.DeepEqual(ifxi
, &ifi
) {
67 t
.Fatalf("InterfaceByIndex(%v) = %v, want %v", ifi
.Index
, ifxi
, ifi
)
69 ifxn
, err
:= InterfaceByName(ifi
.Name
)
71 t
.Fatalf("InterfaceByName(%q) failed: %v", ifi
.Name
, err
)
73 if !reflect
.DeepEqual(ifxn
, &ifi
) {
74 t
.Fatalf("InterfaceByName(%q) = %v, want %v", ifi
.Name
, ifxn
, ifi
)
76 t
.Logf("%q: flags %q, ifindex %v, mtu %v", ifi
.Name
, ifi
.Flags
.String(), ifi
.Index
, ifi
.MTU
)
77 t
.Logf("\thardware address %q", ifi
.HardwareAddr
.String())
78 testInterfaceAddrs(t
, &ifi
)
79 testInterfaceMulticastAddrs(t
, &ifi
)
83 func TestInterfaceAddrs(t
*testing
.T
) {
84 ifat
, err
:= InterfaceAddrs()
86 t
.Fatalf("InterfaceAddrs failed: %v", err
)
88 t
.Logf("table: len/cap = %v/%v", len(ifat
), cap(ifat
))
92 func testInterfaceAddrs(t
*testing
.T
, ifi
*Interface
) {
93 ifat
, err
:= ifi
.Addrs()
95 t
.Fatalf("Interface.Addrs failed: %v", err
)
100 func testInterfaceMulticastAddrs(t
*testing
.T
, ifi
*Interface
) {
101 ifmat
, err
:= ifi
.MulticastAddrs()
103 t
.Fatalf("Interface.MulticastAddrs failed: %v", err
)
105 testMulticastAddrs(t
, ifmat
)
108 func testAddrs(t
*testing
.T
, ifat
[]Addr
) {
109 for _
, ifa
:= range ifat
{
110 switch ifa
:= ifa
.(type) {
112 if ifa
== nil || ifa
.IP
== nil {
113 t
.Errorf("\tunexpected value: %v, %v", ifa
, ifa
.IP
)
115 t
.Logf("\tinterface address %q", ifa
.String())
118 if ifa
== nil || ifa
.IP
== nil || ifa
.Mask
== nil {
119 t
.Errorf("\tunexpected value: %v, %v, %v", ifa
, ifa
.IP
, ifa
.Mask
)
121 _
, prefixLen
:= ifa
.Mask
.Size()
122 if ifa
.IP
.To4() != nil && prefixLen
!= 8*IPv4len || ifa
.IP
.To16() != nil && ifa
.IP
.To4() == nil && prefixLen
!= 8*IPv6len
{
123 t
.Errorf("\tunexpected value: %v, %v, %v, %v", ifa
, ifa
.IP
, ifa
.Mask
, prefixLen
)
125 t
.Logf("\tinterface address %q", ifa
.String())
129 t
.Errorf("\tunexpected type: %T", ifa
)
134 func testMulticastAddrs(t
*testing
.T
, ifmat
[]Addr
) {
135 for _
, ifma
:= range ifmat
{
136 switch ifma
:= ifma
.(type) {
139 t
.Errorf("\tunexpected value: %v", ifma
)
141 t
.Logf("\tjoined group address %q", ifma
.String())
144 t
.Errorf("\tunexpected type: %T", ifma
)
149 func BenchmarkInterfaces(b
*testing
.B
) {
150 for i
:= 0; i
< b
.N
; i
++ {
151 if _
, err
:= Interfaces(); err
!= nil {
152 b
.Fatalf("Interfaces failed: %v", err
)
157 func BenchmarkInterfaceByIndex(b
*testing
.B
) {
158 ifi
:= loopbackInterface()
160 b
.Skip("loopback interface not found")
162 for i
:= 0; i
< b
.N
; i
++ {
163 if _
, err
:= InterfaceByIndex(ifi
.Index
); err
!= nil {
164 b
.Fatalf("InterfaceByIndex failed: %v", err
)
169 func BenchmarkInterfaceByName(b
*testing
.B
) {
170 ifi
:= loopbackInterface()
172 b
.Skip("loopback interface not found")
174 for i
:= 0; i
< b
.N
; i
++ {
175 if _
, err
:= InterfaceByName(ifi
.Name
); err
!= nil {
176 b
.Fatalf("InterfaceByName failed: %v", err
)
181 func BenchmarkInterfaceAddrs(b
*testing
.B
) {
182 for i
:= 0; i
< b
.N
; i
++ {
183 if _
, err
:= InterfaceAddrs(); err
!= nil {
184 b
.Fatalf("InterfaceAddrs failed: %v", err
)
189 func BenchmarkInterfacesAndAddrs(b
*testing
.B
) {
190 ifi
:= loopbackInterface()
192 b
.Skip("loopback interface not found")
194 for i
:= 0; i
< b
.N
; i
++ {
195 if _
, err
:= ifi
.Addrs(); err
!= nil {
196 b
.Fatalf("Interface.Addrs failed: %v", err
)
201 func BenchmarkInterfacesAndMulticastAddrs(b
*testing
.B
) {
202 ifi
:= loopbackInterface()
204 b
.Skip("loopback interface not found")
206 for i
:= 0; i
< b
.N
; i
++ {
207 if _
, err
:= ifi
.MulticastAddrs(); err
!= nil {
208 b
.Fatalf("Interface.MulticastAddrs failed: %v", err
)