Source file src/net/platform_test.go

Documentation: net

     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.
     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  // testableNetwork reports whether network is testable on the current
    18  // platform configuration.
    19  func testableNetwork(network string) bool {
    20  	ss := strings.Split(network, ":")
    21  	switch ss[0] {
    22  	case "ip+nopriv":
    23  		switch runtime.GOOS {
    24  		case "nacl":
    25  			return false
    26  		}
    27  	case "ip", "ip4", "ip6":
    28  		switch runtime.GOOS {
    29  		case "nacl", "plan9":
    30  			return false
    31  		default:
    32  			if os.Getuid() != 0 {
    33  				return false
    34  			}
    35  		}
    36  	case "unix", "unixgram":
    37  		switch runtime.GOOS {
    38  		case "android", "nacl", "plan9", "windows":
    39  			return false
    40  		case "aix":
    41  			// Unix network isn't properly working on AIX 7.2 with Technical Level < 2
    42  			out, err := exec.Command("oslevel", "-s").Output()
    43  			if err != nil {
    44  				return false
    45  			}
    46  			if tl, err := strconv.Atoi(string(out[5:7])); err != nil || tl < 2 {
    47  				return false
    48  			}
    49  			return true
    50  		}
    51  		// iOS does not support unix, unixgram.
    52  		if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
    53  			return false
    54  		}
    55  	case "unixpacket":
    56  		switch runtime.GOOS {
    57  		case "aix", "android", "darwin", "nacl", "plan9", "windows":
    58  			return false
    59  		case "netbsd":
    60  			// It passes on amd64 at least. 386 fails (Issue 22927). arm is unknown.
    61  			if runtime.GOARCH == "386" {
    62  				return false
    63  			}
    64  		}
    65  	}
    66  	switch ss[0] {
    67  	case "tcp4", "udp4", "ip4":
    68  		if !supportsIPv4() {
    69  			return false
    70  		}
    71  	case "tcp6", "udp6", "ip6":
    72  		if !supportsIPv6() {
    73  			return false
    74  		}
    75  	}
    76  	return true
    77  }
    78  
    79  // testableAddress reports whether address of network is testable on
    80  // the current platform configuration.
    81  func testableAddress(network, address string) bool {
    82  	switch ss := strings.Split(network, ":"); ss[0] {
    83  	case "unix", "unixgram", "unixpacket":
    84  		// Abstract unix domain sockets, a Linux-ism.
    85  		if address[0] == '@' && runtime.GOOS != "linux" {
    86  			return false
    87  		}
    88  	}
    89  	return true
    90  }
    91  
    92  // testableListenArgs reports whether arguments are testable on the
    93  // current platform configuration.
    94  func testableListenArgs(network, address, client string) bool {
    95  	if !testableNetwork(network) || !testableAddress(network, address) {
    96  		return false
    97  	}
    98  
    99  	var err error
   100  	var addr Addr
   101  	switch ss := strings.Split(network, ":"); ss[0] {
   102  	case "tcp", "tcp4", "tcp6":
   103  		addr, err = ResolveTCPAddr("tcp", address)
   104  	case "udp", "udp4", "udp6":
   105  		addr, err = ResolveUDPAddr("udp", address)
   106  	case "ip", "ip4", "ip6":
   107  		addr, err = ResolveIPAddr("ip", address)
   108  	default:
   109  		return true
   110  	}
   111  	if err != nil {
   112  		return false
   113  	}
   114  	var ip IP
   115  	var wildcard bool
   116  	switch addr := addr.(type) {
   117  	case *TCPAddr:
   118  		ip = addr.IP
   119  		wildcard = addr.isWildcard()
   120  	case *UDPAddr:
   121  		ip = addr.IP
   122  		wildcard = addr.isWildcard()
   123  	case *IPAddr:
   124  		ip = addr.IP
   125  		wildcard = addr.isWildcard()
   126  	}
   127  
   128  	// Test wildcard IP addresses.
   129  	if wildcard && !testenv.HasExternalNetwork() {
   130  		return false
   131  	}
   132  
   133  	// Test functionality of IPv4 communication using AF_INET and
   134  	// IPv6 communication using AF_INET6 sockets.
   135  	if !supportsIPv4() && ip.To4() != nil {
   136  		return false
   137  	}
   138  	if !supportsIPv6() && ip.To16() != nil && ip.To4() == nil {
   139  		return false
   140  	}
   141  	cip := ParseIP(client)
   142  	if cip != nil {
   143  		if !supportsIPv4() && cip.To4() != nil {
   144  			return false
   145  		}
   146  		if !supportsIPv6() && cip.To16() != nil && cip.To4() == nil {
   147  			return false
   148  		}
   149  	}
   150  
   151  	// Test functionality of IPv4 communication using AF_INET6
   152  	// sockets.
   153  	if !supportsIPv4map() && supportsIPv4() && (network == "tcp" || network == "udp" || network == "ip") && wildcard {
   154  		// At this point, we prefer IPv4 when ip is nil.
   155  		// See favoriteAddrFamily for further information.
   156  		if ip.To16() != nil && ip.To4() == nil && cip.To4() != nil { // a pair of IPv6 server and IPv4 client
   157  			return false
   158  		}
   159  		if (ip.To4() != nil || ip == nil) && cip.To16() != nil && cip.To4() == nil { // a pair of IPv4 server and IPv6 client
   160  			return false
   161  		}
   162  	}
   163  
   164  	return true
   165  }
   166  
   167  func condFatalf(t *testing.T, network string, format string, args ...interface{}) {
   168  	t.Helper()
   169  	// A few APIs like File and Read/WriteMsg{UDP,IP} are not
   170  	// fully implemented yet on Plan 9 and Windows.
   171  	switch runtime.GOOS {
   172  	case "windows":
   173  		if network == "file+net" {
   174  			t.Logf(format, args...)
   175  			return
   176  		}
   177  	case "plan9":
   178  		t.Logf(format, args...)
   179  		return
   180  	}
   181  	t.Fatalf(format, args...)
   182  }
   183  

View as plain text