1
0
mirror of https://github.com/golang/go synced 2024-11-19 04:54:41 -07:00

net: fix TestInterfaces, TestInterfaceAddrs on platforms without loopback interface

Fixes #10214.

Change-Id: If29d26c590eb53d4976e0a6ace0ed33aaf0c3e80
Reviewed-on: https://go-review.googlesource.com/7924
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
This commit is contained in:
Mikio Hara 2015-03-23 06:43:43 +09:00
parent a0fb8f8cce
commit 14082fad9b

View File

@ -47,12 +47,20 @@ func ipv6LinkLocalUnicastAddr(ifi *Interface) string {
return "" return ""
} }
type routeStats struct {
loop int // # of active loopback interfaces
other int // # of active other interfaces
uni4, uni6 int // # of active connected unicast, anycast routes
multi4, multi6 int // # of active connected multicast route clones
}
func TestInterfaces(t *testing.T) { func TestInterfaces(t *testing.T) {
ift, err := Interfaces() ift, err := Interfaces()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
var nifs, naf4, naf6, nmaf4, nmaf6 int var stats routeStats
for _, ifi := range ift { for _, ifi := range ift {
ifxi, err := InterfaceByIndex(ifi.Index) ifxi, err := InterfaceByIndex(ifi.Index)
if err != nil { if err != nil {
@ -70,36 +78,53 @@ func TestInterfaces(t *testing.T) {
} }
t.Logf("%q: flags %q, ifindex %v, mtu %v", ifi.Name, ifi.Flags.String(), ifi.Index, ifi.MTU) t.Logf("%q: flags %q, ifindex %v, mtu %v", ifi.Name, ifi.Flags.String(), ifi.Index, ifi.MTU)
t.Logf("hardware address %q", ifi.HardwareAddr.String()) t.Logf("hardware address %q", ifi.HardwareAddr.String())
if ifi.Flags&FlagUp != 0 && ifi.Flags&FlagLoopback == 0 { if ifi.Flags&FlagUp != 0 {
nifs++ // active interfaces except loopback interfaces if ifi.Flags&FlagLoopback != 0 {
stats.loop++
} else {
stats.other++
}
} }
n4, n6 := testInterfaceAddrs(t, &ifi) n4, n6 := testInterfaceAddrs(t, &ifi)
naf4 += n4 stats.uni4 += n4
naf6 += n6 stats.uni6 += n6
n4, n6 = testInterfaceMulticastAddrs(t, &ifi) n4, n6 = testInterfaceMulticastAddrs(t, &ifi)
nmaf4 += n4 stats.multi4 += n4
nmaf6 += n6 stats.multi6 += n6
} }
switch runtime.GOOS { switch runtime.GOOS {
case "nacl", "plan9", "solaris": case "nacl", "plan9", "solaris":
default: default:
if supportsIPv4 && nifs > 0 && naf4 == 0 { // Test the existence of connected unicast routes for
t.Errorf("got %v; want more than or equal to one", naf4) // IPv4.
if supportsIPv4 && stats.loop+stats.other > 0 && stats.uni4 == 0 {
t.Errorf("num IPv4 unicast routes = 0; want >0; summary: %+v", stats)
} }
if supportsIPv6 && nifs > 0 && naf6 == 0 { // Test the existence of connected unicast routes for
t.Errorf("got %v; want more than or equal to one", naf6) // IPv6. We can assume the existence of ::1/128 when
// at least one looopback interface is installed.
if supportsIPv6 && stats.loop > 0 && stats.uni6 == 0 {
t.Errorf("num IPv6 unicast routes = 0; want >0; summary: %+v", stats)
} }
} }
switch runtime.GOOS { switch runtime.GOOS {
case "dragonfly", "nacl", "netbsd", "openbsd", "plan9", "solaris": case "dragonfly", "nacl", "netbsd", "openbsd", "plan9", "solaris":
default: default:
// Unlike IPv6, IPv4 multicast capability is not a // Test the existence of connected multicast route
// mandatory feature. // clones for IPv4. Unlike IPv6, IPv4 multicast
//if supportsIPv4 && nactvifs > 0 && nmaf4 == 0 { // capability is not a mandatory feature, and so this
// t.Errorf("got %v; want more than or equal to one", nmaf4) // test is disabled.
//if supportsIPv4 && stats.loop > 0 && stats.uni4 > 1 && stats.multi4 == 0 {
// t.Errorf("num IPv4 multicast route clones = 0; want >0; summary: %+v", stats)
//} //}
if supportsIPv6 && nifs > 0 && nmaf6 == 0 { // Test the existence of connected multicast route
t.Errorf("got %v; want more than or equal to one", nmaf6) // clones for IPv6. Some platform never uses loopback
// interface as the nexthop for multicast routing.
// We can assume the existence of connected multicast
// route clones when at least two connected unicast
// routes, ::1/128 and other, are installed.
if supportsIPv6 && stats.loop > 0 && stats.uni6 > 1 && stats.multi6 == 0 {
t.Errorf("num IPv6 multicast route clones = 0; want >0; summary: %+v", stats)
} }
} }
} }
@ -109,22 +134,30 @@ func TestInterfaceAddrs(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
var nifs int var stats routeStats
for _, ifi := range ift { for _, ifi := range ift {
if ifi.Flags&FlagUp != 0 && ifi.Flags&FlagLoopback == 0 { if ifi.Flags&FlagUp != 0 {
nifs++ // active interfaces except loopback interfaces if ifi.Flags&FlagLoopback != 0 {
stats.loop++
} else {
stats.other++
}
} }
} }
ifat, err := InterfaceAddrs() ifat, err := InterfaceAddrs()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
naf4, naf6 := testAddrs(t, ifat) stats.uni4, stats.uni6 = testAddrs(t, ifat)
if supportsIPv4 && nifs > 0 && naf4 == 0 { // Test the existence of connected unicast routes for IPv4.
t.Errorf("got %v; want more than or equal to one", naf4) if supportsIPv4 && stats.loop+stats.other > 0 && stats.uni4 == 0 {
t.Errorf("num IPv4 unicast routes = 0; want >0; summary: %+v", stats)
} }
if supportsIPv6 && nifs > 0 && naf6 == 0 { // Test the existence of connected unicast routes for IPv6.
t.Errorf("got %v; want more than or equal to one", naf6) // We can assume the existence of ::1/128 when at least one
// looopback interface is installed.
if supportsIPv6 && stats.loop > 0 && stats.uni6 == 0 {
t.Errorf("num IPv6 unicast routes = 0; want >0; summary: %+v", stats)
} }
} }