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