1 // Copyright 2014 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.
5 // Package nettest provides utilities for network testing.
6 package nettest // import "golang.org/x/net/internal/nettest"
22 if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
26 if ln, err := net.Listen("tcp6", "[::1]:0"); err == nil {
32 // SupportsIPv4 reports whether the platform supports IPv4 networking
34 func SupportsIPv4() bool { return supportsIPv4 }
36 // SupportsIPv6 reports whether the platform supports IPv6 networking
38 func SupportsIPv6() bool { return supportsIPv6 }
40 // SupportsRawIPSocket reports whether the platform supports raw IP
42 func SupportsRawIPSocket() (string, bool) {
43 return supportsRawIPSocket()
46 // SupportsIPv6MulticastDeliveryOnLoopback reports whether the
47 // platform supports IPv6 multicast packet delivery on software
48 // loopback interface.
49 func SupportsIPv6MulticastDeliveryOnLoopback() bool {
50 return supportsIPv6MulticastDeliveryOnLoopback()
53 // ProtocolNotSupported reports whether err is a protocol not
55 func ProtocolNotSupported(err error) bool {
56 return protocolNotSupported(err)
59 // TestableNetwork reports whether network is testable on the current
60 // platform configuration.
61 func TestableNetwork(network string) bool {
62 // This is based on logic from standard library's
63 // net/platform_test.go.
65 case "unix", "unixgram":
67 case "android", "js", "nacl", "plan9", "windows":
70 if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
75 case "android", "darwin", "freebsd", "js", "nacl", "plan9", "windows":
78 // It passes on amd64 at least. 386 fails (Issue 22927). arm is unknown.
79 if runtime.GOARCH == "386" {
87 // NewLocalListener returns a listener which listens to a loopback IP
88 // address or local file system path.
89 // Network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
90 func NewLocalListener(network string) (net.Listener, error) {
94 if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
99 return net.Listen("tcp6", "[::1]:0")
103 return net.Listen("tcp4", "127.0.0.1:0")
107 return net.Listen("tcp6", "[::1]:0")
109 case "unix", "unixpacket":
110 return net.Listen(network, localPath())
112 return nil, fmt.Errorf("%s is not supported", network)
115 // NewLocalPacketListener returns a packet listener which listens to a
116 // loopback IP address or local file system path.
117 // Network must be "udp", "udp4", "udp6" or "unixgram".
118 func NewLocalPacketListener(network string) (net.PacketConn, error) {
122 if c, err := net.ListenPacket("udp4", "127.0.0.1:0"); err == nil {
127 return net.ListenPacket("udp6", "[::1]:0")
131 return net.ListenPacket("udp4", "127.0.0.1:0")
135 return net.ListenPacket("udp6", "[::1]:0")
138 return net.ListenPacket(network, localPath())
140 return nil, fmt.Errorf("%s is not supported", network)
143 func localPath() string {
144 f, err := ioutil.TempFile("", "nettest")