all: use of nettest, remove internal/nettest

This change uses the nettest package where possible and removes the
internal/nettest package.

Change-Id: I5615a3ab7957183fecea6b5646df99dbb7c186e2
Reviewed-on: https://go-review.googlesource.com/c/net/+/123057
Run-TryBot: Mikio Hara <mikioh.public.networking@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
Mikio Hara
2018-07-11 01:05:18 +09:00
parent 9bff7f1a9d
commit 63eda1eb06
35 changed files with 249 additions and 508 deletions

View File

@@ -17,9 +17,9 @@ import (
"golang.org/x/net/icmp"
"golang.org/x/net/internal/iana"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
)
var testDiag = flag.Bool("diag", false, "whether to test ICMP message exchange with external network")
@@ -68,8 +68,8 @@ func TestDiag(t *testing.T) {
}
})
t.Run("Ping/Privileged", func(t *testing.T) {
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
for i, dt := range []diagTest{
{
@@ -100,8 +100,8 @@ func TestDiag(t *testing.T) {
}
})
t.Run("Probe/Privileged", func(t *testing.T) {
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
for i, dt := range []diagTest{
{

View File

@@ -1,11 +0,0 @@
// Copyright 2016 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.
// +build aix linux solaris
package nettest
func supportsIPv6MulticastDeliveryOnLoopback() bool {
return true
}

View File

@@ -1,28 +0,0 @@
// Copyright 2014 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.
// +build js nacl plan9
package nettest
import (
"fmt"
"runtime"
)
func maxOpenFiles() int {
return defaultMaxOpenFiles
}
func supportsRawIPSocket() (string, bool) {
return fmt.Sprintf("not supported on %s", runtime.GOOS), false
}
func supportsIPv6MulticastDeliveryOnLoopback() bool {
return false
}
func protocolNotSupported(err error) bool {
return false
}

View File

@@ -1,38 +0,0 @@
// Copyright 2015 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 nettest
import (
"fmt"
"runtime"
"syscall"
)
func maxOpenFiles() int {
return 4 * defaultMaxOpenFiles /* actually it's 16581375 */
}
func supportsRawIPSocket() (string, bool) {
// From http://msdn.microsoft.com/en-us/library/windows/desktop/ms740548.aspx:
// Note: To use a socket of type SOCK_RAW requires administrative privileges.
// Users running Winsock applications that use raw sockets must be a member of
// the Administrators group on the local computer, otherwise raw socket calls
// will fail with an error code of WSAEACCES. On Windows Vista and later, access
// for raw sockets is enforced at socket creation. In earlier versions of Windows,
// access for raw sockets is enforced during other socket operations.
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, 0)
if err == syscall.WSAEACCES {
return fmt.Sprintf("no access to raw socket allowed on %s", runtime.GOOS), false
}
if err != nil {
return err.Error(), false
}
syscall.Closesocket(s)
return "", true
}
func supportsIPv6MulticastDeliveryOnLoopback() bool {
return true
}

View File

@@ -1,94 +0,0 @@
// Copyright 2012 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 nettest
import "net"
// IsMulticastCapable reports whether ifi is an IP multicast-capable
// network interface. Network must be "ip", "ip4" or "ip6".
func IsMulticastCapable(network string, ifi *net.Interface) (net.IP, bool) {
switch network {
case "ip", "ip4", "ip6":
default:
return nil, false
}
if ifi == nil || ifi.Flags&net.FlagUp == 0 || ifi.Flags&net.FlagMulticast == 0 {
return nil, false
}
return hasRoutableIP(network, ifi)
}
// RoutedInterface returns a network interface that can route IP
// traffic and satisfies flags. It returns nil when an appropriate
// network interface is not found. Network must be "ip", "ip4" or
// "ip6".
func RoutedInterface(network string, flags net.Flags) *net.Interface {
switch network {
case "ip", "ip4", "ip6":
default:
return nil
}
ift, err := net.Interfaces()
if err != nil {
return nil
}
for _, ifi := range ift {
if ifi.Flags&flags != flags {
continue
}
if _, ok := hasRoutableIP(network, &ifi); !ok {
continue
}
return &ifi
}
return nil
}
func hasRoutableIP(network string, ifi *net.Interface) (net.IP, bool) {
ifat, err := ifi.Addrs()
if err != nil {
return nil, false
}
for _, ifa := range ifat {
switch ifa := ifa.(type) {
case *net.IPAddr:
if ip := routableIP(network, ifa.IP); ip != nil {
return ip, true
}
case *net.IPNet:
if ip := routableIP(network, ifa.IP); ip != nil {
return ip, true
}
}
}
return nil, false
}
func routableIP(network string, ip net.IP) net.IP {
if !ip.IsLoopback() && !ip.IsLinkLocalUnicast() && !ip.IsGlobalUnicast() {
return nil
}
switch network {
case "ip4":
if ip := ip.To4(); ip != nil {
return ip
}
case "ip6":
if ip.IsLoopback() { // addressing scope of the loopback address depends on each implementation
return nil
}
if ip := ip.To16(); ip != nil && ip.To4() == nil {
return ip
}
default:
if ip := ip.To4(); ip != nil {
return ip
}
if ip := ip.To16(); ip != nil {
return ip
}
}
return nil
}

View File

@@ -1,11 +0,0 @@
// Copyright 2015 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 nettest
const defaultMaxOpenFiles = 256
// MaxOpenFiles returns the maximum number of open files for the
// caller's process.
func MaxOpenFiles() int { return maxOpenFiles() }

View File

@@ -1,152 +0,0 @@
// Copyright 2014 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 nettest provides utilities for network testing.
package nettest // import "golang.org/x/net/internal/nettest"
import (
"fmt"
"io/ioutil"
"net"
"os"
"runtime"
)
var (
supportsIPv4 bool
supportsIPv6 bool
)
func init() {
if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
ln.Close()
supportsIPv4 = true
}
if ln, err := net.Listen("tcp6", "[::1]:0"); err == nil {
ln.Close()
supportsIPv6 = true
}
}
// SupportsIPv4 reports whether the platform supports IPv4 networking
// functionality.
func SupportsIPv4() bool { return supportsIPv4 }
// SupportsIPv6 reports whether the platform supports IPv6 networking
// functionality.
func SupportsIPv6() bool { return supportsIPv6 }
// SupportsRawIPSocket reports whether the platform supports raw IP
// sockets.
func SupportsRawIPSocket() (string, bool) {
return supportsRawIPSocket()
}
// SupportsIPv6MulticastDeliveryOnLoopback reports whether the
// platform supports IPv6 multicast packet delivery on software
// loopback interface.
func SupportsIPv6MulticastDeliveryOnLoopback() bool {
return supportsIPv6MulticastDeliveryOnLoopback()
}
// ProtocolNotSupported reports whether err is a protocol not
// supported error.
func ProtocolNotSupported(err error) bool {
return protocolNotSupported(err)
}
// TestableNetwork reports whether network is testable on the current
// platform configuration.
func TestableNetwork(network string) bool {
// This is based on logic from standard library's
// net/platform_test.go.
switch network {
case "unix", "unixgram":
switch runtime.GOOS {
case "android", "js", "nacl", "plan9", "windows":
return false
}
if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
return false
}
case "unixpacket":
switch runtime.GOOS {
case "aix", "android", "darwin", "freebsd", "js", "nacl", "plan9", "windows":
return false
case "netbsd":
// It passes on amd64 at least. 386 fails (Issue 22927). arm is unknown.
if runtime.GOARCH == "386" {
return false
}
}
}
return true
}
// NewLocalListener returns a listener which listens to a loopback IP
// address or local file system path.
// Network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
func NewLocalListener(network string) (net.Listener, error) {
switch network {
case "tcp":
if supportsIPv4 {
if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
return ln, nil
}
}
if supportsIPv6 {
return net.Listen("tcp6", "[::1]:0")
}
case "tcp4":
if supportsIPv4 {
return net.Listen("tcp4", "127.0.0.1:0")
}
case "tcp6":
if supportsIPv6 {
return net.Listen("tcp6", "[::1]:0")
}
case "unix", "unixpacket":
return net.Listen(network, localPath())
}
return nil, fmt.Errorf("%s is not supported", network)
}
// NewLocalPacketListener returns a packet listener which listens to a
// loopback IP address or local file system path.
// Network must be "udp", "udp4", "udp6" or "unixgram".
func NewLocalPacketListener(network string) (net.PacketConn, error) {
switch network {
case "udp":
if supportsIPv4 {
if c, err := net.ListenPacket("udp4", "127.0.0.1:0"); err == nil {
return c, nil
}
}
if supportsIPv6 {
return net.ListenPacket("udp6", "[::1]:0")
}
case "udp4":
if supportsIPv4 {
return net.ListenPacket("udp4", "127.0.0.1:0")
}
case "udp6":
if supportsIPv6 {
return net.ListenPacket("udp6", "[::1]:0")
}
case "unixgram":
return net.ListenPacket(network, localPath())
}
return nil, fmt.Errorf("%s is not supported", network)
}
func localPath() string {
f, err := ioutil.TempFile("", "nettest")
if err != nil {
panic(err)
}
path := f.Name()
f.Close()
os.Remove(path)
return path
}

View File

@@ -14,8 +14,8 @@ import (
"syscall"
"testing"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/internal/socket"
"golang.org/x/net/nettest"
)
func TestSocket(t *testing.T) {

View File

@@ -10,8 +10,8 @@ import (
"io"
"net"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/internal/socks"
"golang.org/x/net/nettest"
)
// An AuthRequest represents an authentication request.

View File

@@ -4,7 +4,7 @@
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris windows
package nettest
package ipv4_test
import (
"os"

11
ipv4/helper_stub_test.go Normal file
View File

@@ -0,0 +1,11 @@
// Copyright 2014 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.
// +build !aix,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris,!windows
package ipv4_test
func protocolNotSupported(err error) bool {
return false
}

View File

@@ -10,8 +10,8 @@ import (
"runtime"
"testing"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv4"
"golang.org/x/net/nettest"
)
var icmpStringTests = []struct {
@@ -66,8 +66,8 @@ func TestSetICMPFilter(t *testing.T) {
default:
t.Skipf("not supported on %s", runtime.GOOS)
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
c, err := net.ListenPacket("ip4:icmp", "127.0.0.1")

View File

@@ -14,8 +14,8 @@ import (
"golang.org/x/net/icmp"
"golang.org/x/net/internal/iana"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv4"
"golang.org/x/net/nettest"
)
var packetConnReadWriteMulticastUDPTests = []struct {
@@ -32,8 +32,8 @@ func TestPacketConnReadWriteMulticastUDP(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "solaris", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if ifi == nil {
ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
@@ -82,7 +82,7 @@ func TestPacketConnReadWriteMulticastUDP(t *testing.T) {
for i, toggle := range []bool{true, false, true} {
if err := p.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Logf("not supported on %s", runtime.GOOS)
continue
}
@@ -120,11 +120,11 @@ func TestPacketConnReadWriteMulticastICMP(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "solaris", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if ifi == nil {
ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
@@ -184,7 +184,7 @@ func TestPacketConnReadWriteMulticastICMP(t *testing.T) {
t.Fatal(err)
}
if err := p.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Logf("not supported on %s", runtime.GOOS)
continue
}
@@ -234,11 +234,11 @@ func TestRawConnReadWriteMulticastICMP(t *testing.T) {
if testing.Short() {
t.Skip("to avoid external network")
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if ifi == nil {
ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
@@ -305,7 +305,7 @@ func TestRawConnReadWriteMulticastICMP(t *testing.T) {
Dst: tt.grp.IP,
}
if err := r.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Logf("not supported on %s", runtime.GOOS)
continue
}

View File

@@ -9,8 +9,8 @@ import (
"runtime"
"testing"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv4"
"golang.org/x/net/nettest"
)
var udpMultipleGroupListenerTests = []net.Addr{
@@ -43,7 +43,7 @@ func TestUDPSinglePacketConnWithMultipleGroupListeners(t *testing.T) {
t.Fatal(err)
}
for i, ifi := range ift {
if _, ok := nettest.IsMulticastCapable("ip4", &ifi); !ok {
if _, err := nettest.MulticastSource("ip4", &ifi); err != nil {
continue
}
if err := p.JoinGroup(&ifi, gaddr); err != nil {
@@ -94,7 +94,7 @@ func TestUDPMultiplePacketConnWithMultipleGroupListeners(t *testing.T) {
t.Fatal(err)
}
for i, ifi := range ift {
if _, ok := nettest.IsMulticastCapable("ip4", &ifi); !ok {
if _, err := nettest.MulticastSource("ip4", &ifi); err != nil {
continue
}
for _, p := range ps {
@@ -136,8 +136,8 @@ func TestUDPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
}
port := "0"
for i, ifi := range ift {
ip, ok := nettest.IsMulticastCapable("ip4", &ifi)
if !ok {
ip, err := nettest.MulticastSource("ip4", &ifi)
if err != nil {
continue
}
c, err := net.ListenPacket("udp4", net.JoinHostPort(ip.String(), port)) // unicast address with non-reusable port
@@ -178,8 +178,8 @@ func TestIPSingleRawConnWithSingleGroupListener(t *testing.T) {
if testing.Short() {
t.Skip("to avoid external network")
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
c, err := net.ListenPacket("ip4:icmp", "0.0.0.0") // wildcard address
@@ -200,7 +200,7 @@ func TestIPSingleRawConnWithSingleGroupListener(t *testing.T) {
t.Fatal(err)
}
for i, ifi := range ift {
if _, ok := nettest.IsMulticastCapable("ip4", &ifi); !ok {
if _, err := nettest.MulticastSource("ip4", &ifi); err != nil {
continue
}
if err := r.JoinGroup(&ifi, &gaddr); err != nil {
@@ -223,8 +223,8 @@ func TestIPPerInterfaceSingleRawConnWithSingleGroupListener(t *testing.T) {
if testing.Short() {
t.Skip("to avoid external network")
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
gaddr := net.IPAddr{IP: net.IPv4(224, 0, 0, 254)} // see RFC 4727
@@ -239,8 +239,8 @@ func TestIPPerInterfaceSingleRawConnWithSingleGroupListener(t *testing.T) {
t.Fatal(err)
}
for i, ifi := range ift {
ip, ok := nettest.IsMulticastCapable("ip4", &ifi)
if !ok {
ip, err := nettest.MulticastSource("ip4", &ifi)
if err != nil {
continue
}
c, err := net.ListenPacket("ip4:253", ip.String()) // unicast address

View File

@@ -9,8 +9,8 @@ import (
"runtime"
"testing"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv4"
"golang.org/x/net/nettest"
)
var packetConnMulticastSocketOptionTests = []struct {
@@ -29,15 +29,15 @@ func TestPacketConnMulticastSocketOptions(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9":
t.Skipf("not supported on %s", runtime.GOOS)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if ifi == nil {
ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
m, ok := nettest.SupportsRawIPSocket()
ok := nettest.SupportsRawSocket()
for _, tt := range packetConnMulticastSocketOptionTests {
if tt.net == "ip4" && !ok {
t.Log(m)
t.Logf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
continue
}
c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
@@ -69,11 +69,11 @@ func TestRawConnMulticastSocketOptions(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9":
t.Skipf("not supported on %s", runtime.GOOS)
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if ifi == nil {
ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}

View File

@@ -14,8 +14,8 @@ import (
"testing"
"golang.org/x/net/internal/iana"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv4"
"golang.org/x/net/nettest"
)
func BenchmarkReadWriteUnicast(b *testing.B) {
@@ -50,7 +50,7 @@ func BenchmarkReadWriteUnicast(b *testing.B) {
b.Fatal(err)
}
cm := ipv4.ControlMessage{TTL: 1}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
ifi, _ := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
if ifi != nil {
cm.IfIndex = ifi.Index
}
@@ -91,7 +91,8 @@ func BenchmarkPacketConnReadWriteUnicast(b *testing.B) {
cm := ipv4.ControlMessage{
Src: net.IPv4(127, 0, 0, 1),
}
if ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); ifi != nil {
ifi, _ := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
if ifi != nil {
cm.IfIndex = ifi.Index
}
@@ -231,12 +232,12 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
defer p.Close()
dst := c.LocalAddr()
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
ifi, _ := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
cf := ipv4.FlagTTL | ipv4.FlagSrc | ipv4.FlagDst | ipv4.FlagInterface
wb := []byte("HELLO-R-U-THERE")
if err := p.SetControlMessage(cf, true); err != nil { // probe before test
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Skipf("not supported on %s", runtime.GOOS)
}
t.Fatal(err)
@@ -358,11 +359,11 @@ func TestPacketConnConcurrentReadWriteUnicast(t *testing.T) {
func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv4.PacketConn, data []byte, dst net.Addr, batch bool) {
t.Helper()
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
ifi, _ := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
cf := ipv4.FlagTTL | ipv4.FlagSrc | ipv4.FlagDst | ipv4.FlagInterface
if err := p.SetControlMessage(cf, true); err != nil { // probe before test
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Skipf("not supported on %s", runtime.GOOS)
}
t.Fatal(err)

View File

@@ -14,8 +14,8 @@ import (
"golang.org/x/net/icmp"
"golang.org/x/net/internal/iana"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv4"
"golang.org/x/net/nettest"
)
func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
@@ -23,8 +23,7 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
if ifi == nil {
if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
@@ -42,7 +41,7 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
for i, toggle := range []bool{true, false, true} {
if err := p.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Logf("not supported on %s", runtime.GOOS)
continue
}
@@ -74,11 +73,10 @@ func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
if ifi == nil {
if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
@@ -112,7 +110,7 @@ func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
t.Fatal(err)
}
if err := p.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Logf("not supported on %s", runtime.GOOS)
continue
}
@@ -160,11 +158,10 @@ func TestRawConnReadWriteUnicastICMP(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
if ifi == nil {
if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
@@ -206,7 +203,7 @@ func TestRawConnReadWriteUnicastICMP(t *testing.T) {
Dst: dst.IP,
}
if err := r.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Logf("not supported on %s", runtime.GOOS)
continue
}

View File

@@ -10,8 +10,8 @@ import (
"testing"
"golang.org/x/net/internal/iana"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv4"
"golang.org/x/net/nettest"
)
func TestConnUnicastSocketOptions(t *testing.T) {
@@ -19,8 +19,7 @@ func TestConnUnicastSocketOptions(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
if ifi == nil {
if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
@@ -65,15 +64,14 @@ func TestPacketConnUnicastSocketOptions(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
if ifi == nil {
if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
m, ok := nettest.SupportsRawIPSocket()
ok := nettest.SupportsRawSocket()
for _, tt := range packetConnUnicastSocketOptionTests {
if tt.net == "ip4" && !ok {
t.Log(m)
t.Logf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
continue
}
c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
@@ -91,11 +89,10 @@ func TestRawConnUnicastSocketOptions(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
if ifi == nil {
if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}

View File

@@ -12,13 +12,14 @@ import (
"golang.org/x/net/bpf"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
)
func TestBPF(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}

31
ipv6/helper_posix_test.go Normal file
View File

@@ -0,0 +1,31 @@
// Copyright 2014 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.
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris windows
package ipv6_test
import (
"os"
"syscall"
)
func protocolNotSupported(err error) bool {
switch err := err.(type) {
case syscall.Errno:
switch err {
case syscall.EPROTONOSUPPORT, syscall.ENOPROTOOPT:
return true
}
case *os.SyscallError:
switch err := err.Err.(type) {
case syscall.Errno:
switch err {
case syscall.EPROTONOSUPPORT, syscall.ENOPROTOOPT:
return true
}
}
}
return false
}

20
ipv6/helper_stub_test.go Normal file
View File

@@ -0,0 +1,20 @@
// Copyright 2014 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.
// +build !aix,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris,!windows
package ipv6_test
import (
"fmt"
"runtime"
)
func supportsIPv6MulticastDeliveryOnLoopback() (string, bool) {
return fmt.Sprintf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH), false
}
func protocolNotSupported(err error) bool {
return false
}

View File

@@ -1,24 +1,25 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Copyright 2015 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.
// +build darwin dragonfly freebsd netbsd openbsd
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
package nettest
package ipv6_test
import (
"fmt"
"runtime"
)
func supportsIPv6MulticastDeliveryOnLoopback() bool {
func supportsIPv6MulticastDeliveryOnLoopback() (string, bool) {
switch runtime.GOOS {
case "freebsd":
// See http://www.freebsd.org/cgi/query-pr.cgi?pr=180065.
// Even after the fix, it looks like the latest
// kernels don't deliver link-local scoped multicast
// packets correctly.
return false
return fmt.Sprintf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH), false
default:
return true
return "", true
}
}

View File

@@ -0,0 +1,9 @@
// Copyright 2015 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 ipv6_test
func supportsIPv6MulticastDeliveryOnLoopback() (string, bool) {
return "", true
}

View File

@@ -10,8 +10,8 @@ import (
"runtime"
"testing"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
)
var icmpStringTests = []struct {
@@ -64,11 +64,11 @@ func TestSetICMPFilter(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
c, err := net.ListenPacket("ip6:ipv6-icmp", "::1")

View File

@@ -14,8 +14,8 @@ import (
"golang.org/x/net/icmp"
"golang.org/x/net/internal/iana"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
)
var packetConnReadWriteMulticastUDPTests = []struct {
@@ -32,14 +32,14 @@ func TestPacketConnReadWriteMulticastUDP(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
if !nettest.SupportsIPv6MulticastDeliveryOnLoopback() {
t.Skipf("multicast delivery doesn't work correctly on %s", runtime.GOOS)
if m, ok := supportsIPv6MulticastDeliveryOnLoopback(); !ok {
t.Skip(m)
}
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if ifi == nil {
ifi, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
@@ -94,7 +94,7 @@ func TestPacketConnReadWriteMulticastUDP(t *testing.T) {
for i, toggle := range []bool{true, false, true} {
if err := p.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Logf("not supported on %s", runtime.GOOS)
continue
}
@@ -132,17 +132,17 @@ func TestPacketConnReadWriteMulticastICMP(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
if !nettest.SupportsIPv6MulticastDeliveryOnLoopback() {
t.Skipf("multicast delivery doesn't work correctly on %s", runtime.GOOS)
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
if m, ok := supportsIPv6MulticastDeliveryOnLoopback(); !ok {
t.Skip(m)
}
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if ifi == nil {
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
ifi, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
@@ -229,7 +229,7 @@ func TestPacketConnReadWriteMulticastICMP(t *testing.T) {
t.Fatal(err)
}
if err := p.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Logf("not supported on %s", runtime.GOOS)
continue
}

View File

@@ -9,8 +9,8 @@ import (
"runtime"
"testing"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
)
var udpMultipleGroupListenerTests = []net.Addr{
@@ -24,7 +24,7 @@ func TestUDPSinglePacketConnWithMultipleGroupListeners(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
@@ -43,7 +43,7 @@ func TestUDPSinglePacketConnWithMultipleGroupListeners(t *testing.T) {
t.Fatal(err)
}
for i, ifi := range ift {
if _, ok := nettest.IsMulticastCapable("ip6", &ifi); !ok {
if _, err := nettest.MulticastSource("ip6", &ifi); err != nil {
continue
}
if err := p.JoinGroup(&ifi, gaddr); err != nil {
@@ -64,7 +64,7 @@ func TestUDPMultiplePacketConnWithMultipleGroupListeners(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
@@ -94,7 +94,7 @@ func TestUDPMultiplePacketConnWithMultipleGroupListeners(t *testing.T) {
t.Fatal(err)
}
for i, ifi := range ift {
if _, ok := nettest.IsMulticastCapable("ip6", &ifi); !ok {
if _, err := nettest.MulticastSource("ip6", &ifi); err != nil {
continue
}
for _, p := range ps {
@@ -119,7 +119,7 @@ func TestUDPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
@@ -136,8 +136,8 @@ func TestUDPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
}
port := "0"
for i, ifi := range ift {
ip, ok := nettest.IsMulticastCapable("ip6", &ifi)
if !ok {
ip, err := nettest.MulticastSource("ip6", &ifi)
if err != nil {
continue
}
c, err := net.ListenPacket("udp6", net.JoinHostPort(ip.String()+"%"+ifi.Name, port)) // unicast address with non-reusable port
@@ -175,11 +175,11 @@ func TestIPSinglePacketConnWithSingleGroupListener(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
c, err := net.ListenPacket("ip6:ipv6-icmp", "::") // wildcard address
@@ -197,7 +197,7 @@ func TestIPSinglePacketConnWithSingleGroupListener(t *testing.T) {
t.Fatal(err)
}
for i, ifi := range ift {
if _, ok := nettest.IsMulticastCapable("ip6", &ifi); !ok {
if _, err := nettest.MulticastSource("ip6", &ifi); err != nil {
continue
}
if err := p.JoinGroup(&ifi, &gaddr); err != nil {
@@ -219,11 +219,11 @@ func TestIPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
gaddr := net.IPAddr{IP: net.ParseIP("ff02::114")} // see RFC 4727
@@ -238,8 +238,8 @@ func TestIPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
t.Fatal(err)
}
for i, ifi := range ift {
ip, ok := nettest.IsMulticastCapable("ip6", &ifi)
if !ok {
ip, err := nettest.MulticastSource("ip6", &ifi)
if err != nil {
continue
}
c, err := net.ListenPacket("ip6:ipv6-icmp", ip.String()+"%"+ifi.Name) // unicast address

View File

@@ -9,8 +9,8 @@ import (
"runtime"
"testing"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
)
var packetConnMulticastSocketOptionTests = []struct {
@@ -29,18 +29,18 @@ func TestPacketConnMulticastSocketOptions(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if ifi == nil {
ifi, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
if err != nil {
t.Skipf("not available on %s", runtime.GOOS)
}
m, ok := nettest.SupportsRawIPSocket()
ok := nettest.SupportsRawSocket()
for _, tt := range packetConnMulticastSocketOptionTests {
if tt.net == "ip6" && !ok {
t.Log(m)
t.Logf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
continue
}
c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)

View File

@@ -14,8 +14,8 @@ import (
"testing"
"golang.org/x/net/internal/iana"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
)
func BenchmarkReadWriteUnicast(b *testing.B) {
@@ -53,7 +53,7 @@ func BenchmarkReadWriteUnicast(b *testing.B) {
TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
HopLimit: 1,
}
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
if ifi != nil {
cm.IfIndex = ifi.Index
}
@@ -91,7 +91,8 @@ func BenchmarkPacketConnReadWriteUnicast(b *testing.B) {
HopLimit: 1,
Src: net.IPv6loopback,
}
if ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback); ifi != nil {
ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
if ifi != nil {
cm.IfIndex = ifi.Index
}
@@ -221,7 +222,7 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
@@ -234,12 +235,12 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
defer p.Close()
dst := c.LocalAddr()
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
wb := []byte("HELLO-R-U-THERE")
if err := p.SetControlMessage(cf, true); err != nil { // probe before test
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Skipf("not supported on %s", runtime.GOOS)
}
t.Fatal(err)
@@ -357,11 +358,11 @@ func TestPacketConnConcurrentReadWriteUnicast(t *testing.T) {
func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv6.PacketConn, data []byte, dst net.Addr, batch bool) {
t.Helper()
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
if err := p.SetControlMessage(cf, true); err != nil { // probe before test
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Skipf("not supported on %s", runtime.GOOS)
}
t.Fatal(err)

View File

@@ -11,18 +11,16 @@ import (
"testing"
"golang.org/x/net/internal/iana"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
)
var supportsIPv6 bool = nettest.SupportsIPv6()
func TestConnInitiatorPathMTU(t *testing.T) {
switch runtime.GOOS {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
@@ -60,7 +58,7 @@ func TestConnResponderPathMTU(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
@@ -98,11 +96,11 @@ func TestPacketConnChecksum(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
c, err := net.ListenPacket(fmt.Sprintf("ip6:%d", iana.ProtocolOSPFIGP), "::") // OSPF for IPv6

View File

@@ -14,8 +14,8 @@ import (
"golang.org/x/net/icmp"
"golang.org/x/net/internal/iana"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
)
func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
@@ -23,7 +23,7 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
@@ -41,7 +41,7 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
Src: net.IPv6loopback,
}
cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
if ifi != nil {
cm.IfIndex = ifi.Index
}
@@ -49,7 +49,7 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
for i, toggle := range []bool{true, false, true} {
if err := p.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Logf("not supported on %s", runtime.GOOS)
continue
}
@@ -81,11 +81,11 @@ func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
if m, ok := nettest.SupportsRawIPSocket(); !ok {
t.Skip(m)
if !nettest.SupportsRawSocket() {
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
c, err := net.ListenPacket("ip6:ipv6-icmp", "::1")
@@ -107,7 +107,7 @@ func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
Src: net.IPv6loopback,
}
cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
if ifi != nil {
cm.IfIndex = ifi.Index
}
@@ -147,7 +147,7 @@ func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
t.Fatal(err)
}
if err := p.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
if protocolNotSupported(err) {
t.Logf("not supported on %s", runtime.GOOS)
continue
}

View File

@@ -10,8 +10,8 @@ import (
"testing"
"golang.org/x/net/internal/iana"
"golang.org/x/net/internal/nettest"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
)
func TestConnUnicastSocketOptions(t *testing.T) {
@@ -19,7 +19,7 @@ func TestConnUnicastSocketOptions(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
@@ -64,14 +64,14 @@ func TestPacketConnUnicastSocketOptions(t *testing.T) {
case "aix", "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !supportsIPv6 {
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
}
m, ok := nettest.SupportsRawIPSocket()
ok := nettest.SupportsRawSocket()
for _, tt := range packetConnUnicastSocketOptionTests {
if tt.net == "ip6" && !ok {
t.Log(m)
t.Logf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
continue
}
c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)

View File

@@ -0,0 +1,11 @@
// Copyright 2014 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.
// +build !aix,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris,!windows
package netutil
func maxOpenFiles() int {
return defaultMaxOpenFiles
}

View File

@@ -4,14 +4,9 @@
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
package nettest
package netutil
import (
"fmt"
"os"
"runtime"
"syscall"
)
import "syscall"
func maxOpenFiles() int {
var rlim syscall.Rlimit
@@ -20,10 +15,3 @@ func maxOpenFiles() int {
}
return int(rlim.Cur)
}
func supportsRawIPSocket() (string, bool) {
if os.Getuid() != 0 {
return fmt.Sprintf("must be root on %s", runtime.GOOS), false
}
return "", true
}

View File

@@ -0,0 +1,9 @@
// Copyright 2015 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 netutil
func maxOpenFiles() int {
return 4 * defaultMaxOpenFiles /* actually it's 16581375 */
}

View File

@@ -15,13 +15,13 @@ import (
"sync/atomic"
"testing"
"time"
"golang.org/x/net/internal/nettest"
)
const defaultMaxOpenFiles = 256
func TestLimitListener(t *testing.T) {
const max = 5
attempts := (nettest.MaxOpenFiles() - max) / 2
attempts := (maxOpenFiles() - max) / 2
if attempts > 256 { // maximum length of accept queue is 128 by default
attempts = 256
}