46d2fccab588c032e471f65c0ffef3bad8901837
[platform/core/system/edge-orchestration.git] / vendor / github.com / miekg / dns / vendor / golang.org / x / net / internal / nettest / stack.go
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.
4
5 // Package nettest provides utilities for network testing.
6 package nettest // import "golang.org/x/net/internal/nettest"
7
8 import (
9         "fmt"
10         "io/ioutil"
11         "net"
12         "os"
13         "runtime"
14 )
15
16 var (
17         supportsIPv4 bool
18         supportsIPv6 bool
19 )
20
21 func init() {
22         if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
23                 ln.Close()
24                 supportsIPv4 = true
25         }
26         if ln, err := net.Listen("tcp6", "[::1]:0"); err == nil {
27                 ln.Close()
28                 supportsIPv6 = true
29         }
30 }
31
32 // SupportsIPv4 reports whether the platform supports IPv4 networking
33 // functionality.
34 func SupportsIPv4() bool { return supportsIPv4 }
35
36 // SupportsIPv6 reports whether the platform supports IPv6 networking
37 // functionality.
38 func SupportsIPv6() bool { return supportsIPv6 }
39
40 // SupportsRawIPSocket reports whether the platform supports raw IP
41 // sockets.
42 func SupportsRawIPSocket() (string, bool) {
43         return supportsRawIPSocket()
44 }
45
46 // SupportsIPv6MulticastDeliveryOnLoopback reports whether the
47 // platform supports IPv6 multicast packet delivery on software
48 // loopback interface.
49 func SupportsIPv6MulticastDeliveryOnLoopback() bool {
50         return supportsIPv6MulticastDeliveryOnLoopback()
51 }
52
53 // ProtocolNotSupported reports whether err is a protocol not
54 // supported error.
55 func ProtocolNotSupported(err error) bool {
56         return protocolNotSupported(err)
57 }
58
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
69                 }
70                 if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
71                         return false
72                 }
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
81                         }
82                 }
83         }
84         return true
85 }
86
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
96                         }
97                 }
98                 if supportsIPv6 {
99                         return net.Listen("tcp6", "[::1]:0")
100                 }
101         case "tcp4":
102                 if supportsIPv4 {
103                         return net.Listen("tcp4", "127.0.0.1:0")
104                 }
105         case "tcp6":
106                 if supportsIPv6 {
107                         return net.Listen("tcp6", "[::1]:0")
108                 }
109         case "unix", "unixpacket":
110                 return net.Listen(network, localPath())
111         }
112         return nil, fmt.Errorf("%s is not supported", network)
113 }
114
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
124                         }
125                 }
126                 if supportsIPv6 {
127                         return net.ListenPacket("udp6", "[::1]:0")
128                 }
129         case "udp4":
130                 if supportsIPv4 {
131                         return net.ListenPacket("udp4", "127.0.0.1:0")
132                 }
133         case "udp6":
134                 if supportsIPv6 {
135                         return net.ListenPacket("udp6", "[::1]:0")
136                 }
137         case "unixgram":
138                 return net.ListenPacket(network, localPath())
139         }
140         return nil, fmt.Errorf("%s is not supported", network)
141 }
142
143 func localPath() string {
144         f, err := ioutil.TempFile("", "nettest")
145         if err != nil {
146                 panic(err)
147         }
148         path := f.Name()
149         f.Close()
150         os.Remove(path)
151         return path
152 }