net: simplify test helpers

This change consolidates test helpers that test platform capabilities.
testNetwork, testAddress and testListenArgs report whether given
ariguments are testable on the current platform configuration to
mitigate to receive weird test results.

Change-Id: Ie1ed568a1f9cc50f3155945ea01562904bc2c389
Reviewed-on: https://go-review.googlesource.com/8076
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Mikio Hara 2015-03-26 23:26:45 +09:00
parent 71756355dc
commit 7b2b45e544
15 changed files with 327 additions and 324 deletions

View file

@ -27,10 +27,6 @@ type connFile interface {
}
func testFileListener(t *testing.T, net, laddr string) {
switch net {
case "tcp", "tcp4", "tcp6":
laddr += ":0" // any available port
}
l, err := Listen(net, laddr)
if err != nil {
t.Fatalf("Listen failed: %v", err)
@ -59,32 +55,30 @@ func testFileListener(t *testing.T, net, laddr string) {
var fileListenerTests = []struct {
net string
laddr string
ipv6 bool // test with underlying AF_INET6 socket
linux bool // test with abstract unix domain socket, a Linux-ism
}{
{net: "tcp", laddr: ""},
{net: "tcp", laddr: "0.0.0.0"},
{net: "tcp", laddr: "[::ffff:0.0.0.0]"},
{net: "tcp", laddr: "[::]", ipv6: true},
{net: "tcp", laddr: ":0"},
{net: "tcp", laddr: "0.0.0.0:0"},
{net: "tcp", laddr: "[::ffff:0.0.0.0]:0"},
{net: "tcp", laddr: "[::]:0"},
{net: "tcp", laddr: "127.0.0.1"},
{net: "tcp", laddr: "[::ffff:127.0.0.1]"},
{net: "tcp", laddr: "[::1]", ipv6: true},
{net: "tcp", laddr: "127.0.0.1:0"},
{net: "tcp", laddr: "[::ffff:127.0.0.1]:0"},
{net: "tcp", laddr: "[::1]:0"},
{net: "tcp4", laddr: ""},
{net: "tcp4", laddr: "0.0.0.0"},
{net: "tcp4", laddr: "[::ffff:0.0.0.0]"},
{net: "tcp4", laddr: ":0"},
{net: "tcp4", laddr: "0.0.0.0:0"},
{net: "tcp4", laddr: "[::ffff:0.0.0.0]:0"},
{net: "tcp4", laddr: "127.0.0.1"},
{net: "tcp4", laddr: "[::ffff:127.0.0.1]"},
{net: "tcp4", laddr: "127.0.0.1:0"},
{net: "tcp4", laddr: "[::ffff:127.0.0.1]:0"},
{net: "tcp6", laddr: "", ipv6: true},
{net: "tcp6", laddr: "[::]", ipv6: true},
{net: "tcp6", laddr: ":0"},
{net: "tcp6", laddr: "[::]:0"},
{net: "tcp6", laddr: "[::1]", ipv6: true},
{net: "tcp6", laddr: "[::1]:0"},
{net: "unix", laddr: "@gotest/net", linux: true},
{net: "unixpacket", laddr: "@gotest/net", linux: true},
{net: "unix", laddr: "@gotest/net"},
{net: "unixpacket", laddr: "@gotest/net"},
}
func TestFileListener(t *testing.T) {
@ -94,10 +88,8 @@ func TestFileListener(t *testing.T) {
}
for _, tt := range fileListenerTests {
if skipServerTest(tt.net, "unix", tt.laddr, tt.ipv6, false, tt.linux) {
continue
}
if skipServerTest(tt.net, "unixpacket", tt.laddr, tt.ipv6, false, tt.linux) {
if !testableListenArgs(tt.net, tt.laddr, "") {
t.Logf("skipping %s test", tt.net+":"+tt.laddr+"->")
continue
}
testFileListener(t, tt.net, tt.laddr)
@ -130,10 +122,6 @@ func testFilePacketConn(t *testing.T, pcf packetConnFile, listen bool) {
}
func testFilePacketConnListen(t *testing.T, net, laddr string) {
switch net {
case "udp", "udp4", "udp6":
laddr += ":0" // any available port
}
l, err := ListenPacket(net, laddr)
if err != nil {
t.Fatalf("ListenPacket failed: %v", err)
@ -145,10 +133,6 @@ func testFilePacketConnListen(t *testing.T, net, laddr string) {
}
func testFilePacketConnDial(t *testing.T, net, raddr string) {
switch net {
case "udp", "udp4", "udp6":
raddr += ":12345"
}
c, err := Dial(net, raddr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
@ -160,23 +144,21 @@ func testFilePacketConnDial(t *testing.T, net, raddr string) {
}
var filePacketConnTests = []struct {
net string
addr string
ipv6 bool // test with underlying AF_INET6 socket
linux bool // test with abstract unix domain socket, a Linux-ism
net string
addr string
}{
{net: "udp", addr: "127.0.0.1"},
{net: "udp", addr: "[::ffff:127.0.0.1]"},
{net: "udp", addr: "[::1]", ipv6: true},
{net: "udp", addr: "127.0.0.1:0"},
{net: "udp", addr: "[::ffff:127.0.0.1]:0"},
{net: "udp", addr: "[::1]:0"},
{net: "udp4", addr: "127.0.0.1"},
{net: "udp4", addr: "[::ffff:127.0.0.1]"},
{net: "udp4", addr: "127.0.0.1:0"},
{net: "udp4", addr: "[::ffff:127.0.0.1]:0"},
{net: "udp6", addr: "[::1]", ipv6: true},
{net: "udp6", addr: "[::1]:0"},
{net: "ip4:icmp", addr: "127.0.0.1"},
{net: "unixgram", addr: "@gotest3/net", linux: true},
{net: "unixgram", addr: "@gotest3/net"},
}
func TestFilePacketConn(t *testing.T) {
@ -186,7 +168,8 @@ func TestFilePacketConn(t *testing.T) {
}
for _, tt := range filePacketConnTests {
if skipServerTest(tt.net, "unixgram", tt.addr, tt.ipv6, false, tt.linux) {
if !testableListenArgs(tt.net, tt.addr, "") {
t.Logf("skipping %s test", tt.net+":"+tt.addr+"->")
continue
}
if os.Getuid() != 0 && tt.net == "ip4:icmp" {
@ -194,12 +177,16 @@ func TestFilePacketConn(t *testing.T) {
continue
}
testFilePacketConnListen(t, tt.net, tt.addr)
switch tt.addr {
case "", "0.0.0.0", "[::ffff:0.0.0.0]", "[::]":
default:
if tt.net != "unixgram" {
testFilePacketConnDial(t, tt.net, tt.addr)
switch tt.net {
case "udp", "udp4", "udp6":
host, _, err := SplitHostPort(tt.addr)
if err != nil {
t.Error(err)
continue
}
testFilePacketConnDial(t, tt.net, JoinHostPort(host, "12345"))
case "ip4:icmp":
testFilePacketConnDial(t, tt.net, tt.addr)
}
}
}