Retro68/gcc/libgo/go/net/interface_test.go

340 lines
8.9 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net
import (
2014-09-21 17:33:12 +00:00
"reflect"
2017-04-10 11:32:00 +00:00
"runtime"
2012-03-27 23:13:14 +00:00
"testing"
)
2014-09-21 17:33:12 +00:00
// loopbackInterface returns an available logical network interface
// for loopback tests. It returns nil if no suitable interface is
// found.
func loopbackInterface() *Interface {
ift, err := Interfaces()
if err != nil {
return nil
}
for _, ifi := range ift {
if ifi.Flags&FlagLoopback != 0 && ifi.Flags&FlagUp != 0 {
return &ifi
}
}
return nil
}
// ipv6LinkLocalUnicastAddr returns an IPv6 link-local unicast address
// on the given network interface for tests. It returns "" if no
// suitable address is found.
func ipv6LinkLocalUnicastAddr(ifi *Interface) string {
if ifi == nil {
return ""
}
ifat, err := ifi.Addrs()
if err != nil {
return ""
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
for _, ifa := range ifat {
2017-04-10 11:32:00 +00:00
if ifa, ok := ifa.(*IPNet); ok {
2014-09-21 17:33:12 +00:00
if ifa.IP.To4() == nil && ifa.IP.IsLinkLocalUnicast() {
return ifa.IP.String()
}
}
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
return ""
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
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
}
2012-03-27 23:13:14 +00:00
func TestInterfaces(t *testing.T) {
ift, err := Interfaces()
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
var stats routeStats
2012-03-27 23:13:14 +00:00
for _, ifi := range ift {
ifxi, err := InterfaceByIndex(ifi.Index)
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
if !reflect.DeepEqual(ifxi, &ifi) {
2017-04-10 11:32:00 +00:00
t.Errorf("got %v; want %v", ifxi, ifi)
2012-03-27 23:13:14 +00:00
}
ifxn, err := InterfaceByName(ifi.Name)
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
if !reflect.DeepEqual(ifxn, &ifi) {
2017-04-10 11:32:00 +00:00
t.Errorf("got %v; want %v", ifxn, ifi)
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
t.Logf("%q: flags %q, ifindex %v, mtu %v", ifi.Name, ifi.Flags.String(), ifi.Index, ifi.MTU)
2017-04-10 11:32:00 +00:00
t.Logf("hardware address %q", ifi.HardwareAddr.String())
if ifi.Flags&FlagUp != 0 {
if ifi.Flags&FlagLoopback != 0 {
stats.loop++
} else {
stats.other++
}
}
n4, n6 := testInterfaceAddrs(t, &ifi)
stats.uni4 += n4
stats.uni6 += n6
n4, n6 = testInterfaceMulticastAddrs(t, &ifi)
stats.multi4 += n4
stats.multi6 += n6
}
switch runtime.GOOS {
case "nacl", "plan9", "solaris":
default:
// Test the existence of connected unicast routes for
// IPv4.
if supportsIPv4 && stats.loop+stats.other > 0 && stats.uni4 == 0 {
t.Errorf("num IPv4 unicast routes = 0; want >0; summary: %+v", stats)
}
// Test the existence of connected unicast routes for
// 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 {
case "dragonfly", "nacl", "netbsd", "openbsd", "plan9", "solaris":
default:
// Test the existence of connected multicast route
// clones for IPv4. Unlike IPv6, IPv4 multicast
// capability is not a mandatory feature, and so this
// 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)
//}
// Test the existence of connected multicast route
// 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)
}
2012-03-27 23:13:14 +00:00
}
}
func TestInterfaceAddrs(t *testing.T) {
2017-04-10 11:32:00 +00:00
ift, err := Interfaces()
if err != nil {
t.Fatal(err)
}
var stats routeStats
for _, ifi := range ift {
if ifi.Flags&FlagUp != 0 {
if ifi.Flags&FlagLoopback != 0 {
stats.loop++
} else {
stats.other++
}
}
}
2012-03-27 23:13:14 +00:00
ifat, err := InterfaceAddrs()
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
}
stats.uni4, stats.uni6 = testAddrs(t, ifat)
// Test the existence of connected unicast routes for IPv4.
if supportsIPv4 && stats.loop+stats.other > 0 && stats.uni4 == 0 {
t.Errorf("num IPv4 unicast routes = 0; want >0; summary: %+v", stats)
}
// Test the existence of connected unicast routes for 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)
2012-03-27 23:13:14 +00:00
}
}
2017-04-10 11:32:00 +00:00
func testInterfaceAddrs(t *testing.T, ifi *Interface) (naf4, naf6 int) {
2012-03-27 23:13:14 +00:00
ifat, err := ifi.Addrs()
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
return testAddrs(t, ifat)
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
func testInterfaceMulticastAddrs(t *testing.T, ifi *Interface) (nmaf4, nmaf6 int) {
2012-03-27 23:13:14 +00:00
ifmat, err := ifi.MulticastAddrs()
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
return testMulticastAddrs(t, ifmat)
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
func testAddrs(t *testing.T, ifat []Addr) (naf4, naf6 int) {
2012-03-27 23:13:14 +00:00
for _, ifa := range ifat {
2014-09-21 17:33:12 +00:00
switch ifa := ifa.(type) {
case *IPNet:
2017-04-10 11:32:00 +00:00
if ifa == nil || ifa.IP == nil || ifa.IP.IsUnspecified() || ifa.IP.IsMulticast() || ifa.Mask == nil {
t.Errorf("unexpected value: %#v", ifa)
continue
}
if len(ifa.IP) != IPv6len {
t.Errorf("should be internal representation either IPv6 or IPv6 IPv4-mapped address: %#v", ifa)
continue
}
prefixLen, maxPrefixLen := ifa.Mask.Size()
if ifa.IP.To4() != nil {
if 0 >= prefixLen || prefixLen > 8*IPv4len || maxPrefixLen != 8*IPv4len {
t.Errorf("unexpected prefix length: %d/%d", prefixLen, maxPrefixLen)
continue
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
if ifa.IP.IsLoopback() && (prefixLen != 8 && prefixLen != 8*IPv4len) { // see RFC 1122
t.Errorf("unexpected prefix length for IPv4 loopback: %d/%d", prefixLen, maxPrefixLen)
continue
}
naf4++
}
if ifa.IP.To16() != nil && ifa.IP.To4() == nil {
if 0 >= prefixLen || prefixLen > 8*IPv6len || maxPrefixLen != 8*IPv6len {
t.Errorf("unexpected prefix length: %d/%d", prefixLen, maxPrefixLen)
continue
}
if ifa.IP.IsLoopback() && prefixLen != 8*IPv6len { // see RFC 4291
t.Errorf("unexpected prefix length for IPv6 loopback: %d/%d", prefixLen, maxPrefixLen)
continue
}
naf6++
}
t.Logf("interface address %q", ifa.String())
case *IPAddr:
if ifa == nil || ifa.IP == nil || ifa.IP.IsUnspecified() || ifa.IP.IsMulticast() {
t.Errorf("unexpected value: %#v", ifa)
continue
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
if len(ifa.IP) != IPv6len {
t.Errorf("should be internal representation either IPv6 or IPv6 IPv4-mapped address: %#v", ifa)
continue
}
if ifa.IP.To4() != nil {
naf4++
}
if ifa.IP.To16() != nil && ifa.IP.To4() == nil {
naf6++
}
t.Logf("interface address %s", ifa.String())
2012-03-27 23:13:14 +00:00
default:
2017-04-10 11:32:00 +00:00
t.Errorf("unexpected type: %T", ifa)
2012-03-27 23:13:14 +00:00
}
}
2017-04-10 11:32:00 +00:00
return
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
func testMulticastAddrs(t *testing.T, ifmat []Addr) (nmaf4, nmaf6 int) {
2012-03-27 23:13:14 +00:00
for _, ifma := range ifmat {
2014-09-21 17:33:12 +00:00
switch ifma := ifma.(type) {
2012-03-27 23:13:14 +00:00
case *IPAddr:
2017-04-10 11:32:00 +00:00
if ifma == nil || ifma.IP == nil || ifma.IP.IsUnspecified() || !ifma.IP.IsMulticast() {
t.Errorf("unexpected value: %+v", ifma)
continue
}
if len(ifma.IP) != IPv6len {
t.Errorf("should be internal representation either IPv6 or IPv6 IPv4-mapped address: %#v", ifma)
continue
}
if ifma.IP.To4() != nil {
nmaf4++
}
if ifma.IP.To16() != nil && ifma.IP.To4() == nil {
nmaf6++
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
t.Logf("joined group address %q", ifma.String())
2012-03-27 23:13:14 +00:00
default:
2017-04-10 11:32:00 +00:00
t.Errorf("unexpected type: %T", ifma)
2012-03-27 23:13:14 +00:00
}
}
2017-04-10 11:32:00 +00:00
return
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
func BenchmarkInterfaces(b *testing.B) {
2017-04-10 11:32:00 +00:00
testHookUninstaller.Do(uninstallTestHooks)
2014-09-21 17:33:12 +00:00
for i := 0; i < b.N; i++ {
if _, err := Interfaces(); err != nil {
2017-04-10 11:32:00 +00:00
b.Fatal(err)
2014-09-21 17:33:12 +00:00
}
}
}
func BenchmarkInterfaceByIndex(b *testing.B) {
2017-04-10 11:32:00 +00:00
testHookUninstaller.Do(uninstallTestHooks)
2014-09-21 17:33:12 +00:00
ifi := loopbackInterface()
if ifi == nil {
b.Skip("loopback interface not found")
}
for i := 0; i < b.N; i++ {
if _, err := InterfaceByIndex(ifi.Index); err != nil {
2017-04-10 11:32:00 +00:00
b.Fatal(err)
2014-09-21 17:33:12 +00:00
}
}
}
func BenchmarkInterfaceByName(b *testing.B) {
2017-04-10 11:32:00 +00:00
testHookUninstaller.Do(uninstallTestHooks)
2014-09-21 17:33:12 +00:00
ifi := loopbackInterface()
if ifi == nil {
b.Skip("loopback interface not found")
}
for i := 0; i < b.N; i++ {
if _, err := InterfaceByName(ifi.Name); err != nil {
2017-04-10 11:32:00 +00:00
b.Fatal(err)
2014-09-21 17:33:12 +00:00
}
}
}
func BenchmarkInterfaceAddrs(b *testing.B) {
2017-04-10 11:32:00 +00:00
testHookUninstaller.Do(uninstallTestHooks)
2014-09-21 17:33:12 +00:00
for i := 0; i < b.N; i++ {
if _, err := InterfaceAddrs(); err != nil {
2017-04-10 11:32:00 +00:00
b.Fatal(err)
2014-09-21 17:33:12 +00:00
}
}
}
func BenchmarkInterfacesAndAddrs(b *testing.B) {
2017-04-10 11:32:00 +00:00
testHookUninstaller.Do(uninstallTestHooks)
2014-09-21 17:33:12 +00:00
ifi := loopbackInterface()
if ifi == nil {
b.Skip("loopback interface not found")
}
for i := 0; i < b.N; i++ {
if _, err := ifi.Addrs(); err != nil {
2017-04-10 11:32:00 +00:00
b.Fatal(err)
2014-09-21 17:33:12 +00:00
}
}
}
func BenchmarkInterfacesAndMulticastAddrs(b *testing.B) {
2017-04-10 11:32:00 +00:00
testHookUninstaller.Do(uninstallTestHooks)
2014-09-21 17:33:12 +00:00
ifi := loopbackInterface()
if ifi == nil {
b.Skip("loopback interface not found")
}
for i := 0; i < b.N; i++ {
if _, err := ifi.MulticastAddrs(); err != nil {
2017-04-10 11:32:00 +00:00
b.Fatal(err)
2014-09-21 17:33:12 +00:00
}
}
}