libgo: update to Go 1.11
[official-gcc.git] / libgo / go / net / platform_test.go
blob8e7d9151dee761db4ce0407e9bed6af5e7ecbecf
1 // Copyright 2015 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 net
7 import (
8 "internal/testenv"
9 "os"
10 "runtime"
11 "strings"
12 "testing"
15 // testableNetwork reports whether network is testable on the current
16 // platform configuration.
17 func testableNetwork(network string) bool {
18 ss := strings.Split(network, ":")
19 switch ss[0] {
20 case "ip+nopriv":
21 switch runtime.GOOS {
22 case "nacl":
23 return false
25 case "ip", "ip4", "ip6":
26 switch runtime.GOOS {
27 case "nacl", "plan9":
28 return false
29 default:
30 if os.Getuid() != 0 {
31 return false
34 case "unix", "unixgram":
35 switch runtime.GOOS {
36 case "android", "nacl", "plan9", "windows":
37 return false
39 // iOS does not support unix, unixgram.
40 if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
41 return false
43 case "unixpacket":
44 switch runtime.GOOS {
45 case "android", "darwin", "nacl", "plan9", "windows":
46 return false
47 case "netbsd":
48 // It passes on amd64 at least. 386 fails (Issue 22927). arm is unknown.
49 if runtime.GOARCH == "386" {
50 return false
54 switch ss[0] {
55 case "tcp4", "udp4", "ip4":
56 if !supportsIPv4() {
57 return false
59 case "tcp6", "udp6", "ip6":
60 if !supportsIPv6() {
61 return false
64 return true
67 // testableAddress reports whether address of network is testable on
68 // the current platform configuration.
69 func testableAddress(network, address string) bool {
70 switch ss := strings.Split(network, ":"); ss[0] {
71 case "unix", "unixgram", "unixpacket":
72 // Abstract unix domain sockets, a Linux-ism.
73 if address[0] == '@' && runtime.GOOS != "linux" {
74 return false
77 return true
80 // testableListenArgs reports whether arguments are testable on the
81 // current platform configuration.
82 func testableListenArgs(network, address, client string) bool {
83 if !testableNetwork(network) || !testableAddress(network, address) {
84 return false
87 var err error
88 var addr Addr
89 switch ss := strings.Split(network, ":"); ss[0] {
90 case "tcp", "tcp4", "tcp6":
91 addr, err = ResolveTCPAddr("tcp", address)
92 case "udp", "udp4", "udp6":
93 addr, err = ResolveUDPAddr("udp", address)
94 case "ip", "ip4", "ip6":
95 addr, err = ResolveIPAddr("ip", address)
96 default:
97 return true
99 if err != nil {
100 return false
102 var ip IP
103 var wildcard bool
104 switch addr := addr.(type) {
105 case *TCPAddr:
106 ip = addr.IP
107 wildcard = addr.isWildcard()
108 case *UDPAddr:
109 ip = addr.IP
110 wildcard = addr.isWildcard()
111 case *IPAddr:
112 ip = addr.IP
113 wildcard = addr.isWildcard()
116 // Test wildcard IP addresses.
117 if wildcard && !testenv.HasExternalNetwork() {
118 return false
121 // Test functionality of IPv4 communication using AF_INET and
122 // IPv6 communication using AF_INET6 sockets.
123 if !supportsIPv4() && ip.To4() != nil {
124 return false
126 if !supportsIPv6() && ip.To16() != nil && ip.To4() == nil {
127 return false
129 cip := ParseIP(client)
130 if cip != nil {
131 if !supportsIPv4() && cip.To4() != nil {
132 return false
134 if !supportsIPv6() && cip.To16() != nil && cip.To4() == nil {
135 return false
139 // Test functionality of IPv4 communication using AF_INET6
140 // sockets.
141 if !supportsIPv4map() && supportsIPv4() && (network == "tcp" || network == "udp" || network == "ip") && wildcard {
142 // At this point, we prefer IPv4 when ip is nil.
143 // See favoriteAddrFamily for further information.
144 if ip.To16() != nil && ip.To4() == nil && cip.To4() != nil { // a pair of IPv6 server and IPv4 client
145 return false
147 if (ip.To4() != nil || ip == nil) && cip.To16() != nil && cip.To4() == nil { // a pair of IPv4 server and IPv6 client
148 return false
152 return true
155 func condFatalf(t *testing.T, network string, format string, args ...interface{}) {
156 t.Helper()
157 // A few APIs like File and Read/WriteMsg{UDP,IP} are not
158 // fully implemented yet on Plan 9 and Windows.
159 switch runtime.GOOS {
160 case "windows":
161 if network == "file+net" {
162 t.Logf(format, args...)
163 return
165 case "plan9":
166 t.Logf(format, args...)
167 return
169 t.Fatalf(format, args...)