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.
14 // loopbackInterface returns an available logical network interface
15 // for loopback tests. It returns nil if no suitable interface is
17 func loopbackInterface() *Interface
{
18 ift
, err
:= Interfaces()
22 for _
, ifi
:= range ift
{
23 if ifi
.Flags
&FlagLoopback
!= 0 && ifi
.Flags
&FlagUp
!= 0 {
30 // ipv6LinkLocalUnicastAddr returns an IPv6 link-local unicast address
31 // on the given network interface for tests. It returns "" if no
32 // suitable address is found.
33 func ipv6LinkLocalUnicastAddr(ifi
*Interface
) string {
37 ifat
, err
:= ifi
.Addrs()
41 for _
, ifa
:= range ifat
{
42 if ifa
, ok
:= ifa
.(*IPNet
); ok
{
43 if ifa
.IP
.To4() == nil && ifa
.IP
.IsLinkLocalUnicast() {
44 return ifa
.IP
.String()
51 func TestInterfaces(t
*testing
.T
) {
52 ift
, err
:= Interfaces()
56 for _
, ifi
:= range ift
{
57 ifxi
, err
:= InterfaceByIndex(ifi
.Index
)
63 if ifxi
.Index
!= ifi
.Index
{
64 t
.Errorf("got %v; want %v", ifxi
, ifi
)
67 if !reflect
.DeepEqual(ifxi
, &ifi
) {
68 t
.Errorf("got %v; want %v", ifxi
, ifi
)
71 ifxn
, err
:= InterfaceByName(ifi
.Name
)
75 if !reflect
.DeepEqual(ifxn
, &ifi
) {
76 t
.Errorf("got %v; want %v", ifxn
, ifi
)
78 t
.Logf("%s: flags=%v index=%d mtu=%d hwaddr=%v", ifi
.Name
, ifi
.Flags
, ifi
.Index
, ifi
.MTU
, ifi
.HardwareAddr
)
82 func TestInterfaceAddrs(t
*testing
.T
) {
83 ift
, err
:= Interfaces()
87 ifStats
:= interfaceStats(ift
)
88 ifat
, err
:= InterfaceAddrs()
92 uniStats
, err
:= validateInterfaceUnicastAddrs(ifat
)
96 if err
:= checkUnicastStats(ifStats
, uniStats
); err
!= nil {
101 func TestInterfaceUnicastAddrs(t
*testing
.T
) {
102 ift
, err
:= Interfaces()
106 ifStats
:= interfaceStats(ift
)
110 var uniStats routeStats
111 for _
, ifi
:= range ift
{
112 ifat
, err
:= ifi
.Addrs()
116 stats
, err
:= validateInterfaceUnicastAddrs(ifat
)
120 uniStats
.ipv4
+= stats
.ipv4
121 uniStats
.ipv6
+= stats
.ipv6
123 if err
:= checkUnicastStats(ifStats
, &uniStats
); err
!= nil {
128 func TestInterfaceMulticastAddrs(t
*testing
.T
) {
129 ift
, err
:= Interfaces()
133 ifStats
:= interfaceStats(ift
)
134 ifat
, err
:= InterfaceAddrs()
138 uniStats
, err
:= validateInterfaceUnicastAddrs(ifat
)
142 var multiStats routeStats
143 for _
, ifi
:= range ift
{
144 ifmat
, err
:= ifi
.MulticastAddrs()
148 stats
, err
:= validateInterfaceMulticastAddrs(ifmat
)
152 multiStats
.ipv4
+= stats
.ipv4
153 multiStats
.ipv6
+= stats
.ipv6
155 if err
:= checkMulticastStats(ifStats
, uniStats
, &multiStats
); err
!= nil {
160 type ifStats
struct {
161 loop
int // # of active loopback interfaces
162 other
int // # of active other interfaces
165 func interfaceStats(ift
[]Interface
) *ifStats
{
167 for _
, ifi
:= range ift
{
168 if ifi
.Flags
&FlagUp
!= 0 {
169 if ifi
.Flags
&FlagLoopback
!= 0 {
179 type routeStats
struct {
180 ipv4
, ipv6
int // # of active connected unicast, anycast or multicast routes
183 func validateInterfaceUnicastAddrs(ifat
[]Addr
) (*routeStats
, error
) {
184 // Note: BSD variants allow assigning any IPv4/IPv6 address
185 // prefix to IP interface. For example,
186 // - 0.0.0.0/0 through 255.255.255.255/32
187 // - ::/0 through ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128
188 // In other words, there is no tightly-coupled combination of
189 // interface address prefixes and connected routes.
190 stats
:= new(routeStats
)
191 for _
, ifa
:= range ifat
{
192 switch ifa
:= ifa
.(type) {
194 if ifa
== nil || ifa
.IP
== nil || ifa
.IP
.IsMulticast() || ifa
.Mask
== nil {
195 return nil, fmt
.Errorf("unexpected value: %#v", ifa
)
197 if len(ifa
.IP
) != IPv6len
{
198 return nil, fmt
.Errorf("should be internal representation either IPv6 or IPv4-mapped IPv6 address: %#v", ifa
)
200 prefixLen
, maxPrefixLen
:= ifa
.Mask
.Size()
201 if ifa
.IP
.To4() != nil {
202 if 0 >= prefixLen || prefixLen
> 8*IPv4len || maxPrefixLen
!= 8*IPv4len
{
203 return nil, fmt
.Errorf("unexpected prefix length: %d/%d for %#v", prefixLen
, maxPrefixLen
, ifa
)
205 if ifa
.IP
.IsLoopback() && (prefixLen
!= 8 && prefixLen
!= 8*IPv4len
) { // see RFC 1122
206 return nil, fmt
.Errorf("unexpected prefix length: %d/%d for %#v", prefixLen
, maxPrefixLen
, ifa
)
210 if ifa
.IP
.To16() != nil && ifa
.IP
.To4() == nil {
211 if 0 >= prefixLen || prefixLen
> 8*IPv6len || maxPrefixLen
!= 8*IPv6len
{
212 return nil, fmt
.Errorf("unexpected prefix length: %d/%d for %#v", prefixLen
, maxPrefixLen
, ifa
)
214 if ifa
.IP
.IsLoopback() && prefixLen
!= 8*IPv6len
{ // see RFC 4291
215 return nil, fmt
.Errorf("unexpected prefix length: %d/%d for %#v", prefixLen
, maxPrefixLen
, ifa
)
220 if ifa
== nil || ifa
.IP
== nil || ifa
.IP
.IsMulticast() {
221 return nil, fmt
.Errorf("unexpected value: %#v", ifa
)
223 if len(ifa
.IP
) != IPv6len
{
224 return nil, fmt
.Errorf("should be internal representation either IPv6 or IPv4-mapped IPv6 address: %#v", ifa
)
226 if ifa
.IP
.To4() != nil {
229 if ifa
.IP
.To16() != nil && ifa
.IP
.To4() == nil {
233 return nil, fmt
.Errorf("unexpected type: %T", ifa
)
239 func validateInterfaceMulticastAddrs(ifat
[]Addr
) (*routeStats
, error
) {
240 stats
:= new(routeStats
)
241 for _
, ifa
:= range ifat
{
242 switch ifa
:= ifa
.(type) {
244 if ifa
== nil || ifa
.IP
== nil || ifa
.IP
.IsUnspecified() ||
!ifa
.IP
.IsMulticast() {
245 return nil, fmt
.Errorf("unexpected value: %#v", ifa
)
247 if len(ifa
.IP
) != IPv6len
{
248 return nil, fmt
.Errorf("should be internal representation either IPv6 or IPv4-mapped IPv6 address: %#v", ifa
)
250 if ifa
.IP
.To4() != nil {
253 if ifa
.IP
.To16() != nil && ifa
.IP
.To4() == nil {
257 return nil, fmt
.Errorf("unexpected type: %T", ifa
)
263 func checkUnicastStats(ifStats
*ifStats
, uniStats
*routeStats
) error
{
264 // Test the existence of connected unicast routes for IPv4.
265 if supportsIPv4() && ifStats
.loop
+ifStats
.other
> 0 && uniStats
.ipv4
== 0 {
266 return fmt
.Errorf("num IPv4 unicast routes = 0; want >0; summary: %+v, %+v", ifStats
, uniStats
)
268 // Test the existence of connected unicast routes for IPv6.
269 // We can assume the existence of ::1/128 when at least one
270 // loopback interface is installed.
271 if supportsIPv6() && ifStats
.loop
> 0 && uniStats
.ipv6
== 0 {
272 return fmt
.Errorf("num IPv6 unicast routes = 0; want >0; summary: %+v, %+v", ifStats
, uniStats
)
277 func checkMulticastStats(ifStats
*ifStats
, uniStats
, multiStats
*routeStats
) error
{
278 switch runtime
.GOOS
{
279 case "dragonfly", "nacl", "netbsd", "openbsd", "plan9", "solaris":
281 // Test the existence of connected multicast route
282 // clones for IPv4. Unlike IPv6, IPv4 multicast
283 // capability is not a mandatory feature, and so IPv4
284 // multicast validation is ignored and we only check
287 // Test the existence of connected multicast route
288 // clones for IPv6. Some platform never uses loopback
289 // interface as the nexthop for multicast routing.
290 // We can assume the existence of connected multicast
291 // route clones when at least two connected unicast
292 // routes, ::1/128 and other, are installed.
293 if supportsIPv6() && ifStats
.loop
> 0 && uniStats
.ipv6
> 1 && multiStats
.ipv6
== 0 {
294 return fmt
.Errorf("num IPv6 multicast route clones = 0; want >0; summary: %+v, %+v, %+v", ifStats
, uniStats
, multiStats
)
300 func BenchmarkInterfaces(b
*testing
.B
) {
301 testHookUninstaller
.Do(uninstallTestHooks
)
303 for i
:= 0; i
< b
.N
; i
++ {
304 if _
, err
:= Interfaces(); err
!= nil {
310 func BenchmarkInterfaceByIndex(b
*testing
.B
) {
311 testHookUninstaller
.Do(uninstallTestHooks
)
313 ifi
:= loopbackInterface()
315 b
.Skip("loopback interface not found")
317 for i
:= 0; i
< b
.N
; i
++ {
318 if _
, err
:= InterfaceByIndex(ifi
.Index
); err
!= nil {
324 func BenchmarkInterfaceByName(b
*testing
.B
) {
325 testHookUninstaller
.Do(uninstallTestHooks
)
327 ifi
:= loopbackInterface()
329 b
.Skip("loopback interface not found")
331 for i
:= 0; i
< b
.N
; i
++ {
332 if _
, err
:= InterfaceByName(ifi
.Name
); err
!= nil {
338 func BenchmarkInterfaceAddrs(b
*testing
.B
) {
339 testHookUninstaller
.Do(uninstallTestHooks
)
341 for i
:= 0; i
< b
.N
; i
++ {
342 if _
, err
:= InterfaceAddrs(); err
!= nil {
348 func BenchmarkInterfacesAndAddrs(b
*testing
.B
) {
349 testHookUninstaller
.Do(uninstallTestHooks
)
351 ifi
:= loopbackInterface()
353 b
.Skip("loopback interface not found")
355 for i
:= 0; i
< b
.N
; i
++ {
356 if _
, err
:= ifi
.Addrs(); err
!= nil {
362 func BenchmarkInterfacesAndMulticastAddrs(b
*testing
.B
) {
363 testHookUninstaller
.Do(uninstallTestHooks
)
365 ifi
:= loopbackInterface()
367 b
.Skip("loopback interface not found")
369 for i
:= 0; i
< b
.N
; i
++ {
370 if _
, err
:= ifi
.MulticastAddrs(); err
!= nil {