libgo: update to Go 1.11
[official-gcc.git] / libgo / go / golang_org / x / net / internal / nettest / stack.go
blob46d2fccab588c032e471f65c0ffef3bad8901837
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"
8 import (
9 "fmt"
10 "io/ioutil"
11 "net"
12 "os"
13 "runtime"
16 var (
17 supportsIPv4 bool
18 supportsIPv6 bool
21 func init() {
22 if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
23 ln.Close()
24 supportsIPv4 = true
26 if ln, err := net.Listen("tcp6", "[::1]:0"); err == nil {
27 ln.Close()
28 supportsIPv6 = true
32 // SupportsIPv4 reports whether the platform supports IPv4 networking
33 // functionality.
34 func SupportsIPv4() bool { return supportsIPv4 }
36 // SupportsIPv6 reports whether the platform supports IPv6 networking
37 // functionality.
38 func SupportsIPv6() bool { return supportsIPv6 }
40 // SupportsRawIPSocket reports whether the platform supports raw IP
41 // sockets.
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
54 // supported error.
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.
64 switch network {
65 case "unix", "unixgram":
66 switch runtime.GOOS {
67 case "android", "js", "nacl", "plan9", "windows":
68 return false
70 if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
71 return false
73 case "unixpacket":
74 switch runtime.GOOS {
75 case "android", "darwin", "freebsd", "js", "nacl", "plan9", "windows":
76 return false
77 case "netbsd":
78 // It passes on amd64 at least. 386 fails (Issue 22927). arm is unknown.
79 if runtime.GOARCH == "386" {
80 return false
84 return true
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) {
91 switch network {
92 case "tcp":
93 if supportsIPv4 {
94 if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
95 return ln, nil
98 if supportsIPv6 {
99 return net.Listen("tcp6", "[::1]:0")
101 case "tcp4":
102 if supportsIPv4 {
103 return net.Listen("tcp4", "127.0.0.1:0")
105 case "tcp6":
106 if supportsIPv6 {
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) {
119 switch network {
120 case "udp":
121 if supportsIPv4 {
122 if c, err := net.ListenPacket("udp4", "127.0.0.1:0"); err == nil {
123 return c, nil
126 if supportsIPv6 {
127 return net.ListenPacket("udp6", "[::1]:0")
129 case "udp4":
130 if supportsIPv4 {
131 return net.ListenPacket("udp4", "127.0.0.1:0")
133 case "udp6":
134 if supportsIPv6 {
135 return net.ListenPacket("udp6", "[::1]:0")
137 case "unixgram":
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")
145 if err != nil {
146 panic(err)
148 path := f.Name()
149 f.Close()
150 os.Remove(path)
151 return path