2017-04-10 11:32:00 +00:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2017-10-07 00:16:47 +00:00
|
|
|
"internal/testenv"
|
2017-04-10 11:32:00 +00:00
|
|
|
"os"
|
2019-06-02 15:48:37 +00:00
|
|
|
"os/exec"
|
2017-04-10 11:32:00 +00:00
|
|
|
"runtime"
|
2019-06-02 15:48:37 +00:00
|
|
|
"strconv"
|
2017-04-10 11:32:00 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// testableNetwork reports whether network is testable on the current
|
|
|
|
// platform configuration.
|
|
|
|
func testableNetwork(network string) bool {
|
|
|
|
ss := strings.Split(network, ":")
|
|
|
|
switch ss[0] {
|
|
|
|
case "ip+nopriv":
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "nacl":
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "nacl", "plan9":
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "unix", "unixgram":
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "android", "nacl", "plan9", "windows":
|
|
|
|
return false
|
2019-06-02 15:48:37 +00:00
|
|
|
case "aix":
|
|
|
|
// Unix network isn't properly working on AIX 7.2 with Technical Level < 2
|
|
|
|
out, err := exec.Command("oslevel", "-s").Output()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if tl, err := strconv.Atoi(string(out[5:7])); err != nil || tl < 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
|
|
|
// iOS does not support unix, unixgram.
|
|
|
|
if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case "unixpacket":
|
|
|
|
switch runtime.GOOS {
|
2019-06-02 15:48:37 +00:00
|
|
|
case "aix", "android", "darwin", "nacl", "plan9", "windows":
|
2017-04-10 11:32:00 +00:00
|
|
|
return false
|
2018-12-28 15:30:48 +00:00
|
|
|
case "netbsd":
|
|
|
|
// It passes on amd64 at least. 386 fails (Issue 22927). arm is unknown.
|
|
|
|
if runtime.GOARCH == "386" {
|
|
|
|
return false
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
switch ss[0] {
|
|
|
|
case "tcp4", "udp4", "ip4":
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4() {
|
2017-04-10 11:32:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
case "tcp6", "udp6", "ip6":
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv6() {
|
2017-04-10 11:32:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// testableAddress reports whether address of network is testable on
|
|
|
|
// the current platform configuration.
|
|
|
|
func testableAddress(network, address string) bool {
|
|
|
|
switch ss := strings.Split(network, ":"); ss[0] {
|
|
|
|
case "unix", "unixgram", "unixpacket":
|
|
|
|
// Abstract unix domain sockets, a Linux-ism.
|
|
|
|
if address[0] == '@' && runtime.GOOS != "linux" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// testableListenArgs reports whether arguments are testable on the
|
|
|
|
// current platform configuration.
|
|
|
|
func testableListenArgs(network, address, client string) bool {
|
|
|
|
if !testableNetwork(network) || !testableAddress(network, address) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var addr Addr
|
|
|
|
switch ss := strings.Split(network, ":"); ss[0] {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
addr, err = ResolveTCPAddr("tcp", address)
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
addr, err = ResolveUDPAddr("udp", address)
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
addr, err = ResolveIPAddr("ip", address)
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var ip IP
|
|
|
|
var wildcard bool
|
|
|
|
switch addr := addr.(type) {
|
|
|
|
case *TCPAddr:
|
|
|
|
ip = addr.IP
|
|
|
|
wildcard = addr.isWildcard()
|
|
|
|
case *UDPAddr:
|
|
|
|
ip = addr.IP
|
|
|
|
wildcard = addr.isWildcard()
|
|
|
|
case *IPAddr:
|
|
|
|
ip = addr.IP
|
|
|
|
wildcard = addr.isWildcard()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test wildcard IP addresses.
|
2017-10-07 00:16:47 +00:00
|
|
|
if wildcard && !testenv.HasExternalNetwork() {
|
2017-04-10 11:32:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test functionality of IPv4 communication using AF_INET and
|
|
|
|
// IPv6 communication using AF_INET6 sockets.
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4() && ip.To4() != nil {
|
2017-04-10 11:32:00 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv6() && ip.To16() != nil && ip.To4() == nil {
|
2017-04-10 11:32:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
cip := ParseIP(client)
|
|
|
|
if cip != nil {
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4() && cip.To4() != nil {
|
2017-04-10 11:32:00 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv6() && cip.To16() != nil && cip.To4() == nil {
|
2017-04-10 11:32:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test functionality of IPv4 communication using AF_INET6
|
|
|
|
// sockets.
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4map() && supportsIPv4() && (network == "tcp" || network == "udp" || network == "ip") && wildcard {
|
2017-04-10 11:32:00 +00:00
|
|
|
// At this point, we prefer IPv4 when ip is nil.
|
|
|
|
// See favoriteAddrFamily for further information.
|
|
|
|
if ip.To16() != nil && ip.To4() == nil && cip.To4() != nil { // a pair of IPv6 server and IPv4 client
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (ip.To4() != nil || ip == nil) && cip.To16() != nil && cip.To4() == nil { // a pair of IPv4 server and IPv6 client
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
func condFatalf(t *testing.T, network string, format string, args ...interface{}) {
|
|
|
|
t.Helper()
|
|
|
|
// A few APIs like File and Read/WriteMsg{UDP,IP} are not
|
|
|
|
// fully implemented yet on Plan 9 and Windows.
|
2017-04-10 11:32:00 +00:00
|
|
|
switch runtime.GOOS {
|
2018-12-28 15:30:48 +00:00
|
|
|
case "windows":
|
|
|
|
if network == "file+net" {
|
|
|
|
t.Logf(format, args...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "plan9":
|
|
|
|
t.Logf(format, args...)
|
|
|
|
return
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
2018-12-28 15:30:48 +00:00
|
|
|
t.Fatalf(format, args...)
|
|
|
|
}
|