Source file
src/net/platform_test.go
1
2
3
4
5 package net
6
7 import (
8 "internal/testenv"
9 "os"
10 "runtime"
11 "strings"
12 "testing"
13 )
14
15
16
17 func testableNetwork(network string) bool {
18 net, _, _ := strings.Cut(network, ":")
19 switch net {
20 case "ip+nopriv":
21 case "ip", "ip4", "ip6":
22 switch runtime.GOOS {
23 case "plan9":
24 return false
25 default:
26 if os.Getuid() != 0 {
27 return false
28 }
29 }
30 case "unixgram":
31 switch runtime.GOOS {
32 case "windows":
33 return false
34 default:
35 return supportsUnixSocket()
36 }
37 case "unix":
38 return supportsUnixSocket()
39 case "unixpacket":
40 switch runtime.GOOS {
41 case "aix", "android", "darwin", "ios", "plan9", "windows":
42 return false
43 }
44 }
45 switch net {
46 case "tcp4", "udp4", "ip4":
47 if !supportsIPv4() {
48 return false
49 }
50 case "tcp6", "udp6", "ip6":
51 if !supportsIPv6() {
52 return false
53 }
54 }
55 return true
56 }
57
58
59
60 func testableAddress(network, address string) bool {
61 switch net, _, _ := strings.Cut(network, ":"); net {
62 case "unix", "unixgram", "unixpacket":
63
64 if address[0] == '@' && runtime.GOOS != "linux" {
65 return false
66 }
67 }
68 return true
69 }
70
71
72
73 func testableListenArgs(network, address, client string) bool {
74 if !testableNetwork(network) || !testableAddress(network, address) {
75 return false
76 }
77
78 var err error
79 var addr Addr
80 switch net, _, _ := strings.Cut(network, ":"); net {
81 case "tcp", "tcp4", "tcp6":
82 addr, err = ResolveTCPAddr("tcp", address)
83 case "udp", "udp4", "udp6":
84 addr, err = ResolveUDPAddr("udp", address)
85 case "ip", "ip4", "ip6":
86 addr, err = ResolveIPAddr("ip", address)
87 default:
88 return true
89 }
90 if err != nil {
91 return false
92 }
93 var ip IP
94 var wildcard bool
95 switch addr := addr.(type) {
96 case *TCPAddr:
97 ip = addr.IP
98 wildcard = addr.isWildcard()
99 case *UDPAddr:
100 ip = addr.IP
101 wildcard = addr.isWildcard()
102 case *IPAddr:
103 ip = addr.IP
104 wildcard = addr.isWildcard()
105 }
106
107
108 if wildcard && !testenv.HasExternalNetwork() {
109 return false
110 }
111
112
113
114 if !supportsIPv4() && ip.To4() != nil {
115 return false
116 }
117 if !supportsIPv6() && ip.To16() != nil && ip.To4() == nil {
118 return false
119 }
120 cip := ParseIP(client)
121 if cip != nil {
122 if !supportsIPv4() && cip.To4() != nil {
123 return false
124 }
125 if !supportsIPv6() && cip.To16() != nil && cip.To4() == nil {
126 return false
127 }
128 }
129
130
131
132 if !supportsIPv4map() && supportsIPv4() && (network == "tcp" || network == "udp" || network == "ip") && wildcard {
133
134
135 if ip.To16() != nil && ip.To4() == nil && cip.To4() != nil {
136 return false
137 }
138 if (ip.To4() != nil || ip == nil) && cip.To16() != nil && cip.To4() == nil {
139 return false
140 }
141 }
142
143 return true
144 }
145
146 func condFatalf(t *testing.T, network string, format string, args ...any) {
147 t.Helper()
148
149
150 switch runtime.GOOS {
151 case "windows", "js", "wasip1":
152 if network == "file+net" {
153 t.Logf(format, args...)
154 return
155 }
156 case "plan9":
157 t.Logf(format, args...)
158 return
159 }
160 t.Fatalf(format, args...)
161 }
162
View as plain text