Commit d4e13832 authored by Mikio Hara's avatar Mikio Hara

net: improve server and file tests

* Splits into three server tests.
  - TestStreamConnServer for tcp, tcp4, tcp6 and unix networks
  - TestSeqpacketConnServer for unixpacket networks
  - TestDatagramPacketConnServer for udp, udp4, udp6 and unixgram networks
* Adds both PacketConn and Conn test clients to datagram packet conn tests.
* Fixes wildcard listen test cases on dual IP stack platform.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5701066
parent 06b70244
......@@ -7,7 +7,6 @@ package net
import (
"os"
"reflect"
"runtime"
"testing"
)
......@@ -27,7 +26,8 @@ type connFile interface {
}
func testFileListener(t *testing.T, net, laddr string) {
if net == "tcp" {
switch net {
case "tcp", "tcp4", "tcp6":
laddr += ":0" // any available port
}
l, err := Listen(net, laddr)
......@@ -55,15 +55,46 @@ 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: "127.0.0.1"},
{net: "tcp", laddr: "[::ffff:127.0.0.1]"},
{net: "tcp", laddr: "[::1]", ipv6: true},
{net: "tcp4", laddr: ""},
{net: "tcp4", laddr: "0.0.0.0"},
{net: "tcp4", laddr: "[::ffff:0.0.0.0]"},
{net: "tcp4", laddr: "127.0.0.1"},
{net: "tcp4", laddr: "[::ffff:127.0.0.1]"},
{net: "tcp6", laddr: "", ipv6: true},
{net: "tcp6", laddr: "[::]", ipv6: true},
{net: "tcp6", laddr: "[::1]", ipv6: true},
{net: "unix", laddr: "@gotest/net", linux: true},
{net: "unixpacket", laddr: "@gotest/net", linux: true},
}
func TestFileListener(t *testing.T) {
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
return
}
testFileListener(t, "tcp", "127.0.0.1")
testFileListener(t, "tcp", "[::ffff:127.0.0.1]")
if runtime.GOOS == "linux" {
testFileListener(t, "unix", "@gotest/net")
testFileListener(t, "unixpacket", "@gotest/net")
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) {
continue
}
testFileListener(t, tt.net, tt.laddr)
}
}
......@@ -93,9 +124,13 @@ 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("Listen failed: %v", err)
t.Fatalf("ListenPacket failed: %v", err)
}
testFilePacketConn(t, l.(packetConnFile), true)
if err := l.Close(); err != nil {
......@@ -104,6 +139,10 @@ 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)
......@@ -114,17 +153,36 @@ 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: "udp", addr: "127.0.0.1"},
{net: "udp", addr: "[::ffff:127.0.0.1]"},
{net: "udp", addr: "[::1]", ipv6: true},
{net: "udp4", addr: "127.0.0.1"},
{net: "udp4", addr: "[::ffff:127.0.0.1]"},
{net: "udp6", addr: "[::1]", ipv6: true},
{net: "unixgram", addr: "@gotest3/net", linux: true},
}
func TestFilePacketConn(t *testing.T) {
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
return
}
testFilePacketConnListen(t, "udp", "127.0.0.1:0")
testFilePacketConnDial(t, "udp", "127.0.0.1:12345")
testFilePacketConnDial(t, "udp", "[::ffff:127.0.0.1]:12345")
if supportsIPv6 {
testFilePacketConnListen(t, "udp", "[::1]:0")
}
if runtime.GOOS == "linux" {
testFilePacketConnListen(t, "unixgram", "@gotest1/net")
for _, tt := range filePacketConnTests {
if skipServerTest(tt.net, "unixgram", tt.addr, tt.ipv6, false, tt.linux) {
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)
}
}
}
}
This diff is collapsed.
......@@ -11,13 +11,13 @@ import (
"time"
)
func testTimeout(t *testing.T, network, addr string, readFrom bool) {
fd, err := Dial(network, addr)
func testTimeout(t *testing.T, net, addr string, readFrom bool) {
c, err := Dial(net, addr)
if err != nil {
t.Errorf("dial %s %s failed: %v", network, addr, err)
t.Errorf("Dial(%q, %q) failed: %v", net, addr, err)
return
}
defer fd.Close()
defer c.Close()
what := "Read"
if readFrom {
what = "ReadFrom"
......@@ -26,22 +26,22 @@ func testTimeout(t *testing.T, network, addr string, readFrom bool) {
errc := make(chan error, 1)
go func() {
t0 := time.Now()
fd.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
var b [100]byte
var n int
var err1 error
var err error
if readFrom {
n, _, err1 = fd.(PacketConn).ReadFrom(b[0:])
n, _, err = c.(PacketConn).ReadFrom(b[0:])
} else {
n, err1 = fd.Read(b[0:])
n, err = c.Read(b[0:])
}
t1 := time.Now()
if n != 0 || err1 == nil || !err1.(Error).Timeout() {
errc <- fmt.Errorf("fd.%s on %s %s did not return 0, timeout: %v, %v", what, network, addr, n, err1)
if n != 0 || err == nil || !err.(Error).Timeout() {
errc <- fmt.Errorf("%s(%q, %q) did not return 0, timeout: %v, %v", what, net, addr, n, err)
return
}
if dt := t1.Sub(t0); dt < 50*time.Millisecond || !testing.Short() && dt > 250*time.Millisecond {
errc <- fmt.Errorf("fd.%s on %s %s took %s, expected 0.1s", what, network, addr, dt)
errc <- fmt.Errorf("%s(%q, %q) took %s, expected 0.1s", what, net, addr, dt)
return
}
errc <- nil
......@@ -52,7 +52,7 @@ func testTimeout(t *testing.T, network, addr string, readFrom bool) {
t.Error(err)
}
case <-time.After(1 * time.Second):
t.Errorf("%s on %s %s took over 1 second, expected 0.1s", what, network, addr)
t.Errorf("%s(%q, %q) took over 1 second, expected 0.1s", what, net, addr)
}
}
......@@ -60,18 +60,27 @@ func TestTimeoutUDP(t *testing.T) {
if runtime.GOOS == "plan9" {
return
}
testTimeout(t, "udp", "127.0.0.1:53", false)
testTimeout(t, "udp", "127.0.0.1:53", true)
// set up a listener that won't talk back
listening := make(chan string)
done := make(chan int)
go runDatagramPacketConnServer(t, "udp", "127.0.0.1:0", listening, done)
addr := <-listening
testTimeout(t, "udp", addr, false)
testTimeout(t, "udp", addr, true)
<-done
}
func TestTimeoutTCP(t *testing.T) {
if runtime.GOOS == "plan9" {
return
}
// set up a listener that won't talk back
listening := make(chan string)
done := make(chan int)
go runServe(t, "tcp", "127.0.0.1:0", listening, done)
go runStreamConnServer(t, "tcp", "127.0.0.1:0", listening, done)
addr := <-listening
testTimeout(t, "tcp", addr, false)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment